Fix warnings with Doxygen 1.8.5.
[alexxy/gromacs.git] / src / gromacs / selection / sm_distance.cpp
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 /*! \internal \file
36  * \brief
37  * Implements distance-based selection methods.
38  *
39  * This file implements the \p distance, \p mindistance and \p within
40  * selection methods.
41  *
42  * \author Teemu Murtola <teemu.murtola@gmail.com>
43  * \ingroup module_selection
44  */
45 #include "gromacs/legacyheaders/macros.h"
46 #include "gromacs/legacyheaders/pbc.h"
47 #include "gromacs/legacyheaders/vec.h"
48
49 #include "gromacs/selection/nbsearch.h"
50 #include "gromacs/selection/position.h"
51 #include "gromacs/selection/selmethod.h"
52 #include "gromacs/utility/exceptions.h"
53
54 /*! \internal
55  * \brief
56  * Data structure for distance-based selection method.
57  *
58  * The same data structure is used by all the distance-based methods.
59  *
60  * \ingroup module_selection
61  */
62 struct t_methoddata_distance
63 {
64     t_methoddata_distance() : cutoff(-1.0)
65     {
66     }
67
68     /** Cutoff distance. */
69     real                             cutoff;
70     /** Positions of the reference points. */
71     gmx_ana_pos_t                    p;
72     /** Neighborhood search data. */
73     gmx::AnalysisNeighborhood        nb;
74     /** Neighborhood search for an invididual frame. */
75     gmx::AnalysisNeighborhoodSearch  nbsearch;
76 };
77
78 /** Allocates data for distance-based selection methods. */
79 static void *
80 init_data_common(int npar, gmx_ana_selparam_t *param);
81 /** Initializes a distance-based selection method. */
82 static void
83 init_common(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
84 /** Frees the data allocated for a distance-based selection method. */
85 static void
86 free_data_common(void *data);
87 /** Initializes the evaluation of a distance-based within selection method for a frame. */
88 static void
89 init_frame_common(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data);
90 /** Evaluates the \p distance selection method. */
91 static void
92 evaluate_distance(t_topology *top, t_trxframe *fr, t_pbc *pbc,
93                   gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data);
94 /** Evaluates the \p within selection method. */
95 static void
96 evaluate_within(t_topology *top, t_trxframe *fr, t_pbc *pbc,
97                 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data);
98
99 /** Parameters for the \p distance selection method. */
100 static gmx_ana_selparam_t smparams_distance[] = {
101     {"cutoff", {REAL_VALUE, 1, {NULL}}, NULL, SPAR_OPTIONAL},
102     {"from",   {POS_VALUE,  1, {NULL}}, NULL, SPAR_DYNAMIC},
103 };
104
105 /** Parameters for the \p mindistance selection method. */
106 static gmx_ana_selparam_t smparams_mindistance[] = {
107     {"cutoff", {REAL_VALUE, 1, {NULL}}, NULL, SPAR_OPTIONAL},
108     {"from",   {POS_VALUE, -1, {NULL}}, NULL, SPAR_DYNAMIC | SPAR_VARNUM},
109 };
110
111 /** Parameters for the \p within selection method. */
112 static gmx_ana_selparam_t smparams_within[] = {
113     {NULL, {REAL_VALUE,  1, {NULL}}, NULL, 0},
114     {"of", {POS_VALUE,  -1, {NULL}}, NULL, SPAR_DYNAMIC | SPAR_VARNUM},
115 };
116
117 /** Help text for the distance selection methods. */
118 static const char *help_distance[] = {
119     "DISTANCE-BASED SELECTION KEYWORDS[PAR]",
120
121     "[TT]distance from POS [cutoff REAL][tt][BR]",
122     "[TT]mindistance from POS_EXPR [cutoff REAL][tt][BR]",
123     "[TT]within REAL of POS_EXPR[tt][PAR]",
124
125     "[TT]distance[tt] and [TT]mindistance[tt] calculate the distance from the",
126     "given position(s), the only difference being in that [TT]distance[tt]",
127     "only accepts a single position, while any number of positions can be",
128     "given for [TT]mindistance[tt], which then calculates the distance to the",
129     "closest position.",
130     "[TT]within[tt] directly selects atoms that are within [TT]REAL[tt] of",
131     "[TT]POS_EXPR[tt].[PAR]",
132
133     "For the first two keywords, it is possible to specify a cutoff to speed",
134     "up the evaluation: all distances above the specified cutoff are",
135     "returned as equal to the cutoff.",
136 };
137
138 /** \internal Selection method data for the \p distance method. */
139 gmx_ana_selmethod_t sm_distance = {
140     "distance", REAL_VALUE, SMETH_DYNAMIC,
141     asize(smparams_distance), smparams_distance,
142     &init_data_common,
143     NULL,
144     &init_common,
145     NULL,
146     &free_data_common,
147     &init_frame_common,
148     NULL,
149     &evaluate_distance,
150     {"distance from POS [cutoff REAL]", asize(help_distance), help_distance},
151 };
152
153 /** \internal Selection method data for the \p distance method. */
154 gmx_ana_selmethod_t sm_mindistance = {
155     "mindistance", REAL_VALUE, SMETH_DYNAMIC,
156     asize(smparams_mindistance), smparams_mindistance,
157     &init_data_common,
158     NULL,
159     &init_common,
160     NULL,
161     &free_data_common,
162     &init_frame_common,
163     NULL,
164     &evaluate_distance,
165     {"mindistance from POS_EXPR [cutoff REAL]", asize(help_distance), help_distance},
166 };
167
168 /** \internal Selection method data for the \p within method. */
169 gmx_ana_selmethod_t sm_within = {
170     "within", GROUP_VALUE, SMETH_DYNAMIC,
171     asize(smparams_within), smparams_within,
172     &init_data_common,
173     NULL,
174     &init_common,
175     NULL,
176     &free_data_common,
177     &init_frame_common,
178     NULL,
179     &evaluate_within,
180     {"within REAL of POS_EXPR", asize(help_distance), help_distance},
181 };
182
183 /*!
184  * \param[in]     npar  Not used (should be 2).
185  * \param[in,out] param Method parameters (should point to one of the distance
186  *   parameter arrays).
187  * \returns       Pointer to the allocated data (\c t_methoddata_distance).
188  *
189  * Allocates memory for a \c t_methoddata_distance structure and
190  * initializes the parameter as follows:
191  *  - the first parameter defines the value for
192  *    \c t_methoddata_distance::cutoff.
193  *  - the second parameter defines the reference positions and the value is
194  *    stored in \c t_methoddata_distance::p.
195  */
196 static void *
197 init_data_common(int npar, gmx_ana_selparam_t *param)
198 {
199     t_methoddata_distance *data = new t_methoddata_distance();
200     param[0].val.u.r = &data->cutoff;
201     param[1].val.u.p = &data->p;
202     return data;
203 }
204
205 /*!
206  * \param   top   Not used.
207  * \param   npar  Not used (should be 2).
208  * \param   param Method parameters (should point to one of the distance
209  *   parameter arrays).
210  * \param   data  Pointer to \c t_methoddata_distance to initialize.
211  * \returns 0 on success, a non-zero error code on failure.
212  *
213  * Initializes the neighborhood search data structure
214  * (\c t_methoddata_distance::nb).
215  * Also checks that the cutoff is valid.
216  */
217 static void
218 init_common(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
219 {
220     t_methoddata_distance *d = (t_methoddata_distance *)data;
221
222     if ((param[0].flags & SPAR_SET) && d->cutoff <= 0)
223     {
224         GMX_THROW(gmx::InvalidInputError("Distance cutoff should be > 0"));
225     }
226     d->nb.setCutoff(d->cutoff);
227 }
228
229 /*!
230  * \param data Data to free (should point to a \c t_methoddata_distance).
231  *
232  * Frees the memory allocated for \c t_methoddata_distance::xref and
233  * \c t_methoddata_distance::nb.
234  */
235 static void
236 free_data_common(void *data)
237 {
238     delete static_cast<t_methoddata_distance *>(data);
239 }
240
241 /*!
242  * \param[in]  top  Not used.
243  * \param[in]  fr   Current frame.
244  * \param[in]  pbc  PBC structure.
245  * \param      data Should point to a \c t_methoddata_distance.
246  * \returns    0 on success, a non-zero error code on error.
247  *
248  * Initializes the neighborhood search for the current frame.
249  */
250 static void
251 init_frame_common(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data)
252 {
253     t_methoddata_distance *d = (t_methoddata_distance *)data;
254
255     d->nbsearch.reset();
256     gmx::AnalysisNeighborhoodPositions pos(d->p.x, d->p.count());
257     d->nbsearch = d->nb.initSearch(pbc, pos);
258 }
259
260 /*!
261  * See sel_updatefunc_pos() for description of the parameters.
262  * \p data should point to a \c t_methoddata_distance.
263  *
264  * Calculates the distance of each position from \c t_methoddata_distance::p
265  * and puts them in \p out->u.r.
266  */
267 static void
268 evaluate_distance(t_topology *top, t_trxframe *fr, t_pbc *pbc,
269                   gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data)
270 {
271     t_methoddata_distance *d = (t_methoddata_distance *)data;
272
273     out->nr = pos->m.mapb.nra;
274     for (int b = 0; b < pos->count(); ++b)
275     {
276         real dist = d->nbsearch.minimumDistance(pos->x[b]);
277         for (int i = pos->m.mapb.index[b]; i < pos->m.mapb.index[b+1]; ++i)
278         {
279             out->u.r[i] = dist;
280         }
281     }
282 }
283
284 /*!
285  * See sel_updatefunc() for description of the parameters.
286  * \p data should point to a \c t_methoddata_distance.
287  *
288  * Finds the atoms that are closer than the defined cutoff to
289  * \c t_methoddata_distance::xref and puts them in \p out.g.
290  */
291 static void
292 evaluate_within(t_topology *top, t_trxframe *fr, t_pbc *pbc,
293                 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data)
294 {
295     t_methoddata_distance *d = (t_methoddata_distance *)data;
296
297     out->u.g->isize = 0;
298     for (int b = 0; b < pos->count(); ++b)
299     {
300         if (d->nbsearch.isWithin(pos->x[b]))
301         {
302             gmx_ana_pos_add_to_group(out->u.g, pos, b);
303         }
304     }
305 }