aboutsummaryrefslogtreecommitdiff
path: root/src/Utils.c
blob: 7abec4d3aba48f67367f1f0045005dabd775a0cd (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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*@@
   @file      Utils.c
   @date      Tue 4th July 2000
   @author    Gabrielle Allen
   @desc
              Utility routines which may be called by other IO thorns
   @enddesc 
   @version   $Id$
 @@*/


#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "cctk.h"
#include "cctk_Parameters.h"
#include "ioutil_Utils.h"
#include "ioutil_CheckpointRecovery.h"

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


/* uncomment this if you want some debugging output */
/* #define DEBUG_IOUTIL 1 */


/*@@
   @routine    IOUtil_1DLines
   @date       July 4 2000
   @author     Gabrielle Allen, Gerd Lanfermann, Thomas Radke
   @desc
               Fills out an array determining where to position 1D lines
               for output on a multidimensional regular Cartesian unigrid.
               The first slot of the array specifies the 1D line direction,
               the second slot fixes the indices of the starting point
               of that line on the grid.
   @enddesc

   @calls      CCTK_CoordSystemHandle
               CCTK_CoordRange

   @var        GH
   @vdesc      Pointer to CCTK GH
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        num_dims
   @vdesc      number of dimensions of underlying grid
   @vtype      int
   @vio        in
   @endvar
   @var        origin_index
   @vdesc      origin for 1D lines given by grid point indices
   @vtype      const int [num_dims][num_dims]
   @vio        in
   @endvar
   @var        origin_phys
   @vdesc      origin for 1D lines given by physical coordinates
   @vtype      const CCTK_REAL [num_dims][num_dims]
   @vio        in
   @endvar
   @var        slice_center
   @vdesc      resulting 1D line slice center
   @vtype      int [num_dims][num_dims]
   @vio        out
   @endvar

   @returntype int
   @returndesc
               0  for success
               -1 no coordinate system of given dimensions found
   @endreturndesc
 @@*/
int IOUtil_1DLines (const cGH *GH,
                    int num_dims,
                    int *const *const origin_index,
                    CCTK_REAL *const *const origin_phys,
                    int *const *slice_center)
{
  int dim, dir;
  char coord_system_name[20];
  CCTK_REAL *lower_range, *upper_range;
  int retval;

  retval = 0;

  /* allocate arrays for ranges */
  lower_range = (CCTK_REAL *) calloc (2 * num_dims, sizeof (CCTK_REAL));
  upper_range = lower_range + num_dims;

  /* get the appropriate coordinate system name */
  sprintf (coord_system_name, "cart%dd", num_dims);
  if (CCTK_CoordSystemHandle (coord_system_name) >= 0)
  {
    /* get the ranges in every direction */
    for (dir = 0; dir < num_dims; dir++)
    {
      if (CCTK_CoordRange (GH, &lower_range[dir], &upper_range[dir],
                           dir + 1, NULL, coord_system_name) < 0)
      {
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "IOUtil_1DLines: Could not get ranges for %c-direction "
                    "of coordinate system '%s'",
                    'x' + dir, coord_system_name);
      }
    }
  }
  else
  {
    CCTK_VWarn (4, __LINE__, __FILE__, CCTK_THORNSTRING,
                "IOUtil_1DLines: Cartesian coordinate system '%s' not found"
                " - no slice center set up for output of 1D lines from %dD "
                "variables", coord_system_name, num_dims);
    for (dir = 0; dir < num_dims; dir++)
    {
      for (dim = 0; dim < num_dims; dim++)
      {
        slice_center[dir][dim] = 0;
      }
    }

    retval = -1;
  }

  /* now set the slice center for each line
     according to origin_index[] or origin_phys[] */

  if (! retval)
  {
    for (dir = 0; dir < num_dims; dir++)
    {
      for (dim = 0; dim < num_dims; dim++)
      {
        if (dim == dir)
        {
          /* line always starts at the first point */
          slice_center[dir][dim] = 0;
        }
        else if (origin_index && origin_index[dir][dim] >= 0)
        {
          /* FIXME: check upper index bounds also ?? */
          slice_center[dir][dim] = origin_index[dir][dim];
        }
        else if (lower_range[dim] > origin_phys[dir][dim] || 
                 upper_range[dim] < origin_phys[dir][dim])
        {
          CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                      "IOUtil_1DLines: %c-coordinate for slice center of 1D "
                      "lines in %c-direction (%f) is out of grid coordinates "
                      "range (%f, %f)",
                      'x' + dim, 'x' + dir, (double) origin_phys[dir][dim],
                      (double) lower_range[dim], (double) upper_range[dim]);
          CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                      "IOUtil_1DLines: no 1D %c-line output will be written for "
                      "%dD variables with this slice center default",
                      'x' + dir, num_dims);
          
          slice_center[dir][dim] = -1;
        }
        else 
        {
          /* Find index for first point above the chosen coordinate */
          slice_center[dir][dim] =
            ceil ((origin_phys[dir][dim] - lower_range[dim]) /
                  GH->cctk_delta_space[dim] - 1e-6);

#ifdef DEBUG_IOUTIL
          printf("spxyz for %c-coord of lines in %c-direction is %d\n",
                 'x' + dim,'x' + dir, slice_center[dir][dim]);
#endif
        }
      }
    }
  }
  
  /* free allocated resources */
  free (lower_range);

  return (retval);
}


