From 6458b0c50d199f53f3d21db627526bc0d1311e7f Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 30 Apr 2015 19:23:44 -0400 Subject: [PATCH] [Python] Enable direct construction of PlogReaction --- interfaces/cython/cantera/_cantera.pxd | 16 ++++++++++++ interfaces/cython/cantera/reaction.pyx | 12 +++++++++ .../cython/cantera/test/test_kinetics.py | 26 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 220a9cf9e..4b52c8206 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -10,6 +10,21 @@ cimport numpy as np ctypedef stdmap[string,double] Composition +cdef extern from "" namespace "std": + cdef cppclass multimap[T, U]: + cppclass iterator: + pair[T, U]& operator*() nogil + iterator operator++() nogil + iterator operator--() nogil + bint operator==(iterator) nogil + bint operator!=(iterator) nogil + multimap() nogil except + + U& operator[](T&) nogil + iterator begin() nogil + iterator end() nogil + pair[iterator, bint] insert(pair[T, U]) nogil + iterator find(T&) nogil + cdef extern from "cantera/base/xml.h" namespace "Cantera": cdef cppclass XML_Node: XML_Node* findByName(string) @@ -261,6 +276,7 @@ cdef extern from "cantera/kinetics/Reaction.h" namespace "Cantera": CxxChemicallyActivatedReaction() cdef cppclass CxxPlog "Cantera::Plog": + CxxPlog(multimap[double,CxxArrhenius]) vector[pair[double,CxxArrhenius]] rates() cdef cppclass CxxPlogReaction "Cantera::PlogReaction" (CxxReaction): diff --git a/interfaces/cython/cantera/reaction.pyx b/interfaces/cython/cantera/reaction.pyx index 618ec5c41..93674f3ea 100644 --- a/interfaces/cython/cantera/reaction.pyx +++ b/interfaces/cython/cantera/reaction.pyx @@ -332,6 +332,18 @@ cdef class PlogReaction(Reaction): rates.append((p_rate.first,copyArrhenius(&p_rate.second))) return rates + def __set__(self, rates): + cdef multimap[double,CxxArrhenius] ratemap + cdef Arrhenius rate + cdef pair[double,CxxArrhenius] item + for p,rate in rates: + item.first = p + item.second = deref(rate.rate) + ratemap.insert(item) + + cdef CxxPlogReaction* r = self.reaction + r.rate = CxxPlog(ratemap) + cdef class ChebyshevReaction(Reaction): reaction_type = CHEBYSHEV_RXN diff --git a/interfaces/cython/cantera/test/test_kinetics.py b/interfaces/cython/cantera/test/test_kinetics.py index 6cb69d6ab..f904e43c7 100644 --- a/interfaces/cython/cantera/test/test_kinetics.py +++ b/interfaces/cython/cantera/test/test_kinetics.py @@ -692,3 +692,29 @@ class TestReaction(utilities.CanteraTest): self.gas.forward_rate_constants[20]) self.assertNear(gas2.net_rates_of_progress[0], self.gas.net_rates_of_progress[20]) + + def test_plog(self): + gas1 = ct.Solution('pdep-test.cti') + species = ct.Species.listFromFile('pdep-test.cti') + + r = ct.PlogReaction() + r.reactants = {'R1A':1, 'R1B':1} + r.products = {'P1':1, 'H':1} + r.rates = [ + (0.01*ct.one_atm, ct.Arrhenius(1.2124e13, -0.5779, 10872.7*4184)), + (1.0*ct.one_atm, ct.Arrhenius(4.9108e28, -4.8507, 24772.8*4184)), + (10.0*ct.one_atm, ct.Arrhenius(1.2866e44, -9.0246, 39796.5*4184)), + (100.0*ct.one_atm, ct.Arrhenius(5.9632e53, -11.529, 52599.6*4184)) + ] + + gas2 = ct.Solution(thermo='IdealGas', kinetics='GasKinetics', + species=species, reactions=[r]) + + gas2.X = gas1.X = 'R1A:0.3, R1B:0.6, P1:0.1' + + for P in [0.001, 0.01, 0.2, 1.0, 1.1, 9.0, 10.0, 99.0, 103.0]: + gas1.TP = gas2.TP = 900, P * ct.one_atm + self.assertNear(gas2.forward_rate_constants[0], + gas1.forward_rate_constants[0]) + self.assertNear(gas2.net_rates_of_progress[0], + gas1.net_rates_of_progress[0])