Implemented nbnxn LJ switch functions
[alexxy/gromacs.git] / src / gromacs / mdlib / nbnxn_kernels / nbnxn_kernel_ref_inner.h
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
36 /* When calculating RF or Ewald interactions we calculate the electrostatic
37  * forces and energies on excluded atom pairs here in the non-bonded loops.
38  */
39 #if defined CHECK_EXCLS && defined CALC_COULOMB
40 #define EXCL_FORCES
41 #endif
42
43 {
44     int cj;
45 #ifdef ENERGY_GROUPS
46     int egp_cj;
47 #endif
48     int i;
49
50     cj = l_cj[cjind].cj;
51
52 #ifdef ENERGY_GROUPS
53     egp_cj = nbat->energrp[cj];
54 #endif
55     for (i = 0; i < UNROLLI; i++)
56     {
57         int ai;
58         int type_i_off;
59         int j;
60
61         ai = ci*UNROLLI + i;
62
63         type_i_off = type[ai]*ntype2;
64
65         for (j = 0; j < UNROLLJ; j++)
66         {
67             int  aj;
68             real dx, dy, dz;
69             real rsq, rinv;
70             real rinvsq, rinvsix;
71             real c6, c12;
72             real FrLJ6 = 0, FrLJ12 = 0, frLJ = 0, VLJ = 0;
73 #if defined VDW_FORCE_SWITCH || defined VDW_POT_SWITCH
74             real r, rsw;
75 #endif
76
77 #ifdef CALC_COULOMB
78             real qq;
79             real fcoul;
80 #ifdef CALC_COUL_TAB
81             real rs, frac;
82             int  ri;
83             real fexcl;
84 #endif
85 #ifdef CALC_ENERGIES
86             real vcoul;
87 #endif
88 #endif
89             real fscal;
90             real fx, fy, fz;
91
92             /* A multiply mask used to zero an interaction
93              * when either the distance cutoff is exceeded, or
94              * (if appropriate) the i and j indices are
95              * unsuitable for this kind of inner loop. */
96             real skipmask;
97
98 #ifdef CHECK_EXCLS
99             /* A multiply mask used to zero an interaction
100              * when that interaction should be excluded
101              * (e.g. because of bonding). */
102             int interact;
103
104             interact = ((l_cj[cjind].excl>>(i*UNROLLI + j)) & 1);
105 #ifndef EXCL_FORCES
106             skipmask = interact;
107 #else
108             skipmask = !(cj == ci_sh && j <= i);
109 #endif
110 #else
111 #define interact 1.0
112             skipmask = 1.0;
113 #endif
114
115             aj = cj*UNROLLJ + j;
116
117             dx  = xi[i*XI_STRIDE+XX] - x[aj*X_STRIDE+XX];
118             dy  = xi[i*XI_STRIDE+YY] - x[aj*X_STRIDE+YY];
119             dz  = xi[i*XI_STRIDE+ZZ] - x[aj*X_STRIDE+ZZ];
120
121             rsq = dx*dx + dy*dy + dz*dz;
122
123             /* Prepare to enforce the cut-off. */
124             skipmask = (rsq >= rcut2) ? 0 : skipmask;
125             /* 9 flops for r^2 + cut-off check */
126
127 #ifdef CHECK_EXCLS
128             /* Excluded atoms are allowed to be on top of each other.
129              * To avoid overflow of rinv, rinvsq and rinvsix
130              * we add a small number to rsq for excluded pairs only.
131              */
132             rsq += (1 - interact)*NBNXN_AVOID_SING_R2_INC;
133 #endif
134
135 #ifdef COUNT_PAIRS
136             npair++;
137 #endif
138
139             rinv = gmx_invsqrt(rsq);
140             /* 5 flops for invsqrt */
141
142             /* Partially enforce the cut-off (and perhaps
143              * exclusions) to avoid possible overflow of
144              * rinvsix when computing LJ, and/or overflowing
145              * the Coulomb table during lookup. */
146             rinv = rinv * skipmask;
147
148             rinvsq  = rinv*rinv;
149
150 #ifdef HALF_LJ
151             if (i < UNROLLI/2)
152 #endif
153             {
154                 rinvsix = interact*rinvsq*rinvsq*rinvsq;
155
156                 c6      = nbfp[type_i_off+type[aj]*2  ];
157                 c12     = nbfp[type_i_off+type[aj]*2+1];
158                 FrLJ6   = c6*rinvsix;
159                 FrLJ12  = c12*rinvsix*rinvsix;
160                 frLJ    = FrLJ12 - FrLJ6;
161                 /* 7 flops for r^-2 + LJ force */
162 #if defined CALC_ENERGIES || defined VDW_POT_SWITCH
163                 VLJ     = (FrLJ12 + c12*ic->repulsion_shift.cpot)/12 -
164                     (FrLJ6 + c6*ic->dispersion_shift.cpot)/6;
165                 /* 7 flops for LJ energy */
166 #endif
167
168 #if defined VDW_FORCE_SWITCH || defined VDW_POT_SWITCH
169                 /* Force or potential switching from ic->rvdw_switch */
170                 r       = rsq*rinv;
171                 rsw     = r - ic->rvdw_switch;
172                 rsw     = (rsw >= 0.0 ? rsw : 0.0);
173 #endif
174 #ifdef VDW_FORCE_SWITCH
175                 frLJ   +=
176                     -c6*(ic->dispersion_shift.c2 + ic->dispersion_shift.c3*rsw)*rsw*rsw*r
177                     + c12*(ic->repulsion_shift.c2 + ic->repulsion_shift.c3*rsw)*rsw*rsw*r;
178 #if defined CALC_ENERGIES
179                 VLJ    +=
180                     -c6*(-ic->dispersion_shift.c2/3 - ic->dispersion_shift.c3/4*rsw)*rsw*rsw*rsw
181                     + c12*(-ic->repulsion_shift.c2/3 - ic->repulsion_shift.c3/4*rsw)*rsw*rsw*rsw;
182 #endif
183 #endif
184
185 #if defined CALC_ENERGIES || defined VDW_POT_SWITCH
186                 /* Masking shoule be done after force switching,
187                  * but before potential switching.
188                  */
189                 /* Need to zero the interaction if r >= rcut
190                  * or there should be exclusion. */
191                 VLJ     = VLJ * skipmask * interact;
192                 /* 2 more flops for LJ energy */
193 #endif
194
195 #ifdef VDW_POT_SWITCH
196                 {
197                     real sw, dsw;
198
199                     sw    = 1.0 + (swV3 + (swV4+ swV5*rsw)*rsw)*rsw*rsw*rsw;
200                     dsw   = (swF2 + (swF3 + swF4*rsw)*rsw)*rsw*rsw;
201
202                     frLJ  = frLJ*sw - r*VLJ*dsw;
203                     VLJ  *= sw;
204                 }
205 #endif
206
207 #ifdef VDW_CUTOFF_CHECK
208                 /* Mask for VdW cut-off shorter than Coulomb cut-off */
209                 {
210                     real skipmask_rvdw;
211
212                     skipmask_rvdw = (rsq < rvdw2);
213                     frLJ         *= skipmask_rvdw;
214 #ifdef CALC_ENERGIES
215                     VLJ    *= skipmask_rvdw;
216 #endif
217                 }
218 #endif
219
220 #ifdef CALC_ENERGIES
221 #ifdef ENERGY_GROUPS
222                 Vvdw[egp_sh_i[i]+((egp_cj>>(nbat->neg_2log*j)) & egp_mask)] += VLJ;
223 #else
224                 Vvdw_ci += VLJ;
225                 /* 1 flop for LJ energy addition */
226 #endif
227 #endif
228             }
229
230 #ifdef CALC_COULOMB
231             /* Enforce the cut-off and perhaps exclusions. In
232              * those cases, rinv is zero because of skipmask,
233              * but fcoul and vcoul will later be non-zero (in
234              * both RF and table cases) because of the
235              * contributions that do not depend on rinv. These
236              * contributions cannot be allowed to accumulate
237              * to the force and potential, and the easiest way
238              * to do this is to zero the charges in
239              * advance. */
240             qq = skipmask * qi[i] * q[aj];
241
242 #ifdef CALC_COUL_RF
243             fcoul  = qq*(interact*rinv*rinvsq - k_rf2);
244             /* 4 flops for RF force */
245 #ifdef CALC_ENERGIES
246             vcoul  = qq*(interact*rinv + k_rf*rsq - c_rf);
247             /* 4 flops for RF energy */
248 #endif
249 #endif
250
251 #ifdef CALC_COUL_TAB
252             rs     = rsq*rinv*ic->tabq_scale;
253             ri     = (int)rs;
254             frac   = rs - ri;
255 #ifndef GMX_DOUBLE
256             /* fexcl = F_i + frac * (F_(i+1)-F_i) */
257             fexcl  = tab_coul_FDV0[ri*4] + frac*tab_coul_FDV0[ri*4+1];
258 #else
259             /* fexcl = (1-frac) * F_i + frac * F_(i+1) */
260             fexcl  = (1 - frac)*tab_coul_F[ri] + frac*tab_coul_F[ri+1];
261 #endif
262             fcoul  = interact*rinvsq - fexcl;
263             /* 7 flops for float 1/r-table force */
264 #ifdef CALC_ENERGIES
265 #ifndef GMX_DOUBLE
266             vcoul  = qq*(interact*(rinv - ic->sh_ewald)
267                          -(tab_coul_FDV0[ri*4+2]
268                            -halfsp*frac*(tab_coul_FDV0[ri*4] + fexcl)));
269             /* 7 flops for float 1/r-table energy (8 with excls) */
270 #else
271             vcoul  = qq*(interact*(rinv - ic->sh_ewald)
272                          -(tab_coul_V[ri]
273                            -halfsp*frac*(tab_coul_F[ri] + fexcl)));
274 #endif
275 #endif
276             fcoul *= qq*rinv;
277 #endif
278
279 #ifdef CALC_ENERGIES
280 #ifdef ENERGY_GROUPS
281             Vc[egp_sh_i[i]+((egp_cj>>(nbat->neg_2log*j)) & egp_mask)] += vcoul;
282 #else
283             Vc_ci += vcoul;
284             /* 1 flop for Coulomb energy addition */
285 #endif
286 #endif
287 #endif
288
289 #ifdef CALC_COULOMB
290 #ifdef HALF_LJ
291             if (i < UNROLLI/2)
292 #endif
293             {
294                 fscal = frLJ*rinvsq + fcoul;
295                 /* 2 flops for scalar LJ+Coulomb force */
296             }
297 #ifdef HALF_LJ
298             else
299             {
300                 fscal = fcoul;
301             }
302 #endif
303 #else
304             fscal = frLJ*rinvsq;
305 #endif
306             fx = fscal*dx;
307             fy = fscal*dy;
308             fz = fscal*dz;
309
310             /* Increment i-atom force */
311             fi[i*FI_STRIDE+XX] += fx;
312             fi[i*FI_STRIDE+YY] += fy;
313             fi[i*FI_STRIDE+ZZ] += fz;
314             /* Decrement j-atom force */
315             f[aj*F_STRIDE+XX]  -= fx;
316             f[aj*F_STRIDE+YY]  -= fy;
317             f[aj*F_STRIDE+ZZ]  -= fz;
318             /* 9 flops for force addition */
319         }
320     }
321 }
322
323 #undef interact
324 #undef EXCL_FORCES