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
33 * Implements position evaluation selection methods.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_selection
38 #include "gromacs/legacyheaders/macros.h"
39 #include "gromacs/legacyheaders/smalloc.h"
40 #include "gromacs/legacyheaders/string2.h"
42 #include "gromacs/selection/indexutil.h"
43 #include "gromacs/selection/poscalc.h"
44 #include "gromacs/selection/position.h"
45 #include "gromacs/selection/selmethod.h"
51 * Data structure for position keyword evaluation.
55 /** Position calculation collection to use. */
56 gmx::PositionCalculationCollection *pcc;
57 /** Index group for which the center should be evaluated. */
59 /** Position evaluation data structure. */
60 gmx_ana_poscalc_t *pc;
61 /** true if periodic boundary conditions should be used. */
63 /** Type of positions to calculate. */
65 /** Flags for the position calculation. */
69 /** Allocates data for position evaluation selection methods. */
71 init_data_pos(int npar, gmx_ana_selparam_t *param);
72 /** Sets the position calculation collection for position evaluation selection methods. */
74 set_poscoll_pos(gmx::PositionCalculationCollection *pcc, void *data);
75 /** Initializes position evaluation keywords. */
77 init_kwpos(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
78 /** Initializes the \p cog selection method. */
80 init_cog(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
81 /** Initializes the \p cog selection method. */
83 init_com(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
84 /** Initializes output for position evaluation selection methods. */
86 init_output_pos(t_topology *top, gmx_ana_selvalue_t *out, void *data);
87 /** Frees the data allocated for position evaluation selection methods. */
89 free_data_pos(void *data);
90 /** Evaluates position evaluation selection methods. */
92 evaluate_pos(t_topology *top, t_trxframe *fr, t_pbc *pbc,
93 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
95 /** Parameters for position keyword evaluation. */
96 static gmx_ana_selparam_t smparams_keyword_pos[] = {
97 {NULL, {GROUP_VALUE, 1, {NULL}}, NULL, SPAR_DYNAMIC},
100 /** Parameters for the \p cog and \p com selection methods. */
101 static gmx_ana_selparam_t smparams_com[] = {
102 {"of", {GROUP_VALUE, 1, {NULL}}, NULL, SPAR_DYNAMIC},
103 {"pbc", {NO_VALUE, 0, {NULL}}, NULL, 0},
106 /** \internal Selection method data for position keyword evaluation. */
107 gmx_ana_selmethod_t sm_keyword_pos = {
108 "kw_pos", POS_VALUE, SMETH_DYNAMIC | SMETH_VARNUMVAL,
109 asize(smparams_keyword_pos), smparams_keyword_pos,
121 /** \internal Selection method data for the \p cog method. */
122 gmx_ana_selmethod_t sm_cog = {
123 "cog", POS_VALUE, SMETH_DYNAMIC | SMETH_SINGLEVAL,
124 asize(smparams_com), smparams_com,
133 {"cog of ATOM_EXPR [pbc]", 0, NULL},
136 /** \internal Selection method data for the \p com method. */
137 gmx_ana_selmethod_t sm_com = {
138 "com", POS_VALUE, SMETH_REQTOP | SMETH_DYNAMIC | SMETH_SINGLEVAL,
139 asize(smparams_com), smparams_com,
148 {"com of ATOM_EXPR [pbc]", 0, NULL},
152 * \param[in] npar Should be 1 or 2.
153 * \param[in,out] param Method parameters (should point to
154 * \ref smparams_keyword_pos or \ref smparams_com).
155 * \returns Pointer to the allocated data (\c t_methoddata_pos).
157 * Allocates memory for a \c t_methoddata_pos structure and initializes
158 * the first parameter to define the value for \c t_methoddata_pos::g.
159 * If a second parameter is present, it is used for setting the
160 * \c t_methoddata_pos::bPBC flag.
163 init_data_pos(int npar, gmx_ana_selparam_t *param)
165 t_methoddata_pos *data;
168 param[0].val.u.g = &data->g;
171 param[1].val.u.b = &data->bPBC;
181 * \param[in] pcc Position calculation collection to use.
182 * \param[in,out] data Should point to \c t_methoddata_pos.
185 set_poscoll_pos(gmx::PositionCalculationCollection *pcc, void *data)
187 ((t_methoddata_pos *)data)->pcc = pcc;
191 * \param[in,out] sel Selection element to initialize.
192 * \param[in] type One of the enum values acceptable for
193 * PositionCalculationCollection::typeFromEnum().
195 * Initializes the reference position type for position evaluation.
196 * If called multiple times, the first setting takes effect, and later calls
200 _gmx_selelem_set_kwpos_type(gmx::SelectionTreeElement *sel, const char *type)
202 t_methoddata_pos *d = (t_methoddata_pos *)sel->u.expr.mdata;
204 if (sel->type != SEL_EXPRESSION || !sel->u.expr.method
205 || sel->u.expr.method->name != sm_keyword_pos.name)
209 if (!d->type && type)
211 d->type = strdup(type);
212 /* FIXME: It would be better not to have the string here hardcoded. */
215 sel->u.expr.method->flags |= SMETH_REQTOP;
221 * \param[in,out] sel Selection element to initialize.
222 * \param[in] flags Default completion flags
223 * (see PositionCalculationCollection::typeFromEnum()).
225 * Initializes the flags for position evaluation.
226 * If called multiple times, the first setting takes effect, and later calls
230 _gmx_selelem_set_kwpos_flags(gmx::SelectionTreeElement *sel, int flags)
232 t_methoddata_pos *d = (t_methoddata_pos *)sel->u.expr.mdata;
234 if (sel->type != SEL_EXPRESSION || !sel->u.expr.method
235 || sel->u.expr.method->name != sm_keyword_pos.name)
246 * \param[in] top Not used.
247 * \param[in] npar Not used.
248 * \param[in] param Not used.
249 * \param[in,out] data Should point to \c t_methoddata_pos.
250 * \returns 0 on success, a non-zero error code on error.
252 * The \c t_methoddata_pos::type field should have been initialized
253 * externally using _gmx_selelem_set_kwpos_type().
256 init_kwpos(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
258 t_methoddata_pos *d = (t_methoddata_pos *)data;
260 if (!(param[0].flags & SPAR_DYNAMIC))
262 d->flags &= ~(POS_DYNAMIC | POS_MASKONLY);
264 else if (!(d->flags & POS_MASKONLY))
266 d->flags |= POS_DYNAMIC;
268 d->pc = d->pcc->createCalculationFromEnum(d->type, d->flags);
269 gmx_ana_poscalc_set_maxindex(d->pc, &d->g);
273 * \param[in] top Topology data structure.
274 * \param[in] npar Not used.
275 * \param[in] param Not used.
276 * \param[in,out] data Should point to \c t_methoddata_pos.
277 * \returns 0 on success, a non-zero error code on error.
280 init_cog(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
282 t_methoddata_pos *d = (t_methoddata_pos *)data;
284 d->flags = (param[0].flags & SPAR_DYNAMIC) ? POS_DYNAMIC : 0;
285 d->pc = d->pcc->createCalculation(d->bPBC ? POS_ALL_PBC : POS_ALL, d->flags);
286 gmx_ana_poscalc_set_maxindex(d->pc, &d->g);
290 * \param[in] top Topology data structure.
291 * \param[in] npar Not used.
292 * \param[in] param Not used.
293 * \param[in,out] data Should point to \c t_methoddata_pos.
294 * \returns 0 on success, a non-zero error code on error.
297 init_com(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
299 t_methoddata_pos *d = (t_methoddata_pos *)data;
301 d->flags = (param[0].flags & SPAR_DYNAMIC) ? POS_DYNAMIC : 0;
302 d->flags |= POS_MASS;
303 d->pc = d->pcc->createCalculation(d->bPBC ? POS_ALL_PBC : POS_ALL, d->flags);
304 gmx_ana_poscalc_set_maxindex(d->pc, &d->g);
308 * \param[in] top Topology data structure.
309 * \param[in,out] out Pointer to output data structure.
310 * \param[in,out] data Should point to \c t_methoddata_pos.
311 * \returns 0 for success.
314 init_output_pos(t_topology *top, gmx_ana_selvalue_t *out, void *data)
316 t_methoddata_pos *d = (t_methoddata_pos *)data;
318 gmx_ana_poscalc_init_pos(d->pc, out->u.p);
319 gmx_ana_pos_set_evalgrp(out->u.p, &d->g);
323 * \param data Data to free (should point to a \c t_methoddata_pos).
325 * Frees the memory allocated for \c t_methoddata_pos::g and
326 * \c t_methoddata_pos::pc.
329 free_data_pos(void *data)
331 t_methoddata_pos *d = (t_methoddata_pos *)data;
334 gmx_ana_poscalc_free(d->pc);
339 * See sel_updatefunc() for description of the parameters.
340 * \p data should point to a \c t_methoddata_pos.
342 * Calculates the positions using \c t_methoddata_pos::pc for the index group
343 * in \c t_methoddata_pos::g and stores the results in \p out->u.p.
346 evaluate_pos(t_topology *top, t_trxframe *fr, t_pbc *pbc,
347 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
349 t_methoddata_pos *d = (t_methoddata_pos *)data;
351 gmx_ana_poscalc_update(d->pc, out->u.p, &d->g, fr, pbc);