95cfb023a6b1abeecd00aa482bbd0d6ad9810663
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_kernel_c / nb_kernel_ElecEwSw_VdwLJSw_GeomP1P1_c.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*
36  * Note: this file was generated by the GROMACS c kernel generator.
37  */
38 #include "config.h"
39
40 #include <math.h>
41
42 #include "../nb_kernel.h"
43 #include "gromacs/legacyheaders/types/simple.h"
44 #include "gromacs/math/vec.h"
45 #include "gromacs/legacyheaders/nrnb.h"
46
47 /*
48  * Gromacs nonbonded kernel:   nb_kernel_ElecEwSw_VdwLJSw_GeomP1P1_VF_c
49  * Electrostatics interaction: Ewald
50  * VdW interaction:            LennardJones
51  * Geometry:                   Particle-Particle
52  * Calculate force/pot:        PotentialAndForce
53  */
54 void
55 nb_kernel_ElecEwSw_VdwLJSw_GeomP1P1_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              vdwjidx0;
73     real             jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
74     real             dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00,cexp1_00,cexp2_00;
75     real             velec,felec,velecsum,facel,crf,krf,krf2;
76     real             *charge;
77     int              nvdwtype;
78     real             rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,br,vvdwexp,sh_vdw_invrcut6;
79     int              *vdwtype;
80     real             *vdwparam;
81     int              ewitab;
82     real             ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace;
83     real             *ewtab;
84     real             rswitch,swV3,swV4,swV5,swF2,swF3,swF4,d,d2,sw,dsw;
85
86     x                = xx[0];
87     f                = ff[0];
88
89     nri              = nlist->nri;
90     iinr             = nlist->iinr;
91     jindex           = nlist->jindex;
92     jjnr             = nlist->jjnr;
93     shiftidx         = nlist->shift;
94     gid              = nlist->gid;
95     shiftvec         = fr->shift_vec[0];
96     fshift           = fr->fshift[0];
97     facel            = fr->epsfac;
98     charge           = mdatoms->chargeA;
99     nvdwtype         = fr->ntype;
100     vdwparam         = fr->nbfp;
101     vdwtype          = mdatoms->typeA;
102
103     sh_ewald         = fr->ic->sh_ewald;
104     ewtab            = fr->ic->tabq_coul_FDV0;
105     ewtabscale       = fr->ic->tabq_scale;
106     ewtabhalfspace   = 0.5/ewtabscale;
107
108     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
109     rcutoff          = fr->rcoulomb;
110     rcutoff2         = rcutoff*rcutoff;
111
112     rswitch          = fr->rcoulomb_switch;
113     /* Setup switch parameters */
114     d                = rcutoff-rswitch;
115     swV3             = -10.0/(d*d*d);
116     swV4             =  15.0/(d*d*d*d);
117     swV5             =  -6.0/(d*d*d*d*d);
118     swF2             = -30.0/(d*d*d);
119     swF3             =  60.0/(d*d*d*d);
120     swF4             = -30.0/(d*d*d*d*d);
121
122     outeriter        = 0;
123     inneriter        = 0;
124
125     /* Start outer loop over neighborlists */
126     for(iidx=0; iidx<nri; iidx++)
127     {
128         /* Load shift vector for this list */
129         i_shift_offset   = DIM*shiftidx[iidx];
130         shX              = shiftvec[i_shift_offset+XX];
131         shY              = shiftvec[i_shift_offset+YY];
132         shZ              = shiftvec[i_shift_offset+ZZ];
133
134         /* Load limits for loop over neighbors */
135         j_index_start    = jindex[iidx];
136         j_index_end      = jindex[iidx+1];
137
138         /* Get outer coordinate index */
139         inr              = iinr[iidx];
140         i_coord_offset   = DIM*inr;
141
142         /* Load i particle coords and add shift vector */
143         ix0              = shX + x[i_coord_offset+DIM*0+XX];
144         iy0              = shY + x[i_coord_offset+DIM*0+YY];
145         iz0              = shZ + x[i_coord_offset+DIM*0+ZZ];
146
147         fix0             = 0.0;
148         fiy0             = 0.0;
149         fiz0             = 0.0;
150
151         /* Load parameters for i particles */
152         iq0              = facel*charge[inr+0];
153         vdwioffset0      = 2*nvdwtype*vdwtype[inr+0];
154
155         /* Reset potential sums */
156         velecsum         = 0.0;
157         vvdwsum          = 0.0;
158
159         /* Start inner kernel loop */
160         for(jidx=j_index_start; jidx<j_index_end; jidx++)
161         {
162             /* Get j neighbor index, and coordinate index */
163             jnr              = jjnr[jidx];
164             j_coord_offset   = DIM*jnr;
165
166             /* load j atom coordinates */
167             jx0              = x[j_coord_offset+DIM*0+XX];
168             jy0              = x[j_coord_offset+DIM*0+YY];
169             jz0              = x[j_coord_offset+DIM*0+ZZ];
170
171             /* Calculate displacement vector */
172             dx00             = ix0 - jx0;
173             dy00             = iy0 - jy0;
174             dz00             = iz0 - jz0;
175
176             /* Calculate squared distance and things based on it */
177             rsq00            = dx00*dx00+dy00*dy00+dz00*dz00;
178
179             rinv00           = gmx_invsqrt(rsq00);
180
181             rinvsq00         = rinv00*rinv00;
182
183             /* Load parameters for j particles */
184             jq0              = charge[jnr+0];
185             vdwjidx0         = 2*vdwtype[jnr+0];
186
187             /**************************
188              * CALCULATE INTERACTIONS *
189              **************************/
190
191             if (rsq00<rcutoff2)
192             {
193
194             r00              = rsq00*rinv00;
195
196             qq00             = iq0*jq0;
197             c6_00            = vdwparam[vdwioffset0+vdwjidx0];
198             c12_00           = vdwparam[vdwioffset0+vdwjidx0+1];
199
200             /* EWALD ELECTROSTATICS */
201
202             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
203             ewrt             = r00*ewtabscale;
204             ewitab           = ewrt;
205             eweps            = ewrt-ewitab;
206             ewitab           = 4*ewitab;
207             felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
208             velec            = qq00*(rinv00-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
209             felec            = qq00*rinv00*(rinvsq00-felec);
210
211             /* LENNARD-JONES DISPERSION/REPULSION */
212
213             rinvsix          = rinvsq00*rinvsq00*rinvsq00;
214             vvdw6            = c6_00*rinvsix;
215             vvdw12           = c12_00*rinvsix*rinvsix;
216             vvdw             = vvdw12*(1.0/12.0) - vvdw6*(1.0/6.0);
217             fvdw             = (vvdw12-vvdw6)*rinvsq00;
218
219             d                = r00-rswitch;
220             d                = (d>0.0) ? d : 0.0;
221             d2               = d*d;
222             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
223
224             dsw              = d2*(swF2+d*(swF3+d*swF4));
225
226             /* Evaluate switch function */
227             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
228             felec            = felec*sw - rinv00*velec*dsw;
229             fvdw             = fvdw*sw - rinv00*vvdw*dsw;
230             velec           *= sw;
231             vvdw            *= sw;
232
233             /* Update potential sums from outer loop */
234             velecsum        += velec;
235             vvdwsum         += vvdw;
236
237             fscal            = felec+fvdw;
238
239             /* Calculate temporary vectorial force */
240             tx               = fscal*dx00;
241             ty               = fscal*dy00;
242             tz               = fscal*dz00;
243
244             /* Update vectorial force */
245             fix0            += tx;
246             fiy0            += ty;
247             fiz0            += tz;
248             f[j_coord_offset+DIM*0+XX] -= tx;
249             f[j_coord_offset+DIM*0+YY] -= ty;
250             f[j_coord_offset+DIM*0+ZZ] -= tz;
251
252             }
253
254             /* Inner loop uses 75 flops */
255         }
256         /* End of innermost loop */
257
258         tx = ty = tz = 0;
259         f[i_coord_offset+DIM*0+XX] += fix0;
260         f[i_coord_offset+DIM*0+YY] += fiy0;
261         f[i_coord_offset+DIM*0+ZZ] += fiz0;
262         tx                         += fix0;
263         ty                         += fiy0;
264         tz                         += fiz0;
265         fshift[i_shift_offset+XX]  += tx;
266         fshift[i_shift_offset+YY]  += ty;
267         fshift[i_shift_offset+ZZ]  += tz;
268
269         ggid                        = gid[iidx];
270         /* Update potential energies */
271         kernel_data->energygrp_elec[ggid] += velecsum;
272         kernel_data->energygrp_vdw[ggid] += vvdwsum;
273
274         /* Increment number of inner iterations */
275         inneriter                  += j_index_end - j_index_start;
276
277         /* Outer loop uses 15 flops */
278     }
279
280     /* Increment number of outer iterations */
281     outeriter        += nri;
282
283     /* Update outer/inner flops */
284
285     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_VF,outeriter*15 + inneriter*75);
286 }
287 /*
288  * Gromacs nonbonded kernel:   nb_kernel_ElecEwSw_VdwLJSw_GeomP1P1_F_c
289  * Electrostatics interaction: Ewald
290  * VdW interaction:            LennardJones
291  * Geometry:                   Particle-Particle
292  * Calculate force/pot:        Force
293  */
294 void
295 nb_kernel_ElecEwSw_VdwLJSw_GeomP1P1_F_c
296                     (t_nblist                    * gmx_restrict       nlist,
297                      rvec                        * gmx_restrict          xx,
298                      rvec                        * gmx_restrict          ff,
299                      t_forcerec                  * gmx_restrict          fr,
300                      t_mdatoms                   * gmx_restrict     mdatoms,
301                      nb_kernel_data_t gmx_unused * gmx_restrict kernel_data,
302                      t_nrnb                      * gmx_restrict        nrnb)
303 {
304     int              i_shift_offset,i_coord_offset,j_coord_offset;
305     int              j_index_start,j_index_end;
306     int              nri,inr,ggid,iidx,jidx,jnr,outeriter,inneriter;
307     real             shX,shY,shZ,tx,ty,tz,fscal,rcutoff,rcutoff2;
308     int              *iinr,*jindex,*jjnr,*shiftidx,*gid;
309     real             *shiftvec,*fshift,*x,*f;
310     int              vdwioffset0;
311     real             ix0,iy0,iz0,fix0,fiy0,fiz0,iq0,isai0;
312     int              vdwjidx0;
313     real             jx0,jy0,jz0,fjx0,fjy0,fjz0,jq0,isaj0;
314     real             dx00,dy00,dz00,rsq00,rinv00,rinvsq00,r00,qq00,c6_00,c12_00,cexp1_00,cexp2_00;
315     real             velec,felec,velecsum,facel,crf,krf,krf2;
316     real             *charge;
317     int              nvdwtype;
318     real             rinvsix,rvdw,vvdw,vvdw6,vvdw12,fvdw,fvdw6,fvdw12,vvdwsum,br,vvdwexp,sh_vdw_invrcut6;
319     int              *vdwtype;
320     real             *vdwparam;
321     int              ewitab;
322     real             ewtabscale,eweps,sh_ewald,ewrt,ewtabhalfspace;
323     real             *ewtab;
324     real             rswitch,swV3,swV4,swV5,swF2,swF3,swF4,d,d2,sw,dsw;
325
326     x                = xx[0];
327     f                = ff[0];
328
329     nri              = nlist->nri;
330     iinr             = nlist->iinr;
331     jindex           = nlist->jindex;
332     jjnr             = nlist->jjnr;
333     shiftidx         = nlist->shift;
334     gid              = nlist->gid;
335     shiftvec         = fr->shift_vec[0];
336     fshift           = fr->fshift[0];
337     facel            = fr->epsfac;
338     charge           = mdatoms->chargeA;
339     nvdwtype         = fr->ntype;
340     vdwparam         = fr->nbfp;
341     vdwtype          = mdatoms->typeA;
342
343     sh_ewald         = fr->ic->sh_ewald;
344     ewtab            = fr->ic->tabq_coul_FDV0;
345     ewtabscale       = fr->ic->tabq_scale;
346     ewtabhalfspace   = 0.5/ewtabscale;
347
348     /* When we use explicit cutoffs the value must be identical for elec and VdW, so use elec as an arbitrary choice */
349     rcutoff          = fr->rcoulomb;
350     rcutoff2         = rcutoff*rcutoff;
351
352     rswitch          = fr->rcoulomb_switch;
353     /* Setup switch parameters */
354     d                = rcutoff-rswitch;
355     swV3             = -10.0/(d*d*d);
356     swV4             =  15.0/(d*d*d*d);
357     swV5             =  -6.0/(d*d*d*d*d);
358     swF2             = -30.0/(d*d*d);
359     swF3             =  60.0/(d*d*d*d);
360     swF4             = -30.0/(d*d*d*d*d);
361
362     outeriter        = 0;
363     inneriter        = 0;
364
365     /* Start outer loop over neighborlists */
366     for(iidx=0; iidx<nri; iidx++)
367     {
368         /* Load shift vector for this list */
369         i_shift_offset   = DIM*shiftidx[iidx];
370         shX              = shiftvec[i_shift_offset+XX];
371         shY              = shiftvec[i_shift_offset+YY];
372         shZ              = shiftvec[i_shift_offset+ZZ];
373
374         /* Load limits for loop over neighbors */
375         j_index_start    = jindex[iidx];
376         j_index_end      = jindex[iidx+1];
377
378         /* Get outer coordinate index */
379         inr              = iinr[iidx];
380         i_coord_offset   = DIM*inr;
381
382         /* Load i particle coords and add shift vector */
383         ix0              = shX + x[i_coord_offset+DIM*0+XX];
384         iy0              = shY + x[i_coord_offset+DIM*0+YY];
385         iz0              = shZ + x[i_coord_offset+DIM*0+ZZ];
386
387         fix0             = 0.0;
388         fiy0             = 0.0;
389         fiz0             = 0.0;
390
391         /* Load parameters for i particles */
392         iq0              = facel*charge[inr+0];
393         vdwioffset0      = 2*nvdwtype*vdwtype[inr+0];
394
395         /* Start inner kernel loop */
396         for(jidx=j_index_start; jidx<j_index_end; jidx++)
397         {
398             /* Get j neighbor index, and coordinate index */
399             jnr              = jjnr[jidx];
400             j_coord_offset   = DIM*jnr;
401
402             /* load j atom coordinates */
403             jx0              = x[j_coord_offset+DIM*0+XX];
404             jy0              = x[j_coord_offset+DIM*0+YY];
405             jz0              = x[j_coord_offset+DIM*0+ZZ];
406
407             /* Calculate displacement vector */
408             dx00             = ix0 - jx0;
409             dy00             = iy0 - jy0;
410             dz00             = iz0 - jz0;
411
412             /* Calculate squared distance and things based on it */
413             rsq00            = dx00*dx00+dy00*dy00+dz00*dz00;
414
415             rinv00           = gmx_invsqrt(rsq00);
416
417             rinvsq00         = rinv00*rinv00;
418
419             /* Load parameters for j particles */
420             jq0              = charge[jnr+0];
421             vdwjidx0         = 2*vdwtype[jnr+0];
422
423             /**************************
424              * CALCULATE INTERACTIONS *
425              **************************/
426
427             if (rsq00<rcutoff2)
428             {
429
430             r00              = rsq00*rinv00;
431
432             qq00             = iq0*jq0;
433             c6_00            = vdwparam[vdwioffset0+vdwjidx0];
434             c12_00           = vdwparam[vdwioffset0+vdwjidx0+1];
435
436             /* EWALD ELECTROSTATICS */
437
438             /* Calculate Ewald table index by multiplying r with scale and truncate to integer */
439             ewrt             = r00*ewtabscale;
440             ewitab           = ewrt;
441             eweps            = ewrt-ewitab;
442             ewitab           = 4*ewitab;
443             felec            = ewtab[ewitab]+eweps*ewtab[ewitab+1];
444             velec            = qq00*(rinv00-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+felec)));
445             felec            = qq00*rinv00*(rinvsq00-felec);
446
447             /* LENNARD-JONES DISPERSION/REPULSION */
448
449             rinvsix          = rinvsq00*rinvsq00*rinvsq00;
450             vvdw6            = c6_00*rinvsix;
451             vvdw12           = c12_00*rinvsix*rinvsix;
452             vvdw             = vvdw12*(1.0/12.0) - vvdw6*(1.0/6.0);
453             fvdw             = (vvdw12-vvdw6)*rinvsq00;
454
455             d                = r00-rswitch;
456             d                = (d>0.0) ? d : 0.0;
457             d2               = d*d;
458             sw               = 1.0+d2*d*(swV3+d*(swV4+d*swV5));
459
460             dsw              = d2*(swF2+d*(swF3+d*swF4));
461
462             /* Evaluate switch function */
463             /* fscal'=f'/r=-(v*sw)'/r=-(v'*sw+v*dsw)/r=-v'*sw/r-v*dsw/r=fscal*sw-v*dsw/r */
464             felec            = felec*sw - rinv00*velec*dsw;
465             fvdw             = fvdw*sw - rinv00*vvdw*dsw;
466
467             fscal            = felec+fvdw;
468
469             /* Calculate temporary vectorial force */
470             tx               = fscal*dx00;
471             ty               = fscal*dy00;
472             tz               = fscal*dz00;
473
474             /* Update vectorial force */
475             fix0            += tx;
476             fiy0            += ty;
477             fiz0            += tz;
478             f[j_coord_offset+DIM*0+XX] -= tx;
479             f[j_coord_offset+DIM*0+YY] -= ty;
480             f[j_coord_offset+DIM*0+ZZ] -= tz;
481
482             }
483
484             /* Inner loop uses 71 flops */
485         }
486         /* End of innermost loop */
487
488         tx = ty = tz = 0;
489         f[i_coord_offset+DIM*0+XX] += fix0;
490         f[i_coord_offset+DIM*0+YY] += fiy0;
491         f[i_coord_offset+DIM*0+ZZ] += fiz0;
492         tx                         += fix0;
493         ty                         += fiy0;
494         tz                         += fiz0;
495         fshift[i_shift_offset+XX]  += tx;
496         fshift[i_shift_offset+YY]  += ty;
497         fshift[i_shift_offset+ZZ]  += tz;
498
499         /* Increment number of inner iterations */
500         inneriter                  += j_index_end - j_index_start;
501
502         /* Outer loop uses 13 flops */
503     }
504
505     /* Increment number of outer iterations */
506     outeriter        += nri;
507
508     /* Update outer/inner flops */
509
510     inc_nrnb(nrnb,eNR_NBKERNEL_ELEC_VDW_F,outeriter*13 + inneriter*71);
511 }