70853e0e703cba69569d31373415a5a4188c5ffc
[alexxy/gromacs.git] / src / gromacs / topology / mtop_lookup.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \libinternal \file
36  *
37  * \brief This file contains inline functions to look up atom information
38  * using the global atom index.
39  *
40  * \author Berk Hess <hess@kth.se>
41  * \inlibraryapi
42  * \ingroup module_mtop
43  */
44
45 #ifndef GMX_TOPOLOGY_MTOP_LOOKUP_H
46 #define GMX_TOPOLOGY_MTOP_LOOKUP_H
47
48 #include "gromacs/topology/topology.h"
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/gmxassert.h"
51
52 struct t_atom;
53
54 /*! \brief Look up the molecule block and other indices of a global atom index
55  *
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.
61  *
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
67  */
68 static inline void
69 mtopGetMolblockIndex(const gmx_mtop_t *mtop,
70                      int               globalAtomIndex,
71                      int              *moleculeBlock,
72                      int              *moleculeIndex,
73                      int              *atomIndexInMolecule)
74 {
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 < mtop->nmolblock, "The starting molecule block index for the search should be within range");
78
79     /* Search the molecue block index using bisection */
80     int molBlock0 = -1;
81     int molBlock1 = mtop->nmolblock;
82
83     int globalAtomStart;
84     while (TRUE)
85     {
86         globalAtomStart = mtop->molblock[*moleculeBlock].globalAtomStart;
87         if (globalAtomIndex < globalAtomStart)
88         {
89             molBlock1 = *moleculeBlock;
90         }
91         else if (globalAtomIndex >= mtop->molblock[*moleculeBlock].globalAtomEnd)
92         {
93             molBlock0 = *moleculeBlock;
94         }
95         else
96         {
97             break;
98         }
99         *moleculeBlock = ((molBlock0 + molBlock1 + 1) >> 1);
100     }
101
102     int molIndex = (globalAtomIndex - globalAtomStart) / mtop->molblock[*moleculeBlock].natoms_mol;
103     if (moleculeIndex != nullptr)
104     {
105         *moleculeIndex = molIndex;
106     }
107     if (atomIndexInMolecule != nullptr)
108     {
109         *atomIndexInMolecule = globalAtomIndex - globalAtomStart - molIndex*mtop->molblock[*moleculeBlock].natoms_mol;
110     }
111 }
112
113 /*! \brief Returns the global molecule index of a global atom index
114  *
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.
120  *
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
124  */
125 static inline int
126 mtopGetMoleculeIndex(const gmx_mtop_t *mtop,
127                      int               globalAtomIndex,
128                      int              *moleculeBlock)
129 {
130     int localMoleculeIndex;
131     mtopGetMolblockIndex(mtop, globalAtomIndex, moleculeBlock, &localMoleculeIndex, nullptr);
132
133     return mtop->molblock[*moleculeBlock].moleculeIndexStart + localMoleculeIndex;
134 }
135
136 /*! \brief Returns the atom data for an atom based on global atom index
137  *
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.
143  *
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
147  */
148 static inline const t_atom &
149 mtopGetAtomParameters(const gmx_mtop_t  *mtop,
150                       int                globalAtomIndex,
151                       int               *moleculeBlock)
152 {
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];
158 }
159
160 /*! \brief Returns the mass of an atom based on global atom index
161  *
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.
168  *
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
172  */
173 static inline real
174 mtopGetAtomMass(const gmx_mtop_t  *mtop,
175                 int                globalAtomIndex,
176                 int               *moleculeBlock)
177 {
178     const t_atom &atom = mtopGetAtomParameters(mtop, globalAtomIndex, moleculeBlock);
179     return atom.m;
180 }
181
182 /*! \brief Look up the atom and residue name and residue number and index of a global atom index
183  *
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.
192  *
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
200  */
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)
209 {
210     int moleculeIndex;
211     int atomIndexInMolecule;
212     mtopGetMolblockIndex(mtop, globalAtomIndex, moleculeBlock,
213                          &moleculeIndex, &atomIndexInMolecule);
214
215     const gmx_molblock_t &molb  = mtop->molblock[*moleculeBlock];
216     const t_atoms        &atoms = mtop->moltype[molb.type].atoms;
217     if (atomName != nullptr)
218     {
219         *atomName = *(atoms.atomname[atomIndexInMolecule]);
220     }
221     if (residueNumber != nullptr)
222     {
223         if (atoms.nres > mtop->maxres_renum)
224         {
225             *residueNumber = atoms.resinfo[atoms.atom[atomIndexInMolecule].resind].nr;
226         }
227         else
228         {
229             /* Single residue molecule, keep counting */
230             *residueNumber = molb.residueNumberStart + moleculeIndex*atoms.nres + atoms.atom[atomIndexInMolecule].resind;
231         }
232     }
233     if (residueName != nullptr)
234     {
235         *residueName = *(atoms.resinfo[atoms.atom[atomIndexInMolecule].resind].name);
236     }
237     if (globalResidueIndex != nullptr)
238     {
239         *globalResidueIndex = molb.globalResidueStart + moleculeIndex*atoms.nres + atoms.atom[atomIndexInMolecule].resind;
240     }
241 }
242
243 /*! \brief Returns residue information for an atom based on global atom index
244  *
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.
250  *
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
254  */
255 static inline const t_resinfo &
256 mtopGetResidueInfo(const gmx_mtop_t  *mtop,
257                    int                globalAtomIndex,
258                    int               *moleculeBlock)
259 {
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];
266 }
267
268 /*! \brief Returns PDB information for an atom based on global atom index
269  *
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.
275  *
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
279  */
280 static inline const t_pdbinfo &
281 mtopGetAtomPdbInfo(const gmx_mtop_t  *mtop,
282                    int                globalAtomIndex,
283                    int               *moleculeBlock)
284 {
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];
291 }
292
293 #endif