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
hydrocode.c
浏览该文件的文档.
1
86#include <errno.h>
87#include <stdio.h>
88#include <stdlib.h>
89#include <string.h>
90#include <math.h>
91
92#include "../include/var_struc.h"
93#include "../include/file_io.h"
94#include "../include/finite_volume.h"
95
96
97double config[N_CONF];
98
102#define CV_INIT_MEM(v, N) \
103 do { \
104 CV.v = (double **)malloc(N * sizeof(double *)); \
105 if(CV.v == NULL) \
106 { \
107 printf("NOT enough memory! %s\n", #v); \
108 retval = 5; \
109 goto return_NULL; \
110 } \
111 CV.v[0] = FV0.v + 1; \
112 for(k = 1; k < N; ++k) \
113 { \
114 CV.v[k] = (double *)malloc(m * sizeof(double)); \
115 if(CV.v[k] == NULL) \
116 { \
117 printf("NOT enough memory! %s[%d]\n", #v, k); \
118 retval = 5; \
119 goto return_NULL; \
120 } \
121 } \
122 } while (0)
123
137int main(int argc, char *argv[])
138{
139 printf("\n");
140 int k, j, retval = 0;
141 for (k = 0; k < argc; k++)
142 printf("%s ", argv[k]);
143 printf("\n");
144 printf("TEST:\n %s\n", argv[1]);
145 if(argc < 5)
146 {
147 printf("Test Beginning: ARGuments Counter %d is less than 5.\n", argc);
148 return 4;
149 }
150 else
151 printf("Test Beginning: ARGuments Counter = %d.\n", argc);
152
153 // Initialize configuration data array
154 for(k = 1; k < N_CONF; k++)
155 config[k] = INFINITY;
156
157 // Set dimension.
158 int dim;
159 dim = atoi(argv[3]);
160 if (dim != 1)
161 {
162 printf("No appropriate dimension was entered!\n");
163 return 4;
164 }
165 config[0] = (double)dim;
166
167 printf("Configurating:\n");
168 char * endptr;
169 double conf_tmp;
170 for (k = 6; k < argc; k++)
171 {
172 errno = 0;
173 j = strtoul(argv[k], &endptr, 10);
174 if (errno != ERANGE && *endptr == '=')
175 {
176 endptr++;
177 errno = 0;
178 conf_tmp = strtod(endptr, &endptr);
179 if (errno != ERANGE && *endptr == '\0')
180 {
181 config[j] = conf_tmp;
182 printf("%3d-th configuration: %g (ARGument)\n", j, conf_tmp);
183 }
184 else
185 {
186 printf("Configuration error in ARGument variable %d! ERROR after '='!\n", k);
187 return 4;
188 }
189 }
190 else
191 {
192 printf("Configuration error in ARGument variable %d! ERROR before '='!\n", k);
193 return 4;
194 }
195 }
196
197 // Set order and scheme.
198 int order; // 1, 2
199 char * scheme; // Riemann_exact(Godunov), GRP
200 printf("Order[_Scheme]: %s\n",argv[4]);
201 errno = 0;
202 order = strtoul(argv[4], &scheme, 10);
203 if (*scheme == '_')
204 scheme++;
205 else if (*scheme != '\0' || errno == ERANGE)
206 {
207 printf("No order or Wrog scheme!\n");
208 return 4;
209 }
210 config[9] = (double)order;
211
212 /*
213 * We read the initial data files.
214 * The function initialize return a point pointing to the position
215 * of a block of memory consisting (m+1) variables of type double.
216 * The value of first array element of these variables is m.
217 * The following m variables are the initial value.
218 */
219 struct flu_var FV0 = _1D_initialize(argv[1]); // Structure of initial data array pointer.
220 /*
221 * m is the number of initial value as well as the number of grids.
222 * As m is frequently use to represent the number of grids,
223 * we do not use the name such as num_grid here to correspond to
224 * notation in the math theory.
225 */
226 const int m = (int)FV0.RHO[0];
227 const double h = config[10], gamma = config[6];
228 // The number of times steps of the fluid data stored for plotting.
229 const int N = 2; // (int)(config[5]) + 1;
230 double time_plot[2];
231
232 struct cell_var_stru CV = {NULL}; // Structure of fluid variables in computational cells array pointer.
233 double ** X = NULL;
234 double * cpu_time = malloc(N * sizeof(double));
235 X = (double **)malloc(N * sizeof(double *));
236 if(cpu_time == NULL)
237 {
238 printf("NOT enough memory! CPU_time\n");
239 retval = 5;
240 goto return_NULL;
241 }
242 if(X == NULL)
243 {
244 printf("NOT enough memory! X\n");
245 retval = 5;
246 goto return_NULL;
247 }
248 for(k = 0; k < N; ++k)
249 {
250 X[k] = (double *)malloc((m+1) * sizeof(double));
251 if(X[k] == NULL)
252 {
253 printf("NOT enough memory! X[%d]\n", k);
254 retval = 5;
255 goto return_NULL;
256 }
257 }
258 // Initialize arrays of fluid variables in cells.
259 CV_INIT_MEM(RHO, N);
260 CV_INIT_MEM(U, N);
261 CV_INIT_MEM(P, N);
262 CV.E = (double **)malloc(N * sizeof(double *));
263 if(CV.E == NULL)
264 {
265 printf("NOT enough memory! E\n");
266 retval = 5;
267 goto return_NULL;
268 }
269 for(k = 0; k < N; ++k)
270 {
271 CV.E[k] = (double *)malloc(m * sizeof(double));
272 if(CV.E[k] == NULL)
273 {
274 printf("NOT enough memory! E[%d]\n", k);
275 retval = 5;
276 goto return_NULL;
277 }
278 }
279 // Initialize the values of energy in computational cells and x-coordinate of the cell interfaces.
280 for(j = 0; j <= m; ++j)
281 X[0][j] = h * j;
282 for(j = 0; j < m; ++j)
283 CV.E[0][j] = 0.5*CV.U[0][j]*CV.U[0][j] + CV.P[0][j]/(gamma - 1.0)/CV.RHO[0][j];
284
285 if (strcmp(argv[5],"LAG") == 0) // Use GRP/Godunov scheme to solve it on Lagrangian coordinate.
286 {
287 config[8] = (double)1;
288 switch(order)
289 {
290 case 1:
291 Godunov_solver_LAG_source(m, CV, X, cpu_time, time_plot);
292 break;
293 case 2:
294 GRP_solver_LAG_source(m, CV, X, cpu_time, time_plot);
295 break;
296 default:
297 printf("NOT appropriate order of the scheme! The order is %d.\n", order);
298 retval = 4;
299 goto return_NULL;
300 }
301 }
302 else if (strcmp(argv[5],"EUL") == 0) // Use GRP/Godunov scheme to solve it on Eulerian coordinate.
303 {
304 config[8] = (double)0;
305 for (k = 1; k < N; ++k)
306 for (j = 0; j <= m; ++j)
307 X[k][j] = X[0][j];
308 switch(order)
309 {
310 case 1:
311 Godunov_solver_EUL_source(m, CV, cpu_time, time_plot);
312 break;
313 case 2:
314 GRP_solver_EUL_source(m, CV, cpu_time, time_plot);
315 break;
316 default:
317 printf("NOT appropriate order of the scheme! The order is %d.\n", order);
318 retval = 4;
319 goto return_NULL;
320 }
321 }
322 else
323 {
324 printf("NOT appropriate coordinate framework! The framework is %s.\n", argv[5]);
325 retval = 4;
326 goto return_NULL;
327 }
328
329 // Write the final data down.
330 _1D_file_write(m, N, CV, X, cpu_time, argv[2], time_plot);
331
332 return_NULL:
333 free(FV0.RHO);
334 free(FV0.U);
335 free(FV0.P);
336 FV0.RHO = NULL;
337 FV0.U = NULL;
338 FV0.P = NULL;
339 for(k = 1; k < N; ++k)
340 {
341 free(CV.E[k]);
342 free(CV.RHO[k]);
343 free(CV.U[k]);
344 free(CV.P[k]);
345 free(X[k]);
346 CV.E[k] = NULL;
347 CV.RHO[k] = NULL;
348 CV.U[k] = NULL;
349 CV.P[k] = NULL;
350 X[k] = NULL;
351 }
352 free(CV.E[0]);
353 CV.E[0] = NULL;
354 CV.RHO[0] = NULL;
355 CV.U[0] = NULL;
356 CV.P[0] = NULL;
357 free(CV.E);
358 free(CV.RHO);
359 free(CV.U);
360 free(CV.P);
361 CV.E = NULL;
362 CV.RHO = NULL;
363 CV.U = NULL;
364 CV.P = NULL;
365 free(X);
366 X = NULL;
367 free(cpu_time);
368 cpu_time = NULL;
369
370 return retval;
371}
struct flu_var _1D_initialize(const char *name)
This function reads the 1-D initial data file of velocity/pressure/density.
Definition: _1D_file_in.c:70
void _1D_file_write(const int m, const int N, const struct cell_var_stru CV, double *X[], const double *cpu_time, const char *name, const double *time_plot)
This function write the 1-D solution into output .dat files.
Definition: _1D_file_out.c:50
void GRP_solver_LAG_source(const int m, struct cell_var_stru CV, double *X[], double *cpu_time, double *time_plot)
This function use GRP scheme to solve 1-D Euler equations of motion on Lagrangian coordinate.
void Godunov_solver_EUL_source(const int m, struct cell_var_stru CV, double *cpu_time, double *time_plot)
This function use Godunov scheme to solve 1-D Euler equations of motion on Eulerian coordinate.
void GRP_solver_EUL_source(const int m, struct cell_var_stru CV, double *cpu_time, double *time_plot)
This function use GRP scheme to solve 1-D Euler equations of motion on Eulerian coordinate.
void Godunov_solver_LAG_source(const int m, struct cell_var_stru CV, double *X[], double *cpu_time, double *time_plot)
This function use Godunov scheme to solve 1-D Euler equations of motion on Lagrangian coordinate.
int main(int argc, char *argv[])
This is the main function which constructs the main structure of the Lagrangian/Eulerian hydrocode.
Definition: hydrocode.c:137
double config[N_CONF]
Initial configuration data array.
Definition: hydrocode.c:97
#define CV_INIT_MEM(v, N)
N memory allocations to the initial fluid variable 'v' in the structure cell_var_stru.
Definition: hydrocode.c:102
pointer structure of VARiables on STRUctural computational grid CELLs.
Definition: var_struc.h:35
pointer structure of FLUid VARiables.
Definition: var_struc.h:30
double * P
Definition: var_struc.h:31
double * RHO
Definition: var_struc.h:31
double * U
Definition: var_struc.h:31
#define N_CONF
Define the number of configuration parameters.
Definition: var_struc.h:24