[Test] Convert 'fracCoeff' test to GTest

This commit is contained in:
Ray Speth 2014-09-26 13:45:06 +00:00
parent 992782d1e2
commit 53102e8d70
9 changed files with 149 additions and 406 deletions

View file

@ -196,7 +196,7 @@ else:
# Instantiate tests
addTestProgram('thermo', 'thermo', env_vars=python_env_vars)
addTestProgram('kinetics', 'kinetics')
addTestProgram('kinetics', 'kinetics', env_vars=python_env_vars)
python_subtests = ['']
test_root = '#interfaces/cython/cantera/test'

View file

@ -1,11 +1,7 @@
#
# Input file to test use of non-integral stoichiometric
# coefficients. Used with script frac.py.
#
# Input file to test use of non-integral stoichiometric coefficients.
units(length = "cm", time = "s", quantity = "mol", act_energy = "cal/mol")
ideal_gas(name = "gas",
elements = " O H ",
species = """ H2 H O O2 OH H2O """,
@ -13,8 +9,6 @@ ideal_gas(name = "gas",
initial_state = state(temperature = 300.0,
pressure = OneAtm) )
#-------------------------------------------------------------------------------
# Species data
#-------------------------------------------------------------------------------
@ -103,4 +97,3 @@ reaction( "H2O => 1.4 H + 0.6 OH + 0.2 O2", [1.0e13, 0.0, 0.0])
# coefficients.
reaction( "0.7 H2 + 0.6 OH + 0.2 O2 => H2O", [1.0e13, 0.0, 0.0],
order = "H2:0.8 OH:2 O2:1")

147
test/kinetics/rates.cpp Normal file
View file

@ -0,0 +1,147 @@
#include "gtest/gtest.h"
#include "cantera/kinetics.h"
#include "cantera/thermo/IdealGasPhase.h"
namespace Cantera
{
#ifndef HAS_NO_PYTHON
TEST(FracCoeff, ConvertFracCoeff)
{
IdealGasPhase thermo1("../data/frac.cti", "gas");
std::vector<ThermoPhase*> phases1;
phases1.push_back(&thermo1);
GasKinetics kinetics1;
importKinetics(thermo1.xml(), phases1, &kinetics1);
IdealGasPhase thermo2("../data/frac.xml", "gas");
std::vector<ThermoPhase*> phases2;
phases2.push_back(&thermo2);
GasKinetics kinetics2;
importKinetics(thermo2.xml(), phases2, &kinetics2);
ASSERT_EQ(thermo2.nSpecies(), thermo1.nSpecies());
ASSERT_EQ(kinetics2.nReactions(), kinetics1.nReactions());
for (size_t i = 0; i < kinetics1.nReactions(); i++) {
for (size_t k = 0; k < thermo1.nSpecies(); k++) {
EXPECT_EQ(kinetics1.reactantStoichCoeff(k,i),
kinetics2.reactantStoichCoeff(k,i));
EXPECT_EQ(kinetics1.productStoichCoeff(k,i),
kinetics2.productStoichCoeff(k,i));
}
}
}
#endif
class FracCoeffTest : public testing::Test
{
public:
FracCoeffTest() :
therm("../data/frac.xml", "gas")
{
std::vector<ThermoPhase*> phases;
phases.push_back(&therm);
importKinetics(therm.xml(), phases, &kin);
therm.setState_TPX(2000, 4*OneAtm,
"H2O:0.5, OH:.05, H:0.1, O2:0.15, H2:0.2");
kH2O = therm.speciesIndex("H2O");
kH = therm.speciesIndex("H");
kOH = therm.speciesIndex("OH");
kO2 = therm.speciesIndex("O2");
kH2 = therm.speciesIndex("H2");
}
IdealGasPhase therm;
GasKinetics kin;
size_t kH2O, kH, kOH, kO2, kH2;
};
TEST_F(FracCoeffTest, StoichCoeffs)
{
EXPECT_DOUBLE_EQ(1.0, kin.reactantStoichCoeff(kH2O, 0));
EXPECT_DOUBLE_EQ(1.4, kin.productStoichCoeff(kH, 0));
EXPECT_DOUBLE_EQ(0.6, kin.productStoichCoeff(kOH, 0));
EXPECT_DOUBLE_EQ(0.2, kin.productStoichCoeff(kO2, 0));
EXPECT_DOUBLE_EQ(0.7, kin.reactantStoichCoeff(kH2, 1));
EXPECT_DOUBLE_EQ(0.6, kin.reactantStoichCoeff(kOH, 1));
EXPECT_DOUBLE_EQ(0.2, kin.reactantStoichCoeff(kO2, 1));
EXPECT_DOUBLE_EQ(1.0, kin.productStoichCoeff(kH2O, 1));
}
TEST_F(FracCoeffTest, RateConstants)
{
vector_fp kf(kin.nReactions(), 0.0);
vector_fp kr(kin.nReactions(), 0.0);
kin.getFwdRateConstants(&kf[0]);
kin.getRevRateConstants(&kr[0]);
// sum of reaction orders is 1.0; kf has units of 1/s
EXPECT_DOUBLE_EQ(1e13, kf[0]);
// sum of reaction orders is 3.8.
// kf = 1e13 (mol/cm^3)^-2.8 s^-1 = 1e13*1000^-2.8 (kmol/m^3)^-2.8 s^-1
EXPECT_NEAR(1e13*pow(1e3, -2.8), kf[1], 1e-2);
// Reactions are irreversible
EXPECT_DOUBLE_EQ(0.0, kr[0]);
EXPECT_DOUBLE_EQ(0.0, kr[1]);
}
TEST_F(FracCoeffTest, RatesOfProgress)
{
vector_fp kf(kin.nReactions(), 0.0);
vector_fp conc(therm.nSpecies(), 0.0);
vector_fp ropf(kin.nReactions(), 0.0);
therm.getConcentrations(&conc[0]);
kin.getFwdRateConstants(&kf[0]);
kin.getFwdRatesOfProgress(&ropf[0]);
EXPECT_DOUBLE_EQ(conc[kH2O]*kf[0], ropf[0]);
EXPECT_DOUBLE_EQ(pow(conc[kH2], 0.8)*conc[kO2]*pow(conc[kOH],2)*kf[1],
ropf[1]);
}
TEST_F(FracCoeffTest, CreationDestructionRates)
{
vector_fp ropf(kin.nReactions(), 0.0);
vector_fp cdot(therm.nSpecies(), 0.0);
vector_fp ddot(therm.nSpecies(), 0.0);
kin.getFwdRatesOfProgress(&ropf[0]);
kin.getCreationRates(&cdot[0]);
kin.getDestructionRates(&ddot[0]);
EXPECT_DOUBLE_EQ(ropf[0], ddot[kH2O]);
EXPECT_DOUBLE_EQ(1.4*ropf[0], cdot[kH]);
EXPECT_DOUBLE_EQ(0.6*ropf[0], cdot[kOH]);
EXPECT_DOUBLE_EQ(0.2*ropf[0], cdot[kO2]);
EXPECT_DOUBLE_EQ(0.7*ropf[1], ddot[kH2]);
EXPECT_DOUBLE_EQ(0.6*ropf[1], ddot[kOH]);
EXPECT_DOUBLE_EQ(0.2*ropf[1], ddot[kO2]);
EXPECT_DOUBLE_EQ(ropf[1], cdot[kH2O]);
EXPECT_DOUBLE_EQ(0.0, cdot[therm.speciesIndex("O")]);
EXPECT_DOUBLE_EQ(0.0, ddot[therm.speciesIndex("O")]);
}
TEST_F(FracCoeffTest, EquilibriumConstants)
{
vector_fp Kc(kin.nReactions(), 0.0);
vector_fp mu0(therm.nSpecies(), 0.0);
kin.getEquilibriumConstants(&Kc[0]);
therm.getGibbs_ref(&mu0[0]); // at pRef
double deltaG0_0 = 1.4 * mu0[kH] + 0.6 * mu0[kOH] + 0.2 * mu0[kO2] - mu0[kH2O];
double deltaG0_1 = mu0[kH2O] - 0.7 * mu0[kH2] - 0.6 * mu0[kOH] - 0.2 * mu0[kO2];
double pRef = therm.refPressure();
double RT = GasConstant * therm.temperature();
// Net stoichiometric coefficients are 1.2 and -0.5
EXPECT_NEAR(exp(-deltaG0_0/RT) * pow(pRef/RT, 1.2), Kc[0], 1e-13 * Kc[0]);
EXPECT_NEAR(exp(-deltaG0_1/RT) * pow(pRef/RT, -0.5), Kc[1], 1e-13 * Kc[1]);
}
}

View file

@ -257,7 +257,6 @@ if haveConverters:
options='diamond.cti', artifacts=diamond_name)
CompileAndTest('dustyGasTransport', 'dustyGasTransport', 'dustyGasTransport',
'output_blessed.txt')
CompileAndTest('fracCoeff', 'fracCoeff', 'fracCoeff', 'frac_blessed.out')
CompileAndTest('mixGasTransport',
'mixGasTransport', 'mixGasTransport', 'output_blessed.txt')
CompileAndTest('multiGasTransport',

View file

@ -1,161 +0,0 @@
/**
* @file runDiamond.cpp
*/
// Example
//
// Note that this example needs updating. It works fine, but is
// written in a way that is less than transparent or
// user-friendly. This could be rewritten using class Interface to
// make things simpler.
#include "cantera/kinetics.h"
#include <cstdio>
using namespace std;
using namespace Cantera;
void printDbl(double val)
{
if (fabs(val) < 5.0E-200) {
cout << " nil";
} else {
cout << val;
}
}
int main(int argc, char** argv)
{
#ifdef _MSC_VER
_set_output_format(_TWO_DIGIT_EXPONENT);
#endif
string infile = "frac.xml";
double x[10], kc[10];
double cdot[10], ddot[10];
//double fwd_rop[10];
try {
XML_Node* xc = get_XML_File(infile);
XML_Node* const xg = xc->findNameID("phase", "gas");
ThermoPhase* gasTP = newPhase(*xg);
size_t nsp = gasTP->nSpecies();
cout.precision(4);
cout << "Number of species = " << nsp << endl;
vector<ThermoPhase*> phaseList;
phaseList.push_back(gasTP);
GasKinetics* iKin_ptr = new GasKinetics();
importKinetics(*xg, phaseList, iKin_ptr);
size_t nr = iKin_ptr->nReactions();
cout << "Number of reactions = " << nr << endl;
size_t iH2 = gasTP->speciesIndex("H2");
size_t iH = gasTP->speciesIndex("H");
size_t iO2 = gasTP->speciesIndex("O2");
size_t iOH = gasTP->speciesIndex("OH");
size_t iH2O = gasTP->speciesIndex("H2O");
for (size_t i = 0; i < nsp; i++) {
x[i] = 0.0;
}
x[iH2O] = 1.0/2.0;
x[iOH] = 0.1/2.0;
x[iH] = 0.2/2.0;
x[iO2] = 0.3/2.0;
x[iH2] = 0.4/2.0;
double p = OneAtm;
gasTP->setState_TPX(2000., p, x);
double src[20];
for (size_t i = 0; i < 20; i++) {
src[i] = 0.0;
}
iKin_ptr->getNetProductionRates(src);
double fwd_rop[10];
iKin_ptr->getFwdRatesOfProgress(fwd_rop);
cout << "fwd_rop[0] = " << fwd_rop[0] << endl;
cout << "fwd_rop[1] = " << fwd_rop[1] << endl;
iKin_ptr->getCreationRates(cdot);
iKin_ptr->getDestructionRates(ddot);
for (size_t k = 0; k < nsp; k++) {
string sss = gasTP->speciesName(k);
cout << k << " " << sss << " ";
printDbl(src[k]);
cout << endl;
}
printf("Creation Rates: \n");
for (size_t k = 0; k < nsp - 1; k++) {
string sss = gasTP->speciesName(k);
cout << k << " " << sss << " ";
cout << cdot[k] << " ";
cout << cdot[k] / fwd_rop[0] << " ";
cout << endl;
}
string sss = gasTP->speciesName(iH2O);
cout << iH2O << " " << sss << " ";
cout << cdot[iH2O] << " ";
cout << cdot[iH2O] / fwd_rop[1] << " ";
cout << endl;
printf("Destruction Rates: \n");
for (size_t k = 0; k < nsp-1; k++) {
string sss = gasTP->speciesName(k);
cout << k << " " << sss << " ";
cout << ddot[k] << " ";
cout << ddot[k] / fwd_rop[1] << " ";
cout << endl;
}
sss = gasTP->speciesName(iH2O);
cout << iH2O << " " << sss << " ";
cout << ddot[iH2O] << " ";
cout << ddot[iH2O] / fwd_rop[0] << " ";
cout << endl;
double c[10];
gasTP->getConcentrations(c);
double order_H2 = 0.8;
double order_OH = 2.0;
double order_O2 = 1.0;
double kf[10];
iKin_ptr->getFwdRateConstants(kf);
printf("kf[0] = %g\n", kf[0]);
printf("kf[1] = %g\n", kf[1]);
//double cprod0 = c[iH2O];
double cprod1 = pow(c[iH2], order_H2) * pow(c[iOH], order_OH) * pow(c[iO2], order_O2);
printf("equal numbers 0: %g %g \n", kf[0] * c[iH2O], fwd_rop[0]);
printf("equal numbers 1: %g %g\n", kf[1] * cprod1, fwd_rop[1]);
iKin_ptr->getEquilibriumConstants(kc);
printf("Equilibrium constants for irreversible fractional rxns:\n");
printf("Kc[0] = %g\n", kc[0]);
printf("Kc[1] = %g\n", kc[1]);
delete iKin_ptr;
iKin_ptr = 0;
delete gasTP;
appdelete();
} catch (CanteraError& err) {
std::cout << err.what() << std::endl;
return -1;
}
return 0;
}
/***********************************************************/

View file

@ -1,31 +0,0 @@
Number of species = 6
Number of reactions = 2
fwd_rop[0] = 3.047e+10
fwd_rop[1] = 1.575e-08
0 H2 -1.103e-08
1 H 4.265e+10
2 O nil
3 O2 6.093e+09
4 OH 1.828e+10
5 H2O -3.047e+10
Creation Rates:
0 H2 0 0
1 H 4.265e+10 1.4
2 O 0 0
3 O2 6.093e+09 0.2
4 OH 1.828e+10 0.6
5 H2O 1.575e-08 1
Destruction Rates:
0 H2 1.103e-08 0.7
1 H 0 0
2 O 0 0
3 O2 3.15e-09 0.2
4 OH 9.45e-09 0.6
5 H2O 3.047e+10 1
kf[0] = 1e+13
kf[1] = 39810.7
equal numbers 0: 3.04665e+10 3.04665e+10
equal numbers 1: 1.57505e-08 1.57505e-08
Equilibrium constants for irreversible fractional rxns:
Kc[0] = 5.60014e-11
Kc[1] = 62705

View file

@ -1,157 +0,0 @@
<?xml version="1.0"?>
<ctml>
<validate reactions="yes" species="yes"/>
<!-- phase gas -->
<phase dim="3" id="gas">
<elementArray datasrc="elements.xml">O H </elementArray>
<speciesArray datasrc="#species_data">H2 H O O2 OH H2O </speciesArray>
<reactionArray datasrc="#reaction_data"/>
<state>
<temperature units="K">300.0</temperature>
<pressure units="Pa">101325.0</pressure>
</state>
<thermo model="IdealGas"/>
<kinetics model="GasKinetics"/>
<transport model="None"/>
</phase>
<!-- species definitions -->
<speciesData id="species_data">
<!-- species H2 -->
<species name="H2">
<atomArray>H:2 </atomArray>
<thermo>
<NASA Tmax="1000.0" Tmin="200.0" P0="100000.0">
<floatArray name="coeffs" size="7">
2.344331120E+00, 7.980520750E-03, -1.947815100E-05, 2.015720940E-08,
-7.376117610E-12, -9.179351730E+02, 6.830102380E-01</floatArray>
</NASA>
<NASA Tmax="3500.0" Tmin="1000.0" P0="100000.0">
<floatArray name="coeffs" size="7">
3.337279200E+00, -4.940247310E-05, 4.994567780E-07, -1.795663940E-10,
2.002553760E-14, -9.501589220E+02, -3.205023310E+00</floatArray>
</NASA>
</thermo>
</species>
<!-- species H -->
<species name="H">
<atomArray>H:1 </atomArray>
<thermo>
<NASA Tmax="1000.0" Tmin="200.0" P0="100000.0">
<floatArray name="coeffs" size="7">
2.500000000E+00, 7.053328190E-13, -1.995919640E-15, 2.300816320E-18,
-9.277323320E-22, 2.547365990E+04, -4.466828530E-01</floatArray>
</NASA>
<NASA Tmax="3500.0" Tmin="1000.0" P0="100000.0">
<floatArray name="coeffs" size="7">
2.500000010E+00, -2.308429730E-11, 1.615619480E-14, -4.735152350E-18,
4.981973570E-22, 2.547365990E+04, -4.466829140E-01</floatArray>
</NASA>
</thermo>
</species>
<!-- species O -->
<species name="O">
<atomArray>O:1 </atomArray>
<thermo>
<NASA Tmax="1000.0" Tmin="200.0" P0="100000.0">
<floatArray name="coeffs" size="7">
3.168267100E+00, -3.279318840E-03, 6.643063960E-06, -6.128066240E-09,
2.112659710E-12, 2.912225920E+04, 2.051933460E+00</floatArray>
</NASA>
<NASA Tmax="3500.0" Tmin="1000.0" P0="100000.0">
<floatArray name="coeffs" size="7">
2.569420780E+00, -8.597411370E-05, 4.194845890E-08, -1.001777990E-11,
1.228336910E-15, 2.921757910E+04, 4.784338640E+00</floatArray>
</NASA>
</thermo>
</species>
<!-- species O2 -->
<species name="O2">
<atomArray>O:2 </atomArray>
<thermo>
<NASA Tmax="1000.0" Tmin="200.0" P0="100000.0">
<floatArray name="coeffs" size="7">
3.782456360E+00, -2.996734160E-03, 9.847302010E-06, -9.681295090E-09,
3.243728370E-12, -1.063943560E+03, 3.657675730E+00</floatArray>
</NASA>
<NASA Tmax="3500.0" Tmin="1000.0" P0="100000.0">
<floatArray name="coeffs" size="7">
3.282537840E+00, 1.483087540E-03, -7.579666690E-07, 2.094705550E-10,
-2.167177940E-14, -1.088457720E+03, 5.453231290E+00</floatArray>
</NASA>
</thermo>
</species>
<!-- species OH -->
<species name="OH">
<atomArray>H:1 O:1 </atomArray>
<thermo>
<NASA Tmax="1000.0" Tmin="200.0" P0="100000.0">
<floatArray name="coeffs" size="7">
3.992015430E+00, -2.401317520E-03, 4.617938410E-06, -3.881133330E-09,
1.364114700E-12, 3.615080560E+03, -1.039254580E-01</floatArray>
</NASA>
<NASA Tmax="3500.0" Tmin="1000.0" P0="100000.0">
<floatArray name="coeffs" size="7">
3.092887670E+00, 5.484297160E-04, 1.265052280E-07, -8.794615560E-11,
1.174123760E-14, 3.858657000E+03, 4.476696100E+00</floatArray>
</NASA>
</thermo>
</species>
<!-- species H2O -->
<species name="H2O">
<atomArray>H:2 O:1 </atomArray>
<thermo>
<NASA Tmax="1000.0" Tmin="200.0" P0="100000.0">
<floatArray name="coeffs" size="7">
4.198640560E+00, -2.036434100E-03, 6.520402110E-06, -5.487970620E-09,
1.771978170E-12, -3.029372670E+04, -8.490322080E-01</floatArray>
</NASA>
<NASA Tmax="3500.0" Tmin="1000.0" P0="100000.0">
<floatArray name="coeffs" size="7">
3.033992490E+00, 2.176918040E-03, -1.640725180E-07, -9.704198700E-11,
1.682009920E-14, -3.000429710E+04, 4.966770100E+00</floatArray>
</NASA>
</thermo>
</species>
</speciesData>
<reactionData id="reaction_data">
<!-- reaction 0001 -->
<reaction reversible="no" id="0001">
<equation>H2O =] 1.4 H + 0.6 OH + 0.2 O2</equation>
<rateCoeff>
<Arrhenius>
<A>1.000000E+13</A>
<b>0.0</b>
<E units="cal/mol">0.000000</E>
</Arrhenius>
</rateCoeff>
<reactants>H2O:1.0</reactants>
<products>H:1.3999999999999999 O2:0.20000000000000001 OH:0.59999999999999998</products>
</reaction>
<!-- reaction 0002 -->
<reaction reversible="no" id="0002">
<equation>0.7 H2 + 0.6 OH + 0.2 O2 =] H2O</equation>
<order species="H2">0.80000000000000004</order>
<order species="O2">1.0</order>
<order species="OH">2.0</order>
<rateCoeff>
<Arrhenius>
<A>3.981072E+04</A>
<b>0.0</b>
<E units="cal/mol">0.000000</E>
</Arrhenius>
</rateCoeff>
<reactants>H2:0.69999999999999996 O2:0.20000000000000001 OH:0.59999999999999998</reactants>
<products>H2O:1.0</products>
</reaction>
</reactionData>
</ctml>

