2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014,2015,2016, 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 functions in evaluate.h.
40 * One of the major bottlenecks for selection performance is that all the
41 * evaluation is carried out for atoms.
42 * There are several cases when the evaluation could be done for residues
43 * or molecules instead, including keywords that select by residue and
44 * cases where residue centers are used as reference positions.
45 * Implementing this would require a mechanism for recognizing whether
46 * something can be evaluated by residue/molecule instead by atom, and
47 * converting selections by residue/molecule into selections by atom
50 * \author Teemu Murtola <teemu.murtola@gmail.com>
51 * \ingroup module_selection
61 #include "gromacs/math/utilities.h"
62 #include "gromacs/math/vec.h"
63 #include "gromacs/selection/indexutil.h"
64 #include "gromacs/selection/selection.h"
65 #include "gromacs/utility/exceptions.h"
66 #include "gromacs/utility/gmxassert.h"
67 #include "gromacs/utility/smalloc.h"
71 #include "selectioncollection-impl.h"
73 #include "selmethod.h"
75 using gmx::SelectionTreeElement
;
76 using gmx::SelectionTreeElementPointer
;
82 * Reserves memory for a selection element from the evaluation memory pool.
84 * This class implements RAII semantics for allocating memory for selection
85 * element values from a selection evaluation memory pool.
87 * \ingroup module_selection
89 class MempoolSelelemReserver
92 //! Constructs a reserver without initial reservation.
93 MempoolSelelemReserver() {}
95 * Constructs a reserver with initial reservation.
97 * \param[in,out] sel Selection element for which to reserve.
98 * \param[in] count Number of values to reserve.
102 MempoolSelelemReserver(const SelectionTreeElementPointer
&sel
, int count
)
106 //! Frees any memory allocated using this reserver.
107 ~MempoolSelelemReserver()
111 sel_
->mempoolRelease();
116 * Reserves memory for selection element values using this reserver.
118 * \param[in,out] sel Selection element for which to reserve.
119 * \param[in] count Number of values to reserve.
121 * Allocates space to store \p count output values in \p sel from the
122 * memory pool associated with \p sel, or from the heap if there is no
123 * memory pool. Type of values to allocate is automatically determined
126 void reserve(const SelectionTreeElementPointer
&sel
, int count
)
128 GMX_RELEASE_ASSERT(!sel_
,
129 "Can only reserve one element with one instance");
130 sel
->mempoolReserve(count
);
135 SelectionTreeElementPointer sel_
;
139 * Reserves memory for an index group from the evaluation memory pool.
141 * This class implements RAII semantics for allocating memory for an index
142 * group from a selection evaluation memory pool.
144 * \ingroup module_selection
146 class MempoolGroupReserver
150 * Creates a reserver associated with a given memory pool.
152 * \param mp Memory pool from which to reserve memory.
154 explicit MempoolGroupReserver(gmx_sel_mempool_t
*mp
)
158 //! Frees any memory allocated using this reserver.
159 ~MempoolGroupReserver()
163 _gmx_sel_mempool_free_group(mp_
, g_
);
168 * Reserves memory for an index group using this reserver.
170 * \param[in,out] g Index group to reserve.
171 * \param[in] count Number of atoms to reserve space for.
173 * Allocates memory from the memory pool to store \p count atoms in
176 void reserve(gmx_ana_index_t
*g
, int count
)
178 GMX_RELEASE_ASSERT(g_
== NULL
, "Can only reserve one element with one instance");
179 _gmx_sel_mempool_alloc_group(mp_
, g
, count
);
184 gmx_sel_mempool_t
*mp_
;
189 * Assigns a temporary value for a selection element.
191 * This class implements RAII semantics for temporarily assigning the value
192 * pointer of a selection element to point to a different location.
194 * \ingroup module_selection
196 class SelelemTemporaryValueAssigner
199 //! Constructs an assigner without an initial assignment.
200 SelelemTemporaryValueAssigner()
201 : old_ptr_(NULL
), old_nalloc_(0)
205 * Constructs an assigner with an initial assignment.
207 * \param[in,out] sel Selection element for which to assign.
208 * \param[in] vsource Element to which \p sel values will point to.
212 SelelemTemporaryValueAssigner(const SelectionTreeElementPointer
&sel
,
213 const SelectionTreeElement
&vsource
)
215 assign(sel
, vsource
);
217 //! Undoes any temporary assignment done using this assigner.
218 ~SelelemTemporaryValueAssigner()
222 _gmx_selvalue_setstore_alloc(&sel_
->v
, old_ptr_
, old_nalloc_
);
227 * Assigns a temporary value pointer.
229 * \param[in,out] sel Selection element for which to assign.
230 * \param[in] vsource Element to which \p sel values will point to.
232 * Assigns the value pointer in \p sel to point to the values in
233 * \p vsource, i.e., any access/modification to values in \p sel
234 * actually accesses values in \p vsource.
236 void assign(const SelectionTreeElementPointer
&sel
,
237 const SelectionTreeElement
&vsource
)
239 GMX_RELEASE_ASSERT(!sel_
,
240 "Can only assign one element with one instance");
241 GMX_RELEASE_ASSERT(sel
->v
.type
== vsource
.v
.type
,
242 "Mismatching selection value types");
243 _gmx_selvalue_getstore_and_release(&sel
->v
, &old_ptr_
, &old_nalloc_
);
244 _gmx_selvalue_setstore(&sel
->v
, vsource
.v
.u
.ptr
);
249 SelectionTreeElementPointer sel_
;
255 * Expands a value array from one-per-position to one-per-atom.
257 * \param[in,out] value Array to expand.
258 * \param[in,out] nr Number of values in \p value.
259 * \param[in] pos Position data.
260 * \tparam T Value type of the array to expand.
262 * On input, \p value contains one value for each position in \p pos (and `*nr`
263 * must match). On output, \p value will contain a value for each atom used to
264 * evaluate `pos`: each input value is replicated to all atoms that make up the
265 * corresponding position.
266 * The operation is done in-place.
270 template <typename T
>
271 void expandValueForPositions(T value
[], int *nr
, gmx_ana_pos_t
*pos
)
273 GMX_RELEASE_ASSERT(*nr
== pos
->count(),
274 "Position update method did not return the correct number of values");
275 *nr
= pos
->m
.mapb
.nra
;
276 // Loop over the positions in reverse order so that the expansion can be
277 // done in-place (assumes that each position has at least one atom, which
278 // should always be the case).
279 int outputIndex
= pos
->m
.mapb
.nra
;
280 for (int i
= pos
->count() - 1; i
>= 0; --i
)
282 const int atomCount
= pos
->m
.mapb
.index
[i
+ 1] - pos
->m
.mapb
.index
[i
];
283 outputIndex
-= atomCount
;
284 GMX_ASSERT(outputIndex
>= i
,
285 "In-place algorithm would overwrite data not yet used");
286 std::fill(&value
[outputIndex
], &value
[outputIndex
+ atomCount
], value
[i
]);
293 * \param[in] fp File handle to receive the output.
294 * \param[in] evalfunc Function pointer to print.
297 _gmx_sel_print_evalfunc_name(FILE *fp
, gmx::sel_evalfunc evalfunc
)
303 else if (evalfunc
== &_gmx_sel_evaluate_root
)
307 else if (evalfunc
== &_gmx_sel_evaluate_static
)
309 fprintf(fp
, "static");
311 else if (evalfunc
== &_gmx_sel_evaluate_subexpr_simple
)
313 fprintf(fp
, "subexpr_simple");
315 else if (evalfunc
== &_gmx_sel_evaluate_subexpr_staticeval
)
317 fprintf(fp
, "subexpr_staticeval");
319 else if (evalfunc
== &_gmx_sel_evaluate_subexpr
)
321 fprintf(fp
, "subexpr");
323 else if (evalfunc
== &_gmx_sel_evaluate_subexprref_simple
)
325 fprintf(fp
, "ref_simple");
327 else if (evalfunc
== &_gmx_sel_evaluate_subexprref
)
331 else if (evalfunc
== &_gmx_sel_evaluate_method
)
333 fprintf(fp
, "method");
335 else if (evalfunc
== &_gmx_sel_evaluate_modifier
)
339 else if (evalfunc
== &_gmx_sel_evaluate_not
)
343 else if (evalfunc
== &_gmx_sel_evaluate_and
)
347 else if (evalfunc
== &_gmx_sel_evaluate_or
)
351 else if (evalfunc
== &_gmx_sel_evaluate_arithmetic
)
353 fprintf(fp
, "arithmetic");
357 fprintf(fp
, "%p", (void*)(evalfunc
));
362 * \param[out] data Evaluation data structure to initialize.
363 * \param[in] mp Memory pool for intermediate evaluation values.
364 * \param[in] gall Index group with all the atoms.
365 * \param[in] top Topology structure for evaluation.
366 * \param[in] fr New frame for evaluation.
367 * \param[in] pbc New PBC information for evaluation.
370 _gmx_sel_evaluate_init(gmx_sel_evaluate_t
*data
,
371 gmx_sel_mempool_t
*mp
, gmx_ana_index_t
*gall
,
372 const gmx_mtop_t
*top
, t_trxframe
*fr
, t_pbc
*pbc
)
382 * Recursively initializes the flags for evaluation.
384 * \param[in,out] sel Selection element to clear.
386 * The \ref SEL_INITFRAME flag is set for \ref SEL_EXPRESSION elements whose
387 * method defines the \p init_frame callback (see sel_framefunc()), and
388 * cleared for other elements.
390 * The \ref SEL_EVALFRAME flag is cleared for all elements.
393 init_frame_eval(SelectionTreeElementPointer sel
)
397 sel
->flags
&= ~(SEL_INITFRAME
| SEL_EVALFRAME
);
398 if (sel
->type
== SEL_EXPRESSION
)
400 if (sel
->u
.expr
.method
&& sel
->u
.expr
.method
->init_frame
)
402 sel
->flags
|= SEL_INITFRAME
;
405 if (sel
->child
&& sel
->type
!= SEL_SUBEXPRREF
)
407 init_frame_eval(sel
->child
);
416 SelectionEvaluator::SelectionEvaluator()
421 * \param[in,out] coll The selection collection to evaluate.
422 * \param[in] fr Frame for which the evaluation should be carried out.
423 * \param[in] pbc PBC data, or NULL if no PBC should be used.
424 * \returns 0 on successful evaluation, a non-zero error code on error.
426 * This functions sets the global variables for topology, frame and PBC,
427 * clears some information in the selection to initialize the evaluation
428 * for a new frame, and evaluates \p sel and all the selections pointed by
429 * the \p next pointers of \p sel.
431 * This is the only function that user code should call if they want to
432 * evaluate a selection for a new frame.
435 SelectionEvaluator::evaluate(SelectionCollection
*coll
,
436 t_trxframe
*fr
, t_pbc
*pbc
)
438 gmx_ana_selcollection_t
*sc
= &coll
->impl_
->sc_
;
439 gmx_sel_evaluate_t data
;
441 _gmx_sel_evaluate_init(&data
, sc
->mempool
, &sc
->gall
, sc
->top
, fr
, pbc
);
442 init_frame_eval(sc
->root
);
443 SelectionTreeElementPointer sel
= sc
->root
;
446 /* Clear the evaluation group of subexpressions */
447 if (sel
->child
&& sel
->child
->type
== SEL_SUBEXPR
448 && sel
->child
->evaluate
!= NULL
)
450 sel
->child
->u
.cgrp
.isize
= 0;
451 /* Not strictly necessary, because the value will be overwritten
452 * during first evaluation of the subexpression anyways, but we
453 * clear the group for clarity. Note that this is _not_ done during
454 * compilation because of some additional complexities involved
455 * (see compiler.cpp), so it should not be relied upon in
456 * _gmx_sel_evaluate_subexpr(). */
457 if (sel
->child
->v
.type
== GROUP_VALUE
)
459 sel
->child
->v
.u
.g
->isize
= 0;
464 sel
->evaluate(&data
, sel
, NULL
);
468 /* Update selection information */
469 SelectionDataList::const_iterator isel
;
470 for (isel
= sc
->sel
.begin(); isel
!= sc
->sel
.end(); ++isel
)
472 internal::SelectionData
&sel
= **isel
;
473 sel
.refreshMassesAndCharges(sc
->top
);
474 sel
.updateCoveredFractionForFrame();
479 * \param[in,out] coll The selection collection to evaluate.
480 * \param[in] nframes Total number of frames.
483 SelectionEvaluator::evaluateFinal(SelectionCollection
*coll
, int nframes
)
485 gmx_ana_selcollection_t
*sc
= &coll
->impl_
->sc_
;
487 SelectionDataList::const_iterator isel
;
488 for (isel
= sc
->sel
.begin(); isel
!= sc
->sel
.end(); ++isel
)
490 internal::SelectionData
&sel
= **isel
;
491 sel
.restoreOriginalPositions(sc
->top
);
492 sel
.computeAverageCoveredFraction(nframes
);
499 * \param[in] data Data for the current frame.
500 * \param[in] sel Selection element being evaluated.
501 * \param[in] g Group for which \p sel should be evaluated.
502 * \returns 0 on success, a non-zero error code on error.
504 * Evaluates each child of \p sel in \p g.
507 _gmx_sel_evaluate_children(gmx_sel_evaluate_t
*data
,
508 const gmx::SelectionTreeElementPointer
&sel
,
511 SelectionTreeElementPointer child
= sel
->child
;
516 child
->evaluate(data
, child
, g
);
523 _gmx_sel_evaluate_root(gmx_sel_evaluate_t
*data
,
524 const gmx::SelectionTreeElementPointer
&sel
,
525 gmx_ana_index_t
* /* g */)
527 if (sel
->u
.cgrp
.isize
== 0 || !sel
->child
->evaluate
)
532 sel
->child
->evaluate(data
, sel
->child
,
533 sel
->u
.cgrp
.isize
< 0 ? NULL
: &sel
->u
.cgrp
);
537 _gmx_sel_evaluate_static(gmx_sel_evaluate_t
* /* data */,
538 const gmx::SelectionTreeElementPointer
&sel
,
541 if (sel
->flags
& SEL_UNSORTED
)
543 // This only works if g contains all the atoms, but that is currently
544 // the only supported case.
545 gmx_ana_index_copy(sel
->v
.u
.g
, &sel
->u
.cgrp
, false);
549 gmx_ana_index_intersection(sel
->v
.u
.g
, &sel
->u
.cgrp
, g
);
554 /*********************************************************************
555 * SUBEXPRESSION EVALUATION
556 *********************************************************************/
559 * \param[in] data Data for the current frame.
560 * \param[in] sel Selection element being evaluated.
561 * \param[in] g Group for which \p sel should be evaluated.
562 * \returns 0 on success, a non-zero error code on error.
564 * Evaluates the child element (there should be exactly one) in \p g.
565 * The compiler has taken care that the child actually stores the evaluated
566 * value in the value pointer of this element.
568 * This function is used as gmx::SelectionTreeElement::evaluate for
569 * \ref SEL_SUBEXPR elements that are used only once, and hence do not need
570 * full subexpression handling.
573 _gmx_sel_evaluate_subexpr_simple(gmx_sel_evaluate_t
*data
,
574 const gmx::SelectionTreeElementPointer
&sel
,
577 if (sel
->child
->evaluate
)
579 sel
->child
->evaluate(data
, sel
->child
, g
);
581 sel
->v
.nr
= sel
->child
->v
.nr
;
585 * \param[in] data Data for the current frame.
586 * \param[in] sel Selection element being evaluated.
587 * \param[in] g Group for which \p sel should be evaluated.
588 * \returns 0 on success, a non-zero error code on error.
590 * If this is the first call for this frame, evaluates the child element
591 * there should be exactly one in \p g.
592 * The compiler has taken care that the child actually stores the evaluated
593 * value in the value pointer of this element.
594 * Assumes that \p g is persistent for the duration of the whole evaluation.
596 * This function is used as gmx::SelectionTreeElement::evaluate for
597 * \ref SEL_SUBEXPR elements that have a static evaluation group, and hence do
598 * not need full subexpression handling.
601 _gmx_sel_evaluate_subexpr_staticeval(gmx_sel_evaluate_t
*data
,
602 const gmx::SelectionTreeElementPointer
&sel
,
605 if (sel
->u
.cgrp
.isize
== 0)
607 sel
->child
->evaluate(data
, sel
->child
, g
);
608 sel
->v
.nr
= sel
->child
->v
.nr
;
611 sel
->u
.cgrp
.isize
= -1;
615 gmx_ana_index_set(&sel
->u
.cgrp
, g
->isize
, g
->index
, 0);
621 * \param[in] data Data for the current frame.
622 * \param[in] sel Selection element being evaluated.
623 * \param[in] g Group for which \p sel should be evaluated.
624 * \returns 0 on success, a non-zero error code on error.
626 * Finds the part of \p g for which the subexpression
627 * has not yet been evaluated by comparing \p g to \p sel->u.cgrp.
628 * If the part is not empty, the child expression is evaluated for this
629 * part, and the results merged to the old values of the child.
630 * The value of \p sel itself is undefined after the call.
633 * The call to gmx_ana_index_difference() can take quite a lot of unnecessary
634 * time if the subexpression is evaluated either several times for the same
635 * group or for completely distinct groups.
636 * However, in the majority of cases, these situations occur when
637 * _gmx_sel_evaluate_subexpr_staticeval() can be used, so this should not be a
641 _gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t
*data
,
642 const gmx::SelectionTreeElementPointer
&sel
,
645 gmx_ana_index_t gmiss
;
647 MempoolGroupReserver
gmissreserver(data
->mp
);
648 if (sel
->u
.cgrp
.isize
== 0)
651 SelelemTemporaryValueAssigner
assigner(sel
->child
, *sel
);
652 sel
->child
->evaluate(data
, sel
->child
, g
);
654 gmx_ana_index_copy(&sel
->u
.cgrp
, g
, false);
659 gmissreserver
.reserve(&gmiss
, g
->isize
);
660 gmx_ana_index_difference(&gmiss
, g
, &sel
->u
.cgrp
);
664 MempoolSelelemReserver
reserver(sel
->child
, gmiss
.isize
);
665 /* Evaluate the missing values for the child */
666 sel
->child
->evaluate(data
, sel
->child
, &gmiss
);
667 /* Merge the missing values to the existing ones. */
668 if (sel
->v
.type
== GROUP_VALUE
)
670 gmx_ana_index_merge(sel
->v
.u
.g
, sel
->child
->v
.u
.g
, sel
->v
.u
.g
);
676 i
= sel
->u
.cgrp
.isize
- 1;
678 /* TODO: This switch is kind of ugly, but it may be difficult to
679 * do this portably without C++ templates. */
683 for (k
= sel
->u
.cgrp
.isize
+ gmiss
.isize
- 1; k
>= 0; k
--)
685 if (i
< 0 || (j
>= 0 && sel
->u
.cgrp
.index
[i
] < gmiss
.index
[j
]))
687 sel
->v
.u
.i
[k
] = sel
->child
->v
.u
.i
[j
--];
691 sel
->v
.u
.i
[k
] = sel
->v
.u
.i
[i
--];
697 for (k
= sel
->u
.cgrp
.isize
+ gmiss
.isize
- 1; k
>= 0; k
--)
699 if (i
< 0 || (j
>= 0 && sel
->u
.cgrp
.index
[i
] < gmiss
.index
[j
]))
701 sel
->v
.u
.r
[k
] = sel
->child
->v
.u
.r
[j
--];
705 sel
->v
.u
.r
[k
] = sel
->v
.u
.r
[i
--];
711 // Note: with the currently allowed syntax, this case is never
713 for (k
= sel
->u
.cgrp
.isize
+ gmiss
.isize
- 1; k
>= 0; k
--)
715 if (i
< 0 || (j
>= 0 && sel
->u
.cgrp
.index
[i
] < gmiss
.index
[j
]))
717 sel
->v
.u
.s
[k
] = sel
->child
->v
.u
.s
[j
--];
721 sel
->v
.u
.s
[k
] = sel
->v
.u
.s
[i
--];
727 /* TODO: Implement this */
728 GMX_THROW(gmx::NotImplementedError("position subexpressions not implemented properly"));
732 GMX_THROW(gmx::InternalError("Invalid subexpression type"));
735 gmx_ana_index_merge(&sel
->u
.cgrp
, &sel
->u
.cgrp
, &gmiss
);
740 * \param[in] data Data for the current frame.
741 * \param[in] sel Selection element being evaluated.
742 * \param[in] g Group for which \p sel should be evaluated.
743 * \returns 0 for success.
745 * Sets the value pointers of the child and its child to point to the same
746 * memory as the value pointer of this element to avoid copying, and then
747 * evaluates evaluates the child.
749 * This function is used as gmx::SelectionTreeElement:evaluate for
750 * \ref SEL_SUBEXPRREF elements for which the \ref SEL_SUBEXPR does not have
754 _gmx_sel_evaluate_subexprref_simple(gmx_sel_evaluate_t
*data
,
755 const gmx::SelectionTreeElementPointer
&sel
,
760 _gmx_selvalue_setstore(&sel
->child
->v
, sel
->v
.u
.ptr
);
761 _gmx_selvalue_setstore_alloc(&sel
->child
->child
->v
, sel
->v
.u
.ptr
,
762 sel
->child
->child
->v
.nalloc
);
763 sel
->child
->evaluate(data
, sel
->child
, g
);
765 sel
->v
.nr
= sel
->child
->v
.nr
;
768 sel
->u
.param
->val
.nr
= sel
->v
.nr
;
769 if (sel
->u
.param
->nvalptr
)
771 *sel
->u
.param
->nvalptr
= sel
->u
.param
->val
.nr
;
777 * \param[in] data Data for the current frame.
778 * \param[in] sel Selection element being evaluated.
779 * \param[in] g Group for which \p sel should be evaluated.
780 * \returns 0 on success, a non-zero error code on error.
782 * If the value type is \ref POS_VALUE, the value of the child is simply
783 * copied to set the value of \p sel (the child subexpression should
784 * already have been evaluated by its root).
785 * If the value type is something else, the child is evaluated for the
786 * group \p g, and the value of the child is then copied.
787 * There should be only one child element.
789 * This function is used as gmx::SelectionTreeElement::evaluate for
790 * \ref SEL_SUBEXPRREF elements.
793 _gmx_sel_evaluate_subexprref(gmx_sel_evaluate_t
*data
,
794 const gmx::SelectionTreeElementPointer
&sel
,
799 if (g
!= NULL
&& sel
->child
->evaluate
!= NULL
)
801 sel
->child
->evaluate(data
, sel
->child
, g
);
803 const SelectionTreeElementPointer
&expr
= sel
->child
;
809 sel
->v
.nr
= expr
->v
.nr
;
810 memcpy(sel
->v
.u
.i
, expr
->v
.u
.i
, sel
->v
.nr
*sizeof(*sel
->v
.u
.i
));
814 sel
->v
.nr
= g
->isize
;
815 /* Extract the values corresponding to g */
816 for (i
= j
= 0; i
< g
->isize
; ++i
, ++j
)
818 while (sel
->child
->u
.cgrp
.index
[j
] < g
->index
[i
])
822 sel
->v
.u
.i
[i
] = expr
->v
.u
.i
[j
];
830 sel
->v
.nr
= expr
->v
.nr
;
831 memcpy(sel
->v
.u
.r
, expr
->v
.u
.r
, sel
->v
.nr
*sizeof(*sel
->v
.u
.r
));
835 sel
->v
.nr
= g
->isize
;
836 /* Extract the values corresponding to g */
837 for (i
= j
= 0; i
< g
->isize
; ++i
, ++j
)
839 while (sel
->child
->u
.cgrp
.index
[j
] < g
->index
[i
])
843 sel
->v
.u
.r
[i
] = expr
->v
.u
.r
[j
];
851 sel
->v
.nr
= expr
->v
.nr
;
852 memcpy(sel
->v
.u
.s
, expr
->v
.u
.s
, sel
->v
.nr
*sizeof(*sel
->v
.u
.s
));
856 sel
->v
.nr
= g
->isize
;
857 /* Extract the values corresponding to g */
858 for (i
= j
= 0; i
< g
->isize
; ++i
, ++j
)
860 while (sel
->child
->u
.cgrp
.index
[j
] < g
->index
[i
])
864 sel
->v
.u
.s
[i
] = expr
->v
.u
.s
[j
];
870 /* Currently, there is no need to do anything fancy here,
871 * but some future extensions may need a more flexible
873 gmx_ana_pos_copy(sel
->v
.u
.p
, expr
->v
.u
.p
, false);
879 gmx_ana_index_copy(sel
->v
.u
.g
, expr
->v
.u
.g
, false);
883 gmx_ana_index_intersection(sel
->v
.u
.g
, expr
->v
.u
.g
, g
);
887 default: /* should not be reached */
888 GMX_THROW(gmx::InternalError("Invalid subexpression reference type"));
890 /* Store the number of values if needed */
893 sel
->u
.param
->val
.nr
= sel
->v
.nr
;
894 if (sel
->u
.param
->nvalptr
)
896 *sel
->u
.param
->nvalptr
= sel
->u
.param
->val
.nr
;
901 /********************************************************************
902 * METHOD EXPRESSION EVALUATION
903 ********************************************************************/
906 * \param[in] data Data for the current frame.
907 * \param[in] sel Selection element being evaluated.
908 * \param[in] g Group for which \p sel should be evaluated.
909 * \returns 0 on success, a non-zero error code on error.
911 * Evaluates each child of a \ref SEL_EXPRESSION element.
912 * The value of \p sel is not touched.
914 * This function is not used as gmx::SelectionTreeElement::evaluate,
915 * but is used internally.
918 _gmx_sel_evaluate_method_params(gmx_sel_evaluate_t
*data
,
919 const gmx::SelectionTreeElementPointer
&sel
,
922 SelectionTreeElementPointer child
= sel
->child
;
925 if (child
->evaluate
&& !(child
->flags
& SEL_EVALFRAME
))
927 if (child
->flags
& SEL_ATOMVAL
)
929 child
->evaluate(data
, child
, g
);
933 child
->flags
|= SEL_EVALFRAME
;
934 child
->evaluate(data
, child
, NULL
);
942 * \param[in] data Data for the current frame.
943 * \param[in] sel Selection element being evaluated.
944 * \param[in] g Group for which \p sel should be evaluated.
945 * \returns 0 on success, a non-zero error code on error.
947 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
948 * to evaluate any parameter values.
949 * If this is the first time this expression is evaluated for
950 * the frame, sel_framefunc() callback is called if one is provided.
951 * If a reference position calculation has been initialized for this element,
952 * the positions are also updated, and sel_updatefunc_pos() is used to
953 * evaluate the value. Otherwise, sel_updatefunc() is used.
955 * This function is used as gmx::SelectionTreeElement::evaluate for
956 * \ref SEL_EXPRESSION elements.
959 _gmx_sel_evaluate_method(gmx_sel_evaluate_t
*data
,
960 const gmx::SelectionTreeElementPointer
&sel
,
963 _gmx_sel_evaluate_method_params(data
, sel
, g
);
964 gmx::SelMethodEvalContext
context(data
->top
, data
->fr
, data
->pbc
);
965 if (sel
->flags
& SEL_INITFRAME
)
967 sel
->flags
&= ~SEL_INITFRAME
;
968 sel
->u
.expr
.method
->init_frame(context
, sel
->u
.expr
.mdata
);
972 gmx_ana_poscalc_update(sel
->u
.expr
.pc
, sel
->u
.expr
.pos
, g
,
973 data
->fr
, data
->pbc
);
974 sel
->u
.expr
.method
->pupdate(context
, sel
->u
.expr
.pos
, &sel
->v
,
976 if ((sel
->flags
& SEL_ATOMVAL
) && sel
->v
.nr
< g
->isize
)
981 expandValueForPositions(sel
->v
.u
.r
, &sel
->v
.nr
,
985 GMX_RELEASE_ASSERT(false, "Unimplemented value type for position update method");
991 sel
->u
.expr
.method
->update(context
, g
, &sel
->v
, sel
->u
.expr
.mdata
);
996 * \param[in] data Data for the current frame.
997 * \param[in] sel Selection element being evaluated.
998 * \param[in] g Group for which \p sel should be evaluated.
999 * \returns 0 on success, a non-zero error code on error.
1001 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
1002 * to evaluate any parameter values.
1003 * If this is the first time this expression is evaluated for
1004 * the frame, sel_framefunc() callback is called if one is provided.
1005 * The modifier is then evaluated using sel_updatefunc_pos().
1007 * This function is used as gmx::SelectionTreeElement::evaluate for
1008 * \ref SEL_MODIFIER elements.
1011 _gmx_sel_evaluate_modifier(gmx_sel_evaluate_t
*data
,
1012 const gmx::SelectionTreeElementPointer
&sel
,
1015 _gmx_sel_evaluate_method_params(data
, sel
, g
);
1016 gmx::SelMethodEvalContext
context(data
->top
, data
->fr
, data
->pbc
);
1017 if (sel
->flags
& SEL_INITFRAME
)
1019 sel
->flags
&= ~SEL_INITFRAME
;
1020 sel
->u
.expr
.method
->init_frame(context
, sel
->u
.expr
.mdata
);
1022 if (sel
->child
&& sel
->child
->v
.type
!= POS_VALUE
)
1024 GMX_THROW(gmx::NotImplementedError("Non-position valued modifiers not implemented"));
1026 sel
->u
.expr
.method
->pupdate(context
, NULL
, &sel
->v
, sel
->u
.expr
.mdata
);
1030 /********************************************************************
1031 * BOOLEAN EXPRESSION EVALUATION
1032 ********************************************************************/
1035 * \param[in] data Data for the current frame.
1036 * \param[in] sel Selection element being evaluated.
1037 * \param[in] g Group for which \p sel should be evaluated.
1038 * \returns 0 on success, a non-zero error code on error.
1040 * Evaluates the child element (there should be only one) in the group
1041 * \p g, and then sets the value of \p sel to the complement of the
1044 * This function is used as gmx::SelectionTreeElement::evaluate for
1045 * \ref SEL_BOOLEAN elements with \ref BOOL_NOT.
1048 _gmx_sel_evaluate_not(gmx_sel_evaluate_t
*data
,
1049 const gmx::SelectionTreeElementPointer
&sel
,
1052 MempoolSelelemReserver
reserver(sel
->child
, g
->isize
);
1053 sel
->child
->evaluate(data
, sel
->child
, g
);
1054 gmx_ana_index_difference(sel
->v
.u
.g
, g
, sel
->child
->v
.u
.g
);
1058 * \param[in] data Data for the current frame.
1059 * \param[in] sel Selection element being evaluated.
1060 * \param[in] g Group for which \p sel should be evaluated.
1061 * \returns 0 on success, a non-zero error code on error.
1063 * Short-circuiting evaluation of logical AND expressions.
1065 * Starts by evaluating the first child element in the group \p g.
1066 * The each following child element is evaluated in the intersection
1067 * of all the previous values until all children have been evaluated
1068 * or the intersection becomes empty.
1069 * The value of \p sel is set to the intersection of all the (evaluated)
1072 * If the first child does not have an evaluation function, it is skipped
1073 * and the evaluation is started at the second child.
1074 * This happens if the first child is a constant expression and during
1075 * compilation it was detected that the evaluation group is always a subset
1076 * of the constant group
1077 * (currently, the compiler never detects this).
1079 * This function is used as gmx::SelectionTreeElement::evaluate for
1080 * \ref SEL_BOOLEAN elements with \ref BOOL_AND.
1083 _gmx_sel_evaluate_and(gmx_sel_evaluate_t
*data
,
1084 const gmx::SelectionTreeElementPointer
&sel
,
1087 SelectionTreeElementPointer child
= sel
->child
;
1088 /* Skip the first child if it does not have an evaluation function. */
1089 if (!child
->evaluate
)
1091 child
= child
->next
;
1093 /* Evaluate the first child */
1095 MempoolSelelemReserver
reserver(child
, g
->isize
);
1096 child
->evaluate(data
, child
, g
);
1097 gmx_ana_index_copy(sel
->v
.u
.g
, child
->v
.u
.g
, false);
1099 child
= child
->next
;
1100 while (child
&& sel
->v
.u
.g
->isize
> 0)
1102 MempoolSelelemReserver
reserver(child
, sel
->v
.u
.g
->isize
);
1103 child
->evaluate(data
, child
, sel
->v
.u
.g
);
1104 gmx_ana_index_intersection(sel
->v
.u
.g
, sel
->v
.u
.g
, child
->v
.u
.g
);
1105 child
= child
->next
;
1110 * \param[in] data Data for the current frame.
1111 * \param[in] sel Selection element being evaluated.
1112 * \param[in] g Group for which \p sel should be evaluated.
1113 * \returns 0 on success, a non-zero error code on error.
1115 * Short-circuiting evaluation of logical OR expressions.
1117 * Starts by evaluating the first child element in the group \p g.
1118 * For each subsequent child, finds the part of \p g that is not
1119 * included the value of any previous child, and evaluates the child
1120 * in that group until the last child is evaluated or all of \p g
1121 * is included in some child value.
1122 * The value of \p sel is set to the union of all the (evaluated)
1125 * If the first child does not have an evaluation function, its value is
1126 * used without evaluation.
1127 * This happens if the first child is a constant expression, the selection
1128 * has been compiled, and the evaluation group is the same for each frame.
1129 * In this case, the compiler has taken care of that the child value is a
1130 * subset of \p g, making it unnecessary to evaluate it.
1132 * This function is used as gmx::SelectionTreeElement::evaluate for
1133 * \ref SEL_BOOLEAN elements with \ref BOOL_OR.
1136 _gmx_sel_evaluate_or(gmx_sel_evaluate_t
*data
,
1137 const gmx::SelectionTreeElementPointer
&sel
,
1140 gmx_ana_index_t tmp
, tmp2
;
1142 SelectionTreeElementPointer child
= sel
->child
;
1143 if (child
->evaluate
)
1145 MempoolSelelemReserver
reserver(child
, g
->isize
);
1146 child
->evaluate(data
, child
, g
);
1147 gmx_ana_index_partition(sel
->v
.u
.g
, &tmp
, g
, child
->v
.u
.g
);
1151 gmx_ana_index_partition(sel
->v
.u
.g
, &tmp
, g
, child
->v
.u
.g
);
1153 child
= child
->next
;
1154 while (child
&& tmp
.isize
> 0)
1157 MempoolSelelemReserver
reserver(child
, tmp
.isize
);
1158 child
->evaluate(data
, child
, &tmp
);
1159 gmx_ana_index_partition(&tmp
, &tmp2
, &tmp
, child
->v
.u
.g
);
1161 sel
->v
.u
.g
->isize
+= tmp
.isize
;
1162 tmp
.isize
= tmp2
.isize
;
1163 tmp
.index
= tmp2
.index
;
1164 child
= child
->next
;
1166 gmx_ana_index_sort(sel
->v
.u
.g
);
1170 /********************************************************************
1171 * ARITHMETIC EVALUATION
1172 ********************************************************************/
1175 * \param[in] data Data for the current frame.
1176 * \param[in] sel Selection element being evaluated.
1177 * \param[in] g Group for which \p sel should be evaluated.
1178 * \returns 0 on success, a non-zero error code on error.
1181 _gmx_sel_evaluate_arithmetic(gmx_sel_evaluate_t
*data
,
1182 const gmx::SelectionTreeElementPointer
&sel
,
1186 real lval
, rval
= 0., val
= 0.;
1188 const SelectionTreeElementPointer
&left
= sel
->child
;
1189 const SelectionTreeElementPointer
&right
= left
->next
;
1191 SelelemTemporaryValueAssigner assigner
;
1192 MempoolSelelemReserver reserver
;
1195 assigner
.assign(left
, *sel
);
1198 reserver
.reserve(right
, g
->isize
);
1201 else if (right
&& right
->mempool
)
1203 assigner
.assign(right
, *sel
);
1205 _gmx_sel_evaluate_children(data
, sel
, g
);
1207 n
= (sel
->flags
& SEL_SINGLEVAL
) ? 1 : g
->isize
;
1210 bool bArithNeg
= (sel
->u
.arith
.type
== ARITH_NEG
);
1211 GMX_ASSERT(right
|| bArithNeg
,
1212 "Right operand cannot be null except for negations");
1213 for (i
= i1
= i2
= 0; i
< n
; ++i
)
1215 lval
= left
->v
.u
.r
[i1
];
1218 rval
= right
->v
.u
.r
[i2
];
1220 switch (sel
->u
.arith
.type
)
1222 case ARITH_PLUS
: val
= lval
+ rval
; break;
1223 case ARITH_MINUS
: val
= lval
- rval
; break;
1224 case ARITH_NEG
: val
= -lval
; break;
1225 case ARITH_MULT
: val
= lval
* rval
; break;
1226 case ARITH_DIV
: val
= lval
/ rval
; break;
1227 case ARITH_EXP
: val
= pow(lval
, rval
); break;
1229 sel
->v
.u
.r
[i
] = val
;
1230 if (!(left
->flags
& SEL_SINGLEVAL
))
1234 if (!bArithNeg
&& !(right
->flags
& SEL_SINGLEVAL
))