Cantera  3.1.0b1
Loading...
Searching...
No Matches
Mu0Poly.cpp
Go to the documentation of this file.
1/**
2 * @file Mu0Poly.cpp
3 * Definitions for a single-species standard state object derived
4 * from @link Cantera::SpeciesThermoInterpType SpeciesThermoInterpType@endlink based
5 * on a piecewise constant mu0 interpolation
6 * (see @ref spthermo and class @link Cantera::Mu0Poly Mu0Poly@endlink).
7 */
8
9// This file is part of Cantera. See License.txt in the top-level directory or
10// at https://cantera.org/license.txt for license and copyright information.
11
14#include "cantera/base/AnyMap.h"
15
16namespace Cantera
17{
18Mu0Poly::Mu0Poly()
19 : SpeciesThermoInterpType(0.0, std::numeric_limits<double>::infinity(), 0.0)
20{
21}
22
23Mu0Poly::Mu0Poly(double tlow, double thigh, double pref, const double* coeffs) :
24 SpeciesThermoInterpType(tlow, thigh, pref),
25 m_numIntervals(0),
26 m_H298(0.0)
27{
28 map<double, double> T_mu;
29 size_t nPoints = (size_t) coeffs[0];
30 for (size_t i = 0; i < nPoints; i++) {
31 T_mu[coeffs[2*i+2]] = coeffs[2*i+3];
32 }
33 setParameters(coeffs[1], T_mu);
34}
35
36void Mu0Poly::setParameters(double h0, const map<double, double>& T_mu)
37{
38 size_t nPoints = T_mu.size();
39 if (nPoints < 2) {
40 throw CanteraError("Mu0Poly::setParameters", "nPoints must be >= 2");
41 }
42 m_numIntervals = nPoints - 1;
43 m_H298 = h0 / GasConstant;
44
45 // Distribute the data into the internal arrays, and find the index of the
46 // point at 298.15 K.
47 size_t iT298 = npos;
48 for (const auto& [T1, mu] : T_mu) {
49 if (T1 == 298.15) {
50 iT298 = m_t0_int.size();
51 }
52 m_t0_int.push_back(T1);
53 m_mu0_R_int.push_back(mu / GasConstant);
54 }
55 if (iT298 == npos) {
56 throw CanteraError("Mu0Poly::setParameters",
57 "One temperature has to be 298.15");
58 }
59
60 // Resize according to the number of points
61 m_h0_R_int.resize(nPoints);
62 m_s0_R_int.resize(nPoints);
63 m_cp0_R_int.resize(nPoints);
64
65 // Starting from the interval with T298, we go up
66 m_h0_R_int[iT298] = m_H298;
67 m_s0_R_int[iT298] = - (m_mu0_R_int[iT298] - m_h0_R_int[iT298]) / m_t0_int[iT298];
68 for (size_t i = iT298; i < m_numIntervals; i++) {
69 double T1 = m_t0_int[i];
70 double s1 = m_s0_R_int[i];
71 double T2 = m_t0_int[i+1];
72 double deltaMu = m_mu0_R_int[i+1] - m_mu0_R_int[i];
73 double deltaT = T2 - T1;
74 double cpi = (deltaMu - T1 * s1 + T2 * s1) / (deltaT - T2 * log(T2/T1));
75 m_cp0_R_int[i] = cpi;
76 m_h0_R_int[i+1] = m_h0_R_int[i] + cpi * deltaT;
77 m_s0_R_int[i+1] = s1 + cpi * log(T2/T1);
78 m_cp0_R_int[i+1] = cpi;
79 }
80
81 // Starting from the interval with T298, we go down
82 if (iT298 != 0) {
83 m_h0_R_int[iT298] = m_H298;
84 m_s0_R_int[iT298] = - (m_mu0_R_int[iT298] - m_h0_R_int[iT298]) / m_t0_int[iT298];
85 for (size_t i = iT298 - 1; i != npos; i--) {
86 double T1 = m_t0_int[i];
87 double T2 = m_t0_int[i+1];
88 double s2 = m_s0_R_int[i+1];
89 double deltaMu = m_mu0_R_int[i+1] - m_mu0_R_int[i];
90 double deltaT = T2 - T1;
91 double cpi = (deltaMu - T1 * s2 + T2 * s2) / (deltaT - T1 * log(T2/T1));
92 m_cp0_R_int[i] = cpi;
93 m_h0_R_int[i] = m_h0_R_int[i+1] - cpi * deltaT;
94 m_s0_R_int[i] = s2 - cpi * log(T2/T1);
95 if (i == (m_numIntervals-1)) {
96 m_cp0_R_int[i+1] = cpi;
97 }
98 }
99 }
100}
101
102void Mu0Poly::updateProperties(const double* tt, double* cp_R,
103 double* h_RT, double* s_R) const
104{
105 size_t j = m_numIntervals;
106 double T = *tt;
107 for (size_t i = 0; i < m_numIntervals; i++) {
108 double T2 = m_t0_int[i+1];
109 if (T <=T2) {
110 j = i;
111 break;
112 }
113 }
114 double T1 = m_t0_int[j];
115 double cp_Rj = m_cp0_R_int[j];
116 *cp_R = cp_Rj;
117 *h_RT = (m_h0_R_int[j] + (T - T1) * cp_Rj)/T;
118 *s_R = m_s0_R_int[j] + cp_Rj * (log(T/T1));
119}
120
122 double* cp_R,
123 double* h_RT,
124 double* s_R) const
125{
126 updateProperties(&T, cp_R, h_RT, s_R);
127}
128
129size_t Mu0Poly::nCoeffs() const
130{
131 return 2*m_numIntervals + 4;
132}
133
134void Mu0Poly::reportParameters(size_t& n, int& type, double& tlow, double& thigh,
135 double& pref, double* const coeffs) const
136{
137 n = 0;
138 type = MU0_INTERP;
139 tlow = m_lowT;
140 thigh = m_highT;
141 pref = m_Pref;
142 coeffs[0] = int(m_numIntervals)+1;
143 coeffs[1] = m_H298 * GasConstant;
144 int j = 2;
145 for (size_t i = 0; i < m_numIntervals+1; i++) {
146 coeffs[j] = m_t0_int[i];
147 coeffs[j+1] = m_mu0_R_int[i] * GasConstant;
148 j += 2;
149 }
150}
151
153{
155 thermo["model"] = "piecewise-Gibbs";
156 thermo["h0"].setQuantity(m_H298 * GasConstant, "J/kmol");
157 AnyMap data;
158 bool dimensionless = m_input.getBool("dimensionless", false);
159 if (dimensionless) {
160 thermo["dimensionless"] = true;
161 }
162 for (size_t i = 0; i < m_numIntervals+1; i++) {
163 if (dimensionless) {
164 data[fmt::format("{}", m_t0_int[i])] = m_mu0_R_int[i] / m_t0_int[i];
165 } else {
166 data[fmt::format("{}", m_t0_int[i])].setQuantity(
167 m_mu0_R_int[i] * GasConstant, "J/kmol");
168 }
169 }
170 thermo["data"] = std::move(data);
171}
172
173}
Header for a single-species standard state object derived from SpeciesThermoInterpType based on a pie...
A map of string keys to values whose type can vary at runtime.
Definition AnyMap.h:431
bool getBool(const string &key, bool default_) const
If key exists, return it as a bool, otherwise return default_.
Definition AnyMap.cpp:1575
Base class for exceptions thrown by Cantera classes.
vector< double > m_t0_int
Points at which the standard state chemical potential are given.
Definition Mu0Poly.h:144
double m_H298
Value of the enthalpy at T = 298.15.
Definition Mu0Poly.h:141
void getParameters(AnyMap &thermo) const override
Store the parameters of the species thermo object such that an identical species thermo object could ...
Definition Mu0Poly.cpp:152
vector< double > m_h0_R_int
Dimensionless Enthalpies at the temperature points.
Definition Mu0Poly.h:151
size_t nCoeffs() const override
This utility function returns the number of coefficients for a given type of species parameterization...
Definition Mu0Poly.cpp:129
void reportParameters(size_t &n, int &type, double &tlow, double &thigh, double &pref, double *const coeffs) const override
This utility function returns the type of parameterization and all of the parameters for the species.
Definition Mu0Poly.cpp:134
vector< double > m_cp0_R_int
Heat capacity at the points.
Definition Mu0Poly.h:157
size_t m_numIntervals
Number of intervals in the interpolating linear approximation.
Definition Mu0Poly.h:137
vector< double > m_s0_R_int
Entropy at the points.
Definition Mu0Poly.h:154
void updateProperties(const double *tt, double *cp_R, double *h_RT, double *s_R) const override
Update the properties for this species, given a temperature polynomial.
Definition Mu0Poly.cpp:102
void setParameters(double h0, const map< double, double > &T_mu)
Set parameters for .
Definition Mu0Poly.cpp:36
vector< double > m_mu0_R_int
Mu0's are primary input data.
Definition Mu0Poly.h:148
void updatePropertiesTemp(const double temp, double *cp_R, double *h_RT, double *s_R) const override
Compute the reference-state property of one species.
Definition Mu0Poly.cpp:121
Abstract Base class for the thermodynamic manager for an individual species' reference state.
double m_Pref
Reference state pressure.
virtual void getParameters(AnyMap &thermo) const
Store the parameters of the species thermo object such that an identical species thermo object could ...
double m_lowT
lowest valid temperature
double m_highT
Highest valid temperature.
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition ct_defs.h:120
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
const size_t npos
index returned by functions to indicate "no position"
Definition ct_defs.h:180
#define MU0_INTERP
piecewise interpolation of mu0.
Contains declarations for string manipulation functions within Cantera.