votca 2024.2-dev
Loading...
Searching...
No Matches
atomcontainer.h
Go to the documentation of this file.
1
2
3/*
4 * Copyright 2009-2024 The VOTCA Development Team
5 * (http://www.votca.org)
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License")
8 *
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22#pragma once
23#ifndef VOTCA_XTP_ATOMCONTAINER_H
24#define VOTCA_XTP_ATOMCONTAINER_H
25
26// Standard includes
27#include <limits>
28#include <typeinfo>
29
30// VOTCA includes
32
33// Local VOTCA includes
34#include "checkpoint.h"
35#include "eigen.h"
36
44namespace votca {
45namespace xtp {
46
47template <class T>
49 public:
50 AtomContainer(std::string type, Index id) : type_(type), id_(id) {};
51
52 using Atom_Type = T;
53
55 virtual ~AtomContainer() = default;
56
57 using iterator = typename std::vector<T>::iterator;
58
59 const std::string& getType() const { return type_; }
60
61 void setType(std::string type) { type_ = type; }
62
63 void clearAtoms() { atomlist_.clear(); }
64
65 Index getId() const { return id_; }
66
67 Index size() const { return atomlist_.size(); }
68
69 void push_back(const T& atom) {
70 atomlist_.push_back(atom);
71 calcPos();
72 }
73 void push_back(T&& atom) {
74 atomlist_.push_back(atom);
75 calcPos();
76 }
77
78 const T& at(Index index) const { return atomlist_.at(index); }
79 T& at(Index index) { return atomlist_.at(index); }
80
81 const T& operator[](Index index) const { return atomlist_[index]; }
82 T& operator[](Index index) { return atomlist_[index]; }
83
84 typename std::vector<T>::iterator begin() { return atomlist_.begin(); }
85 typename std::vector<T>::iterator end() { return atomlist_.end(); }
86
87 typename std::vector<T>::const_iterator begin() const {
88 return atomlist_.begin();
89 }
90 typename std::vector<T>::const_iterator end() const {
91 return atomlist_.end();
92 }
93
94 const Eigen::Vector3d& getPos() const { return pos_; }
95
96 // calculates the lowest and highest point in the cube, sorrounding the
97 // molecule
98 std::pair<Eigen::Vector3d, Eigen::Vector3d> CalcSpatialMinMax() const {
99 std::pair<Eigen::Vector3d, Eigen::Vector3d> result;
100 Eigen::Vector3d min =
101 std::numeric_limits<double>::max() * Eigen::Vector3d::Ones();
102 Eigen::Vector3d max =
103 std::numeric_limits<double>::min() * Eigen::Vector3d::Ones();
104 for (const T& atom : atomlist_) {
105 const Eigen::Vector3d& pos = atom.getPos();
106 if (pos.x() < min.x()) {
107 min.x() = pos.x();
108 }
109 if (pos.x() > max.x()) {
110 max.x() = pos.x();
111 }
112 if (pos.y() < min.y()) {
113 min.y() = pos.y();
114 }
115 if (pos.y() > max.y()) {
116 max.y() = pos.y();
117 }
118 if (pos.z() < min.z()) {
119 min.z() = pos.z();
120 }
121 if (pos.z() > max.z()) {
122 max.z() = pos.z();
123 }
124 }
125 result.first = min;
126 result.second = max;
127 return result;
128 }
129
130 std::vector<std::string> FindUniqueElements() const {
131 std::vector<std::string> result;
132 for (const T& atom : atomlist_) {
133 if (std::find(result.begin(), result.end(), atom.getElement()) ==
134 result.end()) {
135 result.push_back(atom.getElement());
136 }
137 }
138 return result;
139 }
140
141 void Translate(const Eigen::Vector3d& shift) {
142 for (T& atom : atomlist_) {
143 atom.Translate(shift);
144 }
145 pos_ += shift;
146 }
147
148 void Rotate(const Eigen::Matrix3d& R, const Eigen::Vector3d& ref_pos) {
149 for (T& atom : atomlist_) {
150 atom.Rotate(R, ref_pos);
151 }
152 calcPos();
153 }
154
155 virtual void WriteToCpt(CheckpointWriter& w) const {
156 w(type_, "type");
157 w(id_, "id");
158 w(int(atomlist_.size()), "size");
159 T element(0, "H", Eigen::Vector3d::Zero());
160 CptTable table = w.openTable<T>(element.identify() + "s", atomlist_.size());
161 std::vector<typename T::data> dataVec(atomlist_.size());
162 for (std::size_t i = 0; i < atomlist_.size(); ++i) {
163 atomlist_[i].WriteData(dataVec[i]);
164 }
165
166 table.write(dataVec);
167 }
168 virtual void ReadFromCpt(CheckpointReader& r) {
169 r(type_, "type");
170 r(id_, "id");
171 Index size = 0;
172 r(size, "size");
173 if (size == 0) {
174 return;
175 }
176 T element(0, "H", Eigen::Vector3d::Zero()); // dummy element to get
177 // .identify for type
178 CptTable table = r.openTable<T>(element.identify() + "s");
179 atomlist_.clear();
180 atomlist_.reserve(table.numRows());
181 std::vector<typename T::data> dataVec(table.numRows());
182 table.read(dataVec);
183 for (std::size_t i = 0; i < table.numRows(); ++i) {
184 atomlist_.push_back(T(dataVec[i]));
185 }
186 calcPos();
187 }
188
189 void calcPos() {
190 tools::Elements element;
191 Eigen::Vector3d pos = Eigen::Vector3d::Zero();
192 double totalmass = 0.0;
193 for (const T& atom : atomlist_) {
194 double mass = element.getMass(atom.getElement());
195 totalmass += mass;
196 pos += mass * atom.getPos();
197 }
198 pos_ = pos / totalmass;
199 }
200
201 protected:
202 std::vector<T> atomlist_;
203 std::string type_;
205
206 private:
207 Eigen::Vector3d pos_ = Eigen::Vector3d::Zero();
208};
209} // namespace xtp
210} // namespace votca
211
212#endif // VOTCA_XTP_ATOMCONTAINER_H
information about an element
Definition elements.h:42
const std::string & getType() const
virtual void WriteToCpt(CheckpointWriter &w) const
void Rotate(const Eigen::Matrix3d &R, const Eigen::Vector3d &ref_pos)
void push_back(const T &atom)
std::vector< T >::const_iterator begin() const
std::vector< T >::iterator end()
std::vector< T >::const_iterator end() const
virtual void ReadFromCpt(CheckpointReader &r)
const T & at(Index index) const
void setType(std::string type)
std::pair< Eigen::Vector3d, Eigen::Vector3d > CalcSpatialMinMax() const
std::vector< T >::iterator begin()
AtomContainer(std::string type, Index id)
const T & operator[](Index index) const
AtomContainer(CheckpointReader &r)
std::vector< std::string > FindUniqueElements() const
T & operator[](Index index)
typename std::vector< T >::iterator iterator
const Eigen::Vector3d & getPos() const
virtual ~AtomContainer()=default
std::vector< T > atomlist_
void Translate(const Eigen::Vector3d &shift)
CptTable openTable(const std::string &name)
CptTable openTable(const std::string &name, std::size_t nRows, bool compact=false)
void write(void *buffer, const std::size_t &startIdx, const std::size_t &endIdx)
void read(void *buffer, const std::size_t &startIdx, const std::size_t &endIdx)
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26