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