Remove forcerec from ewald_charge_correction
[alexxy/gromacs.git] / src / gromacs / mdlib / force.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
11  *
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.
16  *
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.
21  *
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.
26  *
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.
34  *
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.
37  */
38 #include "gmxpre.h"
39
40 #include "force.h"
41
42 #include <cassert>
43 #include <cmath>
44 #include <cstring>
45
46 #include "gromacs/domdec/dlbtiming.h"
47 #include "gromacs/domdec/domdec.h"
48 #include "gromacs/domdec/domdec_struct.h"
49 #include "gromacs/ewald/ewald.h"
50 #include "gromacs/ewald/long_range_correction.h"
51 #include "gromacs/ewald/pme.h"
52 #include "gromacs/gmxlib/network.h"
53 #include "gromacs/gmxlib/nrnb.h"
54 #include "gromacs/math/vec.h"
55 #include "gromacs/math/vecdump.h"
56 #include "gromacs/mdlib/forcerec_threading.h"
57 #include "gromacs/mdtypes/commrec.h"
58 #include "gromacs/mdtypes/enerdata.h"
59 #include "gromacs/mdtypes/forceoutput.h"
60 #include "gromacs/mdtypes/forcerec.h"
61 #include "gromacs/mdtypes/inputrec.h"
62 #include "gromacs/mdtypes/interaction_const.h"
63 #include "gromacs/mdtypes/md_enums.h"
64 #include "gromacs/mdtypes/mdatom.h"
65 #include "gromacs/mdtypes/simulation_workload.h"
66 #include "gromacs/pbcutil/ishift.h"
67 #include "gromacs/pbcutil/pbc.h"
68 #include "gromacs/timing/wallcycle.h"
69 #include "gromacs/utility/exceptions.h"
70 #include "gromacs/utility/fatalerror.h"
71 #include "gromacs/utility/smalloc.h"
72
73 using gmx::ArrayRef;
74 using gmx::RVec;
75
76 static void clearEwaldThreadOutput(ewald_corr_thread_t* ewc_t)
77 {
78     ewc_t->Vcorr_q                                        = 0;
79     ewc_t->Vcorr_lj                                       = 0;
80     ewc_t->dvdl[FreeEnergyPerturbationCouplingType::Coul] = 0;
81     ewc_t->dvdl[FreeEnergyPerturbationCouplingType::Vdw]  = 0;
82     clear_mat(ewc_t->vir_q);
83     clear_mat(ewc_t->vir_lj);
84 }
85
86 static void reduceEwaldThreadOuput(int nthreads, gmx::ArrayRef<ewald_corr_thread_t> ewc_t)
87 {
88     ewald_corr_thread_t& dest = ewc_t[0];
89
90     for (int t = 1; t < nthreads; t++)
91     {
92         dest.Vcorr_q += ewc_t[t].Vcorr_q;
93         dest.Vcorr_lj += ewc_t[t].Vcorr_lj;
94         dest.dvdl[FreeEnergyPerturbationCouplingType::Coul] +=
95                 ewc_t[t].dvdl[FreeEnergyPerturbationCouplingType::Coul];
96         dest.dvdl[FreeEnergyPerturbationCouplingType::Vdw] +=
97                 ewc_t[t].dvdl[FreeEnergyPerturbationCouplingType::Vdw];
98         m_add(dest.vir_q, ewc_t[t].vir_q, dest.vir_q);
99         m_add(dest.vir_lj, ewc_t[t].vir_lj, dest.vir_lj);
100     }
101 }
102
103 void calculateLongRangeNonbondeds(t_forcerec*                    fr,
104                                   const t_inputrec&              ir,
105                                   const t_commrec*               cr,
106                                   t_nrnb*                        nrnb,
107                                   gmx_wallcycle*                 wcycle,
108                                   const t_mdatoms*               md,
109                                   gmx::ArrayRef<const RVec>      coordinates,
110                                   gmx::ForceWithVirial*          forceWithVirial,
111                                   gmx_enerdata_t*                enerd,
112                                   const matrix                   box,
113                                   gmx::ArrayRef<const real>      lambda,
114                                   gmx::ArrayRef<const gmx::RVec> mu_tot,
115                                   const gmx::StepWorkload&       stepWork,
116                                   const DDBalanceRegionHandler&  ddBalanceRegionHandler)
117 {
118     const bool computePmeOnCpu = (EEL_PME(fr->ic->eeltype) || EVDW_PME(fr->ic->vdwtype))
119                                  && thisRankHasDuty(cr, DUTY_PME)
120                                  && (pme_run_mode(fr->pmedata) == PmeRunMode::CPU);
121
122     const bool haveEwaldSurfaceTerm = haveEwaldSurfaceContribution(ir);
123
124     /* Do long-range electrostatics and/or LJ-PME
125      * and compute PME surface terms when necessary.
126      */
127     if ((computePmeOnCpu || fr->ic->eeltype == CoulombInteractionType::Ewald || haveEwaldSurfaceTerm)
128         && stepWork.computeNonbondedForces)
129     {
130         int  status = 0;
131         real Vlr_q = 0, Vlr_lj = 0;
132
133         /* We reduce all virial, dV/dlambda and energy contributions, except
134          * for the reciprocal energies (Vlr_q, Vlr_lj) into the same struct.
135          */
136         ewald_corr_thread_t& ewaldOutput = fr->ewc_t[0];
137         clearEwaldThreadOutput(&ewaldOutput);
138
139         if (EEL_PME_EWALD(fr->ic->eeltype) || EVDW_PME(fr->ic->vdwtype))
140         {
141             /* Calculate the Ewald surface force and energy contributions, when necessary */
142             if (haveEwaldSurfaceTerm)
143             {
144                 wallcycle_sub_start(wcycle, WallCycleSubCounter::EwaldCorrection);
145
146                 int nthreads = fr->nthread_ewc;
147 #pragma omp parallel for num_threads(nthreads) schedule(static)
148                 for (int t = 0; t < nthreads; t++)
149                 {
150                     try
151                     {
152                         ewald_corr_thread_t& ewc_t = fr->ewc_t[t];
153                         if (t > 0)
154                         {
155                             clearEwaldThreadOutput(&ewc_t);
156                         }
157
158                         /* Threading is only supported with the Verlet cut-off
159                          * scheme and then only single particle forces (no
160                          * exclusion forces) are calculated, so we can store
161                          * the forces in the normal, single forceWithVirial->force_ array.
162                          */
163                         ewald_LRcorrection(
164                                 md->homenr,
165                                 cr,
166                                 nthreads,
167                                 t,
168                                 *fr,
169                                 ir,
170                                 md->chargeA ? gmx::constArrayRefFromArray(md->chargeA, md->nr)
171                                             : gmx::ArrayRef<const real>{},
172                                 md->chargeB ? gmx::constArrayRefFromArray(md->chargeB, md->nr)
173                                             : gmx::ArrayRef<const real>{},
174                                 (md->nChargePerturbed != 0),
175                                 coordinates,
176                                 box,
177                                 mu_tot,
178                                 forceWithVirial->force_,
179                                 &ewc_t.Vcorr_q,
180                                 lambda[static_cast<int>(FreeEnergyPerturbationCouplingType::Coul)],
181                                 &ewc_t.dvdl[FreeEnergyPerturbationCouplingType::Coul]);
182                     }
183                     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
184                 }
185                 if (nthreads > 1)
186                 {
187                     reduceEwaldThreadOuput(nthreads, fr->ewc_t);
188                 }
189                 wallcycle_sub_stop(wcycle, WallCycleSubCounter::EwaldCorrection);
190             }
191
192             if (EEL_PME_EWALD(fr->ic->eeltype) && fr->n_tpi == 0)
193             {
194                 /* This is not in a subcounter because it takes a
195                    negligible and constant-sized amount of time */
196                 ewaldOutput.Vcorr_q += ewald_charge_correction(
197                         cr,
198                         fr->ic->epsilon_r,
199                         fr->ic->ewaldcoeff_q,
200                         fr->qsum,
201                         lambda[static_cast<int>(FreeEnergyPerturbationCouplingType::Coul)],
202                         box,
203                         &ewaldOutput.dvdl[FreeEnergyPerturbationCouplingType::Coul],
204                         ewaldOutput.vir_q);
205             }
206
207             if (computePmeOnCpu)
208             {
209                 /* Do reciprocal PME for Coulomb and/or LJ. */
210                 assert(fr->n_tpi >= 0);
211                 if (fr->n_tpi == 0 || stepWork.stateChanged)
212                 {
213                     /* With domain decomposition we close the CPU side load
214                      * balancing region here, because PME does global
215                      * communication that acts as a global barrier.
216                      */
217                     ddBalanceRegionHandler.closeAfterForceComputationCpu();
218
219                     wallcycle_start(wcycle, WallCycleCounter::PmeMesh);
220                     status = gmx_pme_do(
221                             fr->pmedata,
222                             gmx::constArrayRefFromArray(coordinates.data(), md->homenr - fr->n_tpi),
223                             forceWithVirial->force_,
224                             md->chargeA ? gmx::constArrayRefFromArray(md->chargeA, md->nr)
225                                         : gmx::ArrayRef<const real>{},
226                             md->chargeB ? gmx::constArrayRefFromArray(md->chargeB, md->nr)
227                                         : gmx::ArrayRef<const real>{},
228                             md->sqrt_c6A ? gmx::constArrayRefFromArray(md->sqrt_c6A, md->nr)
229                                          : gmx::ArrayRef<const real>{},
230                             md->sqrt_c6B ? gmx::constArrayRefFromArray(md->sqrt_c6B, md->nr)
231                                          : gmx::ArrayRef<const real>{},
232                             md->sigmaA ? gmx::constArrayRefFromArray(md->sigmaA, md->nr)
233                                        : gmx::ArrayRef<const real>{},
234                             md->sigmaB ? gmx::constArrayRefFromArray(md->sigmaB, md->nr)
235                                        : gmx::ArrayRef<const real>{},
236                             box,
237                             cr,
238                             DOMAINDECOMP(cr) ? dd_pme_maxshift_x(*cr->dd) : 0,
239                             DOMAINDECOMP(cr) ? dd_pme_maxshift_y(*cr->dd) : 0,
240                             nrnb,
241                             wcycle,
242                             ewaldOutput.vir_q,
243                             ewaldOutput.vir_lj,
244                             &Vlr_q,
245                             &Vlr_lj,
246                             lambda[static_cast<int>(FreeEnergyPerturbationCouplingType::Coul)],
247                             lambda[static_cast<int>(FreeEnergyPerturbationCouplingType::Vdw)],
248                             &ewaldOutput.dvdl[FreeEnergyPerturbationCouplingType::Coul],
249                             &ewaldOutput.dvdl[FreeEnergyPerturbationCouplingType::Vdw],
250                             stepWork);
251                     wallcycle_stop(wcycle, WallCycleCounter::PmeMesh);
252                     if (status != 0)
253                     {
254                         gmx_fatal(FARGS, "Error %d in reciprocal PME routine", status);
255                     }
256
257                     /* We should try to do as little computation after
258                      * this as possible, because parallel PME synchronizes
259                      * the nodes, so we want all load imbalance of the
260                      * rest of the force calculation to be before the PME
261                      * call.  DD load balancing is done on the whole time
262                      * of the force call (without PME).
263                      */
264                 }
265                 if (fr->n_tpi > 0)
266                 {
267                     /* Determine the PME grid energy of the test molecule
268                      * with the PME grid potential of the other charges.
269                      */
270                     Vlr_q = gmx_pme_calc_energy(
271                             fr->pmedata,
272                             coordinates.subArray(md->homenr - fr->n_tpi, fr->n_tpi),
273                             gmx::arrayRefFromArray(md->chargeA + md->homenr - fr->n_tpi, fr->n_tpi));
274                 }
275             }
276         }
277
278         if (fr->ic->eeltype == CoulombInteractionType::Ewald)
279         {
280             Vlr_q = do_ewald(ir,
281                              coordinates,
282                              forceWithVirial->force_,
283                              gmx::arrayRefFromArray(md->chargeA, md->nr),
284                              gmx::arrayRefFromArray(md->chargeB, md->nr),
285                              box,
286                              cr,
287                              md->homenr,
288                              ewaldOutput.vir_q,
289                              fr->ic->ewaldcoeff_q,
290                              lambda[static_cast<int>(FreeEnergyPerturbationCouplingType::Coul)],
291                              &ewaldOutput.dvdl[FreeEnergyPerturbationCouplingType::Coul],
292                              fr->ewald_table.get());
293         }
294
295         /* Note that with separate PME nodes we get the real energies later */
296         // TODO it would be simpler if we just accumulated a single
297         // long-range virial contribution.
298         forceWithVirial->addVirialContribution(ewaldOutput.vir_q);
299         forceWithVirial->addVirialContribution(ewaldOutput.vir_lj);
300         enerd->dvdl_lin[FreeEnergyPerturbationCouplingType::Coul] +=
301                 ewaldOutput.dvdl[FreeEnergyPerturbationCouplingType::Coul];
302         enerd->dvdl_lin[FreeEnergyPerturbationCouplingType::Vdw] +=
303                 ewaldOutput.dvdl[FreeEnergyPerturbationCouplingType::Vdw];
304         enerd->term[F_COUL_RECIP] = Vlr_q + ewaldOutput.Vcorr_q;
305         enerd->term[F_LJ_RECIP]   = Vlr_lj + ewaldOutput.Vcorr_lj;
306
307         if (debug)
308         {
309             fprintf(debug,
310                     "Vlr_q = %g, Vcorr_q = %g, Vlr_corr_q = %g\n",
311                     Vlr_q,
312                     ewaldOutput.Vcorr_q,
313                     enerd->term[F_COUL_RECIP]);
314             pr_rvecs(debug, 0, "vir_el_recip after corr", ewaldOutput.vir_q, DIM);
315             fprintf(debug,
316                     "Vlr_lj: %g, Vcorr_lj = %g, Vlr_corr_lj = %g\n",
317                     Vlr_lj,
318                     ewaldOutput.Vcorr_lj,
319                     enerd->term[F_LJ_RECIP]);
320             pr_rvecs(debug, 0, "vir_lj_recip after corr", ewaldOutput.vir_lj, DIM);
321         }
322     }
323
324     if (debug)
325     {
326         print_nrnb(debug, nrnb);
327     }
328 }