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
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 void ChebyshevRate::setParameters(const AnyMap& node, const UnitStack& units)
58{
59 m_rate_units = units.product();
60 const UnitSystem& unit_system = node.units();
61 Array2D coeffs;
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 }
79 setLimits(
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
92 setData(coeffs);
93}
94
95 void 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().");
100 setLimits(Tmin, Tmax, Pmin, Pmax);
101 setData(coeffs);
102}
103
104 void 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
122 void ChebyshevRate::setData(const Array2D& coeffs)
123{
124 m_coeffs = coeffs;
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
139 void 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 };
169 AnyValue coeffs;
170 coeffs = std::move(coeffs2d);
171 rateNode["data"].setQuantity(coeffs, converter);
172}
173
174 void 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}
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:399
Cantera::AnyMap::units
const UnitSystem & units() const
Return the default units that should be used to convert stored values.
Definition: AnyMap.h:598
Cantera::AnyMap::hasKey
bool hasKey(const std::string &key) const
Returns true if the map contains an item named key.
Definition: AnyMap.cpp:1406
Cantera::AnyValue
A wrapper for a variable whose type is determined at runtime.
Definition: AnyMap.h:84
Cantera::Array2D
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Definition: Array.h:30
Cantera::Array2D::nRows
size_t nRows() const
Number of rows.
Definition: Array.h:188
Cantera::Array2D::nColumns
size_t nColumns() const
Number of columns.
Definition: Array.h:193
Cantera::Array2D::data
vector_fp & 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:61
Cantera::ChebyshevRate
Pressure-dependent rate expression where the rate coefficient is expressed as a bivariate Chebyshev p...
Definition: ChebyshevRate.h:90
Cantera::ChebyshevRate::setParameters
void setParameters(const AnyMap &node, const UnitStack &units)
Perform object setup based on AnyMap node information.
Definition: ChebyshevRate.cpp:57
Cantera::ChebyshevRate::Tmax_
double Tmax_
valid temperature range
Definition: ChebyshevRate.h:269
Cantera::ChebyshevRate::ChebyshevRate
ChebyshevRate()
Default constructor.
Definition: ChebyshevRate.h:93
Cantera::ChebyshevRate::TrDen_
double TrDen_
terms appearing in the reduced temperature
Definition: ChebyshevRate.h:271
Cantera::ChebyshevRate::m_rate_units
Units m_rate_units
Reaction rate units.
Definition: ChebyshevRate.h:278
Cantera::ChebyshevRate::Pmin
double Pmin() const
Minimum valid pressure [Pa].
Definition: ChebyshevRate.h:226
Cantera::ChebyshevRate::Tmin
double Tmin() const
Minimum valid temperature [K].
Definition: ChebyshevRate.h:216
Cantera::ChebyshevRate::setData
void setData(const Array2D &coeffs)
Set the Chebyshev coefficients as 2-dimensional array.
Definition: ChebyshevRate.cpp:122
Cantera::ChebyshevRate::Tmax
double Tmax() const
Maximum valid temperature [K].
Definition: ChebyshevRate.h:221
Cantera::ChebyshevRate::type
const std::string type() const
String identifying reaction rate specialization.
Definition: ChebyshevRate.h:119
Cantera::ChebyshevRate::m_coeffs
Array2D m_coeffs
< coefficient array
Definition: ChebyshevRate.h:274
Cantera::ChebyshevRate::Pmax_
double Pmax_
valid pressure range
Definition: ChebyshevRate.h:270
Cantera::ChebyshevRate::dotProd_
vector_fp dotProd_
dot product of chebCoeffs with the reduced pressure polynomial
Definition: ChebyshevRate.h:276
Cantera::ChebyshevRate::setup
void setup(double Tmin, double Tmax, double Pmin, double Pmax, const Array2D &coeffs)
Set up ChebyshevRate object.
Definition: ChebyshevRate.cpp:95
Cantera::ChebyshevRate::Pmax
double Pmax() const
Maximum valid pressure [Pa].
Definition: ChebyshevRate.h:231
Cantera::ChebyshevRate::PrDen_
double PrDen_
terms appearing in the reduced pressure
Definition: ChebyshevRate.h:272
Cantera::ChebyshevRate::chebCoeffs_
vector_fp chebCoeffs_
Chebyshev coefficients, length nP * nT.
Definition: ChebyshevRate.h:275
Cantera::ChebyshevRate::coeffs
const vector_fp & coeffs() const
Access the ChebyshevRate coefficients.
Definition: ChebyshevRate.h:252
Cantera::ChebyshevRate::setLimits
void setLimits(double Tmin, double Tmax, double Pmin, double Pmax)
Set limits for ChebyshevRate object.
Definition: ChebyshevRate.cpp:104
Cantera::ChebyshevRate::validate
virtual void validate(const std::string &equation, const Kinetics &kin)
Validate the reaction rate expression.
Definition: ChebyshevRate.cpp:174
Cantera::InputFileError
Error thrown for problems processing information contained in an AnyMap or AnyValue.
Definition: AnyMap.h:702
Cantera::Kinetics
Public interface for kinetics managers.
Definition: Kinetics.h:114
Cantera::Phase::temperature
doublereal temperature() const
Temperature (K).
Definition: Phase.h:654
Cantera::Phase::pressure
virtual double pressure() const
Return the thermodynamic pressure (Pa).
Definition: Phase.h:672
Cantera::ThermoPhase
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:102
Cantera::UnitSystem
Unit conversion utility.
Definition: Units.h:161
Cantera::UnitSystem::convert
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
Cantera::UnitSystem::convertTo
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
Cantera::Units
A representation of the units associated with a dimensional quantity.
Definition: Units.h:30
Cantera::Units::factor
double factor() const
Return the factor for converting from this unit to Cantera's base units.
Definition: Units.h:48
Cantera
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:557
Cantera::warn_deprecated
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
Cantera::vector_fp
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
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
virtual void update(double T) override
Update data container based on temperature T
Definition: ChebyshevRate.cpp:12
Cantera::ChebyshevData::restore
virtual 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:109
Cantera::ReactionData::restore
virtual void restore()
Restore data container after a perturbation.
Definition: ReactionData.h:89
Cantera::UnitStack
Unit aggregation utility.
Definition: Units.h:99
Cantera::UnitStack::product
Units product() const
Calculate product of units-exponent stack.
Definition: Units.cpp:388