Cantera 2.6.0
ChebyshevRate.cpp
Go to the documentation of this file.
1//! @file ChebyshevRate.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
9namespace Cantera
10{
11
13{
14 throw CanteraError("ChebyshevData::update",
15 "Missing state information: 'ChebyshevData' requires pressure.");
16}
17
18bool ChebyshevData::update(const ThermoPhase& phase, const Kinetics& kin)
19{
20 double T = phase.temperature();
21 double P = phase.pressure();
22 if (P != pressure || T != temperature) {
23 update(T, P);
24 return true;
25 }
26 return false;
27}
28
30{
31 if (m_pressure_buf > 0.) {
32 throw CanteraError("ChebyshevData::perturbPressure",
33 "Cannot apply another perturbation as state is already perturbed.");
34 }
36 update(temperature, pressure * (1. + deltaP));
37}
38
40{
42 // only restore if there is a valid buffered value
43 if (m_pressure_buf < 0.) {
44 return;
45 }
47 m_pressure_buf = -1.;
48}
49
50ChebyshevRate::ChebyshevRate(double Tmin, double Tmax, double Pmin, double Pmax,
51 const Array2D& coeffs) : ChebyshevRate()
52{
55}
56
57void ChebyshevRate::setParameters(const AnyMap& node, const UnitStack& units)
58{
59 m_rate_units = units.product();
60 const UnitSystem& unit_system = node.units();
62 if (node.hasKey("data")) {
63 const auto& T_range = node["temperature-range"].asVector<AnyValue>(2);
64 const auto& P_range = node["pressure-range"].asVector<AnyValue>(2);
65 auto& vcoeffs = node["data"].asVector<vector_fp>();
66 coeffs = Array2D(vcoeffs.size(), vcoeffs[0].size());
67 for (size_t i = 0; i < coeffs.nRows(); i++) {
68 if (vcoeffs[i].size() != vcoeffs[0].size()) {
69 throw InputFileError("ChebyshevRate::setParameters", node["data"],
70 "Inconsistent number of coefficients in row {} of matrix", i + 1);
71 }
72 for (size_t j = 0; j < coeffs.nColumns(); j++) {
73 coeffs(i, j) = vcoeffs[i][j];
74 }
75 }
76 if (m_rate_units.factor()) {
77 coeffs(0, 0) += std::log10(unit_system.convertTo(1.0, m_rate_units));
78 }
80 unit_system.convert(T_range[0], "K"),
81 unit_system.convert(T_range[1], "K"),
82 unit_system.convert(P_range[0], "Pa"),
83 unit_system.convert(P_range[1], "Pa")
84 );
85 } else {
86 // ensure that reaction rate can be evaluated (but returns NaN)
87 coeffs = Array2D(1, 1);
88 coeffs(0, 0) = NAN;
89 setLimits(290., 3000., 1.e-7, 1.e14);
90 }
91
93}
94
95void ChebyshevRate::setup(double Tmin, double Tmax, double Pmin, double Pmax,
96 const Array2D& coeffs)
97{
98 warn_deprecated("ChebyshevRate::setup", "Deprecated in Cantera 2.6; "
99 "replaceable with setLimits() and setData().");
102}
103
104void ChebyshevRate::setLimits(double Tmin, double Tmax, double Pmin, double Pmax)
105{
106 double logPmin = std::log10(Pmin);
107 double logPmax = std::log10(Pmax);
108 double TminInv = 1.0 / Tmin;
109 double TmaxInv = 1.0 / Tmax;
110
111 TrNum_ = - TminInv - TmaxInv;
112 TrDen_ = 1.0 / (TmaxInv - TminInv);
113 PrNum_ = - logPmin - logPmax;
114 PrDen_ = 1.0 / (logPmax - logPmin);
115
116 Tmin_ = Tmin;
117 Tmax_ = Tmax;
118 Pmin_ = Pmin;
119 Pmax_ = Pmax;
120}
121
123{
125 dotProd_.resize(coeffs.nRows());
126
127 // convert to row major for legacy output
128 // note: chebCoeffs_ is not used internally (@todo: remove after Cantera 2.6)
129 size_t rows = m_coeffs.nRows();
130 size_t cols = m_coeffs.nColumns();
131 chebCoeffs_.resize(rows * cols);
132 for (size_t i = 0; i < rows; i++) {
133 for (size_t j = 0; j < cols; j++) {
134 chebCoeffs_[cols * i + j] = m_coeffs(i, j);
135 }
136 }
137}
138
139void ChebyshevRate::getParameters(AnyMap& rateNode) const
140{
141 rateNode["type"] = type();
142 if (!m_coeffs.data().size() || std::isnan(m_coeffs(0, 0))) {
143 // object not fully set up
144 return;
145 }
146 rateNode["temperature-range"].setQuantity({Tmin(), Tmax()}, "K");
147 rateNode["pressure-range"].setQuantity({Pmin(), Pmax()}, "Pa");
148 size_t nT = m_coeffs.nRows();
149 size_t nP = m_coeffs.nColumns();
150 std::vector<vector_fp> coeffs2d(nT, vector_fp(nP));
151 for (size_t i = 0; i < nT; i++) {
152 for (size_t j = 0; j < nP; j++) {
153 coeffs2d[i][j] = m_coeffs(i, j);
154 }
155 }
156 // Unit conversions must take place later, after the destination unit system
157 // is known. A lambda function is used here to override the default behavior
158 Units rate_units2 = m_rate_units;
159 auto converter = [rate_units2](AnyValue& coeffs, const UnitSystem& units) {
160 if (rate_units2.factor() != 0.0) {
161 coeffs.asVector<vector_fp>()[0][0] += \
162 std::log10(units.convertFrom(1.0, rate_units2));
163 } else if (units.getDelta(UnitSystem()).size()) {
164 throw CanteraError("ChebyshevRate::getParameters lambda",
165 "Cannot convert rate constant with unknown dimensions to a "
166 "non-default unit system");
167 }
168 };
170 coeffs = std::move(coeffs2d);
171 rateNode["data"].setQuantity(coeffs, converter);
172}
173
174void ChebyshevRate::validate(const std::string& equation, const Kinetics& kin)
175{
176 if (m_coeffs.data().empty() || isnan(m_coeffs(0, 0))) {
177 throw CanteraError("ChebyshevRate::validate",
178 "Rate object for reaction '{}' is not configured.", equation);
179 }
180}
181
182}
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
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:30
size_t nRows() const
Number of rows.
Definition: Array.h:188
size_t nColumns() const
Number of columns.
Definition: Array.h:193
vector_fp & data()
Return a reference to the data vector.
Definition: Array.h:218
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
Pressure-dependent rate expression where the rate coefficient is expressed as a bivariate Chebyshev p...
Definition: ChebyshevRate.h:90
void setParameters(const AnyMap &node, const UnitStack &units)
Perform object setup based on AnyMap node information.
double Tmax_
valid temperature range
ChebyshevRate()
Default constructor.
Definition: ChebyshevRate.h:93
double TrDen_
terms appearing in the reduced temperature
Units m_rate_units
Reaction rate units.
double Pmin() const
Minimum valid pressure [Pa].
double Tmin() const
Minimum valid temperature [K].
void setData(const Array2D &coeffs)
Set the Chebyshev coefficients as 2-dimensional array.
double Tmax() const
Maximum valid temperature [K].
const std::string type() const
String identifying reaction rate specialization.
Array2D m_coeffs
< coefficient array
double Pmax_
valid pressure range
vector_fp dotProd_
dot product of chebCoeffs with the reduced pressure polynomial
void setup(double Tmin, double Tmax, double Pmin, double Pmax, const Array2D &coeffs)
Set up ChebyshevRate object.
double Pmax() const
Maximum valid pressure [Pa].
double PrDen_
terms appearing in the reduced pressure
vector_fp chebCoeffs_
Chebyshev coefficients, length nP * nT.
const vector_fp & coeffs() const
Access the ChebyshevRate coefficients.
void setLimits(double Tmin, double Tmax, double Pmin, double Pmax)
Set limits for ChebyshevRate object.
virtual void validate(const std::string &equation, const Kinetics &kin)
Validate the reaction rate expression.
Error thrown for problems processing information contained in an AnyMap or AnyValue.
Definition: AnyMap.h:702
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
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:102
Unit conversion utility.
Definition: Units.h:161
double convert(double value, const std::string &src, const std::string &dest) const
Convert value from the units of src to the units of dest.
Definition: Units.cpp:558
double convertTo(double value, const std::string &dest) const
Convert value to the specified dest units from the appropriate units for this unit system (defined by...
Definition: Units.cpp:575
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 warn_deprecated(const std::string &source, const AnyBase &node, const std::string &message)
A deprecation warning for syntax in an input file.
Definition: AnyMap.cpp:1901
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:184
void perturbPressure(double deltaP)
Perturb pressure of data container.
double m_pressure_buf
buffered pressure
Definition: ChebyshevRate.h:57
virtual void update(double T) override
Update data container based on temperature T
virtual void restore() override
Restore data container after a perturbation.
double pressure
pressure
Definition: ChebyshevRate.h:53
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
Units product() const
Calculate product of units-exponent stack.
Definition: Units.cpp:388