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
51 #include "gromacs/legacyheaders/maths.h"
52 #include "gromacs/legacyheaders/smalloc.h"
53 #include "gromacs/legacyheaders/vec.h"
55 #include "gromacs/selection/indexutil.h"
56 #include "gromacs/selection/poscalc.h"
57 #include "gromacs/selection/selection.h"
58 #include "gromacs/selection/selmethod.h"
59 #include "gromacs/utility/exceptions.h"
60 #include "gromacs/utility/gmxassert.h"
64 #include "selectioncollection-impl.h"
67 using gmx::SelectionTreeElement;
68 using gmx::SelectionTreeElementPointer;
74 * Reserves memory for a selection element from the evaluation memory pool.
76 * This class implements RAII semantics for allocating memory for selection
77 * element values from a selection evaluation memory pool.
79 * \ingroup module_selection
81 class MempoolSelelemReserver
84 //! Constructs a reserver without initial reservation.
85 MempoolSelelemReserver() {}
87 * Constructs a reserver with initial reservation.
89 * \param[in,out] sel Selection element for which to reserve.
90 * \param[in] count Number of values to reserve.
94 MempoolSelelemReserver(const SelectionTreeElementPointer &sel, int count)
98 //! Frees any memory allocated using this reserver.
99 ~MempoolSelelemReserver()
103 sel_->mempoolRelease();
108 * Reserves memory for selection element values using this reserver.
110 * \param[in,out] sel Selection element for which to reserve.
111 * \param[in] count Number of values to reserve.
113 * Allocates space to store \p count output values in \p sel from the
114 * memory pool associated with \p sel, or from the heap if there is no
115 * memory pool. Type of values to allocate is automatically determined
118 void reserve(const SelectionTreeElementPointer &sel, int count)
120 GMX_RELEASE_ASSERT(!sel_,
121 "Can only reserve one element with one instance");
122 sel->mempoolReserve(count);
127 SelectionTreeElementPointer sel_;
131 * Reserves memory for an index group from the evaluation memory pool.
133 * This class implements RAII semantics for allocating memory for an index
134 * group from a selection evaluation memory pool.
136 * \ingroup module_selection
138 class MempoolGroupReserver
142 * Creates a reserver associated with a given memory pool.
144 * \param mp Memory pool from which to reserve memory.
146 explicit MempoolGroupReserver(gmx_sel_mempool_t *mp)
150 //! Frees any memory allocated using this reserver.
151 ~MempoolGroupReserver()
155 _gmx_sel_mempool_free_group(mp_, g_);
160 * Reserves memory for an index group using this reserver.
162 * \param[in,out] g Index group to reserve.
163 * \param[in] count Number of atoms to reserve space for.
165 * Allocates memory from the memory pool to store \p count atoms in
168 void reserve(gmx_ana_index_t *g, int count)
170 GMX_RELEASE_ASSERT(g_ == NULL, "Can only reserve one element with one instance");
171 _gmx_sel_mempool_alloc_group(mp_, g, count);
176 gmx_sel_mempool_t *mp_;
181 * Assigns a temporary value for a selection element.
183 * This class implements RAII semantics for temporarily assigning the value
184 * pointer of a selection element to point to a different location.
186 * \ingroup module_selection
188 class SelelemTemporaryValueAssigner
191 //! Constructs an assigner without an initial assignment.
192 SelelemTemporaryValueAssigner()
193 : old_ptr_(NULL), old_nalloc_(0)
197 * Constructs an assigner with an initial assignment.
199 * \param[in,out] sel Selection element for which to assign.
200 * \param[in] vsource Element to which \p sel values will point to.
204 SelelemTemporaryValueAssigner(const SelectionTreeElementPointer &sel,
205 const SelectionTreeElement &vsource)
207 assign(sel, vsource);
209 //! Undoes any temporary assignment done using this assigner.
210 ~SelelemTemporaryValueAssigner()
214 _gmx_selvalue_setstore_alloc(&sel_->v, old_ptr_, old_nalloc_);
219 * Assigns a temporary value pointer.
221 * \param[in,out] sel Selection element for which to assign.
222 * \param[in] vsource Element to which \p sel values will point to.
224 * Assigns the value pointer in \p sel to point to the values in
225 * \p vsource, i.e., any access/modification to values in \p sel
226 * actually accesses values in \p vsource.
228 void assign(const SelectionTreeElementPointer &sel,
229 const SelectionTreeElement &vsource)
231 GMX_RELEASE_ASSERT(!sel_,
232 "Can only assign one element with one instance");
233 GMX_RELEASE_ASSERT(sel->v.type == vsource.v.type,
234 "Mismatching selection value types");
235 old_ptr_ = sel->v.u.ptr;
236 old_nalloc_ = sel->v.nalloc;
237 _gmx_selvalue_setstore(&sel->v, vsource.v.u.ptr);
242 SelectionTreeElementPointer sel_;
250 * \param[in] fp File handle to receive the output.
251 * \param[in] evalfunc Function pointer to print.
254 _gmx_sel_print_evalfunc_name(FILE *fp, gmx::sel_evalfunc evalfunc)
258 else if (evalfunc == &_gmx_sel_evaluate_root)
260 else if (evalfunc == &_gmx_sel_evaluate_static)
261 fprintf(fp, "static");
262 else if (evalfunc == &_gmx_sel_evaluate_subexpr_simple)
263 fprintf(fp, "subexpr_simple");
264 else if (evalfunc == &_gmx_sel_evaluate_subexpr_staticeval)
265 fprintf(fp, "subexpr_staticeval");
266 else if (evalfunc == &_gmx_sel_evaluate_subexpr)
267 fprintf(fp, "subexpr");
268 else if (evalfunc == &_gmx_sel_evaluate_subexprref_simple)
269 fprintf(fp, "ref_simple");
270 else if (evalfunc == &_gmx_sel_evaluate_subexprref)
272 else if (evalfunc == &_gmx_sel_evaluate_method)
273 fprintf(fp, "method");
274 else if (evalfunc == &_gmx_sel_evaluate_modifier)
276 else if (evalfunc == &_gmx_sel_evaluate_not)
278 else if (evalfunc == &_gmx_sel_evaluate_and)
280 else if (evalfunc == &_gmx_sel_evaluate_or)
282 else if (evalfunc == &_gmx_sel_evaluate_arithmetic)
283 fprintf(fp, "arithmetic");
285 fprintf(fp, "%p", (void*)(evalfunc));
289 * \param[out] data Evaluation data structure to initialize.
290 * \param[in] mp Memory pool for intermediate evaluation values.
291 * \param[in] gall Index group with all the atoms.
292 * \param[in] top Topology structure for evaluation.
293 * \param[in] fr New frame for evaluation.
294 * \param[in] pbc New PBC information for evaluation.
297 _gmx_sel_evaluate_init(gmx_sel_evaluate_t *data,
298 gmx_sel_mempool_t *mp, gmx_ana_index_t *gall,
299 t_topology *top, t_trxframe *fr, t_pbc *pbc)
309 * Recursively initializes the flags for evaluation.
311 * \param[in,out] sel Selection element to clear.
313 * The \ref SEL_INITFRAME flag is set for \ref SEL_EXPRESSION elements whose
314 * method defines the \p init_frame callback (see sel_framefunc()), and
315 * cleared for other elements.
317 * The \ref SEL_EVALFRAME flag is cleared for all elements.
320 init_frame_eval(SelectionTreeElementPointer sel)
324 sel->flags &= ~(SEL_INITFRAME | SEL_EVALFRAME);
325 if (sel->type == SEL_EXPRESSION)
327 if (sel->u.expr.method && sel->u.expr.method->init_frame)
329 sel->flags |= SEL_INITFRAME;
332 if (sel->child && sel->type != SEL_SUBEXPRREF)
334 init_frame_eval(sel->child);
343 SelectionEvaluator::SelectionEvaluator()
348 * \param[in,out] coll The selection collection to evaluate.
349 * \param[in] fr Frame for which the evaluation should be carried out.
350 * \param[in] pbc PBC data, or NULL if no PBC should be used.
351 * \returns 0 on successful evaluation, a non-zero error code on error.
353 * This functions sets the global variables for topology, frame and PBC,
354 * clears some information in the selection to initialize the evaluation
355 * for a new frame, and evaluates \p sel and all the selections pointed by
356 * the \p next pointers of \p sel.
358 * This is the only function that user code should call if they want to
359 * evaluate a selection for a new frame.
362 SelectionEvaluator::evaluate(SelectionCollection *coll,
363 t_trxframe *fr, t_pbc *pbc)
365 gmx_ana_selcollection_t *sc = &coll->impl_->sc_;
366 gmx_sel_evaluate_t data;
368 _gmx_sel_evaluate_init(&data, sc->mempool, &sc->gall, sc->top, fr, pbc);
369 init_frame_eval(sc->root);
370 SelectionTreeElementPointer sel = sc->root;
373 /* Clear the evaluation group of subexpressions */
374 if (sel->child && sel->child->type == SEL_SUBEXPR)
376 sel->child->u.cgrp.isize = 0;
377 /* Not strictly necessary, because the value will be overwritten
378 * during first evaluation of the subexpression anyways, but we
379 * clear the group for clarity. Note that this is _not_ done during
380 * compilation because of some additional complexities involved
381 * (see compiler.c), so it should not be relied upon in
382 * _gmx_sel_evaluate_subexpr(). */
383 if (sel->child->v.type == GROUP_VALUE)
385 sel->child->v.u.g->isize = 0;
390 sel->evaluate(&data, sel, NULL);
394 /* Update selection information */
395 SelectionDataList::const_iterator isel;
396 for (isel = sc->sel.begin(); isel != sc->sel.end(); ++isel)
398 internal::SelectionData &sel = **isel;
399 sel.refreshMassesAndCharges();
400 sel.updateCoveredFractionForFrame();
405 * \param[in,out] coll The selection collection to evaluate.
406 * \param[in] nframes Total number of frames.
409 SelectionEvaluator::evaluateFinal(SelectionCollection *coll, int nframes)
411 gmx_ana_selcollection_t *sc = &coll->impl_->sc_;
413 SelectionDataList::const_iterator isel;
414 for (isel = sc->sel.begin(); isel != sc->sel.end(); ++isel)
416 internal::SelectionData &sel = **isel;
417 sel.restoreOriginalPositions();
418 sel.computeAverageCoveredFraction(nframes);
425 * \param[in] data Data for the current frame.
426 * \param[in] sel Selection element being evaluated.
427 * \param[in] g Group for which \p sel should be evaluated.
428 * \returns 0 on success, a non-zero error code on error.
430 * Evaluates each child of \p sel in \p g.
433 _gmx_sel_evaluate_children(gmx_sel_evaluate_t *data,
434 const SelectionTreeElementPointer &sel,
437 SelectionTreeElementPointer child = sel->child;
442 child->evaluate(data, child, g);
449 * \param[in] data Data for the current frame.
450 * \param[in] sel Selection element being evaluated.
451 * \param[in] g Group for which \p sel should be evaluated
452 * (not used, can be NULL).
453 * \returns 0 on success, a non-zero error code on error.
455 * Evaluates the first child element in the group defined by \p sel->u.cgrp.
456 * If \p sel->u.cgrp is empty, nothing is done.
457 * The value of \p sel is not touched (root elements do not evaluate to
460 * This function can be used as gmx::SelectionTreeElement::evaluate for
461 * \ref SEL_ROOT elements.
464 _gmx_sel_evaluate_root(gmx_sel_evaluate_t *data,
465 const SelectionTreeElementPointer &sel,
468 if (sel->u.cgrp.isize == 0 || !sel->child->evaluate)
473 sel->child->evaluate(data, sel->child,
474 sel->u.cgrp.isize < 0 ? NULL : &sel->u.cgrp);
478 * \param[in] data Data for the current frame.
479 * \param[in] sel Selection element being evaluated.
480 * \param[in] g Group for which \p sel should be evaluated.
481 * \returns 0 for success.
483 * Sets the value of \p sel to the intersection of \p g and \p sel->u.cgrp.
485 * This function can be used as gmx::SelectionTreeElement::evaluate for
486 * \ref SEL_CONST elements with value type \ref GROUP_VALUE.
489 _gmx_sel_evaluate_static(gmx_sel_evaluate_t *data,
490 const SelectionTreeElementPointer &sel,
493 gmx_ana_index_intersection(sel->v.u.g, &sel->u.cgrp, g);
497 /*********************************************************************
498 * SUBEXPRESSION EVALUATION
499 *********************************************************************/
502 * \param[in] data Data for the current frame.
503 * \param[in] sel Selection element being evaluated.
504 * \param[in] g Group for which \p sel should be evaluated.
505 * \returns 0 on success, a non-zero error code on error.
507 * Evaluates the child element (there should be exactly one) in \p g.
508 * The compiler has taken care that the child actually stores the evaluated
509 * value in the value pointer of this element.
511 * This function is used as gmx::SelectionTreeElement::evaluate for
512 * \ref SEL_SUBEXPR elements that are used only once, and hence do not need
513 * full subexpression handling.
516 _gmx_sel_evaluate_subexpr_simple(gmx_sel_evaluate_t *data,
517 const SelectionTreeElementPointer &sel,
520 if (sel->child->evaluate)
522 sel->child->evaluate(data, sel->child, g);
524 sel->v.nr = sel->child->v.nr;
528 * \param[in] data Data for the current frame.
529 * \param[in] sel Selection element being evaluated.
530 * \param[in] g Group for which \p sel should be evaluated.
531 * \returns 0 on success, a non-zero error code on error.
533 * If this is the first call for this frame, evaluates the child element
534 * there should be exactly one in \p g.
535 * The compiler has taken care that the child actually stores the evaluated
536 * value in the value pointer of this element.
537 * Assumes that \p g is persistent for the duration of the whole evaluation.
539 * This function is used as gmx::SelectionTreeElement::evaluate for
540 * \ref SEL_SUBEXPR elements that have a static evaluation group, and hence do
541 * not need full subexpression handling.
544 _gmx_sel_evaluate_subexpr_staticeval(gmx_sel_evaluate_t *data,
545 const SelectionTreeElementPointer &sel,
548 if (sel->u.cgrp.isize == 0)
550 sel->child->evaluate(data, sel->child, g);
551 sel->v.nr = sel->child->v.nr;
554 sel->u.cgrp.isize = -1;
558 gmx_ana_index_set(&sel->u.cgrp, g->isize, g->index, sel->u.cgrp.name, 0);
564 * \param[in] data Data for the current frame.
565 * \param[in] sel Selection element being evaluated.
566 * \param[in] g Group for which \p sel should be evaluated.
567 * \returns 0 on success, a non-zero error code on error.
569 * Finds the part of \p g for which the subexpression
570 * has not yet been evaluated by comparing \p g to \p sel->u.cgrp.
571 * If the part is not empty, the child expression is evaluated for this
572 * part, and the results merged to the old values of the child.
573 * The value of \p sel itself is undefined after the call.
576 * The call to gmx_ana_index_difference() can take quite a lot of unnecessary
577 * time if the subexpression is evaluated either several times for the same
578 * group or for completely distinct groups.
579 * However, in the majority of cases, these situations occur when
580 * _gmx_sel_evaluate_subexpr_staticeval() can be used, so this should not be a
584 _gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t *data,
585 const SelectionTreeElementPointer &sel,
588 gmx_ana_index_t gmiss;
590 MempoolGroupReserver gmissreserver(data->mp);
591 if (sel->u.cgrp.isize == 0)
594 SelelemTemporaryValueAssigner assigner(sel->child, *sel);
595 sel->child->evaluate(data, sel->child, g);
597 /* We need to keep the name for the cgrp across the copy to avoid
598 * problems if g has a name set. */
599 char *name = sel->u.cgrp.name;
600 gmx_ana_index_copy(&sel->u.cgrp, g, false);
601 sel->u.cgrp.name = name;
606 gmissreserver.reserve(&gmiss, g->isize);
607 gmx_ana_index_difference(&gmiss, g, &sel->u.cgrp);
612 MempoolSelelemReserver reserver(sel->child, gmiss.isize);
613 /* Evaluate the missing values for the child */
614 sel->child->evaluate(data, sel->child, &gmiss);
615 /* Merge the missing values to the existing ones. */
616 if (sel->v.type == GROUP_VALUE)
618 gmx_ana_index_merge(sel->v.u.g, sel->child->v.u.g, sel->v.u.g);
624 i = sel->u.cgrp.isize - 1;
626 /* TODO: This switch is kind of ugly, but it may be difficult to
627 * do this portably without C++ templates. */
631 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
633 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
635 sel->v.u.i[k] = sel->child->v.u.i[j--];
639 sel->v.u.i[k] = sel->v.u.i[i--];
645 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
647 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
649 sel->v.u.r[k] = sel->child->v.u.r[j--];
653 sel->v.u.r[k] = sel->v.u.r[i--];
659 // Note: with the currently allowed syntax, this case is never
661 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
663 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
665 sel->v.u.s[k] = sel->child->v.u.s[j--];
669 sel->v.u.s[k] = sel->v.u.s[i--];
675 /* TODO: Implement this */
676 GMX_THROW(gmx::NotImplementedError("position subexpressions not implemented properly"));
680 GMX_THROW(gmx::InternalError("Invalid subexpression type"));
683 gmx_ana_index_merge(&sel->u.cgrp, &sel->u.cgrp, &gmiss);
688 * \param[in] data Data for the current frame.
689 * \param[in] sel Selection element being evaluated.
690 * \param[in] g Group for which \p sel should be evaluated.
691 * \returns 0 for success.
693 * Sets the value pointers of the child and its child to point to the same
694 * memory as the value pointer of this element to avoid copying, and then
695 * evaluates evaluates the child.
697 * This function is used as gmx::SelectionTreeElement:evaluate for
698 * \ref SEL_SUBEXPRREF elements for which the \ref SEL_SUBEXPR does not have
702 _gmx_sel_evaluate_subexprref_simple(gmx_sel_evaluate_t *data,
703 const SelectionTreeElementPointer &sel,
708 _gmx_selvalue_setstore(&sel->child->v, sel->v.u.ptr);
709 _gmx_selvalue_setstore_alloc(&sel->child->child->v, sel->v.u.ptr,
710 sel->child->child->v.nalloc);
711 sel->child->evaluate(data, sel->child, g);
713 sel->v.nr = sel->child->v.nr;
716 sel->u.param->val.nr = sel->v.nr;
717 if (sel->u.param->nvalptr)
719 *sel->u.param->nvalptr = sel->u.param->val.nr;
725 * \param[in] data Data for the current frame.
726 * \param[in] sel Selection element being evaluated.
727 * \param[in] g Group for which \p sel should be evaluated.
728 * \returns 0 on success, a non-zero error code on error.
730 * If the value type is \ref POS_VALUE, the value of the child is simply
731 * copied to set the value of \p sel (the child subexpression should
732 * already have been evaluated by its root).
733 * If the value type is something else, the child is evaluated for the
734 * group \p g, and the value of the child is then copied.
735 * There should be only one child element.
737 * This function is used as gmx::SelectionTreeElement::evaluate for
738 * \ref SEL_SUBEXPRREF elements.
741 _gmx_sel_evaluate_subexprref(gmx_sel_evaluate_t *data,
742 const SelectionTreeElementPointer &sel,
749 sel->child->evaluate(data, sel->child, g);
751 const SelectionTreeElementPointer &expr = sel->child;
757 sel->v.nr = expr->v.nr;
758 memcpy(sel->v.u.i, expr->v.u.i, sel->v.nr*sizeof(*sel->v.u.i));
762 sel->v.nr = g->isize;
763 /* Extract the values corresponding to g */
764 for (i = j = 0; i < g->isize; ++i, ++j)
766 while (sel->child->u.cgrp.index[j] < g->index[i])
770 sel->v.u.i[i] = expr->v.u.i[j];
778 sel->v.nr = expr->v.nr;
779 memcpy(sel->v.u.r, expr->v.u.r, sel->v.nr*sizeof(*sel->v.u.r));
783 sel->v.nr = g->isize;
784 /* Extract the values corresponding to g */
785 for (i = j = 0; i < g->isize; ++i, ++j)
787 while (sel->child->u.cgrp.index[j] < g->index[i])
791 sel->v.u.r[i] = expr->v.u.r[j];
799 sel->v.nr = expr->v.nr;
800 memcpy(sel->v.u.s, expr->v.u.s, sel->v.nr*sizeof(*sel->v.u.s));
804 sel->v.nr = g->isize;
805 /* Extract the values corresponding to g */
806 for (i = j = 0; i < g->isize; ++i, ++j)
808 while (sel->child->u.cgrp.index[j] < g->index[i])
812 sel->v.u.s[i] = expr->v.u.s[j];
818 /* Currently, there is no need to do anything fancy here,
819 * but some future extensions may need a more flexible
821 gmx_ana_pos_copy(sel->v.u.p, expr->v.u.p, false);
827 gmx_ana_index_copy(sel->v.u.g, expr->v.u.g, false);
831 gmx_ana_index_intersection(sel->v.u.g, expr->v.u.g, g);
835 default: /* should not be reached */
836 GMX_THROW(gmx::InternalError("Invalid subexpression reference type"));
838 /* Store the number of values if needed */
841 sel->u.param->val.nr = sel->v.nr;
842 if (sel->u.param->nvalptr)
844 *sel->u.param->nvalptr = sel->u.param->val.nr;
849 /********************************************************************
850 * METHOD EXPRESSION EVALUATION
851 ********************************************************************/
854 * \param[in] data Data for the current frame.
855 * \param[in] sel Selection element being evaluated.
856 * \param[in] g Group for which \p sel should be evaluated.
857 * \returns 0 on success, a non-zero error code on error.
859 * Evaluates each child of a \ref SEL_EXPRESSION element.
860 * The value of \p sel is not touched.
862 * This function is not used as gmx::SelectionTreeElement::evaluate,
863 * but is used internally.
866 _gmx_sel_evaluate_method_params(gmx_sel_evaluate_t *data,
867 const SelectionTreeElementPointer &sel,
870 SelectionTreeElementPointer child = sel->child;
873 if (child->evaluate && !(child->flags & SEL_EVALFRAME))
875 if (child->flags & SEL_ATOMVAL)
877 child->evaluate(data, child, g);
881 child->flags |= SEL_EVALFRAME;
882 child->evaluate(data, child, NULL);
890 * \param[in] data Data for the current frame.
891 * \param[in] sel Selection element being evaluated.
892 * \param[in] g Group for which \p sel should be evaluated.
893 * \returns 0 on success, a non-zero error code on error.
895 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
896 * to evaluate any parameter values.
897 * If this is the first time this expression is evaluated for
898 * the frame, sel_framefunc() callback is called if one is provided.
899 * If a reference position calculation has been initialized for this element,
900 * the positions are also updated, and sel_updatefunc_pos() is used to
901 * evaluate the value. Otherwise, sel_updatefunc() is used.
903 * This function is used as gmx::SelectionTreeElement::evaluate for
904 * \ref SEL_EXPRESSION elements.
907 _gmx_sel_evaluate_method(gmx_sel_evaluate_t *data,
908 const SelectionTreeElementPointer &sel,
911 _gmx_sel_evaluate_method_params(data, sel, g);
912 if (sel->flags & SEL_INITFRAME)
914 sel->flags &= ~SEL_INITFRAME;
915 sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
920 gmx_ana_poscalc_update(sel->u.expr.pc, sel->u.expr.pos, g,
921 data->fr, data->pbc);
922 sel->u.expr.method->pupdate(data->top, data->fr, data->pbc,
923 sel->u.expr.pos, &sel->v,
928 sel->u.expr.method->update(data->top, data->fr, data->pbc, g,
929 &sel->v, sel->u.expr.mdata);
934 * \param[in] data Data for the current frame.
935 * \param[in] sel Selection element being evaluated.
936 * \param[in] g Group for which \p sel should be evaluated.
937 * \returns 0 on success, a non-zero error code on error.
939 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
940 * to evaluate any parameter values.
941 * If this is the first time this expression is evaluated for
942 * the frame, sel_framefunc() callback is called if one is provided.
943 * The modifier is then evaluated using sel_updatefunc_pos().
945 * This function is used as gmx::SelectionTreeElement::evaluate for
946 * \ref SEL_MODIFIER elements.
949 _gmx_sel_evaluate_modifier(gmx_sel_evaluate_t *data,
950 const SelectionTreeElementPointer &sel,
953 _gmx_sel_evaluate_method_params(data, sel, g);
954 if (sel->flags & SEL_INITFRAME)
956 sel->flags &= ~SEL_INITFRAME;
957 sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
960 GMX_RELEASE_ASSERT(sel->child != NULL,
961 "Modifier element with a value must have a child");
962 if (sel->child->v.type != POS_VALUE)
964 GMX_THROW(gmx::NotImplementedError("Non-position valued modifiers not implemented"));
966 sel->u.expr.method->pupdate(data->top, data->fr, data->pbc,
968 &sel->v, sel->u.expr.mdata);
972 /********************************************************************
973 * BOOLEAN EXPRESSION EVALUATION
974 ********************************************************************/
977 * \param[in] data Data for the current frame.
978 * \param[in] sel Selection element being evaluated.
979 * \param[in] g Group for which \p sel should be evaluated.
980 * \returns 0 on success, a non-zero error code on error.
982 * Evaluates the child element (there should be only one) in the group
983 * \p g, and then sets the value of \p sel to the complement of the
986 * This function is used as gmx::SelectionTreeElement::evaluate for
987 * \ref SEL_BOOLEAN elements with \ref BOOL_NOT.
990 _gmx_sel_evaluate_not(gmx_sel_evaluate_t *data,
991 const SelectionTreeElementPointer &sel,
994 MempoolSelelemReserver reserver(sel->child, g->isize);
995 sel->child->evaluate(data, sel->child, g);
996 gmx_ana_index_difference(sel->v.u.g, g, sel->child->v.u.g);
1000 * \param[in] data Data for the current frame.
1001 * \param[in] sel Selection element being evaluated.
1002 * \param[in] g Group for which \p sel should be evaluated.
1003 * \returns 0 on success, a non-zero error code on error.
1005 * Short-circuiting evaluation of logical AND expressions.
1007 * Starts by evaluating the first child element in the group \p g.
1008 * The each following child element is evaluated in the intersection
1009 * of all the previous values until all children have been evaluated
1010 * or the intersection becomes empty.
1011 * The value of \p sel is set to the intersection of all the (evaluated)
1014 * If the first child does not have an evaluation function, it is skipped
1015 * and the evaluation is started at the second child.
1016 * This happens if the first child is a constant expression and during
1017 * compilation it was detected that the evaluation group is always a subset
1018 * of the constant group
1019 * (currently, the compiler never detects this).
1021 * This function is used as gmx::SelectionTreeElement::evaluate for
1022 * \ref SEL_BOOLEAN elements with \ref BOOL_AND.
1025 _gmx_sel_evaluate_and(gmx_sel_evaluate_t *data,
1026 const SelectionTreeElementPointer &sel,
1029 SelectionTreeElementPointer child = sel->child;
1030 /* Skip the first child if it does not have an evaluation function. */
1031 if (!child->evaluate)
1033 child = child->next;
1035 /* Evaluate the first child */
1037 MempoolSelelemReserver reserver(child, g->isize);
1038 child->evaluate(data, child, g);
1039 gmx_ana_index_copy(sel->v.u.g, child->v.u.g, false);
1041 child = child->next;
1042 while (child && sel->v.u.g->isize > 0)
1044 MempoolSelelemReserver reserver(child, sel->v.u.g->isize);
1045 child->evaluate(data, child, sel->v.u.g);
1046 gmx_ana_index_intersection(sel->v.u.g, sel->v.u.g, child->v.u.g);
1047 child = child->next;
1052 * \param[in] data Data for the current frame.
1053 * \param[in] sel Selection element being evaluated.
1054 * \param[in] g Group for which \p sel should be evaluated.
1055 * \returns 0 on success, a non-zero error code on error.
1057 * Short-circuiting evaluation of logical OR expressions.
1059 * Starts by evaluating the first child element in the group \p g.
1060 * For each subsequent child, finds the part of \p g that is not
1061 * included the value of any previous child, and evaluates the child
1062 * in that group until the last child is evaluated or all of \p g
1063 * is included in some child value.
1064 * The value of \p sel is set to the union of all the (evaluated)
1067 * If the first child does not have an evaluation function, its value is
1068 * used without evaluation.
1069 * This happens if the first child is a constant expression, the selection
1070 * has been compiled, and the evaluation group is the same for each frame.
1071 * In this case, the compiler has taken care of that the child value is a
1072 * subset of \p g, making it unnecessary to evaluate it.
1074 * This function is used as gmx::SelectionTreeElement::evaluate for
1075 * \ref SEL_BOOLEAN elements with \ref BOOL_OR.
1078 _gmx_sel_evaluate_or(gmx_sel_evaluate_t *data,
1079 const SelectionTreeElementPointer &sel,
1082 gmx_ana_index_t tmp, tmp2;
1084 SelectionTreeElementPointer child = sel->child;
1085 if (child->evaluate)
1087 MempoolSelelemReserver reserver(child, g->isize);
1088 child->evaluate(data, child, g);
1089 gmx_ana_index_partition(sel->v.u.g, &tmp, g, child->v.u.g);
1093 gmx_ana_index_partition(sel->v.u.g, &tmp, g, child->v.u.g);
1095 child = child->next;
1096 while (child && tmp.isize > 0)
1100 MempoolSelelemReserver reserver(child, tmp.isize);
1101 child->evaluate(data, child, &tmp);
1102 gmx_ana_index_partition(&tmp, &tmp2, &tmp, child->v.u.g);
1104 sel->v.u.g->isize += tmp.isize;
1105 tmp.isize = tmp2.isize;
1106 tmp.index = tmp2.index;
1107 child = child->next;
1109 gmx_ana_index_sort(sel->v.u.g);
1113 /********************************************************************
1114 * ARITHMETIC EVALUATION
1115 ********************************************************************/
1118 * \param[in] data Data for the current frame.
1119 * \param[in] sel Selection element being evaluated.
1120 * \param[in] g Group for which \p sel should be evaluated.
1121 * \returns 0 on success, a non-zero error code on error.
1124 _gmx_sel_evaluate_arithmetic(gmx_sel_evaluate_t *data,
1125 const SelectionTreeElementPointer &sel,
1129 real lval, rval=0., val=0.;
1131 const SelectionTreeElementPointer &left = sel->child;
1132 const SelectionTreeElementPointer &right = left->next;
1134 SelelemTemporaryValueAssigner assigner;
1135 MempoolSelelemReserver reserver;
1138 assigner.assign(left, *sel);
1141 reserver.reserve(right, g->isize);
1144 else if (right && right->mempool)
1146 assigner.assign(right, *sel);
1148 _gmx_sel_evaluate_children(data, sel, g);
1150 n = (sel->flags & SEL_SINGLEVAL) ? 1 : g->isize;
1153 bool bArithNeg = (sel->u.arith.type == ARITH_NEG);
1154 GMX_ASSERT(right || bArithNeg,
1155 "Right operand cannot be null except for negations");
1156 for (i = i1 = i2 = 0; i < n; ++i)
1158 lval = left->v.u.r[i1];
1161 rval = right->v.u.r[i2];
1163 switch (sel->u.arith.type)
1165 case ARITH_PLUS: val = lval + rval; break;
1166 case ARITH_MINUS: val = lval - rval; break;
1167 case ARITH_NEG: val = -lval; break;
1168 case ARITH_MULT: val = lval * rval; break;
1169 case ARITH_DIV: val = lval / rval; break;
1170 case ARITH_EXP: val = pow(lval, rval); break;
1172 sel->v.u.r[i] = val;
1173 if (!(left->flags & SEL_SINGLEVAL))
1177 if (!bArithNeg && !(right->flags & SEL_SINGLEVAL))