fc6ca9bc67dad6cccce09f9ac0bbd7da00f6cb7c
[alexxy/gromacs.git] / src / gromacs / gmxlib / nonbonded / nb_free_energy.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include "config.h"
40
41 #include <math.h>
42
43 #include "gromacs/math/vec.h"
44 #include "gromacs/legacyheaders/typedefs.h"
45 #include "gromacs/legacyheaders/nonbonded.h"
46 #include "nb_kernel.h"
47 #include "gromacs/legacyheaders/nrnb.h"
48 #include "gromacs/legacyheaders/macros.h"
49 #include "nb_free_energy.h"
50
51 #include "gromacs/utility/fatalerror.h"
52
53 void
54 gmx_nb_free_energy_kernel(const t_nblist * gmx_restrict    nlist,
55                           rvec * gmx_restrict              xx,
56                           rvec * gmx_restrict              ff,
57                           t_forcerec * gmx_restrict        fr,
58                           const t_mdatoms * gmx_restrict   mdatoms,
59                           nb_kernel_data_t * gmx_restrict  kernel_data,
60                           t_nrnb * gmx_restrict            nrnb)
61 {
62
63 #define  STATE_A  0
64 #define  STATE_B  1
65 #define  NSTATES  2
66     int           i, j, n, ii, is3, ii3, k, nj0, nj1, jnr, j3, ggid;
67     real          shX, shY, shZ;
68     real          tx, ty, tz, Fscal;
69     double        FscalC[NSTATES], FscalV[NSTATES];  /* Needs double for sc_power==48 */
70     double        Vcoul[NSTATES], Vvdw[NSTATES];     /* Needs double for sc_power==48 */
71     real          rinv6, r, rt, rtC, rtV;
72     real          iqA, iqB;
73     real          qq[NSTATES], vctot, krsq;
74     int           ntiA, ntiB, tj[NSTATES];
75     real          Vvdw6, Vvdw12, vvtot;
76     real          ix, iy, iz, fix, fiy, fiz;
77     real          dx, dy, dz, rsq, rinv;
78     real          c6[NSTATES], c12[NSTATES], c6grid;
79     real          LFC[NSTATES], LFV[NSTATES], DLF[NSTATES];
80     double        dvdl_coul, dvdl_vdw;
81     real          lfac_coul[NSTATES], dlfac_coul[NSTATES], lfac_vdw[NSTATES], dlfac_vdw[NSTATES];
82     real          sigma6[NSTATES], alpha_vdw_eff, alpha_coul_eff, sigma2_def, sigma2_min;
83     double        rp, rpm2, rC, rV, rinvC, rpinvC, rinvV, rpinvV; /* Needs double for sc_power==48 */
84     real          sigma2[NSTATES], sigma_pow[NSTATES], sigma_powm2[NSTATES], rs, rs2;
85     int           do_tab, tab_elemsize;
86     int           n0, n1C, n1V, nnn;
87     real          Y, F, G, H, Fp, Geps, Heps2, epsC, eps2C, epsV, eps2V, VV, FF;
88     int           icoul, ivdw;
89     int           nri;
90     const int *   iinr;
91     const int *   jindex;
92     const int *   jjnr;
93     const int *   shift;
94     const int *   gid;
95     const int *   typeA;
96     const int *   typeB;
97     int           ntype;
98     const real *  shiftvec;
99     real          dvdl_part;
100     real *        fshift;
101     real          tabscale = 0;
102     const real *  VFtab    = NULL;
103     const real *  x;
104     real *        f;
105     real          facel, krf, crf;
106     const real *  chargeA;
107     const real *  chargeB;
108     real          sigma6_min, sigma6_def, lam_power, sc_power, sc_r_power;
109     real          alpha_coul, alpha_vdw, lambda_coul, lambda_vdw, ewc_lj;
110     real          ewcljrsq, ewclj, ewclj2, exponent, poly, vvdw_disp, vvdw_rep, sh_lj_ewald;
111     real          ewclj6;
112     const real *  nbfp, *nbfp_grid;
113     real *        dvdl;
114     real *        Vv;
115     real *        Vc;
116     gmx_bool      bDoForces, bDoShiftForces, bDoPotential;
117     real          rcoulomb, rvdw, sh_invrc6;
118     gmx_bool      bExactElecCutoff, bExactVdwCutoff, bExactCutoffAll;
119     gmx_bool      bEwald, bEwaldLJ;
120     real          rcutoff_max2;
121     const real *  tab_ewald_F_lj;
122     const real *  tab_ewald_V_lj;
123     real          d, d2, sw, dsw, rinvcorr;
124     real          elec_swV3, elec_swV4, elec_swV5, elec_swF2, elec_swF3, elec_swF4;
125     real          vdw_swV3, vdw_swV4, vdw_swV5, vdw_swF2, vdw_swF3, vdw_swF4;
126     gmx_bool      bConvertEwaldToCoulomb, bConvertLJEwaldToLJ6;
127     gmx_bool      bComputeVdwInteraction, bComputeElecInteraction;
128     const real *  ewtab;
129     int           ewitab;
130     real          ewrt, eweps, ewtabscale, ewtabhalfspace, sh_ewald;
131
132     sh_ewald            = fr->ic->sh_ewald;
133     ewtab               = fr->ic->tabq_coul_FDV0;
134     ewtabscale          = fr->ic->tabq_scale;
135     ewtabhalfspace      = 0.5/ewtabscale;
136     tab_ewald_F_lj      = fr->ic->tabq_vdw_F;
137     tab_ewald_V_lj      = fr->ic->tabq_vdw_V;
138
139     x                   = xx[0];
140     f                   = ff[0];
141
142     fshift              = fr->fshift[0];
143
144     nri                 = nlist->nri;
145     iinr                = nlist->iinr;
146     jindex              = nlist->jindex;
147     jjnr                = nlist->jjnr;
148     icoul               = nlist->ielec;
149     ivdw                = nlist->ivdw;
150     shift               = nlist->shift;
151     gid                 = nlist->gid;
152
153     shiftvec            = fr->shift_vec[0];
154     chargeA             = mdatoms->chargeA;
155     chargeB             = mdatoms->chargeB;
156     facel               = fr->epsfac;
157     krf                 = fr->k_rf;
158     crf                 = fr->c_rf;
159     ewc_lj              = fr->ewaldcoeff_lj;
160     Vc                  = kernel_data->energygrp_elec;
161     typeA               = mdatoms->typeA;
162     typeB               = mdatoms->typeB;
163     ntype               = fr->ntype;
164     nbfp                = fr->nbfp;
165     nbfp_grid           = fr->ljpme_c6grid;
166     Vv                  = kernel_data->energygrp_vdw;
167     lambda_coul         = kernel_data->lambda[efptCOUL];
168     lambda_vdw          = kernel_data->lambda[efptVDW];
169     dvdl                = kernel_data->dvdl;
170     alpha_coul          = fr->sc_alphacoul;
171     alpha_vdw           = fr->sc_alphavdw;
172     lam_power           = fr->sc_power;
173     sc_r_power          = fr->sc_r_power;
174     sigma6_def          = fr->sc_sigma6_def;
175     sigma6_min          = fr->sc_sigma6_min;
176     bDoForces           = kernel_data->flags & GMX_NONBONDED_DO_FORCE;
177     bDoShiftForces      = kernel_data->flags & GMX_NONBONDED_DO_SHIFTFORCE;
178     bDoPotential        = kernel_data->flags & GMX_NONBONDED_DO_POTENTIAL;
179
180     rcoulomb            = fr->rcoulomb;
181     rvdw                = fr->rvdw;
182     sh_invrc6           = fr->ic->sh_invrc6;
183     sh_lj_ewald         = fr->ic->sh_lj_ewald;
184     ewclj               = fr->ewaldcoeff_lj;
185     ewclj2              = ewclj*ewclj;
186     ewclj6              = ewclj2*ewclj2*ewclj2;
187
188     if (fr->coulomb_modifier == eintmodPOTSWITCH)
189     {
190         d               = fr->rcoulomb-fr->rcoulomb_switch;
191         elec_swV3       = -10.0/(d*d*d);
192         elec_swV4       =  15.0/(d*d*d*d);
193         elec_swV5       =  -6.0/(d*d*d*d*d);
194         elec_swF2       = -30.0/(d*d*d);
195         elec_swF3       =  60.0/(d*d*d*d);
196         elec_swF4       = -30.0/(d*d*d*d*d);
197     }
198     else
199     {
200         /* Avoid warnings from stupid compilers (looking at you, Clang!) */
201         elec_swV3 = elec_swV4 = elec_swV5 = elec_swF2 = elec_swF3 = elec_swF4 = 0.0;
202     }
203
204     if (fr->vdw_modifier == eintmodPOTSWITCH)
205     {
206         d               = fr->rvdw-fr->rvdw_switch;
207         vdw_swV3        = -10.0/(d*d*d);
208         vdw_swV4        =  15.0/(d*d*d*d);
209         vdw_swV5        =  -6.0/(d*d*d*d*d);
210         vdw_swF2        = -30.0/(d*d*d);
211         vdw_swF3        =  60.0/(d*d*d*d);
212         vdw_swF4        = -30.0/(d*d*d*d*d);
213     }
214     else
215     {
216         /* Avoid warnings from stupid compilers (looking at you, Clang!) */
217         vdw_swV3 = vdw_swV4 = vdw_swV5 = vdw_swF2 = vdw_swF3 = vdw_swF4 = 0.0;
218     }
219
220     if (fr->cutoff_scheme == ecutsVERLET)
221     {
222         const interaction_const_t *ic;
223
224         ic = fr->ic;
225         if (EVDW_PME(ic->vdwtype))
226         {
227             ivdw         = GMX_NBKERNEL_VDW_LJEWALD;
228         }
229         else
230         {
231             ivdw         = GMX_NBKERNEL_VDW_LENNARDJONES;
232         }
233
234         if (ic->eeltype == eelCUT || EEL_RF(ic->eeltype))
235         {
236             icoul        = GMX_NBKERNEL_ELEC_REACTIONFIELD;
237         }
238         else if (EEL_PME_EWALD(ic->eeltype))
239         {
240             icoul        = GMX_NBKERNEL_ELEC_EWALD;
241         }
242         else
243         {
244             gmx_incons("Unsupported eeltype with Verlet and free-energy");
245         }
246
247         bExactElecCutoff = TRUE;
248         bExactVdwCutoff  = TRUE;
249     }
250     else
251     {
252         bExactElecCutoff = (fr->coulomb_modifier != eintmodNONE) || fr->eeltype == eelRF_ZERO;
253         bExactVdwCutoff  = (fr->vdw_modifier != eintmodNONE);
254     }
255
256     bExactCutoffAll = (bExactElecCutoff && bExactVdwCutoff);
257     rcutoff_max2    = max(fr->rcoulomb, fr->rvdw);
258     rcutoff_max2    = rcutoff_max2*rcutoff_max2;
259
260     bEwald          = (icoul == GMX_NBKERNEL_ELEC_EWALD);
261     bEwaldLJ        = (ivdw == GMX_NBKERNEL_VDW_LJEWALD);
262
263     /* For Ewald/PME interactions we cannot easily apply the soft-core component to
264      * reciprocal space. When we use vanilla (not switch/shift) Ewald interactions, we
265      * can apply the small trick of subtracting the _reciprocal_ space contribution
266      * in this kernel, and instead apply the free energy interaction to the 1/r
267      * (standard coulomb) interaction.
268      *
269      * However, we cannot use this approach for switch-modified since we would then
270      * effectively end up evaluating a significantly different interaction here compared to the
271      * normal (non-free-energy) kernels, either by applying a cutoff at a different
272      * position than what the user requested, or by switching different
273      * things (1/r rather than short-range Ewald). For these settings, we just
274      * use the traditional short-range Ewald interaction in that case.
275      */
276     bConvertEwaldToCoulomb = (bEwald && (fr->coulomb_modifier != eintmodPOTSWITCH));
277     /* For now the below will always be true (since LJ-PME only works with Shift in Gromacs-5.0),
278      * but writing it this way means we stay in sync with coulomb, and it avoids future bugs.
279      */
280     bConvertLJEwaldToLJ6   = (bEwaldLJ && (fr->vdw_modifier   != eintmodPOTSWITCH));
281
282     /* We currently don't implement exclusion correction, needed with the Verlet cut-off scheme, without conversion */
283     if (fr->cutoff_scheme == ecutsVERLET &&
284         ((bEwald   && !bConvertEwaldToCoulomb) ||
285          (bEwaldLJ && !bConvertLJEwaldToLJ6)))
286     {
287         gmx_incons("Unimplemented non-bonded setup");
288     }
289
290     /* fix compiler warnings */
291     nj1   = 0;
292     n1C   = n1V   = 0;
293     epsC  = epsV  = 0;
294     eps2C = eps2V = 0;
295
296     dvdl_coul  = 0;
297     dvdl_vdw   = 0;
298
299     /* Lambda factor for state A, 1-lambda*/
300     LFC[STATE_A] = 1.0 - lambda_coul;
301     LFV[STATE_A] = 1.0 - lambda_vdw;
302
303     /* Lambda factor for state B, lambda*/
304     LFC[STATE_B] = lambda_coul;
305     LFV[STATE_B] = lambda_vdw;
306
307     /*derivative of the lambda factor for state A and B */
308     DLF[STATE_A] = -1;
309     DLF[STATE_B] = 1;
310
311     for (i = 0; i < NSTATES; i++)
312     {
313         lfac_coul[i]  = (lam_power == 2 ? (1-LFC[i])*(1-LFC[i]) : (1-LFC[i]));
314         dlfac_coul[i] = DLF[i]*lam_power/sc_r_power*(lam_power == 2 ? (1-LFC[i]) : 1);
315         lfac_vdw[i]   = (lam_power == 2 ? (1-LFV[i])*(1-LFV[i]) : (1-LFV[i]));
316         dlfac_vdw[i]  = DLF[i]*lam_power/sc_r_power*(lam_power == 2 ? (1-LFV[i]) : 1);
317     }
318     /* precalculate */
319     sigma2_def = pow(sigma6_def, 1.0/3.0);
320     sigma2_min = pow(sigma6_min, 1.0/3.0);
321
322     /* Ewald (not PME) table is special (icoul==enbcoulFEWALD) */
323
324     do_tab = (icoul == GMX_NBKERNEL_ELEC_CUBICSPLINETABLE ||
325               ivdw == GMX_NBKERNEL_VDW_CUBICSPLINETABLE);
326     if (do_tab)
327     {
328         tabscale         = kernel_data->table_elec_vdw->scale;
329         VFtab            = kernel_data->table_elec_vdw->data;
330         /* we always use the combined table here */
331         tab_elemsize     = 12;
332     }
333
334     for (n = 0; (n < nri); n++)
335     {
336         int npair_within_cutoff;
337
338         npair_within_cutoff = 0;
339
340         is3              = 3*shift[n];
341         shX              = shiftvec[is3];
342         shY              = shiftvec[is3+1];
343         shZ              = shiftvec[is3+2];
344         nj0              = jindex[n];
345         nj1              = jindex[n+1];
346         ii               = iinr[n];
347         ii3              = 3*ii;
348         ix               = shX + x[ii3+0];
349         iy               = shY + x[ii3+1];
350         iz               = shZ + x[ii3+2];
351         iqA              = facel*chargeA[ii];
352         iqB              = facel*chargeB[ii];
353         ntiA             = 2*ntype*typeA[ii];
354         ntiB             = 2*ntype*typeB[ii];
355         vctot            = 0;
356         vvtot            = 0;
357         fix              = 0;
358         fiy              = 0;
359         fiz              = 0;
360
361         for (k = nj0; (k < nj1); k++)
362         {
363             jnr              = jjnr[k];
364             j3               = 3*jnr;
365             dx               = ix - x[j3];
366             dy               = iy - x[j3+1];
367             dz               = iz - x[j3+2];
368             rsq              = dx*dx + dy*dy + dz*dz;
369
370             if (bExactCutoffAll && rsq >= rcutoff_max2)
371             {
372                 /* We save significant time by skipping all code below.
373                  * Note that with soft-core interactions, the actual cut-off
374                  * check might be different. But since the soft-core distance
375                  * is always larger than r, checking on r here is safe.
376                  */
377                 continue;
378             }
379             npair_within_cutoff++;
380
381             if (rsq > 0)
382             {
383                 rinv         = gmx_invsqrt(rsq);
384                 r            = rsq*rinv;
385             }
386             else
387             {
388                 /* The force at r=0 is zero, because of symmetry.
389                  * But note that the potential is in general non-zero,
390                  * since the soft-cored r will be non-zero.
391                  */
392                 rinv         = 0;
393                 r            = 0;
394             }
395
396             if (sc_r_power == 6.0)
397             {
398                 rpm2             = rsq*rsq;  /* r4 */
399                 rp               = rpm2*rsq; /* r6 */
400             }
401             else if (sc_r_power == 48.0)
402             {
403                 rp               = rsq*rsq*rsq; /* r6 */
404                 rp               = rp*rp;       /* r12 */
405                 rp               = rp*rp;       /* r24 */
406                 rp               = rp*rp;       /* r48 */
407                 rpm2             = rp/rsq;      /* r46 */
408             }
409             else
410             {
411                 rp             = pow(r, sc_r_power);  /* not currently supported as input, but can handle it */
412                 rpm2           = rp/rsq;
413             }
414
415             Fscal = 0;
416
417             qq[STATE_A]      = iqA*chargeA[jnr];
418             qq[STATE_B]      = iqB*chargeB[jnr];
419
420             tj[STATE_A]      = ntiA+2*typeA[jnr];
421             tj[STATE_B]      = ntiB+2*typeB[jnr];
422
423             if (nlist->excl_fep == NULL || nlist->excl_fep[k])
424             {
425                 c6[STATE_A]      = nbfp[tj[STATE_A]];
426                 c6[STATE_B]      = nbfp[tj[STATE_B]];
427
428                 for (i = 0; i < NSTATES; i++)
429                 {
430                     c12[i]             = nbfp[tj[i]+1];
431                     if ((c6[i] > 0) && (c12[i] > 0))
432                     {
433                         /* c12 is stored scaled with 12.0 and c6 is scaled with 6.0 - correct for this */
434                         sigma6[i]       = 0.5*c12[i]/c6[i];
435                         sigma2[i]       = pow(sigma6[i], 1.0/3.0);
436                         /* should be able to get rid of this ^^^ internal pow call eventually.  Will require agreement on
437                            what data to store externally.  Can't be fixed without larger scale changes, so not 4.6 */
438                         if (sigma6[i] < sigma6_min)   /* for disappearing coul and vdw with soft core at the same time */
439                         {
440                             sigma6[i] = sigma6_min;
441                             sigma2[i] = sigma2_min;
442                         }
443                     }
444                     else
445                     {
446                         sigma6[i]       = sigma6_def;
447                         sigma2[i]       = sigma2_def;
448                     }
449                     if (sc_r_power == 6.0)
450                     {
451                         sigma_pow[i]    = sigma6[i];
452                         sigma_powm2[i]  = sigma6[i]/sigma2[i];
453                     }
454                     else if (sc_r_power == 48.0)
455                     {
456                         sigma_pow[i]    = sigma6[i]*sigma6[i];       /* sigma^12 */
457                         sigma_pow[i]    = sigma_pow[i]*sigma_pow[i]; /* sigma^24 */
458                         sigma_pow[i]    = sigma_pow[i]*sigma_pow[i]; /* sigma^48 */
459                         sigma_powm2[i]  = sigma_pow[i]/sigma2[i];
460                     }
461                     else
462                     {    /* not really supported as input, but in here for testing the general case*/
463                         sigma_pow[i]    = pow(sigma2[i], sc_r_power/2);
464                         sigma_powm2[i]  = sigma_pow[i]/(sigma2[i]);
465                     }
466                 }
467
468                 /* only use softcore if one of the states has a zero endstate - softcore is for avoiding infinities!*/
469                 if ((c12[STATE_A] > 0) && (c12[STATE_B] > 0))
470                 {
471                     alpha_vdw_eff    = 0;
472                     alpha_coul_eff   = 0;
473                 }
474                 else
475                 {
476                     alpha_vdw_eff    = alpha_vdw;
477                     alpha_coul_eff   = alpha_coul;
478                 }
479
480                 for (i = 0; i < NSTATES; i++)
481                 {
482                     FscalC[i]    = 0;
483                     FscalV[i]    = 0;
484                     Vcoul[i]     = 0;
485                     Vvdw[i]      = 0;
486
487                     /* Only spend time on A or B state if it is non-zero */
488                     if ( (qq[i] != 0) || (c6[i] != 0) || (c12[i] != 0) )
489                     {
490                         /* this section has to be inside the loop because of the dependence on sigma_pow */
491                         rpinvC         = 1.0/(alpha_coul_eff*lfac_coul[i]*sigma_pow[i]+rp);
492                         rinvC          = pow(rpinvC, 1.0/sc_r_power);
493                         rC             = 1.0/rinvC;
494
495                         rpinvV         = 1.0/(alpha_vdw_eff*lfac_vdw[i]*sigma_pow[i]+rp);
496                         rinvV          = pow(rpinvV, 1.0/sc_r_power);
497                         rV             = 1.0/rinvV;
498
499                         if (do_tab)
500                         {
501                             rtC        = rC*tabscale;
502                             n0         = rtC;
503                             epsC       = rtC-n0;
504                             eps2C      = epsC*epsC;
505                             n1C        = tab_elemsize*n0;
506
507                             rtV        = rV*tabscale;
508                             n0         = rtV;
509                             epsV       = rtV-n0;
510                             eps2V      = epsV*epsV;
511                             n1V        = tab_elemsize*n0;
512                         }
513
514                         /* Only process the coulomb interactions if we have charges,
515                          * and if we either include all entries in the list (no cutoff
516                          * used in the kernel), or if we are within the cutoff.
517                          */
518                         bComputeElecInteraction = !bExactElecCutoff ||
519                             ( bConvertEwaldToCoulomb && r < rcoulomb) ||
520                             (!bConvertEwaldToCoulomb && rC < rcoulomb);
521
522                         if ( (qq[i] != 0) && bComputeElecInteraction)
523                         {
524                             switch (icoul)
525                             {
526                                 case GMX_NBKERNEL_ELEC_COULOMB:
527                                     /* simple cutoff */
528                                     Vcoul[i]   = qq[i]*rinvC;
529                                     FscalC[i]  = Vcoul[i];
530                                     /* The shift for the Coulomb potential is stored in
531                                      * the RF parameter c_rf, which is 0 without shift.
532                                      */
533                                     Vcoul[i]  -= qq[i]*fr->ic->c_rf;
534                                     break;
535
536                                 case GMX_NBKERNEL_ELEC_REACTIONFIELD:
537                                     /* reaction-field */
538                                     Vcoul[i]   = qq[i]*(rinvC + krf*rC*rC-crf);
539                                     FscalC[i]  = qq[i]*(rinvC - 2.0*krf*rC*rC);
540                                     break;
541
542                                 case GMX_NBKERNEL_ELEC_CUBICSPLINETABLE:
543                                     /* non-Ewald tabulated coulomb */
544                                     nnn        = n1C;
545                                     Y          = VFtab[nnn];
546                                     F          = VFtab[nnn+1];
547                                     Geps       = epsC*VFtab[nnn+2];
548                                     Heps2      = eps2C*VFtab[nnn+3];
549                                     Fp         = F+Geps+Heps2;
550                                     VV         = Y+epsC*Fp;
551                                     FF         = Fp+Geps+2.0*Heps2;
552                                     Vcoul[i]   = qq[i]*VV;
553                                     FscalC[i]  = -qq[i]*tabscale*FF*rC;
554                                     break;
555
556                                 case GMX_NBKERNEL_ELEC_GENERALIZEDBORN:
557                                     gmx_fatal(FARGS, "Free energy and GB not implemented.\n");
558                                     break;
559
560                                 case GMX_NBKERNEL_ELEC_EWALD:
561                                     if (bConvertEwaldToCoulomb)
562                                     {
563                                         /* Ewald FEP is done only on the 1/r part */
564                                         Vcoul[i]   = qq[i]*(rinvC-sh_ewald);
565                                         FscalC[i]  = qq[i]*rinvC;
566                                     }
567                                     else
568                                     {
569                                         ewrt      = rC*ewtabscale;
570                                         ewitab    = (int) ewrt;
571                                         eweps     = ewrt-ewitab;
572                                         ewitab    = 4*ewitab;
573                                         FscalC[i] = ewtab[ewitab]+eweps*ewtab[ewitab+1];
574                                         rinvcorr  = rinvC-sh_ewald;
575                                         Vcoul[i]  = qq[i]*(rinvcorr-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+FscalC[i])));
576                                         FscalC[i] = qq[i]*(rinvC-rC*FscalC[i]);
577                                     }
578                                     break;
579
580                                 case GMX_NBKERNEL_ELEC_NONE:
581                                     FscalC[i]  = 0.0;
582                                     Vcoul[i]   = 0.0;
583                                     break;
584
585                                 default:
586                                     gmx_incons("Invalid icoul in free energy kernel");
587                                     break;
588                             }
589
590                             if (fr->coulomb_modifier == eintmodPOTSWITCH)
591                             {
592                                 d                = rC-fr->rcoulomb_switch;
593                                 d                = (d > 0.0) ? d : 0.0;
594                                 d2               = d*d;
595                                 sw               = 1.0+d2*d*(elec_swV3+d*(elec_swV4+d*elec_swV5));
596                                 dsw              = d2*(elec_swF2+d*(elec_swF3+d*elec_swF4));
597
598                                 FscalC[i]        = FscalC[i]*sw - rC*Vcoul[i]*dsw;
599                                 Vcoul[i]        *= sw;
600
601                                 FscalC[i]        = (rC < rcoulomb) ? FscalC[i] : 0.0;
602                                 Vcoul[i]         = (rC < rcoulomb) ? Vcoul[i] : 0.0;
603                             }
604                         }
605
606                         /* Only process the VDW interactions if we have
607                          * some non-zero parameters, and if we either
608                          * include all entries in the list (no cutoff used
609                          * in the kernel), or if we are within the cutoff.
610                          */
611                         bComputeVdwInteraction = !bExactVdwCutoff ||
612                             ( bConvertLJEwaldToLJ6 && r < rvdw) ||
613                             (!bConvertLJEwaldToLJ6 && rV < rvdw);
614                         if ((c6[i] != 0 || c12[i] != 0) && bComputeVdwInteraction)
615                         {
616                             switch (ivdw)
617                             {
618                                 case GMX_NBKERNEL_VDW_LENNARDJONES:
619                                     /* cutoff LJ */
620                                     if (sc_r_power == 6.0)
621                                     {
622                                         rinv6            = rpinvV;
623                                     }
624                                     else
625                                     {
626                                         rinv6            = rinvV*rinvV;
627                                         rinv6            = rinv6*rinv6*rinv6;
628                                     }
629                                     Vvdw6            = c6[i]*rinv6;
630                                     Vvdw12           = c12[i]*rinv6*rinv6;
631
632                                     Vvdw[i]          = ( (Vvdw12 - c12[i]*sh_invrc6*sh_invrc6)*(1.0/12.0)
633                                                          - (Vvdw6 - c6[i]*sh_invrc6)*(1.0/6.0));
634                                     FscalV[i]        = Vvdw12 - Vvdw6;
635                                     break;
636
637                                 case GMX_NBKERNEL_VDW_BUCKINGHAM:
638                                     gmx_fatal(FARGS, "Buckingham free energy not supported.");
639                                     break;
640
641                                 case GMX_NBKERNEL_VDW_CUBICSPLINETABLE:
642                                     /* Table LJ */
643                                     nnn = n1V+4;
644                                     /* dispersion */
645                                     Y          = VFtab[nnn];
646                                     F          = VFtab[nnn+1];
647                                     Geps       = epsV*VFtab[nnn+2];
648                                     Heps2      = eps2V*VFtab[nnn+3];
649                                     Fp         = F+Geps+Heps2;
650                                     VV         = Y+epsV*Fp;
651                                     FF         = Fp+Geps+2.0*Heps2;
652                                     Vvdw[i]   += c6[i]*VV;
653                                     FscalV[i] -= c6[i]*tabscale*FF*rV;
654
655                                     /* repulsion */
656                                     Y          = VFtab[nnn+4];
657                                     F          = VFtab[nnn+5];
658                                     Geps       = epsV*VFtab[nnn+6];
659                                     Heps2      = eps2V*VFtab[nnn+7];
660                                     Fp         = F+Geps+Heps2;
661                                     VV         = Y+epsV*Fp;
662                                     FF         = Fp+Geps+2.0*Heps2;
663                                     Vvdw[i]   += c12[i]*VV;
664                                     FscalV[i] -= c12[i]*tabscale*FF*rV;
665                                     break;
666
667                                 case GMX_NBKERNEL_VDW_LJEWALD:
668                                     if (sc_r_power == 6.0)
669                                     {
670                                         rinv6            = rpinvV;
671                                     }
672                                     else
673                                     {
674                                         rinv6            = rinvV*rinvV;
675                                         rinv6            = rinv6*rinv6*rinv6;
676                                     }
677                                     c6grid           = nbfp_grid[tj[i]];
678
679                                     if (bConvertLJEwaldToLJ6)
680                                     {
681                                         /* cutoff LJ */
682                                         Vvdw6            = c6[i]*rinv6;
683                                         Vvdw12           = c12[i]*rinv6*rinv6;
684
685                                         Vvdw[i]          = ( (Vvdw12 - c12[i]*sh_invrc6*sh_invrc6)*(1.0/12.0)
686                                                              - (Vvdw6 - c6[i]*sh_invrc6 - c6grid*sh_lj_ewald)*(1.0/6.0));
687                                         FscalV[i]        = Vvdw12 - Vvdw6;
688                                     }
689                                     else
690                                     {
691                                         /* Normal LJ-PME */
692                                         ewcljrsq         = ewclj2*rV*rV;
693                                         exponent         = exp(-ewcljrsq);
694                                         poly             = exponent*(1.0 + ewcljrsq + ewcljrsq*ewcljrsq*0.5);
695                                         vvdw_disp        = (c6[i]-c6grid*(1.0-poly))*rinv6;
696                                         vvdw_rep         = c12[i]*rinv6*rinv6;
697                                         FscalV[i]        = vvdw_rep - vvdw_disp - c6grid*(1.0/6.0)*exponent*ewclj6;
698                                         Vvdw[i]          = (vvdw_rep - c12[i]*sh_invrc6*sh_invrc6)/12.0 - (vvdw_disp - c6[i]*sh_invrc6 - c6grid*sh_lj_ewald)/6.0;
699                                     }
700                                     break;
701
702                                 case GMX_NBKERNEL_VDW_NONE:
703                                     Vvdw[i]    = 0.0;
704                                     FscalV[i]  = 0.0;
705                                     break;
706
707                                 default:
708                                     gmx_incons("Invalid ivdw in free energy kernel");
709                                     break;
710                             }
711
712                             if (fr->vdw_modifier == eintmodPOTSWITCH)
713                             {
714                                 d                = rV-fr->rvdw_switch;
715                                 d                = (d > 0.0) ? d : 0.0;
716                                 d2               = d*d;
717                                 sw               = 1.0+d2*d*(vdw_swV3+d*(vdw_swV4+d*vdw_swV5));
718                                 dsw              = d2*(vdw_swF2+d*(vdw_swF3+d*vdw_swF4));
719
720                                 FscalV[i]        = FscalV[i]*sw - rV*Vvdw[i]*dsw;
721                                 Vvdw[i]         *= sw;
722
723                                 FscalV[i]  = (rV < rvdw) ? FscalV[i] : 0.0;
724                                 Vvdw[i]    = (rV < rvdw) ? Vvdw[i] : 0.0;
725                             }
726                         }
727
728                         /* FscalC (and FscalV) now contain: dV/drC * rC
729                          * Now we multiply by rC^-p, so it will be: dV/drC * rC^1-p
730                          * Further down we first multiply by r^p-2 and then by
731                          * the vector r, which in total gives: dV/drC * (r/rC)^1-p
732                          */
733                         FscalC[i] *= rpinvC;
734                         FscalV[i] *= rpinvV;
735                     }
736                 }
737
738                 /* Assemble A and B states */
739                 for (i = 0; i < NSTATES; i++)
740                 {
741                     vctot         += LFC[i]*Vcoul[i];
742                     vvtot         += LFV[i]*Vvdw[i];
743
744                     Fscal         += LFC[i]*FscalC[i]*rpm2;
745                     Fscal         += LFV[i]*FscalV[i]*rpm2;
746
747                     dvdl_coul     += Vcoul[i]*DLF[i] + LFC[i]*alpha_coul_eff*dlfac_coul[i]*FscalC[i]*sigma_pow[i];
748                     dvdl_vdw      += Vvdw[i]*DLF[i] + LFV[i]*alpha_vdw_eff*dlfac_vdw[i]*FscalV[i]*sigma_pow[i];
749                 }
750             }
751             else if (icoul == GMX_NBKERNEL_ELEC_REACTIONFIELD)
752             {
753                 /* For excluded pairs, which are only in this pair list when
754                  * using the Verlet scheme, we don't use soft-core.
755                  * The group scheme also doesn't soft-core for these.
756                  * As there is no singularity, there is no need for soft-core.
757                  */
758                 VV = krf*rsq - crf;
759                 FF = -2.0*krf;
760
761                 if (ii == jnr)
762                 {
763                     VV *= 0.5;
764                 }
765
766                 for (i = 0; i < NSTATES; i++)
767                 {
768                     vctot      += LFC[i]*qq[i]*VV;
769                     Fscal      += LFC[i]*qq[i]*FF;
770                     dvdl_coul  += DLF[i]*qq[i]*VV;
771                 }
772             }
773
774             if (bConvertEwaldToCoulomb && ( !bExactElecCutoff || r < rcoulomb ) )
775             {
776                 /* See comment in the preamble. When using Ewald interactions
777                  * (unless we use a switch modifier) we subtract the reciprocal-space
778                  * Ewald component here which made it possible to apply the free
779                  * energy interaction to 1/r (vanilla coulomb short-range part)
780                  * above. This gets us closer to the ideal case of applying
781                  * the softcore to the entire electrostatic interaction,
782                  * including the reciprocal-space component.
783                  */
784                 real v_lr, f_lr;
785
786                 ewrt      = r*ewtabscale;
787                 ewitab    = (int) ewrt;
788                 eweps     = ewrt-ewitab;
789                 ewitab    = 4*ewitab;
790                 f_lr      = ewtab[ewitab]+eweps*ewtab[ewitab+1];
791                 v_lr      = (ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+f_lr));
792                 f_lr     *= rinv;
793
794                 /* Note that any possible Ewald shift has already been applied in
795                  * the normal interaction part above.
796                  */
797
798                 if (ii == jnr)
799                 {
800                     /* If we get here, the i particle (ii) has itself (jnr)
801                      * in its neighborlist. This can only happen with the Verlet
802                      * scheme, and corresponds to a self-interaction that will
803                      * occur twice. Scale it down by 50% to only include it once.
804                      */
805                     v_lr *= 0.5;
806                 }
807
808                 for (i = 0; i < NSTATES; i++)
809                 {
810                     vctot      -= LFC[i]*qq[i]*v_lr;
811                     Fscal      -= LFC[i]*qq[i]*f_lr;
812                     dvdl_coul  -= (DLF[i]*qq[i])*v_lr;
813                 }
814             }
815
816             if (bConvertLJEwaldToLJ6 && (!bExactVdwCutoff || r < rvdw))
817             {
818                 /* See comment in the preamble. When using LJ-Ewald interactions
819                  * (unless we use a switch modifier) we subtract the reciprocal-space
820                  * Ewald component here which made it possible to apply the free
821                  * energy interaction to r^-6 (vanilla LJ6 short-range part)
822                  * above. This gets us closer to the ideal case of applying
823                  * the softcore to the entire VdW interaction,
824                  * including the reciprocal-space component.
825                  */
826                 /* We could also use the analytical form here
827                  * iso a table, but that can cause issues for
828                  * r close to 0 for non-interacting pairs.
829                  */
830                 real rs, frac, f_lr;
831                 int  ri;
832
833                 rs     = rsq*rinv*ewtabscale;
834                 ri     = (int)rs;
835                 frac   = rs - ri;
836                 f_lr   = (1 - frac)*tab_ewald_F_lj[ri] + frac*tab_ewald_F_lj[ri+1];
837                 /* TODO: Currently the Ewald LJ table does not contain
838                  * the factor 1/6, we should add this.
839                  */
840                 FF     = f_lr*rinv/6.0;
841                 VV     = (tab_ewald_V_lj[ri] - ewtabhalfspace*frac*(tab_ewald_F_lj[ri] + f_lr))/6.0;
842
843                 if (ii == jnr)
844                 {
845                     /* If we get here, the i particle (ii) has itself (jnr)
846                      * in its neighborlist. This can only happen with the Verlet
847                      * scheme, and corresponds to a self-interaction that will
848                      * occur twice. Scale it down by 50% to only include it once.
849                      */
850                     VV *= 0.5;
851                 }
852
853                 for (i = 0; i < NSTATES; i++)
854                 {
855                     c6grid      = nbfp_grid[tj[i]];
856                     vvtot      += LFV[i]*c6grid*VV;
857                     Fscal      += LFV[i]*c6grid*FF;
858                     dvdl_vdw   += (DLF[i]*c6grid)*VV;
859                 }
860             }
861
862             if (bDoForces)
863             {
864                 tx         = Fscal*dx;
865                 ty         = Fscal*dy;
866                 tz         = Fscal*dz;
867                 fix        = fix + tx;
868                 fiy        = fiy + ty;
869                 fiz        = fiz + tz;
870                 /* OpenMP atomics are expensive, but this kernels is also
871                  * expensive, so we can take this hit, instead of using
872                  * thread-local output buffers and extra reduction.
873                  */
874 #pragma omp atomic
875                 f[j3]     -= tx;
876 #pragma omp atomic
877                 f[j3+1]   -= ty;
878 #pragma omp atomic
879                 f[j3+2]   -= tz;
880             }
881         }
882
883         /* The atomics below are expensive with many OpenMP threads.
884          * Here unperturbed i-particles will usually only have a few
885          * (perturbed) j-particles in the list. Thus with a buffered list
886          * we can skip a significant number of i-reductions with a check.
887          */
888         if (npair_within_cutoff > 0)
889         {
890             if (bDoForces)
891             {
892 #pragma omp atomic
893                 f[ii3]        += fix;
894 #pragma omp atomic
895                 f[ii3+1]      += fiy;
896 #pragma omp atomic
897                 f[ii3+2]      += fiz;
898             }
899             if (bDoShiftForces)
900             {
901 #pragma omp atomic
902                 fshift[is3]   += fix;
903 #pragma omp atomic
904                 fshift[is3+1] += fiy;
905 #pragma omp atomic
906                 fshift[is3+2] += fiz;
907             }
908             if (bDoPotential)
909             {
910                 ggid               = gid[n];
911 #pragma omp atomic
912                 Vc[ggid]          += vctot;
913 #pragma omp atomic
914                 Vv[ggid]          += vvtot;
915             }
916         }
917     }
918
919 #pragma omp atomic
920     dvdl[efptCOUL]     += dvdl_coul;
921  #pragma omp atomic
922     dvdl[efptVDW]      += dvdl_vdw;
923
924     /* Estimate flops, average for free energy stuff:
925      * 12  flops per outer iteration
926      * 150 flops per inner iteration
927      */
928 #pragma omp atomic
929     inc_nrnb(nrnb, eNR_NBKERNEL_FREE_ENERGY, nlist->nri*12 + nlist->jindex[n]*150);
930 }
931
932 real
933 nb_free_energy_evaluate_single(real r2, real sc_r_power, real alpha_coul, real alpha_vdw,
934                                real tabscale, real *vftab,
935                                real qqA, real c6A, real c12A, real qqB, real c6B, real c12B,
936                                real LFC[2], real LFV[2], real DLF[2],
937                                real lfac_coul[2], real lfac_vdw[2], real dlfac_coul[2], real dlfac_vdw[2],
938                                real sigma6_def, real sigma6_min, real sigma2_def, real sigma2_min,
939                                real *velectot, real *vvdwtot, real *dvdl)
940 {
941     real       r, rp, rpm2, rtab, eps, eps2, Y, F, Geps, Heps2, Fp, VV, FF, fscal;
942     real       qq[2], c6[2], c12[2], sigma6[2], sigma2[2], sigma_pow[2], sigma_powm2[2];
943     real       alpha_coul_eff, alpha_vdw_eff, dvdl_coul, dvdl_vdw;
944     real       rpinv, r_coul, r_vdw, velecsum, vvdwsum;
945     real       fscal_vdw[2], fscal_elec[2];
946     real       velec[2], vvdw[2];
947     int        i, ntab;
948
949     qq[0]    = qqA;
950     qq[1]    = qqB;
951     c6[0]    = c6A;
952     c6[1]    = c6B;
953     c12[0]   = c12A;
954     c12[1]   = c12B;
955
956     if (sc_r_power == 6.0)
957     {
958         rpm2             = r2*r2;   /* r4 */
959         rp               = rpm2*r2; /* r6 */
960     }
961     else if (sc_r_power == 48.0)
962     {
963         rp               = r2*r2*r2; /* r6 */
964         rp               = rp*rp;    /* r12 */
965         rp               = rp*rp;    /* r24 */
966         rp               = rp*rp;    /* r48 */
967         rpm2             = rp/r2;    /* r46 */
968     }
969     else
970     {
971         rp             = pow(r2, 0.5*sc_r_power);  /* not currently supported as input, but can handle it */
972         rpm2           = rp/r2;
973     }
974
975     /* Loop over state A(0) and B(1) */
976     for (i = 0; i < 2; i++)
977     {
978         if ((c6[i] > 0) && (c12[i] > 0))
979         {
980             /* The c6 & c12 coefficients now contain the constants 6.0 and 12.0, respectively.
981              * Correct for this by multiplying with (1/12.0)/(1/6.0)=6.0/12.0=0.5.
982              */
983             sigma6[i]       = 0.5*c12[i]/c6[i];
984             sigma2[i]       = pow(0.5*c12[i]/c6[i], 1.0/3.0);
985             /* should be able to get rid of this ^^^ internal pow call eventually.  Will require agreement on
986                what data to store externally.  Can't be fixed without larger scale changes, so not 5.0 */
987             if (sigma6[i] < sigma6_min)   /* for disappearing coul and vdw with soft core at the same time */
988             {
989                 sigma6[i] = sigma6_min;
990                 sigma2[i] = sigma2_min;
991             }
992         }
993         else
994         {
995             sigma6[i]       = sigma6_def;
996             sigma2[i]       = sigma2_def;
997         }
998         if (sc_r_power == 6.0)
999         {
1000             sigma_pow[i]    = sigma6[i];
1001             sigma_powm2[i]  = sigma6[i]/sigma2[i];
1002         }
1003         else if (sc_r_power == 48.0)
1004         {
1005             sigma_pow[i]    = sigma6[i]*sigma6[i];       /* sigma^12 */
1006             sigma_pow[i]    = sigma_pow[i]*sigma_pow[i]; /* sigma^24 */
1007             sigma_pow[i]    = sigma_pow[i]*sigma_pow[i]; /* sigma^48 */
1008             sigma_powm2[i]  = sigma_pow[i]/sigma2[i];
1009         }
1010         else
1011         {    /* not really supported as input, but in here for testing the general case*/
1012             sigma_pow[i]    = pow(sigma2[i], sc_r_power/2);
1013             sigma_powm2[i]  = sigma_pow[i]/(sigma2[i]);
1014         }
1015     }
1016
1017     /* only use softcore if one of the states has a zero endstate - softcore is for avoiding infinities!*/
1018     if ((c12[0] > 0) && (c12[1] > 0))
1019     {
1020         alpha_vdw_eff    = 0;
1021         alpha_coul_eff   = 0;
1022     }
1023     else
1024     {
1025         alpha_vdw_eff    = alpha_vdw;
1026         alpha_coul_eff   = alpha_coul;
1027     }
1028
1029     /* Loop over A and B states again */
1030     for (i = 0; i < 2; i++)
1031     {
1032         fscal_elec[i] = 0;
1033         fscal_vdw[i]  = 0;
1034         velec[i]      = 0;
1035         vvdw[i]       = 0;
1036
1037         /* Only spend time on A or B state if it is non-zero */
1038         if ( (qq[i] != 0) || (c6[i] != 0) || (c12[i] != 0) )
1039         {
1040             /* Coulomb */
1041             rpinv            = 1.0/(alpha_coul_eff*lfac_coul[i]*sigma_pow[i]+rp);
1042             r_coul           = pow(rpinv, -1.0/sc_r_power);
1043
1044             /* Electrostatics table lookup data */
1045             rtab             = r_coul*tabscale;
1046             ntab             = rtab;
1047             eps              = rtab-ntab;
1048             eps2             = eps*eps;
1049             ntab             = 12*ntab;
1050             /* Electrostatics */
1051             Y                = vftab[ntab];
1052             F                = vftab[ntab+1];
1053             Geps             = eps*vftab[ntab+2];
1054             Heps2            = eps2*vftab[ntab+3];
1055             Fp               = F+Geps+Heps2;
1056             VV               = Y+eps*Fp;
1057             FF               = Fp+Geps+2.0*Heps2;
1058             velec[i]         = qq[i]*VV;
1059             fscal_elec[i]    = -qq[i]*FF*r_coul*rpinv*tabscale;
1060
1061             /* Vdw */
1062             rpinv            = 1.0/(alpha_vdw_eff*lfac_vdw[i]*sigma_pow[i]+rp);
1063             r_vdw            = pow(rpinv, -1.0/sc_r_power);
1064             /* Vdw table lookup data */
1065             rtab             = r_vdw*tabscale;
1066             ntab             = rtab;
1067             eps              = rtab-ntab;
1068             eps2             = eps*eps;
1069             ntab             = 12*ntab;
1070             /* Dispersion */
1071             Y                = vftab[ntab+4];
1072             F                = vftab[ntab+5];
1073             Geps             = eps*vftab[ntab+6];
1074             Heps2            = eps2*vftab[ntab+7];
1075             Fp               = F+Geps+Heps2;
1076             VV               = Y+eps*Fp;
1077             FF               = Fp+Geps+2.0*Heps2;
1078             vvdw[i]          = c6[i]*VV;
1079             fscal_vdw[i]     = -c6[i]*FF;
1080
1081             /* Repulsion */
1082             Y                = vftab[ntab+8];
1083             F                = vftab[ntab+9];
1084             Geps             = eps*vftab[ntab+10];
1085             Heps2            = eps2*vftab[ntab+11];
1086             Fp               = F+Geps+Heps2;
1087             VV               = Y+eps*Fp;
1088             FF               = Fp+Geps+2.0*Heps2;
1089             vvdw[i]         += c12[i]*VV;
1090             fscal_vdw[i]    -= c12[i]*FF;
1091             fscal_vdw[i]    *= r_vdw*rpinv*tabscale;
1092         }
1093     }
1094     /* Now we have velec[i], vvdw[i], and fscal[i] for both states */
1095     /* Assemble A and B states */
1096     velecsum  = 0;
1097     vvdwsum   = 0;
1098     dvdl_coul = 0;
1099     dvdl_vdw  = 0;
1100     fscal     = 0;
1101     for (i = 0; i < 2; i++)
1102     {
1103         velecsum      += LFC[i]*velec[i];
1104         vvdwsum       += LFV[i]*vvdw[i];
1105
1106         fscal         += (LFC[i]*fscal_elec[i]+LFV[i]*fscal_vdw[i])*rpm2;
1107
1108         dvdl_coul     += velec[i]*DLF[i] + LFC[i]*alpha_coul_eff*dlfac_coul[i]*fscal_elec[i]*sigma_pow[i];
1109         dvdl_vdw      += vvdw[i]*DLF[i] + LFV[i]*alpha_vdw_eff*dlfac_vdw[i]*fscal_vdw[i]*sigma_pow[i];
1110     }
1111
1112     dvdl[efptCOUL]     += dvdl_coul;
1113     dvdl[efptVDW]      += dvdl_vdw;
1114
1115     *velectot           = velecsum;
1116     *vvdwtot            = vvdwsum;
1117
1118     return fscal;
1119 }