votca 2024.1-dev
Loading...
Searching...
No Matches
dlpolytrajectorywriter.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 <filesystem>
20#include <iomanip>
21#include <string>
22
23// Local private VOTCA includes
25
26namespace votca {
27namespace csg {
28
29using namespace std;
30
31void DLPOLYTrajectoryWriter::Open(string file, bool bAppend)
32// open/create a dlpoly configuration or trajectory file
33// NOTE: allowed file naming - <name>.dlpc or <name>.dlph (convention:
34// ".dlpc"="CONFIG_CGV", ".dlph"="HISTORY_CGV")
35{
36 if (bAppend) {
37 throw std::runtime_error(
38 "Error: appending to dlpoly files not implemented");
39 }
40
41 std::filesystem::path filepath(file.c_str());
42 string out_name = "HISTORY_CGV";
43
44 if (!filepath.has_extension()) {
45
46 throw std::ios_base::failure("Error on creating dlpoly file '" + file +
47 "' - extension is expected, .dlph or .dlpc");
48
49 } else if (filepath.extension() == ".dlpc") {
50
51 isConfig_ = true;
52 out_name = "CONFIG_CGV";
53
54 } else if (filepath.extension() == ".dlph") {
55
56 isConfig_ = false;
57
58 } else {
59 throw std::ios_base::failure("Error on creating dlpoly file '" + file +
60 "' - wrong extension, use .dlph or .dlpc");
61 }
62
63 if (!filepath.has_stem()) {
64 if (filepath.parent_path().string().size() == 0) {
65 fname_ = out_name;
66 } else {
67 fname_ = filepath.parent_path().string() + "/" + out_name;
68 }
69 } else {
70 fname_ = file;
71 }
72
73 fl_.open(fname_);
74 if (!fl_.is_open()) {
75 throw std::ios_base::failure("Error on creating dlpoly file '" + fname_ +
76 "'");
77 }
78}
79
81
83 static Index nstep = 1;
84 const double scale = 10.0; // nm -> A factor
85 Index mavecs = 0;
86 Index mpbct = 0;
87
88 if (conf->HasForce() && conf->HasVel()) {
89 mavecs = 2;
90 } else if (conf->HasVel()) {
91 mavecs = 1;
92 }
93
95 mpbct = 2;
96 }
98 mpbct = 3;
99 }
100
101 if (isConfig_) {
102 double energy = 0.0;
103 fl_ << "From VOTCA with love" << endl;
104 fl_ << setw(10) << mavecs << setw(10) << mpbct << setw(10)
105 << conf->BeadCount() << setw(20) << energy << endl;
106 Eigen::Matrix3d m = conf->getBox();
107 for (Index i = 0; i < 3; i++) {
108 fl_ << fixed << setprecision(10) << setw(20) << m(i, 0) * scale
109 << setw(20) << m(i, 1) * scale << setw(20) << m(i, 2) * scale << endl;
110 }
111
112 } else {
113 static double dstep = 0.0;
114 if (nstep == 1) {
115 fl_ << "From VOTCA with love" << endl;
116 fl_ << setw(10) << mavecs << setw(10) << mpbct << setw(10)
117 << conf->BeadCount() << endl;
118 dstep = conf->getTime() / (double)(conf->getStep());
119 }
120
121 fl_ << "timestep" << setprecision(9) << setw(10) << conf->getStep()
122 << setw(10) << conf->BeadCount() << setw(10) << mavecs << setw(10)
123 << mpbct;
124 fl_ << setprecision(9) << setw(12) << dstep << setw(12) << conf->getTime()
125 << endl;
126
127 Eigen::Matrix3d m = conf->getBox();
128 for (Index i = 0; i < 3; i++) {
129 fl_ << setprecision(12) << setw(20) << m(i, 0) * scale << setw(20)
130 << m(i, 1) * scale << setw(20) << m(i, 2) * scale << endl;
131 }
132 }
133
134 for (Index i = 0; i < conf->BeadCount(); i++) {
135 Bead *bead = conf->getBead(i);
136
137 // AB: DL_POLY needs bead TYPE, not name!
138
139 if (isConfig_) {
140 fl_ << setw(8) << left << bead->getType() << right << setw(10) << i + 1
141 << endl;
142 } else {
143 fl_ << setw(8) << left << bead->getType() << right << setw(10) << i + 1;
144 fl_ << setprecision(6) << setw(12) << bead->getMass() << setw(12)
145 << bead->getQ() << setw(12) << " 0.0" << endl;
146 }
147
148 // alternative with atom NAME & fixed floating point format (in case the
149 // need arises)
150 // fl_ << setw(8) << left << bead->getName() << right << setw(10) << i+1;
151 // fl_ << fixed << setprecision(6) << setw(12) << bead->getMass() <<
152 // setw(12)
153 //<< bead->getQ() << " 0.0" << endl;
154
155 // nm -> Angs
156 fl_ << resetiosflags(std::ios::fixed) << setprecision(12) << setw(20)
157 << bead->getPos().x() * scale;
158 fl_ << setw(20) << bead->getPos().y() * scale << setw(20)
159 << bead->getPos().z() * scale << endl;
160
161 if (mavecs > 0) {
162 if (!bead->HasVel()) {
163 throw std::ios_base::failure(
164 "Error: dlpoly frame is supposed to contain velocities, but bead "
165 "does not have v-data");
166 }
167
168 // nm -> Angs
169 fl_ << setprecision(12) << setw(20) << bead->getVel().x() * scale
170 << setw(20);
171 fl_ << bead->getVel().y() * scale << setw(20)
172 << bead->getVel().z() * scale << endl;
173
174 if (mavecs > 1) {
175 if (!bead->HasF()) {
176 throw std::ios_base::failure(
177 "Error: dlpoly frame is supposed to contain forces, but bead "
178 "does not have f-data");
179 }
180
181 // nm -> Angs
182 fl_ << setprecision(12) << setw(20) << bead->getF().x() * scale
183 << setw(20);
184 fl_ << bead->getF().y() * scale << setw(20) << bead->getF().z() * scale
185 << endl;
186 }
187 }
188 }
189 nstep++;
190}
191
192} // namespace csg
193} // namespace votca
virtual const std::string getType() const noexcept
Definition basebead.h:84
virtual const Eigen::Vector3d & getPos() const
Definition basebead.h:166
virtual const double & getMass() const noexcept
Definition basebead.h:104
information about a bead
Definition bead.h:50
const Eigen::Vector3d & getF() const
get the force acting on the bead
Definition bead.h:361
bool HasF() const noexcept
Definition bead.h:235
bool HasVel() const noexcept
Definition bead.h:232
const Eigen::Vector3d & getVel() const
Definition bead.h:320
virtual const double & getQ() const
Definition bead.h:67
void Open(std::string file, bool bAppend=false) override
void Write(Topology *conf) override
topology of the whole system
Definition topology.h:81
double getTime() const
Definition topology.h:317
BoundaryCondition::eBoxtype getBoxType() const
Definition topology.h:394
const Eigen::Matrix3d & getBox() const
Definition topology.h:298
Index BeadCount() const
Definition topology.h:150
Index getStep() const
Definition topology.h:329
Bead * getBead(const Index i)
Returns a pointer to the bead with index i.
Definition topology.h:227
STL namespace.
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26