aboutsummaryrefslogtreecommitdiff
path: root/src/WriteGF.c
blob: 71f7b05be7e45af47d9f2b01418615397d1ab8c0 (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
/*@@
   @routine    WriteGF
   @date       Tue Apr  1 16:45:35 1997
   @author     Paul Walker
   @desc 
               Dumps the scalar data of CCTK_ARRAY and CCTK_GF variables.
   @enddesc 
   @version   $Id$
@@*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>      /* strlen(3) */
#include <ctype.h>       /* isalpha(3) */
#include <sys/types.h>
#include <sys/stat.h>    /* stat(2) */

#include "cctk.h"
#include "cctk_Parameters.h"
#include "CactusBase/IOUtil/src/ioutil_AdvertisedFiles.h"
#include "CactusBase/IOUtil/src/ioutil_CheckpointRecovery.h"
#include "iobasicGH.h"


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


 /*@@
   @routine   IOBasic_WriteGF
   @date      Mon Jun 19 2000
   @author    Thomas Radke
   @desc
              Apply the given reduction operators on the CCTK_ARRAY or
              CCTK_GF variable and output the result into a text file
              suitable for postprocessing by either gluplot or xgraph.
   @enddesc

   @calls     CCTK_QueryGroupStorageI
              CCTK_Reduce
              CCTK_ReductionHandle
              IOUtil_RestartFromRecovery
              IOUtil_AdvertiseFile

   @var       GH
   @vdesc     Pointer to CCTK grid hierarchy
   @vtype     cGH *
   @vio       in
   @endvar
   @var       vindex
   @vdesc     CCTK index of the variable to output
   @vtype     int
   @vio       in
   @endvar
   @var       alias
   @vdesc     alias name to use for building the output filename
   @vtype     const char *
   @vio       in
   @endvar
@@*/
void IOBasic_WriteGF (cGH *GH,
                      int vindex,
                      const char *alias)
{
  DECLARE_CCTK_PARAMETERS
  int ierr;
  int reduction_handle;
  iobasicGH *myGH;
  FILE *file;
  char *openmode;
  char *filename;
  char *reduction_op;
  char *string_start;
  char *string_end;
  char comment_char;
  char format_str[15];
  const char *file_extension;
  char *fullname, buffer[128];
  struct stat fileinfo;
  ioAdvertisedFileDesc advertised_file;
  CCTK_REAL reduction_value;
  /* add more reductions to this table if you want, telling it
       - the reduction operator
       - the label for the file header
       - the filename extension (suffixed by ".tl" for trace line) */


  /* first, check if variable has storage assigned */
  if (! CCTK_QueryGroupStorageI (GH, CCTK_GroupIndexFromVarI (vindex)))
  {
    fullname = CCTK_FullName (vindex);
    CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                "IOBasic_WriteGF: No scalar output for '%s' (no storage)", 
                fullname);
    free (fullname);
    return;
  }

  /* set output format */
  sprintf (format_str, "%%%s\t%%%s\n", out_format, out_format);

  /* set the output file extension and the output style */
  if (CCTK_Equals (outScalar_style, "gnuplot"))
  {
    file_extension = ".asc";
    comment_char = '#';
  }
  else
  {
    file_extension = ".xg";
    comment_char = '"';
  }

  /* get the GH extension handle for IOBasic */
  myGH = (iobasicGH *) CCTK_GHExtension (GH, "IOBasic");

  /* allocate strings for the filename and the reduction operator */
  filename = (char *) malloc (strlen (myGH->outdirScalar) + strlen (alias) +
                              strlen (outScalar_reductions) +
                              strlen (file_extension) + 3);
  reduction_op = (char *) malloc (strlen (outScalar_reductions) + 1);

  /* now loop over all reduction operators */
  string_start = outScalar_reductions;
  while (string_start && *string_start)
  {
    /* skip leading spaces */
    while (isspace (*string_start))
    {
      string_start++;
    }
    if (! *string_start)
    {
      break;
    }

    /* advance to end of the operator string */
    string_end = string_start + 1;
    while (*string_end && ! isspace (*string_end))
    {
      string_end++;
    }

    /* copy the operator string */
    strncpy (reduction_op, string_start, string_end - string_start);
    reduction_op[string_end - string_start] = 0;
    string_start = string_end;

    /* get the reduction handle from the reduction operator */
    reduction_handle = CCTK_ReductionHandle (reduction_op);
    if (reduction_handle < 0) 
    {
      CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "IOBasic_WriteGF: Invalid reduction operator '%s'", 
                  reduction_op);
      continue;
    }

    /* do the reduction (all processors) */
    ierr = CCTK_Reduce (GH, 0, reduction_handle, 1, CCTK_VARIABLE_REAL,
                        &reduction_value, 1, vindex);

    /* dump the reduction value to file by processor 0 */
    if (ierr == 0 && CCTK_MyProc (GH) == 0)
    {

      /* build the filename */
      if (new_filename_scheme)
      {
        /* skip the pathname if output goes into current directory */
        if (strcmp (myGH->outdirScalar, "."))
        {
          sprintf (filename, "%s/%s_%s%s", myGH->outdirScalar, alias,
                                           reduction_op, file_extension);
        }
        else
        {
          sprintf (filename, "%s_%s%s", alias, reduction_op, file_extension);
        }
      }
      else
      {
        /* FIXME: this check can go if we switch to the new filename scheme */
        if (strcmp (reduction_op, "minimum") == 0)
        {
          file_extension = "min";
        }
        else if (strcmp (reduction_op, "maximum") == 0)
        {
          file_extension = "max";
        }
        else if (strcmp (reduction_op, "norm1") == 0)
        {
          file_extension = "nm1";
        }
        else if (strcmp (reduction_op, "norm2") == 0)
        {
          file_extension = "nm2";
        }
        else
        {
          file_extension = "unknown";
        }
        /* skip the pathname if output goes into current directory */
        if (strcmp (myGH->outdirScalar, "."))
        {
          sprintf (filename, "%s/%s_%s.tl", myGH->outdirScalar, alias,
                                            file_extension);
        }
        else
        {
          sprintf (filename, "%s_%s.tl", alias, file_extension);
        }
      }

      /* see if output files for this alias name were already created */
      if (GetNamedData (myGH->filenameListScalar, filename) == NULL)
      {
        /* if restart from recovery, all existing files are opened
           in append mode */
        if (IOUtil_RestartFromRecovery (GH))
        {
          openmode = stat (filename, &fileinfo) == 0 ? "a" : "w";
        }
        else
        {
          openmode = "w";
        }
      }
      else
      {
        openmode = "a";
      }

      file = fopen (filename, openmode);
      if (file == NULL)
      {
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "IOBasic_WriteGF: Could not open output file '%s'", 
                    filename);
        continue;
      }

      /* when creating the file, advertise it for downloading,
         write the header and the file info,
         and save the filename in the database */
      if (*openmode == 'w')
      {
        /* advertise the file for downloading */
        advertised_file.slice       = reduction_op;
        advertised_file.thorn       = CCTK_THORNSTRING;
        advertised_file.varname     = CCTK_FullName (vindex);
        advertised_file.description = "Reduction on Grid Functions";
        advertised_file.mimetype    = CCTK_Equals (outScalar_style, "gnuplot") ?
                                  "application/gnuplot" : "application/x-graph";

        IOUtil_AdvertiseFile (GH, filename, &advertised_file);

        if (CCTK_Equals (out_fileinfo, "parameter filename") ||
            CCTK_Equals (out_fileinfo, "all"))
        {
          buffer[0] = 0;
          CCTK_ParameterFilename (sizeof (buffer), buffer);
          fprintf (file, "%cParameter file %s\n", comment_char, buffer);
        }
        if (CCTK_Equals (out_fileinfo, "creation date") ||
            CCTK_Equals (out_fileinfo, "all"))
        { 
          buffer[0] = 0;
          Util_CurrentDate (sizeof (buffer), buffer);
          fprintf (file, "%cCreated %s ", comment_char, buffer);
          Util_CurrentTime (sizeof (buffer), buffer);
          fprintf (file, "%s\n", buffer);
        }
        if (CCTK_Equals (out_fileinfo, "axis labels") ||
            CCTK_Equals (out_fileinfo, "all"))
        {
          fprintf (file, "%cx-label time\n", comment_char);
          fprintf (file, "%cy-label %s\n",
                   comment_char, advertised_file.varname);
        }
        fprintf (file, "%c%s %s v time\n", comment_char, alias,
                 reduction_op);

        free (advertised_file.varname);

        /* just store a non-NULL pointer in database */
        StoreNamedData (&myGH->filenameListScalar, filename, (void *) 1);
      }

      /* write the data and close the file */
      fprintf (file, format_str, GH->cctk_time, reduction_value);
      fclose (file);
    }
  }

  /* free allocated resources */
  free (reduction_op);
  free (filename);
}