/[ascend]/branches/vishnu/models/johnpye/fprops/vishnu/scratch/main.c
ViewVC logotype

Annotation of /branches/vishnu/models/johnpye/fprops/vishnu/scratch/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3116 - (hide annotations) (download) (as text)
Mon Jun 13 02:22:51 2016 UTC (6 years, 2 months ago) by vishnu
File MIME type: text/x-csrc
File size: 5619 byte(s)
added evaluation functions, test results generated, testing unsuccesfull
1 vishnu 3115
2     #include <stdio.h>
3     #include <stdlib.h>
4     #include <string.h>
5     #include <assert.h>
6 vishnu 3116 #include <math.h>
7 vishnu 3115
8     #define BUF 1024
9    
10     #define MAXC 10
11    
12     #define PATH "incomp_liq_data/ZS55.dat"
13    
14     #define ABORT \
15     printf("\nExiting Program!\n"); \
16     exit(0);
17    
18     #define READ(Q) \
19     pos = ftell(in); \
20     while(!strstr(word,"},")) { \
21     fgets(word,BUF,in); \
22     if(strstr(word,"\"type\": \"notdefined\"")) { \
23     test_liq->Q.type = (char*)malloc(sizeof("notdefined")); \
24     strcpy(test_liq->Q.type,"notdefined"); \
25     } \
26     else if(strstr(word,"\"type\": \"polynomial\"")) { \
27     test_liq->Q.type = (char*)malloc(sizeof("polynomial")); \
28     strcpy(test_liq->Q.type,"polynomial"); \
29     } \
30     else if(strstr(word,"\"type\": \"exppolynomial\"")) { \
31     test_liq->Q.type = (char*)malloc(sizeof("exppolynomial")); \
32     strcpy(test_liq->Q.type,"exppolynomial"); \
33     } \
34 vishnu 3116 else if(strstr(word,"\"type\": \"exponential\"")) { \
35     test_liq->Q.type = (char*)malloc(sizeof("exponential")); \
36     strcpy(test_liq->Q.type,"exponential"); \
37     } \
38 vishnu 3115 } \
39     if(strcmp(test_liq->Q.type,"notdefined")) { \
40     fseek(in,pos,SEEK_SET); \
41     fgets(word,BUF,in); \
42     double coefs[MAXC]; \
43     int numc = 0; \
44     while(!strstr(word,"},")) { \
45     if(strstr(word,"NRMS")) { \
46     coefs[numc] = extracted_number(word); \
47     numc++; \
48     assert(numc==1); \
49     } \
50     else if(!strstr(word,"coeffs")&&strstr(word,"[")) { \
51     fgets(word,BUF,in); \
52     coefs[numc] = to_number(word); \
53     numc++; \
54     assert(numc<MAXC); \
55     } \
56     fgets(word,BUF,in); \
57     } \
58     test_liq->Q.coeff = (double*)malloc(numc*sizeof(double)); \
59     test_liq->Q.numc = numc; \
60     while(numc) { \
61     numc--; \
62     test_liq->Q.coeff[numc] = coefs[numc]; \
63     } \
64     }
65    
66     typedef struct {
67    
68     char* type;
69     double* coeff;
70     int numc;
71    
72     } coefficients;
73    
74     typedef struct {
75    
76     coefficients T_freeze, conductivity, density, specific_heat, viscosity;
77     char* x_id;
78     char* description;
79     double T_base, T_max, T_min, T_minPsat, x_base, x_max, x_min;
80    
81     } fprops;
82    
83     double extracted_number(char w[BUF]) {
84    
85     char *token;
86    
87     /* get the first token */
88     token = strtok(w, " ,");
89    
90     /* get the second token which is the number*/
91     token = strtok(NULL, " ,");
92    
93     return (atof(token));
94    
95     }
96    
97     double to_number(char w[BUF]) {
98    
99     char *token;
100    
101     /* get the first token */
102     token = strtok(w, " ");
103    
104     return (atof(token));
105    
106 vishnu 3116 }
107    
108     // eveluation function for polynamial type coefficients
109     double eval_poly(coefficients c, double T, double x) {
110    
111     double res = 0.0;
112     int i;
113    
114     for(i=1;i<c.numc;i++)
115     res += c.coeff[i]*pow(T,i-1);
116    
117     return res;
118    
119     }
120    
121     // eveluation function for exponential-polynomial type coefficients
122     double eval_exppoly(coefficients c, double T, double x) {
123    
124     return exp(eval_poly(c,T,x));
125    
126     }
127    
128     // eveluation function for exponential type coefficients
129     double eval_expo(coefficients c, double T, double x) {
130    
131     return exp(c.coeff[1]/(T+c.coeff[2])+c.coeff[3]);
132    
133 vishnu 3115 }
134    
135 vishnu 3116 // generalized property evaluation functions: macro definition
136     #define PROP_EVAL(Q) \
137     double eval_ ## Q (fprops *fluid, double T, double x) { \
138     assert(T>0&&T>=fluid->T_min&&T<=fluid->T_max); \
139     assert(x>=fluid->x_min&&x<=fluid->x_max); \
140     if(!strcmp(fluid->Q.type,"polynomial")) return eval_poly(fluid->Q,T,x); \
141     else if(!strcmp(fluid->Q.type,"exppolynomial")) return eval_exppoly(fluid->Q,T,x); \
142     else if(!strcmp(fluid->Q.type,"exponential")) return eval_expo(fluid->Q,T,x); \
143     else { \
144     printf("\nType not defined.\n"); \
145     ABORT \
146     } \
147     }
148    
149     // declaration and definition of property evaluation functions for conductivity, density, specific-heat and viscosity
150     PROP_EVAL(conductivity)
151     PROP_EVAL(density)
152     PROP_EVAL(specific_heat)
153     PROP_EVAL(viscosity)
154    
155 vishnu 3115 int main() {
156    
157 vishnu 3116 // parsing from input (json format) and loading data to new fluid data structures
158 vishnu 3115 FILE* in;
159    
160     fprops* test_liq;
161     test_liq = (fprops*)malloc(sizeof(fprops));
162    
163     long int pos;
164    
165     if(!(in = fopen(PATH,"r"))) {
166    
167     printf("\nInput file cannot be located.\n");
168     ABORT
169    
170     }
171    
172     char word[BUF];
173    
174     while(!feof(in)) {
175    
176 vishnu 3116 fgets(word,BUF,in);
177 vishnu 3115
178 vishnu 3116 if(strstr(word,"\"T_freeze\"")) { READ(T_freeze); } // not required for incompressible EOS; implemented to check input coefficients of "type": "notdefined"
179     else if(strstr(word,"\"Tbase\"")) test_liq->T_base = extracted_number(word);
180     else if(strstr(word,"\"Tmax\"")) test_liq->T_max = extracted_number(word);
181     else if(strstr(word,"\"Tmin\"")) test_liq->T_min = extracted_number(word);
182     else if(strstr(word,"\"TminPsat\"")) test_liq->T_minPsat = extracted_number(word);
183     else if(strstr(word,"\"conductivity\"")) { READ(conductivity); }
184     else if(strstr(word,"\"density\"")) { READ(density); }
185     else if(strstr(word,"\"specific_heat\"")) { READ(specific_heat); }
186     else if(strstr(word,"\"viscosity\"")) { READ(viscosity); }
187     else if(strstr(word,"\"xbase\"")) test_liq->x_base = extracted_number(word);
188     else if(strstr(word,"\"xmax\"")) test_liq->x_max = extracted_number(word);
189     else if(strstr(word,"\"xmin\"")) test_liq->x_min = extracted_number(word);
190 vishnu 3115 else if(strstr(word,"\"xid\": \"pure\"")) {
191     test_liq->x_id = (char*)malloc(sizeof("pure"));
192     strcpy(test_liq->x_id,"pure");
193     }
194    
195     }
196    
197     fclose(in);
198    
199 vishnu 3116 // test
200     double T = test_liq->T_min;
201    
202     FILE *out;
203     out = fopen("test_res.dat","w");
204    
205     fprintf(out,"VARIABLES = \"Temperature [C]\", \"Conductivity [W/m/K]\", \"Density [kg/m<sup>3</sup>]\", \"Heat Capacity [J/Kg/K]\", \"Viscosity [Pa s]\"\n");
206     do {
207    
208     fprintf(out,"%e\t%e\t%e\t%e\t%e\n",T-273.0,eval_conductivity(test_liq, T, 1),eval_density(test_liq, T, 1),eval_specific_heat(test_liq, T, 1),eval_viscosity(test_liq, T, 1));
209     T+=1;
210    
211     } while(T<=test_liq->T_max);
212    
213     fclose(out);
214    
215 vishnu 3115 return 0;
216    
217     }

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