/*@@
   @routine    IOUtil_2DPlanes
   @date       July 4 2000
   @author     Gabrielle Allen, Gerd Lanfermann, Thomas Radke
   @desc
               Fills out an array determining where to position 2D planes
               for output on a multidimensional regular Cartesian unigrid.
   @enddesc

   @calls      CCTK_CoordRange

   @var        GH
   @vdesc      Pointer to CCTK GH
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        num_dims
   @vdesc      number of dimensions of underlying grid
   @vtype      int
   @vio        in
   @endvar
   @var        origin_index
   @vdesc      origin for 2D planes given by grid point indices
   @vtype      const int [num_dims]
   @vio        in
   @endvar
   @var        origin_phys
   @vdesc      origin for 2D planes given by physical coordinates
   @vtype      const CCTK_REAL [num_dims]
   @vio        in
   @endvar
   @var        slice_center
   @vdesc      resulting 2D plane slice center
   @vtype      int [num_dims]
   @vio        out
   @endvar

   @returntype int
   @returndesc
               0  for success
               -1 no coordinate system of given dimensions found
   @endreturndesc
 @@*/
int IOUtil_2DPlanes (const cGH *GH,
                     int num_dims,
                     const int *origin_index,
                     const CCTK_REAL *origin_phys,
                     int *slice_center)
{
  int dir;
  char coord_system_name[20];
  CCTK_REAL *lower_range, *upper_range;


  /* get the appropriate coordinate system name */
  sprintf (coord_system_name, "cart%dd", num_dims);

  if (CCTK_CoordSystemHandle (coord_system_name) < 0)
  {
    CCTK_VWarn (4, __LINE__, __FILE__, CCTK_THORNSTRING,
                "IOUtil_2DPlanes: Cartesian coordinate system '%s' not found"
                " - no slice center set up for output of 2D planes from %dD "
                "variables", coord_system_name, num_dims);
    return (-1);
  }

  /* allocate arrays for ranges */
  lower_range = (CCTK_REAL *) calloc (2 * num_dims, sizeof (CCTK_REAL));
  upper_range = lower_range + num_dims;

  /* get the ranges in every direction */
  for (dir = 0; dir < num_dims; dir++)
  {
    if (CCTK_CoordRange (GH, &lower_range[dir], &upper_range[dir],
                         num_dims - dir, NULL, coord_system_name) < 0)
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "IOUtil_2DPlanes: Could not get ranges for %c-direction "
                  "of coordinate system '%s'",
                  'x' + (num_dims - dir - 1), coord_system_name);
    }
  }

  /* now set the slice center for each line
     according to origin_index[] or origin_phys[] */
  for (dir = 0; dir < num_dims; dir++)
  {
    if (origin_index && origin_index[dir] >= 0)
    {
      slice_center[dir] = origin_index[dir];
    }
    else if (lower_range[dir] > origin_phys[dir] || 
             upper_range[dir] < origin_phys[dir])
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "IOUtil_2DPlanes: %c-coordinate for slice center of 2D "
                  "planes (%f) is out of grid coordinates range (%f, %f)",
                  'x' + (num_dims - dir - 1), (double) origin_phys[dir],
                  (double) lower_range[dir], (double) upper_range[dir]);
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "IOUtil_2DPlanes: no 2D planes in %c-direction will be "
                  "written for %dD variables with this slice center default",
                  'x' + (num_dims - dir - 1), num_dims);
      slice_center[dir] = 0;
    }
    else
    {
      /* Find index for first point above the chosen coordinate */
      slice_center[dir] = ceil ((origin_phys[dir] - lower_range[dir]) /
                                GH->cctk_delta_space[(num_dims - dir - 1)] -
                                1e-6);

#ifdef DEBUG_IOUTIL
      printf("sp2xyz for planes perpendicular to %d-direction is %d\n",
             (num_dims - dir - 1), (CCTK_INT) slice_center[dir]);
#endif
    }
  }

  /* free allocated resources */
  free (lower_range);

  return (0);
}


 /*@@
   @routine    IOUtil_PrintTimings
   @date       Wed Jun 28 2000
   @author     Thomas Radke
   @desc
               Gets the timing information for the given timers and prints it
               as INFO messages to screen.
   @enddesc
   @history
   @endhistory
   @var        description
   @vdesc      description of the timers
   @vtype      const char *
   @vio        in
   @endvar
   @var        ntimers
   @vdesc      number of timers passed in
   @vtype      int
   @vio        in
   @endvar
   @var        timers
   @vdesc      array of timers
   @vtype      const int [ntimers]
   @vio        in
   @endvar
   @var        timer_descriptions
   @vdesc      array of timer descriptions
   @vtype      const char *const [ntimers]
   @vio        in
   @endvar
@@*/

