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