Skip to content

Commit

Permalink
[Kinetics] deprecate magic numbers for falloff reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ingmar Schoegl committed Oct 25, 2019
1 parent 10af288 commit 36e6b43
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 32 deletions.
23 changes: 23 additions & 0 deletions include/cantera/kinetics/Falloff.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define CT_FALLOFF_H

#include "cantera/kinetics/reaction_defs.h"
#include "cantera/base/global.h"

namespace Cantera
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
20 changes: 20 additions & 0 deletions include/cantera/kinetics/FalloffFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,23 @@ class FalloffFactory : public Factory<Falloff>
* @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;
Expand All @@ -73,7 +87,13 @@ class FalloffFactory : public Factory<Falloff>
};

//! @copydoc FalloffFactory::newFalloff
/*!
* @deprecated To be removed after Cantera 2.5.
*/
shared_ptr<Falloff> newFalloff(int type, const vector_fp& c);

//! @copydoc FalloffFactory::newFalloff
shared_ptr<Falloff> newFalloff(std::string type, const vector_fp& c);

}
#endif
3 changes: 3 additions & 0 deletions include/cantera/kinetics/reaction_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions interfaces/cython/cantera/_cantera.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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":
Expand Down
30 changes: 9 additions & 21 deletions interfaces/cython/cantera/reaction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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:
Expand All @@ -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. """
Expand Down Expand Up @@ -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):
Expand All @@ -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))
Expand Down
16 changes: 16 additions & 0 deletions src/kinetics/FalloffFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, std::string> types {
{SIMPLE_FALLOFF, "Simple"},
{TROE_FALLOFF, "Troe"},
Expand All @@ -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<Falloff> newFalloff(int type, const vector_fp& c)
{
shared_ptr<Falloff> f(FalloffFactory::factory()->newFalloff(type, c));
return f;
}

shared_ptr<Falloff> newFalloff(std::string type, const vector_fp& c)
{
shared_ptr<Falloff> f(FalloffFactory::factory()->newFalloff(type, c));
return f;
}

}
14 changes: 6 additions & 8 deletions src/kinetics/Reaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<AnyMap>();
vector_fp params{
Expand All @@ -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", {});
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/kinetics/kineticsFromScratch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<FalloffReaction>(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);
}
Expand Down

0 comments on commit 36e6b43

Please sign in to comment.