/[ascend]/branches/vishnu/models/johnpye/fprops/test/incomp_test/ZS55.c~
ViewVC logotype

Contents of /branches/vishnu/models/johnpye/fprops/test/incomp_test/ZS55.c~

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3164 - (show annotations) (download)
Tue Aug 9 06:40:46 2016 UTC (5 years, 10 months ago) by vishnu
File size: 6842 byte(s)
skype
1 // "Zitrec S55, Potassium formate/Sodium propionate"
2
3
4 #include "../incompressible.h"
5 #include "../fprops.h"
6
7 /*
8 #define AMMONIA_M 17.03026
9 #define AMMONIA_R (8314.472/AMMONIA_M) //488.189
10 #define AMMONIA_TC 405.40
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <assert.h>
17 #include <math.h>
18
19
20 #define BUF 1024
21
22 #define MAXC 10
23
24 #define PATH "incomp_liq_data/ZS55.dat"
25
26 #define ABORT \
27 printf("\nExiting Program!\n"); \
28 exit(0);
29
30 // json parser
31 #define READ(Q) \
32 pos = ftell(in); \
33 while(!strstr(word,"},")) { \
34 fgets(word,BUF,in); \
35 if(strstr(word,"\"type\": \"notdefined\"")) { \
36 test_liq->Q.type = (char*)malloc(sizeof("notdefined")); \
37 strcpy(test_liq->Q.type,"notdefined"); \
38 } \
39 else if(strstr(word,"\"type\": \"polynomial\"")) { \
40 test_liq->Q.type = (char*)malloc(sizeof("polynomial")); \
41 strcpy(test_liq->Q.type,"polynomial"); \
42 } \
43 else if(strstr(word,"\"type\": \"exppolynomial\"")) { \
44 test_liq->Q.type = (char*)malloc(sizeof("exppolynomial")); \
45 strcpy(test_liq->Q.type,"exppolynomial"); \
46 } \
47 else if(strstr(word,"\"type\": \"exponential\"")) { \
48 test_liq->Q.type = (char*)malloc(sizeof("exponential")); \
49 strcpy(test_liq->Q.type,"exponential"); \
50 } \
51 } \
52 if(strcmp(test_liq->Q.type,"notdefined")) { \
53 fseek(in,pos,SEEK_SET); \
54 fgets(word,BUF,in); \
55 double coefs[MAXC][MAXC]; \
56 int numc_row = 0, numc_col = 0; \
57 while(!strstr(word,"},")) { \
58 if((strstr(word,"e+")||strstr(word,"e-"))&&!strstr(word,"NRMS")) { \
59 coefs[numc_row][numc_col] = to_number(word); \
60 numc_col++; \
61 assert(numc_col<MAXC); \
62 } \
63 else if(strstr(word,"],")) { \
64 numc_row++; \
65 test_liq->Q.numc_c = numc_col; \
66 numc_col = 0; \
67 assert(numc_row<MAXC); \
68 } \
69 fgets(word,BUF,in); \
70 } \
71 test_liq->Q.numc_r = numc_row; \
72 test_liq->Q.coeff = (double**)malloc(test_liq->Q.numc_r*sizeof(double*)); \
73 int i,j; \
74 for(i=0;i<test_liq->Q.numc_r;i++) \
75 test_liq->Q.coeff[i] = (double*)malloc(test_liq->Q.numc_c*sizeof(double)); \
76 for(i=0;i<test_liq->Q.numc_r;i++) \
77 for(j=0;j<test_liq->Q.numc_c;j++) \
78 test_liq->Q.coeff[i][j] = coefs[i][j]; \
79 }
80
81 typedef struct {
82
83 char* type;
84 double** coeff;
85 int numc_r, numc_c; // number of rows and columns in coefficient matrix respectively
86
87 } coefficients;
88
89 typedef struct {
90
91 coefficients T_freeze, conductivity, density, specific_heat, viscosity, saturation_pressure;
92 char* x_id;
93 char* description;
94 double T_base, T_max, T_min, T_minPsat, x_base, x_max, x_min;
95
96 } fprops;
97
98 double extracted_number(char w[BUF]) {
99
100 char *token;
101
102 /* get the first token */
103 token = strtok(w, " ,");
104
105 /* get the second token which is the number*/
106 token = strtok(NULL, " ,");
107
108 return (atof(token));
109
110 }
111
112 double to_number(char w[BUF]) {
113
114 char *token;
115
116 /* get the first token */
117 token = strtok(w, " ,");
118
119 return (atof(token));
120
121 }
122 /*
123 // evaluation function for polynamial type coefficients
124 double eval_poly(coefficients c, double T, double x) {
125
126 double res = 0.0;
127 int i,j;
128
129 for(i=0;i<c.numc_r;i++)
130 for(j=0;j<c.numc_c;j++)
131 res += pow(x,j)*c.coeff[i][j]*pow(T,i);
132
133 return res;
134
135 }
136
137 // evaluation function for exponential-polynomial type coefficients
138 double eval_exppoly(coefficients c, double T, double x) {
139
140 return exp(eval_poly(c,T,x));
141
142 }
143
144 // evaluation function for exponential type coefficients
145 double eval_expo(coefficients c, double T, double x) {
146
147 return exp(c.coeff[0][0]/(T+c.coeff[0][1])-c.coeff[0][2]);
148
149 }
150
151 // generalized property evaluation functions: macro definition
152 #define PROP_EVAL(Q) \
153 double eval_ ## Q (fprops *fluid, double T, double x) { \
154 assert(T>0&&T>=fluid->T_min&&T<=fluid->T_max); \
155 assert(x>=fluid->x_min&&x<=fluid->x_max); \
156 if(!strcmp(fluid->Q.type,"polynomial")) return eval_poly(fluid->Q,T-fluid->T_base,x-fluid->x_base); \
157 else if(!strcmp(fluid->Q.type,"exppolynomial")) return eval_exppoly(fluid->Q,T-fluid->T_base,x-fluid->x_base); \
158 else if(!strcmp(fluid->Q.type,"exponential")) return eval_expo(fluid->Q,T,x); \
159 else { \
160 printf("\nType not defined.\n"); \
161 ABORT \
162 } \
163 }
164
165 // declaration and definition of property evaluation functions for conductivity, density, specific-heat and viscosity
166 PROP_EVAL(conductivity)
167 PROP_EVAL(density)
168 PROP_EVAL(specific_heat)
169 PROP_EVAL(viscosity)
170 PROP_EVAL(saturation_pressure)
171 */
172
173 int main() {
174
175 // parsing from input (json format) and loading data to new fluid data structures
176 FILE* in;
177 IncompressibleData* test_liq;
178 test_liq = (IncompressibleData*)malloc(sizeof(IncompressibleData));
179
180 long int pos;
181
182 if(!(in = fopen(PATH,"r"))) {
183
184 printf("\nInput file cannot be located.\n");
185 ABORT
186
187 }
188
189 char word[BUF];
190
191 while(!feof(in)) {
192
193 fgets(word,BUF,in);
194
195 if(strstr(word,"\"T_freeze\"")) { READ(T_freeze); } // not required for incompressible EOS; implemented to check input coefficients of "type": "notdefined"
196 else if(strstr(word,"\"Tbase\"")) test_liq->T_base = extracted_number(word);
197 else if(strstr(word,"\"Tmax\"")) test_liq->T_max = extracted_number(word);
198 else if(strstr(word,"\"Tmin\"")) test_liq->T_min = extracted_number(word);
199 else if(strstr(word,"\"TminPsat\"")) test_liq->T_minPsat = extracted_number(word);
200 else if(strstr(word,"\"conductivity\"")) { READ(conductivity); }
201 else if(strstr(word,"\"density\"")) { READ(density); }
202 else if(strstr(word,"\"specific_heat\"")) { READ(specific_heat); }
203 else if(strstr(word,"\"viscosity\"")) { READ(viscosity); }
204 else if(strstr(word,"\"saturation_pressure\"")) { READ(saturation_pressure); }
205 else if(strstr(word,"\"xbase\"")) test_liq->x_base = extracted_number(word);
206 else if(strstr(word,"\"xmax\"")) test_liq->x_max = extracted_number(word);
207 else if(strstr(word,"\"xmin\"")) test_liq->x_min = extracted_number(word);
208 else if(strstr(word,"\"xid\": \"pure\"")) {
209 test_liq->x_id = (char*)malloc(sizeof("pure"));
210 strcpy(test_liq->x_id,"pure");
211 }
212
213 }
214
215 fclose(in);
216
217 const EosData eos_ZS55 = {
218 "ZS55"
219 ,"test"
220 , NULL
221 ,100
222 ,FPROPS_INCOMPRESSIBLE
223 ,.data = {.incomp = test_liq}
224 };
225
226 // test
227 PureFluid *P = incompressible_prepare(&eos_ZS55, NULL);
228 FpropsError fprops_err = FPROPS_NO_ERROR;
229
230 #define D P->data->corr.incomp
231
232 double T = D->T_min;
233
234 FILE *out;
235 out = fopen("test_res.dat","w");
236
237 fprintf(out,"VARIABLES = \"Temperature [C]\", \"Cp [W/m/K]\", \"Cv [W/m/K]\", \"Density [kg/m<sup>3</sup>]\", \"Heat Capacity [J/Kg/K]\", \"Viscosity [Pa s]\", \"Saturation Pressure [Pa]\"\n");
238 do {
239 FluidState S = fprops_set_Tpx(T,0.0,1.0,P,&fprops_err);
240 fprintf(out,"%e\t%e\t%e\t%e\t%e\t%e\t%e\n",T-273.0,fprops_cp(S,&fprops_err),fprops_cv(S,&fprops_err),
241 fprops_rho(S,&fprops_err),fprops_lam(S,&fprops_err),fprops_mu(S,&fprops_err),fprops_p_sat(S,&fprops_err));
242 T+=1;
243
244 } while(T<=D->T_max);
245
246 fclose(out);
247
248 return 0;
249
250 }

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