Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / selection / centerofmass.cpp
index 7c120784077b405cf3e9314a6d30cc969621aa4d..474175ef89d0961fa9274a5606af9ea568917841 100644 (file)
@@ -1,56 +1,57 @@
 /*
+ * 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 functions in centerofmass.h.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_selection
  */
-#include "gromacs/legacyheaders/typedefs.h"
-#include "gromacs/legacyheaders/pbc.h"
-#include "gromacs/legacyheaders/vec.h"
+#include "gmxpre.h"
 
-#include "gromacs/selection/centerofmass.h"
+#include "centerofmass.h"
+
+#include <errno.h>
+
+#include "gromacs/math/vec.h"
+#include "gromacs/pbcutil/pbc.h"
+#include "gromacs/topology/block.h"
+#include "gromacs/topology/topology.h"
 
-/*!
- * \param[in]  top    Topology structure (unused, can be NULL).
- * \param[in]  x      Position vectors of all atoms.
- * \param[in]  nrefat Number of atoms in the index.
- * \param[in]  index  Indices of atoms.
- * \param[out] xout   COG position for the indexed atoms.
- * \returns    0 on success.
- */
 int
-gmx_calc_cog(t_topology *top, rvec x[], int nrefat, atom_id index[], rvec xout)
+gmx_calc_cog(t_topology * /* top */, rvec x[], int nrefat, atom_id index[], rvec xout)
 {
     int                 m, ai;
 
@@ -90,7 +91,7 @@ gmx_calc_com(t_topology *top, rvec x[], int nrefat, atom_id index[], rvec xout)
     mtot = 0;
     for (m = 0; m < nrefat; ++m)
     {
-        ai = index[m];
+        ai   = index[m];
         mass = top->atoms.atom[ai].m;
         for (j = 0; j < DIM; ++j)
         {
@@ -109,9 +110,6 @@ gmx_calc_com(t_topology *top, rvec x[], int nrefat, atom_id index[], rvec xout)
  * \param[in]  index  Indices of atoms.
  * \param[out] fout   Force on the COG position for the indexed atoms.
  * \returns    0 on success, EINVAL if \p top is NULL.
- *
- * No special function is provided for calculating the force on the center of
- * mass, because this can be achieved with gmx_calc_cog().
  */
 int
 gmx_calc_cog_f(t_topology *top, rvec f[], int nrefat, atom_id index[], rvec fout)
@@ -128,7 +126,7 @@ gmx_calc_cog_f(t_topology *top, rvec f[], int nrefat, atom_id index[], rvec fout
     mtot = 0;
     for (m = 0; m < nrefat; ++m)
     {
-        ai = index[m];
+        ai   = index[m];
         mass = top->atoms.atom[ai].m;
         for (j = 0; j < DIM; ++j)
         {
@@ -136,7 +134,19 @@ gmx_calc_cog_f(t_topology *top, rvec f[], int nrefat, atom_id index[], rvec fout
         }
         mtot += mass;
     }
-    svmul(mtot, fout, fout);
+    svmul(mtot / nrefat, fout, fout);
+    return 0;
+}
+
+int
+gmx_calc_com_f(t_topology * /* top */, rvec f[], int nrefat, atom_id index[], rvec fout)
+{
+    clear_rvec(fout);
+    for (int m = 0; m < nrefat; ++m)
+    {
+        const int ai = index[m];
+        rvec_inc(fout, f[ai]);
+    }
     return 0;
 }
 
@@ -178,7 +188,7 @@ gmx_calc_comg(t_topology *top, rvec x[], int nrefat, atom_id index[],
  * \param[out] fout  Force on the COM/COG position for the indexed atoms.
  * \returns    0 on success, EINVAL if \p top is NULL and \p bMass is false.
  *
- * Calls either gmx_calc_cog() or gmx_calc_cog_f() depending on the value of
+ * Calls either gmx_calc_cog_f() or gmx_calc_com_f() depending on the value of
  * \p bMass.
  * Other parameters are passed unmodified to these functions.
  */
@@ -188,7 +198,7 @@ gmx_calc_comg_f(t_topology *top, rvec f[], int nrefat, atom_id index[],
 {
     if (bMass)
     {
-        return gmx_calc_cog(top, f, nrefat, index, fout);
+        return gmx_calc_com_f(top, f, nrefat, index, fout);
     }
     else
     {
@@ -285,7 +295,7 @@ gmx_calc_com_pbc(t_topology *top, rvec x[], t_pbc *pbc,
     mtot = 0;
     for (m = 0; m < nrefat; ++m)
     {
-        ai = index[m];
+        ai   = index[m];
         mass = top->atoms.atom[ai].m;
         for (j = 0; j < DIM; ++j)
         {
@@ -303,7 +313,7 @@ gmx_calc_com_pbc(t_topology *top, rvec x[], t_pbc *pbc,
             bChanged = false;
             for (m = 0; m < nrefat; ++m)
             {
-                ai = index[m];
+                ai   = index[m];
                 mass = top->atoms.atom[ai].m / mtot;
                 pbc_dx(pbc, x[ai], xout, dx);
                 rvec_add(xout, dx, xtest);
@@ -355,16 +365,8 @@ gmx_calc_comg_pbc(t_topology *top, rvec x[], t_pbc *pbc,
 }
 
 
-/*!
- * \param[in]  top   Topology structure (unused, can be NULL).
- * \param[in]  x     Position vectors of all atoms.
- * \param[in]  block t_block structure that divides \p index into blocks.
- * \param[in]  index Indices of atoms.
- * \param[out] xout  \p block->nr COG positions.
- * \returns    0 on success.
- */
 int
-gmx_calc_cog_block(t_topology *top, rvec x[], t_block *block, atom_id index[],
+gmx_calc_cog_block(t_topology * /* top */, rvec x[], t_block *block, atom_id index[],
                    rvec xout[])
 {
     int                 b, i, ai;
@@ -413,7 +415,7 @@ gmx_calc_com_block(t_topology *top, rvec x[], t_block *block, atom_id index[],
         mtot = 0;
         for (i = block->index[b]; i < block->index[b+1]; ++i)
         {
-            ai = index[i];
+            ai   = index[i];
             mass = top->atoms.atom[ai].m;
             for (d = 0; d < DIM; ++d)
             {
@@ -453,7 +455,7 @@ gmx_calc_cog_f_block(t_topology *top, rvec f[], t_block *block, atom_id index[],
         mtot = 0;
         for (i = block->index[b]; i < block->index[b+1]; ++i)
         {
-            ai = index[i];
+            ai   = index[i];
             mass = top->atoms.atom[ai].m;
             for (d = 0; d < DIM; ++d)
             {
@@ -461,7 +463,25 @@ gmx_calc_cog_f_block(t_topology *top, rvec f[], t_block *block, atom_id index[],
             }
             mtot += mass;
         }
-        svmul(mtot, fb, fout[b]);
+        svmul(mtot / (block->index[b+1] - block->index[b]), fb, fout[b]);
+    }
+    return 0;
+}
+
+int
+gmx_calc_com_f_block(t_topology * /* top */, rvec f[], t_block *block, atom_id index[],
+                     rvec fout[])
+{
+    for (int b = 0; b < block->nr; ++b)
+    {
+        rvec fb;
+        clear_rvec(fb);
+        for (int i = block->index[b]; i < block->index[b+1]; ++i)
+        {
+            const int ai = index[i];
+            rvec_inc(fb, f[ai]);
+        }
+        copy_rvec(fb, fout[b]);
     }
     return 0;
 }
@@ -504,8 +524,8 @@ gmx_calc_comg_block(t_topology *top, rvec x[], t_block *block, atom_id index[],
  * \param[out] fout  \p block->nr forces on the COM/COG positions.
  * \returns    0 on success, EINVAL if \p top is NULL and \p bMass is true.
  *
- * Calls either gmx_calc_com_block() or gmx_calc_cog_block() depending on the
- * value of \p bMass.
+ * Calls either gmx_calc_com_f_block() or gmx_calc_cog_f_block() depending on
+ * the value of \p bMass.
  * Other parameters are passed unmodified to these functions.
  */
 int
@@ -514,7 +534,7 @@ gmx_calc_comg_f_block(t_topology *top, rvec f[], t_block *block, atom_id index[]
 {
     if (bMass)
     {
-        return gmx_calc_cog_block(top, f, block, index, fout);
+        return gmx_calc_com_f_block(top, f, block, index, fout);
     }
     else
     {