DOLFIN
DOLFIN C++ interface
XMLMeshValueCollection.h
1 // Copyright (C) 2011 Anders Logg
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 // First added: 2011-06-30
19 // Last changed: 2011-11-15
20 
21 #ifndef __XML_MESH_VALUE_COLLECTION_H
22 #define __XML_MESH_VALUE_COLLECTION_H
23 
24 #include <string>
25 #include <dolfin/mesh/MeshValueCollection.h>
26 #include "pugixml.hpp"
27 #include "xmlutils.h"
28 #include "XMLMesh.h"
29 
30 namespace dolfin
31 {
32 
34 
36  {
37  public:
38 
40  template <typename T>
41  static void read(MeshValueCollection<T>& mesh_value_collection,
42  const std::string type,
43  const pugi::xml_node xml_node);
44 
46  template<typename T>
47  static void write(const MeshValueCollection<T>& mesh_value_collection,
48  const std::string type,
49  pugi::xml_node xml_node);
50 
51  };
52 
53  //---------------------------------------------------------------------------
54  template <typename T>
55  void
57  const std::string type,
58  const pugi::xml_node xml_node)
59  {
60  // Get node
61  const pugi::xml_node mvc_node
62  = xmlutils::get_node(xml_node, "mesh_value_collection");
63  dolfin_assert(mvc_node);
64 
65  // Get attributes
66  const std::string name = mvc_node.attribute("name").value();
67  const std::string type_file = mvc_node.attribute("type").value();
68  const std::size_t dim = mvc_node.attribute("dim").as_uint();
69 
70  // Attach name to mesh value collection object
71  mesh_value_collection.rename(name, "a mesh value collection");
72 
73  // Set dimension
74  mesh_value_collection.init(dim);
75 
76  // Check that types match
77  if (type != type_file)
78  {
79  dolfin_error("XMLMeshValueCollection.h",
80  "read mesh value collection from XML file",
81  "Type mismatch, found \"%s\" but expecting \"%s\"",
82  type_file.c_str(), type.c_str());
83  }
84 
85  // Clear old values
86  mesh_value_collection.clear();
87 
88  // Choose data type
89  if (type == "uint")
90  {
91  pugi::xml_node_iterator it;
92  for (it = mvc_node.begin(); it != mvc_node.end(); ++it)
93  {
94  const std::size_t cell_index = it->attribute("cell_index").as_uint();
95  const std::size_t local_entity
96  = it->attribute("local_entity").as_uint();
97  const std::size_t value = it->attribute("value").as_uint();
98  mesh_value_collection.set_value(cell_index, local_entity, value);
99  }
100  }
101  else if (type == "int")
102  {
103  pugi::xml_node_iterator it;
104  for (it = mvc_node.begin(); it != mvc_node.end(); ++it)
105  {
106  const std::size_t cell_index = it->attribute("cell_index").as_uint();
107  const std::size_t local_entity
108  = it->attribute("local_entity").as_uint();
109  const int value = it->attribute("value").as_int();
110  mesh_value_collection.set_value(cell_index, local_entity, value);
111  }
112  }
113  else if (type == "double")
114  {
115  pugi::xml_node_iterator it;
116  for (it = mvc_node.begin(); it != mvc_node.end(); ++it)
117  {
118  const std::size_t cell_index = it->attribute("cell_index").as_uint();
119  const std::size_t local_entity
120  = it->attribute("local_entity").as_uint();
121  const double value = it->attribute("value").as_double();
122  mesh_value_collection.set_value(cell_index, local_entity, value);
123  }
124  }
125  else if (type == "bool")
126  {
127  pugi::xml_node_iterator it;
128  for (it = mvc_node.begin(); it != mvc_node.end(); ++it)
129  {
130  const std::size_t cell_index = it->attribute("cell_index").as_uint();
131  const std::size_t local_entity
132  = it->attribute("local_entity").as_uint();
133  const bool value = it->attribute("value").as_bool();
134  mesh_value_collection.set_value(cell_index, local_entity, value);
135  }
136  }
137  else
138  {
139  dolfin_error("XMLValueCollection.h",
140  "read mesh value collection from XML file",
141  "Unhandled value type \"%s\"", type.c_str());
142  }
143  }
144  //---------------------------------------------------------------------------
145  template<typename T>
147  mesh_value_collection,
148  const std::string type,
149  pugi::xml_node xml_node)
150  {
151  not_working_in_parallel("Writing XML MeshValueCollection");
152 
153  // Add mesh function node and attributes
154  pugi::xml_node mf_node = xml_node.append_child("mesh_value_collection");
155  mf_node.append_attribute("name") = mesh_value_collection.name().c_str();
156  mf_node.append_attribute("type") = type.c_str();
157  mf_node.append_attribute("dim")
158  = (unsigned int)mesh_value_collection.dim();
159  mf_node.append_attribute("size")
160  = (unsigned int) mesh_value_collection.size();
161 
162  // Add data
163  const std::map<std::pair<std::size_t, std::size_t>, T>&
164  values = mesh_value_collection.values();
165  typename std::map<std::pair<std::size_t,
166  std::size_t>, T>::const_iterator it;
167  for (it = values.begin(); it != values.end(); ++it)
168  {
169  pugi::xml_node entity_node = mf_node.append_child("value");
170  entity_node.append_attribute("cell_index")
171  = (unsigned int) it->first.first;
172  entity_node.append_attribute("local_entity")
173  = (unsigned int) it->first.second;
174  entity_node.append_attribute("value")
175  = std::to_string(it->second).c_str();
176  }
177  }
178  //---------------------------------------------------------------------------
179 
180 }
181 #endif
I/O of XML representation of a MeshValueCollection.
Definition: XMLMeshValueCollection.h:35
std::map< std::pair< std::size_t, std::size_t >, T > & values()
Definition: MeshValueCollection.h:521
static void write(const MeshValueCollection< T > &mesh_value_collection, const std::string type, pugi::xml_node xml_node)
Write mesh value collection to XML file.
Definition: XMLMeshValueCollection.h:146
std::string name() const
Return name.
Definition: Variable.cpp:71
void clear()
Clear all values.
Definition: MeshValueCollection.h:534
Definition: adapt.h:29
void rename(const std::string name, const std::string label)
Rename variable.
Definition: Variable.cpp:65
std::size_t size() const
Definition: MeshValueCollection.h:402
bool set_value(std::size_t cell_index, std::size_t local_entity, const T &value)
Definition: MeshValueCollection.h:415
std::size_t dim() const
Definition: MeshValueCollection.h:389
void init(std::shared_ptr< const Mesh > mesh, std::size_t dim)
Definition: MeshValueCollection.h:371
static void read(MeshValueCollection< T > &mesh_value_collection, const std::string type, const pugi::xml_node xml_node)
Read mesh value collection from XML file.
Definition: XMLMeshValueCollection.h:56
Definition: GenericFile.h:38
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition: log.cpp:129
void not_working_in_parallel(std::string what)
Definition: log.cpp:205