Schnek
hdfdiagnostic.hpp
1 /*
2  * hdfdiagnostic.hpp
3  *
4  * Created on: 23 Oct 2012
5  * Author: hschmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #ifndef SCHNEK_HDFDIAGNOSTIC_HPP_
27 #define SCHNEK_HDFDIAGNOSTIC_HPP_
28 
29 #include "../config.hpp"
30 #ifdef SCHNEK_HAVE_HDF5
31 
32 #include "../grid/grid.hpp"
33 #include "diagnostic.hpp"
34 
35 #include <hdf5.h>
36 
37 #include <memory>
38 
39 #if defined (H5_HAVE_PARALLEL) && defined (SCHNEK_USE_HDF_PARALLEL)
40 #include <mpi.h>
41 #endif
42 
43 #include <map>
44 
45 namespace schnek {
46 
47 template<typename TYPE>
48 struct H5DataType{
49  static const hid_t type;
50 };
51 
52 
56 template<typename FieldType>
57 struct GridContainer
58 {
60  FieldType grid;
61 
63  typename FieldType::IndexType global_min;
64 
66  typename FieldType::IndexType global_max;
67 
69  typename FieldType::IndexType local_min;
70 
72  typename FieldType::IndexType local_max;
73 };
74 
80 struct HdfAttributes {
81  friend class HdfOStream;
82  private:
83  struct Info {
84  hid_t type;
85  hsize_t dims;
86  const void *buffer;
87  };
88  typedef std::shared_ptr<Info> pInfo;
89 
90  std::map<std::string, pInfo> attributes;
91  public:
92 
99  template<typename T>
100  void set(std::string name, const T *value, hsize_t dims = 1);
101 
108  template<typename T>
109  void set(std::string name, const T &value, hsize_t dims = 1);
110 };
111 
112 typedef std::shared_ptr<HdfAttributes> pHdfAttributes;
113 
119 class HdfStream {
120  protected:
122  hid_t file_id;
123 
125  herr_t status;
126 
128  std::string blockname;
129 
131  pHdfAttributes attributes;
132 
134  int sets_count;
135 
137  bool active;
138  bool activeModified;
139 
140  public:
142  HdfStream();
144  HdfStream(const HdfStream&);
146  virtual ~HdfStream();
147 
149  virtual int open(const char*)=0;
150 
152  virtual void close();
153 
155  virtual bool good() const;
156 
158  void setBlockName(std::string blockname_);
159 
161  void setAttributes(pHdfAttributes attributes_);
162 
164  HdfStream& operator = (const HdfStream&);
165 
166  void setActive(bool active_) { active = active_; activeModified = true; }
167 
168  protected:
169  std::string getNextBlockName();
170 
171 #if defined (H5_HAVE_PARALLEL) && defined (SCHNEK_USE_HDF_PARALLEL)
172  void makeMPIGroup();
173 
174  MPI_Comm mpiComm;
175  bool commSet;
176 #endif
177 
178 };
179 
180 
182 class HdfIStream : public HdfStream {
183  private:
184  hid_t dxpl_id;
185  public:
187  HdfIStream();
188 
190  HdfIStream(const HdfIStream&);
191 
193  HdfIStream(const char* fname);
194 
196  int open(const char*);
197 
199  template<typename FieldType>
200  void readGrid(GridContainer<FieldType> &g);
201 };
202 
203 
205 class HdfOStream : public HdfStream {
206  private:
207  hid_t dxpl_id;
208 #if defined (H5_HAVE_PARALLEL) && defined (SCHNEK_USE_HDF_PARALLEL)
209  MPI_Info mpi_info;
210 #endif
211  hid_t plist_id;
212  bool initialised;
213  public:
215  HdfOStream();
216 
218  HdfOStream(const HdfOStream&);
219 
221  HdfOStream(const char* fname);
222 
224  int open(const char*);
225 
227  template<typename FieldType>
228  void writeGrid(GridContainer<FieldType> &g);
229 };
233 template<typename Type, class DiagnosticType = IntervalDiagnostic >
234 class HDFGridDiagnostic : public SimpleDiagnostic<Type, Type, DiagnosticType> {
235  public:
236  typedef typename Type::IndexType IndexType;
237  protected:
238  HdfOStream output;
239  GridContainer<Type> container;
240  protected:
242  void open(const std::string &);
244  void write();
246  void close();
247 
249  void init();
251  virtual IndexType getGlobalMin() = 0;
253  virtual IndexType getGlobalMax() = 0;
254 
260  virtual std::string getDatasetName();
261 
269  virtual pHdfAttributes getAttributes() {
270  return std::make_shared<HdfAttributes>();
271  };
272  public:
273  virtual ~HDFGridDiagnostic() {}
274 };
275 
281 template<typename Type>
282 class HDFGridReader : public Block
283 {
284  public:
285  typedef typename Type::IndexType IndexType;
286  protected:
288  HdfIStream input;
290  GridContainer<Type> container;
292  Type field;
294  std::string fieldName;
296  std::string fileName;
297  public:
299  HDFGridReader();
301  virtual ~HDFGridReader() {}
302  protected:
304  void open();
305 
307  void read();
308 
310  void close();
311 
320  void execute();
321 
323  void init();
324 
326  virtual IndexType getGlobalMin() = 0;
327 
329  virtual IndexType getGlobalMax() = 0;
330 
336  virtual std::string getDatasetName();
337 
339  void initParameters(BlockParameters &blockPars);
340 };
341 
342 } // namespace schnek
343 
344 #include "hdfdiagnostic.t"
345 
346 #endif
347 
348 #endif // SCHNEK_HDFDIAGNOSTIC_HPP_
Definition: algo.hpp:30