Cantera 2.6.0
Arrhenius.cpp
Go to the documentation of this file.
1//! @file Arrhenius.cpp
2
3// This file is part of Cantera. See License.txt in the top-level directory or
4// at https://cantera.org/license.txt for license and copyright information.
5
8
9namespace Cantera
10{
11
13 : m_negativeA_ok(false)
14 , m_A(NAN)
15 , m_b(NAN)
16 , m_Ea_R(0.)
17 , m_E4_R(0.)
18 , m_logA(NAN)
19 , m_order(NAN)
20 , m_rate_units(Units(0.))
21{
22}
23
24ArrheniusBase::ArrheniusBase(double A, double b, double Ea)
25 : m_negativeA_ok(false)
26 , m_A(A)
27 , m_b(b)
28 , m_Ea_R(Ea / GasConstant)
29 , m_E4_R(0.)
30 , m_order(NAN)
31 , m_rate_units(Units(0.))
32{
33 if (m_A > 0.0) {
34 m_logA = std::log(m_A);
35 }
36}
37
39 const AnyValue& rate, const UnitSystem& units, const UnitStack& rate_units)
40{
41 m_Ea_R = 0.; // assume zero if not provided
42 m_E4_R = 0.; // assume zero if not provided
43 if (rate.empty()) {
44 m_A = NAN;
45 m_b = NAN;
46 m_logA = NAN;
47 m_order = NAN;
48 m_rate_units = Units(0.);
49 return;
50 }
51
52 setRateUnits(rate_units);
53 if (rate.is<AnyMap>()) {
54
55 auto& rate_map = rate.as<AnyMap>();
56 if (m_rate_units.factor() == 0) {
57 // A zero rate units factor is used as a sentinel to detect
58 // stand-alone reaction rate objects
59 if (rate_map[m_A_str].is<std::string>()) {
60 throw InputFileError("Arrhenius::setRateParameters", rate_map,
61 "Specification of units is not supported for pre-exponential "
62 "factor when\ncreating a standalone 'ReactionRate' object.");
63 }
64 m_A = rate_map[m_A_str].asDouble();
65 } else {
66 m_A = units.convert(rate_map[m_A_str], m_rate_units);
67 }
68 m_b = rate_map[m_b_str].asDouble();
69 if (rate_map.hasKey(m_Ea_str)) {
70 m_Ea_R = units.convertActivationEnergy(rate_map[m_Ea_str], "K");
71 }
72 if (rate_map.hasKey(m_E4_str)) {
73 m_E4_R = units.convertActivationEnergy(rate_map[m_E4_str], "K");
74 }
75 } else {
76 auto& rate_vec = rate.asVector<AnyValue>(2, 4);
77 m_A = units.convert(rate_vec[0], m_rate_units);
78 m_b = rate_vec[1].asDouble();
79 if (rate_vec.size() > 2) {
80 m_Ea_R = units.convertActivationEnergy(rate_vec[2], "K");
81 }
82 if (rate_vec.size() > 3) {
83 m_E4_R = units.convertActivationEnergy(rate_vec[3], "K");
84 }
85 }
86 if (m_A > 0.0) {
87 m_logA = std::log(m_A);
88 }
89}
90
92{
93 if (std::isnan(m_A)) {
94 // Return empty/unmodified AnyMap
95 return;
96 } else if (m_rate_units.factor() != 0.0) {
97 node[m_A_str].setQuantity(m_A, m_rate_units);
98 } else {
99 node[m_A_str] = m_A;
100 // This can't be converted to a different unit system because the dimensions of
101 // the rate constant were not set. Can occur if the reaction was created outside
102 // the context of a Kinetics object and never added to a Kinetics object.
103 node["__unconvertible__"] = true;
104 }
105 node[m_b_str] = m_b;
106 node[m_Ea_str].setQuantity(m_Ea_R, "K", true);
107 if (m_E4_str != "") {
108 node[m_E4_str].setQuantity(m_E4_R, "K", true);
109 }
110 node.setFlowStyle();
111}
112
113void ArrheniusBase::setParameters(const AnyMap& node, const UnitStack& rate_units)
114{
115 ReactionRate::setParameters(node, rate_units);
116 m_negativeA_ok = node.getBool("negative-A", false);
117 if (!node.hasKey("rate-constant")) {
118 setRateParameters(AnyValue(), node.units(), rate_units);
119 return;
120 }
121 setRateParameters(node["rate-constant"], node.units(), rate_units);
122}
123
125 if (m_negativeA_ok) {
126 node["negative-A"] = true;
127 }
128 AnyMap rateNode;
129 getRateParameters(rateNode);
130 if (!rateNode.empty()) {
131 // RateType object is configured
132 node["rate-constant"] = std::move(rateNode);
133 }
134 if (type() != "Arrhenius") {
135 node["type"] = type();
136 }
137}
138
139void ArrheniusBase::check(const std::string& equation, const AnyMap& node)
140{
141 if (!m_negativeA_ok && m_A < 0) {
142 if (equation == "") {
143 throw CanteraError("ArrheniusBase::check",
144 "Detected negative pre-exponential factor (A={}).\n"
145 "Enable 'allowNegativePreExponentialFactor' to suppress "
146 "this message.", m_A);
147 }
148 throw InputFileError("ArrheniusBase::check", node,
149 "Undeclared negative pre-exponential factor found in reaction '{}'",
150 equation);
151 }
152}
153
154void ArrheniusBase::validate(const std::string& equation, const Kinetics& kin)
155{
156 if (isnan(m_A) || isnan(m_b)) {
157 throw CanteraError("ArrheniusBase::validate",
158 "Rate object for reaction '{}' is not configured.", equation);
159 }
160}
161
162bool ArrheniusData::update(const ThermoPhase& phase, const Kinetics& kin)
163{
164 double T = phase.temperature();
165 if (T == temperature) {
166 return false;
167 }
168 update(T);
169 return true;
170}
171
172}
Header for reaction rates that involve Arrhenius-type kinetics.
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
A map of string keys to values whose type can vary at runtime.
Definition: AnyMap.h:399
const UnitSystem & units() const
Return the default units that should be used to convert stored values.
Definition: AnyMap.h:598
bool empty() const
Return boolean indicating whether AnyMap is empty.
Definition: AnyMap.cpp:1401
bool getBool(const std::string &key, bool default_) const
If key exists, return it as a bool, otherwise return default_.
Definition: AnyMap.cpp:1487
void setFlowStyle(bool flow=true)
Use "flow" style when outputting this AnyMap to YAML.
Definition: AnyMap.cpp:1698
bool hasKey(const std::string &key) const
Returns true if the map contains an item named key.
Definition: AnyMap.cpp:1406
A wrapper for a variable whose type is determined at runtime.
Definition: AnyMap.h:84
bool empty() const
Return boolean indicating whether AnyValue is empty.
Definition: AnyMap.cpp:684
const std::vector< T > & asVector(size_t nMin=npos, size_t nMax=npos) const
Return the held value, if it is a vector of type T.
Definition: AnyMap.inl.h:72
bool is() const
Returns true if the held value is of the specified type.
Definition: AnyMap.inl.h:58
const T & as() const
Get the value of this key as the specified type.
Definition: AnyMap.inl.h:19
std::string m_Ea_str
The string for activation energy.
Definition: Arrhenius.h:172
virtual void setParameters(const AnyMap &node, const UnitStack &rate_units) override
Set parameters.
Definition: Arrhenius.cpp:113
virtual void check(const std::string &equation, const AnyMap &node) override
Check rate expression.
Definition: Arrhenius.cpp:139
std::string m_E4_str
The string for an optional 4th parameter.
Definition: Arrhenius.h:173
Units m_rate_units
Reaction rate units.
Definition: Arrhenius.h:174
std::string m_A_str
The string for temperature exponent.
Definition: Arrhenius.h:170
double m_E4_R
Optional 4th energy parameter (in temperature units)
Definition: Arrhenius.h:167
void getRateParameters(AnyMap &node) const
Return parameters.
Definition: Arrhenius.cpp:91
virtual void getParameters(AnyMap &node) const override
Get parameters.
Definition: Arrhenius.cpp:124
double m_A
Pre-exponential factor.
Definition: Arrhenius.h:164
virtual void validate(const std::string &equation, const Kinetics &kin) override
Validate the reaction rate expression.
Definition: Arrhenius.cpp:154
void setRateParameters(const AnyValue &rate, const UnitSystem &units, const UnitStack &rate_units)
Perform object setup based on AnyValue node information.
Definition: Arrhenius.cpp:38
double m_order
Reaction order.
Definition: Arrhenius.h:169
bool m_negativeA_ok
Flag indicating whether negative A values are permitted.
Definition: Arrhenius.h:163
double m_b
Temperature exponent.
Definition: Arrhenius.h:165
ArrheniusBase()
Default constructor.
Definition: Arrhenius.cpp:12
void setRateUnits(const UnitStack &rate_units)
Set units of the reaction rate expression.
Definition: Arrhenius.h:142
double m_logA
Logarithm of pre-exponential factor.
Definition: Arrhenius.h:168
double m_Ea_R
Activation energy (in temperature units)
Definition: Arrhenius.h:166
std::string m_b_str
The string for temperature exponent.
Definition: Arrhenius.h:171
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
Error thrown for problems processing information contained in an AnyMap or AnyValue.
Definition: AnyMap.h:702
Public interface for kinetics managers.
Definition: Kinetics.h:114
doublereal temperature() const
Temperature (K).
Definition: Phase.h:654
virtual void setParameters(const AnyMap &node, const UnitStack &units)
Set parameters.
Definition: ReactionRate.h:88
virtual const std::string type() const =0
String identifying reaction rate specialization.
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:102
Unit conversion utility.
Definition: Units.h:161
double convert(double value, const std::string &src, const std::string &dest) const
Convert value from the units of src to the units of dest.
Definition: Units.cpp:558
double convertActivationEnergy(double value, const std::string &src, const std::string &dest) const
Convert value from the units of src to the units of dest, allowing for the different dimensions that ...
Definition: Units.cpp:658
A representation of the units associated with a dimensional quantity.
Definition: Units.h:30
double factor() const
Return the factor for converting from this unit to Cantera's base units.
Definition: Units.h:48
Namespace for the Cantera kernel.
Definition: AnyMap.h:29
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition: ct_defs.h:113
virtual bool update(const ThermoPhase &phase, const Kinetics &kin)
Update data container based on thermodynamic phase state.
Definition: Arrhenius.cpp:162
double temperature
temperature
Definition: ReactionData.h:109
Unit aggregation utility.
Definition: Units.h:99