Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_generic_adress.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009 Christoph Junghans, Brad Lambeth.
5  * Copyright (c) 2011 Christoph Junghans, Sebastian Fritsch.
6  * Copyright (c) 2011,2012,2013,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 #include "gmxpre.h"
38
39 #include "nb_generic_adress.h"
40
41 #include <math.h>
42
43 #include "gromacs/gmxlib/nonbonded/nb_kernel.h"
44 #include "gromacs/legacyheaders/nonbonded.h"
45 #include "gromacs/legacyheaders/nrnb.h"
46 #include "gromacs/legacyheaders/typedefs.h"
47 #include "gromacs/legacyheaders/types/simple.h"
48 #include "gromacs/math/vec.h"
49 #include "gromacs/utility/fatalerror.h"
50
51 #define ALMOST_ZERO 1e-30
52 #define ALMOST_ONE 1-(1e-30)
53 void
54 gmx_nb_generic_adress_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     real    *     wf;
103     real          weight_cg1;
104     real          weight_cg2;
105     real          weight_product;
106     real          hybscal; /* the multiplicator to the force for hybrid interactions*/
107     real          force_cap;
108     gmx_bool      bCG;
109     int           egp_nr;
110
111     wf                  = mdatoms->wf;
112
113     force_cap           = fr->adress_ex_forcecap;
114
115     x                   = xx[0];
116     f                   = ff[0];
117     ielec               = nlist->ielec;
118     ivdw                = nlist->ivdw;
119
120     fshift              = fr->fshift[0];
121     velecgrp            = kernel_data->energygrp_elec;
122     vvdwgrp             = kernel_data->energygrp_vdw;
123     tabscale            = kernel_data->table_elec_vdw->scale;
124     VFtab               = kernel_data->table_elec_vdw->data;
125
126     sh_ewald            = fr->ic->sh_ewald;
127     ewtab               = fr->ic->tabq_coul_FDV0;
128     ewtabscale          = fr->ic->tabq_scale;
129     ewtabhalfspace      = 0.5/ewtabscale;
130
131     rcoulomb2           = fr->rcoulomb*fr->rcoulomb;
132     rvdw                = fr->rvdw;
133     rvdw2               = rvdw*rvdw;
134     sh_dispersion       = fr->ic->dispersion_shift.cpot;
135     sh_repulsion        = fr->ic->repulsion_shift.cpot;
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         rcutoff = 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
199     for (n = 0; (n < nlist->nri); n++)
200     {
201         is3              = 3*nlist->shift[n];
202         shX              = shiftvec[is3];
203         shY              = shiftvec[is3+1];
204         shZ              = shiftvec[is3+2];
205         nj0              = nlist->jindex[n];
206         nj1              = nlist->jindex[n+1];
207         ii               = nlist->iinr[n];
208         ii3              = 3*ii;
209         ix               = shX + x[ii3+0];
210         iy               = shY + x[ii3+1];
211         iz               = shZ + x[ii3+2];
212         iq               = facel*charge[ii];
213         nti              = nvdwparam*ntype*type[ii];
214         vctot            = 0;
215         vvdwtot          = 0;
216         fix              = 0;
217         fiy              = 0;
218         fiz              = 0;
219
220         /* We need to find out if this i atom is part of an
221            all-atom or CG energy group  */
222         egp_nr = mdatoms->cENER[ii];
223         bCG    = !fr->adress_group_explicit[egp_nr];
224
225         weight_cg1       = wf[ii];
226
227         if ((!bCG) && weight_cg1 < ALMOST_ZERO)
228         {
229             continue;
230         }
231
232         for (k = nj0; (k < nj1); k++)
233         {
234             jnr              = nlist->jjnr[k];
235             weight_cg2       = wf[jnr];
236             weight_product   = weight_cg1*weight_cg2;
237
238             if (weight_product < ALMOST_ZERO)
239             {
240                 /* if it's a explicit loop, skip this atom */
241                 if (!bCG)
242                 {
243                     continue;
244                 }
245                 else /* if it's a coarse grained loop, include this atom */
246                 {
247                     hybscal = 1.0;
248                 }
249             }
250             else if (weight_product >= ALMOST_ONE)
251             {
252
253                 /* if it's a explicit loop, include this atom */
254                 if (!bCG)
255                 {
256                     hybscal = 1.0;
257                 }
258                 else  /* if it's a coarse grained loop, skip this atom */
259                 {
260                     continue;
261                 }
262             }
263             /* both have double identity, get hybrid scaling factor */
264             else
265             {
266                 hybscal = weight_product;
267
268                 if (bCG)
269                 {
270                     hybscal = 1.0 - hybscal;
271                 }
272             }
273
274             j3               = 3*jnr;
275             jx               = x[j3+0];
276             jy               = x[j3+1];
277             jz               = x[j3+2];
278             dx               = ix - jx;
279             dy               = iy - jy;
280             dz               = iz - jz;
281             rsq              = dx*dx+dy*dy+dz*dz;
282             rinv             = gmx_invsqrt(rsq);
283             rinvsq           = rinv*rinv;
284             felec            = 0;
285             fvdw             = 0;
286             velec            = 0;
287             vvdw             = 0;
288
289             if (bExactCutoff && rsq > rcutoff2)
290             {
291                 continue;
292             }
293
294             if (ielec == GMX_NBKERNEL_ELEC_CUBICSPLINETABLE || ivdw == GMX_NBKERNEL_VDW_CUBICSPLINETABLE)
295             {
296                 r                = rsq*rinv;
297                 rt               = r*tabscale;
298                 n0               = rt;
299                 eps              = rt-n0;
300                 eps2             = eps*eps;
301                 nnn              = table_nelements*n0;
302             }
303
304             /* Coulomb interaction. ielec==0 means no interaction */
305             if (ielec != GMX_NBKERNEL_ELEC_NONE)
306             {
307                 qq               = iq*charge[jnr];
308
309                 switch (ielec)
310                 {
311                     case GMX_NBKERNEL_ELEC_NONE:
312                         break;
313
314                     case GMX_NBKERNEL_ELEC_COULOMB:
315                         /* Vanilla cutoff coulomb */
316                         velec            = qq*rinv;
317                         felec            = velec*rinvsq;
318                         break;
319
320                     case GMX_NBKERNEL_ELEC_REACTIONFIELD:
321                         /* Reaction-field */
322                         velec            = qq*(rinv+fr->k_rf*rsq-fr->c_rf);
323                         felec            = qq*(rinv*rinvsq-2.0*fr->k_rf);
324                         break;
325
326                     case GMX_NBKERNEL_ELEC_CUBICSPLINETABLE:
327                         /* Tabulated coulomb */
328                         Y                = VFtab[nnn];
329                         F                = VFtab[nnn+1];
330                         Geps             = eps*VFtab[nnn+2];
331                         Heps2            = eps2*VFtab[nnn+3];
332                         Fp               = F+Geps+Heps2;
333                         VV               = Y+eps*Fp;
334                         FF               = Fp+Geps+2.0*Heps2;
335                         velec            = qq*VV;
336                         felec            = -qq*FF*tabscale*rinv;
337                         break;
338
339                     case GMX_NBKERNEL_ELEC_GENERALIZEDBORN:
340                         /* GB */
341                         gmx_fatal(FARGS, "Death & horror! GB generic interaction not implemented.\n");
342                         break;
343
344                     case GMX_NBKERNEL_ELEC_EWALD:
345                         ewrt             = rsq*rinv*ewtabscale;
346                         ewitab           = ewrt;
347                         eweps            = ewrt-ewitab;
348                         ewitab           = 4*ewitab;
349                         felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
350                         rinvcorr         = (fr->coulomb_modifier == eintmodPOTSHIFT) ? rinv-fr->ic->sh_ewald : rinv;
351                         velec            = qq*(rinvcorr-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
352                         felec            = qq*rinv*(rinvsq-felec);
353                         break;
354
355                     default:
356                         gmx_fatal(FARGS, "Death & horror! No generic coulomb interaction for ielec=%d.\n", ielec);
357                         break;
358                 }
359                 if (fr->coulomb_modifier == eintmodPOTSWITCH)
360                 {
361                     d                = rsq*rinv-fr->rcoulomb_switch;
362                     d                = (d > 0.0) ? d : 0.0;
363                     d2               = d*d;
364                     sw               = 1.0+d2*d*(elec_swV3+d*(elec_swV4+d*elec_swV5));
365                     dsw              = d2*(elec_swF2+d*(elec_swF3+d*elec_swF4));
366                     /* Apply switch function. Note that felec=f/r since it will be multiplied
367                      * by the i-j displacement vector. This means felec'=f'/r=-(v*sw)'/r=
368                      * -(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=felec*sw-v*dsw/r
369                      */
370                     felec            = felec*sw - rinv*velec*dsw;
371                     /* Once we have used velec to update felec we can modify velec too */
372                     velec           *= sw;
373                 }
374                 if (bExactElecCutoff)
375                 {
376                     felec            = (rsq <= rcoulomb2) ? felec : 0.0;
377                     velec            = (rsq <= rcoulomb2) ? velec : 0.0;
378                 }
379                 vctot           += velec;
380             } /* End of coulomb interactions */
381
382
383             /* VdW interaction. ivdw==0 means no interaction */
384             if (ivdw != GMX_NBKERNEL_VDW_NONE)
385             {
386                 tj               = nti+nvdwparam*type[jnr];
387
388                 switch (ivdw)
389                 {
390                     case GMX_NBKERNEL_VDW_NONE:
391                         break;
392
393                     case GMX_NBKERNEL_VDW_LENNARDJONES:
394                         /* Vanilla Lennard-Jones cutoff */
395                         c6               = vdwparam[tj];
396                         c12              = vdwparam[tj+1];
397                         rinvsix          = rinvsq*rinvsq*rinvsq;
398                         vvdw_disp        = c6*rinvsix;
399                         vvdw_rep         = c12*rinvsix*rinvsix;
400                         fvdw             = (vvdw_rep-vvdw_disp)*rinvsq;
401                         if (fr->vdw_modifier == eintmodPOTSHIFT)
402                         {
403                             vvdw             = (vvdw_rep + c12*sh_repulsion)/12.0 - (vvdw_disp + c6*sh_dispersion)/6.0;
404                         }
405                         else
406                         {
407                             vvdw             = vvdw_rep/12.0-vvdw_disp/6.0;
408                         }
409                         break;
410
411                     case GMX_NBKERNEL_VDW_BUCKINGHAM:
412                         /* Buckingham */
413                         c6               = vdwparam[tj];
414                         cexp1            = vdwparam[tj+1];
415                         cexp2            = vdwparam[tj+2];
416
417                         rinvsix          = rinvsq*rinvsq*rinvsq;
418                         vvdw_disp        = c6*rinvsix;
419                         br               = cexp2*rsq*rinv;
420                         vvdw_rep         = cexp1*exp(-br);
421                         fvdw             = (br*vvdw_rep-vvdw_disp)*rinvsq;
422                         if (fr->vdw_modifier == eintmodPOTSHIFT)
423                         {
424                             vvdw             = (vvdw_rep-cexp1*exp(-cexp2*rvdw)) - (vvdw_disp + c6*sh_dispersion)/6.0;
425                         }
426                         else
427                         {
428                             vvdw             = vvdw_rep-vvdw_disp/6.0;
429                         }
430                         break;
431
432                     case GMX_NBKERNEL_VDW_CUBICSPLINETABLE:
433                         /* Tabulated VdW */
434                         c6               = vdwparam[tj];
435                         c12              = vdwparam[tj+1];
436                         Y                = VFtab[nnn+4];
437                         F                = VFtab[nnn+5];
438                         Geps             = eps*VFtab[nnn+6];
439                         Heps2            = eps2*VFtab[nnn+7];
440                         Fp               = F+Geps+Heps2;
441                         VV               = Y+eps*Fp;
442                         FF               = Fp+Geps+2.0*Heps2;
443                         vvdw_disp        = c6*VV;
444                         fijD             = c6*FF;
445                         Y                = VFtab[nnn+8];
446                         F                = VFtab[nnn+9];
447                         Geps             = eps*VFtab[nnn+10];
448                         Heps2            = eps2*VFtab[nnn+11];
449                         Fp               = F+Geps+Heps2;
450                         VV               = Y+eps*Fp;
451                         FF               = Fp+Geps+2.0*Heps2;
452                         vvdw_rep         = c12*VV;
453                         fijR             = c12*FF;
454                         fvdw             = -(fijD+fijR)*tabscale*rinv;
455                         vvdw             = vvdw_disp + vvdw_rep;
456                         break;
457
458                     default:
459                         gmx_fatal(FARGS, "Death & horror! No generic VdW interaction for ivdw=%d.\n", ivdw);
460                         break;
461                 }
462                 if (fr->vdw_modifier == eintmodPOTSWITCH)
463                 {
464                     d                = rsq*rinv-fr->rvdw_switch;
465                     d                = (d > 0.0) ? d : 0.0;
466                     d2               = d*d;
467                     sw               = 1.0+d2*d*(vdw_swV3+d*(vdw_swV4+d*vdw_swV5));
468                     dsw              = d2*(vdw_swF2+d*(vdw_swF3+d*vdw_swF4));
469                     /* See coulomb interaction for the force-switch formula */
470                     fvdw             = fvdw*sw - rinv*vvdw*dsw;
471                     vvdw            *= sw;
472                 }
473                 if (bExactVdwCutoff)
474                 {
475                     fvdw             = (rsq <= rvdw2) ? fvdw : 0.0;
476                     vvdw             = (rsq <= rvdw2) ? vvdw : 0.0;
477                 }
478                 vvdwtot         += vvdw;
479             } /* end VdW interactions */
480
481             fscal            = felec+fvdw;
482
483             if (!bCG && force_cap > 0 && (fabs(fscal) > force_cap))
484             {
485                 fscal = force_cap*fscal/fabs(fscal);
486             }
487
488             fscal           *= hybscal;
489
490             tx               = fscal*dx;
491             ty               = fscal*dy;
492             tz               = fscal*dz;
493             fix              = fix + tx;
494             fiy              = fiy + ty;
495             fiz              = fiz + tz;
496             f[j3+0]          = f[j3+0] - tx;
497             f[j3+1]          = f[j3+1] - ty;
498             f[j3+2]          = f[j3+2] - tz;
499         }
500
501         f[ii3+0]         = f[ii3+0] + fix;
502         f[ii3+1]         = f[ii3+1] + fiy;
503         f[ii3+2]         = f[ii3+2] + fiz;
504         fshift[is3]      = fshift[is3]+fix;
505         fshift[is3+1]    = fshift[is3+1]+fiy;
506         fshift[is3+2]    = fshift[is3+2]+fiz;
507         ggid             = nlist->gid[n];
508         velecgrp[ggid]  += vctot;
509         vvdwgrp[ggid]   += vvdwtot;
510     }
511     /* Estimate flops, average for generic adress kernel:
512      * 14 flops per outer iteration
513      * 54 flops per inner iteration
514      */
515     inc_nrnb(nrnb, eNR_NBKERNEL_GENERIC_ADRESS, nlist->nri*14 + nlist->jindex[n]*54);
516 }