Nektar++
FieldIO.h
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: FieldIO.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 prototype definitions
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_FIELDIO_H
36#define NEKTAR_LIB_UTILITIES_BASIC_UTILS_FIELDIO_H
37
43#include <tinyxml.h>
44
46
48{
49
50typedef std::map<std::string, std::string> FieldMetaDataMap;
52
53/**
54 * @brief Base class for writing hierarchical data (XML or HDF5).
55 */
57{
58public:
59 /// Create a child node.
60 std::shared_ptr<TagWriter> AddChild(const std::string &name)
61 {
62 return v_AddChild(name);
63 }
64 /// Set an attribute on the node.
65 void SetAttr(const std::string &key, const std::string &val)
66 {
67 v_SetAttr(key, val);
68 }
69
70protected:
71 virtual ~TagWriter()
72 {
73 }
74
75 virtual std::shared_ptr<TagWriter> v_AddChild(const std::string &name) = 0;
76 virtual void v_SetAttr(const std::string &key, const std::string &val) = 0;
77};
78typedef std::shared_ptr<TagWriter> TagWriterSharedPtr;
79
80/**
81 * @class A simple class encapsulating a data source. This allows us to pass
82 * around native file formats in virtual functions without resorting to using
83 * the filename.
84 */
86{
87};
88typedef std::shared_ptr<DataSource> DataSourceSharedPtr;
89
90/**
91 * @brief Metadata that describes the storage properties of field output.
92 *
93 * The purpose of this struct is to describe the format of binary field data.
94 * This can then be used in the library to determine appropriate actions. For
95 * example, when restarting a simulation, the information this struct
96 * encapsulates can be used to determine whether interpolation is required to a
97 * different polynomial order depending on the order of the simulation versus
98 * the order of the restart file.
99 *
100 * We note that some of the parameters here include:
101 *
102 * - Element shape type and the basis used
103 * - Element IDs, which determines the order of data written in a field
104 * - The field names (e.g. u for x-velocity) so that multi-field storage can be
105 * agglomerated into one data block. Each field is written in the order
106 * specified here.
107 * - Number of modes, including support for variable polynomial order
108 * - Homogeneous information (dimension of homogeneity, IDs of planes and/or
109 * strips if they are used)
110 */
112{
113 /// Default constructor
116 m_homoStrips(false), m_pointsDef(false), m_uniOrder(true),
117 m_numPointsDef(false)
118 {
119 }
120
121 /// Simple constructor to allocate all internal properties.
123 ShapeType shapeType,
124 const std::vector<unsigned int> &elementIDs, // vector[2]
125 const std::vector<LibUtilities::BasisType> &basis, bool uniOrder,
126 const std::vector<unsigned int> &numModes,
127 const std::vector<std::string> &fields, int NumHomoDir = 0,
128 const std::vector<NekDouble> &HomoLengths = NullNekDoubleVector,
129 bool homoStrips = false,
130 const std::vector<unsigned int> &HomoSIDs = NullUnsignedIntVector,
131 const std::vector<unsigned int> &HomoZIDs = NullUnsignedIntVector,
132 const std::vector<unsigned int> &HomoYIDs = NullUnsignedIntVector,
133 const std::vector<LibUtilities::PointsType> &points =
135 bool pointsDef = false,
136 const std::vector<unsigned int> &numPoints = NullUnsignedIntVector,
137 bool numPointsDef = false)
138 : m_shapeType(shapeType), m_elementIDs(elementIDs), m_basis(basis),
139 m_numHomogeneousDir(NumHomoDir), m_homogeneousLengths(HomoLengths),
140 m_homoStrips(homoStrips), m_homogeneousSIDs(HomoSIDs),
141 m_homogeneousZIDs(HomoZIDs), m_homogeneousYIDs(HomoYIDs),
142 m_points(points), m_pointsDef(pointsDef), m_uniOrder(uniOrder),
143 m_numModes(numModes), m_numPoints(numPoints),
144 m_numPointsDef(numPointsDef), m_fields(fields)
145 {
146 }
147
148 /// Shape type of this field data.
150 /// Element IDs of the field data.
151 std::vector<unsigned int> m_elementIDs;
152 /// Vector of basis types for each of the coordinate directions.
153 std::vector<LibUtilities::BasisType> m_basis;
154 /// Number of homogeneous directions, in the range \f$ 0\leq d \leq 3 \f$.
156 /// Spatial lengths of each homogeneous direction.
157 std::vector<NekDouble> m_homogeneousLengths;
158 /// Boolean determining whether homogeneous strips are used.
160 /// IDs corresponding to homogeneous strip IDs.
161 std::vector<unsigned int> m_homogeneousSIDs;
162 /// IDs corresponding to z-direction homogeneous IDs.
163 std::vector<unsigned int> m_homogeneousZIDs;
164 /// IDs corresponding to y-direction homogeneous IDs.
165 std::vector<unsigned int> m_homogeneousYIDs;
166 /// Define the type of points per direction.
167 std::vector<LibUtilities::PointsType> m_points;
168 /// Boolean determining whether points have been defined in output.
170 /// Define order of the element group.
171 /// * UniOrder: same order for each element
172 /// * MixOrder: definition of a different order for each element.
174 /// Define number of modes per direction.
175 std::vector<unsigned int> m_numModes;
176 /// Define number of points per direction.
177 std::vector<unsigned int> m_numPoints;
178 /// Boolean determining whether number of points has been defined.
180 /// Vector of field names that this data encapsulates.
181 std::vector<std::string> m_fields;
182};
183
184typedef std::shared_ptr<FieldDefinitions> FieldDefinitionsSharedPtr;
185
187 const std::string &outFile,
188 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
189 std::vector<std::vector<NekDouble>> &fielddata,
190 const FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
191 const bool backup = false);
193 const std::string &infilename,
194 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
195 std::vector<std::vector<NekDouble>> &fielddata = NullVectorNekDoubleVector,
197 const Array<OneD, int> &ElementIDs = NullInt1DArray);
198
199// Forward declare
200class FieldIO;
201
202/// Datatype of the NekFactory used to instantiate classes
203typedef LibUtilities::NekFactory<std::string, FieldIO,
206
208
209/**
210 * @brief Class for operating on Nektar++ input/output files.
211 *
212 * Nektar++ input/output of field data can be described as follows:
213 *
214 * - The FieldDefinitions class defines the metadata that is associated with
215 * one or more scalar field variables, and determines the storage length.
216 * - Each scalar field is stored in a single contiguous vector which is
217 * written/read by the functions defined in this class.
218 * - Optional metadata can be read/written in a simple key/value pair map
219 * FieldMetaDataMap. This can be used to define, e.g. the timestep of the
220 * simulation.
221 *
222 * This base class represents the minimum functionality that subclasses need to
223 * implement in order to implement the above functionality. Each subclass is
224 * free to determine its own file structure and parallel behaviour.
225 */
226class FieldIO : public std::enable_shared_from_this<FieldIO>
227{
228public:
230 bool sharedFilesystem);
231
233 {
234 }
235
236 LIB_UTILITIES_EXPORT inline void Write(
237 const std::string &outFile,
238 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
239 std::vector<std::vector<NekDouble>> &fielddata,
240 const FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
241 const bool backup = false);
242
243 LIB_UTILITIES_EXPORT inline void Import(
244 const std::string &infilename,
245 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
246 std::vector<std::vector<NekDouble>> &fielddata =
249 const Array<OneD, int> &ElementIDs = NullInt1DArray);
250
252 const std::string &filename, FieldMetaDataMap &fieldmetadatamap);
253
254 LIB_UTILITIES_EXPORT static const std::string GetFileType(
255 const std::string &filename, CommSharedPtr comm);
256
257 LIB_UTILITIES_EXPORT const std::string &GetClassName() const
258 {
259 return v_GetClassName();
260 }
261
262 LIB_UTILITIES_EXPORT static std::shared_ptr<FieldIO> CreateDefault(
264 LIB_UTILITIES_EXPORT static std::shared_ptr<FieldIO> CreateForFile(
266 const std::string &filename);
268 TagWriterSharedPtr root, const FieldMetaDataMap &fieldmetadatamap);
269
270 /**
271 * @brief Helper function that determines default file extension.
272 */
274 {
275 return v_GetFileEnding();
276 };
277
278protected:
279 /// Communicator to use when writing parallel format
281 /// Boolean dictating whether we are on a shared filesystem.
283
285 const FieldDefinitionsSharedPtr &fielddefs);
286
287 LIB_UTILITIES_EXPORT std::vector<unsigned int> GetNumberOfCoeffsPerElement(
288 const FieldDefinitionsSharedPtr &fielddefs);
289
290 LIB_UTILITIES_EXPORT std::string SetUpOutput(const std::string outname,
291 bool perRank,
292 bool backup = false);
293
294 /// @copydoc FieldIO::Write
296 const std::string &outFile,
297 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
298 std::vector<std::vector<NekDouble>> &fielddata,
299 const FieldMetaDataMap &fieldinfomap, const bool backup = false) = 0;
300
301 /// @copydoc FieldIO::Import
303 const std::string &infilename,
304 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
305 std::vector<std::vector<NekDouble>> &fielddata =
308 const Array<OneD, int> &ElementIDs = NullInt1DArray) = 0;
309
310 /// @copydoc FieldIO::ImportFieldMetaData
312 const std::string &filename, FieldMetaDataMap &fieldmetadatamap) = 0;
313
314 LIB_UTILITIES_EXPORT virtual std::string v_GetFileEnding() const
315 {
316 return "fld";
317 }
318
319 LIB_UTILITIES_EXPORT virtual const std::string &v_GetClassName() const = 0;
320};
321
322typedef std::shared_ptr<FieldIO> FieldIOSharedPtr;
323
324/**
325 * @brief Write out the field information to the file @p outFile.
326 *
327 * @param outFile Output filename
328 * @param fielddefs Field definitions that define the output
329 * @param fielddata Binary field data that stores the output corresponding
330 * to @p fielddefs.
331 * @param fieldinfomap Associated field metadata map.
332 */
333inline void FieldIO::Write(const std::string &outFile,
334 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
335 std::vector<std::vector<NekDouble>> &fielddata,
336 const FieldMetaDataMap &fieldinfomap,
337 const bool backup)
338{
339 v_Write(outFile, fielddefs, fielddata, fieldinfomap, backup);
340}
341
342/**
343 * @brief Read field information from the file @p infilename.
344 *
345 * @param infilename Input filename (or directory if parallel format)
346 * @param fielddefs On return contains field definitions as read from the
347 * input.
348 * @param fielddata On return, contains binary field data that stores the
349 * input corresponding to @p fielddefs.
350 * @param fieldinfo On returnm, contains the associated field metadata map.
351 * @param ElementIDs Element IDs that lie on this processor, which can be
352 * optionally supplied to avoid reading the entire file on
353 * each processor.
354 */
355inline void FieldIO::Import(const std::string &infilename,
356 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
357 std::vector<std::vector<NekDouble>> &fielddata,
358 FieldMetaDataMap &fieldinfo,
359 const Array<OneD, int> &ElementIDs)
360{
361 v_Import(infilename, fielddefs, fielddata, fieldinfo, ElementIDs);
362}
363
364/**
365 * @brief Import the metadata from a field file.
366 *
367 * @param filename Input filename.
368 * @param fieldmetadatamap On return contains the field metadata map from @p
369 * filename.
370 */
372 const std::string &filename, FieldMetaDataMap &fieldmetadatamap)
373{
374 return v_ImportFieldMetaData(filename, fieldmetadatamap);
375}
376
377} // namespace Nektar::LibUtilities
378#endif
#define LIB_UTILITIES_EXPORT
Class for operating on Nektar++ input/output files.
Definition: FieldIO.h:227
int CheckFieldDefinition(const FieldDefinitionsSharedPtr &fielddefs)
Check field definitions for correctness and return storage size.
Definition: FieldIO.cpp:690
static const std::string GetFileType(const std::string &filename, CommSharedPtr comm)
Determine file type of given input file.
Definition: FieldIO.cpp:95
std::vector< unsigned int > GetNumberOfCoeffsPerElement(const FieldDefinitionsSharedPtr &fielddefs)
Compute number of data points needed to store expansion inside each element.
Definition: FieldIO.cpp:767
bool m_sharedFilesystem
Boolean dictating whether we are on a shared filesystem.
Definition: FieldIO.h:282
void Write(const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap, const bool backup=false)
Write out the field information to the file outFile.
Definition: FieldIO.h:333
DataSourceSharedPtr ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap)
Import the metadata from a field file.
Definition: FieldIO.h:371
static std::shared_ptr< FieldIO > CreateForFile(const LibUtilities::SessionReaderSharedPtr session, const std::string &filename)
Construct a FieldIO object for a given input filename.
Definition: FieldIO.cpp:224
virtual void v_Write(const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap, const bool backup=false)=0
Write out the field information to the file outFile.
virtual std::string v_GetFileEnding() const
Definition: FieldIO.h:314
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)=0
Read field information from the file infilename.
static std::shared_ptr< FieldIO > CreateDefault(const LibUtilities::SessionReaderSharedPtr session)
Returns an object for the default FieldIO method.
Definition: FieldIO.cpp:195
std::string SetUpOutput(const std::string outname, bool perRank, bool backup=false)
Set up the filesystem ready for output.
Definition: FieldIO.cpp:404
const std::string & GetClassName() const
Definition: FieldIO.h:257
LibUtilities::CommSharedPtr m_comm
Communicator to use when writing parallel format.
Definition: FieldIO.h:280
FieldIO(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Constructor for FieldIO base class.
Definition: FieldIO.cpp:321
std::string GetFileEnding() const
Helper function that determines default file extension.
Definition: FieldIO.h:273
virtual const std::string & v_GetClassName() const =0
static void AddInfoTag(TagWriterSharedPtr root, const FieldMetaDataMap &fieldmetadatamap)
Add provenance information to the field metadata map.
Definition: FieldIO.cpp:342
virtual DataSourceSharedPtr v_ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap)=0
Import the metadata from a field file.
void 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)
Read field information from the file infilename.
Definition: FieldIO.h:355
Provides a generic Factory class.
Definition: NekFactory.hpp:104
Base class for writing hierarchical data (XML or HDF5).
Definition: FieldIO.h:57
void SetAttr(const std::string &key, const std::string &val)
Set an attribute on the node.
Definition: FieldIO.h:65
std::shared_ptr< TagWriter > AddChild(const std::string &name)
Create a child node.
Definition: FieldIO.h:60
virtual void v_SetAttr(const std::string &key, const std::string &val)=0
virtual std::shared_ptr< TagWriter > v_AddChild(const std::string &name)=0
static std::vector< unsigned int > NullUnsignedIntVector
void Import(const std::string &infilename, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, FieldMetaDataMap &fieldinfomap, const Array< OneD, int > &ElementIDs)
This function allows for data to be imported from an FLD file when a session and/or communicator is n...
Definition: FieldIO.cpp:288
std::shared_ptr< TagWriter > TagWriterSharedPtr
Definition: FieldIO.h:78
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:322
std::shared_ptr< DataSource > DataSourceSharedPtr
Definition: FieldIO.h:88
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:50
LibUtilities::NekFactory< std::string, FieldIO, LibUtilities::CommSharedPtr, bool > FieldIOFactory
Datatype of the NekFactory used to instantiate classes.
Definition: FieldIO.h:205
std::shared_ptr< SessionReader > SessionReaderSharedPtr
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:51
static std::vector< std::vector< NekDouble > > NullVectorNekDoubleVector
std::shared_ptr< FieldDefinitions > FieldDefinitionsSharedPtr
Definition: FieldIO.h:184
FieldIOFactory & GetFieldIOFactory()
Returns the FieldIO factory.
Definition: FieldIO.cpp:70
static std::vector< NekDouble > NullNekDoubleVector
void Write(const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap, const bool backup)
This function allows for data to be written to an FLD file when a session and/or communicator is not ...
Definition: FieldIO.cpp:245
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:55
static std::vector< LibUtilities::PointsType > NullPointsTypeVector
Definition: PointsType.h:98
static Array< OneD, int > NullInt1DArray
Metadata that describes the storage properties of field output.
Definition: FieldIO.h:112
std::vector< NekDouble > m_homogeneousLengths
Spatial lengths of each homogeneous direction.
Definition: FieldIO.h:157
int m_numHomogeneousDir
Number of homogeneous directions, in the range .
Definition: FieldIO.h:155
std::vector< LibUtilities::BasisType > m_basis
Vector of basis types for each of the coordinate directions.
Definition: FieldIO.h:153
bool m_pointsDef
Boolean determining whether points have been defined in output.
Definition: FieldIO.h:169
std::vector< std::string > m_fields
Vector of field names that this data encapsulates.
Definition: FieldIO.h:181
std::vector< unsigned int > m_homogeneousSIDs
IDs corresponding to homogeneous strip IDs.
Definition: FieldIO.h:161
std::vector< unsigned int > m_numPoints
Define number of points per direction.
Definition: FieldIO.h:177
std::vector< LibUtilities::PointsType > m_points
Define the type of points per direction.
Definition: FieldIO.h:167
bool m_uniOrder
Define order of the element group.
Definition: FieldIO.h:173
std::vector< unsigned int > m_homogeneousYIDs
IDs corresponding to y-direction homogeneous IDs.
Definition: FieldIO.h:165
std::vector< unsigned int > m_homogeneousZIDs
IDs corresponding to z-direction homogeneous IDs.
Definition: FieldIO.h:163
std::vector< unsigned int > m_numModes
Define number of modes per direction.
Definition: FieldIO.h:175
ShapeType m_shapeType
Shape type of this field data.
Definition: FieldIO.h:149
bool m_homoStrips
Boolean determining whether homogeneous strips are used.
Definition: FieldIO.h:159
FieldDefinitions(ShapeType shapeType, const std::vector< unsigned int > &elementIDs, const std::vector< LibUtilities::BasisType > &basis, bool uniOrder, const std::vector< unsigned int > &numModes, const std::vector< std::string > &fields, int NumHomoDir=0, const std::vector< NekDouble > &HomoLengths=NullNekDoubleVector, bool homoStrips=false, const std::vector< unsigned int > &HomoSIDs=NullUnsignedIntVector, const std::vector< unsigned int > &HomoZIDs=NullUnsignedIntVector, const std::vector< unsigned int > &HomoYIDs=NullUnsignedIntVector, const std::vector< LibUtilities::PointsType > &points=NullPointsTypeVector, bool pointsDef=false, const std::vector< unsigned int > &numPoints=NullUnsignedIntVector, bool numPointsDef=false)
Simple constructor to allocate all internal properties.
Definition: FieldIO.h:122
bool m_numPointsDef
Boolean determining whether number of points has been defined.
Definition: FieldIO.h:179
std::vector< unsigned int > m_elementIDs
Element IDs of the field data.
Definition: FieldIO.h:151
FieldDefinitions()
Default constructor.
Definition: FieldIO.h:114