aboutsummaryrefslogtreecommitdiff
path: root/src/DumpUtils.c
blob: 1a97f001a86d47064a83f0650760f19201844914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
 /*@@
   @file      DumpUtils.c
   @date      Fri Oct 6 2000
   @author    Thomas Radke
   @desc 
              Utility routines for dumping data into HDF5 files.
   @enddesc 
   @version   $Id$
 @@*/


#include <stdlib.h>

#include "cctk.h"
#include "cctk_Parameters.h"
#include "CactusBase/IOUtil/src/ioGH.h"
#include "CactusBase/IOUtil/src/ioutil_CheckpointRecovery.h"
#include "ioHDF5UtilGH.h"

/* the rcs ID and its dummy function to use it */
static char *rcsid = "$Id$";
CCTK_FILEVERSION(BetaThorns_IOHDF5Util_DumpUtils_c)


/*@@
   @routine    IOHDF5Util_DumpGH
   @date       Tue Oct 10 2000
   @author     Thomas Radke
   @desc
               Dump all variables of the given GH along with
               the global attributes and parameters
               to the (already opened) HDF5 checkpoint file.
   @enddesc
   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      cGH *
   @vio        in
   @endvar
   @var        timers
   @vdesc      array of timers for timing the checkpointing
   @vtype      int []
   @vio        in
   @endvar
   @var        file
   @vdesc      HDF5 file handle
   @vtype      hid_t
   @vio        in
   @endvar

   @returntype int
   @returndesc
               always 0 (for success)
   @endreturndesc
@@*/
int IOHDF5Util_DumpGH (cGH *GH,
                       int timers[],
                       hid_t file)
{
  DECLARE_CCTK_PARAMETERS
  int vindex;
  int maxdim;
  int timelevel, current_timelevel;
  int old_out_single;
  ioGH *ioUtilGH;
  const char *thorn;
  const char *impl;
  ioHDF5Geo_t request;


  /* Get the GH extension for IOUtil */
  ioUtilGH = (ioGH *) CCTK_GHExtension (GH, "IO");

  maxdim = CCTK_MaxDim ();
  request.origin     = (int *) calloc (4 + maxdim, maxdim * sizeof (int));
  request.length     = request.origin + 1 * maxdim;
  request.downsample = request.origin + 2 * maxdim;
  request.actlen     = request.origin + 3 * maxdim;
  request.direction  = request.origin + 4 * maxdim;

  for (vindex = 0; vindex < maxdim; vindex++)
  {
    request.length[vindex]                      = -1;
    request.downsample[vindex]                  = 1;
    request.direction[vindex * maxdim + vindex] = 1;
  }

  /* disable output in single precision */
  old_out_single = ioUtilGH->out_single;
  ioUtilGH->out_single = 0;

  /* sync all active groups */
  for (vindex = CCTK_NumGroups () - 1; vindex >= 0; vindex--)
  {
    if (CCTK_IsImplementationActive (
           CCTK_ImpFromVarI (CCTK_FirstVarIndexI (vindex))))
    {
      CCTK_SyncGroupI (GH, vindex);
    }
  }

  /* start CP_PARAMETERS_TIMER timer */
  if (timers)
  {
    CCTK_TimerResetI (timers[CP_PARAMETERS_TIMER]);
    CCTK_TimerStartI (timers[CP_PARAMETERS_TIMER]);
  }

  /* Great; Now start dumping away! */
  if (file >= 0)
  {
    /* all GH extension variables and parameter stuff which is not
       specific to any dataset goes into dedicated groups */
    if (out3D_parameters)
    {
      IOHDF5Util_DumpParameters (GH, file);
    }

    IOHDF5Util_DumpGHExtensions (GH, file);
  }

  if (verbose)
  {
    CCTK_INFO ("Dumping variables ...");
  }

  /* stop CP_PARAMETERS_TIMER timer and start CP_VARIABLES_TIMER */
  if (timers)
  {
    CCTK_TimerStopI  (timers[CP_PARAMETERS_TIMER]);
    CCTK_TimerResetI (timers[CP_VARIABLES_TIMER]);
    CCTK_TimerStartI (timers[CP_VARIABLES_TIMER]);
  }

  /* ... now the variables */
  for (vindex = CCTK_NumVars () - 1; vindex >= 0; vindex--)
  {
    /* find out the thorn implementing variable with index */
    impl = CCTK_ImpFromVarI (vindex);
    thorn = CCTK_ImplementationThorn (impl);
    if (! thorn)
    {
      thorn = impl;
    }

    /* let only variables pass which belong to an active thorn and
       have storage assigned */
    if (! CCTK_IsThornActive (thorn) ||
        ! CCTK_QueryGroupStorageI (GH, CCTK_GroupIndexFromVarI (vindex)))
    {
      continue;
    }

    if (verbose && file >= 0)
    {
      CCTK_VInfo (CCTK_THORNSTRING, "  %s", CCTK_VarName (vindex));
    }

    /* get the current timelevel */
    current_timelevel = CCTK_NumTimeLevelsFromVarI (vindex) - 1;
    if (current_timelevel > 0)
    {
      current_timelevel--;
    }

    request.sdim = request.vdim = CCTK_GroupDimFromVarI (vindex);

    /* now dump all timelevels up to the current */
    for (timelevel = 0; timelevel <= current_timelevel; timelevel++)
    {
      IOHDF5Util_DumpVar (GH, vindex, timelevel, &request, file, 0);
    }

  } /* end of loop over all variables */

  /* stop CP_VARIABLES_TIMER timer */
  if (timers)
  {
    CCTK_TimerStopI (timers[CP_VARIABLES_TIMER]);
  }

  /* restore output precision flag */
  ioUtilGH->out_single = old_out_single;

  /* free temporary resources */
  free (request.origin);

  return (0);
}


