Skip to content

Commit a52f790

Browse files
committed
Add function to read data from VTKHDF file
1 parent a0ee385 commit a52f790

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
@@ -537,4 +537,45 @@ mesh::Mesh<U> read_mesh(MPI_Comm comm, std::string filename,
537537
points_pruned, {(std::size_t)x_shape[0], gdim}, part,
538538
max_facet_to_cell_links);
539539
}
540+
541+
/// @brief Read data from a VTKHDF format file.
542+
///
543+
/// @tparam U Scalar type of mesh
544+
/// @param[in] point_or_cell String "Point" or "Cell" determining data
545+
/// location.
546+
/// @param filename Name of the file to read from.
547+
/// @param mesh Mesh previously read from the same file.
548+
/// @param range The local range of data to read.
549+
/// @param timestep The time step to read for time-dependent data.
550+
/// @return The data read from file.
551+
template <std::floating_point U>
552+
std::vector<U> read_data(std::string point_or_cell, std::string filename,
553+
const mesh::Mesh<U>& mesh,
554+
std::array<std::int64_t, 2> range, int timestep = 0)
555+
{
556+
hid_t h5file = hdf5::open_file(mesh.comm(), filename, "r", true);
557+
std::string dataset_name = "/VTKHDF/" + point_or_cell + "Data/u";
558+
559+
std::int64_t data_offset = 0;
560+
// Read the offset for the requested timestep
561+
std::string offset_path = "/VTKHDF/Steps/" + point_or_cell + "DataOffsets/u";
562+
hid_t offset_dset = hdf5::open_dataset(h5file, offset_path);
563+
std::vector<std::int64_t> offsets = hdf5::read_dataset<std::int64_t>(
564+
offset_dset, {timestep, timestep + 1}, true);
565+
H5Dclose(offset_dset);
566+
data_offset = offsets[0];
567+
568+
// Adjust range to account for timestep offset
569+
range[0] += data_offset;
570+
range[1] += data_offset;
571+
572+
// Read data using HDF5
573+
hid_t dset_id = hdf5::open_dataset(h5file, dataset_name);
574+
std::vector<U> values = hdf5::read_dataset<U>(dset_id, range, true);
575+
H5Dclose(dset_id);
576+
577+
hdf5::close_file(h5file);
578+
579+
return values;
580+
}
540581
} // namespace dolfinx::io::VTKHDF

0 commit comments

Comments
 (0)