2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
36 * \brief Defines the force element for the modular simulator
38 * \author Pascal Merz <pascal.merz@me.com>
39 * \ingroup module_modularsimulator
44 #include "forceelement.h"
46 #include "gromacs/domdec/mdsetup.h"
47 #include "gromacs/math/vectypes.h"
48 #include "gromacs/mdlib/constr.h"
49 #include "gromacs/mdlib/force.h"
50 #include "gromacs/mdlib/force_flags.h"
51 #include "gromacs/mdlib/mdatoms.h"
52 #include "gromacs/mdrun/shellfc.h"
53 #include "gromacs/mdtypes/forcebuffers.h"
54 #include "gromacs/mdtypes/inputrec.h"
55 #include "gromacs/mdtypes/mdatom.h"
56 #include "gromacs/mdtypes/mdrunoptions.h"
57 #include "gromacs/mdtypes/simulation_workload.h"
58 #include "gromacs/pbcutil/pbc.h"
60 #include "energydata.h"
61 #include "freeenergyperturbationdata.h"
62 #include "modularsimulator.h"
63 #include "simulatoralgorithm.h"
64 #include "statepropagatordata.h"
68 struct gmx_multisim_t;
73 ForceElement::ForceElement(StatePropagatorData* statePropagatorData,
74 EnergyData* energyData,
75 FreeEnergyPerturbationData* freeEnergyPerturbationData,
80 const t_inputrec* inputrec,
81 const MDAtoms* mdAtoms,
85 gmx_wallcycle* wcycle,
86 MdrunScheduleWorkload* runScheduleWork,
87 VirtualSitesHandler* vsite,
88 ImdSession* imdSession,
91 const gmx_mtop_t& globalTopology,
92 gmx_enfrot* enforcedRotation) :
93 shellfc_(init_shell_flexcon(fplog,
95 constr ? constr->numFlexibleConstraints() : 0,
96 inputrec->nstcalcenergy,
98 runScheduleWork->simulationWork.useGpuPme)),
99 doShellFC_(shellfc_ != nullptr),
101 nextEnergyCalculationStep_(-1),
102 nextVirialCalculationStep_(-1),
103 nextFreeEnergyCalculationStep_(-1),
104 statePropagatorData_(statePropagatorData),
105 energyData_(energyData),
106 freeEnergyPerturbationData_(freeEnergyPerturbationData),
107 localTopology_(nullptr),
108 isDynamicBox_(isDynamicBox),
109 isVerbose_(isVerbose),
110 nShellRelaxationSteps_(0),
111 ddBalanceRegionHandler_(cr),
121 imdSession_(imdSession),
122 pull_work_(pull_work),
123 runScheduleWork_(runScheduleWork),
125 enforcedRotation_(enforcedRotation)
127 std::fill(lambda_.begin(), lambda_.end(), 0);
129 if (doShellFC_ && !DOMAINDECOMP(cr))
131 // This was done in mdAlgorithmsSetupAtomData(), but shellfc
132 // won't be available outside this element.
133 make_local_shells(cr, *mdAtoms->mdatoms(), shellfc_);
137 void ForceElement::scheduleTask(Step step, Time time, const RegisterRunFunction& registerRunFunction)
140 (GMX_FORCE_STATECHANGED | GMX_FORCE_ALLFORCES | (isDynamicBox_ ? GMX_FORCE_DYNAMICBOX : 0)
141 | (nextVirialCalculationStep_ == step ? GMX_FORCE_VIRIAL : 0)
142 | (nextEnergyCalculationStep_ == step ? GMX_FORCE_ENERGY : 0)
143 | (nextFreeEnergyCalculationStep_ == step ? GMX_FORCE_DHDL : 0)
144 | (!doShellFC_ && nextNSStep_ == step ? GMX_FORCE_NS : 0));
146 registerRunFunction([this, step, time, flags]() {
149 run<true>(step, time, flags);
153 run<false>(step, time, flags);
158 void ForceElement::elementSetup()
160 GMX_ASSERT(localTopology_, "Setup called before local topology was set.");
163 template<bool doShellFC>
164 void ForceElement::run(Step step, Time time, unsigned int flags)
166 // Disabled functionality
167 gmx_multisim_t* ms = nullptr;
170 if (!DOMAINDECOMP(cr_) && (flags & GMX_FORCE_NS) && inputrecDynamicBox(inputrec_))
172 // TODO: Correcting the box is done in DomDecHelper (if using DD) or here (non-DD simulations).
173 // Think about unifying this responsibility, could this be done in one place?
174 auto box = statePropagatorData_->box();
175 correct_box(fplog_, step, box);
178 /* The coordinates (x) are shifted (to get whole molecules)
180 * This is parallelized as well, and does communication too.
181 * Check comments in sim_util.c
183 auto x = statePropagatorData_->positionsView();
184 auto& forces = statePropagatorData_->forcesView();
185 auto box = statePropagatorData_->constBox();
186 history_t* hist = nullptr; // disabled
188 tensor force_vir = { { 0 } };
189 // TODO: Make lambda const (needs some adjustments in lower force routines)
190 ArrayRef<real> lambda =
191 freeEnergyPerturbationData_ ? freeEnergyPerturbationData_->lambdaView() : lambda_;
195 auto v = statePropagatorData_->velocitiesView();
197 relax_shell_flexcon(fplog_,
207 static_cast<int>(flags),
210 energyData_->enerdata(),
211 statePropagatorData_->localNumAtoms(),
219 *mdAtoms_->mdatoms(),
226 energyData_->muTot(),
228 ddBalanceRegionHandler_);
229 nShellRelaxationSteps_++;
233 // Disabled functionality
235 gmx_edsam* ed = nullptr;
255 energyData_->enerdata(),
260 energyData_->muTot(),
263 static_cast<int>(flags),
264 ddBalanceRegionHandler_);
266 energyData_->addToForceVirial(force_vir, step);
269 void ForceElement::elementTeardown()
273 done_shellfc(fplog_, shellfc_, nShellRelaxationSteps_);
277 void ForceElement::setTopology(const gmx_localtop_t* top)
279 localTopology_ = top;
282 std::optional<SignallerCallback> ForceElement::registerNSCallback()
284 return [this](Step step, Time gmx_unused time) { this->nextNSStep_ = step; };
287 std::optional<SignallerCallback> ForceElement::registerEnergyCallback(EnergySignallerEvent event)
289 if (event == EnergySignallerEvent::EnergyCalculationStep)
291 return [this](Step step, Time /*unused*/) { nextEnergyCalculationStep_ = step; };
293 if (event == EnergySignallerEvent::VirialCalculationStep)
295 return [this](Step step, Time /*unused*/) { nextVirialCalculationStep_ = step; };
297 if (event == EnergySignallerEvent::FreeEnergyCalculationStep)
299 return [this](Step step, Time /*unused*/) { nextFreeEnergyCalculationStep_ = step; };
305 ForceElement::getElementPointerImpl(LegacySimulatorData* legacySimulatorData,
306 ModularSimulatorAlgorithmBuilderHelper* builderHelper,
307 StatePropagatorData* statePropagatorData,
308 EnergyData* energyData,
309 FreeEnergyPerturbationData* freeEnergyPerturbationData,
310 GlobalCommunicationHelper gmx_unused* globalCommunicationHelper)
312 const bool isVerbose = legacySimulatorData->mdrunOptions.verbose;
313 const bool isDynamicBox = inputrecDynamicBox(legacySimulatorData->inputrec);
314 return builderHelper->storeElement(
315 std::make_unique<ForceElement>(statePropagatorData,
317 freeEnergyPerturbationData,
320 legacySimulatorData->fplog,
321 legacySimulatorData->cr,
322 legacySimulatorData->inputrec,
323 legacySimulatorData->mdAtoms,
324 legacySimulatorData->nrnb,
325 legacySimulatorData->fr,
326 legacySimulatorData->wcycle,
327 legacySimulatorData->runScheduleWork,
328 legacySimulatorData->vsite,
329 legacySimulatorData->imdSession,
330 legacySimulatorData->pull_work,
331 legacySimulatorData->constr,
332 legacySimulatorData->top_global,
333 legacySimulatorData->enforcedRotation));