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;
162}
163
164void PureFluidPhase::getPartialMolarEnthalpies(span<double> hbar) const
165{
166 checkArraySize("PureFluidPhase::getPartialMolarEnthalpies", hbar.size(), 1);
167 hbar[0] = enthalpy_mole();
168}
169
170void PureFluidPhase::getPartialMolarEntropies(span<double> sbar) const
171{
172 checkArraySize("PureFluidPhase::getPartialMolarEntropies", sbar.size(), 1);
173 sbar[0] = entropy_mole();
174}
175
177{
178 checkArraySize("PureFluidPhase::getPartialMolarIntEnergies", ubar.size(), 1);
179 ubar[0] = intEnergy_mole();
180}
181
182void PureFluidPhase::getPartialMolarCp(span<double> cpbar) const
183{
184 checkArraySize("PureFluidPhase::getPartialMolarCp", cpbar.size(), 1);
185 cpbar[0] = cp_mole();
186}
187
188void PureFluidPhase::getPartialMolarVolumes(span<double> vbar) const
189{
190 checkArraySize("PureFluidPhase::getPartialMolarVolumes", vbar.size(), 1);
191 vbar[0] = 1.0 / molarDensity();
192}
193
195{
196 return Units(1.0);
197}
198
200{
201 checkArraySize("PureFluidPhase::getActivityConcentrations", c.size(), 1);
202 c[0] = 1.0;
203}
204
206{
207 return 1.0;
208}
209
210void PureFluidPhase::getActivities(span<double> a) const
211{
212 checkArraySize("PureFluidPhase::getActivities", a.size(), 1);
213 a[0] = 1.0;
214}
215
217{
218 checkArraySize("PureFluidPhase::getStandardChemPotentials", mu.size(), 1);
219 mu[0] = gibbs_mole();
220}
221
222void PureFluidPhase::getEnthalpy_RT(span<double> hrt) const
223{
224 checkArraySize("PureFluidPhase::getEnthalpy_RT", hrt.size(), 1);
225 hrt[0] = enthalpy_mole() / RT();
226}
227
228void PureFluidPhase::getEntropy_R(span<double> sr) const
229{
230 checkArraySize("PureFluidPhase::getEntropy_R", sr.size(), 1);
231 sr[0] = entropy_mole() / GasConstant;
232}
233
234void PureFluidPhase::getGibbs_RT(span<double> grt) const
235{
236 checkArraySize("PureFluidPhase::getGibbs_RT", grt.size(), 1);
237 grt[0] = gibbs_mole() / RT();
238}
239
240void PureFluidPhase::getEnthalpy_RT_ref(span<double> hrt) const
241{
242 double rhoSave = density();
243 double t = temperature();
244 double plow = 1.0E-8;
245 Set(tpx::PropertyPair::TP, t, plow);
246 getEnthalpy_RT(hrt);
247 Set(tpx::PropertyPair::TV, t, 1 / rhoSave);
248
249}
250
251void PureFluidPhase::getGibbs_RT_ref(span<double> grt) const
252{
253 double rhoSave = density();
254 double t = temperature();
255 double pref = refPressure();
256 double plow = 1.0E-8;
257 Set(tpx::PropertyPair::TP, t, plow);
258 getGibbs_RT(grt);
259 grt[0] += log(pref/plow);
260 Set(tpx::PropertyPair::TV, t, 1 / rhoSave);
261}
262
263void PureFluidPhase::getGibbs_ref(span<double> g) const
264{
266 g[0] *= RT();
267}
268
269void PureFluidPhase::getEntropy_R_ref(span<double> er) const
270{
271 double rhoSave = density();
272 double t = temperature();
273 double pref = refPressure();
274 double plow = 1.0E-8;
275 Set(tpx::PropertyPair::TP, t, plow);
276 getEntropy_R(er);
277 er[0] -= log(pref/plow);
278 Set(tpx::PropertyPair::TV, t, 1 / rhoSave);
279}
280
282{
283 return m_sub->Tcrit();
284}
285
287{
288 return m_sub->Pcrit();
289}
290
292{
293 return 1.0/m_sub->Vcrit();
294}
295
296double PureFluidPhase::satTemperature(double p) const
297{
298 return m_sub->Tsat(p);
299}
300
301/* The next several functions set the state. They run the Substance::Set
302 * function, followed by setting the state of the ThermoPhase object
303 * to the newly computed temperature and density of the Substance.
304 */
305
306void PureFluidPhase::setState_HP(double h, double p, double tol)
307{
308 Set(tpx::PropertyPair::HP, h, p);
309 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
310}
311
312void PureFluidPhase::setState_UV(double u, double v, double tol)
313{
314 Set(tpx::PropertyPair::UV, u, v);
315 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
316}
317
318void PureFluidPhase::setState_SV(double s, double v, double tol)
319{
320 Set(tpx::PropertyPair::SV, s, v);
321 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
322}
323
324void PureFluidPhase::setState_SP(double s, double p, double tol)
325{
326 Set(tpx::PropertyPair::SP, s, p);
327 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
328}
329
330void PureFluidPhase::setState_ST(double s, double t, double tol)
331{
332 Set(tpx::PropertyPair::ST, s, t);
333 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
334}
335
336void PureFluidPhase::setState_TV(double t, double v, double tol)
337{
338 Set(tpx::PropertyPair::TV, t, v);
339 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
340}
341
342void PureFluidPhase::setState_PV(double p, double v, double tol)
343{
344 Set(tpx::PropertyPair::PV, p, v);
345 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
346}
347
348void PureFluidPhase::setState_UP(double u, double p, double tol)
349{
350 Set(tpx::PropertyPair::UP, u, p);
351 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
352}
353
354void PureFluidPhase::setState_VH(double v, double h, double tol)
355{
356 Set(tpx::PropertyPair::VH, v, h);
357 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
358}
359
360void PureFluidPhase::setState_TH(double t, double h, double tol)
361{
362 Set(tpx::PropertyPair::TH, t, h);
363 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
364}
365
366void PureFluidPhase::setState_SH(double s, double h, double tol)
367{
368 Set(tpx::PropertyPair::SH, s, h);
369 setState_TD(m_sub->Temp(), 1.0/m_sub->v());
370}
371
373{
374 Set(tpx::PropertyPair::TV, t, m_sub->v());
375 return m_sub->Ps();
376}
377
379{
380 return m_sub->x();
381}
382
383void PureFluidPhase::setState_Tsat(double t, double x)
384{
385 Set(tpx::PropertyPair::TX, t, x);
388}
389
390void PureFluidPhase::setState_Psat(double p, double x)
391{
392 Set(tpx::PropertyPair::PX, p, x);
395}
396
397string PureFluidPhase::report(bool show_thermo, double threshold) const
398{
399 fmt::memory_buffer b;
400 // This is the width of the first column of names in the report.
401 int name_width = 18;
402
403 string blank_leader = fmt::format("{:{}}", "", name_width);
404
405 string one_property = fmt::format("{{:>{}}} {{:<.5g}} {{}}\n", name_width);
406
407 constexpr auto two_prop_header = "{} {:^15} {:^15}\n";
408 string kg_kmol_header = fmt::format(
409 two_prop_header, blank_leader, "1 kg", "1 kmol"
410 );
411 string two_prop_sep = fmt::format(
412 "{} {:-^15} {:-^15}\n", blank_leader, "", ""
413 );
414 string two_property = fmt::format(
415 "{{:>{}}} {{:15.5g}} {{:15.5g}} {{}}\n", name_width
416 );
417
418 if (name() != "") {
419 fmt_append(b, "\n {}:\n", name());
420 }
421 fmt_append(b, "\n");
422 fmt_append(b, one_property, "temperature", temperature(), "K");
423 fmt_append(b, one_property, "pressure", pressure(), "Pa");
424 fmt_append(b, one_property, "density", density(), "kg/m^3");
425 fmt_append(b, one_property,
426 "mean mol. weight", meanMolecularWeight(), "kg/kmol");
427 fmt_append(b, "{:>{}} {:<.5g}\n",
428 "vapor fraction", name_width, vaporFraction());
429 fmt_append(b, "{:>{}} {}\n",
430 "phase of matter", name_width, phaseOfMatter());
431
432 if (show_thermo) {
433 fmt_append(b, "\n");
434 fmt_append(b, kg_kmol_header);
435 fmt_append(b, two_prop_sep);
436 fmt_append(b, two_property,
437 "enthalpy", enthalpy_mass(), enthalpy_mole(), "J");
438 fmt_append(b, two_property,
439 "internal energy", intEnergy_mass(), intEnergy_mole(), "J");
440 fmt_append(b, two_property,
441 "entropy", entropy_mass(), entropy_mole(), "J/K");
442 fmt_append(b, two_property,
443 "Gibbs function", gibbs_mass(), gibbs_mole(), "J");
444 fmt_append(b, two_property,
445 "heat capacity c_p", cp_mass(), cp_mole(), "J/K");
446 fmt_append(b, two_property,
447 "heat capacity c_v", cv_mass(), cv_mole(), "J/K");
448 }
449
450 return to_string(b);
451}
452
453}
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 ...
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.