gmx_stochd_t init_stochd(FILE *fplog,t_inputrec *ir) { t_gmx_stochd *sd; gmx_sd_const_t *sdc; int ngtc,n; real y; snew(sd,1); /* Initiate random number generator for langevin type dynamics, * for BD, SD or velocity rescaling temperature coupling. */ sd->gaussrand = gmx_rng_init(ir->ld_seed); ngtc = ir->opts.ngtc; if (ir->eI == eiBD) { snew(sd->bd_rf,ngtc); } else if (EI_SD(ir->eI)) { snew(sd->sdc,ngtc); snew(sd->sdsig,ngtc); sdc = sd->sdc; for(n=0; n<ngtc; n++) { sdc[n].gdt = ir->delta_t/ir->opts.tau_t[n]; sdc[n].eph = exp(sdc[n].gdt/2); sdc[n].emh = exp(-sdc[n].gdt/2); sdc[n].em = exp(-sdc[n].gdt); if (sdc[n].gdt >= 0.05) { sdc[n].b = sdc[n].gdt*(sdc[n].eph*sdc[n].eph - 1) - 4*(sdc[n].eph - 1)*(sdc[n].eph - 1); sdc[n].c = sdc[n].gdt - 3 + 4*sdc[n].emh - sdc[n].em; sdc[n].d = 2 - sdc[n].eph - sdc[n].emh; } else { y = sdc[n].gdt/2; /* Seventh order expansions for small y */ sdc[n].b = y*y*y*y*(1/3.0+y*(1/3.0+y*(17/90.0+y*7/9.0))); sdc[n].c = y*y*y*(2/3.0+y*(-1/2.0+y*(7/30.0+y*(-1/12.0+y*31/1260.0)))); sdc[n].d = y*y*(-1+y*y*(-1/12.0-y*y/360.0)); } if(debug) fprintf(debug,"SD const tc-grp %d: b %g c %g d %g\n", n,sdc[n].b,sdc[n].c,sdc[n].d); } } return sd; }
void init_parallel(FILE *log,char *tpxfile,t_commrec *cr, t_inputrec *inputrec,gmx_mtop_t *mtop, t_state *state, int list) { int step; real t; char buf[256]; if (MASTER(cr)) { init_inputrec(inputrec); read_tpx_state(tpxfile,&step,&t,inputrec,state,NULL,mtop); /* When we will be doing domain decomposition with separate PME nodes * the rng entries will be too large, we correct for this later. */ set_state_entries(state,inputrec,cr->nnodes); } bcast_ir_mtop(cr,inputrec,mtop); if (inputrec->eI == eiBD || EI_SD(inputrec->eI)) { /* Make sure the random seeds are different on each node */ inputrec->ld_seed += cr->nodeid; } /* Printing */ if (list!=0 && log!=NULL) { if (list&LIST_INPUTREC) pr_inputrec(log,0,"parameters of the run",inputrec,FALSE); if (list&LIST_X) pr_rvecs(log,0,"box",state->box,DIM); if (list&LIST_X) pr_rvecs(log,0,"box_rel",state->box_rel,DIM); if (list&LIST_V) pr_rvecs(log,0,"boxv",state->boxv,DIM); if (list&LIST_X) pr_rvecs(log,0,int_title("x",0,buf,255),state->x,state->natoms); if (list&LIST_V) pr_rvecs(log,0,int_title("v",0,buf,255),state->v,state->natoms); if (list&LIST_TOP) pr_mtop(log,0,int_title("topology",cr->nodeid,buf,255),mtop,TRUE); fflush(log); } }
int cmain (int argc, char *argv[]) { const char *desc[] = { "tpbconv can edit run input files in four ways.[PAR]", "[BB]1.[bb] by modifying the number of steps in a run input file", "with options [TT]-extend[tt], [TT]-until[tt] or [TT]-nsteps[tt]", "(nsteps=-1 means unlimited number of steps)[PAR]", "[BB]2.[bb] (OBSOLETE) by creating a run input file", "for a continuation run when your simulation has crashed due to e.g.", "a full disk, or by making a continuation run input file.", "This option is obsolete, since mdrun now writes and reads", "checkpoint files.", "[BB]Note[bb] that a frame with coordinates and velocities is needed.", "When pressure and/or Nose-Hoover temperature coupling is used", "an energy file can be supplied to get an exact continuation", "of the original run.[PAR]", "[BB]3.[bb] by creating a [TT].tpx[tt] file for a subset of your original", "tpx file, which is useful when you want to remove the solvent from", "your [TT].tpx[tt] file, or when you want to make e.g. a pure C[GRK]alpha[grk] [TT].tpx[tt] file.", "Note that you may need to use [TT]-nsteps -1[tt] (or similar) to get", "this to work.", "[BB]WARNING: this [TT].tpx[tt] file is not fully functional[bb].[PAR]", "[BB]4.[bb] by setting the charges of a specified group", "to zero. This is useful when doing free energy estimates", "using the LIE (Linear Interaction Energy) method." }; const char *top_fn, *frame_fn; t_fileio *fp; ener_file_t fp_ener = NULL; t_trnheader head; int i; gmx_large_int_t nsteps_req, run_step, frame; double run_t, state_t; gmx_bool bOK, bNsteps, bExtend, bUntil, bTime, bTraj; gmx_bool bFrame, bUse, bSel, bNeedEner, bReadEner, bScanEner, bFepState; gmx_mtop_t mtop; t_atoms atoms; t_inputrec *ir, *irnew = NULL; t_gromppopts *gopts; t_state state; rvec *newx = NULL, *newv = NULL, *tmpx, *tmpv; matrix newbox; int gnx; char *grpname; atom_id *index = NULL; int nre; gmx_enxnm_t *enm = NULL; t_enxframe *fr_ener = NULL; char buf[200], buf2[200]; output_env_t oenv; t_filenm fnm[] = { { efTPX, NULL, NULL, ffREAD }, { efTRN, "-f", NULL, ffOPTRD }, { efEDR, "-e", NULL, ffOPTRD }, { efNDX, NULL, NULL, ffOPTRD }, { efTPX, "-o", "tpxout", ffWRITE } }; #define NFILE asize(fnm) /* Command line options */ static int nsteps_req_int = 0; static real start_t = -1.0, extend_t = 0.0, until_t = 0.0; static int init_fep_state = 0; static gmx_bool bContinuation = TRUE, bZeroQ = FALSE, bVel = TRUE; static t_pargs pa[] = { { "-extend", FALSE, etREAL, {&extend_t}, "Extend runtime by this amount (ps)" }, { "-until", FALSE, etREAL, {&until_t}, "Extend runtime until this ending time (ps)" }, { "-nsteps", FALSE, etINT, {&nsteps_req_int}, "Change the number of steps" }, { "-time", FALSE, etREAL, {&start_t}, "Continue from frame at this time (ps) instead of the last frame" }, { "-zeroq", FALSE, etBOOL, {&bZeroQ}, "Set the charges of a group (from the index) to zero" }, { "-vel", FALSE, etBOOL, {&bVel}, "Require velocities from trajectory" }, { "-cont", FALSE, etBOOL, {&bContinuation}, "For exact continuation, the constraints should not be applied before the first step" }, { "-init_fep_state", FALSE, etINT, {&init_fep_state}, "fep state to initialize from" }, }; int nerror = 0; CopyRight(stderr, argv[0]); /* Parse the command line */ parse_common_args(&argc, argv, 0, NFILE, fnm, asize(pa), pa, asize(desc), desc, 0, NULL, &oenv); /* Convert int to gmx_large_int_t */ nsteps_req = nsteps_req_int; bNsteps = opt2parg_bSet("-nsteps", asize(pa), pa); bExtend = opt2parg_bSet("-extend", asize(pa), pa); bUntil = opt2parg_bSet("-until", asize(pa), pa); bFepState = opt2parg_bSet("-init_fep_state", asize(pa), pa); bTime = opt2parg_bSet("-time", asize(pa), pa); bTraj = (opt2bSet("-f", NFILE, fnm) || bTime); top_fn = ftp2fn(efTPX, NFILE, fnm); fprintf(stderr, "Reading toplogy and stuff from %s\n", top_fn); snew(ir, 1); read_tpx_state(top_fn, ir, &state, NULL, &mtop); run_step = ir->init_step; run_t = ir->init_step*ir->delta_t + ir->init_t; if (!EI_STATE_VELOCITY(ir->eI)) { bVel = FALSE; } if (bTraj) { fprintf(stderr, "\n" "NOTE: Reading the state from trajectory is an obsolete feature of tpbconv.\n" " Continuation should be done by loading a checkpoint file with mdrun -cpi\n" " This guarantees that all state variables are transferred.\n" " tpbconv is now only useful for increasing nsteps,\n" " but even that can often be avoided by using mdrun -maxh\n" "\n"); if (ir->bContinuation != bContinuation) { fprintf(stderr, "Modifying ir->bContinuation to %s\n", bool_names[bContinuation]); } ir->bContinuation = bContinuation; bNeedEner = (ir->epc == epcPARRINELLORAHMAN || ir->etc == etcNOSEHOOVER); bReadEner = (bNeedEner && ftp2bSet(efEDR, NFILE, fnm)); bScanEner = (bReadEner && !bTime); if (ir->epc != epcNO || EI_SD(ir->eI) || ir->eI == eiBD) { fprintf(stderr, "NOTE: The simulation uses pressure coupling and/or stochastic dynamics.\n" "tpbconv can not provide binary identical continuation.\n" "If you want that, supply a checkpoint file to mdrun\n\n"); } if (EI_SD(ir->eI) || ir->eI == eiBD) { fprintf(stderr, "\nChanging ld-seed from %d ", ir->ld_seed); ir->ld_seed = make_seed(); fprintf(stderr, "to %d\n\n", ir->ld_seed); } frame_fn = ftp2fn(efTRN, NFILE, fnm); if (fn2ftp(frame_fn) == efCPT) { int sim_part; fprintf(stderr, "\nREADING STATE FROM CHECKPOINT %s...\n\n", frame_fn); read_checkpoint_state(frame_fn, &sim_part, &run_step, &run_t, &state); } else { fprintf(stderr, "\nREADING COORDS, VELS AND BOX FROM TRAJECTORY %s...\n\n", frame_fn); fp = open_trn(frame_fn, "r"); if (bScanEner) { fp_ener = open_enx(ftp2fn(efEDR, NFILE, fnm), "r"); do_enxnms(fp_ener, &nre, &enm); snew(fr_ener, 1); fr_ener->t = -1e-12; } /* Now scan until the last set of x and v (step == 0) * or the ones at step step. */ bFrame = TRUE; frame = 0; while (bFrame) { bFrame = fread_trnheader(fp, &head, &bOK); if (bOK && frame == 0) { if (mtop.natoms != head.natoms) { gmx_fatal(FARGS, "Number of atoms in Topology (%d) " "is not the same as in Trajectory (%d)\n", mtop.natoms, head.natoms); } snew(newx, head.natoms); snew(newv, head.natoms); } bFrame = bFrame && bOK; if (bFrame) { bOK = fread_htrn(fp, &head, newbox, newx, newv, NULL); } bFrame = bFrame && bOK; bUse = FALSE; if (bFrame && (head.x_size) && (head.v_size || !bVel)) { bUse = TRUE; if (bScanEner) { /* Read until the energy time is >= the trajectory time */ while (fr_ener->t < head.t && do_enx(fp_ener, fr_ener)) { ; } bUse = (fr_ener->t == head.t); } if (bUse) { tmpx = newx; newx = state.x; state.x = tmpx; tmpv = newv; newv = state.v; state.v = tmpv; run_t = head.t; run_step = head.step; state.fep_state = head.fep_state; state.lambda[efptFEP] = head.lambda; copy_mat(newbox, state.box); } } if (bFrame || !bOK) { sprintf(buf, "\r%s %s frame %s%s: step %s%s time %s", "%s", "%s", "%6", gmx_large_int_fmt, "%6", gmx_large_int_fmt, " %8.3f"); fprintf(stderr, buf, bUse ? "Read " : "Skipped", ftp2ext(fn2ftp(frame_fn)), frame, head.step, head.t); frame++; if (bTime && (head.t >= start_t)) { bFrame = FALSE; } } } if (bScanEner) { close_enx(fp_ener); free_enxframe(fr_ener); free_enxnms(nre, enm); } close_trn(fp); fprintf(stderr, "\n"); if (!bOK) { fprintf(stderr, "%s frame %s (step %s, time %g) is incomplete\n", ftp2ext(fn2ftp(frame_fn)), gmx_step_str(frame-1, buf2), gmx_step_str(head.step, buf), head.t); } fprintf(stderr, "\nUsing frame of step %s time %g\n", gmx_step_str(run_step, buf), run_t); if (bNeedEner) { if (bReadEner) { get_enx_state(ftp2fn(efEDR, NFILE, fnm), run_t, &mtop.groups, ir, &state); } else { fprintf(stderr, "\nWARNING: The simulation uses %s temperature and/or %s pressure coupling,\n" " the continuation will only be exact when an energy file is supplied\n\n", ETCOUPLTYPE(etcNOSEHOOVER), EPCOUPLTYPE(epcPARRINELLORAHMAN)); } } if (bFepState) { ir->fepvals->init_fep_state = init_fep_state; } } } if (bNsteps) { fprintf(stderr, "Setting nsteps to %s\n", gmx_step_str(nsteps_req, buf)); ir->nsteps = nsteps_req; } else { /* Determine total number of steps remaining */ if (bExtend) { ir->nsteps = ir->nsteps - (run_step - ir->init_step) + (gmx_large_int_t)(extend_t/ir->delta_t + 0.5); printf("Extending remaining runtime of by %g ps (now %s steps)\n", extend_t, gmx_step_str(ir->nsteps, buf)); } else if (bUntil) { printf("nsteps = %s, run_step = %s, current_t = %g, until = %g\n", gmx_step_str(ir->nsteps, buf), gmx_step_str(run_step, buf2), run_t, until_t); ir->nsteps = (gmx_large_int_t)((until_t - run_t)/ir->delta_t + 0.5); printf("Extending remaining runtime until %g ps (now %s steps)\n", until_t, gmx_step_str(ir->nsteps, buf)); } else { ir->nsteps -= run_step - ir->init_step; /* Print message */ printf("%s steps (%g ps) remaining from first run.\n", gmx_step_str(ir->nsteps, buf), ir->nsteps*ir->delta_t); } } if (bNsteps || bZeroQ || (ir->nsteps > 0)) { ir->init_step = run_step; if (ftp2bSet(efNDX, NFILE, fnm) || !(bNsteps || bExtend || bUntil || bTraj)) { atoms = gmx_mtop_global_atoms(&mtop); get_index(&atoms, ftp2fn_null(efNDX, NFILE, fnm), 1, &gnx, &index, &grpname); if (!bZeroQ) { bSel = (gnx != state.natoms); for (i = 0; ((i < gnx) && (!bSel)); i++) { bSel = (i != index[i]); } } else { bSel = FALSE; } if (bSel) { fprintf(stderr, "Will write subset %s of original tpx containing %d " "atoms\n", grpname, gnx); reduce_topology_x(gnx, index, &mtop, state.x, state.v); state.natoms = gnx; } else if (bZeroQ) { zeroq(gnx, index, &mtop); fprintf(stderr, "Zero-ing charges for group %s\n", grpname); } else { fprintf(stderr, "Will write full tpx file (no selection)\n"); } } state_t = ir->init_t + ir->init_step*ir->delta_t; sprintf(buf, "Writing statusfile with starting step %s%s and length %s%s steps...\n", "%10", gmx_large_int_fmt, "%10", gmx_large_int_fmt); fprintf(stderr, buf, ir->init_step, ir->nsteps); fprintf(stderr, " time %10.3f and length %10.3f ps\n", state_t, ir->nsteps*ir->delta_t); write_tpx_state(opt2fn("-o", NFILE, fnm), ir, &state, &mtop); } else { printf("You've simulated long enough. Not writing tpr file\n"); } thanx(stderr); return 0; }
static void set_state_entries(t_state *state,t_inputrec *ir,int nnodes) { /* The entries in the state in the tpx file might not correspond * with what is needed, so we correct this here. */ state->flags = 0; if (ir->efep != efepNO) state->flags |= (1<<estLAMBDA); state->flags |= (1<<estX); if (state->x == NULL) snew(state->x,state->nalloc); if (EI_DYNAMICS(ir->eI)) { state->flags |= (1<<estV); if (state->v == NULL) snew(state->v,state->nalloc); } if (ir->eI == eiSD2) { state->flags |= (1<<estSDX); if (state->sd_X == NULL) { /* sd_X is not stored in the tpx file, so we need to allocate it */ snew(state->sd_X,state->nalloc); } } if (ir->eI == eiCG) { state->flags |= (1<<estCGP); } if (EI_SD(ir->eI) || ir->eI == eiBD || ir->etc == etcVRESCALE) { state->nrng = gmx_rng_n(); state->nrngi = 1; if (EI_SD(ir->eI) || ir->eI == eiBD) { /* This will be correct later with DD */ state->nrng *= nnodes; state->nrngi *= nnodes; } state->flags |= ((1<<estLD_RNG) | (1<<estLD_RNGI)); snew(state->ld_rng, state->nrng); snew(state->ld_rngi,state->nrngi); } else { state->nrng = 0; } if (ir->ePBC != epbcNONE) { state->flags |= (1<<estBOX); if (PRESERVE_SHAPE(*ir)) { state->flags |= (1<<estBOX_REL); } if (ir->epc == epcPARRINELLORAHMAN) { state->flags |= (1<<estBOXV); } if (ir->epc != epcNO) { state->flags |= (1<<estPRES_PREV); } if (ir->etc == etcNOSEHOOVER) { state->flags |= (1<<estNH_XI); } } if (ir->etc == etcNOSEHOOVER || ir->etc == etcVRESCALE) { state->flags |= (1<<estTC_INT); } init_ekinstate(&state->ekinstate,ir); init_energyhistory(&state->enerhist); }
void init_md(FILE *fplog, t_commrec *cr,t_inputrec *ir,real *t,real *t0, real *lambda,real *lam0, t_nrnb *nrnb,gmx_mtop_t *mtop, gmx_stochd_t *sd, int nfile,t_filenm fnm[], int *fp_trn,int *fp_xtc,int *fp_ene,char **fn_cpt, FILE **fp_dgdl,FILE **fp_field, t_mdebin **mdebin, tensor force_vir,tensor shake_vir,rvec mu_tot, bool *bNEMD,bool *bSimAnn,t_vcm **vcm, unsigned long Flags) { int i,j,n; real tmpt,mod; char filemode[2]; sprintf(filemode, (Flags & MD_APPENDFILES) ? "a" : "w"); /* Initial values */ *t = *t0 = ir->init_t; if (ir->efep != efepNO) { *lam0 = ir->init_lambda; *lambda = *lam0 + ir->init_step*ir->delta_lambda; } else { *lambda = *lam0 = 0.0; } *bSimAnn=FALSE; for(i=0;i<ir->opts.ngtc;i++) { /* set bSimAnn if any group is being annealed */ if(ir->opts.annealing[i]!=eannNO) *bSimAnn = TRUE; } if (*bSimAnn) { update_annealing_target_temp(&(ir->opts),ir->init_t); } *bNEMD = (ir->opts.ngacc > 1) || (norm(ir->opts.acc[0]) > 0); if (sd && (ir->eI == eiBD || EI_SD(ir->eI) || ir->etc == etcVRESCALE)) { *sd = init_stochd(fplog,ir); } if (vcm) { *vcm = init_vcm(fplog,&mtop->groups,ir); } if (EI_DYNAMICS(ir->eI) && !(Flags & MD_APPENDFILES)) { if (ir->etc == etcBERENDSEN) { please_cite(fplog,"Berendsen84a"); } if (ir->etc == etcVRESCALE) { please_cite(fplog,"Bussi2007a"); } } init_nrnb(nrnb); if (nfile != -1) { *fp_trn = -1; *fp_ene = -1; *fp_xtc = -1; if (MASTER(cr)) { *fp_trn = open_trn(ftp2fn(efTRN,nfile,fnm), filemode); if (ir->nstxtcout > 0) { *fp_xtc = open_xtc(ftp2fn(efXTC,nfile,fnm), filemode); } *fp_ene = open_enx(ftp2fn(efENX,nfile,fnm), filemode); *fn_cpt = opt2fn("-cpo",nfile,fnm); if ((fp_dgdl != NULL) && ir->efep!=efepNO) { if(Flags & MD_APPENDFILES) { *fp_dgdl= gmx_fio_fopen(opt2fn("-dgdl",nfile,fnm),filemode); } else { *fp_dgdl = xvgropen(opt2fn("-dgdl",nfile,fnm), "dG/d\\8l\\4","Time (ps)", "dG/d\\8l\\4 (kJ mol\\S-1\\N [\\8l\\4]\\S-1\\N)"); } } if ((fp_field != NULL) && (ir->ex[XX].n || ir->ex[YY].n ||ir->ex[ZZ].n)) { if(Flags & MD_APPENDFILES) { *fp_field = gmx_fio_fopen(opt2fn("-field",nfile,fnm),filemode); } else { *fp_field = xvgropen(opt2fn("-field",nfile,fnm), "Applied electric field","Time (ps)", "E (V/nm)"); } } } *mdebin = init_mdebin( (Flags & MD_APPENDFILES) ? -1 : *fp_ene,mtop,ir); } /* Initiate variables */ clear_mat(force_vir); clear_mat(shake_vir); clear_rvec(mu_tot); debug_gmx(); }