/home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/discretization/data/hdf5_input.hh Source File#

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/discretization/data/hdf5_input.hh Source File
DiFfRG
hdf5_input.hh
Go to the documentation of this file.
1#pragma once
2
3// DiFfRG
8
9#include <filesystem>
10#include <list>
11
12namespace DiFfRG
13{
21 {
22 public:
23 HDF5Input(const std::string file_name);
24
31 void load_map(const std::string &name, double *data, int series_number = -1)
32 {
33 if (!maps.has_group(name))
34 throw std::runtime_error("HDF5Input::map: The map '" + name + "' has not been written to the file '" +
35 file_name + "'.");
36
37 auto super_group = maps.open_group(name);
38 if (series_number < 0) {
39 size_t max_series = 0;
40 for (const auto &child : super_group.child_names()) {
41 if (!super_group.child_is_group(child)) continue;
42 size_t grp_num = std::stoul(child);
43 if (grp_num > max_series) {
44 max_series = grp_num;
45 }
46 }
47 series_number = static_cast<int>(max_series);
48 }
49 if (!super_group.has_group(std::to_string(series_number)))
50 throw std::runtime_error("HDF5Input::map: The map '" + name + "' does not have series number " +
51 std::to_string(series_number) + " in the file '" + file_name + "'.");
52
53 auto group = super_group.open_group(std::to_string(series_number));
54
55 auto dataset = group.open_dataset("data");
56 auto dataspace = dataset.dataspace();
57 std::vector<double> temp_data(dataspace.size());
58 dataset.read(temp_data);
59 std::copy(temp_data.begin(), temp_data.end(), data);
60 }
61
68 std::vector<double> load_map(const std::string &name, int series_number = -1)
69 {
70 if (!maps.has_group(name))
71 throw std::runtime_error("HDF5Input::map: The map '" + name + "' has not been written to the file '" +
72 file_name + "'.");
73
74 auto super_group = maps.open_group(name);
75 if (series_number < 0) {
76 size_t max_series = 0;
77 for (const auto &child : super_group.child_names()) {
78 if (!super_group.child_is_group(child)) continue;
79 size_t grp_num = std::stoul(child);
80 if (grp_num > max_series) {
81 max_series = grp_num;
82 }
83 }
84 series_number = static_cast<int>(max_series);
85 }
86 if (!super_group.has_group(std::to_string(series_number)))
87 throw std::runtime_error("HDF5Input::map: The map '" + name + "' does not have series number " +
88 std::to_string(series_number) + " in the file '" + file_name + "'.");
89
90 auto group = super_group.open_group(std::to_string(series_number));
91
92 auto dataset = group.open_dataset("data");
93 auto dataspace = dataset.dataspace();
94 std::vector<double> temp_data(dataspace.size());
95 dataset.read(temp_data);
96 return temp_data;
97 }
98
107 template <size_t dim>
108 std::vector<double> load_map_coord(const std::string &name, int series_number = -1)
109 {
110 if (!maps.has_group(name))
111 throw std::runtime_error("HDF5Input::map: The map '" + name + "' has not been written to the file '" +
112 file_name + "'.");
113
114 auto super_group = maps.open_group(name);
115 if (series_number < 0) {
116 size_t max_series = 0;
117 for (const auto &child : super_group.child_names()) {
118 if (!super_group.child_is_group(child)) continue;
119 size_t grp_num = std::stoul(child);
120 if (grp_num > max_series) {
121 max_series = grp_num;
122 }
123 }
124 series_number = static_cast<int>(max_series);
125 }
126 if (!super_group.has_group(std::to_string(series_number)))
127 throw std::runtime_error("HDF5Input::map: The map '" + name + "' does not have series number " +
128 std::to_string(series_number) + " in the file '" + file_name + "'.");
129
130 auto group = super_group.open_group(std::to_string(series_number));
131
132 auto dataset = group.open_dataset("coordinates");
133 auto dataspace = dataset.dataspace();
134
135 using coord_type = device::array<double, dim>;
136 std::vector<coord_type> compound_data(dataspace.size());
137 dataset.read(compound_data);
138
139 std::vector<double> result;
140 result.reserve(dataspace.size() * dim);
141 for (const auto &pt : compound_data)
142 for (size_t d = 0; d < dim; ++d)
143 result.push_back(pt[d]);
144 return result;
145 }
146
155 template <typename Coordinates> void load_map(const std::string &name, double *data, const Coordinates &coordinates)
156 {
157 check_coordinates(coordinates.to_string(), coordinates);
158 load_map(name, data);
159 }
160
161 template <typename T> std::vector<T> load_scalar(const std::string &name)
162 {
163 if (!scalars.has_dataset(name))
164 throw std::runtime_error("HDF5Input::scalar: The scalar '" + name + "' has not been written to the file '" +
165 file_name + "'.");
166
167 auto dataset = scalars.open_dataset(name);
168 auto dataspace = dataset.dataspace();
169 std::vector<T> value(dataspace.size());
170 dataset.read(value);
171 return value;
172 }
173
175
176 private:
177 const std::string file_name;
178
179 template <typename Coordinates>
180 void check_coordinates(const std::string &coord_name, const Coordinates &coordinates)
181 {
182 if (!coords.has_dataset(coord_name))
183 throw std::runtime_error("HDF5Input::map: The coordinates '" + coord_name +
184 "' have not been written to the file '" + file_name + "'.");
185
186 const auto in_data = make_grid(coordinates);
187
188 using ctype = typename Coordinates::ctype;
190 std::vector<cortype> file_data(coordinates.size());
191
192 auto dataset = coords.open_dataset(coord_name);
193 const auto dataspace = dataset.dataspace();
194 if (dataspace.size() != (hssize_t)coordinates.size())
195 throw std::runtime_error("HDF5Input::map: The coordinates '" + coord_name + "' in the file '" + file_name +
196 "' have incorrect size (" + std::to_string(dataspace.size()) +
197 "), not matching the provided coordinates (" + std::to_string(coordinates.size()) +
198 ").");
199 dataset.read(file_data);
200
201 for (size_t i = 0; i < coordinates.size(); ++i) {
202 for (size_t j = 0; j < Coordinates::dim; ++j) {
203 if (std::abs(file_data[i][j] - in_data[i][j]) > 1e-12) {
204 throw std::runtime_error("HDF5Input::map: The coordinates '" + coord_name +
205 "' do not match the coordinates in the file '" + file_name + "'.");
206 }
207 }
208 }
209 }
210
215
216 std::filesystem::path path;
217 };
218} // namespace DiFfRG
A class to output data to a CSV file.
Definition hdf5_input.hh:21
std::filesystem::path path
Definition hdf5_input.hh:216
void load_map(const std::string &name, double *data, const Coordinates &coordinates)
Load a map from the HDF5 file, while checking that the coordinates in the file match the provided coo...
Definition hdf5_input.hh:155
std::vector< T > load_scalar(const std::string &name)
Definition hdf5_input.hh:161
DiFfRG::hdf5::File & get_file()
DiFfRG::hdf5::Group scalars
Definition hdf5_input.hh:212
DiFfRG::hdf5::Group coords
Definition hdf5_input.hh:214
void load_map(const std::string &name, double *data, int series_number=-1)
Load a map from the HDF5 file.
Definition hdf5_input.hh:31
void check_coordinates(const std::string &coord_name, const Coordinates &coordinates)
Definition hdf5_input.hh:180
std::vector< double > load_map(const std::string &name, int series_number=-1)
Load a map from the HDF5 file.
Definition hdf5_input.hh:68
DiFfRG::hdf5::Group maps
Definition hdf5_input.hh:213
const std::string file_name
Definition hdf5_input.hh:177
std::vector< double > load_map_coord(const std::string &name, int series_number=-1)
Load a map's coordinates from the HDF5 file.
Definition hdf5_input.hh:108
HDF5Input(const std::string file_name)
DiFfRG::hdf5::File h5_file
Definition hdf5_input.hh:211
Dataspace dataspace() const
Definition dataset.hh:31
Definition file.hh:13
Definition group.hh:16
Dataset open_dataset(const std::string &name)
Definition group.hh:71
bool has_dataset(const std::string &name) const
Definition group.hh:42
bool has_group(const std::string &name) const
Definition group.hh:41
Group open_group(const std::string &name)
Definition group.hh:34
std::array< T, N > array
Definition kokkos.hh:133
Definition complex_math.hh:10
auto make_grid(const Coordinates &coordinates)
Definition coordinates.hh:388