2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014,2015,2016,2017, 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.
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.
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.
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.
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.
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.
37 * Implements position evaluation selection methods.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
44 #include "gromacs/selection/indexutil.h"
45 #include "gromacs/selection/position.h"
46 #include "gromacs/utility/arraysize.h"
47 #include "gromacs/utility/cstringutil.h"
48 #include "gromacs/utility/smalloc.h"
53 #include "selmethod.h"
56 * Data structure for position keyword evaluation.
60 /** Position calculation collection to use. */
61 gmx::PositionCalculationCollection *pcc;
62 /** Index group for which the center should be evaluated. */
64 /** Position evaluation data structure. */
65 gmx_ana_poscalc_t *pc;
66 /** true if periodic boundary conditions should be used. */
68 /** Type of positions to calculate. */
70 /** Flags for the position calculation. */
74 /** Allocates data for position evaluation selection methods. */
76 init_data_pos(int npar, gmx_ana_selparam_t *param);
77 /** Sets the position calculation collection for position evaluation selection methods. */
79 set_poscoll_pos(gmx::PositionCalculationCollection *pcc, void *data);
81 * Initializes position evaluation keywords.
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.
89 * The \c t_methoddata_pos::type field should have been initialized
90 * externally using _gmx_selelem_set_kwpos_type().
93 init_kwpos(const gmx_mtop_t *top, int npar, gmx_ana_selparam_t *param, void *data);
95 * Initializes the \p cog selection method.
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.
104 init_cog(const gmx_mtop_t *top, int npar, gmx_ana_selparam_t *param, void *data);
106 * Initializes the \p cog selection method.
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.
115 init_com(const gmx_mtop_t *top, int npar, gmx_ana_selparam_t *param, void *data);
117 * Initializes output for position evaluation selection methods.
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.
125 init_output_pos(const gmx_mtop_t *top, gmx_ana_selvalue_t *out, void *data);
126 /** Frees the data allocated for position evaluation selection methods. */
128 free_data_pos(void *data);
129 /** Evaluates position evaluation selection methods. */
131 evaluate_pos(const gmx::SelMethodEvalContext &context,
132 gmx_ana_index_t * /* g */, gmx_ana_selvalue_t *out, void *data);
134 /** Parameters for position keyword evaluation. */
135 static gmx_ana_selparam_t smparams_keyword_pos[] = {
136 {nullptr, {GROUP_VALUE, 1, {nullptr}}, nullptr, SPAR_DYNAMIC},
139 /** Parameters for the \p cog and \p com selection methods. */
140 static gmx_ana_selparam_t smparams_com[] = {
141 {"of", {GROUP_VALUE, 1, {nullptr}}, nullptr, SPAR_DYNAMIC},
142 {"pbc", {NO_VALUE, 0, {nullptr}}, nullptr, 0},
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,
157 {nullptr, nullptr, 0, nullptr},
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,
172 {"cog of ATOM_EXPR [pbc]", nullptr, 0, nullptr},
175 /** Selection method data for the \p com method. */
176 gmx_ana_selmethod_t sm_com = {
177 "com", POS_VALUE, SMETH_REQMASS | SMETH_DYNAMIC | SMETH_SINGLEVAL,
178 asize(smparams_com), smparams_com,
187 {"com of ATOM_EXPR [pbc]", nullptr, 0, nullptr},
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).
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.
202 init_data_pos(int npar, gmx_ana_selparam_t *param)
204 t_methoddata_pos *data;
207 param[0].val.u.g = &data->g;
210 param[1].val.u.b = &data->bPBC;
214 data->type = nullptr;
220 * \param[in] pcc Position calculation collection to use.
221 * \param[in,out] data Should point to \c t_methoddata_pos.
224 set_poscoll_pos(gmx::PositionCalculationCollection *pcc, void *data)
226 ((t_methoddata_pos *)data)->pcc = pcc;
230 _gmx_selelem_is_default_kwpos(const gmx::SelectionTreeElement &sel)
232 if (sel.type != SEL_EXPRESSION || !sel.u.expr.method
233 || sel.u.expr.method->name != sm_keyword_pos.name)
238 t_methoddata_pos *d = static_cast<t_methoddata_pos *>(sel.u.expr.mdata);
239 return d->type == nullptr;
243 * Updates selection method flags about required topology information.
245 * Sets the flags to require topology and/or masses if the position calculation
248 static void set_pos_method_flags(gmx_ana_selmethod_t *method,
251 const bool forces = (d->flags != -1 && (d->flags & POS_FORCES));
252 switch (gmx::PositionCalculationCollection::requiredTopologyInfoForType(d->type, forces))
254 case gmx::PositionCalculationCollection::RequiredTopologyInfo::TopologyAndMasses:
255 method->flags |= SMETH_REQMASS;
257 case gmx::PositionCalculationCollection::RequiredTopologyInfo::Topology:
258 method->flags |= SMETH_REQTOP;
260 case gmx::PositionCalculationCollection::RequiredTopologyInfo::None:
266 * \param[in,out] sel Selection element to initialize.
267 * \param[in] type One of the enum values acceptable for
268 * PositionCalculationCollection::typeFromEnum().
270 * Initializes the reference position type for position evaluation.
271 * If called multiple times, the first setting takes effect, and later calls
275 _gmx_selelem_set_kwpos_type(gmx::SelectionTreeElement *sel, const char *type)
277 t_methoddata_pos *d = (t_methoddata_pos *)sel->u.expr.mdata;
279 if (sel->type != SEL_EXPRESSION || !sel->u.expr.method
280 || sel->u.expr.method->name != sm_keyword_pos.name)
284 if (!d->type && type)
286 d->type = gmx_strdup(type);
287 set_pos_method_flags(sel->u.expr.method, d);
292 * \param[in,out] sel Selection element to initialize.
293 * \param[in] flags Default completion flags
294 * (see PositionCalculationCollection::typeFromEnum()).
296 * Initializes the flags for position evaluation.
297 * If called multiple times, the first setting takes effect, and later calls
301 _gmx_selelem_set_kwpos_flags(gmx::SelectionTreeElement *sel, int flags)
303 t_methoddata_pos *d = (t_methoddata_pos *)sel->u.expr.mdata;
305 if (sel->type != SEL_EXPRESSION || !sel->u.expr.method
306 || sel->u.expr.method->name != sm_keyword_pos.name)
312 GMX_RELEASE_ASSERT(d->type != nullptr,
313 "Position type should be set before flags");
315 set_pos_method_flags(sel->u.expr.method, d);
320 init_kwpos(const gmx_mtop_t * /* top */, int /* npar */, gmx_ana_selparam_t *param, void *data)
322 t_methoddata_pos *d = (t_methoddata_pos *)data;
324 if (!(param[0].flags & SPAR_DYNAMIC))
326 d->flags &= ~(POS_DYNAMIC | POS_MASKONLY);
328 else if (!(d->flags & POS_MASKONLY))
330 d->flags |= POS_DYNAMIC;
332 d->pc = d->pcc->createCalculationFromEnum(d->type, d->flags);
333 gmx_ana_poscalc_set_maxindex(d->pc, &d->g);
337 init_cog(const gmx_mtop_t * /* top */, int /* npar */, gmx_ana_selparam_t *param, void *data)
339 t_methoddata_pos *d = (t_methoddata_pos *)data;
341 d->flags = (param[0].flags & SPAR_DYNAMIC) ? POS_DYNAMIC : 0;
342 d->pc = d->pcc->createCalculation(d->bPBC ? POS_ALL_PBC : POS_ALL, d->flags);
343 gmx_ana_poscalc_set_maxindex(d->pc, &d->g);
347 init_com(const gmx_mtop_t * /* top */, int /* npar */, gmx_ana_selparam_t *param, void *data)
349 t_methoddata_pos *d = (t_methoddata_pos *)data;
351 d->flags = (param[0].flags & SPAR_DYNAMIC) ? POS_DYNAMIC : 0;
352 d->flags |= POS_MASS;
353 d->pc = d->pcc->createCalculation(d->bPBC ? POS_ALL_PBC : POS_ALL, d->flags);
354 gmx_ana_poscalc_set_maxindex(d->pc, &d->g);
358 init_output_pos(const gmx_mtop_t * /* top */, gmx_ana_selvalue_t *out, void *data)
360 t_methoddata_pos *d = (t_methoddata_pos *)data;
362 gmx_ana_poscalc_init_pos(d->pc, out->u.p);
366 * \param data Data to free (should point to a \c t_methoddata_pos).
368 * Frees the memory allocated for \c t_methoddata_pos::g and
369 * \c t_methoddata_pos::pc.
372 free_data_pos(void *data)
374 t_methoddata_pos *d = (t_methoddata_pos *)data;
377 gmx_ana_poscalc_free(d->pc);
382 * See sel_updatefunc() for description of the parameters.
383 * \p data should point to a \c t_methoddata_pos.
385 * Calculates the positions using \c t_methoddata_pos::pc for the index group
386 * in \c t_methoddata_pos::g and stores the results in \p out->u.p.
389 evaluate_pos(const gmx::SelMethodEvalContext &context,
390 gmx_ana_index_t * /* g */, gmx_ana_selvalue_t *out, void *data)
392 t_methoddata_pos *d = (t_methoddata_pos *)data;
394 gmx_ana_poscalc_update(d->pc, out->u.p, &d->g, context.fr, context.pbc);