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