votca 2024.1-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 <iostream>
19#include <string>
20
22
23#include "gmxtopologyreader.h"
24
25#include <gromacs/fileio/tpxio.h>
26#include <gromacs/mdtypes/inputrec.h>
27#include <gromacs/topology/atoms.h>
28#include <gromacs/topology/topology.h>
29#include <gromacs/version.h>
30
31// this one is needed because of bool is defined in one of the headers included
32// by gmx
33#undef bool
34
35namespace votca {
36namespace csg {
37
38bool GMXTopologyReader::ReadTopology(std::string file, Topology &top) {
39 gmx_mtop_t mtop;
40
41 int natoms;
42 // cleanup topology to store new data
43 top.Cleanup();
44
45 t_inputrec ir;
46 ::matrix gbox;
47
48 (void)read_tpx((char *)file.c_str(), &ir, gbox, &natoms, nullptr, nullptr,
49 &mtop);
50
51 size_t ifirstatom = 0;
52
53 size_t nmolblock = mtop.molblock.size();
54
55 for (size_t iblock = 0; iblock < nmolblock; ++iblock) {
56 gmx_moltype_t *mol = &(mtop.moltype[mtop.molblock[iblock].type]);
57
58 std::string molname = *(mol->name);
59
60 Index res_offset = top.ResidueCount();
61
62 t_atoms *atoms = &(mol->atoms);
63
64 for (Index i = 0; i < atoms->nres; i++) {
65 top.CreateResidue(*(atoms->resinfo[i].name));
66 }
67
68 for (Index imol = 0; imol < mtop.molblock[iblock].nmol; ++imol) {
69 Molecule *mi = top.CreateMolecule(molname);
70
71 size_t natoms_mol = mtop.moltype[mtop.molblock[iblock].type].atoms.nr;
72 // read the atoms
73 for (size_t iatom = 0; iatom < natoms_mol; iatom++) {
74 t_atom *a = &(atoms->atom[iatom]);
75
76 std::string bead_type = *(atoms->atomtype[iatom]);
77 if (!top.BeadTypeExist(bead_type)) {
78 top.RegisterBeadType(bead_type);
79 }
80 Bead *bead =
81 top.CreateBead(Bead::spherical, *(atoms->atomname[iatom]),
82 bead_type, a->resind + res_offset, a->m, a->q);
83
84 std::stringstream nm;
85 nm << bead->getResnr() + 1 - res_offset << ":"
86 << top.getResidue(bead->getResnr()).getName() << ":"
87 << bead->getName();
88 mi->AddBead(bead, nm.str());
89 }
90
91 // add exclusions
92 for (size_t iatom = 0; iatom < natoms_mol; iatom++) {
93 std::list<Bead *> excl_list;
94#if GMX_VERSION >= 20210000
95 gmx::ListOfLists<int> &excl = mol->excls;
96 for (const Index k : excl[iatom]) {
97 excl_list.push_back(top.getBead(k + ifirstatom));
98 }
99#else
100 t_blocka *excl = &(mol->excls);
101 for (Index k = excl->index[iatom]; k < excl->index[iatom + 1]; k++) {
102 excl_list.push_back(top.getBead(excl->a[k] + ifirstatom));
103 }
104#endif
105 top.InsertExclusion(top.getBead(iatom + ifirstatom), excl_list);
106 }
107 ifirstatom += natoms_mol;
108 }
109 }
110
111 Eigen::Matrix3d m;
112 for (Index i = 0; i < 3; i++) {
113 for (Index j = 0; j < 3; j++) {
114 m(i, j) = gbox[j][i];
115 }
116 }
117 top.setBox(m);
118
119 return true;
120}
121
122} // namespace csg
123} // 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
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
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 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
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26