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