Cantera  3.1.0a1
Sub.h
Go to the documentation of this file.
1 //! @file Sub.h
2 
3 // This file is part of Cantera. See License.txt in the top-level directory or
4 // at https://cantera.org/license.txt for license and copyright information.
5 
6 #ifndef TPX_SUB_H
7 #define TPX_SUB_H
8 
10 
11 namespace tpx
12 {
13 
14 namespace PropertyPair
15 {
16 enum type {
17  TV = 12, HP = 34, SP = 54, PV = 42, TP = 14, UV = 62, ST = 51,
18  SV = 52, UP = 64, VH = 23, TH = 13, SH = 53, PX = 47, TX = 17,
19  VT = -12, PH = -34, PS = -54, VP = -42, PT = -14, VU = -62, TS = -51,
20  VS = -52, PU = -64, HV = -23, HT = -13, HS = -53, XP = -47, XT = -17
21 };
22 }
23 
24 const int Pgiven = 0, Tgiven = 1;
25 
26 namespace propertyFlag
27 {
28 enum type { H, S, U, V, P, T };
29 }
30 
31 const double Undef = 999.1234;
32 
33 /**
34  * Base class from which all pure substances are derived
35  */
36 class Substance
37 {
38 public:
39  Substance() = default;
40 
41  virtual ~Substance() = default;
42 
43  void setStdState(double h0 = 0.0, double s0 = 0.0,
44  double t0 = 298.15, double p0 = 1.01325e5);
45 
46  //! @name Information about a substance
47  //! @{
48 
49  //! Molecular weight [kg/kmol]
50  virtual double MolWt()=0;
51 
52  //! Critical temperature [K]
53  virtual double Tcrit()=0;
54 
55  //! Critical pressure [Pa]
56  virtual double Pcrit()=0;
57 
58  //! Critical specific volume [m^3/kg]
59  virtual double Vcrit()=0;
60 
61  //! Minimum temperature for which the equation of state is valid
62  virtual double Tmin()=0;
63 
64  //! Maximum temperature for which the equation of state is valid
65  virtual double Tmax()=0;
66 
67  //! Name of the substance
68  const char* name() {
69  return m_name.c_str();
70  }
71 
72  //! Chemical formula for the substance
73  const char* formula() {
74  return m_formula.c_str();
75  }
76 
77  //! @}
78 
79  //! @name Properties
80  //! @{
81 
82  //! Pressure [Pa]. If two phases are present, return the saturation
83  //! pressure; otherwise return the pressure computed directly from the
84  //! underlying eos.
85  double P();
86 
87  //! Temperature [K]
88  double Temp() {
89  return T;
90  }
91 
92  //! Specific volume [m^3/kg]
93  double v() {
94  return prop(propertyFlag::V);
95  }
96 
97  //! Internal energy [J/kg]
98  double u() {
99  return prop(propertyFlag::U);
100  }
101 
102  //! Enthalpy [J/kg]
103  double h() {
104  return prop(propertyFlag::H);
105  }
106 
107  //! Entropy [J/kg/K]
108  double s() {
109  return prop(propertyFlag::S);
110  }
111 
112  //! Helmholtz function [J/kg]
113  double f() {
114  return u() - T*s();
115  }
116 
117  //! Gibbs function [J/kg]
118  double g() {
119  return h() - T*s();
120  }
121 
122  //! Specific heat at constant volume [J/kg/K]
123  virtual double cv();
124 
125  //! Specific heat at constant pressure [J/kg/K]
126  virtual double cp();
127 
128  virtual double thermalExpansionCoeff();
129 
130  virtual double isothermalCompressibility();
131 
132  //! @}
133  //! @name Saturation Properties
134  //! @{
135 
136  double Ps();
137 
138  //! The derivative of the saturation pressure with respect to temperature.
139  virtual double dPsdT();
140 
141  //! Saturation temperature at pressure *p*.
142  double Tsat(double p);
143 
144  //! Vapor mass fraction. If T >= Tcrit, 0 is returned for v < Vcrit, and 1
145  //! is returned if v > Vcrit.
146  double x();
147 
148  //! Returns 1 if the current state is a liquid/vapor mixture, 0 otherwise.
149  //! By default, saturated vapor and saturated liquid are included; setting
150  //! the flag *strict* to true will exclude the boundaries.
151  int TwoPhase(bool strict=false);
152  //! @}
153 
154  virtual double Pp()=0;
155 
156  //! Enthalpy of a single-phase state
157  double hp() {
158  return up() + Pp()/Rho;
159  }
160 
161  //! Gibbs function of a single-phase state
162  double gp() {
163  return hp() - T*sp();
164  }
165 
166  double prop(propertyFlag::type ijob);
167 
168  //! set T and P
169  void set_TPp(double t0, double p0);
170 
171  //! Function to set or change the state for a property pair *XY* where
172  //! *x0* is the value of first property and *y0* is the value of the
173  //! second property.
174  void Set(PropertyPair::type XY, double x0, double y0);
175 
176 protected:
177  double T = Undef;
178  double Rho = Undef;
179  double Tslast = Undef;
180  double Rhf = Undef;
181  double Rhv = Undef;
182  double Pst = Undef;
183  double m_energy_offset = 0.0;
184  double m_entropy_offset = 0.0;
185  std::string m_name;
186  std::string m_formula;
187 
188  virtual double ldens()=0;
189 
190  //! Saturation pressure, Pa
191  virtual double Psat()=0;
192 
193  //! Internal energy of a single-phase state
194  virtual double up()=0;
195 
196  //! Entropy of a single-phase state
197  virtual double sp()=0;
198 
199  virtual int ideal() {
200  return 0;
201  }
202 
203  double vp() {
204  return 1.0/Rho;
205  }
206 
207  //! Uses the lever rule to set state in the dome. Returns 1 if in dome,
208  //! 0 if not, in which case state not set.
209  int Lever(int itp, double sat, double val, propertyFlag::type ifunc);
210 
211  //! Update saturated liquid and vapor densities and saturation pressure
212  void update_sat();
213 
214 private:
215  void set_Rho(double r0);
216  void set_T(double t0);
217  void set_v(double v0);
218  void BracketSlope(double p);
219  double vprop(propertyFlag::type ijob);
220  void set_xy(propertyFlag::type if1, propertyFlag::type if2,
221  double X, double Y,
222  double atx, double aty, double rtx, double rty);
223 
224  int kbr = 0;
225  double Vmin, Vmax;
226  double Pmin, Pmax;
227  double dvbf, dv;
228  double v_here, P_here;
229 };
230 
231 }
232 
233 #endif
Base class from which all pure substances are derived.
Definition: Sub.h:37
virtual double up()=0
Internal energy of a single-phase state.
virtual double Vcrit()=0
Critical specific volume [m^3/kg].
double hp()
Enthalpy of a single-phase state.
Definition: Sub.h:157
int TwoPhase(bool strict=false)
Returns 1 if the current state is a liquid/vapor mixture, 0 otherwise.
Definition: Sub.cpp:250
virtual double cp()
Specific heat at constant pressure [J/kg/K].
Definition: Sub.cpp:80
const char * name()
Name of the substance.
Definition: Sub.h:68
double gp()
Gibbs function of a single-phase state.
Definition: Sub.h:162
double x()
Vapor mass fraction.
Definition: Sub.cpp:262
virtual double Tmax()=0
Maximum temperature for which the equation of state is valid.
double u()
Internal energy [J/kg].
Definition: Sub.h:98
virtual double MolWt()=0
Molecular weight [kg/kmol].
double h()
Enthalpy [J/kg].
Definition: Sub.h:103
virtual double cv()
Specific heat at constant volume [J/kg/K].
Definition: Sub.cpp:42
double Temp()
Temperature [K].
Definition: Sub.h:88
virtual double Tmin()=0
Minimum temperature for which the equation of state is valid.
int Lever(int itp, double sat, double val, propertyFlag::type ifunc)
Uses the lever rule to set state in the dome.
Definition: Sub.cpp:577
double g()
Gibbs function [J/kg].
Definition: Sub.h:118
double P()
Pressure [Pa].
Definition: Sub.cpp:35
virtual double sp()=0
Entropy of a single-phase state.
virtual double Psat()=0
Saturation pressure, Pa.
double f()
Helmholtz function [J/kg].
Definition: Sub.h:113
double v()
Specific volume [m^3/kg].
Definition: Sub.h:93
virtual double Tcrit()=0
Critical temperature [K].
void Set(PropertyPair::type XY, double x0, double y0)
Function to set or change the state for a property pair XY where x0 is the value of first property an...
Definition: Sub.cpp:333
virtual double Pcrit()=0
Critical pressure [Pa].
virtual double dPsdT()
The derivative of the saturation pressure with respect to temperature.
Definition: Sub.cpp:233
double Tsat(double p)
Saturation temperature at pressure p.
Definition: Sub.cpp:280
void update_sat()
Update saturated liquid and vapor densities and saturation pressure.
Definition: Sub.cpp:495
void set_TPp(double t0, double p0)
set T and P
Definition: Sub.cpp:768
const char * formula()
Chemical formula for the substance.
Definition: Sub.h:73
double s()
Entropy [J/kg/K].
Definition: Sub.h:108
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
const double Undef
Fairly random number to be used to initialize variables against to see if they are subsequently defin...
Definition: ct_defs.h:164