Cantera  3.2.0a2
Loading...
Searching...
No Matches
ReactionRate.h
Go to the documentation of this file.
1/**
2 * @file ReactionRate.h
3 */
4
5// This file is part of Cantera. See License.txt in the top-level directory or
6// at https://cantera.org/license.txt for license and copyright information.
7
8#ifndef CT_REACTIONRATE_H
9#define CT_REACTIONRATE_H
10
11#include "MultiRateBase.h"
12#include "cantera/base/AnyMap.h"
13#include "cantera/base/Units.h"
14#include "cantera/base/global.h"
16
17namespace Cantera
18{
19
20
21class Reaction;
22
23//! Abstract base class for reaction rate definitions; this base class is used by
24//! user-facing APIs to access reaction rate objects
25//!
26//! In addition to the pure virtual methods declared in this class, complete derived
27//! classes must implement the method `evalFromStruct(const DataType& shared_data)`,
28//! where `DataType` is a container for parameters needed to evaluate reactions of that
29//! type. In addition, derived classes may also implement the following methods:
30//! - `updateFromStruct(const DataType& shared_data)`, to update buffered data that
31//! is specific to a given reaction rate.
32//! - `modifyRateConstants(const DataType& shared_data, double& kf, double& kr)`, to
33//! implement modifications to the forward and reverse rate constants for reactions
34//! that do not follow mass action kinetics or use the equilibrium constant to
35//! implement reversibility.
36//!
37//! The calculation of derivatives (or Jacobians) relies on the following methods:
38//! - Derived classes may implement the method
39//! `ddTScaledFromStruct(const DataType& shared_data)` for an analytical derivative
40//! with respect to temperature.
41//! - Associated `DataType` containers may overload the method
42//! `perturbTemperature(double deltaT)`, which is used for the calculation of
43//! numerical derivatives with respect to temperature if an analytic implementation
44//! is not available.
45//! - For reaction rate constants that depend on pressure or third-body collision
46//! partners, associated `DataType` containers should implement the methods
47//! `perturbPressure(double deltaP)` and/or `perturbThirdBodies(double deltaM)`,
48//! which allow for the calculation of numerical derivatives.
49//! - For additional information, refer to the @ref kinDerivs "Kinetics Derivatives"
50//! documentation.
51//!
52//! @since The `modifyRateConstants` method is new in Cantera 3.2.
53//! @ingroup reactionGroup
55{
56public:
57 ReactionRate() {}
58
59 // Copy constructor and assignment operator need to be defined because of the
60 // #m_evaluator member that can't (and shouldn't) be copied.
61 ReactionRate(const ReactionRate& other)
62 : m_input(other.m_input)
64 , m_valid(other.m_valid)
66 {}
67
68 ReactionRate& operator=(const ReactionRate& other) {
69 if (this == &other) {
70 return *this;
71 }
72 m_input = other.m_input;
74 m_valid = other.m_valid;
76 return *this;
77 }
78
79 virtual ~ReactionRate() = default;
80
81 //! Create a rate evaluator for reactions of a particular derived type.
82 //! Derived classes usually implement this as:
83 //!
84 //! ```.cpp
85 //! unique_ptr<MultiRateBase> newMultiRate() const override {
86 //! return make_unique<MultiRate<RateType, DataType>>();
87 //! ```
88 //!
89 //! where `RateType` is the derived class name and `DataType` is the corresponding
90 //! container for parameters needed to evaluate reactions of that type.
91 virtual unique_ptr<MultiRateBase> newMultiRate() const {
92 throw NotImplementedError("ReactionRate::newMultiRate",
93 "Not implemented by '{}' object.", type());
94 }
95
96 //! String identifying reaction rate specialization
97 virtual const string type() const = 0;
98
99 //! String identifying sub-type of reaction rate specialization
100 virtual const string subType() const {
101 return "";
102 }
103
104 //! Set parameters
105 //! @param node AnyMap object containing reaction rate specification
106 //! @param units unit definitions specific to rate information
107 virtual void setParameters(const AnyMap& node, const UnitStack& units) {
108 setRateUnits(units);
109 m_input = node;
110 }
111
112 //! Return the parameters such that an identical Reaction could be reconstructed
113 //! using the newReaction() function. Behavior specific to derived classes is
114 //! handled by the getParameters() method.
116 AnyMap out;
117 out["type"] = type();
118 getParameters(out);
119 return out;
120 }
121
122 //! Get the units for converting the leading term in the reaction rate expression.
123 //!
124 //! These units are often the same as the units of the rate expression itself, but
125 //! not always; sticking coefficients are a notable exception.
126 //! @since New in %Cantera 3.0
127 const Units& conversionUnits() const {
128 return m_conversion_units;
129 }
130
131 //! Set the units of the reaction rate expression
132 //!
133 //! Used to determine the units that should be used for converting terms in the
134 //! reaction rate expression, which often have the same units (for example, the
135 //! Arrhenius pre-exponential) but may also be different (for example, sticking
136 //! coefficients).
137 //! @since New in %Cantera 3.0
138 virtual void setRateUnits(const UnitStack& rate_units) {
139 if (rate_units.size() > 1) {
140 m_conversion_units = rate_units.product();
141 } else {
142 m_conversion_units = rate_units.standardUnits();
143 }
144 }
145
146 //! Check basic syntax and settings of reaction rate expression
147 virtual void check(const string& equation) {}
148
149 //! Validate the reaction rate expression
150 virtual void validate(const string& equation, const Kinetics& kin) {}
151
152 //! Reaction rate index within kinetics evaluator
153 size_t rateIndex() const {
154 return m_rate_index;
155 }
156
157 //! Set reaction rate index within kinetics evaluator
158 void setRateIndex(size_t idx) {
159 m_rate_index = idx;
160 }
161
162 //! Set context of reaction rate evaluation
163 //! @param rxn Reaction object associated with rate
164 //! @param kin Kinetics object used for rate evaluation
165 //! This method allows for passing of information specific to the associated
166 //! reaction when a ReactionRate object is added a MultiRate reaction evaluator.
167 virtual void setContext(const Reaction& rxn, const Kinetics& kin) {
168 }
169
170 //! Evaluate reaction rate based on temperature
171 //! @param T temperature [K]
172 //! Used in conjunction with MultiRateBase::evalSingle / ReactionRate::eval.
173 //! This method allows for testing of a reaction rate expression outside of
174 //! Kinetics reaction rate evaluators.
175 double eval(double T) {
176 _evaluator().update(T);
177 return _evaluator().evalSingle(*this);
178 }
179
180 //! Evaluate reaction rate based on temperature and an extra parameter.
181 //! Specific rate parameterizations may require an additional parameter, which
182 //! is specific to the derived ReactionRate object.
183 //! @param T temperature [K]
184 //! @param extra extra parameter used by parameterization
185 //! Used in conjunction with MultiRateBase::evalSingle / ReactionRate::eval.
186 //! This method allows for testing of a reaction rate expression outside of
187 //! Kinetics reaction rate evaluators.
188 double eval(double T, double extra) {
189 _evaluator().update(T, extra);
190 return _evaluator().evalSingle(*this);
191 }
192
193 //! Evaluate reaction rate based on temperature and an extra vector parameter.
194 //! Specific rate parameterizations may require additional parameters, which
195 //! are specific to the derived ReactionRate object.
196 //! @param T temperature [K]
197 //! @param extra extra vector parameter used by parameterization
198 //! Used in conjunction with MultiRateBase::evalSingle / ReactionRate::eval.
199 //! This method allows for testing of a reaction rate expression outside of
200 //! Kinetics reaction rate evaluators.
201 //! @warning This method is an experimental part of the %Cantera API and
202 //! may be changed or removed without notice.
203 double eval(double T, const vector<double>& extra) {
204 _evaluator().update(T, extra);
205 return _evaluator().evalSingle(*this);
206 }
207
208 //! Get flag indicating whether reaction rate is set up correctly
209 bool valid() const {
210 return m_valid;
211 }
212
213 //! Boolean indicating whether rate has compositional dependence
214 //! @since New in %Cantera 3.0
217 }
218
219 //! Set rate compositional dependence
220 //! @since New in %Cantera 3.0
221 void setCompositionDependence(bool comp_dep) {
223 }
224
225protected:
226 //! Get parameters
227 //! @param node AnyMap containing rate information
228 //! Store the parameters of a ReactionRate needed to reconstruct an identical
229 //! object. Does not include user-defined fields available in the #m_input map.
230 virtual void getParameters(AnyMap& node) const {
231 throw NotImplementedError("ReactionRate::getParameters",
232 "Not implemented by '{}' object.", type());
233 }
234
235 //! Input data used for specific models
237
238 //! Index of reaction rate within kinetics evaluator
240
241 //! Flag indicating whether reaction rate is set up correctly
242 bool m_valid = false;
243
244 //! Flag indicating composition dependent rate
246
247 //! Units of the leading term in the reaction rate expression
249
250private:
251 //! Return an object that be used to evaluate the rate by converting general input
252 //! such as temperature and pressure into the `DataType` struct that is particular
253 //! to the rate model.
255 if (!m_evaluator) {
256 m_evaluator = newMultiRate();
257 }
258 return *m_evaluator;
259 }
260
261 unique_ptr<MultiRateBase> m_evaluator;
262};
263
264}
265
266#endif
Header for unit conversion utilities, which are used to translate user input from input files (See In...
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:431
Public interface for kinetics managers.
Definition Kinetics.h:126
An abstract base class for evaluating all reactions of a particular type.
virtual double evalSingle(ReactionRate &rate)=0
Get the rate for a single reaction.
virtual void update(double T)=0
Update common reaction rate data based on temperature.
An error indicating that an unimplemented function has been called.
Abstract base class for reaction rate definitions; this base class is used by user-facing APIs to acc...
void setCompositionDependence(bool comp_dep)
Set rate compositional dependence.
virtual void setParameters(const AnyMap &node, const UnitStack &units)
Set parameters.
bool compositionDependent()
Boolean indicating whether rate has compositional dependence.
double eval(double T)
Evaluate reaction rate based on temperature.
Units m_conversion_units
Units of the leading term in the reaction rate expression.
virtual void setContext(const Reaction &rxn, const Kinetics &kin)
Set context of reaction rate evaluation.
const Units & conversionUnits() const
Get the units for converting the leading term in the reaction rate expression.
bool valid() const
Get flag indicating whether reaction rate is set up correctly.
double eval(double T, const vector< double > &extra)
Evaluate reaction rate based on temperature and an extra vector parameter.
virtual void setRateUnits(const UnitStack &rate_units)
Set the units of the reaction rate expression.
double eval(double T, double extra)
Evaluate reaction rate based on temperature and an extra parameter.
size_t rateIndex() const
Reaction rate index within kinetics evaluator.
AnyMap parameters() const
Return the parameters such that an identical Reaction could be reconstructed using the newReaction() ...
void setRateIndex(size_t idx)
Set reaction rate index within kinetics evaluator.
bool m_valid
Flag indicating whether reaction rate is set up correctly.
virtual unique_ptr< MultiRateBase > newMultiRate() const
Create a rate evaluator for reactions of a particular derived type.
virtual const string subType() const
String identifying sub-type of reaction rate specialization.
virtual void validate(const string &equation, const Kinetics &kin)
Validate the reaction rate expression.
virtual void check(const string &equation)
Check basic syntax and settings of reaction rate expression.
virtual const string type() const =0
String identifying reaction rate specialization.
bool m_composition_dependent_rate
Flag indicating composition dependent rate.
AnyMap m_input
Input data used for specific models.
virtual void getParameters(AnyMap &node) const
Get parameters.
MultiRateBase & _evaluator()
Return an object that be used to evaluate the rate by converting general input such as temperature an...
size_t m_rate_index
Index of reaction rate within kinetics evaluator.
Abstract base class which stores data about a reaction and its rate parameterization so that it can b...
Definition Reaction.h:25
A representation of the units associated with a dimensional quantity.
Definition Units.h:35
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
const size_t npos
index returned by functions to indicate "no position"
Definition ct_defs.h:180
Unit aggregation utility.
Definition Units.h:105
size_t size() const
Size of UnitStack.
Definition Units.h:118
Units standardUnits() const
Get standard unit used by UnitStack.
Definition Units.cpp:323
Units product() const
Calculate product of units-exponent stack.
Definition Units.cpp:377