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