3 * This file is part of the GROMACS molecular simulation package.
5 * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 #error This file must be processed with the Gromacs pre-preprocessor
38 /* #if INCLUDE_HEADER */
45 #include "../nb_kernel.h"
46 #include "types/simple.h"
51 /* ## List of variables set by the generating script: */
53 /* ## Setttings that apply to the entire kernel: */
54 /* ## KERNEL_ELEC: String, choice for electrostatic interactions */
55 /* ## KERNEL_VDW: String, choice for van der Waals interactions */
56 /* ## KERNEL_NAME: String, name of this kernel */
57 /* ## KERNEL_VF: String telling if we calculate potential, force, or both */
58 /* ## GEOMETRY_I/GEOMETRY_J: String, name of each geometry, e.g. 'Water3' or '1Particle' */
60 /* ## Setttings that apply to particles in the outer (I) or inner (J) loops: */
61 /* ## PARTICLES_I[]/ Arrays with lists of i/j particles to use in kernel. It is */
62 /* ## PARTICLES_J[]: just [0] for particle geometry, but can be longer for water */
63 /* ## PARTICLES_ELEC_I[]/ Arrays with lists of i/j particle that have electrostatics */
64 /* ## PARTICLES_ELEC_J[]: interactions that should be calculated in this kernel. */
65 /* ## PARTICLES_VDW_I[]/ Arrays with the list of i/j particle that have VdW */
66 /* ## PARTICLES_VDW_J[]: interactions that should be calculated in this kernel. */
68 /* ## Setttings for pairs of interactions (e.g. 2nd i particle against 1st j particle) */
69 /* ## PAIRS_IJ[]: Array with (i,j) tuples of pairs for which interactions */
70 /* ## should be calculated in this kernel. Zero-charge particles */
71 /* ## do not have interactions with particles without vdw, and */
72 /* ## Vdw-only interactions are not evaluated in a no-vdw-kernel. */
73 /* ## INTERACTION_FLAGS[][]: 2D matrix, dimension e.g. 3*3 for water-water interactions. */
74 /* ## For each i-j pair, the element [I][J] is a list of strings */
75 /* ## defining properties/flags of this interaction. Examples */
76 /* ## include 'electrostatics'/'vdw' if that type of interaction */
77 /* ## should be evaluated, 'rsq'/'rinv'/'rinvsq' if those values */
78 /* ## are needed, and 'exactcutoff' or 'shift','switch' to */
79 /* ## decide if the force/potential should be modified. This way */
80 /* ## we only calculate values absolutely needed for each case. */
82 /* ## Calculate the size and offset for (merged/interleaved) table data */
84 /* #if ('CubicSplineTable' in [KERNEL_ELEC,KERNEL_VDW]) or KERNEL_VF=='PotentialAndForce' */
85 /* #define TABLE_POINT_SIZE 4 */
87 /* #define TABLE_POINT_SIZE 2 */
90 /* #if 'Table' in KERNEL_ELEC and 'Table' in KERNEL_VDW */
91 /* #define TABLE_NINTERACTIONS 3 */
92 /* #define TABLE_VDW_OFFSET TABLE_POINT_SIZE */
93 /* #elif 'Table' in KERNEL_ELEC */
94 /* #define TABLE_NINTERACTIONS 1 */
95 /* #elif 'Table' in KERNEL_VDW */
96 /* #define TABLE_NINTERACTIONS 2 */
97 /* #define TABLE_VDW_OFFSET 0 */
99 /* #define TABLE_NINTERACTIONS 0 */
102 /* #if 'Buckingham' in KERNEL_VDW */
103 /* #define NVDWPARAM 3 */
105 /* #define NVDWPARAM 2 */
109 * Gromacs nonbonded kernel: {KERNEL_NAME}
110 * Electrostatics interaction: {KERNEL_ELEC}
111 * VdW interaction: {KERNEL_VDW}
112 * Geometry: {GEOMETRY_I}-{GEOMETRY_J}
113 * Calculate force/pot: {KERNEL_VF}
117 (t_nblist * gmx_restrict nlist,
118 rvec * gmx_restrict xx,
119 rvec * gmx_restrict ff,
120 t_forcerec * gmx_restrict fr,
121 t_mdatoms * gmx_restrict mdatoms,
122 nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
123 t_nrnb * gmx_restrict nrnb)
125 /* ## Not all variables are used for all kernels, but any optimizing compiler fixes that, */
126 /* ## so there is no point in going to extremes to exclude variables that are not needed. */
127 int i_shift_offset,i_coord_offset,j_coord_offset;
128 int j_index_start,j_index_end;
129 int nri,inr,ggid,iidx,jidx,jnr,outeriter,inneriter;
130 real shX,shY,shZ,tx,ty,tz,fscal,rcutoff,rcutoff2;
131 int *iinr,*jindex,*jjnr,*shiftidx,*gid;
132 real *shiftvec,*fshift,*x,*f;
133 /* #for I in PARTICLES_I */
135 real ix{I},iy{I},iz{I},fix{I},fiy{I},fiz{I},iq{I},isai{I};
137 /* #for J in PARTICLES_J */
139 real jx{J},jy{J},jz{J},fjx{J},fjy{J},fjz{J},jq{J},isaj{J};
141 /* #for I,J in PAIRS_IJ */
142 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};
144 /* #if KERNEL_ELEC != 'None' */
145 real velec,felec,velecsum,facel,crf,krf,krf2;
148 /* #if 'GeneralizedBorn' in KERNEL_ELEC */
150 real vgb,fgb,vgbsum,dvdasum,gbscale,gbtabscale,isaprod,gbqqfactor,gbinvepsdiff,dvdaj,gbeps,dvdatmp;
151 real *invsqrta,*dvda,*gbtab;
153 /* #if KERNEL_VDW != 'None' */
155 real rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,br,vvdwexp,sh_vdw_invrcut6;
159 /* #if 'Table' in KERNEL_ELEC or 'GeneralizedBorn' in KERNEL_ELEC or 'Table' in KERNEL_VDW */
161 real rt,vfeps,vftabscale,Y,F,Geps,Heps2,Fp,VV,FF;
164 /* #if 'LJEwald' in KERNEL_VDW */
165 /* #for I,J in PAIRS_IJ */
168 real ewclj,ewclj2,ewclj6,ewcljrsq,poly,exponent,sh_lj_ewald;
171 /* #if 'Ewald' in KERNEL_ELEC */
173 real ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace;
176 /* #if 'PotentialSwitch' in [KERNEL_MOD_ELEC,KERNEL_MOD_VDW] */
177 real rswitch,swV3,swV4,swV5,swF2,swF3,swF4,d,d2,sw,dsw;
185 jindex = nlist->jindex;
187 shiftidx = nlist->shift;
189 shiftvec = fr->shift_vec[0];
190 fshift = fr->fshift[0];
191 /* #if KERNEL_ELEC != 'None' */
193 charge = mdatoms->chargeA;
194 /* #if 'ReactionField' in KERNEL_ELEC */
200 /* #if KERNEL_VDW != 'None' */
201 nvdwtype = fr->ntype;
203 vdwtype = mdatoms->typeA;
205 /* #if 'LJEwald' in KERNEL_VDW */
206 vdwgridparam = fr->ljpme_c6grid;
207 ewclj = fr->ewaldcoeff_lj;
208 sh_lj_ewald = fr->ic->sh_lj_ewald;
209 ewclj2 = ewclj*ewclj;
210 ewclj6 = ewclj2*ewclj2*ewclj2;
213 /* #if 'Table' in KERNEL_ELEC and 'Table' in KERNEL_VDW */
214 vftab = kernel_data->table_elec_vdw->data;
215 vftabscale = kernel_data->table_elec_vdw->scale;
216 /* #elif 'Table' in KERNEL_ELEC */
217 vftab = kernel_data->table_elec->data;
218 vftabscale = kernel_data->table_elec->scale;
219 /* #elif 'Table' in KERNEL_VDW */
220 vftab = kernel_data->table_vdw->data;
221 vftabscale = kernel_data->table_vdw->scale;
224 /* #if 'Ewald' in KERNEL_ELEC */
225 sh_ewald = fr->ic->sh_ewald;
226 /* #if KERNEL_VF=='Force' and KERNEL_MOD_ELEC!='PotentialSwitch' */
227 ewtab = fr->ic->tabq_coul_F;
228 ewtabscale = fr->ic->tabq_scale;
229 ewtabhalfspace = 0.5/ewtabscale;
231 ewtab = fr->ic->tabq_coul_FDV0;
232 ewtabscale = fr->ic->tabq_scale;
233 ewtabhalfspace = 0.5/ewtabscale;
237 /* #if KERNEL_ELEC=='GeneralizedBorn' */
238 invsqrta = fr->invsqrta;
240 gbtabscale = fr->gbtab.scale;
241 gbtab = fr->gbtab.data;
242 gbinvepsdiff = (1.0/fr->epsilon_r) - (1.0/fr->gb_epsilon_solvent);
245 /* #if 'Water' in GEOMETRY_I */
246 /* Setup water-specific parameters */
247 inr = nlist->iinr[0];
248 /* #for I in PARTICLES_ELEC_I */
249 iq{I} = facel*charge[inr+{I}];
251 /* #for I in PARTICLES_VDW_I */
252 vdwioffset{I} = {NVDWPARAM}*nvdwtype*vdwtype[inr+{I}];
256 /* #if 'Water' in GEOMETRY_J */
257 /* #for J in PARTICLES_ELEC_J */
258 jq{J} = charge[inr+{J}];
260 /* #for J in PARTICLES_VDW_J */
261 vdwjidx{J} = {NVDWPARAM}*vdwtype[inr+{J}];
263 /* #for I,J in PAIRS_IJ */
264 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
265 qq{I}{J} = iq{I}*jq{J};
267 /* #if 'vdw' in INTERACTION_FLAGS[I][J] */
268 /* #if 'Buckingham' in KERNEL_VDW */
269 c6_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}];
270 cexp1_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}+1];
271 cexp2_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}+2];
272 /* #elif 'LJEwald' in KERNEL_VDW */
273 c6_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}];
274 c12_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}+1];
275 c6grid_{I}{J} = vdwgridparam[vdwioffset{I}+vdwjidx{J}];
277 c6_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}];
278 c12_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}+1];
284 /* #if KERNEL_MOD_ELEC!='None' or KERNEL_MOD_VDW!='None' */
285 /* #if KERNEL_ELEC!='None' */
286 /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
287 rcutoff = fr->rcoulomb;
291 rcutoff2 = rcutoff*rcutoff;
294 /* #if KERNEL_MOD_VDW=='PotentialShift' */
295 sh_vdw_invrcut6 = fr->ic->sh_invrc6;
299 /* #if 'PotentialSwitch' in [KERNEL_MOD_ELEC,KERNEL_MOD_VDW] */
300 /* #if KERNEL_MOD_ELEC=='PotentialSwitch' */
301 rswitch = fr->rcoulomb_switch;
303 rswitch = fr->rvdw_switch;
305 /* Setup switch parameters */
307 swV3 = -10.0/(d*d*d);
308 swV4 = 15.0/(d*d*d*d);
309 swV5 = -6.0/(d*d*d*d*d);
310 /* #if 'Force' in KERNEL_VF */
311 swF2 = -30.0/(d*d*d);
312 swF3 = 60.0/(d*d*d*d);
313 swF4 = -30.0/(d*d*d*d*d);
317 /* ## Keep track of the floating point operations we issue for reporting! */
318 /* #define OUTERFLOPS 0 */
319 /* #define INNERFLOPS 0 */
323 /* Start outer loop over neighborlists */
324 for(iidx=0; iidx<nri; iidx++)
326 /* Load shift vector for this list */
327 i_shift_offset = DIM*shiftidx[iidx];
328 shX = shiftvec[i_shift_offset+XX];
329 shY = shiftvec[i_shift_offset+YY];
330 shZ = shiftvec[i_shift_offset+ZZ];
332 /* Load limits for loop over neighbors */
333 j_index_start = jindex[iidx];
334 j_index_end = jindex[iidx+1];
336 /* Get outer coordinate index */
338 i_coord_offset = DIM*inr;
340 /* Load i particle coords and add shift vector */
341 /* ## Loop over i particles, but only include ones that we use - skip e.g. vdw-only sites for elec-only kernel */
342 /* #for I in PARTICLES_I */
343 ix{I} = shX + x[i_coord_offset+DIM*{I}+XX];
344 iy{I} = shY + x[i_coord_offset+DIM*{I}+YY];
345 iz{I} = shZ + x[i_coord_offset+DIM*{I}+ZZ];
346 /* #define OUTERFLOPS OUTERFLOPS+3 */
349 /* #if 'Force' in KERNEL_VF */
350 /* #for I in PARTICLES_I */
357 /* ## For water we already preloaded parameters at the start of the kernel */
358 /* #if not 'Water' in GEOMETRY_I */
359 /* Load parameters for i particles */
360 /* #for I in PARTICLES_ELEC_I */
361 iq{I} = facel*charge[inr+{I}];
362 /* #define OUTERFLOPS OUTERFLOPS+1 */
363 /* #if KERNEL_ELEC=='GeneralizedBorn' */
364 isai{I} = invsqrta[inr+{I}];
367 /* #for I in PARTICLES_VDW_I */
368 vdwioffset{I} = {NVDWPARAM}*nvdwtype*vdwtype[inr+{I}];
372 /* #if 'Potential' in KERNEL_VF */
373 /* Reset potential sums */
374 /* #if KERNEL_ELEC != 'None' */
377 /* #if 'GeneralizedBorn' in KERNEL_ELEC */
380 /* #if KERNEL_VDW != 'None' */
384 /* #if 'GeneralizedBorn' in KERNEL_ELEC and 'Force' in KERNEL_VF */
388 /* Start inner kernel loop */
389 for(jidx=j_index_start; jidx<j_index_end; jidx++)
391 /* Get j neighbor index, and coordinate index */
393 j_coord_offset = DIM*jnr;
395 /* load j atom coordinates */
396 /* #for J in PARTICLES_J */
397 jx{J} = x[j_coord_offset+DIM*{J}+XX];
398 jy{J} = x[j_coord_offset+DIM*{J}+YY];
399 jz{J} = x[j_coord_offset+DIM*{J}+ZZ];
402 /* Calculate displacement vector */
403 /* #for I,J in PAIRS_IJ */
404 dx{I}{J} = ix{I} - jx{J};
405 dy{I}{J} = iy{I} - jy{J};
406 dz{I}{J} = iz{I} - jz{J};
407 /* #define INNERFLOPS INNERFLOPS+3 */
410 /* Calculate squared distance and things based on it */
411 /* #for I,J in PAIRS_IJ */
412 rsq{I}{J} = dx{I}{J}*dx{I}{J}+dy{I}{J}*dy{I}{J}+dz{I}{J}*dz{I}{J};
413 /* #define INNERFLOPS INNERFLOPS+5 */
416 /* #for I,J in PAIRS_IJ */
417 /* #if 'rinv' in INTERACTION_FLAGS[I][J] */
418 rinv{I}{J} = gmx_invsqrt(rsq{I}{J});
419 /* #define INNERFLOPS INNERFLOPS+5 */
423 /* #for I,J in PAIRS_IJ */
424 /* #if 'rinvsq' in INTERACTION_FLAGS[I][J] */
425 /* # if 'rinv' not in INTERACTION_FLAGS[I][J] */
426 rinvsq{I}{J} = 1.0/rsq{I}{J};
427 /* #define INNERFLOPS INNERFLOPS+4 */
429 rinvsq{I}{J} = rinv{I}{J}*rinv{I}{J};
430 /* #define INNERFLOPS INNERFLOPS+1 */
435 /* #if not 'Water' in GEOMETRY_J */
436 /* Load parameters for j particles */
437 /* #for J in PARTICLES_ELEC_J */
438 jq{J} = charge[jnr+{J}];
439 /* #if KERNEL_ELEC=='GeneralizedBorn' */
440 isaj{J} = invsqrta[jnr+{J}];
443 /* #for J in PARTICLES_VDW_J */
444 vdwjidx{J} = {NVDWPARAM}*vdwtype[jnr+{J}];
448 /* #for I,J in PAIRS_IJ */
450 /**************************
451 * CALCULATE INTERACTIONS *
452 **************************/
454 /* ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
455 /* #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
456 /* ## We always calculate rinv/rinvsq above to enable pipelineing in compilers (performance tested on x86) */
457 if (rsq{I}{J}<rcutoff2)
459 /* #if 0 ## this and the next two lines is a hack to maintain auto-indentation in template file */
464 /* #if 'r' in INTERACTION_FLAGS[I][J] */
465 r{I}{J} = rsq{I}{J}*rinv{I}{J};
466 /* #define INNERFLOPS INNERFLOPS+1 */
469 /* ## For water geometries we already loaded parameters at the start of the kernel */
470 /* #if not 'Water' in GEOMETRY_J */
471 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
472 qq{I}{J} = iq{I}*jq{J};
473 /* #define INNERFLOPS INNERFLOPS+1 */
475 /* #if 'vdw' in INTERACTION_FLAGS[I][J] */
476 /* #if KERNEL_VDW=='Buckingham' */
477 c6_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}];
478 cexp1_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}+1];
479 cexp2_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}+2];
480 /* #elif 'LJEwald' in KERNEL_VDW */
481 c6_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}];
482 c12_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}+1];
483 c6grid_{I}{J} = vdwgridparam[vdwioffset{I}+vdwjidx{J}];
485 c6_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}];
486 c12_{I}{J} = vdwparam[vdwioffset{I}+vdwjidx{J}+1];
491 /* #if 'table' in INTERACTION_FLAGS[I][J] */
492 /* Calculate table index by multiplying r with table scale and truncate to integer */
493 rt = r{I}{J}*vftabscale;
496 vfitab = {TABLE_NINTERACTIONS}*{TABLE_POINT_SIZE}*vfitab;
497 /* #define INNERFLOPS INNERFLOPS+2 */
500 /* ## ELECTROSTATIC INTERACTIONS */
501 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
503 /* #if KERNEL_ELEC=='Coulomb' */
505 /* COULOMB ELECTROSTATICS */
506 velec = qq{I}{J}*rinv{I}{J};
507 /* #define INNERFLOPS INNERFLOPS+1 */
508 /* #if 'Force' in KERNEL_VF */
509 felec = velec*rinvsq{I}{J};
510 /* #define INNERFLOPS INNERFLOPS+2 */
513 /* #elif KERNEL_ELEC=='ReactionField' */
515 /* REACTION-FIELD ELECTROSTATICS */
516 /* #if 'Potential' in KERNEL_VF */
517 velec = qq{I}{J}*(rinv{I}{J}+krf*rsq{I}{J}-crf);
518 /* #define INNERFLOPS INNERFLOPS+4 */
520 /* #if 'Force' in KERNEL_VF */
521 felec = qq{I}{J}*(rinv{I}{J}*rinvsq{I}{J}-krf2);
522 /* #define INNERFLOPS INNERFLOPS+3 */
525 /* #elif KERNEL_ELEC=='GeneralizedBorn' */
527 /* GENERALIZED BORN AND COULOMB ELECTROSTATICS */
528 isaprod = isai{I}*isaj{J};
529 gbqqfactor = isaprod*(-qq{I}{J})*gbinvepsdiff;
530 gbscale = isaprod*gbtabscale;
531 dvdaj = dvda[jnr+{J}];
532 /* #define INNERFLOPS INNERFLOPS+5 */
534 /* Calculate generalized born table index - this is a separate table from the normal one,
535 * but we use the same procedure by multiplying r with scale and truncating to integer.
537 rt = r{I}{J}*gbscale;
544 Geps = gbeps*gbtab[gbitab+2];
545 Heps2 = gbeps*gbeps*gbtab[gbitab+3];
549 /* #define INNERFLOPS INNERFLOPS+10 */
551 /* #if 'Force' in KERNEL_VF */
552 FF = Fp+Geps+2.0*Heps2;
553 fgb = gbqqfactor*FF*gbscale;
554 dvdatmp = -0.5*(vgb+fgb*r{I}{J});
555 dvdasum = dvdasum + dvdatmp;
556 dvda[jnr] = dvdaj+dvdatmp*isaj{J}*isaj{J};
557 /* #define INNERFLOPS INNERFLOPS+13 */
559 velec = qq{I}{J}*rinv{I}{J};
560 /* #define INNERFLOPS INNERFLOPS+1 */
561 /* #if 'Force' in KERNEL_VF */
562 felec = (velec*rinv{I}{J}-fgb)*rinv{I}{J};
563 /* #define INNERFLOPS INNERFLOPS+3 */
566 /* #elif KERNEL_ELEC=='Ewald' */
567 /* EWALD ELECTROSTATICS */
569 /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
570 ewrt = r{I}{J}*ewtabscale;
573 /* #define INNERFLOPS INNERFLOPS+2 */
574 /* #if 'Potential' in KERNEL_VF or KERNEL_MOD_ELEC=='PotentialSwitch' */
576 felec = ewtab[ewitab]+eweps*ewtab[ewitab+1];
577 /* #define INNERFLOPS INNERFLOPS+4 */
578 /* #if KERNEL_MOD_ELEC=='PotentialShift' */
579 velec = qq{I}{J}*((rinv{I}{J}-sh_ewald)-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
580 /* #define INNERFLOPS INNERFLOPS+7 */
582 velec = qq{I}{J}*(rinv{I}{J}-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
583 /* #define INNERFLOPS INNERFLOPS+6 */
585 /* #if 'Force' in KERNEL_VF */
586 felec = qq{I}{J}*rinv{I}{J}*(rinvsq{I}{J}-felec);
587 /* #define INNERFLOPS INNERFLOPS+3 */
589 /* #elif KERNEL_VF=='Force' */
590 felec = (1.0-eweps)*ewtab[ewitab]+eweps*ewtab[ewitab+1];
591 felec = qq{I}{J}*rinv{I}{J}*(rinvsq{I}{J}-felec);
592 /* #define INNERFLOPS INNERFLOPS+7 */
595 /* #elif KERNEL_ELEC=='CubicSplineTable' */
597 /* CUBIC SPLINE TABLE ELECTROSTATICS */
598 /* #if 'Potential' in KERNEL_VF */
602 Geps = vfeps*vftab[vfitab+2];
603 Heps2 = vfeps*vfeps*vftab[vfitab+3];
605 /* #define INNERFLOPS INNERFLOPS+5 */
606 /* #if 'Potential' in KERNEL_VF */
609 /* #define INNERFLOPS INNERFLOPS+3 */
611 /* #if 'Force' in KERNEL_VF */
612 FF = Fp+Geps+2.0*Heps2;
613 felec = -qq{I}{J}*FF*vftabscale*rinv{I}{J};
614 /* #define INNERFLOPS INNERFLOPS+7 */
617 /* ## End of check for electrostatics interaction forms */
619 /* ## END OF ELECTROSTATIC INTERACTION CHECK FOR PAIR I-J */
621 /* #if 'vdw' in INTERACTION_FLAGS[I][J] */
623 /* #if KERNEL_VDW=='LennardJones' */
625 /* LENNARD-JONES DISPERSION/REPULSION */
627 rinvsix = rinvsq{I}{J}*rinvsq{I}{J}*rinvsq{I}{J};
628 /* #define INNERFLOPS INNERFLOPS+2 */
629 /* #if 'Potential' in KERNEL_VF or KERNEL_MOD_VDW=='PotentialSwitch' */
630 vvdw6 = c6_{I}{J}*rinvsix;
631 vvdw12 = c12_{I}{J}*rinvsix*rinvsix;
632 /* #define INNERFLOPS INNERFLOPS+3 */
633 /* #if KERNEL_MOD_VDW=='PotentialShift' */
634 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);
635 /* #define INNERFLOPS INNERFLOPS+8 */
637 vvdw = vvdw12*(1.0/12.0) - vvdw6*(1.0/6.0);
638 /* #define INNERFLOPS INNERFLOPS+3 */
640 /* ## Check for force inside potential check, i.e. this means we already did the potential part */
641 /* #if 'Force' in KERNEL_VF */
642 fvdw = (vvdw12-vvdw6)*rinvsq{I}{J};
643 /* #define INNERFLOPS INNERFLOPS+2 */
645 /* #elif KERNEL_VF=='Force' */
646 /* ## Force-only LennardJones makes it possible to save 1 flop (they do add up...) */
647 fvdw = (c12_{I}{J}*rinvsix-c6_{I}{J})*rinvsix*rinvsq{I}{J};
648 /* #define INNERFLOPS INNERFLOPS+4 */
651 /* #elif KERNEL_VDW=='Buckingham' */
653 /* BUCKINGHAM DISPERSION/REPULSION */
654 rinvsix = rinvsq{I}{J}*rinvsq{I}{J}*rinvsq{I}{J};
655 vvdw6 = c6_{I}{J}*rinvsix;
656 br = cexp2_{I}{J}*r{I}{J};
657 vvdwexp = cexp1_{I}{J}*exp(-br);
658 /* ## Estimate exp() to 25 flops */
659 /* #define INNERFLOPS INNERFLOPS+31 */
660 /* #if 'Potential' in KERNEL_VF or KERNEL_MOD_VDW=='PotentialSwitch' */
661 /* #if KERNEL_MOD_VDW=='PotentialShift' */
662 vvdw = (vvdwexp-cexp1_{I}{J}*exp(-cexp2_{I}{J}*rvdw)) - (vvdw6 - c6_{I}{J}*sh_vdw_invrcut6)*(1.0/6.0);
663 /* #define INNERFLOPS INNERFLOPS+33 */
665 vvdw = vvdwexp - vvdw6*(1.0/6.0);
666 /* #define INNERFLOPS INNERFLOPS+2 */
669 /* #if 'Force' in KERNEL_VF */
670 fvdw = (br*vvdwexp-vvdw6)*rinvsq{I}{J};
671 /* #define INNERFLOPS INNERFLOPS+3 */
674 /* #elif KERNEL_VDW=='CubicSplineTable' */
676 /* CUBIC SPLINE TABLE DISPERSION */
677 vfitab += {TABLE_VDW_OFFSET};
678 /* #if 'Potential' in KERNEL_VF */
682 Geps = vfeps*vftab[vfitab+2];
683 Heps2 = vfeps*vfeps*vftab[vfitab+3];
685 /* #define INNERFLOPS INNERFLOPS+5 */
686 /* #if 'Potential' in KERNEL_VF */
688 vvdw6 = c6_{I}{J}*VV;
689 /* #define INNERFLOPS INNERFLOPS+3 */
691 /* #if 'Force' in KERNEL_VF */
692 FF = Fp+Geps+2.0*Heps2;
693 fvdw6 = c6_{I}{J}*FF;
694 /* #define INNERFLOPS INNERFLOPS+4 */
697 /* CUBIC SPLINE TABLE REPULSION */
698 /* #if 'Potential' in KERNEL_VF */
702 Geps = vfeps*vftab[vfitab+6];
703 Heps2 = vfeps*vfeps*vftab[vfitab+7];
705 /* #define INNERFLOPS INNERFLOPS+5 */
706 /* #if 'Potential' in KERNEL_VF */
708 vvdw12 = c12_{I}{J}*VV;
709 /* #define INNERFLOPS INNERFLOPS+3 */
711 /* #if 'Force' in KERNEL_VF */
712 FF = Fp+Geps+2.0*Heps2;
713 fvdw12 = c12_{I}{J}*FF;
714 /* #define INNERFLOPS INNERFLOPS+4 */
716 /* #if 'Potential' in KERNEL_VF */
718 /* #define INNERFLOPS INNERFLOPS+1 */
720 /* #if 'Force' in KERNEL_VF */
721 fvdw = -(fvdw6+fvdw12)*vftabscale*rinv{I}{J};
722 /* #define INNERFLOPS INNERFLOPS+4 */
725 /* #elif KERNEL_VDW=='LJEwald' */
727 rinvsix = rinvsq{I}{J}*rinvsq{I}{J}*rinvsq{I}{J};
728 ewcljrsq = ewclj2*rsq{I}{J};
729 exponent = exp(-ewcljrsq);
730 poly = exponent*(1.0 + ewcljrsq + ewcljrsq*ewcljrsq*0.5);
731 /* #define INNERFLOPS INNERFLOPS+9 */
732 /* #if 'Potential' in KERNEL_VF or KERNEL_MOD_VDW=='PotentialSwitch' */
733 vvdw6 = (c6_{I}{J}-c6grid_{I}{J}*(1.0-poly))*rinvsix;
734 vvdw12 = c12_{I}{J}*rinvsix*rinvsix;
735 /* #define INNERFLOPS INNERFLOPS+6 */
736 /* #if KERNEL_MOD_VDW=='PotentialShift' */
737 vvdw = (vvdw12 - c12_{I}{J}*sh_vdw_invrcut6*sh_vdw_invrcut6)*(1.0/12.0) - (vvdw6 - c6_{I}{J}*sh_vdw_invrcut6 - c6grid_{I}{J}*sh_lj_ewald)*(1.0/6.0);
738 /* #define INNERFLOPS INNERFLOPS+9 */
740 vvdw = vvdw12*(1.0/12.0) - vvdw6*(1.0/6.0);
741 /* #define INNERFLOPS INNERFLOPS+3 */
743 /* ## Check for force inside potential check, i.e. this means we already did the potential part */
744 /* #if 'Force' in KERNEL_VF */
745 fvdw = (vvdw12 - vvdw6 - c6grid_{I}{J}*(1.0/6.0)*exponent*ewclj6)*rinvsq{I}{J};
746 /* #define INNERFLOPS INNERFLOPS+6 */
748 /* #elif KERNEL_VF=='Force' */
749 /* ## Force-only LennardJones makes it possible to save 1 flop (they do add up...) */
750 fvdw = (((c12_{I}{J}*rinvsix - c6_{I}{J} + c6grid_{I}{J}*(1.0-poly))*rinvsix) - c6grid_{I}{J}*(1.0/6.0)*exponent*ewclj6)*rinvsq{I}{J};
751 /* #define INNERFLOPS INNERFLOPS+11 */
754 /* ## End of check for vdw interaction forms */
756 /* ## END OF VDW INTERACTION CHECK FOR PAIR I-J */
758 /* #if 'switch' in INTERACTION_FLAGS[I][J] */
760 d = (d>0.0) ? d : 0.0;
762 sw = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
763 /* #define INNERFLOPS INNERFLOPS+9 */
765 /* #if 'Force' in KERNEL_VF */
766 dsw = d2*(swF2+d*(swF3+d*swF4));
767 /* #define INNERFLOPS INNERFLOPS+5 */
770 /* Evaluate switch function */
771 /* #if 'Force' in KERNEL_VF */
772 /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
773 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_ELEC=='PotentialSwitch' */
774 felec = felec*sw - rinv{I}{J}*velec*dsw;
775 /* #define INNERFLOPS INNERFLOPS+3 */
777 /* #if 'vdw' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_VDW=='PotentialSwitch' */
778 fvdw = fvdw*sw - rinv{I}{J}*vvdw*dsw;
779 /* #define INNERFLOPS INNERFLOPS+3 */
782 /* #if 'Potential' in KERNEL_VF */
783 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_ELEC=='PotentialSwitch' */
785 /* #define INNERFLOPS INNERFLOPS+1 */
787 /* #if 'vdw' in INTERACTION_FLAGS[I][J] and KERNEL_MOD_VDW=='PotentialSwitch' */
789 /* #define INNERFLOPS INNERFLOPS+1 */
794 /* #if 'Potential' in KERNEL_VF */
795 /* Update potential sums from outer loop */
796 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] */
798 /* #define INNERFLOPS INNERFLOPS+1 */
799 /* #if KERNEL_ELEC=='GeneralizedBorn' */
801 /* #define INNERFLOPS INNERFLOPS+1 */
804 /* #if 'vdw' in INTERACTION_FLAGS[I][J] */
806 /* #define INNERFLOPS INNERFLOPS+1 */
810 /* #if 'Force' in KERNEL_VF */
812 /* #if 'electrostatics' in INTERACTION_FLAGS[I][J] and 'vdw' in INTERACTION_FLAGS[I][J] */
814 /* #define INNERFLOPS INNERFLOPS+1 */
815 /* #elif 'electrostatics' in INTERACTION_FLAGS[I][J] */
817 /* #elif 'vdw' in INTERACTION_FLAGS[I][J] */
821 /* Calculate temporary vectorial force */
826 /* Update vectorial force */
830 f[j_coord_offset+DIM*{J}+XX] -= tx;
831 f[j_coord_offset+DIM*{J}+YY] -= ty;
832 f[j_coord_offset+DIM*{J}+ZZ] -= tz;
834 /* #define INNERFLOPS INNERFLOPS+9 */
837 /* ## Note special check for TIP4P-TIP4P. Since we are cutting of all hydrogen interactions we also cut the LJ-only O-O interaction */
838 /* #if 'exactcutoff' in INTERACTION_FLAGS[I][J] or (GEOMETRY_I=='Water4' and GEOMETRY_J=='Water4' and 'exactcutoff' in INTERACTION_FLAGS[1][1]) */
839 /* #if 0 ## This and next two lines is a hack to maintain indentation in template file */
844 /* ## End of check for the interaction being outside the cutoff */
847 /* ## End of loop over i-j interaction pairs */
849 /* Inner loop uses {INNERFLOPS} flops */
851 /* End of innermost loop */
853 /* #if 'Force' in KERNEL_VF */
855 /* #for I in PARTICLES_I */
856 f[i_coord_offset+DIM*{I}+XX] += fix{I};
857 f[i_coord_offset+DIM*{I}+YY] += fiy{I};
858 f[i_coord_offset+DIM*{I}+ZZ] += fiz{I};
862 /* #define OUTERFLOPS OUTERFLOPS+6 */
864 fshift[i_shift_offset+XX] += tx;
865 fshift[i_shift_offset+YY] += ty;
866 fshift[i_shift_offset+ZZ] += tz;
867 /* #define OUTERFLOPS OUTERFLOPS+3 */
870 /* #if 'Potential' in KERNEL_VF */
872 /* Update potential energies */
873 /* #if KERNEL_ELEC != 'None' */
874 kernel_data->energygrp_elec[ggid] += velecsum;
875 /* #define OUTERFLOPS OUTERFLOPS+1 */
877 /* #if 'GeneralizedBorn' in KERNEL_ELEC */
878 kernel_data->energygrp_polarization[ggid] += vgbsum;
879 /* #define OUTERFLOPS OUTERFLOPS+1 */
881 /* #if KERNEL_VDW != 'None' */
882 kernel_data->energygrp_vdw[ggid] += vvdwsum;
883 /* #define OUTERFLOPS OUTERFLOPS+1 */
886 /* #if 'GeneralizedBorn' in KERNEL_ELEC and 'Force' in KERNEL_VF */
887 dvda[inr] = dvda[inr] + dvdasum*isai{I}*isai{I};
890 /* Increment number of inner iterations */
891 inneriter += j_index_end - j_index_start;
893 /* Outer loop uses {OUTERFLOPS} flops */
896 /* Increment number of outer iterations */
899 /* Update outer/inner flops */
900 /* ## NB: This is not important, it just affects the flopcount. However, since our preprocessor is */
901 /* ## primitive and replaces aggressively even in strings inside these directives, we need to */
902 /* ## assemble the main part of the name (containing KERNEL/ELEC/VDW) directly in the source. */
903 /* #if GEOMETRY_I == 'Water3' */
904 /* #define ISUFFIX '_W3' */
905 /* #elif GEOMETRY_I == 'Water4' */
906 /* #define ISUFFIX '_W4' */
908 /* #define ISUFFIX '' */
910 /* #if GEOMETRY_J == 'Water3' */
911 /* #define JSUFFIX 'W3' */
912 /* #elif GEOMETRY_J == 'Water4' */
913 /* #define JSUFFIX 'W4' */
915 /* #define JSUFFIX '' */
917 /* #if 'PotentialAndForce' in KERNEL_VF */
918 /* #define VFSUFFIX '_VF' */
919 /* #elif 'Potential' in KERNEL_VF */
920 /* #define VFSUFFIX '_V' */
922 /* #define VFSUFFIX '_F' */
925 /* #if KERNEL_ELEC != 'None' and KERNEL_VDW != 'None' */
926 inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
927 /* #elif KERNEL_ELEC != 'None' */
928 inc_nrnb(nrnb,eNR_NBKERNEL_ELEC{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});
930 inc_nrnb(nrnb,eNR_NBKERNEL_VDW{ISUFFIX}{JSUFFIX}{VFSUFFIX},outeriter*{OUTERFLOPS} + inneriter*{INNERFLOPS});