HydroCODE_2D 0.1
This is a implementation of fully explict forward Euler scheme for 2-D Euler equations of motion on Eulerian coordinate
config_handle.c
浏览该文件的文档.
1
6#include <stdio.h>
7#include <string.h>
8#include <stdlib.h>
9#include <math.h>
10#include <stdbool.h>
11#include <errno.h>
12#include <ctype.h>
13#include <limits.h>
14
15#include "../include/var_struc.h"
16
17/*
18 * To realize cross-platform programming.
19 * ACCESS: Determine access permissions for files or folders.
20 */
21#ifdef _WIN32
22#include <io.h>
23/*
24 * m=0: Test for existence.
25 * m=2: Test for write permission.
26 * m=4: Test for read permission.
27 */
28#define ACCESS(a,m) _access((a),(m))
29#elif __linux__
30#include <unistd.h>
31#define ACCESS(a,m) access((a),(m))
32#endif
33
34
38static void config_check(void)
39{
40 const int dim = (int)config[0];
41 printf(" dimension\t= %d\n", dim);
42
43 // Maximum number of time steps
44 if(isfinite(config[1]) && config[1] >= 0.0)
45 {
46 config[5] = isfinite(config[5]) ? config[5] : (double)INT_MAX;
47 printf(" total time\t= %g\n", config[1]);
48 }
49 else if(!isfinite(config[5]))
50 {
51 fprintf(stderr, "The total time or the maximum number of time steps must be setted properly!\n");
52 exit(2);
53 }
54 else
55 {
56 config[1] = INFINITY;
57 if(isfinite(config[16]))
58 {
59 printf(" total time\t= %g * %d = %g\n", config[16], (int)config[5], config[16]*(int)config[5]);
60 printf(" delta_t\t= %g\n", config[16]);
61 }
62 }
63 printf(" time step\t= %d\n", (int)config[5]);
64
65 if(isinf(config[4]))
66 config[4] = EPS;
67 double eps = config[4];
68 if(eps < 0.0 || eps > 0.01)
69 {
70 fprintf(stderr, "eps(%f) should in (0, 0.01)!\n", eps);
71 exit(2);
72 }
73 printf(" eps\t\t= %g\n", eps);
74
75 if(isinf(config[6]))
76 config[6] = 1.4;
77 else if(config[6] < 1.0 + eps)
78 {
79 fprintf(stderr, "The constant of the perfect gas(%f) should be larger than 1.0!\n", config[6]);
80 exit(2);
81 }
82 printf(" gamma\t\t= %g\n", config[6]);
83
84 if (isinf(config[7]))
85 {
86 switch(dim)
87 {
88 case 1:
89 config[7] = 0.9; break;
90 case 2:
91 config[7] = 0.45; break;
92 }
93 }
94 else if(config[7] > 1.0 - eps)
95 {
96 fprintf(stderr, "The CFL number(%f) should be smaller than 1.0.\n", config[7]);
97 exit(2);
98 }
99 printf(" CFL number\t= %g\n", config[7]);
100
101 if(isinf(config[41]))
102 config[41] = 1.9;
103 else if(config[41] < -eps || config[41] > 2.0)
104 {
105 fprintf(stderr, "The parameter in minmod limiter(%f) should in [0, 2)!\n", config[41]);
106 exit(2);
107 }
108
109 if(isinf(config[110]))
110 config[110] = 0.72;
111 else if(config[110] < eps)
112 {
113 fprintf(stderr, "The specific heat at constant volume(%f) should be larger than 0.0!\n", config[110]);
114 exit(2);
115 }
116
117 // Specie number
118 config[2] = isfinite(config[2]) ? config[2] : (double)1;
119 // Coordinate framework (EUL/LAG/ALE)
120 config[8] = isfinite(config[8]) ? config[8] : (double)0;
121 // Reconstruction (prim_var/cons_var)
122 config[31] = isfinite(config[31]) ? config[31] : (double)0;
123 // Dimensional splitting
124 config[33] = isfinite(config[33]) ? config[31] : (double)false;
125 // Parameter α in minmod limiter
126 config[41] = isfinite(config[41]) ? config[41] : 1.9;
127 // v_fix
128 config[61] = isfinite(config[61]) ? config[61] : (double)false;
129 // offset_x
130 config[210] = isfinite(config[210]) ? config[210] : 0.0;
131 // offset_y
132 config[211] = isfinite(config[211]) ? config[211] : 0.0;
133 // offset_z
134 config[212] = isfinite(config[212]) ? config[212] : 0.0;
135}
136
145static int config_read(FILE * fp)
146{
147 char one_line[200]; // String to store one line.
148 char *endptr;
149 double tmp;
150 int i, line_num = 1; // Index of config[*], line number.
151
152 while (fgets(one_line, sizeof(one_line), fp) != NULL)
153 {
154 // A line that doesn't begin with digits is a comment.
155 i =strtol(one_line, &endptr, 10);
156 for ( ; isspace(*endptr); endptr++) ;
157
158 // If the value of config[i] doesn't exit, it is 0 by default.
159 if (0 < i && i < N_CONF)
160 {
161 errno = 0;
162 tmp = strtod(endptr, NULL);
163 if(errno == ERANGE)
164 {
165 fprintf(stderr, "Value range error of %d-th configuration in line %d of configuration file!\n", i, line_num);
166 return 1;
167 }
168 else if(isinf(config[i]))
169 printf("%3d-th configuration: %g\n", i, config[i] = tmp);
170 else if(fabs(config[i] - tmp) > EPS)
171 printf("%3d-th configuration is repeatedly assigned with %g and %g(abandon)!\n", i, config[i], tmp);
172 }
173 else if (i != 0 || (*endptr != '#' && *endptr != '\0'))
174 fprintf(stderr, "Warning: unknown row occurrs in line %d of configuration file!\n", line_num);
175 line_num++;
176 }
177 if (ferror(fp))
178 {
179 fprintf(stderr, "Read error occurrs in configuration file!\n");
180 return 0;
181 }
182 return 1;
183}
184
185
191void configurate(const char * add_in)
192{
193 FILE * fp_data;
194 char add[FILENAME_MAX+40];
195 strcpy(add, add_in);
196 strcat(add, "config.txt");
197
198 // Open the configuration data file.
199 if((fp_data = fopen(add, "r")) == NULL)
200 {
201 strcpy(add, add_in);
202 strcat(add, "config.dat");
203 }
204 if((fp_data = fopen(add, "r")) == NULL)
205 {
206 printf("Cannot open configuration data file!\n");
207 exit(1);
208 }
209
210 // Read the configuration data file.
211 if(config_read(fp_data) == 0)
212 {
213 fclose(fp_data);
214 exit(2);
215 }
216 fclose(fp_data);
217
218 printf("Configurated:\n");
219 // Check the configuration data.
220 config_check();
221}
222
223
224void config_write(const char * add_out, const double * cpu_time, const char * name)
225{
226 char file_data[FILENAME_MAX+40];
227 const int dim = (int)config[0];
228 FILE * fp_write;
229
230//======================Write Log File============================
231 strcpy(file_data, add_out);
232 strcat(file_data, "/log");
233 strcat(file_data, ".dat");
234 if((fp_write = fopen(file_data, "w")) == NULL)
235 {
236 printf("Cannot open log output file!\n");
237 exit(1);
238 }
239
240 fprintf(fp_write, "%s is initialized with %d grids.\n\n", name, (int)config[3]);
241 fprintf(fp_write, "Configurated:\n");
242 fprintf(fp_write, "dim\t\t= %d\n", dim);
243 if(isfinite(config[1]))
244 fprintf(fp_write, "t_all\t= %d\n", (int)config[1]);
245 else if(isfinite(config[16]))
246 fprintf(fp_write, "tau\t\t= %g\n", config[16]);
247 fprintf(fp_write, "eps\t\t= %g\n", config[4]);
248 fprintf(fp_write, "gamma\t= %g\n", config[6]);
249 fprintf(fp_write, "CFL\t\t= %g\n", config[7]);
250 fprintf(fp_write, "h\t\t= %g\n", config[10]);
251 fprintf(fp_write, "bond\t= %d\n", (int)config[17]);
252 if(dim == 2)
253 {
254 fprintf(fp_write, "h_y\t\t= %g\n", config[11]);
255 fprintf(fp_write, "bond_y\t= %d\n", (int)config[18]);
256 }
257 fprintf(fp_write, "\nA total of %d time steps are computed.\n", (int)config[5]);
258 /*
259 double * sum = calloc(N, sizeof(double));
260 sum[0] = 0.0;
261 fprintf(fp_write, "CPU time for each step:");
262 for(k = 1; k < N; ++k)
263 {
264 fprintf(fp_write, "%.18f ", cpu_time[k]);
265 sum[k] = sum[k-1] + cpu_time[k];
266 }
267 fprintf(fp_write, "\nTotal CPU time at each step:");
268 for(k = 1; k < N; ++k)
269 fprintf(fp_write, "%.18f ", sum[k]);
270 free(sum);
271 sum = NULL;
272 */
273 fclose(fp_write);
274}
static int config_read(FILE *fp)
This function read the configuration data file, and store the configuration data in the array "config...
void configurate(const char *add_in)
This function controls configuration data reading and validation.
static void config_check(void)
This function check whether the configuration data is reasonable and set the default.
Definition: config_handle.c:38
void config_write(const char *add_out, const double *cpu_time, const char *name)
#define EPS
If the system does not set, the default largest value can be seen as zero is EPS.
Definition: var_struc.h:19
double config[]
Initial configuration data array.
Definition: hydrocode.c:115
#define N_CONF
Define the number of configuration parameters.
Definition: var_struc.h:24