votca 2024.2-dev
Loading...
Searching...
No Matches
xyzreader.h
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#ifndef __VOTCA_CSG_XYZREADER_H
19#define __VOTCA_CSG_XYZREADER_H
20
21// Standard includes
22#include <fstream>
23#include <iostream>
24#include <string>
25#include <type_traits>
26
27// VOTCA includes
29#include <votca/tools/getline.h>
31
32// Local VOTCA includes
33#include "topologyreader.h"
34#include "trajectoryreader.h"
35
36namespace votca {
37namespace csg {
38
47 public:
49
50 XYZReader() = default;
51 ~XYZReader() override = default;
52
54 bool ReadTopology(std::string file, Topology &top) override;
55
57 bool Open(const std::string &file) override;
59 bool FirstFrame(Topology &top) override;
61 bool NextFrame(Topology &top) override;
62
63 template <class T>
64 void ReadFile(T &container) {
65 if (!ReadFrame<true, T>(container)) {
66 throw std::runtime_error("Reading xyz file '" + file_ + "' failed");
67 }
68 }
69
70 void Close() override;
71
72 private:
73 template <class T>
74 Index getContainerSize(T &container) {
75 return container.size();
76 }
77
78 Index getContainerSize(Topology &container) { return container.BeadCount(); }
79
80 template <bool topology, class T>
81 void AddAtom(T &container, std::string name, Index id,
82 const Eigen::Vector3d &pos) {
83 // the typedef returns the type of the objects the container holds
84 using atom =
85 typename std::iterator_traits<decltype(container.begin())>::value_type;
86 Eigen::Vector3d pos2 = pos * tools::conv::ang2bohr;
87 container.push_back(atom(id, name, pos2));
88 }
89
90 template <bool topology, class T>
91 void AddAtom(Topology &container, std::string name, Index id,
92 const Eigen::Vector3d &pos) {
93 Bead *b;
94 Eigen::Vector3d posnm = pos * tools::conv::ang2nm;
95 if (topology) {
96 b = container.CreateBead(Bead::spherical,
97 name + boost::lexical_cast<std::string>(id),
98 name, 0, 0, 0);
99 } else {
100 b = container.getBead(id);
101 }
102 b->setPos(posnm);
103 }
104
105 template <bool topology, class T>
106 bool ReadFrame(T &container);
107
108 std::ifstream fl_;
109 std::string file_;
111};
112
113template <bool topology, class T>
114inline bool XYZReader::ReadFrame(T &container) {
115 std::string line;
116 tools::getline(fl_, line);
117 ++line_;
118 if (!fl_.eof()) {
119 // read the number of atoms
120 std::vector<std::string> line1 = tools::Tokenizer(line, " \t").ToVector();
121 if (line1.size() != 1) {
122 throw std::runtime_error(
123 "First line of xyz file should contain number "
124 "of atoms/beads, nothing else.");
125 }
126 Index natoms = boost::lexical_cast<Index>(line1[0]);
127 if (!topology && natoms != getContainerSize(container)) {
128 throw std::runtime_error(
129 "number of beads in topology and trajectory differ");
130 }
131 // the title line
132 tools::getline(fl_, line);
133 ++line_;
134 // read atoms
135 for (Index i = 0; i < natoms; ++i) {
136 tools::getline(fl_, line);
137 ++line_;
138 if (fl_.eof()) {
139 throw std::runtime_error("unexpected end of file in xyz file");
140 }
141 tools::Tokenizer tok(line, " ");
142 std::vector<std::string> fields = tok.ToVector();
143 if (fields.size() != 4) {
144 throw std::runtime_error("invalide line " +
145 boost::lexical_cast<std::string>(line_) +
146 " in xyz file\n" + line);
147 }
148 Eigen::Vector3d pos =
149 Eigen::Vector3d(boost::lexical_cast<double>(fields[1]),
150 boost::lexical_cast<double>(fields[2]),
151 boost::lexical_cast<double>(fields[3]));
152
153 AddAtom<topology, T>(container, fields[0], i, pos);
154 }
155 }
156 return !fl_.eof();
157}
158} // namespace csg
159} // namespace votca
160
161#endif
virtual void setPos(const Eigen::Vector3d &bead_position)
Definition basebead.h:161
information about a bead
Definition bead.h:50
topology of the whole system
Definition topology.h:81
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
Bead * getBead(const Index i)
Returns a pointer to the bead with index i.
Definition topology.h:227
trajectoryreader interface
class for reading xyz files
Definition xyzreader.h:46
const tools::DistanceUnit distance_unit
Definition xyzreader.h:48
Index getContainerSize(Topology &container)
Definition xyzreader.h:78
void ReadFile(T &container)
Definition xyzreader.h:64
bool Open(const std::string &file) override
open a trajectory file
Definition xyzreader.cc:52
void AddAtom(T &container, std::string name, Index id, const Eigen::Vector3d &pos)
Definition xyzreader.h:81
bool FirstFrame(Topology &top) override
read in the first frame
Definition xyzreader.cc:64
bool ReadFrame(T &container)
Definition xyzreader.h:114
bool NextFrame(Topology &top) override
read in the next frame
Definition xyzreader.cc:66
~XYZReader() override=default
bool ReadTopology(std::string file, Topology &top) override
open a topology file
Definition xyzreader.cc:35
void AddAtom(Topology &container, std::string name, Index id, const Eigen::Vector3d &pos)
Definition xyzreader.h:91
void Close() override
Definition xyzreader.cc:62
std::ifstream fl_
Definition xyzreader.h:108
Index getContainerSize(T &container)
Definition xyzreader.h:74
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
const double ang2bohr
Definition constants.h:48
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