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
6#include "cantera/kinetics/Arrhenius.h"
7#include "cantera/thermo/ThermoPhase.h"
8
9 namespace Cantera
10{
11
12 ArrheniusBase::ArrheniusBase()
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
24 ArrheniusBase::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
38 void ArrheniusBase::setRateParameters(
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
91 void ArrheniusBase::getRateParameters(AnyMap& node) const
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
113 void 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
124 void ArrheniusBase::getParameters(AnyMap& node) const {
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
139 void 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
154 void 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
162 bool 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}
Arrhenius.h
Header for reaction rates that involve Arrhenius-type kinetics.
ThermoPhase.h
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
Cantera::AnyMap
A map of string keys to values whose type can vary at runtime.
Definition: AnyMap.h:399
Cantera::AnyMap::units
const UnitSystem & units() const
Return the default units that should be used to convert stored values.
Definition: AnyMap.h:598
Cantera::AnyMap::empty
bool empty() const
Return boolean indicating whether AnyMap is empty.
Definition: AnyMap.cpp:1401
Cantera::AnyMap::getBool
bool getBool(const std::string &key, bool default_) const
If key exists, return it as a bool, otherwise return default_.
Definition: AnyMap.cpp:1487
Cantera::AnyMap::setFlowStyle
void setFlowStyle(bool flow=true)
Use "flow" style when outputting this AnyMap to YAML.
Definition: AnyMap.cpp:1698
Cantera::AnyMap::hasKey
bool hasKey(const std::string &key) const
Returns true if the map contains an item named key.
Definition: AnyMap.cpp:1406
Cantera::AnyValue
A wrapper for a variable whose type is determined at runtime.
Definition: AnyMap.h:84
Cantera::AnyValue::empty
bool empty() const
Return boolean indicating whether AnyValue is empty.
Definition: AnyMap.cpp:684
Cantera::AnyValue::asVector
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
Cantera::AnyValue::is
bool is() const
Returns true if the held value is of the specified type.
Definition: AnyMap.inl.h:58
Cantera::AnyValue::as
const T & as() const
Get the value of this key as the specified type.
Definition: AnyMap.inl.h:19
Cantera::ArrheniusBase::m_Ea_str
std::string m_Ea_str
The string for activation energy.
Definition: Arrhenius.h:172
Cantera::ArrheniusBase::setParameters
virtual void setParameters(const AnyMap &node, const UnitStack &rate_units) override
Set parameters.
Definition: Arrhenius.cpp:113
Cantera::ArrheniusBase::check
virtual void check(const std::string &equation, const AnyMap &node) override
Check rate expression.
Definition: Arrhenius.cpp:139
Cantera::ArrheniusBase::m_E4_str
std::string m_E4_str
The string for an optional 4th parameter.
Definition: Arrhenius.h:173
Cantera::ArrheniusBase::m_rate_units
Units m_rate_units
Reaction rate units.
Definition: Arrhenius.h:174
Cantera::ArrheniusBase::m_A_str
std::string m_A_str
The string for temperature exponent.
Definition: Arrhenius.h:170
Cantera::ArrheniusBase::m_E4_R
double m_E4_R
Optional 4th energy parameter (in temperature units)
Definition: Arrhenius.h:167
Cantera::ArrheniusBase::getRateParameters
void getRateParameters(AnyMap &node) const
Return parameters.
Definition: Arrhenius.cpp:91
Cantera::ArrheniusBase::getParameters
virtual void getParameters(AnyMap &node) const override
Get parameters.
Definition: Arrhenius.cpp:124
Cantera::ArrheniusBase::m_A
double m_A
Pre-exponential factor.
Definition: Arrhenius.h:164
Cantera::ArrheniusBase::validate
virtual void validate(const std::string &equation, const Kinetics &kin) override
Validate the reaction rate expression.
Definition: Arrhenius.cpp:154
Cantera::ArrheniusBase::setRateParameters
void setRateParameters(const AnyValue &rate, const UnitSystem &units, const UnitStack &rate_units)
Perform object setup based on AnyValue node information.
Definition: Arrhenius.cpp:38
Cantera::ArrheniusBase::m_order
double m_order
Reaction order.
Definition: Arrhenius.h:169
Cantera::ArrheniusBase::m_negativeA_ok
bool m_negativeA_ok
Flag indicating whether negative A values are permitted.
Definition: Arrhenius.h:163
Cantera::ArrheniusBase::m_b
double m_b
Temperature exponent.
Definition: Arrhenius.h:165
Cantera::ArrheniusBase::ArrheniusBase
ArrheniusBase()
Default constructor.
Definition: Arrhenius.cpp:12
Cantera::ArrheniusBase::setRateUnits
void setRateUnits(const UnitStack &rate_units)
Set units of the reaction rate expression.
Definition: Arrhenius.h:142
Cantera::ArrheniusBase::m_logA
double m_logA
Logarithm of pre-exponential factor.
Definition: Arrhenius.h:168
Cantera::ArrheniusBase::m_Ea_R
double m_Ea_R
Activation energy (in temperature units)
Definition: Arrhenius.h:166
Cantera::ArrheniusBase::m_b_str
std::string m_b_str
The string for temperature exponent.
Definition: Arrhenius.h:171
Cantera::CanteraError
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
Cantera::InputFileError
Error thrown for problems processing information contained in an AnyMap or AnyValue.
Definition: AnyMap.h:702
Cantera::Kinetics
Public interface for kinetics managers.
Definition: Kinetics.h:114
Cantera::Phase::temperature
doublereal temperature() const
Temperature (K).
Definition: Phase.h:654
Cantera::ReactionRate::setParameters
virtual void setParameters(const AnyMap &node, const UnitStack &units)
Set parameters.
Definition: ReactionRate.h:88
Cantera::ReactionRate::type
virtual const std::string type() const =0
String identifying reaction rate specialization.
Cantera::ThermoPhase
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:102
Cantera::UnitSystem
Unit conversion utility.
Definition: Units.h:161
Cantera::UnitSystem::convert
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
Cantera::UnitSystem::convertActivationEnergy
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
Cantera::Units
A representation of the units associated with a dimensional quantity.
Definition: Units.h:30
Cantera::Units::factor
double factor() const
Return the factor for converting from this unit to Cantera's base units.
Definition: Units.h:48
Cantera
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:557
Cantera::GasConstant
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition: ct_defs.h:113
Cantera::ArrheniusData::update
virtual bool update(const ThermoPhase &phase, const Kinetics &kin)
Update data container based on thermodynamic phase state.
Definition: Arrhenius.cpp:162
Cantera::ReactionData::temperature
double temperature
temperature
Definition: ReactionData.h:109
Cantera::UnitStack
Unit aggregation utility.
Definition: Units.h:99