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