PLUTO
restart.c File Reference

Restart PLUTO from binary or HDF5 double precision data files. More...

#include "pluto.h"
Include dependency graph for restart.c:

Go to the source code of this file.

Functions

void RestartFromFile (Runtime *ini, int nrestart, int type, Grid *grid)
 
void RestartGet (Runtime *ini, int nrestart, int out_type, int swap_endian)
 
void RestartDump (Runtime *ini)
 

Variables

static int counter = -1
 

Detailed Description

Restart PLUTO from binary or HDF5 double precision data files.

This file collects the necessary functions for restarting PLUTO from a double precision binary or HDF5 file in the static grid version of the code.

Author
A. Mignone (migno.nosp@m.ne@p.nosp@m.h.uni.nosp@m.to.i.nosp@m.t)
Date
Aug 24, 2015

Definition in file restart.c.

Function Documentation

void RestartDump ( Runtime ini)

Write restart information needed for later restarts.

Definition at line 251 of file restart.c.

256 {
257  int n;
258  char fout[512];
259  Restart restart;
260  FILE *fr;
261 
262 /* --------------------------------------------------
263  Define restart structure elements here
264  -------------------------------------------------- */
265 
266  restart.t = g_time;
267  restart.dt = g_dt;
268  restart.nstep = g_stepNumber;
269  for (n = 0; n < MAX_OUTPUT_TYPES; n++){
270  restart.nfile[n] = ini->output[n].nfile;
271  }
272 
273 /* --------------------------------------------------
274  Dump structure to disk
275  -------------------------------------------------- */
276 
277  counter++;
278  if (prank == 0) {
279  sprintf (fout,"%s/restart.out",ini->output_dir);
280  if (counter == 0) {
281  fr = fopen (fout, "wb");
282  }else {
283  fr = fopen (fout, "r+b");
284  fseek (fr, counter*sizeof(Restart), SEEK_SET);
285  }
286 
287  fwrite (&restart, sizeof(Restart), 1, fr);
288  fclose(fr);
289  }
290 }
double dt
Definition: structs.h:293
static int n
Definition: analysis.c:3
double t
Definition: structs.h:292
Output output[MAX_OUTPUT_TYPES]
Definition: structs.h:272
int nfile
current number being saved - one per output
Definition: structs.h:237
char output_dir[256]
The name of the output directory (output_dir for static PLUTO, Output_dir for PLUTO-Chombo) ...
Definition: structs.h:269
double g_dt
The current integration time step.
Definition: globals.h:118
int prank
Processor rank.
Definition: globals.h:33
long int g_stepNumber
Gives the current integration step number.
Definition: globals.h:97
int nstep
Definition: structs.h:290
static int counter
Definition: restart.c:174
#define MAX_OUTPUT_TYPES
Definition: pluto.h:95
double g_time
The current integration time.
Definition: globals.h:117
int nfile[MAX_OUTPUT_TYPES]
Definition: structs.h:291

Here is the caller graph for this function:

void RestartFromFile ( Runtime ini,
int  nrestart,
int  type,
Grid grid 
)

Read input binary / hdf5 data.

Parameters
[in]ini
[in]nrestart
[in]typespecifies the output data type (type should be either DBL_OUTPUT or DBL_H5_OUTPUT).
[in]gridpointer to an array of Grid structures

Definition at line 17 of file restart.c.

