[Cython/Kinetics] stoich_coeff methods accept species by name

reactant_stoich_coeff and product_stoich_coeff now accept either the species
name or the "kinetics species index".
This commit is contained in:
Ray Speth 2014-07-10 22:36:28 +00:00
parent bd14358a68
commit 7af153c75f
3 changed files with 49 additions and 26 deletions

View file

@ -159,6 +159,7 @@ cdef extern from "cantera/kinetics/Kinetics.h" namespace "Cantera":
int reactionPhaseIndex()
int phaseIndex(string)
int kineticsSpeciesIndex(int, int)
int kineticsSpeciesIndex(string)
CxxThermoPhase& thermo(int)

View file

@ -58,14 +58,20 @@ cdef class Kinetics(_SolutionBase):
if not 0 <= n < self.n_total_species:
raise ValueError("Kinetics Species index ({0}) out of range".format(n))
def kinetics_species_index(self, int species, int phase):
def kinetics_species_index(self, species, int phase=0):
"""
The index of species *species* of phase *phase* within arrays returned
by methods of class `Kinetics`.
by methods of class `Kinetics`. If *species* is a string, the *phase*
argument is unused.
"""
self._check_kinetics_species_index(species)
self._check_phase_index(phase)
return self.kinetics.kineticsSpeciesIndex(species, phase)
cdef int k
if isinstance(species, (str, unicode)):
return self.kinetics.kineticsSpeciesIndex(stringify(species))
else:
k = species
self._check_kinetics_species_index(k)
self._check_phase_index(k)
return self.kinetics.kineticsSpeciesIndex(k, phase)
def is_reversible(self, int i_reaction):
"""True if reaction `i_reaction` is reversible."""
@ -132,23 +138,35 @@ cdef class Kinetics(_SolutionBase):
else:
return [self.reaction_equation(i) for i in indices]
def reactant_stoich_coeff(self, int k_spec, int i_reaction):
def reactant_stoich_coeff(self, k_spec, int i_reaction):
"""
The stoichiometric coefficient of species *k_spec* as a reactant in
reaction *i_reaction*.
"""
self._check_kinetics_species_index(k_spec)
self._check_reaction_index(i_reaction)
return self.kinetics.reactantStoichCoeff(k_spec, i_reaction)
cdef int k
if isinstance(k_spec, (str, unicode)):
k = self.kinetics_species_index(k_spec)
else:
k = k_spec
self._check_kinetics_species_index(k_spec)
def product_stoich_coeff(self, int k_spec, int i_reaction):
self._check_reaction_index(i_reaction)
return self.kinetics.reactantStoichCoeff(k, i_reaction)
def product_stoich_coeff(self, k_spec, int i_reaction):
"""
The stoichiometric coefficient of species *k_spec* as a product in
reaction *i_reaction*.
"""
self._check_kinetics_species_index(k_spec)
cdef int k
if isinstance(k_spec, (str, unicode)):
k = self.kinetics_species_index(k_spec)
else:
k = k_spec
self._check_kinetics_species_index(k_spec)
self._check_reaction_index(i_reaction)
return self.kinetics.productStoichCoeff(k_spec, i_reaction)
return self.kinetics.productStoichCoeff(k, i_reaction)
def reactant_stoich_coeffs(self):
"""

View file

@ -78,30 +78,34 @@ class TestKinetics(utilities.CanteraTest):
nu_r = self.phase.reactant_stoich_coeffs()
nu_p = self.phase.product_stoich_coeffs()
def check_reactant(k, i, value):
def check_reactant(s, i, value):
k = self.phase.kinetics_species_index(s)
self.assertEqual(self.phase.reactant_stoich_coeff(s,i), value)
self.assertEqual(self.phase.reactant_stoich_coeff(k,i), value)
self.assertEqual(nu_r[k,i], value)
def check_product(k, i, value):
def check_product(s, i, value):
k = self.phase.kinetics_species_index(s)
self.assertEqual(self.phase.product_stoich_coeff(k,i), value)
self.assertEqual(self.phase.product_stoich_coeff(s,i), value)
self.assertEqual(nu_p[k,i], value)
# H + H2O2 <=> HO2 + H2
check_reactant(1, 16, 1)
check_reactant(7, 16, 1)
check_reactant(6, 16, 0)
check_reactant(0, 16, 0)
check_reactant('H', 16, 1)
check_reactant('H2O2', 16, 1)
check_reactant('HO2', 16, 0)
check_reactant('H2', 16, 0)
check_product(1, 16, 0)
check_product(7, 16, 0)
check_product(6, 16, 1)
check_product(0, 16, 1)
check_product('H', 16, 0)
check_product('H2O2', 16, 0)
check_product('HO2', 16, 1)
check_product('H2', 16, 1)
# 2 O + M <=> O2 + M
check_reactant(2, 0, 2)
check_reactant(3, 0, 0)
check_product(2, 0, 0)
check_product(3, 0, 1)
check_reactant('O', 0, 2)
check_reactant('O2', 0, 0)
check_product('O', 0, 0)
check_product('O2', 0, 1)
def test_rates_of_progress(self):
self.assertEqual(len(self.phase.net_rates_of_progress),