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.
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/listed_forces/listed_forces.h"
55 #include "gromacs/math/vec.h"
56 #include "gromacs/math/vecdump.h"
57 #include "gromacs/mdlib/forcerec_threading.h"
58 #include "gromacs/mdlib/rf_util.h"
59 #include "gromacs/mdlib/wall.h"
60 #include "gromacs/mdtypes/commrec.h"
61 #include "gromacs/mdtypes/enerdata.h"
62 #include "gromacs/mdtypes/forceoutput.h"
63 #include "gromacs/mdtypes/forcerec.h"
64 #include "gromacs/mdtypes/inputrec.h"
65 #include "gromacs/mdtypes/interaction_const.h"
66 #include "gromacs/mdtypes/md_enums.h"
67 #include "gromacs/mdtypes/mdatom.h"
68 #include "gromacs/mdtypes/simulation_workload.h"
69 #include "gromacs/pbcutil/ishift.h"
70 #include "gromacs/pbcutil/pbc.h"
71 #include "gromacs/timing/wallcycle.h"
72 #include "gromacs/utility/exceptions.h"
73 #include "gromacs/utility/fatalerror.h"
74 #include "gromacs/utility/smalloc.h"
79 static void clearEwaldThreadOutput(ewald_corr_thread_t* ewc_t)
83 ewc_t->dvdl[efptCOUL] = 0;
84 ewc_t->dvdl[efptVDW] = 0;
85 clear_mat(ewc_t->vir_q);
86 clear_mat(ewc_t->vir_lj);
89 static void reduceEwaldThreadOuput(int nthreads, ewald_corr_thread_t* ewc_t)
91 ewald_corr_thread_t& dest = ewc_t[0];
93 for (int t = 1; t < nthreads; t++)
95 dest.Vcorr_q += ewc_t[t].Vcorr_q;
96 dest.Vcorr_lj += ewc_t[t].Vcorr_lj;
97 dest.dvdl[efptCOUL] += ewc_t[t].dvdl[efptCOUL];
98 dest.dvdl[efptVDW] += ewc_t[t].dvdl[efptVDW];
99 m_add(dest.vir_q, ewc_t[t].vir_q, dest.vir_q);
100 m_add(dest.vir_lj, ewc_t[t].vir_lj, dest.vir_lj);
104 void do_force_lowlevel(t_forcerec* fr,
105 const t_inputrec* ir,
107 const gmx_multisim_t* ms,
109 gmx_wallcycle_t wcycle,
111 gmx::ArrayRefWithPadding<const RVec> coordinates,
112 ArrayRef<const RVec> xWholeMolecules,
114 gmx::ForceOutputs* forceOutputs,
115 gmx_enerdata_t* enerd,
119 const gmx::StepWorkload& stepWork,
120 const DDBalanceRegionHandler& ddBalanceRegionHandler)
122 // TODO: Replace all uses of x by const coordinates
123 const rvec* x = as_rvec_array(coordinates.paddedArrayRef().data());
125 auto& forceWithVirial = forceOutputs->forceWithVirial();
127 /* Call the short range functions all in one go. */
131 /* foreign lambda component for walls */
132 real dvdl_walls = do_walls(*ir, *fr, box, *md, x, &forceWithVirial, lambda[efptVDW],
133 enerd->grpp.ener[egLJSR].data(), nrnb);
134 enerd->dvdl_lin[efptVDW] += dvdl_walls;
140 /* Check whether we need to take into account PBC in listed interactions. */
141 ListedForces& listedForces = fr->listedForces[0];
142 const auto needPbcForListedForces = fr->bMolPBC && stepWork.computeListedForces
143 && listedForces.haveCpuListedForces(*fr->fcdata);
144 if (needPbcForListedForces)
146 /* Since all atoms are in the rectangular or triclinic unit-cell,
147 * only single box vector shifts (2 in x) are required.
149 set_pbc_dd(&pbc, fr->pbcType, DOMAINDECOMP(cr) ? cr->dd->numCells : nullptr, TRUE, box);
152 listedForces.calculate(wcycle, box, ir->fepvals, cr, ms, x, xWholeMolecules, fr->fcdata.get(),
153 hist, forceOutputs, fr, &pbc, enerd, nrnb, lambda, md,
154 DOMAINDECOMP(cr) ? cr->dd->globalAtomIndices.data() : nullptr, stepWork);
157 const bool computePmeOnCpu = (EEL_PME(fr->ic->eeltype) || EVDW_PME(fr->ic->vdwtype))
158 && thisRankHasDuty(cr, DUTY_PME)
159 && (pme_run_mode(fr->pmedata) == PmeRunMode::CPU);
161 const bool haveEwaldSurfaceTerm = haveEwaldSurfaceContribution(*ir);
163 /* Do long-range electrostatics and/or LJ-PME
164 * and compute PME surface terms when necessary.
166 if (computePmeOnCpu || fr->ic->eeltype == eelEWALD || haveEwaldSurfaceTerm)
169 real Vlr_q = 0, Vlr_lj = 0;
171 /* We reduce all virial, dV/dlambda and energy contributions, except
172 * for the reciprocal energies (Vlr_q, Vlr_lj) into the same struct.
174 ewald_corr_thread_t& ewaldOutput = fr->ewc_t[0];
175 clearEwaldThreadOutput(&ewaldOutput);
177 if (EEL_PME_EWALD(fr->ic->eeltype) || EVDW_PME(fr->ic->vdwtype))
179 /* Calculate the Ewald surface force and energy contributions, when necessary */
180 if (haveEwaldSurfaceTerm)
182 wallcycle_sub_start(wcycle, ewcsEWALD_CORRECTION);
184 int nthreads = fr->nthread_ewc;
185 #pragma omp parallel for num_threads(nthreads) schedule(static)
186 for (int t = 0; t < nthreads; t++)
190 ewald_corr_thread_t& ewc_t = fr->ewc_t[t];
193 clearEwaldThreadOutput(&ewc_t);
196 /* Threading is only supported with the Verlet cut-off
197 * scheme and then only single particle forces (no
198 * exclusion forces) are calculated, so we can store
199 * the forces in the normal, single forceWithVirial->force_ array.
201 ewald_LRcorrection(md->homenr, cr, nthreads, t, *fr, *ir, md->chargeA,
202 md->chargeB, (md->nChargePerturbed != 0), x, box, mu_tot,
203 as_rvec_array(forceWithVirial.force_.data()),
204 &ewc_t.Vcorr_q, lambda[efptCOUL], &ewc_t.dvdl[efptCOUL]);
206 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
210 reduceEwaldThreadOuput(nthreads, fr->ewc_t);
212 wallcycle_sub_stop(wcycle, ewcsEWALD_CORRECTION);
215 if (EEL_PME_EWALD(fr->ic->eeltype) && fr->n_tpi == 0)
217 /* This is not in a subcounter because it takes a
218 negligible and constant-sized amount of time */
219 ewaldOutput.Vcorr_q += ewald_charge_correction(
220 cr, fr, lambda[efptCOUL], box, &ewaldOutput.dvdl[efptCOUL], ewaldOutput.vir_q);
225 /* Do reciprocal PME for Coulomb and/or LJ. */
226 assert(fr->n_tpi >= 0);
227 if (fr->n_tpi == 0 || stepWork.stateChanged)
229 /* With domain decomposition we close the CPU side load
230 * balancing region here, because PME does global
231 * communication that acts as a global barrier.
233 ddBalanceRegionHandler.closeAfterForceComputationCpu();
235 wallcycle_start(wcycle, ewcPMEMESH);
238 gmx::constArrayRefFromArray(coordinates.unpaddedConstArrayRef().data(),
239 md->homenr - fr->n_tpi),
240 forceWithVirial.force_, md->chargeA, md->chargeB, md->sqrt_c6A,
241 md->sqrt_c6B, md->sigmaA, md->sigmaB, box, cr,
242 DOMAINDECOMP(cr) ? dd_pme_maxshift_x(cr->dd) : 0,
243 DOMAINDECOMP(cr) ? dd_pme_maxshift_y(cr->dd) : 0, nrnb, wcycle,
244 ewaldOutput.vir_q, ewaldOutput.vir_lj, &Vlr_q, &Vlr_lj,
245 lambda[efptCOUL], lambda[efptVDW], &ewaldOutput.dvdl[efptCOUL],
246 &ewaldOutput.dvdl[efptVDW], stepWork);
247 wallcycle_stop(wcycle, ewcPMEMESH);
250 gmx_fatal(FARGS, "Error %d in reciprocal PME routine", status);
253 /* We should try to do as little computation after
254 * this as possible, because parallel PME synchronizes
255 * the nodes, so we want all load imbalance of the
256 * rest of the force calculation to be before the PME
257 * call. DD load balancing is done on the whole time
258 * of the force call (without PME).
263 /* Determine the PME grid energy of the test molecule
264 * with the PME grid potential of the other charges.
268 coordinates.unpaddedConstArrayRef().subArray(md->homenr - fr->n_tpi, fr->n_tpi),
269 gmx::arrayRefFromArray(md->chargeA + md->homenr - fr->n_tpi, fr->n_tpi),
275 if (fr->ic->eeltype == eelEWALD)
277 Vlr_q = do_ewald(ir, x, as_rvec_array(forceWithVirial.force_.data()), md->chargeA,
278 md->chargeB, box, cr, md->homenr, ewaldOutput.vir_q, fr->ic->ewaldcoeff_q,
279 lambda[efptCOUL], &ewaldOutput.dvdl[efptCOUL], fr->ewald_table);
282 /* Note that with separate PME nodes we get the real energies later */
283 // TODO it would be simpler if we just accumulated a single
284 // long-range virial contribution.
285 forceWithVirial.addVirialContribution(ewaldOutput.vir_q);
286 forceWithVirial.addVirialContribution(ewaldOutput.vir_lj);
287 enerd->dvdl_lin[efptCOUL] += ewaldOutput.dvdl[efptCOUL];
288 enerd->dvdl_lin[efptVDW] += ewaldOutput.dvdl[efptVDW];
289 enerd->term[F_COUL_RECIP] = Vlr_q + ewaldOutput.Vcorr_q;
290 enerd->term[F_LJ_RECIP] = Vlr_lj + ewaldOutput.Vcorr_lj;
294 fprintf(debug, "Vlr_q = %g, Vcorr_q = %g, Vlr_corr_q = %g\n", Vlr_q,
295 ewaldOutput.Vcorr_q, enerd->term[F_COUL_RECIP]);
296 pr_rvecs(debug, 0, "vir_el_recip after corr", ewaldOutput.vir_q, DIM);
297 rvec* fshift = as_rvec_array(forceOutputs->forceWithShiftForces().shiftForces().data());
298 pr_rvecs(debug, 0, "fshift after LR Corrections", fshift, SHIFTS);
299 fprintf(debug, "Vlr_lj: %g, Vcorr_lj = %g, Vlr_corr_lj = %g\n", Vlr_lj,
300 ewaldOutput.Vcorr_lj, enerd->term[F_LJ_RECIP]);
301 pr_rvecs(debug, 0, "vir_lj_recip after corr", ewaldOutput.vir_lj, DIM);
307 print_nrnb(debug, nrnb);
312 rvec* fshift = as_rvec_array(forceOutputs->forceWithShiftForces().shiftForces().data());
313 pr_rvecs(debug, 0, "fshift after bondeds", fshift, SHIFTS);