PLUTO
runtime_setup.c
Go to the documentation of this file.
1 /* ///////////////////////////////////////////////////////////////////// */
2 /*!
3  \file
4  \brief Read runtime information from pluto.ini.
5 
6  Parse and read runtime information from the initialization file
7  pluto.ini (default) and sets value of the runtime structure.
8 
9  \authors A. Mignone (mignone@ph.unito.it)
10  \date Aug 24, 2015
11 */
12 /* ///////////////////////////////////////////////////////////////////// */
13 #include "pluto.h"
14 
15 #define NOPT 32 /* # of possible options in a menu */
16 #define NLEN 128 /* # default string length */
17 
18 #define COMPARE(s1,s2,ii) \
19  for ( (ii) = 1 ; (ii) < NOPT && !(strcmp ( (s1), (s2)) == 0); (ii)++);
20 
21 /* ********************************************************************* */
22 int RuntimeSetup (Runtime *runtime, Cmd_Line *cmd_line, char *ini_file)
23 /*!
24  * Open and parse the runtime initialization file.
25  * Assign values to the runtime structure.
26  *
27  * \param [out] runtime pointer to a Runtime structure
28  * \param [in] cmd_line pointer to a Cmd_Line structure (useful, e.g.,
29  * to resize the domain using the \c -xres option)
30  * \param [in] ini_file the name of the initialization file (default
31  * is "pluto.ini") specified with the \c -i option.
32  *
33  *********************************************************************** */
34 {
35  int idim, ip, ipos, itype, nlines;
36  char *bound_opt[NOPT], str_var[512], *str;
37  char *glabel[] = {"X1-grid", "X2-grid","X3-grid"};
38  char *bbeg_label[] = {"X1-beg", "X2-beg","X3-beg"};
39  char *bend_label[] = {"X1-end", "X2-end","X3-end"};
40  double dbl_var, rx;
41  Output *output;
42  FILE *fp;
43 
44  for (itype = 0; itype < NOPT; itype++) {
45  bound_opt[itype] = "0000";
46  }
47 
48 /* ---------------------------------------------------
49  available options are given as two set of names;
50  This facilitates when updating the code and
51  people are too lazy to read the manual !
52  --------------------------------------------------- */
53 
54  bound_opt[OUTFLOW] = "outflow";
55  bound_opt[REFLECTIVE] = "reflective";
56  bound_opt[AXISYMMETRIC] = "axisymmetric";
57  bound_opt[EQTSYMMETRIC] = "eqtsymmetric";
58  bound_opt[PERIODIC] = "periodic";
59  bound_opt[SHEARING] = "shearingbox";
60  bound_opt[USERDEF] = "userdef";
61 
62  runtime->log_freq = 1; /* -- default -- */
63 
64  nlines = ParamFileRead(ini_file);
65 
66 /* ------------------------------------------------------------
67  [Grid] Section
68  ------------------------------------------------------------ */
69 
70  for (idim = 0; idim < 3; idim++){
71  runtime->npatch[idim] = atoi(ParamFileGet(glabel[idim], 1));
72  runtime->npoint[idim] = 0;
73 
74  ipos = 1;
75  for (ip = 1; ip <= runtime->npatch[idim]; ip++) {
76 
77  runtime->patch_left_node[idim][ip] = atof(ParamFileGet(glabel[idim], ++ipos));
78  runtime->patch_npoint[idim][ip] = atoi(ParamFileGet(glabel[idim], ++ipos));
79  runtime->npoint[idim] += runtime->patch_npoint[idim][ip];
80  runtime->grid_is_uniform[idim] = 0;
81 
82  strcpy (str_var, ParamFileGet(glabel[idim], ++ipos));
83 /*
84 printf ("%f %d %s\n",runtime->patch_left_node[idim][ip],runtime->patch_npoint[idim][ip],str_var);
85 */
86  if (strcmp(str_var,"u") == 0 || strcmp(str_var,"uniform") == 0) {
87  runtime->patch_type[idim][ip] = UNIFORM_GRID;
88  if (runtime->npatch[idim] == 1) runtime->grid_is_uniform[idim] = 1;
89  }else if (strcmp(str_var,"s") == 0 || strcmp(str_var,"strecthed") == 0) {
90  runtime->patch_type[idim][ip] = STRETCHED_GRID;
91  }else if (strcmp(str_var,"l+") == 0){
92  runtime->patch_type[idim][ip] = LOGARITHMIC_INC_GRID;
93  }else if (strcmp(str_var,"l-") == 0){
94  runtime->patch_type[idim][ip] = LOGARITHMIC_DEC_GRID;
95  }else{
96  printf ("\nSetup: You must specify either 'u', 's', 'l+' or 'l-' as grid-type in %s\n",
97  ini_file);
98  QUIT_PLUTO(1);
99  }
100  }
101 
102  runtime->patch_left_node[idim][ip] = atof(ParamFileGet(glabel[idim], ++ipos));
103 
104  if ( (ipos+1) != (runtime->npatch[idim]*3 + 3)) {
105  printf ("! Setup: domain #%d setup is not properly defined \n", idim);
106  QUIT_PLUTO(1);
107  }
108  if (idim >= DIMENSIONS && runtime->npoint[idim] != 1) {
109  printf ("! Setup: %d point(s) on dim. %d is NOT valid, resetting to 1\n",
110  runtime->npoint[idim],idim+1);
111  runtime->npoint[idim] = 1;
112  runtime->npatch[idim] = 1;
113  runtime->patch_npoint[idim][1] = 1;
114  }
115  }
116 
117 /* ------------------------------------------------------------
118  Change the resolution if cmd_line->xres has been given
119  ------------------------------------------------------------ */
120 
121  if (cmd_line->xres > 1) {
122  rx = (double)cmd_line->xres/(double)runtime->patch_npoint[IDIR][1];
123  for (idim = 0; idim < DIMENSIONS; idim++){
124  if (runtime->npatch[idim] > 1){
125  printf ("! Setup: -xres option works on uniform, single patch grid\n");
126  QUIT_PLUTO(1);
127  }
128 
129  dbl_var = (double)runtime->patch_npoint[idim][1];
130  runtime->patch_npoint[idim][1] = MAX( (int)(dbl_var*rx), 1);
131  dbl_var = (double)runtime->npoint[idim];
132  runtime->npoint[idim] = MAX( (int)(dbl_var*rx), 1);
133  }
134  }
135 
136 /* ------------------------------------------------------------
137  [Time] Section
138  ------------------------------------------------------------ */
139 
140  runtime->cfl = atof(ParamFileGet("CFL", 1));
141 
142  if (ParamExist ("CFL_par")) runtime->cfl_par = atof(ParamFileGet("CFL_par", 1));
143  else runtime->cfl_par = 0.8/(double)DIMENSIONS;
144 
145  if (ParamExist ("rmax_par")) runtime->rmax_par = atof(ParamFileGet("rmax_par", 1));
146  else runtime->rmax_par = 100.0;
147 
148  runtime->cfl_max_var = atof(ParamFileGet("CFL_max_var", 1));
149  runtime->tstop = atof(ParamFileGet("tstop", 1));
150  runtime->first_dt = atof(ParamFileGet("first_dt", 1));
151 
152 /* ------------------------------------------------------------
153  [Solver] Section
154  ------------------------------------------------------------ */
155 
156  sprintf (runtime->solv_type,"%s",ParamFileGet("Solver",1));
157 
158 /* ------------------------------------------------------------
159  [Boundary] Section
160  ------------------------------------------------------------ */
161 
162  for (idim = 0; idim < 3; idim++){
163 
164  str = ParamFileGet(bbeg_label[idim], 1);
165  COMPARE (str, bound_opt[itype], itype);
166  if (itype == NOPT) {
167  printf ("! Setup: don't know how to put left boundary '%s' \n", str);
168  QUIT_PLUTO(1);
169  }
170  runtime->left_bound[idim] = itype;
171  }
172 
173  for (idim = 0; idim < 3; idim++){
174 
175  str = ParamFileGet(bend_label[idim], 1);
176  COMPARE (str, bound_opt[itype], itype);
177  if (itype == NOPT) {
178  printf ("! Setup: don't know how to put left boundary '%s' \n", str);
179  QUIT_PLUTO(1);
180  }
181  runtime->right_bound[idim] = itype;
182  }
183 
184 /* ------------------------------------------------------------
185  [Static Grid Output] Section
186  ------------------------------------------------------------ */
187 
188 #ifndef CHOMBO
189  runtime->user_var = atoi(ParamFileGet("uservar", 1));
190  for (ip = 0; ip < runtime->user_var; ip++){
191  if ( (str = ParamFileGet("uservar", 2 + ip)) != NULL){
192  sprintf (runtime->user_var_name[ip], "%s", str);
193  }else{
194  printf ("! Setup: missing name after user var name '%s'\n",
195  runtime->user_var_name[ip-1]);
196  QUIT_PLUTO(1);
197  }
198  }
199 
200 /* ---- set output directory ---- */
201 
202  sprintf (runtime->output_dir, "%s","./"); /* default value is current directory */
203  if (ParamExist("output_dir")){
204  str = ParamFileGet("output_dir",1);
205  sprintf (runtime->output_dir, "%s",str);
206  }
207 
208 /* -- check if we have write access and if the directory exists -- */
209 
210  sprintf (str_var,"%s/tmp09123.txt",runtime->output_dir); /* -- test file -- */
211  fp = fopen(str_var,"w"); /* -- open test file for writing -- */
212  if (fp == NULL){
213  printf ("! Setup: cannot access directory '%s'.\n", runtime->output_dir);
214  printf ("! Please check that the directory exists\n");
215  printf ("! and you have write permission.\n");
216  QUIT_PLUTO(1);
217  }else{
218  fclose(fp);
219  remove(str_var); /* -- remove file -- */
220  }
221 
222 /* ---- dbl output ---- */
223 
224  ipos = 0;
225  output = runtime->output + (ipos++);
226  output->type = DBL_OUTPUT;
227  output->cgs = 0; /* cannot write .dbl using cgs units */
228  GetOutputFrequency(output, "dbl");
229 
230  sprintf (output->mode,"%s",ParamFileGet("dbl",3));
231  #ifdef USE_ASYNC_IO
232  if ( strcmp(output->mode,"single_file")
233  && strcmp(output->mode,"single_file_async")
234  && strcmp(output->mode,"multiple_files")){
235  printf ("! Setup: expecting 'single_file', 'single_file_async' ");
236  printf ("or 'multiple_files' in dbl output\n");
237  QUIT_PLUTO(1);
238  }
239  #else
240  if ( strcmp(output->mode,"single_file")
241  && strcmp(output->mode,"multiple_files")){
242  printf (
243  "! Setup: expecting 'single_file' or 'multiple_files' in dbl output\n");
244  QUIT_PLUTO(1);
245  }
246  #endif
247 
248  /* ---- flt output ---- */
249 
250  if (ParamExist("flt")){
251  output = runtime->output + (ipos++);
252  output->type = FLT_OUTPUT;
253  GetOutputFrequency(output, "flt");
254 
255  sprintf (output->mode,"%s",ParamFileGet("flt",3));
256  #ifdef USE_ASYNC_IO
257  if ( strcmp(output->mode,"single_file")
258  && strcmp(output->mode,"single_file_async")
259  && strcmp(output->mode,"multiple_files")){
260  printf ("! Setup: expecting 'single_file', 'single_file_async' ");
261  printf ("or 'multiple_files' in flt output\n");
262  QUIT_PLUTO(1);
263  }
264  #else
265  if ( strcmp(output->mode,"single_file")
266  && strcmp(output->mode,"multiple_files")){
267  printf (
268  "! Setup: expecting 'single_file' or 'multiple_files' in flt output\n");
269  QUIT_PLUTO(1);
270  }
271  #endif
272  if (ParamFileHasBoth ("flt","cgs")) output->cgs = 1;
273  else output->cgs = 0;
274  }
275 
276  /* -- hdf5 output -- */
277 
278  if (ParamExist("dbl.h5")){
279  output = runtime->output + (ipos++);
280  output->type = DBL_H5_OUTPUT;
281  output->cgs = 0; /* cannot write .h5 using cgs units */
282  GetOutputFrequency(output, "dbl.h5");
283  }
284  if (ParamExist("flt.h5")){
285  output = runtime->output + (ipos++);
286  output->type = FLT_H5_OUTPUT;
287  output->cgs = 0; /* cannot write .h5 using cgs units */
288  GetOutputFrequency(output, "flt.h5");
289  }
290 
291  /* -- vtk output -- */
292 
293  if (ParamExist ("vtk")){
294  output = runtime->output + (ipos++);
295  output->type = VTK_OUTPUT;
296  GetOutputFrequency(output, "vtk");
297 
298  if (ParamFileGet("vtk",3) == NULL){
299  printf ("! Setup: extra field missing in vtk output\n");
300  QUIT_PLUTO(1);
301  }
302  sprintf (output->mode,"%s",ParamFileGet("vtk",3));
303  if ( strcmp(output->mode,"single_file")
304  && strcmp(output->mode,"multiple_files")){
305  printf ("! Setup: expecting 'single_file' or 'multiple_files' in\n");
306  printf (" vtk output\n");
307  QUIT_PLUTO(1);
308  }
309  if (ParamFileHasBoth ("vtk","cgs")) output->cgs = 1;
310  else output->cgs = 0;
311  }
312 
313  /* -- tab output -- */
314 
315  if (ParamExist ("tab")){
316  output = runtime->output + (ipos++);
317  output->type = TAB_OUTPUT;
318  GetOutputFrequency(output, "tab");
319  if (ParamFileHasBoth ("tab","cgs")) output->cgs = 1;
320  else output->cgs = 0;
321  }
322 
323  /* -- ppm output -- */
324 
325  if (ParamExist ("ppm")){
326  output = runtime->output + (ipos++);
327  output->type = PPM_OUTPUT;
328  output->cgs = 0; /* Cannot write ppm in cgs units */
329  GetOutputFrequency(output, "ppm");
330  }
331 
332  /* -- png output -- */
333 
334  if (ParamExist ("png")){
335  output = runtime->output + (ipos++);
336  output->type = PNG_OUTPUT;
337  output->cgs = 0; /* Cannot write png in cgs units */
338  GetOutputFrequency(output, "png");
339  }
340 
341 
342  /* -- log frequency -- */
343 
344  runtime->log_freq = atoi(ParamFileGet("log", 1));
345  runtime->log_freq = MAX(runtime->log_freq, 1);
346 
347  /* -- set default for remaining output type -- */
348 
349  while (ipos < MAX_OUTPUT_TYPES){
350  output = runtime->output + ipos;
351  output->type = -1;
352  output->cgs = 0;
353  output->dt = -1.0;
354  output->dn = -1;
355  output->dclock = -1.0;
356  ipos++;
357  }
358 
359  /* -- analysis -- */
360 
361  if (ParamExist ("analysis")){
362  runtime->anl_dt = atof(ParamFileGet("analysis", 1));
363  runtime->anl_dn = atoi(ParamFileGet("analysis", 2));
364  }else{
365  runtime->anl_dt = -1.0; /* -- defaults -- */
366  runtime->anl_dn = -1;
367  }
368 #endif
369 
370 #ifdef CHOMBO
371 
372 /* ------------------------------------------------------------
373  [Chombo HDF5 output] section
374  ------------------------------------------------------------ */
375 
376 /* ---- set output directory ---- */
377 
378  sprintf (runtime->output_dir, "%s","./"); /* default value is current directory */
379  if (ParamExist("Output_dir")){
380  str = ParamFileGet("Output_dir",1);
381  sprintf (runtime->output_dir, "%s",str);
382  }
383 
384 /* -- check if we have write access and if the directory exists -- */
385 
386  sprintf (str_var,"%s/tmp09123.txt",runtime->output_dir); /* -- test file -- */
387  fp = fopen(str_var,"w"); /* -- open test file for writing -- */
388  if (fp == NULL){
389  printf ("! Setup: cannot access directory '%s'.\n", runtime->output_dir);
390  printf ("! Please check that the directory exists\n");
391  printf ("! and you have write permission.\n");
392  QUIT_PLUTO(1);
393  }else{
394  fclose(fp);
395  remove(str_var); /* -- remove file -- */
396  }
397 #endif
398 
399 /* ------------------------------------------------------------
400  [Parameters] Section
401  ------------------------------------------------------------ */
402 
403  fp = fopen(ini_file,"r");
404 
405 /* -- find position at "[Parameters" -- */
406 
407  for (ipos = 0; ipos <= nlines; ipos++){
408  fgets(str_var, 512, fp);
409 
410  if (strlen(str_var) > 0) {
411  str = strtok (str_var,"]");
412  if (strcmp(str,"[Parameters") == 0) break;
413  }
414  }
415 
416  fgets(str_var, 512, fp);
417 
418  for (ip = 0; ip < USER_DEF_PARAMETERS; ip++){
419  fscanf (fp,"%s \n", str_var);
420  dbl_var = atof(ParamFileGet(str_var,1));
421  runtime->aux[ip] = dbl_var;
422  fgets(str_var, sizeof(str_var), fp); /* use fgets to advance to next line */
423  }
424  fclose(fp);
425 
426  return(0);
427 }
428 #undef COMPARE
429 #undef NOPT
430 #undef NLEN
431 
432 /* ********************************************************************* */
433 void GetOutputFrequency(Output *output, const char *output_format)
434 /*!
435  * Set the intervals between output files.
436  * This can be done in three different ways:
437  *
438  * - dt: time interval in code units
439  * - dn: step interval
440  * - dclock: actual clock time (in hours)
441  *
442  * However, dn and dclock are mutually exclusive.
443  *
444  *********************************************************************** */
445 {
446  char *str;
447  int len, nhrs, nmin, nsec;
448 
449 /* -- time interval in code units (dt) -- */
450 
451  output->dt = atof(ParamFileGet(output_format, 1));
452 
453 /* -- check the 2nd field and decide to set "dn" or "dclock" -- */
454 
455  str = ParamFileGet(output_format,2);
456  len = strlen(str);
457  if (str[len-1] == 'h'){
458  output->dclock = atof(str); /* clock interval in hours */
459  nhrs = (int)output->dclock; /* integer part */
460  nmin = (int)((output->dclock - nhrs)*100.0); /* remainder in minutes */
461  if (nmin >= 60){
462  printf ("! OutputFrequency: number of minutes exceeds 60 in %s output\n",
463  output_format);
464  QUIT_PLUTO(1);
465  }
466  output->dclock = nhrs*3600.0 + nmin*60; /* convert to seconds */
467  output->dn = -1;
468  }else if (str[len-1] == 'm'){
469  output->dclock = atof(str); /* clock interval in minutes */
470  nmin = (int)output->dclock; /* integer part */
471  nsec = (int)((output->dclock - nmin)*100.0); /* remainder in seconds */
472  if (nsec >= 60){
473  printf ("! OutputFrequency: number of seconds exceeds 60 in %s output\n",
474  output_format);
475  QUIT_PLUTO(1);
476  }
477  output->dclock = nmin*60.0 + nsec;
478  output->dn = -1;
479  }else if (str[len-1] == 's'){
480  output->dclock = atof(str); /* clock interval in seconds */
481  output->dn = -1;
482  }else{
483  output->dclock = -1.0;
484  output->dn = atoi(ParamFileGet(output_format, 2));
485  }
486 
487 }
488 
489 /* ********************************************************************* */
490 static Runtime q;
491 void RuntimeSet(Runtime *runtime)
492 /*!
493  * Store a static copy of the runtime structure for later access.
494  *********************************************************************** */
495 {
496  q = *runtime;
497 }
499 {
500  return &q;
501 }
#define SHEARING
Definition: pluto.h:138
#define MAX(a, b)
Definition: macros.h:101
int user_var
The number of additional user-variables being held in memory and written to disk. ...
Definition: structs.h:264
#define USER_DEF_PARAMETERS
Runtime * RuntimeGet(void)
int ParamExist(const char *label)
Definition: parse_file.c:159
int ParamFileHasBoth(const char *label1, const char *label2)
Definition: parse_file.c:129
#define FLT_H5_OUTPUT
Definition: pluto.h:83
#define PPM_OUTPUT
Definition: pluto.h:85
int npoint[3]
Global number of zones in the interior domain.
Definition: structs.h:256
Output output[MAX_OUTPUT_TYPES]
Definition: structs.h:272
char output_dir[256]
The name of the output directory (output_dir for static PLUTO, Output_dir for PLUTO-Chombo) ...
Definition: structs.h:269
#define LOGARITHMIC_INC_GRID
Definition: pluto.h:40
int left_bound[3]
Array of left boundary types.
Definition: structs.h:257
#define UNIFORM_GRID
Definition: pluto.h:38
int xres
Definition: structs.h:22
char user_var_name[128][128]
Definition: structs.h:268
static int nlines
The total number of lines (including empty ones) contained in the file.
Definition: parse_file.c:47
int grid_is_uniform[3]
Definition: structs.h:259
#define LOGARITHMIC_DEC_GRID
Definition: pluto.h:41
int cgs
when set to 1 saves data in c.g.s units
Definition: structs.h:236
int right_bound[3]
Array of right boundary types.
Definition: structs.h:258
int log_freq
The log frequency (log)
Definition: structs.h:263
void GetOutputFrequency(Output *output, const char *output_format)
double anl_dt
Time step increment for Analysis() ( analysis (double) )
Definition: structs.h:282
double rmax_par
(STS) max ratio between current time step and parabolic time step
Definition: structs.h:278
double dt
time increment between outputs - one per output
Definition: structs.h:245
double patch_left_node[5][16]
Definition: structs.h:273
#define COMPARE(s1, s2, ii)
Definition: runtime_setup.c:18
#define VTK_OUTPUT
Definition: pluto.h:81
#define IDIR
Definition: pluto.h:193
int RuntimeSetup(Runtime *runtime, Cmd_Line *cmd_line, char *ini_file)
Definition: runtime_setup.c:22
#define USERDEF
Definition: pluto.h:139
#define DBL_H5_OUTPUT
Definition: pluto.h:82
void RuntimeSet(Runtime *runtime)
char solv_type[64]
The Riemann solver (Solver)
Definition: structs.h:267
double aux[32]
Definition: structs.h:284
#define STRETCHED_GRID
Definition: pluto.h:39
#define MAX_OUTPUT_TYPES
Definition: pluto.h:95
int ParamFileRead(char *fname)
Definition: parse_file.c:53
double tstop
The final integration time (tstop)
Definition: structs.h:280
#define TAB_OUTPUT
Definition: pluto.h:84
#define FLT_OUTPUT
Definition: pluto.h:80
#define NOPT
Definition: runtime_setup.c:15
PLUTO main header file.
#define OUTFLOW
Definition: pluto.h:133
#define REFLECTIVE
Definition: pluto.h:134
int patch_npoint[5][16]
Definition: structs.h:261
#define DBL_OUTPUT
Definition: pluto.h:79
FILE * fp
Definition: analysis.c:7
char mode[32]
single or multiple files - one per output
Definition: structs.h:241
int patch_type[5][16]
Definition: structs.h:262
int npatch[5]
The number of grid patches.
Definition: structs.h:260
int type
output format (DBL, FLT, ...) - one per output
Definition: structs.h:233
#define AXISYMMETRIC
Definition: pluto.h:135
double first_dt
The initial time step (first_dt)
Definition: structs.h:281
#define PERIODIC
Definition: pluto.h:137
double cfl_par
(STS) parabolic cfl number
Definition: structs.h:277
char * ParamFileGet(const char *label, int pos)
Definition: parse_file.c:86
#define EQTSYMMETRIC
Definition: pluto.h:136
double cfl_max_var
Maximum increment between consecutive time steps (CFL_max_var).
Definition: structs.h:275
static Runtime q
#define QUIT_PLUTO(e_code)
Definition: macros.h:125
int anl_dn
Definition: structs.h:266
#define PNG_OUTPUT
Definition: pluto.h:86
#define DIMENSIONS
Definition: definitions_01.h:2
double dclock
time increment in clock hours - one per output
Definition: structs.h:246
int dn
step increment between outputs - one per output
Definition: structs.h:238
double cfl
Hyperbolic cfl number (CFL)
Definition: structs.h:274