Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
11 
12 using namespace std;
13 
14 namespace Cantera
15 {
16 
17 IdealGasPhase::IdealGasPhase() :
18  m_p0(-1.0),
19  m_logc0(0.0)
20 {
21 }
22 
23 IdealGasPhase::IdealGasPhase(const std::string& inputFile, const std::string& id_) :
24  m_p0(-1.0),
25  m_logc0(0.0)
26 {
27  initThermoFile(inputFile, id_);
28 }
29 
30 IdealGasPhase::IdealGasPhase(XML_Node& phaseRef, const std::string& id_) :
31  m_p0(-1.0),
32  m_logc0(0.0)
33 {
34  importPhase(phaseRef, this);
35 }
36 
38  m_p0(right.m_p0),
39  m_logc0(right.m_logc0)
40 {
41  /*
42  * Use the assignment operator to do the brunt
43  * of the work for the copy constructor.
44  */
45  *this = right;
46 }
47 
49 {
50  if (&right != this) {
52  m_p0 = right.m_p0;
53  m_logc0 = right.m_logc0;
54  m_h0_RT = right.m_h0_RT;
55  m_cp0_R = right.m_cp0_R;
56  m_g0_RT = right.m_g0_RT;
57  m_s0_R = right.m_s0_R;
58  m_expg0_RT = right.m_expg0_RT;
59  m_pp = right.m_pp;
60  }
61  return *this;
62 }
63 
65 {
66  return new IdealGasPhase(*this);
67 }
68 
69 // Molar Thermodynamic Properties of the Solution ------------------
70 
71 doublereal IdealGasPhase::entropy_mole() const
72 {
73  return GasConstant * (mean_X(entropy_R_ref()) - sum_xlogx() - std::log(pressure() / m_spthermo->refPressure()));
74 }
75 
76 doublereal IdealGasPhase::cp_mole() const
77 {
78  return GasConstant * mean_X(cp_R_ref());
79 }
80 
81 doublereal IdealGasPhase::cv_mole() const
82 {
83  return cp_mole() - GasConstant;
84 }
85 
86 doublereal IdealGasPhase::cv_tr(doublereal atomicity) const
87 {
88  warn_deprecated("IdealGasPhase::cv_tr", "To be removed after Cantera 2.2.");
89  // k is the species number
90  int dum = 0;
91  int type = m_spthermo->reportType();
92  doublereal c[12];
93  doublereal minTemp_;
94  doublereal maxTemp_;
95  doublereal refPressure_;
96 
97  if (type != 111) {
98  throw CanteraError("Error in IdealGasPhase.cpp", "cv_tr only supported for StatMech!. \n\n");
99  }
100 
101  m_spthermo->reportParams(dum, type, c, minTemp_, maxTemp_, refPressure_);
102 
103  // see reportParameters for specific details
104  return c[3];
105 }
106 
107 doublereal IdealGasPhase::cv_trans() const
108 {
109  warn_deprecated("IdealGasPhase::cv_trans", "To be removed after Cantera 2.2.");
110  return 1.5 * GasConstant;
111 }
112 
113 doublereal IdealGasPhase::cv_rot(double atom) const
114 {
115  warn_deprecated("IdealGasPhase::cv_rot", "To be removed after Cantera 2.2.");
116  return std::max(cv_tr(atom) - cv_trans(), 0.);
117 }
118 
119 doublereal IdealGasPhase::cv_vib(const int k, const doublereal T) const
120 {
121  warn_deprecated("IdealGasPhase::cv_vib", "To be removed after Cantera 2.2.");
122  // k is the species number
123  int dum = 0;
124  int type = m_spthermo->reportType();
125  doublereal c[12];
126  doublereal minTemp_;
127  doublereal maxTemp_;
128  doublereal refPressure_;
129 
130  c[0] = temperature();
131 
132  // basic sanity check
133  if (type != 111) {
134  throw CanteraError("Error in IdealGasPhase.cpp", "cv_vib only supported for StatMech!. \n\n");
135  }
136 
137  m_spthermo->reportParams(dum, type, c, minTemp_, maxTemp_, refPressure_);
138 
139  // see reportParameters for specific details
140  return c[4];
141 
142 }
143 
144 doublereal IdealGasPhase::standardConcentration(size_t k) const
145 {
146  return pressure() / (GasConstant * temperature());
147 }
148 
149 void IdealGasPhase::getActivityCoefficients(doublereal* ac) const
150 {
151  for (size_t k = 0; k < m_kk; k++) {
152  ac[k] = 1.0;
153  }
154 }
155 
156 void IdealGasPhase::getStandardChemPotentials(doublereal* muStar) const
157 {
158  const vector_fp& gibbsrt = gibbs_RT_ref();
159  scale(gibbsrt.begin(), gibbsrt.end(), muStar, _RT());
160  double tmp = log(pressure() / m_spthermo->refPressure());
161  tmp *= GasConstant * temperature();
162  for (size_t k = 0; k < m_kk; k++) {
163  muStar[k] += tmp; // add RT*ln(P/P_0)
164  }
165 }
166 
167 // Partial Molar Properties of the Solution --------------
168 
169 void IdealGasPhase::getChemPotentials(doublereal* mu) const
170 {
172  doublereal rt = temperature() * GasConstant;
173  for (size_t k = 0; k < m_kk; k++) {
174  double xx = std::max(SmallNumber, moleFraction(k));
175  mu[k] += rt * (log(xx));
176  }
177 }
178 
179 void IdealGasPhase::getPartialMolarEnthalpies(doublereal* hbar) const
180 {
181  const vector_fp& _h = enthalpy_RT_ref();
182  doublereal rt = GasConstant * temperature();
183  scale(_h.begin(), _h.end(), hbar, rt);
184 }
185 
186 void IdealGasPhase::getPartialMolarEntropies(doublereal* sbar) const
187 {
188  const vector_fp& _s = entropy_R_ref();
189  scale(_s.begin(), _s.end(), sbar, GasConstant);
190  doublereal logp = log(pressure() / m_spthermo->refPressure());
191  for (size_t k = 0; k < m_kk; k++) {
192  doublereal xx = std::max(SmallNumber, moleFraction(k));
193  sbar[k] += GasConstant * (-logp - log(xx));
194  }
195 }
196 
197 void IdealGasPhase::getPartialMolarIntEnergies(doublereal* ubar) const
198 {
199  const vector_fp& _h = enthalpy_RT_ref();
200  doublereal rt = GasConstant * temperature();
201  for (size_t k = 0; k < m_kk; k++) {
202  ubar[k] = rt * (_h[k] - 1.0);
203  }
204 }
205 
206 void IdealGasPhase::getPartialMolarCp(doublereal* cpbar) const
207 {
208  const vector_fp& _cp = cp_R_ref();
209  scale(_cp.begin(), _cp.end(), cpbar, GasConstant);
210 }
211 
212 void IdealGasPhase::getPartialMolarVolumes(doublereal* vbar) const
213 {
214  double vol = 1.0 / molarDensity();
215  for (size_t k = 0; k < m_kk; k++) {
216  vbar[k] = vol;
217  }
218 }
219 
220 // Properties of the Standard State of the Species in the Solution --
221 
222 void IdealGasPhase::getEnthalpy_RT(doublereal* hrt) const
223 {
224  const vector_fp& _h = enthalpy_RT_ref();
225  copy(_h.begin(), _h.end(), hrt);
226 }
227 
228 void IdealGasPhase::getEntropy_R(doublereal* sr) const
229 {
230  const vector_fp& _s = entropy_R_ref();
231  copy(_s.begin(), _s.end(), sr);
232  double tmp = log(pressure() / m_spthermo->refPressure());
233  for (size_t k = 0; k < m_kk; k++) {
234  sr[k] -= tmp;
235  }
236 }
237 
238 void IdealGasPhase::getGibbs_RT(doublereal* grt) const
239 {
240  const vector_fp& gibbsrt = gibbs_RT_ref();
241  copy(gibbsrt.begin(), gibbsrt.end(), grt);
242  double tmp = log(pressure() / m_spthermo->refPressure());
243  for (size_t k = 0; k < m_kk; k++) {
244  grt[k] += tmp;
245  }
246 }
247 
248 void IdealGasPhase::getPureGibbs(doublereal* gpure) const
249 {
250  const vector_fp& gibbsrt = gibbs_RT_ref();
251  scale(gibbsrt.begin(), gibbsrt.end(), gpure, _RT());
252  double tmp = log(pressure() / m_spthermo->refPressure());
253  tmp *= _RT();
254  for (size_t k = 0; k < m_kk; k++) {
255  gpure[k] += tmp;
256  }
257 }
258 
259 void IdealGasPhase::getIntEnergy_RT(doublereal* urt) const
260 {
261  const vector_fp& _h = enthalpy_RT_ref();
262  for (size_t k = 0; k < m_kk; k++) {
263  urt[k] = _h[k] - 1.0;
264  }
265 }
266 
267 void IdealGasPhase::getCp_R(doublereal* cpr) const
268 {
269  const vector_fp& _cpr = cp_R_ref();
270  copy(_cpr.begin(), _cpr.end(), cpr);
271 }
272 
273 void IdealGasPhase::getStandardVolumes(doublereal* vol) const
274 {
275  double tmp = 1.0 / molarDensity();
276  for (size_t k = 0; k < m_kk; k++) {
277  vol[k] = tmp;
278  }
279 }
280 
281 // Thermodynamic Values for the Species Reference States ---------
282 
283 void IdealGasPhase::getEnthalpy_RT_ref(doublereal* hrt) const
284 {
285  const vector_fp& _h = enthalpy_RT_ref();
286  copy(_h.begin(), _h.end(), hrt);
287 }
288 
289 void IdealGasPhase::getGibbs_RT_ref(doublereal* grt) const
290 {
291  const vector_fp& gibbsrt = gibbs_RT_ref();
292  copy(gibbsrt.begin(), gibbsrt.end(), grt);
293 }
294 
295 void IdealGasPhase::getGibbs_ref(doublereal* g) const
296 {
297  const vector_fp& gibbsrt = gibbs_RT_ref();
298  scale(gibbsrt.begin(), gibbsrt.end(), g, _RT());
299 }
300 
301 void IdealGasPhase::getEntropy_R_ref(doublereal* er) const
302 {
303  const vector_fp& _s = entropy_R_ref();
304  copy(_s.begin(), _s.end(), er);
305 }
306 
307 void IdealGasPhase::getIntEnergy_RT_ref(doublereal* urt) const
308 {
309  const vector_fp& _h = enthalpy_RT_ref();
310  for (size_t k = 0; k < m_kk; k++) {
311  urt[k] = _h[k] - 1.0;
312  }
313 }
314 
315 void IdealGasPhase::getCp_R_ref(doublereal* cprt) const
316 {
317  const vector_fp& _cpr = cp_R_ref();
318  copy(_cpr.begin(), _cpr.end(), cprt);
319 }
320 
321 void IdealGasPhase::getStandardVolumes_ref(doublereal* vol) const
322 {
323  doublereal tmp = _RT() / m_p0;
324  for (size_t k = 0; k < m_kk; k++) {
325  vol[k] = tmp;
326  }
327 }
328 
330 {
332  m_p0 = refPressure();
333  m_h0_RT.resize(m_kk);
334  m_g0_RT.resize(m_kk);
335  m_expg0_RT.resize(m_kk);
336  m_cp0_R.resize(m_kk);
337  m_s0_R.resize(m_kk);
338  m_pp.resize(m_kk);
339 }
340 
341 void IdealGasPhase::setToEquilState(const doublereal* mu_RT)
342 {
343  const vector_fp& grt = gibbs_RT_ref();
344 
345  /*
346  * Within the method, we protect against inf results if the
347  * exponent is too high.
348  *
349  * If it is too low, we set
350  * the partial pressure to zero. This capability is needed
351  * by the elemental potential method.
352  */
353  doublereal pres = 0.0;
354  for (size_t k = 0; k < m_kk; k++) {
355  double tmp = -grt[k] + mu_RT[k];
356  if (tmp < -600.) {
357  m_pp[k] = 0.0;
358  } else if (tmp > 300.0) {
359  double tmp2 = tmp / 300.;
360  tmp2 *= tmp2;
361  m_pp[k] = m_p0 * exp(300.) * tmp2;
362  } else {
363  m_pp[k] = m_p0 * exp(tmp);
364  }
365  pres += m_pp[k];
366  }
367  // set state
368  setState_PX(pres, &m_pp[0]);
369 }
370 
372 {
373  static const int cacheId = m_cache.getId();
374  CachedScalar cached = m_cache.getScalar(cacheId);
375  doublereal tnow = temperature();
376 
377  // If the temperature has changed since the last time these
378  // properties were computed, recompute them.
379  if (cached.state1 != tnow) {
380  m_spthermo->update(tnow, &m_cp0_R[0], &m_h0_RT[0], &m_s0_R[0]);
381  cached.state1 = tnow;
382 
383  // update the species Gibbs functions
384  for (size_t k = 0; k < m_kk; k++) {
385  m_g0_RT[k] = m_h0_RT[k] - m_s0_R[k];
386  }
387  m_logc0 = log(m_p0 / (GasConstant * tnow));
388  }
389 }
390 }
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.
int getId()
Get a unique id for a cached value.
Definition: ValueCache.cpp:18
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:936
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...
virtual void initThermo()
Initialize the ThermoPhase object after all species have been set up.
ThermoPhase & operator=(const ThermoPhase &right)
Assignment operator.
Definition: ThermoPhase.cpp:60
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...
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
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.
doublereal m_p0
Reference state pressure.
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:78
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:663
virtual void getChemPotentials(doublereal *mu) const
Get the species chemical potentials. Units: J/kmol.
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:97
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:687
virtual void getActivityCoefficients(doublereal *ac) const
Get the array of non-dimensional activity coefficients at the current solution temperature, pressure, and solution concentration.
bool importPhase(XML_Node &phase, ThermoPhase *th, SpeciesThermoFactory *spfactory)
Import a phase information into an empty ThermoPhase object.
ThermoPhase object for the ideal gas equation of state - workhorse for Cantera (see Thermodynamic Pro...
doublereal sum_xlogx() const
Evaluate .
Definition: Phase.cpp:703
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:162
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 ...
ValueCache m_cache
Cached for saved calculations within each ThermoPhase.
Definition: Phase.h:833
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:99
virtual doublereal cp_mole() const
Molar heat capacity at constant pressure.
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:150
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:561
doublereal temperature() const
Temperature (K).
Definition: Phase.h:602
const vector_fp & cp_R_ref() const
Returns a reference to the dimensionless reference state Heat Capacity vector.
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
Templates for operations on vector-like objects.
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:158
const doublereal GasConstant
Universal Gas Constant. [J/kmol/K].
Definition: ct_defs.h:64
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.
double state1
Value of the first state variable for the state at which value was evaluated, e.g.
Definition: ValueCache.h:104
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:843
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:1607
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.