2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
39 #include "nb_free_energy.h"
43 #include "gromacs/gmxlib/nonbonded/nb_kernel.h"
44 #include "gromacs/legacyheaders/macros.h"
45 #include "gromacs/legacyheaders/nonbonded.h"
46 #include "gromacs/legacyheaders/nrnb.h"
47 #include "gromacs/legacyheaders/typedefs.h"
48 #include "gromacs/math/vec.h"
49 #include "gromacs/utility/fatalerror.h"
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)
64 int i, j, n, ii, is3, ii3, k, nj0, nj1, jnr, j3, ggid;
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;
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;
96 const real * shiftvec;
100 const real * VFtab = NULL;
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;
110 const real * nbfp, *nbfp_grid;
114 gmx_bool bDoForces, bDoShiftForces, bDoPotential;
115 real rcoulomb, rvdw, sh_invrc6;
116 gmx_bool bExactElecCutoff, bExactVdwCutoff, bExactCutoffAll;
117 gmx_bool bEwald, bEwaldLJ;
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;
128 real ewrt, eweps, ewtabscale, ewtabhalfspace, sh_ewald;
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;
140 fshift = fr->fshift[0];
144 jindex = nlist->jindex;
146 icoul = nlist->ielec;
148 shift = nlist->shift;
151 shiftvec = fr->shift_vec[0];
152 chargeA = mdatoms->chargeA;
153 chargeB = mdatoms->chargeB;
157 ewc_lj = fr->ewaldcoeff_lj;
158 Vc = kernel_data->energygrp_elec;
159 typeA = mdatoms->typeA;
160 typeB = mdatoms->typeB;
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;
178 rcoulomb = fr->rcoulomb;
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;
186 if (fr->coulomb_modifier == eintmodPOTSWITCH)
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);
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;
202 if (fr->vdw_modifier == eintmodPOTSWITCH)
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);
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;
218 if (fr->cutoff_scheme == ecutsVERLET)
220 const interaction_const_t *ic;
223 if (EVDW_PME(ic->vdwtype))
225 ivdw = GMX_NBKERNEL_VDW_LJEWALD;
229 ivdw = GMX_NBKERNEL_VDW_LENNARDJONES;
232 if (ic->eeltype == eelCUT || EEL_RF(ic->eeltype))
234 icoul = GMX_NBKERNEL_ELEC_REACTIONFIELD;
236 else if (EEL_PME_EWALD(ic->eeltype))
238 icoul = GMX_NBKERNEL_ELEC_EWALD;
242 gmx_incons("Unsupported eeltype with Verlet and free-energy");
245 bExactElecCutoff = TRUE;
246 bExactVdwCutoff = TRUE;
250 bExactElecCutoff = (fr->coulomb_modifier != eintmodNONE) || fr->eeltype == eelRF_ZERO;
251 bExactVdwCutoff = (fr->vdw_modifier != eintmodNONE);
254 bExactCutoffAll = (bExactElecCutoff && bExactVdwCutoff);
255 rcutoff_max2 = max(fr->rcoulomb, fr->rvdw);
256 rcutoff_max2 = rcutoff_max2*rcutoff_max2;
258 bEwald = (icoul == GMX_NBKERNEL_ELEC_EWALD);
259 bEwaldLJ = (ivdw == GMX_NBKERNEL_VDW_LJEWALD);
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.
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.
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.
278 bConvertLJEwaldToLJ6 = (bEwaldLJ && (fr->vdw_modifier != eintmodPOTSWITCH));
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)))
285 gmx_incons("Unimplemented non-bonded setup");
288 /* fix compiler warnings */
297 /* Lambda factor for state A, 1-lambda*/
298 LFC[STATE_A] = 1.0 - lambda_coul;
299 LFV[STATE_A] = 1.0 - lambda_vdw;
301 /* Lambda factor for state B, lambda*/
302 LFC[STATE_B] = lambda_coul;
303 LFV[STATE_B] = lambda_vdw;
305 /*derivative of the lambda factor for state A and B */
309 for (i = 0; i < NSTATES; i++)
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);
317 sigma2_def = pow(sigma6_def, 1.0/3.0);
318 sigma2_min = pow(sigma6_min, 1.0/3.0);
320 /* Ewald (not PME) table is special (icoul==enbcoulFEWALD) */
322 do_tab = (icoul == GMX_NBKERNEL_ELEC_CUBICSPLINETABLE ||
323 ivdw == GMX_NBKERNEL_VDW_CUBICSPLINETABLE);
326 tabscale = kernel_data->table_elec_vdw->scale;
327 VFtab = kernel_data->table_elec_vdw->data;
328 /* we always use the combined table here */
332 for (n = 0; (n < nri); n++)
334 int npair_within_cutoff;
336 npair_within_cutoff = 0;
340 shY = shiftvec[is3+1];
341 shZ = shiftvec[is3+2];
349 iqA = facel*chargeA[ii];
350 iqB = facel*chargeB[ii];
351 ntiA = 2*ntype*typeA[ii];
352 ntiB = 2*ntype*typeB[ii];
359 for (k = nj0; (k < nj1); k++)
366 rsq = dx*dx + dy*dy + dz*dz;
368 if (bExactCutoffAll && rsq >= rcutoff_max2)
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.
377 npair_within_cutoff++;
381 rinv = gmx_invsqrt(rsq);
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.
394 if (sc_r_power == 6.0)
396 rpm2 = rsq*rsq; /* r4 */
397 rp = rpm2*rsq; /* r6 */
399 else if (sc_r_power == 48.0)
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 */
409 rp = pow(r, sc_r_power); /* not currently supported as input, but can handle it */
415 qq[STATE_A] = iqA*chargeA[jnr];
416 qq[STATE_B] = iqB*chargeB[jnr];
418 tj[STATE_A] = ntiA+2*typeA[jnr];
419 tj[STATE_B] = ntiB+2*typeB[jnr];
421 if (nlist->excl_fep == NULL || nlist->excl_fep[k])
423 c6[STATE_A] = nbfp[tj[STATE_A]];
424 c6[STATE_B] = nbfp[tj[STATE_B]];
426 for (i = 0; i < NSTATES; i++)
428 c12[i] = nbfp[tj[i]+1];
429 if ((c6[i] > 0) && (c12[i] > 0))
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 */
438 sigma6[i] = sigma6_min;
439 sigma2[i] = sigma2_min;
444 sigma6[i] = sigma6_def;
445 sigma2[i] = sigma2_def;
447 if (sc_r_power == 6.0)
449 sigma_pow[i] = sigma6[i];
450 sigma_powm2[i] = sigma6[i]/sigma2[i];
452 else if (sc_r_power == 48.0)
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];
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]);
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))
474 alpha_vdw_eff = alpha_vdw;
475 alpha_coul_eff = alpha_coul;
478 for (i = 0; i < NSTATES; i++)
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) )
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);
493 rpinvV = 1.0/(alpha_vdw_eff*lfac_vdw[i]*sigma_pow[i]+rp);
494 rinvV = pow(rpinvV, 1.0/sc_r_power);
503 n1C = tab_elemsize*n0;
509 n1V = tab_elemsize*n0;
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.
516 bComputeElecInteraction = !bExactElecCutoff ||
517 ( bConvertEwaldToCoulomb && r < rcoulomb) ||
518 (!bConvertEwaldToCoulomb && rC < rcoulomb);
520 if ( (qq[i] != 0) && bComputeElecInteraction)
524 case GMX_NBKERNEL_ELEC_COULOMB:
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.
531 Vcoul[i] -= qq[i]*fr->ic->c_rf;
534 case GMX_NBKERNEL_ELEC_REACTIONFIELD:
536 Vcoul[i] = qq[i]*(rinvC + krf*rC*rC-crf);
537 FscalC[i] = qq[i]*(rinvC - 2.0*krf*rC*rC);
540 case GMX_NBKERNEL_ELEC_CUBICSPLINETABLE:
541 /* non-Ewald tabulated coulomb */
545 Geps = epsC*VFtab[nnn+2];
546 Heps2 = eps2C*VFtab[nnn+3];
549 FF = Fp+Geps+2.0*Heps2;
551 FscalC[i] = -qq[i]*tabscale*FF*rC;
554 case GMX_NBKERNEL_ELEC_GENERALIZEDBORN:
555 gmx_fatal(FARGS, "Free energy and GB not implemented.\n");
558 case GMX_NBKERNEL_ELEC_EWALD:
559 if (bConvertEwaldToCoulomb)
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;
567 ewrt = rC*ewtabscale;
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]);
578 case GMX_NBKERNEL_ELEC_NONE:
584 gmx_incons("Invalid icoul in free energy kernel");
588 if (fr->coulomb_modifier == eintmodPOTSWITCH)
590 d = rC-fr->rcoulomb_switch;
591 d = (d > 0.0) ? d : 0.0;
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));
596 FscalC[i] = FscalC[i]*sw - rC*Vcoul[i]*dsw;
599 FscalC[i] = (rC < rcoulomb) ? FscalC[i] : 0.0;
600 Vcoul[i] = (rC < rcoulomb) ? Vcoul[i] : 0.0;
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.
609 bComputeVdwInteraction = !bExactVdwCutoff ||
610 ( bConvertLJEwaldToLJ6 && r < rvdw) ||
611 (!bConvertLJEwaldToLJ6 && rV < rvdw);
612 if ((c6[i] != 0 || c12[i] != 0) && bComputeVdwInteraction)
616 case GMX_NBKERNEL_VDW_LENNARDJONES:
618 if (sc_r_power == 6.0)
625 rinv6 = rinv6*rinv6*rinv6;
628 Vvdw12 = c12[i]*rinv6*rinv6;
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;
635 case GMX_NBKERNEL_VDW_BUCKINGHAM:
636 gmx_fatal(FARGS, "Buckingham free energy not supported.");
639 case GMX_NBKERNEL_VDW_CUBICSPLINETABLE:
645 Geps = epsV*VFtab[nnn+2];
646 Heps2 = eps2V*VFtab[nnn+3];
649 FF = Fp+Geps+2.0*Heps2;
651 FscalV[i] -= c6[i]*tabscale*FF*rV;
656 Geps = epsV*VFtab[nnn+6];
657 Heps2 = eps2V*VFtab[nnn+7];
660 FF = Fp+Geps+2.0*Heps2;
661 Vvdw[i] += c12[i]*VV;
662 FscalV[i] -= c12[i]*tabscale*FF*rV;
665 case GMX_NBKERNEL_VDW_LJEWALD:
666 if (sc_r_power == 6.0)
673 rinv6 = rinv6*rinv6*rinv6;
675 c6grid = nbfp_grid[tj[i]];
677 if (bConvertLJEwaldToLJ6)
681 Vvdw12 = c12[i]*rinv6*rinv6;
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;
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;
700 case GMX_NBKERNEL_VDW_NONE:
706 gmx_incons("Invalid ivdw in free energy kernel");
710 if (fr->vdw_modifier == eintmodPOTSWITCH)
712 d = rV-fr->rvdw_switch;
713 d = (d > 0.0) ? d : 0.0;
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));
718 FscalV[i] = FscalV[i]*sw - rV*Vvdw[i]*dsw;
721 FscalV[i] = (rV < rvdw) ? FscalV[i] : 0.0;
722 Vvdw[i] = (rV < rvdw) ? Vvdw[i] : 0.0;
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
736 /* Assemble A and B states */
737 for (i = 0; i < NSTATES; i++)
739 vctot += LFC[i]*Vcoul[i];
740 vvtot += LFV[i]*Vvdw[i];
742 Fscal += LFC[i]*FscalC[i]*rpm2;
743 Fscal += LFV[i]*FscalV[i]*rpm2;
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];
749 else if (icoul == GMX_NBKERNEL_ELEC_REACTIONFIELD)
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.
764 for (i = 0; i < NSTATES; i++)
766 vctot += LFC[i]*qq[i]*VV;
767 Fscal += LFC[i]*qq[i]*FF;
768 dvdl_coul += DLF[i]*qq[i]*VV;
772 if (bConvertEwaldToCoulomb && ( !bExactElecCutoff || r < rcoulomb ) )
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.
788 f_lr = ewtab[ewitab]+eweps*ewtab[ewitab+1];
789 v_lr = (ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+f_lr));
792 /* Note that any possible Ewald shift has already been applied in
793 * the normal interaction part above.
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.
806 for (i = 0; i < NSTATES; i++)
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;
814 if (bConvertLJEwaldToLJ6 && (!bExactVdwCutoff || r < rvdw))
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.
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.
831 rs = rsq*rinv*ewtabscale;
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.
839 VV = (tab_ewald_V_lj[ri] - ewtabhalfspace*frac*(tab_ewald_F_lj[ri] + f_lr))/6.0;
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.
851 for (i = 0; i < NSTATES; i++)
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;
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.
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.
886 if (npair_within_cutoff > 0)
902 fshift[is3+1] += fiy;
904 fshift[is3+2] += fiz;
918 dvdl[efptCOUL] += dvdl_coul;
920 dvdl[efptVDW] += dvdl_vdw;
922 /* Estimate flops, average for free energy stuff:
923 * 12 flops per outer iteration
924 * 150 flops per inner iteration
927 inc_nrnb(nrnb, eNR_NBKERNEL_FREE_ENERGY, nlist->nri*12 + nlist->jindex[n]*150);
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)
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];
954 if (sc_r_power == 6.0)
956 rpm2 = r2*r2; /* r4 */
957 rp = rpm2*r2; /* r6 */
959 else if (sc_r_power == 48.0)
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 */
969 rp = pow(r2, 0.5*sc_r_power); /* not currently supported as input, but can handle it */
973 /* Loop over state A(0) and B(1) */
974 for (i = 0; i < 2; i++)
976 if ((c6[i] > 0) && (c12[i] > 0))
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.
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 */
987 sigma6[i] = sigma6_min;
988 sigma2[i] = sigma2_min;
993 sigma6[i] = sigma6_def;
994 sigma2[i] = sigma2_def;
996 if (sc_r_power == 6.0)
998 sigma_pow[i] = sigma6[i];
999 sigma_powm2[i] = sigma6[i]/sigma2[i];
1001 else if (sc_r_power == 48.0)
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];
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]);
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))
1023 alpha_vdw_eff = alpha_vdw;
1024 alpha_coul_eff = alpha_coul;
1027 /* Loop over A and B states again */
1028 for (i = 0; i < 2; i++)
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) )
1039 rpinv = 1.0/(alpha_coul_eff*lfac_coul[i]*sigma_pow[i]+rp);
1040 r_coul = pow(rpinv, -1.0/sc_r_power);
1042 /* Electrostatics table lookup data */
1043 rtab = r_coul*tabscale;
1048 /* Electrostatics */
1051 Geps = eps*vftab[ntab+2];
1052 Heps2 = eps2*vftab[ntab+3];
1055 FF = Fp+Geps+2.0*Heps2;
1056 velec[i] = qq[i]*VV;
1057 fscal_elec[i] = -qq[i]*FF*r_coul*rpinv*tabscale;
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;
1071 Geps = eps*vftab[ntab+6];
1072 Heps2 = eps2*vftab[ntab+7];
1075 FF = Fp+Geps+2.0*Heps2;
1077 fscal_vdw[i] = -c6[i]*FF;
1082 Geps = eps*vftab[ntab+10];
1083 Heps2 = eps2*vftab[ntab+11];
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;
1092 /* Now we have velec[i], vvdw[i], and fscal[i] for both states */
1093 /* Assemble A and B states */
1099 for (i = 0; i < 2; i++)
1101 velecsum += LFC[i]*velec[i];
1102 vvdwsum += LFV[i]*vvdw[i];
1104 fscal += (LFC[i]*fscal_elec[i]+LFV[i]*fscal_vdw[i])*rpm2;
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];
1110 dvdl[efptCOUL] += dvdl_coul;
1111 dvdl[efptVDW] += dvdl_vdw;
1113 *velectot = velecsum;