/*@@
   @routine    IOHDF5Util_DumpCommonAttributes
   @date       May 21 1999
   @author     Thomas Radke
   @desc
               Add "Common" attributes to the given dataset, these are:
               <ul>
                 <li> the variable's groupname
                 <li> the grouptype
                 <li> number of timelevels
                 <li> simulation time
                 <li> origin
                 <li> bounding box
                 <li> gridspacings
                 <li> global grid size
               </ul>
   @enddesc
   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      cGH *
   @vio        in
   @endvar
   @var        vindex
   @vdesc      CCTK index of the variable to dump
   @vtype      int
   @vio        in
   @endvar
   @var        timelevel
   @vdesc      timelevel of the variable to dump
   @vtype      int
   @vio        in
   @endvar
   @var        global_shape
   @vdesc      global shape of the grid
   @vtype      CCTK_INT []
   @vio        in
   @endvar
   @var        request
   @vdesc      pointer to IO request structure describing the hyperslab
   @vtype      ioHDF5Geo_t *
   @vio        in
   @endvar
   @var        dataset
   @vdesc      the dataset handle where the attributes should be attached to
   @vtype      hid_t
   @vio        in
   @endvar
@@*/
void IOHDF5Util_DumpCommonAttributes (cGH *GH,
                                      int vindex,
                                      int timelevel,
                                      CCTK_INT global_shape[],
                                      ioHDF5Geo_t *request,
                                      hid_t dataset)
{
  DECLARE_CCTK_PARAMETERS
  char *groupname;
  int maxdim;
  CCTK_INT attr_int;
  CCTK_REAL *attr_real;
  ioHDF5UtilGH *myGH;


  /* prevent compiler warning about unused parameter */
  timelevel = timelevel;

  /* Get the handle for IOHDF5Util extensions */
  myGH = (ioHDF5UtilGH *) CCTK_GHExtension (GH, "IOHDF5Util");

  /* attributes describing the variable */
  groupname = CCTK_GroupNameFromVarI (vindex);
  WRITE_ATTRIBUTE ("groupname", groupname, dataset,
                   myGH->scalar_dataspace, 0, myGH->IOHDF5_STRING);
  free (groupname);
  attr_int = CCTK_GroupTypeFromVarI (vindex);
  WRITE_ATTRIBUTE ("grouptype", &attr_int, dataset,
                   myGH->scalar_dataspace, 0, IOHDF5_INT);
  attr_int = CCTK_NumTimeLevelsFromVarI (vindex);
  WRITE_ATTRIBUTE ("ntimelevels", &attr_int, dataset,
                   myGH->scalar_dataspace, 0, IOHDF5_INT);
  WRITE_ATTRIBUTE ("global_size", global_shape, dataset,
                   myGH->array_dataspace, request->sdim, IOHDF5_INT);
  WRITE_ATTRIBUTE ("time", &GH->cctk_time, dataset,
                   myGH->scalar_dataspace, 0, IOHDF5_REAL);

  /* attributes describing the underlying grid */
  maxdim = CCTK_MaxDim ();
  attr_real = (CCTK_REAL *) malloc (2 * maxdim * sizeof (CCTK_REAL));
  CCTK_CoordRange (GH, &attr_real[0], &attr_real[maxdim+0], -1, "x", "cart3d");
  CCTK_CoordRange (GH, &attr_real[1], &attr_real[maxdim+1], -1, "y", "cart3d");
  CCTK_CoordRange (GH, &attr_real[2], &attr_real[maxdim+2], -1, "z", "cart3d");

  WRITE_ATTRIBUTE ("origin", attr_real, dataset,
                   myGH->array_dataspace, maxdim, IOHDF5_REAL);
  WRITE_ATTRIBUTE ("min_ext", attr_real, dataset,
                   myGH->array_dataspace, maxdim, IOHDF5_REAL);
  WRITE_ATTRIBUTE ("max_ext", attr_real + maxdim, dataset,
                   myGH->array_dataspace, maxdim, IOHDF5_REAL);
  WRITE_ATTRIBUTE ("delta", GH->cctk_delta_space, dataset,
                   myGH->array_dataspace, maxdim, IOHDF5_REAL);
  free (attr_real);

#if 0
  /* attributes describing the hyperslab */
  /* FIXME: what attributes are really needed here ?? ***/
  if (request)
  {
    attr_real = (CCTK_REAL *) malloc (4 * request->sdim * sizeof (CCTK_REAL));
    for (dim = 0; dim < request->sdim; dim++)
    {
      attr_real[dim + 0*request->sdim] =
        request->origin[request->direction[dim]] *
        GH->cctk_delta_space[request->direction[dim]];
      attr_real[dim + 1*request->sdim] =
        request->origin[dim] * GH->cctk_delta_space[dim];
      attr_real[dim + 2*request->sdim] =
        (request->origin[dim] + request->actlen[dim]-1) *
        GH->cctk_delta_space[dim] * request->downsample[dim];
      attr_real[dim + 3*request->sdim] =
        GH->cctk_delta_space[request->direction[dim]] *
        request->downsample[request->direction[dim]];
    }
    WRITE_ATTRIBUTE ("origin_slab",  attr_real + 0*dim, dataset,
                     myGH->array_dataspace, dim, IOHDF5_REAL);
    WRITE_ATTRIBUTE ("min_ext_slab", attr_real + 1*dim, dataset,
                     myGH->array_dataspace, dim, IOHDF5_REAL);
    WRITE_ATTRIBUTE ("max_ext_slab", attr_real + 2*dim, dataset,
                     myGH->array_dataspace, dim, IOHDF5_REAL);
    WRITE_ATTRIBUTE ("delta_slab",   attr_real + 3*dim, dataset,
                     myGH->array_dataspace, dim, IOHDF5_REAL);
    free (attr_real);
  }
#endif
}


 /*@@
   @routine    IOHDF5Util_DumpParameters
   @date       Thu Jan 20 2000
   @author     Thomas Radke
   @desc
               Collects the parameters of all active implementations
               into a single string and writes it as an attribute
               attached to the CACTUS_PARAMETERS_GROUP group in the HDF5 file.
   @enddesc
   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      cGH *
   @vio        in
   @endvar
   @var        file
   @vdesc      the HDF5 file handle
   @vtype      hid_t
   @vio        in
   @endvar
@@*/
void IOHDF5Util_DumpParameters (cGH *GH,
                                hid_t file)
{
  DECLARE_CCTK_PARAMETERS
  ioHDF5UtilGH *myGH;
  char *parameters;
  hid_t group;


  if (verbose)
  {
    CCTK_INFO ("Dumping GH extensions ...");
  }

  /* Get the parameter string buffer */
  parameters = IOUtil_GetAllParameters (GH);

  if (parameters)
  {
    /* Get the handle for IOHDF5Util extensions */
    myGH = (ioHDF5UtilGH *) CCTK_GHExtension (GH, "IOHDF5Util");

    IOHDF5_ERROR (group = H5Gcreate (file, CACTUS_PARAMETERS_GROUP, 0));
    WRITE_ATTRIBUTE (ALL_PARAMETERS, parameters, group,
                     myGH->scalar_dataspace, 0, myGH->IOHDF5_STRING);
    IOHDF5_ERROR (H5Gclose (group));

    free (parameters);
  }
}


 /*@@
   @routine    IOHDF5Util_DumpGHExtensions
   @date       Thu Jan 20 2000
   @author     Thomas Radke
   @desc
               Attaches the current values of important flesh and IO variables
               as attributes to the GLOBAL_ATTRIBUTES_GROUP group
               in the HDF5 file.
   @enddesc
   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      cGH *
   @vio        in
   @endvar
   @var        file
   @vdesc      the HDF5 file handle
   @vtype      hid_t
   @vio        in
   @endvar
@@*/
void IOHDF5Util_DumpGHExtensions (cGH *GH,
                                  hid_t file)
{
  DECLARE_CCTK_PARAMETERS
  int value;
  hid_t group;
  char buffer[128];
  ioGH *ioUtilGH;
  ioHDF5UtilGH *myGH;


  if (verbose)
  {
    CCTK_INFO ("Dumping GH extensions ...");
  }

  /* Get the handles for IOUtil and IOHDF5 extensions */
  ioUtilGH = (ioGH *) CCTK_GHExtension (GH, "IO");
  myGH = (ioHDF5UtilGH *) CCTK_GHExtension (GH, "IOHDF5Util");

  IOHDF5_ERROR (group = H5Gcreate (file, GLOBAL_ATTRIBUTES_GROUP, 0));

  value = CCTK_MainLoopIndex ();
  WRITE_ATTRIBUTE ("main_loop_index", &value, group,
                   myGH->scalar_dataspace, 0, H5T_NATIVE_INT);
  value = CCTK_nProcs (GH);
  WRITE_ATTRIBUTE ("nprocs", &value, group,
                   myGH->scalar_dataspace, 0, H5T_NATIVE_INT);
  WRITE_ATTRIBUTE ("ioproc_every", &ioUtilGH->ioproc_every, group,
                   myGH->scalar_dataspace, 0, H5T_NATIVE_INT);
  WRITE_ATTRIBUTE ("unchunked", &ioUtilGH->unchunked, group,
                   myGH->scalar_dataspace, 0, H5T_NATIVE_INT);
  WRITE_ATTRIBUTE ("cctk_time", &GH->cctk_time, group,
                   myGH->scalar_dataspace, 0, IOHDF5_REAL);
  WRITE_ATTRIBUTE ("cctk_iteration", &GH->cctk_iteration, group,
                   myGH->scalar_dataspace, 0, H5T_NATIVE_INT);

  /* add the parameter filename and the creation date
     as file identification attributes */
  if (CCTK_Equals (out_fileinfo, "parameter filename") ||
      CCTK_Equals (out_fileinfo, "all"))
  {
    buffer[0] = 0;
    CCTK_ParameterFilename (sizeof (buffer), buffer);
    WRITE_ATTRIBUTE ("parameter file", buffer, group,
                     myGH->scalar_dataspace, 0, myGH->IOHDF5_STRING);
  }
  if (CCTK_Equals (out_fileinfo, "creation date") ||
      CCTK_Equals (out_fileinfo, "all"))
  {
    buffer[0] = 0;
    Util_CurrentDate (sizeof (buffer), buffer);
    value = strlen (buffer) + 1;
    buffer[value-1] = ' ';
    Util_CurrentTime (sizeof (buffer) - value, buffer + value);
    WRITE_ATTRIBUTE ("creation date", buffer, group,
                     myGH->scalar_dataspace, 0, myGH->IOHDF5_STRING);
  }

  IOHDF5_ERROR (H5Gclose (group));
}