votca 2024.2-dev
Loading...
Searching...
No Matches
lammpsdumpreader.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Standard includes
19#include <memory>
20#include <vector>
21
22// Third party includes
23#include <boost/algorithm/string.hpp>
24#include <boost/lexical_cast.hpp>
25
26// VOTCA includes
28#include <votca/tools/getline.h>
29
30// Local private VOTCA includes
31#include "lammpsdumpreader.h"
32
33namespace votca {
34namespace csg {
35using namespace boost;
36using namespace std;
37
39 topology_ = true;
40 top.Cleanup();
41
42 fl_.open(file);
43 if (!fl_.is_open()) {
44 throw std::ios_base::failure("Error on open topology file: " + file);
45 }
46 fname_ = file;
47
48 NextFrame(top);
49
50 fl_.close();
51
52 return true;
53}
54
55bool LAMMPSDumpReader::Open(const string &file) {
56 fl_.open(file);
57 if (!fl_.is_open()) {
58 throw std::ios_base::failure("Error on open trajectory file: " + file);
59 }
60 fname_ = file;
61 return true;
62}
63
64void LAMMPSDumpReader::Close() { fl_.close(); }
65
67 topology_ = false;
68 NextFrame(top);
69 return true;
70}
71
73 string line;
74 tools::getline(fl_, line);
75 boost::algorithm::trim(line);
76 while (!fl_.eof()) {
77 if (line.substr(0, 5) != "ITEM:") {
78 throw std::ios_base::failure("unexpected line in lammps file:\n" + line);
79 }
80 if (line.substr(6, 8) == "TIMESTEP") {
81 ReadTimestep(top);
82 } else if (line.substr(6, 15) == "NUMBER OF ATOMS") {
83 ReadNumAtoms(top);
84 } else if (line.substr(6, 10) == "BOX BOUNDS") {
85 ReadBox(top);
86 } else if (line.substr(6, 5) == "ATOMS") {
87 ReadAtoms(top, line);
88 break;
89 }
90
91 else {
92 throw std::ios_base::failure("unknown item lammps file : " +
93 line.substr(6));
94 }
95 tools::getline(fl_, line);
96 boost::algorithm::trim(line);
97 }
98 if (topology_) {
99 cout << "WARNING: topology created from .dump file, masses, charges, "
100 "types, residue names are wrong!\n";
101 }
102 return !fl_.eof();
103 ;
104}
105
107 string s;
109 boost::algorithm::trim(s);
110 top.setStep(boost::lexical_cast<Index>(s));
111 cout << "Reading frame, timestep " << top.getStep() << endl;
112}
113
115 string s;
116
117 Eigen::Matrix3d m = Eigen::Matrix3d::Zero();
118
119 for (Index i = 0; i < 3; ++i) {
121 boost::algorithm::trim(s);
122 vector<double> v = tools::Tokenizer(s, " ").ToVector<double>();
123 if (v.size() != 2) {
124 throw std::ios_base::failure("invalid box format");
125 }
126 m(i, i) = v[1] - v[0];
127 }
129}
130
132 string s;
134 boost::algorithm::trim(s);
135 natoms_ = boost::lexical_cast<Index>(s);
136 if (!topology_ && natoms_ != top.BeadCount()) {
137 std::runtime_error("number of beads in topology and trajectory differ");
138 }
139}
140
141void LAMMPSDumpReader::ReadAtoms(Topology &top, string itemline) {
142 if (topology_) {
143 top.CreateResidue("dum");
144 if (!top.BeadTypeExist("no")) {
145 top.RegisterBeadType("no");
146 }
147 for (Index i = 0; i < natoms_; ++i) {
148 (void)top.CreateBead(Bead::spherical, "no", "no", 0, 0, 0);
149 }
150 }
151
152 bool pos = false;
153 bool force = false;
154 bool vel = false;
155 Index id = -1;
156
157 vector<string> fields;
158
159 {
160 tools::Tokenizer tok(itemline.substr(12), " ");
161 fields = tok.ToVector();
162 Index j = 0;
163 for (tools::Tokenizer::iterator i = tok.begin(); i != tok.end(); ++i, ++j) {
164 if (*i == "x" || *i == "y" || *i == "z") {
165 pos = true;
166 } else if (*i == "xu" || *i == "yu" || *i == "zu") {
167 pos = true;
168 } else if (*i == "xs" || *i == "ys" || *i == "zs") {
169 pos = true;
170 } else if (*i == "vx" || *i == "vy" || *i == "vz") {
171 vel = true;
172 } else if (*i == "fx" || *i == "fy" || *i == "fz") {
173 force = true;
174 } else if (*i == "id") {
175 id = j;
176 }
177 }
178 }
179 if (id < 0) {
180 throw std::runtime_error(
181 "error, id not found in any column of the atoms section");
182 }
183
184 for (Index i = 0; i < natoms_; ++i) {
185 string s;
187 boost::algorithm::trim(s);
188 if (fl_.eof()) {
189 throw std::runtime_error("Error: unexpected end of lammps file '" +
190 fname_ + "' only " +
191 boost::lexical_cast<string>(i) + " atoms of " +
192 boost::lexical_cast<string>(natoms_) + " read.");
193 }
194
195 tools::Tokenizer tok(s, " ");
197 vector<string> fields2 = tok.ToVector();
198 // internal numbering begins with 0
199 Index atom_id = boost::lexical_cast<Index>(fields2[id]);
200 if (atom_id > natoms_) {
201 throw std::runtime_error(
202 "Error: found atom with id " + boost::lexical_cast<string>(atom_id) +
203 " but only " + boost::lexical_cast<string>(natoms_) +
204 " atoms defined in header of file '" + fname_ + "'");
205 }
206 Bead *b = top.getBead(atom_id - 1);
207 b->HasPos(pos);
208 b->HasF(force);
209 b->HasVel(vel);
210 Eigen::Matrix3d m = top.getBox();
211
212 for (size_t j = 0; itok != tok.end(); ++itok, ++j) {
213 if (j == fields.size()) {
214 throw std::runtime_error(
215 "error, wrong number of columns in atoms section");
216 } else if (fields[j] == "x") {
217 b->Pos().x() = stod(*itok) * tools::conv::ang2nm;
218 } else if (fields[j] == "y") {
219 b->Pos().y() = stod(*itok) * tools::conv::ang2nm;
220 } else if (fields[j] == "z") {
221 b->Pos().z() = stod(*itok) * tools::conv::ang2nm;
222 } else if (fields[j] == "xu") {
223 b->Pos().x() = stod(*itok) * tools::conv::ang2nm;
224 } else if (fields[j] == "yu") {
225 b->Pos().y() = stod(*itok) * tools::conv::ang2nm;
226 } else if (fields[j] == "zu") {
227 b->Pos().z() = stod(*itok) * tools::conv::ang2nm;
228 } else if (fields[j] == "xs") {
229 b->Pos().x() = stod(*itok) * m(0, 0); // box is already in nm
230 } else if (fields[j] == "ys") {
231 b->Pos().y() = stod(*itok) * m(1, 1); // box is already in nm
232 } else if (fields[j] == "zs") {
233 b->Pos().z() = stod(*itok) * m(2, 2); // box is already in nm
234 } else if (fields[j] == "vx") {
235 b->Vel().x() = stod(*itok) * tools::conv::ang2nm;
236 } else if (fields[j] == "vy") {
237 b->Vel().y() = stod(*itok) * tools::conv::ang2nm;
238 } else if (fields[j] == "vz") {
239 b->Vel().z() = stod(*itok) * tools::conv::ang2nm;
240 } else if (fields[j] == "fx") {
241 b->F().x() = stod(*itok) * tools::conv::kcal2kj / tools::conv::ang2nm;
242 } else if (fields[j] == "fy") {
243 b->F().y() = stod(*itok) * tools::conv::kcal2kj / tools::conv::ang2nm;
244 } else if (fields[j] == "fz") {
245 b->F().z() = stod(*itok) * tools::conv::kcal2kj / tools::conv::ang2nm;
246 } else if ((fields[j] == "type") && topology_) {
247 if (!top.BeadTypeExist(*itok)) {
248 top.RegisterBeadType(*itok);
249 }
250 b->setType(*itok);
251 }
252 }
253 }
254}
255
256} // namespace csg
257} // namespace votca
virtual Eigen::Vector3d & Pos()
Definition basebead.h:128
virtual void setType(const std::string &type) noexcept
Definition basebead.h:90
bool HasPos() const noexcept
Definition basebead.h:139
information about a bead
Definition bead.h:50
bool HasF() const noexcept
Definition bead.h:235
Eigen::Vector3d & F()
Definition bead.h:210
bool HasVel() const noexcept
Definition bead.h:232
Eigen::Vector3d & Vel()
Definition bead.h:173
void ReadAtoms(Topology &top, std::string itemline)
bool ReadTopology(std::string file, Topology &top) override
open a topology file
bool NextFrame(Topology &top) override
read in the next frame
bool Open(const std::string &file) override
open a trejectory file
bool FirstFrame(Topology &top) override
read in the first frame
topology of the whole system
Definition topology.h:81
Residue & CreateResidue(std::string name)
Create a new resiude.
Definition topology.h:462
void setBox(const Eigen::Matrix3d &box, BoundaryCondition::eBoxtype boxtype=BoundaryCondition::typeAuto)
Definition topology.h:272
void Cleanup()
Cleans up all the stored data.
Definition topology.cc:49
bool BeadTypeExist(std::string type) const
Determine if a bead type exists.
Definition topology.cc:210
const Eigen::Matrix3d & getBox() const
Definition topology.h:298
Bead * CreateBead(Bead::Symmetry symmetry, std::string name, std::string type, Index resnr, double m, double q)
Creates a new Bead.
Definition topology.h:441
Index BeadCount() const
Definition topology.h:150
Index getStep() const
Definition topology.h:329
void RegisterBeadType(std::string type)
Register the bead type with the topology object.
Definition topology.cc:214
Bead * getBead(const Index i)
Returns a pointer to the bead with index i.
Definition topology.h:227
void setStep(Index s)
Definition topology.h:323
break string into words
Definition tokenizer.h:72
iterator end()
end iterator
Definition tokenizer.h:102
iterator begin()
iterator to first element
Definition tokenizer.h:97
boost::tokenizer< boost::char_separator< char > >::iterator iterator
Definition tokenizer.h:74
std::vector< T > ToVector()
store all words in a vector of type T, does type conversion.
Definition tokenizer.h:109
STL namespace.
const double kcal2kj
Definition constants.h:59
const double ang2nm
Definition constants.h:51
std::istream & getline(std::istream &is, std::string &str)
Wrapper for a getline function.
Definition getline.h:35
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26