1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
31 * For more info, check our website at http://www.gromacs.org
34 * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
43 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
80 #include "mpelogging.h"
87 #include "compute_io.h"
89 #include "checkpoint.h"
90 #include "mtop_util.h"
91 #include "sighandler.h"
102 #include "corewrap.h"
106 /* simulation conditions to transmit. Keep in mind that they are
107 transmitted to other nodes through an MPI_Reduce after
108 casting them to a real (so the signals can be sent together with other
109 data). This means that the only meaningful values are positive,
111 enum { eglsNABNSB
, eglsCHKPT
, eglsSTOPCOND
, eglsRESETCOUNTERS
, eglsNR
};
112 /* Is the signal in one simulation independent of other simulations? */
113 gmx_bool gs_simlocal
[eglsNR
] = { TRUE
, FALSE
, FALSE
, TRUE
};
116 int nstms
; /* The frequency for intersimulation communication */
117 int sig
[eglsNR
]; /* The signal set by one process in do_md */
118 int set
[eglsNR
]; /* The communicated signal, equal for all processes */
123 /* check which of the multisim simulations has the shortest number of
124 steps and return that number of nsteps */
125 static gmx_large_int_t
get_multisim_nsteps(const t_commrec
*cr
,
126 gmx_large_int_t nsteps
)
128 gmx_large_int_t steps_out
;
132 gmx_large_int_t
*buf
;
135 snew(buf
,cr
->ms
->nsim
);
137 buf
[cr
->ms
->sim
] = nsteps
;
138 gmx_sumli_sim(cr
->ms
->nsim
, buf
, cr
->ms
);
141 for(s
=0; s
<cr
->ms
->nsim
; s
++)
143 /* find the smallest positive number */
144 if (buf
[s
]>= 0 && ((steps_out
< 0) || (buf
[s
]<steps_out
)) )
151 /* if we're the limiting simulation, don't do anything */
152 if (steps_out
>=0 && steps_out
<nsteps
)
155 snprintf(strbuf
, 255, "Will stop simulation %%d after %s steps (another simulation will end then).\n", gmx_large_int_pfmt
);
156 fprintf(stderr
, strbuf
, cr
->ms
->sim
, steps_out
);
159 /* broadcast to non-masters */
160 gmx_bcast(sizeof(gmx_large_int_t
), &steps_out
, cr
);
164 static int multisim_min(const gmx_multisim_t
*ms
,int nmin
,int n
)
167 gmx_bool bPos
,bEqual
;
172 gmx_sumi_sim(ms
->nsim
,buf
,ms
);
175 for(s
=0; s
<ms
->nsim
; s
++)
177 bPos
= bPos
&& (buf
[s
] > 0);
178 bEqual
= bEqual
&& (buf
[s
] == buf
[0]);
184 nmin
= min(nmin
,buf
[0]);
188 /* Find the least common multiple */
189 for(d
=2; d
<nmin
; d
++)
192 while (s
< ms
->nsim
&& d
% buf
[s
] == 0)
198 /* We found the LCM and it is less than nmin */
210 static int multisim_nstsimsync(const t_commrec
*cr
,
211 const t_inputrec
*ir
,int repl_ex_nst
)
218 nmin
= multisim_min(cr
->ms
,nmin
,ir
->nstlist
);
219 nmin
= multisim_min(cr
->ms
,nmin
,ir
->nstcalcenergy
);
220 nmin
= multisim_min(cr
->ms
,nmin
,repl_ex_nst
);
223 gmx_fatal(FARGS
,"Can not find an appropriate interval for inter-simulation communication, since nstlist, nstcalcenergy and -replex are all <= 0");
225 /* Avoid inter-simulation communication at every (second) step */
232 gmx_bcast(sizeof(int),&nmin
,cr
);
237 static void init_global_signals(globsig_t
*gs
,const t_commrec
*cr
,
238 const t_inputrec
*ir
,int repl_ex_nst
)
244 gs
->nstms
= multisim_nstsimsync(cr
,ir
,repl_ex_nst
);
247 fprintf(debug
,"Syncing simulations for checkpointing and termination every %d steps\n",gs
->nstms
);
255 for(i
=0; i
<eglsNR
; i
++)
262 static void copy_coupling_state(t_state
*statea
,t_state
*stateb
,
263 gmx_ekindata_t
*ekinda
,gmx_ekindata_t
*ekindb
, t_grpopts
* opts
)
266 /* MRS note -- might be able to get rid of some of the arguments. Look over it when it's all debugged */
270 /* Make sure we have enough space for x and v */
271 if (statea
->nalloc
> stateb
->nalloc
)
273 stateb
->nalloc
= statea
->nalloc
;
274 srenew(stateb
->x
,stateb
->nalloc
);
275 srenew(stateb
->v
,stateb
->nalloc
);
278 stateb
->natoms
= statea
->natoms
;
279 stateb
->ngtc
= statea
->ngtc
;
280 stateb
->nnhpres
= statea
->nnhpres
;
281 stateb
->veta
= statea
->veta
;
284 copy_mat(ekinda
->ekin
,ekindb
->ekin
);
285 for (i
=0; i
<stateb
->ngtc
; i
++)
287 ekindb
->tcstat
[i
].T
= ekinda
->tcstat
[i
].T
;
288 ekindb
->tcstat
[i
].Th
= ekinda
->tcstat
[i
].Th
;
289 copy_mat(ekinda
->tcstat
[i
].ekinh
,ekindb
->tcstat
[i
].ekinh
);
290 copy_mat(ekinda
->tcstat
[i
].ekinf
,ekindb
->tcstat
[i
].ekinf
);
291 ekindb
->tcstat
[i
].ekinscalef_nhc
= ekinda
->tcstat
[i
].ekinscalef_nhc
;
292 ekindb
->tcstat
[i
].ekinscaleh_nhc
= ekinda
->tcstat
[i
].ekinscaleh_nhc
;
293 ekindb
->tcstat
[i
].vscale_nhc
= ekinda
->tcstat
[i
].vscale_nhc
;
296 copy_rvecn(statea
->x
,stateb
->x
,0,stateb
->natoms
);
297 copy_rvecn(statea
->v
,stateb
->v
,0,stateb
->natoms
);
298 copy_mat(statea
->box
,stateb
->box
);
299 copy_mat(statea
->box_rel
,stateb
->box_rel
);
300 copy_mat(statea
->boxv
,stateb
->boxv
);
302 for (i
= 0; i
<stateb
->ngtc
; i
++)
304 nc
= i
*opts
->nhchainlength
;
305 for (j
=0; j
<opts
->nhchainlength
; j
++)
307 stateb
->nosehoover_xi
[nc
+j
] = statea
->nosehoover_xi
[nc
+j
];
308 stateb
->nosehoover_vxi
[nc
+j
] = statea
->nosehoover_vxi
[nc
+j
];
311 if (stateb
->nhpres_xi
!= NULL
)
313 for (i
= 0; i
<stateb
->nnhpres
; i
++)
315 nc
= i
*opts
->nhchainlength
;
316 for (j
=0; j
<opts
->nhchainlength
; j
++)
318 stateb
->nhpres_xi
[nc
+j
] = statea
->nhpres_xi
[nc
+j
];
319 stateb
->nhpres_vxi
[nc
+j
] = statea
->nhpres_vxi
[nc
+j
];
325 static real
compute_conserved_from_auxiliary(t_inputrec
*ir
, t_state
*state
, t_extmass
*MassQ
)
335 quantity
= NPT_energy(ir
,state
,MassQ
);
338 quantity
= vrescale_energy(&(ir
->opts
),state
->therm_integral
);
346 static void compute_globals(FILE *fplog
, gmx_global_stat_t gstat
, t_commrec
*cr
, t_inputrec
*ir
,
347 t_forcerec
*fr
, gmx_ekindata_t
*ekind
,
348 t_state
*state
, t_state
*state_global
, t_mdatoms
*mdatoms
,
349 t_nrnb
*nrnb
, t_vcm
*vcm
, gmx_wallcycle_t wcycle
,
350 gmx_enerdata_t
*enerd
,tensor force_vir
, tensor shake_vir
, tensor total_vir
,
351 tensor pres
, rvec mu_tot
, gmx_constr_t constr
,
352 globsig_t
*gs
,gmx_bool bInterSimGS
,
353 matrix box
, gmx_mtop_t
*top_global
, real
*pcurr
,
354 int natoms
, gmx_bool
*bSumEkinhOld
, int flags
)
358 tensor corr_vir
,corr_pres
,shakeall_vir
;
359 gmx_bool bEner
,bPres
,bTemp
, bVV
;
360 gmx_bool bRerunMD
, bStopCM
, bGStat
, bIterate
,
361 bFirstIterate
,bReadEkin
,bEkinAveVel
,bScaleEkin
, bConstrain
;
362 real ekin
,temp
,prescorr
,enercorr
,dvdlcorr
;
364 /* translate CGLO flags to gmx_booleans */
365 bRerunMD
= flags
& CGLO_RERUNMD
;
366 bStopCM
= flags
& CGLO_STOPCM
;
367 bGStat
= flags
& CGLO_GSTAT
;
369 bReadEkin
= (flags
& CGLO_READEKIN
);
370 bScaleEkin
= (flags
& CGLO_SCALEEKIN
);
371 bEner
= flags
& CGLO_ENERGY
;
372 bTemp
= flags
& CGLO_TEMPERATURE
;
373 bPres
= (flags
& CGLO_PRESSURE
);
374 bConstrain
= (flags
& CGLO_CONSTRAINT
);
375 bIterate
= (flags
& CGLO_ITERATE
);
376 bFirstIterate
= (flags
& CGLO_FIRSTITERATE
);
378 /* we calculate a full state kinetic energy either with full-step velocity verlet
379 or half step where we need the pressure */
381 bEkinAveVel
= (ir
->eI
==eiVV
|| (ir
->eI
==eiVVAK
&& bPres
) || bReadEkin
);
383 /* in initalization, it sums the shake virial in vv, and to
384 sums ekinh_old in leapfrog (or if we are calculating ekinh_old) for other reasons */
386 /* ########## Kinetic energy ############## */
390 /* Non-equilibrium MD: this is parallellized, but only does communication
391 * when there really is NEMD.
394 if (PAR(cr
) && (ekind
->bNEMD
))
396 accumulate_u(cr
,&(ir
->opts
),ekind
);
401 restore_ekinstate_from_state(cr
,ekind
,&state_global
->ekinstate
);
406 calc_ke_part(state
,&(ir
->opts
),mdatoms
,ekind
,nrnb
,bEkinAveVel
,bIterate
);
411 /* Calculate center of mass velocity if necessary, also parallellized */
412 if (bStopCM
&& !bRerunMD
&& bEner
)
414 calc_vcm_grp(fplog
,mdatoms
->start
,mdatoms
->homenr
,mdatoms
,
415 state
->x
,state
->v
,vcm
);
419 if (bTemp
|| bPres
|| bEner
|| bConstrain
)
423 /* We will not sum ekinh_old,
424 * so signal that we still have to do it.
426 *bSumEkinhOld
= TRUE
;
433 for(i
=0; i
<eglsNR
; i
++)
435 gs_buf
[i
] = gs
->sig
[i
];
440 wallcycle_start(wcycle
,ewcMoveE
);
441 GMX_MPE_LOG(ev_global_stat_start
);
442 global_stat(fplog
,gstat
,cr
,enerd
,force_vir
,shake_vir
,mu_tot
,
444 gs
!= NULL
? eglsNR
: 0,gs_buf
,
446 *bSumEkinhOld
,flags
);
447 GMX_MPE_LOG(ev_global_stat_finish
);
448 wallcycle_stop(wcycle
,ewcMoveE
);
452 if (MULTISIM(cr
) && bInterSimGS
)
456 /* Communicate the signals between the simulations */
457 gmx_sum_sim(eglsNR
,gs_buf
,cr
->ms
);
459 /* Communicate the signals form the master to the others */
460 gmx_bcast(eglsNR
*sizeof(gs_buf
[0]),gs_buf
,cr
);
462 for(i
=0; i
<eglsNR
; i
++)
464 if (bInterSimGS
|| gs_simlocal
[i
])
466 /* Set the communicated signal only when it is non-zero,
467 * since signals might not be processed at each MD step.
469 gsi
= (gs_buf
[i
] >= 0 ?
470 (int)(gs_buf
[i
] + 0.5) :
471 (int)(gs_buf
[i
] - 0.5));
476 /* Turn off the local signal */
481 *bSumEkinhOld
= FALSE
;
485 if (!ekind
->bNEMD
&& debug
&& bTemp
&& (vcm
->nr
> 0))
488 mdatoms
->start
,mdatoms
->start
+mdatoms
->homenr
,
489 state
->v
,vcm
->group_p
[0],
490 mdatoms
->massT
,mdatoms
->tmass
,ekind
->ekin
);
494 /* Do center of mass motion removal */
495 if (bStopCM
&& !bRerunMD
) /* is this correct? Does it get called too often with this logic? */
497 check_cm_grp(fplog
,vcm
,ir
,1);
498 do_stopcm_grp(fplog
,mdatoms
->start
,mdatoms
->homenr
,mdatoms
->cVCM
,
499 state
->x
,state
->v
,vcm
);
500 inc_nrnb(nrnb
,eNR_STOPCM
,mdatoms
->homenr
);
503 /* Calculate the amplitude of the cosine velocity profile */
504 ekind
->cosacc
.vcos
= ekind
->cosacc
.mvcos
/mdatoms
->tmass
;
509 /* Sum the kinetic energies of the groups & calc temp */
510 /* compute full step kinetic energies if vv, or if vv-avek and we are computing the pressure with IR_NPT_TROTTER */
511 /* three maincase: VV with AveVel (md-vv), vv with AveEkin (md-vv-avek), leap with AveEkin (md).
512 Leap with AveVel is not supported; it's not clear that it will actually work.
513 bEkinAveVel: If TRUE, we simply multiply ekin by ekinscale to get a full step kinetic energy.
514 If FALSE, we average ekinh_old and ekinh*ekinscale_nhc to get an averaged half step kinetic energy.
515 bSaveEkinOld: If TRUE (in the case of iteration = bIterate is TRUE), we don't reset the ekinscale_nhc.
516 If FALSE, we go ahead and erase over it.
518 enerd
->term
[F_TEMP
] = sum_ekin(&(ir
->opts
),ekind
,&(enerd
->term
[F_DKDL
]),
519 bEkinAveVel
,bIterate
,bScaleEkin
);
521 enerd
->term
[F_EKIN
] = trace(ekind
->ekin
);
524 /* ########## Long range energy information ###### */
526 if (bEner
|| bPres
|| bConstrain
)
528 calc_dispcorr(fplog
,ir
,fr
,0,top_global
->natoms
,box
,state
->lambda
,
529 corr_pres
,corr_vir
,&prescorr
,&enercorr
,&dvdlcorr
);
532 if (bEner
&& bFirstIterate
)
534 enerd
->term
[F_DISPCORR
] = enercorr
;
535 enerd
->term
[F_EPOT
] += enercorr
;
536 enerd
->term
[F_DVDL
] += dvdlcorr
;
537 if (fr
->efep
!= efepNO
) {
538 enerd
->dvdl_lin
+= dvdlcorr
;
542 /* ########## Now pressure ############## */
543 if (bPres
|| bConstrain
)
546 m_add(force_vir
,shake_vir
,total_vir
);
548 /* Calculate pressure and apply LR correction if PPPM is used.
549 * Use the box from last timestep since we already called update().
552 enerd
->term
[F_PRES
] = calc_pres(fr
->ePBC
,ir
->nwall
,box
,ekind
->ekin
,total_vir
,pres
,
553 (fr
->eeltype
==eelPPPM
)?enerd
->term
[F_COUL_RECIP
]:0.0);
555 /* Calculate long range corrections to pressure and energy */
556 /* this adds to enerd->term[F_PRES] and enerd->term[F_ETOT],
557 and computes enerd->term[F_DISPCORR]. Also modifies the
558 total_vir and pres tesors */
560 m_add(total_vir
,corr_vir
,total_vir
);
561 m_add(pres
,corr_pres
,pres
);
562 enerd
->term
[F_PDISPCORR
] = prescorr
;
563 enerd
->term
[F_PRES
] += prescorr
;
564 *pcurr
= enerd
->term
[F_PRES
];
565 /* calculate temperature using virial */
566 enerd
->term
[F_VTEMP
] = calc_temp(trace(total_vir
),ir
->opts
.nrdf
[0]);
572 /* Definitions for convergence of iterated constraints */
574 /* iterate constraints up to 50 times */
575 #define MAXITERCONST 50
580 real f
,fprev
,x
,xprev
;
583 real allrelerr
[MAXITERCONST
+2];
584 int num_close
; /* number of "close" violations, caused by limited precision. */
588 #define CONVERGEITER 0.000000001
589 #define CLOSE_ENOUGH 0.000001000
591 #define CONVERGEITER 0.0001
592 #define CLOSE_ENOUGH 0.0050
595 /* we want to keep track of the close calls. If there are too many, there might be some other issues.
596 so we make sure that it's either less than some predetermined number, or if more than that number,
597 only some small fraction of the total. */
598 #define MAX_NUMBER_CLOSE 50
599 #define FRACTION_CLOSE 0.001
601 /* maximum length of cyclic traps to check, emerging from limited numerical precision */
604 static void gmx_iterate_init(gmx_iterate_t
*iterate
,gmx_bool bIterate
)
609 iterate
->bIterate
= bIterate
;
610 iterate
->num_close
= 0;
611 for (i
=0;i
<MAXITERCONST
+2;i
++)
613 iterate
->allrelerr
[i
] = 0;
617 static gmx_bool
done_iterating(const t_commrec
*cr
,FILE *fplog
, int nsteps
, gmx_iterate_t
*iterate
, gmx_bool bFirstIterate
, real fom
, real
*newf
)
619 /* monitor convergence, and use a secant search to propose new
622 The secant method computes x_{i+1} = x_{i} - f(x_{i}) * ---------------------
623 f(x_{i}) - f(x_{i-1})
625 The function we are trying to zero is fom-x, where fom is the
626 "figure of merit" which is the pressure (or the veta value) we
627 would get by putting in an old value of the pressure or veta into
628 the incrementor function for the step or half step. I have
629 verified that this gives the same answer as self consistent
630 iteration, usually in many fewer steps, especially for small tau_p.
632 We could possibly eliminate an iteration with proper use
633 of the value from the previous step, but that would take a bit
634 more bookkeeping, especially for veta, since tests indicate the
635 function of veta on the last step is not sufficiently close to
636 guarantee convergence this step. This is
637 good enough for now. On my tests, I could use tau_p down to
638 0.02, which is smaller that would ever be necessary in
639 practice. Generally, 3-5 iterations will be sufficient */
641 real relerr
,err
,xmin
;
649 iterate
->f
= fom
-iterate
->x
;
656 iterate
->f
= fom
-iterate
->x
; /* we want to zero this difference */
657 if ((iterate
->iter_i
> 1) && (iterate
->iter_i
< MAXITERCONST
))
659 if (iterate
->f
==iterate
->fprev
)
665 *newf
= iterate
->x
- (iterate
->x
-iterate
->xprev
)*(iterate
->f
)/(iterate
->f
-iterate
->fprev
);
670 /* just use self-consistent iteration the first step to initialize, or
671 if it's not converging (which happens occasionally -- need to investigate why) */
675 /* Consider a slight shortcut allowing us to exit one sooner -- we check the
676 difference between the closest of x and xprev to the new
677 value. To be 100% certain, we should check the difference between
678 the last result, and the previous result, or
680 relerr = (fabs((x-xprev)/fom));
682 but this is pretty much never necessary under typical conditions.
683 Checking numerically, it seems to lead to almost exactly the same
684 trajectories, but there are small differences out a few decimal
685 places in the pressure, and eventually in the v_eta, but it could
688 if (fabs(*newf-x) < fabs(*newf - xprev)) { xmin = x;} else { xmin = xprev;}
689 relerr = (fabs((*newf-xmin) / *newf));
692 err
= fabs((iterate
->f
-iterate
->fprev
));
693 relerr
= fabs(err
/fom
);
695 iterate
->allrelerr
[iterate
->iter_i
] = relerr
;
697 if (iterate
->iter_i
> 0)
701 fprintf(debug
,"Iterating NPT constraints: %6i %20.12f%14.6g%20.12f\n",
702 iterate
->iter_i
,fom
,relerr
,*newf
);
705 if ((relerr
< CONVERGEITER
) || (err
< CONVERGEITER
) || (fom
==0) || ((iterate
->x
== iterate
->xprev
) && iterate
->iter_i
> 1))
707 iterate
->bIterate
= FALSE
;
710 fprintf(debug
,"Iterating NPT constraints: CONVERGED\n");
714 if (iterate
->iter_i
> MAXITERCONST
)
716 if (relerr
< CLOSE_ENOUGH
)
719 for (i
=1;i
<CYCLEMAX
;i
++) {
720 if ((iterate
->allrelerr
[iterate
->iter_i
-(1+i
)] == iterate
->allrelerr
[iterate
->iter_i
-1]) &&
721 (iterate
->allrelerr
[iterate
->iter_i
-(1+i
)] == iterate
->allrelerr
[iterate
->iter_i
-(1+2*i
)])) {
725 fprintf(debug
,"Exiting from an NPT iterating cycle of length %d\n",i
);
732 /* step 1: trapped in a numerical attractor */
733 /* we are trapped in a numerical attractor, and can't converge any more, and are close to the final result.
734 Better to give up convergence here than have the simulation die.
736 iterate
->num_close
++;
741 /* Step #2: test if we are reasonably close for other reasons, then monitor the number. If not, die */
743 /* how many close calls have we had? If less than a few, we're OK */
744 if (iterate
->num_close
< MAX_NUMBER_CLOSE
)
746 sprintf(buf
,"Slight numerical convergence deviation with NPT at step %d, relative error only %10.5g, likely not a problem, continuing\n",nsteps
,relerr
);
747 md_print_warning(cr
,fplog
,buf
);
748 iterate
->num_close
++;
750 /* if more than a few, check the total fraction. If too high, die. */
751 } else if (iterate
->num_close
/(double)nsteps
> FRACTION_CLOSE
) {
752 gmx_fatal(FARGS
,"Could not converge NPT constraints, too many exceptions (%d%%\n",iterate
->num_close
/(double)nsteps
);
758 gmx_fatal(FARGS
,"Could not converge NPT constraints\n");
763 iterate
->xprev
= iterate
->x
;
765 iterate
->fprev
= iterate
->f
;
771 static void check_nst_param(FILE *fplog
,t_commrec
*cr
,
772 const char *desc_nst
,int nst
,
773 const char *desc_p
,int *p
)
777 if (*p
> 0 && *p
% nst
!= 0)
779 /* Round up to the next multiple of nst */
780 *p
= ((*p
)/nst
+ 1)*nst
;
781 sprintf(buf
,"NOTE: %s changes %s to %d\n",desc_nst
,desc_p
,*p
);
782 md_print_warning(cr
,fplog
,buf
);
786 static void reset_all_counters(FILE *fplog
,t_commrec
*cr
,
787 gmx_large_int_t step
,
788 gmx_large_int_t
*step_rel
,t_inputrec
*ir
,
789 gmx_wallcycle_t wcycle
,t_nrnb
*nrnb
,
790 gmx_runtime_t
*runtime
)
792 char buf
[STRLEN
],sbuf
[STEPSTRSIZE
];
794 /* Reset all the counters related to performance over the run */
795 sprintf(buf
,"Step %s: resetting all time and cycle counters\n",
796 gmx_step_str(step
,sbuf
));
797 md_print_warning(cr
,fplog
,buf
);
799 wallcycle_stop(wcycle
,ewcRUN
);
800 wallcycle_reset_all(wcycle
);
801 if (DOMAINDECOMP(cr
))
803 reset_dd_statistics_counters(cr
->dd
);
806 ir
->init_step
+= *step_rel
;
807 ir
->nsteps
-= *step_rel
;
809 wallcycle_start(wcycle
,ewcRUN
);
810 runtime_start(runtime
);
811 print_date_and_time(fplog
,cr
->nodeid
,"Restarted time",runtime
);
814 static void min_zero(int *n
,int i
)
816 if (i
> 0 && (*n
== 0 || i
< *n
))
822 static int lcd4(int i1
,int i2
,int i3
,int i4
)
833 gmx_incons("All 4 inputs for determininig nstglobalcomm are <= 0");
836 while (nst
> 1 && ((i1
> 0 && i1
% nst
!= 0) ||
837 (i2
> 0 && i2
% nst
!= 0) ||
838 (i3
> 0 && i3
% nst
!= 0) ||
839 (i4
> 0 && i4
% nst
!= 0)))
847 static int check_nstglobalcomm(FILE *fplog
,t_commrec
*cr
,
848 int nstglobalcomm
,t_inputrec
*ir
)
852 if (!EI_DYNAMICS(ir
->eI
))
857 if (nstglobalcomm
== -1)
859 if (!(ir
->nstcalcenergy
> 0 ||
865 if (ir
->nstenergy
> 0 && ir
->nstenergy
< nstglobalcomm
)
867 nstglobalcomm
= ir
->nstenergy
;
872 /* Ensure that we do timely global communication for
873 * (possibly) each of the four following options.
875 nstglobalcomm
= lcd4(ir
->nstcalcenergy
,
877 ir
->etc
!= etcNO
? ir
->nsttcouple
: 0,
878 ir
->epc
!= epcNO
? ir
->nstpcouple
: 0);
883 if (ir
->nstlist
> 0 &&
884 nstglobalcomm
> ir
->nstlist
&& nstglobalcomm
% ir
->nstlist
!= 0)
886 nstglobalcomm
= (nstglobalcomm
/ ir
->nstlist
)*ir
->nstlist
;
887 sprintf(buf
,"WARNING: nstglobalcomm is larger than nstlist, but not a multiple, setting it to %d\n",nstglobalcomm
);
888 md_print_warning(cr
,fplog
,buf
);
890 if (ir
->nstcalcenergy
> 0)
892 check_nst_param(fplog
,cr
,"-gcom",nstglobalcomm
,
893 "nstcalcenergy",&ir
->nstcalcenergy
);
895 if (ir
->etc
!= etcNO
&& ir
->nsttcouple
> 0)
897 check_nst_param(fplog
,cr
,"-gcom",nstglobalcomm
,
898 "nsttcouple",&ir
->nsttcouple
);
900 if (ir
->epc
!= epcNO
&& ir
->nstpcouple
> 0)
902 check_nst_param(fplog
,cr
,"-gcom",nstglobalcomm
,
903 "nstpcouple",&ir
->nstpcouple
);
906 check_nst_param(fplog
,cr
,"-gcom",nstglobalcomm
,
907 "nstenergy",&ir
->nstenergy
);
909 check_nst_param(fplog
,cr
,"-gcom",nstglobalcomm
,
910 "nstlog",&ir
->nstlog
);
913 if (ir
->comm_mode
!= ecmNO
&& ir
->nstcomm
< nstglobalcomm
)
915 sprintf(buf
,"WARNING: Changing nstcomm from %d to %d\n",
916 ir
->nstcomm
,nstglobalcomm
);
917 md_print_warning(cr
,fplog
,buf
);
918 ir
->nstcomm
= nstglobalcomm
;
921 return nstglobalcomm
;
924 void check_ir_old_tpx_versions(t_commrec
*cr
,FILE *fplog
,
925 t_inputrec
*ir
,gmx_mtop_t
*mtop
)
927 /* Check required for old tpx files */
928 if (IR_TWINRANGE(*ir
) && ir
->nstlist
> 1 &&
929 ir
->nstcalcenergy
% ir
->nstlist
!= 0)
931 md_print_warning(cr
,fplog
,"Old tpr file with twin-range settings: modifying energy calculation and/or T/P-coupling frequencies");
933 if (gmx_mtop_ftype_count(mtop
,F_CONSTR
) +
934 gmx_mtop_ftype_count(mtop
,F_CONSTRNC
) > 0 &&
935 ir
->eConstrAlg
== econtSHAKE
)
937 md_print_warning(cr
,fplog
,"With twin-range cut-off's and SHAKE the virial and pressure are incorrect");
938 if (ir
->epc
!= epcNO
)
940 gmx_fatal(FARGS
,"Can not do pressure coupling with twin-range cut-off's and SHAKE");
943 check_nst_param(fplog
,cr
,"nstlist",ir
->nstlist
,
944 "nstcalcenergy",&ir
->nstcalcenergy
);
945 if (ir
->epc
!= epcNO
)
947 check_nst_param(fplog
,cr
,"nstlist",ir
->nstlist
,
948 "nstpcouple",&ir
->nstpcouple
);
950 check_nst_param(fplog
,cr
,"nstcalcenergy",ir
->nstcalcenergy
,
951 "nstenergy",&ir
->nstenergy
);
952 check_nst_param(fplog
,cr
,"nstcalcenergy",ir
->nstcalcenergy
,
953 "nstlog",&ir
->nstlog
);
954 if (ir
->efep
!= efepNO
)
956 check_nst_param(fplog
,cr
,"nstcalcenergy",ir
->nstcalcenergy
,
957 "nstdhdl",&ir
->nstdhdl
);
963 gmx_bool bGStatEveryStep
;
964 gmx_large_int_t step_ns
;
965 gmx_large_int_t step_nscheck
;
976 static void reset_nlistheuristics(gmx_nlheur_t
*nlh
,gmx_large_int_t step
)
980 nlh
->step_nscheck
= step
;
983 static void init_nlistheuristics(gmx_nlheur_t
*nlh
,
984 gmx_bool bGStatEveryStep
,gmx_large_int_t step
)
986 nlh
->bGStatEveryStep
= bGStatEveryStep
;
993 reset_nlistheuristics(nlh
,step
);
996 static void update_nliststatistics(gmx_nlheur_t
*nlh
,gmx_large_int_t step
)
998 gmx_large_int_t nl_lt
;
999 char sbuf
[STEPSTRSIZE
],sbuf2
[STEPSTRSIZE
];
1001 /* Determine the neighbor list life time */
1002 nl_lt
= step
- nlh
->step_ns
;
1005 fprintf(debug
,"%d atoms beyond ns buffer, updating neighbor list after %s steps\n",nlh
->nabnsb
,gmx_step_str(nl_lt
,sbuf
));
1009 nlh
->s2
+= nl_lt
*nl_lt
;
1010 nlh
->ab
+= nlh
->nabnsb
;
1011 if (nlh
->lt_runav
== 0)
1013 nlh
->lt_runav
= nl_lt
;
1014 /* Initialize the fluctuation average
1015 * such that at startup we check after 0 steps.
1017 nlh
->lt_runav2
= sqr(nl_lt
/2.0);
1019 /* Running average with 0.9 gives an exp. history of 9.5 */
1020 nlh
->lt_runav2
= 0.9*nlh
->lt_runav2
+ 0.1*sqr(nlh
->lt_runav
- nl_lt
);
1021 nlh
->lt_runav
= 0.9*nlh
->lt_runav
+ 0.1*nl_lt
;
1022 if (nlh
->bGStatEveryStep
)
1024 /* Always check the nlist validity */
1025 nlh
->step_nscheck
= step
;
1029 /* We check after: <life time> - 2*sigma
1030 * The factor 2 is quite conservative,
1031 * but we assume that with nstlist=-1 the user
1032 * prefers exact integration over performance.
1034 nlh
->step_nscheck
= step
1035 + (int)(nlh
->lt_runav
- 2.0*sqrt(nlh
->lt_runav2
)) - 1;
1039 fprintf(debug
,"nlist life time %s run av. %4.1f sig %3.1f check %s check with -gcom %d\n",
1040 gmx_step_str(nl_lt
,sbuf
),nlh
->lt_runav
,sqrt(nlh
->lt_runav2
),
1041 gmx_step_str(nlh
->step_nscheck
-step
+1,sbuf2
),
1042 (int)(nlh
->lt_runav
- 2.0*sqrt(nlh
->lt_runav2
)));
1046 static void set_nlistheuristics(gmx_nlheur_t
*nlh
,gmx_bool bReset
,gmx_large_int_t step
)
1052 reset_nlistheuristics(nlh
,step
);
1056 update_nliststatistics(nlh
,step
);
1059 nlh
->step_ns
= step
;
1060 /* Initialize the cumulative coordinate scaling matrix */
1061 clear_mat(nlh
->scale_tot
);
1062 for(d
=0; d
<DIM
; d
++)
1064 nlh
->scale_tot
[d
][d
] = 1.0;
1068 static void rerun_parallel_comm(t_commrec
*cr
,t_trxframe
*fr
,
1069 gmx_bool
*bNotLastFrame
)
1074 bAlloc
= (fr
->natoms
== 0);
1076 if (MASTER(cr
) && !*bNotLastFrame
)
1082 gmx_bcast(sizeof(*fr
),fr
,cr
);
1086 *bNotLastFrame
= (fr
->natoms
>= 0);
1088 if (*bNotLastFrame
&& PARTDECOMP(cr
))
1090 /* x and v are the only variable size quantities stored in trr
1091 * that are required for rerun (f is not needed).
1095 snew(fr
->x
,fr
->natoms
);
1096 snew(fr
->v
,fr
->natoms
);
1100 gmx_bcast(fr
->natoms
*sizeof(fr
->x
[0]),fr
->x
[0],cr
);
1104 gmx_bcast(fr
->natoms
*sizeof(fr
->v
[0]),fr
->v
[0],cr
);
1109 double do_md(FILE *fplog
,t_commrec
*cr
,int nfile
,const t_filenm fnm
[],
1110 const output_env_t oenv
, gmx_bool bVerbose
,gmx_bool bCompact
,
1112 gmx_vsite_t
*vsite
,gmx_constr_t constr
,
1113 int stepout
,t_inputrec
*ir
,
1114 gmx_mtop_t
*top_global
,
1116 t_state
*state_global
,
1118 t_nrnb
*nrnb
,gmx_wallcycle_t wcycle
,
1119 gmx_edsam_t ed
,t_forcerec
*fr
,
1120 int repl_ex_nst
,int repl_ex_seed
,
1121 real cpt_period
,real max_hours
,
1122 const char *deviceOptions
,
1123 unsigned long Flags
,
1124 gmx_runtime_t
*runtime
)
1127 gmx_large_int_t step
,step_rel
;
1130 gmx_bool bGStatEveryStep
,bGStat
,bNstEner
,bCalcEnerPres
;
1131 gmx_bool bNS
,bNStList
,bSimAnn
,bStopCM
,bRerunMD
,bNotLastFrame
=FALSE
,
1132 bFirstStep
,bStateFromTPX
,bInitStep
,bLastStep
,
1133 bBornRadii
,bStartingFromCpt
;
1134 gmx_bool bDoDHDL
=FALSE
;
1135 gmx_bool do_ene
,do_log
,do_verbose
,bRerunWarnNoV
=TRUE
,
1136 bForceUpdate
=FALSE
,bCPT
;
1138 gmx_bool bMasterState
;
1139 int force_flags
,cglo_flags
;
1140 tensor force_vir
,shake_vir
,total_vir
,tmp_vir
,pres
;
1142 t_trxstatus
*status
;
1145 t_state
*bufstate
=NULL
;
1146 matrix
*scale_tot
,pcoupl_mu
,M
,ebox
;
1148 t_trxframe rerun_fr
;
1149 gmx_repl_ex_t repl_ex
=NULL
;
1152 gmx_localtop_t
*top
;
1153 t_mdebin
*mdebin
=NULL
;
1154 t_state
*state
=NULL
;
1155 rvec
*f_global
=NULL
;
1158 gmx_enerdata_t
*enerd
;
1160 gmx_global_stat_t gstat
;
1161 gmx_update_t upd
=NULL
;
1162 t_graph
*graph
=NULL
;
1166 gmx_groups_t
*groups
;
1167 gmx_ekindata_t
*ekind
, *ekind_save
;
1168 gmx_shellfc_t shellfc
;
1169 int count
,nconverged
=0;
1172 gmx_bool bIonize
=FALSE
;
1173 gmx_bool bTCR
=FALSE
,bConverged
=TRUE
,bOK
,bSumEkinhOld
,bExchanged
;
1175 gmx_bool bResetCountersHalfMaxH
=FALSE
;
1176 gmx_bool bVV
,bIterations
,bFirstIterate
,bTemp
,bPres
,bTrotter
;
1177 real temp0
,mu_aver
=0,dvdl
;
1179 atom_id
*grpindex
=NULL
;
1181 t_coupl_rec
*tcr
=NULL
;
1182 rvec
*xcopy
=NULL
,*vcopy
=NULL
,*cbuf
=NULL
;
1183 matrix boxcopy
={{0}},lastbox
;
1185 real fom
,oldfom
,veta_save
,pcurr
,scalevir
,tracevir
;
1188 real saved_conserved_quantity
= 0;
1193 char sbuf
[STEPSTRSIZE
],sbuf2
[STEPSTRSIZE
];
1194 int handled_stop_condition
=gmx_stop_cond_none
; /* compare to get_stop_condition*/
1195 gmx_iterate_t iterate
;
1196 gmx_large_int_t multisim_nsteps
=-1; /* number of steps to do before first multisim
1197 simulation stops. If equal to zero, don't
1198 communicate any more between multisims.*/
1200 /* Temporary addition for FAHCORE checkpointing */
1204 /* Check for special mdrun options */
1205 bRerunMD
= (Flags
& MD_RERUN
);
1206 bIonize
= (Flags
& MD_IONIZE
);
1207 bFFscan
= (Flags
& MD_FFSCAN
);
1208 bAppend
= (Flags
& MD_APPENDFILES
);
1209 if (Flags
& MD_RESETCOUNTERSHALFWAY
)
1213 /* Signal to reset the counters half the simulation steps. */
1214 wcycle_set_reset_counters(wcycle
,ir
->nsteps
/2);
1216 /* Signal to reset the counters halfway the simulation time. */
1217 bResetCountersHalfMaxH
= (max_hours
> 0);
1220 /* md-vv uses averaged full step velocities for T-control
1221 md-vv-avek uses averaged half step velocities for T-control (but full step ekin for P control)
1222 md uses averaged half step kinetic energies to determine temperature unless defined otherwise by GMX_EKIN_AVE_VEL; */
1223 bVV
= EI_VV(ir
->eI
);
1224 if (bVV
) /* to store the initial velocities while computing virial */
1226 snew(cbuf
,top_global
->natoms
);
1228 /* all the iteratative cases - only if there are constraints */
1229 bIterations
= ((IR_NPT_TROTTER(ir
)) && (constr
) && (!bRerunMD
));
1230 bTrotter
= (bVV
&& (IR_NPT_TROTTER(ir
) || (IR_NVT_TROTTER(ir
))));
1234 /* Since we don't know if the frames read are related in any way,
1235 * rebuild the neighborlist at every step.
1238 ir
->nstcalcenergy
= 1;
1242 check_ir_old_tpx_versions(cr
,fplog
,ir
,top_global
);
1244 nstglobalcomm
= check_nstglobalcomm(fplog
,cr
,nstglobalcomm
,ir
);
1245 bGStatEveryStep
= (nstglobalcomm
== 1);
1247 if (!bGStatEveryStep
&& ir
->nstlist
== -1 && fplog
!= NULL
)
1250 "To reduce the energy communication with nstlist = -1\n"
1251 "the neighbor list validity should not be checked at every step,\n"
1252 "this means that exact integration is not guaranteed.\n"
1253 "The neighbor list validity is checked after:\n"
1254 " <n.list life time> - 2*std.dev.(n.list life time) steps.\n"
1255 "In most cases this will result in exact integration.\n"
1256 "This reduces the energy communication by a factor of 2 to 3.\n"
1257 "If you want less energy communication, set nstlist > 3.\n\n");
1260 if (bRerunMD
|| bFFscan
)
1264 groups
= &top_global
->groups
;
1266 /* Initial values */
1267 init_md(fplog
,cr
,ir
,oenv
,&t
,&t0
,&state_global
->lambda
,&lam0
,
1268 nrnb
,top_global
,&upd
,
1269 nfile
,fnm
,&outf
,&mdebin
,
1270 force_vir
,shake_vir
,mu_tot
,&bSimAnn
,&vcm
,state_global
,Flags
);
1272 clear_mat(total_vir
);
1274 /* Energy terms and groups */
1276 init_enerdata(top_global
->groups
.grps
[egcENER
].nr
,ir
->n_flambda
,enerd
);
1277 if (DOMAINDECOMP(cr
))
1283 snew(f
,top_global
->natoms
);
1286 /* Kinetic energy data */
1288 init_ekindata(fplog
,top_global
,&(ir
->opts
),ekind
);
1289 /* needed for iteration of constraints */
1291 init_ekindata(fplog
,top_global
,&(ir
->opts
),ekind_save
);
1292 /* Copy the cos acceleration to the groups struct */
1293 ekind
->cosacc
.cos_accel
= ir
->cos_accel
;
1295 gstat
= global_stat_init(ir
);
1298 /* Check for polarizable models and flexible constraints */
1299 shellfc
= init_shell_flexcon(fplog
,
1300 top_global
,n_flexible_constraints(constr
),
1301 (ir
->bContinuation
||
1302 (DOMAINDECOMP(cr
) && !MASTER(cr
))) ?
1303 NULL
: state_global
->x
);
1308 tMPI_Thread_mutex_lock(&deform_init_box_mutex
);
1310 set_deform_reference_box(upd
,
1311 deform_init_init_step_tpx
,
1312 deform_init_box_tpx
);
1314 tMPI_Thread_mutex_unlock(&deform_init_box_mutex
);
1319 double io
= compute_io(ir
,top_global
->natoms
,groups
,mdebin
->ebin
->nener
,1);
1320 if ((io
> 2000) && MASTER(cr
))
1322 "\nWARNING: This run will generate roughly %.0f Mb of data\n\n",
1326 if (DOMAINDECOMP(cr
)) {
1327 top
= dd_init_local_top(top_global
);
1330 dd_init_local_state(cr
->dd
,state_global
,state
);
1332 if (DDMASTER(cr
->dd
) && ir
->nstfout
) {
1333 snew(f_global
,state_global
->natoms
);
1337 /* Initialize the particle decomposition and split the topology */
1338 top
= split_system(fplog
,top_global
,ir
,cr
);
1340 pd_cg_range(cr
,&fr
->cg0
,&fr
->hcg
);
1341 pd_at_range(cr
,&a0
,&a1
);
1343 top
= gmx_mtop_generate_local_top(top_global
,ir
);
1346 a1
= top_global
->natoms
;
1349 state
= partdec_init_local_state(cr
,state_global
);
1352 atoms2md(top_global
,ir
,0,NULL
,a0
,a1
-a0
,mdatoms
);
1355 set_vsite_top(vsite
,top
,mdatoms
,cr
);
1358 if (ir
->ePBC
!= epbcNONE
&& !ir
->bPeriodicMols
) {
1359 graph
= mk_graph(fplog
,&(top
->idef
),0,top_global
->natoms
,FALSE
,FALSE
);
1363 make_local_shells(cr
,mdatoms
,shellfc
);
1366 if (ir
->pull
&& PAR(cr
)) {
1367 dd_make_local_pull_groups(NULL
,ir
->pull
,mdatoms
);
1371 if (DOMAINDECOMP(cr
))
1373 /* Distribute the charge groups over the nodes from the master node */
1374 dd_partition_system(fplog
,ir
->init_step
,cr
,TRUE
,1,
1375 state_global
,top_global
,ir
,
1376 state
,&f
,mdatoms
,top
,fr
,
1377 vsite
,shellfc
,constr
,
1381 update_mdatoms(mdatoms
,state
->lambda
);
1383 /* The rigid body groups can be initialized now that the atom data is there */
1384 init_local_rigid_groups(ir
, mdatoms
, state
);
1388 if (opt2bSet("-cpi",nfile
,fnm
))
1390 /* Update mdebin with energy history if appending to output files */
1391 if ( Flags
& MD_APPENDFILES
)
1393 restore_energyhistory_from_state(mdebin
,&state_global
->enerhist
);
1397 /* We might have read an energy history from checkpoint,
1398 * free the allocated memory and reset the counts.
1400 done_energyhistory(&state_global
->enerhist
);
1401 init_energyhistory(&state_global
->enerhist
);
1404 /* Set the initial energy history in state by updating once */
1405 update_energyhistory(&state_global
->enerhist
,mdebin
);
1408 if ((state
->flags
& (1<<estLD_RNG
)) && (Flags
& MD_READ_RNG
)) {
1409 /* Set the random state if we read a checkpoint file */
1410 set_stochd_state(upd
,state
);
1413 /* Initialize constraints */
1415 if (!DOMAINDECOMP(cr
))
1416 set_constraints(constr
,top
,ir
,mdatoms
,cr
);
1419 /* Check whether we have to GCT stuff */
1420 bTCR
= ftp2bSet(efGCT
,nfile
,fnm
);
1423 fprintf(stderr
,"Will do General Coupling Theory!\n");
1425 gnx
= top_global
->mols
.nr
;
1427 for(i
=0; (i
<gnx
); i
++) {
1432 if (repl_ex_nst
> 0)
1434 /* We need to be sure replica exchange can only occur
1435 * when the energies are current */
1436 check_nst_param(fplog
,cr
,"nstcalcenergy",ir
->nstcalcenergy
,
1437 "repl_ex_nst",&repl_ex_nst
);
1438 /* This check needs to happen before inter-simulation
1439 * signals are initialized, too */
1441 if (repl_ex_nst
> 0 && MASTER(cr
))
1442 repl_ex
= init_replica_exchange(fplog
,cr
->ms
,state_global
,ir
,
1443 repl_ex_nst
,repl_ex_seed
);
1445 if (!ir
->bContinuation
&& !bRerunMD
)
1447 if (mdatoms
->cFREEZE
&& (state
->flags
& (1<<estV
)))
1449 /* Set the velocities of frozen particles to zero */
1450 for(i
=mdatoms
->start
; i
<mdatoms
->start
+mdatoms
->homenr
; i
++)
1452 for(m
=0; m
<DIM
; m
++)
1454 if (ir
->opts
.nFreeze
[mdatoms
->cFREEZE
[i
]][m
])
1464 /* Constrain the initial coordinates and velocities */
1465 do_constrain_first(fplog
,constr
,ir
,mdatoms
,state
,f
,
1466 graph
,cr
,nrnb
,fr
,top
,shake_vir
);
1470 /* Construct the virtual sites for the initial configuration */
1471 construct_vsites(fplog
,vsite
,state
->x
,nrnb
,ir
->delta_t
,NULL
,
1472 top
->idef
.iparams
,top
->idef
.il
,
1473 fr
->ePBC
,fr
->bMolPBC
,graph
,cr
,state
->box
);
1479 /* I'm assuming we need global communication the first time! MRS */
1480 cglo_flags
= (CGLO_TEMPERATURE
| CGLO_GSTAT
1481 | (bVV
? CGLO_PRESSURE
:0)
1482 | (bVV
? CGLO_CONSTRAINT
:0)
1483 | (bRerunMD
? CGLO_RERUNMD
:0)
1484 | ((Flags
& MD_READ_EKIN
) ? CGLO_READEKIN
:0));
1486 bSumEkinhOld
= FALSE
;
1487 compute_globals(fplog
,gstat
,cr
,ir
,fr
,ekind
,state
,state_global
,mdatoms
,nrnb
,vcm
,
1488 wcycle
,enerd
,force_vir
,shake_vir
,total_vir
,pres
,mu_tot
,
1489 constr
,NULL
,FALSE
,state
->box
,
1490 top_global
,&pcurr
,top_global
->natoms
,&bSumEkinhOld
,cglo_flags
);
1491 if (ir
->eI
== eiVVAK
) {
1492 /* a second call to get the half step temperature initialized as well */
1493 /* we do the same call as above, but turn the pressure off -- internally to
1494 compute_globals, this is recognized as a velocity verlet half-step
1495 kinetic energy calculation. This minimized excess variables, but
1496 perhaps loses some logic?*/
1498 compute_globals(fplog
,gstat
,cr
,ir
,fr
,ekind
,state
,state_global
,mdatoms
,nrnb
,vcm
,
1499 wcycle
,enerd
,force_vir
,shake_vir
,total_vir
,pres
,mu_tot
,
1500 constr
,NULL
,FALSE
,state
->box
,
1501 top_global
,&pcurr
,top_global
->natoms
,&bSumEkinhOld
,
1502 cglo_flags
&~ CGLO_PRESSURE
);
1505 /* Calculate the initial half step temperature, and save the ekinh_old */
1506 if (!(Flags
& MD_STARTFROMCPT
))
1508 for(i
=0; (i
<ir
->opts
.ngtc
); i
++)
1510 copy_mat(ekind
->tcstat
[i
].ekinh
,ekind
->tcstat
[i
].ekinh_old
);
1515 enerd
->term
[F_TEMP
] *= 2; /* result of averages being done over previous and current step,
1516 and there is no previous step */
1518 temp0
= enerd
->term
[F_TEMP
];
1520 /* if using an iterative algorithm, we need to create a working directory for the state. */
1523 bufstate
= init_bufstate(state
);
1527 snew(xcopy
,state
->natoms
);
1528 snew(vcopy
,state
->natoms
);
1529 copy_rvecn(state
->x
,xcopy
,0,state
->natoms
);
1530 copy_rvecn(state
->v
,vcopy
,0,state
->natoms
);
1531 copy_mat(state
->box
,boxcopy
);
1534 /* need to make an initiation call to get the Trotter variables set, as well as other constants for non-trotter
1535 temperature control */
1536 trotter_seq
= init_npt_vars(ir
,state
,&MassQ
,bTrotter
);
1540 if (constr
&& !ir
->bContinuation
&& ir
->eConstrAlg
== econtLINCS
)
1543 "RMS relative constraint deviation after constraining: %.2e\n",
1544 constr_rmsd(constr
,FALSE
));
1546 fprintf(fplog
,"Initial temperature: %g K\n",enerd
->term
[F_TEMP
]);
1549 fprintf(stderr
,"starting md rerun '%s', reading coordinates from"
1550 " input trajectory '%s'\n\n",
1551 *(top_global
->name
),opt2fn("-rerun",nfile
,fnm
));
1554 fprintf(stderr
,"Calculated time to finish depends on nsteps from "
1555 "run input file,\nwhich may not correspond to the time "
1556 "needed to process input trajectory.\n\n");
1562 fprintf(stderr
,"starting mdrun '%s'\n",
1563 *(top_global
->name
));
1564 if (ir
->nsteps
>= 0)
1566 sprintf(tbuf
,"%8.1f",(ir
->init_step
+ir
->nsteps
)*ir
->delta_t
);
1570 sprintf(tbuf
,"%s","infinite");
1572 if (ir
->init_step
> 0)
1574 fprintf(stderr
,"%s steps, %s ps (continuing from step %s, %8.1f ps).\n",
1575 gmx_step_str(ir
->init_step
+ir
->nsteps
,sbuf
),tbuf
,
1576 gmx_step_str(ir
->init_step
,sbuf2
),
1577 ir
->init_step
*ir
->delta_t
);
1581 fprintf(stderr
,"%s steps, %s ps.\n",
1582 gmx_step_str(ir
->nsteps
,sbuf
),tbuf
);
1585 fprintf(fplog
,"\n");
1588 /* Set and write start time */
1589 runtime_start(runtime
);
1590 print_date_and_time(fplog
,cr
->nodeid
,"Started mdrun",runtime
);
1591 wallcycle_start(wcycle
,ewcRUN
);
1593 fprintf(fplog
,"\n");
1595 /* safest point to do file checkpointing is here. More general point would be immediately before integrator call */
1597 chkpt_ret
=fcCheckPointParallel( cr
->nodeid
,
1599 if ( chkpt_ret
== 0 )
1600 gmx_fatal( 3,__FILE__
,__LINE__
, "Checkpoint error on step %d\n", 0 );
1604 /***********************************************************
1606 * Loop over MD steps
1608 ************************************************************/
1610 /* if rerunMD then read coordinates and velocities from input trajectory */
1613 if (getenv("GMX_FORCE_UPDATE"))
1615 bForceUpdate
= TRUE
;
1618 rerun_fr
.natoms
= 0;
1621 bNotLastFrame
= read_first_frame(oenv
,&status
,
1622 opt2fn("-rerun",nfile
,fnm
),
1623 &rerun_fr
,TRX_NEED_X
| TRX_READ_V
);
1624 if (rerun_fr
.natoms
!= top_global
->natoms
)
1627 "Number of atoms in trajectory (%d) does not match the "
1628 "run input file (%d)\n",
1629 rerun_fr
.natoms
,top_global
->natoms
);
1631 if (ir
->ePBC
!= epbcNONE
)
1635 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
);
1637 if (max_cutoff2(ir
->ePBC
,rerun_fr
.box
) < sqr(fr
->rlistlong
))
1639 gmx_fatal(FARGS
,"Rerun trajectory frame step %d time %f has too small box dimensions",rerun_fr
.step
,rerun_fr
.time
);
1646 rerun_parallel_comm(cr
,&rerun_fr
,&bNotLastFrame
);
1649 if (ir
->ePBC
!= epbcNONE
)
1651 /* Set the shift vectors.
1652 * Necessary here when have a static box different from the tpr box.
1654 calc_shifts(rerun_fr
.box
,fr
->shift_vec
);
1658 /* loop over MD steps or if rerunMD to end of input trajectory */
1660 /* Skip the first Nose-Hoover integration when we get the state from tpx */
1661 bStateFromTPX
= !opt2bSet("-cpi",nfile
,fnm
);
1662 bInitStep
= bFirstStep
&& (bStateFromTPX
|| bVV
);
1663 bStartingFromCpt
= (Flags
& MD_STARTFROMCPT
) && bInitStep
;
1665 bSumEkinhOld
= FALSE
;
1668 init_global_signals(&gs
,cr
,ir
,repl_ex_nst
);
1670 step
= ir
->init_step
;
1673 if (ir
->nstlist
== -1)
1675 init_nlistheuristics(&nlh
,bGStatEveryStep
,step
);
1678 if (MULTISIM(cr
) && (repl_ex_nst
<=0 ))
1680 /* check how many steps are left in other sims */
1681 multisim_nsteps
=get_multisim_nsteps(cr
, ir
->nsteps
);
1685 /* and stop now if we should */
1686 bLastStep
= (bRerunMD
|| (ir
->nsteps
>= 0 && step_rel
> ir
->nsteps
) ||
1687 ((multisim_nsteps
>= 0) && (step_rel
>= multisim_nsteps
)));
1688 while (!bLastStep
|| (bRerunMD
&& bNotLastFrame
)) {
1690 wallcycle_start(wcycle
,ewcSTEP
);
1692 GMX_MPE_LOG(ev_timestep1
);
1695 if (rerun_fr
.bStep
) {
1696 step
= rerun_fr
.step
;
1697 step_rel
= step
- ir
->init_step
;
1699 if (rerun_fr
.bTime
) {
1709 bLastStep
= (step_rel
== ir
->nsteps
);
1710 t
= t0
+ step
*ir
->delta_t
;
1713 if (ir
->efep
!= efepNO
)
1715 if (bRerunMD
&& rerun_fr
.bLambda
&& (ir
->delta_lambda
!=0))
1717 state_global
->lambda
= rerun_fr
.lambda
;
1721 state_global
->lambda
= lam0
+ step
*ir
->delta_lambda
;
1723 state
->lambda
= state_global
->lambda
;
1724 bDoDHDL
= do_per_step(step
,ir
->nstdhdl
);
1729 update_annealing_target_temp(&(ir
->opts
),t
);
1734 if (!(DOMAINDECOMP(cr
) && !MASTER(cr
)))
1736 for(i
=0; i
<state_global
->natoms
; i
++)
1738 copy_rvec(rerun_fr
.x
[i
],state_global
->x
[i
]);
1742 for(i
=0; i
<state_global
->natoms
; i
++)
1744 copy_rvec(rerun_fr
.v
[i
],state_global
->v
[i
]);
1749 for(i
=0; i
<state_global
->natoms
; i
++)
1751 clear_rvec(state_global
->v
[i
]);
1755 fprintf(stderr
,"\nWARNING: Some frames do not contain velocities.\n"
1756 " Ekin, temperature and pressure are incorrect,\n"
1757 " the virial will be incorrect when constraints are present.\n"
1759 bRerunWarnNoV
= FALSE
;
1763 copy_mat(rerun_fr
.box
,state_global
->box
);
1764 copy_mat(state_global
->box
,state
->box
);
1766 if (vsite
&& (Flags
& MD_RERUN_VSITE
))
1768 if (DOMAINDECOMP(cr
))
1770 gmx_fatal(FARGS
,"Vsite recalculation with -rerun is not implemented for domain decomposition, use particle decomposition");
1774 /* Following is necessary because the graph may get out of sync
1775 * with the coordinates if we only have every N'th coordinate set
1777 mk_mshift(fplog
,graph
,fr
->ePBC
,state
->box
,state
->x
);
1778 shift_self(graph
,state
->box
,state
->x
);
1780 construct_vsites(fplog
,vsite
,state
->x
,nrnb
,ir
->delta_t
,state
->v
,
1781 top
->idef
.iparams
,top
->idef
.il
,
1782 fr
->ePBC
,fr
->bMolPBC
,graph
,cr
,state
->box
);
1785 unshift_self(graph
,state
->box
,state
->x
);
1790 /* Stop Center of Mass motion */
1791 bStopCM
= (ir
->comm_mode
!= ecmNO
&& do_per_step(step
,ir
->nstcomm
));
1793 /* Copy back starting coordinates in case we're doing a forcefield scan */
1796 for(ii
=0; (ii
<state
->natoms
); ii
++)
1798 copy_rvec(xcopy
[ii
],state
->x
[ii
]);
1799 copy_rvec(vcopy
[ii
],state
->v
[ii
]);
1801 copy_mat(boxcopy
,state
->box
);
1806 /* for rerun MD always do Neighbour Searching */
1807 bNS
= (bFirstStep
|| ir
->nstlist
!= 0);
1812 /* Determine whether or not to do Neighbour Searching and LR */
1813 bNStList
= (ir
->nstlist
> 0 && step
% ir
->nstlist
== 0);
1815 bNS
= (bFirstStep
|| bExchanged
|| bNStList
||
1816 (ir
->nstlist
== -1 && nlh
.nabnsb
> 0));
1818 if (bNS
&& ir
->nstlist
== -1)
1820 set_nlistheuristics(&nlh
,bFirstStep
|| bExchanged
,step
);
1824 /* check whether we should stop because another simulation has
1828 if ( (multisim_nsteps
>= 0) && (step_rel
>= multisim_nsteps
) &&
1829 (multisim_nsteps
!= ir
->nsteps
) )
1836 "Stopping simulation %d because another one has finished\n",
1840 gs
.sig
[eglsCHKPT
] = 1;
1845 /* < 0 means stop at next step, > 0 means stop at next NS step */
1846 if ( (gs
.set
[eglsSTOPCOND
] < 0 ) ||
1847 ( (gs
.set
[eglsSTOPCOND
] > 0 ) && ( bNS
|| ir
->nstlist
==0)) )
1852 /* Determine whether or not to update the Born radii if doing GB */
1853 bBornRadii
=bFirstStep
;
1854 if (ir
->implicit_solvent
&& (step
% ir
->nstgbradii
==0))
1859 do_log
= do_per_step(step
,ir
->nstlog
) || bFirstStep
|| bLastStep
;
1860 do_verbose
= bVerbose
&&
1861 (step
% stepout
== 0 || bFirstStep
|| bLastStep
);
1863 if (bNS
&& !(bFirstStep
&& ir
->bContinuation
&& !bRerunMD
))
1867 bMasterState
= TRUE
;
1871 bMasterState
= FALSE
;
1872 /* Correct the new box if it is too skewed */
1873 if (DYNAMIC_BOX(*ir
))
1875 if (correct_box(fplog
,step
,state
->box
,graph
))
1877 bMasterState
= TRUE
;
1880 if (DOMAINDECOMP(cr
) && bMasterState
)
1882 dd_collect_state(cr
->dd
,state
,state_global
);
1886 if (DOMAINDECOMP(cr
))
1888 /* Repartition the domain decomposition */
1889 wallcycle_start(wcycle
,ewcDOMDEC
);
1890 dd_partition_system(fplog
,step
,cr
,
1891 bMasterState
,nstglobalcomm
,
1892 state_global
,top_global
,ir
,
1893 state
,&f
,mdatoms
,top
,fr
,
1894 vsite
,shellfc
,constr
,
1895 nrnb
,wcycle
,do_verbose
);
1896 wallcycle_stop(wcycle
,ewcDOMDEC
);
1897 /* If using an iterative integrator, reallocate space to match the decomposition */
1901 if (MASTER(cr
) && do_log
&& !bFFscan
)
1903 print_ebin_header(fplog
,step
,t
,state
->lambda
);
1906 if (ir
->efep
!= efepNO
)
1908 update_mdatoms(mdatoms
,state
->lambda
);
1911 if (bRerunMD
&& rerun_fr
.bV
)
1914 /* We need the kinetic energy at minus the half step for determining
1915 * the full step kinetic energy and possibly for T-coupling.*/
1916 /* This may not be quite working correctly yet . . . . */
1917 compute_globals(fplog
,gstat
,cr
,ir
,fr
,ekind
,state
,state_global
,mdatoms
,nrnb
,vcm
,
1918 wcycle
,enerd
,NULL
,NULL
,NULL
,NULL
,mu_tot
,
1919 constr
,NULL
,FALSE
,state
->box
,
1920 top_global
,&pcurr
,top_global
->natoms
,&bSumEkinhOld
,
1921 CGLO_RERUNMD
| CGLO_GSTAT
| CGLO_TEMPERATURE
);
1923 clear_mat(force_vir
);
1925 /* Ionize the atoms if necessary */
1928 ionize(fplog
,oenv
,mdatoms
,top_global
,t
,ir
,state
->x
,state
->v
,
1929 mdatoms
->start
,mdatoms
->start
+mdatoms
->homenr
,state
->box
,cr
);
1932 /* Update force field in ffscan program */
1935 if (update_forcefield(fplog
,
1937 mdatoms
->nr
,state
->x
,state
->box
)) {
1938 if (gmx_parallel_env_initialized())
1946 GMX_MPE_LOG(ev_timestep2
);
1948 /* We write a checkpoint at this MD step when:
1949 * either at an NS step when we signalled through gs,
1950 * or at the last step (but not when we do not want confout),
1951 * but never at the first step or with rerun.
1953 bCPT
= (((gs
.set
[eglsCHKPT
] && (bNS
|| ir
->nstlist
== 0)) ||
1954 (bLastStep
&& (Flags
& MD_CONFOUT
))) &&
1955 step
> ir
->init_step
&& !bRerunMD
);
1958 gs
.set
[eglsCHKPT
] = 0;
1961 /* Determine the energy and pressure:
1962 * at nstcalcenergy steps and at energy output steps (set below).
1964 bNstEner
= do_per_step(step
,ir
->nstcalcenergy
);
1967 (ir
->epc
!= epcNO
&& do_per_step(step
,ir
->nstpcouple
)));
1969 /* Do we need global communication ? */
1970 bGStat
= (bCalcEnerPres
|| bStopCM
||
1971 do_per_step(step
,nstglobalcomm
) ||
1972 (ir
->nstlist
== -1 && !bRerunMD
&& step
>= nlh
.step_nscheck
));
1974 do_ene
= (do_per_step(step
,ir
->nstenergy
) || bLastStep
);
1976 if (do_ene
|| do_log
)
1978 bCalcEnerPres
= TRUE
;
1982 /* these CGLO_ options remain the same throughout the iteration */
1983 cglo_flags
= ((bRerunMD
? CGLO_RERUNMD
: 0) |
1984 (bStopCM
? CGLO_STOPCM
: 0) |
1985 (bGStat
? CGLO_GSTAT
: 0)
1988 force_flags
= (GMX_FORCE_STATECHANGED
|
1989 ((DYNAMIC_BOX(*ir
) || bRerunMD
) ? GMX_FORCE_DYNAMICBOX
: 0) |
1990 GMX_FORCE_ALLFORCES
|
1991 (bNStList
? GMX_FORCE_DOLR
: 0) |
1993 (bCalcEnerPres
? GMX_FORCE_VIRIAL
: 0) |
1994 (bDoDHDL
? GMX_FORCE_DHDL
: 0)
1999 /* Now is the time to relax the shells */
2000 count
=relax_shell_flexcon(fplog
,cr
,bVerbose
,bFFscan
? step
+1 : step
,
2002 bStopCM
,top
,top_global
,
2004 state
,f
,force_vir
,mdatoms
,
2005 nrnb
,wcycle
,graph
,groups
,
2006 shellfc
,fr
,bBornRadii
,t
,mu_tot
,
2007 state
->natoms
,&bConverged
,vsite
,
2018 /* The coordinates (x) are shifted (to get whole molecules)
2020 * This is parallellized as well, and does communication too.
2021 * Check comments in sim_util.c
2024 do_force(fplog
,cr
,ir
,step
,nrnb
,wcycle
,top
,top_global
,groups
,
2025 state
->box
,state
->x
,&state
->hist
,
2026 f
,force_vir
,mdatoms
,enerd
,fcd
,
2027 state
->lambda
,graph
,
2028 fr
,vsite
,mu_tot
,t
,outf
->fp_field
,ed
,bBornRadii
,
2029 (bNS
? GMX_FORCE_NS
: 0) | force_flags
);
2032 GMX_BARRIER(cr
->mpi_comm_mygroup
);
2036 mu_aver
= calc_mu_aver(cr
,state
->x
,mdatoms
->chargeA
,
2037 mu_tot
,&top_global
->mols
,mdatoms
,gnx
,grpindex
);
2040 if (bTCR
&& bFirstStep
)
2042 tcr
=init_coupling(fplog
,nfile
,fnm
,cr
,fr
,mdatoms
,&(top
->idef
));
2043 fprintf(fplog
,"Done init_coupling\n");
2047 if (bVV
&& !bStartingFromCpt
&& !bRerunMD
)
2048 /* ############### START FIRST UPDATE HALF-STEP FOR VV METHODS############### */
2050 if (ir
->eI
==eiVV
&& bInitStep
)
2052 /* if using velocity verlet with full time step Ekin,
2053 * take the first half step only to compute the
2054 * virial for the first step. From there,
2055 * revert back to the initial coordinates
2056 * so that the input is actually the initial step.
2058 copy_rvecn(state
->v
,cbuf
,0,state
->natoms
); /* should make this better for parallelizing? */
2060 /* this is for NHC in the Ekin(t+dt/2) version of vv */
2061 trotter_update(ir
,step
,ekind
,enerd
,state
,total_vir
,mdatoms
,&MassQ
,trotter_seq
,ettTSEQ1
);
2064 update_coords(fplog
,step
,ir
,mdatoms
,state
,
2065 f
,fr
->bTwinRange
&& bNStList
,fr
->f_twin
,fcd
,
2066 ekind
,M
,wcycle
,upd
,bInitStep
,etrtVELOCITY1
,
2067 cr
,nrnb
,constr
,&top
->idef
);
2071 gmx_iterate_init(&iterate
,bIterations
&& !bInitStep
);
2073 /* for iterations, we save these vectors, as we will be self-consistently iterating
2076 /*#### UPDATE EXTENDED VARIABLES IN TROTTER FORMULATION */
2078 /* save the state */
2079 if (bIterations
&& iterate
.bIterate
) {
2080 copy_coupling_state(state
,bufstate
,ekind
,ekind_save
,&(ir
->opts
));
2083 bFirstIterate
= TRUE
;
2084 while (bFirstIterate
|| (bIterations
&& iterate
.bIterate
))
2086 if (bIterations
&& iterate
.bIterate
)
2088 copy_coupling_state(bufstate
,state
,ekind_save
,ekind
,&(ir
->opts
));
2089 if (bFirstIterate
&& bTrotter
)
2091 /* The first time through, we need a decent first estimate
2092 of veta(t+dt) to compute the constraints. Do
2093 this by computing the box volume part of the
2094 trotter integration at this time. Nothing else
2095 should be changed by this routine here. If
2096 !(first time), we start with the previous value
2099 veta_save
= state
->veta
;
2100 trotter_update(ir
,step
,ekind
,enerd
,state
,total_vir
,mdatoms
,&MassQ
,trotter_seq
,ettTSEQ0
);
2101 vetanew
= state
->veta
;
2102 state
->veta
= veta_save
;
2107 if ( !bRerunMD
|| rerun_fr
.bV
|| bForceUpdate
) { /* Why is rerun_fr.bV here? Unclear. */
2110 update_constraints(fplog
,step
,&dvdl
,ir
,ekind
,mdatoms
,state
,graph
,f
,
2111 &top
->idef
,shake_vir
,NULL
,
2112 cr
,nrnb
,wcycle
,upd
,constr
,
2113 bInitStep
,TRUE
,bCalcEnerPres
,vetanew
);
2115 if (!bOK
&& !bFFscan
)
2117 gmx_fatal(FARGS
,"Constraint error: Shake, Lincs or Settle could not solve the constrains");
2122 { /* Need to unshift here if a do_force has been
2123 called in the previous step */
2124 unshift_self(graph
,state
->box
,state
->x
);
2128 /* if VV, compute the pressure and constraints */
2129 /* For VV2, we strictly only need this if using pressure
2130 * control, but we really would like to have accurate pressures
2132 * Think about ways around this in the future?
2133 * For now, keep this choice in comments.
2135 /*bPres = (ir->eI==eiVV || IR_NPT_TROTTER(ir)); */
2136 /*bTemp = ((ir->eI==eiVV &&(!bInitStep)) || (ir->eI==eiVVAK && IR_NPT_TROTTER(ir)));*/
2138 bTemp
= ((ir
->eI
==eiVV
&&(!bInitStep
)) || (ir
->eI
==eiVVAK
));
2139 compute_globals(fplog
,gstat
,cr
,ir
,fr
,ekind
,state
,state_global
,mdatoms
,nrnb
,vcm
,
2140 wcycle
,enerd
,force_vir
,shake_vir
,total_vir
,pres
,mu_tot
,
2141 constr
,NULL
,FALSE
,state
->box
,
2142 top_global
,&pcurr
,top_global
->natoms
,&bSumEkinhOld
,
2145 | (bTemp
? CGLO_TEMPERATURE
:0)
2146 | (bPres
? CGLO_PRESSURE
: 0)
2147 | (bPres
? CGLO_CONSTRAINT
: 0)
2148 | ((bIterations
&& iterate
.bIterate
) ? CGLO_ITERATE
: 0)
2149 | (bFirstIterate
? CGLO_FIRSTITERATE
: 0)
2152 /* explanation of above:
2153 a) We compute Ekin at the full time step
2154 if 1) we are using the AveVel Ekin, and it's not the
2155 initial step, or 2) if we are using AveEkin, but need the full
2156 time step kinetic energy for the pressure (always true now, since we want accurate statistics).
2157 b) If we are using EkinAveEkin for the kinetic energy for the temperture control, we still feed in
2158 EkinAveVel because it's needed for the pressure */
2160 /* temperature scaling and pressure scaling to produce the extended variables at t+dt */
2165 trotter_update(ir
,step
,ekind
,enerd
,state
,total_vir
,mdatoms
,&MassQ
,trotter_seq
,ettTSEQ2
);
2169 update_tcouple(fplog
,step
,ir
,state
,ekind
,wcycle
,upd
,&MassQ
,mdatoms
);
2174 done_iterating(cr
,fplog
,step
,&iterate
,bFirstIterate
,
2175 state
->veta
,&vetanew
))
2179 bFirstIterate
= FALSE
;
2182 if (bTrotter
&& !bInitStep
) {
2183 copy_mat(shake_vir
,state
->svir_prev
);
2184 copy_mat(force_vir
,state
->fvir_prev
);
2185 if (IR_NVT_TROTTER(ir
) && ir
->eI
==eiVV
) {
2186 /* update temperature and kinetic energy now that step is over - this is the v(t+dt) point */
2187 enerd
->term
[F_TEMP
] = sum_ekin(&(ir
->opts
),ekind
,NULL
,(ir
->eI
==eiVV
),FALSE
,FALSE
);
2188 enerd
->term
[F_EKIN
] = trace(ekind
->ekin
);
2191 /* if it's the initial step, we performed this first step just to get the constraint virial */
2192 if (bInitStep
&& ir
->eI
==eiVV
) {
2193 copy_rvecn(cbuf
,state
->v
,0,state
->natoms
);
2196 if (fr
->bSepDVDL
&& fplog
&& do_log
)
2198 fprintf(fplog
,sepdvdlformat
,"Constraint",0.0,dvdl
);
2200 enerd
->term
[F_DHDL_CON
] += dvdl
;
2202 GMX_MPE_LOG(ev_timestep1
);
2205 /* MRS -- now done iterating -- compute the conserved quantity */
2207 saved_conserved_quantity
= compute_conserved_from_auxiliary(ir
,state
,&MassQ
);
2210 last_ekin
= enerd
->term
[F_EKIN
]; /* does this get preserved through checkpointing? */
2212 if ((ir
->eDispCorr
!= edispcEnerPres
) && (ir
->eDispCorr
!= edispcAllEnerPres
))
2214 saved_conserved_quantity
-= enerd
->term
[F_DISPCORR
];
2218 /* ######## END FIRST UPDATE STEP ############## */
2219 /* ######## If doing VV, we now have v(dt) ###### */
2221 /* ################## START TRAJECTORY OUTPUT ################# */
2223 /* Now we have the energies and forces corresponding to the
2224 * coordinates at time t. We must output all of this before
2226 * for RerunMD t is read from input trajectory
2228 GMX_MPE_LOG(ev_output_start
);
2231 if (do_per_step(step
,ir
->nstxout
)) { mdof_flags
|= MDOF_X
; }
2232 if (do_per_step(step
,ir
->nstvout
)) { mdof_flags
|= MDOF_V
; }
2233 if (do_per_step(step
,ir
->nstfout
)) { mdof_flags
|= MDOF_F
; }
2234 if (do_per_step(step
,ir
->nstxtcout
)) { mdof_flags
|= MDOF_XTC
; }
2235 if (bCPT
) { mdof_flags
|= MDOF_CPT
; };
2237 #if defined(GMX_FAHCORE) || defined(GMX_WRITELASTSTEP)
2240 /* Enforce writing positions and velocities at end of run */
2241 mdof_flags
|= (MDOF_X
| MDOF_V
);
2246 fcReportProgress( ir
->nsteps
, step
);
2248 /* sync bCPT and fc record-keeping */
2249 if (bCPT
&& MASTER(cr
))
2250 fcRequestCheckPoint();
2253 if (mdof_flags
!= 0)
2255 wallcycle_start(wcycle
,ewcTRAJ
);
2258 if (state
->flags
& (1<<estLD_RNG
))
2260 get_stochd_state(upd
,state
);
2266 state_global
->ekinstate
.bUpToDate
= FALSE
;
2270 update_ekinstate(&state_global
->ekinstate
,ekind
);
2271 state_global
->ekinstate
.bUpToDate
= TRUE
;
2273 update_energyhistory(&state_global
->enerhist
,mdebin
);
2276 write_traj(fplog
,cr
,outf
,mdof_flags
,top_global
,
2277 step
,t
,state
,state_global
,f
,f_global
,&n_xtc
,&x_xtc
);
2284 if (bLastStep
&& step_rel
== ir
->nsteps
&&
2285 (Flags
& MD_CONFOUT
) && MASTER(cr
) &&
2286 !bRerunMD
&& !bFFscan
)
2288 /* x and v have been collected in write_traj,
2289 * because a checkpoint file will always be written
2292 fprintf(stderr
,"\nWriting final coordinates.\n");
2293 if (ir
->ePBC
!= epbcNONE
&& !ir
->bPeriodicMols
&&
2296 /* Make molecules whole only for confout writing */
2297 do_pbc_mtop(fplog
,ir
->ePBC
,state
->box
,top_global
,state_global
->x
);
2299 write_sto_conf_mtop(ftp2fn(efSTO
,nfile
,fnm
),
2300 *top_global
->name
,top_global
,
2301 state_global
->x
,state_global
->v
,
2302 ir
->ePBC
,state
->box
);
2305 wallcycle_stop(wcycle
,ewcTRAJ
);
2307 GMX_MPE_LOG(ev_output_finish
);
2309 /* kludge -- virial is lost with restart for NPT control. Must restart */
2310 if (bStartingFromCpt
&& bVV
)
2312 copy_mat(state
->svir_prev
,shake_vir
);
2313 copy_mat(state
->fvir_prev
,force_vir
);
2315 /* ################## END TRAJECTORY OUTPUT ################ */
2317 /* Determine the wallclock run time up till now */
2318 run_time
= gmx_gettime() - (double)runtime
->real
;
2320 /* Check whether everything is still allright */
2321 if (((int)gmx_get_stop_condition() > handled_stop_condition
)
2327 /* this is just make gs.sig compatible with the hack
2328 of sending signals around by MPI_Reduce with together with
2330 if ( gmx_get_stop_condition() == gmx_stop_cond_next_ns
)
2331 gs
.sig
[eglsSTOPCOND
]=1;
2332 if ( gmx_get_stop_condition() == gmx_stop_cond_next
)
2333 gs
.sig
[eglsSTOPCOND
]=-1;
2334 /* < 0 means stop at next step, > 0 means stop at next NS step */
2338 "\n\nReceived the %s signal, stopping at the next %sstep\n\n",
2339 gmx_get_signal_name(),
2340 gs
.sig
[eglsSTOPCOND
]==1 ? "NS " : "");
2344 "\n\nReceived the %s signal, stopping at the next %sstep\n\n",
2345 gmx_get_signal_name(),
2346 gs
.sig
[eglsSTOPCOND
]==1 ? "NS " : "");
2348 handled_stop_condition
=(int)gmx_get_stop_condition();
2350 else if (MASTER(cr
) && (bNS
|| ir
->nstlist
<= 0) &&
2351 (max_hours
> 0 && run_time
> max_hours
*60.0*60.0*0.99) &&
2352 gs
.sig
[eglsSTOPCOND
] == 0 && gs
.set
[eglsSTOPCOND
] == 0)
2354 /* Signal to terminate the run */
2355 gs
.sig
[eglsSTOPCOND
] = 1;
2358 fprintf(fplog
,"\nStep %s: Run time exceeded %.3f hours, will terminate the run\n",gmx_step_str(step
,sbuf
),max_hours
*0.99);
2360 fprintf(stderr
, "\nStep %s: Run time exceeded %.3f hours, will terminate the run\n",gmx_step_str(step
,sbuf
),max_hours
*0.99);
2363 if (bResetCountersHalfMaxH
&& MASTER(cr
) &&
2364 run_time
> max_hours
*60.0*60.0*0.495)
2366 gs
.sig
[eglsRESETCOUNTERS
] = 1;
2369 if (ir
->nstlist
== -1 && !bRerunMD
)
2371 /* When bGStatEveryStep=FALSE, global_stat is only called
2372 * when we check the atom displacements, not at NS steps.
2373 * This means that also the bonded interaction count check is not
2374 * performed immediately after NS. Therefore a few MD steps could
2375 * be performed with missing interactions.
2376 * But wrong energies are never written to file,
2377 * since energies are only written after global_stat
2380 if (step
>= nlh
.step_nscheck
)
2382 nlh
.nabnsb
= natoms_beyond_ns_buffer(ir
,fr
,&top
->cgs
,
2383 nlh
.scale_tot
,state
->x
);
2387 /* This is not necessarily true,
2388 * but step_nscheck is determined quite conservatively.
2394 /* In parallel we only have to check for checkpointing in steps
2395 * where we do global communication,
2396 * otherwise the other nodes don't know.
2398 if (MASTER(cr
) && ((bGStat
|| !PAR(cr
)) &&
2401 run_time
>= nchkpt
*cpt_period
*60.0)) &&
2402 gs
.set
[eglsCHKPT
] == 0)
2404 gs
.sig
[eglsCHKPT
] = 1;
2409 gmx_iterate_init(&iterate
,bIterations
);
2412 /* for iterations, we save these vectors, as we will be redoing the calculations */
2413 if (bIterations
&& iterate
.bIterate
)
2415 copy_coupling_state(state
,bufstate
,ekind
,ekind_save
,&(ir
->opts
));
2417 bFirstIterate
= TRUE
;
2418 while (bFirstIterate
|| (bIterations
&& iterate
.bIterate
))
2420 /* We now restore these vectors to redo the calculation with improved extended variables */
2423 copy_coupling_state(bufstate
,state
,ekind_save
,ekind
,&(ir
->opts
));
2426 /* We make the decision to break or not -after- the calculation of Ekin and Pressure,
2427 so scroll down for that logic */
2429 /* ######### START SECOND UPDATE STEP ################# */
2430 GMX_MPE_LOG(ev_update_start
);
2431 /* Box is changed in update() when we do pressure coupling,
2432 * but we should still use the old box for energy corrections and when
2433 * writing it to the energy file, so it matches the trajectory files for
2434 * the same timestep above. Make a copy in a separate array.
2436 copy_mat(state
->box
,lastbox
);
2439 if (!(bRerunMD
&& !rerun_fr
.bV
&& !bForceUpdate
))
2441 wallcycle_start(wcycle
,ewcUPDATE
);
2443 /* UPDATE PRESSURE VARIABLES IN TROTTER FORMULATION WITH CONSTRAINTS */
2446 if (bIterations
&& iterate
.bIterate
)
2454 /* we use a new value of scalevir to converge the iterations faster */
2455 scalevir
= tracevir
/trace(shake_vir
);
2457 msmul(shake_vir
,scalevir
,shake_vir
);
2458 m_add(force_vir
,shake_vir
,total_vir
);
2459 clear_mat(shake_vir
);
2461 trotter_update(ir
,step
,ekind
,enerd
,state
,total_vir
,mdatoms
,&MassQ
,trotter_seq
,ettTSEQ3
);
2462 /* We can only do Berendsen coupling after we have summed
2463 * the kinetic energy or virial. Since the happens
2464 * in global_state after update, we should only do it at
2465 * step % nstlist = 1 with bGStatEveryStep=FALSE.
2470 update_tcouple(fplog
,step
,ir
,state
,ekind
,wcycle
,upd
,&MassQ
,mdatoms
);
2471 update_pcouple(fplog
,step
,ir
,state
,pcoupl_mu
,M
,wcycle
,
2477 /* velocity half-step update */
2478 update_coords(fplog
,step
,ir
,mdatoms
,state
,f
,
2479 fr
->bTwinRange
&& bNStList
,fr
->f_twin
,fcd
,
2480 ekind
,M
,wcycle
,upd
,FALSE
,etrtVELOCITY2
,
2481 cr
,nrnb
,constr
,&top
->idef
);
2484 /* Above, initialize just copies ekinh into ekin,
2485 * it doesn't copy position (for VV),
2486 * and entire integrator for MD.
2491 copy_rvecn(state
->x
,cbuf
,0,state
->natoms
);
2494 update_coords(fplog
,step
,ir
,mdatoms
,state
,f
,fr
->bTwinRange
&& bNStList
,fr
->f_twin
,fcd
,
2495 ekind
,M
,wcycle
,upd
,bInitStep
,etrtPOSITION
,cr
,nrnb
,constr
,&top
->idef
);
2496 wallcycle_stop(wcycle
,ewcUPDATE
);
2498 update_constraints(fplog
,step
,&dvdl
,ir
,ekind
,mdatoms
,state
,graph
,f
,
2499 &top
->idef
,shake_vir
,force_vir
,
2500 cr
,nrnb
,wcycle
,upd
,constr
,
2501 bInitStep
,FALSE
,bCalcEnerPres
,state
->veta
);
2505 /* erase F_EKIN and F_TEMP here? */
2506 /* just compute the kinetic energy at the half step to perform a trotter step */
2507 compute_globals(fplog
,gstat
,cr
,ir
,fr
,ekind
,state
,state_global
,mdatoms
,nrnb
,vcm
,
2508 wcycle
,enerd
,force_vir
,shake_vir
,total_vir
,pres
,mu_tot
,
2509 constr
,NULL
,FALSE
,lastbox
,
2510 top_global
,&pcurr
,top_global
->natoms
,&bSumEkinhOld
,
2511 cglo_flags
| CGLO_TEMPERATURE
2513 wallcycle_start(wcycle
,ewcUPDATE
);
2514 trotter_update(ir
,step
,ekind
,enerd
,state
,total_vir
,mdatoms
,&MassQ
,trotter_seq
,ettTSEQ4
);
2515 /* now we know the scaling, we can compute the positions again again */
2516 copy_rvecn(cbuf
,state
->x
,0,state
->natoms
);
2518 update_coords(fplog
,step
,ir
,mdatoms
,state
,f
,fr
->bTwinRange
&& bNStList
,fr
->f_twin
,fcd
,
2519 ekind
,M
,wcycle
,upd
,bInitStep
,etrtPOSITION
,cr
,nrnb
,constr
,&top
->idef
);
2520 wallcycle_stop(wcycle
,ewcUPDATE
);
2522 /* do we need an extra constraint here? just need to copy out of state->v to upd->xp? */
2523 /* are the small terms in the shake_vir here due
2524 * to numerical errors, or are they important
2525 * physically? I'm thinking they are just errors, but not completely sure.
2526 * For now, will call without actually constraining, constr=NULL*/
2527 update_constraints(fplog
,step
,&dvdl
,ir
,ekind
,mdatoms
,state
,graph
,f
,
2528 &top
->idef
,tmp_vir
,force_vir
,
2529 cr
,nrnb
,wcycle
,upd
,NULL
,
2530 bInitStep
,FALSE
,bCalcEnerPres
,
2533 if (!bOK
&& !bFFscan
)
2535 gmx_fatal(FARGS
,"Constraint error: Shake, Lincs or Settle could not solve the constrains");
2538 if (fr
->bSepDVDL
&& fplog
&& do_log
)
2540 fprintf(fplog
,sepdvdlformat
,"Constraint",0.0,dvdl
);
2542 enerd
->term
[F_DHDL_CON
] += dvdl
;
2546 /* Need to unshift here */
2547 unshift_self(graph
,state
->box
,state
->x
);
2550 GMX_BARRIER(cr
->mpi_comm_mygroup
);
2551 GMX_MPE_LOG(ev_update_finish
);
2555 wallcycle_start(wcycle
,ewcVSITECONSTR
);
2558 shift_self(graph
,state
->box
,state
->x
);
2560 construct_vsites(fplog
,vsite
,state
->x
,nrnb
,ir
->delta_t
,state
->v
,
2561 top
->idef
.iparams
,top
->idef
.il
,
2562 fr
->ePBC
,fr
->bMolPBC
,graph
,cr
,state
->box
);
2566 unshift_self(graph
,state
->box
,state
->x
);
2568 wallcycle_stop(wcycle
,ewcVSITECONSTR
);
2571 /* ############## IF NOT VV, Calculate globals HERE, also iterate constraints ############ */
2572 if (ir
->nstlist
== -1 && bFirstIterate
)
2574 gs
.sig
[eglsNABNSB
] = nlh
.nabnsb
;
2576 compute_globals(fplog
,gstat
,cr
,ir
,fr
,ekind
,state
,state_global
,mdatoms
,nrnb
,vcm
,
2577 wcycle
,enerd
,force_vir
,shake_vir
,total_vir
,pres
,mu_tot
,
2579 bFirstIterate
? &gs
: NULL
,
2580 (step_rel
% gs
.nstms
== 0) &&
2581 (multisim_nsteps
<0 || (step_rel
<multisim_nsteps
)),
2583 top_global
,&pcurr
,top_global
->natoms
,&bSumEkinhOld
,
2585 | (!EI_VV(ir
->eI
) ? CGLO_ENERGY
: 0)
2586 | (!EI_VV(ir
->eI
) ? CGLO_TEMPERATURE
: 0)
2587 | (!EI_VV(ir
->eI
) || bRerunMD
? CGLO_PRESSURE
: 0)
2588 | (bIterations
&& iterate
.bIterate
? CGLO_ITERATE
: 0)
2589 | (bFirstIterate
? CGLO_FIRSTITERATE
: 0)
2592 if (ir
->nstlist
== -1 && bFirstIterate
)
2594 nlh
.nabnsb
= gs
.set
[eglsNABNSB
];
2595 gs
.set
[eglsNABNSB
] = 0;
2597 /* bIterate is set to keep it from eliminating the old ekin kinetic energy terms */
2598 /* ############# END CALC EKIN AND PRESSURE ################# */
2600 /* Note: this is OK, but there are some numerical precision issues with using the convergence of
2601 the virial that should probably be addressed eventually. state->veta has better properies,
2602 but what we actually need entering the new cycle is the new shake_vir value. Ideally, we could
2603 generate the new shake_vir, but test the veta value for convergence. This will take some thought. */
2606 done_iterating(cr
,fplog
,step
,&iterate
,bFirstIterate
,
2607 trace(shake_vir
),&tracevir
))
2611 bFirstIterate
= FALSE
;
2614 update_box(fplog
,step
,ir
,mdatoms
,state
,graph
,f
,
2615 ir
->nstlist
==-1 ? &nlh
.scale_tot
: NULL
,pcoupl_mu
,nrnb
,wcycle
,upd
,bInitStep
,FALSE
);
2617 /* ################# END UPDATE STEP 2 ################# */
2618 /* #### We now have r(t+dt) and v(t+dt/2) ############# */
2620 /* The coordinates (x) were unshifted in update */
2621 if (bFFscan
&& (shellfc
==NULL
|| bConverged
))
2623 if (print_forcefield(fplog
,enerd
->term
,mdatoms
->homenr
,
2625 &(top_global
->mols
),mdatoms
->massT
,pres
))
2627 if (gmx_parallel_env_initialized())
2631 fprintf(stderr
,"\n");
2637 /* We will not sum ekinh_old,
2638 * so signal that we still have to do it.
2640 bSumEkinhOld
= TRUE
;
2645 /* Only do GCT when the relaxation of shells (minimization) has converged,
2646 * otherwise we might be coupling to bogus energies.
2647 * In parallel we must always do this, because the other sims might
2651 /* Since this is called with the new coordinates state->x, I assume
2652 * we want the new box state->box too. / EL 20040121
2654 do_coupling(fplog
,oenv
,nfile
,fnm
,tcr
,t
,step
,enerd
->term
,fr
,
2656 mdatoms
,&(top
->idef
),mu_aver
,
2657 top_global
->mols
.nr
,cr
,
2658 state
->box
,total_vir
,pres
,
2659 mu_tot
,state
->x
,f
,bConverged
);
2663 /* ######### BEGIN PREPARING EDR OUTPUT ########### */
2665 /* sum up the foreign energy and dhdl terms */
2666 sum_dhdl(enerd
,state
->lambda
,ir
);
2668 /* use the directly determined last velocity, not actually the averaged half steps */
2669 if (bTrotter
&& ir
->eI
==eiVV
)
2671 enerd
->term
[F_EKIN
] = last_ekin
;
2673 enerd
->term
[F_ETOT
] = enerd
->term
[F_EPOT
] + enerd
->term
[F_EKIN
];
2677 enerd
->term
[F_ECONSERVED
] = enerd
->term
[F_ETOT
] + saved_conserved_quantity
;
2681 enerd
->term
[F_ECONSERVED
] = enerd
->term
[F_ETOT
] + compute_conserved_from_auxiliary(ir
,state
,&MassQ
);
2683 /* Check for excessively large energies */
2687 real etot_max
= 1e200
;
2689 real etot_max
= 1e30
;
2691 if (fabs(enerd
->term
[F_ETOT
]) > etot_max
)
2693 fprintf(stderr
,"Energy too large (%g), giving up\n",
2694 enerd
->term
[F_ETOT
]);
2697 /* ######### END PREPARING EDR OUTPUT ########### */
2699 /* Time for performance */
2700 if (((step
% stepout
) == 0) || bLastStep
)
2702 runtime_upd_proc(runtime
);
2708 gmx_bool do_dr
,do_or
;
2710 if (!(bStartingFromCpt
&& (EI_VV(ir
->eI
))))
2714 upd_mdebin(mdebin
,bDoDHDL
, TRUE
,
2715 t
,mdatoms
->tmass
,enerd
,state
,lastbox
,
2716 shake_vir
,force_vir
,total_vir
,pres
,
2717 ekind
,mu_tot
,constr
);
2721 upd_mdebin_step(mdebin
);
2724 do_dr
= do_per_step(step
,ir
->nstdisreout
);
2725 do_or
= do_per_step(step
,ir
->nstorireout
);
2727 print_ebin(outf
->fp_ene
,do_ene
,do_dr
,do_or
,do_log
?fplog
:NULL
,
2729 eprNORMAL
,bCompact
,mdebin
,fcd
,groups
,&(ir
->opts
));
2731 if (ir
->ePull
!= epullNO
)
2733 pull_print_output(ir
->pull
,step
,t
);
2736 if (do_per_step(step
,ir
->nstlog
))
2738 if(fflush(fplog
) != 0)
2740 gmx_fatal(FARGS
,"Cannot flush logfile - maybe you are out of quota?");
2746 /* Remaining runtime */
2747 if (MULTIMASTER(cr
) && (do_verbose
|| gmx_got_usr_signal() ))
2751 fprintf(stderr
,"\n");
2753 print_time(stderr
,runtime
,step
,ir
,cr
);
2756 /* Replica exchange */
2758 if ((repl_ex_nst
> 0) && (step
> 0) && !bLastStep
&&
2759 do_per_step(step
,repl_ex_nst
))
2761 bExchanged
= replica_exchange(fplog
,cr
,repl_ex
,
2762 state_global
,enerd
->term
,
2765 if (bExchanged
&& DOMAINDECOMP(cr
))
2767 dd_partition_system(fplog
,step
,cr
,TRUE
,1,
2768 state_global
,top_global
,ir
,
2769 state
,&f
,mdatoms
,top
,fr
,
2770 vsite
,shellfc
,constr
,
2777 bStartingFromCpt
= FALSE
;
2779 /* ####### SET VARIABLES FOR NEXT ITERATION IF THEY STILL NEED IT ###### */
2780 /* With all integrators, except VV, we need to retain the pressure
2781 * at the current step for coupling at the next step.
2783 if ((state
->flags
& (1<<estPRES_PREV
)) &&
2785 (ir
->nstpcouple
> 0 && step
% ir
->nstpcouple
== 0)))
2787 /* Store the pressure in t_state for pressure coupling
2788 * at the next MD step.
2790 copy_mat(pres
,state
->pres_prev
);
2793 /* ####### END SET VARIABLES FOR NEXT ITERATION ###### */
2799 /* read next frame from input trajectory */
2800 bNotLastFrame
= read_next_frame(oenv
,status
,&rerun_fr
);
2805 rerun_parallel_comm(cr
,&rerun_fr
,&bNotLastFrame
);
2809 if (!bRerunMD
|| !rerun_fr
.bStep
)
2811 /* increase the MD step number */
2816 cycles
= wallcycle_stop(wcycle
,ewcSTEP
);
2817 if (DOMAINDECOMP(cr
) && wcycle
)
2819 dd_cycles_add(cr
->dd
,cycles
,ddCyclStep
);
2822 if (step_rel
== wcycle_get_reset_counters(wcycle
) ||
2823 gs
.set
[eglsRESETCOUNTERS
] != 0)
2825 /* Reset all the counters related to performance over the run */
2826 reset_all_counters(fplog
,cr
,step
,&step_rel
,ir
,wcycle
,nrnb
,runtime
);
2827 wcycle_set_reset_counters(wcycle
,-1);
2828 /* Correct max_hours for the elapsed time */
2829 max_hours
-= run_time
/(60.0*60.0);
2830 bResetCountersHalfMaxH
= FALSE
;
2831 gs
.set
[eglsRESETCOUNTERS
] = 0;
2835 /* End of main MD loop */
2839 runtime_end(runtime
);
2841 if (bRerunMD
&& MASTER(cr
))
2846 if (!(cr
->duty
& DUTY_PME
))
2848 /* Tell the PME only node to finish */
2854 if (ir
->nstcalcenergy
> 0 && !bRerunMD
)
2856 print_ebin(outf
->fp_ene
,FALSE
,FALSE
,FALSE
,fplog
,step
,t
,
2857 eprAVER
,FALSE
,mdebin
,fcd
,groups
,&(ir
->opts
));
2865 if (ir
->nstlist
== -1 && nlh
.nns
> 0 && fplog
)
2867 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
)));
2868 fprintf(fplog
,"Average number of atoms that crossed the half buffer length: %.1f\n\n",nlh
.ab
/nlh
.nns
);
2871 if (shellfc
&& fplog
)
2873 fprintf(fplog
,"Fraction of iterations that converged: %.2f %%\n",
2874 (nconverged
*100.0)/step_rel
);
2875 fprintf(fplog
,"Average number of force evaluations per MD step: %.2f\n\n",
2879 if (repl_ex_nst
> 0 && MASTER(cr
))
2881 print_replica_exchange_statistics(fplog
,repl_ex
);
2884 runtime
->nsteps_done
= step_rel
;