aboutsummaryrefslogtreecommitdiff
path: root/src/GHExtension.c
blob: c36facd1b6a71f37c516362ba63bb45163cfb8c3 (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
 /*@@
   @file      GHExtension.c
   @date      Fri May 21 1999
   @author    Thomas Radke
   @desc 
   IOFlexIO GH extension stuff.
   @enddesc 
   @history
   @hauthor Thomas Radke @hdate May 21 1999
   @hdesc Just copied from thorn IO.
   @endhistory
 @@*/

/*#define DEBUG_IO*/

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

#include "cctk.h"
#include "cctk_Parameters.h"
#include "CactusPUGH/PUGH/src/include/pugh.h"
#include "CactusBase/IOUtil/src/ioGH.h"
#include "ioFlexGH.h"


/* local function prototypes */
static void SetupSliceCenter (cGH *GH);


 /*@@
   @routine   IOFlexIO_SetupGH
   @date      Tue May 09 2000
   @author    Thomas Radke
   @desc 
   Allocates the IOFlexIO GH extension structure.
   @enddesc 
   @calledby   CCTK scheduler at CCTK_INITIALIZE
   @var        config
   @vdesc      flesh configuration structure (unused)
   @vtype      tFleshConfig *
   @vio        in
   @endvar
   @var        convergence_level
   @vdesc      convergence level (unused)
   @vtype      int
   @vio        in
   @endvar
   @var        GH
   @vdesc      pointer to grid hierarchy 
   @vtype      cGH *
   @vio        in
   @endvar
   @history 
 
   @endhistory 
@@*/
void *IOFlexIO_SetupGH (tFleshConfig *config, int convergence_level, cGH *GH)
{
  DECLARE_CCTK_PARAMETERS
  int numvars;
  flexioGH *newGH;


  numvars = CCTK_NumVars ();

  newGH = (flexioGH *) malloc (sizeof (flexioGH));
  newGH->do_out2D = (char *) malloc (numvars * sizeof (char));
  newGH->do_out3D = (char *) malloc (numvars * sizeof (char));
  newGH->out2D_last = (int *) malloc (numvars * sizeof (int));
  newGH->out3D_last = (int *) malloc (numvars * sizeof (int));

  return (newGH);
}


 /*@@
   @routine   IOFlexIO_InitGH
   @date      Tue May 09 2000
   @author    Thomas Radke
   @desc 
   The GH initialization routine for IOFlexIO.
   Necessary output dirs are created.
   For 2D output the slice center is set up.
   Checkpoint/recovery timers are created if timing information was requested.
   @enddesc 
   @calledby   CCTK scheduler at CCTK_INITIALIZE
   @var        GH
   @vdesc      pointer to grid hierarchy 
   @vtype      cGH *
   @vio        in
   @endvar
   @history 
 
   @endhistory 
@@*/
int IOFlexIO_InitGH (cGH *GH)
{
  DECLARE_CCTK_PARAMETERS
  int i;
  flexioGH *myGH; 
  const cParamData *paramdata;


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

  /* How often to output */
  myGH->out2D_every = out_every > 0 ? out_every : -1;
  myGH->out3D_every = out_every > 0 ? out_every : -1;
  if (out2D_every > 0)
  {
    myGH->out2D_every = out2D_every;
  }
  if (out3D_every > 0)
  {
    myGH->out3D_every = out3D_every;
  }

  ParseVarsForOutput (out2D_vars, myGH->do_out2D);
  ParseVarsForOutput (out3D_vars, myGH->do_out3D);

  /* Check whether "outdir2D" and/or "outdir3D" was set.
     If so take these dirs otherwise default to "IO::outdir" */
  paramdata = CCTK_ParameterData ("outdir2D", CCTK_THORNSTRING);
  if (paramdata && paramdata->n_set > 0)
  {
    myGH->outdir2D = strdup (outdir2D);
  }
  else
  {
    myGH->outdir2D = strdup (outdir);
  }

  paramdata = CCTK_ParameterData ("outdir3D", CCTK_THORNSTRING);
  if (paramdata && paramdata->n_set > 0)
  {
    myGH->outdir3D = strdup (outdir3D);
  }
  else
  {
    myGH->outdir3D = strdup (outdir);
  }

  /* Create the output directories */
  if (CCTK_MyProc (GH) == 0) {
    i = CCTK_CreateDirectory (myGH->outdir2D,0755);
    if (i < 0)
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
         "Problem creating IOFlexIO 2D output directory '%s'", myGH->outdir2D);
    if (i > 0)
      CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
         "2D IOFlexIO output directory '%s' already exists", myGH->outdir2D);
    i = CCTK_CreateDirectory (myGH->outdir3D,0755);
    if (i < 0)
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
         "Problem creating IOFlexIO 3D output directory '%s'", myGH->outdir3D);
    if (i > 0)
      CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
         "3D IOFlexIO output directory '%s' already exists", myGH->outdir3D);
  }

  for (i=0; i<CCTK_NumVars(); i++)
  {
    myGH->out2D_last [i] = myGH->out3D_last [i] = -1;
  }

  myGH->fileList_2D = myGH->fileList_3D = NULL;

  /* set up 2D planes to plot */
  SetupSliceCenter (GH);

  /* create timers if timing info was requested */
  if (print_timing_info) 
  {
    myGH->dumpVarsTimer = CCTK_TimerCreateI ();
    myGH->dumpParamsTimer = CCTK_TimerCreateI ();
    myGH->checkpointTotalTimer = CCTK_TimerCreateI ();
    myGH->recoverTotalTimer = CCTK_TimerCreateI ();

    if (myGH->dumpVarsTimer < 0)
    {
      CCTK_WARN (1, "Could not create timer. Timing info on dumping datasets "
                    "will not be available.");
    }    
    if (myGH->dumpParamsTimer < 0)
    {
      CCTK_WARN (1, "Could not create timer. Timing info on dumping parameters"
                    " will not be available.");
    }
    if (myGH->checkpointTotalTimer < 0)
    {
      CCTK_WARN (1, "Could not create timer ! Timing info on total checkpoint "
                    "time will not be available.");
    }
    if (myGH->recoverTotalTimer < 0)
    {
      CCTK_WARN (1, "Could not create timer ! Timing info on total recovery "
                    "time will not be available.");
    }
    CCTK_TimerResetI (myGH->checkpointTotalTimer);
    CCTK_TimerResetI (myGH->recoverTotalTimer);
  }

  return (0);
}


 /*@@
   @routine   IOFlexIO_TerminateGH
   @date      Tue May 09 2000
   @author    Thomas Radke
   @desc 
   The termination routine for IOFlexIO.
   All open files (stored in the file lists) are closed,
   and the file lists are freed.
   Also any created timers are destroyed. 
   @enddesc 
   @calledby   CCTK scheduler at TERMINATE
   @var        GH
   @vdesc      pointer to grid hierarchy 
   @vtype      cGH *
   @vio        in
   @endvar
   @history 
 
   @endhistory 
@@*/
int IOFlexIO_TerminateGH (cGH *GH)
{
  DECLARE_CCTK_PARAMETERS
  flexioGH *myGH;
  pNamedData *current;
  IOFile *IEEEfiles_2D;
  IEEEfile_3D_t *IEEEfile_3D;


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

  /* immediately return if IOFlexIO wasn't active */
  if (! myGH)
    return (0);

  /* close any open files by walking through the file lists */
  current = myGH->fileList_2D;
  while (current)
  {
    IEEEfiles_2D = (IOFile *) current->data;
    if (verbose)
      CCTK_VInfo (CCTK_THORNSTRING, "Closing 2D IEEEIO output files "
                  "for variable alias '%s'", current->name);
    IOclose (IEEEfiles_2D [0]);
    IOclose (IEEEfiles_2D [1]);
    IOclose (IEEEfiles_2D [2]);
    free (IEEEfiles_2D);

    current = current->next;
  }

  current = myGH->fileList_3D;
  while (current)
  {
    IEEEfile_3D = (IEEEfile_3D_t *) current->data;
    if (verbose)
      CCTK_VInfo (CCTK_THORNSTRING, "Closing IEEEIO output file '%s'",
                  IEEEfile_3D->filename);
    IOclose (IEEEfile_3D->iofile);
    free (IEEEfile_3D->filename);
    free (IEEEfile_3D);

    current = current->next;
  }

  /* free the file lists */
  DestroyNamedDataList (myGH->fileList_2D);
  DestroyNamedDataList (myGH->fileList_3D);

  if (print_timing_info)
  {
    CCTK_TimerDestroyI (myGH->dumpVarsTimer);
    CCTK_TimerDestroyI (myGH->dumpParamsTimer);
    CCTK_TimerDestroyI (myGH->checkpointTotalTimer);
    CCTK_TimerDestroyI (myGH->recoverTotalTimer);
  }

  /* finally unregister and free my GH extension structure */
  CCTK_UnregisterGHExtension ("IOFlexIO");
  free (myGH);

  return (0);
}


