Skip to content

Commit

Permalink
[Python] Remove deprecated methods from Kinetics API
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed May 26, 2022
1 parent 3257b1b commit 056c4eb
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 149 deletions.
17 changes: 8 additions & 9 deletions interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,7 @@ class SolutionArray:
# From Kinetics
'n_total_species', 'n_reactions', 'n_phases', 'reaction_phase_index',
'kinetics_species_index', 'reaction', 'reactions', 'modify_reaction',
'is_reversible', 'multiplier', 'set_multiplier', 'reaction_type',
'reaction_equation', 'reactants', 'products', 'reaction_equations',
'multiplier', 'set_multiplier', 'reaction_equations',
'reactant_stoich_coeff', 'product_stoich_coeff',
'reactant_stoich_coeffs', 'product_stoich_coeffs',
# from Transport
Expand Down Expand Up @@ -991,8 +990,8 @@ def set_equivalence_ratio(self, phi, *args, **kwargs):
to be matched to the `SolutionArray`.
"""

# If ``phi`` is lower-dimensional than the SolutionArray's shape (for
# example, a scalar), broadcast it to have the right number of
# If ``phi`` is lower-dimensional than the SolutionArray's shape (for
# example, a scalar), broadcast it to have the right number of
# dimensions.
phi, _ = np.broadcast_arrays(phi, self._output_dummy)

Expand All @@ -1005,19 +1004,19 @@ def set_mixture_fraction(self, mixture_fraction, *args, **kwargs):
"""
See `ThermoPhase.set_mixture_fraction`
Note that ``mixture_fraction`` either needs to be a scalar value or
Note that ``mixture_fraction`` either needs to be a scalar value or
dimensions have to be matched to the `SolutionArray`.
"""

# If ``mixture_fraction`` is lower-dimensional than the SolutionArray's
# shape (for example, a scalar), broadcast it to have the right number
# If ``mixture_fraction`` is lower-dimensional than the SolutionArray's
# shape (for example, a scalar), broadcast it to have the right number
# of dimensions.
mixture_fraction, _ = np.broadcast_arrays(mixture_fraction,
mixture_fraction, _ = np.broadcast_arrays(mixture_fraction,
self._output_dummy)

for index in self._indices:
self._phase.state = self._states[index]
self._phase.set_mixture_fraction(mixture_fraction[index], *args,
self._phase.set_mixture_fraction(mixture_fraction[index], *args,
**kwargs)
self._states[index][:] = self._phase.state

Expand Down
153 changes: 41 additions & 112 deletions interfaces/cython/cantera/kinetics.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,6 @@ cdef class Kinetics(_SolutionBase):
""" Add a new reaction to this phase. """
self.kinetics.addReaction(rxn._reaction)

def is_reversible(self, int i_reaction):
"""
True if reaction ``i_reaction`` is reversible.
.. deprecated:: 2.6
Replaced by property `Reaction.reversible`.
Example: ``gas.is_reversible(0)`` is replaced by
``gas.reaction(0).reversible``
"""
rxn = self.reaction(i_reaction)
warnings.warn(
"'is_reversible' is deprecated and will be removed after Cantera 2.6.\n"
"Replaceable by property 'reversible' of the corresponding "
"reaction object.", DeprecationWarning)
return rxn.reversible

def multiplier(self, int i_reaction):
"""
A scaling factor applied to the rate coefficient for reaction
Expand All @@ -214,74 +197,6 @@ cdef class Kinetics(_SolutionBase):
self._check_reaction_index(i_reaction)
self.kinetics.setMultiplier(i_reaction, value)

def reaction_type(self, int i_reaction):
"""
Type code of reaction ``i_reaction``.
.. deprecated:: 2.6
Replaced by properties `Reaction.type` and `Reaction.rate.type`.
Example: ``gas.reaction_type(0)`` is replaced by
``gas.reaction(0).reaction_type`` and ``gas.reaction(0).rate.type``
"""
rxn = self.reaction(i_reaction)
if not rxn.uses_legacy:
warnings.warn(
"'reaction_type' is deprecated and will be removed after "
"Cantera 2.6.\nReplaceable by property 'reaction_type' of the "
"corresponding reaction object (or property 'type' of the\n"
"associated 'rate').", DeprecationWarning)
return rxn.type

def reaction_equation(self, int i_reaction):
"""
The equation for the specified reaction. See also `reaction_equations`.
.. deprecated:: 2.6
Replaced by property `Reaction.equation`.
Example: ``gas.reaction_equation(0)`` is replaced by
``gas.reaction(0).equation``
"""
rxn = self.reaction(i_reaction)
warnings.warn(
"'reaction_equation' is deprecated and will be removed after "
"Cantera 2.6.\nReplaceable by property 'equation' of the corresponding "
"reaction object.", DeprecationWarning)
return rxn.equation

def reactants(self, int i_reaction):
"""
The reactants portion of the reaction equation
.. deprecated:: 2.6
Replaced by property `Reaction.reactants`.
Example: ``gas.reactants(0)`` is replaced by ``gas.reaction(0).reactants``
"""
rxn = self.reaction(i_reaction)
warnings.warn(
"'reactants' is deprecated and will be removed after Cantera 2.6.\n"
"Replaceable by property 'reactant_string' of the corresponding "
"reaction object.", DeprecationWarning)
return rxn.reactant_string