28 {
29  int nv, single_file, origin, nlines=0;
30  int swap_endian=0;
31  char fname[512], fout[512], str[512];
32  double dbl;
33  void *Vpt;
34  Output *output;
35  FILE *fbin;
36 
37 /* ----------------------------------------------------------
38  Get the pointer to the output format specified by "type"
39  ---------------------------------------------------------- */
40 
41  for (nv = 0; nv < MAX_OUTPUT_TYPES; nv++){
42  output = ini->output + nv;
43  if (output->type == type) break;
44  }
45 
46 /* -------------------------------------------------------
47  Compare the endianity of the restart file (by reading
48  the corresponding entry in dbl.out or dbl.h5.out)
49  with that of the current architecture.
50  Turn swap_endian to 1 if they're different.
51  ------------------------------------------------------- */
52 
53  if (prank == 0){
54  if (type == DBL_OUTPUT) {
55  sprintf (fout,"%s/dbl.out",ini->output_dir);
56  fbin = fopen (fout, "r");
57  } else if (type == DBL_H5_OUTPUT) {
58  sprintf (fout,"%s/dbl.h5.out",ini->output_dir);
59  fbin = fopen (fout, "r");
60  }
61  if (fbin == NULL){
62  print1 ("! Restart: cannot find dbl.out or dbl.h5.out\n");
63  QUIT_PLUTO(1);
64  }
65 
66  while (fgets(str, 512, fbin) != 0) nlines++; /* -- count lines in dbl.out -- */
67  rewind(fbin);
68  if (nrestart > nlines-1){
69  printf ("! Restart: position too large\n");
70  QUIT_PLUTO(1);
71  }
72  origin = (nrestart >= 0 ? nrestart:(nlines+nrestart));
73  for (nv = origin; nv--; ) while ( fgetc(fbin) != '\n'){}
74  fscanf(fbin, "%d %lf %lf %d %s %s\n",&nv, &dbl, &dbl, &nv, str, str);
75  if ( (!strcmp(str,"big") && IsLittleEndian()) ||
76  (!strcmp(str,"little") && !IsLittleEndian())) {
77  swap_endian = 1;
78  print1 ("> Restart: endianity is reversed\n");
79  }
80  fclose(fbin);
81  }
82  #ifdef PARALLEL
83  MPI_Bcast (&swap_endian, 1, MPI_INT, 0, MPI_COMM_WORLD);
84  #endif
85 
86 /* ---------------------------------------------
87  Read restart.out and get Restart structure
88  --------------------------------------------- */
89 
90  RestartGet (ini, nrestart, type, swap_endian);
91  if (type == DBL_H5_OUTPUT){
92  #ifdef USE_HDF5
93  ReadHDF5 (output, grid);
94  #endif
95  return;
96  }
97 
98  print1 ("> restarting from file #%d (dbl)\n",output->nfile);
99  single_file = strcmp(output->mode,"single_file") == 0;
100 
101 /* -----------------------------------------------------------------
102  For .dbl output, read data from disk
103  ----------------------------------------------------------------- */
104 
105  if (single_file){
106  int sz;
107  long long offset;
108 
109  sprintf (fname, "%s/data.%04d.dbl", output->dir, output->nfile);
110  offset = 0;
111  #ifndef PARALLEL
112  fbin = OpenBinaryFile (fname, 0, "r");
113  #endif
114  for (nv = 0; nv < output->nvar; nv++) {
115  if (!output->dump_var[nv]) continue;
116 
117  if (output->stag_var[nv] == -1) { /* -- cell-centered data -- */
118  sz = SZ;
119  Vpt = (void *)output->V[nv][0][0];
120  } else if (output->stag_var[nv] == 0) { /* -- x-staggered data -- */
121  sz = SZ_stagx;
122  Vpt = (void *)(output->V[nv][0][0]-1);
123  } else if (output->stag_var[nv] == 1) { /* -- y-staggered data -- */
124  sz = SZ_stagy;
125  Vpt = (void *)output->V[nv][0][-1];
126  } else if (output->stag_var[nv] == 2) { /* -- z-staggered data -- */
127  sz = SZ_stagz;
128  Vpt = (void *)output->V[nv][-1][0];
129  }
130  #ifdef PARALLEL
131  fbin = OpenBinaryFile (fname, sz, "r");
132  AL_Set_offset(sz, offset);
133  #endif
134  ReadBinaryArray (Vpt, sizeof(double), sz, fbin,
135  output->stag_var[nv], swap_endian);
136  #ifdef PARALLEL
137  offset = AL_Get_offset(sz);
138  CloseBinaryFile(fbin, sz);
139  #endif
140  }
141  #ifndef PARALLEL
142  CloseBinaryFile(fbin, sz);
143  #endif
144 
145  }else{
146 
147  int sz;
148  for (nv = 0; nv < output->nvar; nv++) {
149  if (!output->dump_var[nv]) continue;
150  sprintf (fname, "%s/%s.%04d.%s", output->dir, output->var_name[nv],
151  output->nfile, output->ext);
152 
153  if (output->stag_var[nv] == -1) { /* -- cell-centered data -- */
154  sz = SZ;
155  Vpt = (void *)output->V[nv][0][0];
156  } else if (output->stag_var[nv] == 0) { /* -- x-staggered data -- */
157  sz = SZ_stagx;
158  Vpt = (void *)(output->V[nv][0][0]-1);
159  } else if (output->stag_var[nv] == 1) { /* -- y-staggered data -- */
160  sz = SZ_stagy;
161  Vpt = (void *)output->V[nv][0][-1];
162  } else if (output->stag_var[nv] == 2) { /* -- z-staggered data -- */
163  sz = SZ_stagz;
164  Vpt = (void *)output->V[nv][-1][0];
165  }
166  fbin = OpenBinaryFile (fname, sz, "r");
167  ReadBinaryArray (Vpt, sizeof(double), sz, fbin,
168  output->stag_var[nv], swap_endian);
169  CloseBinaryFile (fbin, sz);
170  }
171  }
172 }
void RestartGet(Runtime *ini, int nrestart, int out_type, int swap_endian)
Definition: restart.c:177
void ReadHDF5(Output *output, Grid *grid)
Definition: hdf5_io.c:582
double *** V[64]
pointer to arrays being written - same for all
Definition: structs.h:247
long long AL_Get_offset(int sz_ptr)
Definition: al_io.c:327
Output output[MAX_OUTPUT_TYPES]
Definition: structs.h:272
void print1(const char *fmt,...)
Definition: amrPluto.cpp:511
FILE * OpenBinaryFile(char *filename, int sz, char *mode)
Definition: bin_io.c:31
int nfile
current number being saved - one per output
Definition: structs.h:237
char output_dir[256]
The name of the output directory (output_dir for static PLUTO, Output_dir for PLUTO-Chombo) ...
Definition: structs.h:269
int AL_Set_offset(int sz_ptr, long long offset)
Definition: al_io.c:342
char dir[256]
output directory name
Definition: structs.h:244
static int nlines
The total number of lines (including empty ones) contained in the file.
Definition: parse_file.c:47
int prank
Processor rank.
Definition: globals.h:33
int SZ_stagx
Definition: globals.h:24
int SZ_stagy
Definition: globals.h:25
char ext[8]
output extension
Definition: structs.h:243
void ReadBinaryArray(void *V, size_t dsize, int sz, FILE *fl, int istag, int swap_endian)
Definition: bin_io.c:143
struct szz SZ
#define DBL_H5_OUTPUT
Definition: pluto.h:82
int IsLittleEndian(void)
Definition: tools.c:40
int SZ_stagz
Definition: globals.h:26
int nvar
tot.
Definition: structs.h:234
#define MAX_OUTPUT_TYPES
Definition: pluto.h:95
int * dump_var
select vars being written - one per output
Definition: structs.h:240
#define DBL_OUTPUT
Definition: pluto.h:79
char mode[32]
single or multiple files - one per output
Definition: structs.h:241
int type
output format (DBL, FLT, ...) - one per output
Definition: structs.h:233
int * stag_var
centered or staggered variable - same for all
Definition: structs.h:239
int CloseBinaryFile(FILE *fbin, int sz)
Definition: bin_io.c:78
#define QUIT_PLUTO(e_code)
Definition: macros.h:125
char ** var_name
variable names - same for all
Definition: structs.h:242

