Cantera  3.0.0
Loading...
Searching...
No Matches
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
8
9// @todo remove after Cantera 3.0 (only used for deprecation)
11
12namespace Cantera
13{
14
15void PlogData::update(double T)
16{
17 throw CanteraError("PlogData::update",
18 "Missing state information: 'PlogData' requires pressure.");
19}
20
21bool PlogData::update(const ThermoPhase& phase, const Kinetics& kin)
22{
23 double T = phase.temperature();
24 double P = phase.pressure();
25 if (P != pressure || T != temperature) {
26 update(T, P);
27 return true;
28 }
29 return false;
30}
31
32void PlogData::perturbPressure(double deltaP)
33{
34 if (m_pressure_buf > 0.) {
35 throw CanteraError("PlogData::perturbPressure",
36 "Cannot apply another perturbation as state is already perturbed.");
37 }
39 update(temperature, pressure * (1. + deltaP));
40}
41
43{
45 // only restore if there is a valid buffered value
46 if (m_pressure_buf < 0.) {
47 return;
48 }
50 m_pressure_buf = -1.;
51}
52
53// Methods of class PlogRate
54
55PlogRate::PlogRate(const std::multimap<double, ArrheniusRate>& rates)
56{
57 setRates(rates);
58}
59
60PlogRate::PlogRate(const AnyMap& node, const UnitStack& rate_units)
61{
62 setParameters(node, rate_units);
63}
64
65void PlogRate::setParameters(const AnyMap& node, const UnitStack& rate_units)
66{
67 ReactionRate::setParameters(node, rate_units);
68 std::multimap<double, ArrheniusRate> multi_rates;
69 if (node.hasKey("rate-constants")) {
70 auto& rates = node["rate-constants"].asVector<AnyMap>();
71 for (const auto& rate : rates) {
72 multi_rates.insert({rate.convert("P", "Pa"),
73 ArrheniusRate(AnyValue(rate), node.units(), rate_units)});
74 }
75 }
76 setRates(multi_rates);
77}
78
79void PlogRate::getParameters(AnyMap& rateNode, const Units& rate_units) const
80{
81 vector<AnyMap> rateList;
82 if (!valid()) {
83 // object not fully set up
84 return;
85 }
86 for (const auto& [pressure, rate] : getRates()) {
87 AnyMap rateNode_;
88 rateNode_["P"].setQuantity(pressure, "Pa");
89 rate.getRateParameters(rateNode_);
90 rateList.push_back(std::move(rateNode_));
91 }
92 rateNode["rate-constants"] = std::move(rateList);
93}
94
95void PlogRate::setRates(const std::multimap<double, ArrheniusRate>& rates)
96{
97 size_t j = 0;
98 rates_.clear();
99 pressures_.clear();
100 m_valid = !rates.empty();
101 rates_.reserve(rates.size());
102 // Insert intermediate pressures
103 for (const auto& [pressure, rate] : rates) {
104 double logp = std::log(pressure);
105 if (pressures_.empty() || pressures_.rbegin()->first != logp) {
106 // starting a new group
107 pressures_[logp] = {j, j+1};
108 } else {
109 // another rate expression at the same pressure
110 pressures_[logp].second = j+1;
111 }
112
113 j++;
114 rates_.push_back(rate);
115 }
116 if (!m_valid) {
117 // ensure that reaction rate can be evaluated (but returns NaN)
118 rates_.reserve(1);
119 pressures_[std::log(OneBar)] = {0, 0};
120 rates_.push_back(ArrheniusRate());
121 }
122
123 // Duplicate the first and last groups to handle P < P_0 and P > P_N
124 pressures_.insert({-1000.0, pressures_.begin()->second});
125 pressures_.insert({1000.0, pressures_.rbegin()->second});
126}
127
128void PlogRate::validate(const string& equation, const Kinetics& kin)
129{
130 if (!valid()) {
131 throw InputFileError("PlogRate::validate", m_input,
132 "Rate object for reaction '{}' is not configured.", equation);
133 }
134
135 fmt::memory_buffer err_reactions;
136 double T[] = {300.0, 500.0, 1000.0, 2000.0, 5000.0, 10000.0};
137 PlogData data;
138
139 for (auto iter = ++pressures_.begin(); iter->first < 1000; iter++) {
140 data.update(T[0], exp(iter->first)); // iter->first contains log(p)
141 updateFromStruct(data);
142 for (size_t i=0; i < 6; i++) {
143 double k = 0;
144 for (size_t p = ilow1_; p < ilow2_; p++) {
145 k += rates_.at(p).evalRate(log(T[i]), 1.0 / T[i]);
146 }
147 if (!(k > 0)) {
148 fmt_append(err_reactions,
149 "at P = {:.5g}, T = {:.1f}\n", std::exp(iter->first), T[i]);
150 }
151 }
152 }
153 if (err_reactions.size()) {
154 throw InputFileError("PlogRate::validate", m_input,
155 "\nInvalid rate coefficient for reaction '{}'\n{}",
156 equation, to_string(err_reactions));
157 }
158}
159
160void PlogRate::validate(const string& equation) {
161 warn_deprecated("PlogRate::validate",
162 "To be removed after Cantera 3.0; superseded by two-parameter version.");
163 validate(equation, Kinetics());
164}
165
166std::multimap<double, ArrheniusRate> PlogRate::getRates() const
167{
168 std::multimap<double, ArrheniusRate> rateMap;
169 // initial preincrement to skip rate for P --> 0
170 for (auto iter = ++pressures_.begin();
171 iter->first < 1000; // skip rates for (P --> infinity)
172 ++iter) {
173 for (size_t i = iter->second.first; i < iter->second.second; i++) {
174 rateMap.insert({std::exp(iter->first), rates_[i]});
175 }
176 }
177 return rateMap;
178}
179
180}
Base class for kinetics managers and also contains the kineticsmgr module documentation (see Kinetics...
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:427
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition AnyMap.cpp:1423
const UnitSystem & units() const
Return the default units that should be used to convert stored values.
Definition AnyMap.h:630
A wrapper for a variable whose type is determined at runtime.
Definition AnyMap.h:86
Arrhenius reaction rate type depends only on temperature.
Definition Arrhenius.h:170
Base class for exceptions thrown by Cantera classes.
Error thrown for problems processing information contained in an AnyMap or AnyValue.
Definition AnyMap.h:738
Public interface for kinetics managers.
Definition Kinetics.h:126
double temperature() const
Temperature (K).
Definition Phase.h:662
virtual double pressure() const
Return the thermodynamic pressure (Pa).
Definition Phase.h:680
map< double, pair< size_t, size_t > > pressures_
log(p) to (index range) in the rates_ vector
Definition PlogRate.h:185
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:166
void setParameters(const AnyMap &node, const UnitStack &rate_units) override
Perform object setup based on AnyMap node information.
Definition PlogRate.cpp:65
void setRates(const std::multimap< double, ArrheniusRate > &rates)
Set up Plog object.
Definition PlogRate.cpp:95
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:128
size_t ilow1_
Indices to the ranges within rates_ for the lower / upper pressure, such that rates_[ilow1_] through ...
Definition PlogRate.h:198
virtual void setParameters(const AnyMap &node, const UnitStack &units)
Set parameters.
bool valid() const
Get flag indicating whether reaction rate is set up correctly.
bool m_valid
Flag indicating whether reaction rate is set up correctly.
AnyMap m_input
Input data used for specific models.
Base class for a phase with thermodynamic properties.
A representation of the units associated with a dimensional quantity.
Definition Units.h:35
void fmt_append(fmt::memory_buffer &b, Args... args)
Versions 6.2.0 and 6.2.1 of fmtlib do not include this define before they include windows....
Definition fmt.h:29
const double OneBar
One bar [Pa].
Definition ct_defs.h:99
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564
void warn_deprecated(const string &source, const AnyBase &node, const string &message)
A deprecation warning for syntax in an input file.
Definition AnyMap.cpp:1926
Data container holding shared data specific to PlogRate.
Definition PlogRate.h:20
void perturbPressure(double deltaP)
Perturb pressure of data container.
Definition PlogRate.cpp:32
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:15
void restore() override
Restore data container after a perturbation.
Definition PlogRate.cpp:42
double pressure
pressure
Definition PlogRate.h:49
double temperature
temperature
virtual void restore()
Restore data container after a perturbation.
Unit aggregation utility.
Definition Units.h:105