From 9d4a3e0f75f64c8e6c7dd9c4bf2ec5b2cd9220fb Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 22 Apr 2015 17:35:17 -0400 Subject: [PATCH] [Kinetics] Reaction objects have Falloff objects This approach actually makes use of the fact that we have a Falloff type. --- include/cantera/kinetics/FalloffFactory.h | 4 +++ include/cantera/kinetics/FalloffMgr.h | 31 +++++++++++++---------- include/cantera/kinetics/Reaction.h | 17 +++++-------- src/kinetics/FalloffFactory.cpp | 6 +++++ src/kinetics/GasKinetics.cpp | 3 +-- src/kinetics/Reaction.cpp | 24 ++++++++---------- test/kinetics/kineticsFromScratch.cpp | 4 +-- 7 files changed, 48 insertions(+), 41 deletions(-) diff --git a/include/cantera/kinetics/FalloffFactory.h b/include/cantera/kinetics/FalloffFactory.h index 41dbaa823..47b51f2e6 100644 --- a/include/cantera/kinetics/FalloffFactory.h +++ b/include/cantera/kinetics/FalloffFactory.h @@ -11,6 +11,7 @@ #include "cantera/base/FactoryBase.h" #include "cantera/base/ct_thread.h" +#include "cantera/base/smart_ptr.h" #include "cantera/kinetics/Falloff.h" namespace Cantera @@ -71,5 +72,8 @@ private: static mutex_t falloff_mutex; }; +//! @copydoc FalloffFactory::newFalloff +shared_ptr newFalloff(int type, const vector_fp& c); + } #endif diff --git a/include/cantera/kinetics/FalloffMgr.h b/include/cantera/kinetics/FalloffMgr.h index 13c3f71bd..f3a98423a 100644 --- a/include/cantera/kinetics/FalloffMgr.h +++ b/include/cantera/kinetics/FalloffMgr.h @@ -9,6 +9,7 @@ #include "reaction_defs.h" #include "FalloffFactory.h" +#include "cantera/base/global.h" namespace Cantera { @@ -29,17 +30,6 @@ public: //else m_factory = f; } - //! Destructor. Deletes all installed falloff function calculators. - virtual ~FalloffMgr() { - for (size_t i = 0; i < m_falloff.size(); i++) { - delete m_falloff[i]; - } - //if (m_factory) { - //FalloffFactory::deleteFalloffFactory(); - //m_factory = 0; - //} - } - //! Install a new falloff function calculator. /* * @param rxn Index of the falloff reaction. This will be used to @@ -47,11 +37,26 @@ public: * @param falloffType of falloff function to install. * @param reactionType Either `FALLOFF_RXN` or `CHEMACT_RXN` * @param c vector of coefficients for the falloff function. + * @deprecated Use install(size_t, int, shared_ptr). To be removed + * after Cantera 2.2. */ void install(size_t rxn, int falloffType, int reactionType, const vector_fp& c) { + warn_deprecated("FalloffMgr::install(size_t, int, int, const vector_fp&)", + "Use install(size_t, int, shared_ptr). To be removed after Cantera 2.2."); + shared_ptr f(m_factory->newFalloff(falloffType,c)); + install(rxn, reactionType, f); + } + + //! Install a new falloff function calculator. + /* + * @param rxn Index of the falloff reaction. This will be used to + * determine which array entry is modified in method pr_to_falloff. + * @param reactionType Either `FALLOFF_RXN` or `CHEMACT_RXN` + * @param f The falloff function. + */ + void install(size_t rxn, int reactionType, shared_ptr f) { m_rxn.push_back(rxn); - Falloff* f = m_factory->newFalloff(falloffType,c); m_offset.push_back(m_worksize); m_worksize += f->workSize(); m_falloff.push_back(f); @@ -96,7 +101,7 @@ public: protected: std::vector m_rxn; - std::vector m_falloff; + std::vector > m_falloff; FalloffFactory* m_factory; vector_int m_loc; std::vector m_offset; diff --git a/include/cantera/kinetics/Reaction.h b/include/cantera/kinetics/Reaction.h index 34b42cc22..ffdbfb64d 100644 --- a/include/cantera/kinetics/Reaction.h +++ b/include/cantera/kinetics/Reaction.h @@ -8,6 +8,7 @@ #include "cantera/base/utilities.h" #include "cantera/base/smart_ptr.h" #include "cantera/kinetics/RxnRates.h" +#include "cantera/kinetics/Falloff.h" namespace Cantera { @@ -128,8 +129,7 @@ public: FalloffReaction(); FalloffReaction(const Composition& reactants, const Composition& products, const Arrhenius& low_rate, const Arrhenius& high_rate, - const ThirdBody& tbody, int falloff_type, - const vector_fp& falloff_params); + const ThirdBody& tbody); virtual std::string reactantString() const; virtual std::string productString() const; virtual void validate(); @@ -143,13 +143,9 @@ public: //! Relative efficiencies of third-body species in enhancing the reaction rate ThirdBody third_body; - //! Type of falloff parameterization to use. Values are defined in - //! reaction_defs.h, with names ending in `FALLOFF`. - int falloff_type; - - //! Values used in the falloff parameterization. Meaning of each parameter - //! depends on #falloff_type. - vector_fp falloff_parameters; + //! Falloff function which determines how low_rate and high_rate are + //! combined to determine the rate constant for the reaction. + shared_ptr falloff; }; //! A reaction where the rate decreases as pressure increases due to collisional @@ -162,8 +158,7 @@ public: ChemicallyActivatedReaction(); ChemicallyActivatedReaction(const Composition& reactants, const Composition& products, const Arrhenius& low_rate, - const Arrhenius& high_rate, const ThirdBody& tbody, int falloff_type, - const vector_fp& falloff_params); + const Arrhenius& high_rate, const ThirdBody& tbody); }; //! A pressure-dependent reaction parameterized by logarithmically interpolating diff --git a/src/kinetics/FalloffFactory.cpp b/src/kinetics/FalloffFactory.cpp index 21856387d..ccb11b423 100644 --- a/src/kinetics/FalloffFactory.cpp +++ b/src/kinetics/FalloffFactory.cpp @@ -32,4 +32,10 @@ Falloff* FalloffFactory::newFalloff(int type, const vector_fp& c) return f; } +shared_ptr newFalloff(int type, const vector_fp& c) +{ + shared_ptr f(FalloffFactory::factory()->newFalloff(type, c)); + return f; +} + } diff --git a/src/kinetics/GasKinetics.cpp b/src/kinetics/GasKinetics.cpp index c78191c63..abd131b0e 100644 --- a/src/kinetics/GasKinetics.cpp +++ b/src/kinetics/GasKinetics.cpp @@ -361,8 +361,7 @@ void GasKinetics::addFalloffReaction(FalloffReaction& r) r.third_body.default_efficiency); // install the falloff function calculator for this reaction - m_falloffn.install(m_nfall, r.falloff_type, r.reaction_type, - r.falloff_parameters); + m_falloffn.install(m_nfall, r.reaction_type, r.falloff); // increment the falloff reaction counter ++m_nfall; diff --git a/src/kinetics/Reaction.cpp b/src/kinetics/Reaction.cpp index b94f9dc21..5c0fa31fc 100644 --- a/src/kinetics/Reaction.cpp +++ b/src/kinetics/Reaction.cpp @@ -3,6 +3,7 @@ */ #include "cantera/kinetics/Reaction.h" +#include "cantera/kinetics/FalloffFactory.h" #include "cantera/base/ctml.h" #include "cantera/base/Array.h" #include @@ -157,21 +158,17 @@ std::string ThirdBodyReaction::productString() const { FalloffReaction::FalloffReaction() : Reaction(FALLOFF_RXN) - , falloff_type(-1) { } FalloffReaction::FalloffReaction( const Composition& reactants_, const Composition& products_, const Arrhenius& low_rate_, const Arrhenius& high_rate_, - const ThirdBody& tbody, int type, - const vector_fp& params) + const ThirdBody& tbody) : Reaction(FALLOFF_RXN, reactants_, products_) , low_rate(low_rate_) , high_rate(high_rate_) , third_body(tbody) - , falloff_type(type) - , falloff_parameters(params) { } @@ -212,10 +209,8 @@ ChemicallyActivatedReaction::ChemicallyActivatedReaction() ChemicallyActivatedReaction::ChemicallyActivatedReaction( const Composition& reactants_, const Composition& products_, const Arrhenius& low_rate_, const Arrhenius& high_rate_, - const ThirdBody& tbody, int falloff_type, - const vector_fp& falloff_params) - : FalloffReaction(reactants_, products_, low_rate, high_rate, tbody, - falloff_type, falloff_params) + const ThirdBody& tbody) + : FalloffReaction(reactants_, products_, low_rate, high_rate, tbody) { reaction_type = CHEMACT_RXN; } @@ -300,26 +295,28 @@ void readFalloff(FalloffReaction& R, const XML_Node& rc_node) { XML_Node& falloff = rc_node.child("falloff"); std::vector p; + vector_fp falloff_parameters; getStringArray(falloff, p); size_t np = p.size(); for (size_t n = 0; n < np; n++) { - R.falloff_parameters.push_back(fpValueCheck(p[n])); + falloff_parameters.push_back(fpValueCheck(p[n])); } + int falloff_type = 0; if (lowercase(falloff["type"]) == "lindemann") { - R.falloff_type = SIMPLE_FALLOFF; + falloff_type = SIMPLE_FALLOFF; if (np != 0) { throw CanteraError("readFalloff", "Lindemann parameterization " "takes no parameters, but " + int2str(np) + "were given"); } } else if (lowercase(falloff["type"]) == "troe") { - R.falloff_type = TROE_FALLOFF; + falloff_type = TROE_FALLOFF; if (np != 3 && np != 4) { throw CanteraError("readFalloff", "Troe parameterization takes " "3 or 4 parameters, but " + int2str(np) + "were given"); } } else if (lowercase(falloff["type"]) == "sri") { - R.falloff_type = SRI_FALLOFF; + falloff_type = SRI_FALLOFF; if (np != 3 && np != 5) { throw CanteraError("readFalloff", "SRI parameterization takes " "3 or 5 parameters, but " + int2str(np) + "were given"); @@ -328,6 +325,7 @@ void readFalloff(FalloffReaction& R, const XML_Node& rc_node) throw CanteraError("readFalloff", "Unrecognized falloff type: '" + falloff["type"] + "'"); } + R.falloff = newFalloff(falloff_type, falloff_parameters); } void readEfficiencies(ThirdBody& tbody, const XML_Node& rc_node) diff --git a/test/kinetics/kineticsFromScratch.cpp b/test/kinetics/kineticsFromScratch.cpp index bcf1f789e..76bbc0754 100644 --- a/test/kinetics/kineticsFromScratch.cpp +++ b/test/kinetics/kineticsFromScratch.cpp @@ -125,8 +125,8 @@ TEST_F(KineticsFromScratch, add_falloff_reaction) ThirdBody tbody; tbody.efficiencies = parseCompString("AR:0.7 H2:2.0 H2O:6.0"); shared_ptr R(new FalloffReaction(reac, prod, low_rate, - high_rate, tbody, TROE_FALLOFF, - falloff_params)); + high_rate, tbody)); + R->falloff = newFalloff(TROE_FALLOFF, falloff_params); kin.addReaction(R); kin.finalize(); check_rates(2);