[Kinetics] deprecate magic numbers for falloff reactions

This commit is contained in:
Ingmar Schoegl 2019-10-24 23:17:59 -05:00 committed by Ray Speth
parent 58063b498a
commit e2bfba328c
8 changed files with 82 additions and 33 deletions

View file

@ -5,6 +5,7 @@
#define CT_FALLOFF_H #define CT_FALLOFF_H
#include "cantera/kinetics/reaction_defs.h" #include "cantera/kinetics/reaction_defs.h"
#include "cantera/base/global.h"
namespace Cantera namespace Cantera
{ {
@ -74,8 +75,18 @@ public:
return 0; return 0;
} }
//! Return a string representing the type of the Falloff parameterization.
virtual std::string type() const {
return "Lindemann";
}
//! Return an integer representing the type of the Falloff parameterization. //! Return an integer representing the type of the Falloff parameterization.
/*!
* @deprecated To be removed after Cantera 2.5.
*/
virtual int getType() const { virtual int getType() const {
warn_deprecated("Falloff::getType()",
"Replaced by Falloff::type(). To be removed after Cantera 2.5.");
return SIMPLE_FALLOFF; return SIMPLE_FALLOFF;
} }
@ -146,7 +157,13 @@ public:
return 1; return 1;
} }
virtual std::string type() const {
return "Troe";
}
virtual int getType() const { virtual int getType() const {
warn_deprecated("Troe::getType()",
"Replaced by Troe::type(). To be removed after Cantera 2.5.");
return TROE_FALLOFF; return TROE_FALLOFF;
} }
@ -220,7 +237,13 @@ public:
return 2; return 2;
} }
virtual std::string type() const {
return "SRI";
}
virtual int getType() const { virtual int getType() const {
warn_deprecated("SRI::getType()",
"Replaced by SRI::type(). To be removed after Cantera 2.5.");
return SRI_FALLOFF; return SRI_FALLOFF;
} }

View file

@ -58,9 +58,23 @@ public:
* @param c input vector of doubles which populates the falloff * @param c input vector of doubles which populates the falloff
* parameterization. * parameterization.
* @returns a pointer to a new Falloff class. * @returns a pointer to a new Falloff class.
*
* @deprecated To be removed after Cantera 2.5.
*/ */
virtual Falloff* newFalloff(int type, const vector_fp& c); virtual Falloff* newFalloff(int type, const vector_fp& c);
//! Return a pointer to a new falloff function calculator.
/*!
* @param type String identifier specifying the type of falloff function.
* The standard types match class names defined in Falloff.h.
* A factory class derived from FalloffFactory may define
* other types as well.
* @param c input vector of doubles which populates the falloff
* parameterization.
* @returns a pointer to a new Falloff class.
*/
virtual Falloff* newFalloff(const std::string& type, const vector_fp& c);
private: private:
//! Pointer to the single instance of the factory //! Pointer to the single instance of the factory
static FalloffFactory* s_factory; static FalloffFactory* s_factory;
@ -73,7 +87,13 @@ private:
}; };
//! @copydoc FalloffFactory::newFalloff //! @copydoc FalloffFactory::newFalloff
/*!
* @deprecated To be removed after Cantera 2.5.
*/
shared_ptr<Falloff> newFalloff(int type, const vector_fp& c); shared_ptr<Falloff> newFalloff(int type, const vector_fp& c);
//! @copydoc FalloffFactory::newFalloff
shared_ptr<Falloff> newFalloff(const std::string& type, const vector_fp& c);
} }
#endif #endif

View file

@ -134,6 +134,9 @@ const int CHEBYSHEV_REACTION_RATECOEFF_TYPE = 8;
//@} //@}
/** @name Falloff Function Types /** @name Falloff Function Types
*
* Magic numbers
* @deprecated To be removed after Cantera 2.5.
*/ */
//@{ //@{
const int SIMPLE_FALLOFF = 100; const int SIMPLE_FALLOFF = 100;

View file

@ -327,7 +327,7 @@ cdef extern from "cantera/kinetics/Reaction.h" namespace "Cantera":
size_t workSize() size_t workSize()
size_t nParameters() size_t nParameters()
int getType() string type()
void getParameters(double*) void getParameters(double*)
cdef cppclass CxxFalloffReaction "Cantera::FalloffReaction" (CxxReaction): cdef cppclass CxxFalloffReaction "Cantera::FalloffReaction" (CxxReaction):
@ -378,7 +378,7 @@ cdef extern from "cantera/kinetics/Reaction.h" namespace "Cantera":
string sticking_species string sticking_species
cdef extern from "cantera/kinetics/FalloffFactory.h" namespace "Cantera": cdef extern from "cantera/kinetics/FalloffFactory.h" namespace "Cantera":
cdef shared_ptr[CxxFalloff] CxxNewFalloff "Cantera::newFalloff" (int, vector[double]) except + cdef shared_ptr[CxxFalloff] CxxNewFalloff "Cantera::newFalloff" (string, vector[double]) except +translate_exception
cdef extern from "cantera/kinetics/Kinetics.h" namespace "Cantera": cdef extern from "cantera/kinetics/Kinetics.h" namespace "Cantera":
cdef cppclass CxxKinetics "Cantera::Kinetics": cdef cppclass CxxKinetics "Cantera::Kinetics":

