Skip to content

Commit

Permalink
[Test] Add extensible test suite for thermo consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
speth authored and ischoegl committed Jun 10, 2022
1 parent 399142e commit 700f873
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
15 changes: 15 additions & 0 deletions test/data/consistency-cases.yaml
@@ -0,0 +1,15 @@
# Parameters for test cases defined in test/thermo/consistency.cpp

ideal-gas-h2o2:
input: {file: h2o2.yaml}
states:
- {T: 300, P: 101325, X: {H2: 0.1, O2: 0.7, H2O2: 0.1}}
- {T: 300, P: 101325, Y: {H2: 0.1, O2: 0.7, H2O2: 0.1}}
- {T: 400, density: 5 g/cm^3 , X: {H2: 0.1, O2: 0.7, H2O2: 0.1}}

redlich-kwong:
input: {file: co2_RK_example.yaml}
states:
- {T: 300, P: 101325, X: {CO2: 0.7, CH4: 0.2, H2O: 0.1}}
- {T: 300, P: 200 bar, X: {CO2: 0.3, CH4: 0.5, H2O: 0.2}}
- {T: 600, P: 200 bar, X: {CO2: 0.4, CH4: 0.2, H2O: 0.4}}
69 changes: 69 additions & 0 deletions test/thermo/consistency.cpp
@@ -0,0 +1,69 @@
#include "gtest/gtest.h"
#include "cantera/thermo/ThermoPhase.h"
#include "cantera/thermo/ThermoFactory.h"
#include "cantera/base/Solution.h"

using namespace std;

namespace Cantera
{

vector<AnyMap> getStates(const string& name) {
static AnyMap cases = AnyMap::fromYamlFile("consistency-cases.yaml");
return cases[name]["states"].asVector<AnyMap>();
}

AnyMap getSetup(const string& name) {
static AnyMap cases = AnyMap::fromYamlFile("consistency-cases.yaml");
return cases[name]["input"].as<AnyMap>();
}

class TestConsistency : public testing::TestWithParam<std::tuple<AnyMap, AnyMap>>
{
public:
TestConsistency() {
auto param = GetParam();
AnyMap inp = get<0>(param);
AnyMap state = get<1>(param);
pair<string, string> key = {inp["file"].asString(), inp.getString("phase", "")};
if (cache.count(key) == 0) {
cache[key].reset(newPhase(key.first, key.second));
}
phase = cache[key];
phase->setState(state);
}
static map<pair<string, string>, shared_ptr<ThermoPhase>> cache;
shared_ptr<ThermoPhase> phase;
};

map<pair<string, string>, shared_ptr<ThermoPhase>> TestConsistency::cache = {};

TEST_P(TestConsistency, h_eq_u_plus_Pv) {
double h = phase->enthalpy_mole();
double u = phase->intEnergy_mole();
double v = phase->molarVolume();
double p = phase->pressure();
EXPECT_NEAR(h, u + p*v, 1e-10*p*v);
}

TEST_P(TestConsistency, g_eq_h_minus_Ts) {
double g = phase->gibbs_mole();
double h = phase->enthalpy_mole();
double T = phase->temperature();
double s = phase->entropy_mole();
EXPECT_NEAR(g, h - T*s, 1e-10*T*s);
}

INSTANTIATE_TEST_SUITE_P(IdealGas, TestConsistency,
testing::Combine(
testing::Values(getSetup("ideal-gas-h2o2")),
testing::ValuesIn(getStates("ideal-gas-h2o2")))
);

INSTANTIATE_TEST_SUITE_P(RedlichKwong, TestConsistency,
testing::Combine(
testing::Values(getSetup("redlich-kwong")),
testing::ValuesIn(getStates("redlich-kwong")))
);

}

0 comments on commit 700f873

Please sign in to comment.