Move functionality to mdrunutility
[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, 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.h"
64 #include "gromacs/ewald/pme_load_balancing.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/multisim.h"
99 #include "gromacs/mdrunutility/printtime.h"
100 #include "gromacs/mdtypes/awh_history.h"
101 #include "gromacs/mdtypes/awh_params.h"
102 #include "gromacs/mdtypes/commrec.h"
103 #include "gromacs/mdtypes/df_history.h"
104 #include "gromacs/mdtypes/enerdata.h"
105 #include "gromacs/mdtypes/energyhistory.h"
106 #include "gromacs/mdtypes/fcdata.h"
107 #include "gromacs/mdtypes/forcerec.h"
108 #include "gromacs/mdtypes/group.h"
109 #include "gromacs/mdtypes/inputrec.h"
110 #include "gromacs/mdtypes/interaction_const.h"
111 #include "gromacs/mdtypes/md_enums.h"
112 #include "gromacs/mdtypes/mdatom.h"
113 #include "gromacs/mdtypes/mdrunoptions.h"
114 #include "gromacs/mdtypes/observableshistory.h"
115 #include "gromacs/mdtypes/state.h"
116 #include "gromacs/mimic/communicator.h"
117 #include "gromacs/mimic/utilities.h"
118 #include "gromacs/pbcutil/mshift.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 "integrator.h"
136 #include "replicaexchange.h"
137 #include "shellfc.h"
138
139 using gmx::SimulationSignaller;
140
141 void gmx::Integrator::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     int                      force_flags;
149     tensor                   force_vir, shake_vir, total_vir, pres;
150     rvec                     mu_tot;
151     gmx_localtop_t           top;
152     PaddedVector<gmx::RVec>  f {};
153     gmx_global_stat_t        gstat;
154     t_graph                 *graph = nullptr;
155     gmx_shellfc_t           *shellfc;
156
157     double                   cycles;
158
159     /* Domain decomposition could incorrectly miss a bonded
160        interaction, but checking for that requires a global
161        communication stage, which does not otherwise happen in DD
162        code. So we do that alongside the first global energy reduction
163        after a new DD is made. These variables handle whether the
164        check happens, and the result it returns. */
165     bool              shouldCheckNumberOfBondedInteractions = false;
166     int               totalNumberOfBondedInteractions       = -1;
167
168     SimulationSignals signals;
169     // Most global communnication stages don't propagate mdrun
170     // signals, and will use this object to achieve that.
171     SimulationSignaller nullSignaller(nullptr, nullptr, nullptr, false, false);
172
173     if (ir->bExpanded)
174     {
175         gmx_fatal(FARGS, "Expanded ensemble not supported by MiMiC.");
176     }
177     if (ir->bSimTemp)
178     {
179         gmx_fatal(FARGS, "Simulated tempering not supported by MiMiC.");
180     }
181     if (ir->bDoAwh)
182     {
183         gmx_fatal(FARGS, "AWH not supported by MiMiC.");
184     }
185     if (replExParams.exchangeInterval > 0)
186     {
187         gmx_fatal(FARGS, "Replica exchange not supported by MiMiC.");
188     }
189     if (opt2bSet("-ei", nfile, fnm) || observablesHistory->edsamHistory != nullptr)
190     {
191         gmx_fatal(FARGS, "Essential dynamics not supported by MiMiC.");
192     }
193     if (ir->bIMD)
194     {
195         gmx_fatal(FARGS, "Interactive MD not supported by MiMiC.");
196     }
197     if (isMultiSim(ms))
198     {
199         gmx_fatal(FARGS, "Multiple simulations not supported by MiMiC.");
200     }
201     if (std::any_of(ir->opts.annealing, ir->opts.annealing + ir->opts.ngtc,
202                     [](int i){return i != eannNO; }))
203     {
204         gmx_fatal(FARGS, "Simulated annealing not supported by MiMiC.");
205     }
206
207     /* Settings for rerun */
208     ir->nstlist       = 1;
209     ir->nstcalcenergy = 1;
210     int        nstglobalcomm = 1;
211     const bool bNS           = true;
212
213     // Communicator to interact with MiMiC
214     MimicCommunicator mimicCommunicator {};
215     if (MASTER(cr))
216     {
217         mimicCommunicator.init();
218         mimicCommunicator.sendInitData(top_global, state_global->x);
219         ir->nsteps = mimicCommunicator.getStepNumber();
220     }
221
222     ir->nstxout_compressed                   = 0;
223     SimulationGroups *groups                        = &top_global->groups;
224     top_global->intermolecularExclusionGroup = genQmmmIndices(*top_global);
225
226     initialize_lambdas(fplog, *ir, MASTER(cr), &state_global->fep_state, state_global->lambda, lam0);
227     init_nrnb(nrnb);
228
229     gmx_mdoutf       *outf = init_mdoutf(fplog, nfile, fnm, mdrunOptions, cr, outputProvider, ir, top_global, oenv, wcycle);
230     gmx::EnergyOutput energyOutput;
231     energyOutput.prepare(mdoutf_get_fp_ene(outf), top_global, ir, pull_work, mdoutf_get_fp_dhdl(outf), true);
232
233     /* Kinetic energy data */
234     std::unique_ptr<gmx_ekindata_t> eKinData = std::make_unique<gmx_ekindata_t>();
235     gmx_ekindata_t                 *ekind    = eKinData.get();
236     init_ekindata(fplog, top_global, &(ir->opts), ekind);
237     /* Copy the cos acceleration to the groups struct */
238     ekind->cosacc.cos_accel = ir->cos_accel;
239
240     gstat = global_stat_init(ir);
241
242     /* Check for polarizable models and flexible constraints */
243     shellfc = init_shell_flexcon(fplog,
244                                  top_global, constr ? constr->numFlexibleConstraints() : 0,
245                                  ir->nstcalcenergy, DOMAINDECOMP(cr));
246
247     {
248         double io = compute_io(ir, top_global->natoms, *groups, energyOutput.numEnergyTerms(), 1);
249         if ((io > 2000) && MASTER(cr))
250         {
251             fprintf(stderr,
252                     "\nWARNING: This run will generate roughly %.0f Mb of data\n\n",
253                     io);
254         }
255     }
256
257     // Local state only becomes valid now.
258     std::unique_ptr<t_state> stateInstance;
259     t_state *                state;
260
261     if (DOMAINDECOMP(cr))
262     {
263         dd_init_local_top(*top_global, &top);
264
265         stateInstance = std::make_unique<t_state>();
266         state         = stateInstance.get();
267         dd_init_local_state(cr->dd, state_global, state);
268
269         /* Distribute the charge groups over the nodes from the master node */
270         dd_partition_system(fplog, mdlog, ir->init_step, cr, TRUE, 1,
271                             state_global, *top_global, ir, imdSession,
272                             pull_work,
273                             state, &f, mdAtoms, &top, fr,
274                             vsite, constr,
275                             nrnb, nullptr, FALSE);
276         shouldCheckNumberOfBondedInteractions = true;
277         gmx_bcast(sizeof(ir->nsteps), &ir->nsteps, cr);
278     }
279     else
280     {
281         state_change_natoms(state_global, state_global->natoms);
282         /* We need to allocate one element extra, since we might use
283          * (unaligned) 4-wide SIMD loads to access rvec entries.
284          */
285         f.resizeWithPadding(state_global->natoms);
286         /* Copy the pointer to the global state */
287         state = state_global;
288
289         mdAlgorithmsSetupAtomData(cr, ir, *top_global, &top, fr,
290                                   &graph, mdAtoms, constr, vsite, shellfc);
291     }
292
293     auto mdatoms = mdAtoms->mdatoms();
294
295     // NOTE: The global state is no longer used at this point.
296     // But state_global is still used as temporary storage space for writing
297     // the global state to file and potentially for replica exchange.
298     // (Global topology should persist.)
299
300     update_mdatoms(mdatoms, state->lambda[efptMASS]);
301
302     if (ir->efep != efepNO && ir->fepvals->nstdhdl != 0)
303     {
304         doFreeEnergyPerturbation = true;
305     }
306
307     {
308         int    cglo_flags = (CGLO_INITIALIZATION | CGLO_GSTAT |
309                              (shouldCheckNumberOfBondedInteractions ?
310                               CGLO_CHECK_NUMBER_OF_BONDED_INTERACTIONS : 0));
311         bool   bSumEkinhOld = false;
312         t_vcm *vcm          = nullptr;
313         compute_globals(fplog, gstat, cr, ir, fr, ekind, state, mdatoms, nrnb, vcm,
314                         nullptr, enerd, force_vir, shake_vir, total_vir, pres, mu_tot,
315                         constr, &nullSignaller, state->box,
316                         &totalNumberOfBondedInteractions, &bSumEkinhOld, cglo_flags);
317     }
318     checkNumberOfBondedInteractions(mdlog, cr, totalNumberOfBondedInteractions,
319                                     top_global, &top, state,
320                                     &shouldCheckNumberOfBondedInteractions);
321
322     if (MASTER(cr))
323     {
324         fprintf(stderr, "starting MiMiC MD run '%s'\n\n",
325                 *(top_global->name));
326         if (mdrunOptions.verbose)
327         {
328             fprintf(stderr, "Calculated time to finish depends on nsteps from "
329                     "run input file,\nwhich may not correspond to the time "
330                     "needed to process input trajectory.\n\n");
331         }
332         fprintf(fplog, "\n");
333     }
334
335     walltime_accounting_start_time(walltime_accounting);
336     wallcycle_start(wcycle, ewcRUN);
337     print_start(fplog, cr, walltime_accounting, "mdrun");
338
339     /***********************************************************
340      *
341      *             Loop over MD steps
342      *
343      ************************************************************/
344
345     if (constr)
346     {
347         GMX_LOG(mdlog.info).asParagraph().
348             appendText("Simulations has constraints. Constraints will "
349                        "be handled by CPMD.");
350     }
351
352     GMX_LOG(mdlog.info).asParagraph().
353         appendText("MiMiC does not report kinetic energy, total energy, temperature, virial and pressure.");
354
355     auto stopHandler = stopHandlerBuilder->getStopHandlerMD(
356                 compat::not_null<SimulationSignal*>(&signals[eglsSTOPCOND]), false,
357                 MASTER(cr), ir->nstlist, mdrunOptions.reproducible, nstglobalcomm,
358                 mdrunOptions.maximumHoursToRun, ir->nstlist == 0, fplog, step, bNS, walltime_accounting);
359
360     // we don't do counter resetting in rerun - finish will always be valid
361     walltime_accounting_set_valid_finish(walltime_accounting);
362
363     const DDBalanceRegionHandler ddBalanceRegionHandler(cr);
364
365     step     = ir->init_step;
366     step_rel = 0;
367
368     /* and stop now if we should */
369     isLastStep = (isLastStep || (ir->nsteps >= 0 && step_rel > ir->nsteps));
370     while (!isLastStep)
371     {
372         isLastStep = (isLastStep || (ir->nsteps >= 0 && step_rel == ir->nsteps));
373         wallcycle_start(wcycle, ewcSTEP);
374
375         t = step;
376
377         if (MASTER(cr))
378         {
379             mimicCommunicator.getCoords(&state_global->x, state_global->natoms);
380         }
381
382         if (ir->efep != efepNO)
383         {
384             setCurrentLambdasLocal(step, ir->fepvals, lam0, state);
385         }
386
387         if (MASTER(cr))
388         {
389             const bool constructVsites = ((vsite != nullptr) && mdrunOptions.rerunConstructVsites);
390             if (constructVsites && DOMAINDECOMP(cr))
391             {
392                 gmx_fatal(FARGS, "Vsite recalculation with -rerun is not implemented with domain decomposition, "
393                           "use a single rank");
394             }
395         }
396
397         if (DOMAINDECOMP(cr))
398         {
399             /* Repartition the domain decomposition */
400             const bool bMasterState = true;
401             dd_partition_system(fplog, mdlog, step, cr,
402                                 bMasterState, nstglobalcomm,
403                                 state_global, *top_global, ir, imdSession,
404                                 pull_work,
405                                 state, &f, mdAtoms, &top, fr,
406                                 vsite, constr,
407                                 nrnb, wcycle,
408                                 mdrunOptions.verbose);
409             shouldCheckNumberOfBondedInteractions = true;
410         }
411
412         if (MASTER(cr))
413         {
414             print_ebin_header(fplog, step, t); /* can we improve the information printed here? */
415         }
416
417         if (ir->efep != efepNO)
418         {
419             update_mdatoms(mdatoms, state->lambda[efptMASS]);
420         }
421
422         force_flags = (GMX_FORCE_STATECHANGED |
423                        GMX_FORCE_DYNAMICBOX |
424                        GMX_FORCE_ALLFORCES |
425                        GMX_FORCE_VIRIAL |  // TODO: Get rid of this once #2649 is solved
426                        GMX_FORCE_ENERGY |
427                        (doFreeEnergyPerturbation ? GMX_FORCE_DHDL : 0));
428
429         if (shellfc)
430         {
431             /* Now is the time to relax the shells */
432             relax_shell_flexcon(fplog, cr, ms, mdrunOptions.verbose,
433                                 enforcedRotation, step,
434                                 ir, imdSession, pull_work, bNS, force_flags, &top,
435                                 constr, enerd, fcd,
436                                 state, f.arrayRefWithPadding(), force_vir, mdatoms,
437                                 nrnb, wcycle, graph,
438                                 shellfc, fr, ppForceWorkload, t, mu_tot,
439                                 vsite,
440                                 ddBalanceRegionHandler);
441         }
442         else
443         {
444             /* The coordinates (x) are shifted (to get whole molecules)
445              * in do_force.
446              * This is parallellized as well, and does communication too.
447              * Check comments in sim_util.c
448              */
449             Awh       *awh = nullptr;
450             gmx_edsam *ed  = nullptr;
451             do_force(fplog, cr, ms, ir, awh, enforcedRotation, imdSession,
452                      pull_work,
453                      step, nrnb, wcycle, &top,
454                      state->box, state->x.arrayRefWithPadding(), &state->hist,
455                      f.arrayRefWithPadding(), force_vir, mdatoms, enerd, fcd,
456                      state->lambda, graph,
457                      fr, ppForceWorkload, vsite, mu_tot, t, ed,
458                      GMX_FORCE_NS | force_flags,
459                      ddBalanceRegionHandler);
460         }
461
462         /* Now we have the energies and forces corresponding to the
463          * coordinates at time t.
464          */
465         {
466             const bool isCheckpointingStep = false;
467             const bool doRerun             = false;
468             const bool bSumEkinhOld        = false;
469             do_md_trajectory_writing(fplog, cr, nfile, fnm, step, step_rel, t,
470                                      ir, state, state_global, observablesHistory,
471                                      top_global, fr,
472                                      outf, energyOutput, ekind, f,
473                                      isCheckpointingStep, doRerun, isLastStep,
474                                      mdrunOptions.writeConfout,
475                                      bSumEkinhOld);
476         }
477
478         stopHandler->setSignal();
479
480         if (graph)
481         {
482             /* Need to unshift here */
483             unshift_self(graph, state->box, as_rvec_array(state->x.data()));
484         }
485
486         if (vsite != nullptr)
487         {
488             wallcycle_start(wcycle, ewcVSITECONSTR);
489             if (graph != nullptr)
490             {
491                 shift_self(graph, state->box, as_rvec_array(state->x.data()));
492             }
493             construct_vsites(vsite, as_rvec_array(state->x.data()), ir->delta_t, as_rvec_array(state->v.data()),
494                              top.idef.iparams, top.idef.il,
495                              fr->ePBC, fr->bMolPBC, cr, state->box);
496
497             if (graph != nullptr)
498             {
499                 unshift_self(graph, state->box, as_rvec_array(state->x.data()));
500             }
501             wallcycle_stop(wcycle, ewcVSITECONSTR);
502         }
503
504         {
505             const bool          doInterSimSignal = false;
506             const bool          doIntraSimSignal = true;
507             bool                bSumEkinhOld     = false;
508             t_vcm              *vcm              = nullptr;
509             SimulationSignaller signaller(&signals, cr, ms, doInterSimSignal, doIntraSimSignal);
510
511             compute_globals(fplog, gstat, cr, ir, fr, ekind, state, mdatoms, nrnb, vcm,
512                             wcycle, enerd, nullptr, nullptr, nullptr, nullptr, mu_tot,
513                             constr, &signaller,
514                             state->box,
515                             &totalNumberOfBondedInteractions, &bSumEkinhOld,
516                             CGLO_GSTAT | CGLO_ENERGY
517                             | (shouldCheckNumberOfBondedInteractions ? CGLO_CHECK_NUMBER_OF_BONDED_INTERACTIONS : 0)
518                             );
519             checkNumberOfBondedInteractions(mdlog, cr, totalNumberOfBondedInteractions,
520                                             top_global, &top, state,
521                                             &shouldCheckNumberOfBondedInteractions);
522         }
523
524         {
525             gmx::HostVector<gmx::RVec>     fglobal(top_global->natoms);
526             gmx::ArrayRef<gmx::RVec>       ftemp;
527             gmx::ArrayRef<const gmx::RVec> flocal = gmx::makeArrayRef(f);
528             if (DOMAINDECOMP(cr))
529             {
530                 ftemp = gmx::makeArrayRef(fglobal);
531                 dd_collect_vec(cr->dd, state, flocal, ftemp);
532             }
533             else
534             {
535                 ftemp = gmx::makeArrayRef(f);
536             }
537
538             if (MASTER(cr))
539             {
540                 mimicCommunicator.sendEnergies(enerd->term[F_EPOT]);
541                 mimicCommunicator.sendForces(ftemp, state_global->natoms);
542             }
543         }
544
545
546
547         /* Note: this is OK, but there are some numerical precision issues with using the convergence of
548            the virial that should probably be addressed eventually. state->veta has better properies,
549            but what we actually need entering the new cycle is the new shake_vir value. Ideally, we could
550            generate the new shake_vir, but test the veta value for convergence.  This will take some thought. */
551
552         if (ir->efep != efepNO)
553         {
554             /* Sum up the foreign energy and dhdl terms for md and sd.
555                Currently done every step so that dhdl is correct in the .edr */
556             sum_dhdl(enerd, state->lambda, ir->fepvals);
557         }
558
559         /* Output stuff */
560         if (MASTER(cr))
561         {
562             const bool bCalcEnerStep = true;
563             energyOutput.addDataAtEnergyStep(doFreeEnergyPerturbation, bCalcEnerStep,
564                                              t, mdatoms->tmass, enerd, state,
565                                              ir->fepvals, ir->expandedvals, state->box,
566                                              shake_vir, force_vir, total_vir, pres,
567                                              ekind, mu_tot, constr);
568
569             const bool do_ene = true;
570             const bool do_log = true;
571             Awh       *awh    = nullptr;
572             const bool do_dr  = ir->nstdisreout != 0;
573             const bool do_or  = ir->nstorireout != 0;
574
575             energyOutput.printStepToEnergyFile(mdoutf_get_fp_ene(outf), do_ene, do_dr, do_or,
576                                                do_log ? fplog : nullptr,
577                                                step, t,
578                                                eprNORMAL, fcd, groups, &(ir->opts), awh);
579
580             if (do_per_step(step, ir->nstlog))
581             {
582                 if (fflush(fplog) != 0)
583                 {
584                     gmx_fatal(FARGS, "Cannot flush logfile - maybe you are out of disk space?");
585                 }
586             }
587         }
588
589         /* Print the remaining wall clock time for the run */
590         if (isMasterSimMasterRank(ms, MASTER(cr)) &&
591             (mdrunOptions.verbose || gmx_got_usr_signal()))
592         {
593             if (shellfc)
594             {
595                 fprintf(stderr, "\n");
596             }
597             print_time(stderr, walltime_accounting, step, ir, cr);
598         }
599
600         cycles = wallcycle_stop(wcycle, ewcSTEP);
601         if (DOMAINDECOMP(cr) && wcycle)
602         {
603             dd_cycles_add(cr->dd, cycles, ddCyclStep);
604         }
605
606         /* increase the MD step number */
607         step++;
608         step_rel++;
609     }
610     /* End of main MD loop */
611
612     /* Closing TNG files can include compressing data. Therefore it is good to do that
613      * before stopping the time measurements. */
614     mdoutf_tng_close(outf);
615
616     /* Stop measuring walltime */
617     walltime_accounting_end_time(walltime_accounting);
618
619     if (MASTER(cr))
620     {
621         mimicCommunicator.finalize();
622     }
623
624     if (!thisRankHasDuty(cr, DUTY_PME))
625     {
626         /* Tell the PME only node to finish */
627         gmx_pme_send_finish(cr);
628     }
629
630     done_mdoutf(outf);
631
632     done_shellfc(fplog, shellfc, step_rel);
633
634     walltime_accounting_set_nsteps_done(walltime_accounting, step_rel);
635 }