From 7af153c75f771a2f9ad6ae90b09759cdc902c363 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 10 Jul 2014 22:36:28 +0000 Subject: [PATCH] [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". --- interfaces/cython/cantera/_cantera.pxd | 1 + interfaces/cython/cantera/kinetics.pyx | 42 +++++++++++++------ .../cython/cantera/test/test_kinetics.py | 32 +++++++------- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index edb60b1d4..085728649 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -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) diff --git a/interfaces/cython/cantera/kinetics.pyx b/interfaces/cython/cantera/kinetics.pyx index c1c2dbaed..89918b3cc 100644 --- a/interfaces/cython/cantera/kinetics.pyx +++ b/interfaces/cython/cantera/kinetics.pyx @@ -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): """ diff --git a/interfaces/cython/cantera/test/test_kinetics.py b/interfaces/cython/cantera/test/test_kinetics.py index 262fbeb7e..dbc9d901c 100644 --- a/interfaces/cython/cantera/test/test_kinetics.py +++ b/interfaces/cython/cantera/test/test_kinetics.py @@ -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),