From 36e6b43c201086b154474d35ddbb65d988912bfe Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Thu, 24 Oct 2019 23:17:59 -0500 Subject: [PATCH] [Kinetics] deprecate magic numbers for falloff reactions --- include/cantera/kinetics/Falloff.h | 23 +++++++++++++++++ include/cantera/kinetics/FalloffFactory.h | 20 +++++++++++++++ include/cantera/kinetics/reaction_defs.h | 3 +++ interfaces/cython/cantera/_cantera.pxd | 4 +-- interfaces/cython/cantera/reaction.pyx | 30 +++++++---------------- src/kinetics/FalloffFactory.cpp | 16 ++++++++++++ src/kinetics/Reaction.cpp | 14 +++++------ test/kinetics/kineticsFromScratch.cpp | 2 +- 8 files changed, 80 insertions(+), 32 deletions(-) diff --git a/include/cantera/kinetics/Falloff.h b/include/cantera/kinetics/Falloff.h index 648bae385ad..086b256af4d 100644 --- a/include/cantera/kinetics/Falloff.h +++ b/include/cantera/kinetics/Falloff.h @@ -5,6 +5,7 @@ #define CT_FALLOFF_H #include "cantera/kinetics/reaction_defs.h" +#include "cantera/base/global.h" namespace Cantera { @@ -74,8 +75,18 @@ class Falloff return 0; } + //! Return a string representing the type of the Falloff parameterization. + virtual std::string type() const { + return "Falloff"; + } + //! Return an integer representing the type of the Falloff parameterization. + /*! + * @deprecated To be removed after Cantera 2.5. + */ virtual int getType() const { + warn_deprecated("Falloff::getType()", + "Replaced by Falloff::type(). To be removed after Cantera 2.5."); return SIMPLE_FALLOFF; } @@ -146,7 +157,13 @@ class Troe : public Falloff return 1; } + virtual std::string type() const { + return "Troe"; + } + virtual int getType() const { + warn_deprecated("Troe::getType()", + "Replaced by Troe::type(). To be removed after Cantera 2.5."); return TROE_FALLOFF; } @@ -220,7 +237,13 @@ class SRI : public Falloff return 2; } + virtual std::string type() const { + return "SRI"; + } + virtual int getType() const { + warn_deprecated("SRI::getType()", + "Replaced by SRI::type(). To be removed after Cantera 2.5."); return SRI_FALLOFF; } diff --git a/include/cantera/kinetics/FalloffFactory.h b/include/cantera/kinetics/FalloffFactory.h index a0ff38f1b02..476c8304da7 100644 --- a/include/cantera/kinetics/FalloffFactory.h +++ b/include/cantera/kinetics/FalloffFactory.h @@ -58,9 +58,23 @@ class FalloffFactory : public Factory * @param c input vector of doubles which populates the falloff * parameterization. * @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); + //! Return a pointer to a new falloff function calculator. + /*! + * @param string 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(std::string type, const vector_fp& c); + private: //! Pointer to the single instance of the factory static FalloffFactory* s_factory; @@ -73,7 +87,13 @@ class FalloffFactory : public Factory }; //! @copydoc FalloffFactory::newFalloff +/*! + * @deprecated To be removed after Cantera 2.5. + */ shared_ptr newFalloff(int type, const vector_fp& c); +//! @copydoc FalloffFactory::newFalloff +shared_ptr newFalloff(std::string type, const vector_fp& c); + } #endif diff --git a/include/cantera/kinetics/reaction_defs.h b/include/cantera/kinetics/reaction_defs.h index e9eabac1610..2d6ef59be68 100644 --- a/include/cantera/kinetics/reaction_defs.h +++ b/include/cantera/kinetics/reaction_defs.h @@ -134,6 +134,9 @@ const int CHEBYSHEV_REACTION_RATECOEFF_TYPE = 8; //@} /** @name Falloff Function Types + * + * Magic numbers + * @deprecated To be removed after Cantera 2.5. */ //@{ const int SIMPLE_FALLOFF = 100; diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 9fbd48b6c6b..254614a5e20 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -324,7 +324,7 @@ cdef extern from "cantera/kinetics/Reaction.h" namespace "Cantera": size_t workSize() size_t nParameters() - int getType() + string type() void getParameters(double*) cdef cppclass CxxFalloffReaction "Cantera::FalloffReaction" (CxxReaction): @@ -375,7 +375,7 @@ cdef extern from "cantera/kinetics/Reaction.h" namespace "Cantera": string sticking_species 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 cppclass CxxKinetics "Cantera::Kinetics": diff --git a/interfaces/cython/cantera/reaction.pyx b/interfaces/cython/cantera/reaction.pyx index 91684584c40..6594268f878 100644 --- a/interfaces/cython/cantera/reaction.pyx +++ b/interfaces/cython/cantera/reaction.pyx @@ -10,10 +10,6 @@ cdef extern from "cantera/kinetics/reaction_defs.h" namespace "Cantera": cdef int CHEMACT_RXN cdef int INTERFACE_RXN - cdef int SIMPLE_FALLOFF - cdef int TROE_FALLOFF - cdef int SRI_FALLOFF - cdef class Reaction: """ @@ -422,7 +418,7 @@ cdef class Falloff: :param init: Used internally when wrapping :ct:`Falloff` objects returned from C++. """ - falloff_type = SIMPLE_FALLOFF + falloff_type = "Falloff" def __cinit__(self, params=(), init=True): if not init: @@ -431,21 +427,13 @@ cdef class Falloff: cdef vector[double] c for p in params: c.push_back(p) - self._falloff = CxxNewFalloff(self.falloff_type, c) + self._falloff = CxxNewFalloff(stringify(self.falloff_type), c) self.falloff = self._falloff.get() property type: """ A string defining the type of the falloff parameterization """ def __get__(self): - cdef int falloff_type = self.falloff.getType() - if falloff_type == SIMPLE_FALLOFF: - return "Simple" - elif falloff_type == TROE_FALLOFF: - return "Troe" - elif falloff_type == SRI_FALLOFF: - return "SRI" - else: - return "unknown" + return pystr(self.falloff.type()) property parameters: """ 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 the final parameter is optional (with a default value of 0). """ - falloff_type = TROE_FALLOFF + falloff_type = "Troe" cdef class SriFalloff(Falloff): @@ -486,16 +474,16 @@ cdef class SriFalloff(Falloff): two parameters are optional (with default values of 1 and 0, respectively). """ - falloff_type = SRI_FALLOFF + falloff_type = "SRI" cdef wrapFalloff(shared_ptr[CxxFalloff] falloff): - cdef int falloff_type = falloff.get().getType() - if falloff_type == SIMPLE_FALLOFF: + falloff_type = pystr(falloff.get().type()) + if falloff_type == "Falloff": f = Falloff(init=False) - elif falloff_type == TROE_FALLOFF: + elif falloff_type == "Troe": f = TroeFalloff(init=False) - elif falloff_type == SRI_FALLOFF: + elif falloff_type == "SRI": f = SriFalloff(init=False) else: warnings.warn('Unknown falloff type: {0}'.format(falloff_type)) diff --git a/src/kinetics/FalloffFactory.cpp b/src/kinetics/FalloffFactory.cpp index 64ce29a3ed1..dba7f8870cf 100644 --- a/src/kinetics/FalloffFactory.cpp +++ b/src/kinetics/FalloffFactory.cpp @@ -23,6 +23,9 @@ FalloffFactory::FalloffFactory() 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 types { {SIMPLE_FALLOFF, "Simple"}, {TROE_FALLOFF, "Troe"}, @@ -34,10 +37,23 @@ Falloff* FalloffFactory::newFalloff(int type, const vector_fp& c) return f; } +Falloff* FalloffFactory::newFalloff(std::string type, const vector_fp& c) +{ + Falloff* f = create(type); + f->init(c); + return f; +} + shared_ptr newFalloff(int type, const vector_fp& c) { shared_ptr f(FalloffFactory::factory()->newFalloff(type, c)); return f; } +shared_ptr newFalloff(std::string type, const vector_fp& c) +{ + shared_ptr f(FalloffFactory::factory()->newFalloff(type, c)); + return f; +} + } diff --git a/src/kinetics/Reaction.cpp b/src/kinetics/Reaction.cpp index cb4e6f6dd33..e742336b2f4 100644 --- a/src/kinetics/Reaction.cpp +++ b/src/kinetics/Reaction.cpp @@ -359,30 +359,28 @@ void readFalloff(FalloffReaction& R, const XML_Node& rc_node) falloff_parameters.push_back(fpValueCheck(p[n])); } - int falloff_type = 0; if (caseInsensitiveEquals(falloff["type"], "lindemann")) { - falloff_type = SIMPLE_FALLOFF; if (np != 0) { throw CanteraError("readFalloff", "Lindemann parameterization " "takes no parameters, but {} were given", np); } + R.falloff = newFalloff("Simple", falloff_parameters); } else if (caseInsensitiveEquals(falloff["type"], "troe")) { - falloff_type = TROE_FALLOFF; if (np != 3 && np != 4) { throw CanteraError("readFalloff", "Troe parameterization takes " "3 or 4 parameters, but {} were given", np); } + R.falloff = newFalloff("Troe", falloff_parameters); } else if (caseInsensitiveEquals(falloff["type"], "sri")) { - falloff_type = SRI_FALLOFF; if (np != 3 && np != 5) { throw CanteraError("readFalloff", "SRI parameterization takes " "3 or 5 parameters, but {} were given", np); } + R.falloff = newFalloff("SRI", falloff_parameters); } else { throw CanteraError("readFalloff", "Unrecognized falloff type: '{}'", falloff["type"]); } - R.falloff = newFalloff(falloff_type, falloff_parameters); } void readFalloff(FalloffReaction& R, const AnyMap& node) @@ -395,7 +393,7 @@ void readFalloff(FalloffReaction& R, const AnyMap& node) f["T1"].asDouble(), f.getDouble("T2", 0.0) }; - R.falloff = newFalloff(TROE_FALLOFF, params); + R.falloff = newFalloff("Troe", params); } else if (node.hasKey("SRI")) { auto& f = node["SRI"].as(); vector_fp params{ @@ -405,9 +403,9 @@ void readFalloff(FalloffReaction& R, const AnyMap& node) f.getDouble("D", 1.0), f.getDouble("E", 0.0) }; - R.falloff = newFalloff(SRI_FALLOFF, params); + R.falloff = newFalloff("SRI", params); } else { - R.falloff = newFalloff(SIMPLE_FALLOFF, {}); + R.falloff = newFalloff("Simple", {}); } } diff --git a/test/kinetics/kineticsFromScratch.cpp b/test/kinetics/kineticsFromScratch.cpp index 776170a9b31..acc71bb472f 100644 --- a/test/kinetics/kineticsFromScratch.cpp +++ b/test/kinetics/kineticsFromScratch.cpp @@ -118,7 +118,7 @@ TEST_F(KineticsFromScratch, add_falloff_reaction) ThirdBody tbody; tbody.efficiencies = parseCompString("AR:0.7 H2:2.0 H2O:6.0"); auto R = make_shared(reac, prod, low_rate, high_rate, tbody); - R->falloff = newFalloff(TROE_FALLOFF, falloff_params); + R->falloff = newFalloff("Troe", falloff_params); kin.addReaction(R); check_rates(2); }