votca 2024.2-dev
Loading...
Searching...
No Matches
checkpoint.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Standard includes
21#include <fstream>
22#include <iostream>
23#include <stdexcept>
24#include <string>
25#include <type_traits>
26#include <typeinfo>
27#include <vector>
28
29#include <filesystem>
30
31// Local VOTCA includes
36
37namespace votca {
38namespace xtp {
39
40using namespace checkpoint_utils;
41namespace fs = std::filesystem;
42
43std::ostream& operator<<(std::ostream& s, CheckpointAccessLevel l) {
44
45 switch (l) {
47 s << "read";
48 break;
50 s << "modify";
51 break;
53 s << "create";
54 break;
55 }
56
57 return s;
58}
59
60bool FileExists(const std::string& fileName) { return fs::exists(fileName); }
61
64
66 : fileName_(fN), accessLevel_(access) {
67
68 try {
69 H5::Exception::dontPrint();
70 hid_t fcpl_id = H5Pcreate(H5P_FILE_CREATE);
71 H5::FileCreatPropList fcpList(fcpl_id);
72 switch (accessLevel_) {
73#if defined(__clang__)
74#elif defined(__GNUC__)
75#pragma GCC diagnostic push
76#pragma GCC diagnostic ignored "-Wdeprecated-copy"
77#endif
79 fileHandle_ = H5::H5File(fileName_, H5F_ACC_RDONLY);
80 break;
82 fileHandle_ = H5::H5File(fileName_, H5F_ACC_TRUNC, fcpList);
83 break;
85 if (!FileExists(fileName_)) {
86 fileHandle_ = H5::H5File(fileName_, H5F_ACC_TRUNC, fcpList);
87 } else {
88 fileHandle_ = H5::H5File(fileName_, H5F_ACC_RDWR, fcpList);
89 }
90#if defined(__clang__)
91#elif defined(__GNUC__)
92#pragma GCC diagnostic pop
93#endif
94 }
95
96 } catch (H5::Exception&) {
97 std::stringstream message;
98 message << "Could not access file " << fileName_;
99 message << " with permission to " << accessLevel_ << "." << std::endl;
100
101 throw std::runtime_error(message.str());
102 }
103}
104
105std::string CheckpointFile::getFileName() { return fileName_; }
106
108
111 throw std::runtime_error("Checkpoint file opened as read only.");
112 }
113
114 try {
115 return CheckpointWriter(fileHandle_.createGroup(path_), path_);
116 } catch (H5::Exception&) {
117 try {
118 return CheckpointWriter(fileHandle_.openGroup(path_), path_);
119 } catch (H5::Exception&) {
120 std::stringstream message;
121 message << "Could not create or open " << fileName_ << ":" << path_
122 << std::endl;
123
124 throw std::runtime_error(message.str());
125 }
126 }
127}
128
130
132 try {
133 return CheckpointReader(fileHandle_.openGroup(path_), path_);
134 } catch (H5::Exception&) {
135 std::stringstream message;
136 message << "Could not open " << fileName_ << ":" << path_ << std::endl;
137
138 throw std::runtime_error(message.str());
139 }
140}
141
143
144} // namespace xtp
145} // namespace votca
CheckpointAccessLevel accessLevel_
Definition checkpoint.h:69
CheckpointFile(std::string fN)
Definition checkpoint.cc:62
CheckpointReader getReader()
CheckpointWriter getWriter()
std::ostream & operator<<(std::ostream &out, const Correlate &c)
Definition correlate.h:53
bool FileExists(const std::string &fileName)
Definition checkpoint.cc:60
CheckpointAccessLevel
Definition checkpoint.h:42
base class for all analysis tools
Definition basebead.h:33