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 the \p merge and \p plus selection modifiers.
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/vec.h"
42 #include "gromacs/selection/position.h"
43 #include "gromacs/selection/selmethod.h"
44 #include "gromacs/utility/exceptions.h"
47 * Data structure for the merging selection modifiers.
51 /** Input positions. */
53 /** Other input positions. */
55 /** Group to store the output atom indices. */
57 /** Stride for merging (\c stride values from \c p1 for each in \c p2). */
61 /** Allocates data for the merging selection modifiers. */
63 init_data_merge(int npar, gmx_ana_selparam_t *param);
64 /** Initializes data for the merging selection modifiers. */
66 init_merge(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
67 /** Initializes output for the \p merge selection modifier. */
69 init_output_merge(t_topology *top, gmx_ana_selvalue_t *out, void *data);
70 /** Initializes output for the \p plus selection modifier. */
72 init_output_plus(t_topology *top, gmx_ana_selvalue_t *out, void *data);
73 /** Frees the memory allocated for the merging selection modifiers. */
75 free_data_merge(void *data);
76 /** Evaluates the \p merge selection modifier. */
78 evaluate_merge(t_topology *top, t_trxframe *fr, t_pbc *pbc,
79 gmx_ana_pos_t *p, gmx_ana_selvalue_t *out, void *data);
80 /** Evaluates the \p plus selection modifier. */
82 evaluate_plus(t_topology *top, t_trxframe *fr, t_pbc *pbc,
83 gmx_ana_pos_t *p, gmx_ana_selvalue_t *out, void *data);
85 /** Parameters for the merging selection modifiers. */
86 static gmx_ana_selparam_t smparams_merge[] = {
87 {NULL, {POS_VALUE, -1, {NULL}}, NULL, SPAR_DYNAMIC | SPAR_VARNUM},
88 {NULL, {POS_VALUE, -1, {NULL}}, NULL, SPAR_DYNAMIC | SPAR_VARNUM},
89 {"stride", {INT_VALUE, 1, {NULL}}, NULL, SPAR_OPTIONAL},
92 /** Help text for the merging selection modifiers. */
93 static const char *help_merge[] = {
94 "MERGING SELECTIONS[PAR]",
96 "[TT]POSEXPR merge POSEXPR [stride INT][tt][BR]",
97 "[TT]POSEXPR merge POSEXPR [merge POSEXPR ...][tt][BR]",
98 "[TT]POSEXPR plus POSEXPR [plus POSEXPR ...][tt][PAR]",
100 "Basic selection keywords can only create selections where each atom",
101 "occurs at most once. The [TT]merge[tt] and [TT]plus[tt] selection",
102 "keywords can be used to work around this limitation. Both create",
103 "a selection that contains the positions from all the given position",
104 "expressions, even if they contain duplicates.",
105 "The difference between the two is that [TT]merge[tt] expects two or more",
106 "selections with the same number of positions, and the output contains",
107 "the input positions selected from each expression in turn, i.e.,",
108 "the output is like A1 B1 A2 B2 and so on. It is also possible to merge",
109 "selections of unequal size as long as the size of the first is a",
110 "multiple of the second one. The [TT]stride[tt] parameter can be used",
111 "to explicitly provide this multiplicity.",
112 "[TT]plus[tt] simply concatenates the positions after each other, and",
113 "can work also with selections of different sizes.",
114 "These keywords are valid only at the selection level, not in any",
118 /** \internal Selection method data for the \p plus modifier. */
119 gmx_ana_selmethod_t sm_merge = {
120 "merge", POS_VALUE, SMETH_MODIFIER,
121 asize(smparams_merge), smparams_merge,
130 {"merge POSEXPR", asize(help_merge), help_merge},
133 /** \internal Selection method data for the \p plus modifier. */
134 gmx_ana_selmethod_t sm_plus = {
135 "plus", POS_VALUE, SMETH_MODIFIER,
136 asize(smparams_merge)-1, smparams_merge,
145 {"plus POSEXPR", asize(help_merge), help_merge},
149 * \param[in] npar Should be 2 for \c plus and 3 for \c merge.
150 * \param[in,out] param Method parameters (should point to a copy of
151 * \ref smparams_merge).
152 * \returns Pointer to the allocated data (\p t_methoddata_merge).
154 * Allocates memory for a \p t_methoddata_merge structure.
157 init_data_merge(int npar, gmx_ana_selparam_t *param)
159 t_methoddata_merge *data;
163 param[0].val.u.p = &data->p1;
164 param[1].val.u.p = &data->p2;
167 param[2].val.u.i = &data->stride;
173 * \param[in] top Not used.
174 * \param[in] npar Not used (should be 2 or 3).
175 * \param[in] param Method parameters (should point to \ref smparams_merge).
176 * \param[in] data Should point to a \p t_methoddata_merge.
177 * \returns 0 if everything is successful, -1 on error.
180 init_merge(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
182 t_methoddata_merge *d = (t_methoddata_merge *)data;
186 GMX_THROW(gmx::InvalidInputError("Stride for merging should be positive"));
188 /* If no stride given, deduce it from the input sizes */
191 d->stride = d->p1.nr / d->p2.nr;
193 if (d->p1.nr != d->stride*d->p2.nr)
195 GMX_THROW(gmx::InconsistentInputError("The number of positions to be merged are not compatible"));
197 /* We access the m.b.nra field instead of g->isize in the position
198 * data structures to handle cases where g is NULL
199 * (this occurs with constant positions. */
200 gmx_ana_index_reserve(&d->g, d->p1.m.b.nra + d->p2.m.b.nra);
201 d->g.isize = d->p1.m.b.nra + d->p2.m.b.nra;
205 * Does common initialization to all merging modifiers.
207 * \param[in] top Topology data structure.
208 * \param[in,out] out Pointer to output data structure.
209 * \param[in,out] data Should point to \c t_methoddata_merge.
212 init_output_common(t_topology *top, gmx_ana_selvalue_t *out, void *data)
214 t_methoddata_merge *d = (t_methoddata_merge *)data;
216 if (d->p1.m.type != d->p2.m.type)
218 /* TODO: Maybe we could pick something else here? */
219 out->u.p->m.type = INDEX_UNKNOWN;
223 out->u.p->m.type = d->p1.m.type;
225 gmx_ana_pos_reserve(out->u.p, d->p1.nr + d->p2.nr, d->g.isize);
228 gmx_ana_pos_reserve_velocities(out->u.p);
232 gmx_ana_pos_reserve_forces(out->u.p);
234 gmx_ana_pos_set_evalgrp(out->u.p, &d->g);
235 gmx_ana_pos_empty_init(out->u.p);
240 * \param[in] top Topology data structure.
241 * \param[in,out] out Pointer to output data structure.
242 * \param[in,out] data Should point to \c t_methoddata_merge.
245 init_output_merge(t_topology *top, gmx_ana_selvalue_t *out, void *data)
247 t_methoddata_merge *d = (t_methoddata_merge *)data;
250 init_output_common(top, out, data);
251 for (i = 0; i < d->p2.nr; ++i)
253 for (j = 0; j < d->stride; ++j)
255 gmx_ana_pos_append_init(out->u.p, &d->g, &d->p1, d->stride*i+j);
257 gmx_ana_pos_append_init(out->u.p, &d->g, &d->p2, i);
262 * \param[in] top Topology data structure.
263 * \param[in,out] out Pointer to output data structure.
264 * \param[in,out] data Should point to \c t_methoddata_merge.
267 init_output_plus(t_topology *top, gmx_ana_selvalue_t *out, void *data)
269 t_methoddata_merge *d = (t_methoddata_merge *)data;
272 init_output_common(top, out, data);
273 for (i = 0; i < d->p1.nr; ++i)
275 gmx_ana_pos_append_init(out->u.p, &d->g, &d->p1, i);
277 for (i = 0; i < d->p2.nr; ++i)
279 gmx_ana_pos_append_init(out->u.p, &d->g, &d->p2, i);
284 * \param data Data to free (should point to a \p t_methoddata_merge).
286 * Frees the memory allocated for \c t_methoddata_merge.
289 free_data_merge(void *data)
291 t_methoddata_merge *d = (t_methoddata_merge *)data;
293 gmx_ana_index_deinit(&d->g);
298 * \param[in] top Not used.
299 * \param[in] fr Not used.
300 * \param[in] pbc Not used.
301 * \param[in] p Positions to merge (should point to \p data->p1).
302 * \param[out] out Output data structure (\p out->u.p is used).
303 * \param[in] data Should point to a \p t_methoddata_merge.
306 evaluate_merge(t_topology *top, t_trxframe *fr, t_pbc *pbc,
307 gmx_ana_pos_t *p, gmx_ana_selvalue_t *out, void *data)
309 t_methoddata_merge *d = (t_methoddata_merge *)data;
313 if (d->p1.nr != d->stride*d->p2.nr)
315 GMX_THROW(gmx::InconsistentInputError("The number of positions to be merged are not compatible"));
318 gmx_ana_pos_empty(out->u.p);
319 for (i = 0; i < d->p2.nr; ++i)
321 for (j = 0; j < d->stride; ++j)
323 refid = d->p1.m.refid[d->stride*i+j];
326 refid = (d->stride+1) * (refid / d->stride) + (refid % d->stride);
328 gmx_ana_pos_append(out->u.p, &d->g, &d->p1, d->stride*i+j, refid);
330 refid = (d->stride+1)*d->p2.m.refid[i]+d->stride;
331 gmx_ana_pos_append(out->u.p, &d->g, &d->p2, i, refid);
333 gmx_ana_pos_append_finish(out->u.p);
337 * \param[in] top Not used.
338 * \param[in] fr Not used.
339 * \param[in] pbc Not used.
340 * \param[in] p Positions to merge (should point to \p data->p1).
341 * \param[out] out Output data structure (\p out->u.p is used).
342 * \param[in] data Should point to a \p t_methoddata_merge.
345 evaluate_plus(t_topology *top, t_trxframe *fr, t_pbc *pbc,
346 gmx_ana_pos_t *p, gmx_ana_selvalue_t *out, void *data)
348 t_methoddata_merge *d = (t_methoddata_merge *)data;
353 gmx_ana_pos_empty(out->u.p);
354 for (i = 0; i < d->p1.nr; ++i)
356 refid = d->p1.m.refid[i];
357 gmx_ana_pos_append(out->u.p, &d->g, &d->p1, i, refid);
359 for (i = 0; i < d->p2.nr; ++i)
361 refid = d->p2.m.refid[i];
364 refid += d->p1.m.b.nr;
366 gmx_ana_pos_append(out->u.p, &d->g, &d->p2, i, refid);
368 gmx_ana_pos_append_finish(out->u.p);