Renamed bonded module as 'listed-forces'
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nonbonded.c
index ac5fe893d4bce8455a49506e2f122680893b7e1f..c21e3201b28283e39faf42cb7707d573db1fdb4b 100644 (file)
-/* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
+/*
+ * This file is part of the GROMACS molecular simulation package.
  *
- *
- *                This source code is part of
- *
- *                 G   R   O   M   A   C   S
- *
- *          GROningen MAchine for Chemical Simulations
- *
- *                        VERSION 3.2.0
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2004, 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
+ * Copyright (c) 2001-2004, The GROMACS development team.
+ * Copyright (c) 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.
+ *
+ * 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.
  *
- * 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.
+ * 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.
  *
- * 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.
+ * 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.
  *
- * For more info, check our website at http://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.
  *
- * And Hey:
- * GROningen Mixture of Alchemy and Childrens' Stories
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "gmxpre.h"
 
-#ifdef GMX_THREAD_MPI
-#include <thread_mpi.h>
-#endif
+#include "gromacs/legacyheaders/nonbonded.h"
+
+#include "config.h"
 
 #include <stdio.h>
 #include <stdlib.h>
-#include "typedefs.h"
-#include "txtdump.h"
-#include "smalloc.h"
-#include "ns.h"
-#include "vec.h"
-#include "maths.h"
-#include "macros.h"
-#include "string2.h"
-#include "force.h"
-#include "names.h"
-#include "main.h"
-#include "xvgr.h"
-#include "gmx_fatal.h"
-#include "physics.h"
-#include "force.h"
-#include "bondf.h"
-#include "nrnb.h"
-#include "smalloc.h"
-#include "nonbonded.h"
-
-#include "nb_kernel.h"
-#include "nb_free_energy.h"
-#include "nb_generic.h"
-#include "nb_generic_cg.h"
-#include "nb_generic_adress.h"
-
-/* Different default (c) and accelerated interaction-specific kernels */
-#include "nb_kernel_c/nb_kernel_c.h"
-
-#if (defined GMX_CPU_ACCELERATION_X86_SSE2) && !(defined GMX_DOUBLE)
-#    include "nb_kernel_sse2_single/nb_kernel_sse2_single.h"
+
+#include "thread_mpi/threads.h"
+
+#include "gromacs/gmxlib/nonbonded/nb_free_energy.h"
+#include "gromacs/gmxlib/nonbonded/nb_generic.h"
+#include "gromacs/gmxlib/nonbonded/nb_generic_adress.h"
+#include "gromacs/gmxlib/nonbonded/nb_generic_cg.h"
+#include "gromacs/gmxlib/nonbonded/nb_kernel.h"
+#include "gromacs/legacyheaders/force.h"
+#include "gromacs/legacyheaders/macros.h"
+#include "gromacs/legacyheaders/names.h"
+#include "gromacs/legacyheaders/nrnb.h"
+#include "gromacs/legacyheaders/ns.h"
+#include "gromacs/legacyheaders/txtdump.h"
+#include "gromacs/legacyheaders/typedefs.h"
+#include "gromacs/listed-forces/bonded.h"
+#include "gromacs/math/utilities.h"
+#include "gromacs/math/vec.h"
+#include "gromacs/pbcutil/ishift.h"
+#include "gromacs/pbcutil/mshift.h"
+#include "gromacs/pbcutil/pbc.h"
+#include "gromacs/simd/simd.h"
+#include "gromacs/utility/cstringutil.h"
+#include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/smalloc.h"
+
+/* Different default (c) and SIMD instructions interaction-specific kernels */
+#include "gromacs/gmxlib/nonbonded/nb_kernel_c/nb_kernel_c.h"
+
+#if (defined GMX_SIMD_X86_SSE2) && !(defined GMX_DOUBLE)
+#    include "gromacs/gmxlib/nonbonded/nb_kernel_sse2_single/nb_kernel_sse2_single.h"
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_SSE4_1) && !(defined GMX_DOUBLE)
-#    include "nb_kernel_sse4_1_single/nb_kernel_sse4_1_single.h"
+#if (defined GMX_SIMD_X86_SSE4_1) && !(defined GMX_DOUBLE)
+#    include "gromacs/gmxlib/nonbonded/nb_kernel_sse4_1_single/nb_kernel_sse4_1_single.h"
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_AVX_128_FMA) && !(defined GMX_DOUBLE)
-#    include "nb_kernel_avx_128_fma_single/nb_kernel_avx_128_fma_single.h"
+#if (defined GMX_SIMD_X86_AVX_128_FMA) && !(defined GMX_DOUBLE)
+#    include "gromacs/gmxlib/nonbonded/nb_kernel_avx_128_fma_single/nb_kernel_avx_128_fma_single.h"
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_AVX_256) && !(defined GMX_DOUBLE)
-#    include "nb_kernel_avx_256_single/nb_kernel_avx_256_single.h"
+#if (defined GMX_SIMD_X86_AVX_256_OR_HIGHER) && !(defined GMX_DOUBLE)
+#    include "gromacs/gmxlib/nonbonded/nb_kernel_avx_256_single/nb_kernel_avx_256_single.h"
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_SSE2 && defined GMX_DOUBLE)
-#    include "nb_kernel_sse2_double/nb_kernel_sse2_double.h"
+#if (defined GMX_SIMD_X86_SSE2 && defined GMX_DOUBLE)
+#    include "gromacs/gmxlib/nonbonded/nb_kernel_sse2_double/nb_kernel_sse2_double.h"
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_SSE4_1 && defined GMX_DOUBLE)
-#    include "nb_kernel_sse4_1_double/nb_kernel_sse4_1_double.h"
+#if (defined GMX_SIMD_X86_SSE4_1 && defined GMX_DOUBLE)
+#    include "gromacs/gmxlib/nonbonded/nb_kernel_sse4_1_double/nb_kernel_sse4_1_double.h"
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_AVX_128_FMA && defined GMX_DOUBLE)
-#    include "nb_kernel_avx_128_fma_double/nb_kernel_avx_128_fma_double.h"
+#if (defined GMX_SIMD_X86_AVX_128_FMA && defined GMX_DOUBLE)
+#    include "gromacs/gmxlib/nonbonded/nb_kernel_avx_128_fma_double/nb_kernel_avx_128_fma_double.h"
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_AVX_256 && defined GMX_DOUBLE)
-#    include "nb_kernel_avx_256_double/nb_kernel_avx_256_double.h"
+#if (defined GMX_SIMD_X86_AVX_256_OR_HIGHER && defined GMX_DOUBLE)
+#    include "gromacs/gmxlib/nonbonded/nb_kernel_avx_256_double/nb_kernel_avx_256_double.h"
 #endif
