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/legacyheaders/vec.h"
57 #include "gromacs/math/utilities.h"
58 #include "gromacs/selection/indexutil.h"
59 #include "gromacs/selection/poscalc.h"
60 #include "gromacs/selection/selection.h"
61 #include "gromacs/selection/selmethod.h"
62 #include "gromacs/utility/exceptions.h"
63 #include "gromacs/utility/gmxassert.h"
64 #include "gromacs/utility/smalloc.h"
68 #include "selectioncollection-impl.h"
71 using gmx::SelectionTreeElement;
72 using gmx::SelectionTreeElementPointer;
78 * Reserves memory for a selection element from the evaluation memory pool.
80 * This class implements RAII semantics for allocating memory for selection
81 * element values from a selection evaluation memory pool.
83 * \ingroup module_selection
85 class MempoolSelelemReserver
88 //! Constructs a reserver without initial reservation.
89 MempoolSelelemReserver() {}
91 * Constructs a reserver with initial reservation.
93 * \param[in,out] sel Selection element for which to reserve.
94 * \param[in] count Number of values to reserve.
98 MempoolSelelemReserver(const SelectionTreeElementPointer &sel, int count)
102 //! Frees any memory allocated using this reserver.
103 ~MempoolSelelemReserver()
107 sel_->mempoolRelease();
112 * Reserves memory for selection element values using this reserver.
114 * \param[in,out] sel Selection element for which to reserve.
115 * \param[in] count Number of values to reserve.
117 * Allocates space to store \p count output values in \p sel from the
118 * memory pool associated with \p sel, or from the heap if there is no
119 * memory pool. Type of values to allocate is automatically determined
122 void reserve(const SelectionTreeElementPointer &sel, int count)
124 GMX_RELEASE_ASSERT(!sel_,
125 "Can only reserve one element with one instance");
126 sel->mempoolReserve(count);
131 SelectionTreeElementPointer sel_;
135 * Reserves memory for an index group from the evaluation memory pool.
137 * This class implements RAII semantics for allocating memory for an index
138 * group from a selection evaluation memory pool.
140 * \ingroup module_selection
142 class MempoolGroupReserver
146 * Creates a reserver associated with a given memory pool.
148 * \param mp Memory pool from which to reserve memory.
150 explicit MempoolGroupReserver(gmx_sel_mempool_t *mp)
154 //! Frees any memory allocated using this reserver.
155 ~MempoolGroupReserver()
159 _gmx_sel_mempool_free_group(mp_, g_);
164 * Reserves memory for an index group using this reserver.
166 * \param[in,out] g Index group to reserve.
167 * \param[in] count Number of atoms to reserve space for.
169 * Allocates memory from the memory pool to store \p count atoms in
172 void reserve(gmx_ana_index_t *g, int count)
174 GMX_RELEASE_ASSERT(g_ == NULL, "Can only reserve one element with one instance");
175 _gmx_sel_mempool_alloc_group(mp_, g, count);
180 gmx_sel_mempool_t *mp_;
185 * Assigns a temporary value for a selection element.
187 * This class implements RAII semantics for temporarily assigning the value
188 * pointer of a selection element to point to a different location.
190 * \ingroup module_selection
192 class SelelemTemporaryValueAssigner
195 //! Constructs an assigner without an initial assignment.
196 SelelemTemporaryValueAssigner()
197 : old_ptr_(NULL), old_nalloc_(0)
201 * Constructs an assigner with an initial assignment.
203 * \param[in,out] sel Selection element for which to assign.
204 * \param[in] vsource Element to which \p sel values will point to.
208 SelelemTemporaryValueAssigner(const SelectionTreeElementPointer &sel,
209 const SelectionTreeElement &vsource)
211 assign(sel, vsource);
213 //! Undoes any temporary assignment done using this assigner.
214 ~SelelemTemporaryValueAssigner()
218 _gmx_selvalue_setstore_alloc(&sel_->v, old_ptr_, old_nalloc_);
223 * Assigns a temporary value pointer.
225 * \param[in,out] sel Selection element for which to assign.
226 * \param[in] vsource Element to which \p sel values will point to.
228 * Assigns the value pointer in \p sel to point to the values in
229 * \p vsource, i.e., any access/modification to values in \p sel
230 * actually accesses values in \p vsource.
232 void assign(const SelectionTreeElementPointer &sel,
233 const SelectionTreeElement &vsource)
235 GMX_RELEASE_ASSERT(!sel_,
236 "Can only assign one element with one instance");
237 GMX_RELEASE_ASSERT(sel->v.type == vsource.v.type,
238 "Mismatching selection value types");
239 old_ptr_ = sel->v.u.ptr;
240 old_nalloc_ = sel->v.nalloc;
241 _gmx_selvalue_setstore(&sel->v, vsource.v.u.ptr);
246 SelectionTreeElementPointer sel_;
254 * \param[in] fp File handle to receive the output.
255 * \param[in] evalfunc Function pointer to print.
258 _gmx_sel_print_evalfunc_name(FILE *fp, gmx::sel_evalfunc evalfunc)
264 else if (evalfunc == &_gmx_sel_evaluate_root)
268 else if (evalfunc == &_gmx_sel_evaluate_static)
270 fprintf(fp, "static");
272 else if (evalfunc == &_gmx_sel_evaluate_subexpr_simple)
274 fprintf(fp, "subexpr_simple");
276 else if (evalfunc == &_gmx_sel_evaluate_subexpr_staticeval)
278 fprintf(fp, "subexpr_staticeval");
280 else if (evalfunc == &_gmx_sel_evaluate_subexpr)
282 fprintf(fp, "subexpr");
284 else if (evalfunc == &_gmx_sel_evaluate_subexprref_simple)
286 fprintf(fp, "ref_simple");
288 else if (evalfunc == &_gmx_sel_evaluate_subexprref)
292 else if (evalfunc == &_gmx_sel_evaluate_method)
294 fprintf(fp, "method");
296 else if (evalfunc == &_gmx_sel_evaluate_modifier)
300 else if (evalfunc == &_gmx_sel_evaluate_not)
304 else if (evalfunc == &_gmx_sel_evaluate_and)
308 else if (evalfunc == &_gmx_sel_evaluate_or)
312 else if (evalfunc == &_gmx_sel_evaluate_arithmetic)
314 fprintf(fp, "arithmetic");
318 fprintf(fp, "%p", (void*)(evalfunc));
323 * \param[out] data Evaluation data structure to initialize.
324 * \param[in] mp Memory pool for intermediate evaluation values.
325 * \param[in] gall Index group with all the atoms.
326 * \param[in] top Topology structure for evaluation.
327 * \param[in] fr New frame for evaluation.
328 * \param[in] pbc New PBC information for evaluation.
331 _gmx_sel_evaluate_init(gmx_sel_evaluate_t *data,
332 gmx_sel_mempool_t *mp, gmx_ana_index_t *gall,
333 t_topology *top, t_trxframe *fr, t_pbc *pbc)
343 * Recursively initializes the flags for evaluation.
345 * \param[in,out] sel Selection element to clear.
347 * The \ref SEL_INITFRAME flag is set for \ref SEL_EXPRESSION elements whose
348 * method defines the \p init_frame callback (see sel_framefunc()), and
349 * cleared for other elements.
351 * The \ref SEL_EVALFRAME flag is cleared for all elements.
354 init_frame_eval(SelectionTreeElementPointer sel)
358 sel->flags &= ~(SEL_INITFRAME | SEL_EVALFRAME);
359 if (sel->type == SEL_EXPRESSION)
361 if (sel->u.expr.method && sel->u.expr.method->init_frame)
363 sel->flags |= SEL_INITFRAME;
366 if (sel->child && sel->type != SEL_SUBEXPRREF)
368 init_frame_eval(sel->child);
377 SelectionEvaluator::SelectionEvaluator()
382 * \param[in,out] coll The selection collection to evaluate.
383 * \param[in] fr Frame for which the evaluation should be carried out.
384 * \param[in] pbc PBC data, or NULL if no PBC should be used.
385 * \returns 0 on successful evaluation, a non-zero error code on error.
387 * This functions sets the global variables for topology, frame and PBC,
388 * clears some information in the selection to initialize the evaluation
389 * for a new frame, and evaluates \p sel and all the selections pointed by
390 * the \p next pointers of \p sel.
392 * This is the only function that user code should call if they want to
393 * evaluate a selection for a new frame.
396 SelectionEvaluator::evaluate(SelectionCollection *coll,
397 t_trxframe *fr, t_pbc *pbc)
399 gmx_ana_selcollection_t *sc = &coll->impl_->sc_;
400 gmx_sel_evaluate_t data;
402 _gmx_sel_evaluate_init(&data, sc->mempool, &sc->gall, sc->top, fr, pbc);
403 init_frame_eval(sc->root);
404 SelectionTreeElementPointer sel = sc->root;
407 /* Clear the evaluation group of subexpressions */
408 if (sel->child && sel->child->type == SEL_SUBEXPR
409 && sel->child->evaluate != NULL)
411 sel->child->u.cgrp.isize = 0;
412 /* Not strictly necessary, because the value will be overwritten
413 * during first evaluation of the subexpression anyways, but we
414 * clear the group for clarity. Note that this is _not_ done during
415 * compilation because of some additional complexities involved
416 * (see compiler.cpp), so it should not be relied upon in
417 * _gmx_sel_evaluate_subexpr(). */
418 if (sel->child->v.type == GROUP_VALUE)
420 sel->child->v.u.g->isize = 0;
425 sel->evaluate(&data, sel, NULL);
429 /* Update selection information */
430 SelectionDataList::const_iterator isel;
431 for (isel = sc->sel.begin(); isel != sc->sel.end(); ++isel)
433 internal::SelectionData &sel = **isel;
434 sel.refreshMassesAndCharges(sc->top);
435 sel.updateCoveredFractionForFrame();
440 * \param[in,out] coll The selection collection to evaluate.
441 * \param[in] nframes Total number of frames.
444 SelectionEvaluator::evaluateFinal(SelectionCollection *coll, int nframes)
446 gmx_ana_selcollection_t *sc = &coll->impl_->sc_;
448 SelectionDataList::const_iterator isel;
449 for (isel = sc->sel.begin(); isel != sc->sel.end(); ++isel)
451 internal::SelectionData &sel = **isel;
452 sel.restoreOriginalPositions(sc->top);
453 sel.computeAverageCoveredFraction(nframes);
460 * \param[in] data Data for the current frame.
461 * \param[in] sel Selection element being evaluated.
462 * \param[in] g Group for which \p sel should be evaluated.
463 * \returns 0 on success, a non-zero error code on error.
465 * Evaluates each child of \p sel in \p g.
468 _gmx_sel_evaluate_children(gmx_sel_evaluate_t *data,
469 const gmx::SelectionTreeElementPointer &sel,
472 SelectionTreeElementPointer child = sel->child;
477 child->evaluate(data, child, g);
484 _gmx_sel_evaluate_root(gmx_sel_evaluate_t *data,
485 const gmx::SelectionTreeElementPointer &sel,
486 gmx_ana_index_t * /* g */)
488 if (sel->u.cgrp.isize == 0 || !sel->child->evaluate)
493 sel->child->evaluate(data, sel->child,
494 sel->u.cgrp.isize < 0 ? NULL : &sel->u.cgrp);
498 _gmx_sel_evaluate_static(gmx_sel_evaluate_t * /* data */,
499 const gmx::SelectionTreeElementPointer &sel,
502 if (sel->flags & SEL_UNSORTED)
504 // This only works if g contains all the atoms, but that is currently
505 // the only supported case.
506 gmx_ana_index_copy(sel->v.u.g, &sel->u.cgrp, false);
510 gmx_ana_index_intersection(sel->v.u.g, &sel->u.cgrp, g);
515 /*********************************************************************
516 * SUBEXPRESSION EVALUATION
517 *********************************************************************/
520 * \param[in] data Data for the current frame.
521 * \param[in] sel Selection element being evaluated.
522 * \param[in] g Group for which \p sel should be evaluated.
523 * \returns 0 on success, a non-zero error code on error.
525 * Evaluates the child element (there should be exactly one) in \p g.
526 * The compiler has taken care that the child actually stores the evaluated
527 * value in the value pointer of this element.
529 * This function is used as gmx::SelectionTreeElement::evaluate for
530 * \ref SEL_SUBEXPR elements that are used only once, and hence do not need
531 * full subexpression handling.
534 _gmx_sel_evaluate_subexpr_simple(gmx_sel_evaluate_t *data,
535 const gmx::SelectionTreeElementPointer &sel,
538 if (sel->child->evaluate)
540 sel->child->evaluate(data, sel->child, g);
542 sel->v.nr = sel->child->v.nr;
546 * \param[in] data Data for the current frame.
547 * \param[in] sel Selection element being evaluated.
548 * \param[in] g Group for which \p sel should be evaluated.
549 * \returns 0 on success, a non-zero error code on error.
551 * If this is the first call for this frame, evaluates the child element
552 * there should be exactly one in \p g.
553 * The compiler has taken care that the child actually stores the evaluated
554 * value in the value pointer of this element.
555 * Assumes that \p g is persistent for the duration of the whole evaluation.
557 * This function is used as gmx::SelectionTreeElement::evaluate for
558 * \ref SEL_SUBEXPR elements that have a static evaluation group, and hence do
559 * not need full subexpression handling.
562 _gmx_sel_evaluate_subexpr_staticeval(gmx_sel_evaluate_t *data,
563 const gmx::SelectionTreeElementPointer &sel,
566 if (sel->u.cgrp.isize == 0)
568 sel->child->evaluate(data, sel->child, g);
569 sel->v.nr = sel->child->v.nr;
572 sel->u.cgrp.isize = -1;
576 gmx_ana_index_set(&sel->u.cgrp, g->isize, g->index, 0);
582 * \param[in] data Data for the current frame.
583 * \param[in] sel Selection element being evaluated.
584 * \param[in] g Group for which \p sel should be evaluated.
585 * \returns 0 on success, a non-zero error code on error.
587 * Finds the part of \p g for which the subexpression
588 * has not yet been evaluated by comparing \p g to \p sel->u.cgrp.
589 * If the part is not empty, the child expression is evaluated for this
590 * part, and the results merged to the old values of the child.
591 * The value of \p sel itself is undefined after the call.
594 * The call to gmx_ana_index_difference() can take quite a lot of unnecessary
595 * time if the subexpression is evaluated either several times for the same
596 * group or for completely distinct groups.
597 * However, in the majority of cases, these situations occur when
598 * _gmx_sel_evaluate_subexpr_staticeval() can be used, so this should not be a
602 _gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t *data,
603 const gmx::SelectionTreeElementPointer &sel,
606 gmx_ana_index_t gmiss;
608 MempoolGroupReserver gmissreserver(data->mp);
609 if (sel->u.cgrp.isize == 0)
612 SelelemTemporaryValueAssigner assigner(sel->child, *sel);
613 sel->child->evaluate(data, sel->child, g);
615 gmx_ana_index_copy(&sel->u.cgrp, g, false);
620 gmissreserver.reserve(&gmiss, g->isize);
621 gmx_ana_index_difference(&gmiss, g, &sel->u.cgrp);
625 MempoolSelelemReserver reserver(sel->child, gmiss.isize);
626 /* Evaluate the missing values for the child */
627 sel->child->evaluate(data, sel->child, &gmiss);
628 /* Merge the missing values to the existing ones. */
629 if (sel->v.type == GROUP_VALUE)
631 gmx_ana_index_merge(sel->v.u.g, sel->child->v.u.g, sel->v.u.g);
637 i = sel->u.cgrp.isize - 1;
639 /* TODO: This switch is kind of ugly, but it may be difficult to
640 * do this portably without C++ templates. */
644 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
646 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
648 sel->v.u.i[k] = sel->child->v.u.i[j--];
652 sel->v.u.i[k] = sel->v.u.i[i--];
658 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
660 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
662 sel->v.u.r[k] = sel->child->v.u.r[j--];
666 sel->v.u.r[k] = sel->v.u.r[i--];
672 // Note: with the currently allowed syntax, this case is never
674 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
676 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
678 sel->v.u.s[k] = sel->child->v.u.s[j--];
682 sel->v.u.s[k] = sel->v.u.s[i--];
688 /* TODO: Implement this */
689 GMX_THROW(gmx::NotImplementedError("position subexpressions not implemented properly"));
693 GMX_THROW(gmx::InternalError("Invalid subexpression type"));
696 gmx_ana_index_merge(&sel->u.cgrp, &sel->u.cgrp, &gmiss);
701 * \param[in] data Data for the current frame.
702 * \param[in] sel Selection element being evaluated.
703 * \param[in] g Group for which \p sel should be evaluated.
704 * \returns 0 for success.
706 * Sets the value pointers of the child and its child to point to the same
707 * memory as the value pointer of this element to avoid copying, and then
708 * evaluates evaluates the child.
710 * This function is used as gmx::SelectionTreeElement:evaluate for
711 * \ref SEL_SUBEXPRREF elements for which the \ref SEL_SUBEXPR does not have
715 _gmx_sel_evaluate_subexprref_simple(gmx_sel_evaluate_t *data,
716 const gmx::SelectionTreeElementPointer &sel,
721 _gmx_selvalue_setstore(&sel->child->v, sel->v.u.ptr);
722 _gmx_selvalue_setstore_alloc(&sel->child->child->v, sel->v.u.ptr,
723 sel->child->child->v.nalloc);
724 sel->child->evaluate(data, sel->child, g);
726 sel->v.nr = sel->child->v.nr;
729 sel->u.param->val.nr = sel->v.nr;
730 if (sel->u.param->nvalptr)
732 *sel->u.param->nvalptr = sel->u.param->val.nr;
738 * \param[in] data Data for the current frame.
739 * \param[in] sel Selection element being evaluated.
740 * \param[in] g Group for which \p sel should be evaluated.
741 * \returns 0 on success, a non-zero error code on error.
743 * If the value type is \ref POS_VALUE, the value of the child is simply
744 * copied to set the value of \p sel (the child subexpression should
745 * already have been evaluated by its root).
746 * If the value type is something else, the child is evaluated for the
747 * group \p g, and the value of the child is then copied.
748 * There should be only one child element.
750 * This function is used as gmx::SelectionTreeElement::evaluate for
751 * \ref SEL_SUBEXPRREF elements.
754 _gmx_sel_evaluate_subexprref(gmx_sel_evaluate_t *data,
755 const gmx::SelectionTreeElementPointer &sel,
760 if (g != NULL && sel->child->evaluate != NULL)
762 sel->child->evaluate(data, sel->child, g);
764 const SelectionTreeElementPointer &expr = sel->child;
770 sel->v.nr = expr->v.nr;
771 memcpy(sel->v.u.i, expr->v.u.i, sel->v.nr*sizeof(*sel->v.u.i));
775 sel->v.nr = g->isize;
776 /* Extract the values corresponding to g */
777 for (i = j = 0; i < g->isize; ++i, ++j)
779 while (sel->child->u.cgrp.index[j] < g->index[i])
783 sel->v.u.i[i] = expr->v.u.i[j];
791 sel->v.nr = expr->v.nr;
792 memcpy(sel->v.u.r, expr->v.u.r, sel->v.nr*sizeof(*sel->v.u.r));
796 sel->v.nr = g->isize;
797 /* Extract the values corresponding to g */
798 for (i = j = 0; i < g->isize; ++i, ++j)
800 while (sel->child->u.cgrp.index[j] < g->index[i])
804 sel->v.u.r[i] = expr->v.u.r[j];
812 sel->v.nr = expr->v.nr;
813 memcpy(sel->v.u.s, expr->v.u.s, sel->v.nr*sizeof(*sel->v.u.s));
817 sel->v.nr = g->isize;
818 /* Extract the values corresponding to g */
819 for (i = j = 0; i < g->isize; ++i, ++j)
821 while (sel->child->u.cgrp.index[j] < g->index[i])
825 sel->v.u.s[i] = expr->v.u.s[j];
831 /* Currently, there is no need to do anything fancy here,
832 * but some future extensions may need a more flexible
834 gmx_ana_pos_copy(sel->v.u.p, expr->v.u.p, false);
840 gmx_ana_index_copy(sel->v.u.g, expr->v.u.g, false);
844 gmx_ana_index_intersection(sel->v.u.g, expr->v.u.g, g);
848 default: /* should not be reached */
849 GMX_THROW(gmx::InternalError("Invalid subexpression reference type"));
851 /* Store the number of values if needed */
854 sel->u.param->val.nr = sel->v.nr;
855 if (sel->u.param->nvalptr)
857 *sel->u.param->nvalptr = sel->u.param->val.nr;
862 /********************************************************************
863 * METHOD EXPRESSION EVALUATION
864 ********************************************************************/
867 * \param[in] data Data for the current frame.
868 * \param[in] sel Selection element being evaluated.
869 * \param[in] g Group for which \p sel should be evaluated.
870 * \returns 0 on success, a non-zero error code on error.
872 * Evaluates each child of a \ref SEL_EXPRESSION element.
873 * The value of \p sel is not touched.
875 * This function is not used as gmx::SelectionTreeElement::evaluate,
876 * but is used internally.
879 _gmx_sel_evaluate_method_params(gmx_sel_evaluate_t *data,
880 const gmx::SelectionTreeElementPointer &sel,
883 SelectionTreeElementPointer child = sel->child;
886 if (child->evaluate && !(child->flags & SEL_EVALFRAME))
888 if (child->flags & SEL_ATOMVAL)
890 child->evaluate(data, child, g);
894 child->flags |= SEL_EVALFRAME;
895 child->evaluate(data, child, NULL);
903 * \param[in] data Data for the current frame.
904 * \param[in] sel Selection element being evaluated.
905 * \param[in] g Group for which \p sel should be evaluated.
906 * \returns 0 on success, a non-zero error code on error.
908 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
909 * to evaluate any parameter values.
910 * If this is the first time this expression is evaluated for
911 * the frame, sel_framefunc() callback is called if one is provided.
912 * If a reference position calculation has been initialized for this element,
913 * the positions are also updated, and sel_updatefunc_pos() is used to
914 * evaluate the value. Otherwise, sel_updatefunc() is used.
916 * This function is used as gmx::SelectionTreeElement::evaluate for
917 * \ref SEL_EXPRESSION elements.
920 _gmx_sel_evaluate_method(gmx_sel_evaluate_t *data,
921 const gmx::SelectionTreeElementPointer &sel,
924 _gmx_sel_evaluate_method_params(data, sel, g);
925 if (sel->flags & SEL_INITFRAME)
927 sel->flags &= ~SEL_INITFRAME;
928 sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
933 gmx_ana_poscalc_update(sel->u.expr.pc, sel->u.expr.pos, g,
934 data->fr, data->pbc);
935 sel->u.expr.method->pupdate(data->top, data->fr, data->pbc,
936 sel->u.expr.pos, &sel->v,
941 sel->u.expr.method->update(data->top, data->fr, data->pbc, g,
942 &sel->v, sel->u.expr.mdata);
947 * \param[in] data Data for the current frame.
948 * \param[in] sel Selection element being evaluated.
949 * \param[in] g Group for which \p sel should be evaluated.
950 * \returns 0 on success, a non-zero error code on error.
952 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
953 * to evaluate any parameter values.
954 * If this is the first time this expression is evaluated for
955 * the frame, sel_framefunc() callback is called if one is provided.
956 * The modifier is then evaluated using sel_updatefunc_pos().
958 * This function is used as gmx::SelectionTreeElement::evaluate for
959 * \ref SEL_MODIFIER elements.
962 _gmx_sel_evaluate_modifier(gmx_sel_evaluate_t *data,
963 const gmx::SelectionTreeElementPointer &sel,
966 _gmx_sel_evaluate_method_params(data, sel, g);
967 if (sel->flags & SEL_INITFRAME)
969 sel->flags &= ~SEL_INITFRAME;
970 sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
973 GMX_RELEASE_ASSERT(sel->child != NULL,
974 "Modifier element with a value must have a child");
975 if (sel->child->v.type != POS_VALUE)
977 GMX_THROW(gmx::NotImplementedError("Non-position valued modifiers not implemented"));
979 sel->u.expr.method->pupdate(data->top, data->fr, data->pbc,
981 &sel->v, sel->u.expr.mdata);
985 /********************************************************************
986 * BOOLEAN EXPRESSION EVALUATION
987 ********************************************************************/
990 * \param[in] data Data for the current frame.
991 * \param[in] sel Selection element being evaluated.
992 * \param[in] g Group for which \p sel should be evaluated.
993 * \returns 0 on success, a non-zero error code on error.
995 * Evaluates the child element (there should be only one) in the group
996 * \p g, and then sets the value of \p sel to the complement of the
999 * This function is used as gmx::SelectionTreeElement::evaluate for
1000 * \ref SEL_BOOLEAN elements with \ref BOOL_NOT.
1003 _gmx_sel_evaluate_not(gmx_sel_evaluate_t *data,
1004 const gmx::SelectionTreeElementPointer &sel,
1007 MempoolSelelemReserver reserver(sel->child, g->isize);
1008 sel->child->evaluate(data, sel->child, g);
1009 gmx_ana_index_difference(sel->v.u.g, g, sel->child->v.u.g);
1013 * \param[in] data Data for the current frame.
1014 * \param[in] sel Selection element being evaluated.
1015 * \param[in] g Group for which \p sel should be evaluated.
1016 * \returns 0 on success, a non-zero error code on error.
1018 * Short-circuiting evaluation of logical AND expressions.
1020 * Starts by evaluating the first child element in the group \p g.
1021 * The each following child element is evaluated in the intersection
1022 * of all the previous values until all children have been evaluated
1023 * or the intersection becomes empty.
1024 * The value of \p sel is set to the intersection of all the (evaluated)
1027 * If the first child does not have an evaluation function, it is skipped
1028 * and the evaluation is started at the second child.
1029 * This happens if the first child is a constant expression and during
1030 * compilation it was detected that the evaluation group is always a subset
1031 * of the constant group
1032 * (currently, the compiler never detects this).
1034 * This function is used as gmx::SelectionTreeElement::evaluate for
1035 * \ref SEL_BOOLEAN elements with \ref BOOL_AND.
1038 _gmx_sel_evaluate_and(gmx_sel_evaluate_t *data,
1039 const gmx::SelectionTreeElementPointer &sel,
1042 SelectionTreeElementPointer child = sel->child;
1043 /* Skip the first child if it does not have an evaluation function. */
1044 if (!child->evaluate)
1046 child = child->next;
1048 /* Evaluate the first child */
1050 MempoolSelelemReserver reserver(child, g->isize);
1051 child->evaluate(data, child, g);
1052 gmx_ana_index_copy(sel->v.u.g, child->v.u.g, false);
1054 child = child->next;
1055 while (child && sel->v.u.g->isize > 0)
1057 MempoolSelelemReserver reserver(child, sel->v.u.g->isize);
1058 child->evaluate(data, child, sel->v.u.g);
1059 gmx_ana_index_intersection(sel->v.u.g, sel->v.u.g, child->v.u.g);
1060 child = child->next;
1065 * \param[in] data Data for the current frame.
1066 * \param[in] sel Selection element being evaluated.
1067 * \param[in] g Group for which \p sel should be evaluated.
1068 * \returns 0 on success, a non-zero error code on error.
1070 * Short-circuiting evaluation of logical OR expressions.
1072 * Starts by evaluating the first child element in the group \p g.
1073 * For each subsequent child, finds the part of \p g that is not
1074 * included the value of any previous child, and evaluates the child
1075 * in that group until the last child is evaluated or all of \p g
1076 * is included in some child value.
1077 * The value of \p sel is set to the union of all the (evaluated)
1080 * If the first child does not have an evaluation function, its value is
1081 * used without evaluation.
1082 * This happens if the first child is a constant expression, the selection
1083 * has been compiled, and the evaluation group is the same for each frame.
1084 * In this case, the compiler has taken care of that the child value is a
1085 * subset of \p g, making it unnecessary to evaluate it.
1087 * This function is used as gmx::SelectionTreeElement::evaluate for
1088 * \ref SEL_BOOLEAN elements with \ref BOOL_OR.
1091 _gmx_sel_evaluate_or(gmx_sel_evaluate_t *data,
1092 const gmx::SelectionTreeElementPointer &sel,
1095 gmx_ana_index_t tmp, tmp2;
1097 SelectionTreeElementPointer child = sel->child;
1098 if (child->evaluate)
1100 MempoolSelelemReserver reserver(child, g->isize);
1101 child->evaluate(data, child, g);
1102 gmx_ana_index_partition(sel->v.u.g, &tmp, g, child->v.u.g);
1106 gmx_ana_index_partition(sel->v.u.g, &tmp, g, child->v.u.g);
1108 child = child->next;
1109 while (child && tmp.isize > 0)
1112 MempoolSelelemReserver reserver(child, tmp.isize);
1113 child->evaluate(data, child, &tmp);
1114 gmx_ana_index_partition(&tmp, &tmp2, &tmp, child->v.u.g);
1116 sel->v.u.g->isize += tmp.isize;
1117 tmp.isize = tmp2.isize;
1118 tmp.index = tmp2.index;
1119 child = child->next;
1121 gmx_ana_index_sort(sel->v.u.g);
1125 /********************************************************************
1126 * ARITHMETIC EVALUATION
1127 ********************************************************************/
1130 * \param[in] data Data for the current frame.
1131 * \param[in] sel Selection element being evaluated.
1132 * \param[in] g Group for which \p sel should be evaluated.
1133 * \returns 0 on success, a non-zero error code on error.
1136 _gmx_sel_evaluate_arithmetic(gmx_sel_evaluate_t *data,
1137 const gmx::SelectionTreeElementPointer &sel,
1141 real lval, rval = 0., val = 0.;
1143 const SelectionTreeElementPointer &left = sel->child;
1144 const SelectionTreeElementPointer &right = left->next;
1146 SelelemTemporaryValueAssigner assigner;
1147 MempoolSelelemReserver reserver;
1150 assigner.assign(left, *sel);
1153 reserver.reserve(right, g->isize);
1156 else if (right && right->mempool)
1158 assigner.assign(right, *sel);
1160 _gmx_sel_evaluate_children(data, sel, g);
1162 n = (sel->flags & SEL_SINGLEVAL) ? 1 : g->isize;
1165 bool bArithNeg = (sel->u.arith.type == ARITH_NEG);
1166 GMX_ASSERT(right || bArithNeg,
1167 "Right operand cannot be null except for negations");
1168 for (i = i1 = i2 = 0; i < n; ++i)
1170 lval = left->v.u.r[i1];
1173 rval = right->v.u.r[i2];
1175 switch (sel->u.arith.type)
1177 case ARITH_PLUS: val = lval + rval; break;
1178 case ARITH_MINUS: val = lval - rval; break;
1179 case ARITH_NEG: val = -lval; break;
1180 case ARITH_MULT: val = lval * rval; break;
1181 case ARITH_DIV: val = lval / rval; break;
1182 case ARITH_EXP: val = pow(lval, rval); break;
1184 sel->v.u.r[i] = val;
1185 if (!(left->flags & SEL_SINGLEVAL))
1189 if (!bArithNeg && !(right->flags & SEL_SINGLEVAL))