void IOUtil_PrintTimings (const char *description,
                          int ntimers,
                          const int *timers,
                          const char *const *const timer_descriptions)
{
  int i, j;
  cTimerData *info;


  info = CCTK_TimerCreateData ();
  if (info)
  {
    CCTK_INFO (description);

    for (i = 0; i < info->n_vals; i++)
    {
      for (j = 0; j < ntimers; j++)
      {
        CCTK_TimerI (timers[j], info);
        if (j == 0)
        {
          CCTK_VInfo (CCTK_THORNSTRING, "  %s:", info->vals[i].heading);
        }
        switch (info->vals[i].type)
        {
          case val_int:
            CCTK_VInfo (CCTK_THORNSTRING, "    %s %5d %s",
                        timer_descriptions [j],
                        info->vals[i].val.i, info->vals[i].units);
            break;

          case val_long:
            CCTK_VInfo (CCTK_THORNSTRING, "    %s %5d %s",
                        timer_descriptions [j],
                        (int) info->vals[i].val.l, info->vals[i].units);
            break;

          case val_double:
            CCTK_VInfo (CCTK_THORNSTRING, "    %s %5.1f %s",
                        timer_descriptions [j],
                        info->vals[i].val.d, info->vals[i].units);
            break;

          default:
            CCTK_WARN(1, "Unknown data type for timer info");
            break;
        }
      }
    }
    CCTK_INFO ("-----------------------------------------\n");
    CCTK_TimerDestroyData (info);
  }
  else
  {
    CCTK_WARN (1, "Couldn't create timer info structure ! "
                  "No timing output available.");
  }
}


 /*@@
   @routine    IOUtil_CreateDirectory
   @date       Fri 10 Aug 2001
   @author     Thomas Radke
   @desc
               Creates an output directory path and makes sure it is visible
               on all I/O processors.
               It is assumed that processor 0 is always an I/O processor.
               If there are other I/O processors, they will also try and
               create the directory themselfs. This guarantees the directory
               be created on all nodes in case the I/O processors don't share
               a common filesystem.
   @enddesc
   @calls      CCTK_MyProc
               CCTK_CreateDirectory
               CCTK_Barrier

   @var        GH
   @vdesc      pointer to the GH extensions
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        dirname
   @vdesc      the directory to create
   @vtype      const char *
   @vio        in
   @endvar
   @var        multiple_io_procs
   @vdesc      flag indicating that there will be I/O from multiple I/O procs
   @vtype      int
   @vio        in
   @endvar
   @var        ioproc
   @vdesc      I/O processor associated with this processor
   @vtype      int
   @vio        in
   @endvar

   @returntype int
   @returndesc
               0 for non-I/O processors, or
               return code of @seeroutine CCTK_CreateDirectory
   @endreturndesc
@@*/
int IOUtil_CreateDirectory (const cGH *GH, const char *dirname,
                            int multiple_io_procs, int ioproc)
{
  int myproc, retval;


  /* default return value for non-I/O processors */
  retval = 0;

  /* first, processor 0 creates the directory */
  myproc = CCTK_MyProc (GH);
  if (myproc == 0)
  {
    retval = CCTK_CreateDirectory (0755, dirname);
  }

  if (multiple_io_procs)
  {
    /* now the other I/O processors create the directory
       after syncing with processor 0 */
    CCTK_Barrier (GH);
    if (myproc == ioproc || ioproc != 0)
    {
      retval = CCTK_CreateDirectory (0755, dirname);
    }
  }

  return (retval);
}