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