Convert gmx_mtop_t to C++
[gromacs.git] / src / gromacs / topology / mtop_lookup.h
blobfd4208c9f55d2c55602a9f56745f145392de5919
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \libinternal \file
37 * \brief This file contains inline functions to look up atom information
38 * using the global atom index.
40 * \author Berk Hess <hess@kth.se>
41 * \inlibraryapi
42 * \ingroup module_mtop
45 #ifndef GMX_TOPOLOGY_MTOP_LOOKUP_H
46 #define GMX_TOPOLOGY_MTOP_LOOKUP_H
48 #include "gromacs/topology/topology.h"
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/gmxassert.h"
52 struct t_atom;
54 /*! \brief Look up the molecule block and other indices of a global atom index
56 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
57 * The input value of moleculeBlock should be in range. Use 0 as starting value.
58 * For subsequent calls to this function, e.g. in a loop, pass in the previously
59 * returned value for best performance. Atoms in a group tend to be in the same
60 * molecule(block), so this minimizes the search time.
62 * \param[in] mtop The molecule topology
63 * \param[in] globalAtomIndex The global atom index to look up
64 * \param[in,out] moleculeBlock The molecule block index in \p mtop
65 * \param[out] moleculeIndex The index of the molecule in the block, can be NULL
66 * \param[out] atomIndexInMolecule The atom index in the molecule, can be NULL
68 static inline void
69 mtopGetMolblockIndex(const gmx_mtop_t *mtop,
70 int globalAtomIndex,
71 int *moleculeBlock,
72 int *moleculeIndex,
73 int *atomIndexInMolecule)
75 GMX_ASSERT(globalAtomIndex >= 0 && globalAtomIndex < mtop->natoms, "The atom index to look up should be within range");
76 GMX_ASSERT(moleculeBlock != nullptr, "molBlock can not be NULL");
77 GMX_ASSERT(*moleculeBlock >= 0 && *moleculeBlock < static_cast<int>(mtop->molblock.size()), "The starting molecule block index for the search should be within range");
79 /* Search the molecue block index using bisection */
80 int molBlock0 = -1;
81 int molBlock1 = mtop->molblock.size();
83 int globalAtomStart;
84 while (TRUE)
86 globalAtomStart = mtop->molblock[*moleculeBlock].globalAtomStart;
87 if (globalAtomIndex < globalAtomStart)
89 molBlock1 = *moleculeBlock;
91 else if (globalAtomIndex >= mtop->molblock[*moleculeBlock].globalAtomEnd)
93 molBlock0 = *moleculeBlock;
95 else
97 break;
99 *moleculeBlock = ((molBlock0 + molBlock1 + 1) >> 1);
102 int molIndex = (globalAtomIndex - globalAtomStart) / mtop->molblock[*moleculeBlock].natoms_mol;
103 if (moleculeIndex != nullptr)
105 *moleculeIndex = molIndex;
107 if (atomIndexInMolecule != nullptr)
109 *atomIndexInMolecule = globalAtomIndex - globalAtomStart - molIndex*mtop->molblock[*moleculeBlock].natoms_mol;
113 /*! \brief Returns the global molecule index of a global atom index
115 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
116 * The input value of moleculeBlock should be in range. Use 0 as starting value.
117 * For subsequent calls to this function, e.g. in a loop, pass in the previously
118 * returned value for best performance. Atoms in a group tend to be in the same
119 * molecule(block), so this minimizes the search time.
121 * \param[in] mtop The molecule topology
122 * \param[in] globalAtomIndex The global atom index to look up
123 * \param[in,out] moleculeBlock The molecule block index in \p mtop
125 static inline int
126 mtopGetMoleculeIndex(const gmx_mtop_t *mtop,
127 int globalAtomIndex,
128 int *moleculeBlock)
130 int localMoleculeIndex;
131 mtopGetMolblockIndex(mtop, globalAtomIndex, moleculeBlock, &localMoleculeIndex, nullptr);
133 return mtop->molblock[*moleculeBlock].moleculeIndexStart + localMoleculeIndex;
136 /*! \brief Returns the atom data for an atom based on global atom index
138 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
139 * The input value of moleculeBlock should be in range. Use 0 as starting value.
140 * For subsequent calls to this function, e.g. in a loop, pass in the previously
141 * returned value for best performance. Atoms in a group tend to be in the same
142 * molecule(block), so this minimizes the search time.
144 * \param[in] mtop The molecule topology
145 * \param[in] globalAtomIndex The global atom index to look up
146 * \param[in,out] moleculeBlock The molecule block index in \p mtop
148 static inline const t_atom &
149 mtopGetAtomParameters(const gmx_mtop_t *mtop,
150 int globalAtomIndex,
151 int *moleculeBlock)
153 int atomIndexInMolecule;
154 mtopGetMolblockIndex(mtop, globalAtomIndex, moleculeBlock,
155 nullptr, &atomIndexInMolecule);
156 const gmx_moltype_t &moltype = mtop->moltype[mtop->molblock[*moleculeBlock].type];
157 return moltype.atoms.atom[atomIndexInMolecule];
160 /*! \brief Returns the mass of an atom based on global atom index
162 * Returns that A-state mass of the atom with global index \p globalAtomIndex.
163 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
164 * The input value of moleculeBlock should be in range. Use 0 as starting value.
165 * For subsequent calls to this function, e.g. in a loop, pass in the previously
166 * returned value for best performance. Atoms in a group tend to be in the same
167 * molecule(block), so this minimizes the search time.
169 * \param[in] mtop The molecule topology
170 * \param[in] globalAtomIndex The global atom index to look up
171 * \param[in,out] moleculeBlock The molecule block index in \p mtop
173 static inline real
174 mtopGetAtomMass(const gmx_mtop_t *mtop,
175 int globalAtomIndex,
176 int *moleculeBlock)
178 const t_atom &atom = mtopGetAtomParameters(mtop, globalAtomIndex, moleculeBlock);
179 return atom.m;
182 /*! \brief Look up the atom and residue name and residue number and index of a global atom index
184 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
185 * The input value of moleculeBlock should be in range. Use 0 as starting value.
186 * For subsequent calls to this function, e.g. in a loop, pass in the previously
187 * returned value for best performance. Atoms in a group tend to be in the same
188 * molecule(block), so this minimizes the search time.
189 * Note that this function does a (somewhat expensive) lookup. If you want
190 * to look up data sequentially for all atoms in a molecule or the system,
191 * use one of the mtop loop functionalities.
193 * \param[in] mtop The molecule topology
194 * \param[in] globalAtomIndex The global atom index to look up
195 * \param[in,out] moleculeBlock The molecule block index in \p mtop
196 * \param[out] atomName The atom name, input can be NULL
197 * \param[out] residueNumber The residue number, input can be NULL
198 * \param[out] residueName The residue name, input can be NULL
199 * \param[out] globalResidueIndex The gobal residue index, input can be NULL
201 static inline void
202 mtopGetAtomAndResidueName(const gmx_mtop_t *mtop,
203 int globalAtomIndex,
204 int *moleculeBlock,
205 const char **atomName,
206 int *residueNumber,
207 const char **residueName,
208 int *globalResidueIndex)
210 int moleculeIndex;
211 int atomIndexInMolecule;
212 mtopGetMolblockIndex(mtop, globalAtomIndex, moleculeBlock,
213 &moleculeIndex, &atomIndexInMolecule);
215 const gmx_molblock_t &molb = mtop->molblock[*moleculeBlock];
216 const t_atoms &atoms = mtop->moltype[molb.type].atoms;
217 if (atomName != nullptr)
219 *atomName = *(atoms.atomname[atomIndexInMolecule]);
221 if (residueNumber != nullptr)
223 if (atoms.nres > mtop->maxres_renum)
225 *residueNumber = atoms.resinfo[atoms.atom[atomIndexInMolecule].resind].nr;
227 else
229 /* Single residue molecule, keep counting */
230 *residueNumber = molb.residueNumberStart + moleculeIndex*atoms.nres + atoms.atom[atomIndexInMolecule].resind;
233 if (residueName != nullptr)
235 *residueName = *(atoms.resinfo[atoms.atom[atomIndexInMolecule].resind].name);
237 if (globalResidueIndex != nullptr)
239 *globalResidueIndex = molb.globalResidueStart + moleculeIndex*atoms.nres + atoms.atom[atomIndexInMolecule].resind;
243 /*! \brief Returns residue information for an atom based on global atom index
245 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
246 * The input value of moleculeBlock should be in range. Use 0 as starting value.
247 * For subsequent calls to this function, e.g. in a loop, pass in the previously
248 * returned value for best performance. Atoms in a group tend to be in the same
249 * molecule(block), so this minimizes the search time.
251 * \param[in] mtop The molecule topology
252 * \param[in] globalAtomIndex The global atom index to look up
253 * \param[in,out] moleculeBlock The molecule block index in \p mtop
255 static inline const t_resinfo &
256 mtopGetResidueInfo(const gmx_mtop_t *mtop,
257 int globalAtomIndex,
258 int *moleculeBlock)
260 int atomIndexInMolecule;
261 mtopGetMolblockIndex(mtop, globalAtomIndex, moleculeBlock,
262 nullptr, &atomIndexInMolecule);
263 const gmx_moltype_t &moltype = mtop->moltype[mtop->molblock[*moleculeBlock].type];
264 const int resind = moltype.atoms.atom[atomIndexInMolecule].resind;
265 return moltype.atoms.resinfo[resind];
268 /*! \brief Returns PDB information for an atom based on global atom index
270 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
271 * The input value of moleculeBlock should be in range. Use 0 as starting value.
272 * For subsequent calls to this function, e.g. in a loop, pass in the previously
273 * returned value for best performance. Atoms in a group tend to be in the same
274 * molecule(block), so this minimizes the search time.
276 * \param[in] mtop The molecule topology
277 * \param[in] globalAtomIndex The global atom index to look up
278 * \param[in,out] moleculeBlock The molecule block index in \p mtop
280 static inline const t_pdbinfo &
281 mtopGetAtomPdbInfo(const gmx_mtop_t *mtop,
282 int globalAtomIndex,
283 int *moleculeBlock)
285 int atomIndexInMolecule;
286 mtopGetMolblockIndex(mtop, globalAtomIndex, moleculeBlock,
287 nullptr, &atomIndexInMolecule);
288 const gmx_moltype_t &moltype = mtop->moltype[mtop->molblock[*moleculeBlock].type];
289 GMX_ASSERT(moltype.atoms.havePdbInfo, "PDB information not present when requested");
290 return moltype.atoms.pdbinfo[atomIndexInMolecule];
293 #endif