Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / selection / sm_distance.cpp
index e338efac7c32cc83b068183504aad75caaefd81f..636d28c1124a042945426a1dcbb3be9267c6ec9a 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
  * This file implements the \p distance, \p mindistance and \p within
  * selection methods.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_selection
  */
-#include "gromacs/legacyheaders/macros.h"
-#include "gromacs/legacyheaders/pbc.h"
-#include "gromacs/legacyheaders/smalloc.h"
-#include "gromacs/legacyheaders/vec.h"
+#include "gmxpre.h"
 
+#include "gromacs/legacyheaders/macros.h"
+#include "gromacs/math/vec.h"
 #include "gromacs/selection/nbsearch.h"
 #include "gromacs/selection/position.h"
-#include "gromacs/selection/selmethod.h"
 #include "gromacs/utility/exceptions.h"
 
-/*! \internal \brief
+#include "selmethod.h"
+
+/*! \internal
+ * \brief
  * Data structure for distance-based selection method.
  *
  * The same data structure is used by all the distance-based methods.
+ *
+ * \ingroup module_selection
  */
-typedef struct
+struct t_methoddata_distance
 {
+    t_methoddata_distance() : cutoff(-1.0)
+    {
+    }
+
     /** Cutoff distance. */
-    real                cutoff;
+    real                             cutoff;
     /** Positions of the reference points. */
-    gmx_ana_pos_t       p;
+    gmx_ana_pos_t                    p;
     /** Neighborhood search data. */
-    gmx_ana_nbsearch_t *nb;
-} t_methoddata_distance;
+    gmx::AnalysisNeighborhood        nb;
+    /** Neighborhood search for an invididual frame. */
+    gmx::AnalysisNeighborhoodSearch  nbsearch;
+};
 
-/** Allocates data for distance-based selection methods. */
+/*! \brief
+ * Allocates data for distance-based selection methods.
+ *
+ * \param[in]     npar  Not used (should be 2).
+ * \param[in,out] param Method parameters (should point to one of the distance
+ *   parameter arrays).
+ * \returns       Pointer to the allocated data (\c t_methoddata_distance).
+ *
+ * Allocates memory for a \c t_methoddata_distance structure and
+ * initializes the parameter as follows:
+ *  - the first parameter defines the value for
+ *    \c t_methoddata_distance::cutoff.
+ *  - the second parameter defines the reference positions and the value is
+ *    stored in \c t_methoddata_distance::p.
+ */
 static void *
 init_data_common(int npar, gmx_ana_selparam_t *param);
