aboutsummaryrefslogtreecommitdiff
path: root/src/File.c
blob: 01bef623edaeab2d05d9435cf09cc56d1b19dcac (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
#include "cctk.h"
#include "cctk_Parameters.h"
#include "StoreNamedData.h"
#include "CactusPUGH/PUGH/src/include/pugh.h"
#include "CactusBase/IOUtil/src/ioGH.h"
#include "ioHDF5GH.h"

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

/*@@
  @routine IOHDF5_Get_filename_ND
  @author  Paul Walker
  @date    Feb 1997
  @desc    Returns the filename for output
           The names of all output files are stored in the filenames database.
           The routine first searches in this database if there is already
           registered a filename for alias. If so, this name is returned.
           Otherwise it builds the filename from alias and stores it in the
           database.
           NOTE: Filenames are never stored if "out2D_septimefiles = true"
                 since they are used only once.
		 
	   Generalized for Hyperslab extraction (GL)
  @enddesc
  @var     GH
  @vdesc   Pointer to CCTK GH
  @vtype   cGH
  @vio     in
  @endvar
  @var     index
  @vdesc   index of variable to output
  @vtype   int
  @vio     in
  @endvar
  @var     name
  @vdesc   alias name of variable to output
  @vtype   const char *
  @vio     in
  @endvar
  @var     isNewFile
  @vdesc   flag indicating if the file is to be created
  @vtype   int
  @vio     out
  @endvar
@@*/

char *IOHDF5_Get_filename_ND (cGH *GH, int index, ioHDF5Geo_t *slab_geo,
			      const char *varname, int *isNewFile)
{
  DECLARE_CCTK_PARAMETERS
  ioGH *ioUtilGH;       /* handle for IOUtil extensions */
  ioHDF5GH *myGH;       /* handle for IOHDF5 extensions */
  pGH *pughGH;          /* handle for PUGH extensions */
  int myproc;
  char extra [256];     /* Extra stuff in fname based on mode */
  char extradir [256];  /* Extra stuff for an output dir */
  char *extrageo;       /* Extra stuff for geometry information */
  char *filename;       /* the return value */
  int idim;

  char name[128];

  /* get GH extensions for PUGH and IOUtil */
  pughGH   = PUGH_pGH (GH);
  ioUtilGH = (ioGH *) GH->extensions [CCTK_GHExtensionHandle ("IO")];
  myGH     = (ioHDF5GH *) GH->extensions [CCTK_GHExtensionHandle ("IOHDF5")];

  /* To identify the slab in a volume  (if sdim<vdim)
     build a string like: "012" (3d volume, spanned by xyz)
     or "12" (2d volume, spanned by yz(!) and append this to the name:
     You get something like this: phi_s12_3d */
  if (slab_geo->sdim<slab_geo->vdim) {
    extrageo = (char*) malloc((slab_geo->sdim+1)*sizeof(char));
    sprintf(extrageo,"%d",slab_geo->direction[0]);
    for (idim=1;idim<slab_geo->sdim;idim++)
      sprintf(extrageo,"%s%d",extrageo,slab_geo->direction[idim]);
    sprintf(name,"%s_s%s_%dd",varname,extrageo,slab_geo->vdim);
    free(extrageo);
  }
  /* If sdim==vdim, make it simple: e.g. phi_3d */
  else if (slab_geo->sdim==slab_geo->vdim)
  { 
    sprintf(name,"%s_%dd",varname,slab_geo->vdim);
  }
  else {
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
		"Inconsitent dimensions for >%s<: slab (%dD), variable (%dD)\n",
		varname, slab_geo->sdim,slab_geo->vdim);
    return(NULL);
  }

  filename = (char *) GetNamedData (myGH->filenameList, name);
  if (filename != NULL)
  {
    /* set flag to indicate that file should be opened in append mode */
    *isNewFile = 0;
    return (filename);
  }

  extra[0]   = '\0';
  extradir[0]= '\0';

  myproc = CCTK_MyProc (GH);

  if (out2D_septimefiles)
  {
    char *tmp = extra;

    sprintf (extra, "%s.time_%7.3f", extra, GH->cctk_time);

    /* And be sure to replace any spaces in the filename with an _ */
    do
      if (*tmp == ' ')
        *tmp = '_';
    while (*++tmp);
  }

  
   /* OUTPUT ONE FILE FOR EACH N PROCESSORS FIXME
   *  ------------------------------------- 
   *  
   *  If only one output file, the single file for each variable is written 
   *  in the normal output dir (that is extradir = ""). Otherwise
   *  a directory is created for each output variable to hold the multiple
   *  files.
   */

  if (! ioUtilGH->unchunked && ioUtilGH->ioproc_every < CCTK_nProcs (GH))
  {
    /* Add the output processor number to the extra string */
    sprintf (extra, "%s.file_%d", extra, myproc / ioUtilGH->ioproc_every);
    
    /* If necessary create the output directory */
    if (myproc == 0)
    {
      int result;
      char *outputdir = (char *) malloc (strlen (myGH->outdir3D) +
                                         strlen (name) + 5);

      sprintf (outputdir, "%s/%s_3d", myGH->outdir3D, name);
      
      result = CCTK_CreateDirectory (0755, outputdir);
      if (result < 0)
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Problem creating 3D output directory '%s'", outputdir);
      if (result > 0)
        CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "3D output directory '%s' already exists", outputdir);

      free (outputdir);
    }
    
#ifdef CCTK_MPI
    /* Wait for all processors to catch up */
    CCTK_Barrier (GH);
#endif
    
    /* extradir is the relative output directory */
    sprintf (extradir, "%s_3d/", name);
    
  }
  
  /* CREATE THE COMPLETE OUTPUT FILENAME 
     ----------------------------------- */
  filename = (char *) malloc (strlen (myGH->outdir2D) +
                              strlen (extradir) +
                              strlen (name) +
                              strlen (extra) +
                              (pughGH->identity_string ? strlen (pughGH->identity_string) : 0) +
                              8);
  sprintf (filename, "%s/%s%s%s%s.h5", 
	   myGH->outdir2D, extradir, name, extra, (pughGH->identity_string ? pughGH->identity_string : ""));

  /* no need to store filenames if used only once */
  if (! out2D_septimefiles)
  {
    if (myproc == ioUtilGH->ioproc)
    {
      if (ioUtilGH->recovered)
      {
        CACTUS_IOHDF5_ERROR (H5Eset_auto (NULL, NULL));
        *isNewFile = H5Fis_hdf5 (filename) <= 0;
        H5Eset_auto (myGH->printErrorFn, myGH->printErrorFnArg);
        if (! *isNewFile)
          myGH->checkForExistingObjects [index] = 1;
      }
      else
      {
        *isNewFile = 1;
      }
    }
    StoreNamedData (&myGH->filenameList, name, filename);
  }
  else
  {
    *isNewFile = 1;
  }

  return (filename);
}