Skip to content

Commit

Permalink
[UnitTests] Update tests (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Mar 16, 2022
1 parent d3e91ff commit 1270d66
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 49 deletions.
4 changes: 2 additions & 2 deletions interfaces/cython/cantera/test/test_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ class TestThreeBody(HydrogenOxygen, utilities.CanteraTest):
# Three body reaction with default efficiency
rxn_idx = 1
equation = "H + O + M <=> OH + M"
rate_type = "Arrhenius"
rate_type = "three-body-Arrhenius"

@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -463,7 +463,7 @@ class TestThreeBodyNoDefault(EdgeCases, utilities.CanteraTest):
# Three body reaction without default efficiency
rxn_idx = 4
equation = "H + O + M <=> OH + M"
rate_type = "Arrhenius"
rate_type = "three-body-Arrhenius"

@classmethod
def setUpClass(cls):
Expand Down
17 changes: 9 additions & 8 deletions interfaces/cython/cantera/test/test_kinetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ def test_legacy_reaction_rate(self):
ct.make_deprecation_warnings_fatal() # re-enable fatal deprecation warnings

fwd_rates = self.phase.forward_rate_constants
ix_3b = np.array([r.reaction_type == "three-body" for r in self.phase.reactions()])
ix_3b = np.array([r.rate.type == "three-body-Arrhenius" for r in self.phase.reactions()])
ix_other = ix_3b == False

self.assertArrayNear(fwd_rates_legacy[ix_other], fwd_rates[ix_other])
self.assertFalse((fwd_rates_legacy[ix_3b] == fwd_rates[ix_3b]).any())

def test_reaction_type(self):
self.assertIn(self.phase.reaction(0).reaction_type, "three-body")
self.assertIn(self.phase.reaction(0).reaction_type, "reaction")
self.assertIn(self.phase.reaction(0).rate.type, "three-body-Arrhenius")
self.assertIn(self.phase.reaction(2).reaction_type, "reaction")
self.assertIn(self.phase.reaction(2).rate.type, "Arrhenius")
self.assertEqual(self.phase.reaction(21).reaction_type, "falloff")
Expand Down Expand Up @@ -992,10 +993,10 @@ def test_from_yaml(self):
" efficiencies: {H2: 2.4, H2O: 15.4, AR: 0.83}}",
self.gas)

self.assertTrue(isinstance(r, ct.ThreeBodyReaction))
self.assertTrue(isinstance(r.rate, ct.ThreeBodyArrheniusRate))
self.assertEqual(r.reactants['O'], 2)
self.assertEqual(r.products['O2'], 1)
self.assertEqual(r.efficiencies['H2O'], 15.4)
self.assertEqual(r.rate.efficiencies["H2O"], 15.4)
self.assertEqual(r.rate.temperature_exponent, -1.0)
self.assertIn('O', r)
self.assertIn('O2', r)
Expand Down Expand Up @@ -1127,9 +1128,9 @@ def test_negative_A_falloff(self):
self.assertLess(gas.forward_rate_constants, 0)

def test_threebody(self):
r = ct.ThreeBodyReaction({"O":1, "H":1}, {"OH":1},
ct.ArrheniusRate(5e11, -1.0, 0.0))
r.efficiencies = {"AR":0.7, "H2":2.0, "H2O":6.0}
r = ct.Reaction({"O":1, "H":1}, {"OH":1},
ct.ThreeBodyArrheniusRate(5e11, -1.0, 0.0))
r.rate.efficiencies = {"AR":0.7, "H2":2.0, "H2O":6.0}

gas2 = ct.Solution(thermo='IdealGas', kinetics='GasKinetics',
species=self.species, reactions=[r])
Expand Down Expand Up @@ -1469,7 +1470,7 @@ def test_modify_third_body(self):

