Cantera  2.1.2
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 #include "cantera/base/ct_defs.h"
12 
13 using namespace std;
14 
15 namespace Cantera
16 {
17 
18 IdealGasPhase::IdealGasPhase() :
19  m_p0(-1.0),
20  m_tlast(0.0),
21  m_logc0(0.0)
22 {
23 }
24 
25 IdealGasPhase::IdealGasPhase(const std::string& inputFile, const std::string& id_) :
26  m_p0(-1.0),
27  m_tlast(0.0),
28  m_logc0(0.0)
29 {
30  initThermoFile(inputFile, id_);
31 }
32 
33 IdealGasPhase::IdealGasPhase(XML_Node& phaseRef, const std::string& id_) :
34  m_p0(-1.0),
35  m_tlast(0.0),
36  m_logc0(0.0)
37 {
38  initThermoXML(phaseRef, id_);
39 }
40 
42  m_p0(right.m_p0),
43  m_tlast(right.m_tlast),
44  m_logc0(right.m_logc0)
45 {
46  /*
47  * Use the assignment operator to do the brunt
48  * of the work for the copy constructor.
49  */
50  *this = right;
51 }
52 
54 {
55  if (&right != this) {
57  m_p0 = right.m_p0;
58  m_tlast = right.m_tlast;
59  m_logc0 = right.m_logc0;
60  m_h0_RT = right.m_h0_RT;
61  m_cp0_R = right.m_cp0_R;
62  m_g0_RT = right.m_g0_RT;
63  m_s0_R = right.m_s0_R;
64  m_expg0_RT = right.m_expg0_RT;
65  m_pp = right.m_pp;
66  }
67  return *this;
68 }
69 
71 {
72  return new IdealGasPhase(*this);
73 }
74 
75 // Molar Thermodynamic Properties of the Solution ------------------
76 
78 {
79  return GasConstant * temperature() * (mean_X(&enthalpy_RT_ref()[0]) - 1.0);
80 }
81 
82 doublereal IdealGasPhase::entropy_mole() const
83 {
84  return GasConstant * (mean_X(&entropy_R_ref()[0]) - sum_xlogx() - std::log(pressure() / m_spthermo->refPressure()));
85 }
86 
87 doublereal IdealGasPhase::gibbs_mole() const
88 {
89  return enthalpy_mole() - temperature() * entropy_mole();
90 }
91 
92 doublereal IdealGasPhase::cp_mole() const
93 {
94  return GasConstant * mean_X(&cp_R_ref()[0]);
95 }
96 
97 doublereal IdealGasPhase::cv_mole() const
98 {
99  return cp_mole() - GasConstant;
100 }
101 
102 doublereal IdealGasPhase::cv_tr(doublereal atomicity) const
103 {
104  // k is the species number
105  int dum = 0;
106  int type = m_spthermo->reportType();
107  doublereal c[12];
108  doublereal minTemp_;
109  doublereal maxTemp_;
110  doublereal refPressure_;
111 
112  if (type != 111) {
113  throw CanteraError("Error in IdealGasPhase.cpp", "cv_tr only supported for StatMech!. \n\n");
114  }
115 
116  m_spthermo->reportParams(dum, type, c, minTemp_, maxTemp_, refPressure_);
117 
118  // see reportParameters for specific details
119  return c[3];
120 }
121 
122 doublereal IdealGasPhase::cv_trans() const
123 {
124  return 1.5 * GasConstant;
125 }
126 
127 doublereal IdealGasPhase::cv_rot(double atom) const
128 {
129  return std::max(cv_tr(atom) - cv_trans(), 0.);
130 }
131 
132 doublereal IdealGasPhase::cv_vib(const int k, const doublereal T) const
133 {
134 
135  // k is the species number
136  int dum = 0;
137  int type = m_spthermo->reportType();
138  doublereal c[12];
139  doublereal minTemp_;
140  doublereal maxTemp_;
141  doublereal refPressure_;
142 
143  c[0] = temperature();
144 
145  // basic sanity check
146  if (type != 111) {
147  throw CanteraError("Error in IdealGasPhase.cpp", "cv_vib only supported for StatMech!. \n\n");
148  }
149 
150  m_spthermo->reportParams(dum, type, c, minTemp_, maxTemp_, refPressure_);
151 
152  // see reportParameters for specific details
153  return c[4];
154 
155 }
156 
157 doublereal IdealGasPhase::standardConcentration(size_t k) const
158 {
159  double p = pressure();
160  return p / (GasConstant * temperature());
161 }
162 
163 doublereal IdealGasPhase::logStandardConc(size_t k) const
164 {
165  _updateThermo();
166  double p = pressure();
167  return std::log(p / (GasConstant * temperature()));
168 }
169 
170 void IdealGasPhase::getActivityCoefficients(doublereal* ac) const
171 {
172  for (size_t k = 0; k < m_kk; k++) {
173  ac[k] = 1.0;
174  }
175 }
176 
177 void IdealGasPhase::getStandardChemPotentials(doublereal* muStar) const
178 {
179  const vector_fp& gibbsrt = gibbs_RT_ref();
180  scale(gibbsrt.begin(), gibbsrt.end(), muStar, _RT());
181  double tmp = log(pressure() / m_spthermo->refPressure());
182  tmp *= GasConstant * temperature();
183  for (size_t k = 0; k < m_kk; k++) {
184  muStar[k] += tmp; // add RT*ln(P/P_0)
185  }
186 }
187 
188 // Partial Molar Properties of the Solution --------------
189 
190 void IdealGasPhase::getChemPotentials(doublereal* mu) const
191 {
193  //doublereal logp = log(pressure()/m_spthermo->refPressure());
194  doublereal xx;
195  doublereal rt = temperature() * GasConstant;
196  //const vector_fp& g_RT = gibbs_RT_ref();
197  for (size_t k = 0; k < m_kk; k++) {
198  xx = std::max(SmallNumber, moleFraction(k));
199  mu[k] += rt * (log(xx));
200  }
201 }
202 
203 void IdealGasPhase::getPartialMolarEnthalpies(doublereal* hbar) const
204 {
205  const vector_fp& _h = enthalpy_RT_ref();
206  doublereal rt = GasConstant * temperature();
207  scale(_h.begin(), _h.end(), hbar, rt);
208 }
209 
210 void IdealGasPhase::getPartialMolarEntropies(doublereal* sbar) const
211 {
212  const vector_fp& _s = entropy_R_ref();
213  doublereal r = GasConstant;
214  scale(_s.begin(), _s.end(), sbar, r);
215  doublereal logp = log(pressure() / m_spthermo->refPressure());
216  for (size_t k = 0; k < m_kk; k++) {
217  doublereal xx = std::max(SmallNumber, moleFraction(k));
218  sbar[k] += r * (-logp - log(xx));
219  }
220 }
221 
222 void IdealGasPhase::getPartialMolarIntEnergies(doublereal* ubar) const
223 {
224  const vector_fp& _h = enthalpy_RT_ref();
225  doublereal rt = GasConstant * temperature();
226  for (size_t k = 0; k < m_kk; k++) {
227  ubar[k] = rt * (_h[k] - 1.0);
228  }
229 }
230 
231 void IdealGasPhase::getPartialMolarCp(doublereal* cpbar) const
232 {
233  const vector_fp& _cp = cp_R_ref();
234  scale(_cp.begin(), _cp.end(), cpbar, GasConstant);
235 }
236 
237 void IdealGasPhase::getPartialMolarVolumes(doublereal* vbar) const
238 {
239  double vol = 1.0 / molarDensity();
240  for (size_t k = 0; k < m_kk; k++) {
241  vbar[k] = vol;
242  }
243 }
244 
245 // Properties of the Standard State of the Species in the Solution --
246 
247 void IdealGasPhase::getEnthalpy_RT(doublereal* hrt) const
248 {
249  const vector_fp& _h = enthalpy_RT_ref();
250  copy(_h.begin(), _h.end(), hrt);
251 }
252 
253 void IdealGasPhase::getEntropy_R(doublereal* sr) const
254 {
255  const vector_fp& _s = entropy_R_ref();
256  copy(_s.begin(), _s.end(), sr);
257  double tmp = log(pressure() / m_spthermo->refPressure());
258  for (size_t k = 0; k < m_kk; k++) {
259  sr[k] -= tmp;
260  }
261 }
262 
263 void IdealGasPhase::getGibbs_RT(doublereal* grt) const
264 {
265  const vector_fp& gibbsrt = gibbs_RT_ref();
266  copy(gibbsrt.begin(), gibbsrt.end(), grt);
267  double tmp = log(pressure() / m_spthermo->refPressure());
268  for (size_t k = 0; k < m_kk; k++) {
269  grt[k] += tmp;
270  }
271 }
272 
273 void IdealGasPhase::getPureGibbs(doublereal* gpure) const
274 {
275  const vector_fp& gibbsrt = gibbs_RT_ref();
276  scale(gibbsrt.begin(), gibbsrt.end(), gpure, _RT());
277  double tmp = log(pressure() / m_spthermo->refPressure());
278  tmp *= _RT();
279  for (size_t k = 0; k < m_kk; k++) {
280  gpure[k] += tmp;
281  }
282 }
283 
284 void IdealGasPhase::getIntEnergy_RT(doublereal* urt) const
285 {
286  const vector_fp& _h = enthalpy_RT_ref();
287  for (size_t k = 0; k < m_kk; k++) {
288  urt[k] = _h[k] - 1.0;
289  }
290 }
291 
292 void IdealGasPhase::getCp_R(doublereal* cpr) const
293 {
294  const vector_fp& _cpr = cp_R_ref();
295  copy(_cpr.begin(), _cpr.end(), cpr);
296 }
297 
298 void IdealGasPhase::getStandardVolumes(doublereal* vol) const
299 {
300  double tmp = 1.0 / molarDensity();
301  for (size_t k = 0; k < m_kk; k++) {
302  vol[k] = tmp;
303  }
304 }
305 
306 // Thermodynamic Values for the Species Reference States ---------
307 
308 void IdealGasPhase::getEnthalpy_RT_ref(doublereal* hrt) const
309 {
310  const vector_fp& _h = enthalpy_RT_ref();
311  copy(_h.begin(), _h.end(), hrt);
312 }
313 
314 void IdealGasPhase::getGibbs_RT_ref(doublereal* grt) const
315 {
316  const vector_fp& gibbsrt = gibbs_RT_ref();
317  copy(gibbsrt.begin(), gibbsrt.end(), grt);
318 }
319 
320 void IdealGasPhase::getGibbs_ref(doublereal* g) const
321 {
322  const vector_fp& gibbsrt = gibbs_RT_ref();
323  scale(gibbsrt.begin(), gibbsrt.end(), g, _RT());
324 }
325 
326 void IdealGasPhase::getEntropy_R_ref(doublereal* er) const
327 {
328  const vector_fp& _s = entropy_R_ref();
329  copy(_s.begin(), _s.end(), er);
330 }
331 
332 void IdealGasPhase::getIntEnergy_RT_ref(doublereal* urt) const
333 {
334  const vector_fp& _h = enthalpy_RT_ref();
335  for (size_t k = 0; k < m_kk; k++) {
336  urt[k] = _h[k] - 1.0;
337  }
338 }
339 
340 void IdealGasPhase::getCp_R_ref(doublereal* cprt) const
341 {
342  const vector_fp& _cpr = cp_R_ref();
343  copy(_cpr.begin(), _cpr.end(), cprt);
344 }
345 
346 void IdealGasPhase::getStandardVolumes_ref(doublereal* vol) const
347 {
348  doublereal tmp = _RT() / m_p0;
349  for (size_t k = 0; k < m_kk; k++) {
350  vol[k] = tmp;
351  }
352 }
353 
355 {
356  m_p0 = refPressure();
357  m_h0_RT.resize(m_kk);
358  m_g0_RT.resize(m_kk);
359  m_expg0_RT.resize(m_kk);
360  m_cp0_R.resize(m_kk);
361  m_s0_R.resize(m_kk);
362  m_pp.resize(m_kk);
363 }
364 
365 void IdealGasPhase::setToEquilState(const doublereal* mu_RT)
366 {
367  double tmp, tmp2;
368  const vector_fp& grt = gibbs_RT_ref();
369 
370  /*
371  * Within the method, we protect against inf results if the
372  * exponent is too high.
373  *
374  * If it is too low, we set
375  * the partial pressure to zero. This capability is needed
376  * by the elemental potential method.
377  */
378  doublereal pres = 0.0;
379  for (size_t k = 0; k < m_kk; k++) {
380  tmp = -grt[k] + mu_RT[k];
381  if (tmp < -600.) {
382  m_pp[k] = 0.0;
383  } else if (tmp > 300.0) {
384  tmp2 = tmp / 300.;
385  tmp2 *= tmp2;
386  m_pp[k] = m_p0 * exp(300.) * tmp2;
387  } else {
388  m_pp[k] = m_p0 * exp(tmp);
389  }
390  pres += m_pp[k];
391  }
392  // set state
393  setState_PX(pres, &m_pp[0]);
394 }
395 
397 {
398  doublereal tnow = temperature();
399 
400  // If the temperature has changed since the last time these
401  // properties were computed, recompute them.
402  if (m_tlast != tnow) {
403  m_spthermo->update(tnow, &m_cp0_R[0], &m_h0_RT[0], &m_s0_R[0]);
404  m_tlast = tnow;
405 
406  // update the species Gibbs functions
407  for (size_t k = 0; k < m_kk; k++) {
408  m_g0_RT[k] = m_h0_RT[k] - m_s0_R[k];
409  }
410  m_logc0 = log(m_p0 / (GasConstant * tnow));
411  m_tlast = tnow;
412  }
413 }
414 }
virtual void getEntropy_R(doublereal *sr) const
Get the array of nondimensional Entropy functions for the species standard states at the current T an...
const vector_fp & entropy_R_ref() const
Returns a reference to the dimensionless reference state Entropy vector.
virtual doublereal standardConcentration(size_t k=0) const
Returns the standard concentration , which is used to normalize the generalized concentration.
virtual void setToEquilState(const doublereal *lambda_RT)
Method used by the ChemEquil equilibrium solver.
void _updateThermo() const
Update the species reference state thermodynamic functions.
virtual void getCp_R_ref(doublereal *cprt) const
Returns the vector of nondimensional constant pressure heat capacities of the reference state at the ...
ThermoPhase * duplMyselfAsThermoPhase() const
Duplicator from the ThermoPhase parent class.
virtual void getGibbs_RT_ref(doublereal *grt) const
Returns the vector of nondimensional Gibbs Free Energies of the reference state at the current temper...
vector_fp m_pp
Temporary array containing internally calculated partial pressures.
virtual void getPartialMolarCp(doublereal *cpbar) const
Get the partial molar heat capacities Units: J/kmol/K.
virtual void getIntEnergy_RT_ref(doublereal *urt) const
Returns the vector of nondimensional internal Energies of the reference state at the current temperat...
doublereal _RT() const
Return the Gas Constant multiplied by the current temperature.
Definition: ThermoPhase.h:977
virtual doublereal gibbs_mole() const
Molar Gibbs free Energy for an ideal gas.
virtual void getCp_R(doublereal *cpr) const
Get the nondimensional Heat Capacities at constant pressure for the species standard states at the cu...
Class IdealGasPhase represents low-density gases that obey the ideal gas equation of state...
ThermoPhase & operator=(const ThermoPhase &right)
Assignment operator.
Definition: ThermoPhase.cpp:57
virtual void getIntEnergy_RT(doublereal *urt) const
Returns the vector of nondimensional Internal Energies of the standard state species at the current T...
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 logStandardConc(size_t k=0) const
Returns the natural logarithm of the standard concentration of the kth species.
virtual doublereal pressure() const
Pressure.
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:100
virtual void getPartialMolarIntEnergies(doublereal *ubar) const
Get the species partial molar enthalpies. Units: J/kmol.
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
doublereal m_p0
Reference state pressure.
Virtual base class for the calculation of multiple-species thermodynamic reference-state property man...
virtual void getPartialMolarEnthalpies(doublereal *hbar) const
Get the species partial molar enthalpies. Units: J/kmol.
virtual doublereal cv_mole() const
Molar heat capacity at constant volume.
doublereal molarDensity() const
Molar density (kmol/m^3).
Definition: Phase.cpp:597
virtual void getChemPotentials(doublereal *mu) const
Get the species chemical potentials. Units: J/kmol.
doublereal m_tlast
last value of the temperature processed by reference state
virtual void getEnthalpy_RT_ref(doublereal *hrt) const
Returns the vector of nondimensional enthalpies of the reference state at the current temperature of ...
const vector_fp & gibbs_RT_ref() const
Returns a reference to the dimensionless reference state Gibbs free energy vector.
virtual int reportType(size_t index=npos) const =0
This utility function reports the type of parameterization used for the species with index number ind...
vector_fp m_g0_RT
Temporary storage for dimensionless reference state gibbs energies.
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:101
virtual doublereal cv_trans() const
doublereal mean_X(const doublereal *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition: Phase.cpp:623
virtual void getActivityCoefficients(doublereal *ac) const
Get the array of non-dimensional activity coefficients at the current solution temperature, pressure, and solution concentration.
ThermoPhase object for the ideal gas equation of state - workhorse for Cantera (see Thermodynamic Pro...
doublereal sum_xlogx() const
Evaluate .
Definition: Phase.cpp:633
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 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 getStandardChemPotentials(doublereal *mu) const
Get the array of chemical potentials at unit activity for the species standard states at the current ...
virtual doublereal refPressure(size_t k=npos) const =0
The reference-state pressure for species k.
doublereal m_logc0
Temporary storage for log of p/RT.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:68
virtual doublereal intEnergy_mole() const
Molar internal energy.
virtual doublereal cp_mole() const
Molar heat capacity at constant pressure.
virtual doublereal enthalpy_mole() const
Return the Molar enthalpy. Units: J/kmol.
virtual void getPartialMolarVolumes(doublereal *vbar) const
Get the species partial molar volumes. Units: m^3/kmol.
virtual doublereal refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:159
virtual void initThermo()
Initialize the ThermoPhase object after all species have been set up.
IdealGasPhase & operator=(const IdealGasPhase &right)
Assignment operator.
doublereal moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition: Phase.cpp:524
doublereal temperature() const
Temperature (K).
Definition: Phase.h:528
const vector_fp & cp_R_ref() const
Returns a reference to the dimensionless reference state Heat Capacity vector.
virtual void initThermoXML(XML_Node &phaseNode, const std::string &id)
Import and initialize a ThermoPhase object using an XML tree.
const doublereal SmallNumber
smallest number to compare to zero.
Definition: ct_defs.h:139
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:165
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:154
const doublereal GasConstant
Universal Gas Constant. [J/kmol/K].
Definition: ct_defs.h:66
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 setState_PX(doublereal p, doublereal *x)
Set the pressure (Pa) and mole fractions.
virtual doublereal cv_rot(double atomicity) const
const vector_fp & enthalpy_RT_ref() const
Returns a reference to the dimensionless reference state enthalpy vector.
virtual void getGibbs_RT(doublereal *grt) const
Get the nondimensional Gibbs functions for the species standard states at the current T and P of the ...
size_t m_kk
Number of species in the phase.
Definition: Phase.h:716
virtual void getEnthalpy_RT(doublereal *hrt) const
Get the nondimensional Enthalpy functions for the species standard states at their standard states at...
virtual void reportParams(size_t index, int &type, doublereal *const c, doublereal &minTemp, doublereal &maxTemp, doublereal &refPressure) const =0
This utility function reports back the type of parameterization and all of the parameters for the spe...
virtual void update(doublereal T, doublereal *cp_R, doublereal *h_RT, doublereal *s_R) const =0
Compute the reference-state properties for all species.
virtual void initThermoFile(const std::string &inputFile, const std::string &id)
virtual void getPartialMolarEntropies(doublereal *sbar) const
Get the species partial molar entropies. Units: J/kmol/K.
virtual doublereal cv_tr(doublereal) const
virtual void getStandardVolumes_ref(doublereal *vol) const
Get the molar volumes of the species standard states at the current T and P_ref of the solution...
vector_fp m_s0_R
Temporary storage for dimensionless reference state entropies.
vector_fp m_h0_RT
Temporary storage for dimensionless reference state enthalpies.
SpeciesThermo * m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition: ThermoPhase.h:1625
virtual doublereal cv_vib(int k, doublereal T) const
vector_fp m_cp0_R
Temporary storage for dimensionless reference state heat capacities.
virtual doublereal entropy_mole() const
Molar entropy.