-#if (defined GMX_CPU_ACCELERATION_SPARC64_HPC_ACE && defined GMX_DOUBLE)
-#    include "nb_kernel_sparc64_hpc_ace_double/nb_kernel_sparc64_hpc_ace_double.h"
+#if (defined GMX_SIMD_SPARC64_HPC_ACE && defined GMX_DOUBLE)
+#    include "gromacs/gmxlib/nonbonded/nb_kernel_sparc64_hpc_ace_double/nb_kernel_sparc64_hpc_ace_double.h"
 #endif
 
 
-#ifdef GMX_THREAD_MPI
 static tMPI_Thread_mutex_t nonbonded_setup_mutex = TMPI_THREAD_MUTEX_INITIALIZER;
-#endif
 static gmx_bool            nonbonded_setup_done  = FALSE;
 
 
 void
-gmx_nonbonded_setup(FILE *         fplog,
-                    t_forcerec *   fr,
+gmx_nonbonded_setup(t_forcerec *   fr,
                     gmx_bool       bGenericKernelOnly)
 {
-#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_lock(&nonbonded_setup_mutex);
-#endif
     /* Here we are guaranteed only one thread made it. */
     if (nonbonded_setup_done == FALSE)
     {
@@ -123,37 +117,37 @@ gmx_nonbonded_setup(FILE *         fplog,
             /* Add the generic kernels to the structure stored statically in nb_kernel.c */
             nb_kernel_list_add_kernels(kernellist_c, kernellist_c_size);
 
-            if (!(fr != NULL && fr->use_cpu_acceleration == FALSE))
+            if (!(fr != NULL && fr->use_simd_kernels == FALSE))
             {
                 /* Add interaction-specific kernels for different architectures */
                 /* Single precision */
-#if (defined GMX_CPU_ACCELERATION_X86_SSE2) && !(defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_SSE2) && !(defined GMX_DOUBLE)
                 nb_kernel_list_add_kernels(kernellist_sse2_single, kernellist_sse2_single_size);
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_SSE4_1) && !(defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_SSE4_1) && !(defined GMX_DOUBLE)
                 nb_kernel_list_add_kernels(kernellist_sse4_1_single, kernellist_sse4_1_single_size);
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_AVX_128_FMA) && !(defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_AVX_128_FMA) && !(defined GMX_DOUBLE)
                 nb_kernel_list_add_kernels(kernellist_avx_128_fma_single, kernellist_avx_128_fma_single_size);
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_AVX_256) && !(defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_AVX_256_OR_HIGHER) && !(defined GMX_DOUBLE)
                 nb_kernel_list_add_kernels(kernellist_avx_256_single, kernellist_avx_256_single_size);
 #endif
                 /* Double precision */
-#if (defined GMX_CPU_ACCELERATION_X86_SSE2 && defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_SSE2 && defined GMX_DOUBLE)
                 nb_kernel_list_add_kernels(kernellist_sse2_double, kernellist_sse2_double_size);
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_SSE4_1 && defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_SSE4_1 && defined GMX_DOUBLE)
                 nb_kernel_list_add_kernels(kernellist_sse4_1_double, kernellist_sse4_1_double_size);
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_AVX_128_FMA && defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_AVX_128_FMA && defined GMX_DOUBLE)
                 nb_kernel_list_add_kernels(kernellist_avx_128_fma_double, kernellist_avx_128_fma_double_size);
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_AVX_256 && defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_AVX_256_OR_HIGHER && defined GMX_DOUBLE)
                 nb_kernel_list_add_kernels(kernellist_avx_256_double, kernellist_avx_256_double_size);
 #endif
