2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,2019, 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>
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"
54 // TODO All of the functions taking a const gmx_mtop * are deprecated
55 // and should be replaced by versions taking const gmx_mtop & when
56 // their callers are refactored similarly.
58 /*! \brief Look up the molecule block and other indices of a global atom index
60 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
61 * The input value of moleculeBlock should be in range. Use 0 as starting value.
62 * For subsequent calls to this function, e.g. in a loop, pass in the previously
63 * returned value for best performance. Atoms in a group tend to be in the same
64 * molecule(block), so this minimizes the search time.
66 * \param[in] mtop The molecule topology
67 * \param[in] globalAtomIndex The global atom index to look up
68 * \param[in,out] moleculeBlock The molecule block index in \p mtop
69 * \param[out] moleculeIndex The index of the molecule in the block, can be NULL
70 * \param[out] atomIndexInMolecule The atom index in the molecule, can be NULL
73 mtopGetMolblockIndex(const gmx_mtop_t
*mtop
,
77 int *atomIndexInMolecule
)
79 GMX_ASSERT(globalAtomIndex
>= 0, "The atom index to look up should not be negative");
80 GMX_ASSERT(globalAtomIndex
< mtop
->natoms
, "The atom index to look up should be within range");
81 GMX_ASSERT(moleculeBlock
!= nullptr, "molBlock can not be NULL");
82 GMX_ASSERT(!mtop
->moleculeBlockIndices
.empty(),
83 "The moleculeBlockIndices should not be empty");
84 GMX_ASSERT(*moleculeBlock
>= 0,
85 "The starting molecule block index for the search should not be negative");
86 GMX_ASSERT(*moleculeBlock
< gmx::ssize(mtop
->moleculeBlockIndices
),
87 "The starting molecule block index for the search should be within range");
89 /* Search the molecule block index using bisection */
91 int molBlock1
= mtop
->molblock
.size();
96 globalAtomStart
= mtop
->moleculeBlockIndices
[*moleculeBlock
].globalAtomStart
;
97 if (globalAtomIndex
< globalAtomStart
)
99 molBlock1
= *moleculeBlock
;
101 else if (globalAtomIndex
>= mtop
->moleculeBlockIndices
[*moleculeBlock
].globalAtomEnd
)
103 molBlock0
= *moleculeBlock
;
109 *moleculeBlock
= ((molBlock0
+ molBlock1
+ 1) >> 1);
112 int molIndex
= (globalAtomIndex
- globalAtomStart
) / mtop
->moleculeBlockIndices
[*moleculeBlock
].numAtomsPerMolecule
;
113 if (moleculeIndex
!= nullptr)
115 *moleculeIndex
= molIndex
;
117 if (atomIndexInMolecule
!= nullptr)
119 *atomIndexInMolecule
= globalAtomIndex
- globalAtomStart
- molIndex
*mtop
->moleculeBlockIndices
[*moleculeBlock
].numAtomsPerMolecule
;
123 /*! \brief Returns the global molecule index of a global atom index
125 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
126 * The input value of moleculeBlock should be in range. Use 0 as starting value.
127 * For subsequent calls to this function, e.g. in a loop, pass in the previously
128 * returned value for best performance. Atoms in a group tend to be in the same
129 * molecule(block), so this minimizes the search time.
131 * \param[in] mtop The molecule topology
132 * \param[in] globalAtomIndex The global atom index to look up
133 * \param[in,out] moleculeBlock The molecule block index in \p mtop
136 mtopGetMoleculeIndex(const gmx_mtop_t
*mtop
,
140 int localMoleculeIndex
;
141 mtopGetMolblockIndex(mtop
, globalAtomIndex
, moleculeBlock
, &localMoleculeIndex
, nullptr);
143 return mtop
->moleculeBlockIndices
[*moleculeBlock
].moleculeIndexStart
+ localMoleculeIndex
;
146 /*! \brief Returns the atom data for an atom based on global atom index
148 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
149 * The input value of moleculeBlock should be in range. Use 0 as starting value.
150 * For subsequent calls to this function, e.g. in a loop, pass in the previously
151 * returned value for best performance. Atoms in a group tend to be in the same
152 * molecule(block), so this minimizes the search time.
154 * \param[in] mtop The molecule topology
155 * \param[in] globalAtomIndex The global atom index to look up
156 * \param[in,out] moleculeBlock The molecule block index in \p mtop
158 static inline const t_atom
&
159 mtopGetAtomParameters(const gmx_mtop_t
*mtop
,
163 int atomIndexInMolecule
;
164 mtopGetMolblockIndex(mtop
, globalAtomIndex
, moleculeBlock
,
165 nullptr, &atomIndexInMolecule
);
166 const gmx_moltype_t
&moltype
= mtop
->moltype
[mtop
->molblock
[*moleculeBlock
].type
];
167 return moltype
.atoms
.atom
[atomIndexInMolecule
];
170 /*! \brief Returns the mass of an atom based on global atom index
172 * Returns that A-state mass of the atom with global index \p globalAtomIndex.
173 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
174 * The input value of moleculeBlock should be in range. Use 0 as starting value.
175 * For subsequent calls to this function, e.g. in a loop, pass in the previously
176 * returned value for best performance. Atoms in a group tend to be in the same
177 * molecule(block), so this minimizes the search time.
179 * \param[in] mtop The molecule topology
180 * \param[in] globalAtomIndex The global atom index to look up
181 * \param[in,out] moleculeBlock The molecule block index in \p mtop
184 mtopGetAtomMass(const gmx_mtop_t
*mtop
,
188 const t_atom
&atom
= mtopGetAtomParameters(mtop
, globalAtomIndex
, moleculeBlock
);
192 /*! \brief Look up the atom and residue name and residue number and index of a global atom index
194 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
195 * The input value of moleculeBlock should be in range. Use 0 as starting value.
196 * For subsequent calls to this function, e.g. in a loop, pass in the previously
197 * returned value for best performance. Atoms in a group tend to be in the same
198 * molecule(block), so this minimizes the search time.
199 * Note that this function does a (somewhat expensive) lookup. If you want
200 * to look up data sequentially for all atoms in a molecule or the system,
201 * use one of the mtop loop functionalities.
203 * \param[in] mtop The molecule topology
204 * \param[in] globalAtomIndex The global atom index to look up
205 * \param[in,out] moleculeBlock The molecule block index in \p mtop
206 * \param[out] atomName The atom name, input can be NULL
207 * \param[out] residueNumber The residue number, input can be NULL
208 * \param[out] residueName The residue name, input can be NULL
209 * \param[out] globalResidueIndex The gobal residue index, input can be NULL
212 mtopGetAtomAndResidueName(const gmx_mtop_t
*mtop
,
215 const char **atomName
,
217 const char **residueName
,
218 int *globalResidueIndex
)
221 int atomIndexInMolecule
;
222 mtopGetMolblockIndex(mtop
, globalAtomIndex
, moleculeBlock
,
223 &moleculeIndex
, &atomIndexInMolecule
);
225 const gmx_molblock_t
&molb
= mtop
->molblock
[*moleculeBlock
];
226 const t_atoms
&atoms
= mtop
->moltype
[molb
.type
].atoms
;
227 const MoleculeBlockIndices
&indices
= mtop
->moleculeBlockIndices
[*moleculeBlock
];
228 if (atomName
!= nullptr)
230 *atomName
= *(atoms
.atomname
[atomIndexInMolecule
]);
232 if (residueNumber
!= nullptr)
234 if (atoms
.nres
> mtop
->maxres_renum
)
236 *residueNumber
= atoms
.resinfo
[atoms
.atom
[atomIndexInMolecule
].resind
].nr
;
240 /* Single residue molecule, keep counting */
241 *residueNumber
= indices
.residueNumberStart
+ moleculeIndex
*atoms
.nres
+ atoms
.atom
[atomIndexInMolecule
].resind
;
244 if (residueName
!= nullptr)
246 *residueName
= *(atoms
.resinfo
[atoms
.atom
[atomIndexInMolecule
].resind
].name
);
248 if (globalResidueIndex
!= nullptr)
250 *globalResidueIndex
= indices
.globalResidueStart
+ moleculeIndex
*atoms
.nres
+ atoms
.atom
[atomIndexInMolecule
].resind
;
254 //! \copydoc mtopGetAtomAndResidueName()
256 mtopGetAtomAndResidueName(const gmx_mtop_t
&mtop
,
259 const char **atomName
,
261 const char **residueName
,
262 int *globalResidueIndex
)
264 mtopGetAtomAndResidueName(&mtop
, globalAtomIndex
, moleculeBlock
,
265 atomName
, residueNumber
, residueName
, globalResidueIndex
);
268 /*! \brief Returns residue 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_resinfo
&
281 mtopGetResidueInfo(const gmx_mtop_t
*mtop
,
285 int atomIndexInMolecule
;
286 mtopGetMolblockIndex(mtop
, globalAtomIndex
, moleculeBlock
,
287 nullptr, &atomIndexInMolecule
);
288 const gmx_moltype_t
&moltype
= mtop
->moltype
[mtop
->molblock
[*moleculeBlock
].type
];
289 const int resind
= moltype
.atoms
.atom
[atomIndexInMolecule
].resind
;
290 return moltype
.atoms
.resinfo
[resind
];
293 /*! \brief Returns PDB information for an atom based on global atom index
295 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
296 * The input value of moleculeBlock should be in range. Use 0 as starting value.
297 * For subsequent calls to this function, e.g. in a loop, pass in the previously
298 * returned value for best performance. Atoms in a group tend to be in the same
299 * molecule(block), so this minimizes the search time.
301 * \param[in] mtop The molecule topology
302 * \param[in] globalAtomIndex The global atom index to look up
303 * \param[in,out] moleculeBlock The molecule block index in \p mtop
305 static inline const t_pdbinfo
&
306 mtopGetAtomPdbInfo(const gmx_mtop_t
*mtop
,
310 int atomIndexInMolecule
;
311 mtopGetMolblockIndex(mtop
, globalAtomIndex
, moleculeBlock
,
312 nullptr, &atomIndexInMolecule
);
313 const gmx_moltype_t
&moltype
= mtop
->moltype
[mtop
->molblock
[*moleculeBlock
].type
];
314 GMX_ASSERT(moltype
.atoms
.havePdbInfo
, "PDB information not present when requested");
315 return moltype
.atoms
.pdbinfo
[atomIndexInMolecule
];