aboutsummaryrefslogtreecommitdiff
path: root/CarpetDev/CarpetIOF5/src/iof5.hh
blob: 71524fa2c3047667871ba7409539682258787274 (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
#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 <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>



// This requires defining a boolean local variable "error_flag",
// initialised to false
#define FAILWARN(_expr)                                                 \
  failwarn(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)
{
  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 int const width = 3;
    static int level;
  public:
    indent_t() { ++level; }
    ~indent_t() { --level; }
    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;
  
  
  
  // 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;
  }
  
  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,
                       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);
  
  
  
  // 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
  string
  create_filename (cGH const *const cctkGH,
                   string const basename,
                   int const proc,
                   bool const create_directories);
  
  // Generate a good grid name (simulation name)
  string
  generate_gridname (cGH const *const cctkGH);
  
  // Generate a good tensor basis name (coordinate system name)
  string
  generate_chartname (cGH const *const cctkGH);
  
  // Generate a good fragment name (map name)
  string
  generate_fragmentname (cGH const *const cctkGH, int const m);
  int
  interpret_fragmentname (cGH const *const cctkGH,
                          char const *const fragmentname);
  
  // Generate a good topology name (map and refinement level name)
  string
  generate_topologyname (cGH const *const cctkGH,
                         int const m, ivect const& reffact);
  
  
  
  void
  write_metadata (cGH *const cctkGH, hid_t const file);
  
  // Handle Carpet's grid structure (this should move to Carpet and/or
  // CarpetLib)
  string
  serialise_grid_structure (cGH const *const cctkGH);
  
} // end namespace CarpetIOF5