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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3115 - (show annotations) (download) (as text)
Wed Jun 8 22:38:35 2016 UTC (6 years, 2 months ago) by vishnu
File MIME type: text/x-csrc
File size: 3550 byte(s)
test code for input file parsing and loading data structures: complete
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <assert.h>
6
7 #define BUF 1024
8
9 #define MAXC 10
10
11 #define PATH "incomp_liq_data/ZS55.dat"
12
13 #define ABORT \
14 printf("\nExiting Program!\n"); \
15 exit(0);
16
17 #define READ(Q) \
18 pos = ftell(in); \
19 while(!strstr(word,"},")) { \
20 fgets(word,BUF,in); \
21 if(strstr(word,"\"type\": \"notdefined\"")) { \
22 test_liq->Q.type = (char*)malloc(sizeof("notdefined")); \
23 strcpy(test_liq->Q.type,"notdefined"); \
24 } \
25 else if(strstr(word,"\"type\": \"polynomial\"")) { \
26 test_liq->Q.type = (char*)malloc(sizeof("polynomial")); \
27 strcpy(test_liq->Q.type,"polynomial"); \
28 } \
29 else if(strstr(word,"\"type\": \"exppolynomial\"")) { \
30 test_liq->Q.type = (char*)malloc(sizeof("exppolynomial")); \
31 strcpy(test_liq->Q.type,"exppolynomial"); \
32 } \
33 } \
34 if(strcmp(test_liq->Q.type,"notdefined")) { \
35 fseek(in,pos,SEEK_SET); \
36 fgets(word,BUF,in); \
37 double coefs[MAXC]; \
38 int numc = 0; \
39 while(!strstr(word,"},")) { \
40 if(strstr(word,"NRMS")) { \
41 coefs[numc] = extracted_number(word); \
42 numc++; \
43 assert(numc==1); \
44 } \
45 else if(!strstr(word,"coeffs")&&strstr(word,"[")) { \
46 fgets(word,BUF,in); \
47 coefs[numc] = to_number(word); \
48 numc++; \
49 assert(numc<MAXC); \
50 } \
51 fgets(word,BUF,in); \
52 } \
53 test_liq->Q.coeff = (double*)malloc(numc*sizeof(double)); \
54 test_liq->Q.numc = numc; \
55 while(numc) { \
56 numc--; \
57 test_liq->Q.coeff[numc] = coefs[numc]; \
58 printf("%e\n",test_liq->Q.coeff[numc]); \
59 } \
60 }
61
62 typedef struct {
63
64 char* type;
65 double* coeff;
66 int numc;
67
68 } coefficients;
69
70 typedef struct {
71
72 coefficients T_freeze, conductivity, density, specific_heat, viscosity;
73 char* x_id;
74 char* description;
75 double T_base, T_max, T_min, T_minPsat, x_base, x_max, x_min;
76
77 } fprops;
78
79 double extracted_number(char w[BUF]) {
80
81 char *token;
82
83 /* get the first token */
84 token = strtok(w, " ,");
85
86 /* get the second token which is the number*/
87 token = strtok(NULL, " ,");
88
89 return (atof(token));
90
91 }
92
93 double to_number(char w[BUF]) {
94
95 char *token;
96
97 /* get the first token */
98 token = strtok(w, " ");
99
100 return (atof(token));
101
102 }
103
104 int main() {
105
106 FILE* in;
107
108 fprops* test_liq;
109 test_liq = (fprops*)malloc(sizeof(fprops));
110
111 long int pos;
112
113 if(!(in = fopen(PATH,"r"))) {
114
115 printf("\nInput file cannot be located.\n");
116 ABORT
117
118 }
119
120 char word[BUF];
121
122 while(!feof(in)) {
123
124 fgets(word,BUF,in);
125
126 if(strstr(word,"T_freeze")) { READ(T_freeze); } // not required for incompressible EOS; implemented to check input coefficients of "type": "notdefined"
127 else if(strstr(word,"Tbase")) test_liq->T_base = extracted_number(word);
128 else if(strstr(word,"Tmax")) test_liq->T_max = extracted_number(word);
129 else if(strstr(word,"Tmin")) test_liq->T_min = extracted_number(word);
130 else if(strstr(word,"TminPsat")) test_liq->T_minPsat = extracted_number(word);
131 else if(strstr(word,"conductivity")) { READ(conductivity); }
132 else if(strstr(word,"density")) { READ(density); }
133 else if(strstr(word,"specific_heat")) { READ(specific_heat); }
134 else if(strstr(word,"viscosity")) { READ(viscosity); }
135 else if(strstr(word,"xbase")) test_liq->x_base = extracted_number(word);
136 else if(strstr(word,"xmax")) test_liq->x_max = extracted_number(word);
137 else if(strstr(word,"xmin")) test_liq->x_min = extracted_number(word);
138 else if(strstr(word,"\"xid\": \"pure\"")) {
139 test_liq->x_id = (char*)malloc(sizeof("pure"));
140 strcpy(test_liq->x_id,"pure");
141 }
142
143 }
144
145 fclose(in);
146
147 return 0;
148
149 }

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