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,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 #include "long_range_correction.h"
44 #include "gromacs/ewald/ewald_utils.h"
45 #include "gromacs/math/functions.h"
46 #include "gromacs/math/units.h"
47 #include "gromacs/math/utilities.h"
48 #include "gromacs/math/vec.h"
49 #include "gromacs/mdtypes/commrec.h"
50 #include "gromacs/mdtypes/forcerec.h"
51 #include "gromacs/mdtypes/inputrec.h"
52 #include "gromacs/mdtypes/interaction_const.h"
53 #include "gromacs/mdtypes/md_enums.h"
54 #include "gromacs/utility/fatalerror.h"
55 #include "gromacs/utility/gmxassert.h"
57 /* There's nothing special to do here if just masses are perturbed,
58 * but if either charge or type is perturbed then the implementation
59 * requires that B states are defined for both charge and type, and
60 * does not optimize for the cases where only one changes.
62 * The parameter vectors for B states are left undefined in atoms2md()
63 * when either FEP is inactive, or when there are no mass/charge/type
64 * perturbations. The parameter vectors for LJ-PME are likewise
65 * undefined when LJ-PME is not active. This works because
66 * bHaveChargeOrTypePerturbed handles the control flow. */
67 void ewald_LRcorrection(const int numAtomsLocal,
68 const t_commrec* commrec,
72 gmx::ArrayRef<const double> qsum,
73 EwaldGeometry ewaldGeometry,
74 const real epsilonSurface,
77 gmx::ArrayRef<const real> chargeA,
78 gmx::ArrayRef<const real> chargeB,
79 bool bHaveChargePerturbed,
80 gmx::ArrayRef<const gmx::RVec> coords,
82 gmx::ArrayRef<const gmx::RVec> mu_tot,
83 gmx::ArrayRef<gmx::RVec> forces,
88 /* We need to correct only self interactions */
89 const int start = (numAtomsLocal * thread) / numThreads;
90 const int end = (numAtomsLocal * (thread + 1)) / numThreads;
93 double Vexcl_q, dvdl_excl_q; /* Necessary for precision */
95 real Vself_q[2], Vdipole[2];
96 rvec mutot[2], dipcorrA, dipcorrB;
97 real L1_q, dipole_coeff;
98 real chargecorr[2] = { 0, 0 };
100 /* Scale the Ewald unit cell when dimension z is not periodic */
102 EwaldBoxZScaler boxScaler(havePbcXY2Walls, wallEwaldZfac);
103 boxScaler.scaleBox(box, scaledBox);
105 one_4pi_eps = gmx::c_one4PiEps0 / epsilonR;
110 L1_q = 1.0 - lambda_q;
111 /* Note that we have to transform back to gromacs units, since
112 * mu_tot contains the dipole in debye units (for output).
114 for (i = 0; (i < DIM); i++)
116 mutot[0][i] = mu_tot[0][i] * gmx::c_debye2Enm;
117 mutot[1][i] = mu_tot[1][i] * gmx::c_debye2Enm;
123 real boxVolume = scaledBox[XX][XX] * scaledBox[YY][YY] * scaledBox[ZZ][ZZ];
124 switch (ewaldGeometry)
126 case EwaldGeometry::ThreeD:
127 if (epsilonSurface != 0)
130 2 * M_PI * gmx::c_one4PiEps0 / ((2 * epsilonSurface + epsilonR) * boxVolume);
131 for (i = 0; (i < DIM); i++)
133 dipcorrA[i] = 2 * dipole_coeff * mutot[0][i];
134 dipcorrB[i] = 2 * dipole_coeff * mutot[1][i];
138 case EwaldGeometry::ThreeDC:
139 dipole_coeff = 2 * M_PI * one_4pi_eps / boxVolume;
140 dipcorrA[ZZ] = 2 * dipole_coeff * mutot[0][ZZ];
141 dipcorrB[ZZ] = 2 * dipole_coeff * mutot[1][ZZ];
142 for (int q = 0; q < (bHaveChargePerturbed ? 2 : 1); q++)
144 /* Avoid charge corrections with near-zero net charge */
145 if (fabs(qsum[q]) > 1e-4)
147 chargecorr[q] = 2 * dipole_coeff * qsum[q];
151 default: gmx_incons("Unsupported Ewald geometry");
153 const bool bNeedLongRangeCorrection = (dipole_coeff != 0);
154 if (bNeedLongRangeCorrection && !bHaveChargePerturbed)
156 for (i = start; (i < end); i++)
158 for (j = 0; (j < DIM); j++)
160 forces[i][j] -= dipcorrA[j] * chargeA[i];
162 if (chargecorr[0] != 0)
164 forces[i][ZZ] += chargecorr[0] * chargeA[i] * coords[i][ZZ];
168 else if (bNeedLongRangeCorrection)
170 for (i = start; (i < end); i++)
172 for (j = 0; (j < DIM); j++)
174 forces[i][j] -= L1_q * dipcorrA[j] * chargeA[i] + lambda_q * dipcorrB[j] * chargeB[i];
176 if (chargecorr[0] != 0 || chargecorr[1] != 0)
179 (L1_q * chargecorr[0] * chargeA[i] + lambda_q * chargecorr[1]) * coords[i][ZZ];
187 /* Global corrections only on master process */
188 if (MASTER(commrec) && thread == 0)
190 for (q = 0; q < (bHaveChargePerturbed ? 2 : 1); q++)
192 /* Apply surface and charged surface dipole correction:
193 * correction = dipole_coeff * ( (dipole)^2
194 * - qsum*sum_i q_i z_i^2 - qsum^2 * box_z^2 / 12 )
196 if (dipole_coeff != 0)
198 if (ewaldGeometry == EwaldGeometry::ThreeD)
200 Vdipole[q] = dipole_coeff * iprod(mutot[q], mutot[q]);
202 else if (ewaldGeometry == EwaldGeometry::ThreeDC)
204 Vdipole[q] = dipole_coeff * mutot[q][ZZ] * mutot[q][ZZ];
206 if (chargecorr[q] != 0)
208 /* Here we use a non thread-parallelized loop,
209 * because this is the only loop over atoms for
210 * energies and they need reduction (unlike forces).
211 * We could implement a reduction over threads,
212 * but this case is rarely used.
214 gmx::ArrayRef<const real> charge = (q == 0 ? chargeA : chargeB);
216 for (int i = 0; i < numAtomsLocal; i++)
218 sumQZ2 += charge[i] * coords[i][ZZ] * coords[i][ZZ];
220 Vdipole[q] -= dipole_coeff * qsum[q]
221 * (sumQZ2 + qsum[q] * box[ZZ][ZZ] * box[ZZ][ZZ] / 12);
227 if (!bHaveChargePerturbed)
229 *Vcorr_q = Vdipole[0] - Vself_q[0] - Vexcl_q;
233 *Vcorr_q = L1_q * (Vdipole[0] - Vself_q[0]) + lambda_q * (Vdipole[1] - Vself_q[1]) - Vexcl_q;
234 *dvdlambda_q += Vdipole[1] - Vself_q[1] - (Vdipole[0] - Vself_q[0]) - dvdl_excl_q;