aboutsummaryrefslogtreecommitdiff
path: root/src/Write2D.c
blob: fe2e304e9454caf7fbb4bf83b2aff434173072e4 (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
/*@@
   @file      Write2D.c
   @date      Thu May 11 2000
   @author    Thomas Radke
   @desc 
              Output two-dimensional slices in ASCII gnuplot format.
   @enddesc 
   @version   $Id$
 @@*/


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

#include "cctk.h"
#include "cctk_Parameters.h"
#include "CactusPUGH/PUGHSlab/src/PUGHSlab.h"
#include "CactusBase/IOUtil/src/ioutil_AdvertisedFiles.h"
#include "CactusBase/IOUtil/src/ioutil_CheckpointRecovery.h"
#include "ioASCIIGH.h"

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

/* enable debug output */
/*#define IOASCII_DEBUG 1*/


/* macro to output a time slice as typed data */
#define OUTPUT_TYPED_DATA(coord_hsizes, coord_i, coord_j, stagger_offset_i,   \
                          stagger_offset_j, data_hsizes, cctk_type, c_type,   \
                          data, grouptype, fmt_string, file)                  \
          {                                                                   \
            int i, j;                                                         \
            cctk_type *typed_data = (cctk_type *) (data);                     \
                                                                              \
                                                                              \
            /* grid functions are output along with their coordinates         \
               grid array points are just indexed */                          \
            if (grouptype == CCTK_GF)                                         \
            {                                                                 \
              for (j = 0; j < data_hsizes[1]; j++)                            \
              {                                                               \
                for (i = 0; i < data_hsizes[0]; i++)                          \
                {                                                             \
                  fprintf (file, fmt_string,                                  \
                           (double) (*coord_i++ + stagger_offset_i),          \
                           (double) (*coord_j++ + stagger_offset_j),          \
                           (c_type) *typed_data++);                           \
                }                                                             \
                fprintf (file, "\n");                                         \
                coord_i += coord_hsizes[0] - data_hsizes[0];                  \
                coord_j += coord_hsizes[0] - data_hsizes[0];                  \
              }                                                               \
              coord_i -= coord_hsizes[0] * data_hsizes[1];                    \
              coord_j -= coord_hsizes[0] * data_hsizes[1];                    \
            }                                                                 \
            else                                                              \
            {                                                                 \
              for (j = 0; j < data_hsizes[1]; j++)                            \
              {                                                               \
                for (i = 0; i < data_hsizes[0]; i++)                          \
                {                                                             \
                  fprintf (file, fmt_string,                                  \
                           (double) i,                                        \
                           (double) j,                                        \
                           (c_type) *typed_data++);                           \
                }                                                             \
                fprintf (file, "\n");                                         \
              }                                                               \
            }                                                                 \
          }


