votca 2024.2-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 try {
57 ReadData(loc_, var, name);
58 } catch (H5::Exception&) {
59 std::stringstream message;
60
61 message << "Could not read " << name << " from " << loc_.getFileName()
62 << ":" << path_ << std::endl;
63
64 throw std::runtime_error(message.str());
65 }
66 }
67
68 template <typename T>
69 typename std::enable_if<std::is_fundamental<T>::value &&
70 !std::is_same<T, bool>::value>::type
71 operator()(T& var, const std::string& name) const {
72 try {
73 ReadScalar(loc_, var, name);
74 } catch (H5::Exception&) {
75 std::stringstream message;
76 message << "Could not read " << name << " from " << loc_.getFileName()
77 << ":" << path_ << "/" << std::endl;
78
79 throw std::runtime_error(message.str());
80 }
81 }
82
83 void operator()(bool& v, const std::string& name) const {
84 Index temp = Index(v);
85 try {
86 ReadScalar(loc_, temp, name);
87 } catch (H5::Exception&) {
88 std::stringstream message;
89 message << "Could not read " << name << " from " << loc_.getFileName()
90 << ":" << path_ << std::endl;
91
92 throw std::runtime_error(message.str());
93 }
94 v = static_cast<bool>(temp);
95 }
96
97 void operator()(std::string& var, const std::string& name) const {
98 try {
99 ReadScalar(loc_, var, name);
100 } catch (H5::Exception&) {
101 std::stringstream message;
102 message << "Could not read " << name << " from " << loc_.getFileName()
103 << ":" << path_ << std::endl;
104
105 throw std::runtime_error(message.str());
106 }
107 }
108
109 CheckpointReader openChild(const std::string& childName) const {
110 try {
111 return CheckpointReader(loc_.openGroup(childName),
112 path_ + "/" + childName);
113 } catch (H5::Exception&) {
114 std::stringstream message;
115 message << "Could not open " << loc_.getFileName() << ":/" << path_ << "/"
116 << childName << std::endl;
117
118 throw std::runtime_error(message.str());
119 }
120 }
121
122 std::vector<std::string> getChildGroupNames() const {
123 std::vector<std::string> result;
124 char pStr[128];
125 for (hsize_t i = 0; i < loc_.getNumObjs(); i++) {
126 memset(pStr, 0, 128);
127 loc_.getObjnameByIdx(i, pStr, 128);
128 try {
129 loc_.openGroup(pStr);
130 result.push_back(pStr);
131 } catch (H5::Exception&) {
132 ;
133 }
134 }
135 return result;
136 }
137
138 Index getNumDataSets() const { return loc_.getNumObjs(); }
139
140 CptLoc getLoc() { return loc_; }
141
142 template <typename T>
143 CptTable openTable(const std::string& name) {
144 try {
145 CptTable table = CptTable(name, sizeof(typename T::data), loc_);
146 T::SetupCptTable(table);
147 return table;
148 } catch (H5::Exception&) {
149 std::stringstream message;
150 message << "Could not open table " << name << " in " << loc_.getFileName()
151 << ":" << path_ << std::endl;
152 throw std::runtime_error(message.str());
153 }
154 }
155
156 private:
158 const std::string path_;
159 void ReadScalar(const CptLoc& loc, std::string& var,
160 const std::string& name) const {
161
162 H5::Attribute attr = loc.openAttribute(name);
163 H5::StrType stype = attr.getStrType();
164
165 attr.read(stype, var);
166 }
167
168 template <typename T>
169 void ReadScalar(const CptLoc& loc, T& value, const std::string& name) const {
170
171 H5::Attribute attr = loc.openAttribute(name);
172 const H5::DataType* dataType = InferDataType<T>::get();
173
174 attr.read(*dataType, &value);
175 }
176
177 template <typename T>
178 void ReadData(const CptLoc& loc, Eigen::MatrixBase<T>& matrix,
179 const std::string& name) const {
180
181 const H5::DataType* dataType = InferDataType<typename T::Scalar>::get();
182
183 H5::DataSet dataset = loc.openDataSet(name);
184
185 H5::DataSpace dp = dataset.getSpace();
186
187 hsize_t dims[2];
188 dp.getSimpleExtentDims(dims, nullptr); // ndims is always 2 for us
189
190 hsize_t matRows = dims[0];
191 hsize_t matCols = dims[1];
192
193 matrix.derived().resize(matRows, matCols);
194 if (matrix.size() == 0) {
195 return;
196 }
197
198 hsize_t matColSize = matrix.derived().outerStride();
199
200 hsize_t fileRows = matCols;
201
202 hsize_t fStride[2] = {1, fileRows};
203 hsize_t fCount[2] = {1, 1};
204 hsize_t fBlock[2] = {1, fileRows};
205
206 hsize_t mStride[2] = {matColSize, 1};
207 hsize_t mCount[2] = {1, 1};
208 hsize_t mBlock[2] = {matCols, 1};
209
210 hsize_t mDim[2] = {matCols, matColSize};
211 H5::DataSpace mspace(2, mDim);
212
213 for (hsize_t i = 0; i < matRows; i++) {
214 hsize_t fStart[2] = {i, 0};
215 hsize_t mStart[2] = {0, i};
216 dp.selectHyperslab(H5S_SELECT_SET, fCount, fStart, fStride, fBlock);
217 mspace.selectHyperslab(H5S_SELECT_SET, mCount, mStart, mStride, mBlock);
218 dataset.read(matrix.derived().data(), *dataType, mspace, dp);
219 }
220 }
221
222 template <typename T>
223 typename std::enable_if<std::is_fundamental<T>::value>::type ReadData(
224 const CptLoc& loc, std::vector<T>& v, const std::string& name) const {
225
226 H5::DataSet dataset = loc.openDataSet(name);
227 H5::DataSpace dp = dataset.getSpace();
228
229 const H5::DataType* dataType = InferDataType<T>::get();
230
231 hsize_t dims[2];
232 dp.getSimpleExtentDims(dims, nullptr);
233
234 v.resize(dims[0]);
235 if (v.empty()) {
236 return;
237 }
238 try {
239 dataset.read(&(v[0]), *dataType);
240 } catch (H5::Exception&) {
241 std::stringstream message;
242 message << "Could not read " << name << " from " << loc_.getFileName()
243 << ":" << path_ << std::endl;
244 throw std::runtime_error(message.str());
245 }
246 }
247
248 void ReadData(const CptLoc& loc, std::vector<std::string>& v,
249 const std::string& name) const {
250
251 H5::DataSet dataset = loc.openDataSet(name);
252 H5::DataSpace dp = dataset.getSpace();
253
254 const H5::DataType* dataType = InferDataType<std::string>::get();
255
256 hsize_t dims[2];
257 dp.getSimpleExtentDims(dims, nullptr);
258
259 std::vector<char*> temp(dims[0]);
260 if (temp.empty()) {
261 return;
262 }
263 try {
264 dataset.read(temp.data(), *dataType);
265 } catch (H5::Exception&) {
266 std::stringstream message;
267 message << "Could not read " << name << " from " << loc_.getFileName()
268 << ":" << path_ << std::endl;
269 throw std::runtime_error(message.str());
270 }
271 v.reserve(dims[0]);
272 for (char* s : temp) {
273 v.push_back(std::string(s));
274 free(s);
275 }
276 }
277
278 void ReadData(const CptLoc& loc, tools::EigenSystem& sys,
279 const std::string& name) const {
280
281 CptLoc parent = loc.openGroup(name);
282 ReadData(parent, sys.eigenvalues(), "eigenvalues");
283 ReadData(parent, sys.eigenvectors(), "eigenvectors");
284 ReadData(parent, sys.eigenvectors2(), "eigenvectors2");
285 Index info;
286 ReadScalar(parent, info, "info");
287 sys.info() = static_cast<Eigen::ComputationInfo>(info);
288 }
289
290 void ReadData(const CptLoc& loc, std::vector<Eigen::Vector3d>& v,
291 const std::string& name) const {
292
293 CptLoc parent = loc.openGroup(name);
294 size_t count = parent.getNumObjs();
295
296 v.resize(count);
297
298 size_t c = 0;
299 for (auto& vec : v) {
300 ReadData(parent, vec, "ind" + std::to_string(c));
301 ++c;
302 }
303 }
304};
305
306} // namespace xtp
307} // namespace votca
308
309#if defined(__clang__)
310#elif defined(__GNUC__)
311#pragma GCC diagnostic pop
312#endif
313
314#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)
H5::Group CptLoc
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26