Cantera  3.1.0
Loading...
Searching...
No Matches
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{
54 setData(coeffs);
55}
56
57ChebyshevRate::ChebyshevRate(const AnyMap& node, const UnitStack& rate_units)
59{
60 setParameters(node, rate_units);
61}
62
63void ChebyshevRate::setParameters(const AnyMap& node, const UnitStack& rate_units)
64{
65 ReactionRate::setParameters(node, rate_units);
66 const UnitSystem& unit_system = node.units();
67 Array2D coeffs(0, 0);
68 if (node.hasKey("data")) {
69 const auto& T_range = node["temperature-range"].asVector<AnyValue>(2);
70 const auto& P_range = node["pressure-range"].asVector<AnyValue>(2);
71 auto& vcoeffs = node["data"].asVector<vector<double>>();
72 coeffs = Array2D(vcoeffs.size(), vcoeffs[0].size());
73 for (size_t i = 0; i < coeffs.nRows(); i++) {
74 if (vcoeffs[i].size() != vcoeffs[0].size()) {
75 throw InputFileError("ChebyshevRate::setParameters", node["data"],
76 "Inconsistent number of coefficients in row {} of matrix", i + 1);
77 }
78 for (size_t j = 0; j < coeffs.nColumns(); j++) {
79 coeffs(i, j) = vcoeffs[i][j];
80 }
81 }
82 double offset = unit_system.convertRateCoeff(AnyValue(1.0), conversionUnits());
83 coeffs(0, 0) += std::log10(offset);
85 unit_system.convert(T_range[0], "K"),
86 unit_system.convert(T_range[1], "K"),
87 unit_system.convert(P_range[0], "Pa"),
88 unit_system.convert(P_range[1], "Pa")
89 );
90 } else {
91 setLimits(290., 3000., Tiny, 1. / Tiny);
92 }
93 setData(coeffs);
94}
95
96void ChebyshevRate::setLimits(double Tmin, double Tmax, double Pmin, double Pmax)
97{
98 double logPmin = std::log10(Pmin);
99 double logPmax = std::log10(Pmax);
100 double TminInv = 1.0 / Tmin;
101 double TmaxInv = 1.0 / Tmax;
102
103 TrNum_ = - TminInv - TmaxInv;
104 TrDen_ = 1.0 / (TmaxInv - TminInv);
105 PrNum_ = - logPmin - logPmax;
106 PrDen_ = 1.0 / (logPmax - logPmin);
107
108 Tmin_ = Tmin;
109 Tmax_ = Tmax;
110 Pmin_ = Pmin;
111 Pmax_ = Pmax;
112}
113
115{
116 m_valid = !coeffs.data().empty();
117 if (m_valid) {
118 m_coeffs = coeffs;
119 } else {
120 // ensure that reaction rate can be evaluated (but returns NaN)
121 m_coeffs = Array2D(1, 1, NAN);
122 }
123 dotProd_.resize(m_coeffs.nRows());
124}
125
127{
128 if (!valid()) {
129 // object not fully set up
130 return;
131 }
132 rateNode["temperature-range"].setQuantity({Tmin(), Tmax()}, "K");
133 rateNode["pressure-range"].setQuantity({Pmin(), Pmax()}, "Pa");
134 size_t nT = m_coeffs.nRows();
135 size_t nP = m_coeffs.nColumns();
136 vector<vector<double>> coeffs2d(nT, vector<double>(nP));
137 for (size_t i = 0; i < nT; i++) {
138 for (size_t j = 0; j < nP; j++) {
139 coeffs2d[i][j] = m_coeffs(i, j);
140 }
141 }
142 // Unit conversions must take place later, after the destination unit system
143 // is known. A lambda function is used here to override the default behavior
144 Units rate_units2 = conversionUnits();
145 auto converter = [rate_units2](AnyValue& coeffs, const UnitSystem& units) {
146 if (rate_units2.factor() != 0.0) {
147 coeffs.asVector<vector<double>>()[0][0] += \
148 std::log10(units.convertFrom(1.0, rate_units2));
149 } else if (units.getDelta(UnitSystem()).size()) {
150 throw CanteraError("ChebyshevRate::getParameters lambda",
151 "Cannot convert rate constant with unknown dimensions to a "
152 "non-default unit system");
153 }
154 };
155 AnyValue coeffs;
156 coeffs = std::move(coeffs2d);
157 rateNode["data"].setQuantity(coeffs, converter);
158}
159
160void ChebyshevRate::validate(const string& equation, const Kinetics& kin)
161{
162 if (!valid()) {
163 throw InputFileError("ChebyshevRate::validate", m_input,
164 "Rate object for reaction '{}' is not configured.", equation);
165 }
166}
167
168}
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:431
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition AnyMap.cpp:1477
const UnitSystem & units() const
Return the default units that should be used to convert stored values.
Definition AnyMap.h:640
A wrapper for a variable whose type is determined at runtime.
Definition AnyMap.h:87
const vector< T > & asVector(size_t nMin=npos, size_t nMax=npos) const
Return the held value, if it is a vector of type T.
Definition AnyMap.inl.h:109
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition Array.h:32
size_t nRows() const
Number of rows.
Definition Array.h:176
size_t nColumns() const
Number of columns.
Definition Array.h:181
vector< double > & data()
Return a reference to the data vector.
Definition Array.h:186
Base class for exceptions thrown by Cantera classes.
Pressure-dependent rate expression where the rate coefficient is expressed as a bivariate Chebyshev p...
void getParameters(AnyMap &rateNode) const override
Get parameters.
double Tmax_
valid temperature range
void setParameters(const AnyMap &node, const UnitStack &rate_units) override
Perform object setup based on AnyMap node information.
double TrDen_
terms appearing in the reduced temperature
double Pmin() const
Minimum valid pressure [Pa].
double Tmin() const
Minimum valid temperature [K].
void validate(const string &equation, const Kinetics &kin) override
Validate the reaction rate expression.
vector< double > dotProd_
dot product of coeffs with the reduced pressure polynomial
void setData(const Array2D &coeffs)
Set the Chebyshev coefficients as 2-dimensional array.
double Tmax() const
Maximum valid temperature [K].
ChebyshevRate()=default
Default constructor.
Array2D m_coeffs
< coefficient array
double Pmax_
valid pressure range
double Pmax() const
Maximum valid pressure [Pa].
double PrDen_
terms appearing in the reduced pressure
void setLimits(double Tmin, double Tmax, double Pmin, double Pmax)
Set limits for ChebyshevRate object.
Error thrown for problems processing information contained in an AnyMap or AnyValue.
Definition AnyMap.h:749
Public interface for kinetics managers.
Definition Kinetics.h:125
double temperature() const
Temperature (K).
Definition Phase.h:562
virtual double pressure() const
Return the thermodynamic pressure (Pa).
Definition Phase.h:580
virtual void setParameters(const AnyMap &node, const UnitStack &units)
Set parameters.
const Units & conversionUnits() const
Get the units for converting the leading term in the reaction rate expression.
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.
Unit conversion utility.
Definition Units.h:169
double convertRateCoeff(const AnyValue &val, const Units &dest) const
Convert a generic AnyValue node representing a reaction rate coefficient to the units specified in de...
Definition Units.cpp:631
double convert(double value, const string &src, const string &dest) const
Convert value from the units of src to the units of dest.
Definition Units.cpp:538
A representation of the units associated with a dimensional quantity.
Definition Units.h:35
double factor() const
Return the factor for converting from this unit to Cantera's base units.
Definition Units.h:53
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
const double Tiny
Small number to compare differences of mole fractions against.
Definition ct_defs.h:173
offset
Offsets of solution components in the 1D solution array.
Definition Flow1D.h:24
void perturbPressure(double deltaP)
Perturb pressure of data container.
double m_pressure_buf
buffered pressure
void update(double T) override
Update data container based on temperature T
void restore() override
Restore data container after a perturbation.
double pressure
pressure
double temperature
temperature
virtual void restore()
Restore data container after a perturbation.
Unit aggregation utility.
Definition Units.h:105