Remove use of graph in do_force()
[alexxy/gromacs.git] / src / gromacs / mdrun / mimic.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,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.
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
36 /*! \internal \file
37  *
38  * \brief Declares the loop for MiMiC QM/MM
39  *
40  * \author Viacheslav Bolnykh <v.bolnykh@hpc-leap.eu>
41  * \ingroup module_mdrun
42  */
43 #include "gmxpre.h"
44
45 #include <cinttypes>
46 #include <cmath>
47 #include <cstdio>
48 #include <cstdlib>
49
50 #include <algorithm>
51 #include <memory>
52
53 #include "gromacs/awh/awh.h"
54 #include "gromacs/commandline/filenm.h"
55 #include "gromacs/domdec/collect.h"
56 #include "gromacs/domdec/dlbtiming.h"
57 #include "gromacs/domdec/domdec.h"
58 #include "gromacs/domdec/domdec_network.h"
59 #include "gromacs/domdec/domdec_struct.h"
60 #include "gromacs/domdec/mdsetup.h"
61 #include "gromacs/domdec/partition.h"
62 #include "gromacs/essentialdynamics/edsam.h"
63 #include "gromacs/ewald/pme_load_balancing.h"
64 #include "gromacs/ewald/pme_pp.h"
65 #include "gromacs/fileio/trxio.h"
66 #include "gromacs/gmxlib/network.h"
67 #include "gromacs/gmxlib/nrnb.h"
68 #include "gromacs/gpu_utils/gpu_utils.h"
69 #include "gromacs/listed_forces/manage_threading.h"
70 #include "gromacs/math/functions.h"
71 #include "gromacs/math/utilities.h"
72 #include "gromacs/math/vec.h"
73 #include "gromacs/math/vectypes.h"
74 #include "gromacs/mdlib/checkpointhandler.h"
75 #include "gromacs/mdlib/compute_io.h"
76 #include "gromacs/mdlib/constr.h"
77 #include "gromacs/mdlib/ebin.h"
78 #include "gromacs/mdlib/enerdata_utils.h"
79 #include "gromacs/mdlib/energyoutput.h"
80 #include "gromacs/mdlib/expanded.h"
81 #include "gromacs/mdlib/force.h"
82 #include "gromacs/mdlib/force_flags.h"
83 #include "gromacs/mdlib/forcerec.h"
84 #include "gromacs/mdlib/md_support.h"
85 #include "gromacs/mdlib/mdatoms.h"
86 #include "gromacs/mdlib/mdoutf.h"
87 #include "gromacs/mdlib/membed.h"
88 #include "gromacs/mdlib/resethandler.h"
89 #include "gromacs/mdlib/sighandler.h"
90 #include "gromacs/mdlib/simulationsignal.h"
91 #include "gromacs/mdlib/stat.h"
92 #include "gromacs/mdlib/stophandler.h"
93 #include "gromacs/mdlib/tgroup.h"
94 #include "gromacs/mdlib/trajectory_writing.h"
95 #include "gromacs/mdlib/update.h"
96 #include "gromacs/mdlib/vcm.h"
97 #include "gromacs/mdlib/vsite.h"
98 #include "gromacs/mdrunutility/handlerestart.h"
99 #include "gromacs/mdrunutility/multisim.h"
100 #include "gromacs/mdrunutility/printtime.h"
101 #include "gromacs/mdtypes/awh_history.h"
102 #include "gromacs/mdtypes/awh_params.h"
103 #include "gromacs/mdtypes/commrec.h"
104 #include "gromacs/mdtypes/df_history.h"
105 #include "gromacs/mdtypes/enerdata.h"
106 #include "gromacs/mdtypes/energyhistory.h"
107 #include "gromacs/mdtypes/fcdata.h"
108 #include "gromacs/mdtypes/forcerec.h"
109 #include "gromacs/mdtypes/group.h"
110 #include "gromacs/mdtypes/inputrec.h"
111 #include "gromacs/mdtypes/interaction_const.h"
112 #include "gromacs/mdtypes/md_enums.h"
113 #include "gromacs/mdtypes/mdatom.h"
114 #include "gromacs/mdtypes/mdrunoptions.h"
115 #include "gromacs/mdtypes/observableshistory.h"
116 #include "gromacs/mdtypes/state.h"
117 #include "gromacs/mimic/communicator.h"
118 #include "gromacs/mimic/utilities.h"
119 #include "gromacs/pbcutil/pbc.h"
120 #include "gromacs/pulling/pull.h"
121 #include "gromacs/timing/wallcycle.h"
122 #include "gromacs/timing/walltime_accounting.h"
123 #include "gromacs/topology/atoms.h"
124 #include "gromacs/topology/idef.h"
125 #include "gromacs/topology/mtop_util.h"
126 #include "gromacs/topology/topology.h"
127 #include "gromacs/trajectory/trajectoryframe.h"
128 #include "gromacs/utility/basedefinitions.h"
129 #include "gromacs/utility/cstringutil.h"
130 #include "gromacs/utility/fatalerror.h"
131 #include "gromacs/utility/logger.h"
132 #include "gromacs/utility/real.h"
133 #include "gromacs/utility/smalloc.h"
134
135 #include "legacysimulator.h"
136 #include "replicaexchange.h"
137 #include "shellfc.h"
138
139 using gmx::SimulationSignaller;
140
141 void gmx::LegacySimulator::do_mimic()
142 {
143     t_inputrec*                 ir = inputrec;
144     int64_t                     step, step_rel;
145     double                      t, lam0[efptNR];
146     bool                        isLastStep               = false;
147     bool                        doFreeEnergyPerturbation = false;
148     unsigned int                force_flags;
149     tensor                      force_vir, shake_vir, total_vir, pres;
150     rvec                        mu_tot;
151     PaddedHostVector<gmx::RVec> f{};
152     gmx_global_stat_t           gstat;
153     gmx_shellfc_t*              shellfc;
154
155     double cycles;
156
157     /* Domain decomposition could incorrectly miss a bonded
158        interaction, but checking for that requires a global
159        communication stage, which does not otherwise happen in DD
160        code. So we do that alongside the first global energy reduction
161        after a new DD is made. These variables handle whether the
162        check happens, and the result it returns. */
163     bool shouldCheckNumberOfBondedInteractions = false;
164     int  totalNumberOfBondedInteractions       = -1;
165
166     SimulationSignals signals;
167     // Most global communnication stages don't propagate mdrun
168     // signals, and will use this object to achieve that.
169     SimulationSignaller nullSignaller(nullptr, nullptr, nullptr, false, false);
170
171     if (ir->bExpanded)
172     {
173         gmx_fatal(FARGS, "Expanded ensemble not supported by MiMiC.");
174     }
175     if (ir->bSimTemp)
176     {
177         gmx_fatal(FARGS, "Simulated tempering not supported by MiMiC.");
178     }
179     if (ir->bDoAwh)
180     {
181         gmx_fatal(FARGS, "AWH not supported by MiMiC.");
182     }
183     if (replExParams.exchangeInterval > 0)
184     {
185         gmx_fatal(FARGS, "Replica exchange not supported by MiMiC.");
186     }
187     if (opt2bSet("-ei", nfile, fnm) || observablesHistory->edsamHistory != nullptr)
188     {
189         gmx_fatal(FARGS, "Essential dynamics not supported by MiMiC.");
190     }
191     if (ir->bIMD)
192     {
193         gmx_fatal(FARGS, "Interactive MD not supported by MiMiC.");
194     }
195     if (isMultiSim(ms))
196     {
197         gmx_fatal(FARGS, "Multiple simulations not supported by MiMiC.");
198     }
199     if (std::any_of(ir->opts.annealing, ir->opts.annealing + ir->opts.ngtc,
200                     [](int i) { return i != eannNO; }))
201     {
202         gmx_fatal(FARGS, "Simulated annealing not supported by MiMiC.");
203     }
204
205     /* Settings for rerun */
206     ir->nstlist              = 1;
207     ir->nstcalcenergy        = 1;
208     int        nstglobalcomm = 1;
209     const bool bNS           = true;
210
211     // Communicator to interact with MiMiC
212     MimicCommunicator mimicCommunicator{};
213     if (MASTER(cr))
214     {
215         mimicCommunicator.init();
216         mimicCommunicator.sendInitData(top_global, state_global->x);
217         ir->nsteps = mimicCommunicator.getStepNumber();
218     }
219
220     ir->nstxout_compressed                   = 0;
221     SimulationGroups* groups                 = &top_global->groups;
222     top_global->intermolecularExclusionGroup = genQmmmIndices(*top_global);
223
224     initialize_lambdas(fplog, *ir, MASTER(cr), &state_global->fep_state, state_global->lambda, lam0);
225
226     const bool        simulationsShareState = false;
227     gmx_mdoutf*       outf = init_mdoutf(fplog, nfile, fnm, mdrunOptions, cr, outputProvider,
228                                    mdModulesNotifier, ir, top_global, oenv, wcycle,
229                                    StartingBehavior::NewSimulation, simulationsShareState, ms);
230     gmx::EnergyOutput energyOutput(mdoutf_get_fp_ene(outf), top_global, ir, pull_work,
231                                    mdoutf_get_fp_dhdl(outf), true, StartingBehavior::NewSimulation,
232                                    mdModulesNotifier);
233
234     gstat = global_stat_init(ir);
235
236     /* Check for polarizable models and flexible constraints */
237     shellfc = init_shell_flexcon(fplog, top_global, constr ? constr->numFlexibleConstraints() : 0,
238                                  ir->nstcalcenergy, DOMAINDECOMP(cr));
239
240     {
241         double io = compute_io(ir, top_global->natoms, *groups, energyOutput.numEnergyTerms(), 1);
242         if ((io > 2000) && MASTER(cr))
243         {
244             fprintf(stderr, "\nWARNING: This run will generate roughly %.0f Mb of data\n\n", io);
245         }
246     }
247
248     // Local state only becomes valid now.
249     std::unique_ptr<t_state> stateInstance;
250     t_state*                 state;
251
252     gmx_localtop_t top(top_global->ffparams);
253
254     if (DOMAINDECOMP(cr))
255     {
256         stateInstance = std::make_unique<t_state>();
257         state         = stateInstance.get();
258         dd_init_local_state(cr->dd, state_global, state);
259
260         /* Distribute the charge groups over the nodes from the master node */
261         dd_partition_system(fplog, mdlog, ir->init_step, cr, TRUE, 1, state_global, *top_global, ir,
262                             imdSession, pull_work, state, &f, mdAtoms, &top, fr, vsite, constr,
263                             nrnb, nullptr, FALSE);
264         shouldCheckNumberOfBondedInteractions = true;
265         gmx_bcast(sizeof(ir->nsteps), &ir->nsteps, cr);
266     }
267     else
268     {
269         state_change_natoms(state_global, state_global->natoms);
270         /* We need to allocate one element extra, since we might use
271          * (unaligned) 4-wide SIMD loads to access rvec entries.
272          */
273         f.resizeWithPadding(state_global->natoms);
274         /* Copy the pointer to the global state */
275         state = state_global;
276
277         mdAlgorithmsSetupAtomData(cr, ir, *top_global, &top, fr, mdAtoms, constr, vsite, shellfc);
278     }
279
280     auto mdatoms = mdAtoms->mdatoms();
281
282     // NOTE: The global state is no longer used at this point.
283     // But state_global is still used as temporary storage space for writing
284     // the global state to file and potentially for replica exchange.
285     // (Global topology should persist.)
286
287     update_mdatoms(mdatoms, state->lambda[efptMASS]);
288
289     if (ir->efep != efepNO && ir->fepvals->nstdhdl != 0)
290     {
291         doFreeEnergyPerturbation = true;
292     }
293
294     {
295         int cglo_flags =
296                 (CGLO_GSTAT
297                  | (shouldCheckNumberOfBondedInteractions ? CGLO_CHECK_NUMBER_OF_BONDED_INTERACTIONS : 0));
298         bool   bSumEkinhOld = false;
299         t_vcm* vcm          = nullptr;
300         compute_globals(gstat, cr, ir, fr, ekind, makeConstArrayRef(state->x),
301                         makeConstArrayRef(state->v), state->box, state->lambda[efptVDW], mdatoms, nrnb,
302                         vcm, nullptr, enerd, force_vir, shake_vir, total_vir, pres, constr, &nullSignaller,
303                         state->box, &totalNumberOfBondedInteractions, &bSumEkinhOld, cglo_flags);
304     }
305     checkNumberOfBondedInteractions(mdlog, cr, totalNumberOfBondedInteractions, top_global, &top,
306                                     makeConstArrayRef(state->x), state->box,
307                                     &shouldCheckNumberOfBondedInteractions);
308
309     if (MASTER(cr))
310     {
311         fprintf(stderr, "starting MiMiC MD run '%s'\n\n", *(top_global->name));
312         if (mdrunOptions.verbose)
313         {
314             fprintf(stderr,
315                     "Calculated time to finish depends on nsteps from "
316                     "run input file,\nwhich may not correspond to the time "
317                     "needed to process input trajectory.\n\n");
318         }
319         fprintf(fplog, "\n");
320     }
321
322     walltime_accounting_start_time(walltime_accounting);
323     wallcycle_start(wcycle, ewcRUN);
324     print_start(fplog, cr, walltime_accounting, "mdrun");
325
326     /***********************************************************
327      *
328      *             Loop over MD steps
329      *
330      ************************************************************/
331
332     if (constr)
333     {
334         GMX_LOG(mdlog.info)
335                 .asParagraph()
336                 .appendText(
337                         "Simulations has constraints. Constraints will "
338                         "be handled by CPMD.");
339     }
340
341     GMX_LOG(mdlog.info)
342             .asParagraph()
343             .appendText(
344                     "MiMiC does not report kinetic energy, total energy, temperature, virial and "
345                     "pressure.");
346
347     auto stopHandler = stopHandlerBuilder->getStopHandlerMD(
348             compat::not_null<SimulationSignal*>(&signals[eglsSTOPCOND]), false, MASTER(cr),
349             ir->nstlist, mdrunOptions.reproducible, nstglobalcomm, mdrunOptions.maximumHoursToRun,
350             ir->nstlist == 0, fplog, step, bNS, walltime_accounting);
351
352     // we don't do counter resetting in rerun - finish will always be valid
353     walltime_accounting_set_valid_finish(walltime_accounting);
354
355     const DDBalanceRegionHandler ddBalanceRegionHandler(cr);
356
357     step     = ir->init_step;
358     step_rel = 0;
359
360     /* and stop now if we should */
361     isLastStep = (isLastStep || (ir->nsteps >= 0 && step_rel > ir->nsteps));
362     while (!isLastStep)
363     {
364         isLastStep = (isLastStep || (ir->nsteps >= 0 && step_rel == ir->nsteps));
365         wallcycle_start(wcycle, ewcSTEP);
366
367         t = step;
368
369         if (MASTER(cr))
370         {
371             mimicCommunicator.getCoords(&state_global->x, state_global->natoms);
372         }
373
374         if (ir->efep != efepNO)
375         {
376             setCurrentLambdasLocal(step, ir->fepvals, lam0, state->lambda, state->fep_state);
377         }
378
379         if (MASTER(cr))
380         {
381             const bool constructVsites = ((vsite != nullptr) && mdrunOptions.rerunConstructVsites);
382             if (constructVsites && DOMAINDECOMP(cr))
383             {
384                 gmx_fatal(FARGS,
385                           "Vsite recalculation with -rerun is not implemented with domain "
386                           "decomposition, "
387                           "use a single rank");
388             }
389         }
390
391         if (DOMAINDECOMP(cr))
392         {
393             /* Repartition the domain decomposition */
394             const bool bMasterState = true;
395             dd_partition_system(fplog, mdlog, step, cr, bMasterState, nstglobalcomm, state_global,
396                                 *top_global, ir, imdSession, pull_work, state, &f, mdAtoms, &top,
397                                 fr, vsite, constr, nrnb, wcycle, mdrunOptions.verbose);
398             shouldCheckNumberOfBondedInteractions = true;
399         }
400
401         if (MASTER(cr))
402         {
403             energyOutput.printHeader(fplog, step, t); /* can we improve the information printed here? */
404         }
405
406         if (ir->efep != efepNO)
407         {
408             update_mdatoms(mdatoms, state->lambda[efptMASS]);
409         }
410
411         force_flags = (GMX_FORCE_STATECHANGED | GMX_FORCE_DYNAMICBOX | GMX_FORCE_ALLFORCES
412                        | GMX_FORCE_VIRIAL | // TODO: Get rid of this once #2649 is solved
413                        GMX_FORCE_ENERGY | (doFreeEnergyPerturbation ? GMX_FORCE_DHDL : 0));
414
415         if (shellfc)
416         {
417             /* Now is the time to relax the shells */
418             relax_shell_flexcon(fplog, cr, ms, mdrunOptions.verbose, enforcedRotation, step, ir,
419                                 imdSession, pull_work, bNS, force_flags, &top, constr, enerd, fcd,
420                                 state->natoms, state->x.arrayRefWithPadding(),
421                                 state->v.arrayRefWithPadding(), state->box, state->lambda, &state->hist,
422                                 f.arrayRefWithPadding(), force_vir, mdatoms, nrnb, wcycle, shellfc,
423                                 fr, runScheduleWork, t, mu_tot, vsite, ddBalanceRegionHandler);
424         }
425         else
426         {
427             /* The coordinates (x) are shifted (to get whole molecules)
428              * in do_force.
429              * This is parallellized as well, and does communication too.
430              * Check comments in sim_util.c
431              */
432             Awh*       awh = nullptr;
433             gmx_edsam* ed  = nullptr;
434             do_force(fplog, cr, ms, ir, awh, enforcedRotation, imdSession, pull_work, step, nrnb,
435                      wcycle, &top, state->box, state->x.arrayRefWithPadding(), &state->hist,
436                      f.arrayRefWithPadding(), force_vir, mdatoms, enerd, fcd, state->lambda, fr,
437                      runScheduleWork, vsite, mu_tot, t, ed, GMX_FORCE_NS | force_flags,
438                      ddBalanceRegionHandler);
439         }
440
441         /* Now we have the energies and forces corresponding to the
442          * coordinates at time t.
443          */
444         {
445             const bool isCheckpointingStep = false;
446             const bool doRerun             = false;
447             const bool bSumEkinhOld        = false;
448             do_md_trajectory_writing(fplog, cr, nfile, fnm, step, step_rel, t, ir, state,
449                                      state_global, observablesHistory, top_global, fr, outf,
450                                      energyOutput, ekind, f, isCheckpointingStep, doRerun,
451                                      isLastStep, mdrunOptions.writeConfout, bSumEkinhOld);
452         }
453
454         stopHandler->setSignal();
455
456         if (vsite != nullptr)
457         {
458             wallcycle_start(wcycle, ewcVSITECONSTR);
459             construct_vsites(vsite, as_rvec_array(state->x.data()), ir->delta_t,
460                              as_rvec_array(state->v.data()), top.idef.iparams, top.idef.il,
461                              fr->pbcType, fr->bMolPBC, cr, state->box);
462             wallcycle_stop(wcycle, ewcVSITECONSTR);
463         }
464
465         {
466             const bool          doInterSimSignal = false;
467             const bool          doIntraSimSignal = true;
468             bool                bSumEkinhOld     = false;
469             t_vcm*              vcm              = nullptr;
470             SimulationSignaller signaller(&signals, cr, ms, doInterSimSignal, doIntraSimSignal);
471
472             compute_globals(gstat, cr, ir, fr, ekind, makeConstArrayRef(state->x),
473                             makeConstArrayRef(state->v), state->box, state->lambda[efptVDW], mdatoms,
474                             nrnb, vcm, wcycle, enerd, nullptr, nullptr, nullptr, nullptr, constr,
475                             &signaller, state->box, &totalNumberOfBondedInteractions, &bSumEkinhOld,
476                             CGLO_GSTAT | CGLO_ENERGY
477                                     | (shouldCheckNumberOfBondedInteractions ? CGLO_CHECK_NUMBER_OF_BONDED_INTERACTIONS
478                                                                              : 0));
479             checkNumberOfBondedInteractions(mdlog, cr, totalNumberOfBondedInteractions, top_global,
480                                             &top, makeConstArrayRef(state->x), state->box,
481                                             &shouldCheckNumberOfBondedInteractions);
482         }
483
484         {
485             gmx::HostVector<gmx::RVec>     fglobal(top_global->natoms);
486             gmx::ArrayRef<gmx::RVec>       ftemp;
487             gmx::ArrayRef<const gmx::RVec> flocal = gmx::makeArrayRef(f);
488             if (DOMAINDECOMP(cr))
489             {
490                 ftemp = gmx::makeArrayRef(fglobal);
491                 dd_collect_vec(cr->dd, state, flocal, ftemp);
492             }
493             else
494             {
495                 ftemp = gmx::makeArrayRef(f);
496             }
497
498             if (MASTER(cr))
499             {
500                 mimicCommunicator.sendEnergies(enerd->term[F_EPOT]);
501                 mimicCommunicator.sendForces(ftemp, state_global->natoms);
502             }
503         }
504
505
506         /* Note: this is OK, but there are some numerical precision issues with using the convergence of
507            the virial that should probably be addressed eventually. state->veta has better properies,
508            but what we actually need entering the new cycle is the new shake_vir value. Ideally, we could
509            generate the new shake_vir, but test the veta value for convergence.  This will take some thought. */
510
511         if (ir->efep != efepNO)
512         {
513             /* Sum up the foreign energy and dhdl terms for md and sd.
514                Currently done every step so that dhdl is correct in the .edr */
515             sum_dhdl(enerd, state->lambda, *ir->fepvals);
516         }
517
518         /* Output stuff */
519         if (MASTER(cr))
520         {
521             const bool bCalcEnerStep = true;
522             energyOutput.addDataAtEnergyStep(doFreeEnergyPerturbation, bCalcEnerStep, t,
523                                              mdatoms->tmass, enerd, state, ir->fepvals,
524                                              ir->expandedvals, state->box, shake_vir, force_vir,
525                                              total_vir, pres, ekind, mu_tot, constr);
526
527             const bool do_ene = true;
528             const bool do_log = true;
529             Awh*       awh    = nullptr;
530             const bool do_dr  = ir->nstdisreout != 0;
531             const bool do_or  = ir->nstorireout != 0;
532
533             energyOutput.printAnnealingTemperatures(do_log ? fplog : nullptr, groups, &(ir->opts));
534             energyOutput.printStepToEnergyFile(mdoutf_get_fp_ene(outf), do_ene, do_dr, do_or,
535                                                do_log ? fplog : nullptr, step, t, fcd, awh);
536
537             if (do_per_step(step, ir->nstlog))
538             {
539                 if (fflush(fplog) != 0)
540                 {
541                     gmx_fatal(FARGS, "Cannot flush logfile - maybe you are out of disk space?");
542                 }
543             }
544         }
545
546         /* Print the remaining wall clock time for the run */
547         if (isMasterSimMasterRank(ms, MASTER(cr)) && (mdrunOptions.verbose || gmx_got_usr_signal()))
548         {
549             if (shellfc)
550             {
551                 fprintf(stderr, "\n");
552             }
553             print_time(stderr, walltime_accounting, step, ir, cr);
554         }
555
556         cycles = wallcycle_stop(wcycle, ewcSTEP);
557         if (DOMAINDECOMP(cr) && wcycle)
558         {
559             dd_cycles_add(cr->dd, cycles, ddCyclStep);
560         }
561
562         /* increase the MD step number */
563         step++;
564         step_rel++;
565     }
566     /* End of main MD loop */
567
568     /* Closing TNG files can include compressing data. Therefore it is good to do that
569      * before stopping the time measurements. */
570     mdoutf_tng_close(outf);
571
572     /* Stop measuring walltime */
573     walltime_accounting_end_time(walltime_accounting);
574
575     if (MASTER(cr))
576     {
577         mimicCommunicator.finalize();
578     }
579
580     if (!thisRankHasDuty(cr, DUTY_PME))
581     {
582         /* Tell the PME only node to finish */
583         gmx_pme_send_finish(cr);
584     }
585
586     done_mdoutf(outf);
587
588     done_shellfc(fplog, shellfc, step_rel);
589
590     walltime_accounting_set_nsteps_done(walltime_accounting, step_rel);
591 }