Fix component for libcudart
[alexxy/gromacs.git] / src / gmxlib / nonbonded / nb_kernel_c / nb_kernel_template_c.pre
1 /* #if 0 */
2 #error This file must be processed with the Gromacs pre-preprocessor
3 /* #endif */
4 /* #if INCLUDE_HEADER */
5 #ifdef HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <math.h>
10
11 #include "../nb_kernel.h"
12 #include "types/simple.h"
13 #include "vec.h"
14 #include "nrnb.h"
15 /* #endif */
16
17 /* ## List of variables set by the generating script:                                    */
18 /* ##                                                                                    */
19 /* ## Setttings that apply to the entire kernel:                                         */
20 /* ## KERNEL_ELEC:           String, choice for electrostatic interactions               */
21 /* ## KERNEL_VDW:            String, choice for van der Waals interactions               */
22 /* ## KERNEL_NAME:           String, name of this kernel                                 */
23 /* ## KERNEL_VF:             String telling if we calculate potential, force, or both    */
24 /* ## GEOMETRY_I/GEOMETRY_J: String, name of each geometry, e.g. 'Water3' or '1Particle' */
25 /* ##                                                                                    */
26 /* ## Setttings that apply to particles in the outer (I) or inner (J) loops:             */
27 /* ## PARTICLES_I[]/         Arrays with lists of i/j particles to use in kernel. It is  */
28 /* ## PARTICLES_J[]:         just [0] for particle geometry, but can be longer for water */
29 /* ## PARTICLES_ELEC_I[]/    Arrays with lists of i/j particle that have electrostatics  */
30 /* ## PARTICLES_ELEC_J[]:    interactions that should be calculated in this kernel.      */
31 /* ## PARTICLES_VDW_I[]/     Arrays with the list of i/j particle that have VdW          */
32 /* ## PARTICLES_VDW_J[]:     interactions that should be calculated in this kernel.      */
33 /* ##                                                                                    */
34 /* ## Setttings for pairs of interactions (e.g. 2nd i particle against 1st j particle)   */
35 /* ## PAIRS_IJ[]:            Array with (i,j) tuples of pairs for which interactions     */
36 /* ##                        should be calculated in this kernel. Zero-charge particles  */
37 /* ##                        do not have interactions with particles without vdw, and    */
38 /* ##                        Vdw-only interactions are not evaluated in a no-vdw-kernel. */
39 /* ## INTERACTION_FLAGS[][]: 2D matrix, dimension e.g. 3*3 for water-water interactions. */
40 /* ##                        For each i-j pair, the element [I][J] is a list of strings  */
41 /* ##                        defining properties/flags of this interaction. Examples     */
42 /* ##                        include 'electrostatics'/'vdw' if that type of interaction  */
43 /* ##                        should be evaluated, 'rsq'/'rinv'/'rinvsq' if those values  */
44 /* ##                        are needed, and 'exactcutoff' or 'shift','switch' to        */
45 /* ##                        decide if the force/potential should be modified. This way  */
46 /* ##                        we only calculate values absolutely needed for each case.   */
47
48 /* ## Calculate the size and offset for (merged/interleaved) table data */
49
50 /* #if ('CubicSplineTable' in [KERNEL_ELEC,KERNEL_VDW]) or KERNEL_VF=='PotentialAndForce' */
51 /*     #define TABLE_POINT_SIZE 4 */
52 /* #else */
53 /*     #define TABLE_POINT_SIZE 2 */
54 /* #endif */
55
56 /* #if 'Table' in KERNEL_ELEC and 'Table' in KERNEL_VDW */
57 /*     #define TABLE_NINTERACTIONS 3 */
58 /*     #define TABLE_VDW_OFFSET TABLE_POINT_SIZE */
59 /* #elif 'Table' in KERNEL_ELEC */
60 /*     #define TABLE_NINTERACTIONS 1 */
61 /* #elif 'Table' in KERNEL_VDW */
62 /*     #define TABLE_NINTERACTIONS 2 */
63 /*     #define TABLE_VDW_OFFSET 0 */
64 /* #else */
65 /*     #define TABLE_NINTERACTIONS 0 */
66 /* #endif */
67
68 /* #if 'Buckingham' in KERNEL_VDW */
69 /*   #define NVDWPARAM 3 */
70 /* #else */
71 /*   #define NVDWPARAM 2 */
72 /* #endif */
73
74 /*
75  * Gromacs nonbonded kernel:   {KERNEL_NAME}
76  * Electrostatics interaction: {KERNEL_ELEC}
77  * VdW interaction:            {KERNEL_VDW}
78  * Geometry:                   {GEOMETRY_I}-{GEOMETRY_J}
79  * Calculate force/pot:        {KERNEL_VF}
80  */
81 void
82 {KERNEL_NAME}
83                     (t_nblist * gmx_restrict                nlist,
84                      rvec * gmx_restrict                    xx,
85                      rvec * gmx_restrict                    ff,
86                      t_forcerec * gmx_restrict              fr,
87                      t_mdatoms * gmx_restrict               mdatoms,
88                      nb_kernel_data_t * gmx_restrict        kernel_data,
89                      t_nrnb * gmx_restrict                  nrnb)
90 {
91     /* ## Not all variables are used for all kernels, but any optimizing compiler fixes that, */
92     /* ## so there is no point in going to extremes to exclude variables that are not needed. */
93     int              i_shift_offset,i_coord_offset,j_coord_offset;
94     int              j_index_start,j_index_end;
95     int              nri,inr,ggid,iidx,jidx,jnr,outeriter,inneriter;
96     real             shX,shY,shZ,tx,ty,tz,fscal,rcutoff,rcutoff2;
97     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
98     real             *shiftvec,*fshift,*x,*f;
99     /* #for I in PARTICLES_I */
100     int              vdwioffset{I};
101     real             ix{I},iy{I},iz{I},fix{I},fiy{I},fiz{I},iq{I},isai{I};
102     /* #endfor */
103     /* #for J in PARTICLES_J */
104     int              vdwjidx{J};
105     real             jx{J},jy{J},jz{J},fjx{J},fjy{J},fjz{J},jq{J},isaj{J};
106     /* #endfor */
107     /* #for I,J in PAIRS_IJ */
108     real             dx{I}{J},dy{I}{J},dz{I}{J},rsq{I}{J},rinv{I}{J},rinvsq{I}{J},r{I}{J},qq{I}{J},c6_{I}{J},c12_{I}{J},cexp1_{I}{J},cexp2_{I}{J};
109     /* #endfor */
110     /* #if KERNEL_ELEC != 'None' */
111     real             velec,felec,velecsum,facel,crf,krf,krf2;
112     real             *charge;
113     /* #endif */
114     /* #if 'GeneralizedBorn' in KERNEL_ELEC */
115     int              gbitab;
116     real             vgb,fgb,vgbsum,dvdasum,gbscale,gbtabscale,isaprod,gbqqfactor,gbinvepsdiff,dvdaj,gbeps,dvdatmp;
117     real             *invsqrta,*dvda,*gbtab;
118     /* #endif */
119     /* #if KERNEL_VDW != 'None' */
120     int              nvdwtype;
121     real             rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,br,vvdwexp,sh_vdw_invrcut6;
122     int              *vdwtype;
123     real             *vdwparam;
124     /* #endif */
125     /* #if 'Table' in KERNEL_ELEC or 'GeneralizedBorn' in KERNEL_ELEC or 'Table' in KERNEL_VDW */
126     int              vfitab;
127     real             rt,vfeps,vftabscale,Y,F,Geps,Heps2,Fp,VV,FF;
128     real             *vftab;
129     /* #endif */
130     /* #if 'Ewald' in KERNEL_ELEC */
131     int              ewitab;
132     real             ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace;
133     real             *ewtab;
134     /* #endif */
135     /* #if 'PotentialSwitch' in [KERNEL_MOD_ELEC,KERNEL_MOD_VDW] */
136     real             rswitch,swV3,swV4,swV5,swF2,swF3,swF4,d,d2,sw,dsw;
137     /* #endif */
138
139     x                = xx[0];
140     f                = ff[0];
141
142     nri              = nlist->nri;
143     iinr             = nlist->iinr;
144     jindex           = nlist->jindex;
145     jjnr             = nlist->jjnr;
146     shiftidx         = nlist->shift;
147     gid              = nlist->gid;
148     shiftvec         = fr->shift_vec[0];
149     fshift           = fr->fshift[0];
150     /* #if KERNEL_ELEC != 'None' */
151     facel            = fr->epsfac;
152     charge           = mdatoms->chargeA;
153     /*     #if 'ReactionField' in KERNEL_ELEC */
154     krf              = fr->ic->k_rf;
155     krf2             = krf*2.0;
156     crf              = fr->ic->c_rf;
157     /*     #endif */
158     /* #endif */
159     /* #if KERNEL_VDW != 'None' */
160     nvdwtype         = fr->ntype;
161     vdwparam         = fr->nbfp;
162     vdwtype          = mdatoms->typeA;
163     /* #endif */
164
165     /* #if 'Table' in KERNEL_ELEC and 'Table' in KERNEL_VDW */
166     vftab            = kernel_data->table_elec_vdw->data;
167     vftabscale       = kernel_data->table_elec_vdw->scale;
168     /* #elif 'Table' in KERNEL_ELEC */
169     vftab            = kernel_data->table_elec->data;
170     vftabscale       = kernel_data->table_elec->scale;
171     /* #elif 'Table' in KERNEL_VDW */
172     vftab            = kernel_data->table_vdw->data;
173     vftabscale       = kernel_data->table_vdw->scale;
174     /* #endif */
175
176     /* #if 'Ewald' in KERNEL_ELEC */
177     sh_ewald         = fr->ic->sh_ewald;
178     /*     #if KERNEL_VF=='Force' and KERNEL_MOD_ELEC!='PotentialSwitch' */
179     ewtab            = fr->ic->tabq_coul_F;
180     ewtabscale       = fr->ic->tabq_scale;
181     ewtabhalfspace   = 0.5/ewtabscale;
182     /*     #else */
183     ewtab            = fr->ic->tabq_coul_FDV0;
184     ewtabscale       = fr->ic->tabq_scale;
185     ewtabhalfspace   = 0.5/ewtabscale;
186      /*     #endif */
187     /* #endif */
188
189     /* #if KERNEL_ELEC=='GeneralizedBorn' */
190     invsqrta         = fr->invsqrta;
191     dvda             = fr->dvda;
192     gbtabscale       = fr->gbtab.scale;
193     gbtab            = fr->gbtab.data;
194     gbinvepsdiff     = (1.0/fr->epsilon_r) - (1.0/fr->gb_epsilon_solvent);
195     /* #endif */
196
197     /* #if 'Water' in GEOMETRY_I */
198     /* Setup water-specific parameters */
199     inr              = nlist->iinr[0];
200     /*     #for I in PARTICLES_ELEC_I */
201     iq{I}              = facel*charge[inr+{I}];
202     /*     #endfor */
203     /*     #for I in PARTICLES_VDW_I */
204     vdwioffset{I}      = {NVDWPARAM}*nvdwtype*vdwtype[inr+{I}];
205     /*     #endfor */
206     /* #endif */
207
208     /* #if 'Water' in GEOMETRY_J */
209     /*     #for J in PARTICLES_ELEC_J */
210     jq{J}              = charge[inr+{J}];
211     /*     #endfor */
212     /*     #for J in PARTICLES_VDW_J */
213     vdwjidx{J}         = {NVDWPARAM}*vdwtype[inr+{J}];
214     /*     #endfor */
215     /*     #for I,J in PAIRS_IJ */
216     /*         #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
217     qq{I}{J}             = iq{I}*jq{J};
218     /*         #endif */
219     /*         #if 'vdw' in INTERACTION_FLAGS[I][J] */
220     /*             #if 'Buckingham' in KERNEL_VDW */
221     c6_{I}{J}            = vdwparam[vdwioffset{I}+vdwjidx{J}];
222     cexp1_{I}{J}         = vdwparam[vdwioffset{I}+vdwjidx{J}+1];
223     cexp2_{I}{J}         = vdwparam[vdwioffset{I}+vdwjidx{J}+2];
224     /*             #else */
225     c6_{I}{J}            = vdwparam[vdwioffset{I}+vdwjidx{J}];
226     c12_{I}{J}           = vdwparam[vdwioffset{I}+vdwjidx{J}+1];
227     /*             #endif */
228     /*         #endif */
229     /*     #endfor */
230     /* #endif */
231
232     /* #if KERNEL_MOD_ELEC!='None' or KERNEL_MOD_VDW!='None' */
233     /*     #if KERNEL_ELEC!='None' */
234     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
235     rcutoff          = fr->rcoulomb;
236     /*     #else */
237     rcutoff          = fr->rvdw;
238     /*     #endif */
239     rcutoff2         = rcutoff*rcutoff;
240     /* #endif */
241
242     /* #if KERNEL_MOD_VDW=='PotentialShift' */
243     sh_vdw_invrcut6  = fr->ic->sh_invrc6;
244     rvdw             = fr->rvdw;
245     /* #endif */
246
247     /* #if 'PotentialSwitch' in [KERNEL_MOD_ELEC,KERNEL_MOD_VDW] */
248     /*     #if KERNEL_MOD_ELEC=='PotentialSwitch'  */
249     rswitch          = fr->rcoulomb_switch;
250     /*     #else */
251     rswitch          = fr->rvdw_switch;
252     /*     #endif */
253     /* Setup switch parameters */
254     d                = rcutoff-rswitch;
255     swV3             = -10.0/(d*d*d);
256     swV4             =  15.0/(d*d*d*d);
257     swV5             =  -6.0/(d*d*d*d*d);
258     /*     #if 'Force' in KERNEL_VF */
259     swF2             = -30.0/(d*d*d);
260     swF3             =  60.0/(d*d*d*d);
261     swF4             = -30.0/(d*d*d*d*d);
262     /*     #endif */
263     /* #endif */
264
265     /* ## Keep track of the floating point operations we issue for reporting! */
266     /* #define OUTERFLOPS 0 */
267     /* #define INNERFLOPS 0 */
268     outeriter        = 0;
269     inneriter        = 0;
270
271     /* Start outer loop over neighborlists */
272     for(iidx=0; iidx<nri; iidx++)
273     {
274         /* Load shift vector for this list */
275         i_shift_offset   = DIM*shiftidx[iidx];
276         shX              = shiftvec[i_shift_offset+XX];
277         shY              = shiftvec[i_shift_offset+YY];
278         shZ              = shiftvec[i_shift_offset+ZZ];
279
280         /* Load limits for loop over neighbors */
281         j_index_start    = jindex[iidx];
282         j_index_end      = jindex[iidx+1];
283
284         /* Get outer coordinate index */
285         inr              = iinr[iidx];
286         i_coord_offset   = DIM*inr;
287
288         /* Load i particle coords and add shift vector */
289         /* ## Loop over i particles, but only include ones that we use - skip e.g. vdw-only sites for elec-only kernel */
290         /*     #for I in PARTICLES_I */
291         ix{I}              = shX + x[i_coord_offset+DIM*{I}+XX];
292         iy{I}              = shY + x[i_coord_offset+DIM*{I}+YY];
293         iz{I}              = shZ + x[i_coord_offset+DIM*{I}+ZZ];
294         /*     #define OUTERFLOPS OUTERFLOPS+3 */
295         /* #endfor */
296
297         /* #if 'Force' in KERNEL_VF */
298         /*     #for I in PARTICLES_I */
299         fix{I}             = 0.0;
300         fiy{I}             = 0.0;
301         fiz{I}             = 0.0;
302         /*     #endfor */
303         /* #endif */
304
305         /* ## For water we already preloaded parameters at the start of the kernel */
306         /* #if not 'Water' in GEOMETRY_I */
307         /* Load parameters for i particles */
308         /*     #for I in PARTICLES_ELEC_I */
309         iq{I}              = facel*charge[inr+{I}];
310         /*         #define OUTERFLOPS OUTERFLOPS+1 */
311         /*         #if KERNEL_ELEC=='GeneralizedBorn' */
312         isai{I}            = invsqrta[inr+{I}];
313         /*         #endif */
314         /*     #endfor */
315         /*     #for I in PARTICLES_VDW_I */
316         vdwioffset{I}      = {NVDWPARAM}*nvdwtype*vdwtype[inr+{I}];
317         /*     #endfor */
318         /* #endif */
319
320         /* #if 'Potential' in KERNEL_VF */
321         /* Reset potential sums */
322         /*     #if KERNEL_ELEC != 'None' */
323         velecsum         = 0.0;
324         /*     #endif */
325         /*     #if 'GeneralizedBorn' in KERNEL_ELEC */
326         vgbsum           = 0.0;
327         /*     #endif */
328         /*     #if KERNEL_VDW != 'None' */
329         vvdwsum          = 0.0;
330         /*     #endif */
331         /* #endif */
332         /*     #if 'GeneralizedBorn' in KERNEL_ELEC and 'Force' in KERNEL_VF */
333         dvdasum          = 0.0;
334         /*     #endif */
335
336         /* Start inner kernel loop */
337         for(jidx=j_index_start; jidx<j_index_end; jidx++)
338         {
339             /* Get j neighbor index, and coordinate index */
340             jnr              = jjnr[jidx];
341             j_coord_offset   = DIM*jnr;
342
343             /* load j atom coordinates */
344             /* #for J in PARTICLES_J */
345             jx{J}              = x[j_coord_offset+DIM*{J}+XX];
346             jy{J}              = x[j_coord_offset+DIM*{J}+YY];
347             jz{J}              = x[j_coord_offset+DIM*{J}+ZZ];
348             /* #endfor */
349
350             /* Calculate displacement vector */
351             /* #for I,J in PAIRS_IJ */
352             dx{I}{J}             = ix{I} - jx{J};
353             dy{I}{J}             = iy{I} - jy{J};
354             dz{I}{J}             = iz{I} - jz{J};
355             /*     #define INNERFLOPS INNERFLOPS+3 */
356             /* #endfor */
357
358             /* Calculate squared distance and things based on it */
359             /* #for I,J in PAIRS_IJ */
360             rsq{I}{J}            = dx{I}{J}*dx{I}{J}+dy{I}{J}*dy{I}{J}+dz{I}{J}*dz{I}{J};
361             /*     #define INNERFLOPS INNERFLOPS+5 */
362             /* #endfor */
363
364             /* #for I,J in PAIRS_IJ */
365             /*     #if 'rinv' in INTERACTION_FLAGS[I][J] */
366             rinv{I}{J}           = gmx_invsqrt(rsq{I}{J});
367             /*         #define INNERFLOPS INNERFLOPS+5 */
368             /*     #endif */
369             /* #endfor */
370
371             /* #for I,J in PAIRS_IJ */
372             /*     #if 'rinvsq' in INTERACTION_FLAGS[I][J] */
373             /*         # if 'rinv' not in INTERACTION_FLAGS[I][J] */
374             rinvsq{I}{J}         = 1.0/rsq{I}{J};
375             /*             #define INNERFLOPS INNERFLOPS+4 */
376             /*         #else */
377             rinvsq{I}{J}         = rinv{I}{J}*rinv{I}{J};
378             /*             #define INNERFLOPS INNERFLOPS+1 */
379             /*         #endif */
380             /*     #endif */
381             /* #endfor */
382
383             /* #if not 'Water' in GEOMETRY_J */
384             /* Load parameters for j particles */
385             /*     #for J in PARTICLES_ELEC_J */
386             jq{J}              = charge[jnr+{J}];
387             /*         #if KERNEL_ELEC=='GeneralizedBorn' */
388             isaj{J}           = invsqrta[jnr+{J}];
389             /*         #endif */
390             /*     #endfor */
391             /*     #for J in PARTICLES_VDW_J */
392             vdwjidx{J}         = {NVDWPARAM}*vdwtype[jnr+{J}];
393             /*     #endfor */
394             /* #endif */
395
396             /* #for I,J in PAIRS_IJ */
397
398             /**************************
399              * CALCULATE INTERACTIONS *
400              **************************/
401
402             /*     #if 'exactcutoff' in INTERACTION_FLAGS[I][J] */
403             /*         ## We always calculate rinv/rinvsq above to enable pipelineing in compilers (performance tested on x86) */
404             if (rsq{I}{J}<rcutoff2)
405             {
406                 /*     #if 0    ## this and the next two lines is a hack to maintain auto-indentation in template file */
407             }
408             /*         #endif */
409             /*     #endif */
410
411             /*     #if 'r' in INTERACTION_FLAGS[I][J] */
412             r{I}{J}              = rsq{I}{J}*rinv{I}{J};
413             /*         #define INNERFLOPS INNERFLOPS+1 */
414             /*     #endif */
415
416             /*     ## For water geometries we already loaded parameters at the start of the kernel */
417             /*     #if not 'Water' in GEOMETRY_J */
418             /*         #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
419             qq{I}{J}             = iq{I}*jq{J};
420             /*             #define INNERFLOPS INNERFLOPS+1 */
421             /*         #endif */
422             /*         #if 'vdw' in INTERACTION_FLAGS[I][J] */
423             /*             #if KERNEL_VDW=='Buckingham' */
424             c6_{I}{J}            = vdwparam[vdwioffset{I}+vdwjidx{J}];
425             cexp1_{I}{J}         = vdwparam[vdwioffset{I}+vdwjidx{J}+1];
426             cexp2_{I}{J}         = vdwparam[vdwioffset{I}+vdwjidx{J}+2];
427             /*             #else */
428             c6_{I}{J}            = vdwparam[vdwioffset{I}+vdwjidx{J}];
429             c12_{I}{J}           = vdwparam[vdwioffset{I}+vdwjidx{J}+1];
430             /*             #endif */
431             /*         #endif */
432             /*     #endif */
433
434             /*     #if 'table' in INTERACTION_FLAGS[I][J] */
435             /* Calculate table index by multiplying r with table scale and truncate to integer */
436             rt               = r{I}{J}*vftabscale;
437             vfitab           = rt;
438             vfeps            = rt-vfitab;
439             vfitab           = {TABLE_NINTERACTIONS}*{TABLE_POINT_SIZE}*vfitab;
440             /*         #define INNERFLOPS INNERFLOPS+2 */
441             /*     #endif */
442
443             /*     ## ELECTROSTATIC INTERACTIONS */
444             /*     #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
445
446             /*         #if KERNEL_ELEC=='Coulomb' */
447
448             /* COULOMB ELECTROSTATICS */
449             velec            = qq{I}{J}*rinv{I}{J};
450             /*             #define INNERFLOPS INNERFLOPS+1 */
451             /*             #if 'Force' in KERNEL_VF */
452             felec            = velec*rinvsq{I}{J};
453             /*                 #define INNERFLOPS INNERFLOPS+2 */
454             /*             #endif */
455
456             /*         #elif KERNEL_ELEC=='ReactionField' */
457
458             /* REACTION-FIELD ELECTROSTATICS */
459             /*             #if 'Potential' in KERNEL_VF */
460             velec            = qq{I}{J}*(rinv{I}{J}+krf*rsq{I}{J}-crf);
461             /*                 #define INNERFLOPS INNERFLOPS+4 */
462             /*             #endif */
463             /*             #if 'Force' in KERNEL_VF */
464             felec            = qq{I}{J}*(rinv{I}{J}*rinvsq{I}{J}-krf2);
465             /*                 #define INNERFLOPS INNERFLOPS+3 */
466             /*             #endif */
467
468             /*         #elif KERNEL_ELEC=='GeneralizedBorn' */
469
470             /* GENERALIZED BORN AND COULOMB ELECTROSTATICS */
471             isaprod          = isai{I}*isaj{J};
472             gbqqfactor       = isaprod*(-qq{I}{J})*gbinvepsdiff;
473             gbscale          = isaprod*gbtabscale;
474             dvdaj            = dvda[jnr+{J}];
475             /*             #define INNERFLOPS INNERFLOPS+5 */
476
477             /* Calculate generalized born table index - this is a separate table from the normal one,
478              * but we use the same procedure by multiplying r with scale and truncating to integer.
479              */
480             rt               = r{I}{J}*gbscale;
481             gbitab           = rt;
482             gbeps            = rt-gbitab;
483             gbitab           = 4*gbitab;
484
485             Y                = gbtab[gbitab];
486             F                = gbtab[gbitab+1];
487             Geps             = gbeps*gbtab[gbitab+2];
488             Heps2            = gbeps*gbeps*gbtab[gbitab+3];
489             Fp               = F+Geps+Heps2;
490             VV               = Y+gbeps*Fp;
491             vgb              = gbqqfactor*VV;
492             /*             #define INNERFLOPS INNERFLOPS+10 */
493
494             /*             #if 'Force' in KERNEL_VF */
495             FF               = Fp+Geps+2.0*Heps2;
496             fgb              = gbqqfactor*FF*gbscale;
497             dvdatmp          = -0.5*(vgb+fgb*r{I}{J});
498             dvdasum          = dvdasum + dvdatmp;
499             dvda[jnr]        = dvdaj+dvdatmp*isaj{J}*isaj{J};
500             /*                 #define INNERFLOPS INNERFLOPS+13 */
501             /*             #endif */
502             velec            = qq{I}{J}*rinv{I}{J};
503             /*                 #define INNERFLOPS INNERFLOPS+1 */
504             /*             #if 'Force' in KERNEL_VF */
505             felec            = (velec*rinv{I}{J}-fgb)*rinv{I}{J};
506             /*                 #define INNERFLOPS INNERFLOPS+3 */
507             /*             #endif */
508
509             /*         #elif KERNEL_ELEC=='Ewald' */
510             /* EWALD ELECTROSTATICS */
511
512             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
513             ewrt             = r{I}{J}*ewtabscale;
514             ewitab           = ewrt;
515             eweps            = ewrt-ewitab;
516             /*             #define INNERFLOPS INNERFLOPS+2 */
517             /*             #if 'Potential' in KERNEL_VF or KERNEL_MOD_ELEC=='PotentialSwitch' */
518             ewitab           = 4*ewitab;
519             felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
520             /*                 #define INNERFLOPS INNERFLOPS+4 */
521             /*                 #if KERNEL_MOD_ELEC=='PotentialShift' */
522             velec            = qq{I}{J}*((rinv{I}{J}-sh_ewald)-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
523             /*                     #define INNERFLOPS INNERFLOPS+7 */
524             /*                 #else */
525             velec            = qq{I}{J}*(rinv{I}{J}-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
526             /*                     #define INNERFLOPS INNERFLOPS+6 */
527             /*                 #endif */
528             /*                 #if 'Force' in KERNEL_VF */
529             felec            = qq{I}{J}*rinv{I}{J}*(rinvsq{I}{J}-felec);
530             /*                      #define INNERFLOPS INNERFLOPS+3 */
531             /*                 #endif */
532             /*             #elif KERNEL_VF=='Force' */
533             felec            = (1.0-eweps)*ewtab[ewitab]+eweps*ewtab[ewitab+1];
534             felec            = qq{I}{J}*rinv{I}{J}*(rinvsq{I}{J}-felec);
535             /*                 #define INNERFLOPS INNERFLOPS+7 */
536             /*             #endif */
537
538             /*         #elif KERNEL_ELEC=='CubicSplineTable' */
539
540             /* CUBIC SPLINE TABLE ELECTROSTATICS */
541             /*             #if 'Force' in KERNEL_VF */
542             Y                = vftab[vfitab];
543             /*             #endif */
544             F                = vftab[vfitab+1];
545             Geps             = vfeps*vftab[vfitab+2];
546             Heps2            = vfeps*vfeps*vftab[vfitab+3];
547             Fp               = F+Geps+Heps2;
548             /*             #define INNERFLOPS INNERFLOPS+5 */
549             /*             #if 'Potential' in KERNEL_VF */
550             VV               = Y+vfeps*Fp;
551             velec            = qq{I}{J}*VV;
552             /*                 #define INNERFLOPS INNERFLOPS+3 */
553             /*             #endif */
554             /*             #if 'Force' in KERNEL_VF */
555             FF               = Fp+Geps+2.0*Heps2;
556             felec            = -qq{I}{J}*FF*vftabscale*rinv{I}{J};
557             /*                 #define INNERFLOPS INNERFLOPS+7 */
558             /*             #endif */
559             /*         #endif */
560             /*         ## End of check for electrostatics interaction forms */
561             /*     #endif */
562             /*     ## END OF ELECTROSTATIC INTERACTION CHECK FOR PAIR I-J */
563
564             /*     #if 'vdw' in INTERACTION_FLAGS[I][J] */
565
566             /*         #if KERNEL_VDW=='LennardJones' */
567
568             /* LENNARD-JONES DISPERSION/REPULSION */
569
570             rinvsix          = rinvsq{I}{J}*rinvsq{I}{J}*rinvsq{I}{J};
571             /*             #define INNERFLOPS INNERFLOPS+2 */
572             /*             #if 'Potential' in KERNEL_VF or KERNEL_MOD_VDW=='PotentialSwitch' */
573             vvdw6            = c6_{I}{J}*rinvsix;
574             vvdw12           = c12_{I}{J}*rinvsix*rinvsix;
575             /*                 #define INNERFLOPS INNERFLOPS+3 */
576             /*                 #if KERNEL_MOD_VDW=='PotentialShift' */
577             vvdw             = (vvdw12 - c12_{I}{J}*sh_vdw_invrcut6*sh_vdw_invrcut6)*(1.0/12.0) - (vvdw6 - c6_{I}{J}*sh_vdw_invrcut6)*(1.0/6.0);
578             /*                     #define INNERFLOPS INNERFLOPS+8 */
579             /*                 #else */
580             vvdw             = vvdw12*(1.0/12.0) - vvdw6*(1.0/6.0);
581             /*                     #define INNERFLOPS INNERFLOPS+3 */
582             /*                 #endif */
583             /*                 ## Check for force inside potential check, i.e. this means we already did the potential part */
584             /*                 #if 'Force' in KERNEL_VF */
585             fvdw             = (vvdw12-vvdw6)*rinvsq{I}{J};
586             /*                     #define INNERFLOPS INNERFLOPS+2 */
587             /*                 #endif */
588             /*             #elif KERNEL_VF=='Force' */
589             /*                 ## Force-only LennardJones makes it possible to save 1 flop (they do add up...) */
590             fvdw             = (c12_{I}{J}*rinvsix-c6_{I}{J})*rinvsix*rinvsq{I}{J};
591             /*                 #define INNERFLOPS INNERFLOPS+4 */
592             /*             #endif */
593
594             /*         #elif KERNEL_VDW=='Buckingham' */
595
596             /* BUCKINGHAM DISPERSION/REPULSION */
597             rinvsix          = rinvsq{I}{J}*rinvsq{I}{J}*rinvsq{I}{J};
598             vvdw6            = c6_{I}{J}*rinvsix;
599             br               = cexp2_{I}{J}*r{I}{J};
600             vvdwexp          = cexp1_{I}{J}*exp(-br);
601             /*             ## Estimate exp() to 25 flops */
602             /*             #define INNERFLOPS INNERFLOPS+31 */
603             /*             #if 'Potential' in KERNEL_VF or KERNEL_MOD_VDW=='PotentialSwitch'  */
604             /*                 #if KERNEL_MOD_VDW=='PotentialShift' */
605             vvdw             = (vvdwexp-cexp1_{I}{J}*exp(-cexp2_{I}{J}*rvdw)) - (vvdw6 - c6_{I}{J}*sh_vdw_invrcut6)*(1.0/6.0);
606             /*                     #define INNERFLOPS INNERFLOPS+33 */
607             /*                  #else */
608             vvdw             = vvdwexp - vvdw6*(1.0/6.0);
609             /*                     #define INNERFLOPS INNERFLOPS+2 */
610             /*                 #endif */
611             /*             #endif */
612             /*             #if 'Force' in KERNEL_VF */
613             fvdw             = (br*vvdwexp-vvdw6)*rinvsq{I}{J};
614             /*                 #define INNERFLOPS INNERFLOPS+3 */
615             /*             #endif */
616
617             /*         #elif KERNEL_VDW=='CubicSplineTable' */
618
619             /* CUBIC SPLINE TABLE DISPERSION */
620             vfitab          += {TABLE_VDW_OFFSET};
621             /*             #if 'Force' in KERNEL_VF */
622             Y                = vftab[vfitab];
623             /*             #endif */
624             F                = vftab[vfitab+1];
625             Geps             = vfeps*vftab[vfitab+2];
626             Heps2            = vfeps*vfeps*vftab[vfitab+3];
627             Fp               = F+Geps+Heps2;
628             /*             #define INNERFLOPS INNERFLOPS+5 */
629             /*             #if 'Potential' in KERNEL_VF */
630             VV               = Y+vfeps*Fp;
631             vvdw6            = c6_{I}{J}*VV;
632             /*                 #define INNERFLOPS INNERFLOPS+3 */
633             /*             #endif */
634             /*             #if 'Force' in KERNEL_VF */
635             FF               = Fp+Geps+2.0*Heps2;
636             fvdw6            = c6_{I}{J}*FF;
637             /*                 #define INNERFLOPS INNERFLOPS+4 */
638             /*             #endif */
639
640             /* CUBIC SPLINE TABLE REPULSION */
641             /*             #if 'Force' in KERNEL_VF */
642             Y                = vftab[vfitab+4];
643             /*             #endif */
644             F                = vftab[vfitab+5];
645             Geps             = vfeps*vftab[vfitab+6];
646             Heps2            = vfeps*vfeps*vftab[vfitab+7];
647             Fp               = F+Geps+Heps2;
648             /*             #define INNERFLOPS INNERFLOPS+5 */
649             /*             #if 'Potential' in KERNEL_VF */
650             VV               = Y+vfeps*Fp;
651             vvdw12           = c12_{I}{J}*VV;
652             /*                 #define INNERFLOPS INNERFLOPS+3 */
653             /*             #endif */
654             /*             #if 'Force' in KERNEL_VF */
655             FF               = Fp+Geps+2.0*Heps2;
656             fvdw12           = c12_{I}{J}*FF;
657             /*                 #define INNERFLOPS INNERFLOPS+4 */
658             /*             #endif */
659             /*             #if 'Potential' in KERNEL_VF */
660             vvdw             = vvdw12+vvdw6;
661             /*                 #define INNERFLOPS INNERFLOPS+1 */
662             /*             #endif */
663             /*             #if 'Force' in KERNEL_VF */
664             fvdw             = -(fvdw6+fvdw12)*vftabscale*rinv{I}{J};
665             /*                 #define INNERFLOPS INNERFLOPS+4 */
666             /*             #endif */
667             /*         #endif */
668             /*         ## End of check for vdw interaction forms */
669             /*     #endif */
670             /*     ## END OF VDW INTERACTION CHECK FOR PAIR I-J */
671
672             /*     #if 'switch' in INTERACTION_FLAGS[I][J] */
673             d                = r{I}{J}-rswitch;
674             d                = (d>0.0) ? d : 0.0;
675             d2               = d*d;
676             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
677             /*         #define INNERFLOPS INNERFLOPS+9 */
678
679             /*         #if 'Force' in KERNEL_VF */
680             dsw              = d2*(swF2+d*(swF3+d*swF4));
681             /*             #define INNERFLOPS INNERFLOPS+5 */
682             /*         #endif */
683
684             /* Evaluate switch function */
685             /*         #if 'Force' in KERNEL_VF */
686             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
687             /*             #if 'electrostatics' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_ELEC=='PotentialSwitch' */
688             felec            = felec*sw - rinv{I}{J}*velec*dsw;
689             /*                 #define INNERFLOPS INNERFLOPS+3 */
690             /*             #endif */
691             /*             #if 'vdw' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_VDW=='PotentialSwitch' */
692             fvdw             = fvdw*sw - rinv{I}{J}*vvdw*dsw;
693             /*                 #define INNERFLOPS INNERFLOPS+3 */
694             /*             #endif */
695             /*         #endif */
696             /*         #if 'Potential' in KERNEL_VF */
697             /*             #if 'electrostatics' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_ELEC=='PotentialSwitch' */
698             velec           *= sw;
699             /*                 #define INNERFLOPS INNERFLOPS+1 */
700             /*             #endif */
701             /*             #if 'vdw' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_VDW=='PotentialSwitch' */
702             vvdw            *= sw;
703             /*                 #define INNERFLOPS INNERFLOPS+1 */
704             /*             #endif */
705             /*         #endif */
706             /*     #endif */
707
708             /*     #if 'Potential' in KERNEL_VF */
709             /* Update potential sums from outer loop */
710             /*         #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
711             velecsum        += velec;
712             /*             #define INNERFLOPS INNERFLOPS+1 */
713             /*             #if KERNEL_ELEC=='GeneralizedBorn' */
714             vgbsum          += vgb;
715             /*                 #define INNERFLOPS INNERFLOPS+1 */
716             /*             #endif */
717             /*         #endif */
718             /*         #if 'vdw' in INTERACTION_FLAGS[I][J] */
719             vvdwsum         += vvdw;
720             /*             #define INNERFLOPS INNERFLOPS+1 */
721             /*         #endif */
722             /*     #endif */
723
724             /*     #if 'Force' in KERNEL_VF */
725
726             /*         #if 'electrostatics' in INTERACTION_FLAGS[I][J] and 'vdw' in INTERACTION_FLAGS[I][J] */
727             fscal            = felec+fvdw;
728             /*             #define INNERFLOPS INNERFLOPS+1 */
729             /*         #elif 'electrostatics' in INTERACTION_FLAGS[I][J] */
730             fscal            = felec;
731             /*         #elif 'vdw' in INTERACTION_FLAGS[I][J] */
732             fscal            = fvdw;
733             /*        #endif */
734
735             /* Calculate temporary vectorial force */
736             tx               = fscal*dx{I}{J};
737             ty               = fscal*dy{I}{J};
738             tz               = fscal*dz{I}{J};
739
740             /* Update vectorial force */
741             fix{I}            += tx;
742             fiy{I}            += ty;
743             fiz{I}            += tz;
744             f[j_coord_offset+DIM*{J}+XX] -= tx;
745             f[j_coord_offset+DIM*{J}+YY] -= ty;
746             f[j_coord_offset+DIM*{J}+ZZ] -= tz;
747
748             /*         #define INNERFLOPS INNERFLOPS+9 */
749             /*     #endif */
750
751             /*     #if 'exactcutoff' in INTERACTION_FLAGS[I][J] */
752             /*         #if 0    ## This and next two lines is a hack to maintain indentation in template file */
753             {
754                 /*     #endif */
755             }
756             /*     #endif */
757             /*    ## End of check for the interaction being outside the cutoff */
758
759             /* #endfor */
760             /* ## End of loop over i-j interaction pairs */
761
762             /* Inner loop uses {INNERFLOPS} flops */
763         }
764         /* End of innermost loop */
765
766         /* #if 'Force' in KERNEL_VF */
767         tx = ty = tz = 0;
768         /*     #for I in PARTICLES_I */
769         f[i_coord_offset+DIM*{I}+XX] += fix{I};
770         f[i_coord_offset+DIM*{I}+YY] += fiy{I};
771         f[i_coord_offset+DIM*{I}+ZZ] += fiz{I};
772         tx                         += fix{I};
773         ty                         += fiy{I};
774         tz                         += fiz{I};
775         /*         #define OUTERFLOPS OUTERFLOPS+6 */
776         /*     #endfor */
777         fshift[i_shift_offset+XX]  += tx;
778         fshift[i_shift_offset+YY]  += ty;
779         fshift[i_shift_offset+ZZ]  += tz;
780         /*     #define OUTERFLOPS OUTERFLOPS+3 */
781         /* #endif */
782
783         /* #if 'Potential' in KERNEL_VF */
784         ggid                        = gid[iidx];
785         /* Update potential energies */
786         /*     #if KERNEL_ELEC != 'None' */
787         kernel_data->energygrp_elec[ggid] += velecsum;
788         /*         #define OUTERFLOPS OUTERFLOPS+1 */
789         /*     #endif */
790         /*     #if 'GeneralizedBorn' in KERNEL_ELEC */
791         kernel_data->energygrp_polarization[ggid] += vgbsum;
792         /*         #define OUTERFLOPS OUTERFLOPS+1 */
793         /*     #endif */
794         /*     #if KERNEL_VDW != 'None' */
795         kernel_data->energygrp_vdw[ggid] += vvdwsum;
796         /*         #define OUTERFLOPS OUTERFLOPS+1 */
797         /*     #endif */
798         /* #endif */
799         /*     #if 'GeneralizedBorn' in KERNEL_ELEC and 'Force' in KERNEL_VF */
800         dvda[nri]                   = dvda[nri] + dvdasum*isai{I}*isai{I};
801         /*     #endif */
802
803         /* Increment number of inner iterations */
804         inneriter                  += j_index_end - j_index_start;
805
806         /* Outer loop uses {OUTERFLOPS} flops */
807     }
808
809     /* Increment number of outer iterations */
810     outeriter        += nri;
811
812     /* Update outer/inner flops */
813     /* ## NB: This is not important, it just affects the flopcount. However, since our preprocessor is */
814     /* ## primitive and replaces aggressively even in strings inside these directives, we need to      */
815     /* ## assemble the main part of the name (containing KERNEL/ELEC/VDW) directly in the source.      */
816     /* #if GEOMETRY_I == 'Water3'            */
817     /*     #define ISUFFIX '_W3'             */
818     /* #elif GEOMETRY_I == 'Water4'          */
819     /*     #define ISUFFIX '_W4'             */
820     /* #else                                 */
821     /*     #define ISUFFIX ''                */
822     /* #endif                                */
823     /* #if GEOMETRY_J == 'Water3'            */
824     /*     #define JSUFFIX 'W3'              */
825     /* #elif GEOMETRY_J == 'Water4'          */
826     /*     #define JSUFFIX 'W4'              */
827     /* #else                                 */
828     /*     #define JSUFFIX ''                */
829     /* #endif                                */
830     /* #if 'PotentialAndForce' in KERNEL_VF  */
831     /*     #define VFSUFFIX  '_VF'           */
832     /* #elif 'Potential' in KERNEL_VF        */
833     /*     #define VFSUFFIX '_V'             */
834     /* #else                                 */
835     /*     #define VFSUFFIX '_F'             */
836     /* #endif                                */
837
838     /* #if KERNEL_ELEC != 'None' and KERNEL_VDW != 'None' */
839     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
840     /* #elif KERNEL_ELEC != 'None' */
841     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
842     /* #else */
843     inc_nrnb(nrnb,eNR_NBKERNEL_VDW{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
844     /* #endif  */
845 }