6df5731349e93e794ab6427a5a7b500f0a3dbfdb
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_c / nb_kernel_ElecEwSw_VdwBhamSw_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_VdwBhamSw_GeomW4P1_VF_c
49  * Electrostatics interaction: Ewald
50  * VdW interaction:            Buckingham
51  * Geometry:                   Water4-Particle
52  * Calculate force/pot:        PotentialAndForce
53  */
54 void
55 nb_kernel_ElecEwSw_VdwBhamSw_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      = 3*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         = 3*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             cexp1_00         = vdwparam[vdwioffset0+vdwjidx0+1];
246             cexp2_00         = vdwparam[vdwioffset0+vdwjidx0+2];
247
248             /* BUCKINGHAM DISPERSION/REPULSION */
249             rinvsix          = rinvsq00*rinvsq00*rinvsq00;
250             vvdw6            = c6_00*rinvsix;
251             br               = cexp2_00*r00;
252             vvdwexp          = cexp1_00*exp(-br);
253             vvdw             = vvdwexp - vvdw6*(1.0/6.0);
254             fvdw             = (br*vvdwexp-vvdw6)*rinvsq00;
255
256             d                = r00-rswitch;
257             d                = (d>0.0) ? d : 0.0;
258             d2               = d*d;
259             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
260
261             dsw              = d2*(swF2+d*(swF3+d*swF4));
262
263             /* Evaluate switch function */
264             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
265             fvdw             = fvdw*sw - rinv00*vvdw*dsw;
266             vvdw            *= sw;
267
268             /* Update potential sums from outer loop */
269             vvdwsum         += vvdw;
270
271             fscal            = fvdw;
272
273             /* Calculate temporary vectorial force */
274             tx               = fscal*dx00;
275             ty               = fscal*dy00;
276             tz               = fscal*dz00;
277
278             /* Update vectorial force */
279             fix0            += tx;
280             fiy0            += ty;
281             fiz0            += tz;
282             f[j_coord_offset+DIM*0+XX] -= tx;
283             f[j_coord_offset+DIM*0+YY] -= ty;
284             f[j_coord_offset+DIM*0+ZZ] -= tz;
285
286             }
287
288             /**************************
289              * CALCULATE INTERACTIONS *
290              **************************/
291
292             if (rsq10<rcutoff2)
293             {
294
295             r10              = rsq10*rinv10;
296
297             qq10             = iq1*jq0;
298
299             /* EWALD ELECTROSTATICS */
300
301             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
302             ewrt             = r10*ewtabscale;
303             ewitab           = ewrt;
304             eweps            = ewrt-ewitab;
305             ewitab           = 4*ewitab;
306             felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
307             velec            = qq10*(rinv10-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
308             felec            = qq10*rinv10*(rinvsq10-felec);
309
310             d                = r10-rswitch;
311             d                = (d>0.0) ? d : 0.0;
312             d2               = d*d;
313             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
314
315             dsw              = d2*(swF2+d*(swF3+d*swF4));
316
317             /* Evaluate switch function */
318             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
319             felec            = felec*sw - rinv10*velec*dsw;
320             velec           *= sw;
321
322             /* Update potential sums from outer loop */
323             velecsum        += velec;
324
325             fscal            = felec;
326
327             /* Calculate temporary vectorial force */
328             tx               = fscal*dx10;
329             ty               = fscal*dy10;
330             tz               = fscal*dz10;
331
332             /* Update vectorial force */
333             fix1            += tx;
334             fiy1            += ty;
335             fiz1            += tz;
336             f[j_coord_offset+DIM*0+XX] -= tx;
337             f[j_coord_offset+DIM*0+YY] -= ty;
338             f[j_coord_offset+DIM*0+ZZ] -= tz;
339
340             }
341
342             /**************************
343              * CALCULATE INTERACTIONS *
344              **************************/
345
346             if (rsq20<rcutoff2)
347             {
348
349             r20              = rsq20*rinv20;
350
351             qq20             = iq2*jq0;
352
353             /* EWALD ELECTROSTATICS */
354
355             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
356             ewrt             = r20*ewtabscale;
357             ewitab           = ewrt;
358             eweps            = ewrt-ewitab;
359             ewitab           = 4*ewitab;
360             felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
361             velec            = qq20*(rinv20-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
362             felec            = qq20*rinv20*(rinvsq20-felec);
363
364             d                = r20-rswitch;
365             d                = (d>0.0) ? d : 0.0;
366             d2               = d*d;
367             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
368
369             dsw              = d2*(swF2+d*(swF3+d*swF4));
370
371             /* Evaluate switch function */
372             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
373             felec            = felec*sw - rinv20*velec*dsw;
374             velec           *= sw;
375
376             /* Update potential sums from outer loop */
377             velecsum        += velec;
378
379             fscal            = felec;
380
381             /* Calculate temporary vectorial force */
382             tx               = fscal*dx20;
383             ty               = fscal*dy20;
384             tz               = fscal*dz20;
385
386             /* Update vectorial force */
387             fix2            += tx;
388             fiy2            += ty;
389             fiz2            += tz;
390             f[j_coord_offset+DIM*0+XX] -= tx;
391             f[j_coord_offset+DIM*0+YY] -= ty;
392             f[j_coord_offset+DIM*0+ZZ] -= tz;
393
394             }
395
396             /**************************
397              * CALCULATE INTERACTIONS *
398              **************************/
399
400             if (rsq30<rcutoff2)
401             {
402
403             r30              = rsq30*rinv30;
404
405             qq30             = iq3*jq0;
406
407             /* EWALD ELECTROSTATICS */
408
409             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
410             ewrt             = r30*ewtabscale;
411             ewitab           = ewrt;
412             eweps            = ewrt-ewitab;
413             ewitab           = 4*ewitab;
414             felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
415             velec            = qq30*(rinv30-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
416             felec            = qq30*rinv30*(rinvsq30-felec);
417
418             d                = r30-rswitch;
419             d                = (d>0.0) ? d : 0.0;
420             d2               = d*d;
421             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
422
423             dsw              = d2*(swF2+d*(swF3+d*swF4));
424
425             /* Evaluate switch function */
426             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
427             felec            = felec*sw - rinv30*velec*dsw;
428             velec           *= sw;
429
430             /* Update potential sums from outer loop */
431             velecsum        += velec;
432
433             fscal            = felec;
434
435             /* Calculate temporary vectorial force */
436             tx               = fscal*dx30;
437             ty               = fscal*dy30;
438             tz               = fscal*dz30;
439
440             /* Update vectorial force */
441             fix3            += tx;
442             fiy3            += ty;
443             fiz3            += tz;
444             f[j_coord_offset+DIM*0+XX] -= tx;
445             f[j_coord_offset+DIM*0+YY] -= ty;
446             f[j_coord_offset+DIM*0+ZZ] -= tz;
447
448             }
449
450             /* Inner loop uses 256 flops */
451         }
452         /* End of innermost loop */
453
454         tx = ty = tz = 0;
455         f[i_coord_offset+DIM*0+XX] += fix0;
456         f[i_coord_offset+DIM*0+YY] += fiy0;
457         f[i_coord_offset+DIM*0+ZZ] += fiz0;
458         tx                         += fix0;
459         ty                         += fiy0;
460         tz                         += fiz0;
461         f[i_coord_offset+DIM*1+XX] += fix1;
462         f[i_coord_offset+DIM*1+YY] += fiy1;
463         f[i_coord_offset+DIM*1+ZZ] += fiz1;
464         tx                         += fix1;
465         ty                         += fiy1;
466         tz                         += fiz1;
467         f[i_coord_offset+DIM*2+XX] += fix2;
468         f[i_coord_offset+DIM*2+YY] += fiy2;
469         f[i_coord_offset+DIM*2+ZZ] += fiz2;
470         tx                         += fix2;
471         ty                         += fiy2;
472         tz                         += fiz2;
473         f[i_coord_offset+DIM*3+XX] += fix3;
474         f[i_coord_offset+DIM*3+YY] += fiy3;
475         f[i_coord_offset+DIM*3+ZZ] += fiz3;
476         tx                         += fix3;
477         ty                         += fiy3;
478         tz                         += fiz3;
479         fshift[i_shift_offset+XX]  += tx;
480         fshift[i_shift_offset+YY]  += ty;
481         fshift[i_shift_offset+ZZ]  += tz;
482
483         ggid                        = gid[iidx];
484         /* Update potential energies */
485         kernel_data->energygrp_elec[ggid] += velecsum;
486         kernel_data->energygrp_vdw[ggid] += vvdwsum;
487
488         /* Increment number of inner iterations */
489         inneriter                  += j_index_end - j_index_start;
490
491         /* Outer loop uses 41 flops */
492     }
493
494     /* Increment number of outer iterations */
495     outeriter        += nri;
496
497     /* Update outer/inner flops */
498
499     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_W4_VF,outeriter*41 + inneriter*256);
500 }
501 /*
502  * Gromacs nonbonded kernel:   nb_kernel_ElecEwSw_VdwBhamSw_GeomW4P1_F_c
503  * Electrostatics interaction: Ewald
504  * VdW interaction:            Buckingham
505  * Geometry:                   Water4-Particle
506  * Calculate force/pot:        Force
507  */
508 void
509 nb_kernel_ElecEwSw_VdwBhamSw_GeomW4P1_F_c
510                     (t_nblist                    * gmx_restrict       nlist,
511                      rvec                        * gmx_restrict          xx,
512                      rvec                        * gmx_restrict          ff,
513                      t_forcerec                  * gmx_restrict          fr,
514                      t_mdatoms                   * gmx_restrict     mdatoms,
515                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
516                      t_nrnb                      * gmx_restrict        nrnb)
517 {
518     int              i_shift_offset,i_coord_offset,j_coord_offset;
519     int              j_index_start,j_index_end;
520     int              nri,inr,ggid,iidx,jidx,jnr,outeriter,inneriter;
521     real             shX,shY,shZ,tx,ty,tz,fscal,rcutoff,rcutoff2;
522     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
523     real             *shiftvec,*fshift,*x,*f;
524     int              vdwioffset0;
525     real             ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
526     int              vdwioffset1;
527     real             ix1,iy1,iz1,fix1,fiy1,fiz1,iq1,isai1;
528     int              vdwioffset2;
529     real             ix2,iy2,iz2,fix2,fiy2,fiz2,iq2,isai2;
530     int              vdwioffset3;
531     real             ix3,iy3,iz3,fix3,fiy3,fiz3,iq3,isai3;
532     int              vdwjidx0;
533     real             jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
534     real             dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00,cexp1_00,cexp2_00;
535     real             dx10,dy10,dz10,rsq10,rinv10,rinvsq10,r10,qq10,c6_10,c12_10,cexp1_10,cexp2_10;
536     real             dx20,dy20,dz20,rsq20,rinv20,rinvsq20,r20,qq20,c6_20,c12_20,cexp1_20,cexp2_20;
537     real             dx30,dy30,dz30,rsq30,rinv30,rinvsq30,r30,qq30,c6_30,c12_30,cexp1_30,cexp2_30;
538     real             velec,felec,velecsum,facel,crf,krf,krf2;
539     real             *charge;
540     int              nvdwtype;
541     real             rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,br,vvdwexp,sh_vdw_invrcut6;
542     int              *vdwtype;
543     real             *vdwparam;
544     int              ewitab;
545     real             ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace;
546     real             *ewtab;
547     real             rswitch,swV3,swV4,swV5,swF2,swF3,swF4,d,d2,sw,dsw;
548
549     x                = xx[0];
550     f                = ff[0];
551
552     nri              = nlist->nri;
553     iinr             = nlist->iinr;
554     jindex           = nlist->jindex;
555     jjnr             = nlist->jjnr;
556     shiftidx         = nlist->shift;
557     gid              = nlist->gid;
558     shiftvec         = fr->shift_vec[0];
559     fshift           = fr->fshift[0];
560     facel            = fr->epsfac;
561     charge           = mdatoms->chargeA;
562     nvdwtype         = fr->ntype;
563     vdwparam         = fr->nbfp;
564     vdwtype          = mdatoms->typeA;
565
566     sh_ewald         = fr->ic->sh_ewald;
567     ewtab            = fr->ic->tabq_coul_FDV0;
568     ewtabscale       = fr->ic->tabq_scale;
569     ewtabhalfspace   = 0.5/ewtabscale;
570
571     /* Setup water-specific parameters */
572     inr              = nlist->iinr[0];
573     iq1              = facel*charge[inr+1];
574     iq2              = facel*charge[inr+2];
575     iq3              = facel*charge[inr+3];
576     vdwioffset0      = 3*nvdwtype*vdwtype[inr+0];
577
578     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
579     rcutoff          = fr->rcoulomb;
580     rcutoff2         = rcutoff*rcutoff;
581
582     rswitch          = fr->rcoulomb_switch;
583     /* Setup switch parameters */
584     d                = rcutoff-rswitch;
585     swV3             = -10.0/(d*d*d);
586     swV4             =  15.0/(d*d*d*d);
587     swV5             =  -6.0/(d*d*d*d*d);
588     swF2             = -30.0/(d*d*d);
589     swF3             =  60.0/(d*d*d*d);
590     swF4             = -30.0/(d*d*d*d*d);
591
592     outeriter        = 0;
593     inneriter        = 0;
594
595     /* Start outer loop over neighborlists */
596     for(iidx=0; iidx<nri; iidx++)
597     {
598         /* Load shift vector for this list */
599         i_shift_offset   = DIM*shiftidx[iidx];
600         shX              = shiftvec[i_shift_offset+XX];
601         shY              = shiftvec[i_shift_offset+YY];
602         shZ              = shiftvec[i_shift_offset+ZZ];
603
604         /* Load limits for loop over neighbors */
605         j_index_start    = jindex[iidx];
606         j_index_end      = jindex[iidx+1];
607
608         /* Get outer coordinate index */
609         inr              = iinr[iidx];
610         i_coord_offset   = DIM*inr;
611
612         /* Load i particle coords and add shift vector */
613         ix0              = shX + x[i_coord_offset+DIM*0+XX];
614         iy0              = shY + x[i_coord_offset+DIM*0+YY];
615         iz0              = shZ + x[i_coord_offset+DIM*0+ZZ];
616         ix1              = shX + x[i_coord_offset+DIM*1+XX];
617         iy1              = shY + x[i_coord_offset+DIM*1+YY];
618         iz1              = shZ + x[i_coord_offset+DIM*1+ZZ];
619         ix2              = shX + x[i_coord_offset+DIM*2+XX];
620         iy2              = shY + x[i_coord_offset+DIM*2+YY];
621         iz2              = shZ + x[i_coord_offset+DIM*2+ZZ];
622         ix3              = shX + x[i_coord_offset+DIM*3+XX];
623         iy3              = shY + x[i_coord_offset+DIM*3+YY];
624         iz3              = shZ + x[i_coord_offset+DIM*3+ZZ];
625
626         fix0             = 0.0;
627         fiy0             = 0.0;
628         fiz0             = 0.0;
629         fix1             = 0.0;
630         fiy1             = 0.0;
631         fiz1             = 0.0;
632         fix2             = 0.0;
633         fiy2             = 0.0;
634         fiz2             = 0.0;
635         fix3             = 0.0;
636         fiy3             = 0.0;
637         fiz3             = 0.0;
638
639         /* Start inner kernel loop */
640         for(jidx=j_index_start; jidx<j_index_end; jidx++)
641         {
642             /* Get j neighbor index, and coordinate index */
643             jnr              = jjnr[jidx];
644             j_coord_offset   = DIM*jnr;
645
646             /* load j atom coordinates */
647             jx0              = x[j_coord_offset+DIM*0+XX];
648             jy0              = x[j_coord_offset+DIM*0+YY];
649             jz0              = x[j_coord_offset+DIM*0+ZZ];
650
651             /* Calculate displacement vector */
652             dx00             = ix0 - jx0;
653             dy00             = iy0 - jy0;
654             dz00             = iz0 - jz0;
655             dx10             = ix1 - jx0;
656             dy10             = iy1 - jy0;
657             dz10             = iz1 - jz0;
658             dx20             = ix2 - jx0;
659             dy20             = iy2 - jy0;
660             dz20             = iz2 - jz0;
661             dx30             = ix3 - jx0;
662             dy30             = iy3 - jy0;
663             dz30             = iz3 - jz0;
664
665             /* Calculate squared distance and things based on it */
666             rsq00            = dx00*dx00+dy00*dy00+dz00*dz00;
667             rsq10            = dx10*dx10+dy10*dy10+dz10*dz10;
668             rsq20            = dx20*dx20+dy20*dy20+dz20*dz20;
669             rsq30            = dx30*dx30+dy30*dy30+dz30*dz30;
670
671             rinv00           = gmx_invsqrt(rsq00);
672             rinv10           = gmx_invsqrt(rsq10);
673             rinv20           = gmx_invsqrt(rsq20);
674             rinv30           = gmx_invsqrt(rsq30);
675
676             rinvsq00         = rinv00*rinv00;
677             rinvsq10         = rinv10*rinv10;
678             rinvsq20         = rinv20*rinv20;
679             rinvsq30         = rinv30*rinv30;
680
681             /* Load parameters for j particles */
682             jq0              = charge[jnr+0];
683             vdwjidx0         = 3*vdwtype[jnr+0];
684
685             /**************************
686              * CALCULATE INTERACTIONS *
687              **************************/
688
689             if (rsq00<rcutoff2)
690             {
691
692             r00              = rsq00*rinv00;
693
694             c6_00            = vdwparam[vdwioffset0+vdwjidx0];
695             cexp1_00         = vdwparam[vdwioffset0+vdwjidx0+1];
696             cexp2_00         = vdwparam[vdwioffset0+vdwjidx0+2];
697
698             /* BUCKINGHAM DISPERSION/REPULSION */
699             rinvsix          = rinvsq00*rinvsq00*rinvsq00;
700             vvdw6            = c6_00*rinvsix;
701             br               = cexp2_00*r00;
702             vvdwexp          = cexp1_00*exp(-br);
703             vvdw             = vvdwexp - vvdw6*(1.0/6.0);
704             fvdw             = (br*vvdwexp-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 248 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*248);
929 }