23 #ifndef __XML_MESH_FUNCTION_H 24 #define __XML_MESH_FUNCTION_H 31 #include "pugixml.hpp" 32 #include <dolfin/mesh/LocalMeshValueCollection.h> 33 #include <dolfin/mesh/Mesh.h> 34 #include <dolfin/mesh/MeshFunction.h> 35 #include <dolfin/mesh/MeshValueCollection.h> 37 #include "XMLMeshValueCollection.h" 39 #include <dolfin/common/Timer.h> 53 const pugi::xml_node xml_mesh);
58 const std::string type,
const pugi::xml_node xml_mesh);
63 const std::string type, pugi::xml_node xml_node,
64 bool write_mesh=
true);
71 const std::string type,
72 const pugi::xml_node xml_mesh)
74 bool new_format =
false;
75 pugi::xml_node xml_meshfunction;
76 if (std::string(xml_mesh.name()) ==
"mesh_function")
80 xml_meshfunction = xml_mesh;
85 std::string tag_name =
"mesh_function";
88 if (xml_mesh.child(
"meshfunction"))
90 warning(
"The XML tag <meshfunction> has been changed to <mesh_function>. " 91 "I'll be nice and read your XML data anyway, for now, but you will " 92 "need to update your XML files (a simple search and replace) to use " 93 "future versions of DOLFIN.");
94 tag_name =
"meshfunction";
98 xml_meshfunction = xml_mesh.child(tag_name.c_str());
101 if (!xml_meshfunction)
102 std::cout <<
"Not a DOLFIN MeshFunction XML file." << std::endl;
104 if (xml_meshfunction.attributes_begin() == xml_meshfunction.attributes_end())
113 XMLMeshValueCollection::read<T>(mesh_value_collection, type,
118 mesh_function = mesh_value_collection;
119 mesh_function.
rename(mesh_value_collection.
name(),
120 mesh_value_collection.
label());
125 const std::string file_data_type = xml_meshfunction.attribute(
"type").value();
126 const std::size_t dim = xml_meshfunction.attribute(
"dim").as_uint();
127 const std::size_t size = xml_meshfunction.attribute(
"size").as_uint();
130 if (type != file_data_type)
133 "read mesh function from XML file",
134 "Type mismatch reading XML MeshFunction. MeshFunction type is \"%s\", but file type is \"%s\"",
135 type.c_str(), file_data_type.c_str());
139 mesh_function.
init(dim, size);
144 for (pugi::xml_node_iterator it = xml_meshfunction.begin();
145 it != xml_meshfunction.end(); ++it)
147 const std::size_t index = it->attribute(
"index").as_uint();
148 dolfin_assert(index < size);
149 mesh_function[index] = it->attribute(
"value").as_uint();
152 else if (type ==
"int")
154 for (pugi::xml_node_iterator it = xml_meshfunction.begin();
155 it != xml_meshfunction.end(); ++it)
157 const std::size_t index = it->attribute(
"index").as_uint();
158 dolfin_assert(index < size);
159 mesh_function[index] = it->attribute(
"value").as_int();
162 else if (type ==
"double")
164 for (pugi::xml_node_iterator it = xml_meshfunction.begin();
165 it != xml_meshfunction.end(); ++it)
167 const std::size_t index = it->attribute(
"index").as_uint();
168 dolfin_assert(index < size);
169 mesh_function[index] = it->attribute(
"value").as_double();
172 else if (type ==
"bool")
174 for (pugi::xml_node_iterator it = xml_meshfunction.begin();
175 it != xml_meshfunction.end(); ++it)
177 const std::size_t index = it->attribute(
"index").as_uint();
178 dolfin_assert(index < size);
179 mesh_function[index] = it->attribute(
"value").as_bool();
185 "read mesh function from XML file",
186 "Unknown value type (\"%s\")", type.c_str());
191 template <
typename T>
193 const std::string type,
194 const pugi::xml_node xml_mesh)
197 std::string tag_name(
"mesh_function");
198 if (xml_mesh.child(
"meshfunction"))
200 warning(
"The XML tag <meshfunction> has been changed to <mesh_function>. " 201 "I'll be nice and read your XML data anyway, for now, but you will " 202 "need to update your XML files (a simple search and replace) to use " 203 "future versions of DOLFIN.");
204 tag_name =
"meshfunction";
208 const pugi::xml_node xml_meshfunction = xml_mesh.child(tag_name.c_str());
209 if (!xml_meshfunction)
210 std::cout <<
"Not a DOLFIN MeshFunction XML file." << std::endl;
213 if (xml_meshfunction.attributes_begin() == xml_meshfunction.attributes_end())
216 XMLMeshValueCollection::read<T>(mesh_value_collection, type,
222 "read mesh function from XML file",
223 "Cannot read old-style MeshFunction XML files as a MeshValueCollection");
229 const std::string type, pugi::xml_node xml_node,
239 pugi::xml_node mf_node = xml_node.append_child(
"mesh_function");
243 mesh_value_collection.
rename(mesh_function.
name(), mesh_function.
label());
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 warning(std::string msg,...)
Print warning.
Definition: log.cpp:115
void init(std::size_t dim)
Definition: MeshFunction.h:572
void rename(const std::string name, const std::string label)
Rename variable.
Definition: Variable.cpp:65
static void write(const Mesh &mesh, pugi::xml_node mesh_node)
Write mesh to XML.
Definition: XMLMesh.cpp:73
static void read(MeshFunction< T > &mesh_function, const std::string type, const pugi::xml_node xml_mesh)
Read XML MeshFunction.
Definition: XMLMeshFunction.h:70
std::string label() const
Return label (description)
Definition: Variable.cpp:76
I/O of XML representation of MeshFunction.
Definition: XMLMeshFunction.h:46
Definition: GenericFile.h:38
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition: log.cpp:129
std::shared_ptr< const Mesh > mesh() const
Definition: MeshFunction.h:491
void not_working_in_parallel(std::string what)
Definition: log.cpp:205
static void write(const MeshFunction< T > &mesh_function, const std::string type, pugi::xml_node xml_node, bool write_mesh=true)
Write the XML file.
Definition: XMLMeshFunction.h:228