Cantera  2.5.1
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 https://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(-1),
27  m_mw(-1.0),
28  m_verbose(false)
29 {
30 }
31 
33 {
34  if (m_input.hasKey("pure-fluid-name")) {
35  setSubstance(m_input["pure-fluid-name"].asString());
36  }
37 
38  if (m_tpx_name != "") {
39  m_sub.reset(tpx::newSubstance(m_tpx_name));
40  } else {
41  m_sub.reset(tpx::GetSub(m_subflag));
42  }
43 
44  m_mw = m_sub->MolWt();
46 
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  +name()+"\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 std::vector<std::string> PureFluidPhase::fullStates() const
77 {
78  return {"TD", "UV", "DP", "HP", "SP", "SV",
79  "ST", "TV", "PV", "UP", "VH", "TH", "SH", "TPQ"};
80 }
81 
82 std::vector<std::string> PureFluidPhase::partialStates() const
83 {
84  return {"TP", "TQ", "PQ"};
85 }
86 
87 std::string PureFluidPhase::phaseOfMatter() const
88 {
89  if (temperature() >= critTemperature() || pressure() >= critPressure()) {
90  return "supercritical";
91  } else if (m_sub->TwoPhase() == 1) {
92  return "liquid-gas-mix";
93  } else if (pressure() < m_sub->Ps()) {
94  return "gas";
95  } else {
96  return "liquid";
97  }
98 }
99 
100 double PureFluidPhase::minTemp(size_t k) const
101 {
102  return m_sub->Tmin();
103 }
104 
105 double PureFluidPhase::maxTemp(size_t k) const
106 {
107  return m_sub->Tmax();
108 }
109 
111 {
112  return m_sub->h() * m_mw;
113 }
114 
116 {
117  return m_sub->u() * m_mw;
118 }
119 
121 {
122  return m_sub->s() * m_mw;
123 }
124 
125 doublereal PureFluidPhase::gibbs_mole() const
126 {
127  return m_sub->g() * m_mw;
128 }
129 
130 doublereal PureFluidPhase::cp_mole() const
131 {
132  return m_sub->cp() * m_mw;
133 }
134 
135 doublereal PureFluidPhase::cv_mole() const
136 {
137  return m_sub->cv() * m_mw;
138 }
139 
140 doublereal PureFluidPhase::pressure() const
141 {
142  return m_sub->P();
143 }
144 
145 void PureFluidPhase::setPressure(doublereal p)
146 {
147  Set(tpx::PropertyPair::TP, temperature(), p);
148  ThermoPhase::setDensity(1.0/m_sub->v());
149 }
150 
152 {
154  Set(tpx::PropertyPair::TV, T, m_sub->v());
155 }
156 
158 {
160  Set(tpx::PropertyPair::TV, m_sub->Temp(), 1.0/rho);
161 }
162 
163 void PureFluidPhase::Set(tpx::PropertyPair::type n, double x, double y) const
164 {
165  m_sub->Set(n, x, y);
166 }
167 
169 {
170  return m_sub->isothermalCompressibility();
171 }
172 
174 {
175  return m_sub->thermalExpansionCoeff();
176 }
177 
179 {
180  return *m_sub;
181 }
182 
183 void PureFluidPhase::getPartialMolarEnthalpies(doublereal* hbar) const
184 {
185  hbar[0] = enthalpy_mole();
186 }
187 
188 void PureFluidPhase::getPartialMolarEntropies(doublereal* sbar) const
189 {
190  sbar[0] = entropy_mole();
191 }
192 
193 void PureFluidPhase::getPartialMolarIntEnergies(doublereal* ubar) const
194 {
195  ubar[0] = intEnergy_mole();
196 }
197 
198 void PureFluidPhase::getPartialMolarCp(doublereal* cpbar) const
199 {
200  cpbar[0] = cp_mole();
201 }
202 
203 void PureFluidPhase::getPartialMolarVolumes(doublereal* vbar) const
204 {
205  vbar[0] = 1.0 / molarDensity();
206 }
207 
209 {
210  return Units(1.0);
211 }
212 
214 {
215  c[0] = 1.0;
216 }
217 
218 doublereal PureFluidPhase::standardConcentration(size_t k) const
219 {
220  return 1.0;
221 }
222 
223 void PureFluidPhase::getActivities(doublereal* a) const
224 {
225  a[0] = 1.0;
226 }
227 
229 {
230  mu[0] = gibbs_mole();
231 }
232 
233 void PureFluidPhase::getEnthalpy_RT(doublereal* hrt) const
234 {
235  hrt[0] = enthalpy_mole() / RT();
236 }
237 
238 void PureFluidPhase::getEntropy_R(doublereal* sr) const
239 {
240  sr[0] = entropy_mole() / GasConstant;
241 }
242 
243 void PureFluidPhase::getGibbs_RT(doublereal* grt) const
244 {
245  grt[0] = gibbs_mole() / RT();
246 }
247 
248 void PureFluidPhase::getEnthalpy_RT_ref(doublereal* hrt) const
249 {
250  double psave = pressure();
251  double t = temperature();
252  double plow = 1.0E-8;
253  Set(tpx::PropertyPair::TP, t, plow);
254  getEnthalpy_RT(hrt);
255  Set(tpx::PropertyPair::TP, t, psave);
256 
257 }
258 
259 void PureFluidPhase::getGibbs_RT_ref(doublereal* grt) const
260 {
261  double psave = pressure();
262  double t = temperature();
263  double pref = refPressure();
264  double plow = 1.0E-8;
265  Set(tpx::PropertyPair::TP, t, plow);
266  getGibbs_RT(grt);
267  grt[0] += log(pref/plow);
268  Set(tpx::PropertyPair::TP, t, psave);
269 }
270 
271 void PureFluidPhase::getGibbs_ref(doublereal* g) const
272 {
273  getGibbs_RT_ref(g);
274  g[0] *= RT();
275 }
276 
277 void PureFluidPhase::getEntropy_R_ref(doublereal* er) const
278 {
279  double psave = pressure();
280  double t = temperature();
281  double pref = refPressure();
282  double plow = 1.0E-8;
283  Set(tpx::PropertyPair::TP, t, plow);
284  getEntropy_R(er);
285  er[0] -= log(pref/plow);
286  Set(tpx::PropertyPair::TP, t, psave);
287 }
288 
290 {
291  return m_sub->Tcrit();
292 }
293 
295 {
296  return m_sub->Pcrit();
297 }
298 
299 doublereal PureFluidPhase::critDensity() const
300 {
301  return 1.0/m_sub->Vcrit();
302 }
303 
304 doublereal PureFluidPhase::satTemperature(doublereal p) const
305 {
306  return m_sub->Tsat(p);
307 }
308 
309 /* The next several functions set the state. They run the Substance::Set
310  * function, followed by setting the state of the ThermoPhase object
311  * to the newly computed temperature and density of the Substance.
312  */
313 
314 void PureFluidPhase::setState_HP(double h, double p, double tol)
315 {
316  Set(tpx::PropertyPair::HP, h, p);
317  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
318 }
319 
320 void PureFluidPhase::setState_UV(double u, double v, double tol)
321 {
322  Set(tpx::PropertyPair::UV, u, v);
323  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
324 }
325 
326 void PureFluidPhase::setState_SV(double s, double v, double tol)
327 {
328  Set(tpx::PropertyPair::SV, s, v);
329  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
330 }
331 
332 void PureFluidPhase::setState_SP(double s, double p, double tol)
333 {
334  Set(tpx::PropertyPair::SP, s, p);
335  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
336 }
337 
338 void PureFluidPhase::setState_ST(double s, double t, double tol)
339 {
340  Set(tpx::PropertyPair::ST, s, t);
341  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
342 }
343 
344 void PureFluidPhase::setState_TV(double t, double v, double tol)
345 {
346  Set(tpx::PropertyPair::TV, t, v);
347  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
348 }
349 
350 void PureFluidPhase::setState_PV(double p, double v, double tol)
351 {
352  Set(tpx::PropertyPair::PV, p, v);
353  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
354 }
355 
356 void PureFluidPhase::setState_UP(double u, double p, double tol)
357 {
358  Set(tpx::PropertyPair::UP, u, p);
359  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
360 }
361 
362 void PureFluidPhase::setState_VH(double v, double h, double tol)
363 {
364  Set(tpx::PropertyPair::VH, v, h);
365  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
366 }
367 
368 void PureFluidPhase::setState_TH(double t, double h, double tol)
369 {
370  Set(tpx::PropertyPair::TH, t, h);
371  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
372 }
373 
374 void PureFluidPhase::setState_SH(double s, double h, double tol)
375 {
376  Set(tpx::PropertyPair::SH, s, h);
377  setState_TR(m_sub->Temp(), 1.0/m_sub->v());
378 }
379 
380 doublereal PureFluidPhase::satPressure(doublereal t)
381 {
382  Set(tpx::PropertyPair::TV, t, m_sub->v());
383  return m_sub->Ps();
384 }
385 
387 {
388  return m_sub->x();
389 }
390 
391 void PureFluidPhase::setState_Tsat(doublereal t, doublereal x)
392 {
393  Set(tpx::PropertyPair::TX, t, x);
395  ThermoPhase::setDensity(1.0/m_sub->v());
396 }
397 
398 void PureFluidPhase::setState_Psat(doublereal p, doublereal x)
399 {
400  Set(tpx::PropertyPair::PX, p, x);
402  ThermoPhase::setDensity(1.0/m_sub->v());
403 }
404 
405 std::string PureFluidPhase::report(bool show_thermo, doublereal threshold) const
406 {
407  fmt::memory_buffer b;
408  // This is the width of the first column of names in the report.
409  int name_width = 18;
410 
411  string blank_leader = fmt::format("{:{}}", "", name_width);
412 
413  string one_property = "{:>{}} {:<.5g} {}\n";
414 
415  string two_prop_header = "{} {:^15} {:^15}\n";
416  string kg_kmol_header = fmt::format(
417  two_prop_header, blank_leader, "1 kg", "1 kmol"
418  );
419  string Y_X_header = fmt::format(
420  two_prop_header, blank_leader, "mass frac. Y", "mole frac. X"
421  );
422  string two_prop_sep = fmt::format(
423  "{} {:-^15} {:-^15}\n", blank_leader, "", ""
424  );
425  string two_property = "{:>{}} {:15.5g} {:15.5g} {}\n";
426 
427  string three_prop_header = fmt::format(
428  "{} {:^15} {:^15} {:^15}\n", blank_leader, "mass frac. Y",
429  "mole frac. X", "chem. pot. / RT"
430  );
431  string three_prop_sep = fmt::format(
432  "{} {:-^15} {:-^15} {:-^15}\n", blank_leader, "", "", ""
433  );
434  string three_property = "{:>{}} {:15.5g} {:15.5g} {:15.5g}\n";
435 
436  if (name() != "") {
437  format_to(b, "\n {}:\n", name());
438  }
439  format_to(b, "\n");
440  format_to(b, one_property, "temperature", name_width, temperature(), "K");
441  format_to(b, one_property, "pressure", name_width, pressure(), "Pa");
442  format_to(b, one_property, "density", name_width, density(), "kg/m^3");
443  format_to(b, one_property, "mean mol. weight", name_width, meanMolecularWeight(), "kg/kmol");
444  format_to(b, "{:>{}} {:<.5g}\n", "vapor fraction", name_width, vaporFraction());
445  format_to(b, "{:>{}} {}\n", "phase of matter", name_width, phaseOfMatter());
446 
447  if (show_thermo) {
448  format_to(b, "\n");
449  format_to(b, kg_kmol_header);
450  format_to(b, two_prop_sep);
451  format_to(b, two_property, "enthalpy", name_width, enthalpy_mass(), enthalpy_mole(), "J");
452  format_to(b, two_property, "internal energy", name_width, intEnergy_mass(), intEnergy_mole(), "J");
453  format_to(b, two_property, "entropy", name_width, entropy_mass(), entropy_mole(), "J/K");
454  format_to(b, two_property, "Gibbs function", name_width, gibbs_mass(), gibbs_mole(), "J");
455  format_to(b, two_property, "heat capacity c_p", name_width, cp_mass(), cp_mole(), "J/K");
456  format_to(b, two_property, "heat capacity c_v", name_width, cv_mass(), cv_mole(), "J/K");
457  }
458 
459  return to_string(b);
460 }
461 
462 }
Header for a ThermoPhase class for a pure fluid phase consisting of gas, liquid, mixed-gas-liquid and...
bool hasKey(const std::string &key) const
Returns true if the map contains an item named key.
Definition: AnyMap.cpp:984
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
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.
double molarDensity() const
Molar density (kmol/m^3).
Definition: Phase.cpp:700
std::string name() const
Return the name of the phase.
Definition: Phase.cpp:84
virtual void setDensity(const double density_)
Set the internally stored density (kg/m^3) of the phase.
Definition: Phase.cpp:716
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:748
void setState_TR(doublereal t, doublereal rho)
Set the internally stored temperature (K) and density (kg/m^3)
Definition: Phase.cpp:491
virtual double density() const
Density (kg/m^3).
Definition: Phase.h:685
doublereal temperature() const
Temperature (K).
Definition: Phase.h:667
void setMolecularWeight(const int k, const double mw)
Set the molecular weight of a single species to a given value.
Definition: Phase.cpp:1018
virtual void setTemperature(const doublereal temp)
Set the internally stored temperature of the phase (K).
Definition: Phase.h:724
virtual void setState_PV(double p, double v, double tol=1e-9)
Set the pressure (Pa) and specific volume (m^3/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 setState_HP(double h, double p, double tol=1e-9)
Set the internally stored specific enthalpy (J/kg) and pressure (Pa) of the phase.
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 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 getGibbs_RT_ref(doublereal *grt) const
Returns the vector of nondimensional Gibbs Free Energies of the reference state at the current temper...
std::string m_tpx_name
Name for this substance used by the TPX package.
virtual void getActivities(doublereal *a) const
Get the array of non-dimensional activities at the current solution temperature, pressure,...
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
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 pressure() const
Return the thermodynamic pressure (Pa).
virtual void getPartialMolarIntEnergies(doublereal *ubar) const
Return an array of partial molar internal energies for the species in the mixture.
virtual void setState_UP(double u, double p, double tol=1e-9)
Set the specific internal energy (J/kg) and pressure (Pa).
virtual doublereal cp_mole() const
Molar heat capacity at constant pressure. Units: J/kmol/K.
virtual void getPartialMolarEnthalpies(doublereal *hbar) const
Returns an array of partial molar enthalpies for the species in the mixture.
virtual void getPartialMolarEntropies(doublereal *sbar) const
Returns an array of partial molar entropies of the species in the solution.
virtual doublereal critPressure() const
Critical pressure (Pa).
virtual doublereal enthalpy_mole() const
Molar enthalpy. Units: J/kmol.
virtual void getPartialMolarVolumes(doublereal *vbar) const
Return an array of partial molar volumes for the species in the mixture.
virtual doublereal cv_mole() const
Molar heat capacity at constant volume. Units: J/kmol/K.
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 void setState_SH(double s, double h, double tol=1e-9)
Set the specific entropy (J/kg/K) and the specific enthalpy (J/kg)
virtual doublereal thermalExpansionCoeff() const
Return the volumetric thermal expansion coefficient. Units: 1/K.
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 doublereal satTemperature(doublereal p) const
Return the saturation temperature given the pressure.
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 std::vector< std::string > partialStates() const
Return a vector of settable partial property sets within a phase.
virtual void setPressure(doublereal p)
sets the thermodynamic pressure (Pa).
virtual doublereal entropy_mole() const
Molar entropy. Units: J/kmol/K.
virtual void initThermo()
Initialize the ThermoPhase object after all species have been set up.
virtual doublereal critTemperature() const
Critical temperature (K).
virtual double minTemp(size_t k=npos) const
Minimum temperature for which the thermodynamic data for the species or phase are valid.
virtual void setState_SP(double s, double p, double tol=1e-9)
Set the specific entropy (J/kg/K) and pressure (Pa).
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 setState_Tsat(doublereal t, doublereal x)
Set the state to a saturated system at a particular temperature.
virtual doublereal gibbs_mole() const
Molar Gibbs function. Units: J/kmol.
virtual Units standardConcentrationUnits() const
Returns the units of the "standard concentration" for this phase.
virtual void setParametersFromXML(const XML_Node &eosdata)
Set equation of state parameter values from XML entries.
doublereal m_mw
Molecular weight of the substance (kg kmol-1)
virtual void setDensity(const double rho)
Set the internally stored density (kg/m^3) of the phase.
std::unique_ptr< tpx::Substance > m_sub
Pointer to the underlying tpx object Substance that does the work.
void setSubstance(const std::string &name)
Set the name of the TPX substance to use for the equation of state.
virtual void getStandardChemPotentials(doublereal *mu) const
virtual void setState_Psat(doublereal p, doublereal x)
Set the state to a saturated system at a particular pressure.
virtual double maxTemp(size_t k=npos) const
Maximum temperature for which the thermodynamic data for the species are valid.
virtual doublereal satPressure(doublereal t)
Return the saturation pressure given the temperature.
virtual std::vector< std::string > fullStates() const
Return a vector containing full states defining a phase.
virtual void setTemperature(const double T)
Set the internally stored temperature of the phase (K).
tpx::Substance & TPX_Substance()
Returns a reference to the substance object.
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 getEnthalpy_RT(doublereal *hrt) const
Get the nondimensional Enthalpy functions for the species at their standard states at the current T a...
virtual doublereal critDensity() const
Critical density (kg/m3).
virtual std::string phaseOfMatter() const
String indicating the mechanical phase of the matter in this Phase.
int m_subflag
Int indicating the type of the fluid.
virtual void setState_TV(double t, double v, double tol=1e-9)
Set the temperature (K) and specific volume (m^3/kg).
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).
virtual doublereal intEnergy_mole() const
Molar internal energy. Units: J/kmol.
bool m_verbose
flag to turn on some printing.
virtual void getEnthalpy_RT_ref(doublereal *hrt) const
virtual void setState_ST(double s, double t, double tol=1e-9)
Set the specific entropy (J/kg/K) and temperature (K).
virtual doublereal isothermalCompressibility() const
Returns the isothermal compressibility. Units: 1/Pa.
virtual void setState_TH(double t, double h, double tol=1e-9)
Set the temperature (K) and the specific enthalpy (J/kg)
virtual doublereal standardConcentration(size_t k=0) const
Return the standard concentration for the kth species.
doublereal entropy_mass() const
Specific entropy. Units: J/kg/K.
Definition: ThermoPhase.h:752
doublereal RT() const
Return the Gas Constant multiplied by the current temperature.
Definition: ThermoPhase.h:776
doublereal gibbs_mass() const
Specific Gibbs function. Units: J/kg.
Definition: ThermoPhase.h:757
doublereal cp_mass() const
Specific heat at constant pressure. Units: J/kg/K.
Definition: ThermoPhase.h:762
virtual doublereal refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:145
doublereal cv_mass() const
Specific heat at constant volume. Units: J/kg/K.
Definition: ThermoPhase.h:767
doublereal intEnergy_mass() const
Specific internal energy. Units: J/kg.
Definition: ThermoPhase.h:747
MultiSpeciesThermo m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition: ThermoPhase.h:1870
doublereal enthalpy_mass() const
Specific enthalpy. Units: J/kg.
Definition: ThermoPhase.h:742
AnyMap m_input
Data supplied via setParameters.
Definition: ThermoPhase.h:1874
A representation of the units associated with a dimensional quantity.
Definition: Units.h:30
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:104
void _require(const std::string &a, const std::string &v) const
Require that the current XML node has an attribute named by the first argument, a,...
Definition: xml.cpp:576
void debuglog(const std::string &msg, int loglevel)
Write a message to the log only if loglevel > 0.
Definition: global.h:140
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition: ct_defs.h:109
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264
Contains declarations for string manipulation functions within Cantera.
Classes providing support for XML data files.