From a6038b32f6a955d575cbe68431670e9e2c87d4fe Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 29 Apr 2015 22:48:44 -0400 Subject: [PATCH] [Python] Add functions for constructing falloff / chemact reactions --- interfaces/cython/cantera/_cantera.pxd | 2 + interfaces/cython/cantera/reaction.pyx | 67 ++++++++++++++++--- .../cython/cantera/test/test_kinetics.py | 18 +++++ 3 files changed, 76 insertions(+), 11 deletions(-) diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 06229dcef..220a9cf9e 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -288,6 +288,8 @@ cdef extern from "cantera/kinetics/Reaction.h" namespace "Cantera": cbool is_sticking_coefficient string sticking_species +cdef extern from "cantera/kinetics/FalloffFactory.h" namespace "Cantera": + cdef shared_ptr[CxxFalloff] CxxNewFalloff "Cantera::newFalloff" (int, vector[double]) except + cdef extern from "cantera/kinetics/Kinetics.h" namespace "Cantera": cdef cppclass CxxKinetics "Cantera::Kinetics": diff --git a/interfaces/cython/cantera/reaction.pyx b/interfaces/cython/cantera/reaction.pyx index 9e1953595..618ec5c41 100644 --- a/interfaces/cython/cantera/reaction.pyx +++ b/interfaces/cython/cantera/reaction.pyx @@ -210,10 +210,17 @@ cdef class ThirdBodyReaction(ElementaryReaction): cdef class Falloff: - def __cinit__(self, init=True): - if init: - self._falloff.reset(new CxxFalloff()) - self.falloff = self._falloff.get() + falloff_type = SIMPLE_FALLOFF + + def __cinit__(self, params=(), init=True): + if not init: + return + + cdef vector[double] c + for p in params: + c.push_back(p) + self._falloff = CxxNewFalloff(self.falloff_type, c) + self.falloff = self._falloff.get() property type: def __get__(self): @@ -244,8 +251,25 @@ cdef class Falloff: return self.falloff.F(Pr, &work[0]) +cdef class TroeFalloff(Falloff): + falloff_type = TROE_FALLOFF + + +cdef class SriFalloff(Falloff): + falloff_type = SRI_FALLOFF + + cdef wrapFalloff(shared_ptr[CxxFalloff] falloff): - f = Falloff(init=False) + cdef int falloff_type = falloff.get().getType() + if falloff_type == SIMPLE_FALLOFF: + f = Falloff(init=False) + elif falloff_type == TROE_FALLOFF: + f = TroeFalloff(init=False) + elif falloff_type == SRI_FALLOFF: + f = SriFalloff(init=False) + else: + warnings.warn('Unknown falloff type: {0}'.format(falloff_type)) + f = Falloff(init=False) f._falloff = falloff f.falloff = f._falloff.get() return f @@ -254,20 +278,41 @@ cdef wrapFalloff(shared_ptr[CxxFalloff] falloff): cdef class FalloffReaction(Reaction): reaction_type = FALLOFF_RXN + cdef CxxFalloffReaction* frxn(self): + return self.reaction + property low_rate: def __get__(self): - cdef CxxFalloffReaction* r = self.reaction - return wrapArrhenius(&(r.low_rate), self) + return wrapArrhenius(&(self.frxn().low_rate), self) + def __set__(self, Arrhenius rate): + self.frxn().low_rate = deref(rate.rate) property high_rate: def __get__(self): - cdef CxxFalloffReaction* r = self.reaction - return wrapArrhenius(&(r.high_rate), self) + return wrapArrhenius(&(self.frxn().high_rate), self) + def __set__(self, Arrhenius rate): + self.frxn().high_rate = deref(rate.rate) property falloff: def __get__(self): - cdef CxxFalloffReaction* r = self.reaction - return wrapFalloff(r.falloff) + return wrapFalloff(self.frxn().falloff) + def __set__(self, Falloff f): + self.frxn().falloff = f._falloff + + property efficiencies: + def __get__(self): + return comp_map_to_dict(self.frxn().third_body.efficiencies) + def __set__(self, eff): + self.frxn().third_body.efficiencies = comp_map(eff) + + property default_efficiency: + def __get__(self): + return self.frxn().third_body.default_efficiency + def __set__(self, default_eff): + self.frxn().third_body.default_efficiency = default_eff + + def efficiency(self, species): + return self.frxn().third_body.efficiency(stringify(species)) cdef class ChemicallyActivatedReaction(FalloffReaction): diff --git a/interfaces/cython/cantera/test/test_kinetics.py b/interfaces/cython/cantera/test/test_kinetics.py index c3274ee78..6cb69d6ab 100644 --- a/interfaces/cython/cantera/test/test_kinetics.py +++ b/interfaces/cython/cantera/test/test_kinetics.py @@ -674,3 +674,21 @@ class TestReaction(utilities.CanteraTest): self.gas.forward_rate_constants[1]) self.assertNear(gas2.net_rates_of_progress[0], self.gas.net_rates_of_progress[1]) + + def test_falloff(self): + r = ct.FalloffReaction() + r.reactants = {'OH':2} + r.products = {'H2O2':1} + r.high_rate = ct.Arrhenius(7.4e10, -0.37, 0.0) + r.low_rate = ct.Arrhenius(2.3e12, -0.9, -1700*1000*4.184) + r.falloff = ct.TroeFalloff((0.7346, 94, 1756, 5182)) + r.efficiencies = {'AR':0.7, 'H2':2.0, 'H2O':6.0} + + gas2 = ct.Solution(thermo='IdealGas', kinetics='GasKinetics', + species=self.species, reactions=[r]) + gas2.TPX = self.gas.TPX + + self.assertNear(gas2.forward_rate_constants[0], + self.gas.forward_rate_constants[20]) + self.assertNear(gas2.net_rates_of_progress[0], + self.gas.net_rates_of_progress[20])