votca 2024.2-dev
Loading...
Searching...
No Matches
statesaver.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// Third party includes
21#include <boost/interprocess/sync/file_lock.hpp>
22
23// VOTCA includes
25
26// Local VOTCA includes
28#include "votca/xtp/topology.h"
29
30namespace votca {
31namespace xtp {
32
33std::vector<Index> StateSaver::getFrames() const {
36 std::vector<Index> frames = std::vector<Index>{};
37 try {
38 r(frames, "frames");
39 } catch (std::runtime_error&) {
40 ;
41 }
42 return frames;
43}
46 std::cout << "Creating statefile " << hdf5file_ << std::endl;
48 }
49 boost::interprocess::file_lock flock(hdf5file_.c_str());
50 flock.lock_sharable();
51 if (!TopStepisinFrames(top.getStep())) {
52 std::vector<Index> frames = this->getFrames();
53 frames.push_back(top.getStep());
56 w(frames, "frames");
57 std::cout << "Frame with id " << top.getStep() << " was not in statefile "
58 << hdf5file_ << " ,adding it now." << std::endl;
59 }
60
62 CheckpointWriter w = cpf.getWriter("/frame_" + std::to_string(top.getStep()));
63 top.WriteToCpt(w);
64
65 flock.unlock_sharable();
66
67 std::cout << "Wrote MD topology (step = " << top.getStep()
68 << ", time = " << top.getTime() << ") to " << hdf5file_
69 << std::endl;
70 std::cout << "... ";
71
72 std::cout << ". " << std::endl;
73 return;
74}
75
78 throw std::runtime_error("Statefile " + hdf5file_ + " does not exist.");
79 }
80 std::cout << "Import MD Topology (i.e. frame " << frameid << ")"
81 << " from " << hdf5file_ << std::endl;
82 std::cout << "...";
83 boost::interprocess::file_lock flock(hdf5file_.c_str());
84 flock.lock_sharable();
85 if (!TopStepisinFrames(frameid)) {
86 throw std::runtime_error("Frame with id " + std::to_string(frameid) +
87 " is not in statefile.");
88 }
89
91 CheckpointReader r = cpf.getReader("/frame_" + std::to_string(frameid));
92 Topology top;
93 top.ReadFromCpt(r);
94 flock.unlock_sharable();
95 std::cout << ". " << std::endl;
96 return top;
97}
98
100 std::vector<Index> frames = this->getFrames();
101 return std::find(frames.begin(), frames.end(), frameid) != frames.end();
102}
103
104} // namespace xtp
105} // namespace votca
CheckpointReader getReader()
CheckpointWriter getWriter()
std::string hdf5file_
Definition statesaver.h:47
Topology ReadFrame(Index frameid) const
Definition statesaver.cc:76
void WriteFrame(const Topology &top)
Definition statesaver.cc:44
bool TopStepisinFrames(Index frameid) const
Definition statesaver.cc:99
std::vector< Index > getFrames() const
Definition statesaver.cc:33
Container for segments and box and atoms.
Definition topology.h:41
double getTime() const
Definition topology.h:77
void WriteToCpt(CheckpointWriter &w) const
Definition topology.cc:183
Index getStep() const
Definition topology.h:75
void ReadFromCpt(CheckpointReader &r)
Definition topology.cc:198
bool FileExists(const std::string &filename)
Definition filesystem.cc:50
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26