View file

@ -10,10 +10,6 @@ cdef extern from "cantera/kinetics/reaction_defs.h" namespace "Cantera":
cdef int CHEMACT_RXN cdef int CHEMACT_RXN
cdef int INTERFACE_RXN cdef int INTERFACE_RXN
cdef int SIMPLE_FALLOFF
cdef int TROE_FALLOFF
cdef int SRI_FALLOFF
cdef class Reaction: cdef class Reaction:
""" """
@ -422,7 +418,7 @@ cdef class Falloff:
:param init: :param init:
Used internally when wrapping :ct:`Falloff` objects returned from C++. Used internally when wrapping :ct:`Falloff` objects returned from C++.
""" """
falloff_type = SIMPLE_FALLOFF falloff_type = "Lindemann"
def __cinit__(self, params=(), init=True): def __cinit__(self, params=(), init=True):
if not init: if not init:
@ -431,21 +427,13 @@ cdef class Falloff:
cdef vector[double] c cdef vector[double] c
for p in params: for p in params:
c.push_back(p) c.push_back(p)
self._falloff = CxxNewFalloff(self.falloff_type, c) self._falloff = CxxNewFalloff(stringify(self.falloff_type), c)
self.falloff = self._falloff.get() self.falloff = self._falloff.get()
property type: property type:
""" A string defining the type of the falloff parameterization """ """ A string defining the type of the falloff parameterization """
def __get__(self): def __get__(self):
cdef int falloff_type = self.falloff.getType() return pystr(self.falloff.type())
if falloff_type == SIMPLE_FALLOFF:
return "Simple"
elif falloff_type == TROE_FALLOFF:
return "Troe"
elif falloff_type == SRI_FALLOFF:
return "SRI"
else:
return "unknown"
property parameters: property parameters:
""" The array of parameters used to define this falloff function. """ """ The array of parameters used to define this falloff function. """
@ -474,7 +462,7 @@ cdef class TroeFalloff(Falloff):
An array of 3 or 4 parameters: :math:`[a, T^{***}, T^*, T^{**}]` where An array of 3 or 4 parameters: :math:`[a, T^{***}, T^*, T^{**}]` where
the final parameter is optional (with a default value of 0). the final parameter is optional (with a default value of 0).
""" """
falloff_type = TROE_FALLOFF falloff_type = "Troe"
cdef class SriFalloff(Falloff): cdef class SriFalloff(Falloff):
@ -486,16 +474,16 @@ cdef class SriFalloff(Falloff):
two parameters are optional (with default values of 1 and 0, two parameters are optional (with default values of 1 and 0,
respectively). respectively).
""" """
falloff_type = SRI_FALLOFF falloff_type = "SRI"
cdef wrapFalloff(shared_ptr[CxxFalloff] falloff): cdef wrapFalloff(shared_ptr[CxxFalloff] falloff):
cdef int falloff_type = falloff.get().getType() falloff_type = pystr(falloff.get().type())
if falloff_type == SIMPLE_FALLOFF: if falloff_type in ["Lindemann", "Simple"]:
f = Falloff(init=False) f = Falloff(init=False)
elif falloff_type == TROE_FALLOFF: elif falloff_type == "Troe":
f = TroeFalloff(init=False) f = TroeFalloff(init=False)
elif falloff_type == SRI_FALLOFF: elif falloff_type == "SRI":
f = SriFalloff(init=False) f = SriFalloff(init=False)
else: else:
warnings.warn('Unknown falloff type: {0}'.format(falloff_type)) warnings.warn('Unknown falloff type: {0}'.format(falloff_type))

View file

@ -16,13 +16,17 @@ std::mutex FalloffFactory::falloff_mutex;
FalloffFactory::FalloffFactory() FalloffFactory::FalloffFactory()
{ {
reg("Simple", []() { return new Falloff(); }); reg("Lindemann", []() { return new Falloff(); });
m_synonyms["Simple"] = "Lindemann";
reg("Troe", []() { return new Troe(); }); reg("Troe", []() { return new Troe(); });
reg("SRI", []() { return new SRI(); }); reg("SRI", []() { return new SRI(); });
} }
Falloff* FalloffFactory::newFalloff(int type, const vector_fp& c) Falloff* FalloffFactory::newFalloff(int type, const vector_fp& c)
{ {
warn_deprecated("FalloffFactory::newFalloff",
"Instantiation using magic numbers is deprecated; use string "
"identifier instead. To be removed after Cantera 2.5.");
static const std::unordered_map<int, std::string> types { static const std::unordered_map<int, std::string> types {
{SIMPLE_FALLOFF, "Simple"}, {SIMPLE_FALLOFF, "Simple"},
{TROE_FALLOFF, "Troe"}, {TROE_FALLOFF, "Troe"},
@ -34,10 +38,23 @@ Falloff* FalloffFactory::newFalloff(int type, const vector_fp& c)
return f; return f;
} }
Falloff* FalloffFactory::newFalloff(const std::string& type, const vector_fp& c)
{
Falloff* f = create(type);
f->init(c);
return f;
}
shared_ptr<Falloff> newFalloff(int type, const vector_fp& c) shared_ptr<Falloff> newFalloff(int type, const vector_fp& c)
{ {
shared_ptr<Falloff> f(FalloffFactory::factory()->newFalloff(type, c)); shared_ptr<Falloff> f(FalloffFactory::factory()->newFalloff(type, c));
return f; return f;
} }
shared_ptr<Falloff> newFalloff(const std::string& type, const vector_fp& c)
{
shared_ptr<Falloff> f(FalloffFactory::factory()->newFalloff(type, c));
return f;
}
} }

