Skip to content

Commit 46a530e

Browse files
committed
Add function to read data from VTKHDF file
1 parent ba6be82 commit 46a530e

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

cpp/dolfinx/io/VTKHDF.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,45 @@ mesh::Mesh<U> read_mesh(MPI_Comm comm, std::string filename,
602602
points_pruned, {(std::size_t)x_shape[0], gdim}, part,
603603
max_facet_to_cell_links);
604604
}
605+
606+
/// @brief Read data from a VTKHDF format file.
607+
///
608+
/// @tparam U Scalar type of mesh
609+
/// @param[in] point_or_cell String "Point" or "Cell" determining data
610+
/// location.
611+
/// @param filename Name of the file to read from.
612+
/// @param mesh Mesh previously read from the same file.
613+
/// @param range The local range of data to read.
614+
/// @param timestep The time step to read for time-dependent data.
615+
/// @return The data read from file.
616+
template <std::floating_point U>
617+
std::vector<U> read_data(std::string point_or_cell, std::string filename,
618+
const mesh::Mesh<U>& mesh,
619+
std::array<std::int64_t, 2> range, int timestep = 0)
620+
{
621+
hid_t h5file = hdf5::open_file(mesh.comm(), filename, "r", true);
622+
std::string dataset_name = "/VTKHDF/" + point_or_cell + "Data/u";
623+
624+
std::int64_t data_offset = 0;
625+
// Read the offset for the requested timestep
626+
std::string offset_path = "/VTKHDF/Steps/" + point_or_cell + "DataOffsets/u";
627+
hid_t offset_dset = hdf5::open_dataset(h5file, offset_path);
628+
std::vector<std::int64_t> offsets = hdf5::read_dataset<std::int64_t>(
629+
offset_dset, {timestep, timestep + 1}, true);
630+
H5Dclose(offset_dset);
631+
data_offset = offsets[0];
632+
633+
// Adjust range to account for timestep offset
634+
range[0] += data_offset;
635+
range[1] += data_offset;
636+
637+
// Read data using HDF5
638+
hid_t dset_id = hdf5::open_dataset(h5file, dataset_name);
639+
std::vector<U> values = hdf5::read_dataset<U>(dset_id, range, true);
640+
H5Dclose(dset_id);
641+
642+
hdf5::close_file(h5file);
643+
644+
return values;
645+
}
605646
} // namespace dolfinx::io::VTKHDF

0 commit comments

Comments
 (0)