Skip to content

Commit

Permalink
[Python] Deprecate Kinetics.*_stoich_coeffs() method in favor of prop…
Browse files Browse the repository at this point in the history
…erty

Methods are replaced by property Kinetics.*_stoich_coefficients
  • Loading branch information
ischoegl committed Aug 24, 2021
1 parent afb7370 commit 1b11a99
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 37 deletions.
73 changes: 55 additions & 18 deletions interfaces/cython/cantera/kinetics.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ cdef class Kinetics(_SolutionBase):

def reactant_stoich_coeff(self, k_spec, int i_reaction):
"""
The stoichiometric coefficient of species *k_spec* as a reactant in
reaction *i_reaction*.
The stoichiometric coefficient of species ``k_spec`` as a reactant in
reaction ``i_reaction``.
"""
cdef int k
if isinstance(k_spec, (str, bytes)):
Expand All @@ -250,8 +250,8 @@ cdef class Kinetics(_SolutionBase):

def product_stoich_coeff(self, k_spec, int i_reaction):
"""
The stoichiometric coefficient of species *k_spec* as a product in
reaction *i_reaction*.
The stoichiometric coefficient of species ``k_spec`` as a product in
reaction ``i_reaction``.
"""
cdef int k
if isinstance(k_spec, (str, bytes)):
Expand All @@ -268,28 +268,65 @@ cdef class Kinetics(_SolutionBase):
The array of reactant stoichiometric coefficients. Element *[k,i]* of
this array is the reactant stoichiometric coefficient of species *k* in
reaction *i*.
.. deprecated:: 2.6
To be removed after Cantera 2.6; replaceable by property
`Kinetics.reactant_stoich_coefficients`.
"""
cdef np.ndarray[np.double_t, ndim=2] data = np.empty((self.n_total_species,
self.n_reactions))
cdef int i,k
for i in range(self.n_reactions):
for k in range(self.n_total_species):
data[k,i] = self.kinetics.reactantStoichCoeff(k,i)
return data
warnings.warn("To be removed after Cantera 2.6; replaceable by "
"property 'reactant_stoich_coefficients'.", DeprecationWarning)
return self.reactant_stoich_coefficients

property reactant_stoich_coefficients:
"""
The array of reactant stoichiometric coefficients. Element ``[k,i]`` of
this array is the reactant stoichiometric coefficient of species ``k`` in
reaction ``i``.
For sparse output, set ``ct.use_sparse(True)``.
"""
def __get__(self):
# ensure that output vectors have sufficient size
shape = self.n_total_species, self.n_reactions
max_size = shape[0] * shape[1]
if __use_sparse__:
data, ix_ij = get_sparse(self, kin_reactantStoichCoeffs, max_size)
return _scipy_sparse.coo_matrix((data, ix_ij), shape=shape)
return get_dense(self, kin_reactantStoichCoeffs, max_size, shape)

def product_stoich_coeffs(self):
"""
The array of product stoichiometric coefficients. Element *[k,i]* of
this array is the product stoichiometric coefficient of species *k* in
reaction *i*.
.. deprecated:: 2.6
To be removed after Cantera 2.6; replaceable by property
`Kinetics.product_stoich_coefficients`.
"""
cdef np.ndarray[np.double_t, ndim=2] data = np.empty((self.n_total_species,
self.n_reactions))
cdef int i,k
for i in range(self.n_reactions):
for k in range(self.n_total_species):
data[k,i] = self.kinetics.productStoichCoeff(k,i)
return data
warnings.warn("To be removed after Cantera 2.6; replaceable by "
"property 'product_stoich_coefficients'.", DeprecationWarning)
return self.product_stoich_coefficients

property product_stoich_coefficients:
"""
The array of product stoichiometric coefficients. Element ``[k,i]`` of
this array is the product stoichiometric coefficient of species ``k`` in
reaction ``i``.
For sparse output, set ``ct.use_sparse(True)``.
"""
def __get__(self):
# ensure that output vectors have sufficient size
shape = self.n_total_species, self.n_reactions
max_size = shape[0] * shape[1]
if __use_sparse__:
data, ix_ij = get_sparse(self, kin_productStoichCoeffs, max_size)
return _scipy_sparse.coo_matrix((data, ix_ij), shape=shape)
return get_dense(self, kin_productStoichCoeffs, max_size, shape)

property forward_rates_of_progress:
"""
Expand Down
14 changes: 7 additions & 7 deletions interfaces/cython/cantera/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def checkConversion(self, refFile, testFile):

self.assertEqual(ref.element_names, gas.element_names)
self.assertEqual(ref.species_names, gas.species_names)
coeffs_ref = ref.reactant_stoich_coeffs()
coeffs_gas = gas.reactant_stoich_coeffs()
coeffs_ref = ref.reactant_stoich_coefficients
coeffs_gas = gas.reactant_stoich_coefficients
self.assertEqual(coeffs_gas.shape, coeffs_ref.shape)
self.assertTrue((coeffs_gas == coeffs_ref).all())

