Nektar++
FieldIOXml.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File FieldIOXml.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 XML
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_FIELDIOXML_H
36 #define NEKTAR_LIB_UTILITIES_BASIC_UTILS_FIELDIOXML_H
37 
38 #include <boost/algorithm/string/predicate.hpp>
39 
42 
43 namespace Nektar
44 {
45 namespace LibUtilities
46 {
47 
48 /**
49  * @class Class encapsulating simple XML data source using TinyXML.
50  */
51 class XmlDataSource : public DataSource
52 {
53 public:
54  /// Default constructor.
55  XmlDataSource(TiXmlDocument &doc) : m_doc(&doc), m_needsFree(false)
56  {
57  }
58 
59  /// Constructor based on filename.
60  XmlDataSource(const std::string &fn) : m_needsFree(true)
61  {
62  m_doc = new TiXmlDocument(fn);
63  bool loadOkay = m_doc->LoadFile();
64  std::stringstream errstr;
65  errstr << "Unable to load file: " << fn << std::endl;
66  errstr << "Reason: " << m_doc->ErrorDesc() << std::endl;
67  errstr << "Position: Line " << m_doc->ErrorRow() << ", Column "
68  << m_doc->ErrorCol() << std::endl;
69  ASSERTL0(loadOkay, errstr.str());
70  }
71 
72  /// Destructor cleans up memory usage.
74  {
75  if (m_needsFree)
76  {
77  delete m_doc;
78  }
79  }
80 
81  /// Return the TinyXML document of this source.
82  TiXmlDocument &Get()
83  {
84  return *m_doc;
85  }
86 
87  /// Return the TinyXML document of this source.
88  const TiXmlDocument &Get() const
89  {
90  return *m_doc;
91  }
92 
93  /// Create a new XML data source based on the filename.
94  static DataSourceSharedPtr create(const std::string &fn)
95  {
96  return DataSourceSharedPtr(new XmlDataSource(fn));
97  }
98 
99  /// Create a new XML data source based on a TiXmlDocument.
100  static DataSourceSharedPtr create(TiXmlDocument &fn)
101  {
102  return DataSourceSharedPtr(new XmlDataSource(fn));
103  }
104 
105 private:
106  /// Internal TinyXML document storage.
107  TiXmlDocument *m_doc;
108  /// Boolean dictating whether document needs to be freed or not.
110 };
111 typedef std::shared_ptr<XmlDataSource> XmlDataSourceSharedPtr;
112 
113 /**
114  * @class Simple class for writing XML hierarchical data using TinyXML.
115  */
116 class XmlTagWriter : public TagWriter
117 {
118 public:
119  /// Default constructor.
120  XmlTagWriter(TiXmlElement *elem) : m_El(elem)
121  {
122  }
123 
124  /// Add a child node.
125  virtual TagWriterSharedPtr AddChild(const std::string &name)
126  {
127  TiXmlElement *child = new TiXmlElement(name.c_str());
128  m_El->LinkEndChild(child);
129  return TagWriterSharedPtr(new XmlTagWriter(child));
130  }
131 
132  /// Set an attribute key/value pair on this tag.
133  virtual void SetAttr(const std::string &key, const std::string &val)
134  {
135  if (boost::starts_with(key, "XML_"))
136  {
137  // Auto-expand XML parameters.
138  std::string elmtName = key.substr(4);
139  TiXmlElement *child = new TiXmlElement(elmtName.c_str());
140 
141  // Parse string we're given
142  TiXmlDocument doc;
143  doc.Parse(val.c_str());
144 
145  TiXmlElement *e = doc.FirstChildElement();
146 
147  while (e)
148  {
149  child->LinkEndChild(e->Clone());
150  e = e->NextSiblingElement();
151  }
152 
153  m_El->LinkEndChild(child);
154  }
155  else
156  {
157  TiXmlElement *child = new TiXmlElement(key.c_str());
158  child->LinkEndChild(new TiXmlText(val.c_str()));
159  m_El->LinkEndChild(child);
160  }
161  }
162 
163 private:
164  /// Internal TinyXML document storage.
165  TiXmlElement *m_El;
166 };
167 typedef std::shared_ptr<XmlTagWriter> XmlTagWriterSharedPtr;
168 
169 /**
170  * @class Class for operating on XML FLD files.
171  *
172  * This class is the default for Nektar++ output. It reads and writes one XML
173  * file per processor that represents the underlying field data. For serial
174  * output, the format of an XML file obeys the following structure:
175  *
176  * ```
177  * <NEKTAR>
178  * <Metadata>
179  * ...
180  * </Metadata>
181  * <ELEMENT FIELDS="..." ...> data1 </ELEMENT>
182  * <ELEMENT FIELDS="..." ...> data2 </ELEMENT>
183  * ...
184  * </NEKTAR>
185  * ```
186  *
187  * - Metadata is converted as key/value pairs in the `<Metadata>` tag.
188  * - There are one or more ELEMENT blocks, whose attributes correspond with
189  * the FieldDefinitions class variables.
190  * - Element data is stored as a base64-encoded zlib-compressed binary
191  * double-precision data using the functions from CompressData.
192  *
193  * In parallel, each process writes its contributions into an XML file of the
194  * form `P0000001.fld` (where 1 is replaced by the rank of the process) inside a
195  * directory with the desired output name. These files only include the
196  * `ELEMENT` data. Metadata are instead stored in a separate `Info.xml` file,
197  * which contains the Metadata and additional tags of the form
198  *
199  * `<Partition FileName="P0000000.fld"> ID list </Partition>`
200  *
201  * The ID list enumerates all element IDs on each partition's contribution. For
202  * large parallel jobs, this is used to avoid each process reading in every
203  * single partition in order to load field data.
204  */
205 class FieldIOXml : public FieldIO
206 {
207 public:
208  /// Creates an instance of this class
210  LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
211  {
213  sharedFilesystem);
214  }
215 
216  /// Name of class
217  LIB_UTILITIES_EXPORT static std::string className;
218 
220  bool sharedFilesystem);
221 
223  {
224  }
225 
227  DataSourceSharedPtr dataSource,
228  std::vector<FieldDefinitionsSharedPtr> &fielddefs, bool expChild);
229 
231  DataSourceSharedPtr dataSource,
232  const std::vector<FieldDefinitionsSharedPtr> &fielddefs,
233  std::vector<std::vector<NekDouble>> &fielddata);
234 
236  const std::string &outfile, const std::vector<std::string> fileNames,
237  std::vector<std::vector<unsigned int>> &elementList,
238  const FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap);
239 
241  const std::string &outname,
242  const std::vector<FieldDefinitionsSharedPtr> &fielddefs,
243  const FieldMetaDataMap &fieldmetadatamap);
244 
246  const std::string &inFile, std::vector<std::string> &fileNames,
247  std::vector<std::vector<unsigned int>> &elementList,
248  FieldMetaDataMap &fieldmetadatamap);
249 
251  const std::string &infilename,
252  std::vector<FieldDefinitionsSharedPtr> &fielddefs,
253  std::vector<std::vector<NekDouble>> &fielddata =
255  FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
256  const Array<OneD, int> &ElementIDs = NullInt1DArray);
257 
258  /// Returns the class name.
259  inline virtual const std::string &GetClassName() const
260  {
261  return className;
262  }
263 
264 private:
265  LIB_UTILITIES_EXPORT virtual void v_Write(
266  const std::string &outFile,
267  std::vector<FieldDefinitionsSharedPtr> &fielddefs,
268  std::vector<std::vector<NekDouble>> &fielddata,
269  const FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
270  const bool backup = false);
271 
273  const std::string &filename, FieldMetaDataMap &fieldmetadatamap);
274 };
275 
276 } // namespace LibUtilities
277 } // namespace Nektar
278 #endif
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
#define LIB_UTILITIES_EXPORT
Class for operating on Nektar++ input/output files.
Definition: FieldIO.h:220
virtual DataSourceSharedPtr v_ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap)
Import field metadata from filename and return the data source which wraps filename.
Definition: FieldIOXml.cpp:597
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)
Import an XML format file.
Definition: FieldIOXml.cpp:496
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)
Write an XML file to outFile given the field definitions fielddefs, field data fielddata and metadata...
Definition: FieldIOXml.cpp:87
void ImportFieldDefs(DataSourceSharedPtr dataSource, std::vector< FieldDefinitionsSharedPtr > &fielddefs, bool expChild)
Import field definitions from the target file.
Definition: FieldIOXml.cpp:765
void WriteMultiFldFileIDs(const std::string &outfile, const std::vector< std::string > fileNames, std::vector< std::vector< unsigned int >> &elementList, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap)
Write out a file containing element ID to partition mapping.
Definition: FieldIOXml.cpp:376
static FieldIOSharedPtr create(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Creates an instance of this class.
Definition: FieldIOXml.h:209
virtual const std::string & GetClassName() const
Returns the class name.
Definition: FieldIOXml.h:259
void ImportMultiFldFileIDs(const std::string &inFile, std::vector< std::string > &fileNames, std::vector< std::vector< unsigned int >> &elementList, FieldMetaDataMap &fieldmetadatamap)
Read file containing element ID to partition mapping.
Definition: FieldIOXml.cpp:423
void ImportFieldData(DataSourceSharedPtr dataSource, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble >> &fielddata)
Import field data from a target file.
FieldIOXml(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Default constructor.
Definition: FieldIOXml.cpp:62
void SetUpFieldMetaData(const std::string &outname, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, const FieldMetaDataMap &fieldmetadatamap)
Set up field meta data map.
Definition: FieldIOXml.cpp:689
static std::string className
Name of class.
Definition: FieldIOXml.h:217
Base class for writing hierarchical data (XML or HDF5).
Definition: FieldIO.h:59
TiXmlDocument * m_doc
Internal TinyXML document storage.
Definition: FieldIOXml.h:107
TiXmlDocument & Get()
Return the TinyXML document of this source.
Definition: FieldIOXml.h:82
static DataSourceSharedPtr create(TiXmlDocument &fn)
Create a new XML data source based on a TiXmlDocument.
Definition: FieldIOXml.h:100
static DataSourceSharedPtr create(const std::string &fn)
Create a new XML data source based on the filename.
Definition: FieldIOXml.h:94
~XmlDataSource()
Destructor cleans up memory usage.
Definition: FieldIOXml.h:73
XmlDataSource(const std::string &fn)
Constructor based on filename.
Definition: FieldIOXml.h:60
const TiXmlDocument & Get() const
Return the TinyXML document of this source.
Definition: FieldIOXml.h:88
bool m_needsFree
Boolean dictating whether document needs to be freed or not.
Definition: FieldIOXml.h:109
XmlDataSource(TiXmlDocument &doc)
Default constructor.
Definition: FieldIOXml.h:55
XmlTagWriter(TiXmlElement *elem)
Default constructor.
Definition: FieldIOXml.h:120
virtual TagWriterSharedPtr AddChild(const std::string &name)
Add a child node.
Definition: FieldIOXml.h:125
TiXmlElement * m_El
Internal TinyXML document storage.
Definition: FieldIOXml.h:165
virtual void SetAttr(const std::string &key, const std::string &val)
Set an attribute key/value pair on this tag.
Definition: FieldIOXml.h:133
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
std::shared_ptr< TagWriter > TagWriterSharedPtr
Definition: FieldIO.h:71
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:301
std::shared_ptr< DataSource > DataSourceSharedPtr
Definition: FieldIO.h:81
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:52
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:53
std::shared_ptr< XmlDataSource > XmlDataSourceSharedPtr
Definition: FieldIOXml.h:111
static std::vector< std::vector< NekDouble > > NullVectorNekDoubleVector
std::shared_ptr< XmlTagWriter > XmlTagWriterSharedPtr
Definition: FieldIOXml.h:167
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:54
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
static Array< OneD, int > NullInt1DArray