b0108818469d6d71ad80ac89c69f5c039ff293a9
[alexxy/gromacs.git] / src / gromacs / selection / sm_position.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source 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 position evaluation selection methods.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_selection
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/legacyheaders/macros.h"
45
46 #include "gromacs/selection/indexutil.h"
47 #include "gromacs/selection/poscalc.h"
48 #include "gromacs/selection/position.h"
49 #include "gromacs/selection/selmethod.h"
50 #include "gromacs/utility/cstringutil.h"
51 #include "gromacs/utility/smalloc.h"
52
53 #include "keywords.h"
54 #include "selelem.h"
55
56 /*! \internal \brief
57  * Data structure for position keyword evaluation.
58  */
59 typedef struct
60 {
61     /** Position calculation collection to use. */
62     gmx::PositionCalculationCollection *pcc;
63     /** Index group for which the center should be evaluated. */
64     gmx_ana_index_t                     g;
65     /** Position evaluation data structure. */
66     gmx_ana_poscalc_t                  *pc;
67     /** true if periodic boundary conditions should be used. */
68     bool                                bPBC;
69     /** Type of positions to calculate. */
70     char                               *type;
71     /** Flags for the position calculation. */
72     int                                 flags;
73 } t_methoddata_pos;
74
75 /** Allocates data for position evaluation selection methods. */
76 static void *
77 init_data_pos(int npar, gmx_ana_selparam_t *param);
78 /** Sets the position calculation collection for position evaluation selection methods. */
79 static void
80 set_poscoll_pos(gmx::PositionCalculationCollection *pcc, void *data);
81 /*! \brief
82  * Initializes position evaluation keywords.
83  *
84  * \param[in] top   Not used.
85  * \param[in] npar  Not used.
86  * \param[in] param Not used.
87  * \param[in,out] data  Should point to \c t_methoddata_pos.
88  * \returns       0 on success, a non-zero error code on error.
89  *
90  * The \c t_methoddata_pos::type field should have been initialized
91  * externally using _gmx_selelem_set_kwpos_type().
92  */
93 static void
94 init_kwpos(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
95 /*! \brief
96  * Initializes the \p cog selection method.
97  *
98  * \param[in]     top   Topology data structure.
99  * \param[in]     npar  Not used.
100  * \param[in]     param Not used.
101  * \param[in,out] data  Should point to \c t_methoddata_pos.
102  * \returns       0 on success, a non-zero error code on error.
103  */
104 static void
105 init_cog(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
106 /*! \brief
107  * Initializes the \p cog selection method.
108  *
109  * \param[in]     top   Topology data structure.
110  * \param[in]     npar  Not used.
111  * \param[in]     param Not used.
112  * \param[in,out] data  Should point to \c t_methoddata_pos.
113  * \returns       0 on success, a non-zero error code on error.
114  */
115 static void
116 init_com(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
117 /*! \brief
118  * Initializes output for position evaluation selection methods.
119  *
120  * \param[in]     top   Topology data structure.
121  * \param[in,out] out   Pointer to output data structure.
122  * \param[in,out] data  Should point to \c t_methoddata_pos.
123  * \returns       0 for success.
124  */
125 static void
126 init_output_pos(t_topology *top, gmx_ana_selvalue_t *out, void *data);
127 /** Frees the data allocated for position evaluation selection methods. */
128 static void
129 free_data_pos(void *data);
130 /** Evaluates position evaluation selection methods. */
131 static void
132 evaluate_pos(t_topology * /* top */, t_trxframe *fr, t_pbc *pbc, gmx_ana_index_t * /* g */, gmx_ana_selvalue_t *out, void *data);
133
134 /** Parameters for position keyword evaluation. */
135 static gmx_ana_selparam_t smparams_keyword_pos[] = {
136     {NULL,   {GROUP_VALUE, 1, {NULL}}, NULL, SPAR_DYNAMIC},
137 };
138
139 /** Parameters for the \p cog and \p com selection methods. */
140 static gmx_ana_selparam_t smparams_com[] = {
141     {"of",   {GROUP_VALUE, 1, {NULL}}, NULL, SPAR_DYNAMIC},
142     {"pbc",  {NO_VALUE,    0, {NULL}}, NULL, 0},
143 };
144
145 /** Selection method data for position keyword evaluation. */
146 gmx_ana_selmethod_t sm_keyword_pos = {
147     "kw_pos", POS_VALUE, SMETH_DYNAMIC | SMETH_VARNUMVAL | SMETH_ALLOW_UNSORTED,
148     asize(smparams_keyword_pos), smparams_keyword_pos,
149     &init_data_pos,
150     &set_poscoll_pos,
151     &init_kwpos,
152     &init_output_pos,
153     &free_data_pos,
154     NULL,
155     &evaluate_pos,
156     NULL,
157     {NULL, 0, NULL},
158 };
159
160 /** Selection method data for the \p cog method. */
161 gmx_ana_selmethod_t sm_cog = {
162     "cog", POS_VALUE, SMETH_DYNAMIC | SMETH_SINGLEVAL,
163     asize(smparams_com), smparams_com,
164     &init_data_pos,
165     &set_poscoll_pos,
166     &init_cog,
167     &init_output_pos,
168     &free_data_pos,
169     NULL,
170     &evaluate_pos,
171     NULL,
172     {"cog of ATOM_EXPR [pbc]", 0, NULL},
173 };
174
175 /** Selection method data for the \p com method. */
176 gmx_ana_selmethod_t sm_com = {
177     "com", POS_VALUE, SMETH_REQTOP | SMETH_DYNAMIC | SMETH_SINGLEVAL,
178     asize(smparams_com), smparams_com,
179     &init_data_pos,
180     &set_poscoll_pos,
181     &init_com,
182     &init_output_pos,
183     &free_data_pos,
184     NULL,
185     &evaluate_pos,
186     NULL,
187     {"com of ATOM_EXPR [pbc]", 0, NULL},
188 };
189
190 /*!
191  * \param[in]     npar  Should be 1 or 2.
192  * \param[in,out] param Method parameters (should point to
193  *   \ref smparams_keyword_pos or \ref smparams_com).
194  * \returns       Pointer to the allocated data (\c t_methoddata_pos).
195  *
196  * Allocates memory for a \c t_methoddata_pos structure and initializes
197  * the first parameter to define the value for \c t_methoddata_pos::g.
198  * If a second parameter is present, it is used for setting the
199  * \c t_methoddata_pos::bPBC flag.
200  */
201 static void *
202 init_data_pos(int npar, gmx_ana_selparam_t *param)
203 {
204     t_methoddata_pos *data;
205
206     snew(data, 1);
207     param[0].val.u.g = &data->g;
208     if (npar > 1)
209     {
210         param[1].val.u.b = &data->bPBC;
211     }
212     data->pc       = NULL;
213     data->bPBC     = false;
214     data->type     = NULL;
215     data->flags    = -1;
216     return data;
217 }
218
219 /*!
220  * \param[in]     pcc   Position calculation collection to use.
221  * \param[in,out] data  Should point to \c t_methoddata_pos.
222  */
223 static void
224 set_poscoll_pos(gmx::PositionCalculationCollection *pcc, void *data)
225 {
226     ((t_methoddata_pos *)data)->pcc = pcc;
227 }
228
229 /*!
230  * \param[in,out] sel   Selection element to initialize.
231  * \param[in]     type  One of the enum values acceptable for
232  *     PositionCalculationCollection::typeFromEnum().
233  *
234  * Initializes the reference position type for position evaluation.
235  * If called multiple times, the first setting takes effect, and later calls
236  * are neglected.
237  */
238 void
239 _gmx_selelem_set_kwpos_type(gmx::SelectionTreeElement *sel, const char *type)
240 {
241     t_methoddata_pos *d = (t_methoddata_pos *)sel->u.expr.mdata;
242
243     if (sel->type != SEL_EXPRESSION || !sel->u.expr.method
244         || sel->u.expr.method->name != sm_keyword_pos.name)
245     {
246         return;
247     }
248     if (!d->type && type)
249     {
250         d->type  = gmx_strdup(type);
251         /* FIXME: It would be better not to have the string here hardcoded. */
252         if (type[0] != 'a')
253         {
254             sel->u.expr.method->flags |= SMETH_REQTOP;
255         }
256     }
257 }
258
259 /*!
260  * \param[in,out] sel   Selection element to initialize.
261  * \param[in]     flags Default completion flags
262  *     (see PositionCalculationCollection::typeFromEnum()).
263  *
264  * Initializes the flags for position evaluation.
265  * If called multiple times, the first setting takes effect, and later calls
266  * are neglected.
267  */
268 void
269 _gmx_selelem_set_kwpos_flags(gmx::SelectionTreeElement *sel, int flags)
270 {
271     t_methoddata_pos *d = (t_methoddata_pos *)sel->u.expr.mdata;
272
273     if (sel->type != SEL_EXPRESSION || !sel->u.expr.method
274         || sel->u.expr.method->name != sm_keyword_pos.name)
275     {
276         return;
277     }
278     if (d->flags == -1)
279     {
280         d->flags = flags;
281     }
282 }
283
284 static void
285 init_kwpos(t_topology * /* top */, int /* npar */, gmx_ana_selparam_t *param, void *data)
286 {
287     t_methoddata_pos *d = (t_methoddata_pos *)data;
288
289     if (!(param[0].flags & SPAR_DYNAMIC))
290     {
291         d->flags &= ~(POS_DYNAMIC | POS_MASKONLY);
292     }
293     else if (!(d->flags & POS_MASKONLY))
294     {
295         d->flags |= POS_DYNAMIC;
296     }
297     d->pc = d->pcc->createCalculationFromEnum(d->type, d->flags);
298     gmx_ana_poscalc_set_maxindex(d->pc, &d->g);
299 }
300
301 static void
302 init_cog(t_topology * /* top */, int /* npar */, gmx_ana_selparam_t *param, void *data)
303 {
304     t_methoddata_pos *d = (t_methoddata_pos *)data;
305
306     d->flags = (param[0].flags & SPAR_DYNAMIC) ? POS_DYNAMIC : 0;
307     d->pc    = d->pcc->createCalculation(d->bPBC ? POS_ALL_PBC : POS_ALL, d->flags);
308     gmx_ana_poscalc_set_maxindex(d->pc, &d->g);
309 }
310
311 static void
312 init_com(t_topology * /* top */, int /* npar */, gmx_ana_selparam_t *param, void *data)
313 {
314     t_methoddata_pos *d = (t_methoddata_pos *)data;
315
316     d->flags  = (param[0].flags & SPAR_DYNAMIC) ? POS_DYNAMIC : 0;
317     d->flags |= POS_MASS;
318     d->pc     = d->pcc->createCalculation(d->bPBC ? POS_ALL_PBC : POS_ALL, d->flags);
319     gmx_ana_poscalc_set_maxindex(d->pc, &d->g);
320 }
321
322 static void
323 init_output_pos(t_topology * /* top */, gmx_ana_selvalue_t *out, void *data)
324 {
325     t_methoddata_pos *d = (t_methoddata_pos *)data;
326
327     gmx_ana_poscalc_init_pos(d->pc, out->u.p);
328 }
329
330 /*!
331  * \param data Data to free (should point to a \c t_methoddata_pos).
332  *
333  * Frees the memory allocated for \c t_methoddata_pos::g and
334  * \c t_methoddata_pos::pc.
335  */
336 static void
337 free_data_pos(void *data)
338 {
339     t_methoddata_pos *d = (t_methoddata_pos *)data;
340
341     sfree(d->type);
342     gmx_ana_poscalc_free(d->pc);
343     sfree(d);
344 }
345
346 /*!
347  * See sel_updatefunc() for description of the parameters.
348  * \p data should point to a \c t_methoddata_pos.
349  *
350  * Calculates the positions using \c t_methoddata_pos::pc for the index group
351  * in \c t_methoddata_pos::g and stores the results in \p out->u.p.
352  */
353 static void
354 evaluate_pos(t_topology * /* top */, t_trxframe *fr, t_pbc *pbc,
355              gmx_ana_index_t * /* g */, gmx_ana_selvalue_t *out, void *data)
356 {
357     t_methoddata_pos *d = (t_methoddata_pos *)data;
358
359     gmx_ana_poscalc_update(d->pc, out->u.p, &d->g, fr, pbc);
360 }