7#include "HDF5Interface.h"
10#include <dolfinx/common/IndexMap.h>
11#include <dolfinx/graph/partition.h>
12#include <dolfinx/io/cells.h>
13#include <dolfinx/mesh/Mesh.h>
14#include <dolfinx/mesh/Topology.h>
22namespace dolfinx::io::VTKHDF
31template <std::
floating_po
int U>
32void write_mesh(
const std::filesystem::path& filename,
33 const mesh::Mesh<U>& mesh)
35 hid_t h5file = hdf5::open_file(mesh.comm(), filename,
"w",
true);
38 hdf5::add_group(h5file,
"VTKHDF");
39 hid_t vtk_group = H5Gopen(h5file,
"VTKHDF", H5P_DEFAULT);
40 hdf5::set_attribute(vtk_group,
"Version", std::vector{2, 2});
41 hdf5::set_attribute(vtk_group,
"Type",
"UnstructuredGrid");
45 std::vector<mesh::CellType> cell_types
46 = mesh.topology()->entity_types(mesh.topology()->dim());
48 std::vector cell_index_maps
49 = mesh.topology()->index_maps(mesh.topology()->dim());
50 std::vector<std::int32_t> num_cells;
51 std::vector<std::int64_t> num_cells_global;
52 for (
auto& im : cell_index_maps)
54 num_cells.push_back(im->size_local());
55 num_cells_global.push_back(im->size_global());
59 std::shared_ptr<const common::IndexMap> geom_imap
60 = mesh.geometry().index_map();
61 std::int64_t size_global = geom_imap->size_global();
62 std::vector<std::int64_t> geom_global_shape = {size_global, 3};
63 std::array<std::int64_t, 2> geom_irange = geom_imap->local_range();
64 hdf5::write_dataset(h5file,
"/VTKHDF/Points", mesh.geometry().x().data(),
65 geom_irange, geom_global_shape,
true,
false);
66 hdf5::write_dataset(h5file,
"VTKHDF/NumberOfPoints", &size_global, {0, 1},
71 std::vector<std::int64_t> topology_flattened;
72 std::vector<std::int64_t> topology_offsets;
73 std::vector<std::uint8_t> vtkcelltypes;
74 for (std::size_t i = 0; i < cell_index_maps.size(); ++i)
76 md::mdspan<const std::int32_t, md::dextents<std::size_t, 2>> g_dofmap
77 = mesh.geometry().dofmaps().at(i);
79 std::vector<std::uint16_t> perm
82 std::vector<std::int32_t> local_dm;
83 local_dm.reserve(g_dofmap.extent(1) * num_cells[i]);
84 for (
int j = 0; j < num_cells[i]; ++j)
85 for (std::size_t k = 0; k < g_dofmap.extent(1); ++k)
86 local_dm.push_back(g_dofmap(j, inverse_perm[k]));
88 std::vector<std::int64_t> global_dm(local_dm.size());
89 geom_imap->local_to_global(local_dm, global_dm);
91 topology_flattened.insert(topology_flattened.end(), global_dm.begin(),
93 topology_offsets.insert(topology_offsets.end(), g_dofmap.extent(0),
96 vtkcelltypes.end(), cell_index_maps[i]->size_local(),
101 std::partial_sum(topology_offsets.cbegin(), topology_offsets.cend(),
102 topology_offsets.begin());
104 std::vector<int> num_nodes_per_cell;
105 std::vector<std::int64_t> cell_start_pos;
106 std::vector<std::int64_t> cell_stop_pos;
107 for (std::size_t i = 0; i < cell_index_maps.size(); ++i)
109 num_nodes_per_cell.push_back(mesh.geometry().cmaps().at(i).dim());
110 std::array<std::int64_t, 2> r = cell_index_maps[i]->local_range();
111 cell_start_pos.push_back(r[0]);
112 cell_stop_pos.push_back(r[1]);
116 std::int64_t offset_start_position
117 = std::accumulate(cell_start_pos.begin(), cell_start_pos.end(), 0);
118 std::int64_t offset_stop_position
119 = std::accumulate(cell_stop_pos.begin(), cell_stop_pos.end(), 0);
122 std::int64_t topology_start
123 = std::inner_product(num_nodes_per_cell.begin(), num_nodes_per_cell.end(),
124 cell_start_pos.begin(), 0);
126 std::transform(topology_offsets.cbegin(), topology_offsets.cend(),
127 topology_offsets.begin(),
128 [topology_start](
auto x) { return x + topology_start; });
130 std::int64_t num_all_cells_global
131 = std::accumulate(num_cells_global.begin(), num_cells_global.end(), 0);
132 hdf5::write_dataset(h5file,
"/VTKHDF/Offsets", topology_offsets.data(),
133 {offset_start_position + 1, offset_stop_position + 1},
134 {num_all_cells_global + 1},
true,
false);
137 std::int64_t topology_size_global
138 = std::inner_product(num_nodes_per_cell.begin(), num_nodes_per_cell.end(),
139 num_cells_global.begin(), 0);
141 std::int64_t topology_stop = topology_start + topology_flattened.size();
142 hdf5::write_dataset(h5file,
"/VTKHDF/Connectivity", topology_flattened.data(),
143 {topology_start, topology_stop}, {topology_size_global},
147 hdf5::write_dataset(h5file,
"/VTKHDF/Types", vtkcelltypes.data(),
148 {offset_start_position, offset_stop_position},
149 {num_all_cells_global},
true,
false);
150 hdf5::write_dataset(h5file,
"/VTKHDF/NumberOfConnectivityIds",
151 &topology_size_global, {0, 1}, {1},
true,
false);
152 hdf5::write_dataset(h5file,
"/VTKHDF/NumberOfCells", &num_all_cells_global,
153 {0, 1}, {1},
true,
false);
154 hdf5::close_file(h5file);
178template <std::
floating_po
int U>
179void write_data(std::string_view point_or_cell,
180 const std::filesystem::path& filename,
181 const mesh::Mesh<U>& mesh,
const std::vector<U>& data,
184 std::vector<std::shared_ptr<const common::IndexMap>> index_maps;
185 if (point_or_cell ==
"Point")
186 index_maps = {mesh.geometry().index_map()};
187 else if (point_or_cell ==
"Cell")
188 index_maps = mesh.topology()->index_maps(mesh.topology()->dim());
190 throw std::runtime_error(
"Selection must be Point or Cell");
192 const std::string poc(point_or_cell);
193 std::string dataset_name = std::format(
"/VTKHDF/{}Data/u", poc);
195 = std::accumulate(index_maps.begin(), index_maps.end(), 0,
196 [](
int a,
auto im) { return a + im->size_local(); });
197 int data_width = data.size() / npoints;
198 if (data.size() % npoints != 0)
200 throw std::runtime_error(
201 "Data size mismatch with number of local vertices/cells");
203 spdlog::debug(
"Data vector width={}", data_width);
205 hid_t h5file = hdf5::open_file(mesh.comm(), filename,
"a",
true);
206 hdf5::add_group(h5file,
"VTKHDF/Steps");
207 hid_t vtk_group = H5Gopen(h5file,
"VTKHDF/Steps", H5P_DEFAULT);
209 std::int64_t point_data_offset = 0;
210 if (htri_t attr_exists = H5Aexists(vtk_group,
"NSteps"); attr_exists < 0)
211 throw std::runtime_error(
"Error checking attribute");
212 else if (attr_exists == 0)
213 hdf5::set_attribute(vtk_group,
"NSteps", 1);
217 std::int32_t nsteps = 0;
218 hid_t attr_id = H5Aopen(vtk_group,
"NSteps", H5P_DEFAULT);
219 H5Aread(attr_id, H5T_NATIVE_INT32, &nsteps);
221 H5Awrite(attr_id, H5T_NATIVE_INT32, &nsteps);
224 std::vector<std::int64_t> data_shape
225 = hdf5::get_dataset_shape(h5file, dataset_name);
226 assert(data_shape.size() == 2);
227 point_data_offset = data_shape[0];
233 = [&h5file]<
typename T>(
const std::string& dset_name, T value)
236 if (hdf5::has_dataset(h5file, dset_name))
238 std::vector<std::int64_t> shape
239 = hdf5::get_dataset_shape(h5file, dset_name);
240 assert(shape.size() == 1);
243 hdf5::write_dataset(h5file, dset_name, &value, {s, s + 1}, {s + 1},
true,
248 append_dataset(
"/VTKHDF/Steps/CellOffsets", 0);
249 append_dataset(
"/VTKHDF/Steps/ConnectivityIdOffsets", 0);
250 append_dataset(
"/VTKHDF/Steps/NumberOfParts", 1);
251 append_dataset(
"/VTKHDF/Steps/PartOffsets", 0);
252 append_dataset(
"/VTKHDF/Steps/PointOffsets", 0);
255 hdf5::add_group(h5file, std::format(
"/VTKHDF/Steps/{}DataOffsets", poc));
256 append_dataset(std::format(
"/VTKHDF/Steps/{}DataOffsets/u", poc),
261 append_dataset(
"/VTKHDF/Steps/Values", time);
263 std::string group_name = std::format(
"/VTKHDF/{}Data", poc);
264 hdf5::add_group(h5file, group_name);
268 std::int64_t range0 = std::accumulate(index_maps.begin(), index_maps.end(), 0,
270 { return a + im->local_range()[0]; });
271 std::array<std::int64_t, 2> range{range0, range0 + npoints};
273 std::int64_t global_size = std::accumulate(
274 index_maps.begin(), index_maps.end(), 0,
275 [](std::int64_t a,
auto im) { return a + im->size_global(); });
277 std::vector<std::int64_t> shape0 = {global_size, data_width};
278 if (hdf5::has_dataset(h5file, dataset_name))
280 std::vector<std::int64_t> shape
281 = hdf5::get_dataset_shape(h5file, dataset_name);
282 assert(shape.size() == 2);
283 std::int64_t offset = shape[0];
287 hdf5::write_dataset(h5file, dataset_name, data.data(), range, shape0,
true,
292 hdf5::write_dataset(h5file, dataset_name, data.data(), range, shape0,
true,
296 hid_t dset_id = hdf5::open_dataset(h5file, dataset_name);
297 hdf5::set_attribute(dset_id,
"NumberOfComponents", data_width);
299 hid_t vtk_group = H5Gopen(h5file, group_name.c_str(), H5P_DEFAULT);
300 hdf5::set_attribute(vtk_group,
"Vectors",
"u");
305 hdf5::close_file(h5file);
319template <std::
floating_po
int U>
320mesh::Mesh<U> read_mesh(MPI_Comm comm,
const std::filesystem::path& filename,
321 std::size_t gdim = 3,
322 std::optional<std::int32_t> max_facet_to_cell_links = 2)
324 hid_t h5file = hdf5::open_file(comm, filename,
"r",
true);
326 std::vector<std::int64_t> shape
327 = hdf5::get_dataset_shape(h5file,
"/VTKHDF/Types");
330 std::array<std::int64_t, 2> local_cell_range
333 hid_t dset_id = hdf5::open_dataset(h5file,
"/VTKHDF/Types");
334 std::vector<std::uint8_t> types
335 = hdf5::read_dataset<std::uint8_t>(dset_id, local_cell_range,
true);
339 dset_id = hdf5::open_dataset(h5file,
"/VTKHDF/Offsets");
340 std::vector<std::int64_t> offsets = hdf5::read_dataset<std::int64_t>(
341 dset_id, {local_cell_range[0], local_cell_range[1] + 1},
true);
345 std::vector<std::array<std::uint8_t, 2>> types_unique;
346 std::vector<std::uint8_t> cell_degrees;
347 for (std::size_t i = 0; i < types.size(); ++i)
349 std::int64_t num_nodes = offsets[i + 1] - offsets[i];
356 : (std::uint8_t)degree;
358 cell_degrees.push_back(cell_degree);
361 std::ranges::sort(types_unique);
362 auto [unique_end, range_end] = std::ranges::unique(types_unique);
363 types_unique.erase(unique_end, range_end);
370 int count = 2 * types_unique.size();
371 std::vector<std::int32_t> recv_count(mpi_size);
372 MPI_Allgather(&count, 1, MPI_INT32_T, recv_count.data(), 1, MPI_INT32_T,
374 std::vector<std::int32_t> recv_offsets(mpi_size + 1, 0);
375 std::partial_sum(recv_count.begin(), recv_count.end(),
376 recv_offsets.begin() + 1);
378 std::vector<std::array<std::uint8_t, 2>> recv_types;
380 std::vector<std::uint8_t> send_types;
381 for (std::array<std::uint8_t, 2> t : types_unique)
382 send_types.insert(send_types.end(), t.begin(), t.end());
384 std::vector<std::uint8_t> recv_types_buffer(recv_offsets.back());
385 MPI_Allgatherv(send_types.data(), send_types.size(), MPI_UINT8_T,
386 recv_types_buffer.data(), recv_count.data(),
387 recv_offsets.data(), MPI_UINT8_T, comm);
389 for (std::size_t i = 0; i < recv_types_buffer.size(); i += 2)
390 recv_types.push_back({recv_types_buffer[i], recv_types_buffer[i + 1]});
392 std::ranges::sort(recv_types);
393 auto [unique_end, range_end] = std::ranges::unique(recv_types);
394 recv_types.erase(unique_end, range_end);
398 std::map<std::array<std::uint8_t, 2>, std::int32_t> type_to_index;
399 std::vector<mesh::CellType> dolfinx_cell_type;
400 std::vector<std::uint8_t> dolfinx_cell_degree;
401 for (std::array<std::uint8_t, 2> ct : recv_types)
404 type_to_index.insert({ct, dolfinx_cell_degree.size()});
405 dolfinx_cell_degree.push_back(ct[1]);
406 dolfinx_cell_type.push_back(cell_type);
409 dset_id = hdf5::open_dataset(h5file,
"/VTKHDF/NumberOfPoints");
410 std::vector npoints = hdf5::read_dataset<std::int64_t>(dset_id, {0, 1},
true);
412 spdlog::info(
"Mesh with {} points", npoints[0]);
413 std::array<std::int64_t, 2> local_point_range
416 std::vector<std::int64_t> x_shape
417 = hdf5::get_dataset_shape(h5file,
"/VTKHDF/Points");
418 dset_id = hdf5::open_dataset(h5file,
"/VTKHDF/Points");
419 std::vector<U> points_local
420 = hdf5::read_dataset<U>(dset_id, local_point_range,
true);
426 throw std::runtime_error(
"Geometric dimension must be less than or equal "
429 std::vector<U> points_pruned((local_point_range[1] - local_point_range[0])
431 for (std::int64_t i = 0; i < local_point_range[1] - local_point_range[0]; ++i)
433 std::copy_n(points_local.begin() + i * 3, gdim,
434 points_pruned.begin() + i * gdim);
437 dset_id = hdf5::open_dataset(h5file,
"/VTKHDF/Connectivity");
438 std::vector<std::int64_t> topology = hdf5::read_dataset<std::int64_t>(
439 dset_id, {offsets.front(), offsets.back()},
true);
441 std::transform(offsets.cbegin(), offsets.cend(), offsets.begin(),
442 [offset = offsets.front()](
auto x) { return x - offset; });
443 hdf5::close_file(h5file);
446 std::vector<std::vector<std::int64_t>> cells_local(recv_types.size());
447 for (std::size_t j = 0; j < types.size(); ++j)
449 std::int32_t type_index = type_to_index.at({types[j], cell_degrees[j]});
451 std::vector<std::uint16_t> perm
453 for (std::int64_t k = 0; k < offsets[j + 1] - offsets[j]; ++k)
454 cells_local[type_index].push_back(topology[perm[k] + offsets[j]]);
458 std::vector<fem::CoordinateElement<U>> coordinate_elements;
460 dolfinx_cell_type.cbegin(), dolfinx_cell_type.cend(),
461 dolfinx_cell_degree.cbegin(), std::back_inserter(coordinate_elements),
462 [](
auto cell_type,
auto cell_degree)
464 basix::element::lagrange_variant variant
465 = (cell_degree > 2) ? basix::element::lagrange_variant::equispaced
466 : basix::element::lagrange_variant::unset;
467 return fem::CoordinateElement<U>(cell_type, cell_degree, variant);
470 std::vector<std::span<const std::int64_t>> cells_span(cells_local.begin(),
473 points_pruned, {(std::size_t)x_shape[0], gdim},
474 graph::Partitioner{}, mesh::GhostMode::none,
475 max_facet_to_cell_links, 1);
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
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:186
std::int8_t get_vtk_cell_type(mesh::CellType cell, int dim)
Get VTK cell identifier.
Definition cells.cpp:717
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:534
std::vector< std::uint16_t > transpose(std::span< const std::uint16_t > map)
Compute the transpose of a re-ordering map.
Definition cells.cpp:688
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:610
CellType
Cell type identifier.
Definition cell_types.h:22
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 CellReorderFunction &reorder_fn=graph::reorder_rcm)
Create a distributed mesh::Mesh from mesh data and using the provided graph partitioning function for...
Definition utils.h:1246