Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / selection / sm_compare.cpp
index defbf421b8e83c2fe161a25f47f76bebb97d6c2a..51aa8a6d47d49c7efb9d2f57aef3784c23f3d1dd 100644 (file)
@@ -1,50 +1,55 @@
 /*
+ * 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
  * Implements internal selection method for comparison expressions.
  *
- * \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 <cmath>
 
-#include <maths.h>
-#include <macros.h>
-#include <smalloc.h>
+#include "gromacs/legacyheaders/macros.h"
+#include "gromacs/math/utilities.h"
+#include "gromacs/utility/common.h"
+#include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/smalloc.h"
 
-#include "gromacs/fatalerror/exceptions.h"
-#include "gromacs/selection/selmethod.h"
+#include "selmethod.h"
 
 /** Defines the comparison operator for comparison expressions. */
 typedef enum
@@ -77,9 +82,9 @@ typedef struct
     /** Flags that describe the type of the operand. */
     int             flags;
     /** (Array of) integer value(s). */
-    int        *i;
+    int            *i;
     /** (Array of) real value(s). */
-    real       *r;
+    real           *r;
 } t_compare_value;
 
 /*! \internal \brief
@@ -97,10 +102,27 @@ typedef struct
     t_compare_value  right;
 } t_methoddata_compare;
 
-/** Allocates data for comparison expression evaluation. */
+/*! \brief
+ * Allocates data for comparison expression evaluation.
+ *
+ * \param[in]     npar  Not used (should be 5).
+ * \param[in,out] param Method parameters (should point to a copy of
+ *   \ref smparams_compare).
+ * \returns       Pointer to the allocated data (\c t_methoddata_compare).
+ *
+ * Allocates memory for a \c t_methoddata_compare structure.
+ */
 static void *
 init_data_compare(int npar, gmx_ana_selparam_t *param);
-/** Initializes data for comparison expression evaluation. */
+/*! \brief
+ * Initializes data for comparison expression evaluation.
+ *
+ * \param[in] top   Not used.
+ * \param[in] npar  Not used (should be 5).
+ * \param[in] param Method parameters (should point to \ref smparams_compare).
+ * \param[in] data  Should point to a \c t_methoddata_compare.
+ * \returns   0 if the input data is valid, -1 on error.
+ */
 static void
 init_compare(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
 /** Frees the memory allocated for comparison expression evaluation. */
@@ -171,23 +193,26 @@ comparison_type(char *str)
  * \returns   Pointer to a string that corresponds to \p cmpt.
  *
  * The return value points to a string constant and should not be \p free'd.
- * 
+ *
  * The function returns NULL if \p cmpt is not one of the valid values.
  */
 static const char *
 comparison_type_str(e_comparison_t cmpt)
 {
+    const char *p = NULL;
     switch (cmpt)
     {
-        case CMP_INVALID: return "INVALID"; break;
-        case CMP_LESS:    return "<";  break;
-        case CMP_LEQ:     return "<="; break;
-        case CMP_GTR:     return ">";  break;
-        case CMP_GEQ:     return ">="; break;
-        case CMP_EQUAL:   return "=="; break;
-        case CMP_NEQ:     return "!="; break;
+        case CMP_INVALID: p = "INVALID"; break;
+        case CMP_LESS:    p = "<";       break;
+        case CMP_LEQ:     p = "<=";      break;
+        case CMP_GTR:     p = ">";       break;
+        case CMP_GEQ:     p = ">=";      break;
+        case CMP_EQUAL:   p = "==";      break;
+        case CMP_NEQ:     p = "!=";      break;
+            // No default clause so we intentionally get compiler errors
+            // if new selection choices are added later.
     }
-    return NULL;
+    return p;
 }
 
 /*!
@@ -236,16 +261,8 @@ _gmx_selelem_print_compare_info(FILE *fp, void *data)
     fprintf(fp, "\"");
 }
 
-/*!
- * \param[in]     npar  Not used (should be 5).
- * \param[in,out] param Method parameters (should point to a copy of
- *   \ref smparams_compare).
- * \returns       Pointer to the allocated data (\c t_methoddata_compare).
- *
- * Allocates memory for a \c t_methoddata_compare structure.
- */
 static void *
-init_data_compare(int npar, gmx_ana_selparam_t *param)
+init_data_compare(int /* npar */, gmx_ana_selparam_t *param)
 {
     t_methoddata_compare *data;
 
@@ -254,7 +271,7 @@ init_data_compare(int npar, gmx_ana_selparam_t *param)
     return data;
 }
 
-/* \brief
+/*! \brief
  * Reverses a comparison operator.
  *
  * \param[in] type  Comparison operator to reverse.
@@ -312,7 +329,7 @@ init_comparison_value(t_compare_value *val, gmx_ana_selparam_t param[2])
     return n;
 }
 
-/* \brief
+/*! \brief
  * Converts an integer value to floating point.
  *
  * \param[in]     n   Number of values in the \p val->u array.
@@ -335,7 +352,7 @@ convert_int_real(int n, t_compare_value *val)
     val->flags |= CMP_REALVAL | CMP_ALLOCREAL;
 }
 
-/* \brief
+/*! \brief
  * Converts a floating point value to integer.
  *
  * \param[in]     n      Number of values in the \p val->u array.
@@ -365,17 +382,17 @@ convert_real_int(int n, t_compare_value *val, e_comparison_t cmpt, bool bRight)
         {
             case CMP_LESS:
             case CMP_GEQ:
-                iv[i] = (int)ceil(val->r[i]);
+                iv[i] = static_cast<int>(std::ceil(val->r[i]));
                 break;
             case CMP_GTR:
             case CMP_LEQ:
-                iv[i] = (int)floor(val->r[i]);
+                iv[i] = static_cast<int>(std::floor(val->r[i]));
                 break;
             case CMP_EQUAL:
             case CMP_NEQ:
                 sfree(iv);
                 /* TODO: Implement, although it isn't typically very useful.
-                 * Implementation is only a matter or proper initialization,
+                 * Implementation is only a matter of proper initialization,
                  * the evaluation function can already handle this case with
                  * proper preparations. */
                 GMX_THROW(gmx::NotImplementedError("Equality comparison between dynamic integer and static real expressions not implemented"));
@@ -391,15 +408,8 @@ convert_real_int(int n, t_compare_value *val, e_comparison_t cmpt, bool bRight)
     val->flags |= CMP_ALLOCINT;
 }
 
