New VDW kernel flavour for LJ-PME and updated group kernels
[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, c6grid, cexp1, cexp2, br;
80     real *        charge;
81     real *        shiftvec;
82     real *        vdwparam, *vdwgridparam;
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     real          ewclj, ewclj2, ewclj6, ewcljrsq, poly, exponent, sh_lj_ewald;
101     gmx_bool      bExactElecCutoff, bExactVdwCutoff, bExactCutoff;
102
103     x                   = xx[0];
104     f                   = ff[0];
105     ielec               = nlist->ielec;
106     ivdw                = nlist->ivdw;
107
108     fshift              = fr->fshift[0];
109     velecgrp            = kernel_data->energygrp_elec;
110     vvdwgrp             = kernel_data->energygrp_vdw;
111     tabscale            = kernel_data->table_elec_vdw->scale;
112     VFtab               = kernel_data->table_elec_vdw->data;
113
114     sh_ewald            = fr->ic->sh_ewald;
115     ewtab               = fr->ic->tabq_coul_FDV0;
116     ewtabscale          = fr->ic->tabq_scale;
117     ewtabhalfspace      = 0.5/ewtabscale;
118
119     rcoulomb2           = fr->rcoulomb*fr->rcoulomb;
120     rvdw                = fr->rvdw;
121     rvdw2               = rvdw*rvdw;
122     sh_dispersion       = fr->ic->dispersion_shift.cpot;
123     sh_repulsion        = fr->ic->repulsion_shift.cpot;
124     sh_lj_ewald         = fr->ic->sh_lj_ewald;
125
126     ewclj               = fr->ewaldcoeff_lj;
127     ewclj2              = ewclj*ewclj;
128     ewclj6              = ewclj2*ewclj2*ewclj2;
129
130     if (fr->coulomb_modifier == eintmodPOTSWITCH)
131     {
132         d               = fr->rcoulomb-fr->rcoulomb_switch;
133         elec_swV3       = -10.0/(d*d*d);
134         elec_swV4       =  15.0/(d*d*d*d);
135         elec_swV5       =  -6.0/(d*d*d*d*d);
136         elec_swF2       = -30.0/(d*d*d);
137         elec_swF3       =  60.0/(d*d*d*d);
138         elec_swF4       = -30.0/(d*d*d*d*d);
139     }
140     else
141     {
142         /* Avoid warnings from stupid compilers (looking at you, Clang!) */
143         elec_swV3 = elec_swV4 = elec_swV5 = elec_swF2 = elec_swF3 = elec_swF4 = 0.0;
144     }
145     if (fr->vdw_modifier == eintmodPOTSWITCH)
146     {
147         d               = fr->rvdw-fr->rvdw_switch;
148         vdw_swV3        = -10.0/(d*d*d);
149         vdw_swV4        =  15.0/(d*d*d*d);
150         vdw_swV5        =  -6.0/(d*d*d*d*d);
151         vdw_swF2        = -30.0/(d*d*d);
152         vdw_swF3        =  60.0/(d*d*d*d);
153         vdw_swF4        = -30.0/(d*d*d*d*d);
154     }
155     else
156     {
157         /* Avoid warnings from stupid compilers (looking at you, Clang!) */
158         vdw_swV3 = vdw_swV4 = vdw_swV5 = vdw_swF2 = vdw_swF3 = vdw_swF4 = 0.0;
159     }
160
161     bExactElecCutoff    = (fr->coulomb_modifier != eintmodNONE) || fr->eeltype == eelRF_ZERO;
162     bExactVdwCutoff     = (fr->vdw_modifier != eintmodNONE);
163     bExactCutoff        = bExactElecCutoff && bExactVdwCutoff;
164
165     if (bExactCutoff)
166     {
167         rcutoff  = ( fr->rcoulomb > fr->rvdw ) ? fr->rcoulomb : fr->rvdw;
168         rcutoff2 = rcutoff*rcutoff;
169     }
170     else
171     {
172         /* Fix warnings for stupid compilers */
173         rcutoff = rcutoff2 = 1e30;
174     }
175
176     /* avoid compiler warnings for cases that cannot happen */
177     nnn                 = 0;
178     eps                 = 0.0;
179     eps2                = 0.0;
180
181     /* 3 VdW parameters for Buckingham, otherwise 2 */
182     nvdwparam           = (ivdw == GMX_NBKERNEL_VDW_BUCKINGHAM) ? 3 : 2;
183     table_nelements     = 12;
184
185     charge              = mdatoms->chargeA;
186     type                = mdatoms->typeA;
187     facel               = fr->epsfac;
188     shiftvec            = fr->shift_vec[0];
189     vdwparam            = fr->nbfp;
190     ntype               = fr->ntype;
191     vdwgridparam        = fr->ljpme_c6grid;
192
193     for (n = 0; (n < nlist->nri); n++)
194     {
195         is3              = 3*nlist->shift[n];
196         shX              = shiftvec[is3];
197         shY              = shiftvec[is3+1];
198         shZ              = shiftvec[is3+2];
199         nj0              = nlist->jindex[n];
200         nj1              = nlist->jindex[n+1];
201         ii               = nlist->iinr[n];
202         ii3              = 3*ii;
203         ix               = shX + x[ii3+0];
204         iy               = shY + x[ii3+1];
205         iz               = shZ + x[ii3+2];
206         iq               = facel*charge[ii];
207         nti              = nvdwparam*ntype*type[ii];
208         vctot            = 0;
209         vvdwtot          = 0;
210         fix              = 0;
211         fiy              = 0;
212         fiz              = 0;
213
214         for (k = nj0; (k < nj1); k++)
215         {
216             jnr              = nlist->jjnr[k];
217             j3               = 3*jnr;
218             jx               = x[j3+0];
219             jy               = x[j3+1];
220             jz               = x[j3+2];
221             dx               = ix - jx;
222             dy               = iy - jy;
223             dz               = iz - jz;
224             rsq              = dx*dx+dy*dy+dz*dz;
225             rinv             = gmx_invsqrt(rsq);
226             rinvsq           = rinv*rinv;
227             felec            = 0;
228             fvdw             = 0;
229             velec            = 0;
230             vvdw             = 0;
231
232             if (bExactCutoff && rsq >= rcutoff2)
233             {
234                 continue;
235             }
236
237             if (ielec == GMX_NBKERNEL_ELEC_CUBICSPLINETABLE || ivdw == GMX_NBKERNEL_VDW_CUBICSPLINETABLE)
238             {
239                 r                = rsq*rinv;
240                 rt               = r*tabscale;
241                 n0               = rt;
242                 eps              = rt-n0;
243                 eps2             = eps*eps;
244                 nnn              = table_nelements*n0;
245             }
246
247             /* Coulomb interaction. ielec==0 means no interaction */
248             if (ielec != GMX_NBKERNEL_ELEC_NONE)
249             {
250                 qq               = iq*charge[jnr];
251
252                 switch (ielec)
253                 {
254                     case GMX_NBKERNEL_ELEC_NONE:
255                         break;
256
257                     case GMX_NBKERNEL_ELEC_COULOMB:
258                         /* Vanilla cutoff coulomb */
259                         velec            = qq*rinv;
260                         felec            = velec*rinvsq;
261                         /* The shift for the Coulomb potential is stored in
262                          * the RF parameter c_rf, which is 0 without shift
263                          */
264                         velec           -= qq*fr->ic->c_rf;
265                         break;
266
267                     case GMX_NBKERNEL_ELEC_REACTIONFIELD:
268                         /* Reaction-field */
269                         velec            = qq*(rinv+fr->k_rf*rsq-fr->c_rf);
270                         felec            = qq*(rinv*rinvsq-2.0*fr->k_rf);
271                         break;
272
273                     case GMX_NBKERNEL_ELEC_CUBICSPLINETABLE:
274                         /* Tabulated coulomb */
275                         Y                = VFtab[nnn];
276                         F                = VFtab[nnn+1];
277                         Geps             = eps*VFtab[nnn+2];
278                         Heps2            = eps2*VFtab[nnn+3];
279                         Fp               = F+Geps+Heps2;
280                         VV               = Y+eps*Fp;
281                         FF               = Fp+Geps+2.0*Heps2;
282                         velec            = qq*VV;
283                         felec            = -qq*FF*tabscale*rinv;
284                         break;
285
286                     case GMX_NBKERNEL_ELEC_GENERALIZEDBORN:
287                         /* GB */
288                         gmx_fatal(FARGS, "Death & horror! GB generic interaction not implemented.\n");
289                         break;
290
291                     case GMX_NBKERNEL_ELEC_EWALD:
292                         ewrt             = rsq*rinv*ewtabscale;
293                         ewitab           = ewrt;
294                         eweps            = ewrt-ewitab;
295                         ewitab           = 4*ewitab;
296                         felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
297                         rinvcorr         = (fr->coulomb_modifier == eintmodPOTSHIFT) ? rinv-fr->ic->sh_ewald : rinv;
298                         velec            = qq*(rinvcorr-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
299                         felec            = qq*rinv*(rinvsq-felec);
300                         break;
301
302                     default:
303                         gmx_fatal(FARGS, "Death & horror! No generic coulomb interaction for ielec=%d.\n", ielec);
304                         break;
305                 }
306                 if (fr->coulomb_modifier == eintmodPOTSWITCH)
307                 {
308                     d                = rsq*rinv-fr->rcoulomb_switch;
309                     d                = (d > 0.0) ? d : 0.0;
310                     d2               = d*d;
311                     sw               = 1.0+d2*d*(elec_swV3+d*(elec_swV4+d*elec_swV5));
312                     dsw              = d2*(elec_swF2+d*(elec_swF3+d*elec_swF4));
313                     /* Apply switch function. Note that felec=f/r since it will be multiplied
314                      * by the i-j displacement vector. This means felec'=f'/r=-(v*sw)'/r=
315                      * -(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=felec*sw-v*dsw/r
316                      */
317                     felec            = felec*sw - rinv*velec*dsw;
318                     /* Once we have used velec to update felec we can modify velec too */
319                     velec           *= sw;
320                 }
321                 if (bExactElecCutoff)
322                 {
323                     felec            = (rsq < rcoulomb2) ? felec : 0.0;
324                     velec            = (rsq < rcoulomb2) ? velec : 0.0;
325                 }
326                 vctot           += velec;
327             } /* End of coulomb interactions */
328
329
330             /* VdW interaction. ivdw==0 means no interaction */
331             if (ivdw != GMX_NBKERNEL_VDW_NONE)
332             {
333                 tj               = nti+nvdwparam*type[jnr];
334
335                 switch (ivdw)
336                 {
337                     case GMX_NBKERNEL_VDW_NONE:
338                         break;
339
340                     case GMX_NBKERNEL_VDW_LENNARDJONES:
341                         /* Vanilla Lennard-Jones cutoff */
342                         c6               = vdwparam[tj];
343                         c12              = vdwparam[tj+1];
344                         rinvsix          = rinvsq*rinvsq*rinvsq;
345                         vvdw_disp        = c6*rinvsix;
346                         vvdw_rep         = c12*rinvsix*rinvsix;
347                         fvdw             = (vvdw_rep-vvdw_disp)*rinvsq;
348                         if (fr->vdw_modifier == eintmodPOTSHIFT)
349                         {
350                             vvdw             = (vvdw_rep + c12*sh_repulsion)/12.0 - (vvdw_disp + c6*sh_dispersion)/6.0;
351                         }
352                         else
353                         {
354                             vvdw             = vvdw_rep/12.0-vvdw_disp/6.0;
355                         }
356                         break;
357
358                     case GMX_NBKERNEL_VDW_BUCKINGHAM:
359                         /* Buckingham */
360                         c6               = vdwparam[tj];
361                         cexp1            = vdwparam[tj+1];
362                         cexp2            = vdwparam[tj+2];
363
364                         rinvsix          = rinvsq*rinvsq*rinvsq;
365                         vvdw_disp        = c6*rinvsix;
366                         br               = cexp2*rsq*rinv;
367                         vvdw_rep         = cexp1*exp(-br);
368                         fvdw             = (br*vvdw_rep-vvdw_disp)*rinvsq;
369                         if (fr->vdw_modifier == eintmodPOTSHIFT)
370                         {
371                             vvdw             = (vvdw_rep-cexp1*exp(-cexp2*rvdw))-(vvdw_disp + c6*sh_dispersion)/6.0;
372                         }
373                         else
374                         {
375                             vvdw             = vvdw_rep-vvdw_disp/6.0;
376                         }
377                         break;
378
379                     case GMX_NBKERNEL_VDW_CUBICSPLINETABLE:
380                         /* Tabulated VdW */
381                         c6               = vdwparam[tj];
382                         c12              = vdwparam[tj+1];
383                         Y                = VFtab[nnn+4];
384                         F                = VFtab[nnn+5];
385                         Geps             = eps*VFtab[nnn+6];
386                         Heps2            = eps2*VFtab[nnn+7];
387                         Fp               = F+Geps+Heps2;
388                         VV               = Y+eps*Fp;
389                         FF               = Fp+Geps+2.0*Heps2;
390                         vvdw_disp        = c6*VV;
391                         fijD             = c6*FF;
392                         Y                = VFtab[nnn+8];
393                         F                = VFtab[nnn+9];
394                         Geps             = eps*VFtab[nnn+10];
395                         Heps2            = eps2*VFtab[nnn+11];
396                         Fp               = F+Geps+Heps2;
397                         VV               = Y+eps*Fp;
398                         FF               = Fp+Geps+2.0*Heps2;
399                         vvdw_rep         = c12*VV;
400                         fijR             = c12*FF;
401                         fvdw             = -(fijD+fijR)*tabscale*rinv;
402                         vvdw             = vvdw_disp + vvdw_rep;
403                         break;
404
405
406                     case GMX_NBKERNEL_VDW_LJEWALD:
407                         /* LJ-PME */
408                         rinvsix          = rinvsq*rinvsq*rinvsq;
409                         ewcljrsq         = ewclj2*rsq;
410                         exponent         = exp(-ewcljrsq);
411                         poly             = exponent*(1.0 + ewcljrsq + ewcljrsq*ewcljrsq*0.5);
412                         c6               = vdwparam[tj];
413                         c12              = vdwparam[tj+1];
414                         c6grid           = vdwgridparam[tj];
415                         vvdw_disp        = (c6-c6grid*(1.0-poly))*rinvsix;
416                         vvdw_rep         = c12*rinvsix*rinvsix;
417                         fvdw             = (vvdw_rep - vvdw_disp - c6grid*(1.0/6.0)*exponent*ewclj6)*rinvsq;
418                         if (fr->vdw_modifier == eintmodPOTSHIFT)
419                         {
420                             vvdw             = (vvdw_rep + c12*sh_repulsion)/12.0 - (vvdw_disp + c6*sh_dispersion + c6grid*sh_lj_ewald)/6.0;
421                         }
422                         else
423                         {
424                             vvdw             = vvdw_rep/12.0-vvdw_disp/6.0;
425                         }
426                         break;
427
428                     default:
429                         gmx_fatal(FARGS, "Death & horror! No generic VdW interaction for ivdw=%d.\n", ivdw);
430                         break;
431                 }
432                 if (fr->vdw_modifier == eintmodPOTSWITCH)
433                 {
434                     d                = rsq*rinv-fr->rvdw_switch;
435                     d                = (d > 0.0) ? d : 0.0;
436                     d2               = d*d;
437                     sw               = 1.0+d2*d*(vdw_swV3+d*(vdw_swV4+d*vdw_swV5));
438                     dsw              = d2*(vdw_swF2+d*(vdw_swF3+d*vdw_swF4));
439                     /* See coulomb interaction for the force-switch formula */
440                     fvdw             = fvdw*sw - rinv*vvdw*dsw;
441                     vvdw            *= sw;
442                 }
443                 if (bExactVdwCutoff)
444                 {
445                     fvdw             = (rsq < rvdw2) ? fvdw : 0.0;
446                     vvdw             = (rsq < rvdw2) ? vvdw : 0.0;
447                 }
448                 vvdwtot         += vvdw;
449             } /* end VdW interactions */
450
451             fscal            = felec+fvdw;
452
453             tx               = fscal*dx;
454             ty               = fscal*dy;
455             tz               = fscal*dz;
456             fix              = fix + tx;
457             fiy              = fiy + ty;
458             fiz              = fiz + tz;
459             f[j3+0]          = f[j3+0] - tx;
460             f[j3+1]          = f[j3+1] - ty;
461             f[j3+2]          = f[j3+2] - tz;
462         }
463
464         f[ii3+0]         = f[ii3+0] + fix;
465         f[ii3+1]         = f[ii3+1] + fiy;
466         f[ii3+2]         = f[ii3+2] + fiz;
467         fshift[is3]      = fshift[is3]+fix;
468         fshift[is3+1]    = fshift[is3+1]+fiy;
469         fshift[is3+2]    = fshift[is3+2]+fiz;
470         ggid             = nlist->gid[n];
471         velecgrp[ggid]  += vctot;
472         vvdwgrp[ggid]   += vvdwtot;
473     }
474     /* Estimate flops, average for generic kernel:
475      * 12 flops per outer iteration
476      * 50 flops per inner iteration
477      */
478     inc_nrnb(nrnb, eNR_NBKERNEL_GENERIC, nlist->nri*12 + nlist->jindex[n]*50);
479 }