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
32 * \brief Implementation of the merging selection modifier.
43 #include <selmethod.h>
46 * Data structure for the merging selection modifiers.
50 /** Input positions. */
52 /** Other input positions. */
54 /** Group to store the output atom indices. */
56 /** Stride for merging (\c stride values from \c p1 for each in \c p2). */
60 /** Allocates data for the merging selection modifiers. */
62 init_data_merge(int npar
, gmx_ana_selparam_t
*param
);
63 /** Initializes data for the merging selection modifiers. */
65 init_merge(t_topology
*top
, int npar
, gmx_ana_selparam_t
*param
, void *data
);
66 /** Initializes output for the \p merge selection modifier. */
68 init_output_merge(t_topology
*top
, gmx_ana_selvalue_t
*out
, void *data
);
69 /** Initializes output for the \p plus selection modifier. */
71 init_output_plus(t_topology
*top
, gmx_ana_selvalue_t
*out
, void *data
);
72 /** Frees the memory allocated for the merging selection modifiers. */
74 free_data_merge(void *data
);
75 /** Evaluates the \p merge selection modifier. */
77 evaluate_merge(t_topology
*top
, t_trxframe
*fr
, t_pbc
*pbc
,
78 gmx_ana_pos_t
*p
, gmx_ana_selvalue_t
*out
, void *data
);
79 /** Evaluates the \p plus selection modifier. */
81 evaluate_plus(t_topology
*top
, t_trxframe
*fr
, t_pbc
*pbc
,
82 gmx_ana_pos_t
*p
, gmx_ana_selvalue_t
*out
, void *data
);
84 /** Parameters for the merging selection modifiers. */
85 static gmx_ana_selparam_t smparams_merge
[] = {
86 {NULL
, {POS_VALUE
, -1, {NULL
}}, NULL
, SPAR_DYNAMIC
| SPAR_VARNUM
},
87 {NULL
, {POS_VALUE
, -1, {NULL
}}, NULL
, SPAR_DYNAMIC
| SPAR_VARNUM
},
88 {"stride", {INT_VALUE
, 1, {NULL
}}, NULL
, SPAR_OPTIONAL
},
91 /** Help text for the merging selection modifiers. */
92 static const char *help_merge
[] = {
93 "MERGING SELECTIONS[PAR]",
95 "[TT]POSEXPR merge POSEXPR [stride INT][tt][BR]",
96 "[TT]POSEXPR merge POSEXPR [merge POSEXPR ...][tt][BR]",
97 "[TT]POSEXPR plus POSEXPR [plus POSEXPR ...][tt][PAR]",
99 "Basic selection keywords can only create selections where each atom",
100 "occurs at most once. The [TT]merge[tt] and [TT]plus[tt] selection",
101 "keywords can be used to work around this limitation. Both create",
102 "a selection that contains the positions from all the given position",
103 "expressions, even if they contain duplicates.",
104 "The difference between the two is that [TT]merge[tt] expects two or more",
105 "selections with the same number of positions, and the output contains",
106 "the input positions selected from each expression in turn, i.e.,",
107 "the output is like A1 B1 A2 B2 and so on. It is also possible to merge",
108 "selections of unequal size as long as the size of the first is a",
109 "multiple of the second one. The [TT]stride[tt] parameter can be used",
110 "to explicitly provide this multiplicity.",
111 "[TT]plus[tt] simply concatenates the positions after each other, and",
112 "can work also with selections of different sizes.",
113 "These keywords are valid only at the selection level, not in any",
114 "subexpressions.[PAR]",
117 /** \internal Selection method data for the \p plus modifier. */
118 gmx_ana_selmethod_t sm_merge
= {
119 "merge", POS_VALUE
, SMETH_MODIFIER
,
120 asize(smparams_merge
), smparams_merge
,
129 {"merge POSEXPR", asize(help_merge
), help_merge
},
132 /** \internal Selection method data for the \p plus modifier. */
133 gmx_ana_selmethod_t sm_plus
= {
134 "plus", POS_VALUE
, SMETH_MODIFIER
,
135 asize(smparams_merge
)-1, smparams_merge
,
144 {"plus POSEXPR", asize(help_merge
), help_merge
},
148 * \param[in] npar Should be 2 for \c plus and 3 for \c merge.
149 * \param[in,out] param Method parameters (should point to a copy of
150 * \ref smparams_merge).
151 * \returns Pointer to the allocated data (\p t_methoddata_merge).
153 * Allocates memory for a \p t_methoddata_merge structure.
156 init_data_merge(int npar
, gmx_ana_selparam_t
*param
)
158 t_methoddata_merge
*data
;
162 param
[0].val
.u
.p
= &data
->p1
;
163 param
[1].val
.u
.p
= &data
->p2
;
166 param
[2].val
.u
.i
= &data
->stride
;
172 * \param[in] top Not used.
173 * \param[in] npar Not used (should be 2 or 3).
174 * \param[in] param Method parameters (should point to \ref smparams_merge).
175 * \param[in] data Should point to a \p t_methoddata_merge.
176 * \returns 0 if everything is successful, -1 on error.
179 init_merge(t_topology
*top
, int npar
, gmx_ana_selparam_t
*param
, void *data
)
181 t_methoddata_merge
*d
= (t_methoddata_merge
*)data
;
186 fprintf(stderr
, "error: stride for merging should be positive\n");
189 /* If no stride given, deduce it from the input sizes */
192 d
->stride
= d
->p1
.nr
/ d
->p2
.nr
;
194 if (d
->p1
.nr
!= d
->stride
*d
->p2
.nr
)
196 fprintf(stderr
, "error: the number of positions to be merged are not compatible\n");
199 gmx_ana_index_reserve(&d
->g
, d
->p1
.g
->isize
+ d
->p2
.g
->isize
);
200 d
->g
.isize
= d
->p1
.g
->isize
+ d
->p2
.g
->isize
;
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.
210 * \returns 0 for success.
213 init_output_common(t_topology
*top
, gmx_ana_selvalue_t
*out
, void *data
)
215 t_methoddata_merge
*d
= (t_methoddata_merge
*)data
;
217 if (d
->p1
.m
.type
!= d
->p2
.m
.type
)
219 /* TODO: Maybe we could pick something else here? */
220 out
->u
.p
->m
.type
= INDEX_UNKNOWN
;
224 out
->u
.p
->m
.type
= d
->p1
.m
.type
;
226 gmx_ana_pos_reserve(out
->u
.p
, d
->p1
.nr
+ d
->p2
.nr
, d
->g
.isize
);
227 gmx_ana_pos_set_evalgrp(out
->u
.p
, &d
->g
);
228 gmx_ana_pos_empty_init(out
->u
.p
);
234 * \param[in] top Topology data structure.
235 * \param[in,out] out Pointer to output data structure.
236 * \param[in,out] data Should point to \c t_methoddata_merge.
237 * \returns 0 for success.
240 init_output_merge(t_topology
*top
, gmx_ana_selvalue_t
*out
, void *data
)
242 t_methoddata_merge
*d
= (t_methoddata_merge
*)data
;
245 init_output_common(top
, out
, data
);
246 for (i
= 0; i
< d
->p2
.nr
; ++i
)
248 for (j
= 0; j
< d
->stride
; ++j
)
250 gmx_ana_pos_append_init(out
->u
.p
, &d
->g
, &d
->p1
, d
->stride
*i
+j
);
252 gmx_ana_pos_append_init(out
->u
.p
, &d
->g
, &d
->p2
, i
);
258 * \param[in] top Topology data structure.
259 * \param[in,out] out Pointer to output data structure.
260 * \param[in,out] data Should point to \c t_methoddata_merge.
261 * \returns 0 for success.
264 init_output_plus(t_topology
*top
, gmx_ana_selvalue_t
*out
, void *data
)
266 t_methoddata_merge
*d
= (t_methoddata_merge
*)data
;
269 init_output_common(top
, out
, data
);
270 for (i
= 0; i
< d
->p1
.nr
; ++i
)
272 gmx_ana_pos_append_init(out
->u
.p
, &d
->g
, &d
->p1
, i
);
274 for (i
= 0; i
< d
->p2
.nr
; ++i
)
276 gmx_ana_pos_append_init(out
->u
.p
, &d
->g
, &d
->p2
, i
);
282 * \param data Data to free (should point to a \p t_methoddata_merge).
284 * Frees the memory allocated for \c t_methoddata_merge.
287 free_data_merge(void *data
)
289 t_methoddata_merge
*d
= (t_methoddata_merge
*)data
;
291 gmx_ana_pos_deinit(&d
->p1
);
292 gmx_ana_pos_deinit(&d
->p2
);
293 gmx_ana_index_deinit(&d
->g
);
297 * \param[in] top Not used.
298 * \param[in] fr Not used.
299 * \param[in] pbc Not used.
300 * \param[in] p Positions to merge (should point to \p data->p1).
301 * \param[out] out Output data structure (\p out->u.p is used).
302 * \param[in] data Should point to a \p t_methoddata_merge.
303 * \returns 0 on success.
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 fprintf(stderr
, "error: the number of positions to be merged are not compatible\n");
319 gmx_ana_pos_empty(out
->u
.p
);
320 for (i
= 0; i
< d
->p2
.nr
; ++i
)
322 for (j
= 0; j
< d
->stride
; ++j
)
324 refid
= d
->p1
.m
.refid
[d
->stride
*i
+j
];
327 refid
= (d
->stride
+1) * (refid
/ d
->stride
) + (refid
% d
->stride
);
329 gmx_ana_pos_append(out
->u
.p
, &d
->g
, &d
->p1
, d
->stride
*i
+j
, refid
);
331 refid
= (d
->stride
+1)*d
->p2
.m
.refid
[i
]+d
->stride
;
332 gmx_ana_pos_append(out
->u
.p
, &d
->g
, &d
->p2
, i
, refid
);
334 gmx_ana_pos_append_finish(out
->u
.p
);
339 * \param[in] top Not used.
340 * \param[in] fr Not used.
341 * \param[in] pbc Not used.
342 * \param[in] p Positions to merge (should point to \p data->p1).
343 * \param[out] out Output data structure (\p out->u.p is used).
344 * \param[in] data Should point to a \p t_methoddata_merge.
345 * \returns 0 on success.
348 evaluate_plus(t_topology
*top
, t_trxframe
*fr
, t_pbc
*pbc
,
349 gmx_ana_pos_t
*p
, gmx_ana_selvalue_t
*out
, void *data
)
351 t_methoddata_merge
*d
= (t_methoddata_merge
*)data
;
356 gmx_ana_pos_empty(out
->u
.p
);
357 for (i
= 0; i
< d
->p1
.nr
; ++i
)
359 refid
= d
->p1
.m
.refid
[i
];
360 gmx_ana_pos_append(out
->u
.p
, &d
->g
, &d
->p1
, i
, refid
);
362 for (i
= 0; i
< d
->p2
.nr
; ++i
)
364 refid
= d
->p2
.m
.refid
[i
];
367 refid
+= d
->p1
.m
.b
.nr
;
369 gmx_ana_pos_append(out
->u
.p
, &d
->g
, &d
->p2
, i
, refid
);
371 gmx_ana_pos_append_finish(out
->u
.p
);