-#if (defined GMX_CPU_ACCELERATION_SPARC64_HPC_ACE && defined GMX_DOUBLE)
-                nb_kernel_list_add_kernels(kernellist_sparc64_hpc_ace_double,kernellist_sparc64_hpc_ace_double_size);
+#if (defined GMX_SIMD_SPARC64_HPC_ACE && defined GMX_DOUBLE)
+                nb_kernel_list_add_kernels(kernellist_sparc64_hpc_ace_double, kernellist_sparc64_hpc_ace_double_size);
 #endif
                 ; /* empty statement to avoid a completely empty block */
             }
@@ -163,15 +157,13 @@ gmx_nonbonded_setup(FILE *         fplog,
 
         nonbonded_setup_done = TRUE;
     }
-#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_unlock(&nonbonded_setup_mutex);
-#endif
 }
 
 
 
 void
-gmx_nonbonded_set_kernel_pointers(FILE *log, t_nblist *nl)
+gmx_nonbonded_set_kernel_pointers(FILE *log, t_nblist *nl, gmx_bool bElecAndVdwSwitchDiffers)
 {
     const char *     elec;
     const char *     elec_mod;
@@ -189,38 +181,38 @@ gmx_nonbonded_set_kernel_pointers(FILE *log, t_nblist *nl)
     arch_and_padding[] =
     {
         /* Single precision */
-#if (defined GMX_CPU_ACCELERATION_X86_AVX_256) && !(defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_AVX_256_OR_HIGHER) && !(defined GMX_DOUBLE)
         { "avx_256_single", 8 },
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_AVX_128_FMA) && !(defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_AVX_128_FMA) && !(defined GMX_DOUBLE)
         { "avx_128_fma_single", 4 },
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_SSE4_1) && !(defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_SSE4_1) && !(defined GMX_DOUBLE)
         { "sse4_1_single", 4 },
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_SSE2) && !(defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_SSE2) && !(defined GMX_DOUBLE)
         { "sse2_single", 4 },
 #endif
         /* Double precision */
-#if (defined GMX_CPU_ACCELERATION_X86_AVX_256 && defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_AVX_256_OR_HIGHER && defined GMX_DOUBLE)
         { "avx_256_double", 4 },
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_AVX_128_FMA && defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_AVX_128_FMA && defined GMX_DOUBLE)
         /* Sic. Double precision 2-way SIMD does not require neighbor list padding,
          * since the kernels execute a loop unrolled a factor 2, followed by
          * a possible single odd-element epilogue.
          */
         { "avx_128_fma_double", 1 },
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_SSE2 && defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_SSE2 && defined GMX_DOUBLE)
         /* No padding - see comment above */
         { "sse2_double", 1 },
 #endif
-#if (defined GMX_CPU_ACCELERATION_X86_SSE4_1 && defined GMX_DOUBLE)
+#if (defined GMX_SIMD_X86_SSE4_1 && defined GMX_DOUBLE)
         /* No padding - see comment above */
         { "sse4_1_double", 1 },
 #endif
-#if (defined GMX_CPU_ACCELERATION_SPARC64_HPC_ACE && defined GMX_DOUBLE)
+#if (defined GMX_SIMD_SPARC64_HPC_ACE && defined GMX_DOUBLE)
         /* No padding - see comment above */
         { "sparc64_hpc_ace_double", 1 },
 #endif
@@ -234,7 +226,7 @@ gmx_nonbonded_set_kernel_pointers(FILE *log, t_nblist *nl)
         /* We typically call this setup routine before starting timers,
          * but if that has not been done for whatever reason we do it now.
          */
-        gmx_nonbonded_setup(NULL, NULL, FALSE);
+        gmx_nonbonded_setup(NULL, FALSE);
     }
 
     /* Not used yet */
@@ -292,8 +284,30 @@ gmx_nonbonded_set_kernel_pointers(FILE *log, t_nblist *nl)
             }
         }
 
