Replace DOMAINDECOMP macro by a renamed function
[alexxy/gromacs.git] / src / gromacs / modularsimulator / forceelement.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief Defines the force element for the modular simulator
37  *
38  * \author Pascal Merz <pascal.merz@me.com>
39  * \ingroup module_modularsimulator
40  */
41
42 #include "gmxpre.h"
43
44 #include "forceelement.h"
45
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/forcerec.h"
55 #include "gromacs/mdtypes/inputrec.h"
56 #include "gromacs/mdtypes/interaction_const.h"
57 #include "gromacs/mdtypes/mdatom.h"
58 #include "gromacs/mdtypes/mdrunoptions.h"
59 #include "gromacs/mdtypes/simulation_workload.h"
60 #include "gromacs/pbcutil/pbc.h"
61
62 #include "energydata.h"
63 #include "freeenergyperturbationdata.h"
64 #include "modularsimulator.h"
65 #include "simulatoralgorithm.h"
66 #include "statepropagatordata.h"
67
68 struct gmx_edsam;
69 struct gmx_enfrot;
70 struct gmx_multisim_t;
71 class history_t;
72
73 namespace gmx
74 {
75 ForceElement::ForceElement(StatePropagatorData*        statePropagatorData,
76                            EnergyData*                 energyData,
77                            FreeEnergyPerturbationData* freeEnergyPerturbationData,
78                            bool                        isVerbose,
79                            bool                        isDynamicBox,
80                            FILE*                       fplog,
81                            const t_commrec*            cr,
82                            const t_inputrec*           inputrec,
83                            const MDAtoms*              mdAtoms,
84                            t_nrnb*                     nrnb,
85                            t_forcerec*                 fr,
86                            gmx_wallcycle*              wcycle,
87                            MdrunScheduleWorkload*      runScheduleWork,
88                            VirtualSitesHandler*        vsite,
89                            ImdSession*                 imdSession,
90                            pull_t*                     pull_work,
91                            Constraints*                constr,
92                            const gmx_mtop_t&           globalTopology,
93                            gmx_enfrot*                 enforcedRotation) :
94     shellfc_(init_shell_flexcon(fplog,
95                                 globalTopology,
96                                 constr ? constr->numFlexibleConstraints() : 0,
97                                 inputrec->nstcalcenergy,
98                                 haveDDAtomOrdering(*cr),
99                                 runScheduleWork->simulationWork.useGpuPme)),
100     doShellFC_(shellfc_ != nullptr),
101     nextNSStep_(-1),
102     nextEnergyCalculationStep_(-1),
103     nextVirialCalculationStep_(-1),
104     nextFreeEnergyCalculationStep_(-1),
105     statePropagatorData_(statePropagatorData),
106     energyData_(energyData),
107     freeEnergyPerturbationData_(freeEnergyPerturbationData),
108     localTopology_(nullptr),
109     isDynamicBox_(isDynamicBox),
110     isVerbose_(isVerbose),
111     nShellRelaxationSteps_(0),
112     ddBalanceRegionHandler_(cr),
113     longRangeNonbondeds_(std::make_unique<CpuPpLongRangeNonbondeds>(fr->n_tpi,
114                                                                     fr->ic->ewaldcoeff_q,
115                                                                     fr->ic->epsilon_r,
116                                                                     fr->qsum,
117                                                                     fr->ic->eeltype,
118                                                                     fr->ic->vdwtype,
119                                                                     *inputrec,
120                                                                     nrnb,
121                                                                     wcycle,
122                                                                     fplog)),
123     lambda_(),
124     fplog_(fplog),
125     cr_(cr),
126     inputrec_(inputrec),
127     mdAtoms_(mdAtoms),
128     nrnb_(nrnb),
129     wcycle_(wcycle),
130     fr_(fr),
131     vsite_(vsite),
132     imdSession_(imdSession),
133     pull_work_(pull_work),
134     runScheduleWork_(runScheduleWork),
135     constr_(constr),
136     enforcedRotation_(enforcedRotation)
137 {
138     std::fill(lambda_.begin(), lambda_.end(), 0);
139
140     if (doShellFC_ && !haveDDAtomOrdering(*cr))
141     {
142         // This was done in mdAlgorithmsSetupAtomData(), but shellfc
143         // won't be available outside this element.
144         make_local_shells(cr, *mdAtoms->mdatoms(), shellfc_);
145     }
146 }
147
148 void ForceElement::scheduleTask(Step step, Time time, const RegisterRunFunction& registerRunFunction)
149 {
150     unsigned int flags =
151             (GMX_FORCE_STATECHANGED | GMX_FORCE_ALLFORCES | (isDynamicBox_ ? GMX_FORCE_DYNAMICBOX : 0)
152              | (nextVirialCalculationStep_ == step ? GMX_FORCE_VIRIAL : 0)
153              | (nextEnergyCalculationStep_ == step ? GMX_FORCE_ENERGY : 0)
154              | (nextFreeEnergyCalculationStep_ == step ? GMX_FORCE_DHDL : 0)
155              | (!doShellFC_ && nextNSStep_ == step ? GMX_FORCE_NS : 0));
156
157     registerRunFunction([this, step, time, flags]() {
158         if (doShellFC_)
159         {
160             run<true>(step, time, flags);
161         }
162         else
163         {
164             run<false>(step, time, flags);
165         }
166     });
167 }
168
169 void ForceElement::elementSetup()
170 {
171     GMX_ASSERT(localTopology_, "Setup called before local topology was set.");
172 }
173
174 template<bool doShellFC>
175 void ForceElement::run(Step step, Time time, unsigned int flags)
176 {
177     // Disabled functionality
178     gmx_multisim_t* ms = nullptr;
179
180
181     if (!haveDDAtomOrdering(*cr_) && (flags & GMX_FORCE_NS) && inputrecDynamicBox(inputrec_))
182     {
183         // TODO: Correcting the box is done in DomDecHelper (if using DD) or here (non-DD simulations).
184         //       Think about unifying this responsibility, could this be done in one place?
185         auto* box = statePropagatorData_->box();
186         correct_box(fplog_, step, box);
187     }
188
189     /* The coordinates (x) are shifted (to get whole molecules)
190      * in do_force.
191      * This is parallelized as well, and does communication too.
192      * Check comments in sim_util.c
193      */
194     auto        x      = statePropagatorData_->positionsView();
195     auto&       forces = statePropagatorData_->forcesView();
196     const auto* box    = statePropagatorData_->constBox();
197     history_t*  hist   = nullptr; // disabled
198
199     tensor force_vir = { { 0 } };
200     // TODO: Make lambda const (needs some adjustments in lower force routines)
201     ArrayRef<real> lambda =
202             freeEnergyPerturbationData_ ? freeEnergyPerturbationData_->lambdaView() : lambda_;
203
204     longRangeNonbondeds_->updateAfterPartition(*mdAtoms_->mdatoms());
205
206     if (doShellFC)
207     {
208         auto v = statePropagatorData_->velocitiesView();
209
210         relax_shell_flexcon(fplog_,
211                             cr_,
212                             ms,
213                             isVerbose_,
214                             enforcedRotation_,
215                             step,
216                             inputrec_,
217                             imdSession_,
218                             pull_work_,
219                             step == nextNSStep_,
220                             static_cast<int>(flags),
221                             localTopology_,
222                             constr_,
223                             energyData_->enerdata(),
224                             statePropagatorData_->localNumAtoms(),
225                             x,
226                             v,
227                             box,
228                             lambda,
229                             hist,
230                             &forces,
231                             force_vir,
232                             *mdAtoms_->mdatoms(),
233                             longRangeNonbondeds_.get(),
234                             nrnb_,
235                             wcycle_,
236                             shellfc_,
237                             fr_,
238                             runScheduleWork_,
239                             time,
240                             energyData_->muTot(),
241                             vsite_,
242                             ddBalanceRegionHandler_);
243         nShellRelaxationSteps_++;
244     }
245     else
246     {
247         // Disabled functionality
248         Awh*       awh = nullptr;
249         gmx_edsam* ed  = nullptr;
250
251         do_force(fplog_,
252                  cr_,
253                  ms,
254                  *inputrec_,
255                  awh,
256                  enforcedRotation_,
257                  imdSession_,
258                  pull_work_,
259                  step,
260                  nrnb_,
261                  wcycle_,
262                  localTopology_,
263                  box,
264                  x,
265                  hist,
266                  &forces,
267                  force_vir,
268                  mdAtoms_->mdatoms(),
269                  energyData_->enerdata(),
270                  lambda,
271                  fr_,
272                  runScheduleWork_,
273                  vsite_,
274                  energyData_->muTot(),
275                  time,
276                  ed,
277                  longRangeNonbondeds_.get(),
278                  static_cast<int>(flags),
279                  ddBalanceRegionHandler_);
280     }
281     energyData_->addToForceVirial(force_vir, step);
282 }
283
284 void ForceElement::elementTeardown()
285 {
286     if (doShellFC_)
287     {
288         done_shellfc(fplog_, shellfc_, nShellRelaxationSteps_);
289     }
290 }
291
292 void ForceElement::setTopology(const gmx_localtop_t* top)
293 {
294     localTopology_ = top;
295 }
296
297 std::optional<SignallerCallback> ForceElement::registerNSCallback()
298 {
299     return [this](Step step, Time gmx_unused time) { this->nextNSStep_ = step; };
300 }
301
302 std::optional<SignallerCallback> ForceElement::registerEnergyCallback(EnergySignallerEvent event)
303 {
304     if (event == EnergySignallerEvent::EnergyCalculationStep)
305     {
306         return [this](Step step, Time /*unused*/) { nextEnergyCalculationStep_ = step; };
307     }
308     if (event == EnergySignallerEvent::VirialCalculationStep)
309     {
310         return [this](Step step, Time /*unused*/) { nextVirialCalculationStep_ = step; };
311     }
312     if (event == EnergySignallerEvent::FreeEnergyCalculationStep)
313     {
314         return [this](Step step, Time /*unused*/) { nextFreeEnergyCalculationStep_ = step; };
315     }
316     return std::nullopt;
317 }
318
319 DomDecCallback ForceElement::registerDomDecCallback()
320 {
321     return [this]() { longRangeNonbondeds_->updateAfterPartition(*mdAtoms_->mdatoms()); };
322 }
323
324 ISimulatorElement*
325 ForceElement::getElementPointerImpl(LegacySimulatorData*                    legacySimulatorData,
326                                     ModularSimulatorAlgorithmBuilderHelper* builderHelper,
327                                     StatePropagatorData*                    statePropagatorData,
328                                     EnergyData*                             energyData,
329                                     FreeEnergyPerturbationData* freeEnergyPerturbationData,
330                                     GlobalCommunicationHelper gmx_unused* globalCommunicationHelper,
331                                     ObservablesReducer* /*observablesReducer*/)
332 {
333     const bool isVerbose    = legacySimulatorData->mdrunOptions.verbose;
334     const bool isDynamicBox = inputrecDynamicBox(legacySimulatorData->inputrec);
335     return builderHelper->storeElement(
336             std::make_unique<ForceElement>(statePropagatorData,
337                                            energyData,
338                                            freeEnergyPerturbationData,
339                                            isVerbose,
340                                            isDynamicBox,
341                                            legacySimulatorData->fplog,
342                                            legacySimulatorData->cr,
343                                            legacySimulatorData->inputrec,
344                                            legacySimulatorData->mdAtoms,
345                                            legacySimulatorData->nrnb,
346                                            legacySimulatorData->fr,
347                                            legacySimulatorData->wcycle,
348                                            legacySimulatorData->runScheduleWork,
349                                            legacySimulatorData->vsite,
350                                            legacySimulatorData->imdSession,
351                                            legacySimulatorData->pull_work,
352                                            legacySimulatorData->constr,
353                                            legacySimulatorData->top_global,
354                                            legacySimulatorData->enforcedRotation));
355 }
356 } // namespace gmx