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 // VOTCA includes
21 #include <votca/tools/elements.h>
22 
23 // Local VOTCA includes
24 #include "votca/xtp/atom.h"
25 #include "votca/xtp/checkpointtable.h"
26 
27 namespace votca {
28 namespace xtp {
29 
30 Atom::Atom(Index resnr, std::string md_atom_name, Index atom_id,
31            Eigen::Vector3d pos, std::string type)
32     : id_(atom_id), name_(md_atom_name), resnr_(resnr), pos_(pos) {
33 
34   std::string elename = GetElementFromString(md_atom_name);
35   std::string eletype = GetElementFromString(type);
36   tools::Elements ele;
37   bool found_element_name = true;
38   bool found_element_type = true;
39   try {
40     ele.getMass(elename);
41   } catch (std::runtime_error&) {
42     found_element_name = false;
43   }
44 
45   try {
46     ele.getMass(eletype);
47   } catch (std::runtime_error&) {
48     found_element_type = false;
49   }
50 
51   if (found_element_name && found_element_type) {
52     if (elename != eletype) {
53       throw std::runtime_error("Elements " + elename + " and" + eletype +
54                                " from atom name: " + md_atom_name +
55                                " and atom type:" + type + " do not match.");
56     }
57     element_ = elename;
58   } else if (found_element_name) {
59     element_ = elename;
60   } else if (found_element_type) {
61     element_ = elename;
62   } else {
63     throw std::runtime_error("Could not get Element from atom name:" +
64                              md_atom_name + " or atom type:" + type);
65   }
66 }
67 
68 Atom::Atom(Index atom_id, std::string element, Eigen::Vector3d pos)
69     : Atom(-1, element, atom_id, pos, element) {}
70 
71 std::string Atom::GetElementFromString(const std::string& MDName) {
72   std::string element = MDName.substr(0, 1);
73 
74   if (MDName.size() > 1) {
75     if (std::islower(MDName[1])) {
76       element += MDName[1];
77     }
78   }
79   return element;
80 }
81 
82 void Atom::Rotate(const Eigen::Matrix3d& R, const Eigen::Vector3d& refPos) {
83   Eigen::Vector3d dir = pos_ - refPos;
84   dir = R * dir;
85   pos_ = refPos + dir;  // Rotated Position
86 }
87 
88 void Atom::SetupCptTable(CptTable& table) {
89   table.addCol<Index>("index", HOFFSET(data, id));
90   table.addCol<std::string>("element", HOFFSET(data, element));
91   table.addCol<std::string>("name", HOFFSET(data, name));
92   table.addCol<double>("pos.x", HOFFSET(data, x));
93   table.addCol<double>("pos.y", HOFFSET(data, y));
94   table.addCol<double>("pos.z", HOFFSET(data, z));
95   table.addCol<Index>("resnr", HOFFSET(data, resnr));
96 }
97 
98 void Atom::WriteData(data& d) const {
99   d.id = id_;
100   d.element = const_cast<char*>(element_.c_str());
101   d.name = const_cast<char*>(name_.c_str());
102   d.x = pos_[0];
103   d.y = pos_[1];
104   d.z = pos_[2];
105   d.resnr = resnr_;
106 }
107 
108 void Atom::ReadData(const data& d) {
109   id_ = d.id;
110   element_ = std::string(d.element);
111   free(d.element);
112   name_ = std::string(d.name);
113   free(d.name);
114   pos_[0] = d.x;
115   pos_[2] = d.z;
116   pos_[1] = d.y;
117   resnr_ = d.resnr;
118 }
119 }  // namespace xtp
120 }  // namespace votca
121