Cantera  2.0
Sub.h
1 /*
2  * This is the base substance class from which all substances are derived
3  *
4  * Kate Talmazan: SURF -- July, 1995
5  * original implementation of this class and all derived classes from
6  * formulas given in TPSI. Implementation of P(Rho, T), cv0(T), ldens(T),
7  * and Psat(T) for all substances in TPSI.f
8  *
9  * Dave Goodwin: Fall, 1996
10  * functions for u, h, s, f, g;
11  * functions to set state
12  * error handling
13  * documentation
14  *
15  * Sept., 2001: minor modifications to use with Cantera
16  *
17  */
18 
19 #ifndef TPX_SUB_H
20 #define TPX_SUB_H
21 
22 #include <iostream>
23 #include <string>
24 
25 namespace tpx
26 {
27 
28 class TPX_Error
29 {
30 public:
31  TPX_Error(std::string p, std::string e) {
32  ErrorMessage = e;
33  ErrorProcedure = p;
34  }
35  virtual ~TPX_Error() {}
36  static std::string ErrorMessage;
37  static std::string ErrorProcedure;
38 };
39 
40 
41 const double OneAtm = 1.01325e5;
42 const double Liquid = 0.0;
43 const double Vapor = 1.0;
44 
45 const int TV = 12, HP = 34, SP = 54, PV = 42, TP = 14, UV = 62,
46  ST = 51, SV = 52, UP = 64, VH = 23, TH = 13, SH = 53,
47  PX = 47, TX = 17;
48 
49 const int VT = -12, PH = -34, PS = -54, VP = -42, PT = -14, VU = -62,
50  TS = -51, VS = -52, PU = -64, HV = -23, HT = -13, HS = -53,
51  XP = -47, XT = -17;
52 
53 const int NoConverge = -900;
54 const int GenError = -901;
55 const int InvalidInput = -902;
56 const int TempError = -800;
57 const int PresError = -801;
58 const int CKError = -802;
59 
60 
61 const int Pgiven = 0, Tgiven = 1;
62 
63 const int EvalH = 3;
64 const int EvalS = 5;
65 const int EvalU = 6;
66 const int EvalV = 2;
67 const int EvalP = 4;
68 const int EvalT = 1;
69 const int EvalX = 7;
70 const int EvalF = 8;
71 const int EvalG = 9;
72 const int EvalC = 10;
73 const int EvalM = 11;
74 const int EvalN = 12;
75 const int EvalMW = 13;
76 const int EvalEA = 14;
77 const int EvalCdot = 15;
78 const int EvalDdot = 16;
79 const int EvalWdot = 17;
80 const int EvalTchem = 18;
81 const int EvalRgas = 19;
82 
83 const double Undef = 999.1234;
84 
85 std::string errorMsg(int flag);
86 
87 class Substance
88 {
89 public:
90  Substance();
91 
92  virtual ~Substance() {}
93 
94  void setStdState(double h0 = 0.0, double s0 = 0.0,
95  double t0 = 298.15, double p0 = 1.01325e5) {
96  Set(TP, t0, p0);
97  double hh = h();
98  double ss = s();
99  double hoff = h0 - hh;
100  double soff = s0 - ss;
101  m_entropy_offset += soff;
102  m_energy_offset += hoff;
103  }
104 
105  // information about a substance:
106 
107  virtual double MolWt()=0; // molecular weight, kg/kmol
108  virtual double Tcrit()=0; // critical temperature, K
109  virtual double Pcrit()=0; // critical pressure, Pa
110  virtual double Vcrit()=0; // critical specific vol, m^3/kg
111  virtual double Tmin()=0; // min. temp for which equations valid
112  virtual double Tmax()=0; // max. temp for which equations valid
113  virtual char* name() = 0; // name
114  virtual char* formula() = 0; // chemical formula
115 
116  // properties:
117 
118  double P(); // pressure, Pa
119  double Temp() {
120  return T; // temperature, K
121  }
122  double v() { // specific vol, m^3/kg
123  return prop(EvalV);
124  }
125  double u() { // int. energy, J/kg
126  return prop(EvalU);
127  }
128  double h() { // enthalpy, J/kg
129  return prop(EvalH);
130  }
131  double s() { // entropy, J/kg/K
132  return prop(EvalS);
133  }
134  double f() { // Helmholtz function, J/kg
135  return u() - T*s();
136  }
137  double g() { // Gibbs function, J/kg
138  return h() - T*s();
139  }
140 
141  virtual double cv() {
142  double Tsave = T, dt = 1.e-4*T;
143  set_T(Tsave - dt);
144  double s1 = s();
145  set_T(Tsave + dt);
146  double s2 = s();
147  set_T(Tsave);
148  return T*(s2 - s1)/(2.0*dt);
149  }
150 
151  virtual double cp() {
152  double Tsave = T, dt = 1.e-4*T;
153  double p0 = P();
154  Set(TP, Tsave - dt, p0);
155  double s1 = s();
156  Set(TP, Tsave + dt, p0);
157  double s2 = s();
158  Set(TP, Tsave, p0);
159  return T*(s2 - s1)/(2.0*dt);
160  }
161 
162  virtual double thermalExpansionCoeff() {
163  double Tsave = T, dt = 1.e-4*T;
164  double p0 = P();
165  Set(TP, Tsave - dt, p0);
166  double v1 = v();
167  Set(TP, Tsave + dt, p0);
168  double v2 = v();
169  Set(TP, Tsave, p0);
170  return (v2 - v1)/((v2 + v1)*dt);
171  }
172 
173  virtual double isothermalCompressibility() {
174  double Psave = P(), dp = 1.e-4*Psave;
175  Set(TP, T, Psave - dp);
176  double v1 = v();
177  Set(TP, T, Psave + dp);
178  double v2 = v();
179  Set(TP, T, Psave);
180  return -(v2 - v1)/((v2 + v1)*dp);
181  }
182 
183 
184  // saturation properties
185 
186  double Ps();
187  virtual double dPsdT(); // d(Psat)/dT, Pa/K
188  double Tsat(double p); // saturation temp at p
189  double x(); // vapor mass fraction
190  int TwoPhase(); // =1 if vapor/liquid, 0 otherwise
191  virtual double Pp()=0;
192  double hp() {
193  return up() + Pp()/Rho;
194  }
195  double gp() {
196  return hp() - T*sp();
197  }
198 
199  double prop(int ijob);
200  void set_TPp(double t0, double p0); // set T and P
201 
202 
203  // functions to set or change state:
204 
205  void Set(int XY, double x0, double y0);
206  void Set_meta(double phase, double pp);
207 
208  int Error() {
209  return Err;
210  }
211 
212 
213 protected:
214 
215  double T, Rho;
216  double Tslast, Rhf, Rhv;
217  double Pst;
218  int Err;
219  double m_energy_offset;
220  double m_entropy_offset;
221  std::string m_name;
222  std::string m_formula;
223 
224  //virtual double Xm(int k) { return 1.0;}
225  //virtual int Species() { return 1;}
226 
227  virtual double ldens()=0;
228  virtual double Psat()=0; // saturation pressure, Pa
229  virtual double up()=0;
230  virtual double sp()=0;
231  virtual int ideal() {
232  return 0; // added 9/2/98; default is false
233  }
234  double vp() {
235  return 1.0/Rho;
236  }
237  int Lever(int itp, double sat, double val, int ifunc);
238  void update_sat();
239 
240  void set_Err(int ErrFlag) {
241  if (!Err) {
242  Err = ErrFlag;
243  //throw TPX_Error(""errorMsg(Err));
244  }
245  }
246  void clear_Err() {
247  Err = 0;
248  }
249 
250 
251 private:
252  void set_Rho(double r0);
253  void set_T(double t0);
254  void set_v(double v0);
255  void BracketSlope(double p);
256  double lprop(int ijob);
257  double vprop(int ijob);
258  void set_xy(int if1, int if2, double X, double Y,
259  double atx, double aty, double rtx, double rty);
260 
261  int kbr;
262  double Vmin, Vmax;
263  double Pmin, Pmax;
264  double dvbf, dv;
265  double v_here, P_here;
266 };
267 
268 void Error(char* message, int flag, double val=Undef);
269 void Mess(char* message);
270 
271 }
272 
273 
274 #endif