Remove unnecessary "move" on shared pointer
[alexxy/gromacs.git] / src / programs / mdrun / md.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "typedefs.h"
41 #include "smalloc.h"
42 #include "sysstuff.h"
43 #include "vec.h"
44 #include "statutil.h"
45 #include "vcm.h"
46 #include "mdebin.h"
47 #include "nrnb.h"
48 #include "calcmu.h"
49 #include "index.h"
50 #include "vsite.h"
51 #include "update.h"
52 #include "ns.h"
53 #include "trnio.h"
54 #include "xtcio.h"
55 #include "mdrun.h"
56 #include "confio.h"
57 #include "network.h"
58 #include "pull.h"
59 #include "xvgr.h"
60 #include "physics.h"
61 #include "names.h"
62 #include "xmdrun.h"
63 #include "ionize.h"
64 #include "disre.h"
65 #include "orires.h"
66 #include "pme.h"
67 #include "mdatoms.h"
68 #include "repl_ex.h"
69 #include "qmmm.h"
70 #include "domdec.h"
71 #include "partdec.h"
72 #include "topsort.h"
73 #include "coulomb.h"
74 #include "constr.h"
75 #include "shellfc.h"
76 #include "compute_io.h"
77 #include "mvdata.h"
78 #include "checkpoint.h"
79 #include "mtop_util.h"
80 #include "sighandler.h"
81 #include "txtdump.h"
82 #include "string2.h"
83 #include "membed.h"
84
85 #ifdef GMX_LIB_MPI
86 #include <mpi.h>
87 #endif
88 #ifdef GMX_THREAD_MPI
89 #include "tmpi.h"
90 #endif
91
92 #ifdef GMX_FAHCORE
93 #include "corewrap.h"
94 #endif
95
96 double do_md(FILE *fplog,t_commrec *cr,int nfile,const t_filenm fnm[],
97              const output_env_t oenv, gmx_bool bVerbose,gmx_bool bCompact,
98              int nstglobalcomm,
99              gmx_vsite_t *vsite,gmx_constr_t constr,
100              int stepout,t_inputrec *ir,
101              gmx_mtop_t *top_global,
102              t_fcdata *fcd,
103              t_state *state_global,
104              t_mdatoms *mdatoms,
105              t_nrnb *nrnb,gmx_wallcycle_t wcycle,
106              gmx_edsam_t ed,t_forcerec *fr,
107              int repl_ex_nst,int repl_ex_nex,int repl_ex_seed,gmx_membed_t membed,
108              real cpt_period,real max_hours,
109              const char *deviceOptions,
110              unsigned long Flags,
111              gmx_runtime_t *runtime)
112 {
113     gmx_mdoutf_t *outf;
114     gmx_large_int_t step,step_rel;
115     double     run_time;
116     double     t,t0,lam0[efptNR];
117     gmx_bool   bGStatEveryStep,bGStat,bNstEner,bCalcEnerPres,bEnergyHere;
118     gmx_bool   bNS,bNStList,bSimAnn,bStopCM,bRerunMD,bNotLastFrame=FALSE,
119                bFirstStep,bStateFromCP,bStateFromTPX,bInitStep,bLastStep,
120                bBornRadii,bStartingFromCpt;
121     gmx_bool   bDoDHDL=FALSE,bDoFEP=FALSE,bDoExpanded=FALSE;
122     gmx_bool   do_ene,do_log,do_verbose,bRerunWarnNoV=TRUE,
123                bForceUpdate=FALSE,bCPT;
124     int        mdof_flags;
125     gmx_bool   bMasterState;
126     int        force_flags,cglo_flags;
127     tensor     force_vir,shake_vir,total_vir,tmp_vir,pres;
128     int        i,m;
129     t_trxstatus *status;
130     rvec       mu_tot;
131     t_vcm      *vcm;
132     t_state    *bufstate=NULL;   
133     matrix     *scale_tot,pcoupl_mu,M,ebox;
134     gmx_nlheur_t nlh;
135     t_trxframe rerun_fr;
136     gmx_repl_ex_t repl_ex=NULL;
137     int        nchkpt=1;
138     gmx_localtop_t *top;        
139     t_mdebin *mdebin=NULL;
140     df_history_t df_history;
141     t_state    *state=NULL;
142     rvec       *f_global=NULL;
143     int        n_xtc=-1;
144     rvec       *x_xtc=NULL;
145     gmx_enerdata_t *enerd;
146     rvec       *f=NULL;
147     gmx_global_stat_t gstat;
148     gmx_update_t upd=NULL;
149     t_graph    *graph=NULL;
150     globsig_t   gs;
151     gmx_rng_t mcrng=NULL;
152     gmx_bool        bFFscan;
153     gmx_groups_t *groups;
154     gmx_ekindata_t *ekind, *ekind_save;
155     gmx_shellfc_t shellfc;
156     int         count,nconverged=0;
157     real        timestep=0;
158     double      tcount=0;
159     gmx_bool        bIonize=FALSE;
160     gmx_bool        bTCR=FALSE,bConverged=TRUE,bOK,bSumEkinhOld,bExchanged;
161     gmx_bool        bAppend;
162     gmx_bool        bResetCountersHalfMaxH=FALSE;
163     gmx_bool        bVV,bIterations,bFirstIterate,bTemp,bPres,bTrotter;
164     real        mu_aver=0,dvdl;
165     int         a0,a1,gnx=0,ii;
166     atom_id     *grpindex=NULL;
167     char        *grpname;
168     t_coupl_rec *tcr=NULL;
169     rvec        *xcopy=NULL,*vcopy=NULL,*cbuf=NULL;
170     matrix      boxcopy={{0}},lastbox;
171         tensor      tmpvir;
172         real        fom,oldfom,veta_save,pcurr,scalevir,tracevir;
173         real        vetanew = 0;
174     int         lamnew=0;
175     /* for FEP */
176     int         fep_state=0;
177     int         nstfep;
178     real        rate;
179     double      cycles;
180         real        saved_conserved_quantity = 0;
181     real        last_ekin = 0;
182         int         iter_i;
183         t_extmass   MassQ;
184     int         **trotter_seq; 
185     char        sbuf[STEPSTRSIZE],sbuf2[STEPSTRSIZE];
186     int         handled_stop_condition=gmx_stop_cond_none; /* compare to get_stop_condition*/
187     gmx_iterate_t iterate;
188     gmx_large_int_t multisim_nsteps=-1; /* number of steps to do  before first multisim 
189                                           simulation stops. If equal to zero, don't
190                                           communicate any more between multisims.*/
191 #ifdef GMX_FAHCORE
192     /* Temporary addition for FAHCORE checkpointing */
193     int chkpt_ret;
194 #endif
195
196     /* Check for special mdrun options */
197     bRerunMD = (Flags & MD_RERUN);
198     bIonize  = (Flags & MD_IONIZE);
199     bFFscan  = (Flags & MD_FFSCAN);
200     bAppend  = (Flags & MD_APPENDFILES);
201     if (Flags & MD_RESETCOUNTERSHALFWAY)
202     {
203         if (ir->nsteps > 0)
204         {
205             /* Signal to reset the counters half the simulation steps. */
206             wcycle_set_reset_counters(wcycle,ir->nsteps/2);
207         }
208         /* Signal to reset the counters halfway the simulation time. */
209         bResetCountersHalfMaxH = (max_hours > 0);
210     }
211
212     /* md-vv uses averaged full step velocities for T-control 
213        md-vv-avek uses averaged half step velocities for T-control (but full step ekin for P control)
214        md uses averaged half step kinetic energies to determine temperature unless defined otherwise by GMX_EKIN_AVE_VEL; */
215     bVV = EI_VV(ir->eI);
216     if (bVV) /* to store the initial velocities while computing virial */
217     {
218         snew(cbuf,top_global->natoms);
219     }
220     /* all the iteratative cases - only if there are constraints */ 
221     bIterations = ((IR_NPH_TROTTER(ir) || IR_NPT_TROTTER(ir)) && (constr) && (!bRerunMD));
222     bTrotter = (bVV && (IR_NPT_TROTTER(ir) || IR_NPH_TROTTER(ir) || IR_NVT_TROTTER(ir)));
223     
224     if (bRerunMD)
225     {
226         /* Since we don't know if the frames read are related in any way,
227          * rebuild the neighborlist at every step.
228          */
229         ir->nstlist       = 1;
230         ir->nstcalcenergy = 1;
231         nstglobalcomm     = 1;
232     }
233
234     check_ir_old_tpx_versions(cr,fplog,ir,top_global);
235
236     nstglobalcomm = check_nstglobalcomm(fplog,cr,nstglobalcomm,ir);
237     bGStatEveryStep = (nstglobalcomm == 1);
238
239     if (!bGStatEveryStep && ir->nstlist == -1 && fplog != NULL)
240     {
241         fprintf(fplog,
242                 "To reduce the energy communication with nstlist = -1\n"
243                 "the neighbor list validity should not be checked at every step,\n"
244                 "this means that exact integration is not guaranteed.\n"
245                 "The neighbor list validity is checked after:\n"
246                 "  <n.list life time> - 2*std.dev.(n.list life time)  steps.\n"
247                 "In most cases this will result in exact integration.\n"
248                 "This reduces the energy communication by a factor of 2 to 3.\n"
249                 "If you want less energy communication, set nstlist > 3.\n\n");
250     }
251
252     if (bRerunMD || bFFscan)
253     {
254         ir->nstxtcout = 0;
255     }
256     groups = &top_global->groups;
257
258     /* Initial values */
259     init_md(fplog,cr,ir,oenv,&t,&t0,state_global->lambda,
260             &(state_global->fep_state),lam0,
261             nrnb,top_global,&upd,
262             nfile,fnm,&outf,&mdebin,
263             force_vir,shake_vir,mu_tot,&bSimAnn,&vcm,state_global,Flags);
264
265     clear_mat(total_vir);
266     clear_mat(pres);
267     /* Energy terms and groups */
268     snew(enerd,1);
269     init_enerdata(top_global->groups.grps[egcENER].nr,ir->fepvals->n_lambda,
270                   enerd);
271     if (DOMAINDECOMP(cr))
272     {
273         f = NULL;
274     }
275     else
276     {
277         snew(f,top_global->natoms);
278     }
279
280     /* lambda Monte carlo random number generator  */
281     if (ir->bExpanded)
282     {
283         mcrng = gmx_rng_init(ir->expandedvals->lmc_seed);
284     }
285     /* copy the state into df_history */
286     copy_df_history(&df_history,&state_global->dfhist);
287
288     /* Kinetic energy data */
289     snew(ekind,1);
290     init_ekindata(fplog,top_global,&(ir->opts),ekind);
291     /* needed for iteration of constraints */
292     snew(ekind_save,1);
293     init_ekindata(fplog,top_global,&(ir->opts),ekind_save);
294     /* Copy the cos acceleration to the groups struct */    
295     ekind->cosacc.cos_accel = ir->cos_accel;
296
297     gstat = global_stat_init(ir);
298     debug_gmx();
299
300     /* Check for polarizable models and flexible constraints */
301     shellfc = init_shell_flexcon(fplog,
302                                  top_global,n_flexible_constraints(constr),
303                                  (ir->bContinuation || 
304                                   (DOMAINDECOMP(cr) && !MASTER(cr))) ?
305                                  NULL : state_global->x);
306
307     if (DEFORM(*ir))
308     {
309 #ifdef GMX_THREAD_MPI
310         tMPI_Thread_mutex_lock(&deform_init_box_mutex);
311 #endif
312         set_deform_reference_box(upd,
313                                  deform_init_init_step_tpx,
314                                  deform_init_box_tpx);
315 #ifdef GMX_THREAD_MPI
316         tMPI_Thread_mutex_unlock(&deform_init_box_mutex);
317 #endif
318     }
319
320     {
321         double io = compute_io(ir,top_global->natoms,groups,mdebin->ebin->nener,1);
322         if ((io > 2000) && MASTER(cr))
323             fprintf(stderr,
324                     "\nWARNING: This run will generate roughly %.0f Mb of data\n\n",
325                     io);
326     }
327
328     if (DOMAINDECOMP(cr)) {
329         top = dd_init_local_top(top_global);
330
331         snew(state,1);
332         dd_init_local_state(cr->dd,state_global,state);
333
334         if (DDMASTER(cr->dd) && ir->nstfout) {
335             snew(f_global,state_global->natoms);
336         }
337     } else {
338         if (PAR(cr)) {
339             /* Initialize the particle decomposition and split the topology */
340             top = split_system(fplog,top_global,ir,cr);
341
342             pd_cg_range(cr,&fr->cg0,&fr->hcg);
343             pd_at_range(cr,&a0,&a1);
344         } else {
345             top = gmx_mtop_generate_local_top(top_global,ir);
346
347             a0 = 0;
348             a1 = top_global->natoms;
349         }
350
351         state = partdec_init_local_state(cr,state_global);
352         f_global = f;
353
354         atoms2md(top_global,ir,0,NULL,a0,a1-a0,mdatoms);
355
356         if (vsite) {
357             set_vsite_top(vsite,top,mdatoms,cr);
358         }
359
360         if (ir->ePBC != epbcNONE && !ir->bPeriodicMols) {
361             graph = mk_graph(fplog,&(top->idef),0,top_global->natoms,FALSE,FALSE);
362         }
363
364         if (shellfc) {
365             make_local_shells(cr,mdatoms,shellfc);
366         }
367
368         if (ir->pull && PAR(cr)) {
369             dd_make_local_pull_groups(NULL,ir->pull,mdatoms);
370         }
371     }
372
373     if (DOMAINDECOMP(cr))
374     {
375         /* Distribute the charge groups over the nodes from the master node */
376         dd_partition_system(fplog,ir->init_step,cr,TRUE,1,
377                             state_global,top_global,ir,
378                             state,&f,mdatoms,top,fr,
379                             vsite,shellfc,constr,
380                             nrnb,wcycle,FALSE);
381     }
382
383     update_mdatoms(mdatoms,state->lambda[efptMASS]);
384
385     if (opt2bSet("-cpi",nfile,fnm))
386     {
387         bStateFromCP = gmx_fexist_master(opt2fn_master("-cpi",nfile,fnm,cr),cr);
388     }
389     else
390     {
391         bStateFromCP = FALSE;
392     }
393
394     if (MASTER(cr))
395     {
396         if (bStateFromCP)
397         {
398             /* Update mdebin with energy history if appending to output files */
399             if ( Flags & MD_APPENDFILES )
400             {
401                 restore_energyhistory_from_state(mdebin,&state_global->enerhist);
402             }
403             else
404             {
405                 /* We might have read an energy history from checkpoint,
406                  * free the allocated memory and reset the counts.
407                  */
408                 done_energyhistory(&state_global->enerhist);
409                 init_energyhistory(&state_global->enerhist);
410             }
411         }
412         /* Set the initial energy history in state by updating once */
413         update_energyhistory(&state_global->enerhist,mdebin);
414     }   
415
416     if ((state->flags & (1<<estLD_RNG)) && (Flags & MD_READ_RNG)) 
417     {
418         /* Set the random state if we read a checkpoint file */
419         set_stochd_state(upd,state);
420     }
421
422     if (state->flags & (1<<estMC_RNG))
423     {
424         set_mc_state(mcrng,state);
425     }
426
427     /* Initialize constraints */
428     if (constr) {
429         if (!DOMAINDECOMP(cr))
430             set_constraints(constr,top,ir,mdatoms,cr);
431     }
432
433     /* Check whether we have to GCT stuff */
434     bTCR = ftp2bSet(efGCT,nfile,fnm);
435     if (bTCR) {
436         if (MASTER(cr)) {
437             fprintf(stderr,"Will do General Coupling Theory!\n");
438         }
439         gnx = top_global->mols.nr;
440         snew(grpindex,gnx);
441         for(i=0; (i<gnx); i++) {
442             grpindex[i] = i;
443         }
444     }
445
446     if (repl_ex_nst > 0)
447     {
448         /* We need to be sure replica exchange can only occur
449          * when the energies are current */
450         check_nst_param(fplog,cr,"nstcalcenergy",ir->nstcalcenergy,
451                         "repl_ex_nst",&repl_ex_nst);
452         /* This check needs to happen before inter-simulation
453          * signals are initialized, too */
454     }
455     if (repl_ex_nst > 0 && MASTER(cr))
456     {
457         repl_ex = init_replica_exchange(fplog,cr->ms,state_global,ir,
458                                         repl_ex_nst,repl_ex_nex,repl_ex_seed); 
459     }
460     if (!ir->bContinuation && !bRerunMD)
461     {
462         if (mdatoms->cFREEZE && (state->flags & (1<<estV)))
463         {
464             /* Set the velocities of frozen particles to zero */
465             for(i=mdatoms->start; i<mdatoms->start+mdatoms->homenr; i++)
466             {
467                 for(m=0; m<DIM; m++)
468                 {
469                     if (ir->opts.nFreeze[mdatoms->cFREEZE[i]][m])
470                     {
471                         state->v[i][m] = 0;
472                     }
473                 }
474             }
475         }
476
477         if (constr)
478         {
479             /* Constrain the initial coordinates and velocities */
480             do_constrain_first(fplog,constr,ir,mdatoms,state,f,
481                                graph,cr,nrnb,fr,top,shake_vir);
482         }
483         if (vsite)
484         {
485             /* Construct the virtual sites for the initial configuration */
486             construct_vsites(fplog,vsite,state->x,nrnb,ir->delta_t,NULL,
487                              top->idef.iparams,top->idef.il,
488                              fr->ePBC,fr->bMolPBC,graph,cr,state->box);
489         }
490     }
491
492     debug_gmx();
493
494     /* set free energy calculation frequency as the minimum of nstdhdl, nstexpanded, and nstrepl_ex_nst*/
495     nstfep = ir->fepvals->nstdhdl;
496     if (ir->bExpanded && (nstfep > ir->expandedvals->nstexpanded))
497     {
498         nstfep = ir->expandedvals->nstexpanded;
499     }
500     if (repl_ex_nst > 0 && repl_ex_nst > nstfep)
501     {
502         nstfep = repl_ex_nst;
503     }
504
505     /* I'm assuming we need global communication the first time! MRS */
506     cglo_flags = (CGLO_TEMPERATURE | CGLO_GSTAT
507                   | ((ir->comm_mode != ecmNO) ? CGLO_STOPCM:0)
508                   | (bVV ? CGLO_PRESSURE:0)
509                   | (bVV ? CGLO_CONSTRAINT:0)
510                   | (bRerunMD ? CGLO_RERUNMD:0)
511                   | ((Flags & MD_READ_EKIN) ? CGLO_READEKIN:0));
512     
513     bSumEkinhOld = FALSE;
514     compute_globals(fplog,gstat,cr,ir,fr,ekind,state,state_global,mdatoms,nrnb,vcm,
515                     NULL,enerd,force_vir,shake_vir,total_vir,pres,mu_tot,
516                     constr,NULL,FALSE,state->box,
517                     top_global,&pcurr,top_global->natoms,&bSumEkinhOld,cglo_flags);
518     if (ir->eI == eiVVAK) {
519         /* a second call to get the half step temperature initialized as well */ 
520         /* we do the same call as above, but turn the pressure off -- internally to 
521            compute_globals, this is recognized as a velocity verlet half-step 
522            kinetic energy calculation.  This minimized excess variables, but 
523            perhaps loses some logic?*/
524         
525         compute_globals(fplog,gstat,cr,ir,fr,ekind,state,state_global,mdatoms,nrnb,vcm,
526                         NULL,enerd,force_vir,shake_vir,total_vir,pres,mu_tot,
527                         constr,NULL,FALSE,state->box,
528                         top_global,&pcurr,top_global->natoms,&bSumEkinhOld,
529                         cglo_flags &~ (CGLO_STOPCM | CGLO_PRESSURE));
530     }
531     
532     /* Calculate the initial half step temperature, and save the ekinh_old */
533     if (!(Flags & MD_STARTFROMCPT)) 
534     {
535         for(i=0; (i<ir->opts.ngtc); i++) 
536         {
537             copy_mat(ekind->tcstat[i].ekinh,ekind->tcstat[i].ekinh_old);
538         } 
539     }
540     if (ir->eI != eiVV) 
541     {
542         enerd->term[F_TEMP] *= 2; /* result of averages being done over previous and current step,
543                                      and there is no previous step */
544     }
545     
546     /* if using an iterative algorithm, we need to create a working directory for the state. */
547     if (bIterations) 
548     {
549             bufstate = init_bufstate(state);
550     }
551     if (bFFscan) 
552     {
553         snew(xcopy,state->natoms);
554         snew(vcopy,state->natoms);
555         copy_rvecn(state->x,xcopy,0,state->natoms);
556         copy_rvecn(state->v,vcopy,0,state->natoms);
557         copy_mat(state->box,boxcopy);
558     } 
559     
560     /* need to make an initiation call to get the Trotter variables set, as well as other constants for non-trotter
561        temperature control */
562     trotter_seq = init_npt_vars(ir,state,&MassQ,bTrotter);
563     
564     if (MASTER(cr))
565     {
566         if (constr && !ir->bContinuation && ir->eConstrAlg == econtLINCS)
567         {
568             fprintf(fplog,
569                     "RMS relative constraint deviation after constraining: %.2e\n",
570                     constr_rmsd(constr,FALSE));
571         }
572         if (EI_STATE_VELOCITY(ir->eI))
573         {
574             fprintf(fplog,"Initial temperature: %g K\n",enerd->term[F_TEMP]);
575         }
576         if (bRerunMD)
577         {
578             fprintf(stderr,"starting md rerun '%s', reading coordinates from"
579                     " input trajectory '%s'\n\n",
580                     *(top_global->name),opt2fn("-rerun",nfile,fnm));
581             if (bVerbose)
582             {
583                 fprintf(stderr,"Calculated time to finish depends on nsteps from "
584                         "run input file,\nwhich may not correspond to the time "
585                         "needed to process input trajectory.\n\n");
586             }
587         }
588         else
589         {
590             char tbuf[20];
591             fprintf(stderr,"starting mdrun '%s'\n",
592                     *(top_global->name));
593             if (ir->nsteps >= 0)
594             {
595                 sprintf(tbuf,"%8.1f",(ir->init_step+ir->nsteps)*ir->delta_t);
596             }
597             else
598             {
599                 sprintf(tbuf,"%s","infinite");
600             }
601             if (ir->init_step > 0)
602             {
603                 fprintf(stderr,"%s steps, %s ps (continuing from step %s, %8.1f ps).\n",
604                         gmx_step_str(ir->init_step+ir->nsteps,sbuf),tbuf,
605                         gmx_step_str(ir->init_step,sbuf2),
606                         ir->init_step*ir->delta_t);
607             }
608             else
609             {
610                 fprintf(stderr,"%s steps, %s ps.\n",
611                         gmx_step_str(ir->nsteps,sbuf),tbuf);
612             }
613         }
614         fprintf(fplog,"\n");
615     }
616
617     /* Set and write start time */
618     runtime_start(runtime);
619     print_date_and_time(fplog,cr->nodeid,"Started mdrun",runtime);
620     wallcycle_start(wcycle,ewcRUN);
621     if (fplog)
622     {
623         fprintf(fplog,"\n");
624     }
625
626     /* safest point to do file checkpointing is here.  More general point would be immediately before integrator call */
627 #ifdef GMX_FAHCORE
628     chkpt_ret=fcCheckPointParallel( cr->nodeid,
629                                     NULL,0);
630     if ( chkpt_ret == 0 ) 
631         gmx_fatal( 3,__FILE__,__LINE__, "Checkpoint error on step %d\n", 0 );
632 #endif
633
634     debug_gmx();
635     /***********************************************************
636      *
637      *             Loop over MD steps 
638      *
639      ************************************************************/
640
641     /* if rerunMD then read coordinates and velocities from input trajectory */
642     if (bRerunMD)
643     {
644         if (getenv("GMX_FORCE_UPDATE"))
645         {
646             bForceUpdate = TRUE;
647         }
648
649         rerun_fr.natoms = 0;
650         if (MASTER(cr))
651         {
652             bNotLastFrame = read_first_frame(oenv,&status,
653                                              opt2fn("-rerun",nfile,fnm),
654                                              &rerun_fr,TRX_NEED_X | TRX_READ_V);
655             if (rerun_fr.natoms != top_global->natoms)
656             {
657                 gmx_fatal(FARGS,
658                           "Number of atoms in trajectory (%d) does not match the "
659                           "run input file (%d)\n",
660                           rerun_fr.natoms,top_global->natoms);
661             }
662             if (ir->ePBC != epbcNONE)
663             {
664                 if (!rerun_fr.bBox)
665                 {
666                     gmx_fatal(FARGS,"Rerun trajectory frame step %d time %f does not contain a box, while pbc is used",rerun_fr.step,rerun_fr.time);
667                 }
668                 if (max_cutoff2(ir->ePBC,rerun_fr.box) < sqr(fr->rlistlong))
669                 {
670                     gmx_fatal(FARGS,"Rerun trajectory frame step %d time %f has too small box dimensions",rerun_fr.step,rerun_fr.time);
671                 }
672             }
673         }
674
675         if (PAR(cr))
676         {
677             rerun_parallel_comm(cr,&rerun_fr,&bNotLastFrame);
678         }
679
680         if (ir->ePBC != epbcNONE)
681         {
682             /* Set the shift vectors.
683              * Necessary here when have a static box different from the tpr box.
684              */
685             calc_shifts(rerun_fr.box,fr->shift_vec);
686         }
687     }
688
689     /* loop over MD steps or if rerunMD to end of input trajectory */
690     bFirstStep = TRUE;
691     /* Skip the first Nose-Hoover integration when we get the state from tpx */
692     bStateFromTPX = !bStateFromCP;
693     bInitStep = bFirstStep && (bStateFromTPX || bVV);
694     bStartingFromCpt = (Flags & MD_STARTFROMCPT) && bInitStep;
695     bLastStep    = FALSE;
696     bSumEkinhOld = FALSE;
697     bExchanged   = FALSE;
698
699     init_global_signals(&gs,cr,ir,repl_ex_nst);
700
701     step = ir->init_step;
702     step_rel = 0;
703
704     if (ir->nstlist == -1)
705     {
706         init_nlistheuristics(&nlh,bGStatEveryStep,step);
707     }
708
709     if (MULTISIM(cr) && (repl_ex_nst <=0 ))
710     {
711         /* check how many steps are left in other sims */
712         multisim_nsteps=get_multisim_nsteps(cr, ir->nsteps);
713     }
714
715
716     /* and stop now if we should */
717     bLastStep = (bRerunMD || (ir->nsteps >= 0 && step_rel > ir->nsteps) ||
718                  ((multisim_nsteps >= 0) && (step_rel >= multisim_nsteps )));
719     while (!bLastStep || (bRerunMD && bNotLastFrame)) {
720
721         wallcycle_start(wcycle,ewcSTEP);
722
723         if (bRerunMD) {
724             if (rerun_fr.bStep) {
725                 step = rerun_fr.step;
726                 step_rel = step - ir->init_step;
727             }
728             if (rerun_fr.bTime) {
729                 t = rerun_fr.time;
730             }
731             else
732             {
733                 t = step;
734             }
735         } 
736         else 
737         {
738             bLastStep = (step_rel == ir->nsteps);
739             t = t0 + step*ir->delta_t;
740         }
741
742         if (ir->efep != efepNO || ir->bSimTemp)
743         {
744             /* find and set the current lambdas.  If rerunning, we either read in a state, or a lambda value,
745                requiring different logic. */
746             
747             set_current_lambdas(step,ir->fepvals,bRerunMD,&rerun_fr,state_global,state,lam0);
748             bDoDHDL = do_per_step(step,ir->fepvals->nstdhdl);
749             bDoFEP  = (do_per_step(step,nstfep) && (ir->efep != efepNO));
750             bDoExpanded  = (do_per_step(step,ir->expandedvals->nstexpanded) && (ir->bExpanded) && (step > 0));
751         }
752
753         if (bSimAnn) 
754         {
755             update_annealing_target_temp(&(ir->opts),t);
756         }
757
758         if (bRerunMD)
759         {
760             if (!(DOMAINDECOMP(cr) && !MASTER(cr)))
761             {
762                 for(i=0; i<state_global->natoms; i++)
763                 {
764                     copy_rvec(rerun_fr.x[i],state_global->x[i]);
765                 }
766                 if (rerun_fr.bV)
767                 {
768                     for(i=0; i<state_global->natoms; i++)
769                     {
770                         copy_rvec(rerun_fr.v[i],state_global->v[i]);
771                     }
772                 }
773                 else
774                 {
775                     for(i=0; i<state_global->natoms; i++)
776                     {
777                         clear_rvec(state_global->v[i]);
778                     }
779                     if (bRerunWarnNoV)
780                     {
781                         fprintf(stderr,"\nWARNING: Some frames do not contain velocities.\n"
782                                 "         Ekin, temperature and pressure are incorrect,\n"
783                                 "         the virial will be incorrect when constraints are present.\n"
784                                 "\n");
785                         bRerunWarnNoV = FALSE;
786                     }
787                 }
788             }
789             copy_mat(rerun_fr.box,state_global->box);
790             copy_mat(state_global->box,state->box);
791
792             if (vsite && (Flags & MD_RERUN_VSITE))
793             {
794                 if (DOMAINDECOMP(cr))
795                 {
796                     gmx_fatal(FARGS,"Vsite recalculation with -rerun is not implemented for domain decomposition, use particle decomposition");
797                 }
798                 if (graph)
799                 {
800                     /* Following is necessary because the graph may get out of sync
801                      * with the coordinates if we only have every N'th coordinate set
802                      */
803                     mk_mshift(fplog,graph,fr->ePBC,state->box,state->x);
804                     shift_self(graph,state->box,state->x);
805                 }
806                 construct_vsites(fplog,vsite,state->x,nrnb,ir->delta_t,state->v,
807                                  top->idef.iparams,top->idef.il,
808                                  fr->ePBC,fr->bMolPBC,graph,cr,state->box);
809                 if (graph)
810                 {
811                     unshift_self(graph,state->box,state->x);
812                 }
813             }
814         }
815
816         /* Stop Center of Mass motion */
817         bStopCM = (ir->comm_mode != ecmNO && do_per_step(step,ir->nstcomm));
818
819         /* Copy back starting coordinates in case we're doing a forcefield scan */
820         if (bFFscan)
821         {
822             for(ii=0; (ii<state->natoms); ii++)
823             {
824                 copy_rvec(xcopy[ii],state->x[ii]);
825                 copy_rvec(vcopy[ii],state->v[ii]);
826             }
827             copy_mat(boxcopy,state->box);
828         }
829
830         if (bRerunMD)
831         {
832             /* for rerun MD always do Neighbour Searching */
833             bNS = (bFirstStep || ir->nstlist != 0);
834             bNStList = bNS;
835         }
836         else
837         {
838             /* Determine whether or not to do Neighbour Searching and LR */
839             bNStList = (ir->nstlist > 0  && step % ir->nstlist == 0);
840             
841             bNS = (bFirstStep || bExchanged || bNStList || bDoFEP ||
842                    (ir->nstlist == -1 && nlh.nabnsb > 0));
843
844             if (bNS && ir->nstlist == -1)
845             {
846                 set_nlistheuristics(&nlh,bFirstStep || bExchanged || bDoFEP, step);
847             }
848         } 
849
850         /* check whether we should stop because another simulation has 
851            stopped. */
852         if (MULTISIM(cr))
853         {
854             if ( (multisim_nsteps >= 0) &&  (step_rel >= multisim_nsteps)  &&  
855                  (multisim_nsteps != ir->nsteps) )  
856             {
857                 if (bNS)
858                 {
859                     if (MASTER(cr))
860                     {
861                         fprintf(stderr, 
862                                 "Stopping simulation %d because another one has finished\n",
863                                 cr->ms->sim);
864                     }
865                     bLastStep=TRUE;
866                     gs.sig[eglsCHKPT] = 1;
867                 }
868             }
869         }
870
871         /* < 0 means stop at next step, > 0 means stop at next NS step */
872         if ( (gs.set[eglsSTOPCOND] < 0 ) ||
873              ( (gs.set[eglsSTOPCOND] > 0 ) && ( bNS || ir->nstlist==0)) )
874         {
875             bLastStep = TRUE;
876         }
877
878         /* Determine whether or not to update the Born radii if doing GB */
879         bBornRadii=bFirstStep;
880         if (ir->implicit_solvent && (step % ir->nstgbradii==0))
881         {
882             bBornRadii=TRUE;
883         }
884         
885         do_log = do_per_step(step,ir->nstlog) || bFirstStep || bLastStep;
886         do_verbose = bVerbose &&
887                   (step % stepout == 0 || bFirstStep || bLastStep);
888
889         if (bNS && !(bFirstStep && ir->bContinuation && !bRerunMD))
890         {
891             if (bRerunMD)
892             {
893                 bMasterState = TRUE;
894             }
895             else
896             {
897                 bMasterState = FALSE;
898                 /* Correct the new box if it is too skewed */
899                 if (DYNAMIC_BOX(*ir))
900                 {
901                     if (correct_box(fplog,step,state->box,graph))
902                     {
903                         bMasterState = TRUE;
904                     }
905                 }
906                 if (DOMAINDECOMP(cr) && bMasterState)
907                 {
908                     dd_collect_state(cr->dd,state,state_global);
909                 }
910             }
911
912             if (DOMAINDECOMP(cr))
913             {
914                 /* Repartition the domain decomposition */
915                 wallcycle_start(wcycle,ewcDOMDEC);
916                 dd_partition_system(fplog,step,cr,
917                                     bMasterState,nstglobalcomm,
918                                     state_global,top_global,ir,
919                                     state,&f,mdatoms,top,fr,
920                                     vsite,shellfc,constr,
921                                     nrnb,wcycle,do_verbose);
922                 wallcycle_stop(wcycle,ewcDOMDEC);
923                 /* If using an iterative integrator, reallocate space to match the decomposition */
924             }
925         }
926
927         if (MASTER(cr) && do_log && !bFFscan)
928         {
929             print_ebin_header(fplog,step,t,state->lambda[efptFEP]); /* can we improve the information printed here? */
930         }
931
932         if (ir->efep != efepNO)
933         {
934             update_mdatoms(mdatoms,state->lambda[efptMASS]);
935         }
936
937         if (bRerunMD && rerun_fr.bV)
938         {
939             
940             /* We need the kinetic energy at minus the half step for determining
941              * the full step kinetic energy and possibly for T-coupling.*/
942             /* This may not be quite working correctly yet . . . . */
943             compute_globals(fplog,gstat,cr,ir,fr,ekind,state,state_global,mdatoms,nrnb,vcm,
944                             wcycle,enerd,NULL,NULL,NULL,NULL,mu_tot,
945                             constr,NULL,FALSE,state->box,
946                             top_global,&pcurr,top_global->natoms,&bSumEkinhOld,
947                             CGLO_RERUNMD | CGLO_GSTAT | CGLO_TEMPERATURE);
948         }
949         clear_mat(force_vir);
950         
951         /* Ionize the atoms if necessary */
952         if (bIonize)
953         {
954             ionize(fplog,oenv,mdatoms,top_global,t,ir,state->x,state->v,
955                    mdatoms->start,mdatoms->start+mdatoms->homenr,state->box,cr);
956         }
957         
958         /* Update force field in ffscan program */
959         if (bFFscan)
960         {
961             if (update_forcefield(fplog,
962                                   nfile,fnm,fr,
963                                   mdatoms->nr,state->x,state->box))
964             {
965                 gmx_finalize_par();
966
967                 exit(0);
968             }
969         }
970
971         /* We write a checkpoint at this MD step when:
972          * either at an NS step when we signalled through gs,
973          * or at the last step (but not when we do not want confout),
974          * but never at the first step or with rerun.
975          */
976         bCPT = (((gs.set[eglsCHKPT] && (bNS || ir->nstlist == 0)) ||
977                  (bLastStep && (Flags & MD_CONFOUT))) &&
978                 step > ir->init_step && !bRerunMD);
979         if (bCPT)
980         {
981             gs.set[eglsCHKPT] = 0;
982         }
983
984         /* Determine the energy and pressure:
985          * at nstcalcenergy steps and at energy output steps (set below).
986          */
987
988         if (EI_VV(ir->eI) && (!bInitStep)) {  /* for vv, the first half actually corresponds to the last step */
989             bNstEner = do_per_step(step-1,ir->nstcalcenergy);
990         } else {
991             bNstEner = do_per_step(step,ir->nstcalcenergy);
992         }
993         bCalcEnerPres =
994             (bNstEner ||
995              (ir->epc > epcNO && do_per_step(step,ir->nstpcouple)));
996
997         /* Do we need global communication ? */
998         bGStat = (bCalcEnerPres || bStopCM ||
999                   do_per_step(step,nstglobalcomm) ||
1000                   (ir->nstlist == -1 && !bRerunMD && step >= nlh.step_nscheck));
1001
1002         do_ene = (do_per_step(step,ir->nstenergy) || bLastStep);
1003
1004         if (do_ene || do_log)
1005         {
1006             bCalcEnerPres = TRUE;
1007             bGStat        = TRUE;
1008         }
1009         
1010         /* these CGLO_ options remain the same throughout the iteration */
1011         cglo_flags = ((bRerunMD ? CGLO_RERUNMD : 0) |
1012                       (bGStat ? CGLO_GSTAT : 0)
1013             );
1014         
1015         force_flags = (GMX_FORCE_STATECHANGED |
1016                        ((DYNAMIC_BOX(*ir) || bRerunMD) ? GMX_FORCE_DYNAMICBOX : 0) |
1017                        GMX_FORCE_ALLFORCES |
1018                        (bNStList ? GMX_FORCE_DOLR : 0) |
1019                        GMX_FORCE_SEPLRF |
1020                        (bCalcEnerPres ? GMX_FORCE_VIRIAL : 0) |
1021                        (bDoFEP ? GMX_FORCE_DHDL : 0)
1022             );
1023         
1024         if (shellfc)
1025         {
1026             /* Now is the time to relax the shells */
1027             count=relax_shell_flexcon(fplog,cr,bVerbose,bFFscan ? step+1 : step,
1028                                       ir,bNS,force_flags,
1029                                       bStopCM,top,top_global,
1030                                       constr,enerd,fcd,
1031                                       state,f,force_vir,mdatoms,
1032                                       nrnb,wcycle,graph,groups,
1033                                       shellfc,fr,bBornRadii,t,mu_tot,
1034                                       state->natoms,&bConverged,vsite,
1035                                       outf->fp_field);
1036             tcount+=count;
1037
1038             if (bConverged)
1039             {
1040                 nconverged++;
1041             }
1042         }
1043         else
1044         {
1045             /* The coordinates (x) are shifted (to get whole molecules)
1046              * in do_force.
1047              * This is parallellized as well, and does communication too. 
1048              * Check comments in sim_util.c
1049              */
1050             do_force(fplog,cr,ir,step,nrnb,wcycle,top,top_global,groups,
1051                      state->box,state->x,&state->hist,
1052                      f,force_vir,mdatoms,enerd,fcd,
1053                      state->lambda,graph,
1054                      fr,vsite,mu_tot,t,outf->fp_field,ed,bBornRadii,
1055                      (bNS ? GMX_FORCE_NS : 0) | force_flags);
1056         }
1057         
1058         if (bTCR)
1059         {
1060             mu_aver = calc_mu_aver(cr,state->x,mdatoms->chargeA,
1061                                    mu_tot,&top_global->mols,mdatoms,gnx,grpindex);
1062         }
1063         
1064         if (bTCR && bFirstStep)
1065         {
1066             tcr=init_coupling(fplog,nfile,fnm,cr,fr,mdatoms,&(top->idef));
1067             fprintf(fplog,"Done init_coupling\n"); 
1068             fflush(fplog);
1069         }
1070         
1071         if (bVV && !bStartingFromCpt && !bRerunMD)
1072         /*  ############### START FIRST UPDATE HALF-STEP FOR VV METHODS############### */
1073         {
1074             if (ir->eI==eiVV && bInitStep) 
1075             {
1076                 /* if using velocity verlet with full time step Ekin,
1077                  * take the first half step only to compute the 
1078                  * virial for the first step. From there,
1079                  * revert back to the initial coordinates
1080                  * so that the input is actually the initial step.
1081                  */
1082                 copy_rvecn(state->v,cbuf,0,state->natoms); /* should make this better for parallelizing? */
1083             } else {
1084                 /* this is for NHC in the Ekin(t+dt/2) version of vv */
1085                 trotter_update(ir,step,ekind,enerd,state,total_vir,mdatoms,&MassQ,trotter_seq,ettTSEQ1);            
1086             }
1087
1088             update_coords(fplog,step,ir,mdatoms,state,
1089                           f,fr->bTwinRange && bNStList,fr->f_twin,fcd,
1090                           ekind,M,wcycle,upd,bInitStep,etrtVELOCITY1,
1091                           cr,nrnb,constr,&top->idef);
1092             
1093             if (bIterations)
1094             {
1095                 gmx_iterate_init(&iterate,bIterations && !bInitStep);
1096             }
1097             /* for iterations, we save these vectors, as we will be self-consistently iterating
1098                the calculations */
1099
1100             /*#### UPDATE EXTENDED VARIABLES IN TROTTER FORMULATION */
1101             
1102             /* save the state */
1103             if (bIterations && iterate.bIterate) { 
1104                 copy_coupling_state(state,bufstate,ekind,ekind_save,&(ir->opts));
1105             }
1106             
1107             bFirstIterate = TRUE;
1108             while (bFirstIterate || (bIterations && iterate.bIterate))
1109             {
1110                 if (bIterations && iterate.bIterate) 
1111                 {
1112                     copy_coupling_state(bufstate,state,ekind_save,ekind,&(ir->opts));
1113                     if (bFirstIterate && bTrotter) 
1114                     {
1115                         /* The first time through, we need a decent first estimate
1116                            of veta(t+dt) to compute the constraints.  Do
1117                            this by computing the box volume part of the
1118                            trotter integration at this time. Nothing else
1119                            should be changed by this routine here.  If
1120                            !(first time), we start with the previous value
1121                            of veta.  */
1122                         
1123                         veta_save = state->veta;
1124                         trotter_update(ir,step,ekind,enerd,state,total_vir,mdatoms,&MassQ,trotter_seq,ettTSEQ0);
1125                         vetanew = state->veta;
1126                         state->veta = veta_save;
1127                     } 
1128                 } 
1129                 
1130                 bOK = TRUE;
1131                 if ( !bRerunMD || rerun_fr.bV || bForceUpdate) {  /* Why is rerun_fr.bV here?  Unclear. */
1132                     dvdl = 0;
1133                     
1134                     update_constraints(fplog,step,&dvdl,ir,ekind,mdatoms,state,graph,f,
1135                                        &top->idef,shake_vir,NULL,
1136                                        cr,nrnb,wcycle,upd,constr,
1137                                        bInitStep,TRUE,bCalcEnerPres,vetanew);
1138                     
1139                     if (!bOK && !bFFscan)
1140                     {
1141                         gmx_fatal(FARGS,"Constraint error: Shake, Lincs or Settle could not solve the constrains");
1142                     }
1143                     
1144                 } 
1145                 else if (graph)
1146                 { /* Need to unshift here if a do_force has been
1147                      called in the previous step */
1148                     unshift_self(graph,state->box,state->x);
1149                 }
1150
1151                 
1152                 /* if VV, compute the pressure and constraints */
1153                 /* For VV2, we strictly only need this if using pressure
1154                  * control, but we really would like to have accurate pressures
1155                  * printed out.
1156                  * Think about ways around this in the future?
1157                  * For now, keep this choice in comments.
1158                  */
1159                 /* bPres = (ir->eI==eiVV || IR_NPT_TROTTER(ir)); */
1160                 /*bTemp = ((ir->eI==eiVV &&(!bInitStep)) || (ir->eI==eiVVAK && IR_NPT_TROTTER(ir)));*/
1161                 bPres = TRUE;
1162                 bTemp = ((ir->eI==eiVV &&(!bInitStep)) || (ir->eI==eiVVAK));
1163                 if (bNstEner && ir->eI==eiVVAK)  /*MRS:  7/9/2010 -- this still doesn't fix it?*/
1164                 {
1165                     bSumEkinhOld = TRUE;
1166                 }
1167                 compute_globals(fplog,gstat,cr,ir,fr,ekind,state,state_global,mdatoms,nrnb,vcm,
1168                                 wcycle,enerd,force_vir,shake_vir,total_vir,pres,mu_tot,
1169                                 constr,NULL,FALSE,state->box,
1170                                 top_global,&pcurr,top_global->natoms,&bSumEkinhOld,
1171                                 cglo_flags 
1172                                 | CGLO_ENERGY 
1173                                 | (bStopCM ? CGLO_STOPCM : 0)
1174                                 | (bTemp ? CGLO_TEMPERATURE:0) 
1175                                 | (bPres ? CGLO_PRESSURE : 0) 
1176                                 | (bPres ? CGLO_CONSTRAINT : 0)
1177                                 | ((bIterations && iterate.bIterate) ? CGLO_ITERATE : 0)  
1178                                 | (bFirstIterate ? CGLO_FIRSTITERATE : 0)
1179                                 | CGLO_SCALEEKIN 
1180                     );
1181                 /* explanation of above: 
1182                    a) We compute Ekin at the full time step
1183                    if 1) we are using the AveVel Ekin, and it's not the
1184                    initial step, or 2) if we are using AveEkin, but need the full
1185                    time step kinetic energy for the pressure (always true now, since we want accurate statistics).
1186                    b) If we are using EkinAveEkin for the kinetic energy for the temperture control, we still feed in 
1187                    EkinAveVel because it's needed for the pressure */
1188                 
1189                 /* temperature scaling and pressure scaling to produce the extended variables at t+dt */
1190                 if (!bInitStep) 
1191                 {
1192                     if (bTrotter)
1193                     {
1194                         trotter_update(ir,step,ekind,enerd,state,total_vir,mdatoms,&MassQ,trotter_seq,ettTSEQ2);
1195                     } 
1196                     else 
1197                     {
1198                         update_tcouple(fplog,step,ir,state,ekind,wcycle,upd,&MassQ,mdatoms);
1199                     }
1200                 }
1201                 
1202                 if (bIterations &&
1203                     done_iterating(cr,fplog,step,&iterate,bFirstIterate,
1204                                    state->veta,&vetanew)) 
1205                 {
1206                     break;
1207                 }
1208                 bFirstIterate = FALSE;
1209             }
1210
1211             if (bTrotter && !bInitStep) {
1212                 enerd->term[F_DVDL_BONDED] += dvdl;        /* only add after iterations */
1213                 copy_mat(shake_vir,state->svir_prev);
1214                 copy_mat(force_vir,state->fvir_prev);
1215                 if (IR_NVT_TROTTER(ir) && ir->eI==eiVV) {
1216                     /* update temperature and kinetic energy now that step is over - this is the v(t+dt) point */
1217                     enerd->term[F_TEMP] = sum_ekin(&(ir->opts),ekind,NULL,(ir->eI==eiVV),FALSE,FALSE);
1218                     enerd->term[F_EKIN] = trace(ekind->ekin);
1219                 }
1220             }
1221             /* if it's the initial step, we performed this first step just to get the constraint virial */
1222             if (bInitStep && ir->eI==eiVV) {
1223                 copy_rvecn(cbuf,state->v,0,state->natoms);
1224             }
1225             
1226             if (fr->bSepDVDL && fplog && do_log) 
1227             {
1228                 fprintf(fplog,sepdvdlformat,"Constraint",0.0,dvdl);
1229             }
1230             enerd->term[F_DVDL_BONDED] += dvdl;
1231         }
1232
1233         /* MRS -- now done iterating -- compute the conserved quantity */
1234         if (bVV) {
1235             saved_conserved_quantity = compute_conserved_from_auxiliary(ir,state,&MassQ);
1236             if (ir->eI==eiVV) 
1237             {
1238                 last_ekin = enerd->term[F_EKIN];
1239             }
1240             if ((ir->eDispCorr != edispcEnerPres) && (ir->eDispCorr != edispcAllEnerPres)) 
1241             {
1242                 saved_conserved_quantity -= enerd->term[F_DISPCORR];
1243             }
1244             /* sum up the foreign energy and dhdl terms for vv.  currently done every step so that dhdl is correct in the .edr */
1245             sum_dhdl(enerd,state->lambda,ir->fepvals);
1246         }
1247         
1248         /* ########  END FIRST UPDATE STEP  ############## */
1249         /* ########  If doing VV, we now have v(dt) ###### */
1250         if (bDoExpanded) {
1251             /* perform extended ensemble sampling in lambda - we don't
1252                actually move to the new state before outputting
1253                statistics, but if performing simulated tempering, we
1254                do update the velocities and the tau_t. */
1255
1256             lamnew = ExpandedEnsembleDynamics(fplog,ir,enerd,state,&MassQ,&df_history,step,mcrng,state->v,mdatoms);
1257         }
1258         /* ################## START TRAJECTORY OUTPUT ################# */
1259         
1260         /* Now we have the energies and forces corresponding to the 
1261          * coordinates at time t. We must output all of this before
1262          * the update.
1263          * for RerunMD t is read from input trajectory
1264          */
1265         mdof_flags = 0;
1266         if (do_per_step(step,ir->nstxout)) { mdof_flags |= MDOF_X; }
1267         if (do_per_step(step,ir->nstvout)) { mdof_flags |= MDOF_V; }
1268         if (do_per_step(step,ir->nstfout)) { mdof_flags |= MDOF_F; }
1269         if (do_per_step(step,ir->nstxtcout)) { mdof_flags |= MDOF_XTC; }
1270         if (bCPT) { mdof_flags |= MDOF_CPT; };
1271
1272 #if defined(GMX_FAHCORE) || defined(GMX_WRITELASTSTEP)
1273         if (bLastStep)
1274         {
1275             /* Enforce writing positions and velocities at end of run */
1276             mdof_flags |= (MDOF_X | MDOF_V);
1277         }
1278 #endif
1279 #ifdef GMX_FAHCORE
1280         if (MASTER(cr))
1281             fcReportProgress( ir->nsteps, step );
1282
1283         /* sync bCPT and fc record-keeping */
1284         if (bCPT && MASTER(cr))
1285             fcRequestCheckPoint();
1286 #endif
1287         
1288         if (mdof_flags != 0)
1289         {
1290             wallcycle_start(wcycle,ewcTRAJ);
1291             if (bCPT)
1292             {
1293                 if (state->flags & (1<<estLD_RNG))
1294                 {
1295                     get_stochd_state(upd,state);
1296                 }
1297                 if (state->flags  & (1<<estMC_RNG))
1298                 {
1299                     get_mc_state(mcrng,state);
1300                 }
1301                 if (MASTER(cr))
1302                 {
1303                     if (bSumEkinhOld)
1304                     {
1305                         state_global->ekinstate.bUpToDate = FALSE;
1306                     }
1307                     else
1308                     {
1309                         update_ekinstate(&state_global->ekinstate,ekind);
1310                         state_global->ekinstate.bUpToDate = TRUE;
1311                     }
1312                     update_energyhistory(&state_global->enerhist,mdebin);
1313                     if (ir->efep!=efepNO || ir->bSimTemp) 
1314                     {
1315                         state_global->fep_state = state->fep_state; /* MRS: seems kludgy. The code should be
1316                                                                        structured so this isn't necessary.
1317                                                                        Note this reassignment is only necessary
1318                                                                        for single threads.*/
1319                         copy_df_history(&state_global->dfhist,&df_history);
1320                     }
1321                 }
1322             }
1323             write_traj(fplog,cr,outf,mdof_flags,top_global,
1324                        step,t,state,state_global,f,f_global,&n_xtc,&x_xtc);
1325             if (bCPT)
1326             {
1327                 nchkpt++;
1328                 bCPT = FALSE;
1329             }
1330             debug_gmx();
1331             if (bLastStep && step_rel == ir->nsteps &&
1332                 (Flags & MD_CONFOUT) && MASTER(cr) &&
1333                 !bRerunMD && !bFFscan)
1334             {
1335                 /* x and v have been collected in write_traj,
1336                  * because a checkpoint file will always be written
1337                  * at the last step.
1338                  */
1339                 fprintf(stderr,"\nWriting final coordinates.\n");
1340                 if (ir->ePBC != epbcNONE && !ir->bPeriodicMols &&
1341                     DOMAINDECOMP(cr))
1342                 {
1343                     /* Make molecules whole only for confout writing */
1344                     do_pbc_mtop(fplog,ir->ePBC,state->box,top_global,state_global->x);
1345                 }
1346                 write_sto_conf_mtop(ftp2fn(efSTO,nfile,fnm),
1347                                     *top_global->name,top_global,
1348                                     state_global->x,state_global->v,
1349                                     ir->ePBC,state->box);
1350                 debug_gmx();
1351             }
1352             wallcycle_stop(wcycle,ewcTRAJ);
1353         }
1354         
1355         /* kludge -- virial is lost with restart for NPT control. Must restart */
1356         if (bStartingFromCpt && bVV) 
1357         {
1358             copy_mat(state->svir_prev,shake_vir);
1359             copy_mat(state->fvir_prev,force_vir);
1360         }
1361         /*  ################## END TRAJECTORY OUTPUT ################ */
1362         
1363         /* Determine the pressure:
1364          * always when we want exact averages in the energy file,
1365          * at ns steps when we have pressure coupling,
1366          * otherwise only at energy output steps (set below).
1367          */
1368
1369         
1370         bNstEner = (bGStatEveryStep || do_per_step(step,ir->nstcalcenergy));
1371         bCalcEnerPres = bNstEner;
1372
1373         /* Do we need global communication ? */
1374         bGStat = (bGStatEveryStep || bStopCM || bNS ||
1375                   (ir->nstlist == -1 && !bRerunMD && step >= nlh.step_nscheck));
1376
1377         do_ene = (do_per_step(step,ir->nstenergy) || bLastStep);
1378
1379         if (do_ene || do_log)
1380         {
1381             bCalcEnerPres = TRUE;
1382             bGStat        = TRUE;
1383         }
1384
1385         /* Determine the wallclock run time up till now */
1386         run_time = gmx_gettime() - (double)runtime->real;
1387         /* Check whether everything is still allright */    
1388         if (((int)gmx_get_stop_condition() > handled_stop_condition)
1389 #ifdef GMX_THREAD_MPI
1390             && MASTER(cr)
1391 #endif
1392             )
1393         {
1394             /* this is just make gs.sig compatible with the hack 
1395                of sending signals around by MPI_Reduce with together with
1396                other floats */
1397             if ( gmx_get_stop_condition() == gmx_stop_cond_next_ns )
1398                 gs.sig[eglsSTOPCOND]=1;
1399             if ( gmx_get_stop_condition() == gmx_stop_cond_next )
1400                 gs.sig[eglsSTOPCOND]=-1;
1401             /* < 0 means stop at next step, > 0 means stop at next NS step */
1402             if (fplog)
1403             {
1404                 fprintf(fplog,
1405                         "\n\nReceived the %s signal, stopping at the next %sstep\n\n",
1406                         gmx_get_signal_name(),
1407                         gs.sig[eglsSTOPCOND]==1 ? "NS " : "");
1408                 fflush(fplog);
1409             }
1410             fprintf(stderr,
1411                     "\n\nReceived the %s signal, stopping at the next %sstep\n\n",
1412                     gmx_get_signal_name(),
1413                     gs.sig[eglsSTOPCOND]==1 ? "NS " : "");
1414             fflush(stderr);
1415             handled_stop_condition=(int)gmx_get_stop_condition();
1416         }
1417         else if (MASTER(cr) && (bNS || ir->nstlist <= 0) &&
1418                  (max_hours > 0 && run_time > max_hours*60.0*60.0*0.99) &&
1419                  gs.sig[eglsSTOPCOND] == 0 && gs.set[eglsSTOPCOND] == 0)
1420         {
1421             /* Signal to terminate the run */
1422             gs.sig[eglsSTOPCOND] = 1;
1423             if (fplog)
1424             {
1425                 fprintf(fplog,"\nStep %s: Run time exceeded %.3f hours, will terminate the run\n",gmx_step_str(step,sbuf),max_hours*0.99);
1426             }
1427             fprintf(stderr, "\nStep %s: Run time exceeded %.3f hours, will terminate the run\n",gmx_step_str(step,sbuf),max_hours*0.99);
1428         }
1429
1430         if (bResetCountersHalfMaxH && MASTER(cr) &&
1431             run_time > max_hours*60.0*60.0*0.495)
1432         {
1433             gs.sig[eglsRESETCOUNTERS] = 1;
1434         }
1435
1436         if (ir->nstlist == -1 && !bRerunMD)
1437         {
1438             /* When bGStatEveryStep=FALSE, global_stat is only called
1439              * when we check the atom displacements, not at NS steps.
1440              * This means that also the bonded interaction count check is not
1441              * performed immediately after NS. Therefore a few MD steps could
1442              * be performed with missing interactions.
1443              * But wrong energies are never written to file,
1444              * since energies are only written after global_stat
1445              * has been called.
1446              */
1447             if (step >= nlh.step_nscheck)
1448             {
1449                 nlh.nabnsb = natoms_beyond_ns_buffer(ir,fr,&top->cgs,
1450                                                      nlh.scale_tot,state->x);
1451             }
1452             else
1453             {
1454                 /* This is not necessarily true,
1455                  * but step_nscheck is determined quite conservatively.
1456                  */
1457                 nlh.nabnsb = 0;
1458             }
1459         }
1460
1461         /* In parallel we only have to check for checkpointing in steps
1462          * where we do global communication,
1463          *  otherwise the other nodes don't know.
1464          */
1465         if (MASTER(cr) && ((bGStat || !PAR(cr)) &&
1466                            cpt_period >= 0 &&
1467                            (cpt_period == 0 || 
1468                             run_time >= nchkpt*cpt_period*60.0)) &&
1469             gs.set[eglsCHKPT] == 0)
1470         {
1471             gs.sig[eglsCHKPT] = 1;
1472         }
1473
1474
1475         /* at the start of step, randomize the velocities */
1476         if (ETC_ANDERSEN(ir->etc) && EI_VV(ir->eI))
1477         {
1478             gmx_bool bDoAndersenConstr;
1479             bDoAndersenConstr = update_randomize_velocities(ir,step,mdatoms,state,upd,&top->idef,constr);
1480             /* if we have constraints, we have to remove the kinetic energy parallel to the bonds */
1481             if (bDoAndersenConstr)
1482             {
1483                 update_constraints(fplog,step,&dvdl,ir,ekind,mdatoms,state,graph,f,
1484                                    &top->idef,tmp_vir,NULL,
1485                                    cr,nrnb,wcycle,upd,constr,
1486                                    bInitStep,TRUE,FALSE,vetanew);
1487             }
1488         }
1489
1490         if (bIterations)
1491         {
1492             gmx_iterate_init(&iterate,bIterations);
1493         }
1494     
1495         /* for iterations, we save these vectors, as we will be redoing the calculations */
1496         if (bIterations && iterate.bIterate) 
1497         {
1498             copy_coupling_state(state,bufstate,ekind,ekind_save,&(ir->opts));
1499         }
1500         bFirstIterate = TRUE;
1501         while (bFirstIterate || (bIterations && iterate.bIterate))
1502         {
1503             /* We now restore these vectors to redo the calculation with improved extended variables */    
1504             if (bIterations) 
1505             { 
1506                 copy_coupling_state(bufstate,state,ekind_save,ekind,&(ir->opts));
1507             }
1508
1509             /* We make the decision to break or not -after- the calculation of Ekin and Pressure,
1510                so scroll down for that logic */
1511             
1512             /* #########   START SECOND UPDATE STEP ################# */
1513             /* Box is changed in update() when we do pressure coupling,
1514              * but we should still use the old box for energy corrections and when
1515              * writing it to the energy file, so it matches the trajectory files for
1516              * the same timestep above. Make a copy in a separate array.
1517              */
1518             copy_mat(state->box,lastbox);
1519
1520             bOK = TRUE;
1521             if (!(bRerunMD && !rerun_fr.bV && !bForceUpdate))
1522             {
1523                 wallcycle_start(wcycle,ewcUPDATE);
1524                 dvdl = 0;
1525                 /* UPDATE PRESSURE VARIABLES IN TROTTER FORMULATION WITH CONSTRAINTS */
1526                 if (bTrotter) 
1527                 {
1528                     if (bIterations && iterate.bIterate) 
1529                     {
1530                         if (bFirstIterate) 
1531                         {
1532                             scalevir = 1;
1533                         }
1534                         else 
1535                         {
1536                             /* we use a new value of scalevir to converge the iterations faster */
1537                             scalevir = tracevir/trace(shake_vir);
1538                         }
1539                         msmul(shake_vir,scalevir,shake_vir); 
1540                         m_add(force_vir,shake_vir,total_vir);
1541                         clear_mat(shake_vir);
1542                     }
1543                     trotter_update(ir,step,ekind,enerd,state,total_vir,mdatoms,&MassQ,trotter_seq,ettTSEQ3);
1544                 /* We can only do Berendsen coupling after we have summed
1545                  * the kinetic energy or virial. Since the happens
1546                  * in global_state after update, we should only do it at
1547                  * step % nstlist = 1 with bGStatEveryStep=FALSE.
1548                  */
1549                 }
1550                 else 
1551                 {
1552                     update_tcouple(fplog,step,ir,state,ekind,wcycle,upd,&MassQ,mdatoms);
1553                     update_pcouple(fplog,step,ir,state,pcoupl_mu,M,wcycle,
1554                                    upd,bInitStep);
1555                 }
1556
1557                 if (bVV)
1558                 {
1559                     /* velocity half-step update */
1560                     update_coords(fplog,step,ir,mdatoms,state,f,
1561                                   fr->bTwinRange && bNStList,fr->f_twin,fcd,
1562                                   ekind,M,wcycle,upd,FALSE,etrtVELOCITY2,
1563                                   cr,nrnb,constr,&top->idef);
1564                 }
1565
1566                 /* Above, initialize just copies ekinh into ekin,
1567                  * it doesn't copy position (for VV),
1568                  * and entire integrator for MD.
1569                  */
1570                 
1571                 if (ir->eI==eiVVAK) 
1572                 {
1573                     copy_rvecn(state->x,cbuf,0,state->natoms);
1574                 }
1575                 
1576                 update_coords(fplog,step,ir,mdatoms,state,f,fr->bTwinRange && bNStList,fr->f_twin,fcd,
1577                               ekind,M,wcycle,upd,bInitStep,etrtPOSITION,cr,nrnb,constr,&top->idef);
1578                 wallcycle_stop(wcycle,ewcUPDATE);
1579
1580                 update_constraints(fplog,step,&dvdl,ir,ekind,mdatoms,state,graph,f,
1581                                    &top->idef,shake_vir,force_vir,
1582                                    cr,nrnb,wcycle,upd,constr,
1583                                    bInitStep,FALSE,bCalcEnerPres,state->veta);  
1584                 
1585                 if (ir->eI==eiVVAK)
1586                 {
1587                     /* erase F_EKIN and F_TEMP here? */
1588                     /* just compute the kinetic energy at the half step to perform a trotter step */
1589                     compute_globals(fplog,gstat,cr,ir,fr,ekind,state,state_global,mdatoms,nrnb,vcm,
1590                                     wcycle,enerd,force_vir,shake_vir,total_vir,pres,mu_tot,
1591                                     constr,NULL,FALSE,lastbox,
1592                                     top_global,&pcurr,top_global->natoms,&bSumEkinhOld,
1593                                     cglo_flags | CGLO_TEMPERATURE
1594                         );
1595                     wallcycle_start(wcycle,ewcUPDATE);
1596                     trotter_update(ir,step,ekind,enerd,state,total_vir,mdatoms,&MassQ,trotter_seq,ettTSEQ4);            
1597                     /* now we know the scaling, we can compute the positions again again */
1598                     copy_rvecn(cbuf,state->x,0,state->natoms);
1599
1600                     update_coords(fplog,step,ir,mdatoms,state,f,fr->bTwinRange && bNStList,fr->f_twin,fcd,
1601                                   ekind,M,wcycle,upd,bInitStep,etrtPOSITION,cr,nrnb,constr,&top->idef);
1602                     wallcycle_stop(wcycle,ewcUPDATE);
1603
1604                     /* do we need an extra constraint here? just need to copy out of state->v to upd->xp? */
1605                     /* are the small terms in the shake_vir here due
1606                      * to numerical errors, or are they important
1607                      * physically? I'm thinking they are just errors, but not completely sure. 
1608                      * For now, will call without actually constraining, constr=NULL*/
1609                     update_constraints(fplog,step,&dvdl,ir,ekind,mdatoms,state,graph,f,
1610                                        &top->idef,tmp_vir,force_vir,
1611                                        cr,nrnb,wcycle,upd,NULL,
1612                                        bInitStep,FALSE,bCalcEnerPres,
1613                                        state->veta);  
1614                 }
1615                 if (!bOK && !bFFscan) 
1616                 {
1617                     gmx_fatal(FARGS,"Constraint error: Shake, Lincs or Settle could not solve the constrains");
1618                 }
1619                 
1620                 if (fr->bSepDVDL && fplog && do_log) 
1621                 {
1622                     fprintf(fplog,sepdvdlformat,"Constraint dV/dl",0.0,dvdl);
1623                 }
1624                 enerd->term[F_DVDL_BONDED] += dvdl;
1625             } 
1626             else if (graph) 
1627             {
1628                 /* Need to unshift here */
1629                 unshift_self(graph,state->box,state->x);
1630             }
1631
1632             if (vsite != NULL) 
1633             {
1634                 wallcycle_start(wcycle,ewcVSITECONSTR);
1635                 if (graph != NULL) 
1636                 {
1637                     shift_self(graph,state->box,state->x);
1638                 }
1639                 construct_vsites(fplog,vsite,state->x,nrnb,ir->delta_t,state->v,
1640                                  top->idef.iparams,top->idef.il,
1641                                  fr->ePBC,fr->bMolPBC,graph,cr,state->box);
1642                 
1643                 if (graph != NULL) 
1644                 {
1645                     unshift_self(graph,state->box,state->x);
1646                 }
1647                 wallcycle_stop(wcycle,ewcVSITECONSTR);
1648             }
1649             
1650             /* ############## IF NOT VV, Calculate globals HERE, also iterate constraints ############ */
1651             if (ir->nstlist == -1 && bFirstIterate)
1652             {
1653                 gs.sig[eglsNABNSB] = nlh.nabnsb;
1654             }
1655             bEnergyHere = (!EI_VV(ir->eI) || (EI_VV(ir->eI) && bRerunMD)); /* this is not quite working for vv and rerun! fails for running rerun on multiple threads. This is caught in runner.c. */
1656             compute_globals(fplog,gstat,cr,ir,fr,ekind,state,state_global,mdatoms,nrnb,vcm,
1657                             wcycle,enerd,force_vir,shake_vir,total_vir,pres,mu_tot,
1658                             constr,
1659                             bFirstIterate ? &gs : NULL, 
1660                             (step_rel % gs.nstms == 0) && 
1661                                 (multisim_nsteps<0 || (step_rel<multisim_nsteps)),
1662                             lastbox,
1663                             top_global,&pcurr,top_global->natoms,&bSumEkinhOld,
1664                             cglo_flags 
1665                             | (!EI_VV(ir->eI) ? CGLO_ENERGY : 0) 
1666                             | (!EI_VV(ir->eI) && bStopCM ? CGLO_STOPCM : 0)
1667                             | (!EI_VV(ir->eI) ? CGLO_TEMPERATURE : 0) 
1668                             | (bEnergyHere || bRerunMD ? CGLO_PRESSURE : 0) 
1669                             | (bIterations && iterate.bIterate ? CGLO_ITERATE : 0) 
1670                             | (bFirstIterate ? CGLO_FIRSTITERATE : 0)
1671                             | CGLO_CONSTRAINT
1672                 );
1673             if (ir->nstlist == -1 && bFirstIterate)
1674             {
1675                 nlh.nabnsb = gs.set[eglsNABNSB];
1676                 gs.set[eglsNABNSB] = 0;
1677             }
1678             /* bIterate is set to keep it from eliminating the old ekin kinetic energy terms */
1679             /* #############  END CALC EKIN AND PRESSURE ################# */
1680         
1681             /* Note: this is OK, but there are some numerical precision issues with using the convergence of
1682                the virial that should probably be addressed eventually. state->veta has better properies,
1683                but what we actually need entering the new cycle is the new shake_vir value. Ideally, we could
1684                generate the new shake_vir, but test the veta value for convergence.  This will take some thought. */
1685
1686             if (bIterations && 
1687                 done_iterating(cr,fplog,step,&iterate,bFirstIterate,
1688                                trace(shake_vir),&tracevir)) 
1689             {
1690                 break;
1691             }
1692             bFirstIterate = FALSE;
1693         }
1694
1695         /* only add constraint dvdl after constraints */
1696         enerd->term[F_DVDL_BONDED] += dvdl;
1697         if (!bVV)
1698         {
1699             /* sum up the foreign energy and dhdl terms for md and sd. currently done every step so that dhdl is correct in the .edr */
1700             sum_dhdl(enerd,state->lambda,ir->fepvals);
1701         }
1702         update_box(fplog,step,ir,mdatoms,state,graph,f,
1703                    ir->nstlist==-1 ? &nlh.scale_tot : NULL,pcoupl_mu,nrnb,wcycle,upd,bInitStep,FALSE);
1704         
1705         /* ################# END UPDATE STEP 2 ################# */
1706         /* #### We now have r(t+dt) and v(t+dt/2)  ############# */
1707     
1708         /* The coordinates (x) were unshifted in update */
1709         if (bFFscan && (shellfc==NULL || bConverged))
1710         {
1711             if (print_forcefield(fplog,enerd->term,mdatoms->homenr,
1712                                  f,NULL,xcopy,
1713                                  &(top_global->mols),mdatoms->massT,pres))
1714             {
1715                 gmx_finalize_par();
1716
1717                 fprintf(stderr,"\n");
1718                 exit(0);
1719             }
1720         }
1721         if (!bGStat)
1722         {
1723             /* We will not sum ekinh_old,                                                            
1724              * so signal that we still have to do it.                                                
1725              */
1726             bSumEkinhOld = TRUE;
1727         }
1728         
1729         if (bTCR)
1730         {
1731             /* Only do GCT when the relaxation of shells (minimization) has converged,
1732              * otherwise we might be coupling to bogus energies. 
1733              * In parallel we must always do this, because the other sims might
1734              * update the FF.
1735              */
1736
1737             /* Since this is called with the new coordinates state->x, I assume
1738              * we want the new box state->box too. / EL 20040121
1739              */
1740             do_coupling(fplog,oenv,nfile,fnm,tcr,t,step,enerd->term,fr,
1741                         ir,MASTER(cr),
1742                         mdatoms,&(top->idef),mu_aver,
1743                         top_global->mols.nr,cr,
1744                         state->box,total_vir,pres,
1745                         mu_tot,state->x,f,bConverged);
1746             debug_gmx();
1747         }
1748
1749         /* #########  BEGIN PREPARING EDR OUTPUT  ###########  */
1750
1751         /* use the directly determined last velocity, not actually the averaged half steps */
1752         if (bTrotter && ir->eI==eiVV) 
1753         {
1754             enerd->term[F_EKIN] = last_ekin;
1755         }
1756         enerd->term[F_ETOT] = enerd->term[F_EPOT] + enerd->term[F_EKIN];
1757         
1758         if (bVV)
1759         {
1760             enerd->term[F_ECONSERVED] = enerd->term[F_ETOT] + saved_conserved_quantity;
1761         }
1762         else 
1763         {
1764             enerd->term[F_ECONSERVED] = enerd->term[F_ETOT] + compute_conserved_from_auxiliary(ir,state,&MassQ);
1765         }
1766         /* Check for excessively large energies */
1767         if (bIonize) 
1768         {
1769 #ifdef GMX_DOUBLE
1770             real etot_max = 1e200;
1771 #else
1772             real etot_max = 1e30;
1773 #endif
1774             if (fabs(enerd->term[F_ETOT]) > etot_max) 
1775             {
1776                 fprintf(stderr,"Energy too large (%g), giving up\n",
1777                         enerd->term[F_ETOT]);
1778             }
1779         }
1780         /* #########  END PREPARING EDR OUTPUT  ###########  */
1781         
1782         /* Time for performance */
1783         if (((step % stepout) == 0) || bLastStep) 
1784         {
1785             runtime_upd_proc(runtime);
1786         }
1787         
1788         /* Output stuff */
1789         if (MASTER(cr))
1790         {
1791             gmx_bool do_dr,do_or;
1792             
1793             if (fplog && do_log && bDoExpanded)
1794             {
1795                 /* only needed if doing expanded ensemble */
1796                 PrintFreeEnergyInfoToFile(fplog,ir->fepvals,ir->expandedvals,ir->bSimTemp?ir->simtempvals:NULL,
1797                                           &df_history,state->fep_state,ir->nstlog,step);
1798             }
1799             if (!(bStartingFromCpt && (EI_VV(ir->eI)))) 
1800             {
1801                 if (bNstEner)
1802                 {
1803                     upd_mdebin(mdebin,bDoDHDL,TRUE,
1804                                t,mdatoms->tmass,enerd,state,
1805                                ir->fepvals,ir->expandedvals,lastbox,
1806                                shake_vir,force_vir,total_vir,pres,
1807                                ekind,mu_tot,constr);
1808                 }
1809                 else
1810                 {
1811                     upd_mdebin_step(mdebin);
1812                 }
1813                 
1814                 do_dr  = do_per_step(step,ir->nstdisreout);
1815                 do_or  = do_per_step(step,ir->nstorireout);
1816                 
1817                 print_ebin(outf->fp_ene,do_ene,do_dr,do_or,do_log?fplog:NULL,
1818                            step,t,
1819                            eprNORMAL,bCompact,mdebin,fcd,groups,&(ir->opts));
1820             }
1821             if (ir->ePull != epullNO)
1822             {
1823                 pull_print_output(ir->pull,step,t);
1824             }
1825             
1826             if (do_per_step(step,ir->nstlog))
1827             {
1828                 if(fflush(fplog) != 0)
1829                 {
1830                     gmx_fatal(FARGS,"Cannot flush logfile - maybe you are out of disk space?");
1831                 }
1832             }
1833         }
1834         if (bDoExpanded)
1835         {
1836             /* Have to do this part after outputting the logfile and the edr file */
1837             state->fep_state = lamnew;
1838             for (i=0;i<efptNR;i++)
1839             {
1840                 state->lambda[i] = ir->fepvals->all_lambda[i][lamnew];
1841             }
1842         }
1843         /* Remaining runtime */
1844         if (MULTIMASTER(cr) && (do_verbose || gmx_got_usr_signal() ))
1845         {
1846             if (shellfc) 
1847             {
1848                 fprintf(stderr,"\n");
1849             }
1850             print_time(stderr,runtime,step,ir,cr);
1851         }
1852
1853         /* Replica exchange */
1854         bExchanged = FALSE;
1855         if ((repl_ex_nst > 0) && (step > 0) && !bLastStep &&
1856             do_per_step(step,repl_ex_nst)) 
1857         {
1858             bExchanged = replica_exchange(fplog,cr,repl_ex,
1859                                           state_global,enerd,
1860                                           state,step,t);
1861
1862             if (bExchanged && DOMAINDECOMP(cr)) 
1863             {
1864                 dd_partition_system(fplog,step,cr,TRUE,1,
1865                                     state_global,top_global,ir,
1866                                     state,&f,mdatoms,top,fr,
1867                                     vsite,shellfc,constr,
1868                                     nrnb,wcycle,FALSE);
1869             }
1870         }
1871         
1872         bFirstStep = FALSE;
1873         bInitStep = FALSE;
1874         bStartingFromCpt = FALSE;
1875
1876         /* #######  SET VARIABLES FOR NEXT ITERATION IF THEY STILL NEED IT ###### */
1877         /* With all integrators, except VV, we need to retain the pressure
1878          * at the current step for coupling at the next step.
1879          */
1880         if ((state->flags & (1<<estPRES_PREV)) &&
1881             (bGStatEveryStep ||
1882              (ir->nstpcouple > 0 && step % ir->nstpcouple == 0)))
1883         {
1884             /* Store the pressure in t_state for pressure coupling
1885              * at the next MD step.
1886              */
1887             copy_mat(pres,state->pres_prev);
1888         }
1889         
1890         /* #######  END SET VARIABLES FOR NEXT ITERATION ###### */
1891
1892         if ( (membed!=NULL) && (!bLastStep) )
1893         {
1894             rescale_membed(step_rel,membed,state_global->x);
1895         }
1896
1897         if (bRerunMD) 
1898         {
1899             if (MASTER(cr))
1900             {
1901                 /* read next frame from input trajectory */
1902                 bNotLastFrame = read_next_frame(oenv,status,&rerun_fr);
1903             }
1904
1905             if (PAR(cr))
1906             {
1907                 rerun_parallel_comm(cr,&rerun_fr,&bNotLastFrame);
1908             }
1909         }
1910         
1911         if (!bRerunMD || !rerun_fr.bStep)
1912         {
1913             /* increase the MD step number */
1914             step++;
1915             step_rel++;
1916         }
1917         
1918         cycles = wallcycle_stop(wcycle,ewcSTEP);
1919         if (DOMAINDECOMP(cr) && wcycle)
1920         {
1921             dd_cycles_add(cr->dd,cycles,ddCyclStep);
1922         }
1923         
1924         if (step_rel == wcycle_get_reset_counters(wcycle) ||
1925             gs.set[eglsRESETCOUNTERS] != 0)
1926         {
1927             /* Reset all the counters related to performance over the run */
1928             reset_all_counters(fplog,cr,step,&step_rel,ir,wcycle,nrnb,runtime);
1929             wcycle_set_reset_counters(wcycle,-1);
1930             /* Correct max_hours for the elapsed time */
1931             max_hours -= run_time/(60.0*60.0);
1932             bResetCountersHalfMaxH = FALSE;
1933             gs.set[eglsRESETCOUNTERS] = 0;
1934         }
1935
1936     }
1937     /* End of main MD loop */
1938     debug_gmx();
1939     
1940     /* Stop the time */
1941     runtime_end(runtime);
1942     
1943     if (bRerunMD && MASTER(cr))
1944     {
1945         close_trj(status);
1946     }
1947     
1948     if (!(cr->duty & DUTY_PME))
1949     {
1950         /* Tell the PME only node to finish */
1951         gmx_pme_finish(cr);
1952     }
1953     
1954     if (MASTER(cr))
1955     {
1956         if (ir->nstcalcenergy > 0 && !bRerunMD) 
1957         {
1958             print_ebin(outf->fp_ene,FALSE,FALSE,FALSE,fplog,step,t,
1959                        eprAVER,FALSE,mdebin,fcd,groups,&(ir->opts));
1960         }
1961     }
1962
1963     done_mdoutf(outf);
1964
1965     debug_gmx();
1966
1967     if (ir->nstlist == -1 && nlh.nns > 0 && fplog)
1968     {
1969         fprintf(fplog,"Average neighborlist lifetime: %.1f steps, std.dev.: %.1f steps\n",nlh.s1/nlh.nns,sqrt(nlh.s2/nlh.nns - sqr(nlh.s1/nlh.nns)));
1970         fprintf(fplog,"Average number of atoms that crossed the half buffer length: %.1f\n\n",nlh.ab/nlh.nns);
1971     }
1972     
1973     if (shellfc && fplog)
1974     {
1975         fprintf(fplog,"Fraction of iterations that converged:           %.2f %%\n",
1976                 (nconverged*100.0)/step_rel);
1977         fprintf(fplog,"Average number of force evaluations per MD step: %.2f\n\n",
1978                 tcount/step_rel);
1979     }
1980     
1981     if (repl_ex_nst > 0 && MASTER(cr))
1982     {
1983         print_replica_exchange_statistics(fplog,repl_ex);
1984     }
1985     
1986     runtime->nsteps_done = step_rel;
1987     
1988     return 0;
1989 }