aboutsummaryrefslogtreecommitdiff
path: root/src/Write2D.c
blob: cbaae69bfc394bc33ff5b944047bebae80066a60 (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
/*@@
   @file      Write2D.c
   @date      Tue Feb  3 19:06:12 1998
   @author    Paul Walker
   @desc
   Two dimensional slices into IEEEIO files on MPI distributed cactus data.
   @enddesc
   @history
   @hauthor Gabrielle Allen @hdate 26 Sep 1998
   @hdesc Changed subroutine name (SliceTwoD -> Write2D), renamed IO parameters
   @hauthor Gabrielle Allen @hdate Oct 17 1998
   @hdesc Added the IO_verbose parameter instead of an ifdef
   @hauthor Gabrielle Allen @hdate 19 Oct 1998
   @hdesc Changed names ready for thorn_IO
   @hauthor Thomas Radke @hdate 16 Mar 1999
   @hdesc Converted to Cactus 4.0
   @hauthor Thomas Radke @hdate 17 Mar 1999
   @hdesc Changed naming: IEEEIO -> IOFlexIO
   @hendhistory
   @version $Id$
 @@*/


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

#include "cctk.h"
#include "cctk_Parameters.h"
#include "util_Table.h"
#include "ioFlexGH.h"

static const char *rcsid = "$Id$";
CCTK_FILEVERSION(CactusPUGHIO_IOFlexIO_Write2D_c)


/*@@
   @routine    IOFlexIO_Write2D
   @date       Sat March 6 1999
   @author     Gabrielle Allen
   @desc       Writes the 2D data of a variable into separate IEEEIO files
   @enddesc
   @calledby IOFlexIO_Output2DVarAs
             IOFlexIO_TriggerOutput2D
   @history
   @endhistory
   @var     GH
   @vdesc   Pointer to CCTK GH
   @vtype   const cGH *
   @vio     in
   @vcomment
   @endvar
   @var     vindex
   @vdesc   index of variable to output
   @vtype   int
   @vio     in
   @vcomment
   @endvar
   @var     alias
   @vdesc   alias name of variable to output
   @vtype   const char *
   @vio     in
   @vcomment
   @endvar
@@*/

int IOFlexIO_Write2D (const cGH *GH, int vindex, const char *alias)
{
  DECLARE_CCTK_PARAMETERS
  int dir;                            /* looper for directions */
  int myproc;                         /* my processor ID */
  flexioGH *myGH;                     /* IOFlexIO's extensions */
  int ioflex_type;                    /* IEEEIO output datatype */
  int groupindex;                     /* variable's group index */
  cGroup groupinfo;                   /* variable's group information */
  CCTK_REAL min_ext[3], max_ext[3];   /* bounding box information */
  CCTK_REAL min_ext_attr[2], max_ext_attr[2], delta_attr[2];
  IOFile *IEEEfile_2D;                /* file descriptors */
  int total_hsize, mapping, dir_i, dir_j, coord_system_handle, retval;
  int extent_int[3], extent_flexio[2];
  CCTK_INT origin[3], extent[2], direction[6], hsize[2], coord_handles[3];
  CCTK_INT4 itmp;
  void *data;
  char *fullname, *groupname;


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

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

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

  /* Get the handle for IOFlexIO extension */
  myGH = GH->extensions[CCTK_GHExtensionHandle ("IOFlexIO")];

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

  /* get the appropriate FlexIO data type */
  if (groupinfo.vartype == CCTK_VARIABLE_BYTE)
  {
    ioflex_type = FLEXIO_BYTE;
  }
  else if (groupinfo.vartype == CCTK_VARIABLE_INT)
  {
    ioflex_type = FLEXIO_INT;
  }
  else if (groupinfo.vartype == CCTK_VARIABLE_REAL)
  {
    ioflex_type = FLEXIO_REAL;
  }
  else
  {
    CCTK_WARN (1, "Unsupported variable type in IOFlexIO_Write2D");
    return (-2);
  }

  /* 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 */
    IEEEfile_2D = (IOFile *) GetNamedData (myGH->fileList_2D, alias);
    if (IEEEfile_2D == NULL)
    {
      char *fname;


      IEEEfile_2D = malloc (3 * sizeof (IOFile));
      fname = malloc (strlen (myGH->out2D_dir) + strlen (alias) +
                               strlen (GH->identity) + 20);

      /* Open/Create files */
      for (dir = 0; dir < 3; dir++)
      {
        int len;
        char buffer[128];
        const char *extensions[3] = {"xy", "xz", "yz"};


        sprintf (fname, "%s/%s_2d_%s%s.ieee", myGH->out2D_dir, alias,
                 extensions[dir], GH->identity);

        /* if restart from recovery, try to open an exisiting file ... */
        IEEEfile_2D[dir] = NULL;
        if (! IO_TruncateOutputFiles (GH))
        {
          IEEEfile_2D[dir] = IEEEopen (fname, "a");
        }

        /* otherwise or if that failed, create a new one */
        if (! IOisValid (IEEEfile_2D[dir]))
        {
          IEEEfile_2D[dir] = IEEEopen (fname, "w");
        }
        if (! IOisValid (IEEEfile_2D[dir]))
        {
          CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                      "IOFlexIO_Write2D: Cannot open 2D output file '%s'",
                      fname);
          return (-3);
        }

        /* 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);
          FLEXIO_ERROR (IOwriteAttribute (IEEEfile_2D[dir],
                                                 "parameter file", CHAR,
                                                 strlen (buffer) + 1, buffer));
        }
        if (CCTK_Equals (out_fileinfo, "creation date") ||
            CCTK_Equals (out_fileinfo, "all"))
        {
          buffer[0] = 0;
          Util_CurrentDate (sizeof (buffer), buffer);
          len = strlen (buffer) + 1;
          buffer[len-1] = ' ';
          Util_CurrentTime (sizeof (buffer) - len, buffer + len);
          FLEXIO_ERROR (IOwriteAttribute (IEEEfile_2D[dir],
                                                 "creation date", CHAR,
                                                 strlen (buffer) + 1, buffer));
        }
      }

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

      free (fname);
    }

    if (reuse_filehandles)
    {
      /* resume the files (reopen descriptors)
         This allows descriptor reuse without requiring expensive destruction
         and reallocation of the associated data structures */
      if (CCTK_Equals (verbose, "full"))
      {
        CCTK_VInfo (CCTK_THORNSTRING, "Resuming 2D output files for variable "
                    "'%s'", CCTK_VarName (vindex));
      }

      for (dir = 0; dir < 3; dir++)
      {
        FLEXIO_ERROR (IOresume (IEEEfile_2D[dir]));
      }
    }
  }

  /* get the coordinate ranges */
  groupname = CCTK_GroupNameFromVarI (vindex);
  coord_system_handle = -1;
  if (CCTK_IsFunctionAliased ("Coord_GroupSystem"))
  {
    coord_system_handle = Coord_GroupSystem (GH, groupname);
  }
  free (groupname);

  if (coord_system_handle >= 0 &&
      Util_TableGetIntArray (coord_system_handle, 3,
                             coord_handles, "COORDINATES") >= 0)
  {
    for (dir = 0; dir < 3; dir++)
    {
      Util_TableGetReal (coord_handles[dir], &min_ext[dir], "COMPMIN");
      Util_TableGetReal (coord_handles[dir], &max_ext[dir], "COMPMAX");
    }
  }

  /* get the extents of the variable */
  CCTK_GroupgshVI (GH, 3, extent_int, vindex);

  /* loop over all 3 planes */
  for (dir = 0; dir < 3; dir++)
  {
    /* get the directions to span the hyperslab */
    if (dir == 0)
    {
      dir_i = 0; dir_j = 1;   /* xy */
    }
    else if (dir == 1)
    {
      dir_i = 0; dir_j = 2;   /* xz */
    }
    else
    {
      dir_i = 1; dir_j = 2;   /* yz */
    }

    /* set the extent vector */
    extent[0] = extent_int[dir_i];
    extent[1] = extent_int[dir_j];
    extent_flexio[0] = extent_int[dir_i];
    extent_flexio[1] = extent_int[dir_j];

    /* set the origin using the slice center from IOUtil */
    memset (origin, 0, sizeof (origin));
    origin[3-dir-1] = myGH->sp2xyz[2][dir];

    /* set the direction vector */
    memset (direction, 0, sizeof (direction));
    direction[dir_i] = direction[3 + dir_j] = 1;

    mapping = Hyperslab_GlobalMappingByIndex (GH, vindex, 2,
                                              direction, origin, extent,
                                              NULL, /* downsample */
                                              -1,   /* table handle */
                                              NULL  /* conversion fn */,
                                              hsize);
    if (mapping < 0)
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "IOFlexIO_Write2D: Failed to define hyperslab mapping for "
                  "variable '%s'", fullname);
      continue;
    }
    total_hsize = hsize[0] * hsize[1];
    if (total_hsize <= 0)
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "IOFlexIO_Write2D: selected hyperslab has zero size for "
                  "variable '%s' direction %d", fullname, dir);
      Hyperslab_FreeMapping (mapping);
      continue;
    }

    /* allocate hyperslab buffers */
    data = NULL;
    if (myproc == 0)
    {
      data = malloc (total_hsize * CCTK_VarTypeSize (groupinfo.vartype));
    }

    /* get the variable slice */
    retval = Hyperslab_Get (GH, mapping, 0, vindex, 0, -1, data);

    /* release the mapping structure */
    Hyperslab_FreeMapping (mapping);

    /* processor 0 write the hyperslab data */
    if (myproc == 0)
    {
      if (retval >= 0)
      {
        /* the data itself */
        FLEXIO_ERROR (IOwrite (IEEEfile_2D[dir],
                               ioflex_type, 2, extent_flexio, data));

        /* Time, space, and coordinate attributes */
        min_ext_attr[0] = min_ext[dir_i];
        min_ext_attr[1] = min_ext[dir_j];
        max_ext_attr[0] = max_ext[dir_i];
        max_ext_attr[1] = max_ext[dir_j];
        delta_attr[0] = GH->cctk_delta_space[dir_i];
        delta_attr[1] = GH->cctk_delta_space[dir_j];

        FLEXIO_ERROR (IOwriteAttribute (IEEEfile_2D[dir], "name", CHAR,
                                        strlen (fullname) + 1, fullname));
        itmp = GH->cctk_iteration;
        FLEXIO_ERROR (IOwriteAttribute (IEEEfile_2D[dir], "iteration",
                                        FLEXIO_INT4, 1, &itmp));
        itmp = 0;
        FLEXIO_ERROR (IOwriteAttribute (IEEEfile_2D[dir], "timelevel",
                                        FLEXIO_INT4, 1, &itmp));
        FLEXIO_ERROR (IOwriteAttribute (IEEEfile_2D[dir], "time",
                                        FLEXIO_REAL, 1, &GH->cctk_time));
        FLEXIO_ERROR (IOwriteAttribute (IEEEfile_2D[dir], "origin",
                                        FLEXIO_REAL, 2, min_ext_attr));
        FLEXIO_ERROR (IOwriteAttribute (IEEEfile_2D[dir], "min_ext",
                                        FLEXIO_REAL, 2, min_ext_attr));
        FLEXIO_ERROR (IOwriteAttribute (IEEEfile_2D[dir], "max_ext",
                                        FLEXIO_REAL, 2, max_ext_attr));
        FLEXIO_ERROR (IOwriteAttribute (IEEEfile_2D[dir], "delta",
                                        FLEXIO_REAL, 2, delta_attr));
      }
      else
      {
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Failed to extract 2D hyperslab for variable '%s'", fullname);
      }

      free (data);
    }

  } /* end of loop over all 3 planes */

  free (fullname);

  if (reuse_filehandles && myproc == 0)
  {
    /* pause the files (close system descriptors) */
    if (CCTK_Equals (verbose, "full"))
    {
      CCTK_VInfo (CCTK_THORNSTRING, "Pausing 2D output files for variable '%s'",
                  CCTK_VarName (vindex));
    }

    for (dir = 0; dir < 3; dir++)
    {
      FLEXIO_ERROR (IOpause (IEEEfile_2D[dir]));
    }
  }

  return (0);
}