Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / selection / evaluate.cpp
index ec33ed5243dc8b17d57ff05b868f5ddd6b442c8d..dd3d3d1de59bccd2c6708afc0276ef6337408312 100644 (file)
@@ -1,32 +1,36 @@
 /*
+ * This file is part of the GROMACS molecular simulation package.
  *
- *                This source code is part of
+ * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
  *
- *                 G   R   O   M   A   C   S
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
  *
- *          GROningen MAchine for Chemical Simulations
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2009, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
  *
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
  *
  * To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
- *
- * For more info, check our website at http://www.gromacs.org
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
 /*! \internal \file
  * \brief
  * converting selections by residue/molecule into selections by atom
  * when necessary.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_selection
  */
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "gmxpre.h"
 
-#include <string.h>
+#include "evaluate.h"
 
-#include "maths.h"
-#include "smalloc.h"
-#include "vec.h"
+#include <string.h>
 
+#include "gromacs/math/utilities.h"
+#include "gromacs/math/vec.h"
 #include "gromacs/selection/indexutil.h"
-#include "gromacs/selection/poscalc.h"
 #include "gromacs/selection/selection.h"
-#include "gromacs/selection/selmethod.h"
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/gmxassert.h"
+#include "gromacs/utility/smalloc.h"
 
-#include "evaluate.h"
 #include "mempool.h"
+#include "poscalc.h"
 #include "selectioncollection-impl.h"
 #include "selelem.h"
+#include "selmethod.h"
+
+using gmx::SelectionTreeElement;
+using gmx::SelectionTreeElementPointer;
 
 namespace
 {
 
-/*! \internal \brief
+/*! \brief
  * Reserves memory for a selection element from the evaluation memory pool.
  *
  * This class implements RAII semantics for allocating memory for selection
@@ -83,7 +88,7 @@ class MempoolSelelemReserver
 {
     public:
         //! Constructs a reserver without initial reservation.
-        MempoolSelelemReserver() : sel_(NULL) {}
+        MempoolSelelemReserver() {}
         /*! \brief
          * Constructs a reserver with initial reservation.
          *
@@ -92,17 +97,16 @@ class MempoolSelelemReserver
          *
          * \see reserve()
          */
-        MempoolSelelemReserver(t_selelem *sel, int count)
-            : sel_(NULL)
+        MempoolSelelemReserver(const SelectionTreeElementPointer &sel, int count)
         {
             reserve(sel, count);
         }
         //! Frees any memory allocated using this reserver.
         ~MempoolSelelemReserver()
         {
-            if (sel_ != NULL)
+            if (sel_)
             {
-                _gmx_selelem_mempool_release(sel_);
+                sel_->mempoolRelease();
             }
         }
 
@@ -117,18 +121,19 @@ class MempoolSelelemReserver
          * memory pool.  Type of values to allocate is automatically determined
          * from \p sel.
          */
-        void reserve(t_selelem *sel, int count)
+        void reserve(const SelectionTreeElementPointer &sel, int count)
         {
-            GMX_RELEASE_ASSERT(sel_ == NULL, "Can only reserve one element with one instance");
-            _gmx_selelem_mempool_reserve(sel, count);
+            GMX_RELEASE_ASSERT(!sel_,
+                               "Can only reserve one element with one instance");
+            sel->mempoolReserve(count);
             sel_ = sel;
         }
 
     private:
-        t_selelem              *sel_;
+        SelectionTreeElementPointer     sel_;
 };
 
-/*! \internal \brief
+/*! \brief
  * Reserves memory for an index group from the evaluation memory pool.
  *
  * This class implements RAII semantics for allocating memory for an index
@@ -178,7 +183,7 @@ class MempoolGroupReserver
         gmx_ana_index_t        *g_;
 };
 
-/*! \internal \brief
+/*! \brief
  * Assigns a temporary value for a selection element.
  *
  * This class implements RAII semantics for temporarily assigning the value
@@ -191,7 +196,7 @@ class SelelemTemporaryValueAssigner
     public:
         //! Constructs an assigner without an initial assignment.
         SelelemTemporaryValueAssigner()
-            : sel_(NULL), old_ptr_(NULL), old_nalloc_(0)
+            : old_ptr_(NULL), old_nalloc_(0)
         {
         }
         /*! \brief
@@ -202,15 +207,15 @@ class SelelemTemporaryValueAssigner
          *
          * \see assign()
          */
