2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016, 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 /*! \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
69 mtopGetMolblockIndex(const gmx_mtop_t
*mtop
,
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
< mtop
->nmolblock
, "The starting molecule block index for the search should be within range");
79 /* Search the molecue block index using bisection */
81 int molBlock1
= mtop
->nmolblock
;
86 globalAtomStart
= mtop
->molblock
[*moleculeBlock
].globalAtomStart
;
87 if (globalAtomIndex
< globalAtomStart
)
89 molBlock1
= *moleculeBlock
;
91 else if (globalAtomIndex
>= mtop
->molblock
[*moleculeBlock
].globalAtomEnd
)
93 molBlock0
= *moleculeBlock
;
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 Look up the molecule block and atom data 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
124 * \param[out] atom Atom data of the global atom index
127 mtopGetAtomParameters(const gmx_mtop_t
*mtop
,
132 int atomIndexInMolecule
;
133 mtopGetMolblockIndex(mtop
, globalAtomIndex
, moleculeBlock
,
134 NULL
, &atomIndexInMolecule
);
135 *atom
= &mtop
->moltype
[mtop
->molblock
[*moleculeBlock
].type
].atoms
.atom
[atomIndexInMolecule
];
138 /*! \brief Look up the atom and residue name and residue number and index of a global atom index
140 * The atom index has to be in range: 0 <= \p globalAtomIndex < \p mtop->natoms.
141 * The input value of moleculeBlock should be in range. Use 0 as starting value.
142 * For subsequent calls to this function, e.g. in a loop, pass in the previously
143 * returned value for best performance. Atoms in a group tend to be in the same
144 * molecule(block), so this minimizes the search time.
145 * Note that this function does a (somewhat expensive) lookup. If you want
146 * to look up data sequentially for all atoms in a molecule or the system,
147 * use one of the mtop loop functionalities.
149 * \param[in] mtop The molecule topology
150 * \param[in] globalAtomIndex The global atom index to look up
151 * \param[in,out] moleculeBlock The molecule block index in \p mtop
152 * \param[out] atomName The atom name, input can be NULL
153 * \param[out] residueNumber The residue number, input can be NULL
154 * \param[out] residueName The residue name, input can be NULL
155 * \param[out] globalResidueIndex The gobal residue index, input can be NULL
158 mtopGetAtomAndResidueName(const gmx_mtop_t
*mtop
,
161 const char **atomName
,
163 const char **residueName
,
164 int *globalResidueIndex
)
167 int atomIndexInMolecule
;
168 mtopGetMolblockIndex(mtop
, globalAtomIndex
, moleculeBlock
,
169 &moleculeIndex
, &atomIndexInMolecule
);
171 const gmx_molblock_t
*molb
= &mtop
->molblock
[*moleculeBlock
];
172 const t_atoms
*atoms
= &mtop
->moltype
[molb
->type
].atoms
;
173 if (atomName
!= nullptr)
175 *atomName
= *(atoms
->atomname
[atomIndexInMolecule
]);
177 if (residueNumber
!= nullptr)
179 if (atoms
->nres
> mtop
->maxres_renum
)
181 *residueNumber
= atoms
->resinfo
[atoms
->atom
[atomIndexInMolecule
].resind
].nr
;
185 /* Single residue molecule, keep counting */
186 *residueNumber
= molb
->residueNumberStart
+ moleculeIndex
*atoms
->nres
+ atoms
->atom
[atomIndexInMolecule
].resind
;
189 if (residueName
!= nullptr)
191 *residueName
= *(atoms
->resinfo
[atoms
->atom
[atomIndexInMolecule
].resind
].name
);
193 if (globalResidueIndex
!= nullptr)
195 *globalResidueIndex
= molb
->globalResidueStart
+ moleculeIndex
*atoms
->nres
+ atoms
->atom
[atomIndexInMolecule
].resind
;