10 #include "pugixml.hpp"
11 #include "xdmf_utils.h"
12 #include <Eigen/Dense>
13 #include <boost/algorithm/string.hpp>
14 #include <boost/lexical_cast.hpp>
15 #include <dolfinx/common/IndexMap.h>
16 #include <dolfinx/mesh/Geometry.h>
17 #include <dolfinx/mesh/Mesh.h>
18 #include <dolfinx/mesh/Topology.h>
26 std::vector<T>
get_dataset(MPI_Comm comm,
const pugi::xml_node& dataset_node,
28 std::array<std::int64_t, 2> range = {{0, 0}})
39 pugi::xml_attribute format_attr = dataset_node.attribute(
"Format");
44 const std::vector<std::int64_t> shape_xml
45 = xdmf_utils::get_dataset_shape(dataset_node);
47 const std::string format = format_attr.as_string();
48 std::vector<T> data_vector;
56 pugi::xml_node data_node = dataset_node.first_child();
58 std::string data_str = data_node.value();
61 std::vector<boost::iterator_range<std::string::iterator>> data_vector_str;
62 boost::split(data_vector_str, data_str, boost::is_any_of(
" \n"));
65 data_vector.reserve(data_vector_str.size());
66 for (
auto& v : data_vector_str)
68 if (v.begin() != v.end())
69 data_vector.push_back(
70 boost::lexical_cast<T>(boost::copy_range<std::string>(v)));
74 else if (format ==
"HDF")
77 auto paths = xdmf_utils::get_hdf5_paths(dataset_node);
80 const std::vector<std::int64_t> shape_hdf5
85 assert(!shape_hdf5.empty());
86 assert(shape_hdf5[0] != 0);
95 if (range[0] == 0 and range[1] == 0)
97 if (shape_xml == shape_hdf5)
102 else if (!shape_xml.empty() and shape_hdf5.size() == 1)
105 std::int64_t d = std::accumulate(shape_xml.begin(), shape_xml.end(), 1,
106 std::multiplies<std::int64_t>());
109 if (d * shape_xml[0] != shape_hdf5[0])
111 throw std::runtime_error(
"Data size in XDMF/XML and size of HDF5 "
112 "dataset are inconsistent");
123 throw std::runtime_error(
124 "This combination of array shapes in XDMF and HDF5 "
130 data_vector = HDF5Interface::read_dataset<T>(h5_id, paths[1], range);
133 throw std::runtime_error(
"Storage format \"" + format +
"\" is unknown");
136 if (shape_xml.empty())
138 std::int64_t size = 1;
139 for (
auto dim : shape_xml)
142 std::int64_t size_global = 0;
143 const std::int64_t size_local = data_vector.size();
144 MPI_Allreduce(&size_local, &size_global, 1, MPI_INT64_T, MPI_SUM, comm);
145 if (size != size_global)
147 throw std::runtime_error(
148 "Data sizes in attribute and size of data read are inconsistent");