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.
43 #include "gromacs/math/vec.h"
44 #include "gromacs/legacyheaders/typedefs.h"
45 #include "gromacs/legacyheaders/nonbonded.h"
46 #include "nb_kernel.h"
47 #include "gromacs/legacyheaders/nrnb.h"
48 #include "gromacs/legacyheaders/macros.h"
49 #include "nb_free_energy.h"
51 #include "gromacs/utility/fatalerror.h"
54 gmx_nb_free_energy_kernel(const t_nblist * gmx_restrict nlist,
55 rvec * gmx_restrict xx,
56 rvec * gmx_restrict ff,
57 t_forcerec * gmx_restrict fr,
58 const t_mdatoms * gmx_restrict mdatoms,
59 nb_kernel_data_t * gmx_restrict kernel_data,
60 t_nrnb * gmx_restrict nrnb)
66 int i, j, n, ii, is3, ii3, k, nj0, nj1, jnr, j3, ggid;
68 real tx, ty, tz, Fscal;
69 double FscalC[NSTATES], FscalV[NSTATES]; /* Needs double for sc_power==48 */
70 double Vcoul[NSTATES], Vvdw[NSTATES]; /* Needs double for sc_power==48 */
71 real rinv6, r, rt, rtC, rtV;
73 real qq[NSTATES], vctot, krsq;
74 int ntiA, ntiB, tj[NSTATES];
75 real Vvdw6, Vvdw12, vvtot;
76 real ix, iy, iz, fix, fiy, fiz;
77 real dx, dy, dz, rsq, rinv;
78 real c6[NSTATES], c12[NSTATES], c6grid;
79 real LFC[NSTATES], LFV[NSTATES], DLF[NSTATES];
80 double dvdl_coul, dvdl_vdw;
81 real lfac_coul[NSTATES], dlfac_coul[NSTATES], lfac_vdw[NSTATES], dlfac_vdw[NSTATES];
82 real sigma6[NSTATES], alpha_vdw_eff, alpha_coul_eff, sigma2_def, sigma2_min;
83 double rp, rpm2, rC, rV, rinvC, rpinvC, rinvV, rpinvV; /* Needs double for sc_power==48 */
84 real sigma2[NSTATES], sigma_pow[NSTATES], sigma_powm2[NSTATES], rs, rs2;
85 int do_tab, tab_elemsize;
86 int n0, n1C, n1V, nnn;
87 real Y, F, G, H, Fp, Geps, Heps2, epsC, eps2C, epsV, eps2V, VV, FF;
98 const real * shiftvec;
102 const real * VFtab = NULL;
105 real facel, krf, crf;
106 const real * chargeA;
107 const real * chargeB;
108 real sigma6_min, sigma6_def, lam_power, sc_power, sc_r_power;
109 real alpha_coul, alpha_vdw, lambda_coul, lambda_vdw, ewc_lj;
110 real ewcljrsq, ewclj, ewclj2, exponent, poly, vvdw_disp, vvdw_rep, sh_lj_ewald;
112 const real * nbfp, *nbfp_grid;
116 gmx_bool bDoForces, bDoShiftForces, bDoPotential;
117 real rcoulomb, rvdw, sh_invrc6;
118 gmx_bool bExactElecCutoff, bExactVdwCutoff, bExactCutoffAll;
119 gmx_bool bEwald, bEwaldLJ;
121 const real * tab_ewald_F_lj;
122 const real * tab_ewald_V_lj;
123 real d, d2, sw, dsw, rinvcorr;
124 real elec_swV3, elec_swV4, elec_swV5, elec_swF2, elec_swF3, elec_swF4;
125 real vdw_swV3, vdw_swV4, vdw_swV5, vdw_swF2, vdw_swF3, vdw_swF4;
126 gmx_bool bConvertEwaldToCoulomb, bConvertLJEwaldToLJ6;
127 gmx_bool bComputeVdwInteraction, bComputeElecInteraction;
130 real ewrt, eweps, ewtabscale, ewtabhalfspace, sh_ewald;
132 sh_ewald = fr->ic->sh_ewald;
133 ewtab = fr->ic->tabq_coul_FDV0;
134 ewtabscale = fr->ic->tabq_scale;
135 ewtabhalfspace = 0.5/ewtabscale;
136 tab_ewald_F_lj = fr->ic->tabq_vdw_F;
137 tab_ewald_V_lj = fr->ic->tabq_vdw_V;
142 fshift = fr->fshift[0];
146 jindex = nlist->jindex;
148 icoul = nlist->ielec;
150 shift = nlist->shift;
153 shiftvec = fr->shift_vec[0];
154 chargeA = mdatoms->chargeA;
155 chargeB = mdatoms->chargeB;
159 ewc_lj = fr->ewaldcoeff_lj;
160 Vc = kernel_data->energygrp_elec;
161 typeA = mdatoms->typeA;
162 typeB = mdatoms->typeB;
165 nbfp_grid = fr->ljpme_c6grid;
166 Vv = kernel_data->energygrp_vdw;
167 lambda_coul = kernel_data->lambda[efptCOUL];
168 lambda_vdw = kernel_data->lambda[efptVDW];
169 dvdl = kernel_data->dvdl;
170 alpha_coul = fr->sc_alphacoul;
171 alpha_vdw = fr->sc_alphavdw;
172 lam_power = fr->sc_power;
173 sc_r_power = fr->sc_r_power;
174 sigma6_def = fr->sc_sigma6_def;
175 sigma6_min = fr->sc_sigma6_min;
176 bDoForces = kernel_data->flags & GMX_NONBONDED_DO_FORCE;
177 bDoShiftForces = kernel_data->flags & GMX_NONBONDED_DO_SHIFTFORCE;
178 bDoPotential = kernel_data->flags & GMX_NONBONDED_DO_POTENTIAL;
180 rcoulomb = fr->rcoulomb;
182 sh_invrc6 = fr->ic->sh_invrc6;
183 sh_lj_ewald = fr->ic->sh_lj_ewald;
184 ewclj = fr->ewaldcoeff_lj;
185 ewclj2 = ewclj*ewclj;
186 ewclj6 = ewclj2*ewclj2*ewclj2;
188 if (fr->coulomb_modifier == eintmodPOTSWITCH)
190 d = fr->rcoulomb-fr->rcoulomb_switch;
191 elec_swV3 = -10.0/(d*d*d);
192 elec_swV4 = 15.0/(d*d*d*d);
193 elec_swV5 = -6.0/(d*d*d*d*d);
194 elec_swF2 = -30.0/(d*d*d);
195 elec_swF3 = 60.0/(d*d*d*d);
196 elec_swF4 = -30.0/(d*d*d*d*d);
200 /* Avoid warnings from stupid compilers (looking at you, Clang!) */
201 elec_swV3 = elec_swV4 = elec_swV5 = elec_swF2 = elec_swF3 = elec_swF4 = 0.0;
204 if (fr->vdw_modifier == eintmodPOTSWITCH)
206 d = fr->rvdw-fr->rvdw_switch;
207 vdw_swV3 = -10.0/(d*d*d);
208 vdw_swV4 = 15.0/(d*d*d*d);
209 vdw_swV5 = -6.0/(d*d*d*d*d);
210 vdw_swF2 = -30.0/(d*d*d);
211 vdw_swF3 = 60.0/(d*d*d*d);
212 vdw_swF4 = -30.0/(d*d*d*d*d);
216 /* Avoid warnings from stupid compilers (looking at you, Clang!) */
217 vdw_swV3 = vdw_swV4 = vdw_swV5 = vdw_swF2 = vdw_swF3 = vdw_swF4 = 0.0;
220 if (fr->cutoff_scheme == ecutsVERLET)
222 const interaction_const_t *ic;
225 if (EVDW_PME(ic->vdwtype))
227 ivdw = GMX_NBKERNEL_VDW_LJEWALD;
231 ivdw = GMX_NBKERNEL_VDW_LENNARDJONES;
234 if (ic->eeltype == eelCUT || EEL_RF(ic->eeltype))
236 icoul = GMX_NBKERNEL_ELEC_REACTIONFIELD;
238 else if (EEL_PME_EWALD(ic->eeltype))
240 icoul = GMX_NBKERNEL_ELEC_EWALD;
244 gmx_incons("Unsupported eeltype with Verlet and free-energy");
247 bExactElecCutoff = TRUE;
248 bExactVdwCutoff = TRUE;
252 bExactElecCutoff = (fr->coulomb_modifier != eintmodNONE) || fr->eeltype == eelRF_ZERO;
253 bExactVdwCutoff = (fr->vdw_modifier != eintmodNONE);
256 bExactCutoffAll = (bExactElecCutoff && bExactVdwCutoff);
257 rcutoff_max2 = max(fr->rcoulomb, fr->rvdw);
258 rcutoff_max2 = rcutoff_max2*rcutoff_max2;
260 bEwald = (icoul == GMX_NBKERNEL_ELEC_EWALD);
261 bEwaldLJ = (ivdw == GMX_NBKERNEL_VDW_LJEWALD);
263 /* For Ewald/PME interactions we cannot easily apply the soft-core component to
264 * reciprocal space. When we use vanilla (not switch/shift) Ewald interactions, we
265 * can apply the small trick of subtracting the _reciprocal_ space contribution
266 * in this kernel, and instead apply the free energy interaction to the 1/r
267 * (standard coulomb) interaction.
269 * However, we cannot use this approach for switch-modified since we would then
270 * effectively end up evaluating a significantly different interaction here compared to the
271 * normal (non-free-energy) kernels, either by applying a cutoff at a different
272 * position than what the user requested, or by switching different
273 * things (1/r rather than short-range Ewald). For these settings, we just
274 * use the traditional short-range Ewald interaction in that case.
276 bConvertEwaldToCoulomb = (bEwald && (fr->coulomb_modifier != eintmodPOTSWITCH));
277 /* For now the below will always be true (since LJ-PME only works with Shift in Gromacs-5.0),
278 * but writing it this way means we stay in sync with coulomb, and it avoids future bugs.
280 bConvertLJEwaldToLJ6 = (bEwaldLJ && (fr->vdw_modifier != eintmodPOTSWITCH));
282 /* We currently don't implement exclusion correction, needed with the Verlet cut-off scheme, without conversion */
283 if (fr->cutoff_scheme == ecutsVERLET &&
284 ((bEwald && !bConvertEwaldToCoulomb) ||
285 (bEwaldLJ && !bConvertLJEwaldToLJ6)))
287 gmx_incons("Unimplemented non-bonded setup");
290 /* fix compiler warnings */
299 /* Lambda factor for state A, 1-lambda*/
300 LFC[STATE_A] = 1.0 - lambda_coul;
301 LFV[STATE_A] = 1.0 - lambda_vdw;
303 /* Lambda factor for state B, lambda*/
304 LFC[STATE_B] = lambda_coul;
305 LFV[STATE_B] = lambda_vdw;
307 /*derivative of the lambda factor for state A and B */
311 for (i = 0; i < NSTATES; i++)
313 lfac_coul[i] = (lam_power == 2 ? (1-LFC[i])*(1-LFC[i]) : (1-LFC[i]));
314 dlfac_coul[i] = DLF[i]*lam_power/sc_r_power*(lam_power == 2 ? (1-LFC[i]) : 1);
315 lfac_vdw[i] = (lam_power == 2 ? (1-LFV[i])*(1-LFV[i]) : (1-LFV[i]));
316 dlfac_vdw[i] = DLF[i]*lam_power/sc_r_power*(lam_power == 2 ? (1-LFV[i]) : 1);
319 sigma2_def = pow(sigma6_def, 1.0/3.0);
320 sigma2_min = pow(sigma6_min, 1.0/3.0);
322 /* Ewald (not PME) table is special (icoul==enbcoulFEWALD) */
324 do_tab = (icoul == GMX_NBKERNEL_ELEC_CUBICSPLINETABLE ||
325 ivdw == GMX_NBKERNEL_VDW_CUBICSPLINETABLE);
328 tabscale = kernel_data->table_elec_vdw->scale;
329 VFtab = kernel_data->table_elec_vdw->data;
330 /* we always use the combined table here */
334 for (n = 0; (n < nri); n++)
336 int npair_within_cutoff;
338 npair_within_cutoff = 0;
342 shY = shiftvec[is3+1];
343 shZ = shiftvec[is3+2];
351 iqA = facel*chargeA[ii];
352 iqB = facel*chargeB[ii];
353 ntiA = 2*ntype*typeA[ii];
354 ntiB = 2*ntype*typeB[ii];
361 for (k = nj0; (k < nj1); k++)
368 rsq = dx*dx + dy*dy + dz*dz;
370 if (bExactCutoffAll && rsq >= rcutoff_max2)
372 /* We save significant time by skipping all code below.
373 * Note that with soft-core interactions, the actual cut-off
374 * check might be different. But since the soft-core distance
375 * is always larger than r, checking on r here is safe.
379 npair_within_cutoff++;
383 rinv = gmx_invsqrt(rsq);
388 /* The force at r=0 is zero, because of symmetry.
389 * But note that the potential is in general non-zero,
390 * since the soft-cored r will be non-zero.
396 if (sc_r_power == 6.0)
398 rpm2 = rsq*rsq; /* r4 */
399 rp = rpm2*rsq; /* r6 */
401 else if (sc_r_power == 48.0)
403 rp = rsq*rsq*rsq; /* r6 */
404 rp = rp*rp; /* r12 */
405 rp = rp*rp; /* r24 */
406 rp = rp*rp; /* r48 */
407 rpm2 = rp/rsq; /* r46 */
411 rp = pow(r, sc_r_power); /* not currently supported as input, but can handle it */
417 qq[STATE_A] = iqA*chargeA[jnr];
418 qq[STATE_B] = iqB*chargeB[jnr];
420 tj[STATE_A] = ntiA+2*typeA[jnr];
421 tj[STATE_B] = ntiB+2*typeB[jnr];
423 if (nlist->excl_fep == NULL || nlist->excl_fep[k])
425 c6[STATE_A] = nbfp[tj[STATE_A]];
426 c6[STATE_B] = nbfp[tj[STATE_B]];
428 for (i = 0; i < NSTATES; i++)
430 c12[i] = nbfp[tj[i]+1];
431 if ((c6[i] > 0) && (c12[i] > 0))
433 /* c12 is stored scaled with 12.0 and c6 is scaled with 6.0 - correct for this */
434 sigma6[i] = 0.5*c12[i]/c6[i];
435 sigma2[i] = pow(sigma6[i], 1.0/3.0);
436 /* should be able to get rid of this ^^^ internal pow call eventually. Will require agreement on
437 what data to store externally. Can't be fixed without larger scale changes, so not 4.6 */
438 if (sigma6[i] < sigma6_min) /* for disappearing coul and vdw with soft core at the same time */
440 sigma6[i] = sigma6_min;
441 sigma2[i] = sigma2_min;
446 sigma6[i] = sigma6_def;
447 sigma2[i] = sigma2_def;
449 if (sc_r_power == 6.0)
451 sigma_pow[i] = sigma6[i];
452 sigma_powm2[i] = sigma6[i]/sigma2[i];
454 else if (sc_r_power == 48.0)
456 sigma_pow[i] = sigma6[i]*sigma6[i]; /* sigma^12 */
457 sigma_pow[i] = sigma_pow[i]*sigma_pow[i]; /* sigma^24 */
458 sigma_pow[i] = sigma_pow[i]*sigma_pow[i]; /* sigma^48 */
459 sigma_powm2[i] = sigma_pow[i]/sigma2[i];
462 { /* not really supported as input, but in here for testing the general case*/
463 sigma_pow[i] = pow(sigma2[i], sc_r_power/2);
464 sigma_powm2[i] = sigma_pow[i]/(sigma2[i]);
468 /* only use softcore if one of the states has a zero endstate - softcore is for avoiding infinities!*/
469 if ((c12[STATE_A] > 0) && (c12[STATE_B] > 0))
476 alpha_vdw_eff = alpha_vdw;
477 alpha_coul_eff = alpha_coul;
480 for (i = 0; i < NSTATES; i++)
487 /* Only spend time on A or B state if it is non-zero */
488 if ( (qq[i] != 0) || (c6[i] != 0) || (c12[i] != 0) )
490 /* this section has to be inside the loop because of the dependence on sigma_pow */
491 rpinvC = 1.0/(alpha_coul_eff*lfac_coul[i]*sigma_pow[i]+rp);
492 rinvC = pow(rpinvC, 1.0/sc_r_power);
495 rpinvV = 1.0/(alpha_vdw_eff*lfac_vdw[i]*sigma_pow[i]+rp);
496 rinvV = pow(rpinvV, 1.0/sc_r_power);
505 n1C = tab_elemsize*n0;
511 n1V = tab_elemsize*n0;
514 /* Only process the coulomb interactions if we have charges,
515 * and if we either include all entries in the list (no cutoff
516 * used in the kernel), or if we are within the cutoff.
518 bComputeElecInteraction = !bExactElecCutoff ||
519 ( bConvertEwaldToCoulomb && r < rcoulomb) ||
520 (!bConvertEwaldToCoulomb && rC < rcoulomb);
522 if ( (qq[i] != 0) && bComputeElecInteraction)
526 case GMX_NBKERNEL_ELEC_COULOMB:
528 Vcoul[i] = qq[i]*rinvC;
529 FscalC[i] = Vcoul[i];
530 /* The shift for the Coulomb potential is stored in
531 * the RF parameter c_rf, which is 0 without shift.
533 Vcoul[i] -= qq[i]*fr->ic->c_rf;
536 case GMX_NBKERNEL_ELEC_REACTIONFIELD:
538 Vcoul[i] = qq[i]*(rinvC + krf*rC*rC-crf);
539 FscalC[i] = qq[i]*(rinvC - 2.0*krf*rC*rC);
542 case GMX_NBKERNEL_ELEC_CUBICSPLINETABLE:
543 /* non-Ewald tabulated coulomb */
547 Geps = epsC*VFtab[nnn+2];
548 Heps2 = eps2C*VFtab[nnn+3];
551 FF = Fp+Geps+2.0*Heps2;
553 FscalC[i] = -qq[i]*tabscale*FF*rC;
556 case GMX_NBKERNEL_ELEC_GENERALIZEDBORN:
557 gmx_fatal(FARGS, "Free energy and GB not implemented.\n");
560 case GMX_NBKERNEL_ELEC_EWALD:
561 if (bConvertEwaldToCoulomb)
563 /* Ewald FEP is done only on the 1/r part */
564 Vcoul[i] = qq[i]*(rinvC-sh_ewald);
565 FscalC[i] = qq[i]*rinvC;
569 ewrt = rC*ewtabscale;
573 FscalC[i] = ewtab[ewitab]+eweps*ewtab[ewitab+1];
574 rinvcorr = rinvC-sh_ewald;
575 Vcoul[i] = qq[i]*(rinvcorr-(ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+FscalC[i])));
576 FscalC[i] = qq[i]*(rinvC-rC*FscalC[i]);
580 case GMX_NBKERNEL_ELEC_NONE:
586 gmx_incons("Invalid icoul in free energy kernel");
590 if (fr->coulomb_modifier == eintmodPOTSWITCH)
592 d = rC-fr->rcoulomb_switch;
593 d = (d > 0.0) ? d : 0.0;
595 sw = 1.0+d2*d*(elec_swV3+d*(elec_swV4+d*elec_swV5));
596 dsw = d2*(elec_swF2+d*(elec_swF3+d*elec_swF4));
598 FscalC[i] = FscalC[i]*sw - rC*Vcoul[i]*dsw;
601 FscalC[i] = (rC < rcoulomb) ? FscalC[i] : 0.0;
602 Vcoul[i] = (rC < rcoulomb) ? Vcoul[i] : 0.0;
606 /* Only process the VDW interactions if we have
607 * some non-zero parameters, and if we either
608 * include all entries in the list (no cutoff used
609 * in the kernel), or if we are within the cutoff.
611 bComputeVdwInteraction = !bExactVdwCutoff ||
612 ( bConvertLJEwaldToLJ6 && r < rvdw) ||
613 (!bConvertLJEwaldToLJ6 && rV < rvdw);
614 if ((c6[i] != 0 || c12[i] != 0) && bComputeVdwInteraction)
618 case GMX_NBKERNEL_VDW_LENNARDJONES:
620 if (sc_r_power == 6.0)
627 rinv6 = rinv6*rinv6*rinv6;
630 Vvdw12 = c12[i]*rinv6*rinv6;
632 Vvdw[i] = ( (Vvdw12 - c12[i]*sh_invrc6*sh_invrc6)*(1.0/12.0)
633 - (Vvdw6 - c6[i]*sh_invrc6)*(1.0/6.0));
634 FscalV[i] = Vvdw12 - Vvdw6;
637 case GMX_NBKERNEL_VDW_BUCKINGHAM:
638 gmx_fatal(FARGS, "Buckingham free energy not supported.");
641 case GMX_NBKERNEL_VDW_CUBICSPLINETABLE:
647 Geps = epsV*VFtab[nnn+2];
648 Heps2 = eps2V*VFtab[nnn+3];
651 FF = Fp+Geps+2.0*Heps2;
653 FscalV[i] -= c6[i]*tabscale*FF*rV;
658 Geps = epsV*VFtab[nnn+6];
659 Heps2 = eps2V*VFtab[nnn+7];
662 FF = Fp+Geps+2.0*Heps2;
663 Vvdw[i] += c12[i]*VV;
664 FscalV[i] -= c12[i]*tabscale*FF*rV;
667 case GMX_NBKERNEL_VDW_LJEWALD:
668 if (sc_r_power == 6.0)
675 rinv6 = rinv6*rinv6*rinv6;
677 c6grid = nbfp_grid[tj[i]];
679 if (bConvertLJEwaldToLJ6)
683 Vvdw12 = c12[i]*rinv6*rinv6;
685 Vvdw[i] = ( (Vvdw12 - c12[i]*sh_invrc6*sh_invrc6)*(1.0/12.0)
686 - (Vvdw6 - c6[i]*sh_invrc6 - c6grid*sh_lj_ewald)*(1.0/6.0));
687 FscalV[i] = Vvdw12 - Vvdw6;
692 ewcljrsq = ewclj2*rV*rV;
693 exponent = exp(-ewcljrsq);
694 poly = exponent*(1.0 + ewcljrsq + ewcljrsq*ewcljrsq*0.5);
695 vvdw_disp = (c6[i]-c6grid*(1.0-poly))*rinv6;
696 vvdw_rep = c12[i]*rinv6*rinv6;
697 FscalV[i] = vvdw_rep - vvdw_disp - c6grid*(1.0/6.0)*exponent*ewclj6;
698 Vvdw[i] = (vvdw_rep - c12[i]*sh_invrc6*sh_invrc6)/12.0 - (vvdw_disp - c6[i]*sh_invrc6 - c6grid*sh_lj_ewald)/6.0;
702 case GMX_NBKERNEL_VDW_NONE:
708 gmx_incons("Invalid ivdw in free energy kernel");
712 if (fr->vdw_modifier == eintmodPOTSWITCH)
714 d = rV-fr->rvdw_switch;
715 d = (d > 0.0) ? d : 0.0;
717 sw = 1.0+d2*d*(vdw_swV3+d*(vdw_swV4+d*vdw_swV5));
718 dsw = d2*(vdw_swF2+d*(vdw_swF3+d*vdw_swF4));
720 FscalV[i] = FscalV[i]*sw - rV*Vvdw[i]*dsw;
723 FscalV[i] = (rV < rvdw) ? FscalV[i] : 0.0;
724 Vvdw[i] = (rV < rvdw) ? Vvdw[i] : 0.0;
728 /* FscalC (and FscalV) now contain: dV/drC * rC
729 * Now we multiply by rC^-p, so it will be: dV/drC * rC^1-p
730 * Further down we first multiply by r^p-2 and then by
731 * the vector r, which in total gives: dV/drC * (r/rC)^1-p
738 /* Assemble A and B states */
739 for (i = 0; i < NSTATES; i++)
741 vctot += LFC[i]*Vcoul[i];
742 vvtot += LFV[i]*Vvdw[i];
744 Fscal += LFC[i]*FscalC[i]*rpm2;
745 Fscal += LFV[i]*FscalV[i]*rpm2;
747 dvdl_coul += Vcoul[i]*DLF[i] + LFC[i]*alpha_coul_eff*dlfac_coul[i]*FscalC[i]*sigma_pow[i];
748 dvdl_vdw += Vvdw[i]*DLF[i] + LFV[i]*alpha_vdw_eff*dlfac_vdw[i]*FscalV[i]*sigma_pow[i];
751 else if (icoul == GMX_NBKERNEL_ELEC_REACTIONFIELD)
753 /* For excluded pairs, which are only in this pair list when
754 * using the Verlet scheme, we don't use soft-core.
755 * The group scheme also doesn't soft-core for these.
756 * As there is no singularity, there is no need for soft-core.
766 for (i = 0; i < NSTATES; i++)
768 vctot += LFC[i]*qq[i]*VV;
769 Fscal += LFC[i]*qq[i]*FF;
770 dvdl_coul += DLF[i]*qq[i]*VV;
774 if (bConvertEwaldToCoulomb && ( !bExactElecCutoff || r < rcoulomb ) )
776 /* See comment in the preamble. When using Ewald interactions
777 * (unless we use a switch modifier) we subtract the reciprocal-space
778 * Ewald component here which made it possible to apply the free
779 * energy interaction to 1/r (vanilla coulomb short-range part)
780 * above. This gets us closer to the ideal case of applying
781 * the softcore to the entire electrostatic interaction,
782 * including the reciprocal-space component.
790 f_lr = ewtab[ewitab]+eweps*ewtab[ewitab+1];
791 v_lr = (ewtab[ewitab+2]-ewtabhalfspace*eweps*(ewtab[ewitab]+f_lr));
794 /* Note that any possible Ewald shift has already been applied in
795 * the normal interaction part above.
800 /* If we get here, the i particle (ii) has itself (jnr)
801 * in its neighborlist. This can only happen with the Verlet
802 * scheme, and corresponds to a self-interaction that will
803 * occur twice. Scale it down by 50% to only include it once.
808 for (i = 0; i < NSTATES; i++)
810 vctot -= LFC[i]*qq[i]*v_lr;
811 Fscal -= LFC[i]*qq[i]*f_lr;
812 dvdl_coul -= (DLF[i]*qq[i])*v_lr;
816 if (bConvertLJEwaldToLJ6 && (!bExactVdwCutoff || r < rvdw))
818 /* See comment in the preamble. When using LJ-Ewald interactions
819 * (unless we use a switch modifier) we subtract the reciprocal-space
820 * Ewald component here which made it possible to apply the free
821 * energy interaction to r^-6 (vanilla LJ6 short-range part)
822 * above. This gets us closer to the ideal case of applying
823 * the softcore to the entire VdW interaction,
824 * including the reciprocal-space component.
826 /* We could also use the analytical form here
827 * iso a table, but that can cause issues for
828 * r close to 0 for non-interacting pairs.
833 rs = rsq*rinv*ewtabscale;
836 f_lr = (1 - frac)*tab_ewald_F_lj[ri] + frac*tab_ewald_F_lj[ri+1];
837 /* TODO: Currently the Ewald LJ table does not contain
838 * the factor 1/6, we should add this.
841 VV = (tab_ewald_V_lj[ri] - ewtabhalfspace*frac*(tab_ewald_F_lj[ri] + f_lr))/6.0;
845 /* If we get here, the i particle (ii) has itself (jnr)
846 * in its neighborlist. This can only happen with the Verlet
847 * scheme, and corresponds to a self-interaction that will
848 * occur twice. Scale it down by 50% to only include it once.
853 for (i = 0; i < NSTATES; i++)
855 c6grid = nbfp_grid[tj[i]];
856 vvtot += LFV[i]*c6grid*VV;
857 Fscal += LFV[i]*c6grid*FF;
858 dvdl_vdw += (DLF[i]*c6grid)*VV;
870 /* OpenMP atomics are expensive, but this kernels is also
871 * expensive, so we can take this hit, instead of using
872 * thread-local output buffers and extra reduction.
883 /* The atomics below are expensive with many OpenMP threads.
884 * Here unperturbed i-particles will usually only have a few
885 * (perturbed) j-particles in the list. Thus with a buffered list
886 * we can skip a significant number of i-reductions with a check.
888 if (npair_within_cutoff > 0)
904 fshift[is3+1] += fiy;
906 fshift[is3+2] += fiz;
920 dvdl[efptCOUL] += dvdl_coul;
922 dvdl[efptVDW] += dvdl_vdw;
924 /* Estimate flops, average for free energy stuff:
925 * 12 flops per outer iteration
926 * 150 flops per inner iteration
929 inc_nrnb(nrnb, eNR_NBKERNEL_FREE_ENERGY, nlist->nri*12 + nlist->jindex[n]*150);
933 nb_free_energy_evaluate_single(real r2, real sc_r_power, real alpha_coul, real alpha_vdw,
934 real tabscale, real *vftab,
935 real qqA, real c6A, real c12A, real qqB, real c6B, real c12B,
936 real LFC[2], real LFV[2], real DLF[2],
937 real lfac_coul[2], real lfac_vdw[2], real dlfac_coul[2], real dlfac_vdw[2],
938 real sigma6_def, real sigma6_min, real sigma2_def, real sigma2_min,
939 real *velectot, real *vvdwtot, real *dvdl)
941 real r, rp, rpm2, rtab, eps, eps2, Y, F, Geps, Heps2, Fp, VV, FF, fscal;
942 real qq[2], c6[2], c12[2], sigma6[2], sigma2[2], sigma_pow[2], sigma_powm2[2];
943 real alpha_coul_eff, alpha_vdw_eff, dvdl_coul, dvdl_vdw;
944 real rpinv, r_coul, r_vdw, velecsum, vvdwsum;
945 real fscal_vdw[2], fscal_elec[2];
946 real velec[2], vvdw[2];
956 if (sc_r_power == 6.0)
958 rpm2 = r2*r2; /* r4 */
959 rp = rpm2*r2; /* r6 */
961 else if (sc_r_power == 48.0)
963 rp = r2*r2*r2; /* r6 */
964 rp = rp*rp; /* r12 */
965 rp = rp*rp; /* r24 */
966 rp = rp*rp; /* r48 */
967 rpm2 = rp/r2; /* r46 */
971 rp = pow(r2, 0.5*sc_r_power); /* not currently supported as input, but can handle it */
975 /* Loop over state A(0) and B(1) */
976 for (i = 0; i < 2; i++)
978 if ((c6[i] > 0) && (c12[i] > 0))
980 /* The c6 & c12 coefficients now contain the constants 6.0 and 12.0, respectively.
981 * Correct for this by multiplying with (1/12.0)/(1/6.0)=6.0/12.0=0.5.
983 sigma6[i] = 0.5*c12[i]/c6[i];
984 sigma2[i] = pow(0.5*c12[i]/c6[i], 1.0/3.0);
985 /* should be able to get rid of this ^^^ internal pow call eventually. Will require agreement on
986 what data to store externally. Can't be fixed without larger scale changes, so not 5.0 */
987 if (sigma6[i] < sigma6_min) /* for disappearing coul and vdw with soft core at the same time */
989 sigma6[i] = sigma6_min;
990 sigma2[i] = sigma2_min;
995 sigma6[i] = sigma6_def;
996 sigma2[i] = sigma2_def;
998 if (sc_r_power == 6.0)
1000 sigma_pow[i] = sigma6[i];
1001 sigma_powm2[i] = sigma6[i]/sigma2[i];
1003 else if (sc_r_power == 48.0)
1005 sigma_pow[i] = sigma6[i]*sigma6[i]; /* sigma^12 */
1006 sigma_pow[i] = sigma_pow[i]*sigma_pow[i]; /* sigma^24 */
1007 sigma_pow[i] = sigma_pow[i]*sigma_pow[i]; /* sigma^48 */
1008 sigma_powm2[i] = sigma_pow[i]/sigma2[i];
1011 { /* not really supported as input, but in here for testing the general case*/
1012 sigma_pow[i] = pow(sigma2[i], sc_r_power/2);
1013 sigma_powm2[i] = sigma_pow[i]/(sigma2[i]);
1017 /* only use softcore if one of the states has a zero endstate - softcore is for avoiding infinities!*/
1018 if ((c12[0] > 0) && (c12[1] > 0))
1025 alpha_vdw_eff = alpha_vdw;
1026 alpha_coul_eff = alpha_coul;
1029 /* Loop over A and B states again */
1030 for (i = 0; i < 2; i++)
1037 /* Only spend time on A or B state if it is non-zero */
1038 if ( (qq[i] != 0) || (c6[i] != 0) || (c12[i] != 0) )
1041 rpinv = 1.0/(alpha_coul_eff*lfac_coul[i]*sigma_pow[i]+rp);
1042 r_coul = pow(rpinv, -1.0/sc_r_power);
1044 /* Electrostatics table lookup data */
1045 rtab = r_coul*tabscale;
1050 /* Electrostatics */
1053 Geps = eps*vftab[ntab+2];
1054 Heps2 = eps2*vftab[ntab+3];
1057 FF = Fp+Geps+2.0*Heps2;
1058 velec[i] = qq[i]*VV;
1059 fscal_elec[i] = -qq[i]*FF*r_coul*rpinv*tabscale;
1062 rpinv = 1.0/(alpha_vdw_eff*lfac_vdw[i]*sigma_pow[i]+rp);
1063 r_vdw = pow(rpinv, -1.0/sc_r_power);
1064 /* Vdw table lookup data */
1065 rtab = r_vdw*tabscale;
1073 Geps = eps*vftab[ntab+6];
1074 Heps2 = eps2*vftab[ntab+7];
1077 FF = Fp+Geps+2.0*Heps2;
1079 fscal_vdw[i] = -c6[i]*FF;
1084 Geps = eps*vftab[ntab+10];
1085 Heps2 = eps2*vftab[ntab+11];
1088 FF = Fp+Geps+2.0*Heps2;
1089 vvdw[i] += c12[i]*VV;
1090 fscal_vdw[i] -= c12[i]*FF;
1091 fscal_vdw[i] *= r_vdw*rpinv*tabscale;
1094 /* Now we have velec[i], vvdw[i], and fscal[i] for both states */
1095 /* Assemble A and B states */
1101 for (i = 0; i < 2; i++)
1103 velecsum += LFC[i]*velec[i];
1104 vvdwsum += LFV[i]*vvdw[i];
1106 fscal += (LFC[i]*fscal_elec[i]+LFV[i]*fscal_vdw[i])*rpm2;
1108 dvdl_coul += velec[i]*DLF[i] + LFC[i]*alpha_coul_eff*dlfac_coul[i]*fscal_elec[i]*sigma_pow[i];
1109 dvdl_vdw += vvdw[i]*DLF[i] + LFV[i]*alpha_vdw_eff*dlfac_vdw[i]*fscal_vdw[i]*sigma_pow[i];
1112 dvdl[efptCOUL] += dvdl_coul;
1113 dvdl[efptVDW] += dvdl_vdw;
1115 *velectot = velecsum;