Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
18 namespace Cantera
19 {
20 
22  m_sub(0),
23  m_subflag(0),
24  m_mw(-1.0),
25  m_verbose(false)
26 {
27 }
28 
30  m_sub(0),
31  m_subflag(0),
32  m_mw(-1.0),
33  m_verbose(false)
34 {
35  *this = right;
36 }
37 
39 {
40  if (&right != this) {
42  delete m_sub;
43  m_subflag = right.m_subflag;
44  m_sub = tpx::GetSub(m_subflag);
45  m_mw = right.m_mw;
46  m_verbose = right.m_verbose;
47  }
48  return *this;
49 }
50 
52 {
53  return new PureFluidPhase(*this);
54 }
55 
57 {
58  delete m_sub;
59 }
60 
62 {
63  delete m_sub;
64  m_sub = tpx::GetSub(m_subflag);
65  if (m_sub == 0) {
66  throw CanteraError("PureFluidPhase::initThermo",
67  "could not create new substance object.");
68  }
69  m_mw = m_sub->MolWt();
71  double one = 1.0;
72  setMoleFractions(&one);
73  double cp0_R, h0_RT, s0_R, p;
74  double T0 = 298.15;
75  if (T0 < m_sub->Tcrit()) {
76  m_sub->Set(tpx::PropertyPair::TX, T0, 1.0);
77  p = 0.01*m_sub->P();
78  } else {
79  p = 0.001*m_sub->Pcrit();
80  }
81  p = 0.001 * p;
82  m_sub->Set(tpx::PropertyPair::TP, T0, p);
83 
84  m_spthermo->update_one(0, T0, &cp0_R, &h0_RT, &s0_R);
85  double s_R = s0_R - log(p/refPressure());
86  m_sub->setStdState(h0_RT*GasConstant*298.15/m_mw,
87  s_R*GasConstant/m_mw, T0, p);
88  writelog("PureFluidPhase::initThermo: initialized phase "
89  +id()+"\n", m_verbose);
90 }
91 
93 {
94  eosdata._require("model","PureFluid");
95  m_subflag = atoi(eosdata["fluid_type"].c_str());
96  if (m_subflag < 0)
97  throw CanteraError("PureFluidPhase::setParametersFromXML",
98  "missing or negative substance flag");
99 }
100 
102 {
103  setTPXState();
104  return m_sub->h() * m_mw;
105 }
106 
108 {
109  setTPXState();
110  return m_sub->u() * m_mw;
111 }
112 
114 {
115  setTPXState();
116  return m_sub->s() * m_mw;
117 }
118 
119 doublereal PureFluidPhase::gibbs_mole() const
120 {
121  setTPXState();
122  return m_sub->g() * m_mw;
123 }
124 
125 doublereal PureFluidPhase::cp_mole() const
126 {
127  setTPXState();
128  return m_sub->cp() * m_mw;
129 }
130 
131 doublereal PureFluidPhase::cv_mole() const
132 {
133  setTPXState();
134  return m_sub->cv() * m_mw;
135 }
136 
137 doublereal PureFluidPhase::pressure() const
138 {
139  setTPXState();
140  return m_sub->P();
141 }
142 
143 void PureFluidPhase::setPressure(doublereal p)
144 {
145  Set(tpx::PropertyPair::TP, temperature(), p);
146  setDensity(1.0/m_sub->v());
147 }
148 
149 void PureFluidPhase::Set(tpx::PropertyPair::type n, double x, double y) const
150 {
151  m_sub->Set(n, x, y);
152 }
153 
155 {
156  Set(tpx::PropertyPair::TV, temperature(), 1.0/density());
157 }
158 
160 {
161  return m_sub->isothermalCompressibility();
162 }
163 
165 {
166  return m_sub->thermalExpansionCoeff();
167 }
168 
170 {
171  return *m_sub;
172 }
173 
174 void PureFluidPhase::getPartialMolarEnthalpies(doublereal* hbar) const
175 {
176  hbar[0] = enthalpy_mole();
177 }
178 
179 void PureFluidPhase::getPartialMolarEntropies(doublereal* sbar) const
180 {
181  sbar[0] = entropy_mole();
182 }
183 
184 void PureFluidPhase::getPartialMolarIntEnergies(doublereal* ubar) const
185 {
186  ubar[0] = intEnergy_mole();
187 }
188 
189 void PureFluidPhase::getPartialMolarCp(doublereal* cpbar) const
190 {
191  cpbar[0] = cp_mole();
192 }
193 
194 void PureFluidPhase::getPartialMolarVolumes(doublereal* vbar) const
195 {
196  vbar[0] = 1.0 / molarDensity();
197 }
198 
200 {
201  c[0] = 1.0;
202 }
203 
204 doublereal PureFluidPhase::standardConcentration(size_t k) const
205 {
206  return 1.0;
207 }
208 
209 void PureFluidPhase::getActivities(doublereal* a) const
210 {
211  a[0] = 1.0;
212 }
213 
215 {
216  mu[0] = gibbs_mole();
217 }
218 
219 void PureFluidPhase::getEnthalpy_RT(doublereal* hrt) const
220 {
221  hrt[0] = enthalpy_mole() / _RT();
222 }
223 
224 void PureFluidPhase::getEntropy_R(doublereal* sr) const
225 {
226  sr[0] = entropy_mole() / GasConstant;
227 }
228 
229 void PureFluidPhase::getGibbs_RT(doublereal* grt) const
230 {
231  grt[0] = gibbs_mole() / _RT();
232 }
233 
234 void PureFluidPhase::getEnthalpy_RT_ref(doublereal* hrt) const
235 {
236  double psave = pressure();
237  double t = temperature();
238  double plow = 1.0E-8;
239  Set(tpx::PropertyPair::TP, t, plow);
240  getEnthalpy_RT(hrt);
241  Set(tpx::PropertyPair::TP, t, psave);
242 
243 }
244 
245 void PureFluidPhase::getGibbs_RT_ref(doublereal* grt) const
246 {
247  double psave = pressure();
248  double t = temperature();
249  double pref = m_spthermo->refPressure();
250  double plow = 1.0E-8;
251  Set(tpx::PropertyPair::TP, t, plow);
252  getGibbs_RT(grt);
253  grt[0] += log(pref/plow);
254  Set(tpx::PropertyPair::TP, t, psave);
255 }
256 
257 void PureFluidPhase::getGibbs_ref(doublereal* g) const
258 {
259  getGibbs_RT_ref(g);
260  g[0] *= (GasConstant * temperature());
261 }
262 
263 void PureFluidPhase::getEntropy_R_ref(doublereal* er) const
264 {
265  double psave = pressure();
266  double t = temperature();
267  double pref = m_spthermo->refPressure();
268  double plow = 1.0E-8;
269  Set(tpx::PropertyPair::TP, t, plow);
270  getEntropy_R(er);
271  er[0] -= log(pref/plow);
272  Set(tpx::PropertyPair::TP, t, psave);
273 }
274 
276 {
277  return m_sub->Tcrit();
278 }
279 
281 {
282  return m_sub->Pcrit();
283 }
284 
285 doublereal PureFluidPhase::critDensity() const
286 {
287  return 1.0/m_sub->Vcrit();
288 }
289 
290 doublereal PureFluidPhase::satTemperature(doublereal p) const
291 {
292  return m_sub->Tsat(p);
293 }
294 
295 void PureFluidPhase::setState_HP(doublereal h, doublereal p,
296  doublereal tol)
297 {
298  Set(tpx::PropertyPair::HP, h, p);
299  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
300 }
301 
302 void PureFluidPhase::setState_UV(doublereal u, doublereal v,
303  doublereal tol)
304 {
305  Set(tpx::PropertyPair::UV, u, v);
306  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
307 }
308 
309 void PureFluidPhase::setState_SV(doublereal s, doublereal v,
310  doublereal tol)
311 {
312  Set(tpx::PropertyPair::SV, s, v);
313  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
314 }
315 
316 void PureFluidPhase::setState_SP(doublereal s, doublereal p,
317  doublereal tol)
318 {
319  Set(tpx::PropertyPair::SP, s, p);
320  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
321 }
322 
323 doublereal PureFluidPhase::satPressure(doublereal t)
324 {
325  Set(tpx::PropertyPair::TV, t, m_sub->v());
326  return m_sub->Ps();
327 }
328 
330 {
331  setTPXState();
332  return m_sub->x();
333 }
334 
335 void PureFluidPhase::setState_Tsat(doublereal t, doublereal x)
336 {
337  setTemperature(t);
338  setTPXState();
339  Set(tpx::PropertyPair::TX, t, x);
340  setDensity(1.0/m_sub->v());
341 }
342 
343 void PureFluidPhase::setState_Psat(doublereal p, doublereal x)
344 {
345  setTPXState();
346  Set(tpx::PropertyPair::PX, p, x);
348  setDensity(1.0/m_sub->v());
349 }
350 
351 std::string PureFluidPhase::report(bool show_thermo, doublereal threshold) const
352 {
353  char p[800];
354  string s = "";
355  if (name() != "") {
356  sprintf(p, " \n %s:\n", name().c_str());
357  s += p;
358  }
359  sprintf(p, " \n temperature %12.6g K\n", temperature());
360  s += p;
361  sprintf(p, " pressure %12.6g Pa\n", pressure());
362  s += p;
363  sprintf(p, " density %12.6g kg/m^3\n", density());
364  s += p;
365  sprintf(p, " mean mol. weight %12.6g amu\n", meanMolecularWeight());
366  s += p;
367  sprintf(p, " vapor fraction %12.6g \n", vaporFraction());
368  s += p;
369 
370  doublereal phi = electricPotential();
371  if (phi != 0.0) {
372  sprintf(p, " potential %12.6g V\n", phi);
373  s += p;
374  }
375  if (show_thermo) {
376  sprintf(p, " \n");
377  s += p;
378  sprintf(p, " 1 kg 1 kmol\n");
379  s += p;
380  sprintf(p, " ----------- ------------\n");
381  s += p;
382  sprintf(p, " enthalpy %12.6g %12.4g J\n",
384  s += p;
385  sprintf(p, " internal energy %12.6g %12.4g J\n",
387  s += p;
388  sprintf(p, " entropy %12.6g %12.4g J/K\n",
390  s += p;
391  sprintf(p, " Gibbs function %12.6g %12.4g J\n",
392  gibbs_mass(), gibbs_mole());
393  s += p;
394  sprintf(p, " heat capacity c_p %12.6g %12.4g J/K\n",
395  cp_mass(), cp_mole());
396  s += p;
397  try {
398  sprintf(p, " heat capacity c_v %12.6g %12.4g J/K\n",
399  cv_mass(), cv_mole());
400  s += p;
401  } catch (CanteraError& e) {
402  e.save();
403  sprintf(p, " heat capacity c_v <not implemented> \n");
404  s += p;
405  }
406  }
407  return s;
408 }
409 
410 }
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:603
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:608
virtual ~PureFluidPhase()
Destructor.
doublereal electricPotential() const
Returns the electric potential of this phase (V).
Definition: ThermoPhase.h:352
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:59
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:77
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:936
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:60
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...
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:120
doublereal molarDensity() const
Molar density (kmol/m^3).
Definition: Phase.cpp:663
double v()
Specific volume [m^3/kg].
Definition: Sub.h:105
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:157
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:135
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:899
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:97
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:913
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:906
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:100
ThermoPhase * duplMyselfAsThermoPhase() const
Duplication function.
doublereal cp_mass() const
Specific heat at constant pressure.
Definition: ThermoPhase.h:920
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:99
doublereal cv_mass() const
Specific heat at constant volume.
Definition: ThermoPhase.h:927
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:331
virtual std::string report(bool show_thermo=true, doublereal threshold=1e-14) const
returns a summary of the state of the phase as a string
double g()
Gibbs function [J/kg].
Definition: Sub.h:130
virtual doublereal critDensity() const
critical density
virtual doublereal refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:150
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:838
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:602
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:148
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:638
doublereal enthalpy_mass() const
Specific enthalpy.
Definition: ThermoPhase.h:892
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:115
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:669
const doublereal GasConstant
Universal Gas Constant. [J/kmol/K].
Definition: ct_defs.h:64
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:110
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:33
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:127
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:33
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:464
SpeciesThermo * m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition: ThermoPhase.h:1607
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:623
void save()
Function to put this error onto Cantera's error stack.