aboutsummaryrefslogtreecommitdiff
path: root/src/OutputInfo.c
blob: 779ac80249c6adee134848e37808e98a38dac2fc (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
 /*@@
   @file      OutputInfo.c
   @date      June 31 1999
   @author    Gabrielle Allen
   @desc 
              Functions to deal with info output of variables
   @enddesc 
   @version   $Header$
 @@*/
 
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cctk.h"
#include "cctk_Parameters.h"
#include "iobasicGH.h"

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

/* uncomment the following to get some debugging output */
/* #define IO_DEBUG 1 */

/* the number at which to switch from decimal to exp notation */
#define DECIMAL_PRECISION       1.0e-8
#define DECIMAL_TOOBIG          1.0e+8
#define USE_DECIMAL_NOTATION(x) ((fabs (x) > DECIMAL_PRECISION) ||            \
                                 ((x) == 0.0) ||                              \
                                 (fabs(x) < DECIMAL_TOOBIG))

/* prototypes of routines defined in this source file */
int IOBasic_OutputInfoGH (cGH *GH);
int IOBasic_TimeForInfo (cGH *GH, int vindex);
int IOBasic_TriggerOutputInfo (cGH *GH, int vindex);
static void SetOutputFlag (int vindex, const char *optstring, void *arg);

