HydroCODE_1D 0.1
This is a implementation of fully explict forward Euler scheme for 1-D Euler equations of motion on Lagrangian/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
39static void config_check(void)
40{
41 const int dim = (int)config[0];
42 printf(" dimension\t= %d\n", dim);
43
44 // Maximum number of time steps
45 if(isfinite(config[1]) && config[1] >= 0.0)
46 {
47 config[5] = isfinite(config[5]) ? config[5] : (double)INT_MAX;
48 printf(" total time\t= %g\n", config[1]);
49 }
50 else if(!isfinite(config[5]))
51 {
52 fprintf(stderr, "The total time or the maximum number of time steps must be setted properly!\n");
53 exit(2);
54 }
55 else
56 {
57 config[1] = INFINITY;
58 if(isfinite(config[16]))
59 {
60 printf(" total time\t= %g * %d = %g\n", config[16], (int)config[5], config[16]*(int)config[5]);
61 printf(" delta_t\t= %g\n", config[16]);
62 }
63 }
64 printf(" time step\t= %d\n", (int)config[5]);
65
66 if(isinf(config[4]))
67 config[4] = EPS;
68 double eps = config[4];
69 if(eps < 0.0 || eps > 0.01)
70 {
71 fprintf(stderr, "eps(%f) should in (0, 0.01)!\n", eps);
72 exit(2);
73 }
74 printf(" eps\t\t= %g\n", eps);
75
76 if(isinf(config[6]))
77 config[6] = 1.4;
78 else if(config[6] < 1.0 + eps)
79 {
80 fprintf(stderr, "The constant of the perfect gas(%f) should be larger than 1.0!\n", config[6]);
81 exit(2);
82 }
83 printf(" gamma\t\t= %g\n", config[6]);
84
85 if (isinf(config[7]))
86 {
87 switch(dim)
88 {
89 case 1:
90 config[7] = 0.9; break;
91 case 2:
92 config[7] = 0.45; break;
93 }
94 }
95 else if(config[7] > 1.0 - eps)
96 {
97 fprintf(stderr, "The CFL number(%f) should be smaller than 1.0.\n", config[7]);
98 exit(2);
99 }
100 printf(" CFL number\t= %g\n", config[7]);
101
102 if(isinf(config[41]))
103 config[41] = 1.9;
104 else if(config[41] < -eps || config[41] > 2.0)
105 {
106 fprintf(stderr, "The parameter in minmod limiter(%f) should in [0, 2)!\n", config[41]);
107 exit(2);
108 }
109
110 if(isinf(config[110]))
111 config[110] = 0.72;
112 else if(config[110] < eps)
113 {
114 fprintf(stderr, "The specific heat at constant volume(%f) should be larger than 0.0!\n", config[110]);
115 exit(2);
116 }
117
118 // Specie number
119 config[2] = isfinite(config[2]) ? config[2] : (double)1;
120 // Coordinate framework (EUL/LAG/ALE)
121 config[8] = isfinite(config[8]) ? config[8] : (double)0;
122 // r_0
123 config[20] = isfinite(config[20]) ? config[20] : config[10];
124 // Reconstruction approach (interfacial value / lsq)
125 config[30] = isfinite(config[30]) ? config[30] : (double)0;
126 // Reconstruction variable (prim_var/cons_var)
127 config[31] = isfinite(config[31]) ? config[31] : (double)0;
128 // Output initial data
129 config[32] = isfinite(config[32]) ? config[32] : (double)true;
130 // Dimensional splitting
131 config[33] = isfinite(config[33]) ? config[33] : (double)false;
132 // Slope limiter for least square procedure (Venkatakrishnan/Barth-Jespersen)
133 config[40] = isfinite(config[40]) ? config[40] : (double)0;
134 // Parameter α in minmod limiter
135 config[41] = isfinite(config[41]) ? config[41] : 1.9;
136 // Slope limiter for minmod VIP
137 config[42] = isfinite(config[42]) ? config[42] : (double)1;
138 // Runge-Kutta time discretization
139 config[53] = isfinite(config[53]) ? config[53] : (double)false;
140 // Conservative variable (U_gamma) ργ
141 config[60] = isfinite(config[60]) ? config[60] : (double)false;
142 // v_fix: Shear velocity
143 config[61] = isfinite(config[61]) ? config[61] : (double)0;
144 // Offset of the upper and downside periodic boundary
145 config[70] = isfinite(config[70]) ? config[70] : (double)0;
146 // offset_x: Grid offset in x direction
147 config[210] = isfinite(config[210]) ? config[210] : 0.0;
148 // offset_y: Grid offset in y direction
149 config[211] = isfinite(config[211]) ? config[211] : 0.0;
150 // offset_z: Grid offset in z direction
151 config[212] = isfinite(config[212]) ? config[212] : 0.0;
152}
153
162static int config_read(FILE * fp)
163{
164 char one_line[200]; // String to store one line.
165 char *endptr;
166 double tmp;
167 int i, line_num = 1; // Index of config[*], line number.
168
169 while (fgets(one_line, sizeof(one_line), fp) != NULL)
170 {
171 // A line that doesn't begin with digits is a comment.
172 i = strtoul(one_line, &endptr, 10);
173 for ( ; isspace(*endptr); endptr++) ;
174
175 // If the value of config[i] doesn't exit, it is 0 by default.
176 if (0 < i && i < N_CONF)
177 {
178 errno = 0;
179 tmp = strtod(endptr, NULL);
180 if(errno == ERANGE)
181 {
182 fprintf(stderr, "Value range error of %d-th configuration in line %d of configuration file!\n", i, line_num);
183 return 1;
184 }
185 else if(isinf(config[i]))
186 printf("%3d-th configuration: %g\n", i, config[i] = tmp);
187 else if(fabs(config[i] - tmp) > EPS)
188 printf("%3d-th configuration is repeatedly assigned with %g and %g(abandon)!\n", i, config[i], tmp);
189 }
190 else if (i != 0 || (*endptr != '#' && *endptr != '\0'))
191 fprintf(stderr, "Warning: unknown row occurrs in line %d of configuration file!\n", line_num);
192 line_num++;
193 }
194 if (ferror(fp))
195 {
196 fprintf(stderr, "Read error occurrs in configuration file!\n");
197 return 0;
198 }
199 return 1;
200}
201
202
208void configurate(const char * add_in)
209{
210 FILE * fp_data;
211 char add[FILENAME_MAX+40];
212 strcpy(add, add_in);
213 strcat(add, "config.txt");
214 // Open the configuration data file.
215 if((fp_data = fopen(add, "r")) == NULL)
216 {
217 strcpy(add, add_in);
218 strcat(add, "config.dat");
219 }
220 if((fp_data = fopen(add, "r")) == NULL)
221 {
222 printf("Cannot open configuration data file!\n");
223 perror(add_in);
224 exit(1);
225 }
226
227 // Read the configuration data file.
228 if(config_read(fp_data) == 0)
229 {
230 fclose(fp_data);
231 exit(2);
232 }
233 fclose(fp_data);
234
235#ifdef _WIN32
236 printf("Configurated:\n");
237#elif __linux__
238 printf("\x1b[42;36mConfigurated:\x1b[0m\n");
239#endif
240 // Check the configuration data.
241 config_check();
242}
243
244
253void config_write(const char * add_out, const double * cpu_time, const char * name)
254{
255 char file_data[FILENAME_MAX+40];
256 const int dim = (int)config[0];
257 FILE * fp_write;
258
259//======================Write Log File============================
260 strcpy(file_data, add_out);
261 strcat(file_data, "log");
262 strcat(file_data, ".dat");
263 if((fp_write = fopen(file_data, "w")) == NULL)
264 {
265 printf("Cannot open log output file!\n");
266 exit(1);
267 }
268
269 fprintf(fp_write, "%s is initialized with %d grids.\n\n", name, (int)config[3]);
270 fprintf(fp_write, "Configurated:\n");
271 fprintf(fp_write, "dim\t\t= %d\n", dim);
272 if(isfinite(config[1]))
273 fprintf(fp_write, "t_all\t= %d\n", (int)config[1]);
274 else if(isfinite(config[16]))
275 fprintf(fp_write, "tau\t\t= %g\n", config[16]);
276 fprintf(fp_write, "eps\t\t= %g\n", config[4]);
277 fprintf(fp_write, "gamma\t= %g\n", config[6]);
278 fprintf(fp_write, "CFL\t\t= %g\n", config[7]);
279 fprintf(fp_write, "h\t\t= %g\n", config[10]);
280 fprintf(fp_write, "bond\t= %d\n", (int)config[17]);
281 if(dim == 2)
282 {
283 fprintf(fp_write, "h_y\t\t= %g\n", config[11]);
284 fprintf(fp_write, "bond_y\t= %d\n", (int)config[18]);
285 }
286 fprintf(fp_write, "\nA total of %d time steps are computed.\n", (int)config[5]);
287 /*
288 double * sum = (double*)calloc(N, sizeof(double));
289 sum[0] = 0.0;
290 fprintf(fp_write, "CPU time for each step:");
291 for(k = 1; k < N; ++k)
292 {
293 fprintf(fp_write, "%.18f ", cpu_time[k]);
294 sum[k] = sum[k-1] + cpu_time[k];
295 }
296 fprintf(fp_write, "\nTotal CPU time at each step:");
297 for(k = 1; k < N; ++k)
298 fprintf(fp_write, "%.18f ", sum[k]);
299 free(sum);
300 sum = NULL;
301 */
302 fclose(fp_write);
303}
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:39
void config_write(const char *add_out, const double *cpu_time, const char *name)
This function write configuration data and program record into the file 'log.dat'.
double config[N_CONF]
Initial configuration data array.
Definition: hydrocode.c:111
#define EPS
If the system does not set, the default largest value can be seen as zero is EPS.
Definition: var_struc.h:36
#define N_CONF
Define the number of configuration parameters.
Definition: var_struc.h:41