DOLFIN
DOLFIN C++ interface
XMLArray.h
1 // Copyright (C) 2011 Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Modified by Anders Logg 2011
19 //
20 // First added: 2006-07-02
21 // Last changed: 2011-11-14
22 
23 #ifndef __XMLARRAY_H
24 #define __XMLARRAY_H
25 
26 #include <ostream>
27 #include <string>
28 #include <vector>
29 #include <boost/format.hpp>
30 #include "dolfin/common/Array.h"
31 #include "dolfin/log/log.h"
32 #include "pugixml.hpp"
33 
34 
35 // NOTE: Do not include this file in any header files. Otherwise, it
36 // will expose Boost in the DOLFIN public interface.
37 
38 namespace pugi
39 {
40  class xml_node;
41 }
42 
43 namespace dolfin
44 {
45 
47 
48  class XMLArray
49  {
50  public:
51 
53  template<typename T>
54  static void read(std::vector<T>& x, const pugi::xml_node xml_dolfin);
55 
57  template<typename T>
58  static void write(const std::vector<T>& x, const std::string type,
59  pugi::xml_node xml_node);
60 
61  };
62 
63  //---------------------------------------------------------------------------
64  template<typename T>
65  void XMLArray::read(std::vector<T>& x, const pugi::xml_node xml_node)
66  {
67  // Check that we have a XML Array
68  const pugi::xml_node array = xml_node.child("array");
69  if (!array)
70  {
71  dolfin_error("XMLArray.h",
72  "read array from XML file",
73  "Unable to find <array> tag in XML file");
74  }
75 
76  // Get size and type
77  const std::size_t size = array.attribute("size").as_uint();
78  const std::string type = array.attribute("type").value();
79  if (type != "double")
80  {
81  dolfin_error("XMLArray.h",
82  "read array from XML file",
83  "XML I/O of Array objects only supported when the value type is 'double'");
84  }
85 
86  // Iterate over array entries
87  x.resize(size);
88  Array<std::size_t> indices(size);
89  for (pugi::xml_node_iterator it = array.begin(); it != array.end(); ++it)
90  {
91  const std::size_t index = it->attribute("index").as_uint();
92  const double value = it->attribute("value").as_double();
93  dolfin_assert(index < size);
94  indices[index] = index;
95  x[index] = value;
96  }
97  }
98  //---------------------------------------------------------------------------
99  template<typename T>
100  void XMLArray::write(const std::vector<T>& x, const std::string type,
101  pugi::xml_node xml_node)
102  {
103  // Add array node
104  pugi::xml_node array_node = xml_node.append_child("array");
105 
106  // Add attributes
107  const std::size_t size = x.size();
108  array_node.append_attribute("type") = type.c_str();
109  array_node.append_attribute("size") = (unsigned int) size;
110 
111  // Add data
112  for (std::size_t i = 0; i < size; ++i)
113  {
114  pugi::xml_node element_node = array_node.append_child("element");
115  element_node.append_attribute("index") = (unsigned int) i;
116  // NOTE: Casting to a string to avoid loss of precision when
117  // pugixml performs double-to-char conversion
118  element_node.append_attribute("value")
119  = boost::str(boost::format("%.15e") % x[i]).c_str();
120  }
121  }
122  //---------------------------------------------------------------------------
123 }
124 
125 #endif
Definition: adapt.h:29
Definition: Array.h:41
I/O of array data in XML format.
Definition: XMLArray.h:48
std::size_t size() const
Return size of array.
Definition: Array.h:81
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition: log.cpp:129
Definition: VTKFile.h:27