-/*!
- * \param[in] top   Not used.
- * \param[in] npar  Not used (should be 5).
- * \param[in] param Method parameters (should point to \ref smparams_compare).
- * \param[in] data  Should point to a \c t_methoddata_compare.
- * \returns   0 if the input data is valid, -1 on error.
- */
 static void
-init_compare(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
+init_compare(t_topology * /* top */, int /* npar */, gmx_ana_selparam_t *param, void *data)
 {
     t_methoddata_compare *d = (t_methoddata_compare *)data;
     int                   n1, n2;
@@ -407,10 +417,6 @@ init_compare(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
     /* Store the values */
     n1 = init_comparison_value(&d->left, &param[0]);
     n2 = init_comparison_value(&d->right, &param[3]);
-    if (n1 == 0 || n2 == 0)
-    {
-        GMX_THROW(gmx::InternalError("One of the values for comparison missing"));
-    }
     /* Store the comparison type */
     d->cmpt = comparison_type(d->cmpop);
     if (d->cmpt == CMP_INVALID)
@@ -418,6 +424,11 @@ init_compare(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
         GMX_THROW(gmx::InternalError("Invalid comparison type"));
     }
     /* Convert the values to the same type */
+    /* TODO: Currently, there are no dynamic integer-valued selection methods,
+     * which means that only the branches with convert_int_real() will ever be
+     * taken. It should be considered whether it is necessary to support these
+     * other cases at all.
+     */
     if ((d->left.flags & CMP_REALVAL) && !(d->right.flags & CMP_REALVAL))
     {
         if (d->left.flags & d->right.flags & CMP_DYNAMICVAL)
@@ -439,14 +450,14 @@ init_compare(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
         {
             /* Reverse the sides to place the integer on the right */
             int    flags;
-            d->left.r  = d->right.r;
-            d->right.r = NULL;
-            d->right.i = d->left.i;
-            d->left.i  = NULL;
+            d->left.r      = d->right.r;
+            d->right.r     = NULL;
+            d->right.i     = d->left.i;
+            d->left.i      = NULL;
             flags          = d->left.flags;
             d->left.flags  = d->right.flags;
             d->right.flags = flags;
-            d->cmpt = reverse_comparison_type(d->cmpt);
+            d->cmpt        = reverse_comparison_type(d->cmpt);
         }
         else if (!(d->left.flags & CMP_DYNAMICVAL))
         {
@@ -486,9 +497,12 @@ free_data_compare(void *data)
     {
         sfree(d->right.r);
     }
+    sfree(d);
 }
 
-/*!
+/*! \brief
+ * Implementation for evaluate_compare() for integer values.
+ *
  * \param[in]  top   Not used.
  * \param[in]  fr    Not used.
  * \param[in]  pbc   Not used.
@@ -505,10 +519,13 @@ evaluate_compare_int(t_topology *top, t_trxframe *fr, t_pbc *pbc,
     int                   a, b;
     bool                  bAccept;
 
+    GMX_UNUSED_VALUE(top);
+    GMX_UNUSED_VALUE(fr);
+    GMX_UNUSED_VALUE(pbc);
     for (i = i1 = i2 = ig = 0; i < g->isize; ++i)
     {
-        a = d->left.i[i1];
-        b = d->right.i[i2];
+        a       = d->left.i[i1];
+        b       = d->right.i[i2];
         bAccept = false;
         switch (d->cmpt)
         {
@@ -536,13 +553,18 @@ evaluate_compare_int(t_topology *top, t_trxframe *fr, t_pbc *pbc,
     out->u.g->isize = ig;
 }
 
-/*!
+/*! \brief
+ * Implementation for evaluate_compare() if either value is non-integer.
+ *
  * \param[in]  top   Not used.
  * \param[in]  fr    Not used.
  * \param[in]  pbc   Not used.
  * \param[in]  g     Evaluation index group.
  * \param[out] out   Output data structure (\p out->u.g is used).
  * \param[in]  data  Should point to a \c t_methoddata_compare.
+ *
+ * Left value is assumed to be real-valued; right value can be either.
+ * This is ensured by the initialization method.
  */
 static void
 evaluate_compare_real(t_topology *top, t_trxframe *fr, t_pbc *pbc,
@@ -553,10 +575,13 @@ evaluate_compare_real(t_topology *top, t_trxframe *fr, t_pbc *pbc,
     real                  a, b;
     bool                  bAccept;
 
+    GMX_UNUSED_VALUE(top);
+    GMX_UNUSED_VALUE(fr);
+    GMX_UNUSED_VALUE(pbc);
     for (i = i1 = i2 = ig = 0; i < g->isize; ++i)
     {
-        a = d->left.r[i1];
-        b = (d->right.flags & CMP_REALVAL) ? d->right.r[i2] : d->right.i[i2];
+        a       = d->left.r[i1];
+        b       = (d->right.flags & CMP_REALVAL) ? d->right.r[i2] : d->right.i[i2];
         bAccept = false;
         switch (d->cmpt)
         {