aboutsummaryrefslogtreecommitdiff
path: root/CarpetDev/CarpetIOF5/src/iof5.hh
blob: 7202140ba68f9dab6a31ece71230f0346d8ed703 (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
#include <cctk.h>
#include <cctk_Arguments.h>
#include <cctk_Parameters.h>

#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>

#include <hdf5.h>
#include <F5/F5F.h>
#include <F5/F5R.h>
#include <F5/F5iterate.h>
#include <F5/F5uniform.h>

#include <bbox.hh>
#include <defs.hh>
#include <vect.hh>

#include <carpet.hh>

#include "distribute.hh"



// This requires defining a boolean local variable "error_flag",
// initialised to false
#define FAILWARN(_expr)                                                 \
  failwarn(error_flag, _expr, __LINE__, __FILE__, CCTK_THORNSTRING, #_expr)
#define FAILWARN0(_expr)                                                \
  failwarn0(error_flag, _expr, __LINE__, __FILE__, CCTK_THORNSTRING, #_expr)

template<typename T>
static
T failwarn(bool& error_flag, T const expr,
           int const line, char const *const file, char const *const thorn,
           char const *const msg)
{
  static_assert(T(-1) < T(0), "Type T must be signed");
  if (expr < 0) {
    CCTK_VWarn(CCTK_WARN_ALERT, line, file, thorn,
               "Expression \"%s\" return %d", msg, (int)expr);
    error_flag = true;
  }
  return expr;
}

template<typename T>
static
T failwarn0(bool& error_flag, T const expr,
            int const line, char const *const file, char const *const thorn,
            char const *const msg)
{
  static_assert(T(-1) > T(0), "Type T must be unsigned");
  if (expr == 0) {
    CCTK_VWarn(CCTK_WARN_ALERT, line, file, thorn,
               "Expression \"%s\" return %d", msg, (int)expr);
    error_flag = true;
  }
  return expr;
}

  

namespace CarpetIOF5 {
  class indent_t;
};
ostream& operator<<(ostream& os, CarpetIOF5::indent_t const& indent);



namespace CarpetIOF5 {
  
  using namespace std;
  using namespace Carpet;
  
  
  
  // Indentation
  class indent_t {
    static bool const debug = false;
    static int const width = 3;
    static int level;
  private:
    indent_t(indent_t const&);
    indent_t& operator=(indent_t const&);
  public:
    indent_t();
    ~indent_t();
    ostream& output(ostream& os) const;
  };
  
  
  
  // File mode for creating directories
  int const mode = 0755;
  
  // A nan
  CCTK_REAL const nan = numeric_limits<CCTK_REAL>::quiet_NaN();
  
  // Special group and attribute names
  char const *const metadata_group = "Parameters and Global Attributes";
  char const *const all_parameters = "All Parameters";
  extern char const *const grid_structure;
  
  
  
  // Tensor types
  enum tensortype_t {
    tt_scalar, tt_vector, tt_symtensor, tt_tensor, tt_error
  };
  
  
  
  // Data types for HDF5 and F5 types
  typedef vect<hsize_t,dim> hvect;
  
  // Conversion operators for these datatypes
  static inline
  hvect v2h(ivect const& a)
  {
    return hvect(a);
  }
  static inline
  ivect h2v(hvect const& a)
  {
    return ivect(a);
  }
  static inline
  ivect h2v(hsize_t const *const a)
  {
    ivect res;
    for (int d=0; d<dim; ++d) {
      res[d] = a[d];
    }
    return res;
  }
  
  template<typename T, int D>
  static inline
  vect<T,D> filter(vect<T,D> const& a, vect<bool,D> const& c,
                   T const& fill = T(0))
  {
    vect<T,D> r(fill);
    int ri=0;
    for (int ai=0; ai<dim; ++ai) {
      if (c[ai]) r[ri++] = a[ai];
    }
    return r;
  }
  
  static inline
  F5_vec3_point_t v2p(rvect const& a)
  {
    F5_vec3_point_t res;
    res.x = a[0];
    res.y = a[1];
    res.z = a[2];
    return res;
  }
  
  static inline
  F5_vec3_float_t v2f(rvect const& a)
  {
    F5_vec3_float_t res;
    res.x = a[0];
    res.y = a[1];
    res.z = a[2];
    return res;
  }
  
  static inline
  F5_vec3_double_t v2d(rvect const& a)
  {
    F5_vec3_double_t res;
    res.x = a[0];
    res.y = a[1];
    res.z = a[2];
    return res;
  }
  
  
  
  // Handle HDF5 attributes more comfortably
  bool WriteAttribute(hid_t const group,
                      char const * const name,
                      int const ivalue);
  bool WriteAttribute(hid_t const group,
                      char const * const name,
                      double const dvalue);
  bool WriteAttribute(hid_t const group,
                      char const *const name,
                      char const *const svalue);
  bool WriteAttribute(hid_t const group,
                      char const *const name,
                      string const svalue);
  bool WriteAttribute(hid_t const group,
                      char const *const name,
                      int const *const ivalues,
                      int const nvalues);
  bool WriteAttribute(hid_t const group,
                      char const *const name,
                      double const *const dvalues,
                      int const nvalues);
  bool WriteAttribute(hid_t const group,
                      char const *const name,
                      char const *const *const svalues,
                      int const nvalues);
  bool WriteLargeAttribute(hid_t const group,
                           char const *const name,
                           char const *const svalue);
  
  bool ReadAttribute(hid_t const group,
                     char const * const name,
                     int& ivalue);
  bool ReadAttribute(hid_t const group,
                     char const * const name,
                     double& dvalue);
  bool ReadAttribute(hid_t const group,
                     char const *const name,
                     string& svalue);
  bool ReadLargeAttribute(hid_t const group,
                          char const *const name,
                          string& svalue);
  
  
  
  // Generate a good file name ("alias") for a variable
  string
  generate_basename(cGH const *const cctkGH,
                    int const variable);
  
  // Create the final file name on a particular processor
  enum io_dir_t
    {io_dir_input, io_dir_output, io_dir_recover, io_dir_checkpoint};
  string
  create_filename(cGH const *const cctkGH,
                  string const basename,
                  int const iteration,
                  int const proc,
                  io_dir_t const io_dir,
                  bool const create_directories);
  
  // Generate a good grid name (simulation name)
  string
  generate_gridname(cGH const *const cctkGH);
  
  // Generate a good topology name (refinement level name)
  string
  generate_topologyname(cGH const *const cctkGH,
                        int const gi,
                        ivect const& reffact,
                        ivect const& slice_ipos);
  
  // Generate a good chart name (tensor basis name)
  string
  generate_chartname(cGH const *const cctkGH);
  
  // Generate a good fragment name (map and component name)
  string
  generate_fragmentname(cGH const *const cctkGH, int const m, int const c);
  void
  interpret_fragmentname(cGH const *const cctkGH,
                         char const *const fragmentname,
                         int& m, int& c);
  
  // Generate a good field name (group or variable name)
  string
  generate_fieldname(cGH const *const cctkGH,
                     int const vi, tensortype_t const tt);
  void
  interpret_fieldname(cGH const *const cctkGH, string fieldname, int& vi);
  
  
  
  // Write/read Cactus metadata to a particular location in an HDF5
  // file
  void
  write_metadata(cGH const *const cctkGH, hid_t const group);
  void
  read_metadata(cGH const *const cctkGH, hid_t const group);
  
  // Handle Carpet's grid structure (this should move to Carpet and/or
  // CarpetLib)
  string
  serialise_grid_structure(cGH const *const cctkGH);
  void
  deserialise_grid_structure(cGH const *const cctkGH, string const buf);
  
  
  
  void output(cGH const *const cctkGH,
              hid_t const file,
              vector<bool> const& output_var,
              bool const output_past_timelevels,
              bool const output_metadata);
  
  void input(cGH const *const cctkGH,
             hid_t const file,
             vector<bool> const& input_var,
             bool const input_past_timelevels,
             bool const input_metadata,
             scatter_t& scatter);
  
  
  
  // Scheduled routines
  extern "C" {
    int CarpetIOF5_Startup();
    int CarpetIOF5_RecoverParameters();
    void CarpetIOF5_Init(CCTK_ARGUMENTS);
    void CarpetIOF5_InitialDataCheckpoint(CCTK_ARGUMENTS);
    void CarpetIOF5_EvolutionCheckpoint(CCTK_ARGUMENTS);
    void CarpetIOF5_TerminationCheckpoint(CCTK_ARGUMENTS);
  }
  
  // Registered GH extension setup routine
  void *SetupGH(tFleshConfig *const fleshconfig,
                int const convLevel, cGH *const cctkGH);
  
  // Callbacks for CarpetIOF5's I/O method
  int Input(cGH *const cctkGH,
            char const *const basefilename, int const called_from);
  int OutputGH(cGH const *const cctkGH);
  int TimeToOutput(cGH const *const cctkGH, int const vindex);
  int TriggerOutput(cGH const *const cctkGH, int const vindex);
  int OutputVarAs(cGH const *const cctkGH,
                  const char *const varname, const char *const alias);
  
} // end namespace CarpetIOF5