[Test] Move "ChemEquil_gri_matrix" to GTest suite

This also modifies the test to explicitly test each of the equilibrium solvers,
so that failures in any one solver aren't hidden by the fact that a different
solver works for a particular initial condition.
This commit is contained in:
Ray Speth 2015-06-17 14:38:11 -04:00
parent c2344377ac
commit 30d1d5ea99
5 changed files with 121 additions and 509 deletions

View file

@ -1,11 +1,20 @@
#include "gtest/gtest.h"
#include "cantera/thermo/ThermoFactory.h"
#include "cantera/thermo/IdealGasPhase.h"
#include "cantera/equil/MultiPhase.h"
#include "cantera/base/global.h"
#include "cantera/base/utilities.h"
using namespace Cantera;
bool double_close(double expected, double actual, double tol)
{
return std::abs(expected-actual) / (std::abs(expected) + tol) < tol;
}
#define EXPECT_CLOSE(a,b,tol) EXPECT_PRED3(double_close, a,b,tol)
class OverconstrainedEquil : public testing::Test
{
public:
@ -91,6 +100,118 @@ TEST_F(OverconstrainedEquil, DISABLED_BasisOptimize2)
ASSERT_EQ(1, (int) nc);
}
class GriMatrix : public testing::Test
{
public:
GriMatrix() : gas("gri30.xml", "gri30") {
X.resize(gas.nSpecies());
Yelem.resize(gas.nElements());
};
void save_elemental_mole_fractions() {
for (size_t i = 0; i < gas.nElements(); i++) {
Yelem[i] = gas.elementalMassFraction(i);
}
}
void check(double T, double P) {
EXPECT_CLOSE(gas.temperature(), T, 1e-9);
EXPECT_CLOSE(gas.pressure(), P, 1e-9);
for (size_t i = 0; i < gas.nElements(); i++) {
EXPECT_CLOSE(Yelem[i], gas.elementalMassFraction(i), 1e-8);
}
vector_fp mu(gas.nSpecies());
gas.getChemPotentials(&mu[0]);
double mu_C = mu[gas.speciesIndex("C")];
double mu_H = mu[gas.speciesIndex("H")];
double mu_O = mu[gas.speciesIndex("O")];
double mu_N = mu[gas.speciesIndex("N")];
double mu_Ar = mu[gas.speciesIndex("AR")];
gas.getMoleFractions(&X[0]);
for (size_t k = 0; k < gas.nSpecies(); k++) {
if (X[k] < 1e-15) {
continue;
}
shared_ptr<Species> s = gas.species(k);
double muk = mu_C * getValue(s->composition, std::string("C"), 0.0) +
mu_H * getValue(s->composition, std::string("H"), 0.0) +
mu_O * getValue(s->composition, std::string("O"), 0.0) +
mu_N * getValue(s->composition, std::string("N"), 0.0) +
mu_Ar * getValue(s->composition, std::string("AR"), 0.0);
EXPECT_CLOSE(muk, mu[k], 1e-7);
}
}
void check_CH4_N2(const std::string& solver) {
for (int i = 0; i < 5; i++) {
double T = 500 + 300 * i;
gas.setState_TPX(T, OneAtm, "CH4:3, N2:2");
save_elemental_mole_fractions();
gas.equilibrate("TP", solver);
check(T, OneAtm);
}
}
void check_O2_N2(const std::string& solver) {
for (int i = 0; i < 5; i++) {
double T = 500 + 300 * i;
gas.setState_TPX(T, OneAtm, "O2:3, N2:2");
save_elemental_mole_fractions();
gas.equilibrate("TP", solver);
check(T, OneAtm);
}
}
void check_CH4_O2_N2(const std::string& solver) {
for (int i = 0; i < 6; i++) {
double T = 500 + 300 * i;
gas.setState_TPX(T, OneAtm, "CH4:3, O2:3, N2:4");
save_elemental_mole_fractions();
gas.equilibrate("TP", solver);
check(T, OneAtm);
}
}
void check_CH4_O2(const std::string& solver) {
for (int i = 0; i < 5; i++) {
compositionMap comp;
comp["CH4"] = i * 0.6 / 5.0;
comp["O2"] = 1.0 - i * 0.6 / 5.0;
comp["N2"] = 0.2;
for (int j = 0; j < 8; j++) {
double P = std::pow(10.0, j) * 1e-2;
for (int k = 0; k < 10; k++) {
double T = 300 + 250 * k;
gas.setState_TPX(T, P, "CH4:1, O2:1");
save_elemental_mole_fractions();
gas.equilibrate("TP", solver);
check(T, P);
}
}
}
}
IdealGasPhase gas;
vector_fp X;
vector_fp Yelem;
};
TEST_F(GriMatrix, ChemEquil_CH4_N2) { check_CH4_N2("element_potential"); }
TEST_F(GriMatrix, ChemEquil_O2_N2) { check_O2_N2("element_potential"); }
TEST_F(GriMatrix, ChemEquil_CH4_O2_N2) { check_CH4_O2_N2("element_potential"); }
TEST_F(GriMatrix, ChemEquil_CH4_O2) { check_CH4_O2("element_potential"); }
TEST_F(GriMatrix, MultiPhase_CH4_N2) { check_CH4_N2("gibbs"); }
TEST_F(GriMatrix, MultiPhase_O2_N2) { check_O2_N2("gibbs"); }
TEST_F(GriMatrix, MultiPhase_CH4_O2_N2) { check_CH4_O2_N2("gibbs"); }
TEST_F(GriMatrix, DISABLED_MultiPhase_CH4_O2) { check_CH4_O2("gibbs"); }
TEST_F(GriMatrix, VcsNonideal_CH4_N2) { check_CH4_N2("vcs"); }
TEST_F(GriMatrix, VcsNonideal_O2_N2) { check_O2_N2("vcs"); }
TEST_F(GriMatrix, VcsNonideal_CH4_O2_N2) { check_CH4_O2_N2("vcs"); }
TEST_F(GriMatrix, VcsNonideal_CH4_O2) { check_CH4_O2("vcs"); }
int main(int argc, char** argv)
{
printf("Running main() from equil_gas.cpp\n");

View file

@ -1,182 +0,0 @@
/*
* Copyright 2002 California Institute of Technology
*/
#include "cantera/IdealGasMix.h"
using namespace std;
using namespace Cantera;
int main(int argc, char** argv)
{
#ifdef _MSC_VER
_set_output_format(_TWO_DIGIT_EXPONENT);
#endif
int numSucc = 0;
int numFail = 0;
try {
double T = 500.;
string solver = "auto";
IdealGasMix g("gri30.xml", "gri30_mix");
double pres = OneAtm;
size_t kk = g.nSpecies();
vector_fp Xmol(kk, 0.0);
size_t iCH4 = g.speciesIndex("CH4");
size_t iO2 = g.speciesIndex("O2");
size_t iN2 = g.speciesIndex("N2");
/*
* Do an initial calculation that can be debugged
* easily
*/
Xmol[iCH4] = 0.6;
Xmol[iO2] = 0.0;
Xmol[iN2] = 0.4;
g.setState_TPX(T, pres, DATA_PTR(Xmol));
try {
g.equilibrate("TP", solver);
cout << g.report(true, 0.0);
} catch (CanteraError& err) {
cout << g.report(true, 0.0);
std::cerr << err.what() << std::endl;
cerr << "ERROR: equilibration step failed at "
<< " T = " << T
<< " Pres = " << pres
<< endl;
cout << "ERROR: equilibration step failed at "
<< " T = " << T
<< " Pres = " << pres
<< endl;
exit(-1);
}
/*
* Do an initial calculation that can be debugged
* easily
*/
Xmol[iCH4] = 0.0;
Xmol[iO2] = 0.6;
Xmol[iN2] = 0.4;
g.setState_TPX(T, pres, DATA_PTR(Xmol));
try {
g.equilibrate("TP", solver);
cout << g.report(true, 0.0);
} catch (CanteraError& err) {
cout << g.report(true, 0.0);
std::cerr << err.what() << std::endl;
cerr << "ERROR: equilibration step failed at "
<< " T = " << T
<< " Pres = " << pres
<< endl;
cout << "ERROR: equilibration step failed at "
<< " T = " << T
<< " Pres = " << pres
<< endl;
exit(-1);
}
/*
* Do an initial calculation that can be debugged
* easily
*/
Xmol[iCH4] = 0.3;
Xmol[iO2] = 0.3;
Xmol[iN2] = 0.4;
T = 2000.;
pres = OneAtm;
g.setState_TPX(T, pres, DATA_PTR(Xmol));
try {
g.equilibrate("TP", solver);
cout << g.report(true, 0.0);
} catch (CanteraError& err) {
cout << g.report(true, 0.0);
std::cerr << err.what() << std::endl;
cerr << "ERROR: equilibration step failed at "
<< " T = " << T
<< " Pres = " << pres
<< endl;
cout << "ERROR: equilibration step failed at "
<< " T = " << T
<< " Pres = " << pres
<< endl;
exit(-1);
}
/*
* Do an initial calculation that can be debugged
* easily
*/
Xmol[iCH4] = 0.3;
Xmol[iO2] = 0.3;
Xmol[iN2] = 0.0;
T = 2000.;
pres = 1.0;
g.setState_TPX(T, pres, DATA_PTR(Xmol));
try {
g.equilibrate("TP", solver);
cout << g.report(true, 0.0);
} catch (CanteraError& err) {
cout << g.report(true, 0.0);
std::cerr << err.what() << std::endl;
cerr << "ERROR: equilibration step failed at "
<< " T = " << T
<< " Pres = " << pres
<< endl;
cout << "ERROR: equilibration step failed at "
<< " T = " << T
<< " Pres = " << pres
<< endl;
exit(-1);
}
/*
* OK do the matrix.
*/
bool showResults = false;
for (int iS = 0; iS < 6; iS++) {
Xmol[iCH4] = iS * 0.6 / 5.0;
Xmol[iO2] = 1.0 - Xmol[iCH4] - Xmol[iN2];
for (int iP = 0; iP < 10; iP++) {
pres = pow(10.0, iP) *1.0E-2;
for (int iT = 0; iT < 25; iT++) {
double T = 100. * iT + 300.;
/*
* Reset mole fractions to a base state
*/
g.setState_TPX(T, pres, DATA_PTR(Xmol));
try {
g.equilibrate("TP", solver);
numSucc++;
} catch (CanteraError& err) {
std::cerr << err.what() << std::endl;
cerr << "ERROR: equilibration step failed at "
<< " T = " << T
<< " Pres = " << pres
<< endl;
cout << "ERROR: equilibration step failed at "
<< " T = " << T
<< " Pres = " << pres
<< endl;
numFail++;
}
if (showResults) {
cout << g.report(true, 0.0);
}
}
}
}
cout << "NUMBER OF SUCCESSES = " << numSucc << endl;
cout << "NUMBER OF FAILURES = " << numFail << endl;
return numFail;
} catch (CanteraError& err) {
std::cerr << err.what() << std::endl;
cerr << "ERROR: program terminating due to unforeseen circumstances." << endl;
return -1;
}
}

View file

@ -1,290 +0,0 @@
gri30_mix:
temperature 500 K
pressure 101325 Pa
density 0.507719 kg/m^3
mean mol. weight 20.8311 amu
1 kg 1 kmol
----------- ------------
enthalpy -1.7981e+06 -3.746e+07 J
internal energy -1.99767e+06 -4.161e+07 J
entropy 10204.8 2.126e+05 J/K
Gibbs function -6.90049e+06 -1.437e+08 J
heat capacity c_p 1908.33 3.975e+04 J/K
heat capacity c_v 1509.19 3.144e+04 J/K
X Y Chem. Pot. / RT
------------- ------------ ------------
H2 0.000116242 1.1249e-05 -25.1739
H 7.50073e-23 3.62934e-24 -12.587
O 0 0
O2 0 0
OH 0 0
H2O 0 0
HO2 0 0
H2O2 0 0
C 2.51101e-63 1.44783e-63 8.95153
CH 5.65939e-55 3.53699e-55 -3.63544
CH2 2.17058e-38 1.46159e-38 -16.2224
CH2(S) 1.35699e-42 9.13749e-43 -16.2224
CH3 3.27367e-18 2.36277e-18 -28.8094
CH4 0.599767 0.461903 -41.3964
CO 0 0
CO2 0 0
HCO 0 0
CH2O 0 0
CH2OH 0 0
CH3O 0 0
CH3OH 0 0
C2H 3.61997e-46 4.34965e-46 5.31609
C2H2 6.0347e-17 7.5431e-17 -7.27089
C2H3 3.63018e-28 4.71321e-28 -19.8579
C2H4 1.47659e-08 1.98857e-08 -32.4448
C2H5 1.95525e-19 2.7278e-19 -45.0318
C2H6 0.000116084 0.000167567 -57.6188
HCCO 0 0
CH2CO 0 0
HCCOH 0 0
N 2.91172e-47 1.95783e-47 -12.1739
NH 3.97359e-39 2.8641e-39 -24.7609
NH2 1.27658e-26 9.81909e-27 -37.3479
NH3 2.50763e-07 2.05013e-07 -49.9348
NNH 6.80126e-31 9.47538e-31 -36.9348
NO 0 0
NO2 0 0
N2O 0 0
HNO 0 0
CN 3.39689e-37 4.24268e-37 -3.22238
HCN 1.71533e-10 2.22543e-10 -15.8094
H2CN 6.1207e-27 8.237e-27 -28.3963
HCNN 8.8321e-48 1.73972e-47 -27.9833
HCNO 0 0
HOCN 0 0
HNCO 0 0
NCO 0 0
N2 0.4 0.537918 -24.3478
AR 0 0
C3H7 3.21265e-22 6.64529e-22 -61.2542
C3H8 2.52213e-07 5.339e-07 -73.8412
CH2CHO 0 0
CH3CHO 0 0
gri30_mix:
temperature 500 K
pressure 101325 Pa
density 0.741058 kg/m^3
mean mol. weight 30.4047 amu
1 kg 1 kmol
----------- ------------
enthalpy 198008 6.02e+06 J
internal energy 61277.4 1.863e+06 J
entropy 7258.13 2.207e+05 J/K
Gibbs function -3.43106e+06 -1.043e+08 J
heat capacity c_p 1003.28 3.05e+04 J/K
heat capacity c_v 729.823 2.219e+04 J/K
X Y Chem. Pot. / RT
------------- ------------ ------------
H2 0 0
H 0 0
O 8.98043e-24 4.72564e-24 -12.7954
O2 0.6 0.631458 -25.5908
OH 0 0
H2O 0 0
HO2 0 0
H2O2 0 0
C 0 0
CH 0 0
CH2 0 0
CH2(S) 0 0
CH3 0 0
CH4 0 0
CO 0 0
CO2 0 0
HCO 0 0
CH2O 0 0
CH2OH 0 0
CH3O 0 0
CH3OH 0 0
C2H 0 0
C2H2 0 0
C2H3 0 0
C2H4 0 0
C2H5 0 0
C2H6 0 0
HCCO 0 0
CH2CO 0 0
HCCOH 0 0
N 2.91172e-47 1.34136e-47 -12.1739
NH 0 0
NH2 0 0
NH3 0 0
NNH 0 0
NO 6.40857e-10 6.32457e-10 -24.9693
NO2 6.36105e-08 9.62495e-08 -37.7647
N2O 1.20106e-13 1.73862e-13 -37.1432
HNO 0 0
CN 0 0
HCN 0 0
H2CN 0 0
HCNN 0 0
HCNO 0 0
HOCN 0 0
HNCO 0 0
NCO 0 0
N2 0.4 0.368542 -24.3478
AR 0 0
C3H7 0 0
C3H8 0 0
CH2CHO 0 0
CH3CHO 0 0
gri30_mix:
temperature 2000 K
pressure 101325 Pa
density 0.120021 kg/m^3
mean mol. weight 19.6972 amu
1 kg 1 kmol
----------- ------------
enthalpy -1.14349e+06 -2.252e+07 J
internal energy -1.98772e+06 -3.915e+07 J
entropy 12855 2.532e+05 J/K
Gibbs function -2.68534e+07 -5.289e+08 J
heat capacity c_p 2004.72 3.949e+04 J/K
heat capacity c_v 1582.61 3.117e+04 J/K
X Y Chem. Pot. / RT
------------- ------------ ------------
H2 0.262869 0.0269029 -20.8151
H 0.000834397 4.26975e-05 -10.4075
O 1.45323e-07 1.18041e-07 -22.8166
O2 4.72395e-08 7.67422e-08 -45.6333
OH 6.26815e-05 5.41216e-05 -33.2242
H2O 0.198012 0.181103 -43.6317
HO2 4.13259e-11 6.925e-11 -56.0408
H2O2 8.04483e-11 1.38924e-10 -66.4484
C 9.37794e-16 5.71849e-16 -13.1535
CH 2.5887e-15 1.71101e-15 -23.561
CH2 2.34345e-13 1.66883e-13 -33.9686
CH2(S) 1.13506e-14 8.08304e-15 -33.9686
CH3 5.70093e-11 4.3515e-11 -44.3761
CH4 9.12163e-10 7.42928e-10 -54.7837
CO 0.198076 0.281674 -35.9701
CO2 0.0325892 0.0728146 -58.7868
HCO 5.95957e-08 8.77975e-08 -46.3777
CH2O 2.14619e-08 3.27164e-08 -56.7852
CH2OH 3.20751e-13 5.05364e-13 -67.1928
CH3O 4.25882e-15 6.71004e-15 -67.1928
CH3OH 7.49906e-13 1.2199e-12 -77.6003
C2H 1.13972e-17 1.44828e-17 -36.7145
C2H2 1.92206e-13 2.54078e-13 -47.122
C2H3 8.41511e-18 1.15546e-17 -57.5296
C2H4 3.42916e-16 4.88397e-16 -67.9371
C2H5 2.15437e-20 3.17861e-20 -78.3447
C2H6 4.61488e-20 7.04504e-20 -88.7522
HCCO 5.83197e-15 1.2148e-14 -59.5311
CH2CO 2.60466e-13 5.5588e-13 -69.9387
HCCOH 1.13765e-16 2.42793e-16 -69.9387
N 5.02707e-10 3.57477e-10 -14.0552
NH 1.50719e-09 1.14889e-09 -24.4627
NH2 1.44254e-08 1.17343e-08 -34.8703
NH3 1.53645e-06 1.32844e-06 -45.2778
NNH 6.16147e-10 9.07817e-10 -38.5179
NO 2.27843e-06 3.47089e-06 -36.8718
NO2 1.87305e-12 4.37477e-12 -59.6885
N2O 8.05639e-11 1.80018e-10 -50.9271
HNO 3.16512e-10 4.98361e-10 -47.2794
CN 1.06666e-11 1.40893e-11 -27.2087
HCN 1.6694e-07 2.29051e-07 -37.6162
H2CN 2.03354e-13 2.89418e-13 -48.0238
HCNN 9.59376e-19 1.99853e-18 -51.6714
HCNO 2.14902e-15 4.69415e-15 -60.4329
HOCN 7.61903e-11 1.66424e-10 -60.4329
HNCO 4.82851e-08 1.0547e-07 -60.4329
NCO 4.83294e-11 1.03094e-10 -50.0253
N2 0.307552 0.437403 -28.1104
AR 0 0
C3H7 2.6613e-30 5.82171e-30 -112.313
C3H8 5.43498e-30 1.21674e-29 -122.721
CH2CHO 6.11141e-18 1.33555e-17 -80.3462
CH3CHO 3.34175e-17 7.47388e-17 -90.7538
gri30_mix:
temperature 2000 K
pressure 1 Pa
density 8.11603e-07 kg/m^3
mean mol. weight 13.4961 amu
1 kg 1 kmol
----------- ------------
enthalpy 1.84736e+06 2.493e+07 J
internal energy 615230 8.303e+06 J
entropy 24280.4 3.277e+05 J/K
Gibbs function -4.67135e+07 -6.304e+08 J
heat capacity c_p 2638.92 3.562e+04 J/K
heat capacity c_v 2022.85 2.73e+04 J/K
X Y Chem. Pot. / RT
------------- ------------ ------------
H2 0.234986 0.0350993 -32.4533
H 0.251121 0.0187546 -16.2267
O 0.0158988 0.0188478 -22.7399
O2 0.00558021 0.0132305 -45.4799
OH 0.0203687 0.0256679 -38.9666
H2O 0.19112 0.255117 -55.1932
HO2 1.44997e-08 3.54612e-08 -61.7065
H2O2 8.38392e-11 2.11303e-10 -77.9332
C 1.04601e-15 9.30911e-16 -24.5704
CH 8.57638e-18 8.27316e-18 -40.797
CH2 2.30606e-18 2.39676e-18 -57.0237
CH2(S) 1.11695e-19 1.16088e-19 -57.0237
CH3 1.6663e-18 1.85628e-18 -73.2503
CH4 7.91906e-20 9.41336e-20 -89.477
CO 0.238548 0.495094 -47.3103
CO2 0.0423771 0.138189 -70.0502
HCO 2.13182e-10 4.58369e-10 -63.5369
CH2O 2.28033e-13 5.07331e-13 -79.7636
CH2OH 1.01226e-20 2.32768e-20 -95.9903
CH3O 1.34404e-22 3.09061e-22 -95.9903
CH3OH 7.02945e-23 1.66892e-22 -112.217
C2H 4.15655e-25 7.70877e-25 -65.3674
C2H2 2.08207e-23 4.01692e-23 -81.594
C2H3 2.70758e-30 5.42591e-30 -97.8207
C2H4 3.27719e-31 6.81215e-31 -114.047
C2H5 6.11544e-38 1.31686e-37 -130.274
C2H6 3.89099e-40 8.66922e-40 -146.501
HCCO 2.29649e-22 6.98154e-22 -88.1073
CH2CO 3.04645e-23 9.489e-23 -104.334
HCCOH 1.33061e-26 4.14454e-26 -104.334
N 0 0
NH 0 0
NH2 0 0
NH3 0 0
NNH 0 0
NO 0 0
NO2 0 0
N2O 0 0
HNO 0 0
CN 0 0
HCN 0 0
H2CN 0 0
HCNN 0 0
HCNO 0 0
HOCN 0 0
HNCO 0 0
NCO 0 0
N2 0 0
AR 0 0
C3H7 7.33667e-58 2.34236e-57 -187.298
C3H8 4.45038e-60 1.4541e-59 -203.524
CH2CHO 2.12313e-30 6.77164e-30 -120.561
CH3CHO 3.44828e-32 1.12557e-31 -136.787
NUMBER OF SUCCESSES = 1500
NUMBER OF FAILURES = 0

View file

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

View file

@ -220,8 +220,6 @@ CompileAndTest('ISSPTester2', 'cathermo/VPissp',
'ISSPTester2', 'output_blessed.txt')
CompileAndTest('wtWater', 'cathermo/wtWater',
'wtWater', 'output_blessed.txt')
CompileAndTest('ChemEquil_gri_matrix',
'ChemEquil_gri_matrix', 'gri_matrix', 'output_blessed.txt')
CompileAndTest('ChemEquil_gri_pairs',
'ChemEquil_gri_pairs', 'gri_pairs', 'output_blessed.txt')
CompileAndTest('ChemEquil_ionizedGas',