2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014, 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
55 #include "gromacs/math/utilities.h"
56 #include "gromacs/math/vec.h"
57 #include "gromacs/selection/indexutil.h"
58 #include "gromacs/selection/poscalc.h"
59 #include "gromacs/selection/selection.h"
60 #include "gromacs/selection/selmethod.h"
61 #include "gromacs/utility/exceptions.h"
62 #include "gromacs/utility/gmxassert.h"
63 #include "gromacs/utility/smalloc.h"
67 #include "selectioncollection-impl.h"
70 using gmx::SelectionTreeElement;
71 using gmx::SelectionTreeElementPointer;
77 * Reserves memory for a selection element from the evaluation memory pool.
79 * This class implements RAII semantics for allocating memory for selection
80 * element values from a selection evaluation memory pool.
82 * \ingroup module_selection
84 class MempoolSelelemReserver
87 //! Constructs a reserver without initial reservation.
88 MempoolSelelemReserver() {}
90 * Constructs a reserver with initial reservation.
92 * \param[in,out] sel Selection element for which to reserve.
93 * \param[in] count Number of values to reserve.
97 MempoolSelelemReserver(const SelectionTreeElementPointer &sel, int count)
101 //! Frees any memory allocated using this reserver.
102 ~MempoolSelelemReserver()
106 sel_->mempoolRelease();
111 * Reserves memory for selection element values using this reserver.
113 * \param[in,out] sel Selection element for which to reserve.
114 * \param[in] count Number of values to reserve.
116 * Allocates space to store \p count output values in \p sel from the
117 * memory pool associated with \p sel, or from the heap if there is no
118 * memory pool. Type of values to allocate is automatically determined
121 void reserve(const SelectionTreeElementPointer &sel, int count)
123 GMX_RELEASE_ASSERT(!sel_,
124 "Can only reserve one element with one instance");
125 sel->mempoolReserve(count);
130 SelectionTreeElementPointer sel_;
134 * Reserves memory for an index group from the evaluation memory pool.
136 * This class implements RAII semantics for allocating memory for an index
137 * group from a selection evaluation memory pool.
139 * \ingroup module_selection
141 class MempoolGroupReserver
145 * Creates a reserver associated with a given memory pool.
147 * \param mp Memory pool from which to reserve memory.
149 explicit MempoolGroupReserver(gmx_sel_mempool_t *mp)
153 //! Frees any memory allocated using this reserver.
154 ~MempoolGroupReserver()
158 _gmx_sel_mempool_free_group(mp_, g_);
163 * Reserves memory for an index group using this reserver.
165 * \param[in,out] g Index group to reserve.
166 * \param[in] count Number of atoms to reserve space for.
168 * Allocates memory from the memory pool to store \p count atoms in
171 void reserve(gmx_ana_index_t *g, int count)
173 GMX_RELEASE_ASSERT(g_ == NULL, "Can only reserve one element with one instance");
174 _gmx_sel_mempool_alloc_group(mp_, g, count);
179 gmx_sel_mempool_t *mp_;
184 * Assigns a temporary value for a selection element.
186 * This class implements RAII semantics for temporarily assigning the value
187 * pointer of a selection element to point to a different location.
189 * \ingroup module_selection
191 class SelelemTemporaryValueAssigner
194 //! Constructs an assigner without an initial assignment.
195 SelelemTemporaryValueAssigner()
196 : old_ptr_(NULL), 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,
208 const SelectionTreeElement &vsource)
210 assign(sel, vsource);
212 //! Undoes any temporary assignment done using this assigner.
213 ~SelelemTemporaryValueAssigner()
217 _gmx_selvalue_setstore_alloc(&sel_->v, old_ptr_, old_nalloc_);
222 * Assigns a temporary value pointer.
224 * \param[in,out] sel Selection element for which to assign.
225 * \param[in] vsource Element to which \p sel values will point to.
227 * Assigns the value pointer in \p sel to point to the values in
228 * \p vsource, i.e., any access/modification to values in \p sel
229 * actually accesses values in \p vsource.
231 void assign(const SelectionTreeElementPointer &sel,
232 const SelectionTreeElement &vsource)
234 GMX_RELEASE_ASSERT(!sel_,
235 "Can only assign one element with one instance");
236 GMX_RELEASE_ASSERT(sel->v.type == vsource.v.type,
237 "Mismatching selection value types");
238 old_ptr_ = sel->v.u.ptr;
239 old_nalloc_ = sel->v.nalloc;
240 _gmx_selvalue_setstore(&sel->v, vsource.v.u.ptr);
245 SelectionTreeElementPointer sel_;
253 * \param[in] fp File handle to receive the output.
254 * \param[in] evalfunc Function pointer to print.
257 _gmx_sel_print_evalfunc_name(FILE *fp, gmx::sel_evalfunc evalfunc)
263 else if (evalfunc == &_gmx_sel_evaluate_root)
267 else if (evalfunc == &_gmx_sel_evaluate_static)
269 fprintf(fp, "static");
271 else if (evalfunc == &_gmx_sel_evaluate_subexpr_simple)
273 fprintf(fp, "subexpr_simple");
275 else if (evalfunc == &_gmx_sel_evaluate_subexpr_staticeval)
277 fprintf(fp, "subexpr_staticeval");
279 else if (evalfunc == &_gmx_sel_evaluate_subexpr)
281 fprintf(fp, "subexpr");
283 else if (evalfunc == &_gmx_sel_evaluate_subexprref_simple)
285 fprintf(fp, "ref_simple");
287 else if (evalfunc == &_gmx_sel_evaluate_subexprref)
291 else if (evalfunc == &_gmx_sel_evaluate_method)
293 fprintf(fp, "method");
295 else if (evalfunc == &_gmx_sel_evaluate_modifier)
299 else if (evalfunc == &_gmx_sel_evaluate_not)
303 else if (evalfunc == &_gmx_sel_evaluate_and)
307 else if (evalfunc == &_gmx_sel_evaluate_or)
311 else if (evalfunc == &_gmx_sel_evaluate_arithmetic)
313 fprintf(fp, "arithmetic");
317 fprintf(fp, "%p", (void*)(evalfunc));
322 * \param[out] data Evaluation data structure to initialize.
323 * \param[in] mp Memory pool for intermediate evaluation values.
324 * \param[in] gall Index group with all the atoms.
325 * \param[in] top Topology structure for evaluation.
326 * \param[in] fr New frame for evaluation.
327 * \param[in] pbc New PBC information for evaluation.
330 _gmx_sel_evaluate_init(gmx_sel_evaluate_t *data,
331 gmx_sel_mempool_t *mp, gmx_ana_index_t *gall,
332 t_topology *top, t_trxframe *fr, t_pbc *pbc)
342 * Recursively initializes the flags for evaluation.
344 * \param[in,out] sel Selection element to clear.
346 * The \ref SEL_INITFRAME flag is set for \ref SEL_EXPRESSION elements whose
347 * method defines the \p init_frame callback (see sel_framefunc()), and
348 * cleared for other elements.
350 * The \ref SEL_EVALFRAME flag is cleared for all elements.
353 init_frame_eval(SelectionTreeElementPointer sel)
357 sel->flags &= ~(SEL_INITFRAME | SEL_EVALFRAME);
358 if (sel->type == SEL_EXPRESSION)
360 if (sel->u.expr.method && sel->u.expr.method->init_frame)
362 sel->flags |= SEL_INITFRAME;
365 if (sel->child && sel->type != SEL_SUBEXPRREF)
367 init_frame_eval(sel->child);
376 SelectionEvaluator::SelectionEvaluator()
381 * \param[in,out] coll The selection collection to evaluate.
382 * \param[in] fr Frame for which the evaluation should be carried out.
383 * \param[in] pbc PBC data, or NULL if no PBC should be used.
384 * \returns 0 on successful evaluation, a non-zero error code on error.
386 * This functions sets the global variables for topology, frame and PBC,
387 * clears some information in the selection to initialize the evaluation
388 * for a new frame, and evaluates \p sel and all the selections pointed by
389 * the \p next pointers of \p sel.
391 * This is the only function that user code should call if they want to
392 * evaluate a selection for a new frame.
395 SelectionEvaluator::evaluate(SelectionCollection *coll,
396 t_trxframe *fr, t_pbc *pbc)
398 gmx_ana_selcollection_t *sc = &coll->impl_->sc_;
399 gmx_sel_evaluate_t data;
401 _gmx_sel_evaluate_init(&data, sc->mempool, &sc->gall, sc->top, fr, pbc);
402 init_frame_eval(sc->root);
403 SelectionTreeElementPointer sel = sc->root;
406 /* Clear the evaluation group of subexpressions */
407 if (sel->child && sel->child->type == SEL_SUBEXPR
408 && sel->child->evaluate != NULL)
410 sel->child->u.cgrp.isize = 0;
411 /* Not strictly necessary, because the value will be overwritten
412 * during first evaluation of the subexpression anyways, but we
413 * clear the group for clarity. Note that this is _not_ done during
414 * compilation because of some additional complexities involved
415 * (see compiler.cpp), so it should not be relied upon in
416 * _gmx_sel_evaluate_subexpr(). */
417 if (sel->child->v.type == GROUP_VALUE)
419 sel->child->v.u.g->isize = 0;
424 sel->evaluate(&data, sel, NULL);
428 /* Update selection information */
429 SelectionDataList::const_iterator isel;
430 for (isel = sc->sel.begin(); isel != sc->sel.end(); ++isel)
432 internal::SelectionData &sel = **isel;
433 sel.refreshMassesAndCharges(sc->top);
434 sel.updateCoveredFractionForFrame();
439 * \param[in,out] coll The selection collection to evaluate.
440 * \param[in] nframes Total number of frames.
443 SelectionEvaluator::evaluateFinal(SelectionCollection *coll, int nframes)
445 gmx_ana_selcollection_t *sc = &coll->impl_->sc_;
447 SelectionDataList::const_iterator isel;
448 for (isel = sc->sel.begin(); isel != sc->sel.end(); ++isel)
450 internal::SelectionData &sel = **isel;
451 sel.restoreOriginalPositions(sc->top);
452 sel.computeAverageCoveredFraction(nframes);
459 * \param[in] data Data for the current frame.
460 * \param[in] sel Selection element being evaluated.
461 * \param[in] g Group for which \p sel should be evaluated.
462 * \returns 0 on success, a non-zero error code on error.
464 * Evaluates each child of \p sel in \p g.
467 _gmx_sel_evaluate_children(gmx_sel_evaluate_t *data,
468 const gmx::SelectionTreeElementPointer &sel,
471 SelectionTreeElementPointer child = sel->child;
476 child->evaluate(data, child, g);
483 _gmx_sel_evaluate_root(gmx_sel_evaluate_t *data,
484 const gmx::SelectionTreeElementPointer &sel,
485 gmx_ana_index_t * /* g */)
487 if (sel->u.cgrp.isize == 0 || !sel->child->evaluate)
492 sel->child->evaluate(data, sel->child,
493 sel->u.cgrp.isize < 0 ? NULL : &sel->u.cgrp);
497 _gmx_sel_evaluate_static(gmx_sel_evaluate_t * /* data */,
498 const gmx::SelectionTreeElementPointer &sel,
501 gmx_ana_index_intersection(sel->v.u.g, &sel->u.cgrp, g);
505 /*********************************************************************
506 * SUBEXPRESSION EVALUATION
507 *********************************************************************/
510 * \param[in] data Data for the current frame.
511 * \param[in] sel Selection element being evaluated.
512 * \param[in] g Group for which \p sel should be evaluated.
513 * \returns 0 on success, a non-zero error code on error.
515 * Evaluates the child element (there should be exactly one) in \p g.
516 * The compiler has taken care that the child actually stores the evaluated
517 * value in the value pointer of this element.
519 * This function is used as gmx::SelectionTreeElement::evaluate for
520 * \ref SEL_SUBEXPR elements that are used only once, and hence do not need
521 * full subexpression handling.
524 _gmx_sel_evaluate_subexpr_simple(gmx_sel_evaluate_t *data,
525 const gmx::SelectionTreeElementPointer &sel,
528 if (sel->child->evaluate)
530 sel->child->evaluate(data, sel->child, g);
532 sel->v.nr = sel->child->v.nr;
536 * \param[in] data Data for the current frame.
537 * \param[in] sel Selection element being evaluated.
538 * \param[in] g Group for which \p sel should be evaluated.
539 * \returns 0 on success, a non-zero error code on error.
541 * If this is the first call for this frame, evaluates the child element
542 * there should be exactly one in \p g.
543 * The compiler has taken care that the child actually stores the evaluated
544 * value in the value pointer of this element.
545 * Assumes that \p g is persistent for the duration of the whole evaluation.
547 * This function is used as gmx::SelectionTreeElement::evaluate for
548 * \ref SEL_SUBEXPR elements that have a static evaluation group, and hence do
549 * not need full subexpression handling.
552 _gmx_sel_evaluate_subexpr_staticeval(gmx_sel_evaluate_t *data,
553 const gmx::SelectionTreeElementPointer &sel,
556 if (sel->u.cgrp.isize == 0)
558 sel->child->evaluate(data, sel->child, g);
559 sel->v.nr = sel->child->v.nr;
562 sel->u.cgrp.isize = -1;
566 gmx_ana_index_set(&sel->u.cgrp, g->isize, g->index, 0);
572 * \param[in] data Data for the current frame.
573 * \param[in] sel Selection element being evaluated.
574 * \param[in] g Group for which \p sel should be evaluated.
575 * \returns 0 on success, a non-zero error code on error.
577 * Finds the part of \p g for which the subexpression
578 * has not yet been evaluated by comparing \p g to \p sel->u.cgrp.
579 * If the part is not empty, the child expression is evaluated for this
580 * part, and the results merged to the old values of the child.
581 * The value of \p sel itself is undefined after the call.
584 * The call to gmx_ana_index_difference() can take quite a lot of unnecessary
585 * time if the subexpression is evaluated either several times for the same
586 * group or for completely distinct groups.
587 * However, in the majority of cases, these situations occur when
588 * _gmx_sel_evaluate_subexpr_staticeval() can be used, so this should not be a
592 _gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t *data,
593 const gmx::SelectionTreeElementPointer &sel,
596 gmx_ana_index_t gmiss;
598 MempoolGroupReserver gmissreserver(data->mp);
599 if (sel->u.cgrp.isize == 0)
602 SelelemTemporaryValueAssigner assigner(sel->child, *sel);
603 sel->child->evaluate(data, sel->child, g);
605 gmx_ana_index_copy(&sel->u.cgrp, g, false);
610 gmissreserver.reserve(&gmiss, g->isize);
611 gmx_ana_index_difference(&gmiss, g, &sel->u.cgrp);
615 MempoolSelelemReserver reserver(sel->child, gmiss.isize);
616 /* Evaluate the missing values for the child */
617 sel->child->evaluate(data, sel->child, &gmiss);
618 /* Merge the missing values to the existing ones. */
619 if (sel->v.type == GROUP_VALUE)
621 gmx_ana_index_merge(sel->v.u.g, sel->child->v.u.g, sel->v.u.g);
627 i = sel->u.cgrp.isize - 1;
629 /* TODO: This switch is kind of ugly, but it may be difficult to
630 * do this portably without C++ templates. */
634 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
636 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
638 sel->v.u.i[k] = sel->child->v.u.i[j--];
642 sel->v.u.i[k] = sel->v.u.i[i--];
648 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
650 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
652 sel->v.u.r[k] = sel->child->v.u.r[j--];
656 sel->v.u.r[k] = sel->v.u.r[i--];
662 // Note: with the currently allowed syntax, this case is never
664 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
666 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
668 sel->v.u.s[k] = sel->child->v.u.s[j--];
672 sel->v.u.s[k] = sel->v.u.s[i--];
678 /* TODO: Implement this */
679 GMX_THROW(gmx::NotImplementedError("position subexpressions not implemented properly"));
683 GMX_THROW(gmx::InternalError("Invalid subexpression type"));
686 gmx_ana_index_merge(&sel->u.cgrp, &sel->u.cgrp, &gmiss);
691 * \param[in] data Data for the current frame.
692 * \param[in] sel Selection element being evaluated.
693 * \param[in] g Group for which \p sel should be evaluated.
694 * \returns 0 for success.
696 * Sets the value pointers of the child and its child to point to the same
697 * memory as the value pointer of this element to avoid copying, and then
698 * evaluates evaluates the child.
700 * This function is used as gmx::SelectionTreeElement:evaluate for
701 * \ref SEL_SUBEXPRREF elements for which the \ref SEL_SUBEXPR does not have
705 _gmx_sel_evaluate_subexprref_simple(gmx_sel_evaluate_t *data,
706 const gmx::SelectionTreeElementPointer &sel,
711 _gmx_selvalue_setstore(&sel->child->v, sel->v.u.ptr);
712 _gmx_selvalue_setstore_alloc(&sel->child->child->v, sel->v.u.ptr,
713 sel->child->child->v.nalloc);
714 sel->child->evaluate(data, sel->child, g);
716 sel->v.nr = sel->child->v.nr;
719 sel->u.param->val.nr = sel->v.nr;
720 if (sel->u.param->nvalptr)
722 *sel->u.param->nvalptr = sel->u.param->val.nr;
728 * \param[in] data Data for the current frame.
729 * \param[in] sel Selection element being evaluated.
730 * \param[in] g Group for which \p sel should be evaluated.
731 * \returns 0 on success, a non-zero error code on error.
733 * If the value type is \ref POS_VALUE, the value of the child is simply
734 * copied to set the value of \p sel (the child subexpression should
735 * already have been evaluated by its root).
736 * If the value type is something else, the child is evaluated for the
737 * group \p g, and the value of the child is then copied.
738 * There should be only one child element.
740 * This function is used as gmx::SelectionTreeElement::evaluate for
741 * \ref SEL_SUBEXPRREF elements.
744 _gmx_sel_evaluate_subexprref(gmx_sel_evaluate_t *data,
745 const gmx::SelectionTreeElementPointer &sel,
750 if (g != NULL && sel->child->evaluate != NULL)
752 sel->child->evaluate(data, sel->child, g);
754 const SelectionTreeElementPointer &expr = sel->child;
760 sel->v.nr = expr->v.nr;
761 memcpy(sel->v.u.i, expr->v.u.i, sel->v.nr*sizeof(*sel->v.u.i));
765 sel->v.nr = g->isize;
766 /* Extract the values corresponding to g */
767 for (i = j = 0; i < g->isize; ++i, ++j)
769 while (sel->child->u.cgrp.index[j] < g->index[i])
773 sel->v.u.i[i] = expr->v.u.i[j];
781 sel->v.nr = expr->v.nr;
782 memcpy(sel->v.u.r, expr->v.u.r, sel->v.nr*sizeof(*sel->v.u.r));
786 sel->v.nr = g->isize;
787 /* Extract the values corresponding to g */
788 for (i = j = 0; i < g->isize; ++i, ++j)
790 while (sel->child->u.cgrp.index[j] < g->index[i])
794 sel->v.u.r[i] = expr->v.u.r[j];
802 sel->v.nr = expr->v.nr;
803 memcpy(sel->v.u.s, expr->v.u.s, sel->v.nr*sizeof(*sel->v.u.s));
807 sel->v.nr = g->isize;
808 /* Extract the values corresponding to g */
809 for (i = j = 0; i < g->isize; ++i, ++j)
811 while (sel->child->u.cgrp.index[j] < g->index[i])
815 sel->v.u.s[i] = expr->v.u.s[j];
821 /* Currently, there is no need to do anything fancy here,
822 * but some future extensions may need a more flexible
824 gmx_ana_pos_copy(sel->v.u.p, expr->v.u.p, false);
830 gmx_ana_index_copy(sel->v.u.g, expr->v.u.g, false);
834 gmx_ana_index_intersection(sel->v.u.g, expr->v.u.g, g);
838 default: /* should not be reached */
839 GMX_THROW(gmx::InternalError("Invalid subexpression reference type"));
841 /* Store the number of values if needed */
844 sel->u.param->val.nr = sel->v.nr;
845 if (sel->u.param->nvalptr)
847 *sel->u.param->nvalptr = sel->u.param->val.nr;
852 /********************************************************************
853 * METHOD EXPRESSION EVALUATION
854 ********************************************************************/
857 * \param[in] data Data for the current frame.
858 * \param[in] sel Selection element being evaluated.
859 * \param[in] g Group for which \p sel should be evaluated.
860 * \returns 0 on success, a non-zero error code on error.
862 * Evaluates each child of a \ref SEL_EXPRESSION element.
863 * The value of \p sel is not touched.
865 * This function is not used as gmx::SelectionTreeElement::evaluate,
866 * but is used internally.
869 _gmx_sel_evaluate_method_params(gmx_sel_evaluate_t *data,
870 const gmx::SelectionTreeElementPointer &sel,
873 SelectionTreeElementPointer child = sel->child;
876 if (child->evaluate && !(child->flags & SEL_EVALFRAME))
878 if (child->flags & SEL_ATOMVAL)
880 child->evaluate(data, child, g);
884 child->flags |= SEL_EVALFRAME;
885 child->evaluate(data, child, NULL);
893 * \param[in] data Data for the current frame.
894 * \param[in] sel Selection element being evaluated.
895 * \param[in] g Group for which \p sel should be evaluated.
896 * \returns 0 on success, a non-zero error code on error.
898 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
899 * to evaluate any parameter values.
900 * If this is the first time this expression is evaluated for
901 * the frame, sel_framefunc() callback is called if one is provided.
902 * If a reference position calculation has been initialized for this element,
903 * the positions are also updated, and sel_updatefunc_pos() is used to
904 * evaluate the value. Otherwise, sel_updatefunc() is used.
906 * This function is used as gmx::SelectionTreeElement::evaluate for
907 * \ref SEL_EXPRESSION elements.
910 _gmx_sel_evaluate_method(gmx_sel_evaluate_t *data,
911 const gmx::SelectionTreeElementPointer &sel,
914 _gmx_sel_evaluate_method_params(data, sel, g);
915 if (sel->flags & SEL_INITFRAME)
917 sel->flags &= ~SEL_INITFRAME;
918 sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
923 gmx_ana_poscalc_update(sel->u.expr.pc, sel->u.expr.pos, g,
924 data->fr, data->pbc);
925 sel->u.expr.method->pupdate(data->top, data->fr, data->pbc,
926 sel->u.expr.pos, &sel->v,
931 sel->u.expr.method->update(data->top, data->fr, data->pbc, g,
932 &sel->v, sel->u.expr.mdata);
937 * \param[in] data Data for the current frame.
938 * \param[in] sel Selection element being evaluated.
939 * \param[in] g Group for which \p sel should be evaluated.
940 * \returns 0 on success, a non-zero error code on error.
942 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
943 * to evaluate any parameter values.
944 * If this is the first time this expression is evaluated for
945 * the frame, sel_framefunc() callback is called if one is provided.
946 * The modifier is then evaluated using sel_updatefunc_pos().
948 * This function is used as gmx::SelectionTreeElement::evaluate for
949 * \ref SEL_MODIFIER elements.
952 _gmx_sel_evaluate_modifier(gmx_sel_evaluate_t *data,
953 const gmx::SelectionTreeElementPointer &sel,
956 _gmx_sel_evaluate_method_params(data, sel, g);
957 if (sel->flags & SEL_INITFRAME)
959 sel->flags &= ~SEL_INITFRAME;
960 sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
963 GMX_RELEASE_ASSERT(sel->child != NULL,
964 "Modifier element with a value must have a child");
965 if (sel->child->v.type != POS_VALUE)
967 GMX_THROW(gmx::NotImplementedError("Non-position valued modifiers not implemented"));
969 sel->u.expr.method->pupdate(data->top, data->fr, data->pbc,
971 &sel->v, sel->u.expr.mdata);
975 /********************************************************************
976 * BOOLEAN EXPRESSION EVALUATION
977 ********************************************************************/
980 * \param[in] data Data for the current frame.
981 * \param[in] sel Selection element being evaluated.
982 * \param[in] g Group for which \p sel should be evaluated.
983 * \returns 0 on success, a non-zero error code on error.
985 * Evaluates the child element (there should be only one) in the group
986 * \p g, and then sets the value of \p sel to the complement of the
989 * This function is used as gmx::SelectionTreeElement::evaluate for
990 * \ref SEL_BOOLEAN elements with \ref BOOL_NOT.
993 _gmx_sel_evaluate_not(gmx_sel_evaluate_t *data,
994 const gmx::SelectionTreeElementPointer &sel,
997 MempoolSelelemReserver reserver(sel->child, g->isize);
998 sel->child->evaluate(data, sel->child, g);
999 gmx_ana_index_difference(sel->v.u.g, g, sel->child->v.u.g);
1003 * \param[in] data Data for the current frame.
1004 * \param[in] sel Selection element being evaluated.
1005 * \param[in] g Group for which \p sel should be evaluated.
1006 * \returns 0 on success, a non-zero error code on error.
1008 * Short-circuiting evaluation of logical AND expressions.
1010 * Starts by evaluating the first child element in the group \p g.
1011 * The each following child element is evaluated in the intersection
1012 * of all the previous values until all children have been evaluated
1013 * or the intersection becomes empty.
1014 * The value of \p sel is set to the intersection of all the (evaluated)
1017 * If the first child does not have an evaluation function, it is skipped
1018 * and the evaluation is started at the second child.
1019 * This happens if the first child is a constant expression and during
1020 * compilation it was detected that the evaluation group is always a subset
1021 * of the constant group
1022 * (currently, the compiler never detects this).
1024 * This function is used as gmx::SelectionTreeElement::evaluate for
1025 * \ref SEL_BOOLEAN elements with \ref BOOL_AND.
1028 _gmx_sel_evaluate_and(gmx_sel_evaluate_t *data,
1029 const gmx::SelectionTreeElementPointer &sel,
1032 SelectionTreeElementPointer child = sel->child;
1033 /* Skip the first child if it does not have an evaluation function. */
1034 if (!child->evaluate)
1036 child = child->next;
1038 /* Evaluate the first child */
1040 MempoolSelelemReserver reserver(child, g->isize);
1041 child->evaluate(data, child, g);
1042 gmx_ana_index_copy(sel->v.u.g, child->v.u.g, false);
1044 child = child->next;
1045 while (child && sel->v.u.g->isize > 0)
1047 MempoolSelelemReserver reserver(child, sel->v.u.g->isize);
1048 child->evaluate(data, child, sel->v.u.g);
1049 gmx_ana_index_intersection(sel->v.u.g, sel->v.u.g, child->v.u.g);
1050 child = child->next;
1055 * \param[in] data Data for the current frame.
1056 * \param[in] sel Selection element being evaluated.
1057 * \param[in] g Group for which \p sel should be evaluated.
1058 * \returns 0 on success, a non-zero error code on error.
1060 * Short-circuiting evaluation of logical OR expressions.
1062 * Starts by evaluating the first child element in the group \p g.
1063 * For each subsequent child, finds the part of \p g that is not
1064 * included the value of any previous child, and evaluates the child
1065 * in that group until the last child is evaluated or all of \p g
1066 * is included in some child value.
1067 * The value of \p sel is set to the union of all the (evaluated)
1070 * If the first child does not have an evaluation function, its value is
1071 * used without evaluation.
1072 * This happens if the first child is a constant expression, the selection
1073 * has been compiled, and the evaluation group is the same for each frame.
1074 * In this case, the compiler has taken care of that the child value is a
1075 * subset of \p g, making it unnecessary to evaluate it.
1077 * This function is used as gmx::SelectionTreeElement::evaluate for
1078 * \ref SEL_BOOLEAN elements with \ref BOOL_OR.
1081 _gmx_sel_evaluate_or(gmx_sel_evaluate_t *data,
1082 const gmx::SelectionTreeElementPointer &sel,
1085 gmx_ana_index_t tmp, tmp2;
1087 SelectionTreeElementPointer child = sel->child;
1088 if (child->evaluate)
1090 MempoolSelelemReserver reserver(child, g->isize);
1091 child->evaluate(data, child, g);
1092 gmx_ana_index_partition(sel->v.u.g, &tmp, g, child->v.u.g);
1096 gmx_ana_index_partition(sel->v.u.g, &tmp, g, child->v.u.g);
1098 child = child->next;
1099 while (child && tmp.isize > 0)
1102 MempoolSelelemReserver reserver(child, tmp.isize);
1103 child->evaluate(data, child, &tmp);
1104 gmx_ana_index_partition(&tmp, &tmp2, &tmp, child->v.u.g);
1106 sel->v.u.g->isize += tmp.isize;
1107 tmp.isize = tmp2.isize;
1108 tmp.index = tmp2.index;
1109 child = child->next;
1111 gmx_ana_index_sort(sel->v.u.g);
1115 /********************************************************************
1116 * ARITHMETIC EVALUATION
1117 ********************************************************************/
1120 * \param[in] data Data for the current frame.
1121 * \param[in] sel Selection element being evaluated.
1122 * \param[in] g Group for which \p sel should be evaluated.
1123 * \returns 0 on success, a non-zero error code on error.
1126 _gmx_sel_evaluate_arithmetic(gmx_sel_evaluate_t *data,
1127 const gmx::SelectionTreeElementPointer &sel,
1131 real lval, rval = 0., val = 0.;
1133 const SelectionTreeElementPointer &left = sel->child;
1134 const SelectionTreeElementPointer &right = left->next;
1136 SelelemTemporaryValueAssigner assigner;
1137 MempoolSelelemReserver reserver;
1140 assigner.assign(left, *sel);
1143 reserver.reserve(right, g->isize);
1146 else if (right && right->mempool)
1148 assigner.assign(right, *sel);
1150 _gmx_sel_evaluate_children(data, sel, g);
1152 n = (sel->flags & SEL_SINGLEVAL) ? 1 : g->isize;
1155 bool bArithNeg = (sel->u.arith.type == ARITH_NEG);
1156 GMX_ASSERT(right || bArithNeg,
1157 "Right operand cannot be null except for negations");
1158 for (i = i1 = i2 = 0; i < n; ++i)
1160 lval = left->v.u.r[i1];
1163 rval = right->v.u.r[i2];
1165 switch (sel->u.arith.type)
1167 case ARITH_PLUS: val = lval + rval; break;
1168 case ARITH_MINUS: val = lval - rval; break;
1169 case ARITH_NEG: val = -lval; break;
1170 case ARITH_MULT: val = lval * rval; break;
1171 case ARITH_DIV: val = lval / rval; break;
1172 case ARITH_EXP: val = pow(lval, rval); break;
1174 sel->v.u.r[i] = val;
1175 if (!(left->flags & SEL_SINGLEVAL))
1179 if (!bArithNeg && !(right->flags & SEL_SINGLEVAL))