From ac0ccec7f5b0b96f61f079784154a030184fcf86 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Fri, 21 Jun 2013 20:57:53 +0000 Subject: [PATCH] Use ReactionData struct to store info in class rxninfo This eliminates the need the data members of class rxninfo that were just duplicates of what is in the ReactionData struct. --- include/cantera/kinetics/ReactionData.h | 6 ++ src/kinetics/importKinetics.cpp | 79 ++++++++++--------------- 2 files changed, 36 insertions(+), 49 deletions(-) diff --git a/include/cantera/kinetics/ReactionData.h b/include/cantera/kinetics/ReactionData.h index fb07624f8..09224f106 100644 --- a/include/cantera/kinetics/ReactionData.h +++ b/include/cantera/kinetics/ReactionData.h @@ -55,9 +55,15 @@ public: std::vector pgroups; std::map thirdBodyEfficiencies; + //! Net stoichiometric coefficients for participating species + std::map net_stoich; + //! True if the current reaction is reversible. False otherwise bool reversible; + //! True if the current reaction is marked as duplicate + bool duplicate; + //! Type of the rate coefficient for the forward rate constant /*! * The valid types are listed in the file, reaction_defs.h and they diff --git a/src/kinetics/importKinetics.cpp b/src/kinetics/importKinetics.cpp index 1d20f6a6e..121dc047b 100644 --- a/src/kinetics/importKinetics.cpp +++ b/src/kinetics/importKinetics.cpp @@ -13,7 +13,6 @@ #include "cantera/kinetics/importKinetics.h" #include "cantera/thermo/mix_defs.h" -#include // Cantera includes #include "cantera/thermo/speciesThermoTypes.h" @@ -48,23 +47,7 @@ ReactionRules::ReactionRules() : class rxninfo { public: - //! Net stoichiometric coefficients for each reaction - std::vector< std::map > m_rdata; - - //! string name (i.e. the reaction equation) - std::vector m_eqn; - - //! Indicates whether each reaction is marked "duplicate" - std::vector m_dup; - - //! Number of reactants in each reaction - std::vector m_nr; - - //! Indicates "type" of each reaction (see reaction_defs.h) - std::vector m_typ; - - //! Indicates whether each reaction is reversible - std::vector m_rev; + std::vector m_rdata; //! Map of (vector indicating participating species) to reaction numbers //! Used to speed up duplicate reaction checks. @@ -91,6 +74,12 @@ public: bool installReaction(int i, const XML_Node& r, Kinetics& kin, std::string default_phase, ReactionRules& rules, bool validate_rxn) ; + + ~rxninfo() { + for (size_t i = 0; i < m_rdata.size(); i++) { + delete m_rdata[i]; + } + } }; void checkRxnElementBalance(Kinetics& kin, @@ -632,13 +621,13 @@ bool rxninfo::installReaction(int iRxn, const XML_Node& r, Kinetics& kin, // We use the ReactionData object to store initial values read in from the // xml data. Then, when we have collected everything we add the reaction to // the kinetics object, kin, at the end of the routine. - ReactionData rdata; + ReactionData& rdata = **m_rdata.insert(m_rdata.end(), new ReactionData()); rdata.validate = validate_rxn; // Check to see if the reaction is specified to be a duplicate of another // reaction. It's an error if the reaction is a duplicate and this is not // set. - int dup = (r.hasAttrib("duplicate")) ? 1 : 0; + rdata.duplicate = (r.hasAttrib("duplicate")) ? 1 : 0; // Check to see if the reaction rate constant can be negative. It's an // error if a negative rate constant is found and this is not set. @@ -649,12 +638,12 @@ bool rxninfo::installReaction(int iRxn, const XML_Node& r, Kinetics& kin, // 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. - string eqn = (r.hasChild("equation")) ? r("equation") : ""; - for (size_t nn = 0; nn < eqn.size(); nn++) { - if (eqn[nn] == '[') { - eqn[nn] = '<'; - } else if (eqn[nn] == ']') { - eqn[nn] = '>'; + rdata.equation = (r.hasChild("equation")) ? r("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] = '>'; } } @@ -756,45 +745,37 @@ bool rxninfo::installReaction(int iRxn, const XML_Node& r, Kinetics& kin, // Look for undeclared duplicate reactions. if (validate_rxn) { - map rxnstoich; vector participants(kin.nTotalSpecies(), 0); for (size_t nn = 0; nn < rdata.reactants.size(); nn++) { - rxnstoich[-1 - int(rdata.reactants[nn])] -= rdata.rstoich[nn]; + rdata.net_stoich[-1 - int(rdata.reactants[nn])] -= rdata.rstoich[nn]; participants[rdata.reactants[nn]] += 1; } for (size_t nn = 0; nn < rdata.products.size(); nn++) { - rxnstoich[int(rdata.products[nn])+1] += rdata.pstoich[nn]; + rdata.net_stoich[int(rdata.products[nn])+1] += rdata.pstoich[nn]; participants[rdata.products[nn]] += 2; } vector& related = m_participants[participants]; for (size_t mm = 0; mm < related.size(); mm++) { - size_t nn = related[mm]; - if ((rdata.reactants.size() == m_nr[nn]) - && (rdata.reactionType == m_typ[nn])) { - doublereal c = isDuplicateReaction(rxnstoich, m_rdata[nn]); + ReactionData& other = *m_rdata[related[mm]]; + if ((rdata.reactants.size() == other.reactants.size()) + && (rdata.reactionType == other.reactionType)) { + doublereal c = isDuplicateReaction(rdata.net_stoich, other.net_stoich); if (c > 0.0 || (c < 0.0 && rdata.reversible) - || (c < 0.0 && m_rev[nn])) { - if ((!dup || !m_dup[nn])) { + || (c < 0.0 && other.reversible)) { + if ((!rdata.duplicate || !other.duplicate)) { string msg = string("Undeclared duplicate reactions detected: \n") - +"Reaction "+int2str(nn+1)+": "+m_eqn[nn] - +"\nReaction "+int2str(iRxn+1)+": "+eqn+"\n"; + +"Reaction "+int2str(other.number+1)+": "+other.equation + +"\nReaction "+int2str(iRxn+1)+": "+rdata.equation+"\n"; throw CanteraError("installReaction", msg); } } } } - m_dup.push_back(dup); - m_rev.push_back(rdata.reversible); - m_eqn.push_back(eqn); - m_nr.push_back(rdata.reactants.size()); - m_typ.push_back(rdata.reactionType); - m_rdata.push_back(rxnstoich); m_participants[participants].push_back(m_rdata.size() - 1); } - rdata.equation = eqn; rdata.number = iRxn; rdata.rxn_number = iRxn; @@ -817,7 +798,7 @@ bool rxninfo::installReaction(int iRxn, const XML_Node& r, Kinetics& kin, bool installReactionArrays(const XML_Node& p, Kinetics& kin, std::string default_phase, bool check_for_duplicates) { - const std::auto_ptr _rxns(new rxninfo); + rxninfo _rxns; vector rarrays; int itot = 0; @@ -891,8 +872,8 @@ bool installReactionArrays(const XML_Node& p, Kinetics& kin, for (i = 0; i < nrxns; i++) { const XML_Node* r = allrxns[i]; if (r) { - if (_rxns->installReaction(itot, *r, kin, - default_phase, rxnrule, check_for_duplicates)) { + if (_rxns.installReaction(itot, *r, kin, + default_phase, rxnrule, check_for_duplicates)) { ++itot; } } @@ -926,8 +907,8 @@ bool installReactionArrays(const XML_Node& p, Kinetics& kin, * sometimes has surprising results. */ if ((rxid >= imin) && (rxid <= imax)) { - if (_rxns->installReaction(itot, *r, kin, - default_phase, rxnrule, check_for_duplicates)) { + if (_rxns.installReaction(itot, *r, kin, + default_phase, rxnrule, check_for_duplicates)) { ++itot; } }