Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_generic.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2012,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <math.h>
42
43 #include "types/simple.h"
44 #include "vec.h"
45 #include "typedefs.h"
46 #include "nb_generic.h"
47 #include "nrnb.h"
48
49 #include "nonbonded.h"
50 #include "nb_kernel.h"
51
52
53 void
54 gmx_nb_generic_kernel(t_nblist *                nlist,
55                       rvec *                    xx,
56                       rvec *                    ff,
57                       t_forcerec *              fr,
58                       t_mdatoms *               mdatoms,
59                       nb_kernel_data_t *        kernel_data,
60                       t_nrnb *                  nrnb)
61 {
62     int           nri, ntype, table_nelements, ielec, ivdw;
63     real          facel, gbtabscale;
64     int           n, ii, is3, ii3, k, nj0, nj1, jnr, j3, ggid, nnn, n0;
65     real          shX, shY, shZ;
66     real          fscal, felec, fvdw, velec, vvdw, tx, ty, tz;
67     real          rinvsq;
68     real          iq;
69     real          qq, vctot;
70     int           nti, nvdwparam;
71     int           tj;
72     real          rt, r, eps, eps2, Y, F, Geps, Heps2, VV, FF, Fp, fijD, fijR;
73     real          rinvsix;
74     real          vvdwtot;
75     real          vvdw_rep, vvdw_disp;
76     real          ix, iy, iz, fix, fiy, fiz;
77     real          jx, jy, jz;
78     real          dx, dy, dz, rsq, rinv;
79     real          c6, c12, cexp1, cexp2, br;
80     real *        charge;
81     real *        shiftvec;
82     real *        vdwparam;
83     int *         shift;
84     int *         type;
85     real *        fshift;
86     real *        velecgrp;
87     real *        vvdwgrp;
88     real          tabscale;
89     real *        VFtab;
90     real *        x;
91     real *        f;
92     int           ewitab;
93     real          ewtabscale, eweps, sh_ewald, ewrt, ewtabhalfspace;
94     real *        ewtab;
95     real          rcoulomb2, rvdw, rvdw2, sh_dispersion, sh_repulsion;
96     real          rcutoff, rcutoff2;
97     real          rswitch_elec, rswitch_vdw, d, d2, sw, dsw, rinvcorr;
98     real          elec_swV3, elec_swV4, elec_swV5, elec_swF2, elec_swF3, elec_swF4;
99     real          vdw_swV3, vdw_swV4, vdw_swV5, vdw_swF2, vdw_swF3, vdw_swF4;
100     gmx_bool      bExactElecCutoff, bExactVdwCutoff, bExactCutoff;
101
102     x                   = xx[0];
103     f                   = ff[0];
104     ielec               = nlist->ielec;
105     ivdw                = nlist->ivdw;
106
107     fshift              = fr->fshift[0];
108     velecgrp            = kernel_data->energygrp_elec;
109     vvdwgrp             = kernel_data->energygrp_vdw;
110     tabscale            = kernel_data->table_elec_vdw->scale;
111     VFtab               = kernel_data->table_elec_vdw->data;
112
113     sh_ewald            = fr->ic->sh_ewald;
114     ewtab               = fr->ic->tabq_coul_FDV0;
115     ewtabscale          = fr->ic->tabq_scale;
116     ewtabhalfspace      = 0.5/ewtabscale;
117
118     rcoulomb2           = fr->rcoulomb*fr->rcoulomb;
119     rvdw                = fr->rvdw;
120     rvdw2               = rvdw*rvdw;
121     sh_dispersion       = fr->ic->dispersion_shift.cpot;
122     sh_repulsion        = fr->ic->repulsion_shift.cpot;
123
124     if (fr->coulomb_modifier == eintmodPOTSWITCH)
125     {
126         d               = fr->rcoulomb-fr->rcoulomb_switch;
127         elec_swV3       = -10.0/(d*d*d);
128         elec_swV4       =  15.0/(d*d*d*d);
129         elec_swV5       =  -6.0/(d*d*d*d*d);
130         elec_swF2       = -30.0/(d*d*d);
131         elec_swF3       =  60.0/(d*d*d*d);
132         elec_swF4       = -30.0/(d*d*d*d*d);
133     }
134     else
135     {
136         /* Avoid warnings from stupid compilers (looking at you, Clang!) */
137         elec_swV3 = elec_swV4 = elec_swV5 = elec_swF2 = elec_swF3 = elec_swF4 = 0.0;
138     }
139     if (fr->vdw_modifier == eintmodPOTSWITCH)
140     {
141         d               = fr->rvdw-fr->rvdw_switch;
142         vdw_swV3        = -10.0/(d*d*d);
143         vdw_swV4        =  15.0/(d*d*d*d);
144         vdw_swV5        =  -6.0/(d*d*d*d*d);
145         vdw_swF2        = -30.0/(d*d*d);
146         vdw_swF3        =  60.0/(d*d*d*d);
147         vdw_swF4        = -30.0/(d*d*d*d*d);
148     }
149     else
150     {
151         /* Avoid warnings from stupid compilers (looking at you, Clang!) */
152         vdw_swV3 = vdw_swV4 = vdw_swV5 = vdw_swF2 = vdw_swF3 = vdw_swF4 = 0.0;
153     }
154
155     bExactElecCutoff    = (fr->coulomb_modifier != eintmodNONE) || fr->eeltype == eelRF_ZERO;
156     bExactVdwCutoff     = (fr->vdw_modifier != eintmodNONE);
157     bExactCutoff        = bExactElecCutoff || bExactVdwCutoff;
158
159     if (bExactCutoff)
160     {
161         rcutoff  = ( fr->rcoulomb > fr->rvdw ) ? fr->rcoulomb : fr->rvdw;
162         rcutoff2 = rcutoff*rcutoff;
163     }
164     else
165     {
166         /* Fix warnings for stupid compilers */
167         rcutoff = rcutoff2 = 1e30;
168     }
169
170     /* avoid compiler warnings for cases that cannot happen */
171     nnn                 = 0;
172     eps                 = 0.0;
173     eps2                = 0.0;
174
175     /* 3 VdW parameters for buckingham, otherwise 2 */
176     nvdwparam           = (ivdw == GMX_NBKERNEL_VDW_BUCKINGHAM) ? 3 : 2;
177     table_nelements     = 12;
178
179     charge              = mdatoms->chargeA;
180     type                = mdatoms->typeA;
181     facel               = fr->epsfac;
182     shiftvec            = fr->shift_vec[0];
183     vdwparam            = fr->nbfp;
184     ntype               = fr->ntype;
185
186     for (n = 0; (n < nlist->nri); n++)
187     {
188         is3              = 3*nlist->shift[n];
189         shX              = shiftvec[is3];
190         shY              = shiftvec[is3+1];
191         shZ              = shiftvec[is3+2];
192         nj0              = nlist->jindex[n];
193         nj1              = nlist->jindex[n+1];
194         ii               = nlist->iinr[n];
195         ii3              = 3*ii;
196         ix               = shX + x[ii3+0];
197         iy               = shY + x[ii3+1];
198         iz               = shZ + x[ii3+2];
199         iq               = facel*charge[ii];
200         nti              = nvdwparam*ntype*type[ii];
201         vctot            = 0;
202         vvdwtot          = 0;
203         fix              = 0;
204         fiy              = 0;
205         fiz              = 0;
206
207         for (k = nj0; (k < nj1); k++)
208         {
209             jnr              = nlist->jjnr[k];
210             j3               = 3*jnr;
211             jx               = x[j3+0];
212             jy               = x[j3+1];
213             jz               = x[j3+2];
214             dx               = ix - jx;
215             dy               = iy - jy;
216             dz               = iz - jz;
217             rsq              = dx*dx+dy*dy+dz*dz;
218             rinv             = gmx_invsqrt(rsq);
219             rinvsq           = rinv*rinv;
220             felec            = 0;
221             fvdw             = 0;
222             velec            = 0;
223             vvdw             = 0;
224
225             if (bExactCutoff && rsq > rcutoff2)
226             {
227                 continue;
228             }
229
230             if (ielec == GMX_NBKERNEL_ELEC_CUBICSPLINETABLE || ivdw == GMX_NBKERNEL_VDW_CUBICSPLINETABLE)
231             {
232                 r                = rsq*rinv;
233                 rt               = r*tabscale;
234                 n0               = rt;
235                 eps              = rt-n0;
236                 eps2             = eps*eps;
237                 nnn              = table_nelements*n0;
238             }
239
240             /* Coulomb interaction. ielec==0 means no interaction */
241             if (ielec != GMX_NBKERNEL_ELEC_NONE)
242             {
243                 qq               = iq*charge[jnr];
244
245                 switch (ielec)
246                 {
247                     case GMX_NBKERNEL_ELEC_NONE:
248                         break;
249
250                     case GMX_NBKERNEL_ELEC_COULOMB:
251                         /* Vanilla cutoff coulomb */
252                         velec            = qq*rinv;
253                         felec            = velec*rinvsq;
254                         break;
255
256                     case GMX_NBKERNEL_ELEC_REACTIONFIELD:
257                         /* Reaction-field */
258                         velec            = qq*(rinv+fr->k_rf*rsq-fr->c_rf);
259                         felec            = qq*(rinv*rinvsq-2.0*fr->k_rf);
260                         break;
261
262                     case GMX_NBKERNEL_ELEC_CUBICSPLINETABLE:
263                         /* Tabulated coulomb */
264                         Y                = VFtab[nnn];
265                         F                = VFtab[nnn+1];
266                         Geps             = eps*VFtab[nnn+2];
267                         Heps2            = eps2*VFtab[nnn+3];
268                         Fp               = F+Geps+Heps2;
269                         VV               = Y+eps*Fp;
270                         FF               = Fp+Geps+2.0*Heps2;
271                         velec            = qq*VV;
272                         felec            = -qq*FF*tabscale*rinv;
273                         break;
274
275                     case GMX_NBKERNEL_ELEC_GENERALIZEDBORN:
276                         /* GB */
277                         gmx_fatal(FARGS, "Death & horror! GB generic interaction not implemented.\n");
278                         break;
279
280                     case GMX_NBKERNEL_ELEC_EWALD:
281                         ewrt             = rsq*rinv*ewtabscale;
282                         ewitab           = ewrt;
283                         eweps            = ewrt-ewitab;
284                         ewitab           = 4*ewitab;
285                         felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
286                         rinvcorr         = (fr->coulomb_modifier == eintmodPOTSHIFT) ? rinv-fr->ic->sh_ewald : rinv;
287                         velec            = qq*(rinvcorr-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
288                         felec            = qq*rinv*(rinvsq-felec);
289                         break;
290
291                     default:
292                         gmx_fatal(FARGS, "Death & horror! No generic coulomb interaction for ielec=%d.\n", ielec);
293                         break;
294                 }
295                 if (fr->coulomb_modifier == eintmodPOTSWITCH)
296                 {
297                     d                = rsq*rinv-fr->rcoulomb_switch;
298                     d                = (d > 0.0) ? d : 0.0;
299                     d2               = d*d;
300                     sw               = 1.0+d2*d*(elec_swV3+d*(elec_swV4+d*elec_swV5));
301                     dsw              = d2*(elec_swF2+d*(elec_swF3+d*elec_swF4));
302                     /* Apply switch function. Note that felec=f/r since it will be multiplied
303                      * by the i-j displacement vector. This means felec'=f'/r=-(v*sw)'/r=
304                      * -(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=felec*sw-v*dsw/r
305                      */
306                     felec            = felec*sw - rinv*velec*dsw;
307                     /* Once we have used velec to update felec we can modify velec too */
308                     velec           *= sw;
309                 }
310                 if (bExactElecCutoff)
311                 {
312                     felec            = (rsq <= rcoulomb2) ? felec : 0.0;
313                     velec            = (rsq <= rcoulomb2) ? velec : 0.0;
314                 }
315                 vctot           += velec;
316             } /* End of coulomb interactions */
317
318
319             /* VdW interaction. ivdw==0 means no interaction */
320             if (ivdw != GMX_NBKERNEL_VDW_NONE)
321             {
322                 tj               = nti+nvdwparam*type[jnr];
323
324                 switch (ivdw)
325                 {
326                     case GMX_NBKERNEL_VDW_NONE:
327                         break;
328
329                     case GMX_NBKERNEL_VDW_LENNARDJONES:
330                         /* Vanilla Lennard-Jones cutoff */
331                         c6               = vdwparam[tj];
332                         c12              = vdwparam[tj+1];
333                         rinvsix          = rinvsq*rinvsq*rinvsq;
334                         vvdw_disp        = c6*rinvsix;
335                         vvdw_rep         = c12*rinvsix*rinvsix;
336                         fvdw             = (vvdw_rep-vvdw_disp)*rinvsq;
337                         if (fr->vdw_modifier == eintmodPOTSHIFT)
338                         {
339                             vvdw             = (vvdw_rep + c12*sh_repulsion)/12.0 - (vvdw_disp + c6*sh_dispersion)/6.0;
340                         }
341                         else
342                         {
343                             vvdw             = vvdw_rep/12.0-vvdw_disp/6.0;
344                         }
345                         break;
346
347                     case GMX_NBKERNEL_VDW_BUCKINGHAM:
348                         /* Buckingham */
349                         c6               = vdwparam[tj];
350                         cexp1            = vdwparam[tj+1];
351                         cexp2            = vdwparam[tj+2];
352
353                         rinvsix          = rinvsq*rinvsq*rinvsq;
354                         vvdw_disp        = c6*rinvsix;
355                         br               = cexp2*rsq*rinv;
356                         vvdw_rep         = cexp1*exp(-br);
357                         fvdw             = (br*vvdw_rep-vvdw_disp)*rinvsq;
358                         if (fr->vdw_modifier == eintmodPOTSHIFT)
359                         {
360                             vvdw             = (vvdw_rep-cexp1*exp(-cexp2*rvdw))-(vvdw_disp + c6*sh_dispersion)/6.0;
361                         }
362                         else
363                         {
364                             vvdw             = vvdw_rep-vvdw_disp/6.0;
365                         }
366                         break;
367
368                     case GMX_NBKERNEL_VDW_CUBICSPLINETABLE:
369                         /* Tabulated VdW */
370                         c6               = vdwparam[tj];
371                         c12              = vdwparam[tj+1];
372                         Y                = VFtab[nnn+4];
373                         F                = VFtab[nnn+5];
374                         Geps             = eps*VFtab[nnn+6];
375                         Heps2            = eps2*VFtab[nnn+7];
376                         Fp               = F+Geps+Heps2;
377                         VV               = Y+eps*Fp;
378                         FF               = Fp+Geps+2.0*Heps2;
379                         vvdw_disp        = c6*VV;
380                         fijD             = c6*FF;
381                         Y                = VFtab[nnn+8];
382                         F                = VFtab[nnn+9];
383                         Geps             = eps*VFtab[nnn+10];
384                         Heps2            = eps2*VFtab[nnn+11];
385                         Fp               = F+Geps+Heps2;
386                         VV               = Y+eps*Fp;
387                         FF               = Fp+Geps+2.0*Heps2;
388                         vvdw_rep         = c12*VV;
389                         fijR             = c12*FF;
390                         fvdw             = -(fijD+fijR)*tabscale*rinv;
391                         vvdw             = vvdw_disp + vvdw_rep;
392                         break;
393
394                     default:
395                         gmx_fatal(FARGS, "Death & horror! No generic VdW interaction for ivdw=%d.\n", ivdw);
396                         break;
397                 }
398                 if (fr->vdw_modifier == eintmodPOTSWITCH)
399                 {
400                     d                = rsq*rinv-fr->rvdw_switch;
401                     d                = (d > 0.0) ? d : 0.0;
402                     d2               = d*d;
403                     sw               = 1.0+d2*d*(vdw_swV3+d*(vdw_swV4+d*vdw_swV5));
404                     dsw              = d2*(vdw_swF2+d*(vdw_swF3+d*vdw_swF4));
405                     /* See coulomb interaction for the force-switch formula */
406                     fvdw             = fvdw*sw - rinv*vvdw*dsw;
407                     vvdw            *= sw;
408                 }
409                 if (bExactVdwCutoff)
410                 {
411                     fvdw             = (rsq <= rvdw2) ? fvdw : 0.0;
412                     vvdw             = (rsq <= rvdw2) ? vvdw : 0.0;
413                 }
414                 vvdwtot         += vvdw;
415             } /* end VdW interactions */
416
417             fscal            = felec+fvdw;
418
419             tx               = fscal*dx;
420             ty               = fscal*dy;
421             tz               = fscal*dz;
422             fix              = fix + tx;
423             fiy              = fiy + ty;
424             fiz              = fiz + tz;
425             f[j3+0]          = f[j3+0] - tx;
426             f[j3+1]          = f[j3+1] - ty;
427             f[j3+2]          = f[j3+2] - tz;
428         }
429
430         f[ii3+0]         = f[ii3+0] + fix;
431         f[ii3+1]         = f[ii3+1] + fiy;
432         f[ii3+2]         = f[ii3+2] + fiz;
433         fshift[is3]      = fshift[is3]+fix;
434         fshift[is3+1]    = fshift[is3+1]+fiy;
435         fshift[is3+2]    = fshift[is3+2]+fiz;
436         ggid             = nlist->gid[n];
437         velecgrp[ggid]  += vctot;
438         vvdwgrp[ggid]   += vvdwtot;
439     }
440     /* Estimate flops, average for generic kernel:
441      * 12 flops per outer iteration
442      * 50 flops per inner iteration
443      */
444     inc_nrnb(nrnb, eNR_NBKERNEL_GENERIC, nlist->nri*12 + nlist->jindex[n]*50);
445 }