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 protected:
125  /// Add a child node.
126  virtual TagWriterSharedPtr v_AddChild(const std::string &name) override
127  {
128  TiXmlElement *child = new TiXmlElement(name.c_str());
129  m_El->LinkEndChild(child);
130  return TagWriterSharedPtr(new XmlTagWriter(child));
131  }
132 
133  /// Set an attribute key/value pair on this tag.
134  virtual void v_SetAttr(const std::string &key,
135  const std::string &val) override
136  {
137  if (boost::starts_with(key, "XML_"))
138  {
139  // Auto-expand XML parameters.
140  std::string elmtName = key.substr(4);
141  TiXmlElement *child = new TiXmlElement(elmtName.c_str());
142 
143  // Parse string we're given
144  TiXmlDocument doc;
145  doc.Parse(val.c_str());
146 
147  TiXmlElement *e = doc.FirstChildElement();
148 
149  while (e)
150  {
151  child->LinkEndChild(e->Clone());
152  e = e->NextSiblingElement();
153  }
154 
155  m_El->LinkEndChild(child);
156  }
157  else
158  {
159  TiXmlElement *child = new TiXmlElement(key.c_str());
160  child->LinkEndChild(new TiXmlText(val.c_str()));
161  m_El->LinkEndChild(child);
162  }
163  }
164 
165 private:
166  /// Internal TinyXML document storage.
167  TiXmlElement *m_El;
168 };
169 typedef std::shared_ptr<XmlTagWriter> XmlTagWriterSharedPtr;
170 
171 /**
172  * @class Class for operating on XML FLD files.
173  *
174  * This class is the default for Nektar++ output. It reads and writes one XML
175  * file per processor that represents the underlying field data. For serial
176  * output, the format of an XML file obeys the following structure:
177  *
178  * ```
179  * <NEKTAR>
180  * <Metadata>
181  * ...
182  * </Metadata>
183  * <ELEMENT FIELDS="..." ...> data1 </ELEMENT>
184  * <ELEMENT FIELDS="..." ...> data2 </ELEMENT>
185  * ...
186  * </NEKTAR>
187  * ```
188  *
189  * - Metadata is converted as key/value pairs in the `<Metadata>` tag.
190  * - There are one or more ELEMENT blocks, whose attributes correspond with
191  * the FieldDefinitions class variables.
192  * - Element data is stored as a base64-encoded zlib-compressed binary
193  * double-precision data using the functions from CompressData.
194  *
195  * In parallel, each process writes its contributions into an XML file of the
196  * form `P0000001.fld` (where 1 is replaced by the rank of the process) inside a
197  * directory with the desired output name. These files only include the
198  * `ELEMENT` data. Metadata are instead stored in a separate `Info.xml` file,
199  * which contains the Metadata and additional tags of the form
200  *
201  * `<Partition FileName="P0000000.fld"> ID list </Partition>`
202  *
203  * The ID list enumerates all element IDs on each partition's contribution. For
204  * large parallel jobs, this is used to avoid each process reading in every
205  * single partition in order to load field data.
206  */
207 class FieldIOXml : public FieldIO
208 {
209 public:
210  /// Creates an instance of this class
212  LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
213  {
215  sharedFilesystem);
216  }
217 
218  /// Name of class
219  LIB_UTILITIES_EXPORT static std::string className;
220 
222  bool sharedFilesystem);
223 
225  {
226  }
227 
229  DataSourceSharedPtr dataSource,
230  std::vector<FieldDefinitionsSharedPtr> &fielddefs, bool expChild);
231 
233  DataSourceSharedPtr dataSource,
234  const std::vector<FieldDefinitionsSharedPtr> &fielddefs,
235  std::vector<std::vector<NekDouble>> &fielddata);
236 
238  const std::string &outfile, const std::vector<std::string> fileNames,
239  std::vector<std::vector<unsigned int>> &elementList,
240  const FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap);
241 
243  const std::string &outname,
244  const std::vector<FieldDefinitionsSharedPtr> &fielddefs,
245  const FieldMetaDataMap &fieldmetadatamap);
246 
248  const std::string &inFile, std::vector<std::string> &fileNames,
249  std::vector<std::vector<unsigned int>> &elementList,
250  FieldMetaDataMap &fieldmetadatamap);
251 
252 protected:
253  LIB_UTILITIES_EXPORT virtual void v_Write(
254  const std::string &outFile,
255  std::vector<FieldDefinitionsSharedPtr> &fielddefs,
256  std::vector<std::vector<NekDouble>> &fielddata,
257  const FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
258  const bool backup = false) override;
259 
261  const std::string &infilename,
262  std::vector<FieldDefinitionsSharedPtr> &fielddefs,
263  std::vector<std::vector<NekDouble>> &fielddata =
265  FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
266  const Array<OneD, int> &ElementIDs = NullInt1DArray) override;
267 
269  const std::string &filename,
270  FieldMetaDataMap &fieldmetadatamap) override;
271 
272  virtual const std::string &v_GetClassName() const override;
273 };
274 
275 } // namespace LibUtilities
276 } // namespace Nektar
277 #endif
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
#define LIB_UTILITIES_EXPORT
Class for operating on Nektar++ input/output files.
Definition: FieldIO.h:229
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) override
Write an XML file to outFile given the field definitions fielddefs, field data fielddata and metadata...
Definition: FieldIOXml.cpp:87
virtual const std::string & v_GetClassName() const override
Returns the class name.
Definition: FieldIOXml.cpp:677
void ImportFieldDefs(DataSourceSharedPtr dataSource, std::vector< FieldDefinitionsSharedPtr > &fielddefs, bool expChild)
Import field definitions from the target file.
Definition: FieldIOXml.cpp:771
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:211
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.
virtual DataSourceSharedPtr v_ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap) override
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) override
Import an XML format file.
Definition: FieldIOXml.cpp:496
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:695
static std::string className
Name of class.
Definition: FieldIOXml.h:219
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 v_AddChild(const std::string &name) override
Add a child node.
Definition: FieldIOXml.h:126
virtual void v_SetAttr(const std::string &key, const std::string &val) override
Set an attribute key/value pair on this tag.
Definition: FieldIOXml.h:134
TiXmlElement * m_El
Internal TinyXML document storage.
Definition: FieldIOXml.h:167
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:80
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:327
std::shared_ptr< DataSource > DataSourceSharedPtr
Definition: FieldIO.h:90
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:169
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:2
static Array< OneD, int > NullInt1DArray