| Cantera 3.0.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
6#include "cantera/kinetics/ChebyshevRate.h"
7#include "cantera/thermo/ThermoPhase.h"
8
9 namespace Cantera
10{
11
12 void ChebyshevData::update(double T)
13{
14 throw CanteraError("ChebyshevData::update",
15 "Missing state information: 'ChebyshevData' requires pressure.");
16}
17
18 bool 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
29 void ChebyshevData::perturbPressure(double deltaP)
30{
31 if (m_pressure_buf > 0.) {
32 throw CanteraError("ChebyshevData::perturbPressure",
33 "Cannot apply another perturbation as state is already perturbed.");
34 }
35 m_pressure_buf = pressure;
36 update(temperature, pressure * (1. + deltaP));
37}
38
39 void ChebyshevData::restore()
40{
41 ReactionData::restore();
42 // only restore if there is a valid buffered value
43 if (m_pressure_buf < 0.) {
44 return;
45 }
46 update(temperature, m_pressure_buf);
47 m_pressure_buf = -1.;
48}
49
50 ChebyshevRate::ChebyshevRate(double Tmin, double Tmax, double Pmin, double Pmax,
51 const Array2D& coeffs) : ChebyshevRate()
52{
53 setLimits(Tmin, Tmax, Pmin, Pmax);
54 setData(coeffs);
55}
56
57 ChebyshevRate::ChebyshevRate(const AnyMap& node, const UnitStack& rate_units)
58 : ChebyshevRate()
59{
60 setParameters(node, rate_units);
61}
62
63 void 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);
84 setLimits(
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
96 void 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
114 void ChebyshevRate::setData(const Array2D& coeffs)
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
126 void ChebyshevRate::getParameters(AnyMap& rateNode) const
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
160 void 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}
ChebyshevRate.h
ThermoPhase.h
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
Cantera::AnyMap
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:427
Cantera::AnyMap::hasKey
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition AnyMap.cpp:1423
Cantera::AnyMap::units
const UnitSystem & units() const
Return the default units that should be used to convert stored values.
Definition AnyMap.h:630
Cantera::AnyValue
A wrapper for a variable whose type is determined at runtime.
Definition AnyMap.h:86
Cantera::AnyValue::asVector
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
Cantera::Array2D
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition Array.h:32
Cantera::Array2D::nRows
size_t nRows() const
Number of rows.
Definition Array.h:192
Cantera::Array2D::nColumns
size_t nColumns() const
Number of columns.
Definition Array.h:197
Cantera::Array2D::data
vector< double > & data()
Return a reference to the data vector.
Definition Array.h:218
Cantera::CanteraError
Base class for exceptions thrown by Cantera classes.
Definition ctexceptions.h:66
Cantera::ChebyshevRate
Pressure-dependent rate expression where the rate coefficient is expressed as a bivariate Chebyshev p...
Definition ChebyshevRate.h:92
Cantera::ChebyshevRate::getParameters
void getParameters(AnyMap &rateNode) const override
Get parameters.
Definition ChebyshevRate.cpp:126
Cantera::ChebyshevRate::Tmax_
double Tmax_
valid temperature range
Definition ChebyshevRate.h:230
Cantera::ChebyshevRate::setParameters
void setParameters(const AnyMap &node, const UnitStack &rate_units) override
Perform object setup based on AnyMap node information.
Definition ChebyshevRate.cpp:63
Cantera::ChebyshevRate::TrDen_
double TrDen_
terms appearing in the reduced temperature
Definition ChebyshevRate.h:232
Cantera::ChebyshevRate::Pmin
double Pmin() const
Minimum valid pressure [Pa].
Definition ChebyshevRate.h:200
Cantera::ChebyshevRate::Tmin
double Tmin() const
Minimum valid temperature [K].
Definition ChebyshevRate.h:190
Cantera::ChebyshevRate::validate
void validate(const string &equation, const Kinetics &kin) override
Validate the reaction rate expression.
Definition ChebyshevRate.cpp:160
Cantera::ChebyshevRate::dotProd_
vector< double > dotProd_
dot product of coeffs with the reduced pressure polynomial
Definition ChebyshevRate.h:236
Cantera::ChebyshevRate::setData
void setData(const Array2D &coeffs)
Set the Chebyshev coefficients as 2-dimensional array.
Definition ChebyshevRate.cpp:114
Cantera::ChebyshevRate::Tmax
double Tmax() const
Maximum valid temperature [K].
Definition ChebyshevRate.h:195
Cantera::ChebyshevRate::ChebyshevRate
ChebyshevRate()=default
Default constructor.
Cantera::ChebyshevRate::m_coeffs
Array2D m_coeffs
< coefficient array
Definition ChebyshevRate.h:235
Cantera::ChebyshevRate::Pmax_
double Pmax_
valid pressure range
Definition ChebyshevRate.h:231
Cantera::ChebyshevRate::Pmax
double Pmax() const
Maximum valid pressure [Pa].
Definition ChebyshevRate.h:205
Cantera::ChebyshevRate::PrDen_
double PrDen_
terms appearing in the reduced pressure
Definition ChebyshevRate.h:233
Cantera::ChebyshevRate::setLimits
void setLimits(double Tmin, double Tmax, double Pmin, double Pmax)
Set limits for ChebyshevRate object.
Definition ChebyshevRate.cpp:96
Cantera::InputFileError
Error thrown for problems processing information contained in an AnyMap or AnyValue.
Definition AnyMap.h:738
Cantera::Kinetics
Public interface for kinetics managers.
Definition Kinetics.h:126
Cantera::Phase::temperature
double temperature() const
Temperature (K).
Definition Phase.h:662
Cantera::Phase::pressure
virtual double pressure() const
Return the thermodynamic pressure (Pa).
Definition Phase.h:680
Cantera::ReactionRate::setParameters
virtual void setParameters(const AnyMap &node, const UnitStack &units)
Set parameters.
Definition ReactionRate.h:101
Cantera::ReactionRate::conversionUnits
const Units & conversionUnits() const
Get the units for converting the leading term in the reaction rate expression.
Definition ReactionRate.h:121
Cantera::ReactionRate::valid
bool valid() const
Get flag indicating whether reaction rate is set up correctly.
Definition ReactionRate.h:220
Cantera::ReactionRate::m_valid
bool m_valid
Flag indicating whether reaction rate is set up correctly.
Definition ReactionRate.h:253
Cantera::ReactionRate::m_input
AnyMap m_input
Input data used for specific models.
Definition ReactionRate.h:247
Cantera::ThermoPhase
Base class for a phase with thermodynamic properties.
Definition ThermoPhase.h:390
Cantera::UnitSystem
Unit conversion utility.
Definition Units.h:169
Cantera::UnitSystem::convertRateCoeff
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
Cantera::UnitSystem::convert
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
Cantera::Units
A representation of the units associated with a dimensional quantity.
Definition Units.h:35
Cantera::Units::factor
double factor() const
Return the factor for converting from this unit to Cantera's base units.
Definition Units.h:53
Cantera
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564
Cantera::Tiny
const double Tiny
Small number to compare differences of mole fractions against.
Definition ct_defs.h:173
Cantera::offset
offset
Offsets of solution components in the 1D solution array.
Definition StFlow.h:24
Cantera::ChebyshevData::perturbPressure
void perturbPressure(double deltaP)
Perturb pressure of data container.
Definition ChebyshevRate.cpp:29
Cantera::ChebyshevData::m_pressure_buf
double m_pressure_buf
buffered pressure
Definition ChebyshevRate.h:57
Cantera::ChebyshevData::update
void update(double T) override
Update data container based on temperature T
Definition ChebyshevRate.cpp:12
Cantera::ChebyshevData::restore
void restore() override
Restore data container after a perturbation.
Definition ChebyshevRate.cpp:39
Cantera::ChebyshevData::pressure
double pressure
pressure
Definition ChebyshevRate.h:53
Cantera::ReactionData::temperature
double temperature
temperature
Definition ReactionData.h:110
Cantera::ReactionData::restore
virtual void restore()
Restore data container after a perturbation.
Definition ReactionData.h:90
Cantera::UnitStack
Unit aggregation utility.
Definition Units.h:105