/* static variables */
static int print_header = 1;
static int outInfo_vars_lastset = -1;


 /*@@
   @routine    IOBasic_OutputInfoGH
   @date       June 31 1999
   @author     Gabrielle Allen
   @desc
               Loops over all variables and prints output if requested
   @enddesc
   @calls      CCTK_GHExtension
               CCTK_ParameterQueryTimesSet
               CCTK_TraverseString
               CCTK_NumVars
               CCTK_VarName
               IOBasic_WriteInfo

   @var        GH
   @vdesc      Pointer to CCTK GH
   @vtype      cGH *
   @vio        in
   @endvar 

   @returntype int
   @returndesc
               0 for success
   @endreturndesc
@@*/
int IOBasic_OutputInfoGH (cGH *GH)
{
  DECLARE_CCTK_PARAMETERS
  int i;
  int ierr1;
  int ierr2;
  int times_set;
  const char *vname;
  iobasicGH  *myGH;
  char ll[80], l1[1024], l2[1024], l3[1024];


  /* Get the GH extensions for IOBasic */
  myGH = (iobasicGH *) CCTK_GHExtension (GH, "IOBasic");

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

  /* Return if no output is required */
  if (myGH->outInfo_every < 1 || 
      GH->cctk_iteration % myGH->outInfo_every != 0)
  {
    return (0);
  }

  /* re-parse the 'outInfo_vars' parameter if it was changed
     and also print a header */
  times_set = CCTK_ParameterQueryTimesSet ("outInfo_vars", CCTK_THORNSTRING);
  if (times_set != outInfo_vars_lastset) 
  {  
    memset (myGH->do_outInfo, 0, CCTK_NumVars ());
    CCTK_TraverseString (outInfo_vars, SetOutputFlag, myGH->do_outInfo,
                         CCTK_GROUP_OR_VAR);

    /* Save the last setting of 'outInfo_vars' parameter */
    outInfo_vars_lastset = times_set;

    print_header = 1;
  }

  if (print_header)
  {
    print_header = 0;

    sprintf (l1," it  |         |");
    sprintf (l2,"     |    t    |");
    sprintf (l3,"----------------");
      
    for (i = 0; i < CCTK_NumVars (); i++)
    {
      if (myGH->do_outInfo[i])
      {
        sprintf (ll, "                                        ");
        ll[25 - strlen (CCTK_VarName (i))] = '\0';
        sprintf (l1, "%s %s%s |", l1, CCTK_VarName (i), ll);
        sprintf (l2, "%s Min          Max          |", l2);
        sprintf (l3, "%s----------------------------", l3);
      }
    }
    printf ("%s\n%s\n%s\n%s\n", l3, l1, l2, l3);
    fflush (stdout);
  }

  /* Print the iteration/timestep information for all variables */        
  if (USE_DECIMAL_NOTATION (GH->cctk_time))
  {
    printf ("%4d |%9.3f|", GH->cctk_iteration, GH->cctk_time);    
  }  
  else
  {
    printf ("%4d |%7.3e|", GH->cctk_iteration, GH->cctk_time);
  }

  /* Loop over all variables */
  for (i = 0; i < CCTK_NumVars (); i++) 
  {

    /* Check this Variable should be output */
    if (! myGH->do_outInfo[i])
    {
      continue;
    }

    ierr1 = ierr2 = 0;

    /* Check variable not already output this iteration */
    if (myGH->outInfo_last[i] != GH->cctk_iteration) 
    {

      /* Get the variable name for this index (for filename) */
      vname = CCTK_VarName (i);

#ifdef IO_DEBUG
      printf("\nIn IO OutputInfoGH\n----------------\n");
      printf("  Index = %d\n", i);
      printf("  Variable = -%s-\n", vname);
      printf("  Last output iteration was = %d\n", myGH->outInfo_last[i]);
#endif

      /* Make the IO call */
      ierr1 = IOBasic_WriteInfo (GH, &myGH->infovals[i][0], i, "minimum",vname);
      ierr2 = IOBasic_WriteInfo (GH, &myGH->infovals[i][1], i, "maximum",vname);

      /* Register GF as having info output this iteration */
      myGH->outInfo_last[i] = GH->cctk_iteration;
    }

    if (ierr1 == 0)
    {
      /* finally print out the stuff */
      if (USE_DECIMAL_NOTATION (myGH->infovals[i][0]))
      {
        printf ("%12.8f |", myGH->infovals[i][0]);
      }
      else
      {
        printf ("%10.6e |", myGH->infovals[i][0]);
      }
    }
    else
    {
      printf(" ----------- |");
    }

    if (ierr2 == 0)
    {
      if (USE_DECIMAL_NOTATION (myGH->infovals[i][1]))
      {
        printf ("%12.8f |", myGH->infovals[i][1]);
      }
      else
      {
        printf ("%10.6e |", myGH->infovals[i][1]);
      }
    }
    else
    {
      printf(" ----------- |");
    }

  } /* end of loop over all variables */

  /* Add the new line */
  printf ("\n");
  fflush(stdout);

  return (0);
}


 /*@@
   @routine    IOBasic_TimeForInfo
   @date       June 31 1999
   @author     Gabrielle Allen
   @desc
               Decides if it is time to output a variable using info output
   @enddesc
   @calls      CCTK_GHExtensionHandle 
               CCTK_GroupTypeFromVarI
               CCTK_WARN
               CCTK_QueryGroupStorageI 
               CCTK_GroupFromVar               
 
   @var        GH
   @vdesc      Pointer to CCTK GH
   @vtype      cGH *
   @vio        in
   @endvar 
   @var        vindex
   @vdesc      index of variable to output
   @vtype      int
   @vio        in
   @endvar 

   @returntype int
   @returndesc
               true/false (1 or 0) if this variable should be output or not
   @endreturndesc
@@*/
int IOBasic_TimeForInfo (cGH *GH, int vindex)
{
  DECLARE_CCTK_PARAMETERS
  int retval;
  int times_set;
  iobasicGH *myGH;
  char *fullname;


  /* Default is do not do output */
  retval = 0;

  /* Get the GH extensions for IOBasic */
  myGH = (iobasicGH *) CCTK_GHExtension (GH, "IOBasic");

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

  /* return if no output requested */
  if (myGH->outInfo_every <= 0)
  {
    return (0);
  }

  /* re-parse the 'outInfo_vars' parameter if it was changed
     and also print a header */
  times_set = CCTK_ParameterQueryTimesSet ("outInfo_vars", CCTK_THORNSTRING);
  if (times_set != outInfo_vars_lastset) 
  {
    memset (myGH->do_outInfo, 0, CCTK_NumVars ());
    CCTK_TraverseString (outInfo_vars, SetOutputFlag, myGH->do_outInfo,
                         CCTK_GROUP_OR_VAR);

    /* Save the last setting of 'outInfo_vars' parameter */
    outInfo_vars_lastset = times_set;

    print_header = 1;
  }

  /* Check if this variable should be output */
  if (myGH->do_outInfo[vindex] &&
      (GH->cctk_iteration % myGH->outInfo_every == 0))
  {
      
    /* Check GF not already output this iteration */
    if (myGH->outInfo_last[vindex] == GH->cctk_iteration)
    {
      fullname = CCTK_FullName (vindex);
      CCTK_VWarn (5, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Already done Info output for '%s' in current "
                  "iteration (probably via triggers)", fullname);
      free (fullname);
    }
    else
    {
      retval = 1;
    }
  }

  return (retval);
}


 /*@@
   @routine    IOBasic_TriggerOutputInfo
   @date       June 31 1999
   @author     Gabrielle Allen
   @desc
               Triggers the output of a variable using IOBasic's info output
   @enddesc
   @calls      CCTK_GHExtensionHandle 
               IOBasic_WriteInfo

   @var        GH
   @vdesc      Pointer to CCTK GH
   @vtype      cGH *
   @vio        in
   @endvar
   @var        vindex
   @vdesc      index of variable to output
   @vtype      int
   @vio        in
   @endvar

   @returntype int
   @returndesc
               0 for success
   @endreturndesc
@@*/
int IOBasic_TriggerOutputInfo (cGH *GH, int vindex)
{
  iobasicGH *myGH;
  const char *vname;


  /* Get the GH extension for IOBasic */
  myGH = (iobasicGH *) CCTK_GHExtension (GH, "IOBasic");
 
  vname = CCTK_VarName (vindex);

  /* Do the Info output */
#ifdef IO_DEBUG
  printf ("\nIn IO TriggerOutputInfo\n---------------------\n");
  printf ("  Index = %d\n", vindex);
  printf ("  Variable = -%s-\n", vname);
#endif
  
  IOBasic_WriteInfo (GH, &myGH->infovals[vindex][0], vindex, "minimum", vname);
  IOBasic_WriteInfo (GH, &myGH->infovals[vindex][1], vindex, "maximum", vname);

  /* Register variable as having Info output at this iteration */
  myGH->outInfo_last[vindex] = GH->cctk_iteration;

  return (0);
}


/***************************************************************************/
/*                            local functions                              */
/***************************************************************************/
/* callback for CCTK_TraverseString() to set the output flag
   for the given variable */
static void SetOutputFlag (int vindex, const char *optstring, void *arg)
{
  char *flags = (char *) arg;


  flags[vindex] = 1;

  if (optstring)
  {
    CCTK_VWarn (5, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Optional string '%s' in variable name ignored", optstring);
  }
}