Cantera  2.3.0
PureFluidPhase.cpp
Go to the documentation of this file.
1 /**
2  * @file PureFluidPhase.cpp Definitions for a ThermoPhase object for a pure
3  * fluid phase consisting of gas, liquid, mixed-gas-liquid and supercritical
4  * fluid (see \ref thermoprops and class \link Cantera::PureFluidPhase
5  * PureFluidPhase\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 
11 #include "cantera/base/xml.h"
13 
14 #include "cantera/tpx/Sub.h"
15 #include "cantera/tpx/utils.h"
17 
18 #include <cstdio>
19 
20 using std::string;
21 
22 namespace Cantera
23 {
24 
26  m_subflag(0),
27  m_mw(-1.0),
28  m_verbose(false)
29 {
30 }
31 
33  m_subflag(0),
34  m_mw(-1.0),
35  m_verbose(false)
36 {
37  *this = right;
38 }
39 
40 PureFluidPhase& PureFluidPhase::operator=(const PureFluidPhase& right)
41 {
42  if (&right != this) {
44  m_subflag = right.m_subflag;
45  m_sub.reset(tpx::GetSub(m_subflag));
46  m_mw = right.m_mw;
47  m_verbose = right.m_verbose;
48  }
49  return *this;
50 }
51 
53 {
54  return new PureFluidPhase(*this);
55 }
56 
58 {
59  m_sub.reset(tpx::GetSub(m_subflag));
60  if (!m_sub) {
61  throw CanteraError("PureFluidPhase::initThermo",
62  "could not create new substance object.");
63  }
64  m_mw = m_sub->MolWt();
66  double one = 1.0;
67  setMoleFractions(&one);
68  double cp0_R, h0_RT, s0_R, p;
69  double T0 = 298.15;
70  if (T0 < m_sub->Tcrit()) {
71  m_sub->Set(tpx::PropertyPair::TX, T0, 1.0);
72  p = 0.01*m_sub->P();
73  } else {
74  p = 0.001*m_sub->Pcrit();
75  }
76  p = 0.001 * p;
77  m_sub->Set(tpx::PropertyPair::TP, T0, p);
78 
79  m_spthermo->update_one(0, T0, &cp0_R, &h0_RT, &s0_R);
80  double s_R = s0_R - log(p/refPressure());
81  m_sub->setStdState(h0_RT*GasConstant*298.15/m_mw,
82  s_R*GasConstant/m_mw, T0, p);
83  debuglog("PureFluidPhase::initThermo: initialized phase "
84  +id()+"\n", m_verbose);
85 }
86 
88 {
89  eosdata._require("model","PureFluid");
90  m_subflag = atoi(eosdata["fluid_type"].c_str());
91  if (m_subflag < 0) {
92  throw CanteraError("PureFluidPhase::setParametersFromXML",
93  "missing or negative substance flag");
94  }
95 }
96 
98 {
99  setTPXState();
100  return m_sub->h() * m_mw;
101 }
102 
104 {
105  setTPXState();
106  return m_sub->u() * m_mw;
107 }
108 
110 {
111  setTPXState();
112  return m_sub->s() * m_mw;
113 }
114 
115 doublereal PureFluidPhase::gibbs_mole() const
116 {
117  setTPXState();
118  return m_sub->g() * m_mw;
119 }
120 
121 doublereal PureFluidPhase::cp_mole() const
122 {
123  setTPXState();
124  return m_sub->cp() * m_mw;
125 }
126 
127 doublereal PureFluidPhase::cv_mole() const
128 {
129  setTPXState();
130  return m_sub->cv() * m_mw;
131 }
132 
133 doublereal PureFluidPhase::pressure() const
134 {
135  setTPXState();
136  return m_sub->P();
137 }
138 
139 void PureFluidPhase::setPressure(doublereal p)
140 {
141  Set(tpx::PropertyPair::TP, temperature(), p);
142  setDensity(1.0/m_sub->v());
143 }
144 
145 void PureFluidPhase::Set(tpx::PropertyPair::type n, double x, double y) const
146 {
147  m_sub->Set(n, x, y);
148 }
149 
151 {
152  Set(tpx::PropertyPair::TV, temperature(), 1.0/density());
153 }
154 
156 {
157  return m_sub->isothermalCompressibility();
158 }
159 
161 {
162  return m_sub->thermalExpansionCoeff();
163 }
164 
166 {
167  return *m_sub;
168 }
169 
170 void PureFluidPhase::getPartialMolarEnthalpies(doublereal* hbar) const
171 {
172  hbar[0] = enthalpy_mole();
173 }
174 
175 void PureFluidPhase::getPartialMolarEntropies(doublereal* sbar) const
176 {
177  sbar[0] = entropy_mole();
178 }
179 
180 void PureFluidPhase::getPartialMolarIntEnergies(doublereal* ubar) const
181 {
182  ubar[0] = intEnergy_mole();
183 }
184 
185 void PureFluidPhase::getPartialMolarCp(doublereal* cpbar) const
186 {
187  cpbar[0] = cp_mole();
188 }
189 
190 void PureFluidPhase::getPartialMolarVolumes(doublereal* vbar) const
191 {
192  vbar[0] = 1.0 / molarDensity();
193 }
194 
196 {
197  c[0] = 1.0;
198 }
199 
200 doublereal PureFluidPhase::standardConcentration(size_t k) const
201 {
202  return 1.0;
203 }
204 
205 void PureFluidPhase::getActivities(doublereal* a) const
206 {
207  a[0] = 1.0;
208 }
209 
211 {
212  mu[0] = gibbs_mole();
213 }
214 
215 void PureFluidPhase::getEnthalpy_RT(doublereal* hrt) const
216 {
217  hrt[0] = enthalpy_mole() / RT();
218 }
219 
220 void PureFluidPhase::getEntropy_R(doublereal* sr) const
221 {
222  sr[0] = entropy_mole() / GasConstant;
223 }
224 
225 void PureFluidPhase::getGibbs_RT(doublereal* grt) const
226 {
227  grt[0] = gibbs_mole() / RT();
228 }
229 
230 void PureFluidPhase::getEnthalpy_RT_ref(doublereal* hrt) const
231 {
232  double psave = pressure();
233  double t = temperature();
234  double plow = 1.0E-8;
235  Set(tpx::PropertyPair::TP, t, plow);
236  getEnthalpy_RT(hrt);
237  Set(tpx::PropertyPair::TP, t, psave);
238 
239 }
240 
241 void PureFluidPhase::getGibbs_RT_ref(doublereal* grt) const
242 {
243  double psave = pressure();
244  double t = temperature();
245  double pref = m_spthermo->refPressure();
246  double plow = 1.0E-8;
247  Set(tpx::PropertyPair::TP, t, plow);
248  getGibbs_RT(grt);
249  grt[0] += log(pref/plow);
250  Set(tpx::PropertyPair::TP, t, psave);
251 }
252 
253 void PureFluidPhase::getGibbs_ref(doublereal* g) const
254 {
255  getGibbs_RT_ref(g);
256  g[0] *= RT();
257 }
258 
259 void PureFluidPhase::getEntropy_R_ref(doublereal* er) const
260 {
261  double psave = pressure();
262  double t = temperature();
263  double pref = m_spthermo->refPressure();
264  double plow = 1.0E-8;
265  Set(tpx::PropertyPair::TP, t, plow);
266  getEntropy_R(er);
267  er[0] -= log(pref/plow);
268  Set(tpx::PropertyPair::TP, t, psave);
269 }
270 
272 {
273  return m_sub->Tcrit();
274 }
275 
277 {
278  return m_sub->Pcrit();
279 }
280 
281 doublereal PureFluidPhase::critDensity() const
282 {
283  return 1.0/m_sub->Vcrit();
284 }
285 
286 doublereal PureFluidPhase::satTemperature(doublereal p) const
287 {
288  return m_sub->Tsat(p);
289 }
290 
291 /* The next several functions set the state. They run the Substance::Set
292  * function, followed by setting the state of the ThermoPhase object
293  * to the newly computed temperature and density of the Substance.
294  */
295 
296 void PureFluidPhase::setState_HP(double h, double p, double 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(double u, double v, double tol)
303 {
304  Set(tpx::PropertyPair::UV, u, v);
305  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
306 }
307 
308 void PureFluidPhase::setState_SV(double s, double v, double tol)
309 {
310  Set(tpx::PropertyPair::SV, s, v);
311  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
312 }
313 
314 void PureFluidPhase::setState_SP(double s, double p, double tol)
315 {
316  Set(tpx::PropertyPair::SP, s, p);
317  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
318 }
319 
320 void PureFluidPhase::setState_ST(double s, double t, double tol)
321 {
322  Set(tpx::PropertyPair::ST, s, t);
323  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
324 }
325 
326 void PureFluidPhase::setState_TV(double t, double v, double tol)
327 {
328  Set(tpx::PropertyPair::TV, t, v);
329  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
330 }
331 
332 void PureFluidPhase::setState_PV(double p, double v, double tol)
333 {
334  Set(tpx::PropertyPair::PV, p, v);
335  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
336 }
337 
338 void PureFluidPhase::setState_UP(double u, double p, double tol)
339 {
340  Set(tpx::PropertyPair::UP, u, p);
341  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
342 }
343 
344 void PureFluidPhase::setState_VH(double v, double h, double tol)
345 {
346  Set(tpx::PropertyPair::VH, v, h);
347  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
348 }
349 
350 void PureFluidPhase::setState_TH(double t, double h, double tol)
351 {
352  Set(tpx::PropertyPair::TH, t, h);
353  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
354 }
355 
356 void PureFluidPhase::setState_SH(double s, double h, double tol)
357 {
358  Set(tpx::PropertyPair::SH, s, h);
359  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
360 }
361 
362 doublereal PureFluidPhase::satPressure(doublereal t)
363 {
364  Set(tpx::PropertyPair::TV, t, m_sub->v());
365  return m_sub->Ps();
366 }
367 
369 {
370  setTPXState();
371  return m_sub->x();
372 }
373 
374 void PureFluidPhase::setState_Tsat(doublereal t, doublereal x)
375 {
376  setTemperature(t);
377  setTPXState();
378  Set(tpx::PropertyPair::TX, t, x);
379  setDensity(1.0/m_sub->v());
380 }
381 
382 void PureFluidPhase::setState_Psat(doublereal p, doublereal x)
383 {
384  setTPXState();
385  Set(tpx::PropertyPair::PX, p, x);
386  setTemperature(m_sub->Temp());
387  setDensity(1.0/m_sub->v());
388 }
389 
390 std::string PureFluidPhase::report(bool show_thermo, doublereal threshold) const
391 {
392  fmt::MemoryWriter b;
393  if (name() != "") {
394  b.write("\n {}:\n", name());
395  }
396  b.write("\n");
397  b.write(" temperature {:12.6g} K\n", temperature());
398  b.write(" pressure {:12.6g} Pa\n", pressure());
399  b.write(" density {:12.6g} kg/m^3\n", density());
400  b.write(" mean mol. weight {:12.6g} amu\n", meanMolecularWeight());
401  b.write(" vapor fraction {:12.6g}\n", vaporFraction());
402 
403  doublereal phi = electricPotential();
404  if (phi != 0.0) {
405  b.write(" potential {:12.6g} V\n", phi);
406  }
407  if (show_thermo) {
408  b.write("\n");
409  b.write(" 1 kg 1 kmol\n");
410  b.write(" ----------- ------------\n");
411  b.write(" enthalpy {:12.6g} {:12.4g} J\n",
413  b.write(" internal energy {:12.6g} {:12.4g} J\n",
415  b.write(" entropy {:12.6g} {:12.4g} J/K\n",
417  b.write(" Gibbs function {:12.6g} {:12.4g} J\n",
418  gibbs_mass(), gibbs_mole());
419  b.write(" heat capacity c_p {:12.6g} {:12.4g} J/K\n",
420  cp_mass(), cp_mole());
421  try {
422  b.write(" heat capacity c_v {:12.6g} {:12.4g} J/K\n",
423  cv_mass(), cv_mole());
424  } catch (NotImplementedError&) {
425  b.write(" heat capacity c_v <not implemented>\n");
426  }
427  }
428  return b.str();
429 }
430 
431 }
virtual void getPartialMolarCp(doublereal *cpbar) const
Return an array of partial molar heat capacities for the species in the mixture.
PureFluidPhase()
Empty Base Constructor.
virtual void setPressure(doublereal p)
sets the thermodynamic pressure (Pa).
virtual void setState_SP(double s, double p, double tol=1e-9)
Set the specific entropy (J/kg/K) and pressure (Pa).
virtual doublereal critPressure() const
Critical pressure (Pa).
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 setState_ST(double s, double t, double tol=1e-9)
Set the specific entropy (J/kg/K) and temperature (K).
doublereal temperature() const
Temperature (K).
Definition: Phase.h:601
virtual void getPartialMolarVolumes(doublereal *vbar) const
Return an array of partial molar volumes for the species in the mixture.
An error indicating that an unimplemented function has been called.
Definition: ctexceptions.h:193
virtual doublereal satTemperature(doublereal p) const
Return the saturation temperature given the pressure.
doublereal cp_mass() const
Specific heat at constant pressure. Units: J/kg/K.
Definition: ThermoPhase.h:784
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
tpx::Substance & TPX_Substance()
Returns a reference to the substance object.
ThermoPhase & operator=(const ThermoPhase &right)
Definition: ThermoPhase.cpp:59
virtual doublereal critDensity() const
Critical density (kg/m3).
virtual void setState_VH(double v, double h, double tol=1e-9)
Set the specific volume (m^3/kg) and the specific enthalpy (J/kg)
virtual void getPartialMolarEntropies(doublereal *sbar) const
Returns an array of partial molar entropies of the species in the solution.
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:97
int m_subflag
Int indicating the type of the fluid.
virtual doublereal density() const
Density (kg/m^3).
Definition: Phase.h:607
doublereal m_mw
Molecular weight of the substance (kg kmol-1)
virtual doublereal refPressure(size_t k=npos) const
The reference-state pressure for species k.
virtual void getPartialMolarEnthalpies(doublereal *hbar) const
Returns an array of partial molar enthalpies for the species in the mixture.
virtual doublereal pressure() const
Return the thermodynamic pressure (Pa).
doublereal enthalpy_mass() const
Specific enthalpy. Units: J/kg.
Definition: ThermoPhase.h:764
virtual void setState_PV(double p, double v, double tol=1e-9)
Set the pressure (Pa) and specific volume (m^3/kg).
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 void setState_Tsat(doublereal t, doublereal x)
Set the state to a saturated system at a particular temperature.
doublereal RT() const
Return the Gas Constant multiplied by the current temperature.
Definition: ThermoPhase.h:809
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:93
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 doublereal cp_mole() const
Molar heat capacity at constant pressure. Units: J/kmol/K.
virtual doublereal intEnergy_mole() const
Molar internal energy. Units: J/kmol.
virtual void setState_Psat(doublereal p, doublereal x)
Set the state to a saturated system at a particular pressure.
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:576
virtual doublereal enthalpy_mole() const
Molar enthalpy. Units: J/kmol.
doublereal molarDensity() const
Molar density (kmol/m^3).
Definition: Phase.cpp:666
Header for a ThermoPhase class for a pure fluid phase consisting of gas, liquid, mixed-gas-liquid and...
virtual void setState_TH(double t, double h, double tol=1e-9)
Set the temperature (K) and the specific enthalpy (J/kg)
virtual doublereal thermalExpansionCoeff() const
Return the volumetric thermal expansion coefficient. Units: 1/K.
Classes providing support for XML data files.
doublereal electricPotential() const
Returns the electric potential of this phase (V).
Definition: ThermoPhase.h:335
doublereal entropy_mass() const
Specific entropy. Units: J/kg/K.
Definition: ThermoPhase.h:774
std::unique_ptr< tpx::Substance > m_sub
Pointer to the underlying tpx object Substance that does the work.
virtual doublereal isothermalCompressibility() const
Returns the isothermal compressibility. Units: 1/Pa.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:65
virtual void getEnthalpy_RT_ref(doublereal *hrt) const
virtual void setMoleFractions(const doublereal *const x)
Set the mole fractions to the specified values.
Definition: Phase.cpp:327
virtual void getActivityConcentrations(doublereal *c) const
This method returns an array of generalized concentrations.
virtual doublereal vaporFraction() const
Return the fraction of vapor at the current conditions.
virtual doublereal cv_mole() const
Molar heat capacity at constant volume. Units: J/kmol/K.
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 gibbs_mole() const
Molar Gibbs function. Units: J/kmol.
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_HP(double h, double p, double tol=1e-9)
Set the internally stored specific enthalpy (J/kg) and pressure (Pa) of the phase.
void debuglog(const std::string &msg, int loglevel)
Write a message to the log only if loglevel > 0.
Definition: global.h:161
MultiSpeciesThermo * m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition: ThermoPhase.h:1693
virtual void setState_UP(double u, double p, double tol=1e-9)
Set the specific internal energy (J/kg) and pressure (Pa).
virtual void setParametersFromXML(const XML_Node &eosdata)
Set equation of state parameter values from XML entries.
virtual doublereal entropy_mole() const
Molar entropy. Units: J/kmol/K.
doublereal cv_mass() const
Specific heat at constant volume. Units: J/kg/K.
Definition: ThermoPhase.h:789
void setMolecularWeight(const int k, const double mw)
Set the molecular weight of a single species to a given value.
Definition: Phase.h:770
virtual void setState_TV(double t, double v, double tol=1e-9)
Set the temperature (K) and specific volume (m^3/kg).
virtual doublereal critTemperature() const
Critical temperature (K).
virtual doublereal standardConcentration(size_t k=0) const
Return the standard concentration for the kth species.
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:661
virtual void setTemperature(const doublereal temp)
Set the internally stored temperature of the phase (K).
Definition: Phase.h:637
virtual void getStandardChemPotentials(doublereal *mu) const
doublereal gibbs_mass() const
Specific Gibbs function. Units: J/kg.
Definition: ThermoPhase.h:779
virtual doublereal refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:149
virtual void getPartialMolarIntEnergies(doublereal *ubar) const
Return an array of partial molar internal energies for the species in the mixture.
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.
std::string name() const
Return the name of the phase.
Definition: Phase.cpp:151
Contains declarations for string manipulation functions within Cantera.
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 ThermoPhase * duplMyselfAsThermoPhase() const
Duplication routine for objects which inherit from ThermoPhase.
virtual void setState_SH(double s, double h, double tol=1e-9)
Set the specific entropy (J/kg/K) and the specific enthalpy (J/kg)
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 void getActivities(doublereal *a) const
Get the array of non-dimensional activities at the current solution temperature, pressure, and solution concentration.
virtual void setState_SV(double s, double v, double tol=1e-9)
Set the specific entropy (J/kg/K) and specific volume (m^3/kg).
Namespace for the Cantera kernel.
Definition: application.cpp:29
doublereal intEnergy_mass() const
Specific internal energy. Units: J/kg.
Definition: ThermoPhase.h:769
virtual doublereal satPressure(doublereal t)
Return the saturation pressure given the temperature.
virtual void setState_UV(double u, double v, double tol=1e-9)
Set the specific internal energy (J/kg) and specific volume (m^3/kg).
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 setState_TR(doublereal t, doublereal rho)
Set the internally stored temperature (K) and density (kg/m^3)
Definition: Phase.cpp:466
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.
Definition: Phase.h:622
void setTPXState() const
Sets the state using a TPX::TV call.