| Cantera 3.0.0

Loading...
Searching...
No Matches
IdealGasPhase.cpp
Go to the documentation of this file.
1/**
2 * @file IdealGasPhase.cpp
3 * ThermoPhase object for the ideal gas equation of
4 * state - workhorse for %Cantera (see @ref thermoprops
5 * and class @link Cantera::IdealGasPhase IdealGasPhase@endlink).
6 */
7
8// This file is part of Cantera. See License.txt in the top-level directory or
9// at https://cantera.org/license.txt for license and copyright information.
10
11#include "cantera/thermo/IdealGasPhase.h"
12#include "cantera/thermo/ThermoFactory.h"
13#include "cantera/base/utilities.h"
14
15 namespace Cantera
16{
17
18 IdealGasPhase::IdealGasPhase(const string& inputFile, const string& id_)
19{
20 initThermoFile(inputFile, id_);
21}
22
23// Molar Thermodynamic Properties of the Solution ------------------
24
25 double IdealGasPhase::entropy_mole() const
26{
27 return GasConstant * (mean_X(entropy_R_ref()) - sum_xlogx() - std::log(pressure() / refPressure()));
28}
29
30 double IdealGasPhase::cp_mole() const
31{
32 return GasConstant * mean_X(cp_R_ref());
33}
34
35 double IdealGasPhase::cv_mole() const
36{
37 return cp_mole() - GasConstant;
38}
39
40 double IdealGasPhase::soundSpeed() const {
41 return sqrt(
42 cp_mole() / cv_mole() * GasConstant / meanMolecularWeight() * temperature()
43 );
44}
45
46 double IdealGasPhase::standardConcentration(size_t k) const
47{
48 return pressure() / RT();
49}
50
51 void IdealGasPhase::getActivityCoefficients(double* ac) const
52{
53 for (size_t k = 0; k < m_kk; k++) {
54 ac[k] = 1.0;
55 }
56}
57
58 void IdealGasPhase::getStandardChemPotentials(double* muStar) const
59{
60 getGibbs_ref(muStar);
61 double tmp = log(pressure() / refPressure()) * RT();
62 for (size_t k = 0; k < m_kk; k++) {
63 muStar[k] += tmp; // add RT*ln(P/P_0)
64 }
65}
66
67// Partial Molar Properties of the Solution --------------
68
69 void IdealGasPhase::getChemPotentials(double* mu) const
70{
71 getStandardChemPotentials(mu);
72 for (size_t k = 0; k < m_kk; k++) {
73 double xx = std::max(SmallNumber, moleFraction(k));
74 mu[k] += RT() * log(xx);
75 }
76}
77
78 void IdealGasPhase::getPartialMolarEnthalpies(double* hbar) const
79{
80 const vector<double>& _h = enthalpy_RT_ref();
81 scale(_h.begin(), _h.end(), hbar, RT());
82}
83
84 void IdealGasPhase::getPartialMolarEntropies(double* sbar) const
85{
86 const vector<double>& _s = entropy_R_ref();
87 scale(_s.begin(), _s.end(), sbar, GasConstant);
88 double logp = log(pressure() / refPressure());
89 for (size_t k = 0; k < m_kk; k++) {
90 double xx = std::max(SmallNumber, moleFraction(k));
91 sbar[k] += GasConstant * (-logp - log(xx));
92 }
93}
94
95 void IdealGasPhase::getPartialMolarIntEnergies(double* ubar) const
96{
97 const vector<double>& _h = enthalpy_RT_ref();
98 for (size_t k = 0; k < m_kk; k++) {
99 ubar[k] = RT() * (_h[k] - 1.0);
100 }
101}
102
103 void IdealGasPhase::getPartialMolarCp(double* cpbar) const
104{
105 const vector<double>& _cp = cp_R_ref();
106 scale(_cp.begin(), _cp.end(), cpbar, GasConstant);
107}
108
109 void IdealGasPhase::getPartialMolarVolumes(double* vbar) const
110{
111 double vol = 1.0 / molarDensity();
112 for (size_t k = 0; k < m_kk; k++) {
113 vbar[k] = vol;
114 }
115}
116
117// Properties of the Standard State of the Species in the Solution --
118
119 void IdealGasPhase::getEnthalpy_RT(double* hrt) const
120{
121 const vector<double>& _h = enthalpy_RT_ref();
122 copy(_h.begin(), _h.end(), hrt);
123}
124
125 void IdealGasPhase::getEntropy_R(double* sr) const
126{
127 const vector<double>& _s = entropy_R_ref();
128 copy(_s.begin(), _s.end(), sr);
129 double tmp = log(pressure() / refPressure());
130 for (size_t k = 0; k < m_kk; k++) {
131 sr[k] -= tmp;
132 }
133}
134
135 void IdealGasPhase::getGibbs_RT(double* grt) const
136{
137 const vector<double>& gibbsrt = gibbs_RT_ref();
138 copy(gibbsrt.begin(), gibbsrt.end(), grt);
139 double tmp = log(pressure() / refPressure());
140 for (size_t k = 0; k < m_kk; k++) {
141 grt[k] += tmp;
142 }
143}
144
145 void IdealGasPhase::getPureGibbs(double* gpure) const
146{
147 const vector<double>& gibbsrt = gibbs_RT_ref();
148 scale(gibbsrt.begin(), gibbsrt.end(), gpure, RT());
149 double tmp = log(pressure() / refPressure()) * RT();
150 for (size_t k = 0; k < m_kk; k++) {
151 gpure[k] += tmp;
152 }
153}
154
155 void IdealGasPhase::getIntEnergy_RT(double* urt) const
156{
157 getIntEnergy_RT_ref(urt);
158}
159
160 void IdealGasPhase::getCp_R(double* cpr) const
161{
162 const vector<double>& _cpr = cp_R_ref();
163 copy(_cpr.begin(), _cpr.end(), cpr);
164}
165
166 void IdealGasPhase::getStandardVolumes(double* vol) const
167{
168 double tmp = 1.0 / molarDensity();
169 for (size_t k = 0; k < m_kk; k++) {
170 vol[k] = tmp;
171 }
172}
173
174// Thermodynamic Values for the Species Reference States ---------
175
176 void IdealGasPhase::getEnthalpy_RT_ref(double* hrt) const
177{
178 const vector<double>& _h = enthalpy_RT_ref();
179 copy(_h.begin(), _h.end(), hrt);
180}
181
182 void IdealGasPhase::getGibbs_RT_ref(double* grt) const
183{
184 const vector<double>& gibbsrt = gibbs_RT_ref();
185 copy(gibbsrt.begin(), gibbsrt.end(), grt);
186}
187
188 void IdealGasPhase::getGibbs_ref(double* g) const
189{
190 const vector<double>& gibbsrt = gibbs_RT_ref();
191 scale(gibbsrt.begin(), gibbsrt.end(), g, RT());
192}
193
194 void IdealGasPhase::getEntropy_R_ref(double* er) const
195{
196 const vector<double>& _s = entropy_R_ref();
197 copy(_s.begin(), _s.end(), er);
198}
199
200 void IdealGasPhase::getIntEnergy_RT_ref(double* urt) const
201{
202 const vector<double>& _h = enthalpy_RT_ref();
203 for (size_t k = 0; k < m_kk; k++) {
204 urt[k] = _h[k] - 1.0;
205 }
206}
207
208 void IdealGasPhase::getCp_R_ref(double* cprt) const
209{
210 const vector<double>& _cpr = cp_R_ref();
211 copy(_cpr.begin(), _cpr.end(), cprt);
212}
213
214 void IdealGasPhase::getStandardVolumes_ref(double* vol) const
215{
216 double tmp = RT() / m_p0;
217 for (size_t k = 0; k < m_kk; k++) {
218 vol[k] = tmp;
219 }
220}
221
222 bool IdealGasPhase::addSpecies(shared_ptr<Species> spec)
223{
224 bool added = ThermoPhase::addSpecies(spec);
225 if (added) {
226 if (m_kk == 1) {
227 m_p0 = refPressure();
228 }
229 m_h0_RT.push_back(0.0);
230 m_g0_RT.push_back(0.0);
231 m_expg0_RT.push_back(0.0);
232 m_cp0_R.push_back(0.0);
233 m_s0_R.push_back(0.0);
234 m_pp.push_back(0.0);
235 }
236 return added;
237}
238
239 void IdealGasPhase::setToEquilState(const double* mu_RT)
240{
241 const vector<double>& grt = gibbs_RT_ref();
242
243 // Within the method, we protect against inf results if the exponent is too
244 // high.
245 //
246 // If it is too low, we set the partial pressure to zero. This capability is
247 // needed by the elemental potential method.
248 double pres = 0.0;
249 for (size_t k = 0; k < m_kk; k++) {
250 double tmp = -grt[k] + mu_RT[k];
251 if (tmp < -600.) {
252 m_pp[k] = 0.0;
253 } else if (tmp > 300.0) {
254 double tmp2 = tmp / 300.;
255 tmp2 *= tmp2;
256 m_pp[k] = m_p0 * exp(300.) * tmp2;
257 } else {
258 m_pp[k] = m_p0 * exp(tmp);
259 }
260 pres += m_pp[k];
261 }
262 // set state
263 setMoleFractions(m_pp.data());
264 setPressure(pres);
265}
266
267 void IdealGasPhase::updateThermo() const
268{
269 static const int cacheId = m_cache.getId();
270 CachedScalar cached = m_cache.getScalar(cacheId);
271 double tnow = temperature();
272
273 // If the temperature has changed since the last time these
274 // properties were computed, recompute them.
275 if (cached.state1 != tnow) {
276 m_spthermo.update(tnow, &m_cp0_R[0], &m_h0_RT[0], &m_s0_R[0]);
277 cached.state1 = tnow;
278
279 // update the species Gibbs functions
280 for (size_t k = 0; k < m_kk; k++) {
281 m_g0_RT[k] = m_h0_RT[k] - m_s0_R[k];
282 }
283 }
284}
285}
IdealGasPhase.h
ThermoPhase object for the ideal gas equation of state - workhorse for Cantera (see Thermodynamic Pro...
ThermoFactory.h
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
Cantera::IdealGasPhase::entropy_R_ref
const vector< double > & entropy_R_ref() const
Returns a reference to the dimensionless reference state Entropy vector.
Definition IdealGasPhase.h:535
Cantera::IdealGasPhase::getPartialMolarEnthalpies
void getPartialMolarEnthalpies(double *hbar) const override
Returns an array of partial molar enthalpies for the species in the mixture.
Definition IdealGasPhase.cpp:78
Cantera::IdealGasPhase::getChemPotentials
void getChemPotentials(double *mu) const override
Get the species chemical potentials. Units: J/kmol.
Definition IdealGasPhase.cpp:69
Cantera::IdealGasPhase::m_p0
double m_p0
Reference state pressure.
Definition IdealGasPhase.h:561
Cantera::IdealGasPhase::soundSpeed
double soundSpeed() const override
Return the speed of sound. Units: m/s.
Definition IdealGasPhase.cpp:40
Cantera::IdealGasPhase::pressure
double pressure() const override
Pressure.
Definition IdealGasPhase.h:338
Cantera::IdealGasPhase::m_g0_RT
vector< double > m_g0_RT
Temporary storage for dimensionless reference state Gibbs energies.
Definition IdealGasPhase.h:570
Cantera::IdealGasPhase::getEntropy_R
void getEntropy_R(double *sr) const override
Get the array of nondimensional Entropy functions for the standard state species at the current T and...
Definition IdealGasPhase.cpp:125
Cantera::IdealGasPhase::m_h0_RT
vector< double > m_h0_RT
Temporary storage for dimensionless reference state enthalpies.
Definition IdealGasPhase.h:564
Cantera::IdealGasPhase::getGibbs_ref
void getGibbs_ref(double *g) const override
Returns the vector of the Gibbs function of the reference state at the current temperature of the sol...
Definition IdealGasPhase.cpp:188
Cantera::IdealGasPhase::m_pp
vector< double > m_pp
Temporary array containing internally calculated partial pressures.
Definition IdealGasPhase.h:578
Cantera::IdealGasPhase::getStandardChemPotentials
void getStandardChemPotentials(double *mu) const override
Get the array of chemical potentials at unit activity for the species at their standard states at the...
Definition IdealGasPhase.cpp:58
Cantera::IdealGasPhase::getCp_R
void getCp_R(double *cpr) const override
Get the nondimensional Heat Capacities at constant pressure for the species standard states at the cu...
Definition IdealGasPhase.cpp:160
Cantera::IdealGasPhase::setPressure
void setPressure(double p) override
Set the pressure at constant temperature and composition.
Definition IdealGasPhase.h:352
Cantera::IdealGasPhase::gibbs_RT_ref
const vector< double > & gibbs_RT_ref() const
Returns a reference to the dimensionless reference state Gibbs free energy vector.
Definition IdealGasPhase.h:525
Cantera::IdealGasPhase::getStandardVolumes_ref
void getStandardVolumes_ref(double *vol) const override
Get the molar volumes of the species reference states at the current T and P_ref of the solution.
Definition IdealGasPhase.cpp:214
Cantera::IdealGasPhase::getPartialMolarVolumes
void getPartialMolarVolumes(double *vbar) const override
Return an array of partial molar volumes for the species in the mixture.
Definition IdealGasPhase.cpp:109
Cantera::IdealGasPhase::cv_mole
double cv_mole() const override
Molar heat capacity at constant volume.
Definition IdealGasPhase.cpp:35
Cantera::IdealGasPhase::updateThermo
virtual void updateThermo() const
Update the species reference state thermodynamic functions.
Definition IdealGasPhase.cpp:267
Cantera::IdealGasPhase::getPureGibbs
void getPureGibbs(double *gpure) const override
Get the Gibbs functions for the standard state of the species at the current T and P of the solution.
Definition IdealGasPhase.cpp:145
Cantera::IdealGasPhase::getIntEnergy_RT_ref
void getIntEnergy_RT_ref(double *urt) const override
Returns the vector of nondimensional internal Energies of the reference state at the current temperat...
Definition IdealGasPhase.cpp:200
Cantera::IdealGasPhase::getEnthalpy_RT
void getEnthalpy_RT(double *hrt) const override
Get the nondimensional Enthalpy functions for the species at their standard states at the current T a...
Definition IdealGasPhase.cpp:119
Cantera::IdealGasPhase::getEntropy_R_ref
void getEntropy_R_ref(double *er) const override
Returns the vector of nondimensional entropies of the reference state at the current temperature of t...
Definition IdealGasPhase.cpp:194
Cantera::IdealGasPhase::m_s0_R
vector< double > m_s0_R
Temporary storage for dimensionless reference state entropies.
Definition IdealGasPhase.h:573
Cantera::IdealGasPhase::getGibbs_RT
void getGibbs_RT(double *grt) const override
Get the nondimensional Gibbs functions for the species in their standard states at the current T and ...
Definition IdealGasPhase.cpp:135
Cantera::IdealGasPhase::entropy_mole
double entropy_mole() const override
Molar entropy.
Definition IdealGasPhase.cpp:25
Cantera::IdealGasPhase::getCp_R_ref
void getCp_R_ref(double *cprt) const override
Returns the vector of nondimensional constant pressure heat capacities of the reference state at the ...
Definition IdealGasPhase.cpp:208
Cantera::IdealGasPhase::getStandardVolumes
void getStandardVolumes(double *vol) const override
Get the molar volumes of the species standard states at the current T and P of the solution.
Definition IdealGasPhase.cpp:166
Cantera::IdealGasPhase::getPartialMolarIntEnergies
void getPartialMolarIntEnergies(double *ubar) const override
Return an array of partial molar internal energies for the species in the mixture.
Definition IdealGasPhase.cpp:95
Cantera::IdealGasPhase::cp_mole
double cp_mole() const override
Molar heat capacity at constant pressure.
Definition IdealGasPhase.cpp:30
Cantera::IdealGasPhase::getIntEnergy_RT
void getIntEnergy_RT(double *urt) const override
Returns the vector of nondimensional Internal Energies of the standard state species at the current T...
Definition IdealGasPhase.cpp:155
Cantera::IdealGasPhase::getPartialMolarCp
void getPartialMolarCp(double *cpbar) const override
Return an array of partial molar heat capacities for the species in the mixture.
Definition IdealGasPhase.cpp:103
Cantera::IdealGasPhase::standardConcentration
double standardConcentration(size_t k=0) const override
Returns the standard concentration , which is used to normalize the generalized concentration.
Definition IdealGasPhase.cpp:46
Cantera::IdealGasPhase::m_cp0_R
vector< double > m_cp0_R
Temporary storage for dimensionless reference state heat capacities.
Definition IdealGasPhase.h:567
Cantera::IdealGasPhase::addSpecies
bool addSpecies(shared_ptr< Species > spec) override
Add a Species to this Phase.
Definition IdealGasPhase.cpp:222
Cantera::IdealGasPhase::setToEquilState
void setToEquilState(const double *mu_RT) override
This method is used by the ChemEquil equilibrium solver.
Definition IdealGasPhase.cpp:239
Cantera::IdealGasPhase::getGibbs_RT_ref
void getGibbs_RT_ref(double *grt) const override
Returns the vector of nondimensional Gibbs Free Energies of the reference state at the current temper...
Definition IdealGasPhase.cpp:182
Cantera::IdealGasPhase::getActivityCoefficients
void getActivityCoefficients(double *ac) const override
Get the array of non-dimensional activity coefficients at the current solution temperature,...
Definition IdealGasPhase.cpp:51
Cantera::IdealGasPhase::IdealGasPhase
IdealGasPhase(const string &inputFile="", const string &id="")
Construct and initialize an IdealGasPhase ThermoPhase object directly from an input file.
Definition IdealGasPhase.cpp:18
Cantera::IdealGasPhase::getPartialMolarEntropies
void getPartialMolarEntropies(double *sbar) const override
Returns an array of partial molar entropies of the species in the solution.
Definition IdealGasPhase.cpp:84
Cantera::IdealGasPhase::cp_R_ref
const vector< double > & cp_R_ref() const
Returns a reference to the dimensionless reference state Heat Capacity vector.
Definition IdealGasPhase.h:545
Cantera::IdealGasPhase::getEnthalpy_RT_ref
void getEnthalpy_RT_ref(double *hrt) const override
Returns the vector of nondimensional enthalpies of the reference state at the current temperature of ...
Definition IdealGasPhase.cpp:176
Cantera::IdealGasPhase::enthalpy_RT_ref
const vector< double > & enthalpy_RT_ref() const
Returns a reference to the dimensionless reference state enthalpy vector.
Definition IdealGasPhase.h:515
Cantera::MultiSpeciesThermo::update
virtual void update(double T, double *cp_R, double *h_RT, double *s_R) const
Compute the reference-state properties for all species.
Definition MultiSpeciesThermo.cpp:94
Cantera::Phase::molarDensity
virtual double molarDensity() const
Molar density (kmol/m^3).
Definition Phase.cpp:689
Cantera::Phase::setMoleFractions
virtual void setMoleFractions(const double *const x)
Set the mole fractions to the specified values.
Definition Phase.cpp:304
Cantera::Phase::m_cache
ValueCache m_cache
Cached for saved calculations within each ThermoPhase.
Definition Phase.h:927
Cantera::Phase::m_kk
size_t m_kk
Number of species in the phase.
Definition Phase.h:947
Cantera::Phase::temperature
double temperature() const
Temperature (K).
Definition Phase.h:662
Cantera::Phase::meanMolecularWeight
double meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition Phase.h:760
Cantera::Phase::sum_xlogx
double sum_xlogx() const
Evaluate .
Definition Phase.cpp:747
Cantera::Phase::moleFraction
double moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition Phase.cpp:545
Cantera::Phase::mean_X
double mean_X(const double *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition Phase.cpp:737
Cantera::ThermoPhase::RT
double RT() const
Return the Gas Constant multiplied by the current temperature.
Definition ThermoPhase.h:1077
Cantera::ThermoPhase::initThermoFile
void initThermoFile(const string &inputFile, const string &id)
Initialize a ThermoPhase object using an input file.
Definition ThermoPhase.cpp:1072
Cantera::ThermoPhase::m_spthermo
MultiSpeciesThermo m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition ThermoPhase.h:2126
Cantera::ThermoPhase::refPressure
virtual double refPressure() const
Returns the reference pressure in Pa.
Definition ThermoPhase.h:436
Cantera::ThermoPhase::addSpecies
bool addSpecies(shared_ptr< Species > spec) override
Add a Species to this Phase.
Definition ThermoPhase.cpp:1131
Cantera::ValueCache::getScalar
CachedScalar getScalar(int id)
Get a reference to a CachedValue object representing a scalar (double) with the given id.
Definition ValueCache.h:161
Cantera::ValueCache::getId
int getId()
Get a unique id for a cached value.
Definition ValueCache.cpp:21
Cantera::scale
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition utilities.h:104
Cantera::GasConstant
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition ct_defs.h:120
Cantera
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564
Cantera::SmallNumber
const double SmallNumber
smallest number to compare to zero.
Definition ct_defs.h:158
utilities.h
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...