9 |
|
|
10 |
#define MAXC 10 |
#define MAXC 10 |
11 |
|
|
12 |
#define PATH "incomp_liq_data/Water.dat" |
#define PATH "incomp_liq_data/LiBr.dat" |
13 |
|
|
14 |
#define ABORT \ |
#define ABORT \ |
15 |
printf("\nExiting Program!\n"); \ |
printf("\nExiting Program!\n"); \ |
16 |
exit(0); |
exit(0); |
17 |
|
|
18 |
|
// json parser |
19 |
#define READ(Q) \ |
#define READ(Q) \ |
20 |
pos = ftell(in); \ |
pos = ftell(in); \ |
21 |
while(!strstr(word,"},")) { \ |
while(!strstr(word,"},")) { \ |
37 |
strcpy(test_liq->Q.type,"exponential"); \ |
strcpy(test_liq->Q.type,"exponential"); \ |
38 |
} \ |
} \ |
39 |
} \ |
} \ |
40 |
if(!strcmp(test_liq->Q.type,"polynomial")||!strcmp(test_liq->Q.type,"exppolynomial")) { \ |
if(strcmp(test_liq->Q.type,"notdefined")) { \ |
41 |
fseek(in,pos,SEEK_SET); \ |
fseek(in,pos,SEEK_SET); \ |
42 |
fgets(word,BUF,in); \ |
fgets(word,BUF,in); \ |
43 |
double coefs[MAXC]; \ |
double coefs[MAXC][MAXC]; \ |
44 |
int numc = 0; \ |
int numc_row = 0, numc_col = 0; \ |
45 |
while(!strstr(word,"},")) { \ |
while(!strstr(word,"},")) { \ |
46 |
if(strstr(word,"NRMS")) { \ |
if((strstr(word,"e+")||strstr(word,"e-"))&&!strstr(word,"NRMS")) { \ |
47 |
coefs[numc] = extracted_number(word); \ |
coefs[numc_row][numc_col] = to_number(word); \ |
48 |
numc++; \ |
numc_col++; \ |
49 |
assert(numc==1); \ |
assert(numc_col<MAXC); \ |
50 |
} \ |
} \ |
51 |
else if(!strstr(word,"coeffs")&&strstr(word,"[")) { \ |
else if(strstr(word,"],")) { \ |
52 |
fgets(word,BUF,in); \ |
numc_row++; \ |
53 |
coefs[numc] = to_number(word); \ |
test_liq->Q.numc_c = numc_col; \ |
54 |
numc++; \ |
numc_col = 0; \ |
55 |
assert(numc<MAXC); \ |
assert(numc_row<MAXC); \ |
56 |
} \ |
} \ |
57 |
fgets(word,BUF,in); \ |
fgets(word,BUF,in); \ |
58 |
} \ |
} \ |
59 |
test_liq->Q.coeff = (double*)malloc(numc*sizeof(double)); \ |
test_liq->Q.numc_r = numc_row; \ |
60 |
test_liq->Q.numc = numc; \ |
test_liq->Q.coeff = (double**)malloc(test_liq->Q.numc_r*sizeof(double*)); \ |
61 |
while(numc) { \ |
int i,j; \ |
62 |
numc--; \ |
for(i=0;i<test_liq->Q.numc_r;i++) \ |
63 |
test_liq->Q.coeff[numc] = coefs[numc]; \ |
test_liq->Q.coeff[i] = (double*)malloc(test_liq->Q.numc_c*sizeof(double)); \ |
64 |
} \ |
for(i=0;i<test_liq->Q.numc_r;i++) \ |
65 |
} \ |
for(j=0;j<test_liq->Q.numc_c;j++) \ |
66 |
else if(!strcmp(test_liq->Q.type,"exponential")) { \ |
test_liq->Q.coeff[i][j] = coefs[i][j]; \ |
|
fseek(in,pos,SEEK_SET); \ |
|
|
fgets(word,BUF,in); \ |
|
|
double coefs[MAXC]; \ |
|
|
int numc = 0; \ |
|
|
while(!strstr(word,"},")) { \ |
|
|
if(strstr(word,"NRMS")) { \ |
|
|
coefs[numc] = extracted_number(word); \ |
|
|
numc++; \ |
|
|
assert(numc==1); \ |
|
|
} \ |
|
|
else if(strstr(word,"e+")||strstr(word,"e-")) { \ |
|
|
coefs[numc] = to_number(word); \ |
|
|
numc++; \ |
|
|
assert(numc<MAXC); \ |
|
|
} \ |
|
|
fgets(word,BUF,in); \ |
|
|
} \ |
|
|
test_liq->Q.coeff = (double*)malloc(numc*sizeof(double)); \ |
|
|
test_liq->Q.numc = numc; \ |
|
|
while(numc) { \ |
|
|
numc--; \ |
|
|
test_liq->Q.coeff[numc] = coefs[numc]; \ |
|
|
} \ |
|
67 |
} |
} |
68 |
|
|
69 |
typedef struct { |
typedef struct { |
70 |
|
|
71 |
char* type; |
char* type; |
72 |
double* coeff; |
double** coeff; |
73 |
int numc; |
int numc_r, numc_c; // number of rows and columns in coefficient matrix respectively |
74 |
|
|
75 |
} coefficients; |
} coefficients; |
76 |
|
|
77 |
typedef struct { |
typedef struct { |
78 |
|
|
79 |
coefficients T_freeze, conductivity, density, specific_heat, viscosity; |
coefficients T_freeze, conductivity, density, specific_heat, viscosity, saturation_pressure; |
80 |
char* x_id; |
char* x_id; |
81 |
char* description; |
char* description; |
82 |
double T_base, T_max, T_min, T_minPsat, x_base, x_max, x_min; |
double T_base, T_max, T_min, T_minPsat, x_base, x_max, x_min; |
108 |
|
|
109 |
} |
} |
110 |
|
|
111 |
// eveluation function for polynamial type coefficients |
// evaluation function for polynamial type coefficients |
112 |
double eval_poly(coefficients c, double T, double x) { |
double eval_poly(coefficients c, double T, double x) { |
113 |
|
|
114 |
double res = 0.0; |
double res = 0.0; |
115 |
int i; |
int i,j; |
116 |
|
|
117 |
for(i=1;i<c.numc;i++) |
for(i=0;i<c.numc_r;i++) |
118 |
res += c.coeff[i]*pow(T,i-1); |
for(j=0;j<c.numc_c;j++) |
119 |
|
res += pow(x,j)*c.coeff[i][j]*pow(T,i); |
120 |
|
|
121 |
return res; |
return res; |
122 |
|
|
123 |
} |
} |
124 |
|
|
125 |
// eveluation function for exponential-polynomial type coefficients |
// evaluation function for exponential-polynomial type coefficients |
126 |
double eval_exppoly(coefficients c, double T, double x) { |
double eval_exppoly(coefficients c, double T, double x) { |
127 |
|
|
128 |
return exp(eval_poly(c,T,x)); |
return exp(eval_poly(c,T,x)); |
129 |
|
|
130 |
} |
} |
131 |
|
|
132 |
// eveluation function for exponential type coefficients |
// evaluation function for exponential type coefficients |
133 |
double eval_expo(coefficients c, double T, double x) { |
double eval_expo(coefficients c, double T, double x) { |
134 |
|
|
135 |
return exp(c.coeff[1]/(T+c.coeff[2])-c.coeff[3]); |
return exp(c.coeff[0][0]/(T+c.coeff[0][1])-c.coeff[0][2]); |
136 |
|
|
137 |
} |
} |
138 |
|
|
143 |
assert(x>=fluid->x_min&&x<=fluid->x_max); \ |
assert(x>=fluid->x_min&&x<=fluid->x_max); \ |
144 |
if(!strcmp(fluid->Q.type,"polynomial")) return eval_poly(fluid->Q,T-fluid->T_base,x-fluid->x_base); \ |
if(!strcmp(fluid->Q.type,"polynomial")) return eval_poly(fluid->Q,T-fluid->T_base,x-fluid->x_base); \ |
145 |
else if(!strcmp(fluid->Q.type,"exppolynomial")) return eval_exppoly(fluid->Q,T-fluid->T_base,x-fluid->x_base); \ |
else if(!strcmp(fluid->Q.type,"exppolynomial")) return eval_exppoly(fluid->Q,T-fluid->T_base,x-fluid->x_base); \ |
146 |
else if(!strcmp(fluid->Q.type,"exponential")) return eval_expo(fluid->Q,T,0); \ |
else if(!strcmp(fluid->Q.type,"exponential")) return eval_expo(fluid->Q,T,x); \ |
147 |
else { \ |
else { \ |
148 |
printf("\nType not defined.\n"); \ |
printf("\nType not defined.\n"); \ |
149 |
ABORT \ |
ABORT \ |
155 |
PROP_EVAL(density) |
PROP_EVAL(density) |
156 |
PROP_EVAL(specific_heat) |
PROP_EVAL(specific_heat) |
157 |
PROP_EVAL(viscosity) |
PROP_EVAL(viscosity) |
158 |
|
PROP_EVAL(saturation_pressure) |
159 |
|
|
160 |
int main() { |
int main() { |
161 |
|
|
189 |
else if(strstr(word,"\"density\"")) { READ(density); } |
else if(strstr(word,"\"density\"")) { READ(density); } |
190 |
else if(strstr(word,"\"specific_heat\"")) { READ(specific_heat); } |
else if(strstr(word,"\"specific_heat\"")) { READ(specific_heat); } |
191 |
else if(strstr(word,"\"viscosity\"")) { READ(viscosity); } |
else if(strstr(word,"\"viscosity\"")) { READ(viscosity); } |
192 |
|
else if(strstr(word,"\"saturation_pressure\"")) { READ(saturation_pressure); } |
193 |
else if(strstr(word,"\"xbase\"")) test_liq->x_base = extracted_number(word); |
else if(strstr(word,"\"xbase\"")) test_liq->x_base = extracted_number(word); |
194 |
else if(strstr(word,"\"xmax\"")) test_liq->x_max = extracted_number(word); |
else if(strstr(word,"\"xmax\"")) test_liq->x_max = extracted_number(word); |
195 |
else if(strstr(word,"\"xmin\"")) test_liq->x_min = extracted_number(word); |
else if(strstr(word,"\"xmin\"")) test_liq->x_min = extracted_number(word); |
208 |
FILE *out; |
FILE *out; |
209 |
out = fopen("test_res.dat","w"); |
out = fopen("test_res.dat","w"); |
210 |
|
|
211 |
fprintf(out,"VARIABLES = \"Temperature [C]\", \"Conductivity [W/m/K]\", \"Density [kg/m<sup>3</sup>]\", \"Heat Capacity [J/Kg/K]\", \"Viscosity [Pa s]\"\n"); |
fprintf(out,"VARIABLES = \"Temperature [C]\", \"Conductivity [W/m/K]\", \"Density [kg/m<sup>3</sup>]\", \"Heat Capacity [J/Kg/K]\", \"Viscosity [Pa s]\", \"Saturation Pressure [Pa]\"\n"); |
212 |
do { |
do { |
213 |
|
|
214 |
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)); |
fprintf(out,"%e\t%e\t%e\t%e\t%e\t%e\n",T-273.0,eval_conductivity(test_liq, T, 0.39),eval_density(test_liq, T, 0.39),eval_specific_heat(test_liq, T, 0.39),eval_viscosity(test_liq, T, 0.39),eval_saturation_pressure(test_liq, T, 0.39)); |
215 |
T+=1; |
T+=1; |
216 |
|
|
217 |
} while(T<=test_liq->T_max); |
} while(T<=test_liq->T_max); |