/[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 1870 - (show annotations) (download) (as text)
Wed Sep 17 06:45:10 2008 UTC (15 years, 9 months ago) by jpye
File MIME type: text/x-csrc
File size: 13684 byte(s)
Fixed error in 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 ENTROPY_DEBUG
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 HELMHOLTZ_DEBUG
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, term, 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 #ifdef RESID_DEBUG
245 fprintf(stderr,"tau=%f, del=%f\n",tau,delta);
246 #endif
247
248 /* power terms */
249 sum = 0;
250 dell = ipow(delta,pt->l);
251 ldell = pt->l * dell;
252 unsigned oldl;
253 for(i=0; i<n; ++i){
254 term = pt->a * pow(tau, pt->t) * ipow(delta, pt->d);
255 sum += term;
256 #ifdef RESID_DEBUG
257 fprintf(stderr,"i = %d, a=%e, t=%f, d=%d, term = %f, sum = %f",i,pt->a,pt->t,pt->d,term,sum);
258 if(pt->l==0){
259 fprintf(stderr,",row=%e\n",term);
260 }else{
261 fprintf(stderr,",row=%e\n,",term*exp(-dell));
262 }
263 #endif
264 oldl = pt->l;
265 ++pt;
266 if(i+1==n || oldl != pt->l){
267 if(oldl == 0){
268 #ifdef RESID_DEBUG
269 fprintf(stderr,"linear ");
270 #endif
271 res += sum;
272 }else{
273 #ifdef RESID_DEBUG
274 fprintf(stderr,"exp dell=%f, exp(-dell)=%f sum=%f: ",dell,exp(-dell),sum);
275 #endif
276 res += sum * exp(-dell);
277 }
278 #ifdef RESID_DEBUG
279 fprintf(stderr,"i = %d, res = %f\n",i,res);
280 #endif
281 sum = 0;
282 dell = ipow(delta,pt->l);
283 ldell = pt->l*dell;
284 }
285 }
286
287 #if 1
288 /* now the exponential terms */
289 n = data->ne;
290 et = &(data->et[0]);
291 for(i=0; i< n; ++i){
292 #ifdef RESID_DEBUG
293 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);
294 #endif
295 double e1 = -et->phi * delta*delta
296 + 2 * et->phi * delta
297 - et->beta * tau * tau
298 + 2 * et->beta * et->gamma * tau
299 - et->phi
300 - et->beta * et->gamma * et->gamma;
301 sum = et->a * pow(tau,et->t) * ipow(delta,et->d) * exp(e1);
302 //fprintf(stderr,"sum = %f\n",sum);
303 res += sum;
304 ++et;
305 }
306 #endif
307
308 #ifdef RESID_DEBUG
309 fprintf(stderr,"phir = %f\n",res);
310 #endif
311 return res;
312 }
313
314 /**
315 Derivative of the helmholtz residual function with respect to
316 delta.
317 */
318 double helm_resid_del(double tau,double delta, const HelmholtzData *data){
319 double sum, res = 0;
320 double dell, ldell;
321 unsigned n, i;
322 const HelmholtzPowTerm *pt;
323 const HelmholtzExpTerm *et;
324
325 n = data->np;
326 pt = &(data->pt[0]);
327
328 sum = 0;
329 dell = ipow(delta,pt->l);
330 ldell = pt->l * dell;
331 unsigned oldl;
332 for(i=0; i<n; ++i){
333 sum += pt->a * pow(tau, pt->t) * ipow(delta, pt->d - 1) * (pt->d - ldell);
334 oldl = pt->l;
335 ++pt;
336 if(i+1==n || oldl != pt->l){
337 if(oldl == 0){
338 res += sum;
339 }else{
340 res += sum * exp(-dell);
341 }
342 sum = 0;
343 dell = ipow(delta,pt->l);
344 ldell = pt->l*dell;
345 }
346 }
347
348 #if 1
349 /* now the exponential terms */
350 n = data->ne;
351 et = &(data->et[0]);
352 for(i=0; i< n; ++i){
353 //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);
354
355 double del2 = delta*delta;
356 double tau2 = tau*tau;
357 double gam2 = et->gamma * et->gamma;
358 double e1 = -et->phi * del2
359 + 2 * et->phi * delta
360 - et->beta * tau2
361 + 2 * et->beta * et->gamma * tau
362 - et->phi
363 - et->beta * gam2;
364 sum = -et->a * pow(tau,et->t) * ipow(delta,et->d-1)
365 * (2 * et->phi * del2 - 2 * et->phi * delta - et->d)
366 * exp(e1);
367 //fprintf(stderr,"sum = %f\n",sum);
368 res += sum;
369 ++et;
370 }
371 #endif
372
373 return res;
374 }
375
376 /**
377 Derivative of the helmholtz residual function with respect to
378 tau.
379 */
380 double helm_resid_tau(double tau,double delta,const HelmholtzData *data){
381
382 double sum;
383 double res = 0;
384 double delX;
385 unsigned l;
386 unsigned n, i;
387 const HelmholtzPowTerm *pt;
388 const HelmholtzExpTerm *et;
389
390 n = data->np;
391 pt = &(data->pt[0]);
392
393 delX = 1;
394
395 l = 0;
396 sum = 0;
397 for(i=0; i<n; ++i){
398 if(pt->t){
399 //fprintf(stderr,"i = %d, a = %e, t = %f, d = %d, l = %d\n",i+1, pt->a, pt->t, pt->d, pt->l);
400 sum += pt->a * pow(tau, pt->t - 1) * ipow(delta, pt->d) * pt->t;
401 }
402 ++pt;
403 //fprintf(stderr,"l = %d\n",l);
404 if(i+1==n || l != pt->l){
405 if(l==0){
406 //fprintf(stderr,"Adding non-exp term\n");
407 res += sum;
408 }else{
409 //fprintf(stderr,"Adding exp term with l = %d, delX = %e\n",l,delX);
410 res += sum * exp(-delX);
411 }
412 /* set l to new value */
413 if(i+1!=n){
414 l = pt->l;
415 //fprintf(stderr,"New l = %d\n",l);
416 delX = ipow(delta,l);
417 sum = 0;
418 }
419 }
420 }
421
422 #if 1
423 /* now the exponential terms */
424 n = data->ne;
425 et = &(data->et[0]);
426 for(i=0; i< n; ++i){
427 //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);
428
429 double tau2 = tau*tau;
430 double del2 = delta*delta;
431 double gam2 = et->gamma * et->gamma;
432 double e1 = -et->phi * del2
433 + 2 * et->phi * delta
434 - et->beta * tau2
435 + 2 * et->beta * et->gamma * tau
436 - et->phi
437 - et->beta * gam2;
438 sum = -et->a * pow(tau,et->t - 1) * ipow(delta,et->d)
439 * (2 * et->beta * tau2 - 2 * et->beta * et->gamma * tau - et->t)
440 * exp(e1);
441 //fprintf(stderr,"sum = %f\n",sum);
442 res += sum;
443 ++et;
444 }
445 #endif
446
447 return res;
448 }
449
450
451
452 /**
453 Mixed derivative of the helmholtz residual function with respect to
454 delta and tau
455 */
456 double helm_resid_deltau(double tau,double delta,const HelmholtzData *data){
457
458 double sum;
459 double phir = 0;
460 unsigned i;
461 double XdelX;
462
463 const HelmholtzPowTerm *pt = &(data->pt[0]);
464
465 for(i=0; i<5; ++i){
466 phir += pt->a * pow(tau, pt->t - 1) * ipow(delta, pt->d - 1) * pt->d * pt->t;
467 ++pt;
468 }
469
470 sum = 0;
471 XdelX = delta;
472 for(i=5; i<10; ++i){
473 sum += pt->a * pow(tau, pt->t - 1) * ipow(delta, pt->d - 1) * pt->t *(pt->d - XdelX);
474 ++pt;
475 }
476 phir += exp(-delta) * sum;
477
478 sum = 0;
479 XdelX = 2*delta*delta;
480 for(i=10; i<17; ++i){
481 sum += pt->a * pow(tau, pt->t - 1) * ipow(delta, pt->d - 1) * pt->t *(pt->d - XdelX);
482 ++pt;
483 }
484 phir += exp(-delta*delta) * sum;
485
486 sum = 0;
487 XdelX = 3*delta*delta*delta;
488 for(i=17; i<21; ++i){
489 sum += pt->a * pow(tau, pt->t - 1) * ipow(delta, pt->d - 1) * pt->t *(pt->d - XdelX);
490 ++pt;
491 }
492 phir += exp(-delta*delta*delta) * sum;
493
494 return phir;
495 }
496
497 #define SQ(X) ((X)*(X))
498
499 /**
500 Second derivative of helmholtz residual function with respect to
501 delta (twice).
502 */
503 double helm_resid_deldel(double tau,double delta,const HelmholtzData *data){
504
505 double sum;
506 double phir = 0;
507 unsigned i;
508 unsigned X;
509 double XdelX;
510
511 const HelmholtzPowTerm *pt = &(data->pt[0]);
512
513 for(i=0; i<5; ++i){
514 phir += pt->a * pow(tau, pt->t) * ipow(delta, pt->d - 2) * (SQ(pt->d) - X);
515 ++pt;
516 }
517
518 sum = 0;
519 X = 1;
520 XdelX = delta;
521 for(i=5; i<10; ++i){
522 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);
523 ++pt;
524 }
525 phir += exp(-delta) * sum;
526
527 sum = 0;
528 X = 2;
529 XdelX = 2*delta*delta;
530 for(i=10; i<17; ++i){
531 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);
532 ++pt;
533 }
534 phir += exp(-delta*delta) * sum;
535
536 sum = 0;
537 X = 3;
538 XdelX = 3*delta*delta*delta;
539 for(i=17; i<21; ++i){
540 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);
541 ++pt;
542 }
543 phir += exp(-delta*delta*delta) * sum;
544
545 return phir;
546 }
547

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