votca 2024-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_) {
74 fileHandle_ = H5::H5File(fileName_, H5F_ACC_RDONLY);
75 break;
77 fileHandle_ = H5::H5File(fileName_, H5F_ACC_TRUNC, fcpList);
78 break;
80 if (!FileExists(fileName_)) {
81 fileHandle_ = H5::H5File(fileName_, H5F_ACC_TRUNC, fcpList);
82 } else {
83 fileHandle_ = H5::H5File(fileName_, H5F_ACC_RDWR, fcpList);
84 }
85 }
86
87 } catch (H5::Exception&) {
88 std::stringstream message;
89 message << "Could not access file " << fileName_;
90 message << " with permission to " << accessLevel_ << "." << std::endl;
91
92 throw std::runtime_error(message.str());
93 }
94}
95
96std::string CheckpointFile::getFileName() { return fileName_; }
97
98H5::H5File CheckpointFile::getHandle() { return fileHandle_; }
99
102 throw std::runtime_error("Checkpoint file opened as read only.");
103 }
104
105 try {
106 return CheckpointWriter(fileHandle_.createGroup(path_), path_);
107 } catch (H5::Exception&) {
108 try {
109 return CheckpointWriter(fileHandle_.openGroup(path_), path_);
110 } catch (H5::Exception&) {
111 std::stringstream message;
112 message << "Could not create or open " << fileName_ << ":" << path_
113 << std::endl;
114
115 throw std::runtime_error(message.str());
116 }
117 }
118}
119
121
123 try {
124 return CheckpointReader(fileHandle_.openGroup(path_), path_);
125 } catch (H5::Exception&) {
126 std::stringstream message;
127 message << "Could not open " << fileName_ << ":" << path_ << std::endl;
128
129 throw std::runtime_error(message.str());
130 }
131}
132
134
135} // namespace xtp
136} // namespace votca
CheckpointAccessLevel accessLevel_
Definition checkpoint.h:63
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:36
base class for all analysis tools
Definition basebead.h:33