[Kinetics] Add access to reactant and product strings

In C++, these are the reactantString() and productString() methods.  In Python,
these are the 'reactants' and 'products' properties.

Resolves Issue 110.
This commit is contained in:
Ray Speth 2014-06-20 18:45:17 +00:00
parent a470502932
commit 8bee138553
8 changed files with 65 additions and 6 deletions

View file

@ -112,6 +112,14 @@ public:
return m_rxneqn[i];
}
virtual std::string reactantString(size_t i) const {
return m_reactantStrings[i];
}
virtual std::string productString(size_t i) const {
return m_productStrings[i];
}
virtual bool isReversible(size_t i) {
if (std::find(m_revindex.begin(), m_revindex.end(), i)
< m_revindex.end()) {
@ -200,6 +208,8 @@ protected:
std::vector<size_t> m_revindex;
std::vector<std::string> m_rxneqn;
std::vector<std::string> m_reactantStrings;
std::vector<std::string> m_productStrings;
//! @name Reaction rate data
//!@{

View file

@ -700,6 +700,16 @@ public:
throw NotImplementedError("Kinetics::reactionStd::String");
}
//! Returns a string containing the reactants side of the reaction equation.
virtual std::string reactantString(size_t i) const {
throw NotImplementedError("Kinetics::reactionString");
}
//! Returns a string containing the products side of the reaction equation.
virtual std::string productString(size_t i) const {
throw NotImplementedError("Kinetics::productString");
}
/**
* Return the forward rate constants
*

View file

@ -108,6 +108,12 @@ public:
//! The reaction equation. Used only for display purposes.
std::string equation;
//! The reactants half of the reaction equation, used for display purposes.
std::string reactantString;
//! The products half of the reaction equation, used for display purposes.
std::string productString;
//! The default third body efficiency for species not listed in
//! #thirdBodyEfficiencies.
doublereal default_3b_eff;

View file

@ -165,6 +165,8 @@ cdef extern from "cantera/kinetics/Kinetics.h" namespace "Cantera":
cbool isReversible(int) except +
int reactionType(int) except +
string reactionString(int) except +
string reactantString(int) except +
string productString(int) except +
double reactantStoichCoeff(int, int) except +
double productStoichCoeff(int, int) except +

View file

@ -104,6 +104,16 @@ cdef class Kinetics(_SolutionBase):
self._check_reaction_index(i_reaction)
return pystr(self.kinetics.reactionString(i_reaction))
def reactants(self, int i_reaction):
"""The reactants portion of the reaction equation"""
self._check_reaction_index(i_reaction)
return pystr(self.kinetics.reactantString(i_reaction))
def products(self, int i_reaction):
"""The products portion of the reaction equation"""
self._check_reaction_index(i_reaction)
return pystr(self.kinetics.productString(i_reaction))
def reaction_equations(self, indices=None):
"""
Returns a list containing the reaction equation for all reactions in the

View file

@ -62,6 +62,18 @@ class TestKinetics(utilities.CanteraTest):
self.assertEqual(self.phase.reaction_equation(16),
'H + H2O2 <=> HO2 + H2')
def test_reactants_products(self):
for i in range(self.phase.n_reactions):
R = self.phase.reactants(i)
P = self.phase.products(i)
self.assertTrue(self.phase.reaction_equation(i).startswith(R))
self.assertTrue(self.phase.reaction_equation(i).endswith(P))
for k in range(self.phase.n_species):
if self.phase.reactant_stoich_coeff(k,i) != 0:
self.assertIn(self.phase.species_name(k), R)
if self.phase.product_stoich_coeff(k,i) != 0:
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()

View file

@ -87,6 +87,8 @@ GasKinetics& GasKinetics::operator=(const GasKinetics& right)
m_dn = right.m_dn;
m_revindex = right.m_revindex;
m_rxneqn = right.m_rxneqn;
m_reactantStrings = right.m_reactantStrings;
m_productStrings = right.m_productStrings;
m_logp_ref = right.m_logp_ref;
m_logc_ref = right.m_logc_ref;
@ -505,6 +507,8 @@ void GasKinetics::addReaction(ReactionData& r)
installGroups(reactionNumber(), r.rgroups, r.pgroups);
incrementRxnCount();
m_rxneqn.push_back(r.equation);
m_reactantStrings.push_back(r.reactantString);
m_productStrings.push_back(r.productString);
m_rxntype.push_back(r.reactionType);
}

View file

@ -628,13 +628,18 @@ bool rxninfo::installReaction(int iRxn, const XML_Node& r, Kinetics& kin,
// string representation. Post-process to convert "[" and "]" characters
// back into "<" and ">" which cannot easily be stored in an XML file. This
// reaction string is used only for display purposes. It is not parsed for
// the identities of reactants or products.
// the identities of reactants or products.
rdata.equation = (r.hasChild("equation")) ? r("equation") : "<no equation>";
for (size_t nn = 0; nn < rdata.equation.size(); nn++) {
if (rdata.equation[nn] == '[') {
rdata.equation[nn] = '<';
} else if (rdata.equation[nn] == ']') {
rdata.equation[nn] = '>';
static const char* delimiters[] = {" [=] ", " =] ", " = ", "[=]", "=]", "="};
static const char* replacements[] = {" <=> ", " => ", " = ", "<=>", "=>", "="};
for (size_t i = 0; i < 6; i++) {
size_t n = rdata.equation.find(delimiters[i]);
if (n != npos) {
size_t w = strlen(delimiters[i]);
rdata.reactantString = stripws(rdata.equation.substr(0, n));
rdata.productString = stripws(rdata.equation.substr(n+w, npos));
rdata.equation.replace(n, w, replacements[i]);
break;
}
}