2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019,2020, 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/inputrec.h"
54 #include "gromacs/mdtypes/mdatom.h"
55 #include "gromacs/mdtypes/mdrunoptions.h"
56 #include "gromacs/pbcutil/pbc.h"
58 #include "energydata.h"
59 #include "freeenergyperturbationdata.h"
60 #include "modularsimulator.h"
61 #include "simulatoralgorithm.h"
62 #include "statepropagatordata.h"
66 struct gmx_multisim_t;
71 ForceElement::ForceElement(StatePropagatorData* statePropagatorData,
72 EnergyData* energyData,
73 FreeEnergyPerturbationData* freeEnergyPerturbationData,
78 const t_inputrec* inputrec,
79 const MDAtoms* mdAtoms,
83 gmx_wallcycle* wcycle,
84 MdrunScheduleWorkload* runScheduleWork,
85 VirtualSitesHandler* vsite,
86 ImdSession* imdSession,
89 const gmx_mtop_t* globalTopology,
90 gmx_enfrot* enforcedRotation) :
91 shellfc_(init_shell_flexcon(fplog,
93 constr ? constr->numFlexibleConstraints() : 0,
94 inputrec->nstcalcenergy,
96 doShellFC_(shellfc_ != nullptr),
98 nextEnergyCalculationStep_(-1),
99 nextVirialCalculationStep_(-1),
100 nextFreeEnergyCalculationStep_(-1),
101 statePropagatorData_(statePropagatorData),
102 energyData_(energyData),
103 freeEnergyPerturbationData_(freeEnergyPerturbationData),
104 localTopology_(nullptr),
105 isDynamicBox_(isDynamicBox),
106 isVerbose_(isVerbose),
107 nShellRelaxationSteps_(0),
108 ddBalanceRegionHandler_(cr),
118 imdSession_(imdSession),
119 pull_work_(pull_work),
120 runScheduleWork_(runScheduleWork),
122 enforcedRotation_(enforcedRotation)
126 if (doShellFC_ && !DOMAINDECOMP(cr))
128 // This was done in mdAlgorithmsSetupAtomData(), but shellfc
129 // won't be available outside this element.
130 make_local_shells(cr, mdAtoms->mdatoms(), shellfc_);
134 void ForceElement::scheduleTask(Step step, Time time, const RegisterRunFunction& registerRunFunction)
137 (GMX_FORCE_STATECHANGED | GMX_FORCE_ALLFORCES | (isDynamicBox_ ? GMX_FORCE_DYNAMICBOX : 0)
138 | (nextVirialCalculationStep_ == step ? GMX_FORCE_VIRIAL : 0)
139 | (nextEnergyCalculationStep_ == step ? GMX_FORCE_ENERGY : 0)
140 | (nextFreeEnergyCalculationStep_ == step ? GMX_FORCE_DHDL : 0)
141 | (!doShellFC_ && nextNSStep_ == step ? GMX_FORCE_NS : 0));
143 registerRunFunction([this, step, time, flags]() {
146 run<true>(step, time, flags);
150 run<false>(step, time, flags);
155 void ForceElement::elementSetup()
157 GMX_ASSERT(localTopology_, "Setup called before local topology was set.");
160 template<bool doShellFC>
161 void ForceElement::run(Step step, Time time, unsigned int flags)
163 // Disabled functionality
164 gmx_multisim_t* ms = nullptr;
167 if (!DOMAINDECOMP(cr_) && (flags & GMX_FORCE_NS) && inputrecDynamicBox(inputrec_))
169 // TODO: Correcting the box is done in DomDecHelper (if using DD) or here (non-DD simulations).
170 // Think about unifying this responsibility, could this be done in one place?
171 auto box = statePropagatorData_->box();
172 correct_box(fplog_, step, box);
175 /* The coordinates (x) are shifted (to get whole molecules)
177 * This is parallelized as well, and does communication too.
178 * Check comments in sim_util.c
180 auto x = statePropagatorData_->positionsView();
181 auto forces = statePropagatorData_->forcesView();
182 auto box = statePropagatorData_->constBox();
183 history_t* hist = nullptr; // disabled
185 tensor force_vir = { { 0 } };
186 // TODO: Make lambda const (needs some adjustments in lower force routines)
187 ArrayRef<real> lambda =
188 freeEnergyPerturbationData_ ? freeEnergyPerturbationData_->lambdaView() : lambda_;
192 auto v = statePropagatorData_->velocitiesView();
195 fplog_, cr_, ms, isVerbose_, enforcedRotation_, step, inputrec_, imdSession_,
196 pull_work_, step == nextNSStep_, static_cast<int>(flags), localTopology_, constr_,
197 energyData_->enerdata(), statePropagatorData_->localNumAtoms(), x, v, box, lambda,
198 hist, forces, force_vir, mdAtoms_->mdatoms(), nrnb_, wcycle_, shellfc_, fr_,
199 runScheduleWork_, time, energyData_->muTot(), vsite_, ddBalanceRegionHandler_);
200 nShellRelaxationSteps_++;
204 // Disabled functionality
206 gmx_edsam* ed = nullptr;
208 do_force(fplog_, cr_, ms, inputrec_, awh, enforcedRotation_, imdSession_, pull_work_, step,
209 nrnb_, wcycle_, localTopology_, box, x, hist, forces, force_vir,
210 mdAtoms_->mdatoms(), energyData_->enerdata(), lambda, fr_, runScheduleWork_, vsite_,
211 energyData_->muTot(), time, ed, static_cast<int>(flags), ddBalanceRegionHandler_);
213 energyData_->addToForceVirial(force_vir, step);
216 void ForceElement::elementTeardown()
220 done_shellfc(fplog_, shellfc_, nShellRelaxationSteps_);
224 void ForceElement::setTopology(const gmx_localtop_t* top)
226 localTopology_ = top;
229 std::optional<SignallerCallback> ForceElement::registerNSCallback()
231 return [this](Step step, Time gmx_unused time) { this->nextNSStep_ = step; };
234 std::optional<SignallerCallback> ForceElement::registerEnergyCallback(EnergySignallerEvent event)
236 if (event == EnergySignallerEvent::EnergyCalculationStep)
238 return [this](Step step, Time /*unused*/) { nextEnergyCalculationStep_ = step; };
240 if (event == EnergySignallerEvent::VirialCalculationStep)
242 return [this](Step step, Time /*unused*/) { nextVirialCalculationStep_ = step; };
244 if (event == EnergySignallerEvent::FreeEnergyCalculationStep)
246 return [this](Step step, Time /*unused*/) { nextFreeEnergyCalculationStep_ = step; };
252 ForceElement::getElementPointerImpl(LegacySimulatorData* legacySimulatorData,
253 ModularSimulatorAlgorithmBuilderHelper* builderHelper,
254 StatePropagatorData* statePropagatorData,
255 EnergyData* energyData,
256 FreeEnergyPerturbationData* freeEnergyPerturbationData,
257 GlobalCommunicationHelper gmx_unused* globalCommunicationHelper)
259 const bool isVerbose = legacySimulatorData->mdrunOptions.verbose;
260 const bool isDynamicBox = inputrecDynamicBox(legacySimulatorData->inputrec);
261 return builderHelper->storeElement(std::make_unique<ForceElement>(
262 statePropagatorData, energyData, freeEnergyPerturbationData, isVerbose, isDynamicBox,
263 legacySimulatorData->fplog, legacySimulatorData->cr, legacySimulatorData->inputrec,
264 legacySimulatorData->mdAtoms, legacySimulatorData->nrnb, legacySimulatorData->fr,
265 legacySimulatorData->wcycle, legacySimulatorData->runScheduleWork, legacySimulatorData->vsite,
266 legacySimulatorData->imdSession, legacySimulatorData->pull_work, legacySimulatorData->constr,
267 legacySimulatorData->top_global, legacySimulatorData->enforcedRotation));