Cantera 2.6.0
PlogRate.h
Go to the documentation of this file.
1//! @file PlogRate.h
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#ifndef CT_PLOGRATE_H
7#define CT_PLOGRATE_H
8
10
11namespace Cantera
12{
13
14class Arrhenius2;
15
16//! Data container holding shared data specific to PlogRate
17/**
18 * The data container `PlogData` holds precalculated data common to
19 * all `PlogRate` objects.
20 */
21struct PlogData : public ReactionData
22{
23 PlogData() : pressure(NAN), logP(0.), m_pressure_buf(-1.) {}
24
25 virtual void update(double T) override;
26
27 virtual void update(double T, double P) override {
29 pressure = P;
30 logP = std::log(P);
31 }
32
33 virtual bool update(const ThermoPhase& phase, const Kinetics& kin) override;
34
36
37 //! Perturb pressure of data container
38 /**
39 * The method is used for the evaluation of numerical derivatives.
40 * @param deltaP relative pressure perturbation
41 */
42 void perturbPressure(double deltaP);
43
44 virtual void restore() override;
45
46 virtual void invalidateCache() override {
48 pressure = NAN;
49 }
50
51 double pressure; //!< pressure
52 double logP; //!< logarithm of pressure
53
54protected:
55 double m_pressure_buf; //!< buffered pressure
56};
57
58
59//! Pressure-dependent reaction rate expressed by logarithmically interpolating
60//! between Arrhenius rate expressions at various pressures.
61/*!
62 * Given two rate expressions at two specific pressures:
63 *
64 * * \f$ P_1: k_1(T) = A_1 T^{b_1} e^{-E_1 / RT} \f$
65 * * \f$ P_2: k_2(T) = A_2 T^{b_2} e^{-E_2 / RT} \f$
66 *
67 * The rate at an intermediate pressure \f$ P_1 < P < P_2 \f$ is computed as
68 * \f[
69 * \log k(T,P) = \log k_1(T) + \bigl(\log k_2(T) - \log k_1(T)\bigr)
70 * \frac{\log P - \log P_1}{\log P_2 - \log P_1}
71 * \f]
72 * Multiple rate expressions may be given at the same pressure, in which case
73 * the rate used in the interpolation formula is the sum of all the rates given
74 * at that pressure. For pressures outside the given range, the rate expression
75 * at the nearest pressure is used.
76 */
77class PlogRate final : public ReactionRate
78{
79public:
80 //! Default constructor.
81 PlogRate();
82
83 //! Constructor from Arrhenius rate expressions at a set of pressures
84 explicit PlogRate(const std::multimap<double, ArrheniusRate>& rates);
85
86 //! Constructor using legacy Arrhenius2 framework
87 explicit PlogRate(const std::multimap<double, Arrhenius2>& rates);
88
89 PlogRate(const AnyMap& node, const UnitStack& rate_units={}) : PlogRate() {
90 setParameters(node, rate_units);
91 }
92
93 unique_ptr<MultiRateBase> newMultiRate() const {
94 return unique_ptr<MultiRateBase>(new MultiRate<PlogRate, PlogData>);
95 }
96
97 //! Identifier of reaction rate type
98 const std::string type() const { return "pressure-dependent-Arrhenius"; }
99
100 //! Perform object setup based on AnyMap node information
101 /*!
102 * @param node AnyMap containing rate information
103 * @param units Unit definitions specific to rate information
104 */
105 void setParameters(const AnyMap& node, const UnitStack& units);
106
107 void getParameters(AnyMap& rateNode, const Units& rate_units) const;
108 void getParameters(AnyMap& rateNode) const {
109 return getParameters(rateNode, Units(0));
110 }
111
112 //! Update information specific to reaction
113 /*!
114 * @param shared_data data shared by all reactions of a given type
115 */
116 void updateFromStruct(const PlogData& shared_data) {
117 if (shared_data.logP != logP_) {
118 update_C(&shared_data.logP);
119 }
120 }
121
122 //! Evaluate reaction rate
123 /*!
124 * @param shared_data data shared by all reactions of a given type
125 */
126 double evalFromStruct(const PlogData& shared_data) {
127 return updateRC(shared_data.logT, shared_data.recipT);
128 }
129
130 //! Set up Plog object
131 /*!
132 * @deprecated Deprecated in Cantera 2.6. Replaced by setRates.
133 */
134 void setup(const std::multimap<double, Arrhenius2>& rates);
135
136 //! Set up Plog object
137 void setRates(const std::multimap<double, ArrheniusRate>& rates);
138
139 //! Update concentration-dependent parts of the rate coefficient.
140 //! @param c natural log of the pressure in Pa
141 //! @deprecated To be removed after Cantera 2.6. Implementation will be moved to
142 //! the updateFromStruct() method.
143 void update_C(const double* c) {
144 logP_ = c[0];
145 if (logP_ > logP1_ && logP_ < logP2_) {
146 return;
147 }
148
149 auto iter = pressures_.upper_bound(c[0]);
150 AssertThrowMsg(iter != pressures_.end(), "PlogRate::update_C",
151 "Pressure out of range: {}", logP_);
152 AssertThrowMsg(iter != pressures_.begin(), "PlogRate::update_C",
153 "Pressure out of range: {}", logP_);
154
155 // upper interpolation pressure
156 logP2_ = iter->first;
157 ihigh1_ = iter->second.first;
158 ihigh2_ = iter->second.second;
159
160 // lower interpolation pressure
161 logP1_ = (--iter)->first;
162 ilow1_ = iter->second.first;
163 ilow2_ = iter->second.second;
164
165 rDeltaP_ = 1.0 / (logP2_ - logP1_);
166 }
167
168 /**
169 * Update the value the rate constant.
170 *
171 * This function returns the actual value of the rate constant.
172 * @deprecated To be removed after Cantera 2.6. Implementation will be moved to
173 * the evalFromStruct() method.
174 */
175 double updateRC(double logT, double recipT) const {
176 double log_k1, log_k2;
177 if (ilow1_ == ilow2_) {
178 log_k1 = rates_[ilow1_].evalLog(logT, recipT);
179 } else {
180 double k = 1e-300; // non-zero to make log(k) finite
181 for (size_t i = ilow1_; i < ilow2_; i++) {
182 k += rates_[i].evalRate(logT, recipT);
183 }
184 log_k1 = std::log(k);
185 }
186
187 if (ihigh1_ == ihigh2_) {
188 log_k2 = rates_[ihigh1_].evalLog(logT, recipT);
189 } else {
190 double k = 1e-300; // non-zero to make log(k) finite
191 for (size_t i = ihigh1_; i < ihigh2_; i++) {
192 k += rates_[i].evalRate(logT, recipT);
193 }
194 log_k2 = std::log(k);
195 }
196
197 return std::exp(log_k1 + (log_k2-log_k1) * (logP_-logP1_) * rDeltaP_);
198 }
199
200 //! Check to make sure that the rate expression is finite over a range of
201 //! temperatures at each interpolation pressure. This is potentially an
202 //! issue when one of the Arrhenius expressions at a particular pressure
203 //! has a negative pre-exponential factor.
204 void validate(const std::string& equation, const Kinetics& kin) {
205 validate(equation);
206 }
207
208 void validate(const std::string& equation);
209
210 //! Return the pressures and Arrhenius expressions which comprise this
211 //! reaction.
212 /*!
213 * @deprecated Behavior to change after Cantera 2.6.
214 * @see getRates for new behavior.
215 */
216 std::vector<std::pair<double, Arrhenius2>> rates() const;
217
218 //! Return the pressures and Arrhenius expressions which comprise this
219 //! reaction.
220 std::multimap<double, ArrheniusRate> getRates() const;
221
222protected:
223 //! log(p) to (index range) in the rates_ vector
224 std::map<double, std::pair<size_t, size_t>> pressures_;
225
226 // Rate expressions which are referenced by the indices stored in pressures_
227 std::vector<ArrheniusRate> rates_;
228
229 double logP_; //!< log(p) at the current state
230 double logP1_, logP2_; //!< log(p) at the lower / upper pressure reference
231
232 //! Indices to the ranges within rates_ for the lower / upper pressure, such
233 //! that rates_[ilow1_] through rates_[ilow2_] (inclusive) are the rates
234 //! expressions which are combined to form the rate at the lower reference
235 //! pressure.
236 size_t ilow1_, ilow2_, ihigh1_, ihigh2_;
237
238 double rDeltaP_; //!< reciprocal of (logP2 - logP1)
239};
240
241}
242#endif
Header for reaction rates that involve Arrhenius-type kinetics.
A map of string keys to values whose type can vary at runtime.
Definition: AnyMap.h:399
Public interface for kinetics managers.
Definition: Kinetics.h:114
A class template handling ReactionRate specializations.
Definition: MultiRate.h:21
Pressure-dependent reaction rate expressed by logarithmically interpolating between Arrhenius rate ex...
Definition: PlogRate.h:78
void setParameters(const AnyMap &node, const UnitStack &units)
Perform object setup based on AnyMap node information.
Definition: PlogRate.cpp:71
void updateFromStruct(const PlogData &shared_data)
Update information specific to reaction.
Definition: PlogRate.h:116
std::multimap< double, ArrheniusRate > getRates() const
Return the pressures and Arrhenius expressions which comprise this reaction.
Definition: PlogRate.cpp:182
unique_ptr< MultiRateBase > newMultiRate() const
Create a rate evaluator for reactions of a particular derived type.
Definition: PlogRate.h:93
std::vector< std::pair< double, Arrhenius2 > > rates() const
Return the pressures and Arrhenius expressions which comprise this reaction.
Definition: PlogRate.cpp:172
void getParameters(AnyMap &rateNode) const
Get parameters.
Definition: PlogRate.h:108
void setRates(const std::multimap< double, ArrheniusRate > &rates)
Set up Plog object.
Definition: PlogRate.cpp:121
double evalFromStruct(const PlogData &shared_data)
Evaluate reaction rate.
Definition: PlogRate.h:126
PlogRate()
Default constructor.
Definition: PlogRate.cpp:51
double rDeltaP_
reciprocal of (logP2 - logP1)
Definition: PlogRate.h:238
double updateRC(double logT, double recipT) const
Update the value the rate constant.
Definition: PlogRate.h:175
std::map< double, std::pair< size_t, size_t > > pressures_
log(p) to (index range) in the rates_ vector
Definition: PlogRate.h:224
const std::string type() const
Identifier of reaction rate type.
Definition: PlogRate.h:98
double logP_
log(p) at the current state
Definition: PlogRate.h:229
void update_C(const double *c)
Update concentration-dependent parts of the rate coefficient.
Definition: PlogRate.h:143
void setup(const std::multimap< double, Arrhenius2 > &rates)
Set up Plog object.
Definition: PlogRate.cpp:112
void validate(const std::string &equation, const Kinetics &kin)
Check to make sure that the rate expression is finite over a range of temperatures at each interpolat...
Definition: PlogRate.h:204
size_t ilow1_
Indices to the ranges within rates_ for the lower / upper pressure, such that rates_[ilow1_] through ...
Definition: PlogRate.h:236
double logP2_
log(p) at the lower / upper pressure reference
Definition: PlogRate.h:230
Abstract base class for reaction rate definitions; this base class is used by user-facing APIs to acc...
Definition: ReactionRate.h:45
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:102
A representation of the units associated with a dimensional quantity.
Definition: Units.h:30
#define AssertThrowMsg(expr, procedure,...)
Assertion must be true or an error is thrown.
Definition: ctexceptions.h:271
Namespace for the Cantera kernel.
Definition: AnyMap.h:29
Data container holding shared data specific to PlogRate.
Definition: PlogRate.h:22
void perturbPressure(double deltaP)
Perturb pressure of data container.
Definition: PlogRate.cpp:30
double m_pressure_buf
buffered pressure
Definition: PlogRate.h:55
virtual void update(double T, double P) override
Update data container based on temperature T and an extra parameter.
Definition: PlogRate.h:27
virtual void update(double T) override
Update data container based on temperature T
Definition: PlogRate.cpp:13
double logP
logarithm of pressure
Definition: PlogRate.h:52
virtual void invalidateCache() override
Force shared data and reaction rates to be updated next time.
Definition: PlogRate.h:46
virtual void restore() override
Restore data container after a perturbation.
Definition: PlogRate.cpp:40
double pressure
pressure
Definition: PlogRate.h:51
Data container holding shared data used for ReactionRate calculation.
Definition: ReactionData.h:26
double recipT
inverse of temperature
Definition: ReactionData.h:111
virtual void update(double T)
Update data container based on temperature T
Definition: ReactionData.h:35
double logT
logarithm of temperature
Definition: ReactionData.h:110
virtual void invalidateCache()
Force shared data and reaction rates to be updated next time.
Definition: ReactionData.h:105
Unit aggregation utility.
Definition: Units.h:99