/*@@
   @routine   IOASCII_Write2D
   @date      Thu May 11 2000
   @author    Thomas Radke
   @desc
              Writes the 2D slices of a variable into separate ASCII files.
   @enddesc
   @calls     IOUtil_RestartFromRecovery
              IOUtil_AdvertiseFile
              Hyperslab_GetHyperslab

   @var       GH
   @vdesc     Pointer to CCTK GH
   @vtype     cGH *
   @vio       in
   @endvar
   @var       index
   @vdesc     index of variable to output
   @vtype     int
   @vio       in
   @endvar
   @var       alias
   @vdesc     alias name of variable to output
   @vtype     const char *
   @vio       in
   @endvar
@@*/
void IOASCII_Write2D (cGH *GH, int index, const char *alias)
{
  DECLARE_CCTK_PARAMETERS
  int myproc;
  asciiioGH *myGH;
  const char *header_fmt_string;      /* header format string */
  const char *data_fmt_string_int;    /* data format string for int types */
  const char *data_fmt_string_real;   /* data format string for float types */
  int dir;
  int timelevel;
  int groupindex;
  cGroup groupinfo;
  FILE **fdset_2D;                    /* array of output file pointers */
  int coord_index[3];                 /* variable indices for xyz coordinates */
  int coord_timelevel[3];             /* coordinates' current timelevels */
  int origin[3];                      /* the slice origin */
  char *filename;
  char *fullname;
  char slicename[20];
  ioAdvertisedFileDesc advertised_file; 
  const char *extensions[3] = {"yz", "xz", "xy"};


  /* to make the compiler happy */
  fdset_2D = NULL;

  /* get the variable group indormation */
  groupindex = CCTK_GroupIndexFromVarI (index);
  CCTK_GroupData (groupindex, &groupinfo);

  /* check if variable has storage assigned */
  if (! CCTK_QueryGroupStorageI (GH, groupindex)) 
  {
    fullname = CCTK_FullName (index);
    CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                "No IOASCII 2D output for '%s' (no storage)", fullname);
    free (fullname);
    return;
  }

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

  /* set header and data format strings */
  if (CCTK_Equals (out_format, "f"))
  {
    header_fmt_string    = "\n\n#Time = %f\n";
    data_fmt_string_int  = "%.13f\t\t%.13f\t\t%d\n";
    data_fmt_string_real = "%.13f\t\t%.13f\t\t%.13f\n";
  }
  else
  {
    header_fmt_string    = "\n\n#Time = %e\n";
    data_fmt_string_int  = "%.13e\t\t%.13e\t\t%d\n";
    data_fmt_string_real = "%.13e\t\t%.13e\t\t%.13e\n";
  }

  /* What processor are we on? */
  myproc = CCTK_MyProc (GH);

  /* Open the files on the first trip through if we are proc. 0 */
  if (myproc == 0)
  {

    /* see if output file for this alias name was already created */
    fdset_2D = (FILE **) GetNamedData (myGH->fileList_2D, alias);
    if (fdset_2D == NULL) 
    {
      fdset_2D = (FILE **) malloc (3 * sizeof (FILE *));
      filename = (char *) malloc (strlen (myGH->outdir2D) + strlen (alias) +
                                  sizeof (slicename) + 20 +
#if 0
FIXME: get rid of PUGH here
                               strlen (pughGH->identity_string) + 20);
#else
                               0);
#endif
 
      /* Open/Create files */
      for (dir = 0; dir < 3; dir++)
      {
#if 0
        /* FIXME: move identity_string into cGH ?? */
        sprintf (filename, "%s/%s_2d_%s%s.gnuplot", myGH->outdir2D, alias,
                 extensions[dir], pughGH->identity_string);
#else
        /* FIXME: this can go when we permanently switch the the
                  new filename scheme */
        if (new_filename_scheme)
        {
          /* give the slice origin as range [1 .. n] */
          sprintf (slicename, "%s_[%d]", extensions[dir],
                   myGH->sp2xyz[groupinfo.dim - 1][dir] + 1);

          sprintf (filename, "%s/%s_%s.asc", myGH->outdir2D, alias, slicename);
        }
        else
        {
          sprintf (filename, "%s/%s_2d_%s.gnuplot", myGH->outdir2D, alias,
                   extensions[dir]);
        }
#endif

        /* if restart from recovery, try to open an existing file ... */
        fdset_2D[dir] = NULL;
        if (IOUtil_RestartFromRecovery (GH))
        {
          fdset_2D[dir] = fopen (filename, "a");
        }

        /* otherwise or if that failed, create a new one */
        if (! fdset_2D[dir])
        {
          fdset_2D[dir] = fopen (filename, "w");
        }
        if (! fdset_2D[dir]) 
        {
          CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                      "Cannot open 2D output file '%s'", filename);
          return;
        }

        /* advertise the file for downloading */
        /* FIXME: this can go when we permanently switch the the
                  new filename scheme */
        advertised_file.slice = new_filename_scheme ?
                                slicename : (char *) extensions[dir];
        advertised_file.thorn = CCTK_THORNSTRING;
        advertised_file.varname = CCTK_FullName (index);
        advertised_file.description = "Two-dimensional slice plots";
        advertised_file.mimetype = "application/gnuplot";

        IOUtil_AdvertiseFile (GH, filename, &advertised_file);

        free (advertised_file.varname);
      }

      /* store file desriptors in database */
      StoreNamedData (&myGH->fileList_2D, alias, fdset_2D);

      free (filename);
    }
  }

  /* get the coordinate indices if we output a grid function */
  if (groupinfo.grouptype == CCTK_GF)
  {
    switch (groupinfo.dim)
    {
      case 1:
        coord_index[0] = CCTK_CoordIndex (1, NULL, "cart1d");
        break;
      case 2:
        coord_index[0] = CCTK_CoordIndex (1, NULL, "cart2d");
        coord_index[1] = CCTK_CoordIndex (2, NULL, "cart2d");
      case 3:
        coord_index[0] = CCTK_CoordIndex (1, NULL, "cart3d");
        coord_index[1] = CCTK_CoordIndex (2, NULL, "cart3d");
        coord_index[2] = CCTK_CoordIndex (3, NULL, "cart3d");
        break;
      default:
        CCTK_WARN (4, "Cannot find appropriate coordinate system");
        break;
    }

    /* get the coordinate timelevels */
    for (dir = 0; dir < 3; dir++)
    {
      coord_timelevel[dir] = CCTK_NumTimeLevelsFromVarI (coord_index[dir]) - 1;
      if (coord_timelevel[dir] > 0)
      {
        coord_timelevel[dir]--;
      }
    }
  }

  /* get the timelevel for the variable to output */
  timelevel = CCTK_NumTimeLevelsFromVarI (index) - 1;
  if (timelevel > 0)
  {
    timelevel--;
  }

  for (dir = 0; dir < 3; dir++) 
  {
    int dir_i, dir_j;
    int directions[3];
    const int lengths[2] = {-1, -1};
    const int downsamples[2] = {1, 1};
    int coord_hsizes[2], data_hsizes[2];
    CCTK_REAL *coord_data_i, *coord_data_j;
    void *data;


    /* get the directions to span the hyperslab */
    if (dir == 0)
    {
      dir_i = 1; dir_j = 2;   /* yz */
    }
    else if (dir == 1)
    {
      dir_i = 0; dir_j = 2;   /* xz */
    }
    else
    {
      dir_i = 0; dir_j = 1;   /* xy */
    }

    /* set the origin using the slice center from IOUtil */
    memset (origin, 0, GH->cctk_dim * sizeof (int));
    origin[dir] = myGH->sp2xyz[groupinfo.dim - 1][dir];

    /* set the directions vector */
    memset (directions, 0, sizeof (directions));
    directions[dir] = 1;

    /* get the coordinates for grid function output */
    if (groupinfo.grouptype == CCTK_GF)
    {
      /* get the i-coordinate slice */
      if (Hyperslab_GetHyperslab (GH, 0, coord_index[dir_i],
                                  coord_timelevel[dir_i], 2, origin, directions,
                                  lengths, downsamples,
                                  (void **) &coord_data_i, coord_hsizes) < 0)
      {
        fullname = CCTK_FullName (coord_index[dir_i]);
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Failed to extract 2D hyperslab for variable '%s'",
                    fullname);
        free (fullname);
        return;
      }

      /* get the j-coordinate slice */
      if (Hyperslab_GetHyperslab (GH, 0, coord_index[dir_j],
                                  coord_timelevel[dir_j], 2, origin, directions,
                                  lengths, downsamples,
                                  (void **) &coord_data_j, coord_hsizes) < 0)
      {
        fullname = CCTK_FullName (coord_index[dir_j]);
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Failed to extract 2D hyperslab for variable '%s'",
                    fullname);
        free (fullname);
        free (coord_data_i);
        return;
      }
    }
    else
    {
      /* grid arrays don't have coordinates assigned */
      coord_data_i = NULL;
      coord_data_j = NULL;
    }

    /* get the variable slice */
    if (Hyperslab_GetHyperslab (GH, 0, index, timelevel, 2, origin, directions,
                                lengths, downsamples, &data, data_hsizes) < 0)
    {
      fullname = CCTK_FullName (index);
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Failed to extract 2D hyperslab for variable '%s'", fullname);
      free (fullname);
      if (coord_data_i)
      {
        free (coord_data_i);
      }
      if (coord_data_j)
      {
        free (coord_data_j);
      }
      return;
    }

    /* proc 0 writes */
    if (myproc == 0) 
    {
      CCTK_REAL stagger_offset_i, stagger_offset_j;


      /* get the staggering offset for the coordinates */
      stagger_offset_i = CCTK_StaggerDirIndex (dir_i, groupinfo.stagtype) *
                         0.5 * GH->cctk_delta_space[dir_i];
      stagger_offset_j = CCTK_StaggerDirIndex (dir_j, groupinfo.stagtype) *
                         0.5 * GH->cctk_delta_space[dir_j];

      /* print out header */
      fprintf (fdset_2D[dir], header_fmt_string, GH->cctk_time);

      switch (groupinfo.vartype)
      {
        case CCTK_VARIABLE_CHAR:
          OUTPUT_TYPED_DATA (coord_hsizes, coord_data_i, coord_data_j,
                             stagger_offset_i, stagger_offset_j,
                             data_hsizes, CCTK_CHAR, int, data,
                             groupinfo.grouptype,
                             data_fmt_string_int, fdset_2D[dir]);
          break;

        case CCTK_VARIABLE_INT:
          OUTPUT_TYPED_DATA (coord_hsizes, coord_data_i, coord_data_j,
                             stagger_offset_i, stagger_offset_j,
                             data_hsizes, CCTK_INT, int, data,
                             groupinfo.grouptype,
                             data_fmt_string_int, fdset_2D[dir]);
          break;

        case CCTK_VARIABLE_REAL:
          OUTPUT_TYPED_DATA (coord_hsizes, coord_data_i, coord_data_j,
                             stagger_offset_i, stagger_offset_j,
                             data_hsizes, CCTK_REAL, double, data,
                             groupinfo.grouptype,
                             data_fmt_string_real, fdset_2D[dir]);
          break;

        default:
          CCTK_WARN (1, "Unsupported variable type");
          break;
      }

      /* keep the file open but flush it */
      fflush (fdset_2D[dir]);

      /* free the hyperslabs */
      free (data);
      if (coord_data_i)
      {
        free (coord_data_i);
      }
      if (coord_data_j)
      {
        free (coord_data_j);
      }

    } /* end of outputting the data by processor 0 */

  } /* end of looping through xyz directions */

}