Skip to content

Commit 6ce5d1f

Browse files
committed
Add function to read data from VTKHDF file
1 parent 3b6ae16 commit 6ce5d1f

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

cpp/dolfinx/io/VTKHDF.h

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

0 commit comments

Comments
 (0)