Cantera  2.1.2
PureFluidPhase.cpp
Go to the documentation of this file.
1 /**
2  * @file PureFluidPhase.cpp
3  * Definitions for a ThermoPhase object for a pure fluid phase consisting
4  * of gas, liquid, mixed-gas-liquid
5  * and supercritical fluid (see \ref thermoprops
6  * and class \link Cantera::PureFluidPhase PureFluidPhase\endlink).
7  */
8 #include "cantera/base/xml.h"
10 
11 #include "cantera/tpx/Sub.h"
12 #include "cantera/tpx/utils.h"
13 
14 #include <cstdio>
15 
16 using std::string;
17 using std::endl;
18 
19 namespace Cantera
20 {
21 
23  ThermoPhase(),
24  m_sub(0),
25  m_subflag(0),
26  m_mw(-1.0),
27  m_verbose(false)
28 {
29 }
30 
32  ThermoPhase(),
33  m_sub(0),
34  m_subflag(0),
35  m_mw(-1.0),
36  m_verbose(false)
37 {
38  *this = right;
39 }
40 
42 {
43  if (&right != this) {
45  delete m_sub;
46  m_subflag = right.m_subflag;
47  m_sub = tpx::GetSub(m_subflag);
48  m_mw = right.m_mw;
49  m_verbose = right.m_verbose;
50  }
51  return *this;
52 }
53 
55 {
56  return new PureFluidPhase(*this);
57 }
58 
60 {
61  delete m_sub;
62 }
63 
66 {
67  delete m_sub;
68  m_sub = tpx::GetSub(m_subflag);
69  if (m_sub == 0) {
70  throw CanteraError("PureFluidPhase::initThermo",
71  "could not create new substance object.");
72  }
73  m_mw = m_sub->MolWt();
75  double one = 1.0;
76  setMoleFractions(&one);
77  double cp0_R, h0_RT, s0_R, T0, p;
78  T0 = 298.15;
79  if (T0 < m_sub->Tcrit()) {
80  m_sub->Set(tpx::PropertyPair::TX, T0, 1.0);
81  p = 0.01*m_sub->P();
82  } else {
83  p = 0.001*m_sub->Pcrit();
84  }
85  p = 0.001 * p;
86  m_sub->Set(tpx::PropertyPair::TP, T0, p);
87 
88  m_spthermo->update_one(0, T0, &cp0_R, &h0_RT, &s0_R);
89  double s_R = s0_R - log(p/refPressure());
90  m_sub->setStdState(h0_RT*GasConstant*298.15/m_mw,
91  s_R*GasConstant/m_mw, T0, p);
92  writelog("PureFluidPhase::initThermo: initialized phase "
93  +id()+"\n", m_verbose);
94 }
95 
98 {
99  eosdata._require("model","PureFluid");
100  m_subflag = atoi(eosdata["fluid_type"].c_str());
101  if (m_subflag < 0)
102  throw CanteraError("PureFluidPhase::setParametersFromXML",
103  "missing or negative substance flag");
104 }
105 
106 doublereal PureFluidPhase::
108 {
109  setTPXState();
110  return m_sub->h() * m_mw;
111 }
112 
113 doublereal PureFluidPhase::
115 {
116  setTPXState();
117  return m_sub->u() * m_mw;
118 }
119 
120 doublereal PureFluidPhase::
122 {
123  setTPXState();
124  return m_sub->s() * m_mw;
125 }
126 
127 doublereal PureFluidPhase::
128 gibbs_mole() const
129 {
130  setTPXState();
131  return m_sub->g() * m_mw;
132 }
133 
134 doublereal PureFluidPhase::
135 cp_mole() const
136 {
137  setTPXState();
138  return m_sub->cp() * m_mw;
139 }
140 
141 doublereal PureFluidPhase::
142 cv_mole() const
143 {
144  setTPXState();
145  return m_sub->cv() * m_mw;
146 }
147 
148 doublereal PureFluidPhase::
149 pressure() const
150 {
151  setTPXState();
152  return m_sub->P();
153 }
154 
155 void PureFluidPhase::setPressure(doublereal p)
156 {
157  Set(tpx::PropertyPair::TP, temperature(), p);
158  setDensity(1.0/m_sub->v());
159 }
160 
161 void PureFluidPhase::Set(tpx::PropertyPair::type n, double x, double y) const
162 {
163  m_sub->Set(n, x, y);
164 }
165 
167 {
168  Set(tpx::PropertyPair::TV, temperature(), 1.0/density());
169 }
170 
172 {
173  return m_sub->isothermalCompressibility();
174 }
175 
177 {
178  return m_sub->thermalExpansionCoeff();
179 }
180 
182 {
183  return *m_sub;
184 }
185 
186 void PureFluidPhase::getPartialMolarEnthalpies(doublereal* hbar) const
187 {
188  hbar[0] = enthalpy_mole();
189 }
190 
191 void PureFluidPhase::getPartialMolarEntropies(doublereal* sbar) const
192 {
193  sbar[0] = entropy_mole();
194 }
195 
196 void PureFluidPhase::getPartialMolarIntEnergies(doublereal* ubar) const
197 {
198  ubar[0] = intEnergy_mole();
199 }
200 
201 void PureFluidPhase::getPartialMolarCp(doublereal* cpbar) const
202 {
203  cpbar[0] = cp_mole();
204 }
205 
206 void PureFluidPhase::getPartialMolarVolumes(doublereal* vbar) const
207 {
208  vbar[0] = 1.0 / molarDensity();
209 }
210 
212 {
214 }
215 
217 {
218  c[0] = 1.0;
219 }
220 
221 doublereal PureFluidPhase::standardConcentration(size_t k) const
222 {
223  return 1.0;
224 }
225 
226 void PureFluidPhase::getActivities(doublereal* a) const
227 {
228  a[0] = 1.0;
229 }
230 
232 {
233  mu[0] = gibbs_mole();
234 }
235 
236 void PureFluidPhase::getEnthalpy_RT(doublereal* hrt) const
237 {
238  doublereal rt = _RT();
239  doublereal h = enthalpy_mole();
240  hrt[0] = h / rt;
241 }
242 
243 void PureFluidPhase::getEntropy_R(doublereal* sr) const
244 {
245  doublereal s = entropy_mole();
246  sr[0] = s / GasConstant;
247 }
248 
249 void PureFluidPhase::getGibbs_RT(doublereal* grt) const
250 {
251  doublereal rt = _RT();
252  doublereal g = gibbs_mole();
253  grt[0] = g / rt;
254 }
255 
256 void PureFluidPhase::getEnthalpy_RT_ref(doublereal* hrt) const
257 {
258  double psave = pressure();
259  double t = temperature();
260  //double pref = m_spthermo->refPressure();
261  double plow = 1.0E-8;
262  Set(tpx::PropertyPair::TP, t, plow);
263  getEnthalpy_RT(hrt);
264  Set(tpx::PropertyPair::TP, t, psave);
265 
266 }
267 
268 void PureFluidPhase::getGibbs_RT_ref(doublereal* grt) const
269 {
270  double psave = pressure();
271  double t = temperature();
272  double pref = m_spthermo->refPressure();
273  double plow = 1.0E-8;
274  Set(tpx::PropertyPair::TP, t, plow);
275  getGibbs_RT(grt);
276  grt[0] += log(pref/plow);
277  Set(tpx::PropertyPair::TP, t, psave);
278 }
279 
280 void PureFluidPhase::getGibbs_ref(doublereal* g) const
281 {
282  getGibbs_RT_ref(g);
283  g[0] *= (GasConstant * temperature());
284 }
285 
286 void PureFluidPhase::getEntropy_R_ref(doublereal* er) const
287 {
288  double psave = pressure();
289  double t = temperature();
290  double pref = m_spthermo->refPressure();
291  double plow = 1.0E-8;
292  Set(tpx::PropertyPair::TP, t, plow);
293  getEntropy_R(er);
294  er[0] -= log(pref/plow);
295  Set(tpx::PropertyPair::TP, t, psave);
296 }
297 
299 {
300  return m_sub->Tcrit();
301 }
302 
304 {
305  return m_sub->Pcrit();
306 }
307 
308 doublereal PureFluidPhase::critDensity() const
309 {
310  return 1.0/m_sub->Vcrit();
311 }
312 
313 doublereal PureFluidPhase::satTemperature(doublereal p) const
314 {
315  return m_sub->Tsat(p);
316 }
317 
318 void PureFluidPhase::setState_HP(doublereal h, doublereal p,
319  doublereal tol)
320 {
321  Set(tpx::PropertyPair::HP, h, p);
322  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
323 }
324 
325 void PureFluidPhase::setState_UV(doublereal u, doublereal v,
326  doublereal tol)
327 {
328  Set(tpx::PropertyPair::UV, u, v);
329  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
330 }
331 
332 void PureFluidPhase::setState_SV(doublereal s, doublereal v,
333  doublereal tol)
334 {
335  Set(tpx::PropertyPair::SV, s, v);
336  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
337 }
338 
339 void PureFluidPhase::setState_SP(doublereal s, doublereal p,
340  doublereal tol)
341 {
342  Set(tpx::PropertyPair::SP, s, p);
343  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
344 }
345 
346 doublereal PureFluidPhase::satPressure(doublereal t)
347 {
348  doublereal vsv = m_sub->v();
349  Set(tpx::PropertyPair::TV,t,vsv);
350  return m_sub->Ps();
351 }
352 
354 {
355  setTPXState();
356  return m_sub->x();
357 }
358 
359 void PureFluidPhase::setState_Tsat(doublereal t, doublereal x)
360 {
361  setTemperature(t);
362  setTPXState();
363  Set(tpx::PropertyPair::TX, t, x);
364  setDensity(1.0/m_sub->v());
365 }
366 
367 void PureFluidPhase::setState_Psat(doublereal p, doublereal x)
368 {
369  setTPXState();
370  Set(tpx::PropertyPair::PX, p, x);
372  setDensity(1.0/m_sub->v());
373 }
374 
375 std::string PureFluidPhase::report(bool show_thermo) const
376 {
377  char p[800];
378  string s = "";
379  if (name() != "") {
380  sprintf(p, " \n %s:\n", name().c_str());
381  s += p;
382  }
383  sprintf(p, " \n temperature %12.6g K\n", temperature());
384  s += p;
385  sprintf(p, " pressure %12.6g Pa\n", pressure());
386  s += p;
387  sprintf(p, " density %12.6g kg/m^3\n", density());
388  s += p;
389  sprintf(p, " mean mol. weight %12.6g amu\n", meanMolecularWeight());
390  s += p;
391  sprintf(p, " vapor fraction %12.6g \n", vaporFraction());
392  s += p;
393 
394  doublereal phi = electricPotential();
395  if (phi != 0.0) {
396  sprintf(p, " potential %12.6g V\n", phi);
397  s += p;
398  }
399  if (show_thermo) {
400  sprintf(p, " \n");
401  s += p;
402  sprintf(p, " 1 kg 1 kmol\n");
403  s += p;
404  sprintf(p, " ----------- ------------\n");
405  s += p;
406  sprintf(p, " enthalpy %12.6g %12.4g J\n",
408  s += p;
409  sprintf(p, " internal energy %12.6g %12.4g J\n",
411  s += p;
412  sprintf(p, " entropy %12.6g %12.4g J/K\n",
414  s += p;
415  sprintf(p, " Gibbs function %12.6g %12.4g J\n",
416  gibbs_mass(), gibbs_mole());
417  s += p;
418  sprintf(p, " heat capacity c_p %12.6g %12.4g J/K\n",
419  cp_mass(), cp_mole());
420  s += p;
421  try {
422  sprintf(p, " heat capacity c_v %12.6g %12.4g J/K\n",
423  cv_mass(), cv_mole());
424  s += p;
425  } catch (CanteraError& e) {
426  e.save();
427  sprintf(p, " heat capacity c_v <not implemented> \n");
428  s += p;
429  }
430  }
431  return s;
432 }
433 
434 }
virtual double MolWt()=0
Molecular weight [kg/kmol].
void _require(const std::string &a, const std::string &v) const
Require that the current xml node have an attribute named by the first argument, a, and that this attribute have the the string value listed in the second argument, v.
Definition: xml.cpp:614
virtual double Vcrit()=0
Critical specific volume [m^3/kg].
PureFluidPhase()
Empty Base Constructor.
virtual doublereal density() const
Density (kg/m^3).
Definition: Phase.h:534
virtual ~PureFluidPhase()
Destructor.
doublereal electricPotential() const
Returns the electric potential of this phase (V).
Definition: ThermoPhase.h:391
virtual void setPressure(doublereal p)
sets the thermodynamic pressure (Pa).
virtual doublereal gibbs_mole() const
Molar Gibbs function. Units: J/kmol.
double x()
Vapor mass fraction.
Definition: Sub.cpp:54
bool m_verbose
flag to turn on some printing.
This phase object consists of a single component that can be a gas, a liquid, a mixed gas-liquid flui...
virtual void getEnthalpy_RT_ref(doublereal *hrt) const
Returns the vector of nondimensional enthalpies of the reference state at the current temperature of ...
virtual void getActivities(doublereal *a) const
Get the array of non-dimensional activities at the current solution temperature, pressure, and solution concentration.
double Tsat(double p)
Saturation temperature at pressure p.
Definition: Sub.cpp:72
virtual doublereal intEnergy_mole() const
Molar internal energy. Units: J/kmol.
doublereal _RT() const
Return the Gas Constant multiplied by the current temperature.
Definition: ThermoPhase.h:977
virtual void setState_UV(doublereal u, doublereal v, doublereal tol=1.e-8)
Set the specific internal energy (J/kg) and specific volume (m^3/kg).
tpx::Substance & TPX_Substance()
Returns a reference to the substance object.
ThermoPhase & operator=(const ThermoPhase &right)
Assignment operator.
Definition: ThermoPhase.cpp:57
virtual void update_one(size_t k, doublereal T, doublereal *cp_R, doublereal *h_RT, doublereal *s_R) const
Like update(), but only updates the single species k.
virtual void getEntropy_R(doublereal *sr) const
Get the array of nondimensional Entropy functions for the standard state species at the current T and...
const int cSS_CONVENTION_TEMPERATURE
Standard state uses the molar convention.
Definition: ThermoPhase.h:34
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:100
int m_subflag
Int indicating the type of the fluid.
virtual void getPartialMolarVolumes(doublereal *vbar) const
Return an array of partial molar volumes for the species in the mixture.
doublereal m_mw
Molecular weight of the substance (kg kmol-1)
double s()
Entropy [J/kg/K].
Definition: Sub.h:115
doublereal molarDensity() const
Molar density (kmol/m^3).
Definition: Phase.cpp:597
double v()
Specific volume [m^3/kg].
Definition: Sub.h:100
virtual std::string report(bool show_thermo=true) const
returns a summary of the state of the phase as a string
virtual void getPartialMolarCp(doublereal *cpbar) const
Return an array of partial molar heat capacities for the species in the mixture.
virtual void setState_Tsat(doublereal t, doublereal x)
Set the state to a saturated system at a particular temperature.
std::string name() const
Return the name of the phase.
Definition: Phase.cpp:129
virtual doublereal isothermalCompressibility() const
Returns the isothermal compressibility. Units: 1/Pa.
virtual void getPartialMolarEntropies(doublereal *sbar) const
Returns an array of partial molar entropies of the species in the solution.
virtual double cv()
Specific heat at constant volume [J/kg/K].
Definition: Sub.h:130
virtual void setState_SV(doublereal s, doublereal v, doublereal tol=1.e-8)
Set the specific entropy (J/kg/K) and specific volume (m^3/kg).
virtual doublereal critPressure() const
critical pressure
virtual void getGibbs_RT(doublereal *grt) const
Get the nondimensional Gibbs functions for the species in their standard states at the current T and ...
doublereal intEnergy_mass() const
Specific internal energy.
Definition: ThermoPhase.h:940
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:101
virtual int standardStateConvention() const
This method returns the convention used in specification of the standard state, of which there are cu...
tpx::Substance * m_sub
Pointer to the underlying tpx object Substance that does the work.
virtual void setState_Psat(doublereal p, doublereal x)
Set the state to a saturated system at a particular pressure.
virtual doublereal satTemperature(doublereal p) const
saturation temperature
doublereal gibbs_mass() const
Specific Gibbs function.
Definition: ThermoPhase.h:954
virtual doublereal critTemperature() const
critical temperature
virtual doublereal thermalExpansionCoeff() const
Return the volumetric thermal expansion coefficient. Units: 1/K.
doublereal entropy_mass() const
Specific entropy.
Definition: ThermoPhase.h:947
Header for a ThermoPhase class for a pure fluid phase consisting of gas, liquid, mixed-gas-liquid and...
double Temp()
Temperature [K].
Definition: Sub.h:95
ThermoPhase * duplMyselfAsThermoPhase() const
Duplication function.
doublereal cp_mass() const
Specific heat at constant pressure.
Definition: ThermoPhase.h:961
Classes providing support for XML data files.
void setTPXState() const
Sets the state using a TPX::TV call.
virtual doublereal refPressure(size_t k=npos) const =0
The reference-state pressure for species k.
virtual doublereal standardConcentration(size_t k=0) const
Return the standard concentration for the kth species.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:68
doublereal cv_mass() const
Specific heat at constant volume.
Definition: ThermoPhase.h:968
virtual doublereal pressure() const
Return the thermodynamic pressure (Pa).
virtual void setMoleFractions(const doublereal *const x)
Set the mole fractions to the specified values There is no restriction on the sum of the mole fractio...
Definition: Phase.cpp:306
double g()
Gibbs function [J/kg].
Definition: Sub.h:125
virtual doublereal critDensity() const
critical density
virtual doublereal refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:159
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 doublereal enthalpy_mole() const
Molar enthalpy. Units: J/kmol.
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 setParametersFromXML(const XML_Node &eosdata)
Set equation of state parameter values from XML entries.
void setMolecularWeight(const int k, const double mw)
Set the molecular weight of a single species to a given value.
Definition: Phase.h:711
virtual void setState_HP(doublereal h, doublereal p, doublereal tol=1.e-8)
Set the internally stored specific enthalpy (J/kg) and pressure (Pa) of the phase.
doublereal temperature() const
Temperature (K).
Definition: Phase.h:528
virtual doublereal entropy_mole() const
Molar entropy. Units: J/kmol/K.
virtual double Tcrit()=0
Critical temperature [K].
virtual double cp()
Specific heat at constant pressure [J/kg/K].
Definition: Sub.h:143
PureFluidPhase & operator=(const PureFluidPhase &right)
Assignment operator.
virtual void setTemperature(const doublereal temp)
Set the internally stored temperature of the phase (K).
Definition: Phase.h:562
doublereal enthalpy_mass() const
Specific enthalpy.
Definition: ThermoPhase.h:933
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...
double h()
Enthalpy [J/kg].
Definition: Sub.h:110
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:588
const doublereal GasConstant
Universal Gas Constant. [J/kmol/K].
Definition: ct_defs.h:66
virtual void initThermo()
Initialize the ThermoPhase object after all species have been set up.
virtual doublereal cv_mole() const
Molar heat capacity at constant volume. Units: J/kmol/K.
virtual void getPartialMolarIntEnergies(doublereal *ubar) const
Return an array of partial molar internal energies for the species in the mixture.
virtual void getPartialMolarEnthalpies(doublereal *hbar) const
Returns an array of partial molar enthalpies for the species in the mixture.
double u()
Internal energy [J/kg].
Definition: Sub.h:105
virtual void getActivityConcentrations(doublereal *c) const
This method returns an array of generalized concentrations.
void writelog(const std::string &msg)
Write a message to the screen.
Definition: global.cpp:43
void Set(PropertyPair::type XY, double x0, double y0)
Function to set or change the state for a property pair XY where x0 is the value of first property an...
Definition: Sub.cpp:122
virtual doublereal cp_mole() const
Molar heat capacity at constant pressure. Units: J/kmol/K.
virtual double Pcrit()=0
Critical pressure [Pa].
double P()
Pressure [Pa].
Definition: Sub.cpp:28
void Set(tpx::PropertyPair::type n, double x, double y) const
Main call to the tpx level to set the state of the system.
virtual doublereal satPressure(doublereal t)
Return the saturation pressure given the temperature.
void setState_TR(doublereal t, doublereal rho)
Set the internally stored temperature (K) and density (kg/m^3)
Definition: Phase.cpp:454
SpeciesThermo * m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition: ThermoPhase.h:1625
virtual void setState_SP(doublereal s, doublereal p, doublereal tol=1.e-8)
Set the specific entropy (J/kg/K) and pressure (Pa).
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 doublereal vaporFraction() const
Return the fraction of vapor at the current conditions.
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 setDensity(const doublereal density_)
Set the internally stored density (kg/m^3) of the phase Note the density of a phase is an independent...
Definition: Phase.h:549
void save()
Function to put this error onto Cantera's error stack.