votca 2024.1-dev
Loading...
Searching...
No Matches
gmxtrajectoryreader.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 <cstdlib>
20#include <filesystem>
21#include <iostream>
22
23// Third party includes
24#include <gromacs/utility/programcontext.h>
25#include <gromacs/version.h>
26
27// Local VOTCA includes
28#include "votca/csg/topology.h"
29
30// Local private VOTCA includes
31#include "gmxtrajectoryreader.h"
32
33namespace votca {
34namespace csg {
35
36bool GMXTrajectoryReader::Open(const std::string &file) {
37 filename_ = file;
38 return true;
39}
40
42
44 gmx_output_env_t *oenv;
45#if GMX_VERSION >= 20210000
46 gmx::TimeUnit timeunit = gmx::TimeUnit::Picoseconds;
47 XvgFormat xvg = XvgFormat::None;
48#else
49 time_unit_t timeunit = time_ps;
50 xvg_format_t xvg = exvgNONE;
51#endif
52#if GMX_VERSION >= 20230000
53 const std::filesystem::path path = filename_;
54#else
55 char *path = (char *)filename_.c_str();
56#endif
57 output_env_init(&oenv, gmx::getProgramContext(), timeunit, FALSE, xvg, 0);
58 if (!read_first_frame(oenv, &gmx_status_, path, &gmx_frame_,
59 TRX_READ_X | TRX_READ_V | TRX_READ_F)) {
60 throw std::runtime_error(std::string("cannot open ") + filename_);
61 }
62 output_env_done(oenv);
63
64 Eigen::Matrix3d m;
65 for (Index i = 0; i < 3; i++) {
66 for (Index j = 0; j < 3; j++) {
67 m(i, j) = gmx_frame_.box[j][i];
68 }
69 }
70 conf.setBox(m);
71 conf.setTime(gmx_frame_.time);
72 conf.setStep(gmx_frame_.step);
73 std::cout << std::endl;
74
75 if (gmx_frame_.natoms != (Index)conf.Beads().size()) {
76 throw std::runtime_error(
77 "number of beads in trajectory do not match topology");
78 }
79
80 // conf.HasPos(true);
81 // conf.HasF( gmx_frame_.bF);
82
83 for (Index i = 0; i < gmx_frame_.natoms; i++) {
84 Eigen::Vector3d r = {gmx_frame_.x[i][XX], gmx_frame_.x[i][YY],
85 gmx_frame_.x[i][ZZ]};
86 conf.getBead(i)->setPos(r);
87 if (gmx_frame_.bF) {
88 Eigen::Vector3d f = {gmx_frame_.f[i][XX], gmx_frame_.f[i][YY],
89 gmx_frame_.f[i][ZZ]};
90 conf.getBead(i)->setF(f);
91 }
92 if (gmx_frame_.bV) {
93 Eigen::Vector3d v = {gmx_frame_.v[i][XX], gmx_frame_.v[i][YY],
94 gmx_frame_.v[i][ZZ]};
95 conf.getBead(i)->setVel(v);
96 }
97 }
98 return true;
99}
100
102 gmx_output_env_t *oenv;
103#if GMX_VERSION >= 20210000
104 gmx::TimeUnit timeunit = gmx::TimeUnit::Picoseconds;
105 XvgFormat xvg = XvgFormat::None;
106#else
107 time_unit_t timeunit = time_ps;
108 xvg_format_t xvg = exvgNONE;
109#endif
110 output_env_init(&oenv, gmx::getProgramContext(), timeunit, FALSE, xvg, 0);
111 if (!read_next_frame(oenv, gmx_status_, &gmx_frame_)) {
112 return false;
113 }
114 output_env_done(oenv);
115
116 Eigen::Matrix3d m;
117 for (Index i = 0; i < 3; i++) {
118 for (Index j = 0; j < 3; j++) {
119 m(i, j) = gmx_frame_.box[j][i];
120 }
121 }
122 conf.setTime(gmx_frame_.time);
123 conf.setStep(gmx_frame_.step);
124 conf.setBox(m);
125
126 // conf.HasF( gmx_frame_.bF);
127
128 for (Index i = 0; i < gmx_frame_.natoms; i++) {
129 Eigen::Vector3d r = {gmx_frame_.x[i][XX], gmx_frame_.x[i][YY],
130 gmx_frame_.x[i][ZZ]};
131 conf.getBead(i)->setPos(r);
132 if (gmx_frame_.bF) {
133 Eigen::Vector3d f = {gmx_frame_.f[i][XX], gmx_frame_.f[i][YY],
134 gmx_frame_.f[i][ZZ]};
135 conf.getBead(i)->setF(f);
136 }
137 if (gmx_frame_.bV) {
138 Eigen::Vector3d v = {gmx_frame_.v[i][XX], gmx_frame_.v[i][YY],
139 gmx_frame_.v[i][ZZ]};
140 conf.getBead(i)->setVel(v);
141 }
142 }
143 return true;
144}
145
146} // namespace csg
147} // namespace votca
virtual void setPos(const Eigen::Vector3d &bead_position)
Definition basebead.h:161
void setF(const Eigen::Vector3d &bead_force)
Definition bead.h:356
void setVel(const Eigen::Vector3d &r)
Definition bead.h:315
bool FirstFrame(Topology &conf) override
read in the first frame
bool NextFrame(Topology &conf) override
read in the next frame
bool Open(const std::string &file) override
open a trejectory file
topology of the whole system
Definition topology.h:81
void setBox(const Eigen::Matrix3d &box, BoundaryCondition::eBoxtype boxtype=BoundaryCondition::typeAuto)
Definition topology.h:272
BeadContainer & Beads()
Definition topology.h:169
void setTime(double t)
Definition topology.h:311
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
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26