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