votca 2025.1-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 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
69
70 try {
71 H5::Exception::dontPrint();
72 hid_t fcpl_id = H5Pcreate(H5P_FILE_CREATE);
73 H5::FileCreatPropList fcpList(fcpl_id);
74 switch (accessLevel_) {
75#if defined(__clang__)
76#elif defined(__GNUC__)
77#pragma GCC diagnostic push
78#pragma GCC diagnostic ignored "-Wdeprecated-copy"
79#endif
81 fileHandle_ = H5::H5File(fileName_, H5F_ACC_RDONLY);
82 break;
84 fileHandle_ = H5::H5File(fileName_, H5F_ACC_TRUNC, fcpList);
85 break;
87 if (!FileExists(fileName_)) {
88 fileHandle_ = H5::H5File(fileName_, H5F_ACC_TRUNC, fcpList);
89 } else {
90 fileHandle_ = H5::H5File(fileName_, H5F_ACC_RDWR, fcpList);
91 }
92#if defined(__clang__)
93#elif defined(__GNUC__)
94#pragma GCC diagnostic pop
95#endif
96 }
97
98 } catch (H5::Exception&) {
99 std::stringstream message;
100 message << "Could not access file " << fileName_;
101 message << " with permission to " << accessLevel_ << "." << std::endl;
102
103 throw std::runtime_error(message.str());
104 }
105}
106
107std::string CheckpointFile::getFileName() { return fileName_; }
108
110
113 throw std::runtime_error("Checkpoint file opened as read only.");
114 }
115 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
116
117 try {
118 return CheckpointWriter(fileHandle_.createGroup(path_), path_);
119 } catch (H5::Exception&) {
120 try {
121 return CheckpointWriter(fileHandle_.openGroup(path_), path_);
122 } catch (H5::Exception&) {
123 std::stringstream message;
124 message << "Could not create or open " << fileName_ << ":" << path_
125 << std::endl;
126
127 throw std::runtime_error(message.str());
128 }
129 }
130}
131
133
135 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
136
137 try {
138 return CheckpointReader(fileHandle_.openGroup(path_), path_);
139 } catch (H5::Exception&) {
140 std::stringstream message;
141 message << "Could not open " << fileName_ << ":" << path_ << std::endl;
142
143 throw std::runtime_error(message.str());
144 }
145}
146
148
149} // namespace xtp
150} // 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
std::recursive_mutex & Hdf5Mutex()
Charge transport classes.
Definition ERIs.h:28
bool FileExists(const std::string &fileName)
Definition checkpoint.cc:60
CheckpointAccessLevel
Definition checkpoint.h:42
Provides a means for comparing floating point numbers.
Definition basebead.h:33