Skip to content

Commit

Permalink
[Python] Simplify constructor options for Reaction
Browse files Browse the repository at this point in the history
  • Loading branch information
speth authored and ischoegl committed Feb 4, 2022
1 parent 8e93256 commit 0f46911
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 47 deletions.
47 changes: 9 additions & 38 deletions interfaces/cython/cantera/reaction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -796,51 +796,22 @@ cdef class Reaction:
self.products = products

def __init__(self, reactants=None, products=None, rate=None, *, equation=None,
Kinetics kinetics=None, init=True, legacy=False, **kwargs):
init=True, legacy=False, **kwargs):

if legacy or not init:
return

if not rate:
raise TypeError("No *rate* argument provided")
if equation:
self.reaction.setEquation(stringify(equation))

if isinstance(rate, ReactionRate):
if reactants and products:
self.reactants = reactants
self.products = products
if isinstance(rate, dict):
if set(rate) == {"A", "b", "Ea"}:
# Allow simple syntax for Arrhenius rates
rate = ReactionRate.from_dict({"rate-constant": rate})
else:
self.reaction.setEquation(stringify(equation))
self.reaction.setRate((<ReactionRate>rate)._rate)
return

if reactants and products and not equation:
equation = self.equation

if equation and kinetics:
rxn_type = self._reaction_type
spec = {"equation": equation, "type": rxn_type}
if isinstance(rate, dict):
# Arrhenius-like rates
spec["rate-constant"] = rate
rate = None
elif isinstance(rate, ReactionRate):
spec["type"] = rate.type

self._reaction = CxxNewReaction(dict_to_anymap(spec),
deref(kinetics.kinetics))
self.reaction = self._reaction.get()

if rate is not None:
try:
if isinstance(rate[0][1], Arrhenius):
self.rate = PlogRate(rate)
rate = None
except (IndexError, TypeError, KeyError):
pass

if rate is not None:
raise TypeError("Invalid rate definition")
rate = ReactionRate.from_dict(rate)

self.reaction.setRate((<ReactionRate?>rate)._rate)

@staticmethod
cdef wrap(shared_ptr[CxxReaction] reaction):
Expand Down
24 changes: 15 additions & 9 deletions interfaces/cython/cantera/test/test_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,12 +1330,6 @@ class TestPlog2(ReactionTests, utilities.CanteraTest):
"""
_deprecated_callers = {(1000., ct.one_atm): 530968934612.9017}

@classmethod
def setUpClass(cls):
ReactionTests.setUpClass()
if not cls._legacy:
cls._rate_obj = ct.PlogRate(cls._rate)

def check_rates(self, rates, other):
# helper function used by deprecation tests
self.assertEqual(len(rates), len(other))
Expand All @@ -1353,11 +1347,11 @@ def test_deprecated_getters(self):
self.check_rates(rxn.rates, self._rate)
else:
with self.assertWarnsRegex(DeprecationWarning, "property is moved"):
self.check_rates(rxn.rates, self._rate)
self.check_rates(rxn.rates, TestPlog2._rate)

def test_deprecated_setters(self):
# overload default tester for deprecated property setters
rate = ct.PlogRate(self._rate)
rate = ct.PlogRate(TestPlog2._rate)
rates = rate.rates

rxn = self.from_yaml()
Expand All @@ -1368,14 +1362,21 @@ def test_deprecated_setters(self):
with self.assertWarnsRegex(DeprecationWarning, "Setter is replaceable"):
rxn.rates = rates
with self.assertWarnsRegex(DeprecationWarning, "property is moved"):
self.check_rates(rxn.rates, self._rate)
self.check_rates(rxn.rates, TestPlog2._rate)


class TestPlog(TestPlog2):
# test updated version of Plog reaction

_cls = ct.Reaction
_rate_cls = ct.PlogRate
_rate = {
"type": "pressure-dependent-Arrhenius",
"rate-constants":
[{"P": 1013.25, "A": 1.2124e+16, "b": -0.5779, "Ea": 45491376.8},
{"P": 101325., "A": 4.9108e+31, "b": -4.8507, "Ea": 103649395.2},
{"P": 1013250., "A": 1.2866e+47, "b": -9.0246, "Ea": 166508556.0},
{"P": 10132500., "A": 5.9632e+56, "b": -11.529, "Ea": 220076726.4}]}
_rxn_type = "reaction"
_rate_type = "pressure-dependent-Arrhenius"
_legacy = False
Expand All @@ -1389,6 +1390,11 @@ class TestPlog(TestPlog2):
- {P: 100.0 atm, A: 5.9632e+56, b: -11.529, Ea: 5.25996e+04 cal/mol}
"""

@classmethod
def setUpClass(cls):
ReactionTests.setUpClass()
cls._rate_obj = ct.ReactionRate.from_dict(cls._rate)

def eval_rate(self, rate):
return rate(self.gas.T, self.gas.P)

Expand Down

0 comments on commit 0f46911

Please sign in to comment.