/[ascend]/trunk/models/johnpye/fprops/helmholtz.c
ViewVC logotype

Contents of /trunk/models/johnpye/fprops/helmholtz.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1865 - (show annotations) (download) (as text)
Mon Sep 15 08:40:14 2008 UTC (11 years, 9 months ago) by jpye
File MIME type: text/x-csrc
File size: 13268 byte(s)
Still working on fixing helmholtz_a.
1 /* ASCEND modelling environment
2 Copyright (C) 2008 Carnegie Mellon University
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 *//** @file
19 Implementation of the reduced molar Helmholtz free energy equation of state.
20
21 For nomenclature see Tillner-Roth, Harms-Watzenberg and Baehr, Eine neue
22 Fundamentalgleichung f端r Ammoniak.
23
24 John Pye, 29 Jul 2008.
25 */
26
27 #include <math.h>
28
29 #include "helmholtz.h"
30 #include "ideal_impl.h"
31
32 #ifdef TEST
33 #include <assert.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #endif
37
38 /* forward decls */
39
40 static double helm_resid(double tau, double delta, const HelmholtzData *data);
41 static double helm_resid_del(double tau, double delta, const HelmholtzData *data);
42 static double helm_resid_tau(double tau, double delta, const HelmholtzData *data);
43 static double helm_resid_deltau(double tau, double delta, const HelmholtzData *data);
44 static double helm_resid_deldel(double tau, double delta, const HelmholtzData *data);
45
46 /**
47 Function to calculate pressure from Helmholtz free energy EOS, given temperature
48 and mass density.
49
50 @param T temperature in K
51 @param rho mass density in kg/m続
52 @return pressure in Pa???
53 */
54 double helmholtz_p(double T, double rho, const HelmholtzData *data){
55
56 double tau = data->T_star / T;
57 double delta = rho / data->rho_star;
58
59 #ifdef TEST
60 assert(data->rho_star!=0);
61 assert(T!=0);
62 assert(!isnan(tau));
63 assert(!isnan(delta));
64 assert(!isnan(data->R));
65
66 //fprintf(stderr,"p calc: T = %f\n",T);
67 //fprintf(stderr,"p calc: tau = %f\n",tau);
68 //fprintf(stderr,"p calc: rho = %f\n",rho);
69 //fprintf(stderr,"p calc: delta = %f\n",delta);
70 //fprintf(stderr,"p calc: R*T*rho = %f\n",data->R * T * rho);
71
72 //fprintf(stderr,"T = %f\n", T);
73 //fprintf(stderr,"rhob = %f, rhob* = %f, delta = %f\n", rho/data->M, data->rho_star/data->M, delta);
74 #endif
75
76 return data->R * T * rho * (1 + delta * helm_resid_del(tau,delta,data));
77 }
78
79 /**
80 Function to calculate internal energy from Helmholtz free energy EOS, given
81 temperature and mass density.
82
83 @param T temperature in K
84 @param rho mass density in kg/m続
85 @return internal energy in ???
86 */
87 double helmholtz_u(double T, double rho, const HelmholtzData *data){
88
89 double tau = data->T_star / T;
90 double delta = rho / data->rho_star;
91
92 #ifdef TEST
93 assert(data->rho_star!=0);
94 assert(T!=0);
95 assert(!isnan(tau));
96 assert(!isnan(delta));
97 assert(!isnan(data->R));
98 #endif
99
100 #if 0
101 fprintf(stderr,"ideal_tau = %f\n",helm_ideal_tau(tau,delta,data->ideal));
102 fprintf(stderr,"resid_tau = %f\n",helm_resid_tau(tau,delta,data));
103 fprintf(stderr,"R T = %f\n",data->R * data->T_star);
104 #endif
105
106 return data->R * data->T_star * (helm_ideal_tau(tau,delta,data->ideal) + helm_resid_tau(tau,delta,data));
107 }
108
109 /**
110 Function to calculate enthalpy from Helmholtz free energy EOS, given
111 temperature and mass density.
112
113 @param T temperature in K
114 @param rho mass density in kg/m続
115 @return enthalpy in J/kg
116 */
117 double helmholtz_h(double T, double rho, const HelmholtzData *data){
118
119 double tau = data->T_star / T;
120 double delta = rho / data->rho_star;
121
122 #ifdef TEST
123 assert(data->rho_star!=0);
124 assert(T!=0);
125 assert(!isnan(tau));
126 assert(!isnan(delta));
127 assert(!isnan(data->R));
128 #endif
129
130 return data->R * T * (1 + tau * (helm_ideal_tau(tau,delta,data->ideal) + helm_resid_tau(tau,delta,data)) + delta*helm_resid_del(tau,delta,data));
131 }
132
133 /**
134 Function to calculate entropy from Helmholtz free energy EOS, given
135 temperature and mass density.
136
137 @param T temperature in K
138 @param rho mass density in kg/m続
139 @return entropy in J/kgK
140 */
141 double helmholtz_s(double T, double rho, const HelmholtzData *data){
142
143 double tau = data->T_star / T;
144 double delta = rho / data->rho_star;
145
146 #ifdef TEST
147 assert(data->rho_star!=0);
148 assert(T!=0);
149 assert(!isnan(tau));
150 assert(!isnan(delta));
151 assert(!isnan(data->R));
152
153 fprintf(stderr,"helm_ideal_tau = %f\n",helm_ideal_tau(tau,delta,data->ideal));
154 fprintf(stderr,"helm_resid_tau = %f\n",helm_resid_tau(tau,delta,data));
155 fprintf(stderr,"helm_ideal = %f\n",helm_ideal(tau,delta,data->ideal));
156 fprintf(stderr,"helm_resid = %f\n",helm_resid(tau,delta,data));
157 #endif
158 return data->R * (
159 tau * (helm_ideal_tau(tau,delta,data->ideal) + helm_resid_tau(tau,delta,data))
160 - helm_ideal(tau,delta,data->ideal) - helm_resid(tau,delta,data)
161 );
162 }
163
164 /**
165 Function to calculate Helmholtz energy from the Helmholtz free energy EOS,
166 given temperature and mass density.
167
168 @param T temperature in K
169 @param rho mass density in kg/m続
170 @return Helmholtz energy 'a', in J/kg
171 */
172 double helmholtz_a(double T, double rho, const HelmholtzData *data){
173
174 double tau = data->T_star / T;
175 double delta = rho / data->rho_star;
176
177 #ifdef TEST
178 assert(data->rho_star!=0);
179 assert(T!=0);
180 assert(!isnan(tau));
181 assert(!isnan(delta));
182 assert(!isnan(data->R));
183 #endif
184
185 #ifdef TEST
186 fprintf(stderr,"helmholtz_a: T = %f, rho = %f\n",T,rho);
187 fprintf(stderr,"multiplying by RT = %f\n",data->R*T);
188 #endif
189
190 return data->R * T * (helm_ideal(tau,delta,data->ideal) - helm_resid(tau,delta,data));
191 }
192
193
194 /**
195 Calculation zero-pressure specific heat capacity
196 */
197 double helmholtz_cp0(double T, const HelmholtzData *data){
198 double val = helm_cp0(T,data->ideal);
199 #if 0
200 fprintf(stderr,"val = %f\n",val);
201 #endif
202 return val;
203 }
204
205 /*---------------------------------------------
206 UTILITY FUNCTION(S)
207 */
208
209 /* ipow: public domain by Mark Stephen with suggestions by Keiichi Nakasato */
210 static double ipow(double x, int n){
211 double t = 1.0;
212
213 if(!n)return 1.0; /* At the top. x^0 = 1 */
214
215 if (n < 0){
216 n = -n;
217 x = 1.0/x; /* error if x == 0. Good */
218 } /* ZTC/SC returns inf, which is even better */
219
220 if (x == 0.0)return 0.0;
221
222 do{
223 if(n & 1)t *= x;
224 n /= 2; /* KN prefers if (n/=2) x*=x; This avoids an */
225 x *= x; /* unnecessary but benign multiplication on */
226 }while(n); /* the last pass, but the comparison is always
227 true _except_ on the last pass. */
228
229 return t;
230 }
231
232 /**
233 Residual part of helmholtz function.
234 */
235 double helm_resid(double tau, double delta, const HelmholtzData *data){
236 double dell,ldell, sum, res = 0;
237 unsigned n, i;
238 const HelmholtzPowTerm *pt;
239 const HelmholtzExpTerm *et;
240
241 n = data->np;
242 pt = &(data->pt[0]);
243
244 /* power terms */
245 sum = 0;
246 dell = ipow(delta,pt->l);
247 ldell = pt->l * dell;
248 unsigned oldl;
249 for(i=0; i<n; ++i){
250 sum += pt->a * pow(tau, pt->t) * ipow(delta, pt->d);
251 fprintf(stderr,"i = %d, sum = %f\n",i,sum);
252 oldl = pt->l;
253 ++pt;
254 if(i+1==n || oldl != pt->l){
255 if(oldl == 0){
256 fprintf(stderr,"linear ");
257 res += sum;
258 }else{
259 fprintf(stderr,"exp dell=%f, exp(-dell)=%f sum=%f: ",dell,exp(-dell),sum);
260 res += sum * exp(-dell);
261 }
262 fprintf(stderr,"i = %d, res = %f\n",i,res);
263 sum = 0;
264 dell = ipow(delta,pt->l);
265 ldell = pt->l*dell;
266 }
267 }
268
269 #if 0
270 /* now the exponential terms */
271 n = data->ne;
272 et = &(data->et[0]);
273 for(i=0; i< n; ++i){
274 fprintf(stderr,"i = %d, a = %e, t = %f, d = %d, phi = %d, beta = %d, gamma = %f\n",i+1, et->a, et->t, et->d, et->phi, et->beta, et->gamma);
275
276 double e1 = -et->phi * delta*delta
277 + 2 * et->phi * delta
278 - et->beta * tau * tau
279 + 2 * et->beta * et->gamma * tau
280 - et->phi
281 - et->beta * et->gamma * et->gamma;
282 sum = et->a * pow(tau,et->t) * ipow(delta,et->d) * exp(e1);
283 //fprintf(stderr,"sum = %f\n",sum);
284 res += sum;
285 ++et;
286 }
287 #endif
288
289 #ifdef TEST
290 fprintf(stderr,"phir = %f\n",res);
291 #endif
292 return res;
293 }
294
295 /**
296 Derivative of the helmholtz residual function with respect to
297 delta.
298 */
299 double helm_resid_del(double tau,double delta, const HelmholtzData *data){
300 double sum, res = 0;
301 double dell, ldell;
302 unsigned n, i;
303 const HelmholtzPowTerm *pt;
304 const HelmholtzExpTerm *et;
305
306 n = data->np;
307 pt = &(data->pt[0]);
308
309 sum = 0;
310 dell = ipow(delta,pt->l);
311 ldell = pt->l * dell;
312 unsigned oldl;
313 for(i=0; i<n; ++i){
314 sum += pt->a * pow(tau, pt->t) * ipow(delta, pt->d - 1) * (pt->d - ldell);
315 oldl = pt->l;
316 ++pt;
317 if(i+1==n || oldl != pt->l){
318 if(oldl == 0){
319 res += sum;
320 }else{
321 res += sum * exp(-dell);
322 }
323 sum = 0;
324 dell = ipow(delta,pt->l);
325 ldell = pt->l*dell;
326 }
327 }
328
329 #if 1
330 /* now the exponential terms */
331 n = data->ne;
332 et = &(data->et[0]);
333 for(i=0; i< n; ++i){
334 //fprintf(stderr,"i = %d, a = %e, t = %f, d = %d, phi = %d, beta = %d, gamma = %f\n",i+1, et->a, et->t, et->d, et->phi, et->beta, et->gamma);
335
336 double del2 = delta*delta;
337 double tau2 = tau*tau;
338 double gam2 = et->gamma * et->gamma;
339 double e1 = -et->phi * del2
340 + 2 * et->phi * delta
341 - et->beta * tau2
342 + 2 * et->beta * et->gamma * tau
343 - et->phi
344 - et->beta * gam2;
345 sum = -et->a * pow(tau,et->t) * ipow(delta,et->d-1)
346 * (2 * et->phi * del2 - 2 * et->phi * delta - et->d)
347 * exp(e1);
348 //fprintf(stderr,"sum = %f\n",sum);
349 res += sum;
350 ++et;
351 }
352 #endif
353
354 return res;
355 }
356
357 /**
358 Derivative of the helmholtz residual function with respect to
359 tau.
360 */
361 double helm_resid_tau(double tau,double delta,const HelmholtzData *data){
362
363 double sum;
364 double res = 0;
365 double delX;
366 unsigned l;
367 unsigned n, i;
368 const HelmholtzPowTerm *pt;
369 const HelmholtzExpTerm *et;
370
371 n = data->np;
372 pt = &(data->pt[0]);
373
374 delX = 1;
375
376 l = 0;
377 sum = 0;
378 for(i=0; i<n; ++i){
379 if(pt->t){
380 //fprintf(stderr,"i = %d, a = %e, t = %f, d = %d, l = %d\n",i+1, pt->a, pt->t, pt->d, pt->l);
381 sum += pt->a * pow(tau, pt->t - 1) * ipow(delta, pt->d) * pt->t;
382 }
383 ++pt;
384 //fprintf(stderr,"l = %d\n",l);
385 if(i+1==n || l != pt->l){
386 if(l==0){
387 //fprintf(stderr,"Adding non-exp term\n");
388 res += sum;
389 }else{
390 //fprintf(stderr,"Adding exp term with l = %d, delX = %e\n",l,delX);
391 res += sum * exp(-delX);
392 }
393 /* set l to new value */
394 if(i+1!=n){
395 l = pt->l;
396 //fprintf(stderr,"New l = %d\n",l);
397 delX = ipow(delta,l);
398 sum = 0;
399 }
400 }
401 }
402
403 #if 1
404 /* now the exponential terms */
405 n = data->ne;
406 et = &(data->et[0]);
407 for(i=0; i< n; ++i){
408 //fprintf(stderr,"i = %d, a = %e, t = %f, d = %d, phi = %d, beta = %d, gamma = %f\n",i+1, et->a, et->t, et->d, et->phi, et->beta, et->gamma);
409
410 double tau2 = tau*tau;
411 double del2 = delta*delta;
412 double gam2 = et->gamma * et->gamma;
413 double e1 = -et->phi * del2
414 + 2 * et->phi * delta
415 - et->beta * tau2
416 + 2 * et->beta * et->gamma * tau
417 - et->phi
418 - et->beta * gam2;
419 sum = -et->a * pow(tau,et->t - 1) * ipow(delta,et->d)
420 * (2 * et->beta * tau2 - 2 * et->beta * et->gamma * tau - et->t)
421 * exp(e1);
422 //fprintf(stderr,"sum = %f\n",sum);
423 res += sum;
424 ++et;
425 }
426 #endif
427
428 return res;
429 }
430
431
432
433 /**
434 Mixed derivative of the helmholtz residual function with respect to
435 delta and tau
436 */
437 double helm_resid_deltau(double tau,double delta,const HelmholtzData *data){
438
439 double sum;
440 double phir = 0;
441 unsigned i;
442 double XdelX;
443
444 const HelmholtzPowTerm *pt = &(data->pt[0]);
445
446 for(i=0; i<5; ++i){
447 phir += pt->a * pow(tau, pt->t - 1) * ipow(delta, pt->d - 1) * pt->d * pt->t;
448 ++pt;
449 }
450
451 sum = 0;
452 XdelX = delta;
453 for(i=5; i<10; ++i){
454 sum += pt->a * pow(tau, pt->t - 1) * ipow(delta, pt->d - 1) * pt->t *(pt->d - XdelX);
455 ++pt;
456 }
457 phir += exp(-delta) * sum;
458
459 sum = 0;
460 XdelX = 2*delta*delta;
461 for(i=10; i<17; ++i){
462 sum += pt->a * pow(tau, pt->t - 1) * ipow(delta, pt->d - 1) * pt->t *(pt->d - XdelX);
463 ++pt;
464 }
465 phir += exp(-delta*delta) * sum;
466
467 sum = 0;
468 XdelX = 3*delta*delta*delta;
469 for(i=17; i<21; ++i){
470 sum += pt->a * pow(tau, pt->t - 1) * ipow(delta, pt->d - 1) * pt->t *(pt->d - XdelX);
471 ++pt;
472 }
473 phir += exp(-delta*delta*delta) * sum;
474
475 return phir;
476 }
477
478 #define SQ(X) ((X)*(X))
479
480 /**
481 Second derivative of helmholtz residual function with respect to
482 delta (twice).
483 */
484 double helm_resid_deldel(double tau,double delta,const HelmholtzData *data){
485
486 double sum;
487 double phir = 0;
488 unsigned i;
489 unsigned X;
490 double XdelX;
491
492 const HelmholtzPowTerm *pt = &(data->pt[0]);
493
494 for(i=0; i<5; ++i){
495 phir += pt->a * pow(tau, pt->t) * ipow(delta, pt->d - 2) * (SQ(pt->d) - X);
496 ++pt;
497 }
498
499 sum = 0;
500 X = 1;
501 XdelX = delta;
502 for(i=5; i<10; ++i){
503 sum += pt->a * pow(tau, pt->t) * ipow(delta, pt->d - 2) * (SQ(XdelX) - X*XdelX - 2*pt->d*XdelX + XdelX + SQ(pt->d) - pt->d);
504 ++pt;
505 }
506 phir += exp(-delta) * sum;
507
508 sum = 0;
509 X = 2;
510 XdelX = 2*delta*delta;
511 for(i=10; i<17; ++i){
512 sum += pt->a * pow(tau, pt->t) * ipow(delta, pt->d - 2) * (SQ(XdelX) - X*XdelX - 2*pt->d*XdelX + XdelX + SQ(pt->d) - pt->d);
513 ++pt;
514 }
515 phir += exp(-delta*delta) * sum;
516
517 sum = 0;
518 X = 3;
519 XdelX = 3*delta*delta*delta;
520 for(i=17; i<21; ++i){
521 sum += pt->a * pow(tau, pt->t) * ipow(delta, pt->d - 2) * (SQ(XdelX) - X*XdelX - 2*pt->d*XdelX + XdelX + SQ(pt->d) - pt->d);
522 ++pt;
523 }
524 phir += exp(-delta*delta*delta) * sum;
525
526 return phir;
527 }
528

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22