Skip to content

Commit 1270d66

Browse files
committed
[UnitTests] Update tests (WIP)
1 parent d3e91ff commit 1270d66

File tree

5 files changed

+69
-49
lines changed

5 files changed

+69
-49
lines changed

interfaces/cython/cantera/test/test_jacobian.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ class TestThreeBody(HydrogenOxygen, utilities.CanteraTest):
395395
# Three body reaction with default efficiency
396396
rxn_idx = 1
397397
equation = "H + O + M <=> OH + M"
398-
rate_type = "Arrhenius"
398+
rate_type = "three-body-Arrhenius"
399399

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

468468
@classmethod
469469
def setUpClass(cls):

interfaces/cython/cantera/test/test_kinetics.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,15 @@ def test_legacy_reaction_rate(self):
7070
ct.make_deprecation_warnings_fatal() # re-enable fatal deprecation warnings
7171

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

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

7979
def test_reaction_type(self):
80-
self.assertIn(self.phase.reaction(0).reaction_type, "three-body")
80+
self.assertIn(self.phase.reaction(0).reaction_type, "reaction")
81+
self.assertIn(self.phase.reaction(0).rate.type, "three-body-Arrhenius")
8182
self.assertIn(self.phase.reaction(2).reaction_type, "reaction")
8283
self.assertIn(self.phase.reaction(2).rate.type, "Arrhenius")
8384
self.assertEqual(self.phase.reaction(21).reaction_type, "falloff")
@@ -992,10 +993,10 @@ def test_from_yaml(self):
992993
" efficiencies: {H2: 2.4, H2O: 15.4, AR: 0.83}}",
993994
self.gas)
994995

995-
self.assertTrue(isinstance(r, ct.ThreeBodyReaction))
996+
self.assertTrue(isinstance(r.rate, ct.ThreeBodyArrheniusRate))
996997
self.assertEqual(r.reactants['O'], 2)
997998
self.assertEqual(r.products['O2'], 1)
998-
self.assertEqual(r.efficiencies['H2O'], 15.4)
999+
self.assertEqual(r.rate.efficiencies["H2O"], 15.4)
9991000
self.assertEqual(r.rate.temperature_exponent, -1.0)
10001001
self.assertIn('O', r)
10011002
self.assertIn('O2', r)
@@ -1127,9 +1128,9 @@ def test_negative_A_falloff(self):
11271128
self.assertLess(gas.forward_rate_constants, 0)
11281129

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

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

14701471
A2 = 1.7 * A1
14711472
b2 = b1 - 0.1
1472-
R.rate = ct.ArrheniusRate(A2, b2, 0.0)
1473+
R.rate = ct.ThreeBodyArrheniusRate(A2, b2, 0.0)
14731474
gas.modify_reaction(5, R)
14741475
kf2 = gas.forward_rate_constants[5]
14751476
self.assertNear((A2*T**b2) / (A1*T**b1), kf2/kf1)

