[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:
parent
a470502932
commit
8bee138553
8 changed files with 65 additions and 6 deletions
|
|
@ -112,6 +112,14 @@ public:
|
||||||
return m_rxneqn[i];
|
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) {
|
virtual bool isReversible(size_t i) {
|
||||||
if (std::find(m_revindex.begin(), m_revindex.end(), i)
|
if (std::find(m_revindex.begin(), m_revindex.end(), i)
|
||||||
< m_revindex.end()) {
|
< m_revindex.end()) {
|
||||||
|
|
@ -200,6 +208,8 @@ protected:
|
||||||
std::vector<size_t> m_revindex;
|
std::vector<size_t> m_revindex;
|
||||||
|
|
||||||
std::vector<std::string> m_rxneqn;
|
std::vector<std::string> m_rxneqn;
|
||||||
|
std::vector<std::string> m_reactantStrings;
|
||||||
|
std::vector<std::string> m_productStrings;
|
||||||
|
|
||||||
//! @name Reaction rate data
|
//! @name Reaction rate data
|
||||||
//!@{
|
//!@{
|
||||||
|
|
|
||||||
|
|
@ -700,6 +700,16 @@ public:
|
||||||
throw NotImplementedError("Kinetics::reactionStd::String");
|
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
|
* Return the forward rate constants
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -108,6 +108,12 @@ public:
|
||||||
//! The reaction equation. Used only for display purposes.
|
//! The reaction equation. Used only for display purposes.
|
||||||
std::string equation;
|
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
|
//! The default third body efficiency for species not listed in
|
||||||
//! #thirdBodyEfficiencies.
|
//! #thirdBodyEfficiencies.
|
||||||
doublereal default_3b_eff;
|
doublereal default_3b_eff;
|
||||||
|
|
|
||||||
|
|
@ -165,6 +165,8 @@ cdef extern from "cantera/kinetics/Kinetics.h" namespace "Cantera":
|
||||||
cbool isReversible(int) except +
|
cbool isReversible(int) except +
|
||||||
int reactionType(int) except +
|
int reactionType(int) except +
|
||||||
string reactionString(int) except +
|
string reactionString(int) except +
|
||||||
|
string reactantString(int) except +
|
||||||
|
string productString(int) except +
|
||||||
double reactantStoichCoeff(int, int) except +
|
double reactantStoichCoeff(int, int) except +
|
||||||
double productStoichCoeff(int, int) except +
|
double productStoichCoeff(int, int) except +
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -104,6 +104,16 @@ cdef class Kinetics(_SolutionBase):
|
||||||
self._check_reaction_index(i_reaction)
|
self._check_reaction_index(i_reaction)
|
||||||
return pystr(self.kinetics.reactionString(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):
|
def reaction_equations(self, indices=None):
|
||||||
"""
|
"""
|
||||||
Returns a list containing the reaction equation for all reactions in the
|
Returns a list containing the reaction equation for all reactions in the
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,18 @@ class TestKinetics(utilities.CanteraTest):
|
||||||
self.assertEqual(self.phase.reaction_equation(16),
|
self.assertEqual(self.phase.reaction_equation(16),
|
||||||
'H + H2O2 <=> HO2 + H2')
|
'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):
|
def test_stoich_coeffs(self):
|
||||||
nu_r = self.phase.reactant_stoich_coeffs()
|
nu_r = self.phase.reactant_stoich_coeffs()
|
||||||
nu_p = self.phase.product_stoich_coeffs()
|
nu_p = self.phase.product_stoich_coeffs()
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,8 @@ GasKinetics& GasKinetics::operator=(const GasKinetics& right)
|
||||||
m_dn = right.m_dn;
|
m_dn = right.m_dn;
|
||||||
m_revindex = right.m_revindex;
|
m_revindex = right.m_revindex;
|
||||||
m_rxneqn = right.m_rxneqn;
|
m_rxneqn = right.m_rxneqn;
|
||||||
|
m_reactantStrings = right.m_reactantStrings;
|
||||||
|
m_productStrings = right.m_productStrings;
|
||||||
|
|
||||||
m_logp_ref = right.m_logp_ref;
|
m_logp_ref = right.m_logp_ref;
|
||||||
m_logc_ref = right.m_logc_ref;
|
m_logc_ref = right.m_logc_ref;
|
||||||
|
|
@ -505,6 +507,8 @@ void GasKinetics::addReaction(ReactionData& r)
|
||||||
installGroups(reactionNumber(), r.rgroups, r.pgroups);
|
installGroups(reactionNumber(), r.rgroups, r.pgroups);
|
||||||
incrementRxnCount();
|
incrementRxnCount();
|
||||||
m_rxneqn.push_back(r.equation);
|
m_rxneqn.push_back(r.equation);
|
||||||
|
m_reactantStrings.push_back(r.reactantString);
|
||||||
|
m_productStrings.push_back(r.productString);
|
||||||
m_rxntype.push_back(r.reactionType);
|
m_rxntype.push_back(r.reactionType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -628,13 +628,18 @@ bool rxninfo::installReaction(int iRxn, const XML_Node& r, Kinetics& kin,
|
||||||
// string representation. Post-process to convert "[" and "]" characters
|
// string representation. Post-process to convert "[" and "]" characters
|
||||||
// back into "<" and ">" which cannot easily be stored in an XML file. This
|
// 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
|
// 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>";
|
rdata.equation = (r.hasChild("equation")) ? r("equation") : "<no equation>";
|
||||||
for (size_t nn = 0; nn < rdata.equation.size(); nn++) {
|
static const char* delimiters[] = {" [=] ", " =] ", " = ", "[=]", "=]", "="};
|
||||||
if (rdata.equation[nn] == '[') {
|
static const char* replacements[] = {" <=> ", " => ", " = ", "<=>", "=>", "="};
|
||||||
rdata.equation[nn] = '<';
|
for (size_t i = 0; i < 6; i++) {
|
||||||
} else if (rdata.equation[nn] == ']') {
|
size_t n = rdata.equation.find(delimiters[i]);
|
||||||
rdata.equation[nn] = '>';
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue