From 7efb996335a12addac1ebc2e75dade83045e53fa Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Tue, 16 Jan 2024 13:36:18 -0500 Subject: [PATCH] [Test] Run C++ programs from User Guide as part of test suite --- doc/sphinx/userguide/cxx-tutorial.md | 94 ++----------------- samples/cxx/SConscript | 70 +++++++++----- test_problems/SConscript | 11 +++ .../cxx_userguide/demo1a_blessed.txt | 31 ++++++ .../cxx_userguide/demoequil_blessed.txt | 31 ++++++ .../kinetics_transport_blessed.txt | 27 ++++++ .../cxx_userguide/thermodemo_blessed.txt | 17 ++++ 7 files changed, 169 insertions(+), 112 deletions(-) create mode 100644 test_problems/cxx_userguide/demo1a_blessed.txt create mode 100644 test_problems/cxx_userguide/demoequil_blessed.txt create mode 100644 test_problems/cxx_userguide/kinetics_transport_blessed.txt create mode 100644 test_problems/cxx_userguide/thermodemo_blessed.txt diff --git a/doc/sphinx/userguide/cxx-tutorial.md b/doc/sphinx/userguide/cxx-tutorial.md index d5905ff86a..03ddb8bdc8 100644 --- a/doc/sphinx/userguide/cxx-tutorial.md +++ b/doc/sphinx/userguide/cxx-tutorial.md @@ -35,36 +35,8 @@ and flexible methods of compiling programs that use the Cantera C++ library, see Running this program produces the output below: -```none - ohmech: - - temperature 500 K - pressure 2.0265e+05 Pa - density 0.36118 kg/m^3 - mean mol. weight 7.4093 kg/kmol - phase of matter gas - - 1 kg 1 kmol - --------------- --------------- - enthalpy -2.4772e+06 -1.8354e+07 J - internal energy -3.0382e+06 -2.2511e+07 J - entropy 20699 1.5337e+05 J/K - Gibbs function -1.2827e+07 -9.5038e+07 J -heat capacity c_p 3919.1 29038 J/K -heat capacity c_v 2797 20724 J/K - - mass frac. Y mole frac. X chem. pot. / RT - --------------- --------------- --------------- - H2 0.21767 0.8 -15.644 - H 0 0 - O 0 0 - O2 0 0 - OH 0 0 - H2O 0.24314 0.1 -82.953 - HO2 0 0 - H2O2 0 0 - AR 0.53919 0.1 -20.503 - N2 0 0 +```{literalinclude} ../../../../test_problems/cxx_userguide/demo1a_blessed.txt +:language: none ``` As C++ programs go, this one is quite short. It is the Cantera equivalent of the "Hello, @@ -174,36 +146,8 @@ to a state of chemical equilibrium, holding the temperature and pressure fixed. The program output is: -```none - ohmech: - - temperature 1500 K - pressure 2.0265e+05 Pa - density 0.31683 kg/m^3 - mean mol. weight 19.499 kg/kmol - phase of matter gas - - 1 kg 1 kmol - --------------- --------------- - enthalpy -4.1789e+06 -8.1485e+07 J - internal energy -4.8186e+06 -9.3957e+07 J - entropy 11283 2.2001e+05 J/K - Gibbs function -2.1104e+07 -4.115e+08 J -heat capacity c_p 1893 36912 J/K -heat capacity c_v 1466.6 28597 J/K - - mass frac. Y mole frac. X chem. pot. / RT - --------------- --------------- --------------- - H2 0.025847 0.25 -19.295 - H 3.2181e-07 6.2252e-06 -9.6477 - O 6.2927e-12 7.6693e-12 -26.377 - O2 1.1747e-11 7.1586e-12 -52.753 - OH 3.0994e-07 3.5535e-07 -36.024 - H2O 0.46195 0.5 -45.672 - HO2 1.2362e-14 7.3034e-15 -62.401 - H2O2 6.904e-13 3.9578e-13 -72.049 - AR 0.51221 0.25 -21.339 - N2 0 0 +```{literalinclude} ../../../../test_problems/cxx_userguide/demoequil_blessed.txt +:language: none ``` How can we tell that this is really a state of chemical equilibrium? Well, by applying @@ -239,34 +183,8 @@ types from a {ct}`Solution` object: This program produces the output below: -```none -Net reaction rates for reactions involving CO2 - 11 CO + O (+M) <=> CO2 (+M) 3.54150687e-08 - 13 HCO + O <=> CO2 + H 1.95679990e-11 - 29 CH2CO + O <=> CH2 + CO2 3.45366954e-17 - 30 CO + O2 <=> CO2 + O 2.70102741e-13 - 98 CO + OH <=> CO2 + H 6.46935827e-03 -119 CO + HO2 <=> CO2 + OH 1.86807592e-10 -131 CH + CO2 <=> CO + HCO 9.41365868e-14 -151 CH2(S) + CO2 <=> CH2 + CO2 3.11161343e-12 -152 CH2(S) + CO2 <=> CH2O + CO 2.85339294e-11 -225 NCO + O2 <=> CO2 + NO 3.74127381e-19 -228 NCO + NO <=> CO2 + N2 6.25672710e-14 -261 HNCO + O <=> CO2 + NH 6.84524918e-13 -267 HNCO + OH <=> CO2 + NH2 7.78871222e-10 -279 CO2 + NH <=> CO + HNO -3.30333709e-09 -281 NCO + NO2 <=> CO2 + N2O 2.14286657e-20 -282 CO2 + N <=> CO + NO 6.42658345e-10 -289 CH2 + O2 => CO2 + 2 H 1.51032305e-18 -304 CH2CHO + O => CH2 + CO2 + H 1.00331721e-19 - -T viscosity thermal conductivity ------- ----------- -------------------- -300.0 1.6701e-05 4.2143e-02 -400.0 2.0896e-05 5.2797e-02 -500.0 2.4704e-05 6.2827e-02 -600.0 2.8230e-05 7.2625e-02 -700.0 3.1536e-05 8.2311e-02 +```{literalinclude} ../../../../test_problems/cxx_userguide/kinetics_transport_blessed.txt +:language: none ``` ## Examples of additional C++ functionality diff --git a/samples/cxx/SConscript b/samples/cxx/SConscript index 63ee37d292..4aef1a2f56 100644 --- a/samples/cxx/SConscript +++ b/samples/cxx/SConscript @@ -1,27 +1,45 @@ from os.path import join as pjoin import os from buildutils import * +from collections import namedtuple Import('env', 'build', 'install', 'buildSample') -# (subdir, program name, [source extensions], openmp_flag) +Sample = namedtuple( + 'Sample', + ('name', 'srcdir', 'dest_dir', 'file_patterns', 'openmp', 'install'), + defaults=(None, ['cpp'], False, True) +) + samples = [ - ('combustor', 'combustor', ['cpp'], False), - ('custom', 'custom', ['cpp'], False), - ('demo', 'demo', ['cpp'], False), - ('flamespeed', 'flamespeed', ['cpp'], False), - ('kinetics1', 'kinetics1', ['cpp'], False), - ('jacobian', 'derivative_speed', ['cpp'], False), - ('gas_transport', 'gas_transport', ['cpp'], False), - ('rankine', 'rankine', ['cpp'], False), - ('LiC6_electrode', 'LiC6_electrode', ['cpp'], False), - ('openmp_ignition', 'openmp_ignition', ['cpp'], True), - ('bvp', 'blasius', ['cpp'], False) + Sample('combustor', 'combustor'), + Sample('custom', 'custom'), + Sample('demo', 'demo'), + Sample('flamespeed', 'flamespeed'), + Sample('kinetics1', 'kinetics1'), + Sample('derivative_speed', 'jacobian'), + Sample('gas_transport', 'gas_transport'), + Sample('rankine', 'rankine'), + Sample('LiC6_electrode', 'LiC6_electrode'), + Sample('openmp_ignition', 'openmp_ignition', openmp=True), + Sample('blasius', 'bvp'), + Sample('demo1a', '#doc/sphinx/userguide', + dest_dir='#build/samples/userguide', + file_patterns=['^demo1a.cpp'], install=False), + Sample('thermodemo', '#doc/sphinx/userguide', + dest_dir='#build/samples/userguide', + file_patterns=['^thermodemo.cpp'], install=False), + Sample('demoequil', '#doc/sphinx/userguide', + dest_dir='#build/samples/userguide', + file_patterns=['^demoequil.cpp'], install=False), + Sample('kinetics_transport', '#doc/sphinx/userguide', + dest_dir='#build/samples/userguide', + file_patterns=['^kinetics_transport.cpp'], install=False), ] -for subdir, name, extensions, openmp in samples: +for sample in samples: localenv = env.Clone() - if openmp: + if sample.openmp: localenv.Append(CXXFLAGS=env['openmp_flag'], LINKFLAGS=env['openmp_flag']) if env['using_apple_clang']: localenv.Append(LIBS=['omp']) @@ -43,11 +61,15 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS} localenv.Append(LIBS=env['cantera_shared_libs']) localenv.Prepend(CPPPATH=['#include']) - if openmp and not env['HAS_OPENMP']: - logger.info(f"Skipping sample {name!r} because 'omp.h' was not found.") + dest_dir = sample.dest_dir or sample.srcdir + if sample.openmp and not env['HAS_OPENMP']: + logger.info(f"Skipping sample {sample.name!r} because 'omp.h' was not found.") else: - buildSample(localenv.Program, pjoin(subdir, name), - multi_glob(localenv, subdir, *extensions)) + buildSample(localenv.Program, pjoin(dest_dir, sample.name), + multi_glob(localenv, sample.srcdir, *sample.file_patterns)) + + if not sample.install: + continue # Note: These CMakeLists.txt and SConstruct files are automatically installed # by the "RecursiveInstall" that grabs everything in the cxx directory. @@ -92,8 +114,8 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS} localenv['tmpl_cantera_libdirs'] = repr([x for x in libdirs if x]) localenv['cmake_cantera_libdirs'] = ' '.join(quoted(x) for x in libdirs if x) localenv['tmpl_cantera_linkflags'] = repr(link_flags) - localenv['tmpl_progname'] = name - localenv['tmpl_sourcename'] = name + '.cpp' + localenv['tmpl_progname'] = sample.name + localenv['tmpl_sourcename'] = sample.name + '.cpp' env_args = [] ## Generate SConstruct files to be installed @@ -112,9 +134,9 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS} # environment variables optionally overriding localenv['tmpl_cxx'] = env.subst("env['CXX'] = os.environ.get('CXX', '$CXX')") - sconstruct = localenv.SubstFile(pjoin(subdir, 'SConstruct'), 'SConstruct.in') - install(pjoin('$inst_sampledir', 'cxx', subdir), sconstruct) + sconstruct = localenv.SubstFile(pjoin(dest_dir, 'SConstruct'), 'SConstruct.in') + install(pjoin('$inst_sampledir', 'cxx', dest_dir), sconstruct) ## Generate CMakeList.txt files to be installed - cmakelists = localenv.SubstFile(pjoin(subdir, 'CMakeLists.txt'), 'CMakeLists.txt.in') - install(pjoin('$inst_sampledir', 'cxx', subdir), cmakelists) + cmakelists = localenv.SubstFile(pjoin(dest_dir, 'CMakeLists.txt'), 'CMakeLists.txt.in') + install(pjoin('$inst_sampledir', 'cxx', dest_dir), cmakelists) diff --git a/test_problems/SConscript b/test_problems/SConscript index 5d3527ec31..8fc957d030 100644 --- a/test_problems/SConscript +++ b/test_problems/SConscript @@ -243,6 +243,17 @@ Test('cxx-rankine', 'cxx_samples', '#build/samples/cxx/rankine/rankine', Test('clib-demo', 'clib', '#build/samples/clib/demo', 'clib_demo_blessed.txt') +# C++ programs from User Guide +Test('cxx-guide-demo1a', 'cxx_userguide', '#build/samples/userguide/demo1a', + blessedName='demo1a_blessed.txt') +Test('cxx-guide-thermodemo', 'cxx_userguide', '#build/samples/userguide/thermodemo', + blessedName='thermodemo_blessed.txt') +Test('cxx-guide-demoequil', 'cxx_userguide', '#build/samples/userguide/demoequil', + blessedName='demoequil_blessed.txt') +Test('cxx-guide-kinetics_transport', 'cxx_userguide', + '#build/samples/userguide/kinetics_transport', + blessedName='kinetics_transport_blessed.txt') + if env['f90_interface'] == 'y': Test('f90-demo', 'fortran', '#build/samples/f90/demo', 'f90_demo_blessed.txt', threshold=1e-10) diff --git a/test_problems/cxx_userguide/demo1a_blessed.txt b/test_problems/cxx_userguide/demo1a_blessed.txt new file mode 100644 index 0000000000..cbdc858fd4 --- /dev/null +++ b/test_problems/cxx_userguide/demo1a_blessed.txt @@ -0,0 +1,31 @@ + + ohmech: + + temperature 500 K + pressure 2.0265e+05 Pa + density 0.36118 kg/m^3 + mean mol. weight 7.4093 kg/kmol + phase of matter gas + + 1 kg 1 kmol + --------------- --------------- + enthalpy -2.4772e+06 -1.8354e+07 J + internal energy -3.0382e+06 -2.2511e+07 J + entropy 20699 1.5337e+05 J/K + Gibbs function -1.2827e+07 -9.5038e+07 J + heat capacity c_p 3919.1 29038 J/K + heat capacity c_v 2797 20724 J/K + + mass frac. Y mole frac. X chem. pot. / RT + --------------- --------------- --------------- + H2 0.21767 0.8 -15.644 + H 0 0 + O 0 0 + O2 0 0 + OH 0 0 + H2O 0.24314 0.1 -82.953 + HO2 0 0 + H2O2 0 0 + AR 0.53919 0.1 -20.503 + N2 0 0 + diff --git a/test_problems/cxx_userguide/demoequil_blessed.txt b/test_problems/cxx_userguide/demoequil_blessed.txt new file mode 100644 index 0000000000..26c706b746 --- /dev/null +++ b/test_problems/cxx_userguide/demoequil_blessed.txt @@ -0,0 +1,31 @@ + + ohmech: + + temperature 1500 K + pressure 2.0265e+05 Pa + density 0.31683 kg/m^3 + mean mol. weight 19.499 kg/kmol + phase of matter gas + + 1 kg 1 kmol + --------------- --------------- + enthalpy -4.1789e+06 -8.1485e+07 J + internal energy -4.8186e+06 -9.3957e+07 J + entropy 11283 2.2001e+05 J/K + Gibbs function -2.1104e+07 -4.115e+08 J + heat capacity c_p 1893 36912 J/K + heat capacity c_v 1466.6 28597 J/K + + mass frac. Y mole frac. X chem. pot. / RT + --------------- --------------- --------------- + H2 0.025847 0.25 -19.295 + H 3.2181e-07 6.2252e-06 -9.6477 + O 6.2927e-12 7.6693e-12 -26.377 + O2 1.1747e-11 7.1586e-12 -52.753 + OH 3.0994e-07 3.5535e-07 -36.024 + H2O 0.46195 0.5 -45.672 + HO2 1.2362e-14 7.3034e-15 -62.401 + H2O2 6.904e-13 3.9578e-13 -72.049 + AR 0.51221 0.25 -21.339 + N2 0 0 + diff --git a/test_problems/cxx_userguide/kinetics_transport_blessed.txt b/test_problems/cxx_userguide/kinetics_transport_blessed.txt new file mode 100644 index 0000000000..1bf133edb7 --- /dev/null +++ b/test_problems/cxx_userguide/kinetics_transport_blessed.txt @@ -0,0 +1,27 @@ +Net reaction rates for reactions involving CO2 + 11 CO + O (+M) <=> CO2 (+M) 3.54150687e-08 + 13 HCO + O <=> CO2 + H 1.95679990e-11 + 29 CH2CO + O <=> CH2 + CO2 3.45366954e-17 + 30 CO + O2 <=> CO2 + O 2.70102741e-13 + 98 CO + OH <=> CO2 + H 6.46935827e-03 +119 CO + HO2 <=> CO2 + OH 1.86807592e-10 +131 CH + CO2 <=> CO + HCO 9.41365868e-14 +151 CH2(S) + CO2 <=> CH2 + CO2 3.11161343e-12 +152 CH2(S) + CO2 <=> CH2O + CO 2.85339294e-11 +225 NCO + O2 <=> CO2 + NO 3.74127381e-19 +228 NCO + NO <=> CO2 + N2 6.25672710e-14 +261 HNCO + O <=> CO2 + NH 6.84524918e-13 +267 HNCO + OH <=> CO2 + NH2 7.78871222e-10 +279 CO2 + NH <=> CO + HNO -3.30333709e-09 +281 NCO + NO2 <=> CO2 + N2O 2.14286657e-20 +282 CO2 + N <=> CO + NO 6.42658345e-10 +289 CH2 + O2 => CO2 + 2 H 1.51032305e-18 +304 CH2CHO + O => CH2 + CO2 + H 1.00331721e-19 + +T viscosity thermal conductivity +------ ----------- -------------------- +300.0 1.6701e-05 4.2143e-02 +400.0 2.0896e-05 5.2797e-02 +500.0 2.4704e-05 6.2827e-02 +600.0 2.8230e-05 7.2625e-02 +700.0 3.1536e-05 8.2311e-02 diff --git a/test_problems/cxx_userguide/thermodemo_blessed.txt b/test_problems/cxx_userguide/thermodemo_blessed.txt new file mode 100644 index 0000000000..ca6868f71b --- /dev/null +++ b/test_problems/cxx_userguide/thermodemo_blessed.txt @@ -0,0 +1,17 @@ +1500 +202650 +0.253468 +3.48935e+07 +198734 +2.23688e+06 +12740 +H2 -2.29727e+08 +H -8.58597e+09 +O -8.6249e+09 +O2 -3.57938e+08 +OH -8.87922e+09 +H2O -9.1761e+09 +HO2 -8.98743e+09 +H2O2 -9.15754e+09 +AR -2.68918e+08 +N2 -8.93078e+09