Cantera  4.0.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->internalPressure();
162}
163
165{
166 return *m_sub;
167}
168
169void PureFluidPhase::getPartialMolarEnthalpies(span<double> hbar) const
170{
171 checkArraySize("PureFluidPhase::getPartialMolarEnthalpies", hbar.size(), 1);
172 hbar[0] = enthalpy_mole();
173}
174
175void PureFluidPhase::getPartialMolarEntropies(span<double> sbar) const
176{
177 checkArraySize("PureFluidPhase::getPartialMolarEntropies", sbar.size(), 1);
178 sbar[0] = entropy_mole();
179}
180
182{
183 checkArraySize("PureFluidPhase::getPartialMolarIntEnergies", ubar.size(), 1);
184 ubar[0] = intEnergy_mole();
185}
186
187void PureFluidPhase::getPartialMolarCp(span<double> cpbar) const
188{
189 checkArraySize("PureFluidPhase::getPartialMolarCp", cpbar.size(), 1);
190 cpbar[0] = cp_mole();
191}
192
193void PureFluidPhase::getPartialMolarVolumes(span<double> vbar) const
194{
195 checkArraySize("PureFluidPhase::getPartialMolarVolumes", vbar.size(), 1);
196 vbar[0] = 1.0 / molarDensity();
197}
198
200{
201 return Units(1.0);
202}
203
205{
206 checkArraySize("PureFluidPhase::getActivityConcentrations", c.size(), 1);
207 c[0] = 1.0;
208}
209
211{
212 return 1.0;
213}
214
215void PureFluidPhase::getActivities(span<double> a) const
216{
217 checkArraySize("PureFluidPhase::getActivities", a.size(), 1);
218 a[0] = 1.0;
219}
220
222{
223 checkArraySize("PureFluidPhase::getStandardChemPotentials", mu.size(), 1);
224 mu[0] = gibbs_mole();
225}
226
227void PureFluidPhase::getEnthalpy_RT(span<double> hrt) const
228{
229 checkArraySize("PureFluidPhase::getEnthalpy_RT", hrt.size(), 1);
230 hrt[0] = enthalpy_mole() / RT();
231}
232
233void PureFluidPhase::getEntropy_R(span<double> sr) const
234{
235 checkArraySize("PureFluidPhase::getEntropy_R", sr.size(), 1);
236 sr[0] = entropy_mole() / GasConstant;
237}
238
239void PureFluidPhase::getGibbs_RT(span<double> grt) const
240{
241 checkArraySize("PureFluidPhase::getGibbs_RT", grt.size(), 1);
242 grt[0] = gibbs_mole() / RT();
243}
244
245void PureFluidPhase::getEnthalpy_RT_ref(span<double> hrt) const
246{
247 double rhoSave = density();
248 double t = temperature();
249 double plow = 1.0E-8;
250 Set(tpx::PropertyPair::TP, t, plow);
251 getEnthalpy_RT(hrt);
252 Set(tpx::PropertyPair::TV, t, 1 / rhoSave);
253
254}
255
256void PureFluidPhase::getGibbs_RT_ref(span<double> grt) const
257{
258 double rhoSave = density();
259 double t = temperature();
260 double pref = refPressure();
261 double plow = 1.0E-8;
262 Set(tpx::PropertyPair::TP, t, plow);
263 getGibbs_RT(grt);
264 grt[0] += log(pref/plow);
265 Set(tpx::PropertyPair::TV, t, 1 / rhoSave);
266}
267
268void PureFluidPhase::getGibbs_ref(span<double> g) const
269{
271 g[0] *= RT();
272}
273
274void PureFluidPhase::getEntropy_R_ref(span<double> er) const
275{
276 double rhoSave = density();
277 double t = temperature();
278 double pref = refPressure();
279 double plow = 1.0E-8;
280 Set(tpx::PropertyPair::TP, t, plow);
281 getEntropy_R(er);
282 er[0] -= log(pref/plow);
283 Set(tpx::PropertyPair::TV, t, 1 / rhoSave);
284}
285
287{
288 return m_sub->Tcrit();
289}
290
292{
293 return m_sub->Pcrit();
294}
295
297{
298 return 1.0/m_sub->Vcrit();
299}
300
301double PureFluidPhase::satTemperature(double p) const
302{
303 return m_sub->Tsat(p);
304}
305
306/* The next several functions set the state. They run the Substance::Set
307 * function, followed by setting the state of the ThermoPhase object
308 * to the newly computed temperature and density of the Substance.
309 */
310
311void PureFluidPhase::setState_HP(double h, double p, double tol)
312{
313 Set(tpx::PropertyPair::HP, h, p);
314 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
315}
316
317void PureFluidPhase::setState_UV(double u, double v, double tol)
318{
319 Set(tpx::PropertyPair::UV, u, v);
320 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
321}
322
323void PureFluidPhase::setState_SV(double s, double v, double tol)
324{
325 Set(tpx::PropertyPair::SV, s, v);
326 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
327}
328
329void PureFluidPhase::setState_SP(double s, double p, double tol)
330{
331 Set(tpx::PropertyPair::SP, s, p);
332 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
333}
334
335void PureFluidPhase::setState_ST(double s, double t, double tol)
336{
337 Set(tpx::PropertyPair::ST, s, t);
338 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
339}
340
341void PureFluidPhase::setState_TV(double t, double v, double tol)
342{
343 Set(tpx::PropertyPair::TV, t, v);
344 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
345}
346
347void PureFluidPhase::setState_PV(double p, double v, double tol)
348{
349 Set(tpx::PropertyPair::PV, p, v);
350 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
351}
352
353void PureFluidPhase::setState_UP(double u, double p, double tol)
354{
355 Set(tpx::PropertyPair::UP, u, p);
356 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
357}
358
359void PureFluidPhase::setState_VH(double v, double h, double tol)
360{
361 Set(tpx::PropertyPair::VH, v, h);
362 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
363}
364
365void PureFluidPhase::setState_TH(double t, double h, double tol)
366{
367 Set(tpx::PropertyPair::TH, t, h);
368 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
369}
370
371void PureFluidPhase::setState_SH(double s, double h, double tol)
372{
373 Set(tpx::PropertyPair::SH, s, h);
374 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
375}
376
378{
379 Set(tpx::PropertyPair::TV, t, m_sub->v());
380 return m_sub->Ps();
381}
382
384{
385 return m_sub->x();
386}
387
388void PureFluidPhase::setState_Tsat(double t, double x)
389{
390 Set(tpx::PropertyPair::TX, t, x);
393}
394
395void PureFluidPhase::setState_Psat(double p, double x)
396{
397 Set(tpx::PropertyPair::PX, p, x);
400}
401
402string PureFluidPhase::report(bool show_thermo, double threshold) const
403{
404 fmt::memory_buffer b;
405 // This is the width of the first column of names in the report.
406 int name_width = 18;
407
408 string blank_leader = fmt::format("{:{}}", "", name_width);
409
410 string one_property = fmt::format("{{:>{}}} {{:<.5g}} {{}}\n", name_width);
411
412 constexpr auto two_prop_header = "{} {:^15} {:^15}\n";
413 string kg_kmol_header = fmt::format(
414 two_prop_header, blank_leader, "1 kg", "1 kmol"
415 );
416 string two_prop_sep = fmt::format(
417 "{} {:-^15} {:-^15}\n", blank_leader, "", ""
418 );
419 string two_property = fmt::format(
420 "{{:>{}}} {{:15.5g}} {{:15.5g}} {{}}\n", name_width
421 );
422
423 if (name() != "") {
424 fmt_append(b, "\n {}:\n", name());
425 }
426 fmt_append(b, "\n");
427 fmt_append(b, one_property, "temperature", temperature(), "K");
428 fmt_append(b, one_property, "pressure", pressure(), "Pa");
429 fmt_append(b, one_property, "density", density(), "kg/m^3");
430 fmt_append(b, one_property,
431 "mean mol. weight", meanMolecularWeight(), "kg/kmol");
432 fmt_append(b, "{:>{}} {:<.5g}\n",
433 "vapor fraction", name_width, vaporFraction());
434 fmt_append(b, "{:>{}} {}\n",
435 "phase of matter", name_width, phaseOfMatter());
436
437 if (show_thermo) {
438 fmt_append(b, "\n");
439 fmt_append(b, kg_kmol_header);
440 fmt_append(b, two_prop_sep);
441 fmt_append(b, two_property,
442 "enthalpy", enthalpy_mass(), enthalpy_mole(), "J");
443 fmt_append(b, two_property,
444 "internal energy", intEnergy_mass(), intEnergy_mole(), "J");
445 fmt_append(b, two_property,
446 "entropy", entropy_mass(), entropy_mole(), "J/K");
447 fmt_append(b, two_property,
448 "Gibbs function", gibbs_mass(), gibbs_mole(), "J");
449 fmt_append(b, two_property,
450 "heat capacity c_p", cp_mass(), cp_mole(), "J/K");
451 fmt_append(b, two_property,
452 "heat capacity c_v", cv_mass(), cv_mole(), "J/K");
453 }
454
455 return to_string(b);
456}
457
458}
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:431
bool hasKey(const string &key) const
Returns true if the map contains an item named key.
Definition AnyMap.cpp:1477
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:587
void setState_TD(double t, double rho)
Set the internally stored temperature (K) and density (kg/m^3)
Definition Phase.cpp:375
double temperature() const
Temperature (K).
Definition Phase.h:585
double meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition Phase.h:676
virtual void setDensity(const double density_)
Set the internally stored density (kg/m^3) of the phase.
Definition Phase.cpp:597
virtual double density() const
Density (kg/m^3).
Definition Phase.h:610
virtual void setTemperature(double temp)
Set the internally stored temperature of the phase (K).
Definition Phase.h:646
void setMolecularWeight(const int k, const double mw)
Set the molecular weight of a single species to a given value.
Definition Phase.cpp:915
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.
void getGibbs_ref(span< double > g) const override
Returns the vector of the Gibbs function of the reference state at the current temperature of the sol...
double enthalpy_mole() const override
Molar enthalpy. Units: J/kmol.
void getActivities(span< double > a) const override
Get the array of non-dimensional activities at the current solution temperature, pressure,...
double thermalExpansionCoeff() const override
Return the volumetric thermal expansion coefficient. Units: 1/K.
void getEntropy_R_ref(span< double > er) const override
Returns the vector of nondimensional entropies of the reference state at the current temperature of t...
void getPartialMolarEnthalpies(span< 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 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 getPartialMolarCp(span< double > cpbar) const override
Return an array of partial molar heat capacities for the species in the mixture.
void getStandardChemPotentials(span< 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 getGibbs_RT(span< double > grt) const override
Get the nondimensional Gibbs functions for the species in their standard states at the current T and ...
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 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 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 getEnthalpy_RT_ref(span< double > hrt) const override
Returns the vector of nondimensional enthalpies of the reference state at the current temperature of ...
double internalPressure() const override
Return the internal pressure [Pa].
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 and composition [J/kmol/K].
void getEnthalpy_RT(span< double > hrt) const override
Get the nondimensional Enthalpy functions for the species at their standard states at the current T a...
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 getEntropy_R(span< double > sr) const override
Get the array of nondimensional Entropy functions for the standard state species at the current T and...
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.
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 getGibbs_RT_ref(span< double > grt) const override
Returns the vector of nondimensional Gibbs Free Energies of the reference state at the current temper...
double cp_mole() const override
Molar heat capacity at constant pressure and composition [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).
void getPartialMolarVolumes(span< double > vbar) const override
Return an array of partial molar volumes for the species in the mixture.
Units standardConcentrationUnits() const override
Returns the units of the "standard concentration" for this phase.
void getPartialMolarEntropies(span< double > sbar) const override
Returns an array of partial molar entropies of the species in the solution.
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 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 getActivityConcentrations(span< double > c) const override
This method returns an array of generalized concentrations.
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).
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.
double satPressure(double t) override
Return the saturation pressure given the temperature.
void getPartialMolarIntEnergies(span< double > ubar) const override
Return an array of partial molar internal energies for the species in the mixture.
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 and composition [J/kg/K].
double entropy_mass() const
Specific entropy. Units: J/kg/K.
double cp_mass() const
Specific heat at constant pressure and composition [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
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:154
const double GasConstant
Universal Gas Constant [J/kmol/K].
Definition ct_defs.h:123
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595
void checkArraySize(const char *procedure, size_t available, size_t required)
Wrapper for throwing ArraySizeError.
Contains declarations for string manipulation functions within Cantera.