9 #include "xdmf_utils.h"
10 #include <boost/algorithm/string.hpp>
11 #include <boost/lexical_cast.hpp>
12 #include <dolfinx/common/IndexMap.h>
13 #include <dolfinx/mesh/Geometry.h>
14 #include <dolfinx/mesh/Mesh.h>
15 #include <dolfinx/mesh/Topology.h>
16 #include <pugixml.hpp>
24 std::vector<T>
get_dataset(MPI_Comm comm,
const pugi::xml_node& dataset_node,
26 std::array<std::int64_t, 2> range = {{0, 0}})
37 pugi::xml_attribute format_attr = dataset_node.attribute(
"Format");
42 const std::vector shape_xml = xdmf_utils::get_dataset_shape(dataset_node);
44 const std::string format = format_attr.as_string();
45 std::vector<T> data_vector;
53 pugi::xml_node data_node = dataset_node.first_child();
55 std::string data_str = data_node.value();
58 std::vector<boost::iterator_range<std::string::iterator>> data_vector_str;
59 boost::split(data_vector_str, data_str, boost::is_any_of(
" \n"));
62 data_vector.reserve(data_vector_str.size());
63 for (
auto& v : data_vector_str)
65 if (v.begin() != v.end())
66 data_vector.push_back(
67 boost::lexical_cast<T>(boost::copy_range<std::string>(v)));
71 else if (format ==
"HDF")
74 auto paths = xdmf_utils::get_hdf5_paths(dataset_node);
77 const std::vector shape_hdf5
82 assert(!shape_hdf5.empty());
83 assert(shape_hdf5[0] != 0);
92 if (range[0] == 0 and range[1] == 0)
94 if (shape_xml == shape_hdf5)
99 else if (!shape_xml.empty() and shape_hdf5.size() == 1)
102 std::int64_t d = std::reduce(shape_xml.begin(), shape_xml.end(),
103 std::int64_t(1), std::multiplies{});
106 if (d * shape_xml[0] != shape_hdf5[0])
108 throw std::runtime_error(
"Data size in XDMF/XML and size of HDF5 "
109 "dataset are inconsistent");
120 throw std::runtime_error(
121 "This combination of array shapes in XDMF and HDF5 "
127 data_vector = HDF5Interface::read_dataset<T>(h5_id, paths[1], range);
130 throw std::runtime_error(
"Storage format \"" + format +
"\" is unknown");
133 if (shape_xml.empty())
135 std::int64_t
size = 1;
136 for (
auto dim : shape_xml)
139 std::int64_t size_global = 0;
140 const std::int64_t size_local = data_vector.size();
141 MPI_Allreduce(&size_local, &size_global, 1, MPI_INT64_T, MPI_SUM, comm);
142 if (size != size_global)
144 throw std::runtime_error(
145 "Data sizes in attribute and size of data read are inconsistent");
static std::vector< std::int64_t > get_dataset_shape(const hid_t handle, const std::string &dataset_path)
Get dataset shape (size of each dimension)
Definition: HDF5Interface.cpp:203
int size(MPI_Comm comm)
Return size of the group (number of processes) associated with the communicator.
Definition: MPI.cpp:84
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition: MPI.cpp:76
constexpr std::array< std::int64_t, 2 > local_range(int rank, std::int64_t N, int size)
Return local range for the calling process, partitioning the global [0, N - 1] range across all ranks...
Definition: MPI.h:83
Low-level methods for reading XDMF files.
Definition: xdmf_read.h:20
std::vector< T > get_dataset(MPI_Comm comm, const pugi::xml_node &dataset_node, const hid_t h5_id, std::array< std::int64_t, 2 > range={{0, 0}})
Return data associated with a data set node.
Definition: xdmf_read.h:24