votca 2024.2-dev
Loading...
Searching...
No Matches
topology.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2023 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/lexical_cast.hpp>
22
23// VOTCA includes
24#include <votca/csg/pdbwriter.h>
25#include <votca/tools/globals.h>
26#include <votca/tools/version.h>
27
28// Local VOTCA includes
29#include "votca/xtp/atom.h"
31#include "votca/xtp/segment.h"
32#include "votca/xtp/topology.h"
33
34namespace votca {
35namespace xtp {
36
38 segments_ = top.segments_;
39 time_ = top.time_;
40 step_ = top.step_;
41 this->setBox(top.getBox());
42 for (const QMPair *pair : top.NBList()) {
43 const Segment &seg1 = segments_[pair->Seg1()->getId()];
44 const Segment &seg2 = segments_[pair->Seg2()->getId()];
45 nblist_.Add(seg1, seg2, pair->R());
46 }
47}
48
50 if (&top != this) {
51 segments_ = top.segments_;
52 time_ = top.time_;
53 step_ = top.step_;
54 this->setBox(top.getBox());
56 for (const QMPair *pair : top.NBList()) {
57 const Segment &seg1 = segments_[pair->Seg1()->getId()];
58 const Segment &seg2 = segments_[pair->Seg2()->getId()];
59 nblist_.Add(seg1, seg2, pair->R());
60 }
61 }
62 return *this;
63}
64
65Segment &Topology::AddSegment(std::string segment_name) {
66 Index segment_id = Index(segments_.size());
67 segments_.push_back(Segment(segment_name, segment_id));
68 return segments_.back();
69}
70
71// +++++++++++++++++ //
72// Periodic Boundary //
73// +++++++++++++++++ //
74void Topology::setBox(const Eigen::Matrix3d &box,
76
77 // Determine box type automatically in case boxtype == typeAuto
78 if (boxtype == csg::BoundaryCondition::typeAuto) {
79 boxtype = AutoDetectBoxType(box);
80 }
81
82 if (bc_ != nullptr) {
83 if (Log::verbose()) {
84 std::cout << "Removing periodic box. Creating new... " << std::endl;
85 }
86 }
87 bc_.reset(nullptr);
88 switch (boxtype) {
90 bc_.reset(new csg::TriclinicBox());
91 break;
93 bc_.reset(new csg::OrthorhombicBox());
94 break;
95 default:
96 bc_.reset(new csg::OpenBox());
97 break;
98 }
99
100 bc_->setBox(box);
101}
102
104 const Eigen::Matrix3d &box) {
105
106 // Set box type to OpenBox in case "box" is the zero matrix,
107 // to OrthorhombicBox in case "box" is a diagonal matrix,
108 // or to TriclinicBox otherwise
109
110 if (box.isApproxToConstant(0)) {
111 std::cout << "WARNING: No box vectors specified in trajectory."
112 "Using open-box boundary conditions. "
113 << std::endl;
115 }
116
117 else if ((box - Eigen::Matrix3d(box.diagonal().asDiagonal()))
118 .isApproxToConstant(0)) {
120 }
121
122 else {
124 }
125
127}
128
129Eigen::Vector3d Topology::PbShortestConnect(const Eigen::Vector3d &r1,
130 const Eigen::Vector3d &r2) const {
131 return bc_->BCShortestConnection(r1, r2);
132}
133
135 const Segment &seg2) const {
136 double R2 = std::numeric_limits<double>::max();
137 for (const Atom &atom1 : seg1) {
138 for (const Atom &atom2 : seg2) {
139 double R2_test =
140 PbShortestConnect(atom1.getPos(), atom2.getPos()).squaredNorm();
141 if (R2_test < R2) {
142 R2 = R2_test;
143 }
144 }
145 }
146 return std::sqrt(R2);
147}
148
149std::vector<const Segment *> Topology::FindAllSegmentsOnMolecule(
150 const Segment &seg1, const Segment &seg2) const {
151 const std::vector<Index> &ids1 = seg1.getMoleculeIds();
152 const std::vector<Index> &ids2 = seg2.getMoleculeIds();
153 std::vector<Index> common_elements;
154 std::set_intersection(ids1.begin(), ids1.end(), ids2.begin(), ids2.end(),
155 std::back_inserter(common_elements));
156 std::vector<const Segment *> results;
157 if (common_elements.empty() || common_elements.size() > 1) {
158 return results;
159 }
160 Index molid = common_elements[0];
161
162 for (const Segment &seg : segments_) {
163 if (std::find(seg.getMoleculeIds().begin(), seg.getMoleculeIds().end(),
164 molid) != seg.getMoleculeIds().end()) {
165 results.push_back(&seg);
166 }
167 }
168 return results;
169}
170
171void Topology::WriteToPdb(std::string filename) const {
172
173 csg::PDBWriter writer;
174 writer.Open(filename, false);
175 writer.WriteHeader("Frame:" + std::to_string(this->getStep()));
176 writer.WriteBox(this->getBox() * tools::conv::bohr2ang);
177 for (const Segment &seg : segments_) {
178 writer.WriteContainer(seg);
179 }
180 writer.Close();
181}
182
184 w(votca::tools::ToolsVersionStr(), "XTPVersion");
185 w(topology_version(), "version");
186 w(time_, "time");
187 w(step_, "step");
188 w(this->getBox(), "box");
189 CheckpointWriter ww = w.openChild("segments");
190 for (const Segment &seg : segments_) {
191 CheckpointWriter u = ww.openChild("segment" + std::to_string(seg.getId()));
192 seg.WriteToCpt(u);
193 }
194 CheckpointWriter www = w.openChild("neighborlist");
195 nblist_.WriteToCpt(www);
196}
197
199 r(time_, "time");
200 r(step_, "step");
201 Eigen::Matrix3d box;
202 r(box, "box");
203 setBox(box);
204 CheckpointReader v = r.openChild("segments");
205 segments_.clear();
206 Index count = v.getNumDataSets();
207 segments_.reserve(count);
208 for (Index i = 0; i < count; i++) {
209 CheckpointReader w = v.openChild("segment" + std::to_string(i));
210 segments_.push_back(Segment(w));
211 }
212 CheckpointReader rr = r.openChild("neighborlist");
214}
215
216} // namespace xtp
217} // namespace votca
void WriteHeader(std::string header)
Definition pdbwriter.cc:41
void WriteBox(const Eigen::Matrix3d &box)
Definition pdbwriter.cc:62
void Close() override
Definition pdbwriter.cc:51
void WriteContainer(T &container)
Definition pdbwriter.h:102
void Open(std::string file, bool bAppend=false) override
Definition pdbwriter.cc:33
CheckpointReader openChild(const std::string &childName) const
CheckpointWriter openChild(const std::string &childName) const
void ReadFromCpt(CheckpointReader &r, const std::vector< Segment > &segments)
Definition qmnblist.cc:55
void WriteToCpt(CheckpointWriter &w) const
Definition qmnblist.cc:37
QMPair & Add(const Segment &seg1, const Segment &seg2, const Eigen::Vector3d &r)
Definition qmnblist.cc:27
const std::vector< Index > & getMoleculeIds() const
Definition segment.h:93
Container for segments and box and atoms.
Definition topology.h:41
void WriteToCpt(CheckpointWriter &w) const
Definition topology.cc:183
Index getStep() const
Definition topology.h:75
std::unique_ptr< csg::BoundaryCondition > bc_
Definition topology.h:94
Segment & AddSegment(std::string segment_name)
Definition topology.cc:65
std::vector< Segment > segments_
Definition topology.h:92
Eigen::Vector3d PbShortestConnect(const Eigen::Vector3d &r1, const Eigen::Vector3d &r2) const
Definition topology.cc:129
void setBox(const Eigen::Matrix3d &box, csg::BoundaryCondition::eBoxtype boxtype=csg::BoundaryCondition::typeAuto)
Definition topology.cc:74
std::vector< const Segment * > FindAllSegmentsOnMolecule(const Segment &seg1, const Segment &seg2) const
Definition topology.cc:149
csg::BoundaryCondition::eBoxtype AutoDetectBoxType(const Eigen::Matrix3d &box)
Definition topology.cc:103
const Eigen::Matrix3d & getBox() const
Definition topology.h:64
void ReadFromCpt(CheckpointReader &r)
Definition topology.cc:198
void WriteToPdb(std::string filename) const
Definition topology.cc:171
QMNBList & NBList()
Definition topology.h:70
Topology & operator=(const Topology &top)
Definition topology.cc:49
double GetShortestDist(const Segment &seg1, const Segment &seg2) const
Definition topology.cc:134
static constexpr int topology_version()
Definition topology.h:103
const double bohr2ang
Definition constants.h:49
const std::string & ToolsVersionStr()
Definition version.cc:33
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26
static bool verbose()
Definition globals.h:32