Cantera  3.1.0a1
Loading...
Searching...
No Matches
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
12
13#include "cantera/tpx/Sub.h"
14#include "cantera/tpx/utils.h"
16#include "cantera/base/global.h"
17
18namespace Cantera
19{
20
22{
23 if (m_input.hasKey("pure-fluid-name")) {
24 setSubstance(m_input["pure-fluid-name"].asString());
25 }
26
27 m_sub.reset(tpx::newSubstance(m_tpx_name));
28
29 m_mw = m_sub->MolWt();
31
32 double cp0_R, h0_RT, s0_R, p;
33 double T0 = 298.15;
34 if (T0 < m_sub->Tcrit()) {
35 m_sub->Set(tpx::PropertyPair::TX, T0, 1.0);
36 p = 0.01*m_sub->P();
37 } else {
38 p = 0.001*m_sub->Pcrit();
39 }
40 p = 0.001 * p;
41 m_sub->Set(tpx::PropertyPair::TP, T0, p);
42
43 m_spthermo.update_single(0, T0, &cp0_R, &h0_RT, &s0_R);
44 double s_R = s0_R - log(p/refPressure());
45 m_sub->setStdState(h0_RT*GasConstant*298.15/m_mw,
46 s_R*GasConstant/m_mw, T0, p);
47 debuglog("PureFluidPhase::initThermo: initialized phase "
48 +name()+"\n", m_verbose);
49}
50
52{
54 phaseNode["pure-fluid-name"] = m_sub->name();
55}
56
57vector<string> PureFluidPhase::fullStates() const
58{
59 return {"TD", "UV", "DP", "HP", "SP", "SV",
60 "ST", "TV", "PV", "UP", "VH", "TH", "SH", "TPQ"};
61}
62
63vector<string> PureFluidPhase::partialStates() const
64{
65 return {"TP", "TQ", "PQ"};
66}
67
69{
70 if (temperature() >= critTemperature() || pressure() >= critPressure()) {
71 return "supercritical";
72 } else if (m_sub->TwoPhase() == 1) {
73 return "liquid-gas-mix";
74 } else if (pressure() < m_sub->Ps()) {
75 return "gas";
76 } else {
77 return "liquid";
78 }
79}
80
81double PureFluidPhase::minTemp(size_t k) const
82{
83 return m_sub->Tmin();
84}
85
86double PureFluidPhase::maxTemp(size_t k) const
87{
88 return m_sub->Tmax();
89}
90
92{
93 return m_sub->h() * m_mw;
94}
95
97{
98 return m_sub->u() * m_mw;
99}
100
102{
103 return m_sub->s() * m_mw;
104}
105
107{
108 return m_sub->g() * m_mw;
109}
110
112{
113 return m_sub->cp() * m_mw;
114}
115
117{
118 return m_sub->cv() * m_mw;
119}
120
122{
123 return m_sub->P();
124}
125
127{
128 Set(tpx::PropertyPair::TP, temperature(), p);
130}
131
133{
135 Set(tpx::PropertyPair::TV, T, m_sub->v());
136}
137
139{
141 Set(tpx::PropertyPair::TV, m_sub->Temp(), 1.0/rho);
142}
143
144void PureFluidPhase::Set(tpx::PropertyPair::type n, double x, double y) const
145{
146 m_sub->Set(n, x, y);
147}
148
150{
151 return m_sub->isothermalCompressibility();
152}
153
155{
156 return m_sub->thermalExpansionCoeff();
157}
158
160{
161 return *m_sub;
162}
163
165{
166 hbar[0] = enthalpy_mole();
167}
168
170{
171 sbar[0] = entropy_mole();
172}
173
175{
176 ubar[0] = intEnergy_mole();
177}
178
179void PureFluidPhase::getPartialMolarCp(double* cpbar) const
180{
181 cpbar[0] = cp_mole();
182}
183
185{
186 vbar[0] = 1.0 / molarDensity();
187}
188
190{
191 return Units(1.0);
192}
193
195{
196 c[0] = 1.0;
197}
198
200{
201 return 1.0;
202}
203
204void PureFluidPhase::getActivities(double* a) const
205{
206 a[0] = 1.0;
207}
208
210{
211 mu[0] = gibbs_mole();
212}
213
214void PureFluidPhase::getEnthalpy_RT(double* hrt) const
215{
216 hrt[0] = enthalpy_mole() / RT();
217}
218
219void PureFluidPhase::getEntropy_R(double* sr) const
220{
221 sr[0] = entropy_mole() / GasConstant;
222}
223
224void PureFluidPhase::getGibbs_RT(double* grt) const
225{
226 grt[0] = gibbs_mole() / RT();
227}
228
230{
231 double rhoSave = density();
232 double t = temperature();
233 double plow = 1.0E-8;
234 Set(tpx::PropertyPair::TP, t, plow);
235 getEnthalpy_RT(hrt);
236 Set(tpx::PropertyPair::TV, t, 1 / rhoSave);
237
238}
239
240void PureFluidPhase::getGibbs_RT_ref(double* grt) const
241{
242 double rhoSave = density();
243 double t = temperature();
244 double pref = refPressure();
245 double plow = 1.0E-8;
246 Set(tpx::PropertyPair::TP, t, plow);
247 getGibbs_RT(grt);
248 grt[0] += log(pref/plow);
249 Set(tpx::PropertyPair::TV, t, 1 / rhoSave);
250}
251
252void PureFluidPhase::getGibbs_ref(double* g) const
253{
255 g[0] *= RT();
256}
257
259{
260 double rhoSave = density();
261 double t = temperature();
262 double pref = refPressure();
263 double plow = 1.0E-8;
264 Set(tpx::PropertyPair::TP, t, plow);
265 getEntropy_R(er);
266 er[0] -= log(pref/plow);
267 Set(tpx::PropertyPair::TV, t, 1 / rhoSave);
268}
269
271{
272 return m_sub->Tcrit();
273}
274
276{
277 return m_sub->Pcrit();
278}
279
281{
282 return 1.0/m_sub->Vcrit();
283}
284
285double PureFluidPhase::satTemperature(double p) const
286{
287 return m_sub->Tsat(p);
288}
289
290/* The next several functions set the state. They run the Substance::Set
291 * function, followed by setting the state of the ThermoPhase object
292 * to the newly computed temperature and density of the Substance.
293 */
294
295void PureFluidPhase::setState_HP(double h, double p, double tol)
296{
297 Set(tpx::PropertyPair::HP, h, p);
298 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
299}
300
301void PureFluidPhase::setState_UV(double u, double v, double tol)
302{
303 Set(tpx::PropertyPair::UV, u, v);
304 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
305}
306
307void PureFluidPhase::setState_SV(double s, double v, double tol)
308{
309 Set(tpx::PropertyPair::SV, s, v);
310 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
311}
312
313void PureFluidPhase::setState_SP(double s, double p, double tol)
314{
315 Set(tpx::PropertyPair::SP, s, p);
316 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
317}
318
319void PureFluidPhase::setState_ST(double s, double t, double tol)
320{
321 Set(tpx::PropertyPair::ST, s, t);
322 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
323}
324
325void PureFluidPhase::setState_TV(double t, double v, double tol)
326{
327 Set(tpx::PropertyPair::TV, t, v);
328 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
329}
330
331void PureFluidPhase::setState_PV(double p, double v, double tol)
332{
333 Set(tpx::PropertyPair::PV, p, v);
334 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
335}
336
337void PureFluidPhase::setState_UP(double u, double p, double tol)
338{
339 Set(tpx::PropertyPair::UP, u, p);
340 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
341}
342
343void PureFluidPhase::setState_VH(double v, double h, double tol)
344{
345 Set(tpx::PropertyPair::VH, v, h);
346 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
347}
348
349void PureFluidPhase::setState_TH(double t, double h, double tol)
350{
351 Set(tpx::PropertyPair::TH, t, h);
352 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
353}
354
355void PureFluidPhase::setState_SH(double s, double h, double tol)
356{
357 Set(tpx::PropertyPair::SH, s, h);
358 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
359}
360
362{
363 Set(tpx::PropertyPair::TV, t, m_sub->v());
364 return m_sub->Ps();
365}
366
368{
369 return m_sub->x();
370}
371
372void PureFluidPhase::setState_Tsat(double t, double x)
373{
374 Set(tpx::PropertyPair::TX, t, x);
377}
378
379void PureFluidPhase::setState_Psat(double p, double x)
380{
381 Set(tpx::PropertyPair::PX, p, x);
384}
385
386string PureFluidPhase::report(bool show_thermo, double threshold) const
387{
388 fmt::memory_buffer b;
389 // This is the width of the first column of names in the report.
390 int name_width = 18;
391
392 string blank_leader = fmt::format("{:{}}", "", name_width);
393
394 string one_property = fmt::format("{{:>{}}} {{:<.5g}} {{}}\n", name_width);
395
396 string two_prop_header = "{} {:^15} {:^15}\n";
397 string kg_kmol_header = fmt::format(
398 two_prop_header, blank_leader, "1 kg", "1 kmol"
399 );
400 string two_prop_sep = fmt::format(
401 "{} {:-^15} {:-^15}\n", blank_leader, "", ""
402 );
403 string two_property = fmt::format(
404 "{{:>{}}} {{:15.5g}} {{:15.5g}} {{}}\n", name_width
405 );
406
407 if (name() != "") {
408 fmt_append(b, "\n {}:\n", name());
409 }
410 fmt_append(b, "\n");
411 fmt_append(b, one_property, "temperature", temperature(), "K");
412 fmt_append(b, one_property, "pressure", pressure(), "Pa");
413 fmt_append(b, one_property, "density", density(), "kg/m^3");
414 fmt_append(b, one_property,
415 "mean mol. weight", meanMolecularWeight(), "kg/kmol");
416 fmt_append(b, "{:>{}} {:<.5g}\n",
417 "vapor fraction", name_width, vaporFraction());
418 fmt_append(b, "{:>{}} {}\n",
419 "phase of matter", name_width, phaseOfMatter());
420
421 if (show_thermo) {
422 fmt_append(b, "\n");
423 fmt_append(b, kg_kmol_header);
424 fmt_append(b, two_prop_sep);
425 fmt_append(b, two_property,
426 "enthalpy", enthalpy_mass(), enthalpy_mole(), "J");
427 fmt_append(b, two_property,
428 "internal energy", intEnergy_mass(), intEnergy_mole(), "J");
429 fmt_append(b, two_property,
430 "entropy", entropy_mass(), entropy_mole(), "J/K");
431 fmt_append(b, two_property,
432 "Gibbs function", gibbs_mass(), gibbs_mole(), "J");
433 fmt_append(b, two_property,
434 "heat capacity c_p", cp_mass(), cp_mole(), "J/K");
435 fmt_append(b, two_property,
436 "heat capacity c_v", cv_mass(), cv_mole(), "J/K");
437 }
438
439 return to_string(b);
440}
441
442}
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:427
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition AnyMap.cpp:1423
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.
virtual double molarDensity() const
Molar density (kmol/m^3).
Definition Phase.cpp:576
void setState_TD(double t, double rho)
Set the internally stored temperature (K) and density (kg/m^3)
Definition Phase.cpp:377
double temperature() const
Temperature (K).
Definition Phase.h:562
double meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition Phase.h:655
virtual void setDensity(const double density_)
Set the internally stored density (kg/m^3) of the phase.
Definition Phase.cpp:586
virtual double density() const
Density (kg/m^3).
Definition Phase.h:587
virtual void setTemperature(double temp)
Set the internally stored temperature of the phase (K).
Definition Phase.h:623
void setMolecularWeight(const int k, const double mw)
Set the molecular weight of a single species to a given value.
Definition Phase.cpp:895
string name() const
Return the name of the phase.
Definition Phase.cpp:20
void Set(tpx::PropertyPair::type n, double x, double y) const
Main call to the tpx level to set the state of the system.
double enthalpy_mole() const override
Molar enthalpy. Units: J/kmol.
double thermalExpansionCoeff() const override
Return the volumetric thermal expansion coefficient. Units: 1/K.
void getPartialMolarEnthalpies(double *hbar) const override
Returns an array of partial molar enthalpies for the species in the mixture.
double pressure() const override
Return the thermodynamic pressure (Pa).
vector< string > partialStates() const override
Return a vector of settable partial property sets within a phase.
double critPressure() const override
Critical pressure (Pa).
string m_tpx_name
Name for this substance used by the TPX package.
double critDensity() const override
Critical density (kg/m3).
void getEntropy_R(double *sr) const override
Get the array of nondimensional Entropy functions for the standard state species at the current T and...
void setState_SH(double s, double h, double tol=1e-9) override
Set the specific entropy (J/kg/K) and the specific enthalpy (J/kg)
void getGibbs_ref(double *g) const override
Returns the vector of the Gibbs function of the reference state at the current temperature of the sol...
void getStandardChemPotentials(double *mu) const override
Get the array of chemical potentials at unit activity for the species at their standard states at the...
double critTemperature() const override
Critical temperature (K).
void getActivities(double *a) const override
Get the array of non-dimensional activities at the current solution temperature, pressure,...
void getParameters(AnyMap &phaseNode) const override
Store the parameters of a ThermoPhase object such that an identical one could be reconstructed using ...
void initThermo() override
Initialize the ThermoPhase object after all species have been set up.
vector< string > fullStates() const override
Return a vector containing full states defining a phase.
void getActivityConcentrations(double *c) const override
This method returns an array of generalized concentrations.
void setState_HP(double h, double p, double tol=1e-9) override
Set the internally stored specific enthalpy (J/kg) and pressure (Pa) of the phase.
void setPressure(double p) override
sets the thermodynamic pressure (Pa).
unique_ptr< tpx::Substance > m_sub
Pointer to the underlying tpx object Substance that does the work.
void getPartialMolarVolumes(double *vbar) const override
Return an array of partial molar volumes for the species in the mixture.
void setState_PV(double p, double v, double tol=1e-9) override
Set the pressure (Pa) and specific volume (m^3/kg).
void setState_VH(double v, double h, double tol=1e-9) override
Set the specific volume (m^3/kg) and the specific enthalpy (J/kg)
double vaporFraction() const override
Return the fraction of vapor at the current conditions.
void setState_TH(double t, double h, double tol=1e-9) override
Set the temperature (K) and the specific enthalpy (J/kg)
double cv_mole() const override
Molar heat capacity at constant volume. Units: J/kmol/K.
void setTemperature(const double T) override
Set the internally stored temperature of the phase (K).
void setSubstance(const string &name)
Set the name of the TPX substance to use for the equation of state.
void getEnthalpy_RT(double *hrt) const override
Get the nondimensional Enthalpy functions for the species at their standard states at the current T a...
void getEntropy_R_ref(double *er) const override
Returns the vector of nondimensional entropies of the reference state at the current temperature of t...
double minTemp(size_t k=npos) const override
Minimum temperature for which the thermodynamic data for the species or phase are valid.
double isothermalCompressibility() const override
Returns the isothermal compressibility. Units: 1/Pa.
void getGibbs_RT(double *grt) const override
Get the nondimensional Gibbs functions for the species in their standard states at the current T and ...
double intEnergy_mole() const override
Molar internal energy. Units: J/kmol.
void setState_Tsat(double t, double x) override
Set the state to a saturated system at a particular temperature.
double entropy_mole() const override
Molar entropy. Units: J/kmol/K.
void getPartialMolarIntEnergies(double *ubar) const override
Return an array of partial molar internal energies for the species in the mixture.
double cp_mole() const override
Molar heat capacity at constant pressure. Units: J/kmol/K.
void setState_SV(double s, double v, double tol=1e-9) override
Set the specific entropy (J/kg/K) and specific volume (m^3/kg).
Units standardConcentrationUnits() const override
Returns the units of the "standard concentration" for this phase.
void getPartialMolarCp(double *cpbar) const override
Return an array of partial molar heat capacities for the species in the mixture.
double gibbs_mole() const override
Molar Gibbs function. Units: J/kmol.
double standardConcentration(size_t k=0) const override
Return the standard concentration for the kth species.
double satTemperature(double p) const override
Return the saturation temperature given the pressure.
double m_mw
Molecular weight of the substance (kg kmol-1)
tpx::Substance & TPX_Substance()
Returns a reference to the substance object.
void setState_TV(double t, double v, double tol=1e-9) override
Set the temperature (K) and specific volume (m^3/kg).
void setState_UV(double u, double v, double tol=1e-9) override
Set the specific internal energy (J/kg) and specific volume (m^3/kg).
void getGibbs_RT_ref(double *grt) const override
Returns the vector of nondimensional Gibbs Free Energies of the reference state at the current temper...
void setState_Psat(double p, double x) override
Set the state to a saturated system at a particular pressure.
void setState_UP(double u, double p, double tol=1e-9) override
Set the specific internal energy (J/kg) and pressure (Pa).
void setState_SP(double s, double p, double tol=1e-9) override
Set the specific entropy (J/kg/K) and pressure (Pa).
void setState_ST(double s, double t, double tol=1e-9) override
Set the specific entropy (J/kg/K) and temperature (K).
void getPartialMolarEntropies(double *sbar) const override
Returns an array of partial molar entropies of the species in the solution.
bool m_verbose
flag to turn on some printing.
double maxTemp(size_t k=npos) const override
Maximum temperature for which the thermodynamic data for the species are valid.
string phaseOfMatter() const override
String indicating the mechanical phase of the matter in this Phase.
void getEnthalpy_RT_ref(double *hrt) const override
Returns the vector of nondimensional enthalpies of the reference state at the current temperature of ...
double satPressure(double t) override
Return the saturation pressure given the temperature.
void setDensity(const double rho) override
Set the internally stored density (kg/m^3) of the phase.
string report(bool show_thermo=true, double threshold=1e-14) const override
returns a summary of the state of the phase as a string
virtual void getParameters(AnyMap &phaseNode) const
Store the parameters of a ThermoPhase object such that an identical one could be reconstructed using ...
double RT() const
Return the Gas Constant multiplied by the current temperature.
double gibbs_mass() const
Specific Gibbs function. Units: J/kg.
double cv_mass() const
Specific heat at constant volume. Units: J/kg/K.
double entropy_mass() const
Specific entropy. Units: J/kg/K.
double cp_mass() const
Specific heat at constant pressure. Units: J/kg/K.
double intEnergy_mass() const
Specific internal energy. Units: J/kg.
MultiSpeciesThermo m_spthermo
Pointer to the calculation manager for species reference-state thermodynamic properties.
virtual double refPressure() const
Returns the reference pressure in Pa.
AnyMap m_input
Data supplied via setParameters.
double enthalpy_mass() const
Specific enthalpy. Units: J/kg.
A representation of the units associated with a dimensional quantity.
Definition Units.h:35
Base class from which all pure substances are derived.
Definition Sub.h:37
void fmt_append(fmt::memory_buffer &b, Args... args)
Versions 6.2.0 and 6.2.1 of fmtlib do not include this define before they include windows....
Definition fmt.h:29
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
void debuglog(const string &msg, int loglevel)
Write a message to the log only if loglevel > 0.
Definition global.h:158
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition ct_defs.h:120
Namespace for the Cantera kernel.
Definition AnyMap.cpp:564
Contains declarations for string manipulation functions within Cantera.