Implemented nbnxn LJ switch functions
[alexxy/gromacs.git] / src / gromacs / mdlib / nbnxn_kernels / nbnxn_kernel_ref.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <math.h>
40
41 #include "typedefs.h"
42 #include "vec.h"
43 #include "smalloc.h"
44 #include "force.h"
45 #include "gmx_omp_nthreads.h"
46 #include "nbnxn_kernel_ref.h"
47 #include "../nbnxn_consts.h"
48 #include "nbnxn_kernel_common.h"
49
50 /*! \brief Typedefs for declaring lookup tables of kernel functions.
51  */
52
53 typedef void (*p_nbk_func_noener)(const nbnxn_pairlist_t     *nbl,
54                                   const nbnxn_atomdata_t     *nbat,
55                                   const interaction_const_t  *ic,
56                                   rvec                       *shift_vec,
57                                   real                       *f,
58                                   real                       *fshift);
59
60 typedef void (*p_nbk_func_ener)(const nbnxn_pairlist_t     *nbl,
61                                 const nbnxn_atomdata_t     *nbat,
62                                 const interaction_const_t  *ic,
63                                 rvec                       *shift_vec,
64                                 real                       *f,
65                                 real                       *fshift,
66                                 real                       *Vvdw,
67                                 real                       *Vc);
68
69 /* Analytical reaction-field kernels */
70 #define CALC_COUL_RF
71 #include "nbnxn_kernel_ref_includes.h"
72 #define VDW_FORCE_SWITCH
73 #include "nbnxn_kernel_ref_includes.h"
74 #undef VDW_FORCE_SWITCH
75 #define VDW_POT_SWITCH
76 #include "nbnxn_kernel_ref_includes.h"
77 #undef VDW_POT_SWITCH
78 #undef CALC_COUL_RF
79
80
81 /* Tabulated exclusion interaction electrostatics kernels */
82 #define CALC_COUL_TAB
83 #include "nbnxn_kernel_ref_includes.h"
84 #define VDW_FORCE_SWITCH
85 #include "nbnxn_kernel_ref_includes.h"
86 #undef VDW_FORCE_SWITCH
87 #define VDW_POT_SWITCH
88 #include "nbnxn_kernel_ref_includes.h"
89 #undef VDW_POT_SWITCH
90 /* Twin-range cut-off kernels */
91 #define VDW_CUTOFF_CHECK
92 #include "nbnxn_kernel_ref_includes.h"
93 #define VDW_FORCE_SWITCH
94 #include "nbnxn_kernel_ref_includes.h"
95 #undef VDW_FORCE_SWITCH
96 #define VDW_POT_SWITCH
97 #include "nbnxn_kernel_ref_includes.h"
98 #undef VDW_POT_SWITCH
99 #undef VDW_CUTOFF_CHECK
100 #undef CALC_COUL_TAB
101
102
103 enum {
104     coultRF, coultTAB, coultTAB_TWIN, coultNR
105 };
106
107 enum {
108     vdwtCUT, vdwtFSWITCH, vdwtPSWITCH, vdwtNR
109 };
110
111 p_nbk_func_noener p_nbk_c_noener[coultNR][vdwtNR] =
112 {
113     { nbnxn_kernel_ElecRF_VdwLJ_F_ref,           nbnxn_kernel_ElecRF_VdwLJFsw_F_ref,           nbnxn_kernel_ElecRF_VdwLJPsw_F_ref           },
114     { nbnxn_kernel_ElecQSTab_VdwLJ_F_ref,        nbnxn_kernel_ElecQSTab_VdwLJFsw_F_ref,        nbnxn_kernel_ElecQSTab_VdwLJPsw_F_ref        },
115     { nbnxn_kernel_ElecQSTabTwinCut_VdwLJ_F_ref, nbnxn_kernel_ElecQSTabTwinCut_VdwLJFsw_F_ref, nbnxn_kernel_ElecQSTabTwinCut_VdwLJPsw_F_ref }
116 };
117
118 p_nbk_func_ener p_nbk_c_ener[coultNR][vdwtNR] =
119 {
120     { nbnxn_kernel_ElecRF_VdwLJ_VF_ref,           nbnxn_kernel_ElecRF_VdwLJFsw_VF_ref,           nbnxn_kernel_ElecRF_VdwLJPsw_VF_ref           },
121     { nbnxn_kernel_ElecQSTab_VdwLJ_VF_ref,        nbnxn_kernel_ElecQSTab_VdwLJFsw_VF_ref,        nbnxn_kernel_ElecQSTab_VdwLJPsw_VF_ref        },
122     { nbnxn_kernel_ElecQSTabTwinCut_VdwLJ_VF_ref, nbnxn_kernel_ElecQSTabTwinCut_VdwLJFsw_VF_ref, nbnxn_kernel_ElecQSTabTwinCut_VdwLJPsw_VF_ref }
123 };
124
125 p_nbk_func_ener p_nbk_c_energrp[coultNR][vdwtNR] =
126 {
127     { nbnxn_kernel_ElecRF_VdwLJ_VgrpF_ref,           nbnxn_kernel_ElecRF_VdwLJFsw_VgrpF_ref,           nbnxn_kernel_ElecRF_VdwLJPsw_VgrpF_ref           },
128     { nbnxn_kernel_ElecQSTab_VdwLJ_VgrpF_ref,        nbnxn_kernel_ElecQSTab_VdwLJFsw_VgrpF_ref,        nbnxn_kernel_ElecQSTab_VdwLJPsw_VgrpF_ref        },
129     { nbnxn_kernel_ElecQSTabTwinCut_VdwLJ_VgrpF_ref, nbnxn_kernel_ElecQSTabTwinCut_VdwLJFsw_VgrpF_ref, nbnxn_kernel_ElecQSTabTwinCut_VdwLJPsw_VgrpF_ref }
130 };
131
132 void
133 nbnxn_kernel_ref(const nbnxn_pairlist_set_t *nbl_list,
134                  const nbnxn_atomdata_t     *nbat,
135                  const interaction_const_t  *ic,
136                  rvec                       *shift_vec,
137                  int                         force_flags,
138                  int                         clearF,
139                  real                       *fshift,
140                  real                       *Vc,
141                  real                       *Vvdw)
142 {
143     int                nnbl;
144     nbnxn_pairlist_t **nbl;
145     int                coult;
146     int                vdwt;
147     int                nb;
148
149     nnbl = nbl_list->nnbl;
150     nbl  = nbl_list->nbl;
151
152     if (EEL_RF(ic->eeltype) || ic->eeltype == eelCUT)
153     {
154         coult = coultRF;
155     }
156     else
157     {
158         if (ic->rcoulomb == ic->rvdw)
159         {
160             coult = coultTAB;
161         }
162         else
163         {
164             coult = coultTAB_TWIN;
165         }
166     }
167
168     switch (ic->vdw_modifier)
169     {
170         case eintmodPOTSHIFT:
171         case eintmodNONE:
172             vdwt = vdwtCUT;
173             break;
174         case eintmodFORCESWITCH:
175             vdwt = vdwtFSWITCH;
176             break;
177         case eintmodPOTSWITCH:
178             vdwt = vdwtPSWITCH;
179             break;
180         default:
181             gmx_incons("Unsupported VdW modifier");
182             break;
183     }
184
185 #pragma omp parallel for schedule(static) num_threads(gmx_omp_nthreads_get(emntNonbonded))
186     for (nb = 0; nb < nnbl; nb++)
187     {
188         nbnxn_atomdata_output_t *out;
189         real                    *fshift_p;
190
191         out = &nbat->out[nb];
192
193         if (clearF == enbvClearFYes)
194         {
195             clear_f(nbat, nb, out->f);
196         }
197
198         if ((force_flags & GMX_FORCE_VIRIAL) && nnbl == 1)
199         {
200             fshift_p = fshift;
201         }
202         else
203         {
204             fshift_p = out->fshift;
205
206             if (clearF == enbvClearFYes)
207             {
208                 clear_fshift(fshift_p);
209             }
210         }
211
212         if (!(force_flags & GMX_FORCE_ENERGY))
213         {
214             /* Don't calculate energies */
215             p_nbk_c_noener[coult][vdwt](nbl[nb], nbat,
216                                         ic,
217                                         shift_vec,
218                                         out->f,
219                                         fshift_p);
220         }
221         else if (out->nV == 1)
222         {
223             /* No energy groups */
224             out->Vvdw[0] = 0;
225             out->Vc[0]   = 0;
226
227             p_nbk_c_ener[coult][vdwt](nbl[nb], nbat,
228                                       ic,
229                                       shift_vec,
230                                       out->f,
231                                       fshift_p,
232                                       out->Vvdw,
233                                       out->Vc);
234         }
235         else
236         {
237             /* Calculate energy group contributions */
238             int i;
239
240             for (i = 0; i < out->nV; i++)
241             {
242                 out->Vvdw[i] = 0;
243             }
244             for (i = 0; i < out->nV; i++)
245             {
246                 out->Vc[i] = 0;
247             }
248
249             p_nbk_c_energrp[coult][vdwt](nbl[nb], nbat,
250                                          ic,
251                                          shift_vec,
252                                          out->f,
253                                          fshift_p,
254                                          out->Vvdw,
255                                          out->Vc);
256         }
257     }
258
259     if (force_flags & GMX_FORCE_ENERGY)
260     {
261         reduce_energies_over_lists(nbat, nnbl, Vvdw, Vc);
262     }
263 }