Expand Down Expand Up @@ -154,7 +154,7 @@ def test_pathologicalSpeciesNames(self):
self.assertEqual(gas.species_name(8), 'co')

self.assertEqual(gas.n_reactions, 12)
nu = gas.product_stoich_coeffs() - gas.reactant_stoich_coeffs()
nu = gas.product_stoich_coefficients - gas.reactant_stoich_coefficients
self.assertEqual(list(nu[:,0]), [-1, -1, 0, 2, 0, 0, 0, 0, 0])
self.assertEqual(list(nu[:,1]), [-2, 3, 0, -1, 0, 0, 0, 0, 0])
self.assertEqual(list(nu[:,2]), [-1, 0, 0, 0, 1, 0, 0, 0, 0])
Expand Down Expand Up @@ -233,8 +233,8 @@ def test_explicit_reverse_rate(self):
self.assertEqual(Rr[2], 0.0)
self.assertEqual(Rr[3], 0.0)
self.assertEqual(Rr[4], 0.0)
Rstoich = gas.reactant_stoich_coeffs()
Pstoich = gas.product_stoich_coeffs()
Rstoich = gas.reactant_stoich_coefficients
Pstoich = gas.product_stoich_coefficients
self.assertEqual(list(Rstoich[:, 0]), list(Pstoich[:, 1]))
self.assertEqual(list(Rstoich[:, 1]), list(Pstoich[:, 0]))
self.assertEqual(list(Rstoich[:, 2]), list(Pstoich[:, 3]))
Expand Down Expand Up @@ -289,8 +289,8 @@ def test_float_stoich_coeffs(self):
output = self.convert('float-stoich.inp', thermo='dummy-thermo.dat')
gas = ct.Solution(output)

R = gas.reactant_stoich_coeffs()
P = gas.product_stoich_coeffs()
R = gas.reactant_stoich_coefficients
P = gas.product_stoich_coefficients
self.assertArrayNear(R[:,0], [0, 1.5, 0.5, 0])
self.assertArrayNear(P[:,0], [1, 0, 0, 1])
self.assertArrayNear(R[:,1], [1, 0, 0, 1])
Expand Down
24 changes: 12 additions & 12 deletions interfaces/cython/cantera/test/test_kinetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ def test_reactants_products(self):
self.assertIn(self.phase.species_name(k), P)

def test_stoich_coeffs(self):
nu_r = self.phase.reactant_stoich_coeffs()
nu_p = self.phase.product_stoich_coeffs()
nu_r = self.phase.reactant_stoich_coefficients
nu_p = self.phase.product_stoich_coefficients

def check_reactant(s, i, value):
k = self.phase.kinetics_species_index(s)
Expand Down Expand Up @@ -149,8 +149,8 @@ def test_rate_constants(self):
self.phase.equilibrium_constants[ix])

def test_species_rates(self):
nu_p = self.phase.product_stoich_coeffs()
nu_r = self.phase.reactant_stoich_coeffs()
nu_p = self.phase.product_stoich_coefficients
nu_r = self.phase.reactant_stoich_coefficients
creation = (np.dot(nu_p, self.phase.forward_rates_of_progress) +
np.dot(nu_r, self.phase.reverse_rates_of_progress))
destruction = (np.dot(nu_r, self.phase.forward_rates_of_progress) +
Expand Down Expand Up @@ -187,10 +187,10 @@ def test_idealgas(self):
gas1.TPY = 800, 2*ct.one_atm, 'H2:0.3, O2:0.7, OH:2e-4, O:1e-3, H:5e-5'
gas2.TPY = gas1.TPY

self.assertTrue((gas1.reactant_stoich_coeffs() ==
gas2.reactant_stoich_coeffs()).all())
self.assertTrue((gas1.product_stoich_coeffs() ==
gas2.product_stoich_coeffs()).all())
self.assertTrue((gas1.reactant_stoich_coefficients ==
gas2.reactant_stoich_coefficients).all())
self.assertTrue((gas1.product_stoich_coefficients ==
gas2.product_stoich_coefficients).all())

self.assertArrayNear(gas1.delta_gibbs,
gas2.delta_gibbs)
Expand Down Expand Up @@ -265,10 +265,10 @@ def test_add_reaction(self):

self.assertEqual(gas1.n_reactions, gas2.n_reactions)

self.assertTrue((gas1.reactant_stoich_coeffs() ==
gas2.reactant_stoich_coeffs()).all())
self.assertTrue((gas1.product_stoich_coeffs() ==
gas2.product_stoich_coeffs()).all())
self.assertTrue((gas1.reactant_stoich_coefficients ==
gas2.reactant_stoich_coefficients).all())
self.assertTrue((gas1.product_stoich_coefficients ==
gas2.product_stoich_coefficients).all())

self.assertArrayNear(gas1.delta_gibbs,
gas2.delta_gibbs)
Expand Down

0 comments on commit 1b11a99

Please sign in to comment.