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