Allow adding species / reactions from Python
This enables the base functionality, but does not protect against any erroneous usage.
This commit is contained in:
parent
85e2e9968b
commit
1660167bd7
4 changed files with 61 additions and 0 deletions
|
|
@ -97,6 +97,10 @@ cdef class Kinetics(_SolutionBase):
|
|||
"""
|
||||
self.kinetics.modifyReaction(irxn, rxn._reaction)
|
||||
|
||||
def add_reaction(self, Reaction rxn):
|
||||
""" 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."""
|
||||
self._check_reaction_index(i_reaction)
|
||||
|
|
|
|||
|
|
@ -223,6 +223,34 @@ class KineticsFromReactions(utilities.CanteraTest):
|
|||
k2 = surf2.kinetics_species_index(k)
|
||||
self.assertNear(rop1[k1], rop2[k2])
|
||||
|
||||
def test_add_reaction(self):
|
||||
gas1 = ct.Solution('h2o2.xml')
|
||||
|
||||
S = ct.Species.listFromFile('h2o2.xml')
|
||||
R = ct.Reaction.listFromFile('h2o2.xml')
|
||||
gas2 = ct.Solution(thermo='IdealGas', kinetics='GasKinetics',
|
||||
species=S, reactions=R[:5])
|
||||
|
||||
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
|
||||
|
||||
for r in R[5:]:
|
||||
gas2.add_reaction(r)
|
||||
|
||||
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.assertArrayNear(gas1.delta_gibbs,
|
||||
gas2.delta_gibbs)
|
||||
self.assertArrayNear(gas1.reverse_rate_constants,
|
||||
gas2.reverse_rate_constants)
|
||||
self.assertArrayNear(gas1.net_production_rates,
|
||||
gas2.net_production_rates)
|
||||
|
||||
|
||||
class KineticsRepeatability(utilities.CanteraTest):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -578,6 +578,24 @@ class TestThermoPhase(utilities.CanteraTest):
|
|||
with self.assertRaises(NotImplementedError):
|
||||
copy.copy(self.phase)
|
||||
|
||||
def test_add_species(self):
|
||||
ref = ct.Solution('gri30.xml')
|
||||
n_orig = self.phase.n_species
|
||||
self.phase.add_species(ref.species('CO2'))
|
||||
self.phase.add_species(ref.species('CO'))
|
||||
|
||||
self.assertEqual(self.phase.n_species, n_orig + 2)
|
||||
self.assertIn('CO2', self.phase.species_names)
|
||||
self.assertIn('CO', self.phase.species_names)
|
||||
|
||||
state = 400, 2e5, 'H2:0.7, CO2:0.2, CO:0.1'
|
||||
ref.TPY = state
|
||||
self.phase.TPY = state
|
||||
self.assertNear(self.phase.enthalpy_mass, ref.enthalpy_mass)
|
||||
self.assertNear(self.phase.entropy_mole, ref.entropy_mole)
|
||||
self.assertArrayNear(ref[self.phase.species_names].partial_molar_cp,
|
||||
self.phase.partial_molar_cp)
|
||||
|
||||
|
||||
class TestThermo(utilities.CanteraTest):
|
||||
def setUp(self):
|
||||
|
|
|
|||
|
|
@ -450,6 +450,17 @@ cdef class ThermoPhase(_SolutionBase):
|
|||
if self.kinetics:
|
||||
self.kinetics.invalidateCache()
|
||||
|
||||
def add_species(self, Species species):
|
||||
"""
|
||||
Add a new species to this phase. Missing elements will be added
|
||||
automatically.
|
||||
"""
|
||||
self.thermo.addUndefinedElements()
|
||||
self.thermo.addSpecies(species._species)
|
||||
self.thermo.initThermo()
|
||||
if self.kinetics:
|
||||
self.kinetics.invalidateCache()
|
||||
|
||||
def n_atoms(self, species, element):
|
||||
"""
|
||||
Number of atoms of element *element* in species *species*. The element
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue