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