def products(self, int i_reaction):
"""
The products portion of the reaction equation
.. deprecated:: 2.6
Replaced by property `Reaction.products`.
Example: ``gas.products(0)`` is replaced by ``gas.reaction(0).products``
"""
rxn = self.reaction(i_reaction)
warnings.warn(
"'products' is deprecated and will be removed after Cantera 2.6.\n"
"Replaceable by property 'product_string' of the corresponding "
"reaction object.", DeprecationWarning)
return rxn.product_string

def reaction_equations(self, indices=None):
"""
Returns a list containing the reaction equation for all reactions in the
Expand Down Expand Up @@ -330,28 +245,17 @@ cdef class Kinetics(_SolutionBase):
self._check_reaction_index(i_reaction)
return self.kinetics.productStoichCoeff(k, i_reaction)

def reactant_stoich_coeffs(self):
"""
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
Behavior to change after Cantera 2.6; for new behavior, see property
`Kinetics.reactant_stoich_coeffs3`.
"""
warnings.warn("Behavior to change after Cantera 2.6; for new behavior, see "
"property 'reactant_stoich_coeffs3'.", DeprecationWarning)
return self.reactant_stoich_coeffs3

property reactant_stoich_coeffs3:
property reactant_stoich_coeffs:
"""
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)``.
.. versionchanged:: 3.0
Method was changed to a property in Cantera 3.0.
"""
def __get__(self):
if _USE_SPARSE:
Expand All @@ -360,28 +264,35 @@ cdef class Kinetics(_SolutionBase):
return _scipy_sparse.csc_matrix(tup, shape=shape)
return get_dense(self, kin_reactantStoichCoeffs)

def product_stoich_coeffs(self):
property reactant_stoich_coeffs3:
"""
The array of product stoichiometric coefficients. Element *[k,i]* of
this array is the product stoichiometric coefficient of species *k* in
reaction *i*.
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)``.
.. deprecated:: 2.6
.. deprecated:: 3.0
Behavior to change after Cantera 2.6; for new behavior, see property
`Kinetics.reactant_stoich_coeffs3`.
Method to be removed after Cantera 3.0. Replaceable by
`Kinetics.reactant_stoich_coeffs3`
"""
warnings.warn("Behavior to change after Cantera 2.6; for new behavior, see "
"property 'product_stoich_coeffs3'.", DeprecationWarning)
return self.product_stoich_coeffs3
def __get__(self):
warnings.warn("Method to be removed after Cantera 3.0; use property "
"'reactant_stoich_coeffs' instead.", DeprecationWarning)
return self.reactant_stoich_coeffs

property product_stoich_coeffs3:
property product_stoich_coeffs:
"""
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)``.
.. versionchanged:: 3.0
Method was changed to a property in Cantera 3.0.
"""
def __get__(self):
if _USE_SPARSE:
Expand All @@ -390,6 +301,24 @@ cdef class Kinetics(_SolutionBase):
return _scipy_sparse.csc_matrix(tup, shape=shape)
return get_dense(self, kin_productStoichCoeffs)

property product_stoich_coeffs3:
"""
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)``.
.. deprecated:: 3.0
Method to be removed after Cantera 3.0. Replaceable by
`Kinetics.product_stoich_coeffs3`
"""
def __get__(self):
warnings.warn("Method to be removed after Cantera 3.0; use property "
"'product_stoich_coeffs' instead.", DeprecationWarning)
return self.product_stoich_coeffs

property product_stoich_coeffs_reversible:
"""
The array of product stoichiometric coefficients of reversible reactions.
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 @@ -38,8 +38,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_coeffs3
coeffs_gas = gas.reactant_stoich_coeffs3
coeffs_ref = ref.reactant_stoich_coeffs
coeffs_gas = gas.reactant_stoich_coeffs
self.assertEqual(coeffs_gas.shape, coeffs_ref.shape)
self.assertTrue((coeffs_gas == coeffs_ref).all())

Expand Down Expand Up @@ -148,7 +148,7 @@ def test_pathologicalSpeciesNames(self):
self.assertEqual(gas.species_name(9), "amp&ersand")

self.assertEqual(gas.n_reactions, 13)
nu = gas.product_stoich_coeffs3 - gas.reactant_stoich_coeffs3
nu = gas.product_stoich_coeffs - gas.reactant_stoich_coeffs
self.assertEqual(list(nu[:,0]), [-1, -1, 0, 2, 0, 0, 0, 0, 0, 0])
self.assertEqual(list(nu[:,1]), [-2, 3, 0, -1, 0, 0, 0, 0, 0, 0])
self.assertEqual(list(nu[:,2]), [-1, 0, 0, 0, 1, 0, 0, 0, 0, 0])
Expand Down Expand Up @@ -226,8 +226,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_coeffs3
Pstoich = gas.product_stoich_coeffs3
Rstoich = gas.reactant_stoich_coeffs
Pstoich = gas.product_stoich_coeffs
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 @@ -278,8 +278,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_coeffs3
P = gas.product_stoich_coeffs3
R = gas.reactant_stoich_coeffs
P = gas.product_stoich_coeffs
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
10 changes: 5 additions & 5 deletions interfaces/cython/cantera/test/test_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def setUpClass(cls):
ct.use_legacy_rate_constants(False)
cls.tpx = cls.gas.TPX

