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