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
32 * \brief Implementation of functions in evaluate.h.
35 * One of the major bottlenecks for selection performance is that all the
36 * evaluation is carried out for atoms.
37 * There are several cases when the evaluation could be done for residues
38 * or molecules instead, including keywords that select by residue and
39 * cases where residue centers are used as reference positions.
40 * Implementing this would require a mechanism for recognizing whether
41 * something can be evaluated by residue/molecule instead by atom, and
42 * converting selections by residue/molecule into selections by atom
56 #include <indexutil.h>
58 #include <selection.h>
59 #include <selmethod.h>
63 #include "selcollection.h"
67 * \param[in] fp File handle to receive the output.
68 * \param[in] evalfunc Function pointer to print.
71 _gmx_sel_print_evalfunc_name(FILE *fp, sel_evalfunc evalfunc)
75 else if (evalfunc == &_gmx_sel_evaluate_root)
77 else if (evalfunc == &_gmx_sel_evaluate_static)
78 fprintf(fp, "static");
79 else if (evalfunc == &_gmx_sel_evaluate_subexpr_simple)
80 fprintf(fp, "subexpr_simple");
81 else if (evalfunc == &_gmx_sel_evaluate_subexpr_staticeval)
82 fprintf(fp, "subexpr_staticeval");
83 else if (evalfunc == &_gmx_sel_evaluate_subexpr)
84 fprintf(fp, "subexpr");
85 else if (evalfunc == &_gmx_sel_evaluate_subexprref_simple)
86 fprintf(fp, "ref_simple");
87 else if (evalfunc == &_gmx_sel_evaluate_subexprref)
89 else if (evalfunc == &_gmx_sel_evaluate_method)
90 fprintf(fp, "method");
91 else if (evalfunc == &_gmx_sel_evaluate_modifier)
93 else if (evalfunc == &_gmx_sel_evaluate_not)
95 else if (evalfunc == &_gmx_sel_evaluate_and)
97 else if (evalfunc == &_gmx_sel_evaluate_or)
99 else if (evalfunc == &_gmx_sel_evaluate_arithmetic)
100 fprintf(fp, "arithmetic");
102 fprintf(fp, "%p", (void*)(evalfunc));
106 * \param[out] data Evaluation data structure to initialize.
107 * \param[in] mp Memory pool for intermediate evaluation values.
108 * \param[in] gall Index group with all the atoms.
109 * \param[in] top Topology structure for evaluation.
110 * \param[in] fr New frame for evaluation.
111 * \param[in] pbc New PBC information for evaluation.
114 _gmx_sel_evaluate_init(gmx_sel_evaluate_t *data,
115 gmx_sel_mempool_t *mp, gmx_ana_index_t *gall,
116 t_topology *top, t_trxframe *fr, t_pbc *pbc)
126 * Recursively initializes the flags for evaluation.
128 * \param[in,out] sel Selection element to clear.
130 * The \ref SEL_INITFRAME flag is set for \ref SEL_EXPRESSION elements whose
131 * method defines the \p init_frame callback (see sel_framefunc()), and
132 * cleared for other elements.
134 * The \ref SEL_EVALFRAME flag is cleared for all elements.
137 init_frame_eval(t_selelem *sel)
141 sel->flags &= ~(SEL_INITFRAME | SEL_EVALFRAME);
142 if (sel->type == SEL_EXPRESSION)
144 if (sel->u.expr.method && sel->u.expr.method->init_frame)
146 sel->flags |= SEL_INITFRAME;
149 if (sel->child && sel->type != SEL_SUBEXPRREF)
151 init_frame_eval(sel->child);
158 * \param[in,out] sc The selection collection to evaluate.
159 * \param[in] fr Frame for which the evaluation should be carried out.
160 * \param[in] pbc PBC data, or NULL if no PBC should be used.
161 * \returns 0 on successful evaluation, a non-zero error code on error.
163 * This functions sets the global variables for topology, frame and PBC,
164 * clears some information in the selection to initialize the evaluation
165 * for a new frame, and evaluates \p sel and all the selections pointed by
166 * the \p next pointers of \p sel.
168 * This is the only function that user code should call if they want to
169 * evaluate a selection for a new frame.
172 gmx_ana_selcollection_evaluate(gmx_ana_selcollection_t *sc,
173 t_trxframe *fr, t_pbc *pbc)
175 gmx_sel_evaluate_t data;
180 _gmx_sel_evaluate_init(&data, sc->mempool, &sc->gall, sc->top, fr, pbc);
181 init_frame_eval(sc->root);
185 /* Clear the evaluation group of subexpressions */
186 if (sel->child && sel->child->type == SEL_SUBEXPR)
188 sel->child->u.cgrp.isize = 0;
189 /* Not strictly necessary, because the value will be overwritten
190 * during first evaluation of the subexpression anyways, but we
191 * clear the group for clarity. Note that this is _not_ done during
192 * compilation because of some additional complexities involved
193 * (see compiler.c), so it should not be relied upon in
194 * _gmx_sel_evaluate_subexpr(). */
195 if (sel->child->v.type == GROUP_VALUE)
197 sel->child->v.u.g->isize = 0;
202 rc = sel->evaluate(&data, sel, NULL);
210 /* Update selection information */
211 for (g = 0; g < sc->nr; ++g)
213 gmx_ana_selection_t *sel = sc->sel[g];
215 if (sel->m != sel->orgm)
217 for (i = 0; i < sel->p.nr; ++i)
219 sel->m[i] = sel->orgm[sel->p.m.refid[i]];
220 sel->q[i] = sel->orgq[sel->p.m.refid[i]];
225 sel->cfrac = _gmx_selelem_estimate_coverfrac(sel->selelem);
226 sel->avecfrac += sel->cfrac;
233 * \param[in,out] sc The selection collection to evaluate.
234 * \param[in] nframes Total number of frames.
235 * \returns 0 on successful evaluation, a non-zero error code on error.
238 gmx_ana_selcollection_evaluate_fin(gmx_ana_selcollection_t *sc, int nframes)
243 for (g = 0; g < sc->nr; ++g)
245 sel = sc->sel[g]->selelem;
246 if (sc->sel[g]->bDynamic)
248 gmx_ana_index_copy(sc->sel[g]->g, sel->v.u.g, FALSE);
249 sc->sel[g]->g->name = NULL;
250 gmx_ana_indexmap_update(&sc->sel[g]->p.m, sc->sel[g]->g, sc->bMaskOnly);
251 sc->sel[g]->p.nr = sc->sel[g]->p.m.nr;
254 if (sc->sel[g]->bCFracDyn)
256 sc->sel[g]->avecfrac /= nframes;
263 * \param[in] data Data for the current frame.
264 * \param[in] sel Selection element being evaluated.
265 * \param[in] g Group for which \p sel should be evaluated.
266 * \returns 0 on success, a non-zero error code on error.
268 * Evaluates each child of \p sel in \p g.
271 _gmx_sel_evaluate_children(gmx_sel_evaluate_t *data, t_selelem *sel,
282 rc = child->evaluate(data, child, g);
294 * \param[in] data Data for the current frame.
295 * \param[in] sel Selection element being evaluated.
296 * \param[in] g Group for which \p sel should be evaluated
297 * (not used, can be NULL).
298 * \returns 0 on success, a non-zero error code on error.
300 * Evaluates the first child element in the group defined by \p sel->u.cgrp.
301 * If \p sel->u.cgrp is empty, nothing is done.
302 * The value of \p sel is not touched (root elements do not evaluate to
305 * This function can be used as \c t_selelem::evaluate for \ref SEL_ROOT
309 _gmx_sel_evaluate_root(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
313 if (sel->u.cgrp.isize == 0 || !sel->child->evaluate)
318 rc = sel->child->evaluate(data, sel->child,
319 sel->u.cgrp.isize < 0 ? NULL : &sel->u.cgrp);
325 * \param[in] data Data for the current frame.
326 * \param[in] sel Selection element being evaluated.
327 * \param[in] g Group for which \p sel should be evaluated.
328 * \returns 0 for success.
330 * Sets the value of \p sel to the intersection of \p g and \p sel->u.cgrp.
332 * This function can be used as \c t_selelem::evaluate for \ref SEL_CONST
333 * elements with value type \ref GROUP_VALUE.
336 _gmx_sel_evaluate_static(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
338 gmx_ana_index_intersection(sel->v.u.g, &sel->u.cgrp, g);
343 /*********************************************************************
344 * SUBEXPRESSION EVALUATION
345 *********************************************************************/
348 * \param[in] data Data for the current frame.
349 * \param[in] sel Selection element being evaluated.
350 * \param[in] g Group for which \p sel should be evaluated.
351 * \returns 0 on success, a non-zero error code on error.
353 * Evaluates the child element (there should be exactly one) in \p g.
354 * The compiler has taken care that the child actually stores the evaluated
355 * value in the value pointer of this element.
357 * This function is used as \c t_selelem::evaluate for \ref SEL_SUBEXPR
358 * elements that are used only once, and hence do not need full subexpression
362 _gmx_sel_evaluate_subexpr_simple(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
366 if (sel->child->evaluate)
368 rc = sel->child->evaluate(data, sel->child, g);
374 sel->v.nr = sel->child->v.nr;
379 * \param[in] data Data for the current frame.
380 * \param[in] sel Selection element being evaluated.
381 * \param[in] g Group for which \p sel should be evaluated.
382 * \returns 0 on success, a non-zero error code on error.
384 * If this is the first call for this frame, evaluates the child element
385 * there should be exactly one in \p g.
386 * The compiler has taken care that the child actually stores the evaluated
387 * value in the value pointer of this element.
388 * Assumes that \p g is persistent for the duration of the whole evaluation.
390 * This function is used as \c t_selelem::evaluate for \ref SEL_SUBEXPR
391 * elements that have a static evaluation group, and hence do not need full
392 * subexpression handling.
395 _gmx_sel_evaluate_subexpr_staticeval(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
397 if (sel->u.cgrp.isize == 0)
401 rc = sel->child->evaluate(data, sel->child, g);
406 sel->v.nr = sel->child->v.nr;
409 sel->u.cgrp.isize = -1;
413 gmx_ana_index_set(&sel->u.cgrp, g->isize, g->index, sel->u.cgrp.name, 0);
420 * \param[in] data Data for the current frame.
421 * \param[in] sel Selection element being evaluated.
422 * \param[in] g Group for which \p sel should be evaluated.
423 * \returns 0 on success, a non-zero error code on error.
425 * Finds the part of \p g for which the subexpression
426 * has not yet been evaluated by comparing \p g to \p sel->u.cgrp.
427 * If the part is not empty, the child expression is evaluated for this
428 * part, and the results merged to the old values of the child.
429 * The value of \p sel itself is undefined after the call.
432 * The call to gmx_ana_index_difference() can take quite a lot of unnecessary
433 * time if the subexpression is evaluated either several times for the same
434 * group or for completely distinct groups.
435 * However, in the majority of cases, these situations occur when
436 * _gmx_sel_evaluate_subexpr_staticeval() can be used, so this should not be a
440 _gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
442 gmx_ana_index_t gmiss;
445 if (sel->u.cgrp.isize == 0)
448 void *old_ptr = sel->child->v.u.ptr;
449 int old_nalloc = sel->child->v.nalloc;
450 _gmx_selvalue_setstore(&sel->child->v, sel->v.u.ptr);
451 rc = sel->child->evaluate(data, sel->child, g);
452 _gmx_selvalue_setstore_alloc(&sel->child->v, old_ptr, old_nalloc);
457 /* We need to keep the name for the cgrp across the copy to avoid
458 * problems if g has a name set. */
459 name = sel->u.cgrp.name;
460 gmx_ana_index_copy(&sel->u.cgrp, g, FALSE);
461 sel->u.cgrp.name = name;
466 /* We allocate some extra memory here to avoid some computation. */
467 rc = _gmx_sel_mempool_alloc_group(data->mp, &gmiss, g->isize);
472 gmx_ana_index_difference(&gmiss, g, &sel->u.cgrp);
473 if (gmiss.isize == 0)
475 _gmx_sel_mempool_free_group(data->mp, &gmiss);
481 rc = _gmx_selelem_mempool_reserve(sel->child, gmiss.isize);
486 /* Evaluate the missing values for the child */
487 rc = sel->child->evaluate(data, sel->child, &gmiss);
492 /* Merge the missing values to the existing ones. */
493 if (sel->v.type == GROUP_VALUE)
495 gmx_ana_index_merge(sel->v.u.g, sel->child->v.u.g, sel->v.u.g);
501 i = sel->u.cgrp.isize - 1;
503 /* TODO: This switch is kind of ugly, but it may be difficult to
504 * do this portably without C++ templates. */
508 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
510 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
512 sel->v.u.i[k] = sel->child->v.u.i[j--];
516 sel->v.u.i[k] = sel->v.u.i[i--];
522 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
524 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
526 sel->v.u.r[k] = sel->child->v.u.r[j--];
530 sel->v.u.r[k] = sel->v.u.r[i--];
536 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
538 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
540 sel->v.u.s[k] = sel->child->v.u.s[j--];
544 sel->v.u.s[k] = sel->v.u.s[i--];
550 /* TODO: Implement this */
551 gmx_impl("position subexpressions not implemented properly");
556 gmx_bug("internal error");
560 gmx_ana_index_merge(&sel->u.cgrp, &sel->u.cgrp, &gmiss);
561 _gmx_selelem_mempool_release(sel->child);
562 _gmx_sel_mempool_free_group(data->mp, &gmiss);
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 for success.
573 * Sets the value pointers of the child and its child to point to the same
574 * memory as the value pointer of this element to avoid copying, and then
575 * evaluates evaluates the child.
577 * This function is used as \c t_selelem:evaluate for \ref SEL_SUBEXPRREF
578 * elements for which the \ref SEL_SUBEXPR does not have other references.
581 _gmx_sel_evaluate_subexprref_simple(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
587 _gmx_selvalue_setstore(&sel->child->v, sel->v.u.ptr);
588 _gmx_selvalue_setstore_alloc(&sel->child->child->v, sel->v.u.ptr,
589 sel->child->child->v.nalloc);
590 rc = sel->child->evaluate(data, sel->child, g);
596 sel->v.nr = sel->child->v.nr;
599 sel->u.param->val.nr = sel->v.nr;
600 if (sel->u.param->nvalptr)
602 *sel->u.param->nvalptr = sel->u.param->val.nr;
609 * \param[in] data Data for the current frame.
610 * \param[in] sel Selection element being evaluated.
611 * \param[in] g Group for which \p sel should be evaluated.
612 * \returns 0 on success, a non-zero error code on error.
614 * If the value type is \ref POS_VALUE, the value of the child is simply
615 * copied to set the value of \p sel (the child subexpression should
616 * already have been evaluated by its root).
617 * If the value type is something else, the child is evaluated for the
618 * group \p g, and the value of the child is then copied.
619 * There should be only one child element.
621 * This function is used as \c t_selelem::evaluate for \ref SEL_SUBEXPRREF
625 _gmx_sel_evaluate_subexprref(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
634 rc = sel->child->evaluate(data, sel->child, g);
646 sel->v.nr = expr->v.nr;
647 memcpy(sel->v.u.i, expr->v.u.i, sel->v.nr*sizeof(*sel->v.u.i));
651 sel->v.nr = g->isize;
652 /* Extract the values corresponding to g */
653 for (i = j = 0; i < g->isize; ++i, ++j)
655 while (sel->child->u.cgrp.index[j] < g->index[i])
659 sel->v.u.i[i] = expr->v.u.i[j];
667 sel->v.nr = expr->v.nr;
668 memcpy(sel->v.u.r, expr->v.u.r, sel->v.nr*sizeof(*sel->v.u.r));
672 sel->v.nr = g->isize;
673 /* Extract the values corresponding to g */
674 for (i = j = 0; i < g->isize; ++i, ++j)
676 while (sel->child->u.cgrp.index[j] < g->index[i])
680 sel->v.u.r[i] = expr->v.u.r[j];
688 sel->v.nr = expr->v.nr;
689 memcpy(sel->v.u.s, expr->v.u.s, sel->v.nr*sizeof(*sel->v.u.s));
693 sel->v.nr = g->isize;
694 /* Extract the values corresponding to g */
695 for (i = j = 0; i < g->isize; ++i, ++j)
697 while (sel->child->u.cgrp.index[j] < g->index[i])
701 sel->v.u.s[i] = expr->v.u.s[j];
707 /* Currently, there is no need to do anything fancy here,
708 * but some future extensions may need a more flexible
710 gmx_ana_pos_copy(sel->v.u.p, expr->v.u.p, FALSE);
716 gmx_ana_index_copy(sel->v.u.g, expr->v.u.g, FALSE);
720 gmx_ana_index_intersection(sel->v.u.g, expr->v.u.g, g);
724 default: /* should not be reached */
725 gmx_bug("invalid subexpression reference type");
728 /* Store the number of values if needed */
731 sel->u.param->val.nr = sel->v.nr;
732 if (sel->u.param->nvalptr)
734 *sel->u.param->nvalptr = sel->u.param->val.nr;
740 /********************************************************************
741 * METHOD EXPRESSION EVALUATION
742 ********************************************************************/
745 * \param[in] data Data for the current frame.
746 * \param[in] sel Selection element being evaluated.
747 * \param[in] g Group for which \p sel should be evaluated.
748 * \returns 0 on success, a non-zero error code on error.
750 * Evaluates each child of a \ref SEL_EXPRESSION element.
751 * The value of \p sel is not touched.
753 * This function is not used as \c t_selelem::evaluate,
754 * but is used internally.
757 _gmx_sel_evaluate_method_params(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
765 if (child->evaluate && !(child->flags & SEL_EVALFRAME))
767 if (child->flags & SEL_ATOMVAL)
769 rc = child->evaluate(data, child, g);
773 rc = child->evaluate(data, child, NULL);
774 child->flags |= SEL_EVALFRAME;
787 * \param[in] data Data for the current frame.
788 * \param[in] sel Selection element being evaluated.
789 * \param[in] g Group for which \p sel should be evaluated.
790 * \returns 0 on success, a non-zero error code on error.
792 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
793 * to evaluate any parameter values.
794 * If this is the first time this expression is evaluated for
795 * the frame, sel_framefunc() callback is called if one is provided.
796 * If a reference position calculation has been initialized for this element,
797 * the positions are also updated, and sel_updatefunc_pos() is used to
798 * evaluate the value. Otherwise, sel_updatefunc() is used.
800 * This function is used as \c t_selelem::evaluate for \ref SEL_EXPRESSION
804 _gmx_sel_evaluate_method(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
808 rc = _gmx_sel_evaluate_method_params(data, sel, g);
813 if (sel->flags & SEL_INITFRAME)
815 rc = sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
817 sel->flags &= ~SEL_INITFRAME;
825 gmx_ana_poscalc_update(sel->u.expr.pc, sel->u.expr.pos, g,
826 data->fr, data->pbc);
827 rc = sel->u.expr.method->pupdate(data->top, data->fr, data->pbc,
828 sel->u.expr.pos, &sel->v,
833 rc = sel->u.expr.method->update(data->top, data->fr, data->pbc, g,
834 &sel->v, sel->u.expr.mdata);
840 * \param[in] data Data for the current frame.
841 * \param[in] sel Selection element being evaluated.
842 * \param[in] g Group for which \p sel should be evaluated.
843 * \returns 0 on success, a non-zero error code on error.
845 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
846 * to evaluate any parameter values.
847 * If this is the first time this expression is evaluated for
848 * the frame, sel_framefunc() callback is called if one is provided.
849 * The modifier is then evaluated using sel_updatefunc_pos().
851 * This function is used as \c t_selelem::evaluate for \ref SEL_MODIFIER
855 _gmx_sel_evaluate_modifier(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
859 rc = _gmx_sel_evaluate_method_params(data, sel, g);
864 if (sel->flags & SEL_INITFRAME)
866 rc = sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
868 sel->flags &= ~SEL_INITFRAME;
874 if (sel->child->v.type != POS_VALUE)
876 gmx_bug("non-position valued modifiers not implemented");
879 rc = sel->u.expr.method->pupdate(data->top, data->fr, data->pbc,
881 &sel->v, sel->u.expr.mdata);
886 /********************************************************************
887 * BOOLEAN EXPRESSION EVALUATION
888 ********************************************************************/
891 * \param[in] data Data for the current frame.
892 * \param[in] sel Selection element being evaluated.
893 * \param[in] g Group for which \p sel should be evaluated.
894 * \returns 0 on success, a non-zero error code on error.
896 * Evaluates the child element (there should be only one) in the group
897 * \p g, and then sets the value of \p sel to the complement of the
900 * This function is used as \c t_selelem::evaluate for \ref SEL_BOOLEAN
901 * elements with \ref BOOL_NOT.
904 _gmx_sel_evaluate_not(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
908 rc = _gmx_selelem_mempool_reserve(sel->child, g->isize);
911 rc = sel->child->evaluate(data, sel->child, g);
917 gmx_ana_index_difference(sel->v.u.g, g, sel->child->v.u.g);
918 _gmx_selelem_mempool_release(sel->child);
923 * \param[in] data Data for the current frame.
924 * \param[in] sel Selection element being evaluated.
925 * \param[in] g Group for which \p sel should be evaluated.
926 * \returns 0 on success, a non-zero error code on error.
928 * Short-circuiting evaluation of logical AND expressions.
930 * Starts by evaluating the first child element in the group \p g.
931 * The each following child element is evaluated in the intersection
932 * of all the previous values until all children have been evaluated
933 * or the intersection becomes empty.
934 * The value of \p sel is set to the intersection of all the (evaluated)
937 * If the first child does not have an evaluation function, it is skipped
938 * and the evaluation is started at the second child.
939 * This happens if the first child is a constant expression and during
940 * compilation it was detected that the evaluation group is always a subset
941 * of the constant group
942 * (currently, the compiler never detects this).
944 * This function is used as \c t_selelem::evaluate for \ref SEL_BOOLEAN
945 * elements with \ref BOOL_AND.
948 _gmx_sel_evaluate_and(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
954 /* Skip the first child if it does not have an evaluation function. */
955 if (!child->evaluate)
959 rc = _gmx_selelem_mempool_reserve(child, g->isize);
962 rc = child->evaluate(data, child, g);
968 gmx_ana_index_copy(sel->v.u.g, child->v.u.g, FALSE);
969 _gmx_selelem_mempool_release(child);
971 while (child && sel->v.u.g->isize > 0)
973 rc = _gmx_selelem_mempool_reserve(child, sel->v.u.g->isize);
976 rc = child->evaluate(data, child, sel->v.u.g);
982 gmx_ana_index_intersection(sel->v.u.g, sel->v.u.g, child->v.u.g);
983 _gmx_selelem_mempool_release(child);
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 * Short-circuiting evaluation of logical OR expressions.
997 * Starts by evaluating the first child element in the group \p g.
998 * For each subsequent child, finds the part of \p g that is not
999 * included the value of any previous child, and evaluates the child
1000 * in that group until the last child is evaluated or all of \p g
1001 * is included in some child value.
1002 * The value of \p sel is set to the union of all the (evaluated)
1005 * If the first child does not have an evaluation function, its value is
1006 * used without evaluation.
1007 * This happens if the first child is a constant expression, the selection
1008 * has been compiled, and the evaluation group is the same for each frame.
1009 * In this case, the compiler has taken care of that the child value is a
1010 * subset of \p g, making it unnecessary to evaluate it.
1012 * This function is used as \c t_selelem::evaluate for \ref SEL_BOOLEAN
1013 * elements with \ref BOOL_OR.
1016 _gmx_sel_evaluate_or(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
1019 gmx_ana_index_t tmp, tmp2;
1023 if (child->evaluate)
1025 rc = _gmx_selelem_mempool_reserve(child, g->isize);
1028 rc = child->evaluate(data, child, g);
1034 gmx_ana_index_partition(sel->v.u.g, &tmp, g, child->v.u.g);
1035 _gmx_selelem_mempool_release(child);
1039 gmx_ana_index_partition(sel->v.u.g, &tmp, g, child->v.u.g);
1041 child = child->next;
1042 while (child && tmp.isize > 0)
1045 rc = _gmx_selelem_mempool_reserve(child, tmp.isize);
1048 rc = child->evaluate(data, child, &tmp);
1054 gmx_ana_index_partition(&tmp, &tmp2, &tmp, child->v.u.g);
1055 _gmx_selelem_mempool_release(child);
1056 sel->v.u.g->isize += tmp.isize;
1057 tmp.isize = tmp2.isize;
1058 tmp.index = tmp2.index;
1059 child = child->next;
1061 gmx_ana_index_sort(sel->v.u.g);
1066 /********************************************************************
1067 * ARITHMETIC EVALUATION
1068 ********************************************************************/
1071 * \param[in] data Data for the current frame.
1072 * \param[in] sel Selection element being evaluated.
1073 * \param[in] g Group for which \p sel should be evaluated.
1074 * \returns 0 on success, a non-zero error code on error.
1077 _gmx_sel_evaluate_arithmetic(gmx_sel_evaluate_t *data, t_selelem *sel,
1081 real lval, rval=0., val=0.;
1085 t_selelem *const left = sel->child;
1086 t_selelem *const right = left->next;
1090 _gmx_selvalue_setstore(&left->v, sel->v.u.ptr);
1093 rc = _gmx_selelem_mempool_reserve(right, g->isize);
1100 else if (right && right->mempool)
1102 _gmx_selvalue_setstore(&right->v, sel->v.u.ptr);
1104 rc = _gmx_sel_evaluate_children(data, sel, g);
1106 n = (sel->flags & SEL_SINGLEVAL) ? 1 : g->isize;
1109 bArithNeg = (sel->u.arith.type == ARITH_NEG);
1110 assert(right || bArithNeg);
1111 for (i = i1 = i2 = 0; i < n; ++i)
1113 lval = left->v.u.r[i1];
1116 rval = right->v.u.r[i2];
1118 switch (sel->u.arith.type)
1120 case ARITH_PLUS: val = lval + rval; break;
1121 case ARITH_MINUS: val = lval - rval; break;
1122 case ARITH_NEG: val = -lval; break;
1123 case ARITH_MULT: val = lval * rval; break;
1124 case ARITH_DIV: val = lval / rval; break;
1125 case ARITH_EXP: val = pow(lval, rval); break;
1127 sel->v.u.r[i] = val;
1128 if (!(left->flags & SEL_SINGLEVAL))
1132 if (!bArithNeg && !(right->flags & SEL_SINGLEVAL))
1140 _gmx_selvalue_setstore(&left->v, NULL);
1143 _gmx_selelem_mempool_release(right);
1146 else if (right && right->mempool)
1148 _gmx_selvalue_setstore(&right->v, NULL);