-        SelelemTemporaryValueAssigner(t_selelem *sel, t_selelem *vsource)
-            : sel_(NULL)
+        SelelemTemporaryValueAssigner(const SelectionTreeElementPointer &sel,
+                                      const SelectionTreeElement        &vsource)
         {
             assign(sel, vsource);
         }
         //! Undoes any temporary assignment done using this assigner.
         ~SelelemTemporaryValueAssigner()
         {
-            if (sel_ != NULL)
+            if (sel_)
             {
                 _gmx_selvalue_setstore_alloc(&sel_->v, old_ptr_, old_nalloc_);
             }
@@ -226,22 +231,23 @@ class SelelemTemporaryValueAssigner
          * \p vsource, i.e., any access/modification to values in \p sel
          * actually accesses values in \p vsource.
          */
-        void assign(t_selelem *sel, t_selelem *vsource)
+        void assign(const SelectionTreeElementPointer &sel,
+                    const SelectionTreeElement        &vsource)
         {
-            GMX_RELEASE_ASSERT(sel_ == NULL,
+            GMX_RELEASE_ASSERT(!sel_,
                                "Can only assign one element with one instance");
-            GMX_RELEASE_ASSERT(sel->v.type == vsource->v.type,
+            GMX_RELEASE_ASSERT(sel->v.type == vsource.v.type,
                                "Mismatching selection value types");
-            old_ptr_ = sel->v.u.ptr;
+            old_ptr_    = sel->v.u.ptr;
             old_nalloc_ = sel->v.nalloc;
-            _gmx_selvalue_setstore(&sel->v, vsource->v.u.ptr);
+            _gmx_selvalue_setstore(&sel->v, vsource.v.u.ptr);
             sel_ = sel;
         }
 
     private:
-        t_selelem              *sel_;
-        void                   *old_ptr_;
-        int                     old_nalloc_;
+        SelectionTreeElementPointer     sel_;
+        void                           *old_ptr_;
+        int                             old_nalloc_;
 };
 
 } // namespace
@@ -251,38 +257,68 @@ class SelelemTemporaryValueAssigner
  * \param[in] evalfunc Function pointer to print.
  */
 void
-_gmx_sel_print_evalfunc_name(FILE *fp, sel_evalfunc evalfunc)
+_gmx_sel_print_evalfunc_name(FILE *fp, gmx::sel_evalfunc evalfunc)
 {
     if (!evalfunc)
+    {
         fprintf(fp, "none");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_root)
+    {
         fprintf(fp, "root");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_static)
+    {
         fprintf(fp, "static");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_subexpr_simple)
+    {
         fprintf(fp, "subexpr_simple");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_subexpr_staticeval)
+    {
         fprintf(fp, "subexpr_staticeval");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_subexpr)
+    {
         fprintf(fp, "subexpr");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_subexprref_simple)
+    {
         fprintf(fp, "ref_simple");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_subexprref)
+    {
         fprintf(fp, "ref");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_method)
+    {
         fprintf(fp, "method");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_modifier)
+    {
         fprintf(fp, "mod");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_not)
+    {
         fprintf(fp, "not");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_and)
+    {
         fprintf(fp, "and");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_or)
+    {
         fprintf(fp, "or");
+    }
     else if (evalfunc == &_gmx_sel_evaluate_arithmetic)
+    {
         fprintf(fp, "arithmetic");
+    }
     else
+    {
         fprintf(fp, "%p", (void*)(evalfunc));
+    }
 }
 
 /*!
@@ -317,7 +353,7 @@ _gmx_sel_evaluate_init(gmx_sel_evaluate_t *data,
  * The \ref SEL_EVALFRAME flag is cleared for all elements.
  */
 static void
-init_frame_eval(t_selelem *sel)
+init_frame_eval(SelectionTreeElementPointer sel)
 {
     while (sel)
     {
@@ -363,23 +399,23 @@ SelectionEvaluator::evaluate(SelectionCollection *coll,
                              t_trxframe *fr, t_pbc *pbc)
 {
     gmx_ana_selcollection_t *sc = &coll->impl_->sc_;
-    gmx_sel_evaluate_t  data;
-    t_selelem          *sel;
+    gmx_sel_evaluate_t       data;
 
     _gmx_sel_evaluate_init(&data, sc->mempool, &sc->gall, sc->top, fr, pbc);
     init_frame_eval(sc->root);
-    sel = sc->root;
+    SelectionTreeElementPointer sel = sc->root;
     while (sel)
     {
         /* Clear the evaluation group of subexpressions */
-        if (sel->child && sel->child->type == SEL_SUBEXPR)
+        if (sel->child && sel->child->type == SEL_SUBEXPR
+            && sel->child->evaluate != NULL)
         {
             sel->child->u.cgrp.isize = 0;
             /* Not strictly necessary, because the value will be overwritten
              * during first evaluation of the subexpression anyways, but we
              * clear the group for clarity. Note that this is _not_ done during
              * compilation because of some additional complexities involved
-             * (see compiler.c), so it should not be relied upon in
+             * (see compiler.cpp), so it should not be relied upon in
              * _gmx_sel_evaluate_subexpr(). */
             if (sel->child->v.type == GROUP_VALUE)
             {
@@ -397,7 +433,7 @@ SelectionEvaluator::evaluate(SelectionCollection *coll,
     for (isel = sc->sel.begin(); isel != sc->sel.end(); ++isel)
     {
         internal::SelectionData &sel = **isel;
-        sel.refreshMassesAndCharges();
+        sel.refreshMassesAndCharges(sc->top);
         sel.updateCoveredFractionForFrame();
     }
 }
@@ -409,13 +445,13 @@ SelectionEvaluator::evaluate(SelectionCollection *coll,
 void
 SelectionEvaluator::evaluateFinal(SelectionCollection *coll, int nframes)
 {
-    gmx_ana_selcollection_t *sc = &coll->impl_->sc_;
+    gmx_ana_selcollection_t          *sc = &coll->impl_->sc_;
 
     SelectionDataList::const_iterator isel;
     for (isel = sc->sel.begin(); isel != sc->sel.end(); ++isel)
     {
         internal::SelectionData &sel = **isel;
-        sel.restoreOriginalPositions();
+        sel.restoreOriginalPositions(sc->top);
         sel.computeAverageCoveredFraction(nframes);
     }
 }
@@ -431,12 +467,11 @@ SelectionEvaluator::evaluateFinal(SelectionCollection *coll, int nframes)
  * Evaluates each child of \p sel in \p g.
  */
 void
-_gmx_sel_evaluate_children(gmx_sel_evaluate_t *data, t_selelem *sel,
-                           gmx_ana_index_t *g)
+_gmx_sel_evaluate_children(gmx_sel_evaluate_t                     *data,
+                           const gmx::SelectionTreeElementPointer &sel,
+                           gmx_ana_index_t                        *g)
 {
-    t_selelem  *child;
-
-    child = sel->child;
+    SelectionTreeElementPointer child = sel->child;
     while (child)
     {
         if (child->evaluate)
@@ -447,23 +482,10 @@ _gmx_sel_evaluate_children(gmx_sel_evaluate_t *data, t_selelem *sel,
     }
 }
 
-/*!
- * \param[in] data Data for the current frame.
- * \param[in] sel Selection element being evaluated.
- * \param[in] g   Group for which \p sel should be evaluated
- *   (not used, can be NULL).
- * \returns   0 on success, a non-zero error code on error.
- *
- * Evaluates the first child element in the group defined by \p sel->u.cgrp.
- * If \p sel->u.cgrp is empty, nothing is done.
- * The value of \p sel is not touched (root elements do not evaluate to
- * values).
- *
- * This function can be used as \c t_selelem::evaluate for \ref SEL_ROOT
- * elements.
- */
 void
-_gmx_sel_evaluate_root(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_root(gmx_sel_evaluate_t                     *data,
+                       const gmx::SelectionTreeElementPointer &sel,
+                       gmx_ana_index_t                         * /* g */)
 {
     if (sel->u.cgrp.isize == 0 || !sel->child->evaluate)
     {
@@ -474,21 +496,21 @@ _gmx_sel_evaluate_root(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t
                          sel->u.cgrp.isize < 0 ? NULL : &sel->u.cgrp);
 }
 
-/*!
- * \param[in] data Data for the current frame.
- * \param[in] sel Selection element being evaluated.
- * \param[in] g   Group for which \p sel should be evaluated.
- * \returns   0 for success.
- *
- * Sets the value of \p sel to the intersection of \p g and \p sel->u.cgrp.
- *
- * This function can be used as \c t_selelem::evaluate for \ref SEL_CONST
- * elements with value type \ref GROUP_VALUE.
- */
 void
-_gmx_sel_evaluate_static(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_static(gmx_sel_evaluate_t                      * /* data */,
+                         const gmx::SelectionTreeElementPointer &sel,
+                         gmx_ana_index_t                        *g)
 {
-    gmx_ana_index_intersection(sel->v.u.g, &sel->u.cgrp, g);
+    if (sel->flags & SEL_UNSORTED)
+    {
+        // This only works if g contains all the atoms, but that is currently
+        // the only supported case.
+        gmx_ana_index_copy(sel->v.u.g, &sel->u.cgrp, false);
+    }
+    else
+    {
+        gmx_ana_index_intersection(sel->v.u.g, &sel->u.cgrp, g);
+    }
 }
 
 
@@ -506,12 +528,14 @@ _gmx_sel_evaluate_static(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index
  * The compiler has taken care that the child actually stores the evaluated
  * value in the value pointer of this element.
  *
- * This function is used as \c t_selelem::evaluate for \ref SEL_SUBEXPR
- * elements that are used only once, and hence do not need full subexpression
- * handling.
+ * This function is used as gmx::SelectionTreeElement::evaluate for
+ * \ref SEL_SUBEXPR elements that are used only once, and hence do not need
+ * full subexpression handling.
  */
 void
-_gmx_sel_evaluate_subexpr_simple(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_subexpr_simple(gmx_sel_evaluate_t                     *data,
+                                 const gmx::SelectionTreeElementPointer &sel,
+                                 gmx_ana_index_t                        *g)
 {
     if (sel->child->evaluate)
     {
@@ -532,18 +556,27 @@ _gmx_sel_evaluate_subexpr_simple(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_a
  * value in the value pointer of this element.
  * Assumes that \p g is persistent for the duration of the whole evaluation.
  *
- * This function is used as \c t_selelem::evaluate for \ref SEL_SUBEXPR
- * elements that have a static evaluation group, and hence do not need full
- * subexpression handling.
+ * This function is used as gmx::SelectionTreeElement::evaluate for
+ * \ref SEL_SUBEXPR elements that have a static evaluation group, and hence do
+ * not need full subexpression handling.
  */
 void
-_gmx_sel_evaluate_subexpr_staticeval(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_subexpr_staticeval(gmx_sel_evaluate_t                     *data,
+                                     const gmx::SelectionTreeElementPointer &sel,
+                                     gmx_ana_index_t                        *g)
 {
     if (sel->u.cgrp.isize == 0)
     {
         sel->child->evaluate(data, sel->child, g);
         sel->v.nr = sel->child->v.nr;
-        gmx_ana_index_set(&sel->u.cgrp, g->isize, g->index, sel->u.cgrp.name, 0);
+        if (!g)
+        {
+            sel->u.cgrp.isize = -1;
+        }
+        else
+        {
+            gmx_ana_index_set(&sel->u.cgrp, g->isize, g->index, 0);
+        }
     }
 }
 
@@ -568,23 +601,21 @@ _gmx_sel_evaluate_subexpr_staticeval(gmx_sel_evaluate_t *data, t_selelem *sel, g
  * major problem.
  */
 void
-_gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t                     *data,
+                          const gmx::SelectionTreeElementPointer &sel,
+                          gmx_ana_index_t                        *g)
 {
-    gmx_ana_index_t  gmiss;
+    gmx_ana_index_t      gmiss;
 
     MempoolGroupReserver gmissreserver(data->mp);
     if (sel->u.cgrp.isize == 0)
     {
         {
-            SelelemTemporaryValueAssigner assigner(sel->child, sel);
+            SelelemTemporaryValueAssigner assigner(sel->child, *sel);
             sel->child->evaluate(data, sel->child, g);
         }
-        /* We need to keep the name for the cgrp across the copy to avoid
-         * problems if g has a name set. */
-        char *name = sel->u.cgrp.name;
         gmx_ana_index_copy(&sel->u.cgrp, g, false);
-        sel->u.cgrp.name = name;
-        gmiss.isize = 0;
+        gmiss.isize      = 0;
     }
     else
     {
@@ -616,11 +647,11 @@ _gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_inde
                     {
                         if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
                         {
-                            sel->v.u.i[k] = sel->v.u.i[j--];
+                            sel->v.u.i[k] = sel->child->v.u.i[j--];
                         }
                         else
                         {
-                            sel->v.u.i[k] = sel->child->v.u.i[i--];
+                            sel->v.u.i[k] = sel->v.u.i[i--];
                         }
                     }
                     break;
@@ -630,25 +661,27 @@ _gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_inde
                     {
                         if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
                         {
-                            sel->v.u.r[k] = sel->v.u.r[j--];
+                            sel->v.u.r[k] = sel->child->v.u.r[j--];
                         }
                         else
                         {
-                            sel->v.u.r[k] = sel->child->v.u.r[i--];
+                            sel->v.u.r[k] = sel->v.u.r[i--];
                         }
                     }
                     break;
 
                 case STR_VALUE:
+                    // Note: with the currently allowed syntax, this case is never
+                    // reached.
                     for (k = sel->u.cgrp.isize + gmiss.isize - 1; k >= 0; k--)
                     {
                         if (i < 0 || (j >= 0 && sel->u.cgrp.index[i] < gmiss.index[j]))
                         {
-                            sel->v.u.s[k] = sel->v.u.s[j--];
+                            sel->v.u.s[k] = sel->child->v.u.s[j--];
                         }
                         else
                         {
-                            sel->v.u.s[k] = sel->child->v.u.s[i--];
+                            sel->v.u.s[k] = sel->v.u.s[i--];
                         }
                     }
                     break;
@@ -676,11 +709,14 @@ _gmx_sel_evaluate_subexpr(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_inde
  * memory as the value pointer of this element to avoid copying, and then
  * evaluates evaluates the child.
  *
- * This function is used as \c t_selelem:evaluate for \ref SEL_SUBEXPRREF
- * elements for which the \ref SEL_SUBEXPR does not have other references.
+ * This function is used as gmx::SelectionTreeElement:evaluate for
+ * \ref SEL_SUBEXPRREF elements for which the \ref SEL_SUBEXPR does not have
+ * other references.
  */
 void
-_gmx_sel_evaluate_subexprref_simple(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_subexprref_simple(gmx_sel_evaluate_t                     *data,
+                                    const gmx::SelectionTreeElementPointer &sel,
+                                    gmx_ana_index_t                        *g)
 {
     if (g)
     {
@@ -713,20 +749,21 @@ _gmx_sel_evaluate_subexprref_simple(gmx_sel_evaluate_t *data, t_selelem *sel, gm
  * group \p g, and the value of the child is then copied.
  * There should be only one child element.
  *
- * This function is used as \c t_selelem::evaluate for \ref SEL_SUBEXPRREF
- * elements.
+ * This function is used as gmx::SelectionTreeElement::evaluate for
+ * \ref SEL_SUBEXPRREF elements.
  */
 void
-_gmx_sel_evaluate_subexprref(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_subexprref(gmx_sel_evaluate_t                     *data,
+                             const gmx::SelectionTreeElementPointer &sel,
+                             gmx_ana_index_t                        *g)
 {
-    t_selelem *expr;
     int        i, j;
 
-    if (g)
+    if (g != NULL && sel->child->evaluate != NULL)
     {
         sel->child->evaluate(data, sel->child, g);
     }
-    expr = sel->child;
+    const SelectionTreeElementPointer &expr = sel->child;
     switch (sel->v.type)
     {
         case INT_VALUE:
@@ -837,15 +874,15 @@ _gmx_sel_evaluate_subexprref(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_i
  * Evaluates each child of a \ref SEL_EXPRESSION element.
  * The value of \p sel is not touched.
  *
- * This function is not used as \c t_selelem::evaluate,
+ * This function is not used as gmx::SelectionTreeElement::evaluate,
  * but is used internally.
  */
 void
-_gmx_sel_evaluate_method_params(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_method_params(gmx_sel_evaluate_t                     *data,
+                                const gmx::SelectionTreeElementPointer &sel,
+                                gmx_ana_index_t                        *g)
 {
-    t_selelem *child;
-
-    child = sel->child;
+    SelectionTreeElementPointer child = sel->child;
     while (child)
     {
         if (child->evaluate && !(child->flags & SEL_EVALFRAME))
@@ -878,11 +915,13 @@ _gmx_sel_evaluate_method_params(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_an
  * the positions are also updated, and sel_updatefunc_pos() is used to
  * evaluate the value. Otherwise, sel_updatefunc() is used.
  *
- * This function is used as \c t_selelem::evaluate for \ref SEL_EXPRESSION
- * elements.
+ * This function is used as gmx::SelectionTreeElement::evaluate for
+ * \ref SEL_EXPRESSION elements.
  */
 void
-_gmx_sel_evaluate_method(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_method(gmx_sel_evaluate_t                     *data,
+                         const gmx::SelectionTreeElementPointer &sel,
+                         gmx_ana_index_t                        *g)
 {
     _gmx_sel_evaluate_method_params(data, sel, g);
     if (sel->flags & SEL_INITFRAME)
@@ -918,18 +957,20 @@ _gmx_sel_evaluate_method(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index
  * the frame, sel_framefunc() callback is called if one is provided.
  * The modifier is then evaluated using sel_updatefunc_pos().
  *
- * This function is used as \c t_selelem::evaluate for \ref SEL_MODIFIER
- * elements.
+ * This function is used as gmx::SelectionTreeElement::evaluate for
+ * \ref SEL_MODIFIER elements.
  */
 void
-_gmx_sel_evaluate_modifier(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_modifier(gmx_sel_evaluate_t                     *data,
+                           const gmx::SelectionTreeElementPointer &sel,
+                           gmx_ana_index_t                        *g)
 {
     _gmx_sel_evaluate_method_params(data, sel, g);
     if (sel->flags & SEL_INITFRAME)
     {
         sel->flags &= ~SEL_INITFRAME;
         sel->u.expr.method->init_frame(data->top, data->fr, data->pbc,
-                                            sel->u.expr.mdata);
+                                       sel->u.expr.mdata);
     }
     GMX_RELEASE_ASSERT(sel->child != NULL,
                        "Modifier element with a value must have a child");
@@ -954,14 +995,16 @@ _gmx_sel_evaluate_modifier(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_ind
  * \returns   0 on success, a non-zero error code on error.
  *
  * Evaluates the child element (there should be only one) in the group
- * \p g, and then sets the value of \p sel to the complement of the 
+ * \p g, and then sets the value of \p sel to the complement of the
  * child value.
  *
- * This function is used as \c t_selelem::evaluate for \ref SEL_BOOLEAN
- * elements with \ref BOOL_NOT.
+ * This function is used as gmx::SelectionTreeElement::evaluate for
+ * \ref SEL_BOOLEAN elements with \ref BOOL_NOT.
  */
 void
-_gmx_sel_evaluate_not(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_not(gmx_sel_evaluate_t                     *data,
+                      const gmx::SelectionTreeElementPointer &sel,
+                      gmx_ana_index_t                        *g)
 {
     MempoolSelelemReserver reserver(sel->child, g->isize);
     sel->child->evaluate(data, sel->child, g);
@@ -990,15 +1033,15 @@ _gmx_sel_evaluate_not(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t
  * of the constant group
  * (currently, the compiler never detects this).
  *
- * This function is used as \c t_selelem::evaluate for \ref SEL_BOOLEAN
- * elements with \ref BOOL_AND.
+ * This function is used as gmx::SelectionTreeElement::evaluate for
+ * \ref SEL_BOOLEAN elements with \ref BOOL_AND.
  */
 void
-_gmx_sel_evaluate_and(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_and(gmx_sel_evaluate_t                     *data,
+                      const gmx::SelectionTreeElementPointer &sel,
+                      gmx_ana_index_t                        *g)
 {
-    t_selelem *child;
-
-    child = sel->child;
+    SelectionTreeElementPointer child = sel->child;
     /* Skip the first child if it does not have an evaluation function. */
     if (!child->evaluate)
     {
@@ -1043,16 +1086,17 @@ _gmx_sel_evaluate_and(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t
  * In this case, the compiler has taken care of that the child value is a
  * subset of \p g, making it unnecessary to evaluate it.
  *
- * This function is used as \c t_selelem::evaluate for \ref SEL_BOOLEAN
- * elements with \ref BOOL_OR.
+ * This function is used as gmx::SelectionTreeElement::evaluate for
+ * \ref SEL_BOOLEAN elements with \ref BOOL_OR.
  */
 void
-_gmx_sel_evaluate_or(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *g)
+_gmx_sel_evaluate_or(gmx_sel_evaluate_t                     *data,
+                     const gmx::SelectionTreeElementPointer &sel,
+                     gmx_ana_index_t                        *g)
 {
-    t_selelem     *child;
-    gmx_ana_index_t  tmp, tmp2;
+    gmx_ana_index_t             tmp, tmp2;
 
-    child = sel->child;
+    SelectionTreeElementPointer child = sel->child;
     if (child->evaluate)
     {
         MempoolSelelemReserver reserver(child, g->isize);
@@ -1066,16 +1110,15 @@ _gmx_sel_evaluate_or(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *
     child = child->next;
     while (child && tmp.isize > 0)
     {
-        tmp.name = NULL;
         {
             MempoolSelelemReserver reserver(child, tmp.isize);
             child->evaluate(data, child, &tmp);
             gmx_ana_index_partition(&tmp, &tmp2, &tmp, child->v.u.g);
         }
         sel->v.u.g->isize += tmp.isize;
-        tmp.isize = tmp2.isize;
-        tmp.index = tmp2.index;
-        child = child->next;
+        tmp.isize          = tmp2.isize;
+        tmp.index          = tmp2.index;
+        child              = child->next;
     }
     gmx_ana_index_sort(sel->v.u.g);
 }
@@ -1092,20 +1135,21 @@ _gmx_sel_evaluate_or(gmx_sel_evaluate_t *data, t_selelem *sel, gmx_ana_index_t *
  * \returns   0 on success, a non-zero error code on error.
  */
 void
-_gmx_sel_evaluate_arithmetic(gmx_sel_evaluate_t *data, t_selelem *sel,
-                             gmx_ana_index_t *g)
+_gmx_sel_evaluate_arithmetic(gmx_sel_evaluate_t                     *data,
+                             const gmx::SelectionTreeElementPointer &sel,
+                             gmx_ana_index_t                        *g)
 {
     int         n, i, i1, i2;
-    real        lval, rval=0., val=0.;
+    real        lval, rval = 0., val = 0.;
 
-    t_selelem  *const left  = sel->child;
-    t_selelem  *const right = left->next;
+    const SelectionTreeElementPointer &left  = sel->child;
+    const SelectionTreeElementPointer &right = left->next;
 
-    SelelemTemporaryValueAssigner assigner;
-    MempoolSelelemReserver reserver;
+    SelelemTemporaryValueAssigner      assigner;
+    MempoolSelelemReserver             reserver;
     if (left->mempool)
     {
-        assigner.assign(left, sel);
+        assigner.assign(left, *sel);
         if (right)
         {
             reserver.reserve(right, g->isize);
@@ -1113,11 +1157,11 @@ _gmx_sel_evaluate_arithmetic(gmx_sel_evaluate_t *data, t_selelem *sel,
     }
     else if (right && right->mempool)
     {
-        assigner.assign(right, sel);
+        assigner.assign(right, *sel);
     }
     _gmx_sel_evaluate_children(data, sel, g);
 
-    n = (sel->flags & SEL_SINGLEVAL) ? 1 : g->isize;
+    n         = (sel->flags & SEL_SINGLEVAL) ? 1 : g->isize;
     sel->v.nr = n;
 
     bool bArithNeg = (sel->u.arith.type == ARITH_NEG);