Cantera 2.6.0
PlogRate.cpp
Go to the documentation of this file.
1//! @file PlogRate.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
9
10namespace Cantera
11{
12
13void PlogData::update(double T)
14{
15 throw CanteraError("PlogData::update",
16 "Missing state information: 'PlogData' requires pressure.");
17}
18
19bool PlogData::update(const ThermoPhase& phase, const Kinetics& kin)
20{
21 double T = phase.temperature();
22 double P = phase.pressure();
23 if (P != pressure || T != temperature) {
24 update(T, P);
25 return true;
26 }
27 return false;
28}
29
30void PlogData::perturbPressure(double deltaP)
31{
32 if (m_pressure_buf > 0.) {
33 throw CanteraError("PlogData::perturbPressure",
34 "Cannot apply another perturbation as state is already perturbed.");
35 }
37 update(temperature, pressure * (1. + deltaP));
38}
39
41{
43 // only restore if there is a valid buffered value
44 if (m_pressure_buf < 0.) {
45 return;
46 }
48 m_pressure_buf = -1.;
49}
50
52 : logP_(-1000)
53 , logP1_(1000)
54 , logP2_(-1000)
55 , rDeltaP_(-1.0)
56{
57}
58
59PlogRate::PlogRate(const std::multimap<double, ArrheniusRate>& rates)
60 : PlogRate()
61{
63}
64
65PlogRate::PlogRate(const std::multimap<double, Arrhenius2>& rates)
66 : PlogRate()
67{
68 setup(rates);
69}
70
71void PlogRate::setParameters(const AnyMap& node, const UnitStack& units)
72{
73 std::multimap<double, ArrheniusRate> multi_rates;
74 if (!node.hasKey("rate-constants")) {
75 // ensure that reaction rate can be evaluated (but returns NaN)
76 multi_rates.insert({1.e-7, ArrheniusRate(NAN, NAN, NAN)});
77 multi_rates.insert({1.e14, ArrheniusRate(NAN, NAN, NAN)});
78 } else {
79 auto& rates = node["rate-constants"].asVector<AnyMap>();
80 for (const auto& rate : rates) {
81 multi_rates.insert({rate.convert("P", "Pa"),
82 ArrheniusRate(AnyValue(rate), node.units(), units)});
83 }
84 }
85 setRates(multi_rates);
86}
87
88void PlogRate::getParameters(AnyMap& rateNode, const Units& rate_units) const
89{
90 std::vector<AnyMap> rateList;
91 rateNode["type"] = type();
92 if (!rates_.size()
93 || (rates_.size() > 1 && std::isnan(rates_[1].preExponentialFactor())))
94 {
95 // object not fully set up
96 return;
97 }
98 for (const auto& r : getRates()) {
99 AnyMap rateNode_;
100 rateNode_["P"].setQuantity(r.first, "Pa");
101 if (rate_units.factor() == 0) {
102 r.second.getRateParameters(rateNode_);
103 } else {
104 // legacy rate
105 Arrhenius2(r.second).getParameters(rateNode_, rate_units);
106 }
107 rateList.push_back(std::move(rateNode_));
108 }
109 rateNode["rate-constants"] = std::move(rateList);
110}
111
112void PlogRate::setup(const std::multimap<double, Arrhenius2>& rates)
113{
114 std::multimap<double, ArrheniusRate> rates2;
115 for (const auto& item : rates) {
116 rates2.emplace(item.first, item.second);
117 }
118 setRates(rates2);
119}
120
121void PlogRate::setRates(const std::multimap<double, ArrheniusRate>& rates)
122{
123 size_t j = 0;
124 rates_.clear();
125 pressures_.clear();
126 rates_.reserve(rates.size());
127 // Insert intermediate pressures
128 for (const auto& rate : rates) {
129 double logp = std::log(rate.first);
130 if (pressures_.empty() || pressures_.rbegin()->first != logp) {
131 // starting a new group
132 pressures_[logp] = {j, j+1};
133 } else {
134 // another rate expression at the same pressure
135 pressures_[logp].second = j+1;
136 }
137
138 j++;
139 rates_.push_back(rate.second);
140 }
141
142 // Duplicate the first and last groups to handle P < P_0 and P > P_N
143 pressures_.insert({-1000.0, pressures_.begin()->second});
144 pressures_.insert({1000.0, pressures_.rbegin()->second});
145}
146
147void PlogRate::validate(const std::string& equation)
148{
149 fmt::memory_buffer err_reactions;
150 double T[] = {200.0, 500.0, 1000.0, 2000.0, 5000.0, 10000.0};
151
152 for (auto iter = ++pressures_.begin(); iter->first < 1000; iter++) {
153 update_C(&iter->first);
154 for (size_t i=0; i < 6; i++) {
155 double k = 0;
156 for (size_t p = ilow1_; p < ilow2_; p++) {
157 k += rates_.at(p).evalRate(log(T[i]), 1.0 / T[i]);
158 }
159 if (!(k > 0)) {
160 fmt_append(err_reactions,
161 "\nInvalid rate coefficient for reaction '{}'\n"
162 "at P = {:.5g}, T = {:.1f}\n",
163 equation, std::exp(iter->first), T[i]);
164 }
165 }
166 }
167 if (err_reactions.size()) {
168 throw CanteraError("PlogRate::validate", to_string(err_reactions));
169 }
170}
171
172std::vector<std::pair<double, Arrhenius2> > PlogRate::rates() const
173{
174 auto rateMap = getRates();
175 std::vector<std::pair<double, Arrhenius2>> out;
176 for (const auto& item : rateMap) {
177 out.emplace_back(item.first, Arrhenius2(item.second));
178 }
179 return out;
180}
181
182std::multimap<double, ArrheniusRate> PlogRate::getRates() const
183{
184 std::multimap<double, ArrheniusRate> rateMap;
185 // initial preincrement to skip rate for P --> 0
186 for (auto iter = ++pressures_.begin();
187 iter->first < 1000; // skip rates for (P --> infinity)
188 ++iter) {
189 for (size_t i = iter->second.first; i < iter->second.second; i++) {
190 rateMap.insert({std::exp(iter->first), rates_[i]});
191 }
192 }
193 return rateMap;
194}
195
196}
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 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
Arrhenius reaction rate type depends only on temperature.
Definition: RxnRates.h:42
Arrhenius reaction rate type depends only on temperature.
Definition: Arrhenius.h:192
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
Public interface for kinetics managers.
Definition: Kinetics.h:114
doublereal temperature() const
Temperature (K).
Definition: Phase.h:654
virtual double pressure() const
Return the thermodynamic pressure (Pa).
Definition: Phase.h:672
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
std::multimap< double, ArrheniusRate > getRates() const
Return the pressures and Arrhenius expressions which comprise this reaction.
Definition: PlogRate.cpp:182
std::vector< std::pair< double, Arrhenius2 > > rates() const
Return the pressures and Arrhenius expressions which comprise this reaction.
Definition: PlogRate.cpp:172
void setRates(const std::multimap< double, ArrheniusRate > &rates)
Set up Plog object.
Definition: PlogRate.cpp:121
PlogRate()
Default constructor.
Definition: PlogRate.cpp:51
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
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
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
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
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) override
Update data container based on temperature T
Definition: PlogRate.cpp:13
virtual void restore() override
Restore data container after a perturbation.
Definition: PlogRate.cpp:40
double pressure
pressure
Definition: PlogRate.h:51
double temperature
temperature
Definition: ReactionData.h:109
virtual void restore()
Restore data container after a perturbation.
Definition: ReactionData.h:89
Unit aggregation utility.
Definition: Units.h:99