9#include "HDF5Interface.h"
11#include <basix/mdspan.hpp>
12#include <boost/algorithm/string.hpp>
13#include <boost/lexical_cast.hpp>
14#include <dolfinx/common/MPI.h>
15#include <dolfinx/common/types.h>
31template <dolfinx::scalar T, std::
floating_po
int U>
37template <std::
floating_po
int T>
44template <std::
floating_po
int T>
50namespace io::xdmf_utils
55std::pair<std::string, int> get_cell_type(
const pugi::xml_node& topology_node);
59std::array<std::string, 2> get_hdf5_paths(
const pugi::xml_node& dataitem_node);
62get_hdf5_filename(
const std::filesystem::path& xdmf_filename);
65std::vector<std::int64_t> get_dataset_shape(
const pugi::xml_node& dataset_node);
68std::int64_t get_num_cells(
const pugi::xml_node& topology_node);
71std::string vtk_cell_type_str(
mesh::CellType cell_type,
int num_nodes);
75void add_data_item(pugi::xml_node& xml_node, hid_t h5_id,
76 std::string_view h5_path, std::span<const T> x,
77 std::int64_t offset,
const std::vector<std::int64_t>& shape,
78 std::string_view number_type,
bool use_mpi_io)
82 pugi::xml_node data_item_node = xml_node.append_child(
"DataItem");
83 assert(data_item_node);
88 std::format_to(std::back_inserter(dims),
"{} ", d);
90 data_item_node.append_attribute(
"Dimensions") = dims.c_str();
94 if (!number_type.empty())
96 data_item_node.append_attribute(
"NumberType")
97 = std::string(number_type).c_str();
103 data_item_node.append_attribute(
"Format") =
"XML";
104 assert(shape.size() == 2);
106 for (std::size_t i = 0; i < x.size(); ++i)
108 if constexpr (std::floating_point<T>)
109 std::format_to(std::back_inserter(s),
"{:.16}", x.data()[i]);
111 std::format_to(std::back_inserter(s),
"{}", x.data()[i]);
112 s += ((i + 1) % shape[1] == 0 and shape[1] != 0) ?
'\n' :
' ';
115 data_item_node.append_child(pugi::node_pcdata).set_value(s.c_str());
119 data_item_node.append_attribute(
"Format") =
"HDF";
122 const std::filesystem::path p = io::hdf5::get_filename(h5_id);
123 const std::filesystem::path filename = p.filename().c_str();
126 const std::string xdmf_path
127 = std::format(
"{}:{}", filename.string(), h5_path);
128 data_item_node.append_child(pugi::node_pcdata).set_value(xdmf_path.c_str());
131 std::int64_t local_shape0 = std::reduce(
132 std::next(shape.begin()), shape.end(), x.size(), std::divides{});
134 const std::array
local_range{offset, offset + local_shape0};
135 io::hdf5::write_dataset(h5_id, h5_path, x.data(), local_range, shape,
152std::vector<T> get_dataset(MPI_Comm comm,
const pugi::xml_node& dataset_node,
154 std::array<std::int64_t, 2> range = {0, 0})
164 assert(dataset_node);
165 pugi::xml_attribute format_attr = dataset_node.attribute(
"Format");
170 const std::vector shape_xml = xdmf_utils::get_dataset_shape(dataset_node);
172 const std::string format = format_attr.as_string();
173 std::vector<T> data_vector;
181 pugi::xml_node data_node = dataset_node.first_child();
183 std::string data_str = data_node.value();
186 std::vector<boost::iterator_range<std::string::iterator>> data_vector_str;
187 boost::split(data_vector_str, data_str, boost::is_any_of(
" \n"));
190 data_vector.reserve(data_vector_str.size());
191 for (
auto& v : data_vector_str)
193 if (v.begin() != v.end())
194 data_vector.push_back(
195 boost::lexical_cast<T>(boost::copy_range<std::string>(v)));
199 else if (format ==
"HDF")
202 auto paths = xdmf_utils::get_hdf5_paths(dataset_node);
205 const std::vector shape_hdf5 = io::hdf5::get_dataset_shape(h5_id, paths[1]);
209 assert(!shape_hdf5.empty());
210 assert(shape_hdf5[0] != 0);
219 if (range[0] == 0 and range[1] == 0)
221 if (shape_xml == shape_hdf5)
226 else if (!shape_xml.empty() and shape_hdf5.size() == 1)
229 std::int64_t d = std::reduce(shape_xml.begin(), shape_xml.end(),
230 std::int64_t(1), std::multiplies{});
233 if (d * shape_xml[0] != shape_hdf5[0])
235 throw std::runtime_error(
"Data size in XDMF/XML and size of HDF5 "
236 "dataset are inconsistent");
247 throw std::runtime_error(
"This combination of array shapes in XDMF and "
248 "HDF5 is not supported");
253 if (hid_t dset_id = io::hdf5::open_dataset(h5_id, paths[1]);
254 dset_id == H5I_INVALID_HID)
255 throw std::runtime_error(
"Failed to open HDF5 global dataset.");
258 data_vector = io::hdf5::read_dataset<T>(dset_id, range,
true);
259 if (herr_t err = H5Dclose(dset_id); err < 0)
260 throw std::runtime_error(
"Failed to close HDF5 global dataset.");
264 throw std::runtime_error(
265 std::format(
"Storage format \"{}\" is unknown", format));
268 if (shape_xml.empty())
270 std::int64_t
size = 1;
271 for (
auto dim : shape_xml)
274 std::int64_t size_global = 0;
275 const std::int64_t size_local = data_vector.size();
276 MPI_Allreduce(&size_local, &size_global, 1, MPI_INT64_T, MPI_SUM, comm);
277 if (size != size_global)
279 throw std::runtime_error(
280 "Data sizes in attribute and size of data read are inconsistent");
Definition CoordinateElement.h:38
Definition ElementDofLayout.h:31
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
Topology stores the topology of a mesh, consisting of mesh entities and connectivity (incidence relat...
Definition Topology.h:49
int size(MPI_Comm comm)
Definition MPI.cpp:72
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:64
constexpr std::array< std::int64_t, 2 > local_range(int index, std::int64_t N, int size)
Partition a global range [0, N - 1] across callers into non-overlapping sub-partitions of almost equa...
Definition local_range.h:26
Finite element method functionality.
Definition assemble_expression_impl.h:23
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
CellType
Cell type identifier.
Definition cell_types.h:22
Top-level namespace.
Definition defines.h:12