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

Contents of /trunk/models/johnpye/fprops/test/sat.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2680 - (show annotations) (download) (as text)
Mon Jan 28 06:30:25 2013 UTC (9 years, 4 months ago) by jpye
File MIME type: text/x-csrc
File size: 3697 byte(s)
Working on problem with solve_ph. Could be that one of the deriv routines is wrong in the saturation region?
1 #include "../fluids.h"
2 #include "../fprops.h"
3 #include "../solve_Tx.h"
4 #include "../sat.h"
5 #include <assert.h>
6 #include <math.h>
7 #include "../fluids/fluids_list.h"
8 #include "../color.h"
9
10 #define MSG(FMT, ...) \
11 color_on(stderr,ASC_FG_BRIGHTRED);\
12 fprintf(stderr,"%s:%d: ",__FILE__,__LINE__);\
13 color_on(stderr,ASC_FG_BRIGHTBLUE);\
14 fprintf(stderr,"%s: ",__func__);\
15 color_off(stderr);\
16 fprintf(stderr,FMT "\n",##__VA_ARGS__)
17
18 #define ERRMSG(STR,...) \
19 color_on(stderr,ASC_FG_BRIGHTRED);\
20 fprintf(stderr,"ERROR:");\
21 color_off(stderr);\
22 fprintf(stderr," %s:%d:" STR "\n", __func__, __LINE__ ,##__VA_ARGS__)
23
24 #define TOL_T 1e-3
25 #define TOL_RHO 1e-3
26
27
28 int main(void){
29 const PureFluid *P;
30 FpropsError err;
31 FluidState S;
32
33 #define FNAME(F) #F
34 #define COMMA ,
35 const char *helmfluids[] = { FLUIDS(FNAME,COMMA) COMMA RPPFLUIDS(FNAME,COMMA) };
36 #undef FNAME
37 const char *corrtypes[] = {"pengrob","helmholtz"};
38 const char *corrinitial[] = {"P","H"};
39 enum corrtypes_enum {CORRTYPE_PENGROB,CORRTYPE_HELMHOLTZ,CORRTYPE_N};
40 #define FHELM(F) CORRTYPE_HELMHOLTZ
41 #define FPR(F) CORRTYPE_PENGROB
42 const enum corrtypes_enum corrfluids[] = {FLUIDS(FHELM,COMMA) COMMA RPPFLUIDS(FPR,COMMA) };
43 #undef FHELM
44 #undef FPR
45 #define FRPP(F) "RPP"
46 #define FUN(F) NULL
47 const char *srcfluids[] = {FLUIDS(FUN,COMMA) COMMA RPPFLUIDS(FRPP,COMMA) };
48 #undef FUN
49 #undef FRPP
50 #undef COMMA
51
52 char nloggederrors = 0;
53 #define MAXNLOGGEDERRORS 25
54 #define MAXERRORLOG (MAXNLOGGEDERRORS*80)
55 char errorlog[MAXERRORLOG] = "";
56
57 #define ERRLOG(FMT,...) \
58 if(nloggederrors < MAXNLOGGEDERRORS){\
59 int l = strlen(errorlog);\
60 char *s = errorlog + l;\
61 snprintf(s,MAXERRORLOG-l,FMT "\n",##__VA_ARGS__);\
62 }\
63 nloggederrors++;
64
65 const int n = sizeof(helmfluids)/sizeof(char *);
66 int i,j;
67 int nerrfluids = 0;
68 const char *errfluids[n];
69 MSG("Testing convergence of saturation curves for all helmholtz fluids...");
70 for(i=0; i<n; ++i){
71 int nerr = 0;
72 P = fprops_fluid(helmfluids[i],corrtypes[corrfluids[i]],srcfluids[i]);
73 if(!P){
74 MSG("Error initialising fluid '%s' type '%s'",helmfluids[i],corrtypes[corrfluids[i]]);
75 nerr = 1;
76 color_on(stdout,ASC_FG_BRIGHTRED);
77 fprintf(stdout,"%s",corrinitial[corrfluids[i]]);
78 color_off(stdout);
79 }else{
80 double Tt = P->data->T_t;
81 double Tc = P->data->T_c;
82 if(Tt == 0){
83 color_on(stdout,ASC_FG_YELLOW);
84 fprintf(stdout,"%s",corrinitial[corrfluids[i]]);
85 color_off(stdout);
86 Tt = 273.15 - 20;
87 if(Tt > Tc){
88 Tt = 0.4 * Tc;
89 }
90 }else{
91 color_on(stdout,ASC_FG_BRIGHTGREEN);
92 fprintf(stdout,"%s",corrinitial[corrfluids[i]]);
93 color_off(stdout);
94 }
95 double nT = 500;
96 double rT = 1/Tt;
97 double drT = (1/Tc - 1/Tt) / nT;
98 for(j=0; j<nT; ++j){
99 double T = 1/rT;
100 double psat,rhof,rhog;
101 err = FPROPS_NO_ERROR;
102 fprops_sat_T(T, &psat, &rhof, &rhog, P, &err);
103 if(err){
104 nerr++;
105 color_on(stdout,ASC_FG_BRIGHTRED);
106 fprintf(stdout,"C");
107 color_off(stdout);
108 ERRLOG("sat_T(%f) for '%s', omega=%f",T,P->name,P->data->omega);
109 }else{
110 fprintf(stdout,".");
111 }
112 rT += drT;
113 }
114 }
115 fprintf(stdout,":%s\n",helmfluids[i]);
116 if(nerr)errfluids[nerrfluids++] = helmfluids[i];
117 }
118
119 if(nerrfluids){
120 MSG("There were %d fluids with saturation curve errors:",nerrfluids);
121 for(i=0; i<nerrfluids; ++i){
122 fprintf(stderr," %s",errfluids[i]);
123 }
124 }
125 if(nloggederrors){
126 fprintf(stderr,"\n");
127 MSG("First %d of the %d errors logged:",MAXNLOGGEDERRORS,nloggederrors);
128 fprintf(stderr,"%s",errorlog);
129 }
130
131 if(nerrfluids)return nerrfluids;
132
133 fprintf(stderr,"\n");
134 color_on(stderr,ASC_FG_BRIGHTGREEN);
135 fprintf(stderr,"SUCCESS (%s)",__FILE__);
136 color_off(stderr);
137 fprintf(stderr,"\n");
138 return 0;
139 }
140

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