Here is the call graph for this function:

Here is the caller graph for this function:

void RestartGet ( Runtime ini,
int  nrestart,
int  out_type,
int  swap_endian 
)

Collect restart information needed for (potential) later restarts.

Definition at line 177 of file restart.c.

184 {
185  int origin, n, k;
186  char fout[512];
187  Restart restart;
188  FILE *fr;
189 
190  if (nrestart < 0){
191  printf ("! negative restart file temporarily disabled\n");
192  QUIT_PLUTO(1);
193  }
194 
195 /* -------------------------------------------------
196  Open "restart.out" and scan line by line until
197  the output type specified by "out_type" has
198  nfile = nrestart.
199  counter will contain the line number where this
200  occurs. Processor 0 does the actual reading.
201  ------------------------------------------------- */
202 
203  if (prank == 0) {
204  sprintf (fout,"%s/restart.out",ini->output_dir);
205  fr = fopen (fout, "rb");
206  if (fr == NULL){
207  print1 ("! RestartGet: cannot find restart.out\n");
208  QUIT_PLUTO(1);
209  }
210 
211  origin = (nrestart < 0 ? SEEK_END:SEEK_SET);
212  k = 0;
213  while (counter == -1){
214  if (feof(fr)){
215  print("! RestartGet: end of file encountered.\n");
216  QUIT_PLUTO(1);
217  }
218  fseek (fr, k*sizeof(Restart), origin);
219  fread (&restart, sizeof (Restart), 1, fr);
220  for (n = 0; n < MAX_OUTPUT_TYPES; n++){
221  if (swap_endian) SWAP_VAR(restart.nfile[n]);
222  if (ini->output[n].type == out_type && restart.nfile[n] == nrestart){
223  counter = k;
224  }
225  }
226  k++;
227  }
228  fclose(fr);
229  if (swap_endian){
230  SWAP_VAR(restart.t);
231  SWAP_VAR(restart.dt);
232  SWAP_VAR(restart.nstep);
233  }
234  }
235 
236 /* printf ("counter = %d\n",counter); */
237 
238  #ifdef PARALLEL
239  MPI_Bcast (&restart, sizeof (Restart), MPI_BYTE, 0, MPI_COMM_WORLD);
240  #endif
241 
242  g_time = restart.t;
243  g_dt = restart.dt;
244  g_stepNumber = restart.nstep;
245 
246  for (n = 0; n < MAX_OUTPUT_TYPES; n++){
247  ini->output[n].nfile = restart.nfile[n];
248  }
249 }
double dt
Definition: structs.h:293
static int n
Definition: analysis.c:3
double t
Definition: structs.h:292
Output output[MAX_OUTPUT_TYPES]
Definition: structs.h:272
void print1(const char *fmt,...)
Definition: amrPluto.cpp:511
int nfile
current number being saved - one per output
Definition: structs.h:237
char output_dir[256]
The name of the output directory (output_dir for static PLUTO, Output_dir for PLUTO-Chombo) ...
Definition: structs.h:269
double g_dt
The current integration time step.
Definition: globals.h:118
int prank
Processor rank.
Definition: globals.h:33
#define SWAP_VAR(x)
Definition: macros.h:115
long int g_stepNumber
Gives the current integration step number.
Definition: globals.h:97
int nstep
Definition: structs.h:290
static int counter
Definition: restart.c:174
int k
Definition: analysis.c:2
void print(const char *fmt,...)
Definition: amrPluto.cpp:497
#define MAX_OUTPUT_TYPES
Definition: pluto.h:95
double g_time
The current integration time.
Definition: globals.h:117
int type
output format (DBL, FLT, ...) - one per output
Definition: structs.h:233
int nfile[MAX_OUTPUT_TYPES]
Definition: structs.h:291
#define QUIT_PLUTO(e_code)
Definition: macros.h:125

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

int counter = -1
static

Definition at line 174 of file restart.c.