Cantera  2.4.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 {
34  if (m_tpx_name != "") {
35  m_sub.reset(tpx::newSubstance(m_tpx_name));
36  } else {
37  m_sub.reset(tpx::GetSub(m_subflag));
38  }
39  if (!m_sub) {
40  throw CanteraError("PureFluidPhase::initThermo",
41  "could not create new substance object.");
42  }
43  m_mw = m_sub->MolWt();
45  double one = 1.0;
46  setMoleFractions(&one);
47  double cp0_R, h0_RT, s0_R, p;
48  double T0 = 298.15;
49  if (T0 < m_sub->Tcrit()) {
50  m_sub->Set(tpx::PropertyPair::TX, T0, 1.0);
51  p = 0.01*m_sub->P();
52  } else {
53  p = 0.001*m_sub->Pcrit();
54  }
55  p = 0.001 * p;
56  m_sub->Set(tpx::PropertyPair::TP, T0, p);
57 
58  m_spthermo.update_single(0, T0, &cp0_R, &h0_RT, &s0_R);
59  double s_R = s0_R - log(p/refPressure());
60  m_sub->setStdState(h0_RT*GasConstant*298.15/m_mw,
61  s_R*GasConstant/m_mw, T0, p);
62  debuglog("PureFluidPhase::initThermo: initialized phase "
63  +id()+"\n", m_verbose);
64 }
65 
67 {
68  eosdata._require("model","PureFluid");
69  m_subflag = atoi(eosdata["fluid_type"].c_str());
70  if (m_subflag < 0) {
71  throw CanteraError("PureFluidPhase::setParametersFromXML",
72  "missing or negative substance flag");
73  }
74 }
75 
76 double PureFluidPhase::minTemp(size_t k) const
77 {
78  return m_sub->Tmin();
79 }
80 
81 double PureFluidPhase::maxTemp(size_t k) const
82 {
83  return m_sub->Tmax();
84 }
85 
87 {
88  return m_sub->h() * m_mw;
89 }
90 
92 {
93  return m_sub->u() * m_mw;
94 }
95 
96 doublereal PureFluidPhase::entropy_mole() const
97 {
98  return m_sub->s() * m_mw;
99 }
100 
101 doublereal PureFluidPhase::gibbs_mole() const
102 {
103  return m_sub->g() * m_mw;
104 }
105 
106 doublereal PureFluidPhase::cp_mole() const
107 {
108  return m_sub->cp() * m_mw;
109 }
110 
111 doublereal PureFluidPhase::cv_mole() const
112 {
113  return m_sub->cv() * m_mw;
114 }
115 
116 doublereal PureFluidPhase::pressure() const
117 {
118  return m_sub->P();
119 }
120 
121 void PureFluidPhase::setPressure(doublereal p)
122 {
123  Set(tpx::PropertyPair::TP, temperature(), p);
124  ThermoPhase::setDensity(1.0/m_sub->v());
125 }
126 
128 {
130  Set(tpx::PropertyPair::TV, T, m_sub->v());
131 }
132 
134 {
136  Set(tpx::PropertyPair::TV, m_sub->Temp(), 1.0/rho);
137 }
138 
139 void PureFluidPhase::Set(tpx::PropertyPair::type n, double x, double y) const
140 {
141  m_sub->Set(n, x, y);
142 }
143 
145 {
146  return m_sub->isothermalCompressibility();
147 }
148 
150 {
151  return m_sub->thermalExpansionCoeff();
152 }
153 
155 {
156  return *m_sub;
157 }
158 
159 void PureFluidPhase::getPartialMolarEnthalpies(doublereal* hbar) const
160 {
161  hbar[0] = enthalpy_mole();
162 }
163 
164 void PureFluidPhase::getPartialMolarEntropies(doublereal* sbar) const
165 {
166  sbar[0] = entropy_mole();
167 }
168 
169 void PureFluidPhase::getPartialMolarIntEnergies(doublereal* ubar) const
170 {
171  ubar[0] = intEnergy_mole();
172 }
173 
174 void PureFluidPhase::getPartialMolarCp(doublereal* cpbar) const
175 {
176  cpbar[0] = cp_mole();
177 }
178 
179 void PureFluidPhase::getPartialMolarVolumes(doublereal* vbar) const
180 {
181  vbar[0] = 1.0 / molarDensity();
182 }
183 
185 {
186  c[0] = 1.0;
187 }
188 
189 doublereal PureFluidPhase::standardConcentration(size_t k) const
190 {
191  return 1.0;
192 }
193 
194 void PureFluidPhase::getActivities(doublereal* a) const
195 {
196  a[0] = 1.0;
197 }
198 
200 {
201  mu[0] = gibbs_mole();
202 }
203 
204 void PureFluidPhase::getEnthalpy_RT(doublereal* hrt) const
205 {
206  hrt[0] = enthalpy_mole() / RT();
207 }
208 
209 void PureFluidPhase::getEntropy_R(doublereal* sr) const
210 {
211  sr[0] = entropy_mole() / GasConstant;
212 }
213 
214 void PureFluidPhase::getGibbs_RT(doublereal* grt) const
215 {
216  grt[0] = gibbs_mole() / RT();
217 }
218 
219 void PureFluidPhase::getEnthalpy_RT_ref(doublereal* hrt) const
220 {
221  double psave = pressure();
222  double t = temperature();
223  double plow = 1.0E-8;
224  Set(tpx::PropertyPair::TP, t, plow);
225  getEnthalpy_RT(hrt);
226  Set(tpx::PropertyPair::TP, t, psave);
227 
228 }
229 
230 void PureFluidPhase::getGibbs_RT_ref(doublereal* grt) const
231 {
232  double psave = pressure();
233  double t = temperature();
234  double pref = refPressure();
235  double plow = 1.0E-8;
236  Set(tpx::PropertyPair::TP, t, plow);
237  getGibbs_RT(grt);
238  grt[0] += log(pref/plow);
239  Set(tpx::PropertyPair::TP, t, psave);
240 }
241 
242 void PureFluidPhase::getGibbs_ref(doublereal* g) const
243 {
244  getGibbs_RT_ref(g);
245  g[0] *= RT();
246 }
247 
248 void PureFluidPhase::getEntropy_R_ref(doublereal* er) const
249 {
250  double psave = pressure();
251  double t = temperature();
252  double pref = refPressure();
253  double plow = 1.0E-8;
254  Set(tpx::PropertyPair::TP, t, plow);
255  getEntropy_R(er);
256  er[0] -= log(pref/plow);
257  Set(tpx::PropertyPair::TP, t, psave);
258 }
259 
261 {
262  return m_sub->Tcrit();
263 }
264 
266 {
267  return m_sub->Pcrit();
268 }
269 
270 doublereal PureFluidPhase::critDensity() const
271 {
272  return 1.0/m_sub->Vcrit();
273 }
274 
275 doublereal PureFluidPhase::satTemperature(doublereal p) const
276 {
277  return m_sub->Tsat(p);
278 }
279 
280 /* The next several functions set the state. They run the Substance::Set
281  * function, followed by setting the state of the ThermoPhase object
282  * to the newly computed temperature and density of the Substance.
283  */
284 
285 void PureFluidPhase::setState_HP(double h, double p, double tol)
286 {
287  Set(tpx::PropertyPair::HP, h, p);
288  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
289 }
290 
291 void PureFluidPhase::setState_UV(double u, double v, double tol)
292 {
293  Set(tpx::PropertyPair::UV, u, v);
294  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
295 }
296 
297 void PureFluidPhase::setState_SV(double s, double v, double tol)
298 {
299  Set(tpx::PropertyPair::SV, s, v);
300  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
301 }
302 
303 void PureFluidPhase::setState_SP(double s, double p, double tol)
304 {
305  Set(tpx::PropertyPair::SP, s, p);
306  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
307 }
308 
309 void PureFluidPhase::setState_ST(double s, double t, double tol)
310 {
311  Set(tpx::PropertyPair::ST, s, t);
312  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
313 }
314 
315 void PureFluidPhase::setState_TV(double t, double v, double tol)
316 {
317  Set(tpx::PropertyPair::TV, t, v);
318  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
319 }
320 
321 void PureFluidPhase::setState_PV(double p, double v, double tol)
322 {
323  Set(tpx::PropertyPair::PV, p, v);
324  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
325 }
326 
327 void PureFluidPhase::setState_UP(double u, double p, double tol)
328 {
329  Set(tpx::PropertyPair::UP, u, p);
330  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
331 }
332 
333 void PureFluidPhase::setState_VH(double v, double h, double tol)
334 {
335  Set(tpx::PropertyPair::VH, v, h);
336  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
337 }
338 
339 void PureFluidPhase::setState_TH(double t, double h, double tol)
340 {
341  Set(tpx::PropertyPair::TH, t, h);
342  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
343 }
344 
345 void PureFluidPhase::setState_SH(double s, double h, double tol)
346 {
347  Set(tpx::PropertyPair::SH, s, h);
348  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
349 }
350 
351 doublereal PureFluidPhase::satPressure(doublereal t)
352 {
353  Set(tpx::PropertyPair::TV, t, m_sub->v());
354  return m_sub->Ps();
355 }
356 
358 {
359  return m_sub->x();
360 }
361 
362 void PureFluidPhase::setState_Tsat(doublereal t, doublereal x)
363 {
364  Set(tpx::PropertyPair::TX, t, x);
366  ThermoPhase::setDensity(1.0/m_sub->v());
367 }
368 
369 void PureFluidPhase::setState_Psat(doublereal p, doublereal x)
370 {
371  Set(tpx::PropertyPair::PX, p, x);
373  ThermoPhase::setDensity(1.0/m_sub->v());
374 }
375 
376 std::string PureFluidPhase::report(bool show_thermo, doublereal threshold) const
377 {
378  fmt::memory_buffer b;
379  if (name() != "") {
380  format_to(b, "\n {}:\n", name());
381  }
382  format_to(b, "\n");
383  format_to(b, " temperature {:12.6g} K\n", temperature());
384  format_to(b, " pressure {:12.6g} Pa\n", pressure());
385  format_to(b, " density {:12.6g} kg/m^3\n", density());
386  format_to(b, " mean mol. weight {:12.6g} amu\n", meanMolecularWeight());
387  format_to(b, " vapor fraction {:12.6g}\n", vaporFraction());
388 
389  doublereal phi = electricPotential();
390  if (phi != 0.0) {
391  format_to(b, " potential {:12.6g} V\n", phi);
392  }
393  if (show_thermo) {
394  format_to(b, "\n");
395  format_to(b, " 1 kg 1 kmol\n");
396  format_to(b, " ----------- ------------\n");
397  format_to(b, " enthalpy {:12.6g} {:12.4g} J\n",
399  format_to(b, " internal energy {:12.6g} {:12.4g} J\n",
401  format_to(b, " entropy {:12.6g} {:12.4g} J/K\n",
403  format_to(b, " Gibbs function {:12.6g} {:12.4g} J\n",
404  gibbs_mass(), gibbs_mole());
405  format_to(b, " heat capacity c_p {:12.6g} {:12.4g} J/K\n",
406  cp_mass(), cp_mole());
407  try {
408  format_to(b, " heat capacity c_v {:12.6g} {:12.4g} J/K\n",
409  cv_mass(), cv_mole());
410  } catch (NotImplementedError&) {
411  format_to(b, " heat capacity c_v <not implemented>\n");
412  }
413  }
414  return to_string(b);
415 }
416 
417 }
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).
MultiSpeciesThermo m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition: ThermoPhase.h:1607
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.
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:187
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:734
virtual void setTemperature(const double T)
Set the internally stored temperature of the phase (K).
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.
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 double maxTemp(size_t k=npos) const
Maximum temperature for which the thermodynamic data for the species are valid.
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 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:714
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:748
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:590
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:302
doublereal entropy_mass() const
Specific entropy. Units: J/kg/K.
Definition: ThermoPhase.h:724
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 double minTemp(size_t k=npos) const
Minimum temperature for which the thermodynamic data for the species or phase are valid...
virtual void setMoleFractions(const doublereal *const x)
Set the mole fractions to the specified values.
Definition: Phase.cpp:251
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:135
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:739
void setMolecularWeight(const int k, const double mw)
Set the molecular weight of a single species to a given value.
Definition: Phase.h:774
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.
virtual void setDensity(const double rho)
Set the internally stored density (kg/m^3) of the phase.
std::string m_tpx_name
Name for this substance used by the TPX package.
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:729
virtual doublereal refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:116
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:78
Contains declarations for string manipulation functions within Cantera.
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: AnyMap.cpp:8
virtual void update_single(size_t k, double T, double *cp_R, double *h_RT, double *s_R) const
Like update_one, but without applying offsets to the output pointers.
doublereal intEnergy_mass() const
Specific internal energy. Units: J/kg.
Definition: ThermoPhase.h:719
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:390
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