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"
100 #include "corewrap.h"
105 /* The following two variables and the signal_handler function
106 * is used from pme.c as well
108 extern bool bGotTermSignal
, bGotUsr1Signal
;
110 static RETSIGTYPE
signal_handler(int n
)
114 bGotTermSignal
= TRUE
;
118 bGotUsr1Signal
= TRUE
;
125 gmx_integrator_t
*func
;
128 /* The array should match the eI array in include/types/enums.h */
129 const gmx_intp_t integrator
[eiNR
] = { {do_md
}, {do_steep
}, {do_cg
}, {do_md
}, {do_md
}, {do_nm
}, {do_lbfgs
}, {do_tpi
}, {do_tpi
}, {do_md
} };
131 /* Static variables for temporary use with the deform option */
132 static int init_step_tpx
;
133 static matrix box_tpx
;
135 static tMPI_Thread_mutex_t box_mutex
=TMPI_THREAD_MUTEX_INITIALIZER
;
140 struct mdrunner_arglist
154 const char *dddlb_opt
;
168 int ret
; /* return value */
172 static void mdrunner_start_fn(void *arg
)
174 struct mdrunner_arglist
*mda
=(struct mdrunner_arglist
*)arg
;
175 struct mdrunner_arglist mc
=*mda
; /* copy the arg list to make sure
176 that it's thread-local. This doesn't
177 copy pointed-to items, of course,
178 but those are all const. */
179 t_commrec
*cr
; /* we need a local version of this */
181 t_filenm
*fnm
=dup_tfn(mc
.nfile
, mc
.fnm
);
183 cr
=init_par_threads(mc
.cr
);
190 mda
->ret
=mdrunner(fplog
, cr
, mc
.nfile
, mc
.fnm
, mc
.oenv
, mc
.bVerbose
,
191 mc
.bCompact
, mc
.nstglobalcomm
,
192 mc
.ddxyz
, mc
.dd_node_order
, mc
.rdd
,
193 mc
.rconstr
, mc
.dddlb_opt
, mc
.dlb_scale
,
194 mc
.ddcsx
, mc
.ddcsy
, mc
.ddcsz
, mc
.nstepout
, mc
.resetstep
, mc
.nmultisim
,
195 mc
.repl_ex_nst
, mc
.repl_ex_seed
, mc
.pforce
,
196 mc
.cpt_period
, mc
.max_hours
, mc
.Flags
);
201 int mdrunner_threads(int nthreads
,
202 FILE *fplog
,t_commrec
*cr
,int nfile
,const t_filenm fnm
[],
203 const output_env_t oenv
, bool bVerbose
,bool bCompact
,
205 ivec ddxyz
,int dd_node_order
,real rdd
,real rconstr
,
206 const char *dddlb_opt
,real dlb_scale
,
207 const char *ddcsx
,const char *ddcsy
,const char *ddcsz
,
208 int nstepout
,int resetstep
,int nmultisim
,int repl_ex_nst
,
209 int repl_ex_seed
, real pforce
,real cpt_period
,
210 real max_hours
, unsigned long Flags
)
213 /* first check whether we even need to start tMPI */
216 ret
=mdrunner(fplog
, cr
, nfile
, fnm
, oenv
, bVerbose
, bCompact
,
218 ddxyz
, dd_node_order
, rdd
, rconstr
, dddlb_opt
, dlb_scale
,
219 ddcsx
, ddcsy
, ddcsz
, nstepout
, resetstep
, nmultisim
, repl_ex_nst
,
220 repl_ex_seed
, pforce
, cpt_period
, max_hours
, Flags
);
225 struct mdrunner_arglist mda
;
226 /* fill the data structure to pass as void pointer to thread start fn */
232 mda
.bVerbose
=bVerbose
;
233 mda
.bCompact
=bCompact
;
234 mda
.nstglobalcomm
=nstglobalcomm
;
235 mda
.ddxyz
[XX
]=ddxyz
[XX
];
236 mda
.ddxyz
[YY
]=ddxyz
[YY
];
237 mda
.ddxyz
[ZZ
]=ddxyz
[ZZ
];
238 mda
.dd_node_order
=dd_node_order
;
241 mda
.dddlb_opt
=dddlb_opt
;
242 mda
.dlb_scale
=dlb_scale
;
246 mda
.nstepout
=nstepout
;
247 mda
.resetstep
=resetstep
;
248 mda
.nmultisim
=nmultisim
;
249 mda
.repl_ex_nst
=repl_ex_nst
;
250 mda
.repl_ex_seed
=repl_ex_seed
;
252 mda
.cpt_period
=cpt_period
;
253 mda
.max_hours
=max_hours
;
256 fprintf(stderr
, "Starting %d threads\n",nthreads
);
258 tMPI_Init_fn(nthreads
, mdrunner_start_fn
, (void*)(&mda
) );
262 gmx_comm("Multiple threads requested but not compiled with threads");
269 int mdrunner(FILE *fplog
,t_commrec
*cr
,int nfile
,const t_filenm fnm
[],
270 const output_env_t oenv
, bool bVerbose
,bool bCompact
,
272 ivec ddxyz
,int dd_node_order
,real rdd
,real rconstr
,
273 const char *dddlb_opt
,real dlb_scale
,
274 const char *ddcsx
,const char *ddcsy
,const char *ddcsz
,
275 int nstepout
,int resetstep
,int nmultisim
,int repl_ex_nst
,int repl_ex_seed
,
276 real pforce
,real cpt_period
,real max_hours
,
279 double nodetime
=0,realtime
;
280 t_inputrec
*inputrec
;
287 gmx_mtop_t
*mtop
=NULL
;
288 t_mdatoms
*mdatoms
=NULL
;
292 gmx_pme_t
*pmedata
=NULL
;
293 gmx_vsite_t
*vsite
=NULL
;
295 int i
,m
,nChargePerturbed
=-1,status
,nalloc
;
297 gmx_wallcycle_t wcycle
;
298 bool bReadRNG
,bReadEkin
;
300 gmx_runtime_t runtime
;
302 gmx_large_int_t reset_counters
;
305 /* Essential dynamics */
306 if (opt2bSet("-ei",nfile
,fnm
))
308 /* Open input and output files, allocate space for ED data structure */
309 ed
= ed_open(nfile
,fnm
,cr
);
317 if (bVerbose
&& SIMMASTER(cr
))
319 fprintf(stderr
,"Getting Loaded...\n");
322 if (Flags
& MD_APPENDFILES
)
329 /* The master thread on the master node reads from disk,
330 * then distributes everything to the other processors.
333 list
= (SIMMASTER(cr
) && !(Flags
& MD_APPENDFILES
)) ? (LIST_SCALARS
| LIST_INPUTREC
) : 0;
336 init_parallel(fplog
, opt2fn_master("-s",nfile
,fnm
,cr
),cr
,
337 inputrec
,mtop
,state
,list
);
342 /* Read a file for a single processor */
344 init_single(fplog
,inputrec
,ftp2fn(efTPX
,nfile
,fnm
),mtop
,state
);
346 if (!EEL_PME(inputrec
->coulombtype
) || (Flags
& MD_PARTDEC
))
352 fcRegisterSteps(inputrec
->nsteps
,inputrec
->init_step
);
355 /* NMR restraints must be initialized before load_checkpoint,
356 * since with time averaging the history is added to t_state.
357 * For proper consistency check we therefore need to extend
359 * So the PME-only nodes (if present) will also initialize
360 * the distance restraints.
364 /* This needs to be called before read_checkpoint to extend the state */
365 init_disres(fplog
,mtop
,inputrec
,cr
,Flags
& MD_PARTDEC
,fcd
,state
);
367 if (gmx_mtop_ftype_count(mtop
,F_ORIRES
) > 0)
369 if (PAR(cr
) && !(Flags
& MD_PARTDEC
))
371 gmx_fatal(FARGS
,"Orientation restraints do not work (yet) with domain decomposition, use particle decomposition (mdrun option -pd)");
373 /* Orientation restraints */
376 init_orires(fplog
,mtop
,state
->x
,inputrec
,cr
->ms
,&(fcd
->orires
),
381 if (DEFORM(*inputrec
))
383 /* Store the deform reference box before reading the checkpoint */
386 copy_mat(state
->box
,box
);
390 gmx_bcast(sizeof(box
),box
,cr
);
392 /* Because we do not have the update struct available yet
393 * in which the reference values should be stored,
394 * we store them temporarily in static variables.
395 * This should be thread safe, since they are only written once
396 * and with identical values.
399 tMPI_Thread_mutex_lock(&box_mutex
);
401 init_step_tpx
= inputrec
->init_step
;
402 copy_mat(box
,box_tpx
);
404 tMPI_Thread_mutex_unlock(&box_mutex
);
408 if (opt2bSet("-cpi",nfile
,fnm
))
410 /* Check if checkpoint file exists before doing continuation.
411 * This way we can use identical input options for the first and subsequent runs...
413 if( gmx_fexist_master(opt2fn_master("-cpi",nfile
,fnm
,cr
),cr
) )
415 load_checkpoint(opt2fn_master("-cpi",nfile
,fnm
,cr
),fplog
,
416 cr
,Flags
& MD_PARTDEC
,ddxyz
,
417 inputrec
,state
,&bReadRNG
,&bReadEkin
,
418 (Flags
& MD_APPENDFILES
));
422 Flags
|= MD_READ_RNG
;
426 Flags
|= MD_READ_EKIN
;
431 if (MASTER(cr
) && (Flags
& MD_APPENDFILES
))
433 fplog
= gmx_log_open(ftp2fn(efLOG
,nfile
,fnm
),cr
,!(Flags
& MD_SEPPOT
),
439 copy_mat(state
->box
,box
);
444 gmx_bcast(sizeof(box
),box
,cr
);
447 if (bVerbose
&& SIMMASTER(cr
))
449 fprintf(stderr
,"Loaded with Money\n\n");
452 if (PAR(cr
) && !((Flags
& MD_PARTDEC
) || EI_TPI(inputrec
->eI
)))
454 cr
->dd
= init_domain_decomposition(fplog
,cr
,Flags
,ddxyz
,rdd
,rconstr
,
461 make_dd_communicators(fplog
,cr
,dd_node_order
);
463 /* Set overallocation to avoid frequent reallocation of arrays */
464 set_over_alloc_dd(TRUE
);
468 cr
->duty
= (DUTY_PP
| DUTY_PME
);
469 npme_major
= cr
->nnodes
;
471 if (inputrec
->ePBC
== epbcSCREW
)
474 "pbc=%s is only implemented with domain decomposition",
475 epbc_names
[inputrec
->ePBC
]);
481 /* After possible communicator splitting in make_dd_communicators.
482 * we can set up the intra/inter node communication.
484 gmx_setup_nodecomm(fplog
,cr
);
487 wcycle
= wallcycle_init(fplog
,resetstep
,cr
);
490 /* Master synchronizes its value of reset_counters with all nodes
491 * including PME only nodes */
492 reset_counters
= wcycle_get_reset_counters(wcycle
);
493 gmx_bcast_sim(sizeof(reset_counters
),&reset_counters
,cr
);
494 wcycle_set_reset_counters(wcycle
, reset_counters
);
499 if (cr
->duty
& DUTY_PP
)
501 /* For domain decomposition we allocate dynamically
502 * in dd_partition_system.
504 if (DOMAINDECOMP(cr
))
506 bcast_state_setup(cr
,state
);
516 bcast_state(cr
,state
,TRUE
);
520 /* Dihedral Restraints */
521 if (gmx_mtop_ftype_count(mtop
,F_DIHRES
) > 0)
523 init_dihres(fplog
,mtop
,inputrec
,fcd
);
526 /* Initiate forcerecord */
528 init_forcerec(fplog
,oenv
,fr
,fcd
,inputrec
,mtop
,cr
,box
,FALSE
,
529 opt2fn("-table",nfile
,fnm
),
530 opt2fn("-tablep",nfile
,fnm
),
531 opt2fn("-tableb",nfile
,fnm
),FALSE
,pforce
);
533 /* version for PCA_NOT_READ_NODE (see md.c) */
534 /*init_forcerec(fplog,fr,fcd,inputrec,mtop,cr,box,FALSE,
535 "nofile","nofile","nofile",FALSE,pforce);
537 fr
->bSepDVDL
= ((Flags
& MD_SEPPOT
) == MD_SEPPOT
);
539 /* Initialize QM-MM */
542 init_QMMMrec(cr
,box
,mtop
,inputrec
,fr
);
545 /* Initialize the mdatoms structure.
546 * mdatoms is not filled with atom data,
547 * as this can not be done now with domain decomposition.
549 mdatoms
= init_mdatoms(fplog
,mtop
,inputrec
->efep
!=efepNO
);
551 /* Initialize the virtual site communication */
552 vsite
= init_vsite(mtop
,cr
);
554 calc_shifts(box
,fr
->shift_vec
);
556 /* With periodic molecules the charge groups should be whole at start up
557 * and the virtual sites should not be far from their proper positions.
559 if (!inputrec
->bContinuation
&& MASTER(cr
) &&
560 !(inputrec
->ePBC
!= epbcNONE
&& inputrec
->bPeriodicMols
))
562 /* Make molecules whole at start of run */
563 if (fr
->ePBC
!= epbcNONE
)
565 do_pbc_first_mtop(fplog
,inputrec
->ePBC
,box
,mtop
,state
->x
);
569 /* Correct initial vsite positions are required
570 * for the initial distribution in the domain decomposition
571 * and for the initial shell prediction.
573 construct_vsites_mtop(fplog
,vsite
,mtop
,state
->x
);
577 /* Initiate PPPM if necessary */
578 if (fr
->eeltype
== eelPPPM
)
580 if (mdatoms
->nChargePerturbed
)
582 gmx_fatal(FARGS
,"Free energy with %s is not implemented",
583 eel_names
[fr
->eeltype
]);
585 status
= gmx_pppm_init(fplog
,cr
,oenv
,FALSE
,TRUE
,box
,
586 getenv("GMXGHAT"),inputrec
, (Flags
& MD_REPRODUCIBLE
));
589 gmx_fatal(FARGS
,"Error %d initializing PPPM",status
);
593 if (EEL_PME(fr
->eeltype
))
595 ewaldcoeff
= fr
->ewaldcoeff
;
596 pmedata
= &fr
->pmedata
;
605 /* This is a PME only node */
607 /* We don't need the state */
610 ewaldcoeff
= calc_ewaldcoeff(inputrec
->rcoulomb
, inputrec
->ewald_rtol
);
614 /* Initiate PME if necessary,
615 * either on all nodes or on dedicated PME nodes only. */
616 if (EEL_PME(inputrec
->coulombtype
))
620 nChargePerturbed
= mdatoms
->nChargePerturbed
;
622 if (cr
->npmenodes
> 0)
624 /* The PME only nodes need to know nChargePerturbed */
625 gmx_bcast_sim(sizeof(nChargePerturbed
),&nChargePerturbed
,cr
);
627 if (cr
->duty
& DUTY_PME
)
629 status
= gmx_pme_init(pmedata
,cr
,npme_major
,inputrec
,
630 mtop
? mtop
->natoms
: 0,nChargePerturbed
,
631 (Flags
& MD_REPRODUCIBLE
));
633 gmx_fatal(FARGS
,"Error %d initializing PME",status
);
638 if (integrator
[inputrec
->eI
].func
== do_md
)
640 /* Turn on signal handling on all nodes */
642 * (A user signal from the PME nodes (if any)
643 * is communicated to the PP nodes.
645 if (getenv("GMX_NO_TERM") == NULL
)
649 fprintf(debug
,"Installing signal handler for SIGTERM\n");
651 signal(SIGTERM
,signal_handler
);
654 if (getenv("GMX_NO_USR1") == NULL
)
658 fprintf(debug
,"Installing signal handler for SIGUSR1\n");
660 signal(SIGUSR1
,signal_handler
);
665 if (cr
->duty
& DUTY_PP
)
667 if (inputrec
->ePull
!= epullNO
)
669 /* Initialize pull code */
670 init_pull(fplog
,inputrec
,nfile
,fnm
,mtop
,cr
,oenv
,
671 EI_DYNAMICS(inputrec
->eI
) && MASTER(cr
),Flags
);
674 constr
= init_constraints(fplog
,mtop
,inputrec
,ed
,state
,cr
);
676 if (DOMAINDECOMP(cr
))
678 dd_init_bondeds(fplog
,cr
->dd
,mtop
,vsite
,constr
,inputrec
,
679 Flags
& MD_DDBONDCHECK
,fr
->cginfo_mb
);
681 set_dd_parameters(fplog
,cr
->dd
,dlb_scale
,inputrec
,fr
,&ddbox
);
683 setup_dd_grid(fplog
,cr
->dd
);
686 /* Now do whatever the user wants us to do (how flexible...) */
687 integrator
[inputrec
->eI
].func(fplog
,cr
,nfile
,fnm
,
688 oenv
,bVerbose
,bCompact
,
691 nstepout
,inputrec
,mtop
,
693 mdatoms
,nrnb
,wcycle
,ed
,fr
,
694 repl_ex_nst
,repl_ex_seed
,
695 cpt_period
,max_hours
,
699 if (inputrec
->ePull
!= epullNO
)
701 finish_pull(fplog
,inputrec
->pull
);
707 gmx_pmeonly(*pmedata
,cr
,nrnb
,wcycle
,ewaldcoeff
,FALSE
,inputrec
);
710 if (EI_DYNAMICS(inputrec
->eI
) || EI_TPI(inputrec
->eI
))
712 /* Some timing stats */
715 if (runtime
.proc
== 0)
717 runtime
.proc
= runtime
.real
;
726 wallcycle_stop(wcycle
,ewcRUN
);
728 /* Finish up, write some stuff
729 * if rerunMD, don't write last frame again
731 finish_run(fplog
,cr
,ftp2fn(efSTO
,nfile
,fnm
),
732 inputrec
,nrnb
,wcycle
,&runtime
,
733 EI_DYNAMICS(inputrec
->eI
) && !MULTISIM(cr
));
735 /* Does what it says */
736 print_date_and_time(fplog
,cr
->nodeid
,"Finished mdrun",&runtime
);
738 /* Close logfile already here if we were appending to it */
739 if (MASTER(cr
) && (Flags
& MD_APPENDFILES
))
741 gmx_log_close(fplog
);
748 else if(bGotUsr1Signal
)
760 static void md_print_warning(const t_commrec
*cr
,FILE *fplog
,const char *buf
)
764 fprintf(stderr
,"\n%s\n",buf
);
768 fprintf(fplog
,"\n%s\n",buf
);
772 static void check_nst_param(FILE *fplog
,t_commrec
*cr
,
773 const char *desc_nst
,int nst
,
774 const char *desc_p
,int *p
)
778 if (*p
> 0 && *p
% nst
!= 0)
780 /* Round up to the next multiple of nst */
781 *p
= ((*p
)/nst
+ 1)*nst
;
782 sprintf(buf
,"NOTE: %s changes %s to %d\n",desc_nst
,desc_p
,*p
);
783 md_print_warning(cr
,fplog
,buf
);
787 static void reset_all_counters(FILE *fplog
,t_commrec
*cr
,
788 gmx_large_int_t step
,
789 gmx_large_int_t
*step_rel
,t_inputrec
*ir
,
790 gmx_wallcycle_t wcycle
,t_nrnb
*nrnb
,
791 gmx_runtime_t
*runtime
)
793 char buf
[STRLEN
],sbuf
[22];
795 /* Reset all the counters related to performance over the run */
796 sprintf(buf
,"Step %s: resetting all time and cycle counters\n",
797 gmx_step_str(step
,sbuf
));
798 md_print_warning(cr
,fplog
,buf
);
800 wallcycle_stop(wcycle
,ewcRUN
);
801 wallcycle_reset_all(wcycle
);
802 if (DOMAINDECOMP(cr
))
804 reset_dd_statistics_counters(cr
->dd
);
807 ir
->init_step
+= *step_rel
;
808 ir
->nsteps
-= *step_rel
;
810 wallcycle_start(wcycle
,ewcRUN
);
811 runtime_start(runtime
);
812 print_date_and_time(fplog
,cr
->nodeid
,"Restarted time",runtime
);
815 static int check_nstglobalcomm(FILE *fplog
,t_commrec
*cr
,
816 int nstglobalcomm
,t_inputrec
*ir
)
820 if (!EI_DYNAMICS(ir
->eI
))
825 if (nstglobalcomm
== -1)
827 if (ir
->nstcalcenergy
== 0 && ir
->nstlist
== 0)
830 if (ir
->nstenergy
> 0 && ir
->nstenergy
< nstglobalcomm
)
832 nstglobalcomm
= ir
->nstenergy
;
837 /* We assume that if nstcalcenergy > nstlist,
838 * nstcalcenergy is a multiple of nstlist.
840 if (ir
->nstcalcenergy
== 0 ||
841 (ir
->nstlist
> 0 && ir
->nstlist
< ir
->nstcalcenergy
))
843 nstglobalcomm
= ir
->nstlist
;
847 nstglobalcomm
= ir
->nstcalcenergy
;
853 if (ir
->nstlist
> 0 &&
854 nstglobalcomm
> ir
->nstlist
&& nstglobalcomm
% ir
->nstlist
!= 0)
856 nstglobalcomm
= (nstglobalcomm
/ ir
->nstlist
)*ir
->nstlist
;
857 sprintf(buf
,"WARNING: nstglobalcomm is larger than nstlist, but not a multiple, setting it to %d\n",nstglobalcomm
);
858 md_print_warning(cr
,fplog
,buf
);
860 if (nstglobalcomm
> ir
->nstcalcenergy
)
862 check_nst_param(fplog
,cr
,"-gcom",nstglobalcomm
,
863 "nstcalcenergy",&ir
->nstcalcenergy
);
866 check_nst_param(fplog
,cr
,"-gcom",nstglobalcomm
,
867 "nstenergy",&ir
->nstenergy
);
869 check_nst_param(fplog
,cr
,"-gcom",nstglobalcomm
,
870 "nstlog",&ir
->nstlog
);
873 if (ir
->comm_mode
!= ecmNO
&& ir
->nstcomm
< nstglobalcomm
)
875 sprintf(buf
,"WARNING: Changing nstcomm from %d to %d\n",
876 ir
->nstcomm
,nstglobalcomm
);
877 md_print_warning(cr
,fplog
,buf
);
878 ir
->nstcomm
= nstglobalcomm
;
881 return nstglobalcomm
;
884 static void check_ir_old_tpx_versions(t_commrec
*cr
,FILE *fplog
,
885 t_inputrec
*ir
,gmx_mtop_t
*mtop
)
887 /* Check required for old tpx files */
888 if (IR_TWINRANGE(*ir
) && ir
->nstlist
> 1 &&
889 ir
->nstcalcenergy
% ir
->nstlist
!= 0)
891 md_print_warning(cr
,fplog
,"Old tpr file with twin-range settings: modifiying energy calculation and/or T/P-coupling frequencies");
893 if (gmx_mtop_ftype_count(mtop
,F_CONSTR
) +
894 gmx_mtop_ftype_count(mtop
,F_CONSTRNC
) > 0 &&
895 ir
->eConstrAlg
== econtSHAKE
)
897 md_print_warning(cr
,fplog
,"With twin-range cut-off's and SHAKE the virial and pressure are incorrect");
898 if (ir
->epc
!= epcNO
)
900 gmx_fatal(FARGS
,"Can not do pressure coupling with twin-range cut-off's and SHAKE");
903 check_nst_param(fplog
,cr
,"nstlist",ir
->nstlist
,
904 "nstcalcenergy",&ir
->nstcalcenergy
);
906 check_nst_param(fplog
,cr
,"nstcalcenergy",ir
->nstcalcenergy
,
907 "nstenergy",&ir
->nstenergy
);
908 check_nst_param(fplog
,cr
,"nstcalcenergy",ir
->nstcalcenergy
,
909 "nstlog",&ir
->nstlog
);
910 if (ir
->efep
!= efepNO
)
912 check_nst_param(fplog
,cr
,"nstdhdl",ir
->nstdhdl
,
913 "nstenergy",&ir
->nstenergy
);
918 bool bGStatEveryStep
;
919 gmx_large_int_t step_ns
;
920 gmx_large_int_t step_nscheck
;
931 static void reset_nlistheuristics(gmx_nlheur_t
*nlh
,gmx_large_int_t step
)
935 nlh
->step_nscheck
= step
;
938 static void init_nlistheuristics(gmx_nlheur_t
*nlh
,
939 bool bGStatEveryStep
,gmx_large_int_t step
)
941 nlh
->bGStatEveryStep
= bGStatEveryStep
;
948 reset_nlistheuristics(nlh
,step
);
951 static void update_nliststatistics(gmx_nlheur_t
*nlh
,gmx_large_int_t step
)
953 gmx_large_int_t nl_lt
;
954 char sbuf
[22],sbuf2
[22];
956 /* Determine the neighbor list life time */
957 nl_lt
= step
- nlh
->step_ns
;
960 fprintf(debug
,"%d atoms beyond ns buffer, updating neighbor list after %s steps\n",nlh
->nabnsb
,gmx_step_str(nl_lt
,sbuf
));
964 nlh
->s2
+= nl_lt
*nl_lt
;
965 nlh
->ab
+= nlh
->nabnsb
;
966 if (nlh
->lt_runav
== 0)
968 nlh
->lt_runav
= nl_lt
;
969 /* Initialize the fluctuation average
970 * such that at startup we check after 0 steps.
972 nlh
->lt_runav2
= sqr(nl_lt
/2.0);
974 /* Running average with 0.9 gives an exp. history of 9.5 */
975 nlh
->lt_runav2
= 0.9*nlh
->lt_runav2
+ 0.1*sqr(nlh
->lt_runav
- nl_lt
);
976 nlh
->lt_runav
= 0.9*nlh
->lt_runav
+ 0.1*nl_lt
;
977 if (nlh
->bGStatEveryStep
)
979 /* Always check the nlist validity */
980 nlh
->step_nscheck
= step
;
984 /* We check after: <life time> - 2*sigma
985 * The factor 2 is quite conservative,
986 * but we assume that with nstlist=-1 the user
987 * prefers exact integration over performance.
989 nlh
->step_nscheck
= step
990 + (int)(nlh
->lt_runav
- 2.0*sqrt(nlh
->lt_runav2
)) - 1;
994 fprintf(debug
,"nlist life time %s run av. %4.1f sig %3.1f check %s check with -gcom %d\n",
995 gmx_step_str(nl_lt
,sbuf
),nlh
->lt_runav
,sqrt(nlh
->lt_runav2
),
996 gmx_step_str(nlh
->step_nscheck
-step
+1,sbuf2
),
997 (int)(nlh
->lt_runav
- 2.0*sqrt(nlh
->lt_runav2
)));
1001 static void set_nlistheuristics(gmx_nlheur_t
*nlh
,bool bReset
,gmx_large_int_t step
)
1007 reset_nlistheuristics(nlh
,step
);
1011 update_nliststatistics(nlh
,step
);
1014 nlh
->step_ns
= step
;
1015 /* Initialize the cumulative coordinate scaling matrix */
1016 clear_mat(nlh
->scale_tot
);
1017 for(d
=0; d
<DIM
; d
++)
1019 nlh
->scale_tot
[d
][d
] = 1.0;
1023 double do_md(FILE *fplog
,t_commrec
*cr
,int nfile
,const t_filenm fnm
[],
1024 const output_env_t oenv
, bool bVerbose
,bool bCompact
,
1026 gmx_vsite_t
*vsite
,gmx_constr_t constr
,
1027 int stepout
,t_inputrec
*ir
,
1028 gmx_mtop_t
*top_global
,
1030 t_state
*state_global
,
1032 t_nrnb
*nrnb
,gmx_wallcycle_t wcycle
,
1033 gmx_edsam_t ed
,t_forcerec
*fr
,
1034 int repl_ex_nst
,int repl_ex_seed
,
1035 real cpt_period
,real max_hours
,
1036 unsigned long Flags
,
1037 gmx_runtime_t
*runtime
)
1039 int fp_trn
=0,fp_xtc
=0;
1040 ener_file_t fp_ene
=NULL
;
1041 gmx_large_int_t step
,step_rel
;
1043 FILE *fp_dhdl
=NULL
,*fp_field
=NULL
;
1046 bool bGStatEveryStep
,bGStat
,bNstEner
,bCalcEner
;
1047 bool bNS
,bNStList
,bSimAnn
,bStopCM
,bRerunMD
,bNotLastFrame
=FALSE
,
1048 bFirstStep
,bStateFromTPX
,bLastStep
,bBornRadii
;
1050 bool bNEMD
,do_ene
,do_log
,do_verbose
,bRerunWarnNoV
=TRUE
,
1051 bForceUpdate
=FALSE
,bX
,bV
,bF
,bXTC
,bCPT
;
1054 tensor force_vir
,shake_vir
,total_vir
,pres
,ekin
;
1059 t_trxframe rerun_fr
;
1060 gmx_repl_ex_t repl_ex
=NULL
;
1062 /* Booleans (disguised as a reals) to checkpoint and terminate mdrun */
1063 real chkpt
=0,terminate
=0,terminate_now
=0;
1065 gmx_localtop_t
*top
;
1066 t_mdebin
*mdebin
=NULL
;
1067 t_state
*state
=NULL
;
1068 rvec
*f_global
=NULL
;
1071 gmx_enerdata_t
*enerd
;
1073 gmx_global_stat_t gstat
;
1074 gmx_update_t upd
=NULL
;
1075 t_graph
*graph
=NULL
;
1078 gmx_groups_t
*groups
;
1079 gmx_ekindata_t
*ekind
;
1080 gmx_shellfc_t shellfc
;
1081 int count
,nconverged
=0;
1085 bool bTCR
=FALSE
,bConverged
=TRUE
,bOK
,bSumEkinhOld
,bExchanged
;
1087 real temp0
,mu_aver
=0,dvdl
;
1089 atom_id
*grpindex
=NULL
;
1091 t_coupl_rec
*tcr
=NULL
;
1092 rvec
*xcopy
=NULL
,*vcopy
=NULL
;
1093 matrix boxcopy
,lastbox
;
1095 int reset_counters
=-1;
1096 char sbuf
[22],sbuf2
[22];
1097 bool bHandledSignal
=FALSE
;
1099 /* Temporary addition for FAHCORE checkpointing */
1104 /* Check for special mdrun options */
1105 bRerunMD
= (Flags
& MD_RERUN
);
1106 bIonize
= (Flags
& MD_IONIZE
);
1107 bFFscan
= (Flags
& MD_FFSCAN
);
1108 bAppend
= (Flags
& MD_APPENDFILES
);
1112 /* Since we don't know if the frames read are related in any way,
1113 * rebuild the neighborlist at every step.
1116 ir
->nstcalcenergy
= 1;
1120 check_ir_old_tpx_versions(cr
,fplog
,ir
,top_global
);
1122 nstglobalcomm
= check_nstglobalcomm(fplog
,cr
,nstglobalcomm
,ir
);
1123 bGStatEveryStep
= (nstglobalcomm
== 1);
1125 if (!bGStatEveryStep
&& ir
->nstlist
== -1 && fplog
!= NULL
)
1128 "To reduce the energy communication with nstlist = -1\n"
1129 "the neighbor list validity should not be checked at every step,\n"
1130 "this means that exact integration is not guaranteed.\n"
1131 "The neighbor list validity is checked after:\n"
1132 " <n.list life time> - 2*std.dev.(n.list life time) steps.\n"
1133 "In most cases this will result in exact integration.\n"
1134 "This reduces the energy communication by a factor of 2 to 3.\n"
1135 "If you want less energy communication, set nstlist > 3.\n\n");
1138 if (bRerunMD
|| bFFscan
)
1142 groups
= &top_global
->groups
;
1144 /* Initial values */
1145 init_md(fplog
,cr
,ir
,oenv
,&t
,&t0
,&state_global
->lambda
,&lam0
,
1146 nrnb
,top_global
,&upd
,
1147 nfile
,fnm
,&fp_trn
,&fp_xtc
,&fp_ene
,&fn_cpt
,
1148 &fp_dhdl
,&fp_field
,&mdebin
,
1149 force_vir
,shake_vir
,mu_tot
,&bNEMD
,&bSimAnn
,&vcm
,Flags
);
1151 /* Energy terms and groups */
1153 init_enerdata(top_global
->groups
.grps
[egcENER
].nr
,ir
->n_flambda
,enerd
);
1154 if (DOMAINDECOMP(cr
))
1160 snew(f
,top_global
->natoms
);
1163 /* Kinetic energy data */
1165 init_ekindata(fplog
,top_global
,&(ir
->opts
),ekind
);
1166 /* Copy the cos acceleration to the groups struct */
1167 ekind
->cosacc
.cos_accel
= ir
->cos_accel
;
1169 gstat
= global_stat_init(ir
);
1172 /* Check for polarizable models and flexible constraints */
1173 shellfc
= init_shell_flexcon(fplog
,
1174 top_global
,n_flexible_constraints(constr
),
1175 (ir
->bContinuation
||
1176 (DOMAINDECOMP(cr
) && !MASTER(cr
))) ?
1177 NULL
: state_global
->x
);
1182 tMPI_Thread_mutex_lock(&box_mutex
);
1184 set_deform_reference_box(upd
,init_step_tpx
,box_tpx
);
1186 tMPI_Thread_mutex_unlock(&box_mutex
);
1191 double io
= compute_io(ir
,top_global
->natoms
,groups
,mdebin
->ebin
->nener
,1);
1192 if ((io
> 2000) && MASTER(cr
))
1194 "\nWARNING: This run will generate roughly %.0f Mb of data\n\n",
1198 if (DOMAINDECOMP(cr
)) {
1199 top
= dd_init_local_top(top_global
);
1202 dd_init_local_state(cr
->dd
,state_global
,state
);
1204 if (DDMASTER(cr
->dd
) && ir
->nstfout
) {
1205 snew(f_global
,state_global
->natoms
);
1209 /* Initialize the particle decomposition and split the topology */
1210 top
= split_system(fplog
,top_global
,ir
,cr
);
1212 pd_cg_range(cr
,&fr
->cg0
,&fr
->hcg
);
1213 pd_at_range(cr
,&a0
,&a1
);
1215 top
= gmx_mtop_generate_local_top(top_global
,ir
);
1218 a1
= top_global
->natoms
;
1221 state
= partdec_init_local_state(cr
,state_global
);
1224 atoms2md(top_global
,ir
,0,NULL
,a0
,a1
-a0
,mdatoms
);
1227 set_vsite_top(vsite
,top
,mdatoms
,cr
);
1230 if (ir
->ePBC
!= epbcNONE
&& !ir
->bPeriodicMols
) {
1231 graph
= mk_graph(fplog
,&(top
->idef
),0,top_global
->natoms
,FALSE
,FALSE
);
1235 make_local_shells(cr
,mdatoms
,shellfc
);
1238 if (ir
->pull
&& PAR(cr
)) {
1239 dd_make_local_pull_groups(NULL
,ir
->pull
,mdatoms
);
1243 if (DOMAINDECOMP(cr
))
1245 /* Distribute the charge groups over the nodes from the master node */
1246 dd_partition_system(fplog
,ir
->init_step
,cr
,TRUE
,1,
1247 state_global
,top_global
,ir
,
1248 state
,&f
,mdatoms
,top
,fr
,
1249 vsite
,shellfc
,constr
,
1253 /* If not DD, copy gb data */
1254 if(ir
->implicit_solvent
&& !DOMAINDECOMP(cr
))
1256 make_local_gb(cr
,fr
->born
,ir
->gb_algorithm
);
1259 update_mdatoms(mdatoms
,state
->lambda
);
1263 /* Update mdebin with energy history if appending to output files */
1264 if ( Flags
& MD_APPENDFILES
)
1266 restore_energyhistory_from_state(mdebin
,&state_global
->enerhist
);
1268 /* Set the initial energy history in state to zero by updating once */
1269 update_energyhistory(&state_global
->enerhist
,mdebin
);
1273 if ((state
->flags
& (1<<estLD_RNG
)) && (Flags
& MD_READ_RNG
)) {
1274 /* Set the random state if we read a checkpoint file */
1275 set_stochd_state(upd
,state
);
1278 /* Initialize constraints */
1280 if (!DOMAINDECOMP(cr
))
1281 set_constraints(constr
,top
,ir
,mdatoms
,cr
);
1284 /* Check whether we have to GCT stuff */
1285 bTCR
= ftp2bSet(efGCT
,nfile
,fnm
);
1288 fprintf(stderr
,"Will do General Coupling Theory!\n");
1290 gnx
= top_global
->mols
.nr
;
1292 for(i
=0; (i
<gnx
); i
++) {
1297 if (repl_ex_nst
> 0 && MASTER(cr
))
1298 repl_ex
= init_replica_exchange(fplog
,cr
->ms
,state_global
,ir
,
1299 repl_ex_nst
,repl_ex_seed
);
1302 if (!ir
->bContinuation
&& !bRerunMD
)
1304 if (mdatoms
->cFREEZE
&& (state
->flags
& (1<<estV
)))
1306 /* Set the velocities of frozen particles to zero */
1307 for(i
=mdatoms
->start
; i
<mdatoms
->start
+mdatoms
->homenr
; i
++)
1309 for(m
=0; m
<DIM
; m
++)
1311 if (ir
->opts
.nFreeze
[mdatoms
->cFREEZE
[i
]][m
])
1321 /* Constrain the initial coordinates and velocities */
1322 do_constrain_first(fplog
,constr
,ir
,mdatoms
,state
,
1323 graph
,cr
,nrnb
,fr
,&top
->idef
);
1327 /* Construct the virtual sites for the initial configuration */
1328 construct_vsites(fplog
,vsite
,state
->x
,nrnb
,ir
->delta_t
,NULL
,
1329 top
->idef
.iparams
,top
->idef
.il
,
1330 fr
->ePBC
,fr
->bMolPBC
,graph
,cr
,state
->box
);
1336 if (Flags
& MD_READ_EKIN
)
1338 restore_ekinstate_from_state(cr
,ekind
,&state_global
->ekinstate
);
1342 /* Compute initial EKin for all.. */
1343 calc_ke_part(state
,&(ir
->opts
),mdatoms
,ekind
,nrnb
);
1348 GMX_MPE_LOG(ev_global_stat_start
);
1350 global_stat(fplog
,gstat
,cr
,enerd
,force_vir
,shake_vir
,mu_tot
,
1351 ir
,ekind
,FALSE
,constr
,vcm
,NULL
,NULL
,&terminate
,
1354 GMX_MPE_LOG(ev_global_stat_finish
);
1359 /* Calculate the initial half step temperature */
1360 temp0
= sum_ekin(TRUE
,&(ir
->opts
),ekind
,ekin
,NULL
);
1364 /* Initiate data for the special cases */
1366 snew(xcopy
,state
->natoms
);
1367 snew(vcopy
,state
->natoms
);
1368 for(ii
=0; (ii
<state
->natoms
); ii
++) {
1369 copy_rvec(state
->x
[ii
],xcopy
[ii
]);
1370 copy_rvec(state
->v
[ii
],vcopy
[ii
]);
1372 copy_mat(state
->box
,boxcopy
);
1377 if (constr
&& !ir
->bContinuation
&& ir
->eConstrAlg
== econtLINCS
)
1380 "RMS relative constraint deviation after constraining: %.2e\n",
1381 constr_rmsd(constr
,FALSE
));
1383 fprintf(fplog
,"Initial temperature: %g K\n",temp0
);
1386 fprintf(stderr
,"starting md rerun '%s', reading coordinates from"
1387 " input trajectory '%s'\n\n",
1388 *(top_global
->name
),opt2fn("-rerun",nfile
,fnm
));
1391 fprintf(stderr
,"Calculated time to finish depends on nsteps from "
1392 "run input file,\nwhich may not correspond to the time "
1393 "needed to process input trajectory.\n\n");
1398 fprintf(stderr
,"starting mdrun '%s'\n",
1399 *(top_global
->name
));
1400 if (ir
->init_step
> 0)
1402 fprintf(stderr
,"%s steps, %8.1f ps (continuing from step %s, %8.1f ps).\n",
1403 gmx_step_str(ir
->init_step
+ir
->nsteps
,sbuf
),
1404 (ir
->init_step
+ir
->nsteps
)*ir
->delta_t
,
1405 gmx_step_str(ir
->init_step
,sbuf2
),
1406 ir
->init_step
*ir
->delta_t
);
1410 fprintf(stderr
,"%s steps, %8.1f ps.\n",
1411 gmx_step_str(ir
->nsteps
,sbuf
),ir
->nsteps
*ir
->delta_t
);
1414 fprintf(fplog
,"\n");
1417 /* Set and write start time */
1418 runtime_start(runtime
);
1419 print_date_and_time(fplog
,cr
->nodeid
,"Started mdrun",runtime
);
1420 wallcycle_start(wcycle
,ewcRUN
);
1422 fprintf(fplog
,"\n");
1424 /* safest point to do file checkpointing is here. More general point would be immediately before integrator call */
1426 chkpt_ret
=fcCheckPointParallel( cr
->nodeid
,
1428 if ( chkpt_ret
== 0 )
1429 gmx_fatal( 3,__FILE__
,__LINE__
, "Checkpoint error on step %d\n", 0 );
1433 /***********************************************************
1435 * Loop over MD steps
1437 ************************************************************/
1439 /* if rerunMD then read coordinates and velocities from input trajectory */
1442 if (getenv("GMX_FORCE_UPDATE"))
1444 bForceUpdate
= TRUE
;
1447 bNotLastFrame
= read_first_frame(oenv
,&status
,
1448 opt2fn("-rerun",nfile
,fnm
),
1449 &rerun_fr
,TRX_NEED_X
| TRX_READ_V
);
1450 if (rerun_fr
.natoms
!= top_global
->natoms
)
1453 "Number of atoms in trajectory (%d) does not match the "
1454 "run input file (%d)\n",
1455 rerun_fr
.natoms
,top_global
->natoms
);
1457 if (ir
->ePBC
!= epbcNONE
)
1461 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
);
1463 if (max_cutoff2(ir
->ePBC
,rerun_fr
.box
) < sqr(fr
->rlistlong
))
1465 gmx_fatal(FARGS
,"Rerun trajectory frame step %d time %f has too small box dimensions",rerun_fr
.step
,rerun_fr
.time
);
1468 /* Set the shift vectors.
1469 * Necessary here when have a static box different from the tpr box.
1471 calc_shifts(rerun_fr
.box
,fr
->shift_vec
);
1475 /* loop over MD steps or if rerunMD to end of input trajectory */
1477 /* Skip the first Nose-Hoover integration when we get the state from tpx */
1478 bStateFromTPX
= !opt2bSet("-cpi",nfile
,fnm
);
1480 bSumEkinhOld
= FALSE
;
1483 step
= ir
->init_step
;
1486 if (ir
->nstlist
== -1)
1488 init_nlistheuristics(&nlh
,bGStatEveryStep
,step
);
1491 bLastStep
= (bRerunMD
|| step_rel
> ir
->nsteps
);
1492 while (!bLastStep
|| (bRerunMD
&& bNotLastFrame
)) {
1494 wallcycle_start(wcycle
,ewcSTEP
);
1496 GMX_MPE_LOG(ev_timestep1
);
1499 if (rerun_fr
.bStep
) {
1500 step
= rerun_fr
.step
;
1501 step_rel
= step
- ir
->init_step
;
1508 bLastStep
= (step_rel
== ir
->nsteps
);
1510 t
= t0
+ step
*ir
->delta_t
;
1513 if (ir
->efep
!= efepNO
)
1515 if (bRerunMD
&& rerun_fr
.bLambda
&& (ir
->delta_lambda
!=0))
1517 state_global
->lambda
= rerun_fr
.lambda
;
1521 state_global
->lambda
= lam0
+ step
*ir
->delta_lambda
;
1523 state
->lambda
= state_global
->lambda
;
1524 bDoDHDL
= do_per_step(step
,ir
->nstdhdl
);
1529 update_annealing_target_temp(&(ir
->opts
),t
);
1534 if (!(DOMAINDECOMP(cr
) && !MASTER(cr
)))
1536 for(i
=0; i
<state_global
->natoms
; i
++)
1538 copy_rvec(rerun_fr
.x
[i
],state_global
->x
[i
]);
1542 for(i
=0; i
<state_global
->natoms
; i
++)
1544 copy_rvec(rerun_fr
.v
[i
],state_global
->v
[i
]);
1549 for(i
=0; i
<state_global
->natoms
; i
++)
1551 clear_rvec(state_global
->v
[i
]);
1555 fprintf(stderr
,"\nWARNING: Some frames do not contain velocities.\n"
1556 " Ekin, temperature and pressure are incorrect,\n"
1557 " the virial will be incorrect when constraints are present.\n"
1559 bRerunWarnNoV
= FALSE
;
1563 copy_mat(rerun_fr
.box
,state_global
->box
);
1564 copy_mat(state_global
->box
,state
->box
);
1566 if (vsite
&& (Flags
& MD_RERUN_VSITE
))
1568 if (DOMAINDECOMP(cr
))
1570 gmx_fatal(FARGS
,"Vsite recalculation with -rerun is not implemented for domain decomposition, use particle decomposition");
1574 /* Following is necessary because the graph may get out of sync
1575 * with the coordinates if we only have every N'th coordinate set
1577 mk_mshift(fplog
,graph
,fr
->ePBC
,state
->box
,state
->x
);
1578 shift_self(graph
,state
->box
,state
->x
);
1580 construct_vsites(fplog
,vsite
,state
->x
,nrnb
,ir
->delta_t
,state
->v
,
1581 top
->idef
.iparams
,top
->idef
.il
,
1582 fr
->ePBC
,fr
->bMolPBC
,graph
,cr
,state
->box
);
1585 unshift_self(graph
,state
->box
,state
->x
);
1590 /* Stop Center of Mass motion */
1591 bStopCM
= (ir
->comm_mode
!= ecmNO
&& do_per_step(step
,ir
->nstcomm
));
1593 /* Copy back starting coordinates in case we're doing a forcefield scan */
1596 for(ii
=0; (ii
<state
->natoms
); ii
++)
1598 copy_rvec(xcopy
[ii
],state
->x
[ii
]);
1599 copy_rvec(vcopy
[ii
],state
->v
[ii
]);
1601 copy_mat(boxcopy
,state
->box
);
1606 /* for rerun MD always do Neighbour Searching */
1607 bNS
= (bFirstStep
|| ir
->nstlist
!= 0);
1612 /* Determine whether or not to do Neighbour Searching and LR */
1613 bNStList
= (ir
->nstlist
> 0 && step
% ir
->nstlist
== 0);
1615 bNS
= (bFirstStep
|| bExchanged
|| bNStList
||
1616 (ir
->nstlist
== -1 && nlh
.nabnsb
> 0));
1618 if (bNS
&& ir
->nstlist
== -1)
1620 set_nlistheuristics(&nlh
,bFirstStep
|| bExchanged
,step
);
1624 if (terminate_now
> 0 || (terminate_now
< 0 && bNS
))
1629 /* Determine whether or not to update the Born radii if doing GB */
1630 bBornRadii
=bFirstStep
;
1631 if(ir
->implicit_solvent
&& (step
% ir
->nstgbradii
==0))
1636 do_log
= do_per_step(step
,ir
->nstlog
) || bFirstStep
|| bLastStep
;
1637 do_verbose
= bVerbose
&&
1638 (step
% stepout
== 0 || bFirstStep
|| bLastStep
);
1640 if (bNS
&& !(bFirstStep
&& ir
->bContinuation
&& !bRerunMD
))
1644 bMasterState
= TRUE
;
1648 bMasterState
= FALSE
;
1649 /* Correct the new box if it is too skewed */
1650 if (DYNAMIC_BOX(*ir
))
1652 if (correct_box(fplog
,step
,state
->box
,graph
))
1654 bMasterState
= TRUE
;
1657 if (DOMAINDECOMP(cr
) && bMasterState
)
1659 dd_collect_state(cr
->dd
,state
,state_global
);
1663 if (DOMAINDECOMP(cr
))
1665 /* Repartition the domain decomposition */
1666 wallcycle_start(wcycle
,ewcDOMDEC
);
1667 dd_partition_system(fplog
,step
,cr
,
1668 bMasterState
,nstglobalcomm
,
1669 state_global
,top_global
,ir
,
1670 state
,&f
,mdatoms
,top
,fr
,
1671 vsite
,shellfc
,constr
,
1672 nrnb
,wcycle
,do_verbose
);
1673 wallcycle_stop(wcycle
,ewcDOMDEC
);
1677 if (MASTER(cr
) && do_log
&& !bFFscan
)
1679 print_ebin_header(fplog
,step
,t
,state
->lambda
);
1682 if (ir
->efep
!= efepNO
)
1684 update_mdatoms(mdatoms
,state
->lambda
);
1687 if (bRerunMD
&& rerun_fr
.bV
)
1689 /* We need the kinetic energy at minus the half step for determining
1690 * the full step kinetic energy and possibly for T-coupling.
1692 calc_ke_part(state
,&(ir
->opts
),mdatoms
,ekind
,nrnb
);
1695 global_stat(fplog
,gstat
,cr
,enerd
,force_vir
,shake_vir
,mu_tot
,
1696 ir
,ekind
,FALSE
,constr
,vcm
,NULL
,NULL
,&terminate
,
1699 sum_ekin(FALSE
,&(ir
->opts
),ekind
,ekin
,NULL
);
1701 clear_mat(force_vir
);
1703 /* Ionize the atoms if necessary */
1706 ionize(fplog
,oenv
,mdatoms
,top_global
,t
,ir
,state
->x
,state
->v
,
1707 mdatoms
->start
,mdatoms
->start
+mdatoms
->homenr
,state
->box
,cr
);
1710 /* Update force field in ffscan program */
1713 if (update_forcefield(fplog
,
1715 mdatoms
->nr
,state
->x
,state
->box
)) {
1716 if (gmx_parallel_env_initialized())
1724 GMX_MPE_LOG(ev_timestep2
);
1726 if ((bNS
|| bLastStep
) && (step
> ir
->init_step
) && !bRerunMD
)
1728 bCPT
= (chkpt
> 0 || (bLastStep
&& (Flags
& MD_CONFOUT
)));
1739 /* Determine the pressure:
1740 * always when we want exact averages in the energy file,
1741 * at ns steps when we have pressure coupling,
1742 * otherwise only at energy output steps (set below).
1744 bNstEner
= (bGStatEveryStep
|| do_per_step(step
,ir
->nstcalcenergy
));
1745 bCalcEner
= bNstEner
;
1747 /* Do we need global communication ? */
1748 bGStat
= (bCalcEner
|| bStopCM
||
1749 (ir
->nstlist
== -1 && !bRerunMD
&& step
>= nlh
.step_nscheck
));
1751 do_ene
= (do_per_step(step
,ir
->nstenergy
) || bLastStep
);
1753 if (do_ene
|| do_log
)
1759 force_flags
= (GMX_FORCE_STATECHANGED
|
1760 ((DYNAMIC_BOX(*ir
) || bRerunMD
) ? GMX_FORCE_DYNAMICBOX
: 0) |
1761 GMX_FORCE_ALLFORCES
|
1762 (bNStList
? GMX_FORCE_DOLR
: 0) |
1764 (bCalcEner
? GMX_FORCE_VIRIAL
: 0) |
1765 (bDoDHDL
? GMX_FORCE_DHDL
: 0));
1769 /* Now is the time to relax the shells */
1770 count
=relax_shell_flexcon(fplog
,cr
,bVerbose
,bFFscan
? step
+1 : step
,
1772 bStopCM
,top
,top_global
,
1774 state
,f
,force_vir
,mdatoms
,
1775 nrnb
,wcycle
,graph
,groups
,
1776 shellfc
,fr
,bBornRadii
,t
,mu_tot
,
1777 state
->natoms
,&bConverged
,vsite
,
1788 /* The coordinates (x) are shifted (to get whole molecules)
1790 * This is parallellized as well, and does communication too.
1791 * Check comments in sim_util.c
1794 do_force(fplog
,cr
,ir
,step
,nrnb
,wcycle
,top
,top_global
,groups
,
1795 state
->box
,state
->x
,&state
->hist
,
1796 f
,force_vir
,mdatoms
,enerd
,fcd
,
1797 state
->lambda
,graph
,
1798 fr
,vsite
,mu_tot
,t
,fp_field
,ed
,bBornRadii
,
1799 (bNS
? GMX_FORCE_NS
: 0) | force_flags
);
1802 GMX_BARRIER(cr
->mpi_comm_mygroup
);
1806 mu_aver
= calc_mu_aver(cr
,state
->x
,mdatoms
->chargeA
,
1807 mu_tot
,&top_global
->mols
,mdatoms
,gnx
,grpindex
);
1810 if (bTCR
&& bFirstStep
)
1812 tcr
=init_coupling(fplog
,nfile
,fnm
,cr
,fr
,mdatoms
,&(top
->idef
));
1813 fprintf(fplog
,"Done init_coupling\n");
1817 /* Now we have the energies and forces corresponding to the
1818 * coordinates at time t. We must output all of this before
1820 * for RerunMD t is read from input trajectory
1822 GMX_MPE_LOG(ev_output_start
);
1824 bX
= do_per_step(step
,ir
->nstxout
);
1825 bV
= do_per_step(step
,ir
->nstvout
);
1826 bF
= do_per_step(step
,ir
->nstfout
);
1827 bXTC
= do_per_step(step
,ir
->nstxtcout
);
1831 fcReportProgress( ir
->nsteps
, step
);
1833 bX
= bX
|| bLastStep
; /*enforce writing positions and velocities
1835 bV
= bV
|| bLastStep
;
1836 bXTC
= bXTC
|| bLastStep
;
1838 int nthreads
=(cr
->nthreads
==0 ? 1 : cr
->nthreads
);
1839 int nnodes
=(cr
->nnodes
==0 ? 1 : cr
->nnodes
);
1842 /*Gromacs drives checkpointing; no ||
1843 fcCheckPointPendingThreads(cr->nodeid,
1845 /* sync bCPT and fc record-keeping */
1846 if (bCPT
&& MASTER(cr
))
1847 fcRequestCheckPoint();
1852 if (bX
|| bV
|| bF
|| bXTC
|| bCPT
)
1854 wallcycle_start(wcycle
,ewcTRAJ
);
1857 if (state
->flags
& (1<<estLD_RNG
))
1859 get_stochd_state(upd
,state
);
1865 state_global
->ekinstate
.bUpToDate
= FALSE
;
1869 update_ekinstate(&state_global
->ekinstate
,ekind
);
1870 state_global
->ekinstate
.bUpToDate
= TRUE
;
1872 update_energyhistory(&state_global
->enerhist
,mdebin
);
1875 write_traj(fplog
,cr
,fp_trn
,bX
,bV
,bF
,fp_xtc
,bXTC
,ir
->xtcprec
,
1876 fn_cpt
,bCPT
,top_global
,ir
->eI
,ir
->simulation_part
,
1877 step
,t
,state
,state_global
,f
,f_global
,&n_xtc
,&x_xtc
);
1879 if (bLastStep
&& step_rel
== ir
->nsteps
&&
1880 (Flags
& MD_CONFOUT
) && MASTER(cr
) &&
1881 !bRerunMD
&& !bFFscan
)
1883 /* x and v have been collected in write_traj */
1884 fprintf(stderr
,"\nWriting final coordinates.\n");
1885 if (ir
->ePBC
!= epbcNONE
&& !ir
->bPeriodicMols
&&
1888 /* Make molecules whole only for confout writing */
1889 do_pbc_mtop(fplog
,ir
->ePBC
,state
->box
,top_global
,state_global
->x
);
1891 write_sto_conf_mtop(ftp2fn(efSTO
,nfile
,fnm
),
1892 *top_global
->name
,top_global
,
1893 state_global
->x
,state_global
->v
,
1894 ir
->ePBC
,state
->box
);
1897 wallcycle_stop(wcycle
,ewcTRAJ
);
1899 GMX_MPE_LOG(ev_output_finish
);
1901 clear_mat(shake_vir
);
1903 /* Box is changed in update() when we do pressure coupling,
1904 * but we should still use the old box for energy corrections and when
1905 * writing it to the energy file, so it matches the trajectory files for
1906 * the same timestep above. Make a copy in a separate array.
1908 copy_mat(state
->box
,lastbox
);
1911 GMX_MPE_LOG(ev_update_start
);
1912 /* This is also parallellized, but check code in update.c */
1913 /* bOK = update(nsb->natoms,START(nsb),HOMENR(nsb),step,state->lambda,&ener[F_DVDL], */
1915 if (!bRerunMD
|| rerun_fr
.bV
|| bForceUpdate
)
1917 wallcycle_start(wcycle
,ewcUPDATE
);
1919 /* We can only do Berendsen coupling after we have summed
1920 * the kinetic energy or virial.
1921 * Since the happens in global_state after update,
1922 * we should only do it at step % nstlist = 1
1923 * with bGStatEveryStep=FALSE.
1925 update(fplog
,step
,&dvdl
,ir
,mdatoms
,state
,graph
,
1926 f
,fr
->bTwinRange
&& bNStList
,fr
->f_twin
,fcd
,
1927 &top
->idef
,ekind
,ir
->nstlist
==-1 ? &nlh
.scale_tot
: NULL
,
1928 cr
,nrnb
,wcycle
,upd
,constr
,bCalcEner
,shake_vir
,
1929 bNEMD
,bFirstStep
&& bStateFromTPX
);
1930 if (fr
->bSepDVDL
&& fplog
&& do_log
)
1932 fprintf(fplog
,sepdvdlformat
,"Constraint",0.0,dvdl
);
1934 enerd
->term
[F_DHDL_CON
] += dvdl
;
1935 wallcycle_stop(wcycle
,ewcUPDATE
);
1939 /* Need to unshift here */
1940 unshift_self(graph
,state
->box
,state
->x
);
1943 GMX_BARRIER(cr
->mpi_comm_mygroup
);
1944 GMX_MPE_LOG(ev_update_finish
);
1946 if (!bOK
&& !bFFscan
)
1948 gmx_fatal(FARGS
,"Constraint error: Shake, Lincs or Settle could not solve the constrains");
1953 wallcycle_start(wcycle
,ewcVSITECONSTR
);
1956 shift_self(graph
,state
->box
,state
->x
);
1958 construct_vsites(fplog
,vsite
,state
->x
,nrnb
,ir
->delta_t
,state
->v
,
1959 top
->idef
.iparams
,top
->idef
.il
,
1960 fr
->ePBC
,fr
->bMolPBC
,graph
,cr
,state
->box
);
1964 unshift_self(graph
,state
->box
,state
->x
);
1966 wallcycle_stop(wcycle
,ewcVSITECONSTR
);
1969 /* Non-equilibrium MD: this is parallellized,
1970 * but only does communication when there really is NEMD.
1972 if (PAR(cr
) && bNEMD
)
1974 accumulate_u(cr
,&(ir
->opts
),ekind
);
1978 calc_ke_part(state
,&(ir
->opts
),mdatoms
,ekind
,nrnb
);
1980 /* since we use the new coordinates in calc_ke_part_visc, we should use
1981 * the new box too. Still, won't this be offset by one timestep in the
1982 * energy file? / EL 20040121
1986 /* Calculate center of mass velocity if necessary, also parallellized */
1987 if (bStopCM
&& !bFFscan
&& !bRerunMD
)
1989 calc_vcm_grp(fplog
,mdatoms
->start
,mdatoms
->homenr
,mdatoms
,
1990 state
->x
,state
->v
,vcm
);
1993 /* Determine the wallclock run time up till now */
1994 run_time
= (double)time(NULL
) - (double)runtime
->real
;
1996 /* Check whether everything is still allright */
1997 if ((bGotTermSignal
|| bGotUsr1Signal
) && !bHandledSignal
)
1999 if (bGotTermSignal
|| ir
->nstlist
== 0)
2009 terminate_now
= terminate
;
2014 "\n\nReceived the %s signal, stopping at the next %sstep\n\n",
2015 bGotTermSignal
? "TERM" : "USR1",
2016 terminate
==-1 ? "NS " : "");
2020 "\n\nReceived the %s signal, stopping at the next %sstep\n\n",
2021 bGotTermSignal
? "TERM" : "USR1",
2022 terminate
==-1 ? "NS " : "");
2024 bHandledSignal
=TRUE
;
2026 else if (MASTER(cr
) && (bNS
|| ir
->nstlist
<= 0) &&
2027 (max_hours
> 0 && run_time
> max_hours
*60.0*60.0*0.99) &&
2030 /* Signal to terminate the run */
2031 terminate
= (ir
->nstlist
== 0 ? 1 : -1);
2034 terminate_now
= terminate
;
2038 fprintf(fplog
,"\nStep %s: Run time exceeded %.3f hours, will terminate the run\n",gmx_step_str(step
,sbuf
),max_hours
*0.99);
2040 fprintf(stderr
, "\nStep %s: Run time exceeded %.3f hours, will terminate the run\n",gmx_step_str(step
,sbuf
),max_hours
*0.99);
2043 if (ir
->nstlist
== -1 && !bRerunMD
)
2045 /* When bGStatEveryStep=FALSE, global_stat is only called
2046 * when we check the atom displacements, not at NS steps.
2047 * This means that also the bonded interaction count check is not
2048 * performed immediately after NS. Therefore a few MD steps could
2049 * be performed with missing interactions.
2050 * But wrong energies are never written to file,
2051 * since energies are only written after global_stat
2054 if (step
>= nlh
.step_nscheck
)
2056 nlh
.nabnsb
= natoms_beyond_ns_buffer(ir
,fr
,&top
->cgs
,
2057 nlh
.scale_tot
,state
->x
);
2061 /* This is not necessarily true,
2062 * but step_nscheck is determined quite conservatively.
2068 /* In parallel we only have to check for checkpointing in steps
2069 * where we do global communication,
2070 * otherwise the other nodes don't know.
2072 if (MASTER(cr
) && ((bGStat
|| !PAR(cr
)) &&
2075 run_time
>= nchkpt
*cpt_period
*60.0)))
2086 /* We will not sum ekinh_old,
2087 * so signal that we still have to do it.
2089 bSumEkinhOld
= TRUE
;
2095 wallcycle_start(wcycle
,ewcMoveE
);
2096 /* Globally (over all NODEs) sum energy, virial etc.
2097 * This includes communication
2099 global_stat(fplog
,gstat
,cr
,enerd
,force_vir
,shake_vir
,mu_tot
,
2100 ir
,ekind
,bSumEkinhOld
,constr
,vcm
,
2101 ir
->nstlist
==-1 ? &nlh
.nabnsb
: NULL
,
2106 terminate_now
= terminate
;
2110 wallcycle_stop(wcycle
,ewcMoveE
);
2111 bSumEkinhOld
= FALSE
;
2114 /* This is just for testing. Nothing is actually done to Ekin
2115 * since that would require extra communication.
2117 if (!bNEMD
&& debug
&& (vcm
->nr
> 0))
2120 mdatoms
->start
,mdatoms
->start
+mdatoms
->homenr
,
2121 state
->v
,vcm
->group_p
[0],
2122 mdatoms
->massT
,mdatoms
->tmass
,ekin
);
2125 /* Do center of mass motion removal */
2126 if (bStopCM
&& !bFFscan
&& !bRerunMD
)
2128 check_cm_grp(fplog
,vcm
,ir
,1);
2129 do_stopcm_grp(fplog
,mdatoms
->start
,mdatoms
->homenr
,mdatoms
->cVCM
,
2130 state
->x
,state
->v
,vcm
);
2131 inc_nrnb(nrnb
,eNR_STOPCM
,mdatoms
->homenr
);
2133 calc_vcm_grp(fplog,START(nsb),HOMENR(nsb),mdatoms->massT,x,v,vcm);
2134 check_cm_grp(fplog,vcm,ir);
2135 do_stopcm_grp(fplog,START(nsb),HOMENR(nsb),x,v,vcm);
2136 check_cm_grp(fplog,vcm,ir);
2140 /* Add force and shake contribution to the virial */
2141 m_add(force_vir
,shake_vir
,total_vir
);
2143 /* Calculate the amplitude of the cosine velocity profile */
2144 ekind
->cosacc
.vcos
= ekind
->cosacc
.mvcos
/mdatoms
->tmass
;
2146 /* Sum the kinetic energies of the groups & calc temp */
2147 enerd
->term
[F_TEMP
] = sum_ekin((bRerunMD
&& !rerun_fr
.bV
),
2148 &(ir
->opts
),ekind
,ekin
,
2149 &(enerd
->term
[F_DKDL
]));
2150 enerd
->term
[F_EKIN
] = trace(ekin
);
2152 /* Calculate pressure and apply LR correction if PPPM is used.
2153 * Use the box from last timestep since we already called update().
2155 enerd
->term
[F_PRES
] =
2156 calc_pres(fr
->ePBC
,ir
->nwall
,lastbox
,ekin
,total_vir
,pres
,
2157 (fr
->eeltype
==eelPPPM
)?enerd
->term
[F_COUL_RECIP
]:0.0);
2159 /* Calculate long range corrections to pressure and energy */
2160 if (bTCR
|| bFFscan
)
2162 set_avcsixtwelve(fplog
,fr
,top_global
);
2165 /* Calculate long range corrections to pressure and energy */
2166 calc_dispcorr(fplog
,ir
,fr
,step
,top_global
->natoms
,
2167 lastbox
,state
->lambda
,pres
,total_vir
,enerd
);
2169 sum_dhdl(enerd
,state
->lambda
,ir
);
2171 enerd
->term
[F_ETOT
] = enerd
->term
[F_EPOT
] + enerd
->term
[F_EKIN
];
2175 if (isnan(enerd
->term
[F_ETOT
]))
2176 gmx_fatal(FARGS
, "NaN detected at step %d\n",step
);
2179 if (_isnan(enerd
->term
[F_ETOT
]))
2180 gmx_fatal(FARGS
, "NaN detected at step %d\n",step
);
2189 enerd
->term
[F_ECONSERVED
] =
2190 enerd
->term
[F_ETOT
] +
2191 nosehoover_energy(&(ir
->opts
),ekind
,
2192 state
->nosehoover_xi
,
2193 state
->therm_integral
);
2196 enerd
->term
[F_ECONSERVED
] =
2197 enerd
->term
[F_ETOT
] +
2198 vrescale_energy(&(ir
->opts
),
2199 state
->therm_integral
);
2203 /* We can not just use bCalcEner, since then the simulation results
2204 * would depend on nstenergy and nstlog or step_nscheck.
2206 if ((state
->flags
& (1<<estPRES_PREV
)) && bNstEner
)
2208 /* Store the pressure in t_state for pressure coupling
2209 * at the next MD step.
2211 copy_mat(pres
,state
->pres_prev
);
2214 /* Check for excessively large energies */
2218 real etot_max
= 1e200
;
2220 real etot_max
= 1e30
;
2222 if (fabs(enerd
->term
[F_ETOT
]) > etot_max
)
2224 fprintf(stderr
,"Energy too large (%g), giving up\n",
2225 enerd
->term
[F_ETOT
]);
2231 /* The coordinates (x) were unshifted in update */
2232 if (bFFscan
&& (shellfc
==NULL
|| bConverged
))
2234 if (print_forcefield(fplog
,enerd
->term
,mdatoms
->homenr
,
2236 &(top_global
->mols
),mdatoms
->massT
,pres
))
2238 if (gmx_parallel_env_initialized())
2240 fprintf(stderr
,"\n");
2247 /* Only do GCT when the relaxation of shells (minimization) has converged,
2248 * otherwise we might be coupling to bogus energies.
2249 * In parallel we must always do this, because the other sims might
2253 /* Since this is called with the new coordinates state->x, I assume
2254 * we want the new box state->box too. / EL 20040121
2256 do_coupling(fplog
,oenv
,nfile
,fnm
,tcr
,t
,step
,enerd
->term
,fr
,
2258 mdatoms
,&(top
->idef
),mu_aver
,
2259 top_global
->mols
.nr
,cr
,
2260 state
->box
,total_vir
,pres
,
2261 mu_tot
,state
->x
,f
,bConverged
);
2265 /* Time for performance */
2266 if (((step
% stepout
) == 0) || bLastStep
)
2268 runtime_upd_proc(runtime
);
2278 upd_mdebin(mdebin
,bDoDHDL
? fp_dhdl
: NULL
,TRUE
,
2279 t
,mdatoms
->tmass
,enerd
,state
,lastbox
,
2280 shake_vir
,force_vir
,total_vir
,pres
,
2281 ekind
,mu_tot
,constr
);
2285 upd_mdebin_step(mdebin
);
2288 do_dr
= do_per_step(step
,ir
->nstdisreout
);
2289 do_or
= do_per_step(step
,ir
->nstorireout
);
2291 print_ebin(fp_ene
,do_ene
,do_dr
,do_or
,do_log
?fplog
:NULL
,step
,t
,
2292 eprNORMAL
,bCompact
,mdebin
,fcd
,groups
,&(ir
->opts
));
2294 if (ir
->ePull
!= epullNO
)
2296 pull_print_output(ir
->pull
,step
,t
);
2299 if (do_per_step(step
,ir
->nstlog
))
2301 if(fflush(fplog
) != 0)
2303 gmx_fatal(FARGS
,"Cannot flush logfile - maybe you are out of quota?");
2309 /* Remaining runtime */
2310 if (MULTIMASTER(cr
) && do_verbose
)
2314 fprintf(stderr
,"\n");
2316 print_time(stderr
,runtime
,step
,ir
,cr
);
2319 /* Replica exchange */
2321 if ((repl_ex_nst
> 0) && (step
> 0) && !bLastStep
&&
2322 do_per_step(step
,repl_ex_nst
))
2324 bExchanged
= replica_exchange(fplog
,cr
,repl_ex
,
2325 state_global
,enerd
->term
,
2328 if (bExchanged
&& PAR(cr
))
2330 if (DOMAINDECOMP(cr
))
2332 dd_partition_system(fplog
,step
,cr
,TRUE
,1,
2333 state_global
,top_global
,ir
,
2334 state
,&f
,mdatoms
,top
,fr
,
2335 vsite
,shellfc
,constr
,
2340 bcast_state(cr
,state
,FALSE
);
2348 /* read next frame from input trajectory */
2349 bNotLastFrame
= read_next_frame(oenv
,status
,&rerun_fr
);
2352 if (!bRerunMD
|| !rerun_fr
.bStep
)
2354 /* increase the MD step number */
2359 cycles
= wallcycle_stop(wcycle
,ewcSTEP
);
2360 if (DOMAINDECOMP(cr
) && wcycle
)
2362 dd_cycles_add(cr
->dd
,cycles
,ddCyclStep
);
2365 if (step_rel
== wcycle_get_reset_counters(wcycle
))
2367 /* Reset all the counters related to performance over the run */
2368 reset_all_counters(fplog
,cr
,step
,&step_rel
,ir
,wcycle
,nrnb
,runtime
);
2369 wcycle_set_reset_counters(wcycle
, 0);
2372 /* End of main MD loop */
2376 runtime_end(runtime
);
2383 if (!(cr
->duty
& DUTY_PME
))
2385 /* Tell the PME only node to finish */
2391 print_ebin(fp_ene
,FALSE
,FALSE
,FALSE
,fplog
,step
,t
,
2392 eprAVER
,FALSE
,mdebin
,fcd
,groups
,&(ir
->opts
));
2402 gmx_fio_fclose(fp_dhdl
);
2406 gmx_fio_fclose(fp_field
);
2411 if (ir
->nstlist
== -1 && nlh
.nns
> 0 && fplog
)
2413 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
)));
2414 fprintf(fplog
,"Average number of atoms that crossed the half buffer length: %.1f\n\n",nlh
.ab
/nlh
.nns
);
2417 if (shellfc
&& fplog
)
2419 fprintf(fplog
,"Fraction of iterations that converged: %.2f %%\n",
2420 (nconverged
*100.0)/step_rel
);
2421 fprintf(fplog
,"Average number of force evaluations per MD step: %.2f\n\n",
2425 if (repl_ex_nst
> 0 && MASTER(cr
))
2427 print_replica_exchange_statistics(fplog
,repl_ex
);
2430 runtime
->nsteps_done
= step_rel
;