votca 2026-dev
Loading...
Searching...
No Matches
gmxtopologyreader.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2023 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#include <array>
19#include <iostream>
20#include <string>
21
24
25#include "gmxtopologyreader.h"
26
27#include <gromacs/fileio/tpxio.h>
28#include <gromacs/mdtypes/inputrec.h>
29#include <gromacs/topology/atoms.h>
30#include <gromacs/topology/ifunc.h>
31#include <gromacs/topology/topology.h>
32#include <gromacs/version.h>
33
34// this one is needed because of bool is defined in one of the headers included
35// by gmx
36#undef bool
37
38namespace votca {
39namespace csg {
40
41bool GMXTopologyReader::ReadTopology(std::string file, Topology &top) {
42 gmx_mtop_t mtop;
43
44 int natoms;
45 // cleanup topology to store new data
46 top.Cleanup();
47
48 t_inputrec ir;
49 ::matrix gbox;
50
51 (void)read_tpx((char *)file.c_str(), &ir, gbox, &natoms, nullptr, nullptr,
52 &mtop);
53
54 size_t ifirstatom = 0;
55
56 size_t nmolblock = mtop.molblock.size();
57
58 for (size_t iblock = 0; iblock < nmolblock; ++iblock) {
59 gmx_moltype_t *mol = &(mtop.moltype[mtop.molblock[iblock].type]);
60
61 std::string molname = *(mol->name);
62
63 Index res_offset = top.ResidueCount();
64
65 t_atoms *atoms = &(mol->atoms);
66
67 for (Index i = 0; i < atoms->nres; i++) {
68 top.CreateResidue(*(atoms->resinfo[i].name));
69 }
70
71 for (Index imol = 0; imol < mtop.molblock[iblock].nmol; ++imol) {
72 Molecule *mi = top.CreateMolecule(molname);
73
74 size_t natoms_mol = mtop.moltype[mtop.molblock[iblock].type].atoms.nr;
75 // read the atoms
76 for (size_t iatom = 0; iatom < natoms_mol; iatom++) {
77 t_atom *a = &(atoms->atom[iatom]);
78
79 std::string bead_type = *(atoms->atomtype[iatom]);
80 if (!top.BeadTypeExist(bead_type)) {
81 top.RegisterBeadType(bead_type);
82 }
83 Bead *bead =
84 top.CreateBead(Bead::spherical, *(atoms->atomname[iatom]),
85 bead_type, a->resind + res_offset, a->m, a->q);
86
87 std::stringstream nm;
88 nm << bead->getResnr() + 1 - res_offset << ":"
89 << top.getResidue(bead->getResnr()).getName() << ":"
90 << bead->getName();
91 mi->AddBead(bead, nm.str());
92 }
93
94 // add exclusions
95 for (size_t iatom = 0; iatom < natoms_mol; iatom++) {
96 std::list<Bead *> excl_list;
97 gmx::ListOfLists<int> &excl = mol->excls;
98 for (const Index k : excl[iatom]) {
99 excl_list.push_back(top.getBead(k + ifirstatom));
100 }
101 top.InsertExclusion(top.getBead(iatom + ifirstatom), excl_list);
102 }
103
104 // GROMACS 2026 and later only provide the enum class
105 // InteractionFunction; 2023-2025 only provide the plain enum values.
106 // GMX_VERSION is from gromacs/version.h. Read are the two-atom bond
107 // types that define chemical connectivity, constraints (which replace
108 // bonds with "constraints = h-bonds/all-bonds") and SETTLE (rigid
109 // water). Restraint, harmonic-potential and non-connecting constraint
110 // types are not bonds and are skipped.
111#if GMX_VERSION >= 20260000
112 std::array<int, 8> bond_ftypes = {
113 static_cast<int>(InteractionFunction::Bonds),
114 static_cast<int>(InteractionFunction::GROMOS96Bonds),
115 static_cast<int>(InteractionFunction::MorsePotential),
116 static_cast<int>(InteractionFunction::CubicBonds),
117 static_cast<int>(InteractionFunction::ConnectBonds),
118 static_cast<int>(InteractionFunction::FENEBonds),
119 static_cast<int>(InteractionFunction::TabulatedBonds),
120 static_cast<int>(InteractionFunction::Constraints)};
121 int settle_ftype = static_cast<int>(InteractionFunction::SETTLE);
122#else
123 std::array<int, 8> bond_ftypes = {F_BONDS, F_G96BONDS, F_MORSE,
124 F_CUBICBONDS, F_CONNBONDS, F_FENEBONDS,
125 F_TABBONDS, F_CONSTR};
126 int settle_ftype = F_SETTLE;
127#endif
128
129 // getGroup() asserts a non-empty group, which a new IBond does not
130 // have; use "BONDS" as in lammpsdatareader.cc and pdbreader.cc.
131 const Index first = Index(ifirstatom);
132 auto add_bond = [&top, first](Index a, Index b) {
133 Interaction *ic = new IBond(a + first, b + first);
134 ic->setGroup("BONDS");
135 top.AddBondedInteraction(ic);
136 };
137
138 // mol->ilist holds one flat list per interaction type; each entry
139 // occupies interaction_function[ftype].nratoms + 1 ints: the
140 // parameter type index followed by the atom indices.
141 for (int ftype : bond_ftypes) {
142 const InteractionList &ilist = mol->ilist[ftype];
143 Index stride = interaction_function[ftype].nratoms + 1;
144 for (Index i = 0; i < Index(ilist.size()); i += stride) {
145 add_bond(ilist.iatoms[i + 1], ilist.iatoms[i + 2]);
146 }
147 }
148 // SETTLE entries list O, H1, H2.
149 {
150 const InteractionList &ilist = mol->ilist[settle_ftype];
151 Index stride = interaction_function[settle_ftype].nratoms + 1;
152 for (Index i = 0; i < Index(ilist.size()); i += stride) {
153 add_bond(ilist.iatoms[i + 1], ilist.iatoms[i + 2]);
154 add_bond(ilist.iatoms[i + 1], ilist.iatoms[i + 3]);
155 }
156 }
157
158 ifirstatom += natoms_mol;
159 }
160 }
161
162 Eigen::Matrix3d m;
163 for (Index i = 0; i < 3; i++) {
164 for (Index j = 0; j < 3; j++) {
165 m(i, j) = gbox[j][i];
166 }
167 }
168 top.setBox(m);
169
170 return true;
171}
172
173} // namespace csg
174} // namespace votca
std::string getName() const
Gets the name of the bead.
Definition basebead.h:58
information about a bead
Definition bead.h:50
const Index & getResnr() const
Definition bead.h:61
bool ReadTopology(std::string file, Topology &top) override
read a topology file
bond interaction
base class for all interactions
Definition interaction.h:40
void setGroup(const std::string &group)
Definition interaction.h:49
information about molecules
Definition molecule.h:45
void AddBead(Bead *bead, const std::string &name)
Add a bead to the molecule.
Definition molecule.cc:29
const std::string & getName() const
get the name of the residue
Definition residue.h:52
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
void AddBondedInteraction(Interaction *ic)
Definition topology.cc:188
void RegisterBeadType(std::string type)
Register the bead type with the topology object.
Definition topology.cc:214
Molecule * CreateMolecule(std::string name)
Creates a new molecule.
Definition topology.h:449
Residue & getResidue(const Index i)
Definition topology.h:229
Bead * getBead(const Index i)
Returns a pointer to the bead with index i.
Definition topology.h:227
void InsertExclusion(Bead *bead1, iteratable &l)
Definition topology.h:475
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26