cls.r_stoich = cls.gas.reactant_stoich_coeffs3
cls.p_stoich = cls.gas.product_stoich_coeffs3
cls.r_stoich = cls.gas.reactant_stoich_coeffs
cls.p_stoich = cls.gas.product_stoich_coeffs

cls.rxn = cls.gas.reactions()[cls.rxn_idx]
cls.rix = [cls.gas.species_index(k) for k in cls.rxn.reactants.keys()]
Expand Down Expand Up @@ -575,7 +575,7 @@ def test_forward_rop_ddX(self):
drop = self.gas.forward_rates_of_progress_ddX
dropp = self.gas.forward_rates_of_progress_ddP
drop_num = self.rop_ddX(mode="forward")
stoich = self.gas.reactant_stoich_coeffs3
stoich = self.gas.reactant_stoich_coeffs
for i in range(self.gas.n_reactions):
try:
# test entries that are not spurious
Expand All @@ -593,7 +593,7 @@ def test_reverse_rop_ddX(self):
drop = self.gas.reverse_rates_of_progress_ddX
dropp = self.gas.reverse_rates_of_progress_ddP
drop_num = self.rop_ddX(mode="reverse")
stoich = self.gas.product_stoich_coeffs3
stoich = self.gas.product_stoich_coeffs
for i in range(self.gas.n_reactions):
try:
# test entries that are not spurious
Expand All @@ -611,7 +611,7 @@ def test_net_rop_ddX(self):
drop = self.gas.net_rates_of_progress_ddX
dropp = self.gas.net_rates_of_progress_ddP
drop_num = self.rop_ddX(mode="net")
stoich = self.gas.product_stoich_coeffs3 - self.gas.reactant_stoich_coeffs3
stoich = self.gas.product_stoich_coeffs - self.gas.reactant_stoich_coeffs
for i in range(self.gas.n_reactions):
try:
# test entries that are not spurious
Expand Down
32 changes: 16 additions & 16 deletions interfaces/cython/cantera/test/test_kinetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,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_coeffs3
nu_p = self.phase.product_stoich_coeffs3
nu_r = self.phase.reactant_stoich_coeffs
nu_p = self.phase.product_stoich_coeffs

def check_reactant(s, i, value):
k = self.phase.kinetics_species_index(s)
Expand Down Expand Up @@ -143,12 +143,12 @@ def check_product(s, i, value):

@utilities.unittest.skipIf(isinstance(_scipy_sparse, ImportError), "scipy is not installed")
def test_stoich_coeffs_sparse(self):
nu_r_dense = self.phase.reactant_stoich_coeffs3
nu_p_dense = self.phase.product_stoich_coeffs3
nu_r_dense = self.phase.reactant_stoich_coeffs
nu_p_dense = self.phase.product_stoich_coeffs

ct.use_sparse(True)
nu_r_sparse = self.phase.reactant_stoich_coeffs3
nu_p_sparse = self.phase.product_stoich_coeffs3
nu_r_sparse = self.phase.reactant_stoich_coeffs
nu_p_sparse = self.phase.product_stoich_coeffs

self.assertTrue((nu_r_sparse.toarray() == nu_r_dense).all())
self.assertTrue((nu_p_sparse.toarray() == nu_p_dense).all())
Expand All @@ -174,8 +174,8 @@ def test_rate_constants(self):
self.phase.equilibrium_constants[ix])

def test_species_rates(self):
nu_p = self.phase.product_stoich_coeffs3
nu_r = self.phase.reactant_stoich_coeffs3
nu_p = self.phase.product_stoich_coeffs
nu_r = self.phase.reactant_stoich_coeffs
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 @@ -212,10 +212,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_coeffs3 ==
gas2.reactant_stoich_coeffs3).all())
self.assertTrue((gas1.product_stoich_coeffs3 ==
gas2.product_stoich_coeffs3).all())
self.assertTrue((gas1.reactant_stoich_coeffs ==
gas2.reactant_stoich_coeffs).all())
self.assertTrue((gas1.product_stoich_coeffs ==
gas2.product_stoich_coeffs).all())

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

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

self.assertTrue((gas1.reactant_stoich_coeffs3 ==
gas2.reactant_stoich_coeffs3).all())
self.assertTrue((gas1.product_stoich_coeffs3 ==
gas2.product_stoich_coeffs3).all())
self.assertTrue((gas1.reactant_stoich_coeffs ==
gas2.reactant_stoich_coeffs).all())
self.assertTrue((gas1.product_stoich_coeffs ==
gas2.product_stoich_coeffs).all())

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

0 comments on commit 056c4eb

Please sign in to comment.