Merge branch release-5-1
[alexxy/gromacs.git] / src / gromacs / topology / mtop_util.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifndef GMX_TOPOLOGY_MTOP_UTIL_H
38 #define GMX_TOPOLOGY_MTOP_UTIL_H
39
40 #include "gromacs/legacyheaders/types/inputrec.h"
41 #include "gromacs/utility/basedefinitions.h"
42
43 /* Ugly hack to convince some compilers that returning t_topology (or other
44  * structs) from an extern "C" function is OK; can probably go when the extern
45  * "C" blocks go.
46  */
47 #include "gromacs/topology/topology.h"
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 struct gmx_localtop_t;
54 struct gmx_moltype_t;
55 struct gmx_mtop_t;
56 struct t_atom;
57 struct t_atoms;
58 struct t_block;
59 struct t_ilist;
60 struct t_topology;
61
62 /* Should be called after generating or reading mtop,
63  * to set some compute intesive variables to avoid
64  * N^2 operations later on.
65  */
66 void
67 gmx_mtop_finalize(struct gmx_mtop_t *mtop);
68
69 /* Counts the number of atoms of each type. State should be 0 for
70  * state A and 1 for state B types.  typecount should have at
71  * least mtop->ffparams.atnr elements.
72  */
73 void
74 gmx_mtop_count_atomtypes(const struct gmx_mtop_t *mtop, int state, int typecount[]);
75
76 /* Returns the total number of charge groups in mtop */
77 int
78 ncg_mtop(const struct gmx_mtop_t *mtop);
79
80 /* Removes the charge groups, i.e. makes single atom charge groups, in mtop */
81 void gmx_mtop_remove_chargegroups(struct gmx_mtop_t *mtop);
82
83
84 /* Abstract data type for looking up atoms by global atom number */
85 typedef struct gmx_mtop_atomlookup *gmx_mtop_atomlookup_t;
86
87 /* Initialize atom lookup by global atom number */
88 gmx_mtop_atomlookup_t
89 gmx_mtop_atomlookup_init(const struct gmx_mtop_t *mtop);
90
91 /* As gmx_mtop_atomlookup_init, but optimized for atoms involved in settle */
92 gmx_mtop_atomlookup_t
93 gmx_mtop_atomlookup_settle_init(const struct gmx_mtop_t *mtop);
94
95 /* Destroy a gmx_mtop_atomlookup_t data structure */
96 void
97 gmx_mtop_atomlookup_destroy(gmx_mtop_atomlookup_t alook);
98
99
100 /* Returns a pointer to the t_atom struct belonging to atnr_global.
101  * This can be an expensive operation, so if possible use
102  * one of the atom loop constructs below.
103  */
104 void
105 gmx_mtop_atomnr_to_atom(const gmx_mtop_atomlookup_t alook,
106                         int                         atnr_global,
107                         struct t_atom             **atom);
108
109
110 /* Returns a pointer to the molecule interaction array ilist_mol[F_NRE]
111  * and the local atom number in the molecule belonging to atnr_global.
112  */
113 void
114 gmx_mtop_atomnr_to_ilist(const gmx_mtop_atomlookup_t alook,
115                          int atnr_global,
116                          struct t_ilist **ilist_mol, int *atnr_offset);
117
118
119 /* Returns the molecule block index
120  * and the molecule number in the block
121  * and the atom number offset for the atom indices in moltype
122  * belonging to atnr_global.
123  */
124 void
125 gmx_mtop_atomnr_to_molblock_ind(const gmx_mtop_atomlookup_t alook,
126                                 int atnr_global,
127                                 int *molb, int *molnr, int *atnr_mol);
128
129
130 /* Returns atom name, global resnr and residue name  of atom atnr_global */
131 void
132 gmx_mtop_atominfo_global(const struct gmx_mtop_t *mtop, int atnr_global,
133                          char **atomname, int *resnr, char **resname);
134
135
136 /* Abstract type for atom loop over all atoms */
137 typedef struct gmx_mtop_atomloop_all *gmx_mtop_atomloop_all_t;
138
139 /* Initialize an atom loop over all atoms in the system.
140  * The order of the atoms will be as in the state struct.
141  * Only use this when you really need to loop over all atoms,
142  * i.e. when you use groups which might differ per molecule,
143  * otherwise use gmx_mtop_atomloop_block.
144  */
145 gmx_mtop_atomloop_all_t
146 gmx_mtop_atomloop_all_init(const struct gmx_mtop_t *mtop);
147
148 /* Loop to the next atom.
149  * When not at the end:
150  *   returns TRUE and at_global,
151  *   writes the global atom number in *at_global
152  *   and sets the pointer atom to the t_atom struct of that atom.
153  * When at the end, destroys aloop and returns FALSE.
154  * Use as:
155  * gmx_mtop_atomloop_all_t aloop;
156  * aloop = gmx_mtop_atomloop_all_init(mtop)
157  * while (gmx_mtop_atomloop_all_next(aloop,&at_global,&atom)) {
158  *     ...
159  * }
160  */
161 gmx_bool
162 gmx_mtop_atomloop_all_next(gmx_mtop_atomloop_all_t aloop,
163                            int *at_global, struct t_atom **atom);
164
165 /* Return the atomname, the residue number and residue name
166  * of the current atom in the loop.
167  */
168 void
169 gmx_mtop_atomloop_all_names(gmx_mtop_atomloop_all_t aloop,
170                             char **atomname, int *resnr, char **resname);
171
172 /* Return the a pointer to the moltype struct of the current atom
173  * in the loop and the atom number in the molecule.
174  */
175 void
176 gmx_mtop_atomloop_all_moltype(gmx_mtop_atomloop_all_t aloop,
177                               struct gmx_moltype_t **moltype, int *at_mol);
178
179
180 /* Abstract type for atom loop over atoms in all molecule blocks */
181 typedef struct gmx_mtop_atomloop_block *gmx_mtop_atomloop_block_t;
182
183 /* Initialize an atom loop over atoms in all molecule blocks the system.
184  */
185 gmx_mtop_atomloop_block_t
186 gmx_mtop_atomloop_block_init(const struct gmx_mtop_t *mtop);
187
188 /* Loop to the next atom.
189  * When not at the end:
190  *   returns TRUE
191  *   sets the pointer atom to the t_atom struct of that atom
192  *   and return the number of molecules corresponding to this atom.
193  * When at the end, destroys aloop and returns FALSE.
194  * Use as:
195  * gmx_mtop_atomloop_block_t aloop;
196  * aloop = gmx_mtop_atomloop_block_init(mtop)
197  * while (gmx_mtop_atomloop_block_next(aloop,&atom,&nmol)) {
198  *     ...
199  * }
200  */
201 gmx_bool
202 gmx_mtop_atomloop_block_next(gmx_mtop_atomloop_block_t aloop,
203                              struct t_atom **atom, int *nmol);
204
205
206 /* Abstract type for ilist loop over all ilists */
207 typedef struct gmx_mtop_ilistloop *gmx_mtop_ilistloop_t;
208
209 /* Initialize an ilist loop over all molecule types in the system. */
210 gmx_mtop_ilistloop_t
211 gmx_mtop_ilistloop_init(const struct gmx_mtop_t *mtop);
212
213
214 /* Loop to the next molecule,
215  * When not at the end:
216  *   returns TRUE and a pointer to the next array ilist_mol[F_NRE],
217  *   writes the number of molecules for this ilist in *nmol.
218  * When at the end, destroys iloop and returns FALSE.
219  */
220 gmx_bool
221 gmx_mtop_ilistloop_next(gmx_mtop_ilistloop_t iloop,
222                         struct t_ilist **ilist_mol, int *nmol);
223
224
225 /* Abstract type for ilist loop over all ilists of all molecules */
226 typedef struct gmx_mtop_ilistloop_all *gmx_mtop_ilistloop_all_t;
227
228 /* Initialize an ilist loop over all molecule types in the system.
229  * Only use this when you really need to loop over all molecules,
230  * i.e. when you use groups which might differ per molecule,
231  * otherwise use gmx_mtop_ilistloop.
232  */
233 gmx_mtop_ilistloop_all_t
234 gmx_mtop_ilistloop_all_init(const struct gmx_mtop_t *mtop);
235
236 /* Loop to the next molecule,
237  * When not at the end:
238  *   returns TRUE and a pointer to the next array ilist_mol[F_NRE],
239  *   writes the atom offset which should be added to iatoms in atnr_offset.
240  * When at the end, destroys iloop and returns FALSE.
241  */
242 gmx_bool
243 gmx_mtop_ilistloop_all_next(gmx_mtop_ilistloop_all_t iloop,
244                             struct t_ilist **ilist_mol, int *atnr_offset);
245
246
247 /* Returns the total number of interactions in the system of type ftype */
248 int
249 gmx_mtop_ftype_count(const struct gmx_mtop_t *mtop, int ftype);
250
251
252 /* Returns a charge group index for the whole system */
253 struct t_block
254 gmx_mtop_global_cgs(const struct gmx_mtop_t *mtop);
255
256
257 /* Returns a single t_atoms struct for the whole system */
258 struct t_atoms
259 gmx_mtop_global_atoms(const struct gmx_mtop_t *mtop);
260
261
262 /* Make all charge groups the size of one atom.
263  * When bKeepSingleMolCG==TRUE keep charge groups for molecules
264  * that consist of a single charge group.
265  */
266 void
267 gmx_mtop_make_atomic_charge_groups(struct gmx_mtop_t *mtop, gmx_bool bKeepSingleMolCG);
268
269
270 /* Generate a 'local' topology for the whole system.
271  * When ir!=NULL the free energy interactions will be sorted to the end.
272  */
273 struct gmx_localtop_t *
274 gmx_mtop_generate_local_top(const struct gmx_mtop_t *mtop, const t_inputrec *ir);
275
276
277 /* Converts a gmx_mtop_t struct to t_topology.
278  * All memory relating only to mtop will be freed.
279  */
280 struct t_topology
281 gmx_mtop_t_to_t_topology(struct gmx_mtop_t *mtop);
282
283 #ifdef __cplusplus
284 }
285 #endif
286
287 #endif