View file

@ -359,30 +359,28 @@ void readFalloff(FalloffReaction& R, const XML_Node& rc_node)
falloff_parameters.push_back(fpValueCheck(p[n])); falloff_parameters.push_back(fpValueCheck(p[n]));
} }
int falloff_type = 0;
if (caseInsensitiveEquals(falloff["type"], "lindemann")) { if (caseInsensitiveEquals(falloff["type"], "lindemann")) {
falloff_type = SIMPLE_FALLOFF;
if (np != 0) { if (np != 0) {
throw CanteraError("readFalloff", "Lindemann parameterization " throw CanteraError("readFalloff", "Lindemann parameterization "
"takes no parameters, but {} were given", np); "takes no parameters, but {} were given", np);
} }
R.falloff = newFalloff("Lindemann", falloff_parameters);
} else if (caseInsensitiveEquals(falloff["type"], "troe")) { } else if (caseInsensitiveEquals(falloff["type"], "troe")) {
falloff_type = TROE_FALLOFF;
if (np != 3 && np != 4) { if (np != 3 && np != 4) {
throw CanteraError("readFalloff", "Troe parameterization takes " throw CanteraError("readFalloff", "Troe parameterization takes "
"3 or 4 parameters, but {} were given", np); "3 or 4 parameters, but {} were given", np);
} }
R.falloff = newFalloff("Troe", falloff_parameters);
} else if (caseInsensitiveEquals(falloff["type"], "sri")) { } else if (caseInsensitiveEquals(falloff["type"], "sri")) {
falloff_type = SRI_FALLOFF;
if (np != 3 && np != 5) { if (np != 3 && np != 5) {
throw CanteraError("readFalloff", "SRI parameterization takes " throw CanteraError("readFalloff", "SRI parameterization takes "
"3 or 5 parameters, but {} were given", np); "3 or 5 parameters, but {} were given", np);
} }
R.falloff = newFalloff("SRI", falloff_parameters);
} else { } else {
throw CanteraError("readFalloff", "Unrecognized falloff type: '{}'", throw CanteraError("readFalloff", "Unrecognized falloff type: '{}'",
falloff["type"]); falloff["type"]);
} }
R.falloff = newFalloff(falloff_type, falloff_parameters);
} }
void readFalloff(FalloffReaction& R, const AnyMap& node) void readFalloff(FalloffReaction& R, const AnyMap& node)
@ -395,7 +393,7 @@ void readFalloff(FalloffReaction& R, const AnyMap& node)
f["T1"].asDouble(), f["T1"].asDouble(),
f.getDouble("T2", 0.0) f.getDouble("T2", 0.0)
}; };
R.falloff = newFalloff(TROE_FALLOFF, params); R.falloff = newFalloff("Troe", params);
} else if (node.hasKey("SRI")) { } else if (node.hasKey("SRI")) {
auto& f = node["SRI"].as<AnyMap>(); auto& f = node["SRI"].as<AnyMap>();
vector_fp params{ vector_fp params{
@ -405,9 +403,9 @@ void readFalloff(FalloffReaction& R, const AnyMap& node)
f.getDouble("D", 1.0), f.getDouble("D", 1.0),
f.getDouble("E", 0.0) f.getDouble("E", 0.0)
}; };
R.falloff = newFalloff(SRI_FALLOFF, params); R.falloff = newFalloff("SRI", params);
} else { } else {
R.falloff = newFalloff(SIMPLE_FALLOFF, {}); R.falloff = newFalloff("Lindemann", {});
} }
} }

View file

@ -118,7 +118,7 @@ TEST_F(KineticsFromScratch, add_falloff_reaction)
ThirdBody tbody; ThirdBody tbody;
tbody.efficiencies = parseCompString("AR:0.7 H2:2.0 H2O:6.0"); tbody.efficiencies = parseCompString("AR:0.7 H2:2.0 H2O:6.0");
auto R = make_shared<FalloffReaction>(reac, prod, low_rate, high_rate, tbody); auto R = make_shared<FalloffReaction>(reac, prod, low_rate, high_rate, tbody);
R->falloff = newFalloff(TROE_FALLOFF, falloff_params); R->falloff = newFalloff("Troe", falloff_params);
kin.addReaction(R); kin.addReaction(R);
check_rates(2); check_rates(2);
} }