From 38f6ebd76b0b9d924f507ab723eb5418a4185b40 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 1 Feb 2012 23:35:01 +0000 Subject: [PATCH] Added unit tests for the NasaPoly1 class --- SConstruct | 5 ++ ext/SConscript | 5 +- test/SConscript | 28 ++++++++++ test/thermo/nasapoly.cpp | 118 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 test/SConscript create mode 100644 test/thermo/nasapoly.cpp diff --git a/SConstruct b/SConstruct index 5125e03e0..3c8d7567c 100644 --- a/SConstruct +++ b/SConstruct @@ -962,3 +962,8 @@ if 'msi' in COMMAND_LINE_TARGETS: ### Tests ### if any(target.startswith('test') for target in COMMAND_LINE_TARGETS): SConscript('test_problems/SConscript') + +# Tests written using the gtest framework +if any(target.startswith('newtest') for target in COMMAND_LINE_TARGETS): + VariantDir('build/test', 'test', duplicate=0) + SConscript('build/test/SConscript') diff --git a/ext/SConscript b/ext/SConscript index e7e83a781..0d83c21f6 100644 --- a/ext/SConscript +++ b/ext/SConscript @@ -37,12 +37,13 @@ def prep_sundials(env): def prep_gtest(env): localenv = env.Clone() localenv.Append(CPPPATH=[Dir('#ext/gtest'), - Dir('#ext/gtest/include')]) + Dir('#ext/gtest/include')], + CPPDEFINES={'GTEST_HAS_PTHREAD': 0}) return localenv # (subdir, library name, (file extensions), prepfunction) libs = [('tpx','tpx',['cpp'],prep_default), - ('gtest/src','gtest',['^gtest-all.cc', '^gtest_main.cc'], prep_gtest)] + ('gtest/src','gtest',['^gtest-all.cc'], prep_gtest)] if env['build_with_f2c']: libs.append(('f2c_math', 'ctmath', ['cpp','c'], prep_f2c)) diff --git a/test/SConscript b/test/SConscript new file mode 100644 index 000000000..96269d016 --- /dev/null +++ b/test/SConscript @@ -0,0 +1,28 @@ +from buildutils import * +import subprocess + +Import('env','buildTargets','installTargets') +localenv = env.Clone() + +localenv.Append(CPPPATH='#ext/gtest/include', + LIBPATH='#build/lib', + LIBS=['gtest'] + localenv['cantera_libs']) + +def gtestRunner(target, source, env): + program = source[0] + code = subprocess.call([program.abspath]) + if not code: + open(target[0].path, 'w').write(time.asctime()+'\n') + return 0 + +def addTest(subdir, progName): + program = localenv.Program(pjoin(subdir, progName), + pjoin(subdir, '%s.cpp' % progName)) + passedFile = File(pjoin(str(program[0].dir), '%s.passed' % program[0].name)) + run_program = localenv.Command(passedFile, program, gtestRunner) + Alias('newtest', run_program) + if os.path.exists(passedFile.abspath): + Alias('newtest-reset', localenv.Command('reset-%s%s' % (subdir, progName), + [], [Delete(passedFile.abspath)])) + +addTest('thermo', 'nasapoly') diff --git a/test/thermo/nasapoly.cpp b/test/thermo/nasapoly.cpp new file mode 100644 index 000000000..a036a7040 --- /dev/null +++ b/test/thermo/nasapoly.cpp @@ -0,0 +1,118 @@ +#include "gtest/gtest.h" +#include "cantera/kernel/NasaPoly1.h" + +namespace Cantera +{ + +// CO2 low-temperature polynomial from GRI 3.0. Note that this order is +// different from the order used by CHEMKIN, with the 1/T and log(T) +// coefficients appearing first. +static double coeffs[] = {-4.83719697e+04, + 9.90105222e+00, + 2.35677352e+00, + 8.98459677e-03, + -7.12356269e-06, + 2.45919022e-09, + -1.43699548e-13, + }; + +class NasaPoly1Test : public testing::Test +{ +public: + NasaPoly1Test() + : poly(0, 200.0, 1000.0, 101325.0, coeffs) + , tpow_(6) { + } +protected: + void set_tpow(double T) { + tpow_[0] = T; + tpow_[1] = T*T; + tpow_[2] = T*T*T; + tpow_[3] = T*T*T*T; + tpow_[4] = 1.0/T; + tpow_[5] = std::log(T); + } + + void testEquivalent(NasaPoly1& p, NasaPoly1& q) { + EXPECT_EQ(poly.minTemp(), q.minTemp()); + EXPECT_EQ(poly.maxTemp(), q.maxTemp()); + EXPECT_EQ(poly.refPressure(), q.refPressure()); + EXPECT_EQ(poly.speciesIndex(), q.speciesIndex()); + + double cp_R1, h_RT1, s_R1; + double cp_R2, h_RT2, s_R2; + double T = 481.99; + p.updatePropertiesTemp(T, &cp_R1, &h_RT1, &s_R1); + q.updatePropertiesTemp(T, &cp_R2, &h_RT2, &s_R2); + EXPECT_DOUBLE_EQ(cp_R1, cp_R2); + EXPECT_DOUBLE_EQ(h_RT1, h_RT2); + EXPECT_DOUBLE_EQ(s_R1, s_R2); + } + + std::vector tpow_; + NasaPoly1 poly; +}; + +TEST_F(NasaPoly1Test, Initialization) +{ + EXPECT_EQ(poly.minTemp(), 200.0); + EXPECT_EQ(poly.maxTemp(), 1000.0); + EXPECT_EQ(poly.refPressure(), 101325.0); + EXPECT_EQ(poly.speciesIndex(), 0); +} + +TEST_F(NasaPoly1Test, Copy) +{ + NasaPoly1 q(poly); + testEquivalent(poly, q); +} + +TEST_F(NasaPoly1Test, Assignment) +{ + NasaPoly1 q; + q = poly; + testEquivalent(poly, q); +} + +TEST_F(NasaPoly1Test, updateProperties) +{ + double cp_R, h_RT, s_R; + + // Reference values calculated using CHEMKIN II + // Expect agreement to single-precision tolerance + set_tpow(298.15); + poly.updateProperties(&tpow_[0], &cp_R, &h_RT, &s_R); + EXPECT_NEAR(4.46633496, cp_R, 1e-7); + EXPECT_NEAR(-158.739244, h_RT, 1e-5); + EXPECT_NEAR(25.7125777, s_R, 1e-6); + + set_tpow(876.54); + poly.updateProperties(&tpow_[0], &cp_R, &h_RT, &s_R); + EXPECT_NEAR(6.33029000, cp_R, 1e-7); + EXPECT_NEAR(-50.3179924, h_RT, 1e-5); + EXPECT_NEAR(31.5401226, s_R, 1e-6); +} + +TEST_F(NasaPoly1Test, updatePropertiesTemp) +{ + double cp_R1, h_RT1, s_R1; + double cp_R2, h_RT2, s_R2; + double T = 481.99; + + set_tpow(T); + poly.updatePropertiesTemp(T, &cp_R1, &h_RT1, &s_R1); + poly.updateProperties(&tpow_[0], &cp_R2, &h_RT2, &s_R2); + + EXPECT_DOUBLE_EQ(cp_R1, cp_R2); + EXPECT_DOUBLE_EQ(h_RT1, h_RT2); + EXPECT_DOUBLE_EQ(s_R1, s_R2); +} + +} // namespace Cantera + +int main(int argc, char** argv) +{ + printf("Running main() from nasapoly.cpp\n"); + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}