Better selection stdin handling.
[gromacs/qmmm-gamess-us.git] / src / gmxlib / selection / evaluate.c
blobff20f9821a62c906e60334c119abd82b17a36039
1 /*
3 * This source code is part of
5 * G R O M A C S
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
31 /*! \internal \file
32 * \brief Implementation of functions in evaluate.h.
34 * \todo
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
43 * when necessary.
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif
49 #include <string.h>
51 #include <maths.h>
52 #include <smalloc.h>
53 #include <vec.h>
55 #include <indexutil.h>
56 #include <poscalc.h>
57 #include <selection.h>
58 #include <selmethod.h>
60 #include "evaluate.h"
61 #include "selcollection.h"
62 #include "selelem.h"
64 /*!
65 * \param[out] data Evaluation data structure to initialize.
66 * \param[in] gall Index group with all the atoms.
67 * \param[in] top Topology structure for evaluation.
68 * \param[in] fr New frame for evaluation.
69 * \param[in] pbc New PBC information for evaluation.
71 void
72 _gmx_sel_evaluate_init(gmx_sel_evaluate_t *data, gmx_ana_index_t *gall,
73 t_topology *top, t_trxframe *fr, t_pbc *pbc)
75 data->gall = gall;
76 data->top = top;
77 data->fr = fr;
78 data->pbc = pbc;
81 /*! \brief
82 * Recursively initializes the flags for evaluation.
84 * \param[in,out] sel Selection element to clear.
86 * The \ref SEL_INITFRAME flag is set for \ref SEL_EXPRESSION elements whose
87 * method defines the \p init_frame callback (see sel_framefunc()), and
88 * cleared for other elements.
90 * The \ref SEL_EVALFRAME flag is cleared for all elements.
92 static void
93 init_frame_eval(t_selelem *sel)
95 while (sel)
97 sel->flags &= ~(SEL_INITFRAME | SEL_EVALFRAME);
98 if (sel->type == SEL_EXPRESSION)
100 if (sel->u.expr.method && sel->u.expr.method->init_frame)
102 sel->flags |= SEL_INITFRAME;
105 if (sel->child && sel->type != SEL_SUBEXPRREF)
107 init_frame_eval(sel->child);
109 sel = sel->next;
114 * \param[in,out] sc The selection collection to evaluate.
115 * \param[in] fr Frame for which the evaluation should be carried out.
116 * \param[in] pbc PBC data, or NULL if no PBC should be used.
117 * \returns 0 on successful evaluation, a non-zero error code on error.
119 * This functions sets the global variables for topology, frame and PBC,
120 * clears some information in the selection to initialize the evaluation
121 * for a new frame, and evaluates \p sel and all the selections pointed by
122 * the \p next pointers of \p sel.
124 * This is the only function that user code should call if they want to
125 * evaluate a selection for a new frame.
128 gmx_ana_selcollection_evaluate(gmx_ana_selcollection_t *sc,
129 t_trxframe *fr, t_pbc *pbc)
131 gmx_sel_evaluate_t data;
132 t_selelem *sel;
133 int g, i;
134 int rc;
136 _gmx_sel_evaluate_init(&data, &sc->gall, sc->top, fr, pbc);
137 init_frame_eval(sc->root);
138 sel = sc->root;
139 while (sel)
141 /* Clear the evaluation group of subexpressions */
142 if (sel->child && sel->child->type == SEL_SUBEXPR)
144 sel->child->u.cgrp.isize = 0;
145 if (sel->child->v.type == GROUP_VALUE)
147 sel->child->child->v.u.g->isize = 0;
150 if (sel->evaluate)
152 rc = sel->evaluate(&data, sel, NULL);
153 if (rc != 0)
155 return rc;
158 sel = sel->next;
160 /* Update selection information */
161 for (g = 0; g < sc->nr; ++g)
163 gmx_ana_selection_t *sel = sc->sel[g];
165 if (sel->m != sel->orgm)
167 for (i = 0; i < sel->p.nr; ++i)
169 sel->m[i] = sel->orgm[sel->p.m.refid[i]];
170 sel->q[i] = sel->orgq[sel->p.m.refid[i]];
173 if (sel->bCFracDyn)
175 sel->cfrac = _gmx_selelem_estimate_coverfrac(sel->selelem);
176 sel->avecfrac += sel->cfrac;
179 return 0;
183 * \param[in,out] sc The selection collection to evaluate.
184 * \param[in] nframes Total number of frames.
185 * \returns 0 on successful evaluation, a non-zero error code on error.
188 gmx_ana_selcollection_evaluate_fin(gmx_ana_selcollection_t *sc, int nframes)
190 t_selelem *sel;
191 int g;
193 for (g = 0; g < sc->nr; ++g)
195 sel = sc->sel[g]->selelem;
196 if (sc->sel[g]->g)
198 gmx_ana_index_copy(sc->sel[g]->g, sel->v.u.g, FALSE);
199 sc->sel[g]->g->name = NULL;
200 gmx_ana_indexmap_update(&sc->sel[g]->p.m, sc->sel[g]->g, sc->bMaskOnly);
201 sc->sel[g]->p.nr = sc->sel[g]->p.m.nr;
204 if (sc->sel[g]->bCFracDyn)
206 sc->sel[g]->avecfrac /= nframes;
209 return 0;
213 * \param[in] data Data for the current frame.
214 * \param[in] sel Selection element being evaluated.
215 * \param[in] g Group for which \p sel should be evaluated
216 * (not used, can be NULL).
217 * \returns 0 on success, a non-zero error code on error.
219 * Evaluates the first child element in the group defined by \p sel->u.cgrp.
220 * If \p sel->u.cgrp is empty, nothing is done.
221 * The value of \p sel is not touched (root elements do not evaluate to
222 * values).
224 * This function can be used as \c t_selelem::evaluate for \ref SEL_ROOT
225 * elements.
228 _gmx_sel_evaluate_root(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
230 int rc;
232 if (sel->u.cgrp.isize == 0 || !sel->child->evaluate)
234 return 0;
237 rc = sel->child->evaluate(data, sel->child,
238 sel->u.cgrp.isize < 0 ? NULL : &sel->u.cgrp);
240 return rc;
244 * \param[in] data Data for the current frame.
245 * \param[in] sel Selection element being evaluated.
246 * \param[in] g Group for which \p sel should be evaluated.
247 * \returns 0 for success.
249 * Sets the value of \p sel to the intersection of \p g and \p sel->u.cgrp.
251 * This function can be used as \c t_selelem::evaluate for \ref SEL_CONST
252 * elements with value type \ref GROUP_VALUE.
255 _gmx_sel_evaluate_static(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
257 gmx_ana_index_intersection(sel->v.u.g, &sel->u.cgrp, g);
258 return 0;
262 /*********************************************************************
263 * SUBEXPRESSION EVALUATION
264 *********************************************************************/
267 * \param[in] data Data for the current frame.
268 * \param[in] sel Selection element being evaluated.
269 * \param[in] g Group for which \p sel should be evaluated.
270 * \returns 0 on success, a non-zero error code on error.
272 * Evaluates the child element (there should be exactly one) in \p g.
273 * The number of values is copied from the child to \p sel->v.nr, but
274 * otherwise the value of \p sel is not touched.
276 * This function is used as \c t_selelem::evaluate for \ref SEL_SUBEXPR
277 * elements that are used only once, and hence do not need full subexpression
278 * handling.
281 _gmx_sel_evaluate_subexpr_pass(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
283 int rc;
285 if (sel->child->evaluate)
287 rc = sel->child->evaluate(data, sel->child, g);
288 if (rc != 0)
290 return rc;
293 sel->v.nr = sel->child->v.nr;
294 return 0;
298 * \param[in] data Data for the current frame.
299 * \param[in] sel Selection element being evaluated.
300 * \param[in] g Group for which \p sel should be evaluated.
301 * \returns 0 on success, a non-zero error code on error.
303 * Finds the part of \p g for which the subexpression
304 * has not yet been evaluated by comparing \p g to \p sel->u.cgrp.
305 * If the part is not empty, the child expression is evaluated for this
306 * part, and the results merged to the old values of the child.
307 * The value of \p sel itself is undefined after the call.
309 * \todo
310 * The call to gmx_ana_index_difference() can take quite a lot of unnecessary
311 * time if the subexpression is evaluated either several times for the same
312 * group or for completely distinct groups.
315 _gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
317 gmx_ana_index_t gmiss;
318 int rc;
320 if (sel->u.cgrp.isize == 0)
322 char *name;
323 /* We need to check for the presence because the compiler may clear
324 * it temporarily. */
325 if (sel->child->evaluate)
327 rc = sel->child->evaluate(data, sel->child, g);
328 if (rc != 0)
330 return rc;
333 /* We need to keep the name for the cgrp across the copy to avoid
334 * problems if g has a name set. */
335 name = sel->u.cgrp.name;
336 gmx_ana_index_copy(&sel->u.cgrp, g, FALSE);
337 sel->u.cgrp.name = name;
338 gmiss.isize = 0;
340 else
342 if (sel->v.type == GROUP_VALUE)
344 gmx_ana_index_set(&gmiss, 0, sel->child->v.u.g->index + sel->child->v.u.g->isize, NULL, 0);
346 else
348 gmx_ana_index_set(&gmiss, 0, sel->u.cgrp.index + sel->u.cgrp.isize, NULL, 0);
350 gmx_ana_index_difference(&gmiss, g, &sel->u.cgrp);
352 if (gmiss.isize > 0)
354 /* We use the value of sel to store the old value of the child before
355 * evaluating the missing values. */
356 if (sel->v.type == GROUP_VALUE)
358 atom_id *tmp = sel->child->v.u.g->index;
359 sel->child->v.u.g->index = sel->v.u.g->index;
360 sel->v.u.g->index = tmp;
361 sel->v.u.g->isize = sel->child->v.u.g->isize;
362 sel->child->v.u.g->isize = 0;
364 else
366 void *tmp = sel->child->v.u.ptr;
367 sel->child->v.u.ptr = sel->v.u.ptr;
368 sel->v.u.ptr = tmp;
370 /* Evaluate the missing values for the child */
371 rc = sel->child->evaluate(data, sel->child, &gmiss);
372 if (rc != 0)
374 return rc;
376 /* Merge the missing values to the existing ones. */
377 if (sel->v.type == GROUP_VALUE)
379 gmx_ana_index_merge(sel->child->v.u.g, sel->child->v.u.g, sel->v.u.g);
380 gmx_ana_index_merge(&sel->u.cgrp, &sel->u.cgrp, &gmiss);
382 else
384 int i, j, k;
386 i = sel->u.cgrp.isize - 1;
387 j = gmiss.isize - 1;
388 /* TODO: This switch is kind of ugly, but it may be difficult to
389 * do this portably without C++ templates. */
390 switch (sel->v.type)
392 case INT_VALUE:
393 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
395 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
397 sel->child->v.u.i[k] = sel->child->v.u.i[j--];
399 else
401 sel->child->v.u.i[k] = sel->v.u.i[i--];
404 break;
406 case REAL_VALUE:
407 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
409 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
411 sel->child->v.u.r[k] = sel->child->v.u.r[j--];
413 else
415 sel->child->v.u.r[k] = sel->v.u.r[i--];
418 break;
420 case STR_VALUE:
421 for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
423 if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
425 sel->child->v.u.s[k] = sel->child->v.u.s[j--];
427 else
429 sel->child->v.u.s[k] = sel->v.u.s[i--];
432 break;
434 case POS_VALUE:
435 /* TODO: Implement this */
436 gmx_impl("position subexpressions not implemented properly");
437 return -1;
439 case NO_VALUE:
440 case GROUP_VALUE:
441 gmx_bug("internal error");
442 return -1;
444 /* TODO: With some additional storage, we could do a merge here */
445 sel->u.cgrp.isize += gmiss.isize;
446 gmx_ana_index_sort(&sel->u.cgrp);
449 return 0;
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 * \returns 0 for success.
458 * This function is used as \c t_selelem:evaluate for \ref SEL_SUBEXPRREF
459 * elements for which the actual value is evaluated directly by the
460 * subexpression, but for which the number of values needs to be passed
461 * forward.
464 _gmx_sel_evaluate_subexprref_pass(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
466 sel->v.nr = sel->child->child->v.nr;
467 if (sel->u.param)
469 sel->u.param->val.nr = sel->v.nr;
470 if (sel->u.param->nvalptr)
472 *sel->u.param->nvalptr = sel->u.param->val.nr;
475 return 0;
479 * \param[in] data Data for the current frame.
480 * \param[in] sel Selection element being evaluated.
481 * \param[in] g Group for which \p sel should be evaluated.
482 * \returns 0 on success, a non-zero error code on error.
484 * If the value type is \ref POS_VALUE, the value of the child is simply
485 * copied to set the value of \p sel (the child subexpression should
486 * already have been evaluated by its root).
487 * If the value type is something else, the child is evaluated for the
488 * group \p g, and the value of the child is then copied.
489 * There should be only one child element.
491 * This function is used as \c t_selelem::evaluate for \ref SEL_SUBEXPRREF
492 * elements.
495 _gmx_sel_evaluate_subexprref(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
497 t_selelem *expr;
498 int i, j;
500 /* We need to check for the presence because the compiler may clear
501 * it temporarily. */
502 if (g && sel->child->evaluate)
504 int rc;
506 rc = sel->child->evaluate(data, sel->child, g);
507 if (rc != 0)
509 return rc;
512 expr = sel->child->child;
513 switch (sel->v.type)
515 case INT_VALUE:
516 if (!g)
518 sel->v.nr = expr->v.nr;
519 memcpy(sel->v.u.i, expr->v.u.i, sel->v.nr*sizeof(*sel->v.u.i));
521 else
523 sel->v.nr = g->isize;
524 /* Extract the values corresponding to g */
525 for (i = j = 0; i < g->isize; ++i, ++j)
527 while (sel->child->u.cgrp.index[j] < g->index[i])
529 ++j;
531 sel->v.u.i[i] = expr->v.u.i[j];
534 break;
536 case REAL_VALUE:
537 if (!g)
539 sel->v.nr = expr->v.nr;
540 memcpy(sel->v.u.r, expr->v.u.r, sel->v.nr*sizeof(*sel->v.u.r));
542 else
544 sel->v.nr = g->isize;
545 /* Extract the values corresponding to g */
546 for (i = j = 0; i < g->isize; ++i, ++j)
548 while (sel->child->u.cgrp.index[j] < g->index[i])
550 ++j;
552 sel->v.u.r[i] = expr->v.u.r[j];
555 break;
557 case STR_VALUE:
558 if (!g)
560 sel->v.nr = expr->v.nr;
561 memcpy(sel->v.u.s, expr->v.u.s, sel->v.nr*sizeof(*sel->v.u.s));
563 else
565 sel->v.nr = g->isize;
566 /* Extract the values corresponding to g */
567 for (i = j = 0; i < g->isize; ++i, ++j)
569 while (sel->child->u.cgrp.index[j] < g->index[i])
571 ++j;
573 sel->v.u.s[i] = expr->v.u.s[j];
576 break;
578 case POS_VALUE:
579 /* Currently, there is no need to do anything fancy here,
580 * but some future extensions may need a more flexible
581 * implementation. */
582 gmx_ana_pos_copy(sel->v.u.p, expr->v.u.p, FALSE);
583 break;
585 case GROUP_VALUE:
586 if (!g)
588 gmx_ana_index_copy(sel->v.u.g, expr->v.u.g, FALSE);
590 else
592 gmx_ana_index_intersection(sel->v.u.g, expr->v.u.g, g);
594 break;
596 default: /* should not be reached */
597 gmx_bug("invalid subexpression reference type");
598 return -1;
600 /* Store the number of values if needed */
601 if (sel->u.param)
603 sel->u.param->val.nr = sel->v.nr;
604 if (sel->u.param->nvalptr)
606 *sel->u.param->nvalptr = sel->u.param->val.nr;
609 return 0;
612 /********************************************************************
613 * METHOD EXPRESSION EVALUATION
614 ********************************************************************/
617 * \param[in] data Data for the current frame.
618 * \param[in] sel Selection element being evaluated.
619 * \param[in] g Group for which \p sel should be evaluated.
620 * \returns 0 on success, a non-zero error code on error.
622 * Evaluates each child of a \ref SEL_EXPRESSION element.
623 * The value of \p sel is not touched.
625 * This function is not used as \c t_selelem::evaluate,
626 * but is used internally.
629 _gmx_sel_evaluate_method_params(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
631 t_selelem *child;
632 int rc;
634 child = sel->child;
635 while (child)
637 if (child->evaluate && !(child->flags & SEL_EVALFRAME))
639 if (child->flags & SEL_ATOMVAL)
641 rc = child->evaluate(data, child, g);
643 else
645 rc = child->evaluate(data, child, NULL);
646 child->flags |= SEL_EVALFRAME;
648 if (rc != 0)
650 return rc;
653 child = child->next;
655 return 0;
659 * \param[in] data Data for the current frame.
660 * \param[in] sel Selection element being evaluated.
661 * \param[in] g Group for which \p sel should be evaluated.
662 * \returns 0 on success, a non-zero error code on error.
664 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
665 * to evaluate any parameter values.
666 * If this is the first time this expression is evaluated for
667 * the frame, sel_framefunc() callback is called if one is provided.
668 * If a reference position calculation has been initialized for this element,
669 * the positions are also updated, and sel_updatefunc_pos() is used to
670 * evaluate the value. Otherwise, sel_updatefunc() is used.
672 * This function is used as \c t_selelem::evaluate for \ref SEL_EXPRESSION
673 * elements.
676 _gmx_sel_evaluate_method(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
678 int rc;
680 rc = _gmx_sel_evaluate_method_params(data, sel, g);
681 if (rc != 0)
683 return rc;
685 if (sel->flags & SEL_INITFRAME)
687 rc = sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
688 sel->u.expr.mdata);
689 sel->flags &= ~SEL_INITFRAME;
690 if (rc != 0)
692 return rc;
695 if (sel->u.expr.pc)
697 gmx_ana_poscalc_update(sel->u.expr.pc, sel->u.expr.pos, g,
698 data->fr->x, data->pbc);
699 rc = sel->u.expr.method->pupdate(data->top, data->fr, data->pbc,
700 sel->u.expr.pos, &sel->v,
701 sel->u.expr.mdata);
703 else
705 rc = sel->u.expr.method->update(data->top, data->fr, data->pbc, g,
706 &sel->v, sel->u.expr.mdata);
708 return rc;
712 * \param[in] data Data for the current frame.
713 * \param[in] sel Selection element being evaluated.
714 * \param[in] g Group for which \p sel should be evaluated.
715 * \returns 0 on success, a non-zero error code on error.
717 * Evaluates all child selections (using _gmx_sel_evaluate_method_params())
718 * to evaluate any parameter values.
719 * If this is the first time this expression is evaluated for
720 * the frame, sel_framefunc() callback is called if one is provided.
721 * The modifier is then evaluated using sel_updatefunc_pos().
723 * This function is used as \c t_selelem::evaluate for \ref SEL_MODIFIER
724 * elements.
727 _gmx_sel_evaluate_modifier(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
729 int rc;
731 rc = _gmx_sel_evaluate_method_params(data, sel, g);
732 if (rc != 0)
734 return rc;
736 if (sel->flags & SEL_INITFRAME)
738 rc = sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
739 sel->u.expr.mdata);
740 sel->flags &= ~SEL_INITFRAME;
741 if (rc != 0)
743 return rc;
746 if (sel->child->v.type != POS_VALUE)
748 gmx_bug("non-position valued modifiers not implemented");
749 return -1;
751 rc = sel->u.expr.method->pupdate(data->top, data->fr, data->pbc,
752 sel->child->v.u.p,
753 &sel->v, sel->u.expr.mdata);
754 return rc;
758 /********************************************************************
759 * BOOLEAN EXPRESSION EVALUATION
760 ********************************************************************/
763 * \param[in] data Data for the current frame.
764 * \param[in] sel Selection element being evaluated.
765 * \param[in] g Group for which \p sel should be evaluated.
766 * \returns 0 on success, a non-zero error code on error.
768 * Evaluates the child element (there should be only one) in the group
769 * \p g, and then sets the value of \p sel to the complement of the
770 * child value.
772 * This function is used as \c t_selelem::evaluate for \ref SEL_BOOLEAN
773 * elements with \ref BOOL_NOT.
776 _gmx_sel_evaluate_not(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
778 int rc;
780 rc = sel->child->evaluate(data, sel->child, g);
781 if (rc != 0)
783 return rc;
785 gmx_ana_index_difference(sel->v.u.g, g, sel->child->v.u.g);
786 return 0;
790 * \param[in] data Data for the current frame.
791 * \param[in] sel Selection element being evaluated.
792 * \param[in] g Group for which \p sel should be evaluated.
793 * \returns 0 on success, a non-zero error code on error.
795 * Short-circuiting evaluation of logical AND expressions.
797 * Starts by evaluating the first child element in the group \p g.
798 * The each following child element is evaluated in the intersection
799 * of all the previous values until all children have been evaluated
800 * or the intersection becomes empty.
801 * The value of \p sel is set to the intersection of all the (evaluated)
802 * child values.
804 * If the first child does not have an evaluation function, it is skipped
805 * and the evaluation is started at the second child.
806 * This happens if the first child is a constant expression and during
807 * compilation it was detected that the evaluation group is always a subset
808 * of the constant group
809 * (currently, the compiler never detects this).
811 * This function is used as \c t_selelem::evaluate for \ref SEL_BOOLEAN
812 * elements with \ref BOOL_AND.
815 _gmx_sel_evaluate_and(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
817 t_selelem *child;
818 int rc;
820 child = sel->child;
821 /* Skip the first child if it does not have an evaluation function. */
822 if (!child->evaluate)
824 child = child->next;
826 rc = child->evaluate(data, child, g);
827 if (rc != 0)
829 return rc;
831 gmx_ana_index_copy(sel->v.u.g, child->v.u.g, FALSE);
832 child = child->next;
833 while (child && sel->v.u.g->isize > 0)
835 rc = child->evaluate(data, child, sel->v.u.g);
836 if (rc != 0)
838 return rc;
840 gmx_ana_index_intersection(sel->v.u.g, sel->v.u.g, child->v.u.g);
841 child = child->next;
843 return 0;
847 * \param[in] data Data for the current frame.
848 * \param[in] sel Selection element being evaluated.
849 * \param[in] g Group for which \p sel should be evaluated.
850 * \returns 0 on success, a non-zero error code on error.
852 * Short-circuiting evaluation of logical OR expressions.
854 * Starts by evaluating the first child element in the group \p g.
855 * For each subsequent child, finds the part of \p g that is not
856 * included the value of any previous child, and evaluates the child
857 * in that group until the last child is evaluated or all of \p g
858 * is included in some child value.
859 * The value of \p sel is set to the union of all the (evaluated)
860 * child values.
862 * If the first child does not have an evaluation function, its value is
863 * used without evaluation.
864 * This happens if the first child is a constant expression, the selection
865 * has been compiled, and the evaluation group is the same for each frame.
866 * In this case, the compiler has taken care of that the child value is a
867 * subset of \p g, making it unnecessary to evaluate it.
869 * This function is used as \c t_selelem::evaluate for \ref SEL_BOOLEAN
870 * elements with \ref BOOL_OR.
873 _gmx_sel_evaluate_or(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
875 t_selelem *child;
876 gmx_ana_index_t tmp, tmp2;
877 int rc;
879 child = sel->child;
880 if (child->evaluate)
882 rc = child->evaluate(data, child, g);
883 if (rc != 0)
885 return rc;
888 gmx_ana_index_partition(sel->v.u.g, &tmp, g, child->v.u.g);
889 child = child->next;
890 while (child && tmp.isize > 0)
892 tmp.name = NULL;
893 rc = child->evaluate(data, child, &tmp);
894 if (rc != 0)
896 return rc;
898 gmx_ana_index_partition(&tmp, &tmp2, &tmp, child->v.u.g);
899 sel->v.u.g->isize += tmp.isize;
900 tmp.isize = tmp2.isize;
901 tmp.index = tmp2.index;
902 child = child->next;
904 gmx_ana_index_sort(sel->v.u.g);
905 return 0;