3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
59 /* We use the same defines as in mvdata.c here */
60 #define block_bc(cr, d) gmx_bcast( sizeof(d), &(d),(cr))
61 #define nblock_bc(cr,nr,d) gmx_bcast((nr)*sizeof((d)[0]), (d),(cr))
62 #define snew_bc(cr,d,nr) { if (!MASTER(cr)) snew((d),(nr)); }
64 /* The following two variables and the signal_handler function
65 * is used from pme.c as well
94 int natoms; /*nr of atoms per lipid*/
95 int mol1; /*id of the first lipid molecule*/
116 int search_string(char *s,int ng,char ***gn)
120 for(i=0; (i<ng); i++)
121 if (gmx_strcasecmp(s,*gn[i]) == 0)
124 gmx_fatal(FARGS,"Group %s not found in indexfile.\nMaybe you have non-default groups in your mdp file, while not using the '-n' option of grompp.\nIn that case use the '-n' option.\n",s);
129 int get_mol_id(int at,int nmblock,gmx_molblock_t *mblock, int *type, int *block)
134 for(i=0;i<nmblock;i++)
136 if(at<(mblock[i].nmol*mblock[i].natoms_mol))
138 mol_id+=at/mblock[i].natoms_mol;
139 *type = mblock[i].type;
143 at-= mblock[i].nmol*mblock[i].natoms_mol;
144 mol_id+=mblock[i].nmol;
148 gmx_fatal(FARGS,"Something is wrong in mol ids, at %d, mol_id %d",at,mol_id);
153 int get_block(int mol_id,int nmblock,gmx_molblock_t *mblock)
158 for(i=0;i<nmblock;i++)
160 nmol+=mblock[i].nmol;
165 gmx_fatal(FARGS,"mol_id %d larger than total number of molecules %d.\n",mol_id,nmol);
170 int get_tpr_version(const char *infile)
177 fio = open_tpx(infile,"r");
178 gmx_fio_checktype(fio);
180 precision = sizeof(real);
182 gmx_fio_do_string(fio,buf);
183 if (strncmp(buf,"VERSION",7))
184 gmx_fatal(FARGS,"Can not read file %s,\n"
185 " this file is from a Gromacs version which is older than 2.0\n"
186 " Make a new one with grompp or use a gro or pdb file, if possible",
187 gmx_fio_getname(fio));
188 gmx_fio_do_int(fio,precision);
189 bDouble = (precision == sizeof(double));
190 if ((precision != sizeof(float)) && !bDouble)
191 gmx_fatal(FARGS,"Unknown precision in file %s: real is %d bytes "
192 "instead of %d or %d",
193 gmx_fio_getname(fio),precision,sizeof(float),sizeof(double));
194 gmx_fio_setprecision(fio,bDouble);
195 fprintf(stderr,"Reading file %s, %s (%s precision)\n",
196 gmx_fio_getname(fio),buf,bDouble ? "double" : "single");
198 gmx_fio_do_int(fio,fver);
205 void set_inbox(int natom, rvec *x)
210 tmp[XX]=tmp[YY]=tmp[ZZ]=0.0;
213 if(x[i][XX]<tmp[XX]) tmp[XX]=x[i][XX];
214 if(x[i][YY]<tmp[YY]) tmp[YY]=x[i][YY];
215 if(x[i][ZZ]<tmp[ZZ]) tmp[ZZ]=x[i][ZZ];
222 int get_mtype_list(t_block *at, gmx_mtop_t *mtop, t_block *tlist)
229 snew(tlist->index,at->nr);
230 for (i=0;i<at->nr;i++)
233 mol_id = get_mol_id(at->index[i],mtop->nmolblock,mtop->molblock,&type,&block);
236 if(tlist->index[j]==type)
241 tlist->index[nr]=type;
246 srenew(tlist->index,nr);
250 void check_types(t_block *ins_at,t_block *rest_at,gmx_mtop_t *mtop)
252 t_block *ins_mtype,*rest_mtype;
257 ins_mtype->nr = get_mtype_list(ins_at , mtop, ins_mtype );
258 rest_mtype->nr = get_mtype_list(rest_at, mtop, rest_mtype);
260 for(i=0;i<ins_mtype->nr;i++)
262 for(j=0;j<rest_mtype->nr;j++)
264 if(ins_mtype->index[i]==rest_mtype->index[j])
265 gmx_fatal(FARGS,"Moleculetype %s is found both in the group to insert and the rest of the system.\n"
266 "Because we need to exclude all interactions between the atoms in the group to\n"
267 "insert, the same moleculetype can not be used in both groups. Change the\n"
268 "moleculetype of the molecules %s in the inserted group. Do not forget to provide\n"
269 "an appropriate *.itp file",*(mtop->moltype[rest_mtype->index[j]].name),
270 *(mtop->moltype[rest_mtype->index[j]].name));
274 sfree(ins_mtype->index);
275 sfree(rest_mtype->index);
280 int init_ins_at(t_block *ins_at,t_block *rest_at,t_state *state, pos_ins_t *pos_ins,gmx_groups_t *groups,int ins_grp_id, real xy_max)
283 real x,xmin,xmax,y,ymin,ymax,z,zmin,zmax;
285 snew(rest_at->index,state->natoms);
287 xmin=xmax=state->x[ins_at->index[0]][XX];
288 ymin=ymax=state->x[ins_at->index[0]][YY];
289 zmin=zmax=state->x[ins_at->index[0]][ZZ];
291 for(i=0;i<state->natoms;i++)
293 gid = groups->grpnr[egcFREEZE][i];
294 if(groups->grps[egcFREEZE].nm_ind[gid]==ins_grp_id)
312 srenew(rest_at->index,c);
316 pos_ins->xmin[XX]=xmin-((xmax-xmin)*xy_max-(xmax-xmin))/2;
317 pos_ins->xmin[YY]=ymin-((ymax-ymin)*xy_max-(ymax-ymin))/2;
319 pos_ins->xmax[XX]=xmax+((xmax-xmin)*xy_max-(xmax-xmin))/2;
320 pos_ins->xmax[YY]=ymax+((ymax-ymin)*xy_max-(ymax-ymin))/2;
322 pos_ins->xmin[XX]=xmin;
323 pos_ins->xmin[YY]=ymin;
325 pos_ins->xmax[XX]=xmax;
326 pos_ins->xmax[YY]=ymax;
329 /* 6.0 is estimated thickness of bilayer */
330 if( (zmax-zmin) < 6.0 )
332 pos_ins->xmin[ZZ]=zmin+(zmax-zmin)/2.0-3.0;
333 pos_ins->xmax[ZZ]=zmin+(zmax-zmin)/2.0+3.0;
335 pos_ins->xmin[ZZ]=zmin;
336 pos_ins->xmax[ZZ]=zmax;
342 real est_prot_area(pos_ins_t *pos_ins,rvec *r,t_block *ins_at, mem_t *mem_p)
344 real x,y,dx=0.15,dy=0.15,area=0.0;
348 for(x=pos_ins->xmin[XX];x<pos_ins->xmax[XX];x+=dx)
350 for(y=pos_ins->xmin[YY];y<pos_ins->xmax[YY];y+=dy)
357 if ( (r[at][XX]>=x) && (r[at][XX]<x+dx) &&
358 (r[at][YY]>=y) && (r[at][YY]<y+dy) &&
359 (r[at][ZZ]>mem_p->zmin+1.0) && (r[at][ZZ]<mem_p->zmax-1.0) )
362 } while ( (c<ins_at->nr) && (add<0.5) );
371 void init_lip(matrix box, gmx_mtop_t *mtop, lip_t *lip)
377 mem_area = box[XX][XX]*box[YY][YY]-box[XX][YY]*box[YY][XX];
378 for(i=0;i<mtop->nmolblock;i++)
380 if(mtop->molblock[i].type == lip->id)
382 lip->nr=mtop->molblock[i].nmol;
383 lip->natoms=mtop->molblock[i].natoms_mol;
386 lip->area=2.0*mem_area/(double)lip->nr;
388 for (i=0;i<lip->id;i++)
389 mol1+=mtop->molblock[i].nmol;
393 int init_mem_at(mem_t *mem_p, gmx_mtop_t *mtop, rvec *r, matrix box, pos_ins_t *pos_ins)
395 int i,j,at,mol,nmol,nmolbox,count;
397 real z,zmin,zmax,mem_area;
403 mem_a=&(mem_p->mem_at);
404 snew(mol_id,mem_a->nr);
405 /* snew(index,mem_a->nr); */
406 zmin=pos_ins->xmax[ZZ];
407 zmax=pos_ins->xmin[ZZ];
408 for(i=0;i<mem_a->nr;i++)
411 if( (r[at][XX]>pos_ins->xmin[XX]) && (r[at][XX]<pos_ins->xmax[XX]) &&
412 (r[at][YY]>pos_ins->xmin[YY]) && (r[at][YY]<pos_ins->xmax[YY]) &&
413 (r[at][ZZ]>pos_ins->xmin[ZZ]) && (r[at][ZZ]<pos_ins->xmax[ZZ]) )
415 mol = get_mol_id(at,mtop->nmolblock,mtop->molblock,&type,&block);
432 /* index[count]=at;*/
439 mem_p->mol_id=mol_id;
440 /* srenew(index,count);*/
441 /* mem_p->mem_at.nr=count;*/
442 /* sfree(mem_p->mem_at.index);*/
443 /* mem_p->mem_at.index=index;*/
445 if((zmax-zmin)>(box[ZZ][ZZ]-0.5))
446 gmx_fatal(FARGS,"Something is wrong with your membrane. Max and min z values are %f and %f.\n"
447 "Maybe your membrane is not centered in the box, but located at the box edge in the z-direction,\n"
448 "so that one membrane is distributed over two periodic box images. Another possibility is that\n"
449 "your water layer is not thick enough.\n",zmax,zmin);
452 mem_p->zmed=(zmax-zmin)/2+zmin;
454 /*number of membrane molecules in protein box*/
455 nmolbox = count/mtop->molblock[block].natoms_mol;
456 /*mem_area = box[XX][XX]*box[YY][YY]-box[XX][YY]*box[YY][XX];
457 mem_p->lip_area = 2.0*mem_area/(double)mem_p->nmol;*/
458 mem_area = (pos_ins->xmax[XX]-pos_ins->xmin[XX])*(pos_ins->xmax[YY]-pos_ins->xmin[YY]);
459 mem_p->lip_area = 2.0*mem_area/(double)nmolbox;
461 return mem_p->mem_at.nr;
464 void init_resize(t_block *ins_at,rvec *r_ins,pos_ins_t *pos_ins,mem_t *mem_p,rvec *r, gmx_bool bALLOW_ASYMMETRY)
466 int i,j,at,c,outsidesum,gctr=0;
470 for (i=0;i<pos_ins->pieces;i++)
471 idxsum+=pos_ins->nidx[i];
472 if (idxsum!=ins_at->nr)
473 gmx_fatal(FARGS,"Piecewise sum of inserted atoms not same as size of group selected to insert.");
475 snew(pos_ins->geom_cent,pos_ins->pieces);
476 for (i=0;i<pos_ins->pieces;i++)
481 pos_ins->geom_cent[i][j]=0;
484 pos_ins->geom_cent[i][j]=0;
485 for (j=0;j<pos_ins->nidx[i];j++)
487 at=pos_ins->subindex[i][j];
488 copy_rvec(r[at],r_ins[gctr]);
489 if( (r_ins[gctr][ZZ]<mem_p->zmax) && (r_ins[gctr][ZZ]>mem_p->zmin) )
491 rvec_inc(pos_ins->geom_cent[i],r_ins[gctr]);
499 svmul(1/(double)c,pos_ins->geom_cent[i],pos_ins->geom_cent[i]);
500 if (!bALLOW_ASYMMETRY)
501 pos_ins->geom_cent[i][ZZ]=mem_p->zmed;
503 fprintf(stderr,"Embedding piece %d with center of geometry: %f %f %f\n",i,pos_ins->geom_cent[i][XX],pos_ins->geom_cent[i][YY],pos_ins->geom_cent[i][ZZ]);
505 fprintf(stderr,"\n");
508 void resize(t_block *ins_at, rvec *r_ins, rvec *r, pos_ins_t *pos_ins,rvec fac)
511 for (k=0;k<pos_ins->pieces;k++)
512 for(i=0;i<pos_ins->nidx[k];i++)
514 at=pos_ins->subindex[k][i];
516 r[at][j]=pos_ins->geom_cent[k][j]+fac[j]*(r_ins[c][j]-pos_ins->geom_cent[k][j]);
521 int gen_rm_list(rmm_t *rm_p,t_block *ins_at,t_block *rest_at,t_pbc *pbc, gmx_mtop_t *mtop,
522 rvec *r, rvec *r_ins, mem_t *mem_p, pos_ins_t *pos_ins, real probe_rad, int low_up_rm, gmx_bool bALLOW_ASYMMETRY)
524 int i,j,k,l,at,at2,mol_id;
526 int nrm,nupper,nlower;
527 real r_min_rad,z_lip,min_norm;
533 r_min_rad=probe_rad*probe_rad;
534 snew(rm_p->mol,mtop->mols.nr);
535 snew(rm_p->block,mtop->mols.nr);
538 for(i=0;i<ins_at->nr;i++)
541 for(j=0;j<rest_at->nr;j++)
543 at2=rest_at->index[j];
544 pbc_dx(pbc,r[at],r[at2],dr);
546 if(norm2(dr)<r_min_rad)
548 mol_id = get_mol_id(at2,mtop->nmolblock,mtop->molblock,&type,&block);
551 if(rm_p->mol[l]==mol_id)
555 /*fprintf(stderr,"%d wordt toegevoegd\n",mol_id);*/
556 rm_p->mol[nrm]=mol_id;
557 rm_p->block[nrm]=block;
560 for(l=0;l<mem_p->nmol;l++)
562 if(mol_id==mem_p->mol_id[l])
564 for(k=mtop->mols.index[mol_id];k<mtop->mols.index[mol_id+1];k++)
566 z_lip/=mtop->molblock[block].natoms_mol;
567 if(z_lip<mem_p->zmed)
578 /*make sure equal number of lipids from upper and lower layer are removed */
579 if( (nupper!=nlower) && (!bALLOW_ASYMMETRY) )
581 snew(dist,mem_p->nmol);
582 snew(order,mem_p->nmol);
583 for(i=0;i<mem_p->nmol;i++)
585 at = mtop->mols.index[mem_p->mol_id[i]];
586 pbc_dx(pbc,r[at],pos_ins->geom_cent[0],dr);
587 if (pos_ins->pieces>1)
591 for (k=1;k<pos_ins->pieces;k++)
593 pbc_dx(pbc,r[at],pos_ins->geom_cent[k],dr_tmp);
594 if (norm2(dr_tmp) < min_norm)
596 min_norm=norm2(dr_tmp);
597 copy_rvec(dr_tmp,dr);
601 dist[i]=dr[XX]*dr[XX]+dr[YY]*dr[YY];
603 while (j>=0 && dist[i]<dist[order[j]])
612 while(nupper!=nlower)
614 mol_id=mem_p->mol_id[order[i]];
615 block=get_block(mol_id,mtop->nmolblock,mtop->molblock);
619 if(rm_p->mol[l]==mol_id)
624 for(k=mtop->mols.index[mol_id];k<mtop->mols.index[mol_id+1];k++)
626 z_lip/=mtop->molblock[block].natoms_mol;
627 if(nupper>nlower && z_lip<mem_p->zmed)
629 rm_p->mol[nrm]=mol_id;
630 rm_p->block[nrm]=block;
634 else if (nupper<nlower && z_lip>mem_p->zmed)
636 rm_p->mol[nrm]=mol_id;
637 rm_p->block[nrm]=block;
645 gmx_fatal(FARGS,"Trying to remove more lipid molecules than there are in the membrane");
652 srenew(rm_p->mol,nrm);
653 srenew(rm_p->block,nrm);
655 return nupper+nlower;
658 void rm_group(t_inputrec *ir, gmx_groups_t *groups, gmx_mtop_t *mtop, rmm_t *rm_p, t_state *state, t_block *ins_at, pos_ins_t *pos_ins)
660 int i,j,k,n,rm,mol_id,at,block;
662 atom_id *list,*new_mols;
663 unsigned char *new_egrp[egcNR];
666 snew(list,state->natoms);
668 for(i=0;i<rm_p->nr;i++)
671 at=mtop->mols.index[mol_id];
672 block =rm_p->block[i];
673 mtop->molblock[block].nmol--;
674 for(j=0;j<mtop->molblock[block].natoms_mol;j++)
680 mtop->mols.index[mol_id]=-1;
683 mtop->mols.nr-=rm_p->nr;
684 mtop->mols.nalloc_index-=rm_p->nr;
685 snew(new_mols,mtop->mols.nr);
686 for(i=0;i<mtop->mols.nr+rm_p->nr;i++)
689 if(mtop->mols.index[i]!=-1)
691 new_mols[j]=mtop->mols.index[i];
695 sfree(mtop->mols.index);
696 mtop->mols.index=new_mols;
701 state->nalloc=state->natoms;
702 snew(x_tmp,state->nalloc);
703 snew(v_tmp,state->nalloc);
707 if(groups->grpnr[i]!=NULL)
709 groups->ngrpnr[i]=state->natoms;
710 snew(new_egrp[i],state->natoms);
715 for (i=0;i<state->natoms+n;i++)
731 if(groups->grpnr[j]!=NULL)
733 new_egrp[j][i-rm]=groups->grpnr[j][i];
736 copy_rvec(state->x[i],x_tmp[i-rm]);
737 copy_rvec(state->v[i],v_tmp[i-rm]);
738 for(j=0;j<ins_at->nr;j++)
740 if (i==ins_at->index[j])
741 ins_at->index[j]=i-rm;
743 for(j=0;j<pos_ins->pieces;j++)
745 for(k=0;k<pos_ins->nidx[j];k++)
747 if (i==pos_ins->subindex[j][k])
748 pos_ins->subindex[j][k]=i-rm;
760 if(groups->grpnr[i]!=NULL)
762 sfree(groups->grpnr[i]);
763 groups->grpnr[i]=new_egrp[i];
768 int rm_bonded(t_block *ins_at, gmx_mtop_t *mtop)
771 int type,natom,nmol,at,atom1=0,rm_at=0;
773 /*this routine lives dangerously by assuming that all molecules of a given type are in order in the structure*/
774 /*this routine does not live as dangerously as it seems. There is namely a check in mdrunner_membed to make
775 *sure that g_membed exits with a warning when there are molecules of the same type not in the
776 *ins_at index group. MGWolf 050710 */
779 snew(bRM,mtop->nmoltype);
780 for (i=0;i<mtop->nmoltype;i++)
785 for (i=0;i<mtop->nmolblock;i++)
787 /*loop over molecule blocks*/
788 type =mtop->molblock[i].type;
789 natom =mtop->molblock[i].natoms_mol;
790 nmol =mtop->molblock[i].nmol;
792 for(j=0;j<natom*nmol && bRM[type]==TRUE;j++)
794 /*loop over atoms in the block*/
795 at=j+atom1; /*atom index = block index + offset*/
798 for (m=0;(m<ins_at->nr) && (bINS==FALSE);m++)
800 /*loop over atoms in insertion index group to determine if we're inserting one*/
801 if(at==ins_at->index[m])
808 atom1+=natom*nmol; /*update offset*/
811 rm_at+=natom*nmol; /*increment bonded removal counter by # atoms in block*/
815 for(i=0;i<mtop->nmoltype;i++)
821 mtop->moltype[i].ilist[j].nr=0;
823 for(j=F_POSRES;j<=F_VSITEN;j++)
825 mtop->moltype[i].ilist[j].nr=0;
834 void top_update(const char *topfile, char *ins, rmm_t *rm_p, gmx_mtop_t *mtop)
836 #define TEMP_FILENM "temp.top"
839 char buf[STRLEN],buf2[STRLEN],*temp;
840 int i,*nmol_rm,nmol,line;
842 fpin = ffopen(topfile,"r");
843 fpout = ffopen(TEMP_FILENM,"w");
845 snew(nmol_rm,mtop->nmoltype);
846 for(i=0;i<rm_p->nr;i++)
847 nmol_rm[rm_p->block[i]]++;
850 while(fgets(buf,STRLEN,fpin))
856 if ((temp=strchr(buf2,'\n')) != NULL)
863 if ((temp=strchr(buf2,'\n')) != NULL)
866 if (buf2[strlen(buf2)-1]==']')
868 buf2[strlen(buf2)-1]='\0';
871 if (gmx_strcasecmp(buf2,"molecules")==0)
874 fprintf(fpout,"%s",buf);
875 } else if (bMolecules==1)
877 for(i=0;i<mtop->nmolblock;i++)
879 nmol=mtop->molblock[i].nmol;
880 sprintf(buf,"%-15s %5d\n",*(mtop->moltype[mtop->molblock[i].type].name),nmol);
881 fprintf(fpout,"%s",buf);
884 } else if (bMolecules==2)
889 fprintf(fpout,"%s",buf);
893 fprintf(fpout,"%s",buf);
898 /* use ffopen to generate backup of topinout */
899 fpout=ffopen(topfile,"w");
901 rename(TEMP_FILENM,topfile);
905 double do_md_membed(FILE *fplog,t_commrec *cr,int nfile,const t_filenm fnm[],
906 const output_env_t oenv, gmx_bool bVerbose,gmx_bool bCompact,
908 gmx_vsite_t *vsite,gmx_constr_t constr,
909 int stepout,t_inputrec *ir,
910 gmx_mtop_t *top_global,
912 t_state *state_global,
914 t_nrnb *nrnb,gmx_wallcycle_t wcycle,
915 gmx_edsam_t ed,t_forcerec *fr,
916 int repl_ex_nst,int repl_ex_seed,
917 real cpt_period,real max_hours,
918 const char *deviceOptions,
920 gmx_runtime_t *runtime,
921 rvec fac, rvec *r_ins, pos_ins_t *pos_ins, t_block *ins_at,
922 real xy_step, real z_step, int it_xy, int it_z)
925 gmx_large_int_t step,step_rel;
928 gmx_bool bGStatEveryStep,bGStat,bNstEner,bCalcEnerPres;
929 gmx_bool bNS,bNStList,bSimAnn,bStopCM,bRerunMD,bNotLastFrame=FALSE,
930 bFirstStep,bStateFromTPX,bInitStep,bLastStep,
931 bBornRadii,bStartingFromCpt;
932 gmx_bool bDoDHDL=FALSE;
933 gmx_bool do_ene,do_log,do_verbose,bRerunWarnNoV=TRUE,
934 bForceUpdate=FALSE,bCPT;
936 gmx_bool bMasterState;
937 int force_flags,cglo_flags;
938 tensor force_vir,shake_vir,total_vir,tmp_vir,pres;
943 t_state *bufstate=NULL;
944 matrix *scale_tot,pcoupl_mu,M,ebox;
947 /* gmx_repl_ex_t repl_ex=NULL;*/
951 t_mdebin *mdebin=NULL;
956 gmx_enerdata_t *enerd;
958 gmx_global_stat_t gstat;
959 gmx_update_t upd=NULL;
964 gmx_groups_t *groups;
965 gmx_ekindata_t *ekind, *ekind_save;
966 gmx_shellfc_t shellfc;
967 int count,nconverged=0;
970 gmx_bool bIonize=FALSE;
971 gmx_bool bTCR=FALSE,bConverged=TRUE,bOK,bSumEkinhOld,bExchanged;
973 gmx_bool bResetCountersHalfMaxH=FALSE;
974 gmx_bool bVV,bIterations,bIterate,bFirstIterate,bTemp,bPres,bTrotter;
977 rvec *xcopy=NULL,*vcopy=NULL,*cbuf=NULL;
978 matrix boxcopy={{0}},lastbox;
979 real veta_save,pcurr,scalevir,tracevir;
982 real last_conserved = 0;
986 char sbuf[STEPSTRSIZE],sbuf2[STEPSTRSIZE];
987 int handled_stop_condition=gmx_stop_cond_none; /* compare to get_stop_condition*/
988 gmx_iterate_t iterate;
990 /* Temporary addition for FAHCORE checkpointing */
994 /* Check for special mdrun options */
995 bRerunMD = (Flags & MD_RERUN);
996 bIonize = (Flags & MD_IONIZE);
997 bFFscan = (Flags & MD_FFSCAN);
998 bAppend = (Flags & MD_APPENDFILES);
999 bGStatEveryStep = FALSE;
1000 if (Flags & MD_RESETCOUNTERSHALFWAY)
1004 /* Signal to reset the counters half the simulation steps. */
1005 wcycle_set_reset_counters(wcycle,ir->nsteps/2);
1007 /* Signal to reset the counters halfway the simulation time. */
1008 bResetCountersHalfMaxH = (max_hours > 0);
1011 /* md-vv uses averaged full step velocities for T-control
1012 md-vv-avek uses averaged half step velocities for T-control (but full step ekin for P control)
1013 md uses averaged half step kinetic energies to determine temperature unless defined otherwise by GMX_EKIN_AVE_VEL; */
1014 bVV = EI_VV(ir->eI);
1015 if (bVV) /* to store the initial velocities while computing virial */
1017 snew(cbuf,top_global->natoms);
1019 /* all the iteratative cases - only if there are constraints */
1020 bIterations = ((IR_NPT_TROTTER(ir)) && (constr) && (!bRerunMD));
1021 bTrotter = (bVV && (IR_NPT_TROTTER(ir) || (IR_NVT_TROTTER(ir))));
1025 /* Since we don't know if the frames read are related in any way,
1026 * rebuild the neighborlist at every step.
1029 ir->nstcalcenergy = 1;
1033 check_ir_old_tpx_versions(cr,fplog,ir,top_global);
1035 nstglobalcomm = check_nstglobalcomm(fplog,cr,nstglobalcomm,ir);
1036 /*bGStatEveryStep = (nstglobalcomm == 1);*/
1037 bGStatEveryStep = FALSE;
1039 if (!bGStatEveryStep && ir->nstlist == -1 && fplog != NULL)
1042 "To reduce the energy communication with nstlist = -1\n"
1043 "the neighbor list validity should not be checked at every step,\n"
1044 "this means that exact integration is not guaranteed.\n"
1045 "The neighbor list validity is checked after:\n"
1046 " <n.list life time> - 2*std.dev.(n.list life time) steps.\n"
1047 "In most cases this will result in exact integration.\n"
1048 "This reduces the energy communication by a factor of 2 to 3.\n"
1049 "If you want less energy communication, set nstlist > 3.\n\n");
1052 if (bRerunMD || bFFscan)
1056 groups = &top_global->groups;
1058 /* Initial values */
1059 init_md(fplog,cr,ir,oenv,&t,&t0,&state_global->lambda,&lam0,
1060 nrnb,top_global,&upd,
1061 nfile,fnm,&outf,&mdebin,
1062 force_vir,shake_vir,mu_tot,&bSimAnn,&vcm,state_global,Flags);
1064 clear_mat(total_vir);
1066 /* Energy terms and groups */
1068 init_enerdata(top_global->groups.grps[egcENER].nr,ir->n_flambda,enerd);
1069 if (DOMAINDECOMP(cr))
1075 snew(f,top_global->natoms);
1078 /* Kinetic energy data */
1080 init_ekindata(fplog,top_global,&(ir->opts),ekind);
1081 /* needed for iteration of constraints */
1083 init_ekindata(fplog,top_global,&(ir->opts),ekind_save);
1084 /* Copy the cos acceleration to the groups struct */
1085 ekind->cosacc.cos_accel = ir->cos_accel;
1087 gstat = global_stat_init(ir);
1090 /* Check for polarizable models and flexible constraints */
1091 shellfc = init_shell_flexcon(fplog,
1092 top_global,n_flexible_constraints(constr),
1093 (ir->bContinuation ||
1094 (DOMAINDECOMP(cr) && !MASTER(cr))) ?
1095 NULL : state_global->x);
1100 tMPI_Thread_mutex_lock(&deform_init_box_mutex);
1102 set_deform_reference_box(upd,
1103 deform_init_init_step_tpx,
1104 deform_init_box_tpx);
1106 tMPI_Thread_mutex_unlock(&deform_init_box_mutex);
1111 double io = compute_io(ir,top_global->natoms,groups,mdebin->ebin->nener,1);
1112 if ((io > 2000) && MASTER(cr))
1114 "\nWARNING: This run will generate roughly %.0f Mb of data\n\n",
1118 if (DOMAINDECOMP(cr)) {
1119 top = dd_init_local_top(top_global);
1122 dd_init_local_state(cr->dd,state_global,state);
1124 if (DDMASTER(cr->dd) && ir->nstfout) {
1125 snew(f_global,state_global->natoms);
1129 /* Initialize the particle decomposition and split the topology */
1130 top = split_system(fplog,top_global,ir,cr);
1132 pd_cg_range(cr,&fr->cg0,&fr->hcg);
1133 pd_at_range(cr,&a0,&a1);
1135 top = gmx_mtop_generate_local_top(top_global,ir);
1138 a1 = top_global->natoms;
1141 state = partdec_init_local_state(cr,state_global);
1144 atoms2md(top_global,ir,0,NULL,a0,a1-a0,mdatoms);
1147 set_vsite_top(vsite,top,mdatoms,cr);
1150 if (ir->ePBC != epbcNONE && !ir->bPeriodicMols) {
1151 graph = mk_graph(fplog,&(top->idef),0,top_global->natoms,FALSE,FALSE);
1155 make_local_shells(cr,mdatoms,shellfc);
1158 if (ir->pull && PAR(cr)) {
1159 dd_make_local_pull_groups(NULL,ir->pull,mdatoms);
1163 if (DOMAINDECOMP(cr))
1165 /* Distribute the charge groups over the nodes from the master node */
1166 dd_partition_system(fplog,ir->init_step,cr,TRUE,1,
1167 state_global,top_global,ir,
1168 state,&f,mdatoms,top,fr,
1169 vsite,shellfc,constr,
1173 update_mdatoms(mdatoms,state->lambda);
1177 if (opt2bSet("-cpi",nfile,fnm))
1179 /* Update mdebin with energy history if appending to output files */
1180 if ( Flags & MD_APPENDFILES )
1182 restore_energyhistory_from_state(mdebin,&state_global->enerhist);
1186 /* We might have read an energy history from checkpoint,
1187 * free the allocated memory and reset the counts.
1189 done_energyhistory(&state_global->enerhist);
1190 init_energyhistory(&state_global->enerhist);
1193 /* Set the initial energy history in state by updating once */
1194 update_energyhistory(&state_global->enerhist,mdebin);
1197 if ((state->flags & (1<<estLD_RNG)) && (Flags & MD_READ_RNG)) {
1198 /* Set the random state if we read a checkpoint file */
1199 set_stochd_state(upd,state);
1202 /* Initialize constraints */
1204 if (!DOMAINDECOMP(cr))
1205 set_constraints(constr,top,ir,mdatoms,cr);
1208 /* Check whether we have to GCT stuff */
1209 /* bTCR = ftp2bSet(efGCT,nfile,fnm);
1212 fprintf(stderr,"Will do General Coupling Theory!\n");
1214 gnx = top_global->mols.nr;
1216 for(i=0; (i<gnx); i++) {
1221 /* if (repl_ex_nst > 0 && MASTER(cr))
1222 repl_ex = init_replica_exchange(fplog,cr->ms,state_global,ir,
1223 repl_ex_nst,repl_ex_seed);*/
1225 if (!ir->bContinuation && !bRerunMD)
1227 if (mdatoms->cFREEZE && (state->flags & (1<<estV)))
1229 /* Set the velocities of frozen particles to zero */
1230 for(i=mdatoms->start; i<mdatoms->start+mdatoms->homenr; i++)
1232 for(m=0; m<DIM; m++)
1234 if (ir->opts.nFreeze[mdatoms->cFREEZE[i]][m])
1244 /* Constrain the initial coordinates and velocities */
1245 do_constrain_first(fplog,constr,ir,mdatoms,state,f,
1246 graph,cr,nrnb,fr,top,shake_vir);
1250 /* Construct the virtual sites for the initial configuration */
1251 construct_vsites(fplog,vsite,state->x,nrnb,ir->delta_t,NULL,
1252 top->idef.iparams,top->idef.il,
1253 fr->ePBC,fr->bMolPBC,graph,cr,state->box);
1259 /* I'm assuming we need global communication the first time! MRS */
1260 cglo_flags = (CGLO_TEMPERATURE | CGLO_GSTAT
1261 | (bVV ? CGLO_PRESSURE:0)
1262 | (bVV ? CGLO_CONSTRAINT:0)
1263 | (bRerunMD ? CGLO_RERUNMD:0)
1264 | ((Flags & MD_READ_EKIN) ? CGLO_READEKIN:0));
1266 bSumEkinhOld = FALSE;
1267 compute_globals(fplog,gstat,cr,ir,fr,ekind,state,state_global,mdatoms,nrnb,vcm,
1268 wcycle,enerd,force_vir,shake_vir,total_vir,pres,mu_tot,
1269 constr,NULL,FALSE,state->box,
1270 top_global,&pcurr,top_global->natoms,&bSumEkinhOld,cglo_flags);
1271 if (ir->eI == eiVVAK) {
1272 /* a second call to get the half step temperature initialized as well */
1273 /* we do the same call as above, but turn the pressure off -- internally, this
1274 is recognized as a velocity verlet half-step kinetic energy calculation.
1275 This minimized excess variables, but perhaps loses some logic?*/
1277 compute_globals(fplog,gstat,cr,ir,fr,ekind,state,state_global,mdatoms,nrnb,vcm,
1278 wcycle,enerd,force_vir,shake_vir,total_vir,pres,mu_tot,
1279 constr,NULL,FALSE,state->box,
1280 top_global,&pcurr,top_global->natoms,&bSumEkinhOld,
1281 cglo_flags &~ CGLO_PRESSURE);
1284 /* Calculate the initial half step temperature, and save the ekinh_old */
1285 if (!(Flags & MD_STARTFROMCPT))
1287 for(i=0; (i<ir->opts.ngtc); i++)
1289 copy_mat(ekind->tcstat[i].ekinh,ekind->tcstat[i].ekinh_old);
1294 enerd->term[F_TEMP] *= 2; /* result of averages being done over previous and current step,
1295 and there is no previous step */
1297 temp0 = enerd->term[F_TEMP];
1299 /* if using an iterative algorithm, we need to create a working directory for the state. */
1302 bufstate = init_bufstate(state);
1306 snew(xcopy,state->natoms);
1307 snew(vcopy,state->natoms);
1308 copy_rvecn(state->x,xcopy,0,state->natoms);
1309 copy_rvecn(state->v,vcopy,0,state->natoms);
1310 copy_mat(state->box,boxcopy);
1313 /* need to make an initiation call to get the Trotter variables set, as well as other constants for non-trotter
1314 temperature control */
1315 trotter_seq = init_npt_vars(ir,state,&MassQ,bTrotter);
1319 if (constr && !ir->bContinuation && ir->eConstrAlg == econtLINCS)
1322 "RMS relative constraint deviation after constraining: %.2e\n",
1323 constr_rmsd(constr,FALSE));
1325 fprintf(fplog,"Initial temperature: %g K\n",enerd->term[F_TEMP]);
1328 fprintf(stderr,"starting md rerun '%s', reading coordinates from"
1329 " input trajectory '%s'\n\n",
1330 *(top_global->name),opt2fn("-rerun",nfile,fnm));
1333 fprintf(stderr,"Calculated time to finish depends on nsteps from "
1334 "run input file,\nwhich may not correspond to the time "
1335 "needed to process input trajectory.\n\n");
1341 fprintf(stderr,"starting mdrun '%s'\n",
1342 *(top_global->name));
1343 if (ir->nsteps >= 0)
1345 sprintf(tbuf,"%8.1f",(ir->init_step+ir->nsteps)*ir->delta_t);
1349 sprintf(tbuf,"%s","infinite");
1351 if (ir->init_step > 0)
1353 fprintf(stderr,"%s steps, %s ps (continuing from step %s, %8.1f ps).\n",
1354 gmx_step_str(ir->init_step+ir->nsteps,sbuf),tbuf,
1355 gmx_step_str(ir->init_step,sbuf2),
1356 ir->init_step*ir->delta_t);
1360 fprintf(stderr,"%s steps, %s ps.\n",
1361 gmx_step_str(ir->nsteps,sbuf),tbuf);
1364 fprintf(fplog,"\n");
1367 /* Set and write start time */
1368 runtime_start(runtime);
1369 print_date_and_time(fplog,cr->nodeid,"Started mdrun",runtime);
1370 wallcycle_start(wcycle,ewcRUN);
1372 fprintf(fplog,"\n");
1374 /* safest point to do file checkpointing is here. More general point would be immediately before integrator call */
1375 /*#ifdef GMX_FAHCORE
1376 chkpt_ret=fcCheckPointParallel( cr->nodeid,
1378 if ( chkpt_ret == 0 )
1379 gmx_fatal( 3,__FILE__,__LINE__, "Checkpoint error on step %d\n", 0 );
1383 /***********************************************************
1385 * Loop over MD steps
1387 ************************************************************/
1389 /* if rerunMD then read coordinates and velocities from input trajectory */
1392 if (getenv("GMX_FORCE_UPDATE"))
1394 bForceUpdate = TRUE;
1397 bNotLastFrame = read_first_frame(oenv,&status,
1398 opt2fn("-rerun",nfile,fnm),
1399 &rerun_fr,TRX_NEED_X | TRX_READ_V);
1400 if (rerun_fr.natoms != top_global->natoms)
1403 "Number of atoms in trajectory (%d) does not match the "
1404 "run input file (%d)\n",
1405 rerun_fr.natoms,top_global->natoms);
1407 if (ir->ePBC != epbcNONE)
1411 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);
1413 if (max_cutoff2(ir->ePBC,rerun_fr.box) < sqr(fr->rlistlong))
1415 gmx_fatal(FARGS,"Rerun trajectory frame step %d time %f has too small box dimensions",rerun_fr.step,rerun_fr.time);
1418 /* Set the shift vectors.
1419 * Necessary here when have a static box different from the tpr box.
1421 calc_shifts(rerun_fr.box,fr->shift_vec);
1425 /* loop over MD steps or if rerunMD to end of input trajectory */
1427 /* Skip the first Nose-Hoover integration when we get the state from tpx */
1428 bStateFromTPX = !opt2bSet("-cpi",nfile,fnm);
1429 bInitStep = bFirstStep && (bStateFromTPX || bVV);
1430 bStartingFromCpt = (Flags & MD_STARTFROMCPT) && bInitStep;
1432 bSumEkinhOld = FALSE;
1435 init_global_signals(&gs,cr,ir,repl_ex_nst);
1437 step = ir->init_step;
1440 if (ir->nstlist == -1)
1442 init_nlistheuristics(&nlh,bGStatEveryStep,step);
1445 bLastStep = (bRerunMD || (ir->nsteps >= 0 && step_rel > ir->nsteps));
1446 while (!bLastStep || (bRerunMD && bNotLastFrame)) {
1448 wallcycle_start(wcycle,ewcSTEP);
1450 GMX_MPE_LOG(ev_timestep1);
1453 if (rerun_fr.bStep) {
1454 step = rerun_fr.step;
1455 step_rel = step - ir->init_step;
1457 if (rerun_fr.bTime) {
1467 bLastStep = (step_rel == ir->nsteps);
1468 t = t0 + step*ir->delta_t;
1471 if (ir->efep != efepNO)
1473 if (bRerunMD && rerun_fr.bLambda && (ir->delta_lambda!=0))
1475 state_global->lambda = rerun_fr.lambda;
1479 state_global->lambda = lam0 + step*ir->delta_lambda;
1481 state->lambda = state_global->lambda;
1482 bDoDHDL = do_per_step(step,ir->nstdhdl);
1487 update_annealing_target_temp(&(ir->opts),t);
1492 if (!(DOMAINDECOMP(cr) && !MASTER(cr)))
1494 for(i=0; i<state_global->natoms; i++)
1496 copy_rvec(rerun_fr.x[i],state_global->x[i]);
1500 for(i=0; i<state_global->natoms; i++)
1502 copy_rvec(rerun_fr.v[i],state_global->v[i]);
1507 for(i=0; i<state_global->natoms; i++)
1509 clear_rvec(state_global->v[i]);
1513 fprintf(stderr,"\nWARNING: Some frames do not contain velocities.\n"
1514 " Ekin, temperature and pressure are incorrect,\n"
1515 " the virial will be incorrect when constraints are present.\n"
1517 bRerunWarnNoV = FALSE;
1521 copy_mat(rerun_fr.box,state_global->box);
1522 copy_mat(state_global->box,state->box);
1524 if (vsite && (Flags & MD_RERUN_VSITE))
1526 if (DOMAINDECOMP(cr))
1528 gmx_fatal(FARGS,"Vsite recalculation with -rerun is not implemented for domain decomposition, use particle decomposition");
1532 /* Following is necessary because the graph may get out of sync
1533 * with the coordinates if we only have every N'th coordinate set
1535 mk_mshift(fplog,graph,fr->ePBC,state->box,state->x);
1536 shift_self(graph,state->box,state->x);
1538 construct_vsites(fplog,vsite,state->x,nrnb,ir->delta_t,state->v,
1539 top->idef.iparams,top->idef.il,
1540 fr->ePBC,fr->bMolPBC,graph,cr,state->box);
1543 unshift_self(graph,state->box,state->x);
1548 /* Stop Center of Mass motion */
1549 bStopCM = (ir->comm_mode != ecmNO && do_per_step(step,ir->nstcomm));
1551 /* Copy back starting coordinates in case we're doing a forcefield scan */
1554 for(ii=0; (ii<state->natoms); ii++)
1556 copy_rvec(xcopy[ii],state->x[ii]);
1557 copy_rvec(vcopy[ii],state->v[ii]);
1559 copy_mat(boxcopy,state->box);
1564 /* for rerun MD always do Neighbour Searching */
1565 bNS = (bFirstStep || ir->nstlist != 0);
1570 /* Determine whether or not to do Neighbour Searching and LR */
1571 bNStList = (ir->nstlist > 0 && step % ir->nstlist == 0);
1573 bNS = (bFirstStep || bExchanged || bNStList ||
1574 (ir->nstlist == -1 && nlh.nabnsb > 0));
1576 if (bNS && ir->nstlist == -1)
1578 set_nlistheuristics(&nlh,bFirstStep || bExchanged,step);
1582 /* < 0 means stop at next step, > 0 means stop at next NS step */
1583 if ( (gs.set[eglsSTOPCOND] < 0 ) ||
1584 ( (gs.set[eglsSTOPCOND] > 0 ) && ( bNS || ir->nstlist==0)) )
1589 /* Determine whether or not to update the Born radii if doing GB */
1590 bBornRadii=bFirstStep;
1591 if (ir->implicit_solvent && (step % ir->nstgbradii==0))
1596 do_log = do_per_step(step,ir->nstlog) || bFirstStep || bLastStep;
1597 do_verbose = bVerbose &&
1598 (step % stepout == 0 || bFirstStep || bLastStep);
1600 if (bNS && !(bFirstStep && ir->bContinuation && !bRerunMD))
1604 bMasterState = TRUE;
1608 bMasterState = FALSE;
1609 /* Correct the new box if it is too skewed */
1610 if (DYNAMIC_BOX(*ir))
1612 if (correct_box(fplog,step,state->box,graph))
1614 bMasterState = TRUE;
1617 if (DOMAINDECOMP(cr) && bMasterState)
1619 dd_collect_state(cr->dd,state,state_global);
1623 if (DOMAINDECOMP(cr))
1625 /* Repartition the domain decomposition */
1626 wallcycle_start(wcycle,ewcDOMDEC);
1627 dd_partition_system(fplog,step,cr,
1628 bMasterState,nstglobalcomm,
1629 state_global,top_global,ir,
1630 state,&f,mdatoms,top,fr,
1631 vsite,shellfc,constr,
1632 nrnb,wcycle,do_verbose);
1633 wallcycle_stop(wcycle,ewcDOMDEC);
1634 /* If using an iterative integrator, reallocate space to match the decomposition */
1638 if (MASTER(cr) && do_log && !bFFscan)
1640 print_ebin_header(fplog,step,t,state->lambda);
1643 if (ir->efep != efepNO)
1645 update_mdatoms(mdatoms,state->lambda);
1648 if (bRerunMD && rerun_fr.bV)
1651 /* We need the kinetic energy at minus the half step for determining
1652 * the full step kinetic energy and possibly for T-coupling.*/
1653 /* This may not be quite working correctly yet . . . . */
1654 compute_globals(fplog,gstat,cr,ir,fr,ekind,state,state_global,mdatoms,nrnb,vcm,
1655 wcycle,enerd,NULL,NULL,NULL,NULL,mu_tot,
1656 constr,NULL,FALSE,state->box,
1657 top_global,&pcurr,top_global->natoms,&bSumEkinhOld,
1658 CGLO_RERUNMD | CGLO_GSTAT | CGLO_TEMPERATURE);
1660 clear_mat(force_vir);
1662 /* Ionize the atoms if necessary */
1665 ionize(fplog,oenv,mdatoms,top_global,t,ir,state->x,state->v,
1666 mdatoms->start,mdatoms->start+mdatoms->homenr,state->box,cr);
1669 /* Update force field in ffscan program */
1672 if (update_forcefield(fplog,
1674 mdatoms->nr,state->x,state->box)) {
1675 if (gmx_parallel_env_initialized())
1683 GMX_MPE_LOG(ev_timestep2);
1685 /* We write a checkpoint at this MD step when:
1686 * either at an NS step when we signalled through gs,
1687 * or at the last step (but not when we do not want confout),
1688 * but never at the first step or with rerun.
1690 /* bCPT = (((gs.set[eglsCHKPT] && bNS) ||
1691 (bLastStep && (Flags & MD_CONFOUT))) &&
1692 step > ir->init_step && !bRerunMD);
1695 gs.set[eglsCHKPT] = 0;
1698 /* Determine the energy and pressure:
1699 * at nstcalcenergy steps and at energy output steps (set below).
1701 bNstEner = (bGStatEveryStep || do_per_step(step,ir->nstcalcenergy));
1702 bCalcEnerPres = bNstEner;
1704 /* Do we need global communication ? */
1705 bGStat = (bCalcEnerPres || bStopCM ||
1706 (ir->nstlist == -1 && !bRerunMD && step >= nlh.step_nscheck));
1708 do_ene = (do_per_step(step,ir->nstenergy) || bLastStep);
1710 if (do_ene || do_log)
1712 bCalcEnerPres = TRUE;
1716 /* these CGLO_ options remain the same throughout the iteration */
1717 cglo_flags = ((bRerunMD ? CGLO_RERUNMD : 0) |
1718 (bStopCM ? CGLO_STOPCM : 0) |
1719 (bGStat ? CGLO_GSTAT : 0)
1722 force_flags = (GMX_FORCE_STATECHANGED |
1723 ((DYNAMIC_BOX(*ir) || bRerunMD) ? GMX_FORCE_DYNAMICBOX : 0) |
1724 GMX_FORCE_ALLFORCES |
1725 (bNStList ? GMX_FORCE_DOLR : 0) |
1727 (bCalcEnerPres ? GMX_FORCE_VIRIAL : 0) |
1728 (bDoDHDL ? GMX_FORCE_DHDL : 0)
1733 /* Now is the time to relax the shells */
1734 count=relax_shell_flexcon(fplog,cr,bVerbose,bFFscan ? step+1 : step,
1736 bStopCM,top,top_global,
1738 state,f,force_vir,mdatoms,
1739 nrnb,wcycle,graph,groups,
1740 shellfc,fr,bBornRadii,t,mu_tot,
1741 state->natoms,&bConverged,vsite,
1752 /* The coordinates (x) are shifted (to get whole molecules)
1754 * This is parallellized as well, and does communication too.
1755 * Check comments in sim_util.c
1758 do_force(fplog,cr,ir,step,nrnb,wcycle,top,top_global,groups,
1759 state->box,state->x,&state->hist,
1760 f,force_vir,mdatoms,enerd,fcd,
1761 state->lambda,graph,
1762 fr,vsite,mu_tot,t,outf->fp_field,ed,bBornRadii,
1763 (bNS ? GMX_FORCE_NS : 0) | force_flags);
1766 GMX_BARRIER(cr->mpi_comm_mygroup);
1770 mu_aver = calc_mu_aver(cr,state->x,mdatoms->chargeA,
1771 mu_tot,&top_global->mols,mdatoms,gnx,grpindex);
1774 if (bTCR && bFirstStep)
1776 tcr=init_coupling(fplog,nfile,fnm,cr,fr,mdatoms,&(top->idef));
1777 fprintf(fplog,"Done init_coupling\n");
1781 /* ############### START FIRST UPDATE HALF-STEP ############### */
1783 if (bVV && !bStartingFromCpt && !bRerunMD)
1789 /* if using velocity verlet with full time step Ekin,
1790 * take the first half step only to compute the
1791 * virial for the first step. From there,
1792 * revert back to the initial coordinates
1793 * so that the input is actually the initial step.
1795 copy_rvecn(state->v,cbuf,0,state->natoms); /* should make this better for parallelizing? */
1798 /* this is for NHC in the Ekin(t+dt/2) version of vv */
1801 trotter_update(ir,step,ekind,enerd,state,total_vir,mdatoms,&MassQ,trotter_seq,ettTSEQ2);
1804 if (ir->eI == eiVVAK)
1806 update_tcouple(fplog,step,ir,state,ekind,wcycle,upd,&MassQ,mdatoms);
1809 update_coords(fplog,step,ir,mdatoms,state,
1810 f,fr->bTwinRange && bNStList,fr->f_twin,fcd,
1811 ekind,M,wcycle,upd,bInitStep,etrtVELOCITY1,
1812 cr,nrnb,constr,&top->idef);
1816 gmx_iterate_init(&iterate,bIterations && !bInitStep);
1818 /* for iterations, we save these vectors, as we will be self-consistently iterating
1820 /*#### UPDATE EXTENDED VARIABLES IN TROTTER FORMULATION */
1822 /* save the state */
1823 if (bIterations && iterate.bIterate) {
1824 copy_coupling_state(state,bufstate,ekind,ekind_save,&(ir->opts));
1828 bFirstIterate = TRUE;
1829 while (bFirstIterate || (bIterations && iterate.bIterate))
1831 if (bIterations && iterate.bIterate)
1833 copy_coupling_state(bufstate,state,ekind_save,ekind,&(ir->opts));
1834 if (bFirstIterate && bTrotter)
1836 /* The first time through, we need a decent first estimate
1837 of veta(t+dt) to compute the constraints. Do
1838 this by computing the box volume part of the
1839 trotter integration at this time. Nothing else
1840 should be changed by this routine here. If
1841 !(first time), we start with the previous value
1844 veta_save = state->veta;
1845 trotter_update(ir,step,ekind,enerd,state,total_vir,mdatoms,&MassQ,trotter_seq,ettTSEQ0);
1846 vetanew = state->veta;
1847 state->veta = veta_save;
1852 if ( !bRerunMD || rerun_fr.bV || bForceUpdate) { /* Why is rerun_fr.bV here? Unclear. */
1855 update_constraints(fplog,step,&dvdl,ir,ekind,mdatoms,state,graph,f,
1856 &top->idef,shake_vir,NULL,
1857 cr,nrnb,wcycle,upd,constr,
1858 bInitStep,TRUE,bCalcEnerPres,vetanew);
1860 if (!bOK && !bFFscan)
1862 gmx_fatal(FARGS,"Constraint error: Shake, Lincs or Settle could not solve the constrains");
1867 { /* Need to unshift here if a do_force has been
1868 called in the previous step */
1869 unshift_self(graph,state->box,state->x);
1874 /* if VV, compute the pressure and constraints */
1875 /* if VV2, the pressure and constraints only if using pressure control.*/
1876 bPres = (ir->eI==eiVV || IR_NPT_TROTTER(ir));
1877 bTemp = ((ir->eI==eiVV &&(!bInitStep)) || (ir->eI==eiVVAK && IR_NPT_TROTTER(ir)));
1878 compute_globals(fplog,gstat,cr,ir,fr,ekind,state,state_global,mdatoms,nrnb,vcm,
1879 wcycle,enerd,force_vir,shake_vir,total_vir,pres,mu_tot,
1880 constr,NULL,FALSE,state->box,
1881 top_global,&pcurr,top_global->natoms,&bSumEkinhOld,
1884 | (bTemp ? CGLO_TEMPERATURE:0)
1885 | (bPres ? CGLO_PRESSURE : 0)
1886 | (bPres ? CGLO_CONSTRAINT : 0)
1887 | (iterate.bIterate ? CGLO_ITERATE : 0)
1888 | (bFirstIterate ? CGLO_FIRSTITERATE : 0)
1892 /* explanation of above:
1893 a) We compute Ekin at the full time step
1894 if 1) we are using the AveVel Ekin, and it's not the
1895 initial step, or 2) if we are using AveEkin, but need the full
1896 time step kinetic energy for the pressure.
1897 b) If we are using EkinAveEkin for the kinetic energy for the temperture control, we still feed in
1898 EkinAveVel because it's needed for the pressure */
1900 /* temperature scaling and pressure scaling to produce the extended variables at t+dt */
1901 if (bVV && !bInitStep)
1903 trotter_update(ir,step,ekind,enerd,state,total_vir,mdatoms,&MassQ, trotter_seq,ettTSEQ2);
1907 done_iterating(cr,fplog,step,&iterate,bFirstIterate,
1908 state->veta,&vetanew))
1912 bFirstIterate = FALSE;
1915 if (bTrotter && !bInitStep) {
1916 copy_mat(shake_vir,state->svir_prev);
1917 copy_mat(force_vir,state->fvir_prev);
1918 if (IR_NVT_TROTTER(ir) && ir->eI==eiVV) {
1919 /* update temperature and kinetic energy now that step is over - this is the v(t+dt) point */
1920 enerd->term[F_TEMP] = sum_ekin(&(ir->opts),ekind,NULL,(ir->eI==eiVV),FALSE,FALSE);
1921 enerd->term[F_EKIN] = trace(ekind->ekin);
1924 /* if it's the initial step, we performed this first step just to get the constraint virial */
1925 if (bInitStep && ir->eI==eiVV) {
1926 copy_rvecn(cbuf,state->v,0,state->natoms);
1929 if (fr->bSepDVDL && fplog && do_log)
1931 fprintf(fplog,sepdvdlformat,"Constraint",0.0,dvdl);
1933 enerd->term[F_DHDL_CON] += dvdl;
1935 GMX_MPE_LOG(ev_timestep1);
1939 /* MRS -- now done iterating -- compute the conserved quantity */
1942 if (IR_NVT_TROTTER(ir) || IR_NPT_TROTTER(ir))
1945 NPT_energy(ir,state,&MassQ);
1946 if ((ir->eDispCorr != edispcEnerPres) && (ir->eDispCorr != edispcAllEnerPres))
1948 last_conserved -= enerd->term[F_DISPCORR];
1952 last_ekin = enerd->term[F_EKIN]; /* does this get preserved through checkpointing? */
1956 /* ######## END FIRST UPDATE STEP ############## */
1957 /* ######## If doing VV, we now have v(dt) ###### */
1959 /* ################## START TRAJECTORY OUTPUT ################# */
1961 /* Now we have the energies and forces corresponding to the
1962 * coordinates at time t. We must output all of this before
1964 * for RerunMD t is read from input trajectory
1966 GMX_MPE_LOG(ev_output_start);
1969 if (do_per_step(step,ir->nstxout)) { mdof_flags |= MDOF_X; }
1970 if (do_per_step(step,ir->nstvout)) { mdof_flags |= MDOF_V; }
1971 if (do_per_step(step,ir->nstfout)) { mdof_flags |= MDOF_F; }
1972 if (do_per_step(step,ir->nstxtcout)) { mdof_flags |= MDOF_XTC; }
1973 /* if (bCPT) { mdof_flags |= MDOF_CPT; };*/
1977 fcReportProgress( ir->nsteps, step );
1981 /* Enforce writing positions and velocities at end of run */
1982 mdof_flags |= (MDOF_X | MDOF_V);
1984 /* sync bCPT and fc record-keeping */
1985 /* if (bCPT && MASTER(cr))
1986 fcRequestCheckPoint();*/
1989 if (mdof_flags != 0)
1991 wallcycle_start(wcycle,ewcTRAJ);
1994 if (state->flags & (1<<estLD_RNG))
1996 get_stochd_state(upd,state);
2002 state_global->ekinstate.bUpToDate = FALSE;
2006 update_ekinstate(&state_global->ekinstate,ekind);
2007 state_global->ekinstate.bUpToDate = TRUE;
2009 update_energyhistory(&state_global->enerhist,mdebin);
2012 write_traj(fplog,cr,outf,mdof_flags,top_global,
2013 step,t,state,state_global,f,f_global,&n_xtc,&x_xtc);
2020 if (bLastStep && step_rel == ir->nsteps &&
2021 (Flags & MD_CONFOUT) && MASTER(cr) &&
2022 !bRerunMD && !bFFscan)
2024 /* x and v have been collected in write_traj,
2025 * because a checkpoint file will always be written
2028 fprintf(stderr,"\nWriting final coordinates.\n");
2029 if (ir->ePBC != epbcNONE && !ir->bPeriodicMols &&
2032 /* Make molecules whole only for confout writing */
2033 do_pbc_mtop(fplog,ir->ePBC,state->box,top_global,state_global->x);
2035 /* write_sto_conf_mtop(ftp2fn(efSTO,nfile,fnm),
2036 *top_global->name,top_global,
2037 state_global->x,state_global->v,
2038 ir->ePBC,state->box);*/
2041 wallcycle_stop(wcycle,ewcTRAJ);
2043 GMX_MPE_LOG(ev_output_finish);
2045 /* kludge -- virial is lost with restart for NPT control. Must restart */
2046 if (bStartingFromCpt && bVV)
2048 copy_mat(state->svir_prev,shake_vir);
2049 copy_mat(state->fvir_prev,force_vir);
2051 /* ################## END TRAJECTORY OUTPUT ################ */
2053 /* Determine the pressure:
2054 * always when we want exact averages in the energy file,
2055 * at ns steps when we have pressure coupling,
2056 * otherwise only at energy output steps (set below).
2059 bNstEner = (bGStatEveryStep || do_per_step(step,ir->nstcalcenergy));
2060 bCalcEnerPres = bNstEner;
2062 /* Do we need global communication ? */
2063 bGStat = (bGStatEveryStep || bStopCM || bNS ||
2064 (ir->nstlist == -1 && !bRerunMD && step >= nlh.step_nscheck));
2066 do_ene = (do_per_step(step,ir->nstenergy) || bLastStep);
2068 if (do_ene || do_log)
2070 bCalcEnerPres = TRUE;
2074 /* Determine the wallclock run time up till now */
2075 run_time = gmx_gettime() - (double)runtime->real;
2077 /* Check whether everything is still allright */
2078 if (((int)gmx_get_stop_condition() > handled_stop_condition)
2084 /* this is just make gs.sig compatible with the hack
2085 of sending signals around by MPI_Reduce with together with
2087 if ( gmx_get_stop_condition() == gmx_stop_cond_next_ns )
2088 gs.sig[eglsSTOPCOND]=1;
2089 if ( gmx_get_stop_condition() == gmx_stop_cond_next )
2090 gs.sig[eglsSTOPCOND]=-1;
2091 /* < 0 means stop at next step, > 0 means stop at next NS step */
2095 "\n\nReceived the %s signal, stopping at the next %sstep\n\n",
2096 gmx_get_signal_name(),
2097 gs.sig[eglsSTOPCOND]==1 ? "NS " : "");
2101 "\n\nReceived the %s signal, stopping at the next %sstep\n\n",
2102 gmx_get_signal_name(),
2103 gs.sig[eglsSTOPCOND]==1 ? "NS " : "");
2105 handled_stop_condition=(int)gmx_get_stop_condition();
2107 else if (MASTER(cr) && (bNS || ir->nstlist <= 0) &&
2108 (max_hours > 0 && run_time > max_hours*60.0*60.0*0.99) &&
2109 gs.sig[eglsSTOPCOND] == 0 && gs.set[eglsSTOPCOND] == 0)
2111 /* Signal to terminate the run */
2112 gs.sig[eglsSTOPCOND] = 1;
2115 fprintf(fplog,"\nStep %s: Run time exceeded %.3f hours, will terminate the run\n",gmx_step_str(step,sbuf),max_hours*0.99);
2117 fprintf(stderr, "\nStep %s: Run time exceeded %.3f hours, will terminate the run\n",gmx_step_str(step,sbuf),max_hours*0.99);
2120 if (bResetCountersHalfMaxH && MASTER(cr) &&
2121 run_time > max_hours*60.0*60.0*0.495)
2123 gs.sig[eglsRESETCOUNTERS] = 1;
2126 if (ir->nstlist == -1 && !bRerunMD)
2128 /* When bGStatEveryStep=FALSE, global_stat is only called
2129 * when we check the atom displacements, not at NS steps.
2130 * This means that also the bonded interaction count check is not
2131 * performed immediately after NS. Therefore a few MD steps could
2132 * be performed with missing interactions.
2133 * But wrong energies are never written to file,
2134 * since energies are only written after global_stat
2137 if (step >= nlh.step_nscheck)
2139 nlh.nabnsb = natoms_beyond_ns_buffer(ir,fr,&top->cgs,
2140 nlh.scale_tot,state->x);
2144 /* This is not necessarily true,
2145 * but step_nscheck is determined quite conservatively.
2151 /* In parallel we only have to check for checkpointing in steps
2152 * where we do global communication,
2153 * otherwise the other nodes don't know.
2155 if (MASTER(cr) && ((bGStat || !PAR(cr)) &&
2158 run_time >= nchkpt*cpt_period*60.0)) &&
2159 gs.set[eglsCHKPT] == 0)
2161 gs.sig[eglsCHKPT] = 1;
2166 gmx_iterate_init(&iterate,bIterations);
2169 /* for iterations, we save these vectors, as we will be redoing the calculations */
2170 if (bIterations && iterate.bIterate)
2172 copy_coupling_state(state,bufstate,ekind,ekind_save,&(ir->opts));
2174 bFirstIterate = TRUE;
2175 while (bFirstIterate || (bIterations && iterate.bIterate))
2177 /* We now restore these vectors to redo the calculation with improved extended variables */
2180 copy_coupling_state(bufstate,state,ekind_save,ekind,&(ir->opts));
2183 /* We make the decision to break or not -after- the calculation of Ekin and Pressure,
2184 so scroll down for that logic */
2186 /* ######### START SECOND UPDATE STEP ################# */
2187 GMX_MPE_LOG(ev_update_start);
2189 if (!bRerunMD || rerun_fr.bV || bForceUpdate)
2191 wallcycle_start(wcycle,ewcUPDATE);
2193 /* Box is changed in update() when we do pressure coupling,
2194 * but we should still use the old box for energy corrections and when
2195 * writing it to the energy file, so it matches the trajectory files for
2196 * the same timestep above. Make a copy in a separate array.
2198 copy_mat(state->box,lastbox);
2199 /* UPDATE PRESSURE VARIABLES IN TROTTER FORMULATION WITH CONSTRAINTS */
2202 if (bIterations && iterate.bIterate)
2210 /* we use a new value of scalevir to converge the iterations faster */
2211 scalevir = tracevir/trace(shake_vir);
2213 msmul(shake_vir,scalevir,shake_vir);
2214 m_add(force_vir,shake_vir,total_vir);
2215 clear_mat(shake_vir);
2217 trotter_update(ir,step,ekind,enerd,state,total_vir,mdatoms,&MassQ, trotter_seq,ettTSEQ3);
2219 /* We can only do Berendsen coupling after we have summed
2220 * the kinetic energy or virial. Since the happens
2221 * in global_state after update, we should only do it at
2222 * step % nstlist = 1 with bGStatEveryStep=FALSE.
2225 if (ir->eI != eiVVAK)
2227 update_tcouple(fplog,step,ir,state,ekind,wcycle,upd,&MassQ,mdatoms);
2229 update_pcouple(fplog,step,ir,state,pcoupl_mu,M,wcycle,
2234 /* velocity half-step update */
2235 update_coords(fplog,step,ir,mdatoms,state,f,fr->bTwinRange && bNStList,fr->f_twin,fcd,
2236 ekind,M,wcycle,upd,FALSE,etrtVELOCITY2,cr,nrnb,constr,&top->idef);
2239 /* Above, initialize just copies ekinh into ekin,
2240 * it doesn't copy position (for VV),
2241 * and entire integrator for MD.
2246 copy_rvecn(state->x,cbuf,0,state->natoms);
2249 update_coords(fplog,step,ir,mdatoms,state,f,fr->bTwinRange && bNStList,fr->f_twin,fcd,
2250 ekind,M,wcycle,upd,bInitStep,etrtPOSITION,cr,nrnb,constr,&top->idef);
2251 wallcycle_stop(wcycle,ewcUPDATE);
2253 update_constraints(fplog,step,&dvdl,ir,ekind,mdatoms,state,graph,f,
2254 &top->idef,shake_vir,force_vir,
2255 cr,nrnb,wcycle,upd,constr,
2256 bInitStep,FALSE,bCalcEnerPres,state->veta);
2260 /* erase F_EKIN and F_TEMP here? */
2261 /* just compute the kinetic energy at the half step to perform a trotter step */
2262 compute_globals(fplog,gstat,cr,ir,fr,ekind,state,state_global,mdatoms,nrnb,vcm,
2263 wcycle,enerd,force_vir,shake_vir,total_vir,pres,mu_tot,
2264 constr,NULL,FALSE,lastbox,
2265 top_global,&pcurr,top_global->natoms,&bSumEkinhOld,
2266 cglo_flags | CGLO_TEMPERATURE | CGLO_CONSTRAINT
2268 wallcycle_start(wcycle,ewcUPDATE);
2269 trotter_update(ir,step,ekind,enerd,state,total_vir,mdatoms,&MassQ, trotter_seq,ettTSEQ4);
2270 /* now we know the scaling, we can compute the positions again again */
2271 copy_rvecn(cbuf,state->x,0,state->natoms);
2273 update_coords(fplog,step,ir,mdatoms,state,f,fr->bTwinRange && bNStList,fr->f_twin,fcd,
2274 ekind,M,wcycle,upd,bInitStep,etrtPOSITION,cr,nrnb,constr,&top->idef);
2275 wallcycle_stop(wcycle,ewcUPDATE);
2277 /* do we need an extra constraint here? just need to copy out of state->v to upd->xp? */
2278 /* are the small terms in the shake_vir here due
2279 * to numerical errors, or are they important
2280 * physically? I'm thinking they are just errors, but not completely sure.
2281 * For now, will call without actually constraining, constr=NULL*/
2282 update_constraints(fplog,step,&dvdl,ir,ekind,mdatoms,state,graph,f,
2283 &top->idef,tmp_vir,force_vir,
2284 cr,nrnb,wcycle,upd,NULL,
2285 bInitStep,FALSE,bCalcEnerPres,state->veta);
2287 if (!bOK && !bFFscan)
2289 gmx_fatal(FARGS,"Constraint error: Shake, Lincs or Settle could not solve the constrains");
2292 if (fr->bSepDVDL && fplog && do_log)
2294 fprintf(fplog,sepdvdlformat,"Constraint",0.0,dvdl);
2296 enerd->term[F_DHDL_CON] += dvdl;
2300 /* Need to unshift here */
2301 unshift_self(graph,state->box,state->x);
2304 GMX_BARRIER(cr->mpi_comm_mygroup);
2305 GMX_MPE_LOG(ev_update_finish);
2309 wallcycle_start(wcycle,ewcVSITECONSTR);
2312 shift_self(graph,state->box,state->x);
2314 construct_vsites(fplog,vsite,state->x,nrnb,ir->delta_t,state->v,
2315 top->idef.iparams,top->idef.il,
2316 fr->ePBC,fr->bMolPBC,graph,cr,state->box);
2320 unshift_self(graph,state->box,state->x);
2322 wallcycle_stop(wcycle,ewcVSITECONSTR);
2325 /* ############## IF NOT VV, Calculate globals HERE, also iterate constraints ############ */
2326 if (ir->nstlist == -1 && bFirstIterate)
2328 gs.sig[eglsNABNSB] = nlh.nabnsb;
2330 compute_globals(fplog,gstat,cr,ir,fr,ekind,state,state_global,mdatoms,nrnb,vcm,
2331 wcycle,enerd,force_vir,shake_vir,total_vir,pres,mu_tot,
2333 bFirstIterate ? &gs : NULL,(step % gs.nstms == 0),
2335 top_global,&pcurr,top_global->natoms,&bSumEkinhOld,
2337 | (!EI_VV(ir->eI) ? CGLO_ENERGY : 0)
2338 | (!EI_VV(ir->eI) ? CGLO_TEMPERATURE : 0)
2339 | (!EI_VV(ir->eI) || bRerunMD ? CGLO_PRESSURE : 0)
2340 | (bIterations && iterate.bIterate ? CGLO_ITERATE : 0)
2341 | (bFirstIterate ? CGLO_FIRSTITERATE : 0)
2344 if (ir->nstlist == -1 && bFirstIterate)
2346 nlh.nabnsb = gs.set[eglsNABNSB];
2347 gs.set[eglsNABNSB] = 0;
2349 /* bIterate is set to keep it from eliminating the old ekin kinetic energy terms */
2350 /* ############# END CALC EKIN AND PRESSURE ################# */
2352 /* Note: this is OK, but there are some numerical precision issues with using the convergence of
2353 the virial that should probably be addressed eventually. state->veta has better properies,
2354 but what we actually need entering the new cycle is the new shake_vir value. Ideally, we could
2355 generate the new shake_vir, but test the veta value for convergence. This will take some thought. */
2358 done_iterating(cr,fplog,step,&iterate,bFirstIterate,
2359 trace(shake_vir),&tracevir))
2363 bFirstIterate = FALSE;
2366 update_box(fplog,step,ir,mdatoms,state,graph,f,
2367 ir->nstlist==-1 ? &nlh.scale_tot : NULL,pcoupl_mu,nrnb,wcycle,upd,bInitStep,FALSE);
2369 /* ################# END UPDATE STEP 2 ################# */
2370 /* #### We now have r(t+dt) and v(t+dt/2) ############# */
2372 /* The coordinates (x) were unshifted in update */
2373 /* if (bFFscan && (shellfc==NULL || bConverged))
2375 if (print_forcefield(fplog,enerd->term,mdatoms->homenr,
2377 &(top_global->mols),mdatoms->massT,pres))
2379 if (gmx_parallel_env_initialized())
2383 fprintf(stderr,"\n");
2389 /* We will not sum ekinh_old,
2390 * so signal that we still have to do it.
2392 bSumEkinhOld = TRUE;
2397 /* Only do GCT when the relaxation of shells (minimization) has converged,
2398 * otherwise we might be coupling to bogus energies.
2399 * In parallel we must always do this, because the other sims might
2403 /* Since this is called with the new coordinates state->x, I assume
2404 * we want the new box state->box too. / EL 20040121
2406 /* do_coupling(fplog,oenv,nfile,fnm,tcr,t,step,enerd->term,fr,
2408 mdatoms,&(top->idef),mu_aver,
2409 top_global->mols.nr,cr,
2410 state->box,total_vir,pres,
2411 mu_tot,state->x,f,bConverged);
2415 /* ######### BEGIN PREPARING EDR OUTPUT ########### */
2417 sum_dhdl(enerd,state->lambda,ir);
2418 /* use the directly determined last velocity, not actually the averaged half steps */
2419 if (bTrotter && ir->eI==eiVV)
2421 enerd->term[F_EKIN] = last_ekin;
2423 enerd->term[F_ETOT] = enerd->term[F_EPOT] + enerd->term[F_EKIN];
2432 if (IR_NVT_TROTTER(ir)) {
2433 enerd->term[F_ECONSERVED] = enerd->term[F_ETOT] + last_conserved;
2435 enerd->term[F_ECONSERVED] = enerd->term[F_ETOT] +
2436 NPT_energy(ir,state,&MassQ);
2440 enerd->term[F_ECONSERVED] =
2441 enerd->term[F_ETOT] + vrescale_energy(&(ir->opts),
2442 state->therm_integral);
2448 /* Check for excessively large energies */
2452 real etot_max = 1e200;
2454 real etot_max = 1e30;
2456 if (fabs(enerd->term[F_ETOT]) > etot_max)
2458 fprintf(stderr,"Energy too large (%g), giving up\n",
2459 enerd->term[F_ETOT]);
2462 /* ######### END PREPARING EDR OUTPUT ########### */
2464 /* Time for performance */
2465 if (((step % stepout) == 0) || bLastStep)
2467 runtime_upd_proc(runtime);
2473 gmx_bool do_dr,do_or;
2475 if (!(bStartingFromCpt && (EI_VV(ir->eI))))
2479 upd_mdebin(mdebin,bDoDHDL,TRUE,
2480 t,mdatoms->tmass,enerd,state,lastbox,
2481 shake_vir,force_vir,total_vir,pres,
2482 ekind,mu_tot,constr);
2486 upd_mdebin_step(mdebin);
2489 do_dr = do_per_step(step,ir->nstdisreout);
2490 do_or = do_per_step(step,ir->nstorireout);
2492 print_ebin(outf->fp_ene,do_ene,do_dr,do_or,do_log?fplog:NULL,
2494 eprNORMAL,bCompact,mdebin,fcd,groups,&(ir->opts));
2496 if (ir->ePull != epullNO)
2498 pull_print_output(ir->pull,step,t);
2501 if (do_per_step(step,ir->nstlog))
2503 if(fflush(fplog) != 0)
2505 gmx_fatal(FARGS,"Cannot flush logfile - maybe you are out of quota?");
2511 /* Remaining runtime */
2512 if (MULTIMASTER(cr) && (do_verbose || gmx_got_usr_signal() ))
2516 fprintf(stderr,"\n");
2518 print_time(stderr,runtime,step,ir,cr);
2521 /* Set new positions for the group to embed */
2527 } else if (step_rel<=(it_xy+it_z))
2531 resize(ins_at,r_ins,state_global->x,pos_ins,fac);
2534 /* Replica exchange */
2535 /* bExchanged = FALSE;
2536 if ((repl_ex_nst > 0) && (step > 0) && !bLastStep &&
2537 do_per_step(step,repl_ex_nst))
2539 bExchanged = replica_exchange(fplog,cr,repl_ex,
2540 state_global,enerd->term,
2543 if (bExchanged && PAR(cr))
2545 if (DOMAINDECOMP(cr))
2547 dd_partition_system(fplog,step,cr,TRUE,1,
2548 state_global,top_global,ir,
2549 state,&f,mdatoms,top,fr,
2550 vsite,shellfc,constr,
2555 bcast_state(cr,state,FALSE);
2561 bStartingFromCpt = FALSE;
2563 /* ####### SET VARIABLES FOR NEXT ITERATION IF THEY STILL NEED IT ###### */
2564 /* With all integrators, except VV, we need to retain the pressure
2565 * at the current step for coupling at the next step.
2567 if ((state->flags & (1<<estPRES_PREV)) &&
2569 (ir->nstpcouple > 0 && step % ir->nstpcouple == 0)))
2571 /* Store the pressure in t_state for pressure coupling
2572 * at the next MD step.
2574 copy_mat(pres,state->pres_prev);
2577 /* ####### END SET VARIABLES FOR NEXT ITERATION ###### */
2581 /* read next frame from input trajectory */
2582 bNotLastFrame = read_next_frame(oenv,status,&rerun_fr);
2585 if (!bRerunMD || !rerun_fr.bStep)
2587 /* increase the MD step number */
2592 cycles = wallcycle_stop(wcycle,ewcSTEP);
2593 if (DOMAINDECOMP(cr) && wcycle)
2595 dd_cycles_add(cr->dd,cycles,ddCyclStep);
2598 if (step_rel == wcycle_get_reset_counters(wcycle) ||
2599 gs.set[eglsRESETCOUNTERS] != 0)
2601 /* Reset all the counters related to performance over the run */
2602 reset_all_counters(fplog,cr,step,&step_rel,ir,wcycle,nrnb,runtime);
2603 wcycle_set_reset_counters(wcycle,-1);
2604 bResetCountersHalfMaxH = FALSE;
2605 gs.set[eglsRESETCOUNTERS] = 0;
2608 /* End of main MD loop */
2610 write_sto_conf_mtop(ftp2fn(efSTO,nfile,fnm),
2611 *top_global->name,top_global,
2612 state_global->x,state_global->v,
2613 ir->ePBC,state->box);
2616 runtime_end(runtime);
2623 if (!(cr->duty & DUTY_PME))
2625 /* Tell the PME only node to finish */
2631 if (ir->nstcalcenergy > 0 && !bRerunMD)
2633 print_ebin(outf->fp_ene,FALSE,FALSE,FALSE,fplog,step,t,
2634 eprAVER,FALSE,mdebin,fcd,groups,&(ir->opts));
2642 if (ir->nstlist == -1 && nlh.nns > 0 && fplog)
2644 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)));
2645 fprintf(fplog,"Average number of atoms that crossed the half buffer length: %.1f\n\n",nlh.ab/nlh.nns);
2648 if (shellfc && fplog)
2650 fprintf(fplog,"Fraction of iterations that converged: %.2f %%\n",
2651 (nconverged*100.0)/step_rel);
2652 fprintf(fplog,"Average number of force evaluations per MD step: %.2f\n\n",
2656 /* if (repl_ex_nst > 0 && MASTER(cr))
2658 print_replica_exchange_statistics(fplog,repl_ex);
2661 runtime->nsteps_done = step_rel;
2667 int mdrunner_membed(FILE *fplog,t_commrec *cr,int nfile,const t_filenm fnm[],
2668 const output_env_t oenv, gmx_bool bVerbose,gmx_bool bCompact,
2670 ivec ddxyz,int dd_node_order,real rdd,real rconstr,
2671 const char *dddlb_opt,real dlb_scale,
2672 const char *ddcsx,const char *ddcsy,const char *ddcsz,
2673 int nstepout,int resetstep,int nmultisim,int repl_ex_nst,int repl_ex_seed,
2674 real pforce,real cpt_period,real max_hours,
2675 const char *deviceOptions,
2676 unsigned long Flags,
2677 real xy_fac, real xy_max, real z_fac, real z_max,
2678 int it_xy, int it_z, real probe_rad, int low_up_rm,
2679 int pieces, gmx_bool bALLOW_ASYMMETRY, int maxwarn)
2681 double nodetime=0,realtime;
2682 t_inputrec *inputrec;
2683 t_state *state=NULL;
2686 int npme_major,npme_minor;
2689 gmx_mtop_t *mtop=NULL;
2690 t_mdatoms *mdatoms=NULL;
2691 t_forcerec *fr=NULL;
2694 gmx_pme_t *pmedata=NULL;
2695 gmx_vsite_t *vsite=NULL;
2696 gmx_constr_t constr;
2697 int i,m,nChargePerturbed=-1,status,nalloc;
2699 gmx_wallcycle_t wcycle;
2700 gmx_bool bReadRNG,bReadEkin;
2702 gmx_runtime_t runtime;
2704 gmx_large_int_t reset_counters;
2705 gmx_edsam_t ed=NULL;
2706 t_commrec *cr_old=cr;
2707 int nthreads=1,nthreads_requested=1;
2711 int rm_bonded_at,fr_id,fr_i=0,tmp_id,warn=0;
2712 int ng,j,max_lip_rm,ins_grp_id,ins_nat,mem_nat,ntype,lip_rm,tpr_version;
2713 real xy_step=0,z_step=0;
2715 rvec *r_ins=NULL,fac;
2716 t_block *ins_at,*rest_at;
2720 gmx_groups_t *groups;
2721 gmx_bool bExcl=FALSE;
2724 char **piecename=NULL;
2726 /* CAUTION: threads may be started later on in this function, so
2727 cr doesn't reflect the final parallel state right now */
2731 if (bVerbose && SIMMASTER(cr))
2733 fprintf(stderr,"Getting Loaded...\n");
2736 if (Flags & MD_APPENDFILES)
2744 /* Read (nearly) all data required for the simulation */
2745 read_tpx_state(ftp2fn(efTPX,nfile,fnm),inputrec,state,NULL,mtop);
2747 /* NOW the threads will be started: */
2751 /* END OF CAUTION: cr is now reliable */
2755 /* now broadcast everything to the non-master nodes/threads: */
2756 init_parallel(fplog, cr, inputrec, mtop);
2758 /* now make sure the state is initialized and propagated */
2759 set_state_entries(state,inputrec,cr->nnodes);
2761 if (can_use_allvsall(inputrec,mtop,TRUE,cr,fplog))
2763 /* All-vs-all loops do not work with domain decomposition */
2764 Flags |= MD_PARTDEC;
2767 if (!EEL_PME(inputrec->coulombtype) || (Flags & MD_PARTDEC))
2776 tpr_version = get_tpr_version(ftp2fn(efTPX,nfile,fnm));
2778 gmx_fatal(FARGS,"Version of *.tpr file to old (%d). Rerun grompp with gromacs VERSION 4.0.3 or newer.\n",tpr_version);
2780 if( inputrec->eI != eiMD )
2781 gmx_input("Change integrator to md in mdp file.");
2784 gmx_input("Sorry, parallel g_membed is not yet fully functrional.");
2786 groups=&(mtop->groups);
2788 atoms=gmx_mtop_global_atoms(mtop);
2790 fprintf(stderr,"\nSelect a group to embed in the membrane:\n");
2791 get_index(&atoms,ftp2fn_null(efNDX,nfile,fnm),1,&(ins_at->nr),&(ins_at->index),&ins);
2792 ins_grp_id = search_string(ins,groups->ngrpname,(groups->grpname));
2793 fprintf(stderr,"\nSelect a group to embed %s into (e.g. the membrane):\n",ins);
2794 get_index(&atoms,ftp2fn_null(efNDX,nfile,fnm),1,&(mem_p->mem_at.nr),&(mem_p->mem_at.index),&(mem_p->name));
2796 pos_ins->pieces=pieces;
2797 snew(pos_ins->nidx,pieces);
2798 snew(pos_ins->subindex,pieces);
2799 snew(piecename,pieces);
2802 fprintf(stderr,"\nSelect pieces to embed:\n");
2803 get_index(&atoms,ftp2fn_null(efNDX,nfile,fnm),pieces,pos_ins->nidx,pos_ins->subindex,piecename);
2807 /*use whole embedded group*/
2808 snew(pos_ins->nidx,1);
2809 snew(pos_ins->subindex,1);
2810 pos_ins->nidx[0]=ins_at->nr;
2811 pos_ins->subindex[0]=ins_at->index;
2814 if(probe_rad<0.2199999)
2817 fprintf(stderr,"\nWarning %d:\nA probe radius (-rad) smaller than 0.2 can result in overlap between waters "
2818 "and the group to embed, which will result in Lincs errors etc.\nIf you are sure, you can increase maxwarn.\n\n",warn);
2821 if(xy_fac<0.09999999)
2824 fprintf(stderr,"\nWarning %d:\nThe initial size of %s is probably too smal.\n"
2825 "If you are sure, you can increase maxwarn.\n\n",warn,ins);
2831 fprintf(stderr,"\nWarning %d;\nThe number of steps used to grow the xy-coordinates of %s (%d) is probably too small.\n"
2832 "Increase -nxy or, if you are sure, you can increase maxwarn.\n\n",warn,ins,it_xy);
2835 if( (it_z<100) && ( z_fac<0.99999999 || z_fac>1.0000001) )
2838 fprintf(stderr,"\nWarning %d;\nThe number of steps used to grow the z-coordinate of %s (%d) is probably too small.\n"
2839 "Increase -nz or, if you are sure, you can increase maxwarn.\n\n",warn,ins,it_z);
2842 if(it_xy+it_z>inputrec->nsteps)
2845 fprintf(stderr,"\nWarning %d:\nThe number of growth steps (-nxy + -nz) is larger than the number of steps in the tpr.\n"
2846 "If you are sure, you can increase maxwarn.\n\n",warn);
2850 if( inputrec->opts.ngfrz==1)
2851 gmx_fatal(FARGS,"You did not specify \"%s\" as a freezegroup.",ins);
2852 for(i=0;i<inputrec->opts.ngfrz;i++)
2854 tmp_id = mtop->groups.grps[egcFREEZE].nm_ind[i];
2855 if(ins_grp_id==tmp_id)
2862 gmx_fatal(FARGS,"\"%s\" not as freezegroup defined in the mdp-file.",ins);
2865 if( inputrec->opts.nFreeze[fr_i][i] != 1)
2866 gmx_fatal(FARGS,"freeze dimensions for %s are not Y Y Y\n",ins);
2868 ng = groups->grps[egcENER].nr;
2870 gmx_input("No energy groups defined. This is necessary for energy exclusion in the freeze group");
2876 if (inputrec->opts.egp_flags[ng*i+j] == EGP_EXCL)
2879 if ( (groups->grps[egcENER].nm_ind[i] != ins_grp_id) || (groups->grps[egcENER].nm_ind[j] != ins_grp_id) )
2880 gmx_fatal(FARGS,"Energy exclusions \"%s\" and \"%s\" do not match the group to embed \"%s\"",
2881 *groups->grpname[groups->grps[egcENER].nm_ind[i]],
2882 *groups->grpname[groups->grps[egcENER].nm_ind[j]],ins);
2887 gmx_input("No energy exclusion groups defined. This is necessary for energy exclusion in the freeze group");
2889 /* Set all atoms in box*/
2890 /*set_inbox(state->natoms,state->x);*/
2892 /* Guess the area the protein will occupy in the membrane plane Calculate area per lipid*/
2894 ins_nat = init_ins_at(ins_at,rest_at,state,pos_ins,groups,ins_grp_id,xy_max);
2895 /* Check moleculetypes in insertion group */
2896 check_types(ins_at,rest_at,mtop);
2898 mem_nat = init_mem_at(mem_p,mtop,state->x,state->box,pos_ins);
2900 prot_area = est_prot_area(pos_ins,state->x,ins_at,mem_p);
2901 if ( (prot_area>7.5) && ( (state->box[XX][XX]*state->box[YY][YY]-state->box[XX][YY]*state->box[YY][XX])<50) )
2904 fprintf(stderr,"\nWarning %d:\nThe xy-area is very small compared to the area of the protein.\n"
2905 "This might cause pressure problems during the growth phase. Just try with\n"
2906 "current setup (-maxwarn + 1), but if pressure problems occur, lower the\n"
2907 "compressibility in the mdp-file or use no pressure coupling at all.\n\n",warn);
2910 gmx_fatal(FARGS,"Too many warnings.\n");
2912 printf("The estimated area of the protein in the membrane is %.3f nm^2\n",prot_area);
2913 printf("\nThere are %d lipids in the membrane part that overlaps the protein.\nThe area per lipid is %.4f nm^2.\n",mem_p->nmol,mem_p->lip_area);
2915 /* Maximum number of lipids to be removed*/
2916 max_lip_rm=(int)(2*prot_area/mem_p->lip_area);
2917 printf("Maximum number of lipids that will be removed is %d.\n",max_lip_rm);
2919 printf("\nWill resize the protein by a factor of %.3f in the xy plane and %.3f in the z direction.\n"
2920 "This resizing will be done with respect to the geometrical center of all protein atoms\n"
2921 "that span the membrane region, i.e. z between %.3f and %.3f\n\n",xy_fac,z_fac,mem_p->zmin,mem_p->zmax);
2923 /* resize the protein by xy and by z if necessary*/
2924 snew(r_ins,ins_at->nr);
2925 init_resize(ins_at,r_ins,pos_ins,mem_p,state->x,bALLOW_ASYMMETRY);
2926 fac[0]=fac[1]=xy_fac;
2929 xy_step =(xy_max-xy_fac)/(double)(it_xy);
2930 z_step =(z_max-z_fac)/(double)(it_z-1);
2932 resize(ins_at,r_ins,state->x,pos_ins,fac);
2934 /* remove overlapping lipids and water from the membrane box*/
2935 /*mark molecules to be removed*/
2937 set_pbc(pbc,inputrec->ePBC,state->box);
2940 lip_rm = gen_rm_list(rm_p,ins_at,rest_at,pbc,mtop,state->x, r_ins, mem_p,pos_ins,probe_rad,low_up_rm,bALLOW_ASYMMETRY);
2941 lip_rm -= low_up_rm;
2944 for(i=0;i<rm_p->nr;i++)
2945 fprintf(fplog,"rm mol %d\n",rm_p->mol[i]);
2947 for(i=0;i<mtop->nmolblock;i++)
2950 for(j=0;j<rm_p->nr;j++)
2951 if(rm_p->block[j]==i)
2953 printf("Will remove %d %s molecules\n",ntype,*(mtop->moltype[mtop->molblock[i].type].name));
2956 if(lip_rm>max_lip_rm)
2959 fprintf(stderr,"\nWarning %d:\nTrying to remove a larger lipid area than the estimated protein area\n"
2960 "Try making the -xyinit resize factor smaller. If you are sure about this increase maxwarn.\n\n",warn);
2963 /*remove all lipids and waters overlapping and update all important structures*/
2964 rm_group(inputrec,groups,mtop,rm_p,state,ins_at,pos_ins);
2966 rm_bonded_at = rm_bonded(ins_at,mtop);
2967 if (rm_bonded_at != ins_at->nr)
2969 fprintf(stderr,"Warning: The number of atoms for which the bonded interactions are removed is %d, "
2970 "while %d atoms are embedded. Make sure that the atoms to be embedded are not in the same"
2971 "molecule type as atoms that are not to be embedded.\n",rm_bonded_at,ins_at->nr);
2975 gmx_fatal(FARGS,"Too many warnings.\nIf you are sure these warnings are harmless, you can increase -maxwarn");
2979 if (ftp2bSet(efTOP,nfile,fnm))
2980 top_update(opt2fn("-p",nfile,fnm),ins,rm_p,mtop);
2988 fcRegisterSteps(inputrec->nsteps,inputrec->init_step);
2991 /* NMR restraints must be initialized before load_checkpoint,
2992 * since with time averaging the history is added to t_state.
2993 * For proper consistency check we therefore need to extend
2995 * So the PME-only nodes (if present) will also initialize
2996 * the distance restraints.
3000 /* This needs to be called before read_checkpoint to extend the state */
3001 init_disres(fplog,mtop,inputrec,cr,Flags & MD_PARTDEC,fcd,state);
3003 if (gmx_mtop_ftype_count(mtop,F_ORIRES) > 0)
3005 if (PAR(cr) && !(Flags & MD_PARTDEC))
3007 gmx_fatal(FARGS,"Orientation restraints do not work (yet) with domain decomposition, use particle decomposition (mdrun option -pd)");
3009 /* Orientation restraints */
3012 init_orires(fplog,mtop,state->x,inputrec,cr->ms,&(fcd->orires),
3017 if (DEFORM(*inputrec))
3019 /* Store the deform reference box before reading the checkpoint */
3022 copy_mat(state->box,box);
3026 gmx_bcast(sizeof(box),box,cr);
3028 /* Because we do not have the update struct available yet
3029 * in which the reference values should be stored,
3030 * we store them temporarily in static variables.
3031 * This should be thread safe, since they are only written once
3032 * and with identical values.
3034 /* deform_init_init_step_tpx = inputrec->init_step;*/
3035 /* copy_mat(box,deform_init_box_tpx);*/
3038 if (opt2bSet("-cpi",nfile,fnm))
3040 /* Check if checkpoint file exists before doing continuation.
3041 * This way we can use identical input options for the first and subsequent runs...
3043 if( gmx_fexist_master(opt2fn_master("-cpi",nfile,fnm,cr),cr) )
3045 load_checkpoint(opt2fn_master("-cpi",nfile,fnm,cr),&fplog,
3046 cr,Flags & MD_PARTDEC,ddxyz,
3047 inputrec,state,&bReadRNG,&bReadEkin,
3048 (Flags & MD_APPENDFILES));
3052 Flags |= MD_READ_RNG;
3056 Flags |= MD_READ_EKIN;
3061 if ((MASTER(cr) || (Flags & MD_SEPPOT)) && (Flags & MD_APPENDFILES))
3063 gmx_log_open(ftp2fn(efLOG,nfile,fnm),cr,!(Flags & MD_SEPPOT),
3069 copy_mat(state->box,box);
3074 gmx_bcast(sizeof(box),box,cr);
3077 if (bVerbose && SIMMASTER(cr))
3079 fprintf(stderr,"Loaded with Money\n\n");
3082 if (PAR(cr) && !((Flags & MD_PARTDEC) || EI_TPI(inputrec->eI)))
3084 cr->dd = init_domain_decomposition(fplog,cr,Flags,ddxyz,rdd,rconstr,
3085 dddlb_opt,dlb_scale,
3089 &ddbox,&npme_major,&npme_minor);
3091 make_dd_communicators(fplog,cr,dd_node_order);
3093 /* Set overallocation to avoid frequent reallocation of arrays */
3094 set_over_alloc_dd(TRUE);
3098 /* PME, if used, is done on all nodes with 1D decomposition */
3100 cr->duty = (DUTY_PP | DUTY_PME);
3101 npme_major = cr->nnodes;
3104 if (inputrec->ePBC == epbcSCREW)
3107 "pbc=%s is only implemented with domain decomposition",
3108 epbc_names[inputrec->ePBC]);
3114 /* After possible communicator splitting in make_dd_communicators.
3115 * we can set up the intra/inter node communication.
3117 gmx_setup_nodecomm(fplog,cr);
3120 wcycle = wallcycle_init(fplog,resetstep,cr);
3123 /* Master synchronizes its value of reset_counters with all nodes
3124 * including PME only nodes */
3125 reset_counters = wcycle_get_reset_counters(wcycle);
3126 gmx_bcast_sim(sizeof(reset_counters),&reset_counters,cr);
3127 wcycle_set_reset_counters(wcycle, reset_counters);
3132 if (cr->duty & DUTY_PP)
3134 /* For domain decomposition we allocate dynamically
3135 * in dd_partition_system.
3137 if (DOMAINDECOMP(cr))
3139 bcast_state_setup(cr,state);
3149 bcast_state(cr,state,TRUE);
3153 /* Dihedral Restraints */
3154 if (gmx_mtop_ftype_count(mtop,F_DIHRES) > 0)
3156 init_dihres(fplog,mtop,inputrec,fcd);
3159 /* Initiate forcerecord */
3161 init_forcerec(fplog,oenv,fr,fcd,inputrec,mtop,cr,box,FALSE,
3162 opt2fn("-table",nfile,fnm),
3163 opt2fn("-tablep",nfile,fnm),
3164 opt2fn("-tableb",nfile,fnm),FALSE,pforce);
3166 /* version for PCA_NOT_READ_NODE (see md.c) */
3167 /*init_forcerec(fplog,fr,fcd,inputrec,mtop,cr,box,FALSE,
3168 "nofile","nofile","nofile",FALSE,pforce);
3170 fr->bSepDVDL = ((Flags & MD_SEPPOT) == MD_SEPPOT);
3172 /* Initialize QM-MM */
3175 init_QMMMrec(cr,box,mtop,inputrec,fr);
3178 /* Initialize the mdatoms structure.
3179 * mdatoms is not filled with atom data,
3180 * as this can not be done now with domain decomposition.
3182 mdatoms = init_mdatoms(fplog,mtop,inputrec->efep!=efepNO);
3184 /* Initialize the virtual site communication */
3185 vsite = init_vsite(mtop,cr);
3187 calc_shifts(box,fr->shift_vec);
3189 /* With periodic molecules the charge groups should be whole at start up
3190 * and the virtual sites should not be far from their proper positions.
3192 if (!inputrec->bContinuation && MASTER(cr) &&
3193 !(inputrec->ePBC != epbcNONE && inputrec->bPeriodicMols))
3195 /* Make molecules whole at start of run */
3196 if (fr->ePBC != epbcNONE)
3198 do_pbc_first_mtop(fplog,inputrec->ePBC,box,mtop,state->x);
3202 /* Correct initial vsite positions are required
3203 * for the initial distribution in the domain decomposition
3204 * and for the initial shell prediction.
3206 construct_vsites_mtop(fplog,vsite,mtop,state->x);
3210 /* Initiate PPPM if necessary */
3211 if (fr->eeltype == eelPPPM)
3213 if (mdatoms->nChargePerturbed)
3215 gmx_fatal(FARGS,"Free energy with %s is not implemented",
3216 eel_names[fr->eeltype]);
3218 status = gmx_pppm_init(fplog,cr,oenv,FALSE,TRUE,box,
3219 getenv("GMXGHAT"),inputrec, (Flags & MD_REPRODUCIBLE));
3222 gmx_fatal(FARGS,"Error %d initializing PPPM",status);
3226 if (EEL_PME(fr->eeltype))
3228 ewaldcoeff = fr->ewaldcoeff;
3229 pmedata = &fr->pmedata;
3238 /* This is a PME only node */
3240 /* We don't need the state */
3243 ewaldcoeff = calc_ewaldcoeff(inputrec->rcoulomb, inputrec->ewald_rtol);
3247 /* Initiate PME if necessary,
3248 * either on all nodes or on dedicated PME nodes only. */
3249 if (EEL_PME(inputrec->coulombtype))
3253 nChargePerturbed = mdatoms->nChargePerturbed;
3255 if (cr->npmenodes > 0)
3257 /* The PME only nodes need to know nChargePerturbed */
3258 gmx_bcast_sim(sizeof(nChargePerturbed),&nChargePerturbed,cr);
3260 if (cr->duty & DUTY_PME)
3262 status = gmx_pme_init(pmedata,cr,npme_major,npme_minor,inputrec,
3263 mtop ? mtop->natoms : 0,nChargePerturbed,
3264 (Flags & MD_REPRODUCIBLE));
3267 gmx_fatal(FARGS,"Error %d initializing PME",status);
3273 /* if (integrator[inputrec->eI].func == do_md
3276 integrator[inputrec->eI].func == do_md_openmm
3280 /* Turn on signal handling on all nodes */
3282 * (A user signal from the PME nodes (if any)
3283 * is communicated to the PP nodes.
3285 signal_handler_install();
3288 if (cr->duty & DUTY_PP)
3290 if (inputrec->ePull != epullNO)
3292 /* Initialize pull code */
3293 init_pull(fplog,inputrec,nfile,fnm,mtop,cr,oenv,
3294 EI_DYNAMICS(inputrec->eI) && MASTER(cr),Flags);
3297 constr = init_constraints(fplog,mtop,inputrec,ed,state,cr);
3299 if (DOMAINDECOMP(cr))
3301 dd_init_bondeds(fplog,cr->dd,mtop,vsite,constr,inputrec,
3302 Flags & MD_DDBONDCHECK,fr->cginfo_mb);
3304 set_dd_parameters(fplog,cr->dd,dlb_scale,inputrec,fr,&ddbox);
3306 setup_dd_grid(fplog,cr->dd);
3309 /* Now do whatever the user wants us to do (how flexible...) */
3310 do_md_membed(fplog,cr,nfile,fnm,
3311 oenv,bVerbose,bCompact,
3314 nstepout,inputrec,mtop,
3316 mdatoms,nrnb,wcycle,ed,fr,
3317 repl_ex_nst,repl_ex_seed,
3318 cpt_period,max_hours,
3322 fac, r_ins, pos_ins, ins_at,
3323 xy_step, z_step, it_xy, it_z);
3325 if (inputrec->ePull != epullNO)
3327 finish_pull(fplog,inputrec->pull);
3333 gmx_pmeonly(*pmedata,cr,nrnb,wcycle,ewaldcoeff,FALSE,inputrec);
3336 if (EI_DYNAMICS(inputrec->eI) || EI_TPI(inputrec->eI))
3338 /* Some timing stats */
3341 if (runtime.proc == 0)
3343 runtime.proc = runtime.real;
3352 wallcycle_stop(wcycle,ewcRUN);
3354 /* Finish up, write some stuff
3355 * if rerunMD, don't write last frame again
3357 finish_run(fplog,cr,ftp2fn(efSTO,nfile,fnm),
3358 inputrec,nrnb,wcycle,&runtime,
3359 EI_DYNAMICS(inputrec->eI) && !MULTISIM(cr));
3361 /* Does what it says */
3362 print_date_and_time(fplog,cr->nodeid,"Finished mdrun",&runtime);
3364 /* Close logfile already here if we were appending to it */
3365 if (MASTER(cr) && (Flags & MD_APPENDFILES))
3367 gmx_log_close(fplog);
3375 rc=(int)gmx_get_stop_condition();
3380 int gmx_membed(int argc,char *argv[])
3382 const char *desc[] = {
3383 "[TT]g_membed[tt] embeds a membrane protein into an equilibrated lipid bilayer at the position",
3384 "and orientation specified by the user.[PAR]",
3385 "SHORT MANUAL[BR]------------[BR]",
3386 "The user should merge the structure files of the protein and membrane (+solvent), creating a",
3387 "single structure file with the protein overlapping the membrane at the desired position and",
3388 "orientation. The box size is taken from the membrane structure file. The corresponding topology",
3389 "files should also be merged. Consecutively, create a [TT].tpr[tt] file (input for [TT]g_membed[tt]) from these files,"
3390 "with the following options included in the [TT].mdp[tt] file.[BR]",
3391 " - [TT]integrator = md[tt][BR]",
3392 " - [TT]energygrp = Protein[tt] (or other group that you want to insert)[BR]",
3393 " - [TT]freezegrps = Protein[tt][BR]",
3394 " - [TT]freezedim = Y Y Y[tt][BR]",
3395 " - [TT]energygrp_excl = Protein Protein[tt][BR]",
3396 "The output is a structure file containing the protein embedded in the membrane. If a topology",
3397 "file is provided, the number of lipid and ",
3398 "solvent molecules will be updated to match the new structure file.[BR]",
3399 "For a more extensive manual see Wolf et al, J Comp Chem 31 (2010) 2169-2174, Appendix.[PAR]",
3400 "SHORT METHOD DESCRIPTION[BR]",
3401 "------------------------[BR]",
3402 "1. The protein is resized around its center of mass by a factor [TT]-xy[tt] in the xy-plane",
3403 "(the membrane plane) and a factor [TT]-z[tt] in the [IT]z[it]-direction (if the size of the",
3404 "protein in the z-direction is the same or smaller than the width of the membrane, a",
3405 "[TT]-z[tt] value larger than 1 can prevent that the protein will be enveloped by the lipids).[BR]",
3406 "2. All lipid and solvent molecules overlapping with the resized protein are removed. All",
3407 "intraprotein interactions are turned off to prevent numerical issues for small values of [TT]-xy[tt]",
3408 " or [TT]-z[tt][BR]",
3409 "3. One md step is performed.[BR]",
3410 "4. The resize factor ([TT]-xy[tt] or [TT]-z[tt]) is incremented by a small amount ((1-xy)/nxy or (1-z)/nz) and the",
3411 "protein is resized again around its center of mass. The resize factor for the xy-plane",
3412 "is incremented first. The resize factor for the z-direction is not changed until the [TT]-xy[tt] factor",
3413 "is 1 (thus after [TT]-nxy[tt] iterations).[BR]",
3414 "5. Repeat step 3 and 4 until the protein reaches its original size ([TT]-nxy[tt] + [TT]-nz[tt] iterations).[BR]",
3415 "For a more extensive method description see Wolf et al, J Comp Chem, 31 (2010) 2169-2174.[PAR]",
3417 " - Protein can be any molecule you want to insert in the membrane.[BR]",
3418 " - It is recommended to perform a short equilibration run after the embedding",
3419 "(see Wolf et al, J Comp Chem 31 (2010) 2169-2174, to re-equilibrate the membrane. Clearly",
3420 "protein equilibration might require longer.\n",
3421 " - It is now also possible to use the g_membed functionality with mdrun. You should than pass",
3422 "a data file containing the command line options of g_membed following the -membed option, for",
3423 "example mdrun -s into_mem.tpr -membed membed.dat.",
3427 { efTPX, "-f", "into_mem", ffREAD },
3428 { efNDX, "-n", "index", ffOPTRD },
3429 { efTOP, "-p", "topol", ffOPTRW },
3430 { efTRN, "-o", NULL, ffWRITE },
3431 { efXTC, "-x", NULL, ffOPTWR },
3432 { efSTO, "-c", "membedded", ffWRITE },
3433 { efEDR, "-e", "ener", ffWRITE },
3434 { efDAT, "-dat", "membed", ffWRITE }
3435 { efLOG, "-g", "md", ffWRITE },
3436 { efEDI, "-ei", "sam", ffOPTRD },
3437 { efTRX, "-rerun", "rerun", ffOPTRD },
3438 { efXVG, "-table", "table", ffOPTRD },
3439 { efXVG, "-tablep", "tablep", ffOPTRD },
3440 { efXVG, "-tableb", "table", ffOPTRD },
3441 { efXVG, "-dhdl", "dhdl", ffOPTWR },
3442 { efXVG, "-field", "field", ffOPTWR },
3443 { efXVG, "-table", "table", ffOPTRD },
3444 { efXVG, "-tablep", "tablep", ffOPTRD },
3445 { efXVG, "-tableb", "table", ffOPTRD },
3446 { efTRX, "-rerun", "rerun", ffOPTRD },
3447 { efXVG, "-tpi", "tpi", ffOPTWR },
3448 { efXVG, "-tpid", "tpidist", ffOPTWR },
3449 { efEDI, "-ei", "sam", ffOPTRD },
3450 { efEDO, "-eo", "sam", ffOPTWR },
3451 { efGCT, "-j", "wham", ffOPTRD },
3452 { efGCT, "-jo", "bam", ffOPTWR },
3453 { efXVG, "-ffout", "gct", ffOPTWR },
3454 { efXVG, "-devout", "deviatie", ffOPTWR },
3455 { efXVG, "-runav", "runaver", ffOPTWR },
3456 { efXVG, "-px", "pullx", ffOPTWR },
3457 { efXVG, "-pf", "pullf", ffOPTWR },
3458 { efMTX, "-mtx", "nm", ffOPTWR },
3459 { efNDX, "-dn", "dipole", ffOPTWR },
3460 { efRND, "-multidir",NULL, ffOPTRDMULT}
3462 #define NFILE asize(fnm)
3464 /* Command line options ! */
3471 real probe_rad = 0.22;
3475 gmx_bool bALLOW_ASYMMETRY=FALSE;
3476 gmx_bool bStart=FALSE;
3478 gmx_bool bVerbose=FALSE;
3479 char *mdrun_path=NULL;
3481 /* arguments relevant to OPENMM only*/
3483 gmx_input("g_membed not functional in openmm");
3487 { "-xyinit", FALSE, etREAL, {&xy_fac},
3488 "Resize factor for the protein in the xy dimension before starting embedding" },
3489 { "-xyend", FALSE, etREAL, {&xy_max},
3490 "Final resize factor in the xy dimension" },
3491 { "-zinit", FALSE, etREAL, {&z_fac},
3492 "Resize factor for the protein in the z dimension before starting embedding" },
3493 { "-zend", FALSE, etREAL, {&z_max},
3494 "Final resize faction in the z dimension" },
3495 { "-nxy", FALSE, etINT, {&it_xy},
3496 "Number of iteration for the xy dimension" },
3497 { "-nz", FALSE, etINT, {&it_z},
3498 "Number of iterations for the z dimension" },
3499 { "-rad", FALSE, etREAL, {&probe_rad},
3500 "Probe radius to check for overlap between the group to embed and the membrane"},
3501 { "-pieces", FALSE, etINT, {&pieces},
3502 "Perform piecewise resize. Select parts of the group to insert and resize these with respect to their own geometrical center." },
3503 { "-asymmetry",FALSE, etBOOL,{&bALLOW_ASYMMETRY},
3504 "Allow asymmetric insertion, i.e. the number of lipids removed from the upper and lower leaflet will not be checked." },
3505 { "-ndiff" , FALSE, etINT, {&low_up_rm},
3506 "Number of lipids that will additionally be removed from the lower (negative number) or upper (positive number) membrane leaflet." },
3507 { "-maxwarn", FALSE, etINT, {&maxwarn},
3508 "Maximum number of warning allowed" },
3509 { "-start", FALSE, etBOOL, {&bStart},
3510 "Call mdrun with membed options" },
3511 { "-stepout", FALSE, etINT, {&nstepout},
3512 "HIDDENFrequency of writing the remaining runtime" },
3513 { "-v", FALSE, etBOOL,{&bVerbose},
3514 "Be loud and noisy" },
3515 { "-mdrun_path", FALSE, etSTR, {&mdrun_path},
3516 "Path to the mdrun executable compiled with this g_membed version" }
3521 char buf[256],buf2[64];
3523 unsigned long Flags, PCA_Flags;
3526 gmx_bool HaveCheckpoint;
3527 FILE *fplog,*fptest;
3528 int sim_part,sim_part_fn;
3529 const char *part_suffix=".part";
3530 char suffix[STRLEN];
3532 char **multidir=NULL;
3534 cr = init_par(&argc,&argv);
3536 PCA_Flags = (PCA_KEEP_ARGS | PCA_NOEXIT_ON_ARGS | PCA_CAN_SET_DEFFNM
3537 | (MASTER(cr) ? 0 : PCA_QUIET));
3540 /* Comment this in to do fexist calls only on master
3541 * works not with rerun or tables at the moment
3542 * also comment out the version of init_forcerec in md.c
3543 * with NULL instead of opt2fn
3548 PCA_Flags |= PCA_NOT_READ_NODE;
3552 parse_common_args(&argc,argv,PCA_Flags, NFILE,fnm,asize(pa),pa,
3553 asize(desc),desc,0,NULL, &oenv);
3555 /* we set these early because they might be used in init_multisystem()
3556 Note that there is the potential for npme>nnodes until the number of
3557 threads is set later on, if there's thread parallelization. That shouldn't
3558 lead to problems. */
3559 dd_node_order = nenum(ddno_opt);
3560 cr->npmenodes = npme;
3563 /* now determine the number of threads automatically. The threads are
3564 only started at mdrunner_threads, though. */
3567 nthreads=tMPI_Thread_get_hw_number();
3573 /* now check the -multi and -multidir option */
3574 if (opt2bSet("-multidir", NFILE, fnm))
3579 gmx_fatal(FARGS, "mdrun -multi and -multidir options are mutually exclusive.");
3581 nmultisim = opt2fns(&multidir, "-multidir", NFILE, fnm);
3586 parse_common_args(&argc,argv,0, NFILE,fnm,asize(pa),pa,
3587 asize(desc),desc,0,NULL, &oenv);
3588 if (nmultisim > 1) {
3590 gmx_bool bParFn = (multidir == NULL);
3591 init_multisystem(cr,nmultisim,multidir,NFILE,fnm,TRUE);
3593 gmx_fatal(FARGS,"mdrun -multi is not supported with the thread library.Please compile GROMACS with MPI support");
3597 /* Check if there is ANY checkpoint file available */
3599 sim_part_fn = sim_part;
3600 if (opt2bSet("-cpi",NFILE,fnm))
3603 read_checkpoint_simulation_part(opt2fn_master("-cpi", NFILE,fnm,cr),
3604 &sim_part_fn,NULL,cr,
3605 bAppendFiles,NFILE,fnm,
3606 part_suffix,&bAddPart);
3607 if (sim_part_fn==0 && MASTER(cr))
3609 fprintf(stdout,"No previous checkpoint file present, assuming this is a new run.\n");
3613 sim_part = sim_part_fn + 1;
3618 bAppendFiles = FALSE;
3621 data_out = ffopen(opt2fn("-dat",NFILE,fnm),"w");
3622 fprintf(data_out,"nxy = %d\nnz = %d\nxyinit = %f\nxyend = %f\nzinit = %f\nzend = %f\n"
3623 "rad = %f\npieces = %d\nasymmetry = %s\nndiff = %d\nmaxwarn = %d\n",
3624 it_xy,it_z,xy_fac,xy_max,z_fac,z_max,probe_rad,pieces,
3625 bALLOW_ASYMMETRY ? "yes" : "no",low_up_rm,maxwarn);
3628 sprintf(buf,"%s -s %s -membed %s -o %s -c %s -e %s -nt 1 -cpt -1",
3629 (mdrun_path==NULL) ? "mdrun" : mdrun_path,
3630 opt2fn("-f",NFILE,fnm),opt2fn("-dat",NFILE,fnm),opt2fn("-o",NFILE,fnm),
3631 opt2fn("-c",NFILE,fnm),opt2fn("-e",NFILE,fnm));
3632 if (opt2bSet("-n",NFILE,fnm))
3634 sprintf(buf2," -mn %s",opt2fn("-n",NFILE,fnm));
3637 if (opt2bSet("-x",NFILE,fnm))
3639 sprintf(buf2," -x %s",opt2fn("-x",NFILE,fnm));
3642 if (opt2bSet("-p",NFILE,fnm))
3644 sprintf(buf2," -mp %s",opt2fn("-p",NFILE,fnm));
3649 sprintf(buf2," -v -stepout %d",nstepout);
3658 printf("You can membed your protein now by:\n%s\n",buf);
3661 fprintf(stderr,"Please cite:\nWolf et al, J Comp Chem 31 (2010) 2169-2174.\n");