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