-/** Initializes a distance-based selection method. */
+/*! \brief
+ * Initializes a distance-based selection method.
+ *
+ * \param   top   Not used.
+ * \param   npar  Not used (should be 2).
+ * \param   param Method parameters (should point to one of the distance
+ *   parameter arrays).
+ * \param   data  Pointer to \c t_methoddata_distance to initialize.
+ * \returns 0 on success, a non-zero error code on failure.
+ *
+ * Initializes the neighborhood search data structure
+ * (\c t_methoddata_distance::nb).
+ * Also checks that the cutoff is valid.
+ */
 static void
 init_common(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
 /** Frees the data allocated for a distance-based selection method. */
 static void
 free_data_common(void *data);
-/** Initializes the evaluation of a distance-based within selection method for a frame. */
+/*! \brief
+ * Initializes the evaluation of a distance-based within selection method for a
+ * frame.
+ *
+ * \param[in]  top  Not used.
+ * \param[in]  fr   Current frame.
+ * \param[in]  pbc  PBC structure.
+ * \param      data Should point to a \c t_methoddata_distance.
+ * \returns    0 on success, a non-zero error code on error.
+ *
+ * Initializes the neighborhood search for the current frame.
+ */
 static void
-init_frame_common(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data);
+init_frame_common(t_topology *top, t_trxframe * fr, t_pbc *pbc, void *data);
 /** Evaluates the \p distance selection method. */
 static void
-evaluate_distance(t_topology *top, t_trxframe *fr, t_pbc *pbc,
+evaluate_distance(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */,
                   gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data);
 /** Evaluates the \p within selection method. */
 static void
-evaluate_within(t_topology *top, t_trxframe *fr, t_pbc *pbc,
+evaluate_within(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */,
                 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data);
 
 /** Parameters for the \p distance selection method. */
@@ -123,7 +174,7 @@ static const char *help_distance[] = {
     "returned as equal to the cutoff.",
 };
 
-/** \internal Selection method data for the \p distance method. */
+/** Selection method data for the \p distance method. */
 gmx_ana_selmethod_t sm_distance = {
     "distance", REAL_VALUE, SMETH_DYNAMIC,
     asize(smparams_distance), smparams_distance,
@@ -138,7 +189,7 @@ gmx_ana_selmethod_t sm_distance = {
     {"distance from POS [cutoff REAL]", asize(help_distance), help_distance},
 };
 
-/** \internal Selection method data for the \p distance method. */
+/** Selection method data for the \p distance method. */
 gmx_ana_selmethod_t sm_mindistance = {
     "mindistance", REAL_VALUE, SMETH_DYNAMIC,
     asize(smparams_mindistance), smparams_mindistance,
@@ -153,7 +204,7 @@ gmx_ana_selmethod_t sm_mindistance = {
     {"mindistance from POS_EXPR [cutoff REAL]", asize(help_distance), help_distance},
 };
 
-/** \internal Selection method data for the \p within method. */
+/** Selection method data for the \p within method. */
 gmx_ana_selmethod_t sm_within = {
     "within", GROUP_VALUE, SMETH_DYNAMIC,
     asize(smparams_within), smparams_within,
@@ -168,53 +219,25 @@ gmx_ana_selmethod_t sm_within = {
     {"within REAL of POS_EXPR", asize(help_distance), help_distance},
 };
 
-/*!
- * \param[in]     npar  Not used (should be 2).
- * \param[in,out] param Method parameters (should point to one of the distance
- *   parameter arrays).
- * \returns       Pointer to the allocated data (\c t_methoddata_distance).
- *
- * Allocates memory for a \c t_methoddata_distance structure and
- * initializes the parameter as follows:
- *  - the first parameter defines the value for
- *    \c t_methoddata_distance::cutoff.
- *  - the second parameter defines the reference positions and the value is
- *    stored in \c t_methoddata_distance::p.
- */
 static void *
-init_data_common(int npar, gmx_ana_selparam_t *param)
+init_data_common(int /* npar */, gmx_ana_selparam_t *param)
 {
-    t_methoddata_distance *data;
-
-    snew(data, 1);
-    data->cutoff     = -1;
+    t_methoddata_distance *data = new t_methoddata_distance();
     param[0].val.u.r = &data->cutoff;
     param[1].val.u.p = &data->p;
     return data;
 }
 
-/*!
- * \param   top   Not used.
- * \param   npar  Not used (should be 2).
- * \param   param Method parameters (should point to one of the distance
- *   parameter arrays).
- * \param   data  Pointer to \c t_methoddata_distance to initialize.
- * \returns 0 on success, a non-zero error code on failure.
- *
- * Initializes the neighborhood search data structure
- * (\c t_methoddata_distance::nb).
- * Also checks that the cutoff is valid.
- */
 static void
-init_common(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
+init_common(t_topology * /* top */, int /* npar */, gmx_ana_selparam_t *param, void *data)
 {
-    t_methoddata_distance *d = (t_methoddata_distance *)data;
+    t_methoddata_distance *d = static_cast<t_methoddata_distance *>(data);
 
     if ((param[0].flags & SPAR_SET) && d->cutoff <= 0)
     {
         GMX_THROW(gmx::InvalidInputError("Distance cutoff should be > 0"));
     }
-    d->nb = gmx_ana_nbsearch_create(d->cutoff, d->p.nr);
+    d->nb.setCutoff(d->cutoff);
 }
 
 /*!
@@ -226,30 +249,17 @@ init_common(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
 static void
 free_data_common(void *data)
 {
-    t_methoddata_distance *d = (t_methoddata_distance *)data;
-
-    if (d->nb)
-    {
-        gmx_ana_nbsearch_free(d->nb);
-    }
-    sfree(d);
+    delete static_cast<t_methoddata_distance *>(data);
 }
 
-/*!
- * \param[in]  top  Not used.
- * \param[in]  fr   Current frame.
- * \param[in]  pbc  PBC structure.
- * \param      data Should point to a \c t_methoddata_distance.
- * \returns    0 on success, a non-zero error code on error.
- *
- * Initializes the neighborhood search for the current frame.
- */
 static void
-init_frame_common(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data)
+init_frame_common(t_topology * /* top */, t_trxframe * /* fr */, t_pbc *pbc, void *data)
 {
-    t_methoddata_distance *d = (t_methoddata_distance *)data;
+    t_methoddata_distance *d = static_cast<t_methoddata_distance *>(data);
 
-    gmx_ana_nbsearch_pos_init(d->nb, pbc, &d->p);
+    d->nbsearch.reset();
+    gmx::AnalysisNeighborhoodPositions pos(d->p.x, d->p.count());
+    d->nbsearch = d->nb.initSearch(pbc, pos);
 }
 
 /*!
@@ -260,20 +270,18 @@ init_frame_common(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data)
  * and puts them in \p out->u.r.
  */
 static void
-evaluate_distance(t_topology *top, t_trxframe *fr, t_pbc *pbc,
+evaluate_distance(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */,
                   gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data)
 {
-    t_methoddata_distance *d = (t_methoddata_distance *)data;
-    int  b, i;
-    real n;
+    t_methoddata_distance *d = static_cast<t_methoddata_distance *>(data);
 
-    out->nr = pos->g->isize;
-    for (b = 0; b < pos->nr; ++b)
+    out->nr = pos->m.mapb.nra;
+    for (int b = 0; b < pos->count(); ++b)
     {
-        n = gmx_ana_nbsearch_pos_mindist(d->nb, pos, b);
-        for (i = pos->m.mapb.index[b]; i < pos->m.mapb.index[b+1]; ++i)
+        real dist = d->nbsearch.minimumDistance(pos->x[b]);
+        for (int i = pos->m.mapb.index[b]; i < pos->m.mapb.index[b+1]; ++i)
         {
-            out->u.r[i] = n;
+            out->u.r[i] = dist;
         }
     }
 }
@@ -286,18 +294,17 @@ evaluate_distance(t_topology *top, t_trxframe *fr, t_pbc *pbc,
  * \c t_methoddata_distance::xref and puts them in \p out.g.
  */
 static void
-evaluate_within(t_topology *top, t_trxframe *fr, t_pbc *pbc,
+evaluate_within(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */,
                 gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out, void *data)
 {
-    t_methoddata_distance *d = (t_methoddata_distance *)data;
-    int                    b;
+    t_methoddata_distance *d = static_cast<t_methoddata_distance *>(data);
 
     out->u.g->isize = 0;
-    for (b = 0; b < pos->nr; ++b)
+    for (int b = 0; b < pos->count(); ++b)
     {
-        if (gmx_ana_nbsearch_pos_is_within(d->nb, pos, b))
+        if (d->nbsearch.isWithin(pos->x[b]))
         {
-            gmx_ana_pos_append(NULL, out->u.g, pos, b, 0);
+            gmx_ana_pos_add_to_group(out->u.g, pos, b);
         }
     }
 }