votca 2024.2-dev
Loading...
Searching...
No Matches
xyzwriter.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2021 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_XYZWRITER_H
19#define VOTCA_CSG_XYZWRITER_H
20
21// Standard includes
22#include <cstdio>
23
24// VOTCA includes
27
28// Local VOTCA includes
29#include "topology.h"
30#include "trajectorywriter.h"
31
32namespace votca {
33namespace csg {
34
36 public:
38
39 void Open(std::string file, bool bAppend = false) override;
40 void Close() override;
41
42 void Write(Topology *conf) override;
43
44 template <class T>
45 void Write(T &container, std::string header);
46
47 private:
48 template <class T>
49 Index getSize(T &container) {
50 return getIterable(container).size();
51 }
52
53 template <class Atom>
54 std::string getName(Atom &atom) {
55 return atom.getElement();
56 }
57
58 std::string getName(std::unique_ptr<Bead> &bead) { return bead->getName(); }
59
60 template <class Atom>
61 Eigen::Vector3d getPos(Atom &atom) {
62 return atom.getPos() * tools::conv::bohr2ang;
63 }
64
65 Eigen::Vector3d getPos(std::unique_ptr<Bead> &bead) {
66 return bead->Pos() * tools::conv::nm2ang;
67 }
68
69 template <class T>
70 T &getIterable(T &container) {
71 return container;
72 }
73
74 BeadContainer &getIterable(Topology &top) { return top.Beads(); }
75
76 std::ofstream out_;
77};
78
79template <class T>
80inline void XYZWriter::Write(T &container, std::string header) {
81 out_ << getSize(container) << "\n";
82 out_ << header << "\n";
83
84 boost::format fmter("%1$s%2$10.5f%3$10.5f%4$10.5f\n");
85
86 for (auto &atom : getIterable(container)) {
87 Eigen::Vector3d r = getPos(atom);
88 // truncate strings if necessary
89 std::string atomname = getName(atom);
90 if (atomname.size() > 3) {
91 atomname = atomname.substr(0, 3);
92 }
93 while (atomname.size() < 3) {
94 atomname = " " + atomname;
95 }
96
97 out_ << fmter % atomname % r.x() % r.y() % r.z();
98 }
99 out_ << std::flush;
100}
101} // namespace csg
102} // namespace votca
103
104#endif // VOTCA_CSG_XYZWRITER_H
topology of the whole system
Definition topology.h:81
BeadContainer & Beads()
Definition topology.h:169
void Close() override
Definition xyzwriter.cc:32
BeadContainer & getIterable(Topology &top)
Definition xyzwriter.h:74
T & getIterable(T &container)
Definition xyzwriter.h:70
Eigen::Vector3d getPos(Atom &atom)
Definition xyzwriter.h:61
void Open(std::string file, bool bAppend=false) override
Definition xyzwriter.cc:24
const tools::DistanceUnit distance_unit
Definition xyzwriter.h:37
std::ofstream out_
Definition xyzwriter.h:76
Eigen::Vector3d getPos(std::unique_ptr< Bead > &bead)
Definition xyzwriter.h:65
void Write(Topology *conf) override
Definition xyzwriter.cc:34
std::string getName(Atom &atom)
Definition xyzwriter.h:54
Index getSize(T &container)
Definition xyzwriter.h:49
std::string getName(std::unique_ptr< Bead > &bead)
Definition xyzwriter.h:58
boost::container::deque< Bead, void, block_bead_x4_t > BeadContainer
Definition topology.h:69
const double bohr2ang
Definition constants.h:49
const double nm2ang
Definition constants.h:50
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26