Fixed a bug in the pdb-writing code.
[gromacs.git] / src / kernel / md.c
blob457c270a86c54e4d5929874a5d3e775c7bf40878
1 /*
2 * $Id$
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 3.1
11 * Copyright (c) 1991-2001, University of Groningen, The Netherlands
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * If you want to redistribute modifications, please consider that
18 * scientific software is very special. Version control is crucial -
19 * bugs must be traceable. We will be happy to consider code for
20 * inclusion in the official distribution, but derived work must not
21 * be called official GROMACS. Details are found in the README & COPYING
22 * files - if they are missing, get the official version at www.gromacs.org.
24 * To help us fund GROMACS development, we humbly ask that you cite
25 * the papers on the package - you can find them in the top README file.
27 * For more info, check our website at http://www.gromacs.org
29 * And Hey:
30 * GROningen Mixture of Alchemy and Childrens' Stories
32 static char *SRCID_md_c = "$Id$";
33 #include <signal.h>
34 #include <stdlib.h>
35 #include "typedefs.h"
36 #include "smalloc.h"
37 #include "sysstuff.h"
38 #include "vec.h"
39 #include "statutil.h"
40 #include "vcm.h"
41 #include "mdebin.h"
42 #include "nrnb.h"
43 #include "calcmu.h"
44 #include "rdgroup.h"
45 #include "dummies.h"
46 #include "update.h"
47 #include "ns.h"
48 #include "trnio.h"
49 #include "mdrun.h"
50 #include "confio.h"
51 #include "network.h"
52 #include "sim_util.h"
53 #include "pull.h"
54 #include "xvgr.h"
55 #include "physics.h"
56 #include "names.h"
57 #include "xmdrun.h"
58 #include "disre.h"
59 #include "orires.h"
60 #include "pppm.h"
61 #include "pme.h"
62 #include "mdatoms.h"
63 #include "sim_util.h"
65 volatile bool bGotTermSignal = FALSE, bGotUsr1Signal = FALSE;
67 static RETSIGTYPE signal_handler(int n)
69 switch (n) {
70 case SIGTERM:
71 bGotTermSignal = TRUE;
72 break;
73 case SIGUSR1:
74 bGotUsr1Signal = TRUE;
75 break;
81 void mdrunner(t_commrec *cr,t_commrec *mcr,int nfile,t_filenm fnm[],
82 bool bVerbose,bool bCompact,
83 int nDlb,int nstepout,t_edsamyn *edyn,
84 unsigned long Flags)
86 double nodetime=0,realtime;
87 t_parm *parm;
88 rvec *buf,*f,*vold,*v,*vt,*x,box_size;
89 real tmpr1,tmpr2;
90 real *ener;
91 t_nrnb *nrnb;
92 t_nsborder *nsb;
93 t_topology *top;
94 t_groups *grps;
95 t_graph *graph;
96 t_mdatoms *mdatoms;
97 t_forcerec *fr;
98 t_fcdata *fcd;
99 time_t start_t=0;
100 bool bDummies,bParDummies;
101 t_comm_dummies dummycomm;
102 int i,m;
103 char *gro;
105 /* Initiate everything (snew sets to zero!) */
106 snew(ener,F_NRE);
107 snew(fcd,1);
108 snew(nsb,1);
109 snew(top,1);
110 snew(grps,1);
111 snew(parm,1);
112 snew(nrnb,cr->nnodes);
114 if (bVerbose && MASTER(cr))
115 fprintf(stderr,"Getting Loaded...\n");
117 if (PAR(cr)) {
118 /* The master thread on the master node reads from disk, then passes everything
119 * around the ring, and finally frees the stuff
121 if (MASTER(cr))
122 distribute_parts(cr->left,cr->right,cr->nodeid,cr->nnodes,parm,
123 ftp2fn(efTPX,nfile,fnm),nDlb);
125 /* Every node (including the master) reads the data from the ring */
126 init_parts(stdlog,cr,
127 parm,top,&x,&v,&mdatoms,nsb,
128 MASTER(cr) ? LIST_SCALARS | LIST_PARM : 0,
129 &bParDummies,&dummycomm);
130 } else {
131 /* Read it up... */
132 init_single(stdlog,parm,ftp2fn(efTPX,nfile,fnm),top,&x,&v,&mdatoms,nsb);
133 bParDummies=FALSE;
135 snew(buf,nsb->natoms);
136 snew(f,nsb->natoms);
137 snew(vt,nsb->natoms);
138 snew(vold,nsb->natoms);
140 if (bVerbose && MASTER(cr))
141 fprintf(stderr,"Loaded with Money\n\n");
143 /* Index numbers for parallellism... */
144 nsb->nodeid = cr->nodeid;
145 top->idef.nodeid = cr->nodeid;
147 /* Group stuff (energies etc) */
148 init_groups(stdlog,mdatoms,&(parm->ir.opts),grps);
149 /* Copy the cos acceleration to the groups struct */
150 grps->cosacc.cos_accel = parm->ir.cos_accel;
152 /* Periodicity stuff */
153 graph=mk_graph(&(top->idef),top->atoms.nr,FALSE,FALSE);
154 if (debug)
155 p_graph(debug,"Initial graph",graph);
157 /* Distance Restraints */
158 init_disres(stdlog,top->idef.il[F_DISRES].nr,top->idef.il[F_DISRES].iatoms,
159 top->idef.iparams,&(parm->ir),mcr,fcd);
161 /* Orientation restraints */
162 init_orires(stdlog,top->idef.il[F_ORIRES].nr,top->idef.il[F_ORIRES].iatoms,
163 top->idef.iparams,x,mdatoms,&(parm->ir),mcr,fcd);
165 /* check if there are dummies */
166 bDummies=FALSE;
167 for(i=0; (i<F_NRE) && !bDummies; i++)
168 bDummies = ((interaction_function[i].flags & IF_DUMMY) &&
169 (top->idef.il[i].nr > 0));
171 if(bDummies)
172 please_cite(stdlog,"Feenstra99");
174 /* Initiate forcerecord */
175 fr = mk_forcerec();
176 init_forcerec(stdlog,fr,&(parm->ir),top,cr,mdatoms,nsb,parm->box,FALSE,
177 opt2fn("-table",nfile,fnm),FALSE);
178 fr->bSepDVDL = ((Flags & MD_SEPDVDL) == MD_SEPDVDL);
179 /* Initiate box */
180 for(m=0; (m<DIM); m++)
181 box_size[m]=parm->box[m][m];
183 /* Initiate PPPM if necessary */
184 if (fr->eeltype == eelPPPM)
185 init_pppm(stdlog,cr,nsb,FALSE,TRUE,box_size,getenv("GMXGHAT"),&parm->ir);
186 if (fr->eeltype == eelPME)
187 init_pme(stdlog,cr,parm->ir.nkx,parm->ir.nky,parm->ir.nkz,parm->ir.pme_order,
188 HOMENR(nsb),parm->ir.bOptFFT,parm->ir.ewald_geometry);
190 /* Now do whatever the user wants us to do (how flexible...) */
191 switch (parm->ir.eI) {
192 case eiMD:
193 case eiSD:
194 case eiBD:
195 start_t=do_md(stdlog,cr,mcr,nfile,fnm,
196 bVerbose,bCompact,bDummies,
197 bParDummies ? &dummycomm : NULL,
198 nstepout,parm,grps,top,ener,fcd,x,vold,v,vt,f,buf,
199 mdatoms,nsb,nrnb,graph,edyn,fr,box_size,Flags);
200 break;
201 case eiCG:
202 start_t=do_cg(stdlog,nfile,fnm,parm,top,grps,nsb,
203 x,f,buf,mdatoms,parm->ekin,ener,fcd,
204 nrnb,bVerbose,bDummies,
205 bParDummies ? &dummycomm : NULL,
206 cr,mcr,graph,fr,box_size);
207 break;
208 case eiSteep:
209 start_t=do_steep(stdlog,nfile,fnm,parm,top,grps,nsb,
210 x,f,buf,mdatoms,parm->ekin,ener,fcd,
211 nrnb,bVerbose,bDummies,
212 bParDummies ? &dummycomm : NULL,
213 cr,mcr,graph,fr,box_size);
214 break;
215 case eiNM:
216 start_t=do_nm(stdlog,cr,nfile,fnm,
217 bVerbose,bCompact,nstepout,parm,grps,
218 top,ener,fcd,x,vold,v,vt,f,buf,
219 mdatoms,nsb,nrnb,graph,edyn,fr,box_size);
220 break;
221 default:
222 fatal_error(0,"Invalid integrator (%d)...\n",parm->ir.eI);
225 /* Some timing stats */
226 if (MASTER(cr)) {
227 realtime=difftime(time(NULL),start_t);
228 if ((nodetime=node_time()) == 0)
229 nodetime=realtime;
231 else
232 realtime=0;
234 /* Convert back the atoms */
235 md2atoms(mdatoms,&(top->atoms),TRUE);
237 /* Finish up, write some stuff
238 * if rerunMD, don't write last frame again
240 finish_run(stdlog,cr,ftp2fn(efSTO,nfile,fnm),
241 nsb,top,parm,nrnb,nodetime,realtime,parm->ir.nsteps,
242 parm->ir.eI==eiMD || parm->ir.eI==eiSD || parm->ir.eI==eiBD);
244 /* Does what it says */
245 print_date_and_time(stdlog,cr->nodeid,"Finished mdrun");
248 time_t do_md(FILE *log,t_commrec *cr,t_commrec *mcr,int nfile,t_filenm fnm[],
249 bool bVerbose,bool bCompact,
250 bool bDummies, t_comm_dummies *dummycomm,
251 int stepout,t_parm *parm,t_groups *grps,t_topology *top,
252 real ener[],t_fcdata *fcd,
253 rvec x[],rvec vold[],rvec v[],rvec vt[],rvec f[],
254 rvec buf[],t_mdatoms *mdatoms,t_nsborder *nsb,t_nrnb nrnb[],
255 t_graph *graph,t_edsamyn *edyn,t_forcerec *fr,rvec box_size,
256 unsigned long Flags)
258 t_mdebin *mdebin;
259 int fp_ene=0,fp_trn=0,step;
260 FILE *fp_dgdl=NULL;
261 time_t start_t;
262 real t,lambda,t0,lam0,SAfactor;
263 bool bNS,bStopCM,bTYZ,bRerunMD,bNotLastFrame=FALSE,
264 bFirstStep,bLastStep,bNEMD,do_log,bRerunWarnNoV=TRUE;
265 tensor force_vir,pme_vir,shake_vir;
266 t_nrnb mynrnb;
267 char *traj,*xtc_traj; /* normal and compressed trajectory filename */
268 int i,m,status;
269 rvec mu_tot;
270 rvec *xx,*vv,*ff;
271 t_vcm *vcm;
272 t_trxframe rerun_fr;
273 t_pull pulldata; /* for pull code */
274 /* A boolean (disguised as a real) to terminate mdrun */
275 real terminate=0;
277 /* XMDRUN stuff: shell, general coupling etc. */
278 bool bFFscan;
279 int nshell,nshell_tot,count,nconverged=0;
280 t_shell *shells=NULL;
281 real timestep=0;
282 double tcount=0;
283 bool bShell,bIonize=FALSE,bGlas=FALSE;
284 bool bTCR=FALSE,bConverged=TRUE,bOK;
285 real mu_aver=0,fmax;
286 int gnx,ii;
287 atom_id *grpindex;
288 char *grpname;
289 t_coupl_rec *tcr=NULL;
290 rvec *xcopy=NULL;
291 /* End of XMDRUN stuff */
293 /* Turn on signal handling */
294 signal(SIGTERM,signal_handler);
295 signal(SIGUSR1,signal_handler);
297 /* Check for special mdrun options */
298 bRerunMD = (Flags & MD_RERUN) == MD_RERUN;
299 bIonize = (Flags & MD_IONIZE) == MD_IONIZE;
300 bGlas = (Flags & MD_GLAS) == MD_GLAS;
301 bFFscan = (Flags & MD_FFSCAN) == MD_FFSCAN;
303 /* Initial values */
304 init_md(cr,&parm->ir,parm->box,&t,&t0,&lambda,&lam0,&SAfactor,
305 &mynrnb,&bTYZ,top,
306 nfile,fnm,&traj,&xtc_traj,&fp_ene,&fp_dgdl,&mdebin,grps,
307 force_vir,pme_vir,shake_vir,mdatoms,mu_tot,&bNEMD,&vcm,nsb);
308 debug_gmx();
310 /* Check for polarizable models */
311 shells = init_shells(log,START(nsb),HOMENR(nsb),&top->idef,
312 mdatoms,&nshell);
313 nshell_tot = nshell;
314 if (PAR(cr))
315 gmx_sumi(1,&nshell_tot,cr);
316 bShell = nshell_tot > 0;
318 /* Check whether we have to do dipole stuff */
319 if (opt2bSet("-dn",nfile,fnm))
320 rd_index(opt2fn("-dn",nfile,fnm),1,&gnx,&grpindex,&grpname);
321 else {
322 gnx = top->blocks[ebMOLS].nr;
323 snew(grpindex,gnx);
324 for(i=0; (i<gnx); i++)
325 grpindex[i] = i;
328 /* Check whether we have to GCT stuff */
329 bTCR = ftp2bSet(efGCT,nfile,fnm);
330 if (MASTER(cr) && bTCR)
331 fprintf(stderr,"Will do General Coupling Theory!\n");
333 /* Remove periodicity */
334 if (fr->ePBC != epbcNONE)
335 do_pbc_first(log,parm,box_size,fr,graph,x);
336 debug_gmx();
338 /* Initialize pull code */
339 init_pull(log,nfile,fnm,&pulldata,x,mdatoms,parm->box);
340 if (pulldata.bPull && cr->nnodes>1 && !pulldata.runtype==eUmbrella)
341 fatal_error(0,"Can not pull in parallel");
343 if (!parm->ir.bUncStart)
344 do_shakefirst(log,bTYZ,lambda,ener,parm,nsb,mdatoms,x,vold,buf,f,v,
345 graph,cr,&mynrnb,grps,fr,top,edyn,&pulldata);
346 debug_gmx();
348 /* Compute initial EKin for all.. */
349 if (grps->cosacc.cos_accel == 0)
350 calc_ke_part(TRUE,parm->ir.eI==eiSD,
351 START(nsb),HOMENR(nsb),vold,v,vt,&(parm->ir.opts),
352 mdatoms,grps,&mynrnb,lambda,&ener[F_DVDLKIN]);
353 else
354 calc_ke_part_visc(TRUE,START(nsb),HOMENR(nsb),
355 parm->box,x,vold,v,vt,&(parm->ir.opts),
356 mdatoms,grps,&mynrnb,lambda,&ener[F_DVDLKIN]);
357 debug_gmx();
359 if (PAR(cr))
360 global_stat(log,cr,ener,force_vir,shake_vir,
361 &(parm->ir.opts),grps,&mynrnb,nrnb,vcm,&terminate);
362 debug_gmx();
364 /* Calculate Temperature coupling parameters lambda */
365 ener[F_TEMP] = sum_ekin(&(parm->ir.opts),grps,parm->ekin,bTYZ);
366 if(parm->ir.etc==etcBERENDSEN)
367 berendsen_tcoupl(&(parm->ir.opts),grps,
368 parm->ir.delta_t,SAfactor);
369 else if(parm->ir.etc==etcNOSEHOOVER)
370 nosehoover_tcoupl(&(parm->ir.opts),grps,
371 parm->ir.delta_t,SAfactor);
372 debug_gmx();
374 /* Initiate data for the special cases */
375 if (bFFscan) {
376 snew(xcopy,nsb->natoms);
377 for(ii=0; (ii<nsb->natoms); ii++)
378 copy_rvec(x[ii],xcopy[ii]);
381 /* Write start time and temperature */
382 start_t=print_date_and_time(log,cr->nodeid,"Started mdrun");
384 if (MASTER(cr)) {
385 fprintf(log,"Initial temperature: %g K\n",ener[F_TEMP]);
386 if (bRerunMD) {
387 fprintf(stderr,"starting md rerun '%s', reading coordinates from"
388 " input trajectory '%s'\n\n",
389 *(top->name),opt2fn("-rerun",nfile,fnm));
390 if (bVerbose)
391 fprintf(stderr,"Calculated time to finish depends on nsteps from "
392 "run input file,\nwhich may not correspond to the time "
393 "needed to process input trajectory.\n\n");
394 } else
395 fprintf(stderr,"starting mdrun '%s'\n%d steps, %8.1f ps.\n\n",
396 *(top->name),parm->ir.nsteps,parm->ir.nsteps*parm->ir.delta_t);
398 /* Set the node time counter to 0 after initialisation */
399 start_time();
400 debug_gmx();
401 /***********************************************************
403 * Loop over MD steps
405 ************************************************************/
407 /* if rerunMD then read coordinates and velocities from input trajectory */
408 if (bRerunMD) {
409 bNotLastFrame = read_first_frame(&status,opt2fn("-rerun",nfile,fnm),
410 &rerun_fr,TRX_NEED_X | TRX_READ_V);
411 if (rerun_fr.natoms != mdatoms->nr)
412 fatal_error(0,"Number of atoms in trajectory (%d) does not match the "
413 "run input file (%d)\n",rerun_fr.natoms,mdatoms->nr);
416 /* loop over MD steps or if rerunMD to end of input trajectory */
417 bFirstStep = TRUE;
418 step = 0;
419 while ((!bRerunMD && (step<=parm->ir.nsteps)) ||
420 (bRerunMD && bNotLastFrame)) {
422 if (bRerunMD) {
423 if (rerun_fr.bStep)
424 step = rerun_fr.step;
425 if (rerun_fr.bTime)
426 t = rerun_fr.time;
427 else
428 t = step;
429 for(i=0; i<mdatoms->nr; i++)
430 copy_rvec(rerun_fr.x[i],x[i]);
431 if (rerun_fr.bV)
432 for(i=0; i<mdatoms->nr; i++)
433 copy_rvec(rerun_fr.v[i],v[i]);
434 else {
435 for(i=0; i<mdatoms->nr; i++)
436 clear_rvec(v[i]);
437 if (bRerunWarnNoV) {
438 fprintf(stderr,"\nWARNING: Some frames do not contain velocities.\n"
439 " Ekin, temperature and pressure are incorrect,\n"
440 " the virial will be incorrect when constraints are present.\n"
441 "\n");
442 bRerunWarnNoV = FALSE;
445 copy_mat(rerun_fr.box,parm->box);
447 /* for rerun MD always do Neighbour Searching */
448 bNS = ((parm->ir.nstlist!=0) || bFirstStep);
449 } else
450 /* Determine whether or not to do Neighbour Searching */
451 bNS = ((parm->ir.nstlist && (step % parm->ir.nstlist==0)) || bFirstStep);
453 bLastStep=(step==parm->ir.nsteps);
455 do_log = do_per_step(step,parm->ir.nstlog) || bLastStep;
457 /* Stop Center of Mass motion */
458 bStopCM = do_per_step(step,abs(parm->ir.nstcomm));
460 /* Copy back starting coordinates in case we're doing a forcefield scan */
461 if (bFFscan) {
462 for(ii=0; (ii<nsb->natoms); ii++)
463 copy_rvec(xcopy[ii],x[ii]);
466 if (bDummies) {
467 shift_self(graph,parm->box,x);
469 construct_dummies(log,x,&mynrnb,parm->ir.delta_t,v,&top->idef,
470 graph,cr,parm->box,dummycomm);
472 unshift_self(graph,parm->box,x);
475 debug_gmx();
477 /* Set values for invmass etc. This routine not parallellized, but hardly
478 * ever used, only when doing free energy calculations.
480 init_mdatoms(mdatoms,lambda,bFirstStep);
482 clear_mat(force_vir);
484 /* Ionize the atoms if necessary */
485 if (bIonize)
486 ionize(log,mdatoms,top->atoms.atomname,t,&parm->ir,x,v,
487 START(nsb),START(nsb)+HOMENR(nsb),parm->box,cr);
489 /* Update force field in ffscan program */
490 if (bFFscan)
491 update_forcefield(nfile,fnm,fr);
493 if (bShell) {
494 /* Now is the time to relax the shells */
495 count=relax_shells(log,cr,mcr,bVerbose,step,parm,bNS,bStopCM,top,
496 ener,fcd,x,vold,v,vt,f,buf,mdatoms,nsb,&mynrnb,graph,
497 grps,force_vir,pme_vir,bShell,
498 nshell,shells,fr,traj,t,lambda,mu_tot,
499 nsb->natoms,parm->box,&bConverged);
500 tcount+=count;
502 if (bConverged)
503 nconverged++;
505 else {
506 /* The coordinates (x) are shifted (to get whole molecules) in do_force
507 * This is parallellized as well, and does communication too.
508 * Check comments in sim_util.c
510 do_force(log,cr,mcr,parm,nsb,force_vir,pme_vir,step,&mynrnb,
511 top,grps,x,v,f,buf,mdatoms,ener,fcd,bVerbose && !PAR(cr),
512 lambda,graph,bNS,FALSE,fr,mu_tot,FALSE);
515 if (bTCR)
516 mu_aver=calc_mu_aver(cr,nsb,x,mdatoms->chargeA,mu_tot,top,mdatoms,
517 gnx,grpindex);
519 if (bGlas)
520 do_glas(log,START(nsb),HOMENR(nsb),x,f,
521 fr,mdatoms,top->idef.atnr,&parm->ir,ener);
523 if (bTCR && (step == 0)) {
524 tcr=init_coupling(log,nfile,fnm,cr,fr,mdatoms,&(top->idef));
525 fprintf(log,"Done init_coupling\n");
526 fflush(log);
529 /* Now we have the energies and forces corresponding to the
530 * coordinates at time t. We must output all of this before
531 * the update.
532 * for RerunMD t is read from input trajectory
534 if (!bRerunMD)
535 t = t0 + step*parm->ir.delta_t;
537 if (parm->ir.efep != efepNO) {
538 if (bRerunMD && rerun_fr.bLambda)
539 lambda = rerun_fr.lambda;
540 else
541 lambda = lam0 + step*parm->ir.delta_lambda;
543 if (parm->ir.bSimAnn) {
544 SAfactor = 1.0 - t/parm->ir.zero_temp_time;
545 if (SAfactor < 0)
546 SAfactor = 0;
549 if (MASTER(cr) && do_log && !bFFscan)
550 print_ebin_header(log,step,t,lambda,SAfactor);
552 if (bDummies)
553 spread_dummy_f(log,x,f,&mynrnb,&top->idef,dummycomm,cr);
555 /* Calculation of the virial must be done after dummies! */
556 /* Question: Is it correct to do the PME forces after this? */
557 calc_virial(log,START(nsb),HOMENR(nsb),x,f,
558 force_vir,pme_vir,graph,parm->box,&mynrnb,fr,FALSE);
560 /* Spread the LR force on dummy particle to the other particles...
561 * This is parallellized. MPI communication is performed
562 * if the constructing atoms aren't local.
564 if (bDummies && fr->bEwald)
565 spread_dummy_f(log,x,fr->f_pme,&mynrnb,&top->idef,dummycomm,cr);
567 sum_lrforces(f,fr,START(nsb),HOMENR(nsb));
569 xx = (do_per_step(step,parm->ir.nstxout) || bLastStep) ? x : NULL;
570 vv = (do_per_step(step,parm->ir.nstvout) || bLastStep) ? v : NULL;
571 ff = (do_per_step(step,parm->ir.nstfout)) ? f : NULL;
573 fp_trn = write_traj(log,cr,traj,nsb,step,t,lambda,
574 nrnb,nsb->natoms,xx,vv,ff,parm->box);
575 debug_gmx();
577 /* don't write xtc and last structure for rerunMD */
578 if (!bRerunMD && !bFFscan) {
579 if (do_per_step(step,parm->ir.nstxtcout))
580 write_xtc_traj(log,cr,xtc_traj,nsb,mdatoms,
581 step,t,x,parm->box,parm->ir.xtcprec);
582 if (bLastStep && MASTER(cr)) {
583 fprintf(stderr,"Writing final coordinates.\n");
584 write_sto_conf(ftp2fn(efSTO,nfile,fnm),
585 *top->name, &(top->atoms),x,v,parm->box);
587 debug_gmx();
589 clear_mat(shake_vir);
591 /* Afm and Umbrella type pulling happens before the update,
592 * other types in update
594 if (pulldata.bPull &&
595 (pulldata.runtype == eAfm || pulldata.runtype == eUmbrella ||
596 pulldata.runtype == eTest))
597 pull(&pulldata,x,f,parm->box,top,parm->ir.delta_t,step,
598 mdatoms->nr,mdatoms,START(nsb),HOMENR(nsb));
600 if (bFFscan)
601 clear_rvecs(nsb->natoms,buf);
603 /* This is also parallellized, but check code in update.c */
604 /* bOK = update(nsb->natoms,START(nsb),HOMENR(nsb),step,lambda,&ener[F_DVDL], */
605 bOK = TRUE;
606 update(nsb->natoms,START(nsb),HOMENR(nsb),step,lambda,&ener[F_DVDL],
607 parm,SAfactor,mdatoms,x,graph,f,buf,vold,vt,v,
608 top,grps,shake_vir,cr,&mynrnb,bTYZ,TRUE,edyn,&pulldata,bNEMD);
609 if (!bOK && !bFFscan)
610 fatal_error(0,"Constraint error: Shake, Lincs or Settle could not solve the constrains");
612 /* The coordinates (x) were unshifted in update */
613 if (bFFscan && (!bShell || bConverged))
614 print_forcefield(log,ener[F_EPOT],HOMENR(nsb),f,buf,xcopy,
615 &(top->blocks[ebMOLS]),mdatoms->massT);
617 if (parm->ir.epc!=epcNO)
618 correct_box(parm->box,fr,graph);
619 /* (un)shifting should NOT be done after this,
620 * since the box vectors might have changed
623 /* Non-equilibrium MD: this is parallellized, but only does communication
624 * when there really is NEMD.
626 if (PAR(cr) && bNEMD)
627 accumulate_u(cr,&(parm->ir.opts),grps);
629 debug_gmx();
630 if (grps->cosacc.cos_accel == 0)
631 calc_ke_part(FALSE,parm->ir.eI==eiSD,
632 START(nsb),HOMENR(nsb),vold,v,vt,&(parm->ir.opts),
633 mdatoms,grps,&mynrnb,lambda,&ener[F_DVDLKIN]);
634 else
635 calc_ke_part_visc(FALSE,START(nsb),HOMENR(nsb),
636 parm->box,x,vold,v,vt,&(parm->ir.opts),
637 mdatoms,grps,&mynrnb,lambda,&ener[F_DVDLKIN]);
639 debug_gmx();
640 /* Calculate center of mass velocity if necessary, also parallellized */
641 if (bStopCM && !bFFscan)
642 calc_vcm_grp(log,START(nsb),HOMENR(nsb),mdatoms->massT,x,v,vcm);
644 /* Check whether everything is still allright */
645 if (bGotTermSignal || bGotUsr1Signal) {
646 if (bGotTermSignal)
647 terminate = 1;
648 else
649 terminate = -1;
650 fprintf(log,"\n\nReceived the %s signal\n\n",
651 bGotTermSignal ? "TERM" : "USR1");
652 fflush(log);
653 if (MASTER(cr)) {
654 fprintf(stderr,"\n\nReceived the %s signal\n\n",
655 bGotTermSignal ? "TERM" : "USR1");
656 fflush(stderr);
658 bGotTermSignal = FALSE;
659 bGotUsr1Signal = FALSE;
662 if (PAR(cr)) {
663 /* Globally (over all NODEs) sum energy, virial etc.
664 * This includes communication
666 global_stat(log,cr,ener,force_vir,shake_vir,
667 &(parm->ir.opts),grps,&mynrnb,nrnb,vcm,&terminate);
668 /* Correct for double counting energies, should be moved to
669 * global_stat
671 if (fr->bTwinRange && !bNS)
672 for(i=0; (i<grps->estat.nn); i++) {
673 grps->estat.ee[egLR][i] /= cr->nnodes;
674 grps->estat.ee[egLJLR][i] /= cr->nnodes;
677 else
678 cp_nrnb(&(nrnb[0]),&mynrnb);
680 /* This is just for testing. Nothing is actually done to Ekin
681 * since that would require extra communication.
683 if (!bNEMD && debug && (vcm->nr > 0))
684 correct_ekin(debug,START(nsb),START(nsb)+HOMENR(nsb),v,
685 vcm->group_p[0],
686 mdatoms->massT,mdatoms->tmass,parm->ekin);
688 if ((terminate != 0) && (step < parm->ir.nsteps)) {
689 if (terminate<0 && parm->ir.nstxout)
690 /* this is the USR1 signal and we are writing x to trr,
691 stop at next x frame in trr */
692 parm->ir.nsteps = (step / parm->ir.nstxout + 1) * parm->ir.nstxout;
693 else
694 parm->ir.nsteps = step+1;
695 fprintf(log,"\nSetting nsteps to %d\n\n",parm->ir.nsteps);
696 fflush(log);
697 if (MASTER(cr)) {
698 fprintf(stderr,"\nSetting nsteps to %d\n\n",parm->ir.nsteps);
699 fflush(stderr);
701 /* erase the terminate signal */
702 terminate = 0;
705 /* Do center of mass motion removal */
706 if (bStopCM && !bFFscan) {
707 check_cm_grp(log,vcm);
708 do_stopcm_grp(log,START(nsb),HOMENR(nsb),x,v,vcm);
709 inc_nrnb(&mynrnb,eNR_STOPCM,HOMENR(nsb));
711 calc_vcm_grp(log,START(nsb),HOMENR(nsb),mdatoms->massT,x,v,vcm);
712 check_cm_grp(log,vcm);
713 do_stopcm_grp(log,START(nsb),HOMENR(nsb),x,v,vcm);
714 check_cm_grp(log,vcm);
718 /* Add force and shake contribution to the virial */
719 m_add(force_vir,shake_vir,parm->vir);
721 /* Sum the potential energy terms from group contributions */
722 sum_epot(&(parm->ir.opts),grps,ener);
724 /* Calculate the amplitude of the cosine velocity profile */
725 grps->cosacc.vcos = grps->cosacc.mvcos/mdatoms->tmass;
727 /* Sum the kinetic energies of the groups & calc temp */
728 ener[F_TEMP]=sum_ekin(&(parm->ir.opts),grps,parm->ekin,bTYZ);
729 ener[F_EKIN]=trace(parm->ekin);
730 ener[F_ETOT]=ener[F_EPOT]+ener[F_EKIN];
732 /* Check for excessively large energies */
733 if (bIonize && fabs(ener[F_ETOT]) > 1e18) {
734 fprintf(stderr,"Energy too large (%g), giving up\n",ener[F_ETOT]);
735 break;
738 /* Calculate Temperature coupling parameters lambda and adjust
739 * target temp when doing simulated annealing
741 if(parm->ir.etc==etcBERENDSEN)
742 berendsen_tcoupl(&(parm->ir.opts),grps,parm->ir.delta_t,SAfactor);
743 else if(parm->ir.etc==etcNOSEHOOVER)
744 nosehoover_tcoupl(&(parm->ir.opts),grps,parm->ir.delta_t,SAfactor);
746 /* Calculate pressure and apply LR correction if PPPM is used */
747 calc_pres(fr->ePBC,parm->box,parm->ekin,parm->vir,parm->pres,
748 (fr->eeltype==eelPPPM) ? ener[F_LR] : 0.0);
750 /* Calculate long range corrections to pressure and energy */
751 if (bTCR)
752 set_avcsix(log,fr,mdatoms);
754 /* Calculate long range corrections to pressure and energy */
755 calc_dispcorr(log,parm->ir.eDispCorr,
756 fr,mdatoms->nr,parm->box,parm->pres,parm->vir,ener);
758 if (bTCR) {
759 /* Only do GCT when the relaxation of shells (minimization) has converged,
760 * otherwise we might be coupling to bogus energies.
761 * In parallel we must always do this, because the other sims might
762 * update the FF.
764 do_coupling(log,nfile,fnm,tcr,t,step,ener,fr,
765 &(parm->ir),MASTER(cr),
766 mdatoms,&(top->idef),mu_aver,
767 top->blocks[ebMOLS].nr,(mcr!=NULL) ? mcr : cr,
768 parm->box,parm->vir,parm->pres,
769 mu_tot,x,f,bConverged);
770 debug_gmx();
773 /* Time for performance */
774 if (((step % 10) == 0) || bLastStep)
775 update_time();
777 /* Output stuff */
778 if (MASTER(cr) && !bFFscan) {
779 bool do_ene,do_dr,do_or;
781 upd_mdebin(mdebin,fp_dgdl,mdatoms->tmass,step,t,ener,parm->box,shake_vir,
782 force_vir,parm->vir,parm->pres,grps,mu_tot,
783 (parm->ir.etc==etcNOSEHOOVER));
784 do_ene = do_per_step(step,parm->ir.nstenergy) || bLastStep;
785 do_dr = do_per_step(step,parm->ir.nstdisreout) || bLastStep;
786 do_or = do_per_step(step,parm->ir.nstorireout) || bLastStep;
787 print_ebin(fp_ene,do_ene,do_dr,do_or,do_log?log:NULL,step,t,
788 eprNORMAL,bCompact,mdebin,fcd,&(top->atoms));
789 if (bVerbose)
790 fflush(log);
793 /* Remaining runtime */
794 if (MASTER(cr) && bVerbose && ( ((step % stepout)==0) || bLastStep)) {
795 if (bShell)
796 fprintf(stderr,"\n");
797 print_time(stderr,start_t,step,&parm->ir);
800 bFirstStep = FALSE;
802 if (bRerunMD)
803 /* read next frame from input trajectory */
804 bNotLastFrame = read_next_frame(status,&rerun_fr);
806 if (!bRerunMD || !rerun_fr.bStep)
807 /* increase the MD step number */
808 step++;
810 /* End of main MD loop */
811 debug_gmx();
813 /* Dump the NODE time to the log file on each node */
814 if (PAR(cr)) {
815 double *ct,ctmax,ctsum;
817 snew(ct,cr->nnodes);
818 ct[cr->nodeid] = node_time();
819 gmx_sumd(cr->nnodes,ct,cr);
820 ctmax = ct[0];
821 ctsum = ct[0];
822 for(i=1; (i<cr->nodeid); i++) {
823 ctmax = max(ctmax,ct[i]);
824 ctsum += ct[i];
826 ctsum /= cr->nnodes;
827 fprintf(log,"\nTotal NODE time on node %d: %g\n",cr->nodeid,ct[cr->nodeid]);
828 fprintf(log,"Average NODE time: %g\n",ctsum);
829 fprintf(log,"Load imbalance reduced performance to %3d%% of max\n",
830 (int) (100.0*ctmax/ctsum));
831 sfree(ct);
833 if (MASTER(cr)) {
834 print_ebin(fp_ene,FALSE,FALSE,FALSE,log,step,t,
835 eprAVER,FALSE,mdebin,fcd,&(top->atoms));
836 print_ebin(fp_ene,FALSE,FALSE,FALSE,log,step,t,
837 eprRMS,FALSE,mdebin,fcd,&(top->atoms));
838 close_enx(fp_ene);
839 if (!bRerunMD && parm->ir.nstxtcout)
840 close_xtc_traj();
841 close_trn(fp_trn);
842 if (fp_dgdl)
843 fclose(fp_dgdl);
845 debug_gmx();
847 if (bShell) {
848 fprintf(log,"Fraction of iterations that converged: %.2f %%\n",
849 (nconverged*100.0)/(parm->ir.nsteps+1));
850 fprintf(log,"Average number of force evaluations per MD step: %.2f\n",
851 tcount/(parm->ir.nsteps+1));
854 return start_t;