A2 = 1.7 * A1
b2 = b1 - 0.1
R.rate = ct.ArrheniusRate(A2, b2, 0.0)
R.rate = ct.ThreeBodyArrheniusRate(A2, b2, 0.0)
gas.modify_reaction(5, R)
kf2 = gas.forward_rate_constants[5]
self.assertNear((A2*T**b2) / (A1*T**b1), kf2/kf1)
Expand Down
65 changes: 44 additions & 21 deletions interfaces/cython/cantera/test/test_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def test_implicit_three_body(self):
rate-constant: {A: 2.08e+19, b: -1.24, Ea: 0.0}
"""
rxn1 = ct.Reaction.from_yaml(yaml1, self.gas)
self.assertEqual(rxn1.reaction_type, "three-body")
self.assertEqual(rxn1.default_efficiency, 0.)
self.assertEqual(rxn1.efficiencies, {"O2": 1})
assert rxn1.rate.type == "three-body-Arrhenius"
assert rxn1.rate.default_efficiency == 0.
assert rxn1.rate.efficiencies == {"O2": 1}

yaml2 = """
equation: H + O2 + M <=> HO2 + M
Expand All @@ -36,8 +36,9 @@ def test_implicit_three_body(self):
efficiencies: {O2: 1.0}
"""
rxn2 = ct.Reaction.from_yaml(yaml2, self.gas)
self.assertEqual(rxn1.efficiencies, rxn2.efficiencies)
self.assertEqual(rxn1.default_efficiency, rxn2.default_efficiency)
assert rxn2.rate.type == "three-body-Arrhenius"
assert rxn1.rate.efficiencies == rxn2.rate.efficiencies
assert rxn1.rate.default_efficiency == rxn2.rate.default_efficiency

def test_duplicate(self):
# @todo simplify this test
Expand All @@ -46,25 +47,26 @@ def test_duplicate(self):
species=self.gas.species(), reactions=[])

yaml1 = """
equation: H + O2 + H2O <=> HO2 + H2O
equation: H + O2 + M <=> HO2 + M
rate-constant: {A: 1.126e+19, b: -0.76, Ea: 0.0}
type: three-body
default-efficiency: 0
efficiencies: {H2O: 1}
"""
rxn1 = ct.Reaction.from_yaml(yaml1, gas1)
assert rxn1.rate.type == "three-body-Arrhenius"

yaml2 = """
equation: H + O2 + M <=> HO2 + M
equation: H + O2 + H2O <=> HO2 + H2O
rate-constant: {A: 1.126e+19, b: -0.76, Ea: 0.0}
type: three-body
default-efficiency: 0
efficiencies: {H2O: 1}
"""
rxn2 = ct.Reaction.from_yaml(yaml2, gas1)
assert rxn2.rate.type == "three-body-Arrhenius"

self.assertEqual(rxn1.reaction_type, rxn2.reaction_type)
self.assertEqual(rxn1.reactants, rxn2.reactants)
self.assertEqual(rxn1.products, rxn2.products)
self.assertEqual(rxn1.efficiencies, rxn2.efficiencies)
self.assertEqual(rxn1.default_efficiency, rxn2.default_efficiency)
assert rxn1.rate.efficiencies == rxn2.rate.efficiencies
assert rxn1.rate.default_efficiency == rxn2.rate.default_efficiency

gas1.add_reaction(rxn1)
gas1.add_reaction(rxn2)
Expand Down Expand Up @@ -1286,19 +1288,40 @@ def test_efficiencies(self):
self.assertEqual(rxn.efficiencies, self._kwargs["efficiencies"])


class TestThreeBody(TestThreeBody2):
class TestThreeBody(ReactionTests, utilities.CanteraTest):
# test updated version of three-body reaction

_cls = ct.Reaction
_equation = "2 O + M <=> O2 + M"
_rate = {"A": 1.2e11, "b": -1.0, "Ea": 0.0}
_kwargs = {"efficiencies": {"H2": 2.4, "H2O": 15.4, "AR": 0.83}}
_index = 1
_legacy = False
_rxn_type = "three-body"
_rate_type = "Arrhenius"
_rxn_type = "reaction"
_rate_type = "three-body-Arrhenius"
_rate_cls = ct.ThreeBodyArrheniusRate
_yaml = """
equation: 2 O + M <=> O2 + M
type: three-body
type: three-body-Arrhenius
rate-constant: {A: 1.2e+11, b: -1.0, Ea: 0.0 cal/mol}
efficiencies: {H2: 2.4, H2O: 15.4, AR: 0.83}
"""

@classmethod
def setUpClass(cls):
ReactionTests.setUpClass()
cls._rate_obj = ct.ThreeBodyArrheniusRate(1.2e11, -1.0, 0.0, **cls._kwargs)

def from_parts(self):
rxn = ReactionTests.from_parts(self)
rxn.rate.efficiencies = self._kwargs["efficiencies"]
return rxn

def test_efficiencies(self):
# check efficiencies
rxn = self.from_parts()
self.assertEqual(rxn.rate.efficiencies, self._kwargs["efficiencies"])


class TestImplicitThreeBody(TestThreeBody):
# test three-body reactions with explicit collision parther
Expand All @@ -1314,15 +1337,15 @@ class TestImplicitThreeBody(TestThreeBody):

def from_parts(self):
rxn = ReactionTests.from_parts(self)
rxn.efficiencies = {"O2": 1.}
rxn.default_efficiency = 0
rxn.rate.efficiencies = {"O2": 1.}
rxn.rate.default_efficiency = 0
return rxn

def test_efficiencies(self):
# overload of default tester
rxn = self.from_rate(self._rate_obj)
self.assertEqual(rxn.efficiencies, {"O2": 1.})
self.assertEqual(rxn.default_efficiency, 0.)
self.assertEqual(rxn.rate.efficiencies, {"O2": 1.})
self.assertEqual(rxn.rate.default_efficiency, 0.)


class TestTwoTempPlasma(ReactionTests, utilities.CanteraTest):
Expand Down
21 changes: 9 additions & 12 deletions test/kinetics/kineticsFromScratch3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ TEST_F(KineticsFromScratch3, add_three_body_reaction)
// efficiencies: {AR: 0.83, H2: 2.4, H2O: 15.4}
Composition reac = parseCompString("O:2");
Composition prod = parseCompString("O2:1");
ArrheniusRate rate(1.2e11, -1.0, 0.0);
ThirdBody tbody;
tbody.efficiencies = parseCompString("AR:0.83 H2:2.4 H2O:15.4");
auto R = make_shared<ThreeBodyReaction3>(reac, prod, rate, tbody);
auto rate = make_shared<ThreeBodyArrheniusRate>(1.2e11, -1.0, 0.0);
rate->setEfficiencies(parseCompString("AR:0.83 H2:2.4 H2O:15.4"));
auto R = make_shared<Reaction>(reac, prod, rate);

kin.addReaction(R);
check_rates(1);
Expand All @@ -86,10 +85,9 @@ TEST_F(KineticsFromScratch3, undefined_third_body)
{
Composition reac = parseCompString("O:2");
Composition prod = parseCompString("O2:1");
ArrheniusRate rate(1.2e11, -1.0, 0.0);
ThirdBody tbody;
tbody.efficiencies = parseCompString("H2:0.1 CO2:0.83");
auto R = make_shared<ThreeBodyReaction3>(reac, prod, rate, tbody);
auto rate = make_shared<ThreeBodyArrheniusRate>(1.2e11, -1.0, 0.0);
rate->setEfficiencies(parseCompString("H2:0.1 CO2:0.83"));
auto R = make_shared<Reaction>(reac, prod, rate);

ASSERT_THROW(kin.addReaction(R), CanteraError);
}
Expand All @@ -98,10 +96,9 @@ TEST_F(KineticsFromScratch3, skip_undefined_third_body)
{
Composition reac = parseCompString("O:2");
Composition prod = parseCompString("O2:1");
ArrheniusRate rate(1.2e11, -1.0, 0.0);
ThirdBody tbody;
tbody.efficiencies = parseCompString("H2:0.1 CO2:0.83");
auto R = make_shared<ThreeBodyReaction3>(reac, prod, rate, tbody);
auto rate = make_shared<ThreeBodyArrheniusRate>(1.2e11, -1.0, 0.0);
rate->setEfficiencies(parseCompString("H2:0.1 CO2:0.83"));
auto R = make_shared<Reaction>(reac, prod, rate);

kin.skipUndeclaredThirdBodies(true);
kin.addReaction(R);
Expand Down
11 changes: 5 additions & 6 deletions test/kinetics/kineticsFromYaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ TEST(Reaction, ThreeBodyFromYaml3)

auto R = newReaction(rxn, *(sol->kinetics()));
EXPECT_EQ(R->reactants.count("M"), (size_t) 0);
EXPECT_EQ(R->type(), "three-body");
EXPECT_DOUBLE_EQ(R->thirdBody()->efficiencies["H2O"], 5.0);
EXPECT_DOUBLE_EQ(R->thirdBody()->default_efficiency, 1.0);

const auto& rate = std::dynamic_pointer_cast<ArrheniusRate>(R->rate());
EXPECT_EQ(R->type(), "reaction");
const auto& rate = std::dynamic_pointer_cast<ThreeBodyArrheniusRate>(R->rate());
EXPECT_DOUBLE_EQ(rate->efficiencies()["H2O"], 5.0);
EXPECT_DOUBLE_EQ(rate->defaultEfficiency(), 1.0);
EXPECT_DOUBLE_EQ(rate->preExponentialFactor(), 1.2e11);
}

Expand Down Expand Up @@ -539,7 +538,7 @@ TEST_F(ReactionToYaml, threeBody)
soln = newSolution("h2o2.yaml", "", "None");
soln->thermo()->setState_TPY(1000, 2e5, "H2:1.0, O2:0.5, O:1e-8, OH:3e-8, H:2e-7");
duplicateReaction(1);
EXPECT_TRUE(std::dynamic_pointer_cast<ThreeBodyReaction3>(duplicate));
EXPECT_TRUE(std::dynamic_pointer_cast<ThreeBodyArrheniusRate>(duplicate->rate()));
compareReactions();
}

Expand Down

0 comments on commit 1270d66

Please sign in to comment.