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