interfaces/cython/cantera/test/test_reaction.py

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ def test_implicit_three_body(self):
2424
rate-constant: {A: 2.08e+19, b: -1.24, Ea: 0.0}
2525
"""
2626
rxn1 = ct.Reaction.from_yaml(yaml1, self.gas)
27-
self.assertEqual(rxn1.reaction_type, "three-body")
28-
self.assertEqual(rxn1.default_efficiency, 0.)
29-
self.assertEqual(rxn1.efficiencies, {"O2": 1})
27+
assert rxn1.rate.type == "three-body-Arrhenius"
28+
assert rxn1.rate.default_efficiency == 0.
29+
assert rxn1.rate.efficiencies == {"O2": 1}
3030

3131
yaml2 = """
3232
equation: H + O2 + M <=> HO2 + M
@@ -36,8 +36,9 @@ def test_implicit_three_body(self):
3636
efficiencies: {O2: 1.0}
3737
"""
3838
rxn2 = ct.Reaction.from_yaml(yaml2, self.gas)
39-
self.assertEqual(rxn1.efficiencies, rxn2.efficiencies)
40-
self.assertEqual(rxn1.default_efficiency, rxn2.default_efficiency)
39+
assert rxn2.rate.type == "three-body-Arrhenius"
40+
assert rxn1.rate.efficiencies == rxn2.rate.efficiencies
41+
assert rxn1.rate.default_efficiency == rxn2.rate.default_efficiency
4142

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

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

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

63-
self.assertEqual(rxn1.reaction_type, rxn2.reaction_type)
6466
self.assertEqual(rxn1.reactants, rxn2.reactants)
6567
self.assertEqual(rxn1.products, rxn2.products)
66-
self.assertEqual(rxn1.efficiencies, rxn2.efficiencies)
67-
self.assertEqual(rxn1.default_efficiency, rxn2.default_efficiency)
68+
assert rxn1.rate.efficiencies == rxn2.rate.efficiencies
69+
assert rxn1.rate.default_efficiency == rxn2.rate.default_efficiency
6870

6971
gas1.add_reaction(rxn1)
7072
gas1.add_reaction(rxn2)
@@ -1286,19 +1288,40 @@ def test_efficiencies(self):
12861288
self.assertEqual(rxn.efficiencies, self._kwargs["efficiencies"])
12871289

12881290

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

1294+
_cls = ct.Reaction
1295+
_equation = "2 O + M <=> O2 + M"
1296+
_rate = {"A": 1.2e11, "b": -1.0, "Ea": 0.0}
1297+
_kwargs = {"efficiencies": {"H2": 2.4, "H2O": 15.4, "AR": 0.83}}
1298+
_index = 1
12921299
_legacy = False
1293-
_rxn_type = "three-body"
1294-
_rate_type = "Arrhenius"
1300+
_rxn_type = "reaction"
1301+
_rate_type = "three-body-Arrhenius"
1302+
_rate_cls = ct.ThreeBodyArrheniusRate
12951303
_yaml = """
12961304
equation: 2 O + M <=> O2 + M
1297-
type: three-body
1305+
type: three-body-Arrhenius
12981306
rate-constant: {A: 1.2e+11, b: -1.0, Ea: 0.0 cal/mol}
12991307
efficiencies: {H2: 2.4, H2O: 15.4, AR: 0.83}
13001308
"""
13011309

1310+
@classmethod
1311+
def setUpClass(cls):
1312+
ReactionTests.setUpClass()
1313+
cls._rate_obj = ct.ThreeBodyArrheniusRate(1.2e11, -1.0, 0.0, **cls._kwargs)
1314+
1315+
def from_parts(self):
1316+
rxn = ReactionTests.from_parts(self)
1317+
rxn.rate.efficiencies = self._kwargs["efficiencies"]
1318+
return rxn
1319+
1320+
def test_efficiencies(self):
1321+
# check efficiencies
1322+
rxn = self.from_parts()
1323+
self.assertEqual(rxn.rate.efficiencies, self._kwargs["efficiencies"])
1324+
13021325

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

13151338
def from_parts(self):
13161339
rxn = ReactionTests.from_parts(self)
1317-
rxn.efficiencies = {"O2": 1.}
1318-
rxn.default_efficiency = 0
1340+
rxn.rate.efficiencies = {"O2": 1.}
1341+
rxn.rate.default_efficiency = 0
13191342
return rxn
13201343

13211344
def test_efficiencies(self):
13221345
# overload of default tester
13231346
rxn = self.from_rate(self._rate_obj)
1324-
self.assertEqual(rxn.efficiencies, {"O2": 1.})
1325-
self.assertEqual(rxn.default_efficiency, 0.)
1347+
self.assertEqual(rxn.rate.efficiencies, {"O2": 1.})
1348+
self.assertEqual(rxn.rate.default_efficiency, 0.)
13261349

13271350

13281351
class TestTwoTempPlasma(ReactionTests, utilities.CanteraTest):

test/kinetics/kineticsFromScratch3.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ TEST_F(KineticsFromScratch3, add_three_body_reaction)
7373
// efficiencies: {AR: 0.83, H2: 2.4, H2O: 15.4}
7474
Composition reac = parseCompString("O:2");
7575
Composition prod = parseCompString("O2:1");
76-
ArrheniusRate rate(1.2e11, -1.0, 0.0);
77-
ThirdBody tbody;
78-
tbody.efficiencies = parseCompString("AR:0.83 H2:2.4 H2O:15.4");
79-
auto R = make_shared<ThreeBodyReaction3>(reac, prod, rate, tbody);
76+
auto rate = make_shared<ThreeBodyArrheniusRate>(1.2e11, -1.0, 0.0);
77+
rate->setEfficiencies(parseCompString("AR:0.83 H2:2.4 H2O:15.4"));
78+
auto R = make_shared<Reaction>(reac, prod, rate);
8079

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

9492
ASSERT_THROW(kin.addReaction(R), CanteraError);
9593
}
@@ -98,10 +96,9 @@ TEST_F(KineticsFromScratch3, skip_undefined_third_body)
9896
{
9997
Composition reac = parseCompString("O:2");
10098
Composition prod = parseCompString("O2:1");
101-
ArrheniusRate rate(1.2e11, -1.0, 0.0);
102-
ThirdBody tbody;
103-
tbody.efficiencies = parseCompString("H2:0.1 CO2:0.83");
104-
auto R = make_shared<ThreeBodyReaction3>(reac, prod, rate, tbody);
99+
auto rate = make_shared<ThreeBodyArrheniusRate>(1.2e11, -1.0, 0.0);
100+
rate->setEfficiencies(parseCompString("H2:0.1 CO2:0.83"));
101+
auto R = make_shared<Reaction>(reac, prod, rate);
105102

106103
kin.skipUndeclaredThirdBodies(true);
107104
kin.addReaction(R);

test/kinetics/kineticsFromYaml.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,10 @@ TEST(Reaction, ThreeBodyFromYaml3)
7979

8080
auto R = newReaction(rxn, *(sol->kinetics()));
8181
EXPECT_EQ(R->reactants.count("M"), (size_t) 0);
82-
EXPECT_EQ(R->type(), "three-body");
83-
EXPECT_DOUBLE_EQ(R->thirdBody()->efficiencies["H2O"], 5.0);
84-
EXPECT_DOUBLE_EQ(R->thirdBody()->default_efficiency, 1.0);
85-
86-
const auto& rate = std::dynamic_pointer_cast<ArrheniusRate>(R->rate());
82+
EXPECT_EQ(R->type(), "reaction");
83+
const auto& rate = std::dynamic_pointer_cast<ThreeBodyArrheniusRate>(R->rate());
84+
EXPECT_DOUBLE_EQ(rate->efficiencies()["H2O"], 5.0);
85+
EXPECT_DOUBLE_EQ(rate->defaultEfficiency(), 1.0);
8786
EXPECT_DOUBLE_EQ(rate->preExponentialFactor(), 1.2e11);
8887
}
8988

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

0 commit comments

Comments
 (0)