10#include "HDF5Interface.h"
13#include <basix/cell.h>
14#include <basix/finite-element.h>
15#include <basix/mdspan.hpp>
20#include <dolfinx/common/IndexMap.h>
21#include <dolfinx/common/MPI.h>
22#include <dolfinx/common/log.h>
23#include <dolfinx/fem/CoordinateElement.h>
24#include <dolfinx/graph/partition.h>
25#include <dolfinx/io/cells.h>
26#include <dolfinx/mesh/Mesh.h>
27#include <dolfinx/mesh/Topology.h>
28#include <dolfinx/mesh/cell_types.h>
46namespace dolfinx::io::VTKHDF
51template <std::
floating_po
int U>
52fem::CoordinateElement<U>
53coordinate_element(std::array<std::uint8_t, 2> type_degree)
63 if (type_degree[0] == 26 or type_degree[0] == 27)
65 throw std::runtime_error(std::format(
66 "VTK cell type {} is a serendipity layout that Basix cannot express.",
67 static_cast<int>(type_degree[0])));
72 if (type_degree[0] == 23 or type_degree[0] == 25)
75 basix::FiniteElement<U> element
76 = basix::create_element<U>(basix::element::family::serendipity, cell, 2,
77 basix::element::lagrange_variant::unset,
78 basix::element::dpc_variant::unset,
false);
81 auto [geometry, geometry_shape] = basix::cell::geometry<U>(cell);
82 const std::vector<std::vector<int>> edges = basix::cell::topology(cell)[1];
85 for (std::size_t i = 0; i < edges.size(); ++i)
87 std::vector<U> midpoint(geometry_shape[1]);
88 for (std::size_t j = 0; j < geometry_shape[1]; ++j)
90 midpoint[j] = (geometry[edges[i][0] * geometry_shape[1] + j]
91 + geometry[edges[i][1] * geometry_shape[1] + j])
94 x[1][i] = {std::move(midpoint), {1, geometry_shape[1]}};
95 M[1][i] = {{1}, {1, 1, 1, 1}};
97 std::array<std::vector<basix::impl::mdspan_t<const U, 2>>, 4> x_view;
98 std::array<std::vector<basix::impl::mdspan_t<const U, 4>>, 4> M_view;
99 for (std::size_t d = 0; d < 4; ++d)
101 for (
const auto& [data, shape] : x[d])
102 x_view[d].emplace_back(data.data(), shape);
103 for (
const auto& [data, shape] : M[d])
104 M_view[d].emplace_back(data.data(), shape);
106 const auto& [wcoeffs, wshape] = element.wcoeffs();
107 return fem::CoordinateElement<U>(std::make_shared<basix::FiniteElement<U>>(
108 basix::create_custom_element<U>(
109 cell, {}, {wcoeffs.data(), wshape}, x_view, M_view, 0,
110 element.map_type(), element.sobolev_space(),
false,
111 element.embedded_subdegree(), element.embedded_superdegree(),
112 element.polyset_type())));
115 basix::element::lagrange_variant variant
116 = (
cell_degree > 2) ? basix::element::lagrange_variant::equispaced
117 : basix::element::lagrange_variant::unset;
118 return fem::CoordinateElement<U>(cell_type, cell_degree, variant);
129template <std::
floating_po
int U>
130void write_mesh(
const std::filesystem::path& filename,
131 const mesh::Mesh<U>& mesh)
133 const int tdim = mesh.topology()->dim();
134 std::vector<mesh::CellType> cell_types = mesh.topology()->entity_types(tdim);
138 std::vector<std::int8_t> vtk_types;
139 vtk_types.reserve(cell_types.size());
140 for (std::size_t i = 0; i < cell_types.size(); ++i)
143 cell_types[i], mesh.geometry().cmaps().at(i).dim()));
146 hdf5::Handle h5file(hdf5::open_file(mesh.comm(), filename,
"w",
true),
150 hdf5::add_group(h5file,
"VTKHDF");
152 hdf5::Handle vtk_group(H5Gopen(h5file,
"VTKHDF", H5P_DEFAULT), H5Gclose);
154 throw std::runtime_error(
"Failed to open HDF5 group \"VTKHDF\".");
155 hdf5::set_attribute(vtk_group,
"Version", std::vector{2, 2});
156 hdf5::set_attribute(vtk_group,
"Type",
"UnstructuredGrid");
160 std::vector cell_index_maps = mesh.topology()->index_maps(tdim);
161 std::vector<std::int32_t> num_cells;
162 std::vector<std::int64_t> num_cells_global;
163 for (
auto& im : cell_index_maps)
165 num_cells.push_back(im->size_local());
166 num_cells_global.push_back(im->size_global());
170 std::shared_ptr<const common::IndexMap> geom_imap
171 = mesh.geometry().index_map();
172 std::int64_t size_global = geom_imap->size_global();
173 std::vector<std::int64_t> geom_global_shape = {size_global, 3};
174 std::array<std::int64_t, 2> geom_irange = geom_imap->local_range();
175 hdf5::write_dataset(h5file,
"/VTKHDF/Points", mesh.geometry().x().data(),
176 geom_irange, geom_global_shape,
true,
false);
177 hdf5::write_dataset(h5file,
"VTKHDF/NumberOfPoints", &size_global, {0, 1},
182 std::vector<std::int64_t> topology_flattened;
183 std::vector<std::int64_t> topology_offsets;
184 std::vector<std::uint8_t> vtkcelltypes;
185 for (std::size_t i = 0; i < cell_index_maps.size(); ++i)
187 md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>> g_dofmap
188 = mesh.geometry().dofmaps().at(i);
190 std::vector<std::uint16_t> perm
193 std::vector<std::int32_t> local_dm;
194 local_dm.reserve(g_dofmap.extent(1) * num_cells[i]);
195 for (
int j = 0; j < num_cells[i]; ++j)
196 for (std::size_t k = 0; k < g_dofmap.extent(1); ++k)
197 local_dm.push_back(g_dofmap(j, inverse_perm[k]));
199 std::vector<std::int64_t> global_dm(local_dm.size());
200 geom_imap->local_to_global(local_dm, global_dm);
202 topology_flattened.insert(topology_flattened.end(), global_dm.begin(),
204 topology_offsets.insert(topology_offsets.end(), g_dofmap.extent(0),
206 vtkcelltypes.insert(vtkcelltypes.end(), cell_index_maps[i]->size_local(),
211 std::partial_sum(topology_offsets.cbegin(), topology_offsets.cend(),
212 topology_offsets.begin());
214 std::vector<int> num_nodes_per_cell;
215 std::vector<std::int64_t> cell_start_pos;
216 std::vector<std::int64_t> cell_stop_pos;
217 for (std::size_t i = 0; i < cell_index_maps.size(); ++i)
219 num_nodes_per_cell.push_back(mesh.geometry().cmaps().at(i).dim());
220 std::array<std::int64_t, 2> r = cell_index_maps[i]->local_range();
221 cell_start_pos.push_back(r[0]);
222 cell_stop_pos.push_back(r[1]);
226 std::int64_t offset_start_position = std::accumulate(
227 cell_start_pos.begin(), cell_start_pos.end(), std::int64_t(0));
228 std::int64_t offset_stop_position = std::accumulate(
229 cell_stop_pos.begin(), cell_stop_pos.end(), std::int64_t(0));
232 std::int64_t topology_start
233 = std::inner_product(num_nodes_per_cell.begin(), num_nodes_per_cell.end(),
234 cell_start_pos.begin(), std::int64_t(0));
236 std::ranges::transform(topology_offsets, topology_offsets.begin(),
237 [topology_start](std::int64_t x)
238 { return x + topology_start; });
240 std::int64_t num_all_cells_global = std::accumulate(
241 num_cells_global.begin(), num_cells_global.end(), std::int64_t(0));
242 hdf5::write_dataset(h5file,
"/VTKHDF/Offsets", topology_offsets.data(),
243 {offset_start_position + 1, offset_stop_position + 1},
244 {num_all_cells_global + 1},
true,
false);
247 std::int64_t topology_size_global
248 = std::inner_product(num_nodes_per_cell.begin(), num_nodes_per_cell.end(),
249 num_cells_global.begin(), std::int64_t(0));
251 std::int64_t topology_stop = topology_start + topology_flattened.size();
252 hdf5::write_dataset(h5file,
"/VTKHDF/Connectivity", topology_flattened.data(),
253 {topology_start, topology_stop}, {topology_size_global},
257 hdf5::write_dataset(h5file,
"/VTKHDF/Types", vtkcelltypes.data(),
258 {offset_start_position, offset_stop_position},
259 {num_all_cells_global},
true,
false);
260 hdf5::write_dataset(h5file,
"/VTKHDF/NumberOfConnectivityIds",
261 &topology_size_global, {0, 1}, {1},
true,
false);
262 hdf5::write_dataset(h5file,
"/VTKHDF/NumberOfCells", &num_all_cells_global,
263 {0, 1}, {1},
true,
false);
288template <std::
floating_po
int U>
289void write_data(std::string_view point_or_cell,
290 const std::filesystem::path& filename,
291 const mesh::Mesh<U>& mesh,
const std::vector<U>& data,
294 std::vector<std::shared_ptr<const common::IndexMap>> index_maps;
295 if (point_or_cell ==
"Point")
296 index_maps = {mesh.geometry().index_map()};
297 else if (point_or_cell ==
"Cell")
298 index_maps = mesh.topology()->index_maps(mesh.topology()->dim());
300 throw std::invalid_argument(
"Selection must be Point or Cell.");
302 const std::string poc(point_or_cell);
303 std::string dataset_name = std::format(
"/VTKHDF/{}Data/u", poc);
304 std::int32_t npoints = std::accumulate(
305 index_maps.begin(), index_maps.end(), std::int32_t(0),
306 [](std::int32_t a,
auto& im) { return a + im->size_local(); });
307 if (npoints == 0 and !data.empty())
309 throw std::invalid_argument(
310 "Data supplied on a process with no local vertices/cells.");
312 if (npoints > 0 and data.size() % npoints != 0)
314 throw std::invalid_argument(
315 "Data size mismatch with number of local vertices/cells.");
323 const std::int32_t width_local = npoints == 0 ? 0 : data.size() / npoints;
324 std::array<std::int32_t, 2> width_local_pair{
326 npoints == 0 ? std::numeric_limits<std::int32_t>::min() : -width_local};
327 std::array<std::int32_t, 2> width{0, 0};
328 MPI_Allreduce(width_local_pair.data(), width.data(), 2, MPI_INT32_T, MPI_MAX,
330 const std::int32_t data_width = width[0];
331 if (data_width == 0 or data_width != -width[1])
333 throw std::runtime_error(
334 "Data width is zero, or differs between processes.");
336 logger().debug(
"Data vector width={}", data_width);
338 hdf5::Handle h5file(hdf5::open_file(mesh.comm(), filename,
"a",
true),
340 hdf5::add_group(h5file,
"VTKHDF/Steps");
342 std::int64_t point_data_offset = 0;
344 hdf5::Handle vtk_group(H5Gopen(h5file,
"VTKHDF/Steps", H5P_DEFAULT),
347 throw std::runtime_error(
"Failed to open HDF5 group \"VTKHDF/Steps\".");
349 if (htri_t attr_exists = H5Aexists(vtk_group,
"NSteps"); attr_exists < 0)
350 throw std::runtime_error(
"Error checking attribute");
351 else if (attr_exists == 0)
352 hdf5::set_attribute(vtk_group,
"NSteps", 1);
356 std::int32_t nsteps = 0;
358 hdf5::Handle attr_id(H5Aopen(vtk_group,
"NSteps", H5P_DEFAULT),
361 throw std::runtime_error(
"Failed to open HDF5 attribute \"NSteps\".");
362 if (H5Aread(attr_id, H5T_NATIVE_INT32, &nsteps) < 0)
363 throw std::runtime_error(
"Failed to read HDF5 attribute \"NSteps\".");
365 hdf5::set_attribute(vtk_group,
"NSteps", nsteps + 1);
367 std::vector<std::int64_t> data_shape
368 = hdf5::get_dataset_shape(h5file, dataset_name);
369 assert(data_shape.size() == 2);
370 point_data_offset = data_shape[0];
376 = [&h5file]<
typename T>(
const std::string& dset_name, T value)
379 if (hdf5::has_dataset(h5file, dset_name))
381 std::vector<std::int64_t> shape
382 = hdf5::get_dataset_shape(h5file, dset_name);
383 assert(shape.size() == 1);
386 hdf5::write_dataset(h5file, dset_name, &value, {s, s + 1}, {s + 1},
true,
391 append_dataset(
"/VTKHDF/Steps/CellOffsets", 0);
392 append_dataset(
"/VTKHDF/Steps/ConnectivityIdOffsets", 0);
393 append_dataset(
"/VTKHDF/Steps/NumberOfParts", 1);
394 append_dataset(
"/VTKHDF/Steps/PartOffsets", 0);
395 append_dataset(
"/VTKHDF/Steps/PointOffsets", 0);
398 hdf5::add_group(h5file, std::format(
"/VTKHDF/Steps/{}DataOffsets", poc));
399 append_dataset(std::format(
"/VTKHDF/Steps/{}DataOffsets/u", poc),
404 append_dataset(
"/VTKHDF/Steps/Values", time);
406 std::string group_name = std::format(
"/VTKHDF/{}Data", poc);
407 hdf5::add_group(h5file, group_name);
411 std::int64_t range0 = std::accumulate(
412 index_maps.begin(), index_maps.end(), std::int64_t(0),
413 [](std::int64_t a,
auto& im) { return a + im->local_range()[0]; });
414 std::array<std::int64_t, 2> range{range0, range0 + npoints};
416 std::int64_t global_size = std::accumulate(
417 index_maps.begin(), index_maps.end(), std::int64_t(0),
418 [](std::int64_t a,
auto& im) { return a + im->size_global(); });
420 std::vector<std::int64_t> shape0 = {global_size, data_width};
421 if (hdf5::has_dataset(h5file, dataset_name))
423 std::vector<std::int64_t> shape
424 = hdf5::get_dataset_shape(h5file, dataset_name);
425 assert(shape.size() == 2);
426 std::int64_t offset = shape[0];
430 hdf5::write_dataset(h5file, dataset_name, data.data(), range, shape0,
true,
435 hdf5::write_dataset(h5file, dataset_name, data.data(), range, shape0,
true,
439 hdf5::Handle dset_id(hdf5::open_dataset(h5file, dataset_name), H5Dclose);
440 hdf5::set_attribute(dset_id,
"NumberOfComponents", data_width);
441 hdf5::Handle vec_group(H5Gopen(h5file, group_name.c_str(), H5P_DEFAULT),
444 throw std::runtime_error(
"Failed to open HDF5 group for vector data.");
445 hdf5::set_attribute(vec_group,
"Vectors",
"u");
465template <std::
floating_po
int U>
466mesh::Mesh<U> read_mesh(MPI_Comm comm,
const std::filesystem::path& filename,
467 std::size_t gdim = 3,
468 std::optional<std::int32_t> max_facet_to_cell_links = 2,
473 throw std::invalid_argument(
474 "Geometric dimension must be less than or equal to 3.");
477 hdf5::Handle h5file(hdf5::open_file(comm, filename,
"r",
true), H5Fclose);
479 std::vector<std::int64_t> shape
480 = hdf5::get_dataset_shape(h5file,
"/VTKHDF/Types");
483 std::array<std::int64_t, 2> local_cell_range
486 std::vector<std::uint8_t> types = hdf5::read_dataset<std::uint8_t>(
487 h5file,
"/VTKHDF/Types", local_cell_range,
true);
490 std::vector<std::int64_t> offsets = hdf5::read_dataset<std::int64_t>(
491 h5file,
"/VTKHDF/Offsets", {local_cell_range[0], local_cell_range[1] + 1},
495 std::vector<std::array<std::uint8_t, 2>> types_unique;
496 std::vector<std::uint8_t> cell_degrees;
497 for (std::size_t i = 0; i < types.size(); ++i)
499 std::int64_t num_nodes = offsets[i + 1] - offsets[i];
506 : (std::uint8_t)degree;
508 cell_degrees.push_back(cell_degree);
511 std::ranges::sort(types_unique);
512 auto [unique_end, range_end] = std::ranges::unique(types_unique);
513 types_unique.erase(unique_end, range_end);
520 int count = 2 * types_unique.size();
521 std::vector<std::int32_t> recv_count(mpi_size);
522 MPI_Allgather(&count, 1, MPI_INT32_T, recv_count.data(), 1, MPI_INT32_T,
524 std::vector<std::int32_t> recv_offsets(mpi_size + 1, 0);
525 std::partial_sum(recv_count.begin(), recv_count.end(),
526 recv_offsets.begin() + 1);
528 std::vector<std::array<std::uint8_t, 2>> recv_types;
530 std::vector<std::uint8_t> send_types;
531 for (std::array<std::uint8_t, 2> t : types_unique)
532 send_types.insert(send_types.end(), t.begin(), t.end());
534 std::vector<std::uint8_t> recv_types_buffer(recv_offsets.back());
535 MPI_Allgatherv(send_types.data(), send_types.size(), MPI_UINT8_T,
536 recv_types_buffer.data(), recv_count.data(),
537 recv_offsets.data(), MPI_UINT8_T, comm);
539 for (std::size_t i = 0; i < recv_types_buffer.size(); i += 2)
540 recv_types.push_back({recv_types_buffer[i], recv_types_buffer[i + 1]});
542 std::ranges::sort(recv_types);
543 auto [unique_end, range_end] = std::ranges::unique(recv_types);
544 recv_types.erase(unique_end, range_end);
548 std::map<std::array<std::uint8_t, 2>, std::int32_t> type_to_index;
549 std::vector<mesh::CellType> dolfinx_cell_type;
550 for (std::array<std::uint8_t, 2> ct : recv_types)
553 type_to_index.insert({ct, dolfinx_cell_type.size()});
554 dolfinx_cell_type.push_back(cell_type);
557 std::vector npoints = hdf5::read_dataset<std::int64_t>(
558 h5file,
"/VTKHDF/NumberOfPoints", {0, 1},
true);
559 logger().info(
"Mesh with {} points", npoints[0]);
560 std::array<std::int64_t, 2> local_point_range
563 std::vector<std::int64_t> x_shape
564 = hdf5::get_dataset_shape(h5file,
"/VTKHDF/Points");
565 std::vector<U> points_local = hdf5::read_dataset<U>(h5file,
"/VTKHDF/Points",
566 local_point_range,
true);
569 std::vector<U> points_pruned((local_point_range[1] - local_point_range[0])
571 for (std::int64_t i = 0; i < local_point_range[1] - local_point_range[0]; ++i)
573 std::copy_n(points_local.begin() + i * 3, gdim,
574 points_pruned.begin() + i * gdim);
577 std::vector<std::int64_t> topology = hdf5::read_dataset<std::int64_t>(
578 h5file,
"/VTKHDF/Connectivity", {offsets.front(), offsets.back()},
true);
579 std::ranges::transform(offsets, offsets.begin(),
580 [offset = offsets.front()](std::int64_t x)
581 { return x - offset; });
585 std::vector<std::vector<std::int64_t>> cells_local(recv_types.size());
587 std::vector<std::size_t> num_nodes_per_type(recv_types.size(), 0);
588 for (std::size_t j = 0; j < types.size(); ++j)
590 std::int32_t type_index = type_to_index.at({types[j], cell_degrees[j]});
591 num_nodes_per_type[type_index] += offsets[j + 1] - offsets[j];
593 for (std::size_t t = 0; t < cells_local.size(); ++t)
594 cells_local[t].reserve(num_nodes_per_type[t]);
596 for (std::size_t j = 0; j < types.size(); ++j)
598 std::int32_t type_index = type_to_index.at({types[j], cell_degrees[j]});
600 std::vector<std::uint16_t> perm
602 for (std::int64_t k = 0; k < offsets[j + 1] - offsets[j]; ++k)
603 cells_local[type_index].push_back(topology[perm[k] + offsets[j]]);
607 std::vector<fem::CoordinateElement<U>> coordinate_elements;
608 std::ranges::transform(recv_types, std::back_inserter(coordinate_elements),
609 impl::coordinate_element<U>);
611 std::vector<std::span<const std::int64_t>> cells_span(cells_local.begin(),
614 points_pruned, {(std::size_t)x_shape[0], gdim},
615 graph::Partitioner{}, mesh::GhostMode::none,
616 max_facet_to_cell_links, num_threads);
Functions supporting mesh operations.
int size(MPI_Comm comm)
Definition MPI.cpp:81
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:73
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
@ cell
Cell.
Definition Form.h:44
std::tuple< mesh::CellType, std::int8_t > vtk_to_dolfinx(std::int8_t vtk_cell_type)
Get DOLFINx cell type and degree from VTK cell type.
Definition cells.h:213
std::int8_t get_vtk_cell_type(mesh::CellType cell, int num_nodes)
Get the VTK cell identifier for a cell type and node layout.
Definition cells.cpp:749
std::vector< std::uint16_t > perm_vtk(mesh::CellType type, int num_nodes)
Permutation array to map from VTK to DOLFINx node ordering.
Definition cells.cpp:556
std::vector< std::uint16_t > transpose(std::span< const std::uint16_t > map)
Compute the transpose of a re-ordering map.
Definition cells.cpp:720
int cell_degree(mesh::CellType type, int num_nodes)
Get the Lagrange order of a given cell with a given number of nodes.
Definition cells.cpp:632
Mesh< typename std::remove_reference_t< typename U::value_type > > create_mesh(MPI_Comm comm, MPI_Comm commt, std::vector< std::span< const std::int64_t > > cells, const std::vector< fem::CoordinateElement< typename std::remove_reference_t< typename U::value_type > > > &elements, MPI_Comm commg, const U &x, std::array< std::size_t, 2 > xshape, const graph::Partitioner &partitioner, GhostMode ghost_mode, std::optional< std::int32_t > max_facet_to_cell_links, int num_threads, const graph::Reorder &reorder_fn=graph::Reorder{})
Create a distributed mesh::Mesh from mesh data and using the provided graph partitioning function for...
Definition utils.h:1402
CellType
Cell type identifier.
Definition cell_types.h:24
basix::cell::type cell_type_to_basix_type(CellType celltype)
Convert a cell type to a Basix cell type.
Definition cell_types.cpp:171
auto logger() -> spdlog::logger &
Dolfinx's logger.
Definition log.cpp:24