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