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