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  /// Constructor based on filename.
58  XmlDataSource(const std::string &fn) : m_needsFree(true)
59  {
60  m_doc = new TiXmlDocument(fn);
61  bool loadOkay = m_doc->LoadFile();
62  std::stringstream errstr;
63  errstr << "Unable to load file: " << fn << std::endl;
64  errstr << "Reason: " << m_doc->ErrorDesc() << std::endl;
65  errstr << "Position: Line " << m_doc->ErrorRow() << ", Column "
66  << m_doc->ErrorCol() << std::endl;
67  ASSERTL0(loadOkay, errstr.str());
68  }
69 
70  /// Destructor cleans up memory usage.
72  {
73  if (m_needsFree)
74  {
75  delete m_doc;
76  }
77  }
78 
79  /// Return the TinyXML document of this source.
80  TiXmlDocument &Get()
81  {
82  return *m_doc;
83  }
84 
85  /// Return the TinyXML document of this source.
86  const TiXmlDocument &Get() const
87  {
88  return *m_doc;
89  }
90 
91  /// Create a new XML data source based on the filename.
92  static DataSourceSharedPtr create(const std::string &fn)
93  {
94  return DataSourceSharedPtr(new XmlDataSource(fn));
95  }
96 
97  /// Create a new XML data source based on a TiXmlDocument.
98  static DataSourceSharedPtr create(TiXmlDocument &fn)
99  {
100  return DataSourceSharedPtr(new XmlDataSource(fn));
101  }
102 
103 private:
104  /// Internal TinyXML document storage.
105  TiXmlDocument *m_doc;
106  /// Boolean dictating whether document needs to be freed or not.
108 };
109 typedef std::shared_ptr<XmlDataSource> XmlDataSourceSharedPtr;
110 
111 /**
112  * @class Simple class for writing XML hierarchical data using TinyXML.
113  */
114 class XmlTagWriter : public TagWriter
115 {
116 public:
117  /// Default constructor.
118  XmlTagWriter(TiXmlElement *elem) : m_El(elem) {}
119 
120  /// Add a child node.
121  virtual TagWriterSharedPtr AddChild(const std::string &name)
122  {
123  TiXmlElement *child = new TiXmlElement(name.c_str());
124  m_El->LinkEndChild(child);
125  return TagWriterSharedPtr(new XmlTagWriter(child));
126  }
127 
128  /// Set an attribute key/value pair on this tag.
129  virtual void SetAttr(const std::string &key, const std::string &val)
130  {
131  if (boost::starts_with(key, "XML_"))
132  {
133  // Auto-expand XML parameters.
134  std::string elmtName = key.substr(4);
135  TiXmlElement *child = new TiXmlElement(elmtName.c_str());
136 
137  // Parse string we're given
138  TiXmlDocument doc;
139  doc.Parse(val.c_str());
140 
141  TiXmlElement *e = doc.FirstChildElement();
142 
143  while (e)
144  {
145  child->LinkEndChild(e->Clone());
146  e = e->NextSiblingElement();
147  }
148 
149  m_El->LinkEndChild(child);
150  }
151  else
152  {
153  TiXmlElement *child = new TiXmlElement(key.c_str());
154  child->LinkEndChild(new TiXmlText(val.c_str()));
155  m_El->LinkEndChild(child);
156  }
157  }
158 
159 private:
160  /// Internal TinyXML document storage.
161  TiXmlElement *m_El;
162 };
163 typedef std::shared_ptr<XmlTagWriter> XmlTagWriterSharedPtr;
164 
165 /**
166  * @class Class for operating on XML FLD files.
167  *
168  * This class is the default for Nektar++ output. It reads and writes one XML
169  * file per processor that represents the underlying field data. For serial
170  * output, the format of an XML file obeys the following structure:
171  *
172  * ```
173  * <NEKTAR>
174  * <Metadata>
175  * ...
176  * </Metadata>
177  * <ELEMENT FIELDS="..." ...> data1 </ELEMENT>
178  * <ELEMENT FIELDS="..." ...> data2 </ELEMENT>
179  * ...
180  * </NEKTAR>
181  * ```
182  *
183  * - Metadata is converted as key/value pairs in the `<Metadata>` tag.
184  * - There are one or more ELEMENT blocks, whose attributes correspond with
185  * the FieldDefinitions class variables.
186  * - Element data is stored as a base64-encoded zlib-compressed binary
187  * double-precision data using the functions from CompressData.
188  *
189  * In parallel, each process writes its contributions into an XML file of the
190  * form `P0000001.fld` (where 1 is replaced by the rank of the process) inside a
191  * directory with the desired output name. These files only include the
192  * `ELEMENT` data. Metadata are instead stored in a separate `Info.xml` file,
193  * which contains the Metadata and additional tags of the form
194  *
195  * `<Partition FileName="P0000000.fld"> ID list </Partition>`
196  *
197  * The ID list enumerates all element IDs on each partition's contribution. For
198  * large parallel jobs, this is used to avoid each process reading in every
199  * single partition in order to load field data.
200  */
201 class FieldIOXml : public FieldIO
202 {
203 public:
204  /// Creates an instance of this class
206  LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
207  {
209  sharedFilesystem);
210  }
211 
212  /// Name of class
213  LIB_UTILITIES_EXPORT static std::string className;
214 
217  bool sharedFilesystem);
218 
220  {
221  }
222 
224  DataSourceSharedPtr dataSource,
225  std::vector<FieldDefinitionsSharedPtr> &fielddefs,
226  bool expChild);
227 
229  DataSourceSharedPtr dataSource,
230  const std::vector<FieldDefinitionsSharedPtr> &fielddefs,
231  std::vector<std::vector<NekDouble> > &fielddata);
232 
234  const std::string &outfile,
235  const std::vector<std::string> fileNames,
236  std::vector<std::vector<unsigned int> > &elementList,
237  const FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap);
238 
240  const std::string &outname,
241  const std::vector<FieldDefinitionsSharedPtr> &fielddefs,
242  const FieldMetaDataMap &fieldmetadatamap);
243 
245  const std::string &inFile,
246  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 }
277 }
278 #endif
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
#define LIB_UTILITIES_EXPORT
Class for operating on Nektar++ input/output files.
Definition: FieldIO.h:223
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:428
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:605
void ImportFieldData(DataSourceSharedPtr dataSource, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata)
Import field data from a target file.
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:775
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:380
static FieldIOSharedPtr create(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Creates an instance of this class.
Definition: FieldIOXml.h:205
virtual const std::string & GetClassName() const
Returns the class name.
Definition: FieldIOXml.h:259
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:504
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:697
static std::string className
Name of class.
Definition: FieldIOXml.h:213
Base class for writing hierarchical data (XML or HDF5).
Definition: FieldIO.h:59
TiXmlDocument * m_doc
Internal TinyXML document storage.
Definition: FieldIOXml.h:105
TiXmlDocument & Get()
Return the TinyXML document of this source.
Definition: FieldIOXml.h:80
static DataSourceSharedPtr create(TiXmlDocument &fn)
Create a new XML data source based on a TiXmlDocument.
Definition: FieldIOXml.h:98
static DataSourceSharedPtr create(const std::string &fn)
Create a new XML data source based on the filename.
Definition: FieldIOXml.h:92
~XmlDataSource()
Destructor cleans up memory usage.
Definition: FieldIOXml.h:71
XmlDataSource(const std::string &fn)
Constructor based on filename.
Definition: FieldIOXml.h:58
const TiXmlDocument & Get() const
Return the TinyXML document of this source.
Definition: FieldIOXml.h:86
bool m_needsFree
Boolean dictating whether document needs to be freed or not.
Definition: FieldIOXml.h:107
XmlDataSource(TiXmlDocument &doc)
Default constructor.
Definition: FieldIOXml.h:55
XmlTagWriter(TiXmlElement *elem)
Default constructor.
Definition: FieldIOXml.h:118
virtual TagWriterSharedPtr AddChild(const std::string &name)
Add a child node.
Definition: FieldIOXml.h:121
TiXmlElement * m_El
Internal TinyXML document storage.
Definition: FieldIOXml.h:161
virtual void SetAttr(const std::string &key, const std::string &val)
Set an attribute key/value pair on this tag.
Definition: FieldIOXml.h:129
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: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
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:53
std::shared_ptr< XmlDataSource > XmlDataSourceSharedPtr
Definition: FieldIOXml.h:109
static std::vector< std::vector< NekDouble > > NullVectorNekDoubleVector
std::shared_ptr< XmlTagWriter > XmlTagWriterSharedPtr
Definition: FieldIOXml.h:163
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