Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_c / nb_kernel_ElecEwSw_VdwLJSw_GeomW4P1_c.c
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  * Note: this file was generated by the GROMACS c kernel generator.
37  */
38 #include "gmxpre.h"
39
40 #include "config.h"
41
42 #include <math.h>
43
44 #include "../nb_kernel.h"
45 #include "gromacs/legacyheaders/types/simple.h"
46 #include "gromacs/math/vec.h"
47 #include "gromacs/legacyheaders/nrnb.h"
48
49 /*
50  * Gromacs nonbonded kernel:   nb_kernel_ElecEwSw_VdwLJSw_GeomW4P1_VF_c
51  * Electrostatics interaction: Ewald
52  * VdW interaction:            LennardJones
53  * Geometry:                   Water4-Particle
54  * Calculate force/pot:        PotentialAndForce
55  */
56 void
57 nb_kernel_ElecEwSw_VdwLJSw_GeomW4P1_VF_c
58                     (t_nblist                    * gmx_restrict       nlist,
59                      rvec                        * gmx_restrict          xx,
60                      rvec                        * gmx_restrict          ff,
61                      t_forcerec                  * gmx_restrict          fr,
62                      t_mdatoms                   * gmx_restrict     mdatoms,
63                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
64                      t_nrnb                      * gmx_restrict        nrnb)
65 {
66     int              i_shift_offset,i_coord_offset,j_coord_offset;
67     int              j_index_start,j_index_end;
68     int              nri,inr,ggid,iidx,jidx,jnr,outeriter,inneriter;
69     real             shX,shY,shZ,tx,ty,tz,fscal,rcutoff,rcutoff2;
70     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
71     real             *shiftvec,*fshift,*x,*f;
72     int              vdwioffset0;
73     real             ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
74     int              vdwioffset1;
75     real             ix1,iy1,iz1,fix1,fiy1,fiz1,iq1,isai1;
76     int              vdwioffset2;
77     real             ix2,iy2,iz2,fix2,fiy2,fiz2,iq2,isai2;
78     int              vdwioffset3;
79     real             ix3,iy3,iz3,fix3,fiy3,fiz3,iq3,isai3;
80     int              vdwjidx0;
81     real             jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
82     real             dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00,cexp1_00,cexp2_00;
83     real             dx10,dy10,dz10,rsq10,rinv10,rinvsq10,r10,qq10,c6_10,c12_10,cexp1_10,cexp2_10;
84     real             dx20,dy20,dz20,rsq20,rinv20,rinvsq20,r20,qq20,c6_20,c12_20,cexp1_20,cexp2_20;
85     real             dx30,dy30,dz30,rsq30,rinv30,rinvsq30,r30,qq30,c6_30,c12_30,cexp1_30,cexp2_30;
86     real             velec,felec,velecsum,facel,crf,krf,krf2;
87     real             *charge;
88     int              nvdwtype;
89     real             rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,br,vvdwexp,sh_vdw_invrcut6;
90     int              *vdwtype;
91     real             *vdwparam;
92     int              ewitab;
93     real             ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace;
94     real             *ewtab;
95     real             rswitch,swV3,swV4,swV5,swF2,swF3,swF4,d,d2,sw,dsw;
96
97     x                = xx[0];
98     f                = ff[0];
99
100     nri              = nlist->nri;
101     iinr             = nlist->iinr;
102     jindex           = nlist->jindex;
103     jjnr             = nlist->jjnr;
104     shiftidx         = nlist->shift;
105     gid              = nlist->gid;
106     shiftvec         = fr->shift_vec[0];
107     fshift           = fr->fshift[0];
108     facel            = fr->epsfac;
109     charge           = mdatoms->chargeA;
110     nvdwtype         = fr->ntype;
111     vdwparam         = fr->nbfp;
112     vdwtype          = mdatoms->typeA;
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     /* Setup water-specific parameters */
120     inr              = nlist->iinr[0];
121     iq1              = facel*charge[inr+1];
122     iq2              = facel*charge[inr+2];
123     iq3              = facel*charge[inr+3];
124     vdwioffset0      = 2*nvdwtype*vdwtype[inr+0];
125
126     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
127     rcutoff          = fr->rcoulomb;
128     rcutoff2         = rcutoff*rcutoff;
129
130     rswitch          = fr->rcoulomb_switch;
131     /* Setup switch parameters */
132     d                = rcutoff-rswitch;
133     swV3             = -10.0/(d*d*d);
134     swV4             =  15.0/(d*d*d*d);
135     swV5             =  -6.0/(d*d*d*d*d);
136     swF2             = -30.0/(d*d*d);
137     swF3             =  60.0/(d*d*d*d);
138     swF4             = -30.0/(d*d*d*d*d);
139
140     outeriter        = 0;
141     inneriter        = 0;
142
143     /* Start outer loop over neighborlists */
144     for(iidx=0; iidx<nri; iidx++)
145     {
146         /* Load shift vector for this list */
147         i_shift_offset   = DIM*shiftidx[iidx];
148         shX              = shiftvec[i_shift_offset+XX];
149         shY              = shiftvec[i_shift_offset+YY];
150         shZ              = shiftvec[i_shift_offset+ZZ];
151
152         /* Load limits for loop over neighbors */
153         j_index_start    = jindex[iidx];
154         j_index_end      = jindex[iidx+1];
155
156         /* Get outer coordinate index */
157         inr              = iinr[iidx];
158         i_coord_offset   = DIM*inr;
159
160         /* Load i particle coords and add shift vector */
161         ix0              = shX + x[i_coord_offset+DIM*0+XX];
162         iy0              = shY + x[i_coord_offset+DIM*0+YY];
163         iz0              = shZ + x[i_coord_offset+DIM*0+ZZ];
164         ix1              = shX + x[i_coord_offset+DIM*1+XX];
165         iy1              = shY + x[i_coord_offset+DIM*1+YY];
166         iz1              = shZ + x[i_coord_offset+DIM*1+ZZ];
167         ix2              = shX + x[i_coord_offset+DIM*2+XX];
168         iy2              = shY + x[i_coord_offset+DIM*2+YY];
169         iz2              = shZ + x[i_coord_offset+DIM*2+ZZ];
170         ix3              = shX + x[i_coord_offset+DIM*3+XX];
171         iy3              = shY + x[i_coord_offset+DIM*3+YY];
172         iz3              = shZ + x[i_coord_offset+DIM*3+ZZ];
173
174         fix0             = 0.0;
175         fiy0             = 0.0;
176         fiz0             = 0.0;
177         fix1             = 0.0;
178         fiy1             = 0.0;
179         fiz1             = 0.0;
180         fix2             = 0.0;
181         fiy2             = 0.0;
182         fiz2             = 0.0;
183         fix3             = 0.0;
184         fiy3             = 0.0;
185         fiz3             = 0.0;
186
187         /* Reset potential sums */
188         velecsum         = 0.0;
189         vvdwsum          = 0.0;
190
191         /* Start inner kernel loop */
192         for(jidx=j_index_start; jidx<j_index_end; jidx++)
193         {
194             /* Get j neighbor index, and coordinate index */
195             jnr              = jjnr[jidx];
196             j_coord_offset   = DIM*jnr;
197
198             /* load j atom coordinates */
199             jx0              = x[j_coord_offset+DIM*0+XX];
200             jy0              = x[j_coord_offset+DIM*0+YY];
201             jz0              = x[j_coord_offset+DIM*0+ZZ];
202
203             /* Calculate displacement vector */
204             dx00             = ix0 - jx0;
205             dy00             = iy0 - jy0;
206             dz00             = iz0 - jz0;
207             dx10             = ix1 - jx0;
208             dy10             = iy1 - jy0;
209             dz10             = iz1 - jz0;
210             dx20             = ix2 - jx0;
211             dy20             = iy2 - jy0;
212             dz20             = iz2 - jz0;
213             dx30             = ix3 - jx0;
214             dy30             = iy3 - jy0;
215             dz30             = iz3 - jz0;
216
217             /* Calculate squared distance and things based on it */
218             rsq00            = dx00*dx00+dy00*dy00+dz00*dz00;
219             rsq10            = dx10*dx10+dy10*dy10+dz10*dz10;
220             rsq20            = dx20*dx20+dy20*dy20+dz20*dz20;
221             rsq30            = dx30*dx30+dy30*dy30+dz30*dz30;
222
223             rinv00           = gmx_invsqrt(rsq00);
224             rinv10           = gmx_invsqrt(rsq10);
225             rinv20           = gmx_invsqrt(rsq20);
226             rinv30           = gmx_invsqrt(rsq30);
227
228             rinvsq00         = rinv00*rinv00;
229             rinvsq10         = rinv10*rinv10;
230             rinvsq20         = rinv20*rinv20;
231             rinvsq30         = rinv30*rinv30;
232
233             /* Load parameters for j particles */
234             jq0              = charge[jnr+0];
235             vdwjidx0         = 2*vdwtype[jnr+0];
236
237             /**************************
238              * CALCULATE INTERACTIONS *
239              **************************/
240
241             if (rsq00<rcutoff2)
242             {
243
244             r00              = rsq00*rinv00;
245
246             c6_00            = vdwparam[vdwioffset0+vdwjidx0];
247             c12_00           = vdwparam[vdwioffset0+vdwjidx0+1];
248
249             /* LENNARD-JONES DISPERSION/REPULSION */
250
251             rinvsix          = rinvsq00*rinvsq00*rinvsq00;
252             vvdw6            = c6_00*rinvsix;
253             vvdw12           = c12_00*rinvsix*rinvsix;
254             vvdw             = vvdw12*(1.0/12.0) - vvdw6*(1.0/6.0);
255             fvdw             = (vvdw12-vvdw6)*rinvsq00;
256
257             d                = r00-rswitch;
258             d                = (d>0.0) ? d : 0.0;
259             d2               = d*d;
260             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
261
262             dsw              = d2*(swF2+d*(swF3+d*swF4));
263
264             /* Evaluate switch function */
265             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
266             fvdw             = fvdw*sw - rinv00*vvdw*dsw;
267             vvdw            *= sw;
268
269             /* Update potential sums from outer loop */
270             vvdwsum         += vvdw;
271
272             fscal            = fvdw;
273
274             /* Calculate temporary vectorial force */
275             tx               = fscal*dx00;
276             ty               = fscal*dy00;
277             tz               = fscal*dz00;
278
279             /* Update vectorial force */
280             fix0            += tx;
281             fiy0            += ty;
282             fiz0            += tz;
283             f[j_coord_offset+DIM*0+XX] -= tx;
284             f[j_coord_offset+DIM*0+YY] -= ty;
285             f[j_coord_offset+DIM*0+ZZ] -= tz;
286
287             }
288
289             /**************************
290              * CALCULATE INTERACTIONS *
291              **************************/
292
293             if (rsq10<rcutoff2)
294             {
295
296             r10              = rsq10*rinv10;
297
298             qq10             = iq1*jq0;
299
300             /* EWALD ELECTROSTATICS */
301
302             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
303             ewrt             = r10*ewtabscale;
304             ewitab           = ewrt;
305             eweps            = ewrt-ewitab;
306             ewitab           = 4*ewitab;
307             felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
308             velec            = qq10*(rinv10-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
309             felec            = qq10*rinv10*(rinvsq10-felec);
310
311             d                = r10-rswitch;
312             d                = (d>0.0) ? d : 0.0;
313             d2               = d*d;
314             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
315
316             dsw              = d2*(swF2+d*(swF3+d*swF4));
317
318             /* Evaluate switch function */
319             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
320             felec            = felec*sw - rinv10*velec*dsw;
321             velec           *= sw;
322
323             /* Update potential sums from outer loop */
324             velecsum        += velec;
325
326             fscal            = felec;
327
328             /* Calculate temporary vectorial force */
329             tx               = fscal*dx10;
330             ty               = fscal*dy10;
331             tz               = fscal*dz10;
332
333             /* Update vectorial force */
334             fix1            += tx;
335             fiy1            += ty;
336             fiz1            += tz;
337             f[j_coord_offset+DIM*0+XX] -= tx;
338             f[j_coord_offset+DIM*0+YY] -= ty;
339             f[j_coord_offset+DIM*0+ZZ] -= tz;
340
341             }
342
343             /**************************
344              * CALCULATE INTERACTIONS *
345              **************************/
346
347             if (rsq20<rcutoff2)
348             {
349
350             r20              = rsq20*rinv20;
351
352             qq20             = iq2*jq0;
353
354             /* EWALD ELECTROSTATICS */
355
356             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
357             ewrt             = r20*ewtabscale;
358             ewitab           = ewrt;
359             eweps            = ewrt-ewitab;
360             ewitab           = 4*ewitab;
361             felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
362             velec            = qq20*(rinv20-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
363             felec            = qq20*rinv20*(rinvsq20-felec);
364
365             d                = r20-rswitch;
366             d                = (d>0.0) ? d : 0.0;
367             d2               = d*d;
368             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
369
370             dsw              = d2*(swF2+d*(swF3+d*swF4));
371
372             /* Evaluate switch function */
373             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
374             felec            = felec*sw - rinv20*velec*dsw;
375             velec           *= sw;
376
377             /* Update potential sums from outer loop */
378             velecsum        += velec;
379
380             fscal            = felec;
381
382             /* Calculate temporary vectorial force */
383             tx               = fscal*dx20;
384             ty               = fscal*dy20;
385             tz               = fscal*dz20;
386
387             /* Update vectorial force */
388             fix2            += tx;
389             fiy2            += ty;
390             fiz2            += tz;
391             f[j_coord_offset+DIM*0+XX] -= tx;
392             f[j_coord_offset+DIM*0+YY] -= ty;
393             f[j_coord_offset+DIM*0+ZZ] -= tz;
394
395             }
396
397             /**************************
398              * CALCULATE INTERACTIONS *
399              **************************/
400
401             if (rsq30<rcutoff2)
402             {
403
404             r30              = rsq30*rinv30;
405
406             qq30             = iq3*jq0;
407
408             /* EWALD ELECTROSTATICS */
409
410             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
411             ewrt             = r30*ewtabscale;
412             ewitab           = ewrt;
413             eweps            = ewrt-ewitab;
414             ewitab           = 4*ewitab;
415             felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
416             velec            = qq30*(rinv30-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
417             felec            = qq30*rinv30*(rinvsq30-felec);
418
419             d                = r30-rswitch;
420             d                = (d>0.0) ? d : 0.0;
421             d2               = d*d;
422             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
423
424             dsw              = d2*(swF2+d*(swF3+d*swF4));
425
426             /* Evaluate switch function */
427             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
428             felec            = felec*sw - rinv30*velec*dsw;
429             velec           *= sw;
430
431             /* Update potential sums from outer loop */
432             velecsum        += velec;
433
434             fscal            = felec;
435
436             /* Calculate temporary vectorial force */
437             tx               = fscal*dx30;
438             ty               = fscal*dy30;
439             tz               = fscal*dz30;
440
441             /* Update vectorial force */
442             fix3            += tx;
443             fiy3            += ty;
444             fiz3            += tz;
445             f[j_coord_offset+DIM*0+XX] -= tx;
446             f[j_coord_offset+DIM*0+YY] -= ty;
447             f[j_coord_offset+DIM*0+ZZ] -= tz;
448
449             }
450
451             /* Inner loop uses 230 flops */
452         }
453         /* End of innermost loop */
454
455         tx = ty = tz = 0;
456         f[i_coord_offset+DIM*0+XX] += fix0;
457         f[i_coord_offset+DIM*0+YY] += fiy0;
458         f[i_coord_offset+DIM*0+ZZ] += fiz0;
459         tx                         += fix0;
460         ty                         += fiy0;
461         tz                         += fiz0;
462         f[i_coord_offset+DIM*1+XX] += fix1;
463         f[i_coord_offset+DIM*1+YY] += fiy1;
464         f[i_coord_offset+DIM*1+ZZ] += fiz1;
465         tx                         += fix1;
466         ty                         += fiy1;
467         tz                         += fiz1;
468         f[i_coord_offset+DIM*2+XX] += fix2;
469         f[i_coord_offset+DIM*2+YY] += fiy2;
470         f[i_coord_offset+DIM*2+ZZ] += fiz2;
471         tx                         += fix2;
472         ty                         += fiy2;
473         tz                         += fiz2;
474         f[i_coord_offset+DIM*3+XX] += fix3;
475         f[i_coord_offset+DIM*3+YY] += fiy3;
476         f[i_coord_offset+DIM*3+ZZ] += fiz3;
477         tx                         += fix3;
478         ty                         += fiy3;
479         tz                         += fiz3;
480         fshift[i_shift_offset+XX]  += tx;
481         fshift[i_shift_offset+YY]  += ty;
482         fshift[i_shift_offset+ZZ]  += tz;
483
484         ggid                        = gid[iidx];
485         /* Update potential energies */
486         kernel_data->energygrp_elec[ggid] += velecsum;
487         kernel_data->energygrp_vdw[ggid] += vvdwsum;
488
489         /* Increment number of inner iterations */
490         inneriter                  += j_index_end - j_index_start;
491
492         /* Outer loop uses 41 flops */
493     }
494
495     /* Increment number of outer iterations */
496     outeriter        += nri;
497
498     /* Update outer/inner flops */
499
500     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_W4_VF,outeriter*41 + inneriter*230);
501 }
502 /*
503  * Gromacs nonbonded kernel:   nb_kernel_ElecEwSw_VdwLJSw_GeomW4P1_F_c
504  * Electrostatics interaction: Ewald
505  * VdW interaction:            LennardJones
506  * Geometry:                   Water4-Particle
507  * Calculate force/pot:        Force
508  */
509 void
510 nb_kernel_ElecEwSw_VdwLJSw_GeomW4P1_F_c
511                     (t_nblist                    * gmx_restrict       nlist,
512                      rvec                        * gmx_restrict          xx,
513                      rvec                        * gmx_restrict          ff,
514                      t_forcerec                  * gmx_restrict          fr,
515                      t_mdatoms                   * gmx_restrict     mdatoms,
516                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
517                      t_nrnb                      * gmx_restrict        nrnb)
518 {
519     int              i_shift_offset,i_coord_offset,j_coord_offset;
520     int              j_index_start,j_index_end;
521     int              nri,inr,ggid,iidx,jidx,jnr,outeriter,inneriter;
522     real             shX,shY,shZ,tx,ty,tz,fscal,rcutoff,rcutoff2;
523     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
524     real             *shiftvec,*fshift,*x,*f;
525     int              vdwioffset0;
526     real             ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
527     int              vdwioffset1;
528     real             ix1,iy1,iz1,fix1,fiy1,fiz1,iq1,isai1;
529     int              vdwioffset2;
530     real             ix2,iy2,iz2,fix2,fiy2,fiz2,iq2,isai2;
531     int              vdwioffset3;
532     real             ix3,iy3,iz3,fix3,fiy3,fiz3,iq3,isai3;
533     int              vdwjidx0;
534     real             jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
535     real             dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00,cexp1_00,cexp2_00;
536     real             dx10,dy10,dz10,rsq10,rinv10,rinvsq10,r10,qq10,c6_10,c12_10,cexp1_10,cexp2_10;
537     real             dx20,dy20,dz20,rsq20,rinv20,rinvsq20,r20,qq20,c6_20,c12_20,cexp1_20,cexp2_20;
538     real             dx30,dy30,dz30,rsq30,rinv30,rinvsq30,r30,qq30,c6_30,c12_30,cexp1_30,cexp2_30;
539     real             velec,felec,velecsum,facel,crf,krf,krf2;
540     real             *charge;
541     int              nvdwtype;
542     real             rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,br,vvdwexp,sh_vdw_invrcut6;
543     int              *vdwtype;
544     real             *vdwparam;
545     int              ewitab;
546     real             ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace;
547     real             *ewtab;
548     real             rswitch,swV3,swV4,swV5,swF2,swF3,swF4,d,d2,sw,dsw;
549
550     x                = xx[0];
551     f                = ff[0];
552
553     nri              = nlist->nri;
554     iinr             = nlist->iinr;
555     jindex           = nlist->jindex;
556     jjnr             = nlist->jjnr;
557     shiftidx         = nlist->shift;
558     gid              = nlist->gid;
559     shiftvec         = fr->shift_vec[0];
560     fshift           = fr->fshift[0];
561     facel            = fr->epsfac;
562     charge           = mdatoms->chargeA;
563     nvdwtype         = fr->ntype;
564     vdwparam         = fr->nbfp;
565     vdwtype          = mdatoms->typeA;
566
567     sh_ewald         = fr->ic->sh_ewald;
568     ewtab            = fr->ic->tabq_coul_FDV0;
569     ewtabscale       = fr->ic->tabq_scale;
570     ewtabhalfspace   = 0.5/ewtabscale;
571
572     /* Setup water-specific parameters */
573     inr              = nlist->iinr[0];
574     iq1              = facel*charge[inr+1];
575     iq2              = facel*charge[inr+2];
576     iq3              = facel*charge[inr+3];
577     vdwioffset0      = 2*nvdwtype*vdwtype[inr+0];
578
579     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
580     rcutoff          = fr->rcoulomb;
581     rcutoff2         = rcutoff*rcutoff;
582
583     rswitch          = fr->rcoulomb_switch;
584     /* Setup switch parameters */
585     d                = rcutoff-rswitch;
586     swV3             = -10.0/(d*d*d);
587     swV4             =  15.0/(d*d*d*d);
588     swV5             =  -6.0/(d*d*d*d*d);
589     swF2             = -30.0/(d*d*d);
590     swF3             =  60.0/(d*d*d*d);
591     swF4             = -30.0/(d*d*d*d*d);
592
593     outeriter        = 0;
594     inneriter        = 0;
595
596     /* Start outer loop over neighborlists */
597     for(iidx=0; iidx<nri; iidx++)
598     {
599         /* Load shift vector for this list */
600         i_shift_offset   = DIM*shiftidx[iidx];
601         shX              = shiftvec[i_shift_offset+XX];
602         shY              = shiftvec[i_shift_offset+YY];
603         shZ              = shiftvec[i_shift_offset+ZZ];
604
605         /* Load limits for loop over neighbors */
606         j_index_start    = jindex[iidx];
607         j_index_end      = jindex[iidx+1];
608
609         /* Get outer coordinate index */
610         inr              = iinr[iidx];
611         i_coord_offset   = DIM*inr;
612
613         /* Load i particle coords and add shift vector */
614         ix0              = shX + x[i_coord_offset+DIM*0+XX];
615         iy0              = shY + x[i_coord_offset+DIM*0+YY];
616         iz0              = shZ + x[i_coord_offset+DIM*0+ZZ];
617         ix1              = shX + x[i_coord_offset+DIM*1+XX];
618         iy1              = shY + x[i_coord_offset+DIM*1+YY];
619         iz1              = shZ + x[i_coord_offset+DIM*1+ZZ];
620         ix2              = shX + x[i_coord_offset+DIM*2+XX];
621         iy2              = shY + x[i_coord_offset+DIM*2+YY];
622         iz2              = shZ + x[i_coord_offset+DIM*2+ZZ];
623         ix3              = shX + x[i_coord_offset+DIM*3+XX];
624         iy3              = shY + x[i_coord_offset+DIM*3+YY];
625         iz3              = shZ + x[i_coord_offset+DIM*3+ZZ];
626
627         fix0             = 0.0;
628         fiy0             = 0.0;
629         fiz0             = 0.0;
630         fix1             = 0.0;
631         fiy1             = 0.0;
632         fiz1             = 0.0;
633         fix2             = 0.0;
634         fiy2             = 0.0;
635         fiz2             = 0.0;
636         fix3             = 0.0;
637         fiy3             = 0.0;
638         fiz3             = 0.0;
639
640         /* Start inner kernel loop */
641         for(jidx=j_index_start; jidx<j_index_end; jidx++)
642         {
643             /* Get j neighbor index, and coordinate index */
644             jnr              = jjnr[jidx];
645             j_coord_offset   = DIM*jnr;
646
647             /* load j atom coordinates */
648             jx0              = x[j_coord_offset+DIM*0+XX];
649             jy0              = x[j_coord_offset+DIM*0+YY];
650             jz0              = x[j_coord_offset+DIM*0+ZZ];
651
652             /* Calculate displacement vector */
653             dx00             = ix0 - jx0;
654             dy00             = iy0 - jy0;
655             dz00             = iz0 - jz0;
656             dx10             = ix1 - jx0;
657             dy10             = iy1 - jy0;
658             dz10             = iz1 - jz0;
659             dx20             = ix2 - jx0;
660             dy20             = iy2 - jy0;
661             dz20             = iz2 - jz0;
662             dx30             = ix3 - jx0;
663             dy30             = iy3 - jy0;
664             dz30             = iz3 - jz0;
665
666             /* Calculate squared distance and things based on it */
667             rsq00            = dx00*dx00+dy00*dy00+dz00*dz00;
668             rsq10            = dx10*dx10+dy10*dy10+dz10*dz10;
669             rsq20            = dx20*dx20+dy20*dy20+dz20*dz20;
670             rsq30            = dx30*dx30+dy30*dy30+dz30*dz30;
671
672             rinv00           = gmx_invsqrt(rsq00);
673             rinv10           = gmx_invsqrt(rsq10);
674             rinv20           = gmx_invsqrt(rsq20);
675             rinv30           = gmx_invsqrt(rsq30);
676
677             rinvsq00         = rinv00*rinv00;
678             rinvsq10         = rinv10*rinv10;
679             rinvsq20         = rinv20*rinv20;
680             rinvsq30         = rinv30*rinv30;
681
682             /* Load parameters for j particles */
683             jq0              = charge[jnr+0];
684             vdwjidx0         = 2*vdwtype[jnr+0];
685
686             /**************************
687              * CALCULATE INTERACTIONS *
688              **************************/
689
690             if (rsq00<rcutoff2)
691             {
692
693             r00              = rsq00*rinv00;
694
695             c6_00            = vdwparam[vdwioffset0+vdwjidx0];
696             c12_00           = vdwparam[vdwioffset0+vdwjidx0+1];
697
698             /* LENNARD-JONES DISPERSION/REPULSION */
699
700             rinvsix          = rinvsq00*rinvsq00*rinvsq00;
701             vvdw6            = c6_00*rinvsix;
702             vvdw12           = c12_00*rinvsix*rinvsix;
703             vvdw             = vvdw12*(1.0/12.0) - vvdw6*(1.0/6.0);
704             fvdw             = (vvdw12-vvdw6)*rinvsq00;
705
706             d                = r00-rswitch;
707             d                = (d>0.0) ? d : 0.0;
708             d2               = d*d;
709             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
710
711             dsw              = d2*(swF2+d*(swF3+d*swF4));
712
713             /* Evaluate switch function */
714             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
715             fvdw             = fvdw*sw - rinv00*vvdw*dsw;
716
717             fscal            = fvdw;
718
719             /* Calculate temporary vectorial force */
720             tx               = fscal*dx00;
721             ty               = fscal*dy00;
722             tz               = fscal*dz00;
723
724             /* Update vectorial force */
725             fix0            += tx;
726             fiy0            += ty;
727             fiz0            += tz;
728             f[j_coord_offset+DIM*0+XX] -= tx;
729             f[j_coord_offset+DIM*0+YY] -= ty;
730             f[j_coord_offset+DIM*0+ZZ] -= tz;
731
732             }
733
734             /**************************
735              * CALCULATE INTERACTIONS *
736              **************************/
737
738             if (rsq10<rcutoff2)
739             {
740
741             r10              = rsq10*rinv10;
742
743             qq10             = iq1*jq0;
744
745             /* EWALD ELECTROSTATICS */
746
747             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
748             ewrt             = r10*ewtabscale;
749             ewitab           = ewrt;
750             eweps            = ewrt-ewitab;
751             ewitab           = 4*ewitab;
752             felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
753             velec            = qq10*(rinv10-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
754             felec            = qq10*rinv10*(rinvsq10-felec);
755
756             d                = r10-rswitch;
757             d                = (d>0.0) ? d : 0.0;
758             d2               = d*d;
759             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
760
761             dsw              = d2*(swF2+d*(swF3+d*swF4));
762
763             /* Evaluate switch function */
764             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
765             felec            = felec*sw - rinv10*velec*dsw;
766
767             fscal            = felec;
768
769             /* Calculate temporary vectorial force */
770             tx               = fscal*dx10;
771             ty               = fscal*dy10;
772             tz               = fscal*dz10;
773
774             /* Update vectorial force */
775             fix1            += tx;
776             fiy1            += ty;
777             fiz1            += tz;
778             f[j_coord_offset+DIM*0+XX] -= tx;
779             f[j_coord_offset+DIM*0+YY] -= ty;
780             f[j_coord_offset+DIM*0+ZZ] -= tz;
781
782             }
783
784             /**************************
785              * CALCULATE INTERACTIONS *
786              **************************/
787
788             if (rsq20<rcutoff2)
789             {
790
791             r20              = rsq20*rinv20;
792
793             qq20             = iq2*jq0;
794
795             /* EWALD ELECTROSTATICS */
796
797             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
798             ewrt             = r20*ewtabscale;
799             ewitab           = ewrt;
800             eweps            = ewrt-ewitab;
801             ewitab           = 4*ewitab;
802             felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
803             velec            = qq20*(rinv20-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
804             felec            = qq20*rinv20*(rinvsq20-felec);
805
806             d                = r20-rswitch;
807             d                = (d>0.0) ? d : 0.0;
808             d2               = d*d;
809             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
810
811             dsw              = d2*(swF2+d*(swF3+d*swF4));
812
813             /* Evaluate switch function */
814             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
815             felec            = felec*sw - rinv20*velec*dsw;
816
817             fscal            = felec;
818
819             /* Calculate temporary vectorial force */
820             tx               = fscal*dx20;
821             ty               = fscal*dy20;
822             tz               = fscal*dz20;
823
824             /* Update vectorial force */
825             fix2            += tx;
826             fiy2            += ty;
827             fiz2            += tz;
828             f[j_coord_offset+DIM*0+XX] -= tx;
829             f[j_coord_offset+DIM*0+YY] -= ty;
830             f[j_coord_offset+DIM*0+ZZ] -= tz;
831
832             }
833
834             /**************************
835              * CALCULATE INTERACTIONS *
836              **************************/
837
838             if (rsq30<rcutoff2)
839             {
840
841             r30              = rsq30*rinv30;
842
843             qq30             = iq3*jq0;
844
845             /* EWALD ELECTROSTATICS */
846
847             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
848             ewrt             = r30*ewtabscale;
849             ewitab           = ewrt;
850             eweps            = ewrt-ewitab;
851             ewitab           = 4*ewitab;
852             felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
853             velec            = qq30*(rinv30-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
854             felec            = qq30*rinv30*(rinvsq30-felec);
855
856             d                = r30-rswitch;
857             d                = (d>0.0) ? d : 0.0;
858             d2               = d*d;
859             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
860
861             dsw              = d2*(swF2+d*(swF3+d*swF4));
862
863             /* Evaluate switch function */
864             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
865             felec            = felec*sw - rinv30*velec*dsw;
866
867             fscal            = felec;
868
869             /* Calculate temporary vectorial force */
870             tx               = fscal*dx30;
871             ty               = fscal*dy30;
872             tz               = fscal*dz30;
873
874             /* Update vectorial force */
875             fix3            += tx;
876             fiy3            += ty;
877             fiz3            += tz;
878             f[j_coord_offset+DIM*0+XX] -= tx;
879             f[j_coord_offset+DIM*0+YY] -= ty;
880             f[j_coord_offset+DIM*0+ZZ] -= tz;
881
882             }
883
884             /* Inner loop uses 222 flops */
885         }
886         /* End of innermost loop */
887
888         tx = ty = tz = 0;
889         f[i_coord_offset+DIM*0+XX] += fix0;
890         f[i_coord_offset+DIM*0+YY] += fiy0;
891         f[i_coord_offset+DIM*0+ZZ] += fiz0;
892         tx                         += fix0;
893         ty                         += fiy0;
894         tz                         += fiz0;
895         f[i_coord_offset+DIM*1+XX] += fix1;
896         f[i_coord_offset+DIM*1+YY] += fiy1;
897         f[i_coord_offset+DIM*1+ZZ] += fiz1;
898         tx                         += fix1;
899         ty                         += fiy1;
900         tz                         += fiz1;
901         f[i_coord_offset+DIM*2+XX] += fix2;
902         f[i_coord_offset+DIM*2+YY] += fiy2;
903         f[i_coord_offset+DIM*2+ZZ] += fiz2;
904         tx                         += fix2;
905         ty                         += fiy2;
906         tz                         += fiz2;
907         f[i_coord_offset+DIM*3+XX] += fix3;
908         f[i_coord_offset+DIM*3+YY] += fiy3;
909         f[i_coord_offset+DIM*3+ZZ] += fiz3;
910         tx                         += fix3;
911         ty                         += fiy3;
912         tz                         += fiz3;
913         fshift[i_shift_offset+XX]  += tx;
914         fshift[i_shift_offset+YY]  += ty;
915         fshift[i_shift_offset+ZZ]  += tz;
916
917         /* Increment number of inner iterations */
918         inneriter                  += j_index_end - j_index_start;
919
920         /* Outer loop uses 39 flops */
921     }
922
923     /* Increment number of outer iterations */
924     outeriter        += nri;
925
926     /* Update outer/inner flops */
927
928     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_W4_F,outeriter*39 + inneriter*222);
929 }