3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements functions in evaluate.h.
36 * One of the major bottlenecks for selection performance is that all the
37 * evaluation is carried out for atoms.
38 * There are several cases when the evaluation could be done for residues
39 * or molecules instead, including keywords that select by residue and
40 * cases where residue centers are used as reference positions.
41 * Implementing this would require a mechanism for recognizing whether
42 * something can be evaluated by residue/molecule instead by atom, and
43 * converting selections by residue/molecule into selections by atom
46 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
47 * \ingroup module_selection
55 #include "gromacs/legacyheaders/maths.h"
56 #include "gromacs/legacyheaders/smalloc.h"
57 #include "gromacs/legacyheaders/vec.h"
59 #include "gromacs/selection/indexutil.h"
60 #include "gromacs/selection/poscalc.h"
61 #include "gromacs/selection/selection.h"
62 #include "gromacs/selection/selmethod.h"
63 #include "gromacs/utility/exceptions.h"
64 #include "gromacs/utility/gmxassert.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)
262 else if (evalfunc == &_gmx_sel_evaluate_root)
264 else if (evalfunc == &_gmx_sel_evaluate_static)
265 fprintf(fp, "static");
266 else if (evalfunc == &_gmx_sel_evaluate_subexpr_simple)
267 fprintf(fp, "subexpr_simple");
268 else if (evalfunc == &_gmx_sel_evaluate_subexpr_staticeval)
269 fprintf(fp, "subexpr_staticeval");
270 else if (evalfunc == &_gmx_sel_evaluate_subexpr)
271 fprintf(fp, "subexpr");
272 else if (evalfunc == &_gmx_sel_evaluate_subexprref_simple)
273 fprintf(fp, "ref_simple");
274 else if (evalfunc == &_gmx_sel_evaluate_subexprref)
276 else if (evalfunc == &_gmx_sel_evaluate_method)
277 fprintf(fp, "method");
278 else if (evalfunc == &_gmx_sel_evaluate_modifier)
280 else if (evalfunc == &_gmx_sel_evaluate_not)
282 else if (evalfunc == &_gmx_sel_evaluate_and)
284 else if (evalfunc == &_gmx_sel_evaluate_or)
286 else if (evalfunc == &_gmx_sel_evaluate_arithmetic)
287 fprintf(fp, "arithmetic");
289 fprintf(fp, "%p", (void*)(evalfunc));
293 * \param[out] data Evaluation data structure to initialize.
294 * \param[in] mp Memory pool for intermediate evaluation values.
295 * \param[in] gall Index group with all the atoms.
296 * \param[in] top Topology structure for evaluation.
297 * \param[in] fr New frame for evaluation.
298 * \param[in] pbc New PBC information for evaluation.
301 _gmx_sel_evaluate_init(gmx_sel_evaluate_t *data,
302 gmx_sel_mempool_t *mp, gmx_ana_index_t *gall,
303 t_topology *top, t_trxframe *fr, t_pbc *pbc)
313 * Recursively initializes the flags for evaluation.
315 * \param[in,out] sel Selection element to clear.
317 * The \ref SEL_INITFRAME flag is set for \ref SEL_EXPRESSION elements whose
318 * method defines the \p init_frame callback (see sel_framefunc()), and
319 * cleared for other elements.
321 * The \ref SEL_EVALFRAME flag is cleared for all elements.
324 init_frame_eval(SelectionTreeElementPointer sel)
328 sel->flags &= ~(SEL_INITFRAME | SEL_EVALFRAME);
329 if (sel->type == SEL_EXPRESSION)
331 if (sel->u.expr.method && sel->u.expr.method->init_frame)
333 sel->flags |= SEL_INITFRAME;
336 if (sel->child && sel->type != SEL_SUBEXPRREF)
338 init_frame_eval(sel->child);
347 SelectionEvaluator::SelectionEvaluator()
352 * \param[in,out] coll The selection collection to evaluate.
353 * \param[in] fr Frame for which the evaluation should be carried out.
354 * \param[in] pbc PBC data, or NULL if no PBC should be used.
355 * \returns 0 on successful evaluation, a non-zero error code on error.
357 * This functions sets the global variables for topology, frame and PBC,
358 * clears some information in the selection to initialize the evaluation
359 * for a new frame, and evaluates \p sel and all the selections pointed by
360 * the \p next pointers of \p sel.
362 * This is the only function that user code should call if they want to
363 * evaluate a selection for a new frame.
366 SelectionEvaluator::evaluate(SelectionCollection *coll,
367 t_trxframe *fr, t_pbc *pbc)
369 gmx_ana_selcollection_t *sc = &coll->impl_->sc_;
370 gmx_sel_evaluate_t data;
372 _gmx_sel_evaluate_init(&data, sc->mempool, &sc->gall, sc->top, fr, pbc);
373 init_frame_eval(sc->root);
374 SelectionTreeElementPointer sel = sc->root;
377 /* Clear the evaluation group of subexpressions */
378 if (sel->child && sel->child->type == SEL_SUBEXPR)
380 sel->child->u.cgrp.isize = 0;
381 /* Not strictly necessary, because the value will be overwritten
382 * during first evaluation of the subexpression anyways, but we
383 * clear the group for clarity. Note that this is _not_ done during
384 * compilation because of some additional complexities involved
385 * (see compiler.c), so it should not be relied upon in
386 * _gmx_sel_evaluate_subexpr(). */
387 if (sel->child->v.type == GROUP_VALUE)
389 sel->child->v.u.g->isize = 0;
394 sel->evaluate(&data, sel, NULL);
398 /* Update selection information */
399 SelectionDataList::const_iterator isel;
400 for (isel = sc->sel.begin(); isel != sc->sel.end(); ++isel)
402 internal::SelectionData &sel = **isel;
403 sel.refreshMassesAndCharges();
404 sel.updateCoveredFractionForFrame();
409 * \param[in,out] coll The selection collection to evaluate.
410 * \param[in] nframes Total number of frames.
413 SelectionEvaluator::evaluateFinal(SelectionCollection *coll, int nframes)
415 gmx_ana_selcollection_t *sc = &coll->impl_->sc_;
417 SelectionDataList::const_iterator isel;
418 for (isel = sc->sel.begin(); isel != sc->sel.end(); ++isel)
420 internal::SelectionData &sel = **isel;
421 sel.restoreOriginalPositions();
422 sel.computeAverageCoveredFraction(nframes);
429 * \param[in] data Data for the current frame.
430 * \param[in] sel Selection element being evaluated.
431 * \param[in] g Group for which \p sel should be evaluated.
432 * \returns 0 on success, a non-zero error code on error.
434 * Evaluates each child of \p sel in \p g.
437 _gmx_sel_evaluate_children(gmx_sel_evaluate_t *data,
438 const SelectionTreeElementPointer &sel,
441 SelectionTreeElementPointer child = sel->child;
446 child->evaluate(data, child, g);
453 * \param[in] data Data for the current frame.
454 * \param[in] sel Selection element being evaluated.
455 * \param[in] g Group for which \p sel should be evaluated
456 * (not used, can be NULL).
457 * \returns 0 on success, a non-zero error code on error.
459 * Evaluates the first child element in the group defined by \p sel->u.cgrp.
460 * If \p sel->u.cgrp is empty, nothing is done.
461 * The value of \p sel is not touched (root elements do not evaluate to
464 * This function can be used as gmx::SelectionTreeElement::evaluate for
465 * \ref SEL_ROOT elements.
468 _gmx_sel_evaluate_root(gmx_sel_evaluate_t *data,
469 const SelectionTreeElementPointer &sel,
472 if (sel->u.cgrp.isize == 0 || !sel->child->evaluate)
477 sel->child->evaluate(data, sel->child,
478 sel->u.cgrp.isize < 0 ? NULL : &sel->u.cgrp);
482 * \param[in] data Data for the current frame.
483 * \param[in] sel Selection element being evaluated.
484 * \param[in] g Group for which \p sel should be evaluated.
485 * \returns 0 for success.
487 * Sets the value of \p sel to the intersection of \p g and \p sel->u.cgrp.
489 * This function can be used as gmx::SelectionTreeElement::evaluate for
490 * \ref SEL_CONST elements with value type \ref GROUP_VALUE.
493 _gmx_sel_evaluate_static(gmx_sel_evaluate_t *data,
494 const SelectionTreeElementPointer &sel,
497 gmx_ana_index_intersection(sel->v.u.g, &sel->u.cgrp, g);
501 /*********************************************************************
502 * SUBEXPRESSION EVALUATION
503 *********************************************************************/
506 * \param[in] data Data for the current frame.
507 * \param[in] sel Selection element being evaluated.
508 * \param[in] g Group for which \p sel should be evaluated.
509 * \returns 0 on success, a non-zero error code on error.
511 * Evaluates the child element (there should be exactly one) in \p g.
512 * The compiler has taken care that the child actually stores the evaluated
513 * value in the value pointer of this element.
515 * This function is used as gmx::SelectionTreeElement::evaluate for
516 * \ref SEL_SUBEXPR elements that are used only once, and hence do not need
517 * full subexpression handling.
520 _gmx_sel_evaluate_subexpr_simple(gmx_sel_evaluate_t *data,
521 const SelectionTreeElementPointer &sel,
524 if (sel->child->evaluate)
526 sel->child->evaluate(data, sel->child, g);
528 sel->v.nr = sel->child->v.nr;
532 * \param[in] data Data for the current frame.
533 * \param[in] sel Selection element being evaluated.
534 * \param[in] g Group for which \p sel should be evaluated.
535 * \returns 0 on success, a non-zero error code on error.
537 * If this is the first call for this frame, evaluates the child element
538 * there should be exactly one in \p g.
539 * The compiler has taken care that the child actually stores the evaluated
540 * value in the value pointer of this element.
541 * Assumes that \p g is persistent for the duration of the whole evaluation.
543 * This function is used as gmx::SelectionTreeElement::evaluate for
544 * \ref SEL_SUBEXPR elements that have a static evaluation group, and hence do
545 * not need full subexpression handling.
548 _gmx_sel_evaluate_subexpr_staticeval(gmx_sel_evaluate_t *data,
549 const SelectionTreeElementPointer &sel,
552 if (sel->u.cgrp.isize == 0)
554 sel->child->evaluate(data, sel->child, g);
555 sel->v.nr = sel->child->v.nr;
558 sel->u.cgrp.isize = -1;
562 gmx_ana_index_set(&sel->u.cgrp, g->isize, g->index, sel->u.cgrp.name, 0);
568 * \param[in] data Data for the current frame.
569 * \param[in] sel Selection element being evaluated.
570 * \param[in] g Group for which \p sel should be evaluated.
571 * \returns 0 on success, a non-zero error code on error.
573 * Finds the part of \p g for which the subexpression
574 * has not yet been evaluated by comparing \p g to \p sel->u.cgrp.
575 * If the part is not empty, the child expression is evaluated for this
576 * part, and the results merged to the old values of the child.
577 * The value of \p sel itself is undefined after the call.
580 * The call to gmx_ana_index_difference() can take quite a lot of unnecessary
581 * time if the subexpression is evaluated either several times for the same
582 * group or for completely distinct groups.
583 * However, in the majority of cases, these situations occur when
584 * _gmx_sel_evaluate_subexpr_staticeval() can be used, so this should not be a
588 _gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t *data,
589 const SelectionTreeElementPointer &sel,
592 gmx_ana_index_t gmiss;
594 MempoolGroupReserver gmissreserver(data->mp);
595 if (sel->u.cgrp.isize == 0)
598 SelelemTemporaryValueAssigner assigner(sel->child, *sel);
599 sel->child->evaluate(data, sel->child, g);
601 /* We need to keep the name for the cgrp across the copy to avoid
602 * problems if g has a name set. */
603 char *name = sel->u.cgrp.name;
604 gmx_ana_index_copy(&sel->u.cgrp, g, false);
605 sel->u.cgrp.name = name;
610 gmissreserver.reserve(&gmiss, g->isize);
611 gmx_ana_index_difference(&gmiss, g, &sel->u.cgrp);
616 MempoolSelelemReserver reserver(sel->child, gmiss.isize);
617 /* Evaluate the missing values for the child */
618 sel->child->evaluate(data, sel->child, &gmiss);
619 /* Merge the missing values to the existing ones. */
620 if (sel->v.type == GROUP_VALUE)
622 gmx_ana_index_merge(sel->v.u.g, sel->child->v.u.g, sel->v.u.g);
628 i = sel->u.cgrp.isize - 1;
630 /* TODO: This switch is kind of ugly, but it may be difficult to
631 * do this portably without C++ templates. */
635 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
637 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
639 sel->v.u.i[k] = sel->child->v.u.i[j--];
643 sel->v.u.i[k] = sel->v.u.i[i--];
649 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
651 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
653 sel->v.u.r[k] = sel->child->v.u.r[j--];
657 sel->v.u.r[k] = sel->v.u.r[i--];
663 // Note: with the currently allowed syntax, this case is never
665 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
667 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
669 sel->v.u.s[k] = sel->child->v.u.s[j--];
673 sel->v.u.s[k] = sel->v.u.s[i--];
679 /* TODO: Implement this */
680 GMX_THROW(gmx::NotImplementedError("position subexpressions not implemented properly"));
684 GMX_THROW(gmx::InternalError("Invalid subexpression type"));
687 gmx_ana_index_merge(&sel->u.cgrp, &sel->u.cgrp, &gmiss);
692 * \param[in] data Data for the current frame.
693 * \param[in] sel Selection element being evaluated.
694 * \param[in] g Group for which \p sel should be evaluated.
695 * \returns 0 for success.
697 * Sets the value pointers of the child and its child to point to the same
698 * memory as the value pointer of this element to avoid copying, and then
699 * evaluates evaluates the child.
701 * This function is used as gmx::SelectionTreeElement:evaluate for
702 * \ref SEL_SUBEXPRREF elements for which the \ref SEL_SUBEXPR does not have
706 _gmx_sel_evaluate_subexprref_simple(gmx_sel_evaluate_t *data,
707 const SelectionTreeElementPointer &sel,
712 _gmx_selvalue_setstore(&sel->child->v, sel->v.u.ptr);
713 _gmx_selvalue_setstore_alloc(&sel->child->child->v, sel->v.u.ptr,
714 sel->child->child->v.nalloc);
715 sel->child->evaluate(data, sel->child, g);
717 sel->v.nr = sel->child->v.nr;
720 sel->u.param->val.nr = sel->v.nr;
721 if (sel->u.param->nvalptr)
723 *sel->u.param->nvalptr = sel->u.param->val.nr;
729 * \param[in] data Data for the current frame.
730 * \param[in] sel Selection element being evaluated.
731 * \param[in] g Group for which \p sel should be evaluated.
732 * \returns 0 on success, a non-zero error code on error.
734 * If the value type is \ref POS_VALUE, the value of the child is simply
735 * copied to set the value of \p sel (the child subexpression should
736 * already have been evaluated by its root).
737 * If the value type is something else, the child is evaluated for the
738 * group \p g, and the value of the child is then copied.
739 * There should be only one child element.
741 * This function is used as gmx::SelectionTreeElement::evaluate for
742 * \ref SEL_SUBEXPRREF elements.
745 _gmx_sel_evaluate_subexprref(gmx_sel_evaluate_t *data,
746 const SelectionTreeElementPointer &sel,
753 sel->child->evaluate(data, sel->child, g);
755 const SelectionTreeElementPointer &expr = sel->child;
761 sel->v.nr = expr->v.nr;
762 memcpy(sel->v.u.i, expr->v.u.i, sel->v.nr*sizeof(*sel->v.u.i));
766 sel->v.nr = g->isize;
767 /* Extract the values corresponding to g */
768 for (i = j = 0; i < g->isize; ++i, ++j)
770 while (sel->child->u.cgrp.index[j] < g->index[i])
774 sel->v.u.i[i] = expr->v.u.i[j];
782 sel->v.nr = expr->v.nr;
783 memcpy(sel->v.u.r, expr->v.u.r, sel->v.nr*sizeof(*sel->v.u.r));
787 sel->v.nr = g->isize;
788 /* Extract the values corresponding to g */
789 for (i = j = 0; i < g->isize; ++i, ++j)
791 while (sel->child->u.cgrp.index[j] < g->index[i])
795 sel->v.u.r[i] = expr->v.u.r[j];
803 sel->v.nr = expr->v.nr;
804 memcpy(sel->v.u.s, expr->v.u.s, sel->v.nr*sizeof(*sel->v.u.s));
808 sel->v.nr = g->isize;
809 /* Extract the values corresponding to g */
810 for (i = j = 0; i < g->isize; ++i, ++j)
812 while (sel->child->u.cgrp.index[j] < g->index[i])
816 sel->v.u.s[i] = expr->v.u.s[j];
822 /* Currently, there is no need to do anything fancy here,
823 * but some future extensions may need a more flexible
825 gmx_ana_pos_copy(sel->v.u.p, expr->v.u.p, false);
831 gmx_ana_index_copy(sel->v.u.g, expr->v.u.g, false);
835 gmx_ana_index_intersection(sel->v.u.g, expr->v.u.g, g);
839 default: /* should not be reached */
840 GMX_THROW(gmx::InternalError("Invalid subexpression reference type"));
842 /* Store the number of values if needed */
845 sel->u.param->val.nr = sel->v.nr;
846 if (sel->u.param->nvalptr)
848 *sel->u.param->nvalptr = sel->u.param->val.nr;
853 /********************************************************************
854 * METHOD EXPRESSION EVALUATION
855 ********************************************************************/
858 * \param[in] data Data for the current frame.
859 * \param[in] sel Selection element being evaluated.
860 * \param[in] g Group for which \p sel should be evaluated.
861 * \returns 0 on success, a non-zero error code on error.
863 * Evaluates each child of a \ref SEL_EXPRESSION element.
864 * The value of \p sel is not touched.
866 * This function is not used as gmx::SelectionTreeElement::evaluate,
867 * but is used internally.
870 _gmx_sel_evaluate_method_params(gmx_sel_evaluate_t *data,
871 const SelectionTreeElementPointer &sel,
874 SelectionTreeElementPointer child = sel->child;
877 if (child->evaluate && !(child->flags & SEL_EVALFRAME))
879 if (child->flags & SEL_ATOMVAL)
881 child->evaluate(data, child, g);
885 child->flags |= SEL_EVALFRAME;
886 child->evaluate(data, child, NULL);
894 * \param[in] data Data for the current frame.
895 * \param[in] sel Selection element being evaluated.
896 * \param[in] g Group for which \p sel should be evaluated.
897 * \returns 0 on success, a non-zero error code on error.
899 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
900 * to evaluate any parameter values.
901 * If this is the first time this expression is evaluated for
902 * the frame, sel_framefunc() callback is called if one is provided.
903 * If a reference position calculation has been initialized for this element,
904 * the positions are also updated, and sel_updatefunc_pos() is used to
905 * evaluate the value. Otherwise, sel_updatefunc() is used.
907 * This function is used as gmx::SelectionTreeElement::evaluate for
908 * \ref SEL_EXPRESSION elements.
911 _gmx_sel_evaluate_method(gmx_sel_evaluate_t *data,
912 const SelectionTreeElementPointer &sel,
915 _gmx_sel_evaluate_method_params(data, sel, g);
916 if (sel->flags & SEL_INITFRAME)
918 sel->flags &= ~SEL_INITFRAME;
919 sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
924 gmx_ana_poscalc_update(sel->u.expr.pc, sel->u.expr.pos, g,
925 data->fr, data->pbc);
926 sel->u.expr.method->pupdate(data->top, data->fr, data->pbc,
927 sel->u.expr.pos, &sel->v,
932 sel->u.expr.method->update(data->top, data->fr, data->pbc, g,
933 &sel->v, sel->u.expr.mdata);
938 * \param[in] data Data for the current frame.
939 * \param[in] sel Selection element being evaluated.
940 * \param[in] g Group for which \p sel should be evaluated.
941 * \returns 0 on success, a non-zero error code on error.
943 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
944 * to evaluate any parameter values.
945 * If this is the first time this expression is evaluated for
946 * the frame, sel_framefunc() callback is called if one is provided.
947 * The modifier is then evaluated using sel_updatefunc_pos().
949 * This function is used as gmx::SelectionTreeElement::evaluate for
950 * \ref SEL_MODIFIER elements.
953 _gmx_sel_evaluate_modifier(gmx_sel_evaluate_t *data,
954 const SelectionTreeElementPointer &sel,
957 _gmx_sel_evaluate_method_params(data, sel, g);
958 if (sel->flags & SEL_INITFRAME)
960 sel->flags &= ~SEL_INITFRAME;
961 sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
964 GMX_RELEASE_ASSERT(sel->child != NULL,
965 "Modifier element with a value must have a child");
966 if (sel->child->v.type != POS_VALUE)
968 GMX_THROW(gmx::NotImplementedError("Non-position valued modifiers not implemented"));
970 sel->u.expr.method->pupdate(data->top, data->fr, data->pbc,
972 &sel->v, sel->u.expr.mdata);
976 /********************************************************************
977 * BOOLEAN EXPRESSION EVALUATION
978 ********************************************************************/
981 * \param[in] data Data for the current frame.
982 * \param[in] sel Selection element being evaluated.
983 * \param[in] g Group for which \p sel should be evaluated.
984 * \returns 0 on success, a non-zero error code on error.
986 * Evaluates the child element (there should be only one) in the group
987 * \p g, and then sets the value of \p sel to the complement of the
990 * This function is used as gmx::SelectionTreeElement::evaluate for
991 * \ref SEL_BOOLEAN elements with \ref BOOL_NOT.
994 _gmx_sel_evaluate_not(gmx_sel_evaluate_t *data,
995 const SelectionTreeElementPointer &sel,
998 MempoolSelelemReserver reserver(sel->child, g->isize);
999 sel->child->evaluate(data, sel->child, g);
1000 gmx_ana_index_difference(sel->v.u.g, g, sel->child->v.u.g);
1004 * \param[in] data Data for the current frame.
1005 * \param[in] sel Selection element being evaluated.
1006 * \param[in] g Group for which \p sel should be evaluated.
1007 * \returns 0 on success, a non-zero error code on error.
1009 * Short-circuiting evaluation of logical AND expressions.
1011 * Starts by evaluating the first child element in the group \p g.
1012 * The each following child element is evaluated in the intersection
1013 * of all the previous values until all children have been evaluated
1014 * or the intersection becomes empty.
1015 * The value of \p sel is set to the intersection of all the (evaluated)
1018 * If the first child does not have an evaluation function, it is skipped
1019 * and the evaluation is started at the second child.
1020 * This happens if the first child is a constant expression and during
1021 * compilation it was detected that the evaluation group is always a subset
1022 * of the constant group
1023 * (currently, the compiler never detects this).
1025 * This function is used as gmx::SelectionTreeElement::evaluate for
1026 * \ref SEL_BOOLEAN elements with \ref BOOL_AND.
1029 _gmx_sel_evaluate_and(gmx_sel_evaluate_t *data,
1030 const SelectionTreeElementPointer &sel,
1033 SelectionTreeElementPointer child = sel->child;
1034 /* Skip the first child if it does not have an evaluation function. */
1035 if (!child->evaluate)
1037 child = child->next;
1039 /* Evaluate the first child */
1041 MempoolSelelemReserver reserver(child, g->isize);
1042 child->evaluate(data, child, g);
1043 gmx_ana_index_copy(sel->v.u.g, child->v.u.g, false);
1045 child = child->next;
1046 while (child && sel->v.u.g->isize > 0)
1048 MempoolSelelemReserver reserver(child, sel->v.u.g->isize);
1049 child->evaluate(data, child, sel->v.u.g);
1050 gmx_ana_index_intersection(sel->v.u.g, sel->v.u.g, child->v.u.g);
1051 child = child->next;
1056 * \param[in] data Data for the current frame.
1057 * \param[in] sel Selection element being evaluated.
1058 * \param[in] g Group for which \p sel should be evaluated.
1059 * \returns 0 on success, a non-zero error code on error.
1061 * Short-circuiting evaluation of logical OR expressions.
1063 * Starts by evaluating the first child element in the group \p g.
1064 * For each subsequent child, finds the part of \p g that is not
1065 * included the value of any previous child, and evaluates the child
1066 * in that group until the last child is evaluated or all of \p g
1067 * is included in some child value.
1068 * The value of \p sel is set to the union of all the (evaluated)
1071 * If the first child does not have an evaluation function, its value is
1072 * used without evaluation.
1073 * This happens if the first child is a constant expression, the selection
1074 * has been compiled, and the evaluation group is the same for each frame.
1075 * In this case, the compiler has taken care of that the child value is a
1076 * subset of \p g, making it unnecessary to evaluate it.
1078 * This function is used as gmx::SelectionTreeElement::evaluate for
1079 * \ref SEL_BOOLEAN elements with \ref BOOL_OR.
1082 _gmx_sel_evaluate_or(gmx_sel_evaluate_t *data,
1083 const SelectionTreeElementPointer &sel,
1086 gmx_ana_index_t tmp, tmp2;
1088 SelectionTreeElementPointer child = sel->child;
1089 if (child->evaluate)
1091 MempoolSelelemReserver reserver(child, g->isize);
1092 child->evaluate(data, child, g);
1093 gmx_ana_index_partition(sel->v.u.g, &tmp, g, child->v.u.g);
1097 gmx_ana_index_partition(sel->v.u.g, &tmp, g, child->v.u.g);
1099 child = child->next;
1100 while (child && tmp.isize > 0)
1104 MempoolSelelemReserver reserver(child, tmp.isize);
1105 child->evaluate(data, child, &tmp);
1106 gmx_ana_index_partition(&tmp, &tmp2, &tmp, child->v.u.g);
1108 sel->v.u.g->isize += tmp.isize;
1109 tmp.isize = tmp2.isize;
1110 tmp.index = tmp2.index;
1111 child = child->next;
1113 gmx_ana_index_sort(sel->v.u.g);
1117 /********************************************************************
1118 * ARITHMETIC EVALUATION
1119 ********************************************************************/
1122 * \param[in] data Data for the current frame.
1123 * \param[in] sel Selection element being evaluated.
1124 * \param[in] g Group for which \p sel should be evaluated.
1125 * \returns 0 on success, a non-zero error code on error.
1128 _gmx_sel_evaluate_arithmetic(gmx_sel_evaluate_t *data,
1129 const SelectionTreeElementPointer &sel,
1133 real lval, rval=0., val=0.;
1135 const SelectionTreeElementPointer &left = sel->child;
1136 const SelectionTreeElementPointer &right = left->next;
1138 SelelemTemporaryValueAssigner assigner;
1139 MempoolSelelemReserver reserver;
1142 assigner.assign(left, *sel);
1145 reserver.reserve(right, g->isize);
1148 else if (right && right->mempool)
1150 assigner.assign(right, *sel);
1152 _gmx_sel_evaluate_children(data, sel, g);
1154 n = (sel->flags & SEL_SINGLEVAL) ? 1 : g->isize;
1157 bool bArithNeg = (sel->u.arith.type == ARITH_NEG);
1158 GMX_ASSERT(right || bArithNeg,
1159 "Right operand cannot be null except for negations");
1160 for (i = i1 = i2 = 0; i < n; ++i)
1162 lval = left->v.u.r[i1];
1165 rval = right->v.u.r[i2];
1167 switch (sel->u.arith.type)
1169 case ARITH_PLUS: val = lval + rval; break;
1170 case ARITH_MINUS: val = lval - rval; break;
1171 case ARITH_NEG: val = -lval; break;
1172 case ARITH_MULT: val = lval * rval; break;
1173 case ARITH_DIV: val = lval / rval; break;
1174 case ARITH_EXP: val = pow(lval, rval); break;
1176 sel->v.u.r[i] = val;
1177 if (!(left->flags & SEL_SINGLEVAL))
1181 if (!bArithNeg && !(right->flags & SEL_SINGLEVAL))