Merge branch 'release-4-6'
[alexxy/gromacs.git] / src / gromacs / selection / indexutil.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009,2010,2011,2012,2013, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * 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 /*! \file
36  * \brief API for handling index files and index groups.
37  *
38  * The API contains functions and data structures for handling index
39  * files more conveniently than as several separate variables.
40  * In addition to basic functions for initializing the data structures and
41  * making copies, functions are provided for performing (most) set operations
42  * on sorted index groups.
43  * There is also a function for partitioning a index group based on
44  * topology information such as residues or molecules.
45  * Finally, there is a set of functions for constructing mappings between
46  * an index group and its subgroups such.
47  * These can be used with dynamic index group in calculations if one
48  * needs to have a unique ID for each possible atom/residue/molecule in the
49  * selection, e.g., for analysis of dynamics or for look-up tables.
50  *
51  * Mostly, these functions are used internally by the selection engine, but
52  * it is necessary to use some of these functions in order to provide external
53  * index groups to a gmx::SelectionCollection.
54  * Some of the checking functions can be useful outside the selection engine to
55  * check the validity of input groups.
56  *
57  * \author Teemu Murtola <teemu.murtola@gmail.com>
58  * \inlibraryapi
59  * \ingroup module_selection
60  */
61 #ifndef GMX_SELECTION_INDEXUTIL_H
62 #define GMX_SELECTION_INDEXUTIL_H
63
64 #include <string>
65
66 #include "../legacyheaders/typedefs.h"
67
68 /** Stores a set of index groups. */
69 typedef struct gmx_ana_indexgrps_t gmx_ana_indexgrps_t;
70
71 /*! \brief
72  * Specifies the type of index partition or index mapping in several contexts.
73  *
74  * \see gmx_ana_index_make_block(), gmx_ana_indexmap_init()
75  */
76 typedef enum
77 {
78     INDEX_UNKNOWN, /**< Unknown index type.*/
79     INDEX_ATOM,    /**< Each atom in a separate block.*/
80     INDEX_RES,     /**< Each residue in a separate block.*/
81     INDEX_MOL,     /**< Each molecule in a separate block.*/
82     INDEX_ALL      /**< All atoms in a single block.*/
83 } e_index_t;
84
85 /*! \brief
86  * Stores a single index group.
87  */
88 typedef struct gmx_ana_index_t
89 {
90     /** Number of atoms. */
91     int                 isize;
92     /** List of atoms. */
93     atom_id            *index;
94     /** Number of items allocated for \p index. */
95     int                 nalloc_index;
96 } gmx_ana_index_t;
97
98 /*! \brief
99  * Data structure for calculating index group mappings.
100  */
101 typedef struct gmx_ana_indexmap_t
102 {
103     /** Type of the mapping. */
104     e_index_t           type;
105     /*! \brief
106      * Current number of mapped values.
107      *
108      * This is the current number of values in the \p refid and \p mapid
109      * arrays.
110      * If \p bMaskOnly is provided to gmx_ana_indexmap_update(), this
111      * is always equal to \p b.nr, i.e., the number of blocks in the
112      * original index group.
113      */
114     int                 nr;
115     /*! \brief
116      * Current reference IDs.
117      *
118      * This array provides a mapping from the current index group (last given
119      * to gmx_ana_indexmap_update()) to the blocks in \p b, i.e., the
120      * original index group used in gmx_ana_indexmap_init().
121      * The mapping is zero-based.
122      * If \p bMaskOnly is provided to gmx_ana_indexmap_update(), the indices
123      * for blocks not present in the current group are set to -1, otherwise
124      * they are removed completely and the \p nr field updated.
125      */
126     int                *refid;
127     /*! \brief
128      * Current mapped IDs.
129      *
130      * This array provides an arbitrary mapping from the current index group
131      * to the original index group. Instead of a zero-based mapping, the
132      * values from the \p orgid array are used. That is,
133      * \c mapid[i]=orgid[refid[i]].
134      * If \p bMaskOnly is provided to gmx_ana_indexmap_update(), this array
135      * equals \p orgid.
136      */
137     int                *mapid;
138     /*! \brief
139      * Mapped block structure.
140      *
141      * A block structure that corresponds to the current index group.
142      */
143     t_block             mapb;
144
145     /*! \brief
146      * Arbitrary ID numbers for the blocks.
147      *
148      * This array has \p b.nr elements, each defining an ID number for a
149      * block in \p b.
150      * These are initialized in gmx_ana_indexmap_init() based on the type:
151      *  - \ref INDEX_ATOM : the atom indices
152      *  - \ref INDEX_RES :  the residue numbers
153      *  - \ref INDEX_MOL :  the molecule numbers
154      *
155      * All the above numbers are zero-based.
156      * After gmx_ana_indexmap_init(), the user is free to change these values
157      * if the above are not appropriate.
158      * The mapped values can be read through \p mapid.
159      */
160     int                *orgid;
161
162     /*! \brief
163      * Block data that defines the mapping (internal use only).
164      *
165      * The data is initialized by gmx_ana_indexmap_init() and is not changed
166      * after that.
167      * Hence, it cannot be directly applied to the index group passed to
168      * gmx_ana_indexmap_update() unless \p bMaskOnly was specified or the
169      * index group is identical to the one provided to gmx_ana_indexmap_init().
170      */
171     t_blocka            b;
172     /*! \brief
173      * true if the current reference IDs are for the whole group (internal use only).
174      *
175      * This is used internally to optimize the evaluation such that
176      * gmx_ana_indexmap_update() does not take any time if the group is
177      * actually static.
178      */
179     bool                bStatic;
180     /*! \brief
181      * true if the current mapping is for the whole group (internal use only).
182      *
183      * This is used internally to optimize the evaluation such that
184      * gmx_ana_indexmap_update() does not take any time if the group is
185      * actually static.
186      */
187     bool                bMapStatic;
188 } gmx_ana_indexmap_t;
189
190
191 /*! \name Functions for handling gmx_ana_indexgrps_t
192  */
193 /*@{*/
194 /** Reads index groups from a file or constructs them from topology. */
195 void
196 gmx_ana_indexgrps_init(gmx_ana_indexgrps_t **g, t_topology *top,
197                        const char *fnm);
198 /** Frees memory allocated for index groups. */
199 void
200 gmx_ana_indexgrps_free(gmx_ana_indexgrps_t *g);
201 /** Returns true if the index group structure is emtpy. */
202 bool
203 gmx_ana_indexgrps_is_empty(gmx_ana_indexgrps_t *g);
204
205 /** Returns a pointer to an index group. */
206 gmx_ana_index_t *
207 gmx_ana_indexgrps_get_grp(gmx_ana_indexgrps_t *g, int n);
208 /** Extracts a single index group. */
209 bool
210 gmx_ana_indexgrps_extract(gmx_ana_index_t *dest, std::string *destName,
211                           gmx_ana_indexgrps_t *src, int n);
212 /** Finds and extracts a single index group by name. */
213 bool
214 gmx_ana_indexgrps_find(gmx_ana_index_t *dest, std::string *destName,
215                        gmx_ana_indexgrps_t *src, const char *name);
216
217 /** Writes out a list of index groups. */
218 void
219 gmx_ana_indexgrps_print(FILE *fp, gmx_ana_indexgrps_t *g, int maxn);
220 /*@}*/
221
222 /*! \name Functions for handling gmx_ana_index_t
223  */
224 /*@{*/
225 /** Reserves memory to store an index group of size \p isize. */
226 void
227 gmx_ana_index_reserve(gmx_ana_index_t *g, int isize);
228 /** Frees any memory not necessary to hold the current contents. */
229 void
230 gmx_ana_index_squeeze(gmx_ana_index_t *g);
231 /** Initializes an empty index group. */
232 void
233 gmx_ana_index_clear(gmx_ana_index_t *g);
234 /** Constructs a \c gmx_ana_index_t from given values. */
235 void
236 gmx_ana_index_set(gmx_ana_index_t *g, int isize, atom_id *index, int nalloc);
237 /** Creates a simple index group from the first to the \p natoms'th atom. */
238 void
239 gmx_ana_index_init_simple(gmx_ana_index_t *g, int natoms);
240 /** Frees memory allocated for an index group. */
241 void
242 gmx_ana_index_deinit(gmx_ana_index_t *g);
243 /** Copies a \c gmx_ana_index_t. */
244 void
245 gmx_ana_index_copy(gmx_ana_index_t *dest, gmx_ana_index_t *src, bool bAlloc);
246
247 /** Writes out the contents of a index group. */
248 void
249 gmx_ana_index_dump(FILE *fp, gmx_ana_index_t *g, int maxn);
250
251 /** Checks whether an index group is sorted. */
252 bool
253 gmx_ana_index_check_sorted(gmx_ana_index_t *g);
254 /*@}*/
255
256 /*! \name Functions for set operations on gmx_ana_index_t
257  */
258 /*@{*/
259 /** Sorts the indices within an index group. */
260 void
261 gmx_ana_index_sort(gmx_ana_index_t *g);
262 /** Checks whether two index groups are equal. */
263 bool
264 gmx_ana_index_equals(gmx_ana_index_t *a, gmx_ana_index_t *b);
265 /** Checks whether a sorted index group contains another sorted index group. */
266 bool
267 gmx_ana_index_contains(gmx_ana_index_t *a, gmx_ana_index_t *b);
268
269 /** Calculates the intersection between two sorted index groups. */
270 void
271 gmx_ana_index_intersection(gmx_ana_index_t *dest,
272                            gmx_ana_index_t *a, gmx_ana_index_t *b);
273 /** Calculates the set difference between two sorted index groups. */
274 void
275 gmx_ana_index_difference(gmx_ana_index_t *dest,
276                          gmx_ana_index_t *a, gmx_ana_index_t *b);
277 /** Calculates the size of the difference between two sorted index groups. */
278 int
279 gmx_ana_index_difference_size(gmx_ana_index_t *a, gmx_ana_index_t *b);
280 /** Calculates the union of two sorted index groups. */
281 void
282 gmx_ana_index_union(gmx_ana_index_t *dest,
283                     gmx_ana_index_t *a, gmx_ana_index_t *b);
284 /** Merges two distinct sorted index groups. */
285 void
286 gmx_ana_index_merge(gmx_ana_index_t *dest,
287                     gmx_ana_index_t *a, gmx_ana_index_t *b);
288 /** Calculates the intersection and the difference in one call. */
289 void
290 gmx_ana_index_partition(gmx_ana_index_t *dest1, gmx_ana_index_t *dest2,
291                         gmx_ana_index_t *src, gmx_ana_index_t *g);
292 /*@}*/
293
294 /*! \name Functions for handling gmx_ana_indexmap_t and related things
295  */
296 /*@{*/
297 /** Partition a group based on topology information. */
298 void
299 gmx_ana_index_make_block(t_blocka *t, t_topology *top, gmx_ana_index_t *g,
300                          e_index_t type, bool bComplete);
301 /** Checks whether a group consists of full blocks. */
302 bool
303 gmx_ana_index_has_full_blocks(gmx_ana_index_t *g, t_block *b);
304 /** Checks whether a group consists of full blocks. */
305 bool
306 gmx_ana_index_has_full_ablocks(gmx_ana_index_t *g, t_blocka *b);
307 /** Checks whether a group consists of full residues/molecules. */
308 bool
309 gmx_ana_index_has_complete_elems(gmx_ana_index_t *g, e_index_t type, t_topology *top);
310
311 /** Initializes an empty index group mapping. */
312 void
313 gmx_ana_indexmap_clear(gmx_ana_indexmap_t *m);
314 /** Reserves memory for an index group mapping. */
315 void
316 gmx_ana_indexmap_reserve(gmx_ana_indexmap_t *m, int nr, int isize);
317 /** Initializes an index group mapping. */
318 void
319 gmx_ana_indexmap_init(gmx_ana_indexmap_t *m, gmx_ana_index_t *g,
320                       t_topology *top, e_index_t type);
321 /** Sets an index group mapping to be static. */
322 void
323 gmx_ana_indexmap_set_static(gmx_ana_indexmap_t *m, t_blocka *b);
324 /** Frees memory allocated for index group mapping. */
325 void
326 gmx_ana_indexmap_deinit(gmx_ana_indexmap_t *m);
327 /** Makes a deep copy of an index group mapping. */
328 void
329 gmx_ana_indexmap_copy(gmx_ana_indexmap_t *dest, gmx_ana_indexmap_t *src, bool bFirst);
330 /** Updates an index group mapping. */
331 void
332 gmx_ana_indexmap_update(gmx_ana_indexmap_t *m, gmx_ana_index_t *g, bool bMaskOnly);
333 /*@}*/
334
335 #endif