3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
32 * \brief Implementation of functions in indexutil.h.
42 #include <gmx_fatal.h>
44 #include <indexutil.h>
46 /********************************************************************
47 * gmx_ana_indexgrps_t functions
48 ********************************************************************/
51 * Stores a set of index groups.
53 struct gmx_ana_indexgrps_t
55 /** Number of index groups. */
57 /** Array of index groups. */
62 * \param[out] g Index group structure.
63 * \param[in] ngrps Number of groups for which memory is allocated.
66 gmx_ana_indexgrps_alloc(gmx_ana_indexgrps_t **g, int ngrps)
74 * \param[out] g Index group structure.
75 * \param[in] ngrps Number of index groups.
76 * \param[in] isize Array of index group sizes.
77 * \param[in] index Array of pointers to indices of each group.
78 * \param[in] name Array of names of the groups.
79 * \param[in] bFree If TRUE, the \p isize, \p index and \p name arrays
80 * are freed after they have been copied.
83 gmx_ana_indexgrps_set(gmx_ana_indexgrps_t **g, int ngrps, int *isize,
84 atom_id **index, char **name, bool bFree)
88 gmx_ana_indexgrps_alloc(g, ngrps);
89 for (i = 0; i < ngrps; ++i)
91 gmx_ana_index_set(&(*g)->g[i], isize[i], index[i], name[i], isize[i]);
102 * \param[out] g Index group structure.
103 * \param[in] top Topology structure.
104 * \param[in] fnm File name for the index file.
105 * Memory is automatically allocated.
107 * One or both of \p top or \p fnm can be NULL.
108 * If \p top is NULL, an index file is required and the groups are read
109 * from the file (uses Gromacs routine init_index()).
110 * If \p fnm is NULL, default groups are constructed based on the
111 * topology (uses Gromacs routine analyse()).
112 * If both are null, the index group structure is initialized empty.
115 gmx_ana_indexgrps_init(gmx_ana_indexgrps_t **g, t_topology *top,
118 t_blocka *block = NULL;
124 block = init_index(fnm, &names);
128 block = new_blocka();
129 analyse(&top->atoms, block, &names, FALSE, FALSE);
139 gmx_ana_indexgrps_alloc(g, block->nr);
140 for (i = 0; i < block->nr; ++i)
142 gmx_ana_index_t *grp = &(*g)->g[i];
144 grp->isize = block->index[i+1] - block->index[i];
145 snew(grp->index, grp->isize);
146 for (j = 0; j < grp->isize; ++j)
148 grp->index[j] = block->a[block->index[i]+j];
150 grp->name = names[i];
151 grp->nalloc_index = grp->isize;
160 * \param[out] g Index group structure.
161 * \param[in] top Topology structure.
162 * \param[in] fnm File name for the index file.
163 * \param[in] ngrps Number of required groups.
164 * Memory is automatically allocated.
166 * One of \p top or \p fnm can be NULL, but not both.
167 * If \p top is NULL, an index file is required and the groups are read
168 * from the file (uses Gromacs routine rd_index()).
169 * If \p fnm is NULL, default groups are constructed based on the
170 * topology (uses Gromacs routine get_index()).
173 gmx_ana_indexgrps_get(gmx_ana_indexgrps_t **g, t_topology *top,
174 const char *fnm, int ngrps)
185 rd_index(fnm, ngrps, isize, index, name);
189 get_index(&(top->atoms), fnm, ngrps, isize, index, name);
191 gmx_ana_indexgrps_set(g, ngrps, isize, index, name, TRUE);
195 * \param[out] g Index group structure.
196 * \param[in] fnm File name for the index file.
197 * \param[in] ngrps Number of required groups.
198 * Memory is automatically allocated.
200 * This is a convenience function for calling the Gromacs routine
204 gmx_ana_indexgrps_rd(gmx_ana_indexgrps_t **g, const char *fnm, int ngrps)
206 gmx_ana_indexgrps_get(g, NULL, fnm, ngrps);
210 * \param[in] g Index groups structure.
212 * The pointer \p g is invalid after the call.
215 gmx_ana_indexgrps_free(gmx_ana_indexgrps_t *g)
224 for (i = 0; i < g->nr; ++i)
226 gmx_ana_index_deinit(&g->g[i]);
235 * \param[out] dest Destination index groups.
236 * \param[in] src Source index groups.
238 * A deep copy is made for all fields, including the group names.
241 gmx_ana_indexgrps_clone(gmx_ana_indexgrps_t **dest, gmx_ana_indexgrps_t *src)
245 gmx_ana_indexgrps_alloc(dest, src->nr);
246 for (g = 0; g < src->nr; ++g)
248 gmx_ana_index_copy(&(*dest)->g[g], &src->g[g], TRUE);
253 * \param[out] g Index group structure.
254 * \returns TRUE if \p g is empty, i.e., has 0 index groups.
257 gmx_ana_indexgrps_is_empty(gmx_ana_indexgrps_t *g)
263 * \param[in] g Index groups structure.
264 * \param[in] n Index group number to get.
265 * \returns Pointer to the \p n'th index group in \p g.
267 * The returned pointer should not be freed.
270 gmx_ana_indexgrps_get_grp(gmx_ana_indexgrps_t *g, int n)
272 if (n < 0 || n >= g->nr)
280 * \param[out] dest Output structure.
281 * \param[in] src Input index groups.
282 * \param[in] n Number of the group to extract.
283 * \returns TRUE if \p n is a valid group in \p src, FALSE otherwise.
286 gmx_ana_indexgrps_extract(gmx_ana_index_t *dest, gmx_ana_indexgrps_t *src, int n)
288 if (n < 0 || n >= src->nr)
294 gmx_ana_index_copy(dest, &src->g[n], TRUE);
299 * \param[out] dest Output structure.
300 * \param[in] src Input index groups.
301 * \param[in] name Name (or part of the name) of the group to extract.
302 * \returns TRUE if \p name is a valid group in \p src, FALSE otherwise.
304 * Uses the Gromacs routine find_group() to find the actual group;
305 * the comparison is case-insensitive.
308 gmx_ana_indexgrps_find(gmx_ana_index_t *dest, gmx_ana_indexgrps_t *src, char *name)
313 snew(names, src->nr);
314 for (i = 0; i < src->nr; ++i)
316 names[i] = src->g[i].name;
318 i = find_group(name, src->nr, names);
326 return gmx_ana_indexgrps_extract(dest, src, i);
330 * \param[in] g Index groups to print.
331 * \param[in] maxn Maximum number of indices to print
332 * (-1 = print all, 0 = print only names).
335 gmx_ana_indexgrps_print(gmx_ana_indexgrps_t *g, int maxn)
339 for (i = 0; i < g->nr; ++i)
341 fprintf(stderr, " %2d: ", i);
342 gmx_ana_index_dump(&g->g[i], i, maxn);
346 /********************************************************************
347 * gmx_ana_index_t functions
348 ********************************************************************/
351 * \param[in,out] g Index group structure.
352 * \param[in] isize Maximum number of atoms to reserve space for.
355 gmx_ana_index_reserve(gmx_ana_index_t *g, int isize)
357 if (g->nalloc_index < isize)
359 srenew(g->index, isize);
360 g->nalloc_index = isize;
365 * \param[in,out] g Index group structure.
367 * Resizes the memory allocated for holding the indices such that the
368 * current contents fit.
371 gmx_ana_index_squeeze(gmx_ana_index_t *g)
373 srenew(g->index, g->isize);
374 g->nalloc_index = g->isize;
378 * \param[out] g Output structure.
380 * Any contents of \p g are discarded without freeing.
383 gmx_ana_index_clear(gmx_ana_index_t *g)
392 * \param[out] g Output structure.
393 * \param[in] isize Number of atoms in the new group.
394 * \param[in] index Array of \p isize atoms (can be NULL if \p isize is 0).
395 * \param[in] name Name for the new group (can be NULL).
396 * \param[in] nalloc Number of elements allocated for \p index
397 * (if 0, \p index is not freed in gmx_ana_index_deinit())
399 * No copy if \p index is made.
402 gmx_ana_index_set(gmx_ana_index_t *g, int isize, atom_id *index, char *name,
408 g->nalloc_index = nalloc;
412 * \param[out] g Output structure.
413 * \param[in] natoms Number of atoms.
414 * \param[in] name Name for the new group (can be NULL).
417 gmx_ana_index_init_simple(gmx_ana_index_t *g, int natoms, char *name)
422 snew(g->index, natoms);
423 for (i = 0; i < natoms; ++i)
428 g->nalloc_index = natoms;
432 * \param[in] g Index group structure.
434 * The pointer \p g is not freed.
437 gmx_ana_index_deinit(gmx_ana_index_t *g)
439 if (g->nalloc_index > 0)
444 gmx_ana_index_clear(g);
448 * \param[out] dest Destination index group.
449 * \param[in] src Source index group.
450 * \param[in] bAlloc If TRUE, memory is allocated at \p dest; otherwise,
451 * it is assumed that enough memory has been allocated for index.
453 * A deep copy of the name is only made if \p bAlloc is TRUE.
456 gmx_ana_index_copy(gmx_ana_index_t *dest, gmx_ana_index_t *src, bool bAlloc)
458 dest->isize = src->isize;
463 snew(dest->index, dest->isize);
464 dest->nalloc_index = dest->isize;
466 memcpy(dest->index, src->index, dest->isize*sizeof(*dest->index));
468 if (bAlloc && src->name)
470 dest->name = strdup(src->name);
472 else if (bAlloc || src->name)
474 dest->name = src->name;
479 * \param[in] g Index group to print.
480 * \param[in] i Group number to use if the name is NULL.
481 * \param[in] maxn Maximum number of indices to print (-1 = print all).
484 gmx_ana_index_dump(gmx_ana_index_t *g, int i, int maxn)
490 fprintf(stderr, "\"%s\"", g->name);
494 fprintf(stderr, "Group %d", i+1);
496 fprintf(stderr, " (%d atoms)", g->isize);
499 fprintf(stderr, ":");
501 if (maxn >= 0 && n > maxn)
505 for (j = 0; j < n; ++j)
507 fprintf(stderr, " %d", g->index[j]+1);
511 fprintf(stderr, " ...");
514 fprintf(stderr, "\n");
518 * \param[in] g Input index group.
519 * \param[in] natoms Number of atoms to check against.
521 * If any atom index in the index group is less than zero or >= \p natoms,
522 * gmx_fatal() is called.
525 gmx_ana_index_check(gmx_ana_index_t *g, int natoms)
529 for (j = 0; j < g->isize; ++j)
531 if (g->index[j] >= natoms)
533 gmx_fatal(FARGS,"Atom index (%d) in index group %s (%d atoms) "
534 "larger than number of atoms in trajectory (%d atoms)",
535 g->index[j], g->name, g->isize, natoms);
537 else if (g->index[j] < 0)
539 gmx_fatal(FARGS,"Atom index (%d) in index group %s (%d atoms) "
541 g->index[j], g->name, g->isize);
547 * \param[in] g Index group to check.
548 * \returns TRUE if the index group is sorted and has no duplicates,
552 gmx_ana_index_check_sorted(gmx_ana_index_t *g)
556 for (i = 0; i < g->isize-1; ++i)
558 if (g->index[i+1] <= g->index[i])
566 /********************************************************************
568 ********************************************************************/
570 /** Helper function for gmx_ana_index_sort(). */
572 cmp_atomid(const void *a, const void *b)
574 if (*(atom_id *)a < *(atom_id *)b) return -1;
575 if (*(atom_id *)a > *(atom_id *)b) return 1;
580 * \param[in,out] g Index group to be sorted.
583 gmx_ana_index_sort(gmx_ana_index_t *g)
585 qsort(g->index, g->isize, sizeof(*g->index), cmp_atomid);
589 * \param[in] a Index group to check.
590 * \param[in] b Index group to check.
591 * \returns TRUE if \p a and \p b are equal, FALSE otherwise.
594 gmx_ana_index_equals(gmx_ana_index_t *a, gmx_ana_index_t *b)
598 if (a->isize != b->isize)
602 for (i = 0; i < a->isize; ++i)
604 if (a->index[i] != b->index[i])
613 * \param[in] a Index group to check against.
614 * \param[in] b Index group to check.
615 * \returns TRUE if \p b is contained in \p a,
618 * If the elements are not in the same order in both groups, the function
619 * fails. However, the groups do not need to be sorted.
622 gmx_ana_index_contains(gmx_ana_index_t *a, gmx_ana_index_t *b)
626 for (i = j = 0; j < b->isize; ++i, ++j) {
627 while (i < a->isize && a->index[i] != b->index[j])
640 * \param[out] dest Output index group (the intersection of \p a and \p b).
641 * \param[in] a First index group.
642 * \param[in] b Second index group.
644 * \p dest can be the same as \p a or \p b.
647 gmx_ana_index_intersection(gmx_ana_index_t *dest,
648 gmx_ana_index_t *a, gmx_ana_index_t *b)
652 for (i = j = k = 0; i < a->isize && j < b->isize; ++i) {
653 while (j < b->isize && b->index[j] < a->index[i])
657 if (j < b->isize && b->index[j] == a->index[i])
659 dest->index[k++] = b->index[j++];
666 * \param[out] dest Output index group (the difference \p a - \p b).
667 * \param[in] a First index group.
668 * \param[in] b Second index group.
670 * \p dest can equal \p a, but not \p b.
673 gmx_ana_index_difference(gmx_ana_index_t *dest,
674 gmx_ana_index_t *a, gmx_ana_index_t *b)
678 for (i = j = k = 0; i < a->isize; ++i)
680 while (j < b->isize && b->index[j] < a->index[i])
684 if (j == b->isize || b->index[j] != a->index[i])
686 dest->index[k++] = a->index[i];
693 * \param[in] a First index group.
694 * \param[in] b Second index group.
695 * \returns Size of the difference \p a - \p b.
698 gmx_ana_index_difference_size(gmx_ana_index_t *a, gmx_ana_index_t *b)
702 for (i = j = k = 0; i < a->isize; ++i)
704 while (j < b->isize && b->index[j] < a->index[i])
708 if (j == b->isize || b->index[j] != a->index[i])
717 * \param[out] dest1 Output group 1 (will equal \p g).
718 * \param[out] dest2 Output group 2 (will equal \p src - \p g).
719 * \param[in] src Group to be partitioned.
720 * \param[in] g One partition.
722 * \pre \p g is a subset of \p src and both sets are sorted
723 * \pre \p dest1 has allocated storage to store \p src
724 * \post \p dest1 == \p g
725 * \post \p dest2 == \p src - \p g
727 * No storage should be allocated for \p dest2; after the call,
728 * \p dest2->index points to the memory allocated for \p dest1
729 * (to a part that is not used by \p dest1).
731 * The calculation can be performed in-place by setting \p dest1 equal to
735 gmx_ana_index_partition(gmx_ana_index_t *dest1, gmx_ana_index_t *dest2,
736 gmx_ana_index_t *src, gmx_ana_index_t *g)
741 dest2->index = dest1->index + g->isize;
742 dest2->isize = src->isize - g->isize;
743 for (i = g->isize-1, j = src->isize-1, k = dest2->isize-1; i >= 0; --i, --j)
745 while (j >= 0 && src->index[j] != g->index[i])
747 dest2->index[k--] = src->index[j--];
752 dest2->index[k--] = src->index[j--];
754 gmx_ana_index_copy(dest1, g, FALSE);
758 * \param[out] dest Output index group (the union of \p a and \p b).
759 * \param[in] a First index group.
760 * \param[in] b Second index group.
762 * \p a and \p b can have common items.
763 * \p dest can equal \p a or \p b.
765 * \see gmx_ana_index_merge()
768 gmx_ana_index_union(gmx_ana_index_t *dest,
769 gmx_ana_index_t *a, gmx_ana_index_t *b)
774 dsize = gmx_ana_index_difference_size(b, a);
777 dest->isize = a->isize + dsize;
778 for (k = dest->isize - 1; k >= 0; k--)
780 if (i < 0 || (j >= 0 && a->index[i] < b->index[j]))
782 dest->index[k] = b->index[j--];
786 if (j >= 0 && a->index[i] == b->index[j])
790 dest->index[k] = a->index[i--];
796 * \param[out] dest Output index group (the union of \p a and \p b).
797 * \param[in] a First index group.
798 * \param[in] b Second index group.
800 * \p a and \p b should not have common items.
801 * \p dest can equal \p a or \p b.
803 * \see gmx_ana_index_union()
806 gmx_ana_index_merge(gmx_ana_index_t *dest,
807 gmx_ana_index_t *a, gmx_ana_index_t *b)
813 dest->isize = a->isize + b->isize;
814 for (k = dest->isize - 1; k >= 0; k--)
816 if (i < 0 || (j >= 0 && a->index[i] < b->index[j]))
818 dest->index[k] = b->index[j--];
822 dest->index[k] = a->index[i--];
827 /********************************************************************
828 * gmx_ana_indexmap_t and related things
829 ********************************************************************/
832 * \param[in,out] t Output block.
833 * \param[in] top Topology structure
834 * (only used if \p type is \ref INDEX_RES or \ref INDEX_MOL, can be NULL
836 * \param[in] g Index group
837 * (can be NULL if \p type is \ref INDEX_UNKNOWN).
838 * \param[in] type Type of partitioning to make.
839 * \param[in] bComplete
840 * If TRUE, the index group is expanded to include any residue/molecule
841 * (depending on \p type) that is partially contained in the group.
842 * If \p type is not INDEX_RES or INDEX_MOL, this has no effect.
844 * \p m should have been initialized somehow (calloc() is enough) unless
845 * \p type is INDEX_UNKNOWN.
846 * \p g should be sorted.
849 gmx_ana_index_make_block(t_blocka *t, t_topology *top, gmx_ana_index_t *g,
850 e_index_t type, bool bComplete)
855 if (type == INDEX_UNKNOWN)
868 /* bComplete only does something for INDEX_RES or INDEX_MOL, so turn it
870 if (type != INDEX_RES && type != INDEX_MOL)
874 /* Allocate memory for the atom array and fill it unless we are using
879 /* We may allocate some extra memory here because we don't know in
880 * advance how much will be needed. */
881 if (t->nalloc_a < top->atoms.nr)
883 srenew(t->a, top->atoms.nr);
884 t->nalloc_a = top->atoms.nr;
890 if (t->nalloc_a < g->isize)
892 srenew(t->a, g->isize);
893 t->nalloc_a = g->isize;
895 memcpy(t->a, g->index, g->isize*sizeof(*(t->a)));
898 /* Allocate memory for the block index. We don't know in advance
899 * how much will be needed, so we allocate some extra and free it in the
901 if (t->nalloc_index < g->isize + 1)
903 srenew(t->index, g->isize + 1);
904 t->nalloc_index = g->isize + 1;
908 j = 0; /* j is used by residue completion for the first atom not stored */
910 for (i = 0; i < g->isize; ++i)
913 /* Find the ID number of the atom/residue/molecule corresponding to
921 id = top->atoms.atom[ai].resind;
924 while (ai >= top->mols.index[id+1])
929 case INDEX_UNKNOWN: /* Should not occur */
934 /* If this is the first atom in a new block, initialize the block. */
939 /* For completion, we first set the start of the block. */
940 t->index[t->nr++] = t->nra;
941 /* And then we find all the atoms that should be included. */
945 while (top->atoms.atom[j].resind != id)
949 while (j < top->atoms.nr && top->atoms.atom[j].resind == id)
957 for (j = top->mols.index[id]; j < top->mols.index[id+1]; ++j)
963 default: /* Should not be reached */
964 gmx_bug("internal error");
970 /* If not using completion, simply store the start of the block. */
971 t->index[t->nr++] = i;
976 /* Set the end of the last block */
977 t->index[t->nr] = t->nra;
978 /* Free any unnecessary memory */
979 srenew(t->index, t->nr+1);
980 t->nalloc_index = t->nr+1;
983 srenew(t->a, t->nra);
984 t->nalloc_a = t->nra;
989 * \param[in] g Index group to check.
990 * \param[in] b Block data to check against.
991 * \returns TRUE if \p g consists of one or more complete blocks from \p b,
994 * The atoms in \p g are assumed to be sorted.
997 gmx_ana_index_has_full_blocks(gmx_ana_index_t *g, t_block *b)
1002 /* Each round in the loop matches one block */
1003 while (i < g->isize)
1005 /* Find the block that begins with the first unmatched atom */
1006 while (bi < b->nr && b->index[bi] != g->index[i])
1010 /* If not found, or if too large, return */
1011 if (bi == b->nr || i + b->index[bi+1] - b->index[bi] > g->isize)
1015 /* Check that the block matches the index */
1016 for (j = b->index[bi]; j < b->index[bi+1]; ++j, ++i)
1018 if (g->index[i] != j)
1023 /* Move the search to the next block */
1030 * \param[in] g Index group to check.
1031 * \param[in] b Block data to check against.
1032 * \returns TRUE if \p g consists of one or more complete blocks from \p b,
1035 * The atoms in \p g and \p b->a are assumed to be in the same order.
1038 gmx_ana_index_has_full_ablocks(gmx_ana_index_t *g, t_blocka *b)
1043 /* Each round in the loop matches one block */
1044 while (i < g->isize)
1046 /* Find the block that begins with the first unmatched atom */
1047 while (bi < b->nr && b->a[b->index[bi]] != g->index[i])
1051 /* If not found, or if too large, return */
1052 if (bi == b->nr || i + b->index[bi+1] - b->index[bi] > g->isize)
1056 /* Check that the block matches the index */
1057 for (j = b->index[bi]; j < b->index[bi+1]; ++j, ++i)
1059 if (b->a[j] != g->index[i])
1064 /* Move the search to the next block */
1071 * \param[in] g Index group to check.
1072 * \param[in] type Block data to check against.
1073 * \param[in] top Topology data.
1074 * \returns TRUE if \p g consists of one or more complete elements of type
1075 * \p type, FALSE otherwise.
1077 * If \p type is \ref INDEX_ATOM, the return value is always TRUE.
1078 * If \p type is \ref INDEX_UNKNOWN or \ref INDEX_ALL, the return value is
1082 gmx_ana_index_has_complete_elems(gmx_ana_index_t *g, e_index_t type,
1100 for (i = 0; i < g->isize; ++i)
1103 id = top->atoms.atom[ai].resind;
1106 if (ai > 0 && top->atoms.atom[ai-1].resind == id)
1110 if (i > 0 && g->index[i-1] < top->atoms.nr - 1
1111 && top->atoms.atom[g->index[i-1]+1].resind == prev)
1118 if (g->index[i-1] < top->atoms.nr - 1
1119 && top->atoms.atom[g->index[i-1]+1].resind == prev)
1127 return gmx_ana_index_has_full_blocks(g, &top->mols);
1133 * \param[out] m Output structure.
1135 * Any contents of \p m are discarded without freeing.
1138 gmx_ana_indexmap_clear(gmx_ana_indexmap_t *m)
1140 m->type = INDEX_UNKNOWN;
1145 m->mapb.index = NULL;
1146 m->mapb.nalloc_index = 0;
1152 m->b.nalloc_index = 0;
1155 m->bMapStatic = TRUE;
1159 * \param[in,out] m Mapping structure.
1160 * \param[in] nr Maximum number of blocks to reserve space for.
1161 * \param[in] isize Maximum number of atoms to reserve space for.
1164 gmx_ana_indexmap_reserve(gmx_ana_indexmap_t *m, int nr, int isize)
1166 if (m->mapb.nalloc_index < nr + 1)
1168 srenew(m->refid, nr);
1169 srenew(m->mapid, nr);
1170 srenew(m->orgid, nr);
1171 srenew(m->mapb.index, nr + 1);
1172 srenew(m->b.index, nr + 1);
1173 m->mapb.nalloc_index = nr + 1;
1174 m->b.nalloc_index = nr + 1;
1176 if (m->b.nalloc_a < isize)
1178 srenew(m->b.a, isize);
1179 m->b.nalloc_a = isize;
1184 * \param[in,out] m Mapping structure to initialize.
1185 * \param[in] g Index group to map
1186 * (can be NULL if \p type is \ref INDEX_UNKNOWN).
1187 * \param[in] top Topology structure
1188 * (can be NULL if \p type is not \ref INDEX_RES or \ref INDEX_MOL).
1189 * \param[in] type Type of mapping to construct.
1191 * Initializes a new index group mapping.
1192 * The index group provided to gmx_ana_indexmap_update() should always be a
1193 * subset of the \p g given here.
1195 * \p m should have been initialized somehow (calloc() is enough).
1198 gmx_ana_indexmap_init(gmx_ana_indexmap_t *m, gmx_ana_index_t *g,
1199 t_topology *top, e_index_t type)
1204 gmx_ana_index_make_block(&m->b, top, g, type, FALSE);
1205 gmx_ana_indexmap_reserve(m, m->b.nr, m->b.nra);
1207 for (i = mi = 0; i < m->nr; ++i)
1209 ii = (type == INDEX_UNKNOWN ? 0 : m->b.a[m->b.index[i]]);
1216 m->orgid[i] = top->atoms.atom[ii].resind;
1219 while (top->mols.index[mi+1] <= ii)
1231 for (i = 0; i < m->nr; ++i)
1234 m->mapid[i] = m->orgid[i];
1237 memcpy(m->mapb.index, m->b.index, (m->nr+1)*sizeof(*(m->mapb.index)));
1239 m->bMapStatic = TRUE;
1243 * \param[in,out] m Mapping structure to initialize.
1244 * \param[in] b Block information to use for data.
1246 * Frees some memory that is not necessary for static index group mappings.
1247 * Internal pointers are set to point to data in \p b; it is the responsibility
1248 * of the caller to ensure that the block information matches the contents of
1250 * After this function has been called, the index group provided to
1251 * gmx_ana_indexmap_update() should always be the same as \p g given here.
1253 * This function breaks modularity of the index group mapping interface in an
1254 * ugly way, but allows reducing memory usage of static selections by a
1255 * significant amount.
1258 gmx_ana_indexmap_set_static(gmx_ana_indexmap_t *m, t_blocka *b)
1261 m->mapid = m->orgid;
1263 m->b.nalloc_index = 0;
1264 m->b.index = b->index;
1265 sfree(m->mapb.index);
1266 m->mapb.nalloc_index = 0;
1267 m->mapb.index = m->b.index;
1274 * \param[in,out] dest Destination data structure.
1275 * \param[in] src Source mapping.
1276 * \param[in] bFirst If TRUE, memory is allocated for \p dest and a full
1277 * copy is made; otherwise, only variable parts are copied, and no memory
1280 * \p dest should have been initialized somehow (calloc() is enough).
1283 gmx_ana_indexmap_copy(gmx_ana_indexmap_t *dest, gmx_ana_indexmap_t *src, bool bFirst)
1287 gmx_ana_indexmap_reserve(dest, src->b.nr, src->b.nra);
1288 dest->type = src->type;
1289 dest->b.nr = src->b.nr;
1290 dest->b.nra = src->b.nra;
1291 memcpy(dest->orgid, src->orgid, dest->b.nr*sizeof(*dest->orgid));
1292 memcpy(dest->b.index, src->b.index, (dest->b.nr+1)*sizeof(*dest->b.index));
1293 memcpy(dest->b.a, src->b.a, dest->b.nra*sizeof(*dest->b.a));
1296 dest->mapb.nr = src->mapb.nr;
1297 memcpy(dest->refid, src->refid, dest->nr*sizeof(*dest->refid));
1298 memcpy(dest->mapid, src->mapid, dest->nr*sizeof(*dest->mapid));
1299 memcpy(dest->mapb.index, src->mapb.index,(dest->mapb.nr+1)*sizeof(*dest->mapb.index));
1300 dest->bStatic = src->bStatic;
1301 dest->bMapStatic = src->bMapStatic;
1305 * \param[in,out] m Mapping structure.
1306 * \param[in] g Current index group.
1307 * \param[in] bMaskOnly TRUE if the unused blocks should be masked with
1308 * -1 instead of removing them.
1310 * Updates the index group mapping with the new index group \p g.
1312 * \see gmx_ana_indexmap_t
1315 gmx_ana_indexmap_update(gmx_ana_indexmap_t *m, gmx_ana_index_t *g,
1321 /* Process the simple cases first */
1322 if (m->type == INDEX_UNKNOWN && m->b.nra == 0)
1326 if (m->type == INDEX_ALL)
1330 m->mapb.index[1] = g->isize;
1334 /* Reset the reference IDs and mapping if necessary */
1335 bStatic = (g->isize == m->b.nra && m->nr == m->b.nr);
1336 if (bStatic || bMaskOnly)
1340 for (bj = 0; bj < m->b.nr; ++bj)
1347 for (bj = 0; bj < m->b.nr; ++bj)
1349 m->mapid[bj] = m->orgid[bj];
1351 for (bj = 0; bj <= m->b.nr; ++bj)
1353 m->mapb.index[bj] = m->b.index[bj];
1355 m->bMapStatic = TRUE;
1358 /* Exit immediately if the group is static */
1368 for (i = j = bj = 0; i < g->isize; ++i, ++j)
1370 /* Find the next atom in the block */
1371 while (m->b.a[j] != g->index[i])
1375 /* Mark blocks that did not contain any atoms */
1376 while (bj < m->b.nr && m->b.index[bj+1] <= j)
1378 m->refid[bj++] = -1;
1380 /* Advance the block index if we have reached the next block */
1381 if (m->b.index[bj] <= j)
1386 /* Mark the last blocks as not accessible */
1387 while (bj < m->b.nr)
1389 m->refid[bj++] = -1;
1394 for (i = j = bi = 0, bj = -1; i < g->isize; ++i)
1396 /* Find the next atom in the block */
1397 while (m->b.a[j] != g->index[i])
1401 /* If we have reached a new block, add it */
1402 if (m->b.index[bj+1] <= j)
1404 /* Skip any blocks in between */
1405 while (bj < m->b.nr && m->b.index[bj+1] <= j)
1410 m->mapid[bi] = m->orgid[bj];
1411 m->mapb.index[bi] = i;
1415 /* Update the number of blocks */
1416 m->mapb.index[bi] = g->isize;
1418 m->bMapStatic = FALSE;
1425 * \param[in,out] m Mapping structure to free.
1427 * All the memory allocated for the mapping structure is freed, and
1428 * the pointers set to NULL.
1429 * The pointer \p m is not freed.
1432 gmx_ana_indexmap_deinit(gmx_ana_indexmap_t *m)
1435 if (m->mapid != m->orgid)
1439 if (m->mapb.nalloc_index > 0)
1441 sfree(m->mapb.index);
1444 if (m->b.nalloc_index > 0)
1448 if (m->b.nalloc_a > 0)
1452 gmx_ana_indexmap_clear(m);