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 } // namespace H5
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.
78  static DataSourceSharedPtr create(const std::string &fn,
79  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  }
100 
101  /// Add a child node.
102  TagWriterSharedPtr AddChild(const std::string &name)
103  {
104  H5::GroupSharedPtr child = m_Group->CreateGroup(name);
105  return TagWriterSharedPtr(new H5TagWriter(child));
106  }
107 
108  /// Set an attribute key/value pair on this tag.
109  void SetAttr(const std::string &key, const std::string &val)
110  {
111  m_Group->SetAttribute(key, val);
112  }
113 
114 private:
115  /// HDF5 group for this tag.
117 };
118 typedef std::shared_ptr<H5TagWriter> H5TagWriterSharedPtr;
119 
120 /**
121  * @class Class for operating on HDF5-based FLD files.
122  *
123  * This class implements a HDF5 reader/writer based on MPI/O that is designed to
124  * operate on a single file across all processors of a simulation. The
125  * definition follows vaguely similar lines to XML output but is stored somewhat
126  * differently to accommodate parallel reading and writing. At a basic level
127  * metadata is organised as follows:
128  *
129  * - Nektar++ data lies in the root `/NEKTAR` group.
130  * - The contents of a FieldDefinitions object is hashed to construct a unique
131  * identifier for each object, which becomes the name of a group within the
132  * root group. We then use the H5TagWriter to assign the field definitions
133  * to each group.
134  * - In a similar fashion, we create a `Metadata` group to contain field
135  * metadata that is written.
136  *
137  * We then define five data sets to contain field data:
138  *
139  * - The `DATA` dataset contains the double-precision modal coefficient data.
140  * - The `IDS` dataset contains the element IDs of the elements that are
141  * written out.
142  * - The `POLYORDERS` dataset is written if the field data contains variable
143  * polynomial order, and contains the (possibly hetergeneous) mode orders in
144  * each direction for each of the elements.
145  * - The `HOMOGENEOUSZIDS` dataset contains the IDs of z-planes for
146  * homogeneous simulations, if the data are homogeneous.
147  * - The `HOMOGENEOUSYIDS` dataset contains the IDs of y-planes for
148  * homogeneous simulations, if the data are homogeneous.
149  * - The `HOMOGENEOUSSIDS` dataset contains the strip IDs for
150  * homogeneous simulations, if the data are homogeneous and use strips.
151  *
152  * The ordering is defined according to the `DECOMPOSITION` dataset. A
153  * `decomposition' in this class is essentially a single field definition with
154  * its accompanying data. Data are written into each dataset by the order of
155  * each decomposition. Each decomposition contains the following seven integers
156  * that define it per field definition per processor:
157  *
158  * - Number of elements in this field definition (index #ELEM_DCMP_IDX).
159  * - Number of entries in the `DATA` array for this field definition
160  * (index #VAL_DCMP_IDX)
161  * - Number of entries in the `POLYORDERS` array for this field definition
162  * (index #ORDER_DCMP_IDX)
163  * - Number of entries in the `HOMOGENEOUSZIDS` array (index #HOMZ_DCMP_IDX).
164  * - Number of entries in the `HOMOGENEOUSYIDS` array (index #HOMY_DCMP_IDX).
165  * - Number of entries in the `HOMOGENEOUSSIDS` array (index #HOMS_DCMP_IDX).
166  * - Hash of the field definition, represented as a 32-bit integer, which
167  * describes the name of the attribute that contains the rest of the field
168  * definition information (e.g. field names, basis type, etc).
169  *
170  * The number of decompositions is therefore calculated as the field size
171  * divided by #MAX_DCMPS which allows us to calculate the offsets of the data
172  * for each field definition within the arrays.
173  */
174 class FieldIOHdf5 : public FieldIO
175 {
176 public:
177  static const unsigned int FORMAT_VERSION;
178 
179  static const unsigned int ELEM_DCMP_IDX;
180  static const unsigned int VAL_DCMP_IDX;
181  static const unsigned int ORDER_DCMP_IDX;
182  static const unsigned int HOMY_DCMP_IDX;
183  static const unsigned int HOMZ_DCMP_IDX;
184  static const unsigned int HOMS_DCMP_IDX;
185  static const unsigned int HASH_DCMP_IDX;
186  static const unsigned int MAX_DCMPS;
187 
188  static const unsigned int ELEM_CNT_IDX;
189  static const unsigned int VAL_CNT_IDX;
190  static const unsigned int ORDER_CNT_IDX;
191  static const unsigned int HOMY_CNT_IDX;
192  static const unsigned int HOMZ_CNT_IDX;
193  static const unsigned int HOMS_CNT_IDX;
194  static const unsigned int MAX_CNTS;
195 
196  static const unsigned int IDS_IDX_IDX;
197  static const unsigned int DATA_IDX_IDX;
198  static const unsigned int ORDER_IDX_IDX;
199  static const unsigned int HOMY_IDX_IDX;
200  static const unsigned int HOMZ_IDX_IDX;
201  static const unsigned int HOMS_IDX_IDX;
202  static const unsigned int MAX_IDXS;
203 
204  /// Creates an instance of this class
206  LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
207  {
209  sharedFilesystem);
210  }
211 
212  /// Name of class
213  LIB_UTILITIES_EXPORT static std::string className;
214 
216  bool sharedFilesystem);
217 
219  {
220  }
221 
222  /// Get class name
223  inline virtual const std::string &GetClassName() const
224  {
225  return className;
226  }
227 
228 private:
230  {
231  OffsetHelper() : data(0), order(0), homy(0), homz(0), homs(0)
232  {
233  }
235  : data(in.data), order(in.order), homy(in.homy), homz(in.homz),
236  homs(in.homs)
237  {
238  }
239  OffsetHelper &operator=(const OffsetHelper &in) = default;
240 
241  uint64_t data, order, homy, homz, homs;
242  };
243 
244  LIB_UTILITIES_EXPORT virtual void v_Write(
245  const std::string &outFile,
246  std::vector<FieldDefinitionsSharedPtr> &fielddefs,
247  std::vector<std::vector<NekDouble>> &fielddata,
248  const FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
249  const bool backup = false);
250 
251  LIB_UTILITIES_EXPORT virtual void v_Import(
252  const std::string &infilename,
253  std::vector<FieldDefinitionsSharedPtr> &fielddefs,
254  std::vector<std::vector<NekDouble>> &fielddata =
256  FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
257  const Array<OneD, int> &ElementIDs = NullInt1DArray);
258 
260  const std::string &filename, FieldMetaDataMap &fieldmetadatamap);
261 
263  DataSourceSharedPtr dataSource, FieldMetaDataMap &fieldmetadatamap);
264 
267  std::vector<uint64_t> &decomps, uint64_t decomp, OffsetHelper offset,
268  std::string group, FieldDefinitionsSharedPtr def);
269 
271  H5::PListSharedPtr readPL, H5::DataSetSharedPtr data_dset,
272  H5::DataSpaceSharedPtr data_fspace, uint64_t data_i,
273  std::vector<uint64_t> &decomps, uint64_t decomp,
274  const FieldDefinitionsSharedPtr fielddef,
275  std::vector<NekDouble> &fielddata);
276 };
277 } // namespace LibUtilities
278 } // namespace Nektar
279 #endif
#define LIB_UTILITIES_EXPORT
virtual const std::string & GetClassName() const
Get class name.
Definition: FieldIOHdf5.h:223
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:188
static FieldIOSharedPtr create(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Creates an instance of this class.
Definition: FieldIOHdf5.h:205
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:194
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_CNT_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the number of homogeneous y-planes in th...
Definition: FieldIOHdf5.h:191
static std::string className
Name of class.
Definition: FieldIOHdf5.h:213
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:181
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:192
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:177
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:185
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:193
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:189
virtual DataSourceSharedPtr v_ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap)
Import field metadata from filename and return the data source which wraps filename.
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 IDS_IDX_IDX
A helper for FieldIOHdf5::v_Write. Describes the position of the element IDs within the indexing set.
Definition: FieldIOHdf5.h:196
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:184
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:179
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:190
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:198
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:183
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:186
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:202
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:180
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:197
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:201
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:182
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:200
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:199
Class for operating on Nektar++ input/output files.
Definition: FieldIO.h:220
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:102
void SetAttr(const std::string &key, const std::string &val)
Set an attribute key/value pair on this tag.
Definition: FieldIOHdf5.h:109
H5::GroupSharedPtr m_Group
HDF5 group for this tag.
Definition: FieldIOHdf5.h:116
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:83
std::shared_ptr< PList > PListSharedPtr
Definition: H5.h:97
std::shared_ptr< File > FileSharedPtr
Definition: H5.h:93
std::shared_ptr< Group > GroupSharedPtr
Definition: FieldIOHdf5.h:49
std::shared_ptr< DataSet > DataSetSharedPtr
Definition: H5.h:95
std::shared_ptr< H5TagWriter > H5TagWriterSharedPtr
Definition: FieldIOHdf5.h:118
std::shared_ptr< TagWriter > TagWriterSharedPtr
Definition: FieldIO.h:71
std::shared_ptr< H5DataSource > H5DataSourceSharedPtr
Definition: FieldIOHdf5.h:88
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:301
std::shared_ptr< DataSource > DataSourceSharedPtr
Definition: FieldIO.h:81
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:177
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