Cantera  3.1.0a1
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 
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  }
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 
50 ChebyshevRate::ChebyshevRate(double Tmin, double Tmax, double Pmin, double Pmax,
51  const Array2D& coeffs) : ChebyshevRate()
52 {
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 
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 }
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
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
vector< double > & data()
Return a reference to the data vector.
Definition: Array.h:186
size_t nRows() const
Number of rows.
Definition: Array.h:176
size_t nColumns() const
Number of columns.
Definition: Array.h:181
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:66
Pressure-dependent rate expression where the rate coefficient is expressed as a bivariate Chebyshev p...
Definition: ChebyshevRate.h:92
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:738
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.
Definition: ReactionRate.h:101
bool valid() const
Get flag indicating whether reaction rate is set up correctly.
Definition: ReactionRate.h:203
const Units & conversionUnits() const
Get the units for converting the leading term in the reaction rate expression.
Definition: ReactionRate.h:121
bool m_valid
Flag indicating whether reaction rate is set up correctly.
Definition: ReactionRate.h:236
AnyMap m_input
Input data used for specific models.
Definition: ReactionRate.h:230
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:390
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:564
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: StFlow.h:24
void perturbPressure(double deltaP)
Perturb pressure of data container.
double m_pressure_buf
buffered pressure
Definition: ChebyshevRate.h:57
virtual void update(double T)
Update data container based on temperature T
Definition: ReactionData.h:36
void restore() override
Restore data container after a perturbation.
double pressure
pressure
Definition: ChebyshevRate.h:53
double temperature
temperature
Definition: ReactionData.h:110
virtual void restore()
Restore data container after a perturbation.
Definition: ReactionData.h:90
Unit aggregation utility.
Definition: Units.h:105