Cantera 2.6.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 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#include "cantera/base/global.h"
18
19using std::string;
20
21namespace Cantera
22{
23
25 m_subflag(-1),
26 m_mw(-1.0),
27 m_verbose(false)
28{
29}
30
32{
33 if (m_input.hasKey("pure-fluid-name")) {
34 setSubstance(m_input["pure-fluid-name"].asString());
35 }
36
37 if (m_tpx_name != "") {
38 m_sub.reset(tpx::newSubstance(m_tpx_name));
39 } else {
40 m_sub.reset(tpx::GetSub(m_subflag));
41 }
42
43 m_mw = m_sub->MolWt();
45
46 double cp0_R, h0_RT, s0_R, p;
47 double T0 = 298.15;
48 if (T0 < m_sub->Tcrit()) {
49 m_sub->Set(tpx::PropertyPair::TX, T0, 1.0);
50 p = 0.01*m_sub->P();
51 } else {
52 p = 0.001*m_sub->Pcrit();
53 }
54 p = 0.001 * p;
55 m_sub->Set(tpx::PropertyPair::TP, T0, p);
56
57 m_spthermo.update_single(0, T0, &cp0_R, &h0_RT, &s0_R);
58 double s_R = s0_R - log(p/refPressure());
59 m_sub->setStdState(h0_RT*GasConstant*298.15/m_mw,
60 s_R*GasConstant/m_mw, T0, p);
61 debuglog("PureFluidPhase::initThermo: initialized phase "
62 +name()+"\n", m_verbose);
63}
64
66{
68 phaseNode["pure-fluid-name"] = m_sub->name();
69}
70
72{
73 eosdata._require("model","PureFluid");
74 m_subflag = atoi(eosdata["fluid_type"].c_str());
75 if (m_subflag < 0) {
76 throw CanteraError("PureFluidPhase::setParametersFromXML",
77 "missing or negative substance flag");
78 }
79}
80
81std::vector<std::string> PureFluidPhase::fullStates() const
82{
83 return {"TD", "UV", "DP", "HP", "SP", "SV",
84 "ST", "TV", "PV", "UP", "VH", "TH", "SH", "TPQ"};
85}
86
87std::vector<std::string> PureFluidPhase::partialStates() const
88{
89 return {"TP", "TQ", "PQ"};
90}
91
93{
94 if (temperature() >= critTemperature() || pressure() >= critPressure()) {
95 return "supercritical";
96 } else if (m_sub->TwoPhase() == 1) {
97 return "liquid-gas-mix";
98 } else if (pressure() < m_sub->Ps()) {
99 return "gas";
100 } else {
101 return "liquid";
102 }
103}
104
105double PureFluidPhase::minTemp(size_t k) const
106{
107 return m_sub->Tmin();
108}
109
110double PureFluidPhase::maxTemp(size_t k) const
111{
112 return m_sub->Tmax();
113}
114
116{
117 return m_sub->h() * m_mw;
118}
119
121{
122 return m_sub->u() * m_mw;
123}
124
126{
127 return m_sub->s() * m_mw;
128}
129
131{
132 return m_sub->g() * m_mw;
133}
134
135doublereal PureFluidPhase::cp_mole() const
136{
137 return m_sub->cp() * m_mw;
138}
139
140doublereal PureFluidPhase::cv_mole() const
141{
142 return m_sub->cv() * m_mw;
143}
144
145doublereal PureFluidPhase::pressure() const
146{
147 return m_sub->P();
148}
149
151{
152 Set(tpx::PropertyPair::TP, temperature(), p);
154}
155
157{
159 Set(tpx::PropertyPair::TV, T, m_sub->v());
160}
161
163{
165 Set(tpx::PropertyPair::TV, m_sub->Temp(), 1.0/rho);
166}
167
168void PureFluidPhase::Set(tpx::PropertyPair::type n, double x, double y) const
169{
170 m_sub->Set(n, x, y);
171}
172
174{
175 return m_sub->isothermalCompressibility();
176}
177
179{
180 return m_sub->thermalExpansionCoeff();
181}
182
184{
185 return *m_sub;
186}
187
189{
190 hbar[0] = enthalpy_mole();
191}
192
194{
195 sbar[0] = entropy_mole();
196}
197
199{
200 ubar[0] = intEnergy_mole();
201}
202
203void PureFluidPhase::getPartialMolarCp(doublereal* cpbar) const
204{
205 cpbar[0] = cp_mole();
206}
207
208void PureFluidPhase::getPartialMolarVolumes(doublereal* vbar) const
209{
210 vbar[0] = 1.0 / molarDensity();
211}
212
214{
215 return Units(1.0);
216}
217
219{
220 c[0] = 1.0;
221}
222
224{
225 return 1.0;
226}
227
228void PureFluidPhase::getActivities(doublereal* a) const
229{
230 a[0] = 1.0;
231}
232
234{
235 mu[0] = gibbs_mole();
236}
237
238void PureFluidPhase::getEnthalpy_RT(doublereal* hrt) const
239{
240 hrt[0] = enthalpy_mole() / RT();
241}
242
243void PureFluidPhase::getEntropy_R(doublereal* sr) const
244{
245 sr[0] = entropy_mole() / GasConstant;
246}
247
248void PureFluidPhase::getGibbs_RT(doublereal* grt) const
249{
250 grt[0] = gibbs_mole() / RT();
251}
252
253void PureFluidPhase::getEnthalpy_RT_ref(doublereal* hrt) const
254{
255 double psave = pressure();
256 double t = temperature();
257 double plow = 1.0E-8;
258 Set(tpx::PropertyPair::TP, t, plow);
259 getEnthalpy_RT(hrt);
260 Set(tpx::PropertyPair::TP, t, psave);
261
262}
263
264void PureFluidPhase::getGibbs_RT_ref(doublereal* grt) const
265{
266 double psave = pressure();
267 double t = temperature();
268 double pref = refPressure();
269 double plow = 1.0E-8;
270 Set(tpx::PropertyPair::TP, t, plow);
271 getGibbs_RT(grt);
272 grt[0] += log(pref/plow);
273 Set(tpx::PropertyPair::TP, t, psave);
274}
275
276void PureFluidPhase::getGibbs_ref(doublereal* g) const
277{
279 g[0] *= RT();
280}
281
282void PureFluidPhase::getEntropy_R_ref(doublereal* er) const
283{
284 double psave = pressure();
285 double t = temperature();
286 double pref = refPressure();
287 double plow = 1.0E-8;
288 Set(tpx::PropertyPair::TP, t, plow);
289 getEntropy_R(er);
290 er[0] -= log(pref/plow);
291 Set(tpx::PropertyPair::TP, t, psave);
292}
293
295{
296 return m_sub->Tcrit();
297}
298
300{
301 return m_sub->Pcrit();
302}
303
305{
306 return 1.0/m_sub->Vcrit();
307}
308
309doublereal PureFluidPhase::satTemperature(doublereal p) const
310{
311 return m_sub->Tsat(p);
312}
313
314/* The next several functions set the state. They run the Substance::Set
315 * function, followed by setting the state of the ThermoPhase object
316 * to the newly computed temperature and density of the Substance.
317 */
318
319void PureFluidPhase::setState_HP(double h, double p, double tol)
320{
321 Set(tpx::PropertyPair::HP, h, p);
322 setState_TR(m_sub->Temp(), 1.0/m_sub->v());
323}
324
325void PureFluidPhase::setState_UV(double u, double v, double tol)
326{
327 Set(tpx::PropertyPair::UV, u, v);
328 setState_TR(m_sub->Temp(), 1.0/m_sub->v());
329}
330
331void PureFluidPhase::setState_SV(double s, double v, double tol)
332{
333 Set(tpx::PropertyPair::SV, s, v);
334 setState_TR(m_sub->Temp(), 1.0/m_sub->v());
335}
336
337void PureFluidPhase::setState_SP(double s, double p, double tol)
338{
339 Set(tpx::PropertyPair::SP, s, p);
340 setState_TR(m_sub->Temp(), 1.0/m_sub->v());
341}
342
343void PureFluidPhase::setState_ST(double s, double t, double tol)
344{
345 Set(tpx::PropertyPair::ST, s, t);
346 setState_TR(m_sub->Temp(), 1.0/m_sub->v());
347}
348
349void PureFluidPhase::setState_TV(double t, double v, double tol)
350{
351 Set(tpx::PropertyPair::TV, t, v);
352 setState_TR(m_sub->Temp(), 1.0/m_sub->v());
353}
354
355void PureFluidPhase::setState_PV(double p, double v, double tol)
356{
357 Set(tpx::PropertyPair::PV, p, v);
358 setState_TR(m_sub->Temp(), 1.0/m_sub->v());
359}
360
361void PureFluidPhase::setState_UP(double u, double p, double tol)
362{
363 Set(tpx::PropertyPair::UP, u, p);
364 setState_TR(m_sub->Temp(), 1.0/m_sub->v());
365}
366
367void PureFluidPhase::setState_VH(double v, double h, double tol)
368{
369 Set(tpx::PropertyPair::VH, v, h);
370 setState_TR(m_sub->Temp(), 1.0/m_sub->v());
371}
372
373void PureFluidPhase::setState_TH(double t, double h, double tol)
374{
375 Set(tpx::PropertyPair::TH, t, h);
376 setState_TR(m_sub->Temp(), 1.0/m_sub->v());
377}
378
379void PureFluidPhase::setState_SH(double s, double h, double tol)
380{
381 Set(tpx::PropertyPair::SH, s, h);
382 setState_TR(m_sub->Temp(), 1.0/m_sub->v());
383}
384
385doublereal PureFluidPhase::satPressure(doublereal t)
386{
387 Set(tpx::PropertyPair::TV, t, m_sub->v());
388 return m_sub->Ps();
389}
390
392{
393 return m_sub->x();
394}
395
396void PureFluidPhase::setState_Tsat(doublereal t, doublereal x)
397{
398 Set(tpx::PropertyPair::TX, t, x);
401}
402
403void PureFluidPhase::setState_Psat(doublereal p, doublereal x)
404{
405 Set(tpx::PropertyPair::PX, p, x);
408}
409
410std::string PureFluidPhase::report(bool show_thermo, doublereal threshold) const
411{
412 fmt::memory_buffer b;
413 // This is the width of the first column of names in the report.
414 int name_width = 18;
415
416 string blank_leader = fmt::format("{:{}}", "", name_width);
417
418 string one_property = fmt::format("{{:>{}}} {{:<.5g}} {{}}\n", name_width);
419
420 string two_prop_header = "{} {:^15} {:^15}\n";
421 string kg_kmol_header = fmt::format(
422 two_prop_header, blank_leader, "1 kg", "1 kmol"
423 );
424 string two_prop_sep = fmt::format(
425 "{} {:-^15} {:-^15}\n", blank_leader, "", ""
426 );
427 string two_property = fmt::format(
428 "{{:>{}}} {{:15.5g}} {{:15.5g}} {{}}\n", name_width
429 );
430
431 if (name() != "") {
432 fmt_append(b, "\n {}:\n", name());
433 }
434 fmt_append(b, "\n");
435 fmt_append(b, one_property, "temperature", temperature(), "K");
436 fmt_append(b, one_property, "pressure", pressure(), "Pa");
437 fmt_append(b, one_property, "density", density(), "kg/m^3");
438 fmt_append(b, one_property,
439 "mean mol. weight", meanMolecularWeight(), "kg/kmol");
440 fmt_append(b, "{:>{}} {:<.5g}\n",
441 "vapor fraction", name_width, vaporFraction());
442 fmt_append(b, "{:>{}} {}\n",
443 "phase of matter", name_width, phaseOfMatter());
444
445 if (show_thermo) {
446 fmt_append(b, "\n");
447 fmt_append(b, kg_kmol_header);
448 fmt_append(b, two_prop_sep);
449 fmt_append(b, two_property,
450 "enthalpy", enthalpy_mass(), enthalpy_mole(), "J");
451 fmt_append(b, two_property,
452 "internal energy", intEnergy_mass(), intEnergy_mole(), "J");
453 fmt_append(b, two_property,
454 "entropy", entropy_mass(), entropy_mole(), "J/K");
455 fmt_append(b, two_property,
456 "Gibbs function", gibbs_mass(), gibbs_mole(), "J");
457 fmt_append(b, two_property,
458 "heat capacity c_p", cp_mass(), cp_mole(), "J/K");
459 fmt_append(b, two_property,
460 "heat capacity c_v", cv_mass(), cv_mole(), "J/K");
461 }
462
463 return to_string(b);
464}
465
466}
Header for a ThermoPhase class for a pure fluid phase consisting of gas, liquid, mixed-gas-liquid and...
A map of string keys to values whose type can vary at runtime.
Definition: AnyMap.h:399
bool hasKey(const std::string &key) const
Returns true if the map contains an item named key.
Definition: AnyMap.cpp:1406
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
Get reference-state properties for a single species.
double molarDensity() const
Molar density (kmol/m^3).
Definition: Phase.cpp:671
std::string name() const
Return the name of the phase.
Definition: Phase.cpp:70
virtual void setDensity(const double density_)
Set the internally stored density (kg/m^3) of the phase.
Definition: Phase.cpp:687
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:751
void setState_TR(doublereal t, doublereal rho)
Set the internally stored temperature (K) and density (kg/m^3)
Definition: Phase.cpp:462
virtual double density() const
Density (kg/m^3).
Definition: Phase.h:679
doublereal temperature() const
Temperature (K).
Definition: Phase.h:654
virtual void setTemperature(double temp)
Set the internally stored temperature of the phase (K).
Definition: Phase.h:719
void setMolecularWeight(const int k, const double mw)
Set the molecular weight of a single species to a given value.
Definition: Phase.cpp:989
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 getParameters(AnyMap &phaseNode) const
Store the parameters of a ThermoPhase object such that an identical one could be reconstructed using ...
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:758
doublereal RT() const
Return the Gas Constant multiplied by the current temperature.
Definition: ThermoPhase.h:782
doublereal gibbs_mass() const
Specific Gibbs function. Units: J/kg.
Definition: ThermoPhase.h:763
doublereal cp_mass() const
Specific heat at constant pressure. Units: J/kg/K.
Definition: ThermoPhase.h:768
virtual doublereal refPressure() const
Returns the reference pressure in Pa.
Definition: ThermoPhase.h:150
doublereal cv_mass() const
Specific heat at constant volume. Units: J/kg/K.
Definition: ThermoPhase.h:773
doublereal intEnergy_mass() const
Specific internal energy. Units: J/kg.
Definition: ThermoPhase.h:753
MultiSpeciesThermo m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
Definition: ThermoPhase.h:1894
doublereal enthalpy_mass() const
Specific enthalpy. Units: J/kg.
Definition: ThermoPhase.h:748
AnyMap m_input
Data supplied via setParameters.
Definition: ThermoPhase.h:1898
virtual void getParameters(int &n, doublereal *const c) const
Get the equation of state parameters in a vector.
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:103
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:577
This file contains definitions for utility functions and text for modules, inputfiles,...
Namespace for the Cantera kernel.
Definition: AnyMap.h:29
void debuglog(const std::string &msg, int loglevel)
Write a message to the log only if loglevel > 0.
Definition: global.h:146
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition: ct_defs.h:113
Contains declarations for string manipulation functions within Cantera.
Classes providing support for XML data files.