/****************************************************************************/
/* local routines                                                           */
/****************************************************************************/
static void SetupSliceCenter (cGH *GH)
{
  DECLARE_CCTK_PARAMETERS
  int dim;
  int slice_center;
  pGH *pughGH;
  flexioGH *myGH;


  /* Get the handles for PUGH and IOFlexIO extensions */
  pughGH = PUGH_pGH (GH);
  myGH = (flexioGH *) GH->extensions [CCTK_GHExtensionHandle ("IOFlexIO")];

  slice_center=1;
  if (   CCTK_Equals (domain, "octant")
      || CCTK_Equals (domain, "quadrant")
      || CCTK_Equals (domain, "bitant"))
  {
    slice_center=0;
  }

  if (slice_center)
  {
    /* For NONE octant mode: the center for slicings */
    /* is moved to the center index */
    /* (unless nx,ny,nz=1) */

    for (dim = 0; dim < pughGH->dim; dim++)
    {
      if (pughGH->GFExtras[dim]->lnsize [dim] == 1)
      {
        myGH->sp2xyz [dim] = 0;
      }
      else
      {
        myGH->sp2xyz [dim] = 
	  pughGH->GFExtras[dim]->nsize [dim]/2 -
	  pughGH->GFExtras[dim]->lb [pughGH->myproc][dim];
      }
    }

  }

  /* for octant mode, the slicing center is the index 1 or zero*/
  else

  {
    for (dim = 0; dim < GH->cctk_dim; dim++)
    {
      if (pughGH->GFExtras[dim]->lnsize [dim] == 1)
      {
        myGH->sp2xyz [dim] = 0;
      }
      else
      {
        myGH->sp2xyz [dim] = 1 - 
	  pughGH->GFExtras[dim]->lb [pughGH->myproc][dim];
      }
    }
  }

  /* In quadrant mode x and y are like full, but z is like octant */
  if (CCTK_Equals (domain, "quadrant")) 
  {
    if (pughGH->GFExtras[2]->lnsize [2] == 1)
    {
      myGH->sp2xyz[2] = 0;
    }
    else
    {
      myGH->sp2xyz[2] = 
	pughGH->GFExtras[2]->lnsize [2]/2 - 
	pughGH->GFExtras[2]->lb [pughGH->myproc][2];
    }
  }

  if (CCTK_Equals (domain, "bitant")) 
  {
    if (pughGH->GFExtras[2]->lnsize [0] == 1)
    {
      myGH->sp2xyz[0] = 0;
    }
    else
    {
      myGH->sp2xyz[0] = 
	myGH->sp2xyz[0]/2 - 
	pughGH->GFExtras[2]->lb [pughGH->myproc][0];
    }

    if (pughGH->GFExtras[2]->lnsize [1] == 1)
    {
      myGH->sp2xyz[1] = 0;
    }
    else
    {
      myGH->sp2xyz[1] = 
	pughGH->GFExtras[2]->lnsize [1]/2 - 
	pughGH->GFExtras[2]->lb [pughGH->myproc][1];
    }
  }
}