Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Field IO to/from XML
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_FIELDIOXML_H
37 #define NEKTAR_LIB_UTILITIES_BASIC_UTILS_FIELDIOXML_H
38 
41 
42 namespace Nektar
43 {
44 namespace LibUtilities
45 {
46 
47 /**
48  * @class Class encapsulating simple XML data source using TinyXML.
49  */
50 class XmlDataSource : public DataSource
51 {
52 public:
53  /// Default constructor.
54  XmlDataSource(TiXmlDocument &doc) : m_doc(&doc), m_needsFree(false) { }
55 
56  /// Constructor based on filename.
57  XmlDataSource(const std::string &fn) : m_needsFree(true)
58  {
59  m_doc = new TiXmlDocument(fn);
60  bool loadOkay = m_doc->LoadFile();
61  std::stringstream errstr;
62  errstr << "Unable to load file: " << fn << std::endl;
63  errstr << "Reason: " << m_doc->ErrorDesc() << std::endl;
64  errstr << "Position: Line " << m_doc->ErrorRow() << ", Column "
65  << m_doc->ErrorCol() << std::endl;
66  ASSERTL0(loadOkay, errstr.str());
67  }
68 
69  /// Destructor cleans up memory usage.
71  {
72  if (m_needsFree)
73  {
74  delete m_doc;
75  }
76  }
77 
78  /// Return the TinyXML document of this source.
79  TiXmlDocument &Get()
80  {
81  return *m_doc;
82  }
83 
84  /// Return the TinyXML document of this source.
85  const TiXmlDocument &Get() const
86  {
87  return *m_doc;
88  }
89 
90  /// Create a new XML data source based on the filename.
91  static DataSourceSharedPtr create(const std::string &fn)
92  {
93  return DataSourceSharedPtr(new XmlDataSource(fn));
94  }
95 
96  /// Create a new XML data source based on a TiXmlDocument.
97  static DataSourceSharedPtr create(TiXmlDocument &fn)
98  {
99  return DataSourceSharedPtr(new XmlDataSource(fn));
100  }
101 
102 private:
103  /// Internal TinyXML document storage.
104  TiXmlDocument *m_doc;
105  /// Boolean dictating whether document needs to be freed or not.
107 };
108 typedef boost::shared_ptr<XmlDataSource> XmlDataSourceSharedPtr;
109 
110 /**
111  * @class Simple class for writing XML hierarchical data using TinyXML.
112  */
113 class XmlTagWriter : public TagWriter
114 {
115 public:
116  /// Default constructor.
117  XmlTagWriter(TiXmlElement *elem) : m_El(elem) {}
118 
119  /// Add a child node.
120  virtual TagWriterSharedPtr AddChild(const std::string &name)
121  {
122  TiXmlElement *child = new TiXmlElement(name.c_str());
123  m_El->LinkEndChild(child);
124  return TagWriterSharedPtr(new XmlTagWriter(child));
125  }
126 
127  /// Set an attribute key/value pair on this tag.
128  virtual void SetAttr(const std::string &key, const std::string &val)
129  {
130  TiXmlElement *child = new TiXmlElement(key.c_str());
131  child->LinkEndChild(new TiXmlText(val.c_str()));
132  m_El->LinkEndChild(child);
133  }
134 
135 private:
136  /// Internal TinyXML document storage.
137  TiXmlElement *m_El;
138 };
139 typedef boost::shared_ptr<XmlTagWriter> XmlTagWriterSharedPtr;
140 
141 /**
142  * @class Class for operating on XML FLD files.
143  *
144  * This class is the default for Nektar++ output. It reads and writes one XML
145  * file per processor that represents the underlying field data. For serial
146  * output, the format of an XML file obeys the following structure:
147  *
148  * ```
149  * <NEKTAR>
150  * <Metadata>
151  * ...
152  * </Metadata>
153  * <ELEMENT FIELDS="..." ...> data1 </ELEMENT>
154  * <ELEMENT FIELDS="..." ...> data2 </ELEMENT>
155  * ...
156  * </NEKTAR>
157  * ```
158  *
159  * - Metadata is converted as key/value pairs in the `<Metadata>` tag.
160  * - There are one or more ELEMENT blocks, whose attributes correspond with
161  * the FieldDefinitions class variables.
162  * - Element data is stored as a base64-encoded zlib-compressed binary
163  * double-precision data using the functions from CompressData.
164  *
165  * In parallel, each process writes its contributions into an XML file of the
166  * form `P0000001.fld` (where 1 is replaced by the rank of the process) inside a
167  * directory with the desired output name. These files only include the
168  * `ELEMENT` data. Metadata are instead stored in a separate `Info.xml` file,
169  * which contains the Metadata and additional tags of the form
170  *
171  * `<Partition FileName="P0000000.fld"> ID list </Partition>`
172  *
173  * The ID list enumerates all element IDs on each partition's contribution. For
174  * large parallel jobs, this is used to avoid each process reading in every
175  * single partition in order to load field data.
176  */
177 class FieldIOXml : public FieldIO
178 {
179 public:
180  /// Creates an instance of this class
182  LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
183  {
185  sharedFilesystem);
186  }
187 
188  /// Name of class
189  LIB_UTILITIES_EXPORT static std::string className;
190 
193  bool sharedFilesystem);
194 
196  {
197  }
198 
200  DataSourceSharedPtr dataSource,
201  std::vector<FieldDefinitionsSharedPtr> &fielddefs,
202  bool expChild);
203 
205  DataSourceSharedPtr dataSource,
206  const std::vector<FieldDefinitionsSharedPtr> &fielddefs,
207  std::vector<std::vector<NekDouble> > &fielddata);
208 
210  const std::string &outfile,
211  const std::vector<std::string> fileNames,
212  std::vector<std::vector<unsigned int> > &elementList,
213  const FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap);
214 
216  const std::string &outname,
217  const std::vector<FieldDefinitionsSharedPtr> &fielddefs,
218  const FieldMetaDataMap &fieldmetadatamap);
219 
221  const std::string &inFile,
222  std::vector<std::string> &fileNames,
223  std::vector<std::vector<unsigned int> > &elementList,
224  FieldMetaDataMap &fieldmetadatamap);
225 
227  const std::string &infilename,
228  std::vector<FieldDefinitionsSharedPtr> &fielddefs,
229  std::vector<std::vector<NekDouble> > &fielddata =
231  FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
232  const Array<OneD, int> &ElementIDs = NullInt1DArray);
233 
234  /// Returns the class name.
235  inline virtual const std::string &GetClassName() const
236  {
237  return className;
238  }
239 
240 private:
241  LIB_UTILITIES_EXPORT virtual void v_Write(
242  const std::string &outFile,
243  std::vector<FieldDefinitionsSharedPtr> &fielddefs,
244  std::vector<std::vector<NekDouble> > &fielddata,
245  const FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
246  const bool backup = false);
247 
249  const std::string &filename, FieldMetaDataMap &fieldmetadatamap);
250 };
251 
252 }
253 }
254 #endif
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
bool m_needsFree
Boolean dictating whether document needs to be freed or not.
Definition: FieldIOXml.h:106
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
TiXmlElement * m_El
Internal TinyXML document storage.
Definition: FieldIOXml.h:137
static DataSourceSharedPtr create(TiXmlDocument &fn)
Create a new XML data source based on a TiXmlDocument.
Definition: FieldIOXml.h:97
virtual TagWriterSharedPtr AddChild(const std::string &name)
Add a child node.
Definition: FieldIOXml.h:120
FieldIOXml(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Default constructor.
Definition: FieldIOXml.cpp:62
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
boost::shared_ptr< XmlTagWriter > XmlTagWriterSharedPtr
Definition: FieldIOXml.h:139
TiXmlDocument & Get()
Return the TinyXML document of this source.
Definition: FieldIOXml.h:79
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
static std::string className
Name of class.
Definition: FieldIOXml.h:189
const TiXmlDocument & Get() const
Return the TinyXML document of this source.
Definition: FieldIOXml.h:85
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:54
boost::shared_ptr< DataSource > DataSourceSharedPtr
Definition: FieldIO.h:81
Base class for writing hierarchical data (XML or HDF5).
Definition: FieldIO.h:60
boost::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:55
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:502
static DataSourceSharedPtr create(const std::string &fn)
Create a new XML data source based on the filename.
Definition: FieldIOXml.h:91
#define LIB_UTILITIES_EXPORT
static std::vector< std::vector< NekDouble > > NullVectorNekDoubleVector
static FieldIOSharedPtr create(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Creates an instance of this class.
Definition: FieldIOXml.h:181
boost::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:309
void ImportFieldData(DataSourceSharedPtr dataSource, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata)
Import field data from a target file.
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
TiXmlDocument * m_doc
Internal TinyXML document storage.
Definition: FieldIOXml.h:104
XmlTagWriter(TiXmlElement *elem)
Default constructor.
Definition: FieldIOXml.h:117
virtual void SetAttr(const std::string &key, const std::string &val)
Set an attribute key/value pair on this tag.
Definition: FieldIOXml.h:128
~XmlDataSource()
Destructor cleans up memory usage.
Definition: FieldIOXml.h:70
XmlDataSource(const std::string &fn)
Constructor based on filename.
Definition: FieldIOXml.h:57
boost::shared_ptr< XmlDataSource > XmlDataSourceSharedPtr
Definition: FieldIOXml.h:108
Class for operating on Nektar++ input/output files.
Definition: FieldIO.h:224
virtual const std::string & GetClassName() const
Returns the class name.
Definition: FieldIOXml.h:235
boost::shared_ptr< TagWriter > TagWriterSharedPtr
Definition: FieldIO.h:71
void SetUpFieldMetaData(const std::string &outname, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, const FieldMetaDataMap &fieldmetadatamap)
Set up field meta data map.
Definition: FieldIOXml.cpp:690
void ImportFieldDefs(DataSourceSharedPtr dataSource, std::vector< FieldDefinitionsSharedPtr > &fielddefs, bool expChild)
Import field definitions from the target file.
Definition: FieldIOXml.cpp:762
static Array< OneD, int > NullInt1DArray
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:55
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
XmlDataSource(TiXmlDocument &doc)
Default constructor.
Definition: FieldIOXml.h:54