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
44{
45
46/**
47 * @class Class encapsulating simple XML data source using TinyXML.
48 */
50{
51public:
52 /// Default constructor.
53 XmlDataSource(TiXmlDocument &doc) : m_doc(&doc), m_needsFree(false)
54 {
55 }
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
103private:
104 /// Internal TinyXML document storage.
105 TiXmlDocument *m_doc;
106 /// Boolean dictating whether document needs to be freed or not.
108};
109typedef std::shared_ptr<XmlDataSource> XmlDataSourceSharedPtr;
110
111/**
112 * @class Simple class for writing XML hierarchical data using TinyXML.
113 */
115{
116public:
117 /// Default constructor.
118 XmlTagWriter(TiXmlElement *elem) : m_El(elem)
119 {
120 }
121
122protected:
123 /// Add a child node.
124 TagWriterSharedPtr v_AddChild(const std::string &name) override
125 {
126 TiXmlElement *child = new TiXmlElement(name.c_str());
127 m_El->LinkEndChild(child);
128 return TagWriterSharedPtr(new XmlTagWriter(child));
129 }
130
131 /// Set an attribute key/value pair on this tag.
132 void v_SetAttr(const std::string &key, const std::string &val) override
133 {
134 if (boost::starts_with(key, "XML_"))
135 {
136 // Auto-expand XML parameters.
137 std::string elmtName = key.substr(4);
138 TiXmlElement *child = new TiXmlElement(elmtName.c_str());
139
140 // Parse string we're given
141 TiXmlDocument doc;
142 doc.Parse(val.c_str());
143
144 TiXmlElement *e = doc.FirstChildElement();
145
146 while (e)
147 {
148 child->LinkEndChild(e->Clone());
149 e = e->NextSiblingElement();
150 }
151
152 m_El->LinkEndChild(child);
153 }
154 else
155 {
156 TiXmlElement *child = new TiXmlElement(key.c_str());
157 child->LinkEndChild(new TiXmlText(val.c_str()));
158 m_El->LinkEndChild(child);
159 }
160 }
161
162private:
163 /// Internal TinyXML document storage.
164 TiXmlElement *m_El;
165};
166typedef std::shared_ptr<XmlTagWriter> XmlTagWriterSharedPtr;
167
168/**
169 * @class Class for operating on XML FLD files.
170 *
171 * This class is the default for Nektar++ output. It reads and writes one XML
172 * file per processor that represents the underlying field data. For serial
173 * output, the format of an XML file obeys the following structure:
174 *
175 * ```
176 * <NEKTAR>
177 * <Metadata>
178 * ...
179 * </Metadata>
180 * <ELEMENT FIELDS="..." ...> data1 </ELEMENT>
181 * <ELEMENT FIELDS="..." ...> data2 </ELEMENT>
182 * ...
183 * </NEKTAR>
184 * ```
185 *
186 * - Metadata is converted as key/value pairs in the `<Metadata>` tag.
187 * - There are one or more ELEMENT blocks, whose attributes correspond with
188 * the FieldDefinitions class variables.
189 * - Element data is stored as a base64-encoded zlib-compressed binary
190 * double-precision data using the functions from CompressData.
191 *
192 * In parallel, each process writes its contributions into an XML file of the
193 * form `P0000001.fld` (where 1 is replaced by the rank of the process) inside a
194 * directory with the desired output name. These files only include the
195 * `ELEMENT` data. Metadata are instead stored in a separate `Info.xml` file,
196 * which contains the Metadata and additional tags of the form
197 *
198 * `<Partition FileName="P0000000.fld"> ID list </Partition>`
199 *
200 * The ID list enumerates all element IDs on each partition's contribution. For
201 * large parallel jobs, this is used to avoid each process reading in every
202 * single partition in order to load field data.
203 */
204class FieldIOXml : public FieldIO
205{
206public:
207 /// Creates an instance of this class
209 LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
210 {
212 sharedFilesystem);
213 }
214
215 /// Name of class
216 LIB_UTILITIES_EXPORT static std::string className;
217
219 bool sharedFilesystem);
220
222 {
223 }
224
226 DataSourceSharedPtr dataSource,
227 std::vector<FieldDefinitionsSharedPtr> &fielddefs, bool expChild);
228
230 DataSourceSharedPtr dataSource,
231 const std::vector<FieldDefinitionsSharedPtr> &fielddefs,
232 std::vector<std::vector<NekDouble>> &fielddata);
233
235 const std::string &outfile, 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, std::vector<std::string> &fileNames,
246 std::vector<std::vector<unsigned int>> &elementList,
247 FieldMetaDataMap &fieldmetadatamap);
248
249protected:
251 const std::string &outFile,
252 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
253 std::vector<std::vector<NekDouble>> &fielddata,
254 const FieldMetaDataMap &fieldinfomap = NullFieldMetaDataMap,
255 const bool backup = false) override;
256
258 const std::string &infilename,
259 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
260 std::vector<std::vector<NekDouble>> &fielddata =
263 const Array<OneD, int> &ElementIDs = NullInt1DArray) override;
264
266 v_ImportFieldMetaData(const std::string &filename,
267 FieldMetaDataMap &fieldmetadatamap) override;
268
269 const std::string &v_GetClassName() const override;
270};
271
272} // namespace Nektar::LibUtilities
273#endif
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
#define LIB_UTILITIES_EXPORT
Class for operating on Nektar++ input/output files.
Definition: FieldIO.h:227
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:419
void ImportFieldData(DataSourceSharedPtr dataSource, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata)
Import field data from a target file.
const std::string & v_GetClassName() const override
Returns the class name.
Definition: FieldIOXml.cpp:634
void ImportFieldDefs(DataSourceSharedPtr dataSource, std::vector< FieldDefinitionsSharedPtr > &fielddefs, bool expChild)
Import field definitions from the target file.
Definition: FieldIOXml.cpp:728
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:372
static FieldIOSharedPtr create(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Creates an instance of this class.
Definition: FieldIOXml.h:208
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:591
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:490
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:83
FieldIOXml(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Default constructor.
Definition: FieldIOXml.cpp:58
void SetUpFieldMetaData(const std::string &outname, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, const FieldMetaDataMap &fieldmetadatamap)
Set up field meta data map.
Definition: FieldIOXml.cpp:652
static std::string className
Name of class.
Definition: FieldIOXml.h:216
Base class for writing hierarchical data (XML or HDF5).
Definition: FieldIO.h:57
TiXmlDocument * m_doc
Internal TinyXML document storage.
Definition: FieldIOXml.h:105
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
const TiXmlDocument & Get() const
Return the TinyXML document of this source.
Definition: FieldIOXml.h:86
XmlDataSource(const std::string &fn)
Constructor based on filename.
Definition: FieldIOXml.h:58
bool m_needsFree
Boolean dictating whether document needs to be freed or not.
Definition: FieldIOXml.h:107
TiXmlDocument & Get()
Return the TinyXML document of this source.
Definition: FieldIOXml.h:80
XmlDataSource(TiXmlDocument &doc)
Default constructor.
Definition: FieldIOXml.h:53
XmlTagWriter(TiXmlElement *elem)
Default constructor.
Definition: FieldIOXml.h:118
void v_SetAttr(const std::string &key, const std::string &val) override
Set an attribute key/value pair on this tag.
Definition: FieldIOXml.h:132
TiXmlElement * m_El
Internal TinyXML document storage.
Definition: FieldIOXml.h:164
TagWriterSharedPtr v_AddChild(const std::string &name) override
Add a child node.
Definition: FieldIOXml.h:124
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:78
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:322
std::shared_ptr< DataSource > DataSourceSharedPtr
Definition: FieldIO.h:88
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:50
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:51
std::shared_ptr< XmlDataSource > XmlDataSourceSharedPtr
Definition: FieldIOXml.h:109
static std::vector< std::vector< NekDouble > > NullVectorNekDoubleVector
std::shared_ptr< XmlTagWriter > XmlTagWriterSharedPtr
Definition: FieldIOXml.h:166
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:55
static Array< OneD, int > NullInt1DArray