2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013 by the GROMACS development team.
5 * Copyright (c) 2014,2015,2016,2017,2018 by the GROMACS development team.
6 * Copyright (c) 2019,2020, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
39 * Implements functions in evaluate.h.
42 * One of the major bottlenecks for selection performance is that all the
43 * evaluation is carried out for atoms.
44 * There are several cases when the evaluation could be done for residues
45 * or molecules instead, including keywords that select by residue and
46 * cases where residue centers are used as reference positions.
47 * Implementing this would require a mechanism for recognizing whether
48 * something can be evaluated by residue/molecule instead by atom, and
49 * converting selections by residue/molecule into selections by atom
52 * \author Teemu Murtola <teemu.murtola@gmail.com>
53 * \ingroup module_selection
63 #include "gromacs/math/utilities.h"
64 #include "gromacs/math/vec.h"
65 #include "gromacs/selection/indexutil.h"
66 #include "gromacs/selection/selection.h"
67 #include "gromacs/utility/exceptions.h"
68 #include "gromacs/utility/gmxassert.h"
69 #include "gromacs/utility/smalloc.h"
73 #include "selectioncollection_impl.h"
75 #include "selmethod.h"
77 using gmx::SelectionTreeElement
;
78 using gmx::SelectionTreeElementPointer
;
84 * Reserves memory for a selection element from the evaluation memory pool.
86 * This class implements RAII semantics for allocating memory for selection
87 * element values from a selection evaluation memory pool.
89 * \ingroup module_selection
91 class MempoolSelelemReserver
94 //! Constructs a reserver without initial reservation.
95 MempoolSelelemReserver() {}
97 * Constructs a reserver with initial reservation.
99 * \param[in,out] sel Selection element for which to reserve.
100 * \param[in] count Number of values to reserve.
104 MempoolSelelemReserver(const SelectionTreeElementPointer
& sel
, int count
)
108 //! Frees any memory allocated using this reserver.
109 ~MempoolSelelemReserver()
113 sel_
->mempoolRelease();
118 * Reserves memory for selection element values using this reserver.
120 * \param[in,out] sel Selection element for which to reserve.
121 * \param[in] count Number of values to reserve.
123 * Allocates space to store \p count output values in \p sel from the
124 * memory pool associated with \p sel, or from the heap if there is no
125 * memory pool. Type of values to allocate is automatically determined
128 void reserve(const SelectionTreeElementPointer
& sel
, int count
)
130 GMX_RELEASE_ASSERT(!sel_
, "Can only reserve one element with one instance");
131 sel
->mempoolReserve(count
);
136 SelectionTreeElementPointer sel_
;
140 * Reserves memory for an index group from the evaluation memory pool.
142 * This class implements RAII semantics for allocating memory for an index
143 * group from a selection evaluation memory pool.
145 * \ingroup module_selection
147 class MempoolGroupReserver
151 * Creates a reserver associated with a given memory pool.
153 * \param mp Memory pool from which to reserve memory.
155 explicit MempoolGroupReserver(gmx_sel_mempool_t
* mp
) : mp_(mp
), g_(nullptr) {}
156 //! Frees any memory allocated using this reserver.
157 ~MempoolGroupReserver()
161 _gmx_sel_mempool_free_group(mp_
, g_
);
166 * Reserves memory for an index group using this reserver.
168 * \param[in,out] g Index group to reserve.
169 * \param[in] count Number of atoms to reserve space for.
171 * Allocates memory from the memory pool to store \p count atoms in
174 void reserve(gmx_ana_index_t
* g
, int count
)
176 GMX_RELEASE_ASSERT(g_
== nullptr, "Can only reserve one element with one instance");
177 _gmx_sel_mempool_alloc_group(mp_
, g
, count
);
182 gmx_sel_mempool_t
* mp_
;
187 * Assigns a temporary value for a selection element.
189 * This class implements RAII semantics for temporarily assigning the value
190 * pointer of a selection element to point to a different location.
192 * \ingroup module_selection
194 class SelelemTemporaryValueAssigner
197 //! Constructs an assigner without an initial assignment.
198 SelelemTemporaryValueAssigner() : old_ptr_(nullptr), old_nalloc_(0) {}
200 * Constructs an assigner with an initial assignment.
202 * \param[in,out] sel Selection element for which to assign.
203 * \param[in] vsource Element to which \p sel values will point to.
207 SelelemTemporaryValueAssigner(const SelectionTreeElementPointer
& sel
, const SelectionTreeElement
& vsource
)
209 assign(sel
, vsource
);
211 //! Undoes any temporary assignment done using this assigner.
212 ~SelelemTemporaryValueAssigner()
216 _gmx_selvalue_setstore_alloc(&sel_
->v
, old_ptr_
, old_nalloc_
);
221 * Assigns a temporary value pointer.
223 * \param[in,out] sel Selection element for which to assign.
224 * \param[in] vsource Element to which \p sel values will point to.
226 * Assigns the value pointer in \p sel to point to the values in
227 * \p vsource, i.e., any access/modification to values in \p sel
228 * actually accesses values in \p vsource.
230 void assign(const SelectionTreeElementPointer
& sel
, const SelectionTreeElement
& vsource
)
232 GMX_RELEASE_ASSERT(!sel_
, "Can only assign one element with one instance");
233 GMX_RELEASE_ASSERT(sel
->v
.type
== vsource
.v
.type
, "Mismatching selection value types");
234 _gmx_selvalue_getstore_and_release(&sel
->v
, &old_ptr_
, &old_nalloc_
);
235 _gmx_selvalue_setstore(&sel
->v
, vsource
.v
.u
.ptr
);
240 SelectionTreeElementPointer sel_
;
246 * Expands a value array from one-per-position to one-per-atom.
248 * \param[in,out] value Array to expand.
249 * \param[in,out] nr Number of values in \p value.
250 * \param[in] pos Position data.
251 * \tparam T Value type of the array to expand.
253 * On input, \p value contains one value for each position in \p pos (and `*nr`
254 * must match). On output, \p value will contain a value for each atom used to
255 * evaluate `pos`: each input value is replicated to all atoms that make up the
256 * corresponding position.
257 * The operation is done in-place.
262 void expandValueForPositions(T value
[], int* nr
, gmx_ana_pos_t
* pos
)
264 GMX_RELEASE_ASSERT(*nr
== pos
->count(),
265 "Position update method did not return the correct number of values");
266 *nr
= pos
->m
.mapb
.nra
;
267 // Loop over the positions in reverse order so that the expansion can be
268 // done in-place (assumes that each position has at least one atom, which
269 // should always be the case).
270 int outputIndex
= pos
->m
.mapb
.nra
;
271 for (int i
= pos
->count() - 1; i
>= 0; --i
)
273 const int atomCount
= pos
->m
.mapb
.index
[i
+ 1] - pos
->m
.mapb
.index
[i
];
274 outputIndex
-= atomCount
;
275 GMX_ASSERT(outputIndex
>= i
, "In-place algorithm would overwrite data not yet used");
276 std::fill(&value
[outputIndex
], &value
[outputIndex
+ atomCount
], value
[i
]);
283 * \param[in] fp File handle to receive the output.
284 * \param[in] evalfunc Function pointer to print.
286 void _gmx_sel_print_evalfunc_name(FILE* fp
, gmx::sel_evalfunc evalfunc
)
292 else if (evalfunc
== &_gmx_sel_evaluate_root
)
296 else if (evalfunc
== &_gmx_sel_evaluate_static
)
298 fprintf(fp
, "static");
300 else if (evalfunc
== &_gmx_sel_evaluate_subexpr_simple
)
302 fprintf(fp
, "subexpr_simple");
304 else if (evalfunc
== &_gmx_sel_evaluate_subexpr_staticeval
)
306 fprintf(fp
, "subexpr_staticeval");
308 else if (evalfunc
== &_gmx_sel_evaluate_subexpr
)
310 fprintf(fp
, "subexpr");
312 else if (evalfunc
== &_gmx_sel_evaluate_subexprref_simple
)
314 fprintf(fp
, "ref_simple");
316 else if (evalfunc
== &_gmx_sel_evaluate_subexprref
)
320 else if (evalfunc
== &_gmx_sel_evaluate_method
)
322 fprintf(fp
, "method");
324 else if (evalfunc
== &_gmx_sel_evaluate_modifier
)
328 else if (evalfunc
== &_gmx_sel_evaluate_not
)
332 else if (evalfunc
== &_gmx_sel_evaluate_and
)
336 else if (evalfunc
== &_gmx_sel_evaluate_or
)
340 else if (evalfunc
== &_gmx_sel_evaluate_arithmetic
)
342 fprintf(fp
, "arithmetic");
346 fprintf(fp
, "%p", reinterpret_cast<void*>(evalfunc
));
351 * \param[out] data Evaluation data structure to initialize.
352 * \param[in] mp Memory pool for intermediate evaluation values.
353 * \param[in] gall Index group with all the atoms.
354 * \param[in] top Topology structure for evaluation.
355 * \param[in] fr New frame for evaluation.
356 * \param[in] pbc New PBC information for evaluation.
358 void _gmx_sel_evaluate_init(gmx_sel_evaluate_t
* data
,
359 gmx_sel_mempool_t
* mp
,
360 gmx_ana_index_t
* gall
,
361 const gmx_mtop_t
* top
,
373 * Recursively initializes the flags for evaluation.
375 * \param[in,out] sel Selection element to clear.
377 * The \ref SEL_INITFRAME flag is set for \ref SEL_EXPRESSION elements whose
378 * method defines the \p init_frame callback (see sel_framefunc()), and
379 * cleared for other elements.
381 * The \ref SEL_EVALFRAME flag is cleared for all elements.
383 static void init_frame_eval(SelectionTreeElementPointer sel
)
387 sel
->flags
&= ~(SEL_INITFRAME
| SEL_EVALFRAME
);
388 if (sel
->type
== SEL_EXPRESSION
)
390 if (sel
->u
.expr
.method
&& sel
->u
.expr
.method
->init_frame
)
392 sel
->flags
|= SEL_INITFRAME
;
395 if (sel
->child
&& sel
->type
!= SEL_SUBEXPRREF
)
397 init_frame_eval(sel
->child
);
406 SelectionEvaluator::SelectionEvaluator() {}
409 * \param[in,out] coll The selection collection to evaluate.
410 * \param[in] fr Frame for which the evaluation should be carried out.
411 * \param[in] pbc PBC data, or NULL if no PBC should be used.
412 * \returns 0 on successful evaluation, a non-zero error code on error.
414 * This functions sets the global variables for topology, frame and PBC,
415 * clears some information in the selection to initialize the evaluation
416 * for a new frame, and evaluates \p sel and all the selections pointed by
417 * the \p next pointers of \p sel.
419 * This is the only function that user code should call if they want to
420 * evaluate a selection for a new frame.
422 // NOLINTNEXTLINE readability-convert-member-functions-to-static
423 void SelectionEvaluator::evaluate(SelectionCollection
* coll
, t_trxframe
* fr
, t_pbc
* pbc
)
425 gmx_ana_selcollection_t
* sc
= &coll
->impl_
->sc_
;
426 gmx_sel_evaluate_t data
;
428 _gmx_sel_evaluate_init(&data
, sc
->mempool
, &sc
->gall
, sc
->top
, fr
, pbc
);
429 init_frame_eval(sc
->root
);
430 SelectionTreeElementPointer sel
= sc
->root
;
433 /* Clear the evaluation group of subexpressions */
434 if (sel
->child
&& sel
->child
->type
== SEL_SUBEXPR
&& sel
->child
->evaluate
!= nullptr)
436 sel
->child
->u
.cgrp
.isize
= 0;
437 /* Not strictly necessary, because the value will be overwritten
438 * during first evaluation of the subexpression anyways, but we
439 * clear the group for clarity. Note that this is _not_ done during
440 * compilation because of some additional complexities involved
441 * (see compiler.cpp), so it should not be relied upon in
442 * _gmx_sel_evaluate_subexpr(). */
443 if (sel
->child
->v
.type
== GROUP_VALUE
)
445 sel
->child
->v
.u
.g
->isize
= 0;
450 sel
->evaluate(&data
, sel
, nullptr);
454 /* Update selection information */
455 SelectionDataList::const_iterator isel
;
456 for (isel
= sc
->sel
.begin(); isel
!= sc
->sel
.end(); ++isel
)
458 internal::SelectionData
& sel
= **isel
;
459 sel
.refreshMassesAndCharges(sc
->top
);
460 sel
.updateCoveredFractionForFrame();
465 * \param[in,out] coll The selection collection to evaluate.
466 * \param[in] nframes Total number of frames.
468 // NOLINTNEXTLINE readability-convert-member-functions-to-static
469 void SelectionEvaluator::evaluateFinal(SelectionCollection
* coll
, int nframes
)
471 gmx_ana_selcollection_t
* sc
= &coll
->impl_
->sc_
;
473 SelectionDataList::const_iterator isel
;
474 for (isel
= sc
->sel
.begin(); isel
!= sc
->sel
.end(); ++isel
)
476 internal::SelectionData
& sel
= **isel
;
477 sel
.restoreOriginalPositions(sc
->top
);
478 sel
.computeAverageCoveredFraction(nframes
);
485 * \param[in] data Data for the current frame.
486 * \param[in] sel Selection element being evaluated.
487 * \param[in] g Group for which \p sel should be evaluated.
488 * \returns 0 on success, a non-zero error code on error.
490 * Evaluates each child of \p sel in \p g.
492 void _gmx_sel_evaluate_children(gmx_sel_evaluate_t
* data
,
493 const gmx::SelectionTreeElementPointer
& sel
,
496 SelectionTreeElementPointer child
= sel
->child
;
501 child
->evaluate(data
, child
, g
);
507 void _gmx_sel_evaluate_root(gmx_sel_evaluate_t
* data
,
508 const gmx::SelectionTreeElementPointer
& sel
,
509 gmx_ana_index_t
* /* g */)
511 if (sel
->u
.cgrp
.isize
== 0 || !sel
->child
->evaluate
)
516 sel
->child
->evaluate(data
, sel
->child
, sel
->u
.cgrp
.isize
< 0 ? nullptr : &sel
->u
.cgrp
);
519 void _gmx_sel_evaluate_static(gmx_sel_evaluate_t
* /* data */,
520 const gmx::SelectionTreeElementPointer
& sel
,
523 if (sel
->flags
& SEL_UNSORTED
)
525 // This only works if g contains all the atoms, but that is currently
526 // the only supported case.
527 gmx_ana_index_copy(sel
->v
.u
.g
, &sel
->u
.cgrp
, false);
531 gmx_ana_index_intersection(sel
->v
.u
.g
, &sel
->u
.cgrp
, g
);
536 /*********************************************************************
537 * SUBEXPRESSION EVALUATION
538 *********************************************************************/
541 * \param[in] data Data for the current frame.
542 * \param[in] sel Selection element being evaluated.
543 * \param[in] g Group for which \p sel should be evaluated.
544 * \returns 0 on success, a non-zero error code on error.
546 * Evaluates the child element (there should be exactly one) in \p g.
547 * The compiler has taken care that the child actually stores the evaluated
548 * value in the value pointer of this element.
550 * This function is used as gmx::SelectionTreeElement::evaluate for
551 * \ref SEL_SUBEXPR elements that are used only once, and hence do not need
552 * full subexpression handling.
554 void _gmx_sel_evaluate_subexpr_simple(gmx_sel_evaluate_t
* data
,
555 const gmx::SelectionTreeElementPointer
& sel
,
558 if (sel
->child
->evaluate
)
560 sel
->child
->evaluate(data
, sel
->child
, g
);
562 sel
->v
.nr
= sel
->child
->v
.nr
;
566 * \param[in] data Data for the current frame.
567 * \param[in] sel Selection element being evaluated.
568 * \param[in] g Group for which \p sel should be evaluated.
569 * \returns 0 on success, a non-zero error code on error.
571 * If this is the first call for this frame, evaluates the child element
572 * there should be exactly one in \p g.
573 * The compiler has taken care that the child actually stores the evaluated
574 * value in the value pointer of this element.
575 * Assumes that \p g is persistent for the duration of the whole evaluation.
577 * This function is used as gmx::SelectionTreeElement::evaluate for
578 * \ref SEL_SUBEXPR elements that have a static evaluation group, and hence do
579 * not need full subexpression handling.
581 void _gmx_sel_evaluate_subexpr_staticeval(gmx_sel_evaluate_t
* data
,
582 const gmx::SelectionTreeElementPointer
& sel
,
585 if (sel
->u
.cgrp
.isize
== 0)
587 sel
->child
->evaluate(data
, sel
->child
, g
);
588 sel
->v
.nr
= sel
->child
->v
.nr
;
591 sel
->u
.cgrp
.isize
= -1;
595 gmx_ana_index_set(&sel
->u
.cgrp
, g
->isize
, g
->index
, 0);
601 * \param[in] data Data for the current frame.
602 * \param[in] sel Selection element being evaluated.
603 * \param[in] g Group for which \p sel should be evaluated.
604 * \returns 0 on success, a non-zero error code on error.
606 * Finds the part of \p g for which the subexpression
607 * has not yet been evaluated by comparing \p g to \p sel->u.cgrp.
608 * If the part is not empty, the child expression is evaluated for this
609 * part, and the results merged to the old values of the child.
610 * The value of \p sel itself is undefined after the call.
613 * The call to gmx_ana_index_difference() can take quite a lot of unnecessary
614 * time if the subexpression is evaluated either several times for the same
615 * group or for completely distinct groups.
616 * However, in the majority of cases, these situations occur when
617 * _gmx_sel_evaluate_subexpr_staticeval() can be used, so this should not be a
620 void _gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t
* data
,
621 const gmx::SelectionTreeElementPointer
& sel
,
624 gmx_ana_index_t gmiss
;
626 MempoolGroupReserver
gmissreserver(data
->mp
);
627 if (sel
->u
.cgrp
.isize
== 0)
630 SelelemTemporaryValueAssigner
assigner(sel
->child
, *sel
);
631 sel
->child
->evaluate(data
, sel
->child
, g
);
633 gmx_ana_index_copy(&sel
->u
.cgrp
, g
, false);
638 gmissreserver
.reserve(&gmiss
, g
->isize
);
639 gmx_ana_index_difference(&gmiss
, g
, &sel
->u
.cgrp
);
643 MempoolSelelemReserver
reserver(sel
->child
, gmiss
.isize
);
644 /* Evaluate the missing values for the child */
645 sel
->child
->evaluate(data
, sel
->child
, &gmiss
);
646 /* Merge the missing values to the existing ones. */
647 if (sel
->v
.type
== GROUP_VALUE
)
649 gmx_ana_index_merge(sel
->v
.u
.g
, sel
->child
->v
.u
.g
, sel
->v
.u
.g
);
655 i
= sel
->u
.cgrp
.isize
- 1;
657 /* TODO: This switch is kind of ugly, but it may be difficult to
658 * do this portably without C++ templates. */
662 for (k
= sel
->u
.cgrp
.isize
+ gmiss
.isize
- 1; k
>= 0; k
--)
664 if (i
< 0 || (j
>= 0 && sel
->u
.cgrp
.index
[i
] < gmiss
.index
[j
]))
666 sel
->v
.u
.i
[k
] = sel
->child
->v
.u
.i
[j
--];
670 sel
->v
.u
.i
[k
] = sel
->v
.u
.i
[i
--];
676 for (k
= sel
->u
.cgrp
.isize
+ gmiss
.isize
- 1; k
>= 0; k
--)
678 if (i
< 0 || (j
>= 0 && sel
->u
.cgrp
.index
[i
] < gmiss
.index
[j
]))
680 sel
->v
.u
.r
[k
] = sel
->child
->v
.u
.r
[j
--];
684 sel
->v
.u
.r
[k
] = sel
->v
.u
.r
[i
--];
690 // Note: with the currently allowed syntax, this case is never
692 for (k
= sel
->u
.cgrp
.isize
+ gmiss
.isize
- 1; k
>= 0; k
--)
694 if (i
< 0 || (j
>= 0 && sel
->u
.cgrp
.index
[i
] < gmiss
.index
[j
]))
696 sel
->v
.u
.s
[k
] = sel
->child
->v
.u
.s
[j
--];
700 sel
->v
.u
.s
[k
] = sel
->v
.u
.s
[i
--];
706 /* TODO: Implement this */
707 GMX_THROW(gmx::NotImplementedError(
708 "position subexpressions not implemented properly"));
711 case GROUP_VALUE
: GMX_THROW(gmx::InternalError("Invalid subexpression type"));
714 gmx_ana_index_merge(&sel
->u
.cgrp
, &sel
->u
.cgrp
, &gmiss
);
719 * \param[in] data Data for the current frame.
720 * \param[in] sel Selection element being evaluated.
721 * \param[in] g Group for which \p sel should be evaluated.
722 * \returns 0 for success.
724 * Sets the value pointers of the child and its child to point to the same
725 * memory as the value pointer of this element to avoid copying, and then
726 * evaluates evaluates the child.
728 * This function is used as gmx::SelectionTreeElement:evaluate for
729 * \ref SEL_SUBEXPRREF elements for which the \ref SEL_SUBEXPR does not have
732 void _gmx_sel_evaluate_subexprref_simple(gmx_sel_evaluate_t
* data
,
733 const gmx::SelectionTreeElementPointer
& sel
,
738 _gmx_selvalue_setstore(&sel
->child
->v
, sel
->v
.u
.ptr
);
739 _gmx_selvalue_setstore_alloc(&sel
->child
->child
->v
, sel
->v
.u
.ptr
, sel
->child
->child
->v
.nalloc
);
740 sel
->child
->evaluate(data
, sel
->child
, g
);
742 sel
->v
.nr
= sel
->child
->v
.nr
;
745 sel
->u
.param
->val
.nr
= sel
->v
.nr
;
746 if (sel
->u
.param
->nvalptr
)
748 *sel
->u
.param
->nvalptr
= sel
->u
.param
->val
.nr
;
754 * \param[in] data Data for the current frame.
755 * \param[in] sel Selection element being evaluated.
756 * \param[in] g Group for which \p sel should be evaluated.
757 * \returns 0 on success, a non-zero error code on error.
759 * If the value type is \ref POS_VALUE, the value of the child is simply
760 * copied to set the value of \p sel (the child subexpression should
761 * already have been evaluated by its root).
762 * If the value type is something else, the child is evaluated for the
763 * group \p g, and the value of the child is then copied.
764 * There should be only one child element.
766 * This function is used as gmx::SelectionTreeElement::evaluate for
767 * \ref SEL_SUBEXPRREF elements.
769 void _gmx_sel_evaluate_subexprref(gmx_sel_evaluate_t
* data
,
770 const gmx::SelectionTreeElementPointer
& sel
,
775 if (g
!= nullptr && sel
->child
->evaluate
!= nullptr)
777 sel
->child
->evaluate(data
, sel
->child
, g
);
779 const SelectionTreeElementPointer
& expr
= sel
->child
;
785 sel
->v
.nr
= expr
->v
.nr
;
786 memcpy(sel
->v
.u
.i
, expr
->v
.u
.i
, sel
->v
.nr
* sizeof(*sel
->v
.u
.i
));
790 sel
->v
.nr
= g
->isize
;
791 /* Extract the values corresponding to g */
792 for (i
= j
= 0; i
< g
->isize
; ++i
, ++j
)
794 while (sel
->child
->u
.cgrp
.index
[j
] < g
->index
[i
])
798 sel
->v
.u
.i
[i
] = expr
->v
.u
.i
[j
];
806 sel
->v
.nr
= expr
->v
.nr
;
807 memcpy(sel
->v
.u
.r
, expr
->v
.u
.r
, sel
->v
.nr
* sizeof(*sel
->v
.u
.r
));
811 sel
->v
.nr
= g
->isize
;
812 /* Extract the values corresponding to g */
813 for (i
= j
= 0; i
< g
->isize
; ++i
, ++j
)
815 while (sel
->child
->u
.cgrp
.index
[j
] < g
->index
[i
])
819 sel
->v
.u
.r
[i
] = expr
->v
.u
.r
[j
];
827 sel
->v
.nr
= expr
->v
.nr
;
828 memcpy(sel
->v
.u
.s
, expr
->v
.u
.s
, sel
->v
.nr
* sizeof(*sel
->v
.u
.s
));
832 sel
->v
.nr
= g
->isize
;
833 /* Extract the values corresponding to g */
834 for (i
= j
= 0; i
< g
->isize
; ++i
, ++j
)
836 while (sel
->child
->u
.cgrp
.index
[j
] < g
->index
[i
])
840 sel
->v
.u
.s
[i
] = expr
->v
.u
.s
[j
];
846 /* Currently, there is no need to do anything fancy here,
847 * but some future extensions may need a more flexible
849 gmx_ana_pos_copy(sel
->v
.u
.p
, expr
->v
.u
.p
, false);
855 gmx_ana_index_copy(sel
->v
.u
.g
, expr
->v
.u
.g
, false);
859 gmx_ana_index_intersection(sel
->v
.u
.g
, expr
->v
.u
.g
, g
);
863 default: /* should not be reached */
864 GMX_THROW(gmx::InternalError("Invalid subexpression reference type"));
866 /* Store the number of values if needed */
869 sel
->u
.param
->val
.nr
= sel
->v
.nr
;
870 if (sel
->u
.param
->nvalptr
)
872 *sel
->u
.param
->nvalptr
= sel
->u
.param
->val
.nr
;
877 /********************************************************************
878 * METHOD EXPRESSION EVALUATION
879 ********************************************************************/
882 * \param[in] data Data for the current frame.
883 * \param[in] sel Selection element being evaluated.
884 * \param[in] g Group for which \p sel should be evaluated.
885 * \returns 0 on success, a non-zero error code on error.
887 * Evaluates each child of a \ref SEL_EXPRESSION element.
888 * The value of \p sel is not touched.
890 * This function is not used as gmx::SelectionTreeElement::evaluate,
891 * but is used internally.
893 void _gmx_sel_evaluate_method_params(gmx_sel_evaluate_t
* data
,
894 const gmx::SelectionTreeElementPointer
& sel
,
897 SelectionTreeElementPointer child
= sel
->child
;
900 if (child
->evaluate
&& !(child
->flags
& SEL_EVALFRAME
))
902 if (child
->flags
& SEL_ATOMVAL
)
904 child
->evaluate(data
, child
, g
);
908 child
->flags
|= SEL_EVALFRAME
;
909 child
->evaluate(data
, child
, nullptr);
917 * \param[in] data Data for the current frame.
918 * \param[in] sel Selection element being evaluated.
919 * \param[in] g Group for which \p sel should be evaluated.
920 * \returns 0 on success, a non-zero error code on error.
922 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
923 * to evaluate any parameter values.
924 * If this is the first time this expression is evaluated for
925 * the frame, sel_framefunc() callback is called if one is provided.
926 * If a reference position calculation has been initialized for this element,
927 * the positions are also updated, and sel_updatefunc_pos() is used to
928 * evaluate the value. Otherwise, sel_updatefunc() is used.
930 * This function is used as gmx::SelectionTreeElement::evaluate for
931 * \ref SEL_EXPRESSION elements.
933 void _gmx_sel_evaluate_method(gmx_sel_evaluate_t
* data
,
934 const gmx::SelectionTreeElementPointer
& sel
,
937 _gmx_sel_evaluate_method_params(data
, sel
, g
);
938 gmx::SelMethodEvalContext
context(data
->top
, data
->fr
, data
->pbc
);
939 if (sel
->flags
& SEL_INITFRAME
)
941 sel
->flags
&= ~SEL_INITFRAME
;
942 sel
->u
.expr
.method
->init_frame(context
, sel
->u
.expr
.mdata
);
946 gmx_ana_poscalc_update(sel
->u
.expr
.pc
, sel
->u
.expr
.pos
, g
, data
->fr
, data
->pbc
);
947 sel
->u
.expr
.method
->pupdate(context
, sel
->u
.expr
.pos
, &sel
->v
, sel
->u
.expr
.mdata
);
948 if ((sel
->flags
& SEL_ATOMVAL
) && sel
->v
.nr
< g
->isize
)
953 expandValueForPositions(sel
->v
.u
.r
, &sel
->v
.nr
, sel
->u
.expr
.pos
);
956 GMX_RELEASE_ASSERT(false,
957 "Unimplemented value type for position update method");
963 sel
->u
.expr
.method
->update(context
, g
, &sel
->v
, sel
->u
.expr
.mdata
);
968 * \param[in] data Data for the current frame.
969 * \param[in] sel Selection element being evaluated.
970 * \param[in] g Group for which \p sel should be evaluated.
971 * \returns 0 on success, a non-zero error code on error.
973 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
974 * to evaluate any parameter values.
975 * If this is the first time this expression is evaluated for
976 * the frame, sel_framefunc() callback is called if one is provided.
977 * The modifier is then evaluated using sel_updatefunc_pos().
979 * This function is used as gmx::SelectionTreeElement::evaluate for
980 * \ref SEL_MODIFIER elements.
982 void _gmx_sel_evaluate_modifier(gmx_sel_evaluate_t
* data
,
983 const gmx::SelectionTreeElementPointer
& sel
,
986 _gmx_sel_evaluate_method_params(data
, sel
, g
);
987 gmx::SelMethodEvalContext
context(data
->top
, data
->fr
, data
->pbc
);
988 if (sel
->flags
& SEL_INITFRAME
)
990 sel
->flags
&= ~SEL_INITFRAME
;
991 sel
->u
.expr
.method
->init_frame(context
, sel
->u
.expr
.mdata
);
993 if (sel
->child
&& sel
->child
->v
.type
!= POS_VALUE
)
995 GMX_THROW(gmx::NotImplementedError("Non-position valued modifiers not implemented"));
997 sel
->u
.expr
.method
->pupdate(context
, nullptr, &sel
->v
, sel
->u
.expr
.mdata
);
1001 /********************************************************************
1002 * BOOLEAN EXPRESSION EVALUATION
1003 ********************************************************************/
1006 * \param[in] data Data for the current frame.
1007 * \param[in] sel Selection element being evaluated.
1008 * \param[in] g Group for which \p sel should be evaluated.
1009 * \returns 0 on success, a non-zero error code on error.
1011 * Evaluates the child element (there should be only one) in the group
1012 * \p g, and then sets the value of \p sel to the complement of the
1015 * This function is used as gmx::SelectionTreeElement::evaluate for
1016 * \ref SEL_BOOLEAN elements with \ref BOOL_NOT.
1018 void _gmx_sel_evaluate_not(gmx_sel_evaluate_t
* data
,
1019 const gmx::SelectionTreeElementPointer
& sel
,
1022 MempoolSelelemReserver
reserver(sel
->child
, g
->isize
);
1023 sel
->child
->evaluate(data
, sel
->child
, g
);
1024 gmx_ana_index_difference(sel
->v
.u
.g
, g
, sel
->child
->v
.u
.g
);
1028 * \param[in] data Data for the current frame.
1029 * \param[in] sel Selection element being evaluated.
1030 * \param[in] g Group for which \p sel should be evaluated.
1031 * \returns 0 on success, a non-zero error code on error.
1033 * Short-circuiting evaluation of logical AND expressions.
1035 * Starts by evaluating the first child element in the group \p g.
1036 * The each following child element is evaluated in the intersection
1037 * of all the previous values until all children have been evaluated
1038 * or the intersection becomes empty.
1039 * The value of \p sel is set to the intersection of all the (evaluated)
1042 * If the first child does not have an evaluation function, it is skipped
1043 * and the evaluation is started at the second child.
1044 * This happens if the first child is a constant expression and during
1045 * compilation it was detected that the evaluation group is always a subset
1046 * of the constant group
1047 * (currently, the compiler never detects this).
1049 * This function is used as gmx::SelectionTreeElement::evaluate for
1050 * \ref SEL_BOOLEAN elements with \ref BOOL_AND.
1052 void _gmx_sel_evaluate_and(gmx_sel_evaluate_t
* data
,
1053 const gmx::SelectionTreeElementPointer
& sel
,
1056 SelectionTreeElementPointer child
= sel
->child
;
1057 /* Skip the first child if it does not have an evaluation function. */
1058 if (!child
->evaluate
)
1060 child
= child
->next
;
1062 /* Evaluate the first child */
1064 MempoolSelelemReserver
reserver(child
, g
->isize
);
1065 child
->evaluate(data
, child
, g
);
1066 gmx_ana_index_copy(sel
->v
.u
.g
, child
->v
.u
.g
, false);
1068 child
= child
->next
;
1069 while (child
&& sel
->v
.u
.g
->isize
> 0)
1071 MempoolSelelemReserver
reserver(child
, sel
->v
.u
.g
->isize
);
1072 child
->evaluate(data
, child
, sel
->v
.u
.g
);
1073 gmx_ana_index_intersection(sel
->v
.u
.g
, sel
->v
.u
.g
, child
->v
.u
.g
);
1074 child
= child
->next
;
1079 * \param[in] data Data for the current frame.
1080 * \param[in] sel Selection element being evaluated.
1081 * \param[in] g Group for which \p sel should be evaluated.
1082 * \returns 0 on success, a non-zero error code on error.
1084 * Short-circuiting evaluation of logical OR expressions.
1086 * Starts by evaluating the first child element in the group \p g.
1087 * For each subsequent child, finds the part of \p g that is not
1088 * included the value of any previous child, and evaluates the child
1089 * in that group until the last child is evaluated or all of \p g
1090 * is included in some child value.
1091 * The value of \p sel is set to the union of all the (evaluated)
1094 * If the first child does not have an evaluation function, its value is
1095 * used without evaluation.
1096 * This happens if the first child is a constant expression, the selection
1097 * has been compiled, and the evaluation group is the same for each frame.
1098 * In this case, the compiler has taken care of that the child value is a
1099 * subset of \p g, making it unnecessary to evaluate it.
1101 * This function is used as gmx::SelectionTreeElement::evaluate for
1102 * \ref SEL_BOOLEAN elements with \ref BOOL_OR.
1104 void _gmx_sel_evaluate_or(gmx_sel_evaluate_t
* data
,
1105 const gmx::SelectionTreeElementPointer
& sel
,
1108 gmx_ana_index_t tmp
, tmp2
;
1110 SelectionTreeElementPointer child
= sel
->child
;
1111 if (child
->evaluate
)
1113 MempoolSelelemReserver
reserver(child
, g
->isize
);
1114 child
->evaluate(data
, child
, g
);
1115 gmx_ana_index_partition(sel
->v
.u
.g
, &tmp
, g
, child
->v
.u
.g
);
1119 gmx_ana_index_partition(sel
->v
.u
.g
, &tmp
, g
, child
->v
.u
.g
);
1121 child
= child
->next
;
1122 while (child
&& tmp
.isize
> 0)
1125 MempoolSelelemReserver
reserver(child
, tmp
.isize
);
1126 child
->evaluate(data
, child
, &tmp
);
1127 gmx_ana_index_partition(&tmp
, &tmp2
, &tmp
, child
->v
.u
.g
);
1129 sel
->v
.u
.g
->isize
+= tmp
.isize
;
1130 tmp
.isize
= tmp2
.isize
;
1131 tmp
.index
= tmp2
.index
;
1132 child
= child
->next
;
1134 gmx_ana_index_sort(sel
->v
.u
.g
);
1138 /********************************************************************
1139 * ARITHMETIC EVALUATION
1140 ********************************************************************/
1143 * \param[in] data Data for the current frame.
1144 * \param[in] sel Selection element being evaluated.
1145 * \param[in] g Group for which \p sel should be evaluated.
1146 * \returns 0 on success, a non-zero error code on error.
1148 void _gmx_sel_evaluate_arithmetic(gmx_sel_evaluate_t
* data
,
1149 const gmx::SelectionTreeElementPointer
& sel
,
1153 real lval
, rval
= 0., val
= 0.;
1155 const SelectionTreeElementPointer
& left
= sel
->child
;
1156 const SelectionTreeElementPointer
& right
= left
->next
;
1158 SelelemTemporaryValueAssigner assigner
;
1159 MempoolSelelemReserver reserver
;
1162 assigner
.assign(left
, *sel
);
1165 reserver
.reserve(right
, g
->isize
);
1168 else if (right
&& right
->mempool
)
1170 assigner
.assign(right
, *sel
);
1172 _gmx_sel_evaluate_children(data
, sel
, g
);
1174 n
= (sel
->flags
& SEL_SINGLEVAL
) ? 1 : g
->isize
;
1177 bool bArithNeg
= (sel
->u
.arith
.type
== ARITH_NEG
);
1178 GMX_ASSERT(right
|| bArithNeg
, "Right operand cannot be null except for negations");
1179 for (i
= i1
= i2
= 0; i
< n
; ++i
)
1181 lval
= left
->v
.u
.r
[i1
];
1184 rval
= right
->v
.u
.r
[i2
];
1186 switch (sel
->u
.arith
.type
)
1188 case ARITH_PLUS
: val
= lval
+ rval
; break;
1189 case ARITH_MINUS
: val
= lval
- rval
; break;
1190 case ARITH_NEG
: val
= -lval
; break;
1191 case ARITH_MULT
: val
= lval
* rval
; break;
1192 case ARITH_DIV
: val
= lval
/ rval
; break;
1193 case ARITH_EXP
: val
= pow(lval
, rval
); break;
1195 sel
->v
.u
.r
[i
] = val
;
1196 if (!(left
->flags
& SEL_SINGLEVAL
))
1200 if (!bArithNeg
&& !(right
->flags
& SEL_SINGLEVAL
))