View file

@ -1,47 +0,0 @@
#!/bin/sh
#
#
temp_success="0"
/bin/rm -f output.txt outputa.txt
#################################################################
#
#################################################################
CANTERA_DATA=${CANTERA_DATA:=../../data/inputs}; export CANTERA_DATA
CANTERA_BIN=${CANTERA_BIN:=../../bin}
./fracCoeff > output.txt
retnStat=$?
if [ $retnStat != "0" ]
then
temp_success="1"
echo "fracCoeff returned with bad status, $retnStat, check output"
fi
../../bin/exp3to2.sh output.txt > outputa.txt
diff -w outputa.txt frac_blessed.out > diff_test.out
retnStat=$?
if [ $retnStat = "0" ]
then
echo "successful diff comparison on fracCoeff test"
else
echo "unsuccessful diff comparison on fracCoeff test"
echo "FAILED" > csvCode.txt
temp_success="1"
fi
../../bin/exp3to2.sh frac.xml > fraca.xml
diff -w fraca.xml frac_blessed.xml > xml_diff_test.out
retnStat=$?
if [ $retnStat = "0" ]
then
echo "successful diff comparison on fracCoeff.xml test"
else
echo "unsuccessful diff comparison on fracCoeff.xml test"
echo "FAILED" > csvCode.txt
temp_success="1"
fi
exit $temp_success