Skip to content

Commit

Permalink
[Python] Fix documentation and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
g3bk47 committed Feb 28, 2022
1 parent 2d4d0ae commit da8e2d0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
22 changes: 12 additions & 10 deletions interfaces/cython/cantera/test/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ def test_equil_results(gas, fuel, ox, Y_Cf, Y_Of, Y_Co, Y_Oo, basis):
def test_equivalence_ratio_simple_dilution(self):
gas = ct.Solution("gri30.yaml")

phi = 2
inv_afr = 2 * phi # inverse molar AFR for H2/O2
X = "H2:4,O2:1,CO:3,CO2:4,N2:5,CH4:6"
T, P = 300, 1e5
gas.TPX = T, P, X
Expand All @@ -449,31 +451,31 @@ def test_simple_dilution(fraction, basis):
M_H2 = gas.molecular_weights[gas.species_index("H2")]
M_O2 = gas.molecular_weights[gas.species_index("O2")]

phi = 2
gas.TP = T, P
gas.set_equivalence_ratio(phi, "H2", "O2", fraction=fraction,
diluent="CO2", basis=basis)
if basis == "mole" and fraction_type == "diluent":
self.assertNear(gas["H2"].X[0], (1 - fraction_value) * 0.8)
self.assertNear(gas["O2"].X[0], (1 - fraction_value) * 0.2)
self.assertNear(gas["H2"].X[0], (1 - fraction_value)
* inv_afr / (inv_afr + 1))
self.assertNear(gas["O2"].X[0], (1 - fraction_value) / (inv_afr + 1))
self.assertNear(gas["CO2"].X[0], fraction_value)
elif basis == "mass" and fraction_type == "diluent":
self.assertNear(gas["H2"].Y[0] / gas["O2"].Y[0], 4 * M_H2 / M_O2)
self.assertNear(gas["H2"].Y[0] / gas["O2"].Y[0], inv_afr * M_H2 / M_O2)
self.assertNear(gas["CO2"].Y[0], fraction_value)
elif basis == "mole" and fraction_type == "fuel":
self.assertNear(gas["H2"].X[0], fraction_value)
self.assertNear(gas["O2"].X[0], fraction_value / 4)
self.assertNear(gas["CO2"].X[0], 1 - fraction_value * (1 + 1.0 / 4))
self.assertNear(gas["O2"].X[0], fraction_value / inv_afr)
self.assertNear(gas["CO2"].X[0], 1 - fraction_value * (1 + 1 / inv_afr))
elif basis == "mass" and fraction_type == "fuel":
self.assertNear(gas["H2"].Y[0], fraction_value)
self.assertNear(gas["H2"].Y[0] / gas["O2"].Y[0], 4 * M_H2 / M_O2)
self.assertNear(gas["H2"].Y[0] / gas["O2"].Y[0], inv_afr * M_H2 / M_O2)
elif basis == "mole" and fraction_type == "oxidizer":
self.assertNear(gas["H2"].X[0], fraction_value * 4)
self.assertNear(gas["H2"].X[0], fraction_value * inv_afr)
self.assertNear(gas["O2"].X[0], fraction_value)
self.assertNear(gas["CO2"].X[0], 1 - fraction_value * (1 + 4))
self.assertNear(gas["CO2"].X[0], 1 - fraction_value * (1 + inv_afr))
elif basis == "mass" and fraction_type == "oxidizer":
self.assertNear(gas["O2"].Y[0], fraction_value)
self.assertNear(gas["H2"].Y[0] / gas["O2"].Y[0], 4 * M_H2 / M_O2)
self.assertNear(gas["H2"].Y[0] / gas["O2"].Y[0], inv_afr * M_H2 / M_O2)

Y = gas.Y
self.assertNear(gas.equivalence_ratio("H2", "O2",
Expand Down
31 changes: 13 additions & 18 deletions interfaces/cython/cantera/thermo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,9 @@ cdef class ThermoPhase(_SolutionBase):
N ends up as N2). The ``basis`` determines the fuel and oxidizer
compositions: ``basis='mole'`` means mole fractions (default),
``basis='mass'`` means mass fractions. The fuel/oxidizer mixture can be
be diluted by a ``diluent`` based on a mixing ``fraction``, specifying
the amount of diluent, fuel or oxidizer in the mixture. For more
information, see `Python example
be diluted by a ``diluent`` based on a mixing ``fraction``. The amount of
diluent is quantified as a fraction of fuel, oxidizer or the fuel/oxidizer
mixture. For more information, see `Python example
<https://cantera.org/examples/python/thermo/equivalenceRatio.py.html>`_ ::
>>> gas.set_equivalence_ratio(0.5, 'CH4', 'O2:1.0, N2:3.76', basis='mole')
Expand Down Expand Up @@ -857,12 +857,10 @@ cdef class ThermoPhase(_SolutionBase):
dilution or ``fraction=None``. May be given as string or dictionary (for
example ``fraction={"fuel":0.7})``
"""
cdef np.ndarray[np.double_t, ndim=1] fuel_comp = \
np.ascontiguousarray(self.__composition_to_array(fuel, basis),
dtype=np.double)
cdef np.ndarray[np.double_t, ndim=1] ox_comp = \
np.ascontiguousarray(self.__composition_to_array(oxidizer, basis),
dtype=np.double)
cdef np.ndarray[np.double_t, ndim=1] fuel_comp = np.ascontiguousarray(
self.__composition_to_array(fuel, basis), dtype=np.double)
cdef np.ndarray[np.double_t, ndim=1] ox_comp = np.ascontiguousarray(
self.__composition_to_array(oxidizer, basis), dtype=np.double)

self.thermo.setEquivalenceRatio(phi, &fuel_comp[0], &ox_comp[0],
ThermoBasis.mass if basis == "mass"
Expand Down Expand Up @@ -897,9 +895,8 @@ cdef class ThermoPhase(_SolutionBase):
raise ValueError("The fraction must specify 'fuel', 'oxidizer' or "
"'diluent'")

cdef np.ndarray[np.double_t, ndim=1] diluent_comp = \
np.ascontiguousarray(self.__composition_to_array(diluent, basis),
dtype=np.double)
cdef np.ndarray[np.double_t, ndim=1] diluent_comp = np.ascontiguousarray(
self.__composition_to_array(diluent, basis), dtype=np.double)

# this function changes the composition and fixes temperature and pressure
T, P = self.T, self.P
Expand Down Expand Up @@ -1039,12 +1036,10 @@ cdef class ThermoPhase(_SolutionBase):
self.TPY = T_orig, P_orig, Y_orig
return phi

cdef np.ndarray[np.double_t, ndim=1] f = \
np.ascontiguousarray(self.__composition_to_array(fuel, basis),
dtype=np.double)
cdef np.ndarray[np.double_t, ndim=1] o = \
np.ascontiguousarray(self.__composition_to_array(oxidizer, basis),
dtype=np.double)
cdef np.ndarray[np.double_t, ndim=1] f = np.ascontiguousarray(
self.__composition_to_array(fuel, basis), dtype=np.double)
cdef np.ndarray[np.double_t, ndim=1] o = np.ascontiguousarray(
self.__composition_to_array(oxidizer, basis), dtype=np.double)

phi = self.thermo.equivalenceRatio(&f[0], &o[0],
ThermoBasis.mass if basis=="mass"
Expand Down

0 comments on commit da8e2d0

Please sign in to comment.