Nektar++
FieldIOHdf5.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File FieldIOHdf5.h
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Field IO to/from HDF5
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_FIELDIOHDF5_H
36 #define NEKTAR_LIB_UTILITIES_BASIC_UTILS_FIELDIOHDF5_H
37 
41 
42 namespace Nektar
43 {
44 namespace LibUtilities
45 {
46 
47 namespace H5
48 {
49 class Group;
50 typedef std::shared_ptr<Group> GroupSharedPtr;
51 }
52 
53 /**
54  * @class Class encapsulating simple HDF5 data source using H5 reader utilities.
55  */
56 class H5DataSource : public DataSource
57 {
58 public:
59  /// Constructor based on filename.
60  H5DataSource(const std::string &fn, H5::PListSharedPtr parallelProps)
61  : doc(H5::File::Open(fn, H5F_ACC_RDONLY, parallelProps))
62  {
63  }
64 
65  /// Get H5::FileSharedPtr reference to file.
67  {
68  return doc;
69  }
70 
71  /// Get H5::FileSharedPtr reference to file.
72  const H5::FileSharedPtr Get() const
73  {
74  return doc;
75  }
76 
77  /// Static constructor for this data source.
79  const std::string &fn, H5::PListSharedPtr parallelProps)
80  {
81  return DataSourceSharedPtr(new H5DataSource(fn, parallelProps));
82  }
83 
84 private:
85  /// HDF5 document.
87 };
88 typedef std::shared_ptr<H5DataSource> H5DataSourceSharedPtr;
89 
90 /**
91  * @class Simple class for writing hierarchical data using HDF5.
92  */
93 class H5TagWriter : public TagWriter
94 {
95 public:
96  /// Default constructor.
98 
99  /// Add a child node.
100  TagWriterSharedPtr AddChild(const std::string &name)
101  {
102  H5::GroupSharedPtr child = m_Group->CreateGroup(name);
103  return TagWriterSharedPtr(new H5TagWriter(child));
104  }
105 
106  /// Set an attribute key/value pair on this tag.
107  void SetAttr(const std::string &key, const std::string &val)
108  {
109  m_Group->SetAttribute(key, val);
110  }
111 
112 private:
113  /// HDF5 group for this tag.
115 };
116 typedef std::shared_ptr<H5TagWriter> H5TagWriterSharedPtr;
117 
118 /**
119  * @class Class for operating on HDF5-based FLD files.
120  *
121  * This class implements a HDF5 reader/writer based on MPI/O that is designed to
122  * operate on a single file across all processors of a simulation. The
123  * definition follows vaguely similar lines to XML output but is stored somewhat
124  * differently to accommodate parallel reading and writing. At a basic level
125  * metadata is organised as follows:
126  *
127  * - Nektar++ data lies in the root `/NEKTAR` group.
128  * - The contents of a FieldDefinitions object is hashed to construct a unique
129  * identifier for each object, which becomes the name of a group within the
130  * root group. We then use the H5TagWriter to assign the field definitions
131  * to each group.
132  * - In a similar fashion, we create a `Metadata` group to contain field
133  * metadata that is written.
134  *
135  * We then define five data sets to contain field data:
136  *
137  * - The `DATA` dataset contains the double-precision modal coefficient data.
138  * - The `IDS` dataset contains the element IDs of the elements that are
139  * written out.
140  * - The `POLYORDERS` dataset is written if the field data contains variable
141  * polynomial order, and contains the (possibly hetergeneous) mode orders in
142  * each direction for each of the elements.
143  * - The `HOMOGENEOUSZIDS` dataset contains the IDs of z-planes for
144  * homogeneous simulations, if the data are homogeneous.
145  * - The `HOMOGENEOUSYIDS` dataset contains the IDs of y-planes for
146  * homogeneous simulations, if the data are homogeneous.
147  * - The `HOMOGENEOUSSIDS` dataset contains the strip IDs for
148  * homogeneous simulations, if the data are homogeneous and use strips.
149  *
150  * The ordering is defined according to the `DECOMPOSITION` dataset. A
151  * `decomposition' in this class is essentially a single field definition with
152  * its accompanying data. Data are written into each dataset by the order of
153  * each decomposition. Each decomposition contains the following seven integers
154  * that define it per field definition per processor:
155  *
156  * - Number of elements in this field definition (index #ELEM_DCMP_IDX).
157  * - Number of entries in the `DATA` array for this field definition
158  * (index #VAL_DCMP_IDX)
159  * - Number of entries in the `POLYORDERS` array for this field definition
160  * (index #ORDER_DCMP_IDX)
161  * - Number of entries in the `HOMOGENEOUSZIDS` array (index #HOMZ_DCMP_IDX).
162  * - Number of entries in the `HOMOGENEOUSYIDS` array (index #HOMY_DCMP_IDX).
163  * - Number of entries in the `HOMOGENEOUSSIDS` array (index #HOMS_DCMP_IDX).
164  * - Hash of the field definition, represented as a 32-bit integer, which
165  * describes the name of the attribute that contains the rest of the field
166  * definition information (e.g. field names, basis type, etc).
167  *
168  * The number of decompositions is therefore calculated as the field size
169  * divided by #MAX_DCMPS which allows us to calculate the offsets of the data
170  * for each field definition within the arrays.
171  */
172 class FieldIOHdf5 : public FieldIO
173 {
174 public:
175  static const unsigned int FORMAT_VERSION;
176 
177  static const unsigned int ELEM_DCMP_IDX;
178  static const unsigned int VAL_DCMP_IDX;
179  static const unsigned int ORDER_DCMP_IDX;
180  static const unsigned int HOMY_DCMP_IDX;
181  static const unsigned int HOMZ_DCMP_IDX;
182  static const unsigned int HOMS_DCMP_IDX;
183  static const unsigned int HASH_DCMP_IDX;
184  static const unsigned int MAX_DCMPS;
185 
186  static const unsigned int ELEM_CNT_IDX;
187  static const unsigned int VAL_CNT_IDX;
188  static const unsigned int ORDER_CNT_IDX;
189  static const unsigned int HOMY_CNT_IDX;
190  static const unsigned int HOMZ_CNT_IDX;
191  static const unsigned int HOMS_CNT_IDX;
192  static const unsigned int MAX_CNTS;
193 
194  static const unsigned int IDS_IDX_IDX;
195  static const unsigned int DATA_IDX_IDX;
196  static const unsigned int ORDER_IDX_IDX;
197  static const unsigned int HOMY_IDX_IDX;
198  static const unsigned int HOMZ_IDX_IDX;
199  static const unsigned int HOMS_IDX_IDX;
200  static const unsigned int MAX_IDXS;
201 
202  /// Creates an instance of this class
204  LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
205  {
207  sharedFilesystem);
208  }
209 
210  /// Name of class
211  LIB_UTILITIES_EXPORT static std::string className;
212 
215  bool sharedFilesystem);
216 
218  {
219  }
220 
221  /// Get class name
222  inline virtual const std::string &GetClassName() const
223  {
224  return className;
225  }
226 
227 private:
228  struct OffsetHelper {
229  OffsetHelper() : data(0), order(0), homy(0), homz(0), homs(0) {}
231  data(in.data), order(in.order), homy(in.homy), homz(in.homz),
232  homs(in.homs)
233  {
234  }
235  OffsetHelper& operator=(const OffsetHelper &in) = default;
236 
237  uint64_t data, order, homy, homz, homs;
238  };
239 
240  LIB_UTILITIES_EXPORT virtual void v_Write(
241  const std::string &outFile,
242  std::vector<FieldDefinitionsSharedPtr> &fielddefs,
243  std::vector<std::vector<NekDouble> > &fielddata,
244  const FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
245  const bool backup = false);
246 
247  LIB_UTILITIES_EXPORT virtual void v_Import(
248  const std::string &infilename,
249  std::vector<FieldDefinitionsSharedPtr> &fielddefs,
250  std::vector<std::vector<NekDouble> > &fielddata =
252  FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
253  const Array<OneD, int> &ElementIDs = NullInt1DArray);
254 
256  const std::string &filename, FieldMetaDataMap &fieldmetadatamap);
257 
259  DataSourceSharedPtr dataSource, FieldMetaDataMap &fieldmetadatamap);
260 
262  H5::PListSharedPtr readPL,
263  H5::GroupSharedPtr root,
264  std::vector<uint64_t> &decomps,
265  uint64_t decomp,
266  OffsetHelper offset,
267  std::string group,
269 
271  H5::PListSharedPtr readPL,
272  H5::DataSetSharedPtr data_dset,
273  H5::DataSpaceSharedPtr data_fspace,
274  uint64_t data_i,
275  std::vector<uint64_t> &decomps,
276  uint64_t decomp,
277  const FieldDefinitionsSharedPtr fielddef,
278  std::vector<NekDouble> &fielddata);
279 };
280 }
281 }
282 #endif
#define LIB_UTILITIES_EXPORT
virtual const std::string & GetClassName() const
Get class name.
Definition: FieldIOHdf5.h:222
void ImportFieldData(H5::PListSharedPtr readPL, H5::DataSetSharedPtr data_dset, H5::DataSpaceSharedPtr data_fspace, uint64_t data_i, std::vector< uint64_t > &decomps, uint64_t decomp, const FieldDefinitionsSharedPtr fielddef, std::vector< NekDouble > &fielddata)
Import the field data from the HDF5 document.
static const unsigned int ELEM_CNT_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the number of elements in the cnt array.
Definition: FieldIOHdf5.h:186
static FieldIOSharedPtr create(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Creates an instance of this class.
Definition: FieldIOHdf5.h:203
static const unsigned int MAX_CNTS
A helper for FieldIOHdf5::v_Write. Describes the maximum number of items in the cnt array per field d...
Definition: FieldIOHdf5.h:192
static const unsigned int HOMY_CNT_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the number of homogeneous y-planes in th...
Definition: FieldIOHdf5.h:189
static std::string className
Name of class.
Definition: FieldIOHdf5.h:211
virtual void v_Write(const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap, const bool backup=false)
Write a HDF5 file to outFile given the field definitions fielddefs, field data fielddata and metadata...
static const unsigned int ORDER_DCMP_IDX
A helper for FieldIOHdf5::v_Write and FieldIOHdf5::v_Import. Describes the position of the number of ...
Definition: FieldIOHdf5.h:179
static const unsigned int HOMZ_CNT_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the number of homogeneous z-planes in th...
Definition: FieldIOHdf5.h:190
static const unsigned int FORMAT_VERSION
Version of the Nektar++ HDF5 format, which is embedded into the main NEKTAR group as an attribute.
Definition: FieldIOHdf5.h:175
static const unsigned int HASH_DCMP_IDX
The hash of the field definition information, which defines the name of the attribute containing the ...
Definition: FieldIOHdf5.h:183
static const unsigned int HOMS_CNT_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the number of homogeneous strips in the ...
Definition: FieldIOHdf5.h:191
static const unsigned int VAL_CNT_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the number of data points in the cnt arr...
Definition: FieldIOHdf5.h:187
virtual DataSourceSharedPtr v_ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap)
Import field metadata from filename and return the data source which wraps filename.
static const unsigned int IDS_IDX_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the element IDs within the indexing set.
Definition: FieldIOHdf5.h:194
static const unsigned int HOMS_DCMP_IDX
A helper for FieldIOHdf5::v_Write and FieldIOHdf5::v_Import. Describes the position of the number of ...
Definition: FieldIOHdf5.h:182
static const unsigned int ELEM_DCMP_IDX
A helper for FieldIOHdf5::v_Write and FieldIOHdf5::v_Import. Describes the position of the number of ...
Definition: FieldIOHdf5.h:177
static const unsigned int ORDER_CNT_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the number of order points in the cnt ar...
Definition: FieldIOHdf5.h:188
void ImportFieldDef(H5::PListSharedPtr readPL, H5::GroupSharedPtr root, std::vector< uint64_t > &decomps, uint64_t decomp, OffsetHelper offset, std::string group, FieldDefinitionsSharedPtr def)
Import field definitions from a HDF5 document.
static const unsigned int ORDER_IDX_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the element order within the indexing se...
Definition: FieldIOHdf5.h:196
static const unsigned int HOMZ_DCMP_IDX
A helper for FieldIOHdf5::v_Write and FieldIOHdf5::v_Import. Describes the position of the number of ...
Definition: FieldIOHdf5.h:181
static const unsigned int MAX_DCMPS
A helper for FieldIOHdf5::v_Write. Describes the maximum number of items in the decomposition per fie...
Definition: FieldIOHdf5.h:184
FieldIOHdf5(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Construct the FieldIO object for HDF5 output.
static const unsigned int MAX_IDXS
A helper for FieldIOHdf5::v_Write. Describes the maximum number of items in the indexing set.
Definition: FieldIOHdf5.h:200
static const unsigned int VAL_DCMP_IDX
A helper for FieldIOHdf5::v_Write and FieldIOHdf5::v_Import. Describes the position of the number of ...
Definition: FieldIOHdf5.h:178
static const unsigned int DATA_IDX_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the data size within the indexing set.
Definition: FieldIOHdf5.h:195
void ImportHDF5FieldMetaData(DataSourceSharedPtr dataSource, FieldMetaDataMap &fieldmetadatamap)
Import field metadata from dataSource.
static const unsigned int HOMS_IDX_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the number of homogeneous strips within ...
Definition: FieldIOHdf5.h:199
virtual void v_Import(const std::string &infilename, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata=NullVectorNekDoubleVector, FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap, const Array< OneD, int > &ElementIDs=NullInt1DArray)
Import a HDF5 format file.
static const unsigned int HOMY_DCMP_IDX
A helper for FieldIOHdf5::v_Write and FieldIOHdf5::v_Import. Describes the position of the number of ...
Definition: FieldIOHdf5.h:180
static const unsigned int HOMZ_IDX_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the number of z-planes within the indexi...
Definition: FieldIOHdf5.h:198
static const unsigned int HOMY_IDX_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the number of y-planes within the indexi...
Definition: FieldIOHdf5.h:197
Class for operating on Nektar++ input/output files.
Definition: FieldIO.h:223
H5DataSource(const std::string &fn, H5::PListSharedPtr parallelProps)
Constructor based on filename.
Definition: FieldIOHdf5.h:60
const H5::FileSharedPtr Get() const
Get H5::FileSharedPtr reference to file.
Definition: FieldIOHdf5.h:72
H5::FileSharedPtr Get()
Get H5::FileSharedPtr reference to file.
Definition: FieldIOHdf5.h:66
H5::FileSharedPtr doc
HDF5 document.
Definition: FieldIOHdf5.h:86
static DataSourceSharedPtr create(const std::string &fn, H5::PListSharedPtr parallelProps)
Static constructor for this data source.
Definition: FieldIOHdf5.h:78
TagWriterSharedPtr AddChild(const std::string &name)
Add a child node.
Definition: FieldIOHdf5.h:100
void SetAttr(const std::string &key, const std::string &val)
Set an attribute key/value pair on this tag.
Definition: FieldIOHdf5.h:107
H5::GroupSharedPtr m_Group
HDF5 group for this tag.
Definition: FieldIOHdf5.h:114
H5TagWriter(H5::GroupSharedPtr grp)
Default constructor.
Definition: FieldIOHdf5.h:97
Base class for writing hierarchical data (XML or HDF5).
Definition: FieldIO.h:59
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
std::shared_ptr< DataSpace > DataSpaceSharedPtr
Definition: H5.h:90
std::shared_ptr< PList > PListSharedPtr
Definition: H5.h:104
std::shared_ptr< File > FileSharedPtr
Definition: H5.h:100
std::shared_ptr< Group > GroupSharedPtr
Definition: FieldIOHdf5.h:49
std::shared_ptr< DataSet > DataSetSharedPtr
Definition: H5.h:102
std::shared_ptr< H5TagWriter > H5TagWriterSharedPtr
Definition: FieldIOHdf5.h:116
std::shared_ptr< TagWriter > TagWriterSharedPtr
Definition: FieldIO.h:69
std::shared_ptr< H5DataSource > H5DataSourceSharedPtr
Definition: FieldIOHdf5.h:88
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:306
std::shared_ptr< DataSource > DataSourceSharedPtr
Definition: FieldIO.h:79
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:52
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:53
static std::vector< std::vector< NekDouble > > NullVectorNekDoubleVector
std::shared_ptr< FieldDefinitions > FieldDefinitionsSharedPtr
Definition: FieldIO.h:179
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:54
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
static Array< OneD, int > NullInt1DArray
OffsetHelper & operator=(const OffsetHelper &in)=default