Cantera  2.1.2
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  virtual char* name() = 0;
81 
82  //! Chemical formula for the substance
83  virtual char* formula() = 0;
84  //! @}
85 
86  //! @name Properties
87  //! @{
88 
89  //! Pressure [Pa]. If two phases are present, return the saturation
90  //! pressure; otherwise return the pressure computed directly from the
91  //! underlying eos.
92  double P();
93 
94  //! Temperature [K]
95  double Temp() {
96  return T;
97  }
98 
99  //! Specific volume [m^3/kg]
100  double v() {
101  return prop(propertyFlag::V);
102  }
103 
104  //! Internal energy [J/kg]
105  double u() {
106  return prop(propertyFlag::U);
107  }
108 
109  //! Enthalpy [J/kg]
110  double h() {
111  return prop(propertyFlag::H);
112  }
113 
114  //! Entropy [J/kg/K]
115  double s() {
116  return prop(propertyFlag::S);
117  }
118 
119  //! Helmholtz function [J/kg]
120  double f() {
121  return u() - T*s();
122  }
123 
124  //! Gibbs function [J/kg]
125  double g() {
126  return h() - T*s();
127  }
128 
129  //! Specific heat at constant volume [J/kg/K]
130  virtual double cv() {
131  double Tsave = T, dt = 1.e-4*T;
132  double T1 = std::max(Tmin(), Tsave - dt);
133  double T2 = std::min(Tmax(), Tsave + dt);
134  set_T(T1);
135  double s1 = s();
136  set_T(T2);
137  double s2 = s();
138  set_T(Tsave);
139  return T*(s2 - s1)/(T2-T1);
140  }
141 
142  //! Specific heat at constant pressure [J/kg/K]
143  virtual double cp() {
144  double Tsave = T, dt = 1.e-4*T;
145  double T1 = std::max(Tmin(), Tsave - dt);
146  double T2 = std::min(Tmax(), Tsave + dt);
147  double p0 = P();
148  Set(PropertyPair::TP, T1, p0);
149  double s1 = s();
150  Set(PropertyPair::TP, T2, p0);
151  double s2 = s();
152  Set(PropertyPair::TP, Tsave, p0);
153  return T*(s2 - s1)/(T2-T1);
154  }
155 
156  virtual double thermalExpansionCoeff() {
157  double Tsave = T, dt = 1.e-4*T;
158  double T1 = std::max(Tmin(), Tsave - dt);
159  double T2 = std::min(Tmax(), Tsave + dt);
160  double p0 = P();
161  Set(PropertyPair::TP, T1, p0);
162  double v1 = v();
163  Set(PropertyPair::TP, T2, p0);
164  double v2 = v();
165  Set(PropertyPair::TP, Tsave, p0);
166  return (v2 - v1)/((v2 + v1)*(T2-T1));
167  }
168 
169  virtual double isothermalCompressibility() {
170  double Psave = P(), dp = 1.e-4*Psave;
171  Set(PropertyPair::TP, T, Psave - dp);
172  double v1 = v();
173  Set(PropertyPair::TP, T, Psave + dp);
174  double v2 = v();
175  Set(PropertyPair::TP, T, Psave);
176  return -(v2 - v1)/((v2 + v1)*dp);
177  }
178 
179  //! @}
180  //! @name Saturation Properties
181  //! @{
182 
183  double Ps();
184 
185  //! The derivative of the saturation pressure with respect to temperature.
186  virtual double dPsdT();
187 
188  //! Saturation temperature at pressure *p*.
189  double Tsat(double p);
190 
191  //! Vapor mass fraction. If T >= Tcrit, 0 is returned for v < Vcrit, and 1
192  //! is returned if v > Vcrit.
193  double x();
194 
195  //! Returns 1 if the current state is a liquid/vapor mixture, 0 otherwise
196  int TwoPhase();
197  //! @}
198 
199  virtual double Pp()=0;
200 
201  //! Enthaply of a single-phase state
202  double hp() {
203  return up() + Pp()/Rho;
204  }
205 
206  //! Gibbs function of a single-phase state
207  double gp() {
208  return hp() - T*sp();
209  }
210 
211  double prop(propertyFlag::type ijob);
212 
213  //! set T and P
214  void set_TPp(double t0, double p0);
215 
216  //! Function to set or change the state for a property pair *XY* where
217  //! *x0* is the value of first property and *y0* is the value of the
218  //! second property.
219  void Set(PropertyPair::type XY, double x0, double y0);
220 
221 protected:
222  double T, Rho;
223  double Tslast, Rhf, Rhv;
224  double Pst;
225  double m_energy_offset;
226  double m_entropy_offset;
227  std::string m_name;
228  std::string m_formula;
229 
230  virtual double ldens()=0;
231 
232  //! Saturation pressure, Pa
233  virtual double Psat()=0;
234 
235  //! Internal energy of a single-phase state
236  virtual double up()=0;
237 
238  //! Entropy of a single-phase state
239  virtual double sp()=0;
240 
241  virtual int ideal() {
242  return 0;
243  }
244 
245  double vp() {
246  return 1.0/Rho;
247  }
248 
249  //! Uses the lever rule to set state in the dome. Returns 1 if in dome,
250  //! 0 if not, in which case state not set.
251  int Lever(int itp, double sat, double val, propertyFlag::type ifunc);
252 
253  //! Update saturated liquid and vapor densities and saturation pressure
254  void update_sat();
255 
256 private:
257  void set_Rho(double r0);
258  void set_T(double t0);
259  void set_v(double v0);
260  void BracketSlope(double p);
261  double vprop(propertyFlag::type ijob);
262  void set_xy(propertyFlag::type if1, propertyFlag::type if2,
263  double X, double Y,
264  double atx, double aty, double rtx, double rty);
265 
266  int kbr;
267  double Vmin, Vmax;
268  double Pmin, Pmax;
269  double dvbf, dv;
270  double v_here, P_here;
271 };
272 
273 }
274 
275 #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:35
virtual double sp()=0
Entropy of a single-phase state.
double x()
Vapor mass fraction.
Definition: Sub.cpp:54
int TwoPhase()
Returns 1 if the current state is a liquid/vapor mixture, 0 otherwise.
Definition: Sub.cpp:45
double Tsat(double p)
Saturation temperature at pressure p.
Definition: Sub.cpp:72
double gp()
Gibbs function of a single-phase state.
Definition: Sub.h:207
virtual double Tmin()=0
Minimum temperature for which the equation of state is valid.
double s()
Entropy [J/kg/K].
Definition: Sub.h:115
double v()
Specific volume [m^3/kg].
Definition: Sub.h:100
virtual double cv()
Specific heat at constant volume [J/kg/K].
Definition: Sub.h:130
double f()
Helmholtz function [J/kg].
Definition: Sub.h:120
const doublereal Undef
Fairly random number to be used to initialize variables against to see if they are subsequently defin...
Definition: ct_defs.h:147
void update_sat()
Update saturated liquid and vapor densities and saturation pressure.
Definition: Sub.cpp:292
void set_TPp(double t0, double p0)
set T and P
Definition: Sub.cpp:567
virtual double up()=0
Internal energy of a single-phase state.
virtual char * name()=0
Name of the substance.
double Temp()
Temperature [K].
Definition: Sub.h:95
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:68
double hp()
Enthaply of a single-phase state.
Definition: Sub.h:202
double g()
Gibbs function [J/kg].
Definition: Sub.h:125
virtual double Tmax()=0
Maximum temperature for which the equation of state is valid.
virtual char * formula()=0
Chemical formula for the substance.
virtual double Tcrit()=0
Critical temperature [K].
virtual double cp()
Specific heat at constant pressure [J/kg/K].
Definition: Sub.h:143
virtual double Psat()=0
Saturation pressure, Pa.
double h()
Enthalpy [J/kg].
Definition: Sub.h:110
double u()
Internal energy [J/kg].
Definition: Sub.h:105
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:122
virtual double Pcrit()=0
Critical pressure [Pa].
double P()
Pressure [Pa].
Definition: Sub.cpp:28
int Lever(int itp, double sat, double val, propertyFlag::type ifunc)
Uses the lever rule to set state in the dome.
Definition: Sub.cpp:382
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...