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,2017,2018 by the GROMACS development team.
7 * Copyright (c) 2019,2020,2021, 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 * \brief This file contains function definitions necessary for
41 * computing energies and forces for the plain-Ewald long-ranged part,
42 * and the correction for overall system charge for all Ewald-family
45 * \author David van der Spoel <david.vanderspoel@icm.uu.se>
46 * \author Mark Abraham <mark.j.abraham@gmail.com>
47 * \ingroup module_ewald
59 #include "gromacs/ewald/ewald_utils.h"
60 #include "gromacs/math/functions.h"
61 #include "gromacs/math/gmxcomplex.h"
62 #include "gromacs/math/units.h"
63 #include "gromacs/math/utilities.h"
64 #include "gromacs/math/vec.h"
65 #include "gromacs/math/vectypes.h"
66 #include "gromacs/mdtypes/commrec.h"
67 #include "gromacs/mdtypes/forcerec.h"
68 #include "gromacs/mdtypes/inputrec.h"
69 #include "gromacs/mdtypes/interaction_const.h"
70 #include "gromacs/mdtypes/md_enums.h"
71 #include "gromacs/utility/arrayref.h"
72 #include "gromacs/utility/fatalerror.h"
73 #include "gromacs/utility/smalloc.h"
75 using cvec = std::array<t_complex, DIM>;
77 gmx_ewald_tab_t::gmx_ewald_tab_t(const t_inputrec& ir, FILE* fp)
81 fprintf(fp, "Will do ordinary reciprocal space Ewald sum.\n");
87 kmax = std::max(nx, std::max(ny, nz));
90 gmx_ewald_tab_t::~gmx_ewald_tab_t() = default;
92 //! Calculates wave vectors.
93 static void calc_lll(const rvec box, rvec lll)
95 lll[XX] = 2.0 * M_PI / box[XX];
96 lll[YY] = 2.0 * M_PI / box[YY];
97 lll[ZZ] = 2.0 * M_PI / box[ZZ];
100 //! Make tables for the structure factor parts
101 static void tabulateStructureFactors(int natom, gmx::ArrayRef<const gmx::RVec> x, int kmax, cvec** eir, const rvec lll)
107 printf("Go away! kmax = %d\n", kmax);
111 for (i = 0; (i < natom); i++)
113 for (m = 0; (m < 3); m++)
119 for (m = 0; (m < 3); m++)
121 eir[1][i][m].re = std::cos(x[i][m] * lll[m]);
122 eir[1][i][m].im = std::sin(x[i][m] * lll[m]);
124 for (j = 2; (j < kmax); j++)
126 for (m = 0; (m < 3); m++)
128 eir[j][i][m] = cmul(eir[j - 1][i][m], eir[1][i][m]);
134 real do_ewald(const t_inputrec& ir,
135 gmx::ArrayRef<const gmx::RVec> x,
136 gmx::ArrayRef<gmx::RVec> f,
137 gmx::ArrayRef<const real> chargeA,
138 gmx::ArrayRef<const real> chargeB,
148 real factor = -1.0 / (4 * ewaldcoeff * ewaldcoeff);
149 real energy_AB[2], energy;
151 int lowiy, lowiz, ix, iy, iz, n, q;
152 real tmp, cs, ss, ak, akv, mx, my, mz, m2, scale;
160 gmx_fatal(FARGS, "No parallel Ewald. Use PME instead.\n");
164 /* Scale box with Ewald wall factor */
166 EwaldBoxZScaler boxScaler(inputrecPbcXY2Walls(&ir), ir.wall_ewald_zfac);
167 boxScaler.scaleBox(box, scaledBox);
170 for (int i = 0; (i < DIM); i++)
172 boxDiag[i] = scaledBox[i][i];
177 4.0 * M_PI / (boxDiag[XX] * boxDiag[YY] * boxDiag[ZZ]) * gmx::c_one4PiEps0 / ir.epsilon_r;
180 for (n = 0; n < et->kmax; n++)
182 snew(eir[n], natoms);
184 et->tab_xy.resize(natoms);
185 et->tab_qxyz.resize(natoms);
187 bFreeEnergy = (ir.efep != FreeEnergyPerturbationType::No);
191 calc_lll(boxDiag, lll);
192 tabulateStructureFactors(natoms, x, et->kmax, eir, lll);
194 gmx::ArrayRef<const real> charge;
195 for (q = 0; q < (bFreeEnergy ? 2 : 1); q++)
205 scale = 1.0 - lambda;
215 for (ix = 0; ix < et->nx; ix++)
218 for (iy = lowiy; iy < et->ny; iy++)
223 for (n = 0; n < natoms; n++)
225 et->tab_xy[n] = cmul(eir[ix][n][XX], eir[iy][n][YY]);
230 for (n = 0; n < natoms; n++)
232 et->tab_xy[n] = cmul(eir[ix][n][XX], conjugate(eir[-iy][n][YY]));
235 for (iz = lowiz; iz < et->nz; iz++)
238 m2 = mx * mx + my * my + mz * mz;
239 ak = std::exp(m2 * factor) / m2;
240 akv = 2.0 * ak * (1.0 / m2 - factor);
243 for (n = 0; n < natoms; n++)
245 et->tab_qxyz[n] = rcmul(charge[n], cmul(et->tab_xy[n], eir[iz][n][ZZ]));
250 for (n = 0; n < natoms; n++)
253 rcmul(charge[n], cmul(et->tab_xy[n], conjugate(eir[-iz][n][ZZ])));
258 for (n = 0; n < natoms; n++)
260 cs += et->tab_qxyz[n].re;
261 ss += et->tab_qxyz[n].im;
263 energy_AB[q] += ak * (cs * cs + ss * ss);
264 tmp = scale * akv * (cs * cs + ss * ss);
265 lrvir[XX][XX] -= tmp * mx * mx;
266 lrvir[XX][YY] -= tmp * mx * my;
267 lrvir[XX][ZZ] -= tmp * mx * mz;
268 lrvir[YY][YY] -= tmp * my * my;
269 lrvir[YY][ZZ] -= tmp * my * mz;
270 lrvir[ZZ][ZZ] -= tmp * mz * mz;
271 for (n = 0; n < natoms; n++)
273 /*tmp=scale*ak*(cs*tab_qxyz[n].im-ss*tab_qxyz[n].re);*/
274 tmp = scale * ak * (cs * et->tab_qxyz[n].im - ss * et->tab_qxyz[n].re);
275 f[n][XX] += tmp * mx * 2 * scaleRecip;
276 f[n][YY] += tmp * my * 2 * scaleRecip;
277 f[n][ZZ] += tmp * mz * 2 * scaleRecip;
293 energy = energy_AB[0];
297 energy = (1.0 - lambda) * energy_AB[0] + lambda * energy_AB[1];
298 *dvdlambda += scaleRecip * (energy_AB[1] - energy_AB[0]);
301 lrvir[XX][XX] = -0.5 * scaleRecip * (lrvir[XX][XX] + energy);
302 lrvir[XX][YY] = -0.5 * scaleRecip * (lrvir[XX][YY]);
303 lrvir[XX][ZZ] = -0.5 * scaleRecip * (lrvir[XX][ZZ]);
304 lrvir[YY][YY] = -0.5 * scaleRecip * (lrvir[YY][YY] + energy);
305 lrvir[YY][ZZ] = -0.5 * scaleRecip * (lrvir[YY][ZZ]);
306 lrvir[ZZ][ZZ] = -0.5 * scaleRecip * (lrvir[ZZ][ZZ] + energy);
308 lrvir[YY][XX] = lrvir[XX][YY];
309 lrvir[ZZ][XX] = lrvir[XX][ZZ];
310 lrvir[ZZ][YY] = lrvir[YY][ZZ];
312 energy *= scaleRecip;
317 real ewald_charge_correction(const t_commrec* commrec,
319 const real ewaldcoeffQ,
320 gmx::ArrayRef<const double> qsum,
331 /* Apply charge correction */
332 real vol = box[XX][XX] * box[YY][YY] * box[ZZ][ZZ];
334 real fac = M_PI * gmx::c_one4PiEps0 / (epsilonR * 2.0 * vol * vol * gmx::square(ewaldcoeffQ));
336 real qs2A = qsum[0] * qsum[0];
337 real qs2B = qsum[1] * qsum[1];
339 real vc = (qs2A * (1 - lambda) + qs2B * lambda) * fac;
341 enercorr = -vol * vc;
343 *dvdlambda += -vol * (qs2B - qs2A) * fac;
345 for (int d = 0; d < DIM; d++)