aboutsummaryrefslogtreecommitdiff
path: root/src/Output2D.c
blob: 9c8a8e44d6a60fca46a2b8573db2a7aff74093c3 (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
 /*@@
   @file      Output2D.c
   @date      Sun 23 Dec 2001
   @author    Thomas Radke
   @desc
              Functions to deal with 2D JPeg output.
   @enddesc
   @version   $Id$
 @@*/


#include "cctk.h"

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

#include "cctk_Parameters.h"
#include "CactusBase/IOUtil/src/ioutil_Utils.h"
#include "IOJpeg.h"

static const char *rcsid = "$Header$";

CCTK_FILEVERSION(CactusIO_IOJpeg_Output2D_c)

/* function prototypes */
static void CheckSteerableParameters (IOJpegGH *myGH);
static void SetOutputFlag (int vindex, const char *optstring, void *arg);

 /*@@
   @routine    IOJpeg_Output2DGH
   @date       Thu Sep 14 10:47:58 2000
   @author     Gerd Lanfermann
   @desc 
     Preparing 2D data for output
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
int IOJpeg_Output2DGH (const cGH *GH)
{
  int i, retval;
  IOJpegGH *myGH;
  const char *name;
  char *fullname;
  DECLARE_CCTK_PARAMETERS


  /* Get the GH extension for IOJpeg */
  myGH = (IOJpegGH *) CCTK_GHExtension (GH, "IOJpeg");

  CheckSteerableParameters (myGH);

  if (myGH->out2D_every <= 0)
  {
    return (0);
  }

  /* Loop over all variables */
  for (i = retval = 0; i < CCTK_NumVars (); i++)
  {
    if (IOJpeg_TimeFor2D (GH, i))
    {
      name     = CCTK_VarName (i);
      fullname = CCTK_FullName (i);

      if (!CCTK_Equals(verbose,"no"))
      {
        CCTK_VInfo (CCTK_THORNSTRING, "IOJpeg_Output2DGH: fullname / name = "
                    "%s / %s", fullname, name);
      }

      if (IOJpeg_Output2DVarAs (GH, fullname, name) == 0)
      {
        /* Register variable as having 2D output this iteration */
        myGH->out2D_last [i] = GH->cctk_iteration;
        retval++;
      }

      free (fullname);
    }
  }

  return (retval);
}


int IOJpeg_Output2DVarAs (const cGH *GH, const char *fullname, const char *alias)
{
  int vindex, retval;
  DECLARE_CCTK_PARAMETERS


  vindex = CCTK_VarIndex (fullname);

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

  if (!CCTK_Equals(verbose,"no"))
  {
    CCTK_VInfo (CCTK_THORNSTRING, "IOJpeg_Output2DVarAs: fullname, alias, "
                "index = (%s, %s, %d)", fullname, alias, vindex);
  }

  /* Do the 2D output */
  retval = IOJpeg_Write2D (GH, vindex, alias);

  return (retval);
}


int IOJpeg_TimeFor2D (const cGH *GH, int vindex)
{
  IOJpegGH *myGH;


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

  CheckSteerableParameters (myGH);

  /* Check if any output was requested */
  if(myGH->out2D_every <= 0)
  {
    return (0);
  }

  /* Check this variable should be output */
  if (! (myGH->do_out2D [vindex] && GH->cctk_iteration % myGH->out2D_every == 0))
  {
    return (0);
  }


  /* Check variable not already output this iteration */
  if (myGH->out2D_last [vindex] == GH->cctk_iteration)
  {
    CCTK_WARN (2, "Already done 2D output in IOJpeg");
    return (0);
  }

  return (1);
}


int IOJpeg_TriggerOutput2D (const cGH *GH, int vindex)
{
  int retval;
  IOJpegGH *myGH;
  const char *varname;
  DECLARE_CCTK_PARAMETERS


  varname = CCTK_VarName (vindex);

  myGH = (IOJpegGH *) CCTK_GHExtension (GH, "IOJpeg");

  if (!CCTK_Equals(verbose,"no"))
  {
    CCTK_VInfo (CCTK_THORNSTRING, "IOJpeg_TriggerOutput2D: varname, index = "
                "(%s, %d)", varname, vindex);
  }

  /* Do the 2D output */
  retval = IOJpeg_Write2D (GH, vindex, varname);

  if (retval == 0)
  {
    /* Register variable as having 2D output this iteration */
    myGH->out2D_last [vindex] = GH->cctk_iteration;
  }

  return (retval);
}


/**************************** local functions ******************************/
static void CheckSteerableParameters (IOJpegGH *myGH)
{
  int i, out_old, out2D_vars_current_nset;
  char *msg, *fullname;
  static int out2D_vars_lastset = 0;
  DECLARE_CCTK_PARAMETERS


  out_old = myGH->out2D_every;

  /* How often to output */

  myGH->out2D_every = out_every > 0 ? out_every : -1;
  if (out2D_every > 0)
  {
    myGH->out2D_every = out2D_every;
  }

  /* Report if frequency changed */

  if (myGH->out2D_every != out_old)
  {
    if (CCTK_Equals (newverbose, "standard") ||
        CCTK_Equals (newverbose, "full"))
    {
      CCTK_VInfo (CCTK_THORNSTRING, "IOJpeg_2D: Output every %d iterations",
                  myGH->out2D_every);
    }
    out_old = myGH->out2D_every;
  }

  /* re-parse the 'out2D_vars' parameter if it was changed */

  out2D_vars_current_nset = CCTK_ParameterQueryTimesSet ("out2D_vars",
                                                         CCTK_THORNSTRING);
  if (out2D_vars_current_nset != out2D_vars_lastset)
  {
    memset (myGH->do_out2D, 0, CCTK_NumVars ());
    CCTK_TraverseString (out2D_vars, SetOutputFlag, myGH->do_out2D,
                         CCTK_GROUP_OR_VAR);

    if (CCTK_Equals (newverbose, "standard") ||
        CCTK_Equals (newverbose, "full"))
    {
      msg = NULL;
      for (i = CCTK_NumVars () - 1; i >= 0; i--)
      {
	if (myGH->do_out2D[i])
	{
          fullname = CCTK_FullName (i);
          if (! msg)
          {
            Util_asprintf (&msg, "IOJpeg_2D: Output requested for %s",fullname);
          }
          else
          {
            Util_asprintf (&msg, "%s %s", msg, fullname);
          }
          free (fullname);
	}
      }
      if (msg)
      {
        CCTK_INFO (msg);
        free (msg);
      }
    }    

    /* Save the last setting of 'out2D_vars' parameter */
    out2D_vars_lastset = out2D_vars_current_nset;
  }
}


/* 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);
  }
}