A bit more modular selection position handling.
[gromacs/qmmm-gamess-us.git] / src / gmxlib / selection / sm_same.c
blob2b07c63dcef7a228a1a53af3c0c5a84461c7ace4
1 /*
3 * This source code is part of
5 * G R O M A C S
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
31 /*! \internal \file
32 * \brief Implementation of the \p same selection method.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 #include <macros.h>
39 #include <smalloc.h>
41 #include <indexutil.h>
42 #include <selmethod.h>
44 /*! \internal \brief
45 * Data structure for the \p same selection method.
47 * All angle values are in the units of radians.
49 typedef struct
51 /*! \brief Input group. */
52 gmx_ana_index_t g;
53 } t_methoddata_same;
55 /*! \brief Allocates data for the \p same selection method. */
56 static void *
57 init_data_same(int npar, gmx_ana_selparam_t *param);
58 /*! \brief Initializes the \p same selection method. */
59 static int
60 init_same(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
61 /*! \brief Frees the data allocated for the \p same selection method. */
62 static void
63 free_data_same(void *data);
64 /*! \brief Initializes the evaluation of the \p same selection method for a frame. */
65 static int
66 init_frame_same(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data);
67 /*! \brief Evaluates the \p same selection method. */
68 static int
69 evaluate_same(t_topology *top, t_trxframe *fr, t_pbc *pbc,
70 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
72 /*! \brief Enum array for the \p same selection method. */
73 static char *same_enum[] = { NULL, "residue", NULL };
75 /*! \brief Parameters for the \p same selection method. */
76 static gmx_ana_selparam_t smparams_same[] = {
77 {NULL, {STR_VALUE, 1, {same_enum}}, NULL, SPAR_ENUMVAL},
78 {"as", {GROUP_VALUE, 1, {NULL}}, NULL, SPAR_DYNAMIC},
81 /*! \internal \brief Selection method data for the \p same method. */
82 gmx_ana_selmethod_t sm_same = {
83 "same", GROUP_VALUE, SMETH_REQTOP,
84 asize(smparams_same), smparams_same,
85 &init_data_same,
86 NULL,
87 NULL,
88 NULL,
89 &free_data_same,
90 NULL,
91 &evaluate_same,
92 NULL,
93 {"same residue as ATOM_EXPR", 0, NULL},
96 /*!
97 * \param[in] npar Not used (should be 2).
98 * \param[in,out] param Method parameters (should point to
99 * \ref smparams_same).
100 * \returns Pointer to the allocated data (\ref t_methoddata_same).
102 static void *
103 init_data_same(int npar, gmx_ana_selparam_t *param)
105 t_methoddata_same *data;
107 snew(data, 1);
108 gmx_ana_index_clear(&data->g);
109 param[1].val.u.g = &data->g;
110 return data;
114 * \param data Data to free (should point to a \ref t_methoddata_same).
116 static void
117 free_data_same(void *data)
119 t_methoddata_same *d = (t_methoddata_same *)data;
121 gmx_ana_index_deinit(&d->g);
125 * See sel_updatefunc() for description of the parameters.
126 * \p data should point to a \c t_methoddata_same.
128 * Calculates which atoms in \p g are in the same residues as the atoms in
129 * \c t_methoddata_same::g.
131 static int
132 evaluate_same(t_topology *top, t_trxframe *fr, t_pbc *pbc,
133 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
135 t_methoddata_same *d = (t_methoddata_same *)data;
136 int i, j, resind;
138 out->u.g->isize = 0;
139 i = j = 0;
140 while (i < d->g.isize)
142 resind = top->atoms.atom[d->g.index[i]].resind;
143 /* Find the first atom in the current residue */
144 while (top->atoms.atom[g->index[j]].resind < resind) ++j;
145 /* Copy all the atoms in the residue to the output */
146 while (j < g->isize && top->atoms.atom[g->index[j]].resind == resind)
148 out->u.g->index[out->u.g->isize++] = g->index[j];
149 ++j;
151 /* Skip the rest of the atoms in the residue */
152 ++i;
153 while (i < d->g.isize && top->atoms.atom[d->g.index[i]].resind == resind) ++i;
155 return 0;