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