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,2015,2016,2017 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
40 #include "forcetable.h"
46 #include "gromacs/fileio/xvgr.h"
47 #include "gromacs/math/functions.h"
48 #include "gromacs/math/multidimarray.h"
49 #include "gromacs/math/units.h"
50 #include "gromacs/math/utilities.h"
51 #include "gromacs/math/vec.h"
52 #include "gromacs/mdspan/extensions.h"
53 #include "gromacs/mdtypes/fcdata.h"
54 #include "gromacs/mdtypes/interaction_const.h"
55 #include "gromacs/mdtypes/md_enums.h"
56 #include "gromacs/mdtypes/nblist.h"
57 #include "gromacs/utility/arrayref.h"
58 #include "gromacs/utility/cstringutil.h"
59 #include "gromacs/utility/fatalerror.h"
60 #include "gromacs/utility/futil.h"
61 #include "gromacs/utility/smalloc.h"
63 /* All the possible (implemented) table functions */
87 /** Evaluates to true if the table type contains user data. */
88 #define ETAB_USER(e) ((e) == etabUSER || (e) == etabEwaldUser || (e) == etabEwaldUserSwitch)
96 /* This structure holds name and a flag that tells whether
97 this is a Coulomb type funtion */
98 static const t_tab_props tprops[etabNR] = {
99 { "LJ6", FALSE }, { "LJ12", FALSE }, { "LJ6Shift", FALSE },
100 { "LJ12Shift", FALSE }, { "Shift", TRUE }, { "RF", TRUE },
101 { "RF-zero", TRUE }, { "COUL", TRUE }, { "Ewald", TRUE },
102 { "Ewald-Switch", TRUE }, { "Ewald-User", TRUE }, { "Ewald-User-Switch", TRUE },
103 { "LJ6Ewald", FALSE }, { "LJ6Switch", FALSE }, { "LJ12Switch", FALSE },
104 { "COULSwitch", TRUE }, { "EXPMIN", FALSE }, { "USER", FALSE },
114 double v_q_ewald_lr(double beta, double r)
118 return beta * 2 / sqrt(M_PI);
122 return std::erf(beta * r) / r;
126 double v_lj_ewald_lr(double beta, double r)
128 double br, br2, br4, r6, factor;
131 return gmx::power6(beta) / 6;
139 factor = (1.0 - std::exp(-br2) * (1 + br2 + 0.5 * br4)) / r6;
144 EwaldCorrectionTables generateEwaldCorrectionTables(const int numPoints,
145 const double tableScaling,
147 real_space_grid_contribution_computer v_lr)
152 gmx_bool bOutOfRange;
153 double v_r0, v_r1, v_inrange, vi, a0, a1, a2dx;
156 /* This function is called using either v_ewald_lr or v_lj_ewald_lr as a function argument
157 * depending on wether we should create electrostatic or Lennard-Jones Ewald tables.
162 gmx_fatal(FARGS, "Can not make a spline table with less than 2 points");
165 const double dx = 1 / tableScaling;
167 EwaldCorrectionTables tables;
168 tables.scale = tableScaling;
169 tables.tableF.resize(numPoints);
170 tables.tableV.resize(numPoints);
171 tables.tableFDV0.resize(numPoints * 4);
172 gmx::ArrayRef<real> table_f = tables.tableF;
173 gmx::ArrayRef<real> table_v = tables.tableV;
174 gmx::ArrayRef<real> table_fdv0 = tables.tableFDV0;
176 /* We need some margin to be able to divide table values by r
177 * in the kernel and also to do the integration arithmetics
178 * without going out of range. Furthemore, we divide by dx below.
180 tab_max = GMX_REAL_MAX * 0.0001;
182 /* This function produces a table with:
183 * maximum energy error: V'''/(6*12*sqrt(3))*dx^3
184 * maximum force error: V'''/(6*4)*dx^2
185 * The rms force error is the max error times 1/sqrt(5)=0.45.
189 i_inrange = numPoints;
192 for (i = numPoints - 1; i >= 0; i--)
196 v_r0 = (*v_lr)(beta, x_r0);
207 /* Linear continuation for the last point in range */
208 vi = v_inrange - dc * (i - i_inrange) * dx;
218 /* Get the potential at table point i-1 */
219 v_r1 = (*v_lr)(beta, (i - 1) * dx);
221 if (v_r1 != v_r1 || v_r1 < -tab_max || v_r1 > tab_max)
228 /* Calculate the average second derivative times dx over interval i-1 to i.
229 * Using the function values at the end points and in the middle.
231 a2dx = (v_r0 + v_r1 - 2 * (*v_lr)(beta, x_r0 - 0.5 * dx)) / (0.25 * dx);
232 /* Set the derivative of the spline to match the difference in potential
233 * over the interval plus the average effect of the quadratic term.
234 * This is the essential step for minimizing the error in the force.
236 dc = (v_r0 - v_r1) / dx + 0.5 * a2dx;
239 if (i == numPoints - 1)
241 /* Fill the table with the force, minus the derivative of the spline */
246 /* tab[i] will contain the average of the splines over the two intervals */
247 table_f[i] += -0.5 * dc;
252 /* Make spline s(x) = a0 + a1*(x - xr) + 0.5*a2*(x - xr)^2
253 * matching the potential at the two end points
254 * and the derivative dc at the end point xr.
258 a2dx = (a1 * dx + v_r1 - a0) * 2 / dx;
260 /* Set dc to the derivative at the next point */
263 if (dc_new != dc_new || dc_new < -tab_max || dc_new > tab_max)
273 table_f[(i - 1)] = -0.5 * dc;
275 /* Currently the last value only contains half the force: double it */
278 if (!table_fdv0.empty())
280 /* Copy to FDV0 table too. Allocation occurs in forcerec.c,
281 * init_ewald_f_table().
283 for (i = 0; i < numPoints - 1; i++)
285 table_fdv0[4 * i] = table_f[i];
286 table_fdv0[4 * i + 1] = table_f[i + 1] - table_f[i];
287 table_fdv0[4 * i + 2] = table_v[i];
288 table_fdv0[4 * i + 3] = 0.0;
290 const int lastPoint = numPoints - 1;
291 table_fdv0[4 * lastPoint] = table_f[lastPoint];
292 table_fdv0[4 * lastPoint + 1] = -table_f[lastPoint];
293 table_fdv0[4 * lastPoint + 2] = table_v[lastPoint];
294 table_fdv0[4 * lastPoint + 3] = 0.0;
300 /* Returns the spacing for a function using the maximum of
301 * the third derivative, x_scale (unit 1/length)
302 * and function tolerance.
304 static double spline3_table_scale(double third_deriv_max, double x_scale, double func_tol)
307 double sc_deriv, sc_func;
309 /* Force tolerance: single precision accuracy */
310 deriv_tol = GMX_FLOAT_EPS;
311 sc_deriv = sqrt(third_deriv_max / (6 * 4 * deriv_tol * x_scale)) * x_scale;
313 /* Don't try to be more accurate on energy than the precision */
314 func_tol = std::max(func_tol, static_cast<double>(GMX_REAL_EPS));
315 sc_func = std::cbrt(third_deriv_max / (6 * 12 * std::sqrt(3.0) * func_tol)) * x_scale;
317 return std::max(sc_deriv, sc_func);
320 /* The scale (1/spacing) for third order spline interpolation
321 * of the Ewald mesh contribution which needs to be subtracted
322 * from the non-bonded interactions.
323 * Since there is currently only one spacing for Coulomb and LJ,
324 * the finest spacing is used if both Ewald types are present.
326 * Note that we could also implement completely separate tables
327 * for Coulomb and LJ Ewald, each with their own spacing.
328 * The current setup with the same spacing can provide slightly
329 * faster kernels with both Coulomb and LJ Ewald, especially
330 * when interleaving both tables (currently not implemented).
332 real ewald_spline3_table_scale(const interaction_const_t& ic,
333 const bool generateCoulombTables,
334 const bool generateVdwTables)
336 GMX_RELEASE_ASSERT(!generateCoulombTables || EEL_PME_EWALD(ic.eeltype),
337 "Can only use tables with Ewald");
338 GMX_RELEASE_ASSERT(!generateVdwTables || EVDW_PME(ic.vdwtype),
339 "Can only use tables with Ewald");
343 if (generateCoulombTables)
345 GMX_RELEASE_ASSERT(ic.ewaldcoeff_q > 0, "The Ewald coefficient shoule be positive");
347 double erf_x_d3 = 1.0522; /* max of (erf(x)/x)''' */
351 /* Energy tolerance: 0.1 times the cut-off jump */
352 etol = 0.1 * std::erfc(ic.ewaldcoeff_q * ic.rcoulomb);
354 sc_q = spline3_table_scale(erf_x_d3, ic.ewaldcoeff_q, etol);
358 fprintf(debug, "Ewald Coulomb quadratic spline table spacing: %f nm\n", 1 / sc_q);
361 sc = std::max(sc, sc_q);
364 if (generateVdwTables)
366 GMX_RELEASE_ASSERT(ic.ewaldcoeff_lj > 0, "The Ewald coefficient shoule be positive");
368 double func_d3 = 0.42888; /* max of (x^-6 (1 - exp(-x^2)(1+x^2+x^4/2)))''' */
372 /* Energy tolerance: 0.1 times the cut-off jump */
373 xrc2 = gmx::square(ic.ewaldcoeff_lj * ic.rvdw);
374 etol = 0.1 * std::exp(-xrc2) * (1 + xrc2 + xrc2 * xrc2 / 2.0);
376 sc_lj = spline3_table_scale(func_d3, ic.ewaldcoeff_lj, etol);
380 fprintf(debug, "Ewald LJ quadratic spline table spacing: %f nm\n", 1 / sc_lj);
383 sc = std::max(sc, sc_lj);
389 static void copy2table(int n,
398 /* Use double prec. for the intermediary variables
399 * and temporary x/vtab/vtab2 data to avoid unnecessary
406 for (i = 0; (i < n); i++)
412 G = 3 * (Vtab[i + 1] - Vtab[i]) + (Ftab[i + 1] + 2 * Ftab[i]) * h;
413 H = -2 * (Vtab[i + 1] - Vtab[i]) - (Ftab[i + 1] + Ftab[i]) * h;
417 /* Fill the last entry with a linear potential,
418 * this is mainly for rounding issues with angle and dihedral potentials.
424 nn0 = offset + i * stride;
425 dest[nn0] = scalefactor * Vtab[i];
426 dest[nn0 + 1] = scalefactor * F;
427 dest[nn0 + 2] = scalefactor * G;
428 dest[nn0 + 3] = scalefactor * H;
432 static void init_table(int n, int nx0, double tabscale, t_tabledata* td, gmx_bool bAlloc)
436 td->tabscale = tabscale;
445 static void spline_forces(int nx, double h, const double v[], gmx_bool bS3, gmx_bool bE3, double f[])
448 double v3, b_s, b_e, b;
451 /* Formulas can be found in:
452 * H.J.C. Berendsen, Simulating the Physical World, Cambridge 2007
455 if (nx < 4 && (bS3 || bE3))
458 "Can not generate splines with third derivative boundary conditions with less "
459 "than 4 (%d) points",
463 /* To make life easy we initially set the spacing to 1
464 * and correct for this at the end.
468 /* Fit V''' at the start */
469 v3 = v[3] - 3 * v[2] + 3 * v[1] - v[0];
472 fprintf(debug, "The left third derivative is %g\n", v3 / (h * h * h));
474 b_s = 2 * (v[1] - v[0]) + v3 / 6;
479 /* Fit V'' at the start */
482 v2 = -v[3] + 4 * v[2] - 5 * v[1] + 2 * v[0];
483 /* v2 = v[2] - 2*v[1] + v[0]; */
486 fprintf(debug, "The left second derivative is %g\n", v2 / (h * h));
488 b_s = 3 * (v[1] - v[0]) - v2 / 2;
494 b_s = 3 * (v[2] - v[0]) + f[0] * h;
499 /* Fit V''' at the end */
500 v3 = v[nx - 1] - 3 * v[nx - 2] + 3 * v[nx - 3] - v[nx - 4];
503 fprintf(debug, "The right third derivative is %g\n", v3 / (h * h * h));
505 b_e = 2 * (v[nx - 1] - v[nx - 2]) + v3 / 6;
510 /* V'=0 at the end */
511 b_e = 3 * (v[nx - 1] - v[nx - 3]) + f[nx - 1] * h;
516 beta = (bS3 ? 1 : 4);
518 /* For V'' fitting */
519 /* beta = (bS3 ? 2 : 4); */
521 f[start] = b_s / beta;
522 for (i = start + 1; i < end; i++)
526 b = 3 * (v[i + 1] - v[i - 1]);
527 f[i] = (b - f[i - 1]) / beta;
529 gamma[end - 1] = 1 / beta;
530 beta = (bE3 ? 1 : 4) - gamma[end - 1];
531 f[end - 1] = (b_e - f[end - 2]) / beta;
533 for (i = end - 2; i >= start; i--)
535 f[i] -= gamma[i + 1] * f[i + 1];
539 /* Correct for the minus sign and the spacing */
540 for (i = start; i < end; i++)
546 static void set_forces(FILE* fp, int angle, int nx, double h, double v[], double f[], int table)
552 gmx_fatal(FARGS, "Force generation for dihedral tables is not (yet) implemented");
556 while (v[start] == 0)
562 while (v[end - 1] == 0)
577 fprintf(fp, "Generating forces for table %d, boundary conditions: V''' at %g, %s at %g\n",
578 table + 1, start * h, end == nx ? "V'''" : "V'=0", (end - 1) * h);
580 spline_forces(end - start, h, v + start, TRUE, end == nx, f + start);
583 static void read_tables(FILE* fp, const char* filename, int ntab, int angle, t_tabledata td[])
586 double start, end, dx0, dx1, ssd, vm, vp, f, numf;
587 int k, i, nx0 = 0, nny, ns;
588 gmx_bool bAllZero, bZeroV, bZeroF;
592 std::string libfn = gmx::findLibraryFile(filename);
593 gmx::MultiDimArray<std::vector<double>, gmx::dynamicExtents2D> xvgData = readXvgData(libfn);
594 int numColumns = xvgData.extent(0);
595 if (numColumns != nny)
597 gmx_fatal(FARGS, "Trying to read file %s, but nr columns = %d, should be %d", libfn.c_str(),
600 int numRows = xvgData.extent(1);
602 const auto& yy = xvgData.asView();
607 gmx_fatal(FARGS, "The first distance in file %s is %f nm instead of %f nm",
608 libfn.c_str(), yy[0][0], 0.0);
622 if (yy[0][0] != start || yy[0][numRows - 1] != end)
624 gmx_fatal(FARGS, "The angles in file %s should go from %f to %f instead of %f to %f\n",
625 libfn.c_str(), start, end, yy[0][0], yy[0][numRows - 1]);
629 tabscale = (numRows - 1) / (yy[0][numRows - 1] - yy[0][0]);
633 fprintf(fp, "Read user tables from %s with %d data points.\n", libfn.c_str(), numRows);
636 fprintf(fp, "Tabscale = %g points/nm\n", tabscale);
641 for (k = 0; k < ntab; k++)
645 for (i = 0; (i < numRows); i++)
649 dx0 = yy[0][i - 1] - yy[0][i - 2];
650 dx1 = yy[0][i] - yy[0][i - 1];
651 /* Check for 1% deviation in spacing */
652 if (fabs(dx1 - dx0) >= 0.005 * (fabs(dx0) + fabs(dx1)))
655 "In table file '%s' the x values are not equally spaced: %f %f %f",
656 filename, yy[0][i - 2], yy[0][i - 1], yy[0][i]);
659 if (yy[1 + k * 2][i] != 0)
667 if (yy[1 + k * 2][i] > 0.01 * GMX_REAL_MAX || yy[1 + k * 2][i] < -0.01 * GMX_REAL_MAX)
669 gmx_fatal(FARGS, "Out of range potential value %g in file '%s'",
670 yy[1 + k * 2][i], filename);
673 if (yy[1 + k * 2 + 1][i] != 0)
681 if (yy[1 + k * 2 + 1][i] > 0.01 * GMX_REAL_MAX || yy[1 + k * 2 + 1][i] < -0.01 * GMX_REAL_MAX)
683 gmx_fatal(FARGS, "Out of range force value %g in file '%s'",
684 yy[1 + k * 2 + 1][i], filename);
689 if (!bZeroV && bZeroF)
691 set_forces(fp, angle, numRows, 1 / tabscale, yy[1 + k * 2].data(),
692 yy[1 + k * 2 + 1].data(), k);
696 /* Check if the second column is close to minus the numerical
697 * derivative of the first column.
701 for (i = 1; (i < numRows - 1); i++)
703 vm = yy[1 + 2 * k][i - 1];
704 vp = yy[1 + 2 * k][i + 1];
705 f = yy[1 + 2 * k + 1][i];
706 if (vm != 0 && vp != 0 && f != 0)
708 /* Take the centered difference */
709 numf = -(vp - vm) * 0.5 * tabscale;
712 ssd += fabs(2 * (f - numf) / (f + numf));
721 "For the %d non-zero entries for table %d in %s the forces deviate on "
723 "%% from minus the numerical derivative of the potential\n",
724 ns, k, libfn.c_str(), gmx::roundToInt64(100 * ssd));
727 fprintf(debug, "%s", buf);
733 fprintf(fp, "\nWARNING: %s\n", buf);
735 fprintf(stderr, "\nWARNING: %s\n", buf);
742 fprintf(fp, "\nNOTE: All elements in table %s are zero\n\n", libfn.c_str());
745 for (k = 0; (k < ntab); k++)
747 init_table(numRows, nx0, tabscale, &(td[k]), TRUE);
748 for (i = 0; (i < numRows); i++)
750 td[k].x[i] = yy[0][i];
751 td[k].v[i] = yy[2 * k + 1][i];
752 td[k].f[i] = yy[2 * k + 2][i];
757 static void done_tabledata(t_tabledata* td)
769 static void fill_table(t_tabledata* td, int tp, const interaction_const_t* ic, gmx_bool b14only)
771 /* Fill the table according to the formulas in the manual.
772 * In principle, we only need the potential and the second
773 * derivative, but then we would have to do lots of calculations
774 * in the inner loop. By precalculating some terms (see manual)
775 * we get better eventual performance, despite a larger table.
777 * Since some of these higher-order terms are very small,
778 * we always use double precision to calculate them here, in order
779 * to avoid unnecessary loss of precision.
783 double r1, rc, r12, r13;
784 double r, r2, r6, rc2;
785 double expr, Vtab, Ftab;
786 /* Parameters for David's function */
787 double A = 0, B = 0, C = 0, A_3 = 0, B_4 = 0;
788 /* Parameters for the switching function */
789 double ksw, swi, swi1;
790 /* Temporary parameters */
791 gmx_bool bPotentialSwitch, bForceSwitch, bPotentialShift;
792 double ewc = ic->ewaldcoeff_q;
793 double ewclj = ic->ewaldcoeff_lj;
798 bPotentialSwitch = FALSE;
799 bForceSwitch = FALSE;
800 bPotentialShift = FALSE;
804 bPotentialSwitch = ((tp == etabLJ6Switch) || (tp == etabLJ12Switch) || (tp == etabCOULSwitch)
805 || (tp == etabEwaldSwitch) || (tp == etabEwaldUserSwitch)
806 || (tprops[tp].bCoulomb && (ic->coulomb_modifier == eintmodPOTSWITCH))
807 || (!tprops[tp].bCoulomb && (ic->vdw_modifier == eintmodPOTSWITCH)));
808 bForceSwitch = ((tp == etabLJ6Shift) || (tp == etabLJ12Shift) || (tp == etabShift)
809 || (tprops[tp].bCoulomb && (ic->coulomb_modifier == eintmodFORCESWITCH))
810 || (!tprops[tp].bCoulomb && (ic->vdw_modifier == eintmodFORCESWITCH)));
811 bPotentialShift = ((tprops[tp].bCoulomb && (ic->coulomb_modifier == eintmodPOTSHIFT))
812 || (!tprops[tp].bCoulomb && (ic->vdw_modifier == eintmodPOTSHIFT)));
817 if (tprops[tp].bCoulomb)
819 r1 = ic->rcoulomb_switch;
824 r1 = ic->rvdw_switch;
827 if (bPotentialSwitch)
829 ksw = 1.0 / (gmx::power5(rc - r1));
841 else if (tp == etabLJ6Shift)
850 A = p * ((p + 1) * r1 - (p + 4) * rc) / (std::pow(rc, p + 2) * gmx::square(rc - r1));
851 B = -p * ((p + 1) * r1 - (p + 3) * rc) / (std::pow(rc, p + 2) * gmx::power3(rc - r1));
852 C = 1.0 / std::pow(rc, p) - A / 3.0 * gmx::power3(rc - r1) - B / 4.0 * gmx::power4(rc - r1);
853 if (tp == etabLJ6Shift)
864 fprintf(debug, "Setting up tables\n");
871 double rc6 = 1.0 / (rc2 * rc2 * rc2);
873 if (gmx_within_tol(reppow, 12.0, 10 * GMX_DOUBLE_EPS))
879 rc12 = std::pow(rc, -reppow);
889 Vcut = -rc6 * exp(-ewclj * ewclj * rc2)
890 * (1 + ewclj * ewclj * rc2 + gmx::power4(ewclj) * rc2 * rc2 / 2);
896 case etabCOUL: Vcut = 1.0 / rc; break;
898 case etabEwaldSwitch: Vcut = std::erfc(ewc * rc) / rc; break;
900 /* Only calculate minus the reciprocal space contribution */
901 Vcut = -std::erf(ewc * rc) / rc;
905 /* No need for preventing the usage of modifiers with RF */
908 case etabEXPMIN: Vcut = exp(-rc); break;
911 "Cannot apply new potential-shift modifier to interaction type '%s' yet. "
913 tprops[tp].name, __FILE__, __LINE__);
917 for (i = 0; (i < td->nx); i++)
919 td->x[i] = i / td->tabscale;
921 for (i = td->nx0; (i < td->nx); i++)
925 r6 = 1.0 / (r2 * r2 * r2);
926 if (gmx_within_tol(reppow, 12.0, 10 * GMX_DOUBLE_EPS))
932 r12 = std::pow(r, -reppow);
936 if (bPotentialSwitch)
938 /* swi is function, swi1 1st derivative and swi2 2nd derivative */
939 /* The switch function is 1 for r<r1, 0 for r>rc, and smooth for
940 * r1<=r<=rc. The 1st and 2nd derivatives are both zero at
942 * ksw is just the constant 1/(rc-r1)^5, to save some calculations...
956 swi = 1 - 10 * gmx::power3(r - r1) * ksw * gmx::square(rc - r1)
957 + 15 * gmx::power4(r - r1) * ksw * (rc - r1) - 6 * gmx::power5(r - r1) * ksw;
958 swi1 = -30 * gmx::square(r - r1) * ksw * gmx::square(rc - r1)
959 + 60 * gmx::power3(r - r1) * ksw * (rc - r1) - 30 * gmx::power4(r - r1) * ksw;
962 else /* not really needed, but avoids compiler warnings... */
973 Ftab = 6.0 * Vtab / r;
981 Ftab = 6.0 * Vtab / r;
988 Ftab = reppow * Vtab / r;
996 Ftab = reppow * Vtab / r;
1003 case etabCOULSwitch:
1012 case etabEwaldSwitch:
1013 Vtab = std::erfc(ewc * r) / r;
1014 Ftab = std::erfc(ewc * r) / r2 + exp(-(ewc * ewc * r2)) * ewc * M_2_SQRTPI / r;
1017 case etabEwaldUserSwitch:
1018 /* Only calculate the negative of the reciprocal space contribution */
1019 Vtab = -std::erf(ewc * r) / r;
1020 Ftab = -std::erf(ewc * r) / r2 + exp(-(ewc * ewc * r2)) * ewc * M_2_SQRTPI / r;
1023 Vtab = -r6 * exp(-ewclj * ewclj * r2)
1024 * (1 + ewclj * ewclj * r2 + gmx::power4(ewclj) * r2 * r2 / 2);
1025 Ftab = 6.0 * Vtab / r
1026 - r6 * exp(-ewclj * ewclj * r2) * gmx::power5(ewclj) * ewclj * r2 * r2 * r;
1030 Vtab = 1.0 / r + ic->k_rf * r2 - ic->c_rf;
1031 Ftab = 1.0 / r2 - 2 * ic->k_rf * r;
1032 if (tp == etabRF_ZERO && r >= rc)
1044 gmx_fatal(FARGS, "Table type %d not implemented yet. (%s,%d)", tp, __FILE__, __LINE__);
1048 /* Normal coulomb with cut-off correction for potential */
1052 /* If in Shifting range add something to it */
1055 r12 = (r - r1) * (r - r1);
1056 r13 = (r - r1) * r12;
1057 Vtab += -A_3 * r13 - B_4 * r12 * r12;
1058 Ftab += A * r12 + B * r13;
1063 /* Make sure interactions are zero outside cutoff with modifiers */
1068 if (bPotentialShift)
1076 /* Make sure interactions are zero outside cutoff with modifiers */
1088 if (bPotentialSwitch)
1092 /* Make sure interactions are zero outside cutoff with modifiers */
1098 Ftab = Ftab * swi - Vtab * swi1;
1102 /* Convert to single precision when we store to mem */
1107 /* Continue the table linearly from nx0 to 0.
1108 * These values are only required for energy minimization with overlap or TPI.
1110 for (i = td->nx0 - 1; i >= 0; i--)
1112 td->v[i] = td->v[i + 1] + td->f[i + 1] * (td->x[i + 1] - td->x[i]);
1113 td->f[i] = td->f[i + 1];
1117 static void set_table_type(int tabsel[], const interaction_const_t* ic, gmx_bool b14only)
1119 int eltype, vdwtype;
1121 /* Set the different table indices.
1128 switch (ic->eeltype)
1132 case eelPMEUSERSWITCH: eltype = eelUSER; break;
1133 default: eltype = eelCUT;
1138 eltype = ic->eeltype;
1143 case eelCUT: tabsel[etiCOUL] = etabCOUL; break;
1144 case eelPOISSON: tabsel[etiCOUL] = etabShift; break;
1146 if (ic->rcoulomb > ic->rcoulomb_switch)
1148 tabsel[etiCOUL] = etabShift;
1152 tabsel[etiCOUL] = etabCOUL;
1157 case eelP3M_AD: tabsel[etiCOUL] = etabEwald; break;
1158 case eelPMESWITCH: tabsel[etiCOUL] = etabEwaldSwitch; break;
1159 case eelPMEUSER: tabsel[etiCOUL] = etabEwaldUser; break;
1160 case eelPMEUSERSWITCH: tabsel[etiCOUL] = etabEwaldUserSwitch; break;
1162 case eelRF_ZERO: tabsel[etiCOUL] = etabRF_ZERO; break;
1163 case eelSWITCH: tabsel[etiCOUL] = etabCOULSwitch; break;
1164 case eelUSER: tabsel[etiCOUL] = etabUSER; break;
1165 default: gmx_fatal(FARGS, "Invalid eeltype %d", eltype);
1168 /* Van der Waals time */
1169 if (ic->useBuckingham && !b14only)
1171 tabsel[etiLJ6] = etabLJ6;
1172 tabsel[etiLJ12] = etabEXPMIN;
1176 if (b14only && ic->vdwtype != evdwUSER)
1182 vdwtype = ic->vdwtype;
1188 tabsel[etiLJ6] = etabLJ6Switch;
1189 tabsel[etiLJ12] = etabLJ12Switch;
1192 tabsel[etiLJ6] = etabLJ6Shift;
1193 tabsel[etiLJ12] = etabLJ12Shift;
1196 tabsel[etiLJ6] = etabUSER;
1197 tabsel[etiLJ12] = etabUSER;
1200 tabsel[etiLJ6] = etabLJ6;
1201 tabsel[etiLJ12] = etabLJ12;
1204 tabsel[etiLJ6] = etabLJ6Ewald;
1205 tabsel[etiLJ12] = etabLJ12;
1208 gmx_fatal(FARGS, "Invalid vdwtype %d in %s line %d", vdwtype, __FILE__, __LINE__);
1211 if (!b14only && ic->vdw_modifier != eintmodNONE)
1213 if (ic->vdw_modifier != eintmodPOTSHIFT && ic->vdwtype != evdwCUT)
1216 "Potential modifiers other than potential-shift are only implemented for "
1220 /* LJ-PME and other (shift-only) modifiers are handled by applying the modifiers
1221 * to the original interaction forms when we fill the table, so we only check cutoffs here.
1223 if (ic->vdwtype == evdwCUT)
1225 switch (ic->vdw_modifier)
1228 case eintmodPOTSHIFT:
1229 case eintmodEXACTCUTOFF:
1230 /* No modification */
1232 case eintmodPOTSWITCH:
1233 tabsel[etiLJ6] = etabLJ6Switch;
1234 tabsel[etiLJ12] = etabLJ12Switch;
1236 case eintmodFORCESWITCH:
1237 tabsel[etiLJ6] = etabLJ6Shift;
1238 tabsel[etiLJ12] = etabLJ12Shift;
1240 default: gmx_incons("Unsupported vdw_modifier");
1247 t_forcetable* make_tables(FILE* out, const interaction_const_t* ic, const char* fn, real rtab, int flags)
1250 gmx_bool b14only, useUserTable;
1251 int nx0, tabsel[etiNR];
1254 t_forcetable* table = new t_forcetable(GMX_TABLE_INTERACTION_ELEC_VDWREP_VDWDISP,
1255 GMX_TABLE_FORMAT_CUBICSPLINE_YFGH);
1257 b14only = ((flags & GMX_MAKETABLES_14ONLY) != 0);
1259 if (flags & GMX_MAKETABLES_FORCEUSER)
1261 tabsel[etiCOUL] = etabUSER;
1262 tabsel[etiLJ6] = etabUSER;
1263 tabsel[etiLJ12] = etabUSER;
1267 set_table_type(tabsel, ic, b14only);
1274 table->formatsize = 4;
1275 table->ninteractions = etiNR;
1276 table->stride = table->formatsize * table->ninteractions;
1278 /* Check whether we have to read or generate */
1279 useUserTable = FALSE;
1280 for (unsigned int i = 0; (i < etiNR); i++)
1282 if (ETAB_USER(tabsel[i]))
1284 useUserTable = TRUE;
1289 read_tables(out, fn, etiNR, 0, td);
1290 if (rtab == 0 || (flags & GMX_MAKETABLES_14ONLY))
1292 table->n = td[0].nx;
1296 if (td[0].x[td[0].nx - 1] < rtab)
1299 "Tables in file %s not long enough for cut-off:\n"
1300 "\tshould be at least %f nm\n",
1303 table->n = gmx::roundToInt(rtab * td[0].tabscale);
1305 table->scale = td[0].tabscale;
1310 // No tables are read
1312 table->scale = 2000.0;
1314 table->scale = 500.0;
1316 table->n = static_cast<int>(rtab * table->scale);
1320 /* Each table type (e.g. coul,lj6,lj12) requires four
1321 * numbers per table->n+1 data points. For performance reasons we want
1322 * the table data to be aligned to (at least) a 32-byte boundary.
1324 table->data.resize(table->stride * (table->n + 1) * sizeof(real));
1326 for (int k = 0; (k < etiNR); k++)
1328 /* Now fill data for tables that have not been read
1329 * or add the Ewald long-range correction for Ewald user tables.
1331 if (tabsel[k] != etabUSER)
1333 real scale = table->scale;
1334 if (ic->useBuckingham && (ic->buckinghamBMax != 0) && tabsel[k] == etabEXPMIN)
1336 scale /= ic->buckinghamBMax;
1338 init_table(table->n, nx0, scale, &(td[k]), !useUserTable);
1340 fill_table(&(td[k]), tabsel[k], ic, b14only);
1344 "Generated table with %d data points for %s%s.\n"
1345 "Tabscale = %g points/nm\n",
1346 td[k].nx, b14only ? "1-4 " : "", tprops[tabsel[k]].name, td[k].tabscale);
1350 /* Set scalefactor for c6/c12 tables. This is because we save flops in the non-table kernels
1351 * by including the derivative constants (6.0 or 12.0) in the parameters, since
1352 * we no longer calculate force in most steps. This means the c6/c12 parameters
1353 * have been scaled up, so we need to scale down the table interactions too.
1354 * It comes here since we need to scale user tables too.
1358 scalefactor = 1.0 / 6.0;
1360 else if (k == etiLJ12 && tabsel[k] != etabEXPMIN)
1362 scalefactor = 1.0 / 12.0;
1369 copy2table(table->n, k * table->formatsize, table->stride, td[k].x, td[k].v, td[k].f,
1370 scalefactor, table->data.data());
1372 done_tabledata(&(td[k]));
1379 bondedtable_t make_bonded_table(FILE* fplog, const char* fn, int angle)
1386 read_tables(fplog, fn, 1, angle, &td);
1389 /* Convert the table from degrees to radians */
1390 for (i = 0; i < td.nx; i++)
1395 td.tabscale *= RAD2DEG;
1398 tab.scale = td.tabscale;
1399 tab.data.resize(tab.n * stride);
1400 copy2table(tab.n, 0, stride, td.x, td.v, td.f, 1.0, tab.data.data());
1401 done_tabledata(&td);
1406 std::unique_ptr<t_forcetable>
1407 makeDispersionCorrectionTable(FILE* fp, const interaction_const_t* ic, real rtab, const char* tabfn)
1409 GMX_RELEASE_ASSERT(ic->vdwtype != evdwUSER || tabfn,
1410 "With VdW user tables we need a table file name");
1412 if (tabfn == nullptr)
1414 return std::unique_ptr<t_forcetable>(nullptr);
1417 t_forcetable* fullTable = make_tables(fp, ic, tabfn, rtab, 0);
1418 /* Copy the contents of the table to one that has just dispersion
1419 * and repulsion, to improve cache performance. We want the table
1420 * data to be aligned to 32-byte boundaries.
1422 std::unique_ptr<t_forcetable> dispersionCorrectionTable =
1423 std::make_unique<t_forcetable>(GMX_TABLE_INTERACTION_VDWREP_VDWDISP, fullTable->format);
1424 dispersionCorrectionTable->r = fullTable->r;
1425 dispersionCorrectionTable->n = fullTable->n;
1426 dispersionCorrectionTable->scale = fullTable->scale;
1427 dispersionCorrectionTable->formatsize = fullTable->formatsize;
1428 dispersionCorrectionTable->ninteractions = 2;
1429 dispersionCorrectionTable->stride =
1430 dispersionCorrectionTable->formatsize * dispersionCorrectionTable->ninteractions;
1431 dispersionCorrectionTable->data.resize(dispersionCorrectionTable->stride
1432 * (dispersionCorrectionTable->n + 1));
1434 for (int i = 0; i <= fullTable->n; i++)
1436 for (int j = 0; j < 8; j++)
1438 dispersionCorrectionTable->data[8 * i + j] = fullTable->data[12 * i + 4 + j];
1443 return dispersionCorrectionTable;
1446 t_forcetable::t_forcetable(enum gmx_table_interaction interaction, enum gmx_table_format format) :
1447 interaction(interaction),