votca 2024.2-dev
Loading...
Searching...
No Matches
groreader.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 <fstream>
20#include <iostream>
21
22// Third party inlcudes
23#include <boost/algorithm/string.hpp>
24#include <boost/lexical_cast.hpp>
25
26// VOTCA includes
27#include <votca/tools/getline.h>
28
29// Local private VOTCA includes
30#include "groreader.h"
31
32namespace votca {
33namespace csg {
34
35using namespace std;
36
37bool GROReader::ReadTopology(string file, Topology &top) {
38 topology_ = true;
39 top.Cleanup();
40
41 fl_.open(file);
42 if (!fl_.is_open()) {
43 throw std::ios_base::failure("Error on open topology file: " + file);
44 }
45
46 NextFrame(top);
47
48 fl_.close();
49
50 return true;
51}
52
53bool GROReader::Open(const string &file) {
54 fl_.open(file);
55 if (!fl_.is_open()) {
56 throw std::ios_base::failure("Error on open trajectory file: " + file);
57 }
58 return true;
59}
60
61void GROReader::Close() { fl_.close(); }
62
64 topology_ = false;
65 NextFrame(top);
66 return true;
67}
68
70 string tmp;
71 tools::getline(fl_, tmp); // title
72 if (fl_.eof()) {
73 return !fl_.eof();
74 }
75 tools::getline(fl_, tmp); // number atoms
76 Index natoms = std::stoi(tmp);
77 if (!topology_ && natoms != top.BeadCount()) {
78 throw std::runtime_error(
79 "number of beads in topology and trajectory differ");
80 }
81
82 for (Index i = 0; i < natoms; i++) {
83 string line;
84 tools::getline(fl_, line);
85 string resNum, resName, atName, x, y, z;
86 try {
87 resNum = string(line, 0, 5); // %5i
88 resName = string(line, 5, 5); //%5s
89 atName = string(line, 10, 5); // %5s
90 // atNum= string(line,15,5); // %5i not needed
91 x = string(line, 20, 8); // %8.3f
92 y = string(line, 28, 8); // %8.3f
93 z = string(line, 36, 8); // %8.3f
94 } catch (std::out_of_range &) {
95 throw std::runtime_error("Misformated gro file");
96 }
97 boost::algorithm::trim(atName);
98 boost::algorithm::trim(resName);
99 boost::algorithm::trim(resNum);
100 boost::algorithm::trim(x);
101 boost::algorithm::trim(y);
102 boost::algorithm::trim(z);
103 string vx, vy, vz;
104 bool hasVel = true;
105 try {
106 vx = string(line, 44, 8); // %8.4f
107 vy = string(line, 52, 8); // %8.4f
108 vz = string(line, 60, 8); // %8.4f
109 } catch (std::out_of_range &) {
110 hasVel = false;
111 }
112
113 Bead *b;
114 if (topology_) {
115 Index resnr = boost::lexical_cast<Index>(resNum);
116 if (resnr < 1) {
117 throw std::runtime_error("Misformated gro file, resnr has to be > 0");
118 }
119 // TODO: fix the case that resnr is not in ascending order
120 if (resnr > top.ResidueCount()) {
121 while ((resnr - 1) > top.ResidueCount()) { // gro resnr should start
122 // with 1 but accept sloppy
123 // files
124 top.CreateResidue("DUMMY"); // create dummy residue, hopefully it
125 // will never show
126 cout << "Warning: residue numbers not continous, create DUMMY "
127 "residue with nr "
128 << top.ResidueCount() << endl;
129 }
130 top.CreateResidue(resName);
131 }
132 // this is not correct, but still better than no type at all!
133 if (!top.BeadTypeExist(atName)) {
134 top.RegisterBeadType(atName);
135 }
136
137 // res -1 as internal number starts with 0
138 b = top.CreateBead(Bead::spherical, atName, atName, resnr - 1, 1., 0.);
139 } else {
140 b = top.getBead(i);
141 }
142
143 b->setPos(Eigen::Vector3d(stod(x), stod(y), stod(z)));
144 if (hasVel) {
145 boost::algorithm::trim(vx);
146 boost::algorithm::trim(vy);
147 boost::algorithm::trim(vz);
148 b->setVel(Eigen::Vector3d(stod(vx), stod(vy), stod(vz)));
149 }
150 }
151
152 tools::getline(fl_, tmp); // read box line
153 if (fl_.eof()) {
154 throw std::runtime_error(
155 "unexpected end of file in poly file, when boxline");
156 }
157 vector<double> fields = tools::Tokenizer(tmp, " ").ToVector<double>();
158 Eigen::Matrix3d box;
159 if (fields.size() == 3) {
160 box = Eigen::Matrix3d::Zero();
161 for (Index i = 0; i < 3; i++) {
162 box(i, i) = fields[i];
163 }
164 } else if (fields.size() == 9) {
165 box(0, 0) = fields[0];
166 box(1, 1) = fields[1];
167 box(2, 2) = fields[2];
168 box(1, 0) = fields[3];
169 box(2, 0) = fields[4];
170 box(0, 1) = fields[5];
171 box(2, 1) = fields[6];
172 box(0, 2) = fields[7];
173 box(1, 2) = fields[8];
174 } else {
175 throw std::runtime_error("Error while reading box (last) line");
176 }
177 top.setBox(box);
178
179 if (topology_) {
180 cout << "WARNING: topology created from .gro file, masses and charges are "
181 "wrong!\n";
182 }
183
184 return !fl_.eof();
185}
186
187} // namespace csg
188} // namespace votca
virtual void setPos(const Eigen::Vector3d &bead_position)
Definition basebead.h:161
information about a bead
Definition bead.h:50
void setVel(const Eigen::Vector3d &r)
Definition bead.h:315
std::ifstream fl_
Definition groreader.h:72
void Close() override
Definition groreader.cc:61
bool ReadTopology(std::string file, Topology &top) override
open a topology file
Definition groreader.cc:37
bool NextFrame(Topology &top) override
read in the next frame
Definition groreader.cc:69
bool FirstFrame(Topology &top) override
read in the first frame
Definition groreader.cc:63
bool Open(const std::string &file) override
open a trejectory file
Definition groreader.cc:53
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
Index ResidueCount() const
Definition topology.h:156
bool BeadTypeExist(std::string type) const
Determine if a bead type exists.
Definition topology.cc:210
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
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
break string into words
Definition tokenizer.h:72
std::vector< T > ToVector()
store all words in a vector of type T, does type conversion.
Definition tokenizer.h:109
STL namespace.
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