Skip to content

Commit

Permalink
[Kinetics] Ensure explicit types are serialized
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and speth committed Jul 17, 2022
1 parent 2454968 commit 3613251
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
5 changes: 3 additions & 2 deletions include/cantera/kinetics/Reaction.h
Expand Up @@ -202,6 +202,9 @@ class Reaction
//! Flag indicating whether reaction is set up correctly
bool m_valid = true;

//! Flag indicating that serialization uses explicit type
bool m_explicit_rate = false;

//! Reaction rate used by generic reactions
shared_ptr<ReactionRate> m_rate;

Expand Down Expand Up @@ -311,15 +314,13 @@ class FalloffReaction : public Reaction
//! Create a new empty Reaction object
/*!
* @param type string identifying type of reaction.
* @deprecated To be removed after Cantera 3.0. Use explicit constructor instead
*/
unique_ptr<Reaction> newReaction(const std::string& type);

//! Create a new Reaction object using the specified parameters
/*!
* @param rxn_node AnyMap node describing reaction.
* @param kin kinetics manager
* @deprecated To be removed after Cantera 3.0. Use explicit constructor instead
*/
unique_ptr<Reaction> newReaction(const AnyMap& rxn_node,
const Kinetics& kin);
Expand Down
7 changes: 3 additions & 4 deletions src/kinetics/KineticsFactory.cpp
Expand Up @@ -154,7 +154,7 @@ void addReactions(Kinetics& kin, const AnyMap& phaseNode, const AnyMap& rootNode
rootNode.getString("__file__", ""));
for (const auto& R : reactions[node].asVector<AnyMap>()) {
try {
kin.addReaction(shared_ptr<Reaction>(new Reaction(R, kin)), false);
kin.addReaction(newReaction(R, kin), false);
} catch (CanteraError& err) {
fmt_append(add_rxn_err, "{}", err.what());
}
Expand All @@ -164,13 +164,12 @@ void addReactions(Kinetics& kin, const AnyMap& phaseNode, const AnyMap& rootNode
for (const auto& R : rootNode.at(sections[i]).asVector<AnyMap>()) {
#ifdef NDEBUG
try {
kin.addReaction(
shared_ptr<Reaction>(new Reaction(R, kin)), false);
kin.addReaction(newReaction(R, kin), false);
} catch (CanteraError& err) {
fmt_append(add_rxn_err, "{}", err.what());
}
#else
kin.addReaction(shared_ptr<Reaction>(new Reaction(R, kin)), false);
kin.addReaction(newReaction(R, kin), false);
#endif
}
}
Expand Down
47 changes: 26 additions & 21 deletions src/kinetics/Reaction.cpp
Expand Up @@ -162,6 +162,13 @@ void Reaction::getParameters(AnyMap& reactionNode) const
std::string type = reactionNode["type"].asString();
if (type == "pressure-dependent-Arrhenius") {
// skip
} else if (m_explicit_rate && ba::ends_with(type, "Arrhenius")) {
// retain type information
if (m_third_body) {
reactionNode["type"] = "three-body";
} else {
reactionNode["type"] = "elementary";
}
} else if (ba::ends_with(type, "Arrhenius")) {
reactionNode.erase("type");
} else if (ba::ends_with(type, "Blowers-Masel")) {
Expand Down Expand Up @@ -218,15 +225,7 @@ void Reaction::setRate(shared_ptr<ReactionRate> rate)

std::string rate_type = input.getString("type", "");
if (m_third_body) {
if (std::dynamic_pointer_cast<ChebyshevRate>(m_rate)) {
warn_deprecated("Chebyshev reaction equation", input, "Specifying '(+M)' "
"in the reaction equation for Chebyshev reactions is deprecated.");
m_third_body.reset();
} else if (std::dynamic_pointer_cast<PlogRate>(m_rate)) {
throw InputFileError("Reaction::setRate", input,
"Found superfluous '{}' in pressure-dependent-Arrhenius reaction.",
m_third_body->name());
} else if (std::dynamic_pointer_cast<FalloffRate>(m_rate)) {
if (std::dynamic_pointer_cast<FalloffRate>(m_rate)) {
m_third_body->mass_action = false;
} else if (std::dynamic_pointer_cast<TwoTempPlasmaRate>(m_rate)) {
// two-temperature-plasma rates do not support third-body colliders
Expand All @@ -240,6 +239,14 @@ void Reaction::setRate(shared_ptr<ReactionRate> rate)
throw InputFileError("Reaction::setRate", input,
"Reactants for reaction '{}'\n"
"contain multiple third body colliders.", equation());
} else if (std::dynamic_pointer_cast<ChebyshevRate>(m_rate)) {
warn_deprecated("Chebyshev reaction equation", input, "Specifying '(+M)' "
"in the reaction equation for Chebyshev reactions is deprecated.");
m_third_body.reset();
} else if (std::dynamic_pointer_cast<PlogRate>(m_rate)) {
throw InputFileError("Reaction::setRate", input,
"Found superfluous '{}' in pressure-dependent-Arrhenius reaction.",
m_third_body->name());
}
} else {
if (std::dynamic_pointer_cast<FalloffRate>(m_rate)) {
Expand Down Expand Up @@ -310,11 +317,15 @@ void Reaction::setEquation(const std::string& equation, const Kinetics* kin)
parseReactionEquation(*this, equation, input, kin);

std::string rate_type = input.getString("type", "");
if (rate_type == "two-temperature-plasma") {
// two-temperature-plasma reactions do not use third bodies
return;
if (rate_type == "three-body") {
// state type when serializing
m_explicit_rate = true;
} else if (rate_type == "elementary") {
// user override
m_explicit_rate = true;
return;
} else if (rate_type == "two-temperature-plasma") {
// two-temperature-plasma reactions do not use third bodies
return;
} else if (kin && kin->thermo(kin->reactionPhaseIndex()).nDim() != 3) {
// interface reactions
Expand Down Expand Up @@ -690,12 +701,10 @@ void ThirdBody::setParameters(const AnyMap& node)
void ThirdBody::getParameters(AnyMap& node) const
{
if (m_name == "M") {
if (mass_action) {
// this can be removed
node["type"] = "three-body";
if (efficiencies.size()) {
node["efficiencies"] = efficiencies;
node["efficiencies"].setFlowStyle();
}
node["efficiencies"] = efficiencies;
node["efficiencies"].setFlowStyle();
if (default_efficiency != 1.0) {
node["default-efficiency"] = default_efficiency;
}
Expand Down Expand Up @@ -810,15 +819,11 @@ FalloffReaction::FalloffReaction(const AnyMap& node, const Kinetics& kin)

unique_ptr<Reaction> newReaction(const std::string& type)
{
warn_deprecated("newReaction",
"To be removed after Cantera 3.0. Use explicit constructor instead.");
return unique_ptr<Reaction>(new Reaction());
}

unique_ptr<Reaction> newReaction(const AnyMap& rxn_node, const Kinetics& kin)
{
warn_deprecated("newReaction",
"To be removed after Cantera 3.0. Use explicit constructor instead.");
return unique_ptr<Reaction>(new Reaction(rxn_node, kin));
}

Expand Down

0 comments on commit 3613251

Please sign in to comment.