-        /* Give up, pick a generic one instead */
-        if (nl->kernelptr_vf == NULL)
+        /* For now, the accelerated kernels cannot handle the combination of switch functions for both
+         * electrostatics and VdW that use different switch radius or switch cutoff distances
+         * (both of them enter in the switch function calculation). This would require
+         * us to evaluate two completely separate switch functions for every interaction.
+         * Instead, we disable such kernels by setting the pointer to NULL.
+         * This will cause the generic kernel (which can handle it) to be called instead.
+         *
+         * Note that we typically already enable tabulated coulomb interactions for this case,
+         * so this is mostly a safe-guard to make sure we call the generic kernel if the
+         * tables are disabled.
+         */
+        if ((nl->ielec != GMX_NBKERNEL_ELEC_NONE) && (nl->ielecmod == eintmodPOTSWITCH) &&
+            (nl->ivdw  != GMX_NBKERNEL_VDW_NONE)  && (nl->ivdwmod  == eintmodPOTSWITCH) &&
+            bElecAndVdwSwitchDiffers)
+        {
+            nl->kernelptr_vf = NULL;
+            nl->kernelptr_f  = NULL;
+        }
+
+        /* Give up, pick a generic one instead.
+         * We only do this for particle-particle kernels; by leaving the water-optimized kernel
+         * pointers to NULL, the water optimization will automatically be disabled for this interaction.
+         */
+        if (nl->kernelptr_vf == NULL && !gmx_strcasecmp_min(geom, "Particle-Particle"))
         {
             nl->kernelptr_vf       = (void *) gmx_nb_generic_kernel;
             nl->kernelptr_f        = (void *) gmx_nb_generic_kernel;
@@ -303,19 +317,17 @@ gmx_nonbonded_set_kernel_pointers(FILE *log, t_nblist *nl)
                 fprintf(debug,
                         "WARNING - Slow generic NB kernel used for neighborlist with\n"
                         "    Elec: '%s', Modifier: '%s'\n"
-                        "    Vdw:  '%s', Modifier: '%s'\n"
-                        "    Geom: '%s', Other: '%s'\n\n",
-                        elec, elec_mod, vdw, vdw_mod, geom, other);
+                        "    Vdw:  '%s', Modifier: '%s'\n",
+                        elec, elec_mod, vdw, vdw_mod);
             }
         }
     }
-
     return;
 }
 
-void do_nonbonded(t_commrec *cr, t_forcerec *fr,
+void do_nonbonded(t_forcerec *fr,
                   rvec x[], rvec f_shortrange[], rvec f_longrange[], t_mdatoms *mdatoms, t_blocka *excl,
-                  gmx_grppairener_t *grppener, rvec box_size,
+                  gmx_grppairener_t *grppener,
                   t_nrnb *nrnb, real *lambda, real *dvdl,
                   int nls, int eNL, int flags)
 {
@@ -333,6 +345,7 @@ void do_nonbonded(t_commrec *cr, t_forcerec *fr,
 
     if (fr->bAllvsAll)
     {
+        gmx_incons("All-vs-all kernels have not been implemented in version 4.6");
         return;
     }
 
@@ -382,7 +395,7 @@ void do_nonbonded(t_commrec *cr, t_forcerec *fr,
                 nlist = nblists->nlist_sr;
                 f                                   = f_shortrange;
             }
-            else if (range == 1)
+            else
             {
                 /* Long-range */
                 if (!(flags & GMX_NONBONDED_DO_LR))
@@ -416,7 +429,15 @@ void do_nonbonded(t_commrec *cr, t_forcerec *fr,
                         /* We don't need the non-perturbed interactions */
                         continue;
                     }
-                    (*kernelptr)(&(nlist[i]), x, f, fr, mdatoms, &kernel_data, nrnb);
+                    /* Neighborlists whose kernelptr==NULL will always be empty */
+                    if (kernelptr != NULL)
+                    {
+                        (*kernelptr)(&(nlist[i]), x, f, fr, mdatoms, &kernel_data, nrnb);
+                    }
+                    else
+                    {
+                        gmx_fatal(FARGS, "Non-empty neighborlist does not have any kernel pointer assigned.");
+                    }
                 }
             }
         }
@@ -635,6 +656,8 @@ do_nonbonded_listed(int ftype, int nbonds,
 
         if (r2 >= fr->tab14.r*fr->tab14.r)
         {
+            /* This check isn't race free. But it doesn't matter because if a race occurs the only
+             * disadvantage is that the warning is printed twice */
             if (warned_rlimit == FALSE)
             {
                 nb_listed_warning_rlimit(x, ai, aj, global_atom_index, sqrt(r2), fr->tab14.r);