votca 2025.1-dev
Loading...
Searching...
No Matches
checkpointreader.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16#pragma once
17#ifndef VOTCA_XTP_CHECKPOINTREADER_H
18#define VOTCA_XTP_CHECKPOINTREADER_H
19
20// Standard includes
21#include <string>
22#include <type_traits>
23#include <typeinfo>
24#include <vector>
25
26#if defined(__clang__)
27#elif defined(__GNUC__)
28#pragma GCC diagnostic push
29#pragma GCC diagnostic ignored "-Wdeprecated-copy"
30#endif
31// Third party includes
32#include <H5Cpp.h>
33
34// VOTCA includes
35#include <votca/tools/linalg.h>
36
37// Local VOTCA includes
38#include "checkpoint_utils.h"
39#include "checkpointtable.h"
40#include "eigen.h"
41
42namespace votca {
43namespace xtp {
44using namespace checkpoint_utils;
45
47 public:
48 CheckpointReader(const CptLoc& loc) : CheckpointReader(loc, "/") {};
49
50 CheckpointReader(const CptLoc& loc, const std::string path)
51 : loc_(loc), path_(path) {};
52
53 template <typename T>
54 typename std::enable_if<!std::is_fundamental<T>::value>::type operator()(
55 T& var, const std::string& name) const {
56 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
57
58 try {
59 ReadData(loc_, var, name);
60 } catch (H5::Exception&) {
61 std::stringstream message;
62
63 message << "Could not read " << name << " from " << loc_.getFileName()
64 << ":" << path_ << std::endl;
65
66 throw std::runtime_error(message.str());
67 }
68 }
69
70 template <typename T>
71 typename std::enable_if<std::is_fundamental<T>::value &&
72 !std::is_same<T, bool>::value>::type
73 operator()(T& var, const std::string& name) const {
74 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
75
76 try {
77 ReadScalar(loc_, var, name);
78 } catch (H5::Exception&) {
79 std::stringstream message;
80 message << "Could not read " << name << " from " << loc_.getFileName()
81 << ":" << path_ << "/" << std::endl;
82
83 throw std::runtime_error(message.str());
84 }
85 }
86
87 void operator()(bool& v, const std::string& name) const {
88 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
89
90 Index temp = Index(v);
91 try {
92 ReadScalar(loc_, temp, name);
93 } catch (H5::Exception&) {
94 std::stringstream message;
95 message << "Could not read " << name << " from " << loc_.getFileName()
96 << ":" << path_ << std::endl;
97
98 throw std::runtime_error(message.str());
99 }
100 v = static_cast<bool>(temp);
101 }
102
103 void operator()(std::string& var, const std::string& name) const {
104 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
105
106 try {
107 ReadScalar(loc_, var, name);
108 } catch (H5::Exception&) {
109 std::stringstream message;
110 message << "Could not read " << name << " from " << loc_.getFileName()
111 << ":" << path_ << std::endl;
112
113 throw std::runtime_error(message.str());
114 }
115 }
116
117 CheckpointReader openChild(const std::string& childName) const {
118 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
119
120 try {
121 return CheckpointReader(loc_.openGroup(childName),
122 path_ + "/" + childName);
123 } catch (H5::Exception&) {
124 std::stringstream message;
125 message << "Could not open " << loc_.getFileName() << ":/" << path_ << "/"
126 << childName << std::endl;
127
128 throw std::runtime_error(message.str());
129 }
130 }
131
132 std::vector<std::string> getChildGroupNames() const {
133 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
134
135 std::vector<std::string> result;
136 char pStr[128];
137 for (hsize_t i = 0; i < loc_.getNumObjs(); i++) {
138 memset(pStr, 0, 128);
139 loc_.getObjnameByIdx(i, pStr, 128);
140 try {
141 loc_.openGroup(pStr);
142 result.push_back(pStr);
143 } catch (H5::Exception&) {
144 ;
145 }
146 }
147 return result;
148 }
149
150 Index getNumDataSets() const { return loc_.getNumObjs(); }
151
152 CptLoc getLoc() { return loc_; }
153
154 template <typename T>
155 CptTable openTable(const std::string& name) {
156 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
157
158 try {
159 CptTable table = CptTable(name, sizeof(typename T::data), loc_);
160 T::SetupCptTable(table);
161 return table;
162 } catch (H5::Exception&) {
163 std::stringstream message;
164 message << "Could not open table " << name << " in " << loc_.getFileName()
165 << ":" << path_ << std::endl;
166 throw std::runtime_error(message.str());
167 }
168 }
169
170 private:
172 const std::string path_;
173 void ReadScalar(const CptLoc& loc, std::string& var,
174 const std::string& name) const {
175
176 H5::Attribute attr = loc.openAttribute(name);
177 H5::StrType stype = attr.getStrType();
178
179 attr.read(stype, var);
180 }
181
182 template <typename T>
183 void ReadScalar(const CptLoc& loc, T& value, const std::string& name) const {
184
185 H5::Attribute attr = loc.openAttribute(name);
186 const H5::DataType* dataType = InferDataType<T>::get();
187
188 attr.read(*dataType, &value);
189 }
190
191 template <typename T>
192 void ReadData(const CptLoc& loc, Eigen::MatrixBase<T>& matrix,
193 const std::string& name) const {
194
195 const H5::DataType* dataType = InferDataType<typename T::Scalar>::get();
196
197 H5::DataSet dataset = loc.openDataSet(name);
198
199 H5::DataSpace dp = dataset.getSpace();
200
201 hsize_t dims[2];
202 dp.getSimpleExtentDims(dims, nullptr); // ndims is always 2 for us
203
204 hsize_t matRows = dims[0];
205 hsize_t matCols = dims[1];
206
207 matrix.derived().resize(matRows, matCols);
208 if (matrix.size() == 0) {
209 return;
210 }
211
212 hsize_t matColSize = matrix.derived().outerStride();
213
214 hsize_t fileRows = matCols;
215
216 hsize_t fStride[2] = {1, fileRows};
217 hsize_t fCount[2] = {1, 1};
218 hsize_t fBlock[2] = {1, fileRows};
219
220 hsize_t mStride[2] = {matColSize, 1};
221 hsize_t mCount[2] = {1, 1};
222 hsize_t mBlock[2] = {matCols, 1};
223
224 hsize_t mDim[2] = {matCols, matColSize};
225 H5::DataSpace mspace(2, mDim);
226
227 for (hsize_t i = 0; i < matRows; i++) {
228 hsize_t fStart[2] = {i, 0};
229 hsize_t mStart[2] = {0, i};
230 dp.selectHyperslab(H5S_SELECT_SET, fCount, fStart, fStride, fBlock);
231 mspace.selectHyperslab(H5S_SELECT_SET, mCount, mStart, mStride, mBlock);
232 dataset.read(matrix.derived().data(), *dataType, mspace, dp);
233 }
234 }
235
236 template <typename T>
237 typename std::enable_if<std::is_fundamental<T>::value>::type ReadData(
238 const CptLoc& loc, std::vector<T>& v, const std::string& name) const {
239
240 H5::DataSet dataset = loc.openDataSet(name);
241 H5::DataSpace dp = dataset.getSpace();
242
243 const H5::DataType* dataType = InferDataType<T>::get();
244
245 hsize_t dims[2];
246 dp.getSimpleExtentDims(dims, nullptr);
247
248 v.resize(dims[0]);
249 if (v.empty()) {
250 return;
251 }
252 try {
253 dataset.read(&(v[0]), *dataType);
254 } catch (H5::Exception&) {
255 std::stringstream message;
256 message << "Could not read " << name << " from " << loc_.getFileName()
257 << ":" << path_ << std::endl;
258 throw std::runtime_error(message.str());
259 }
260 }
261
262 void ReadData(const CptLoc& loc, std::vector<std::string>& v,
263 const std::string& name) const {
264
265 H5::DataSet dataset = loc.openDataSet(name);
266 H5::DataSpace dp = dataset.getSpace();
267
268 const H5::DataType* dataType = InferDataType<std::string>::get();
269
270 hsize_t dims[2];
271 dp.getSimpleExtentDims(dims, nullptr);
272
273 std::vector<char*> temp(dims[0]);
274 if (temp.empty()) {
275 return;
276 }
277 try {
278 dataset.read(temp.data(), *dataType);
279 } catch (H5::Exception&) {
280 std::stringstream message;
281 message << "Could not read " << name << " from " << loc_.getFileName()
282 << ":" << path_ << std::endl;
283 throw std::runtime_error(message.str());
284 }
285 v.reserve(dims[0]);
286 for (char* s : temp) {
287 v.push_back(std::string(s));
288 free(s);
289 }
290 }
291
292 void ReadData(const CptLoc& loc, tools::EigenSystem& sys,
293 const std::string& name) const {
294
295 CptLoc parent = loc.openGroup(name);
296 ReadData(parent, sys.eigenvalues(), "eigenvalues");
297 ReadData(parent, sys.eigenvectors(), "eigenvectors");
298 ReadData(parent, sys.eigenvectors2(), "eigenvectors2");
299 Index info;
300 ReadScalar(parent, info, "info");
301 sys.info() = static_cast<Eigen::ComputationInfo>(info);
302 }
303
304 void ReadData(const CptLoc& loc, std::vector<Eigen::Vector3d>& v,
305 const std::string& name) const {
306
307 CptLoc parent = loc.openGroup(name);
308 size_t count = parent.getNumObjs();
309
310 v.resize(count);
311
312 size_t c = 0;
313 for (auto& vec : v) {
314 ReadData(parent, vec, "ind" + std::to_string(c));
315 ++c;
316 }
317 }
318};
319
320} // namespace xtp
321} // namespace votca
322
323#if defined(__clang__)
324#elif defined(__GNUC__)
325#pragma GCC diagnostic pop
326#endif
327
328#endif // VOTCA_XTP_CHECKPOINTREADER_H
const Eigen::MatrixXd & eigenvectors2() const
Definition eigensystem.h:36
Eigen::ComputationInfo info() const
Definition eigensystem.h:39
const Eigen::VectorXd & eigenvalues() const
Definition eigensystem.h:30
const Eigen::MatrixXd & eigenvectors() const
Definition eigensystem.h:33
void ReadData(const CptLoc &loc, std::vector< Eigen::Vector3d > &v, const std::string &name) const
void operator()(std::string &var, const std::string &name) const
std::vector< std::string > getChildGroupNames() const
void operator()(bool &v, const std::string &name) const
void ReadScalar(const CptLoc &loc, std::string &var, const std::string &name) const
void ReadScalar(const CptLoc &loc, T &value, const std::string &name) const
std::enable_if< std::is_fundamental< T >::value &&!std::is_same< T, bool >::value >::type operator()(T &var, const std::string &name) const
void ReadData(const CptLoc &loc, tools::EigenSystem &sys, const std::string &name) const
std::enable_if< std::is_fundamental< T >::value >::type ReadData(const CptLoc &loc, std::vector< T > &v, const std::string &name) const
CheckpointReader(const CptLoc &loc, const std::string path)
void ReadData(const CptLoc &loc, std::vector< std::string > &v, const std::string &name) const
CheckpointReader openChild(const std::string &childName) const
std::enable_if<!std::is_fundamental< T >::value >::type operator()(T &var, const std::string &name) const
CheckpointReader(const CptLoc &loc)
void ReadData(const CptLoc &loc, Eigen::MatrixBase< T > &matrix, const std::string &name) const
CptTable openTable(const std::string &name)
std::recursive_mutex & Hdf5Mutex()
H5::Group CptLoc
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26