aboutsummaryrefslogtreecommitdiff
path: root/src/Utils.c
blob: 5b25bc8658f51c19ade75fb665b45899cb855149 (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
/*@@
   @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 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_CoordRange

   @var        GH
   @vdesc      Pointer to CCTK GH
   @vtype      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 (cGH *GH,
                    int num_dims,
                    int *const origin_index[],
                    CCTK_REAL *const origin_phys[],
                    int *slice_center[])
{
  DECLARE_CCTK_PARAMETERS
  int dim, dir;
  char system_name[20];
  CCTK_REAL *lower_range, *upper_range;


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

  if (CCTK_CoordSystemHandle (system_name) < 0)
  {
    CCTK_VWarn (4, __LINE__, __FILE__, CCTK_THORNSTRING,
                "IOUtil_1DLines: Cartesian coordinate system '%s' not found",
                system_name);
    memset (slice_center, 0, num_dims * num_dims * sizeof (int));
    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],
                         dir + 1, NULL, 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, system_name);
    }
  }

  /* now set the slice center for each line
     according to origin_index[] or origin_phys[] */
  for (dir = 0; dir < num_dims; dir++)
  {
    for (dim = 0; dim < num_dims; dim++)
    {
      if (dim == dir)
      {
        slice_center[dir][dim] = 0;
      }
      else if (origin_index && origin_index[dir][dim] >= 0)
      {
        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 (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "IOUtil_1DLines: Coordinate in %c-direction (%f, %f) "
                    "doesn't contain %f",
                    'x' + dim, (double) lower_range[dim],
                    (double) upper_range[dim], (double) origin_phys[dir][dim]);
        slice_center[dir][dim] = 0;
      }
      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 %d-coord of lines in %d-direction is %d\n",
               dim,dir,slice_center[dir][dim]);
#endif
      }
    }
  }

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

  return (0);
}


/*@@
   @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      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 (cGH *GH,
                     int num_dims,
                     const int origin_index[],
                     const CCTK_REAL origin_phys[],
                     int slice_center[])
{
  DECLARE_CCTK_PARAMETERS
  int dir;
  char system_name[20];
  CCTK_REAL *lower_range, *upper_range;


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

  if (CCTK_CoordSystemHandle (system_name) < 0)
  {
    CCTK_VWarn (4, __LINE__, __FILE__, CCTK_THORNSTRING,
                "IOUtil_1DLines: Cartesian coordinate system '%s' not found",
                system_name);
    memset (slice_center, 0, num_dims * sizeof (int));
    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],
                         dir + 1, NULL, system_name) < 0)
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "IOUtil_2DPlanes: Could not get ranges for %c-direction "
                  "of coordinate system '%s'",
                  'x' + dir, 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 (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "IOUtil_2DPlanes: Coordinate in %c-direction (%f, %f) "
                  "doesn't contain %f",
                  'x' + dir, (double) lower_range[dir],
                  (double) upper_range[dir], (double) origin_phys[dir]);
      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[dir] - 1e-6);

#ifdef DEBUG_IOUTIL
      printf("sp2xyz for planes perpendicular to"
             " %d-direction is %d\n",
             dir,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      int [ntimers]
   @vio        in
   @endvar
   @var        timer_descriptions
   @vdesc      array of timer descriptions
   @vtype      const char * [ntimers]
   @vio        in
   @endvar
@@*/

void IOUtil_PrintTimings (const char *description,
                          int ntimers,
                          int timers[/* ntimers */],
                          const char *timer_descriptions[/* ntimers */])
{
  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.");
  }
}