static gmx_bool gmx_next_frame(t_trxstatus *status, t_trxframe *fr) { t_trnheader sh; gmx_bool bOK, bRet; bRet = FALSE; if (fread_trnheader(status->fio, &sh, &bOK)) { fr->bDouble = sh.bDouble; fr->natoms = sh.natoms; fr->bStep = TRUE; fr->step = sh.step; fr->bTime = TRUE; fr->time = sh.t; fr->bLambda = TRUE; fr->bFepState = TRUE; fr->lambda = sh.lambda; fr->bBox = sh.box_size > 0; if (fr->flags & (TRX_READ_X | TRX_NEED_X)) { if (fr->x == NULL) { snew(fr->x, sh.natoms); } fr->bX = sh.x_size > 0; } if (fr->flags & (TRX_READ_V | TRX_NEED_V)) { if (fr->v == NULL) { snew(fr->v, sh.natoms); } fr->bV = sh.v_size > 0; } if (fr->flags & (TRX_READ_F | TRX_NEED_F)) { if (fr->f == NULL) { snew(fr->f, sh.natoms); } fr->bF = sh.f_size > 0; } if (fread_htrn(status->fio, &sh, fr->box, fr->x, fr->v, fr->f)) { bRet = TRUE; } else { fr->not_ok = DATA_NOT_OK; } } else if (!bOK) { fr->not_ok = HEADER_NOT_OK; } return bRet; }
/* Test traj functions */ void test5() { const char *trrpath = "../sim/traj.trr"; t_trnheader header; gmx_bool bOK; t_fileio *fh = open_trn(trrpath, "r"); int step, natoms; real t, lambda; rvec *box, *x, *v, *f; int frame = 0; while(fread_trnheader(fh, &header, &bOK)) { box = (rvec *)calloc(3, sizeof(rvec)); x = (rvec *)calloc(header.natoms, sizeof(rvec)); v = (rvec *)calloc(header.natoms, sizeof(rvec)); f = (rvec *)calloc(header.natoms, sizeof(rvec)); // `fread_trn` doesn't work so use `fread_htrn` instead if(fread_htrn(fh, &header, box, x, v, f)) { printf("%5d %5d %10.2f %f\n", frame++, header.step, header.t, header.lambda); guamps_write_rvec(stdout, f, header.natoms); } free(box); free(x); free(v); free(f); } }
static void list_trn(char *fn) { int fpread,fpwrite,nframe,indent; char buf[256]; rvec *x,*v,*f; matrix box; t_trnheader trn; bool bOK; fpread = open_trn(fn,"r"); fpwrite = open_tpx(NULL,"w"); gmx_fio_setdebug(fpwrite,TRUE); nframe = 0; while (fread_trnheader(fpread,&trn,&bOK)) { snew(x,trn.natoms); snew(v,trn.natoms); snew(f,trn.natoms); if (fread_htrn(fpread,&trn, trn.box_size ? box : NULL, trn.x_size ? x : NULL, trn.v_size ? v : NULL, trn.f_size ? f : NULL)) { sprintf(buf,"%s frame %d",fn,nframe); indent=0; indent=pr_title(stdout,indent,buf); pr_indent(stdout,indent); fprintf(stdout,"natoms=%10d step=%10d time=%12.7e lambda=%10g\n", trn.natoms,trn.step,trn.t,trn.lambda); if (trn.box_size) pr_rvecs(stdout,indent,"box",box,DIM); if (trn.x_size) pr_rvecs(stdout,indent,"x",x,trn.natoms); if (trn.v_size) pr_rvecs(stdout,indent,"v",v,trn.natoms); if (trn.f_size) pr_rvecs(stdout,indent,"f",f,trn.natoms); } else fprintf(stderr,"\nWARNING: Incomplete frame: nr %d, t=%g\n", nframe,trn.t); sfree(x); sfree(v); sfree(f); nframe++; } if (!bOK) fprintf(stderr,"\nWARNING: Incomplete frame header: nr %d, t=%g\n", nframe,trn.t); close_tpx(fpwrite); close_trn(fpread); }
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 list_trn(char *fn) { static real mass[5] = { 15.9994, 1.008, 1.008, 0.0, 0.0 }; int i,j=0,m,fpread,fpwrite,nframe; rvec *x,*v,*f,fmol[2],xcm[2],torque[j],dx; real mmm,len; matrix box; t_trnheader trn; gmx_bool bOK; printf("Going to open %s\n",fn); fpread = open_trn(fn,"r"); fpwrite = open_tpx(NULL,"w"); gmx_fio_setdebug(fpwrite,TRUE); mmm=mass[0]+2*mass[1]; for(i=0; (i<5); i++) mass[i] /= mmm; nframe = 0; while (fread_trnheader(fpread,&trn,&bOK)) { snew(x,trn.natoms); snew(v,trn.natoms); snew(f,trn.natoms); if (fread_htrn(fpread,&trn, trn.box_size ? box : NULL, trn.x_size ? x : NULL, trn.v_size ? v : NULL, trn.f_size ? f : NULL)) { if (trn.x_size && trn.f_size) { printf("There are %d atoms\n",trn.natoms); for(j=0; (j<2); j++) { clear_rvec(xcm[j]); clear_rvec(fmol[j]); clear_rvec(torque[j]); for(i=5*j; (i<5*j+5); i++) { rvec_inc(fmol[j],f[i]); for(m=0; (m<DIM); m++) xcm[j][m] += mass[i%5]*x[i][m]; } for(i=5*j; (i<5*j+5); i++) { rvec_dec(x[i],xcm[j]); cprod(x[i],f[i],dx); rvec_inc(torque[j],dx); rvec_inc(x[i],xcm[j]); } } pr_rvecs(stdout,0,"FMOL ",fmol,2); pr_rvecs(stdout,0,"TORQUE",torque,2); printf("Distance matrix Water1-Water2\n%5s",""); for(j=0; (j<5); j++) printf(" %10s",nm[j]); printf("\n"); for(j=0; (j<5); j++) { printf("%5s",nm[j]); for(i=5; (i<10); i++) { rvec_sub(x[i],x[j],dx); len = sqrt(iprod(dx,dx)); printf(" %10.7f",len); } printf("\n"); } } } sfree(x); sfree(v); sfree(f); nframe++; } if (!bOK) fprintf(stderr,"\nWARNING: Incomplete frame header: nr %d, t=%g\n", nframe,trn.t); close_tpx(fpwrite); close_trn(fpread); }
void read_eigenvectors(char *file,int *natoms,bool *bFit, rvec **xref,bool *bDMR, rvec **xav,bool *bDMA, int *nvec, int **eignr, rvec ***eigvec,real **eigval) { t_trnheader head; int status,i,snew_size; rvec *x; matrix box; bool bOK; *bDMR=FALSE; /* read (reference (t=-1) and) average (t=0) structure */ status=open_trn(file,"r"); fread_trnheader(status,&head,&bOK); *natoms=head.natoms; snew(*xav,*natoms); fread_htrn(status,&head,box,*xav,NULL,NULL); if ((head.t>=-1.1) && (head.t<=-0.9)) { snew(*xref,*natoms); for(i=0; i<*natoms; i++) copy_rvec((*xav)[i],(*xref)[i]); *bDMR = (head.lambda > 0.5); *bFit = (head.lambda > -0.5); if (*bFit) { fprintf(stderr,"Read %smass weighted reference structure with %d atoms from %s\n", *bDMR ? "" : "non ",*natoms,file); } else { fprintf(stderr,"Eigenvectors in %s were determined without fitting\n",file); sfree(*xref); *xref=NULL; } fread_trnheader(status,&head,&bOK); fread_htrn(status,&head,box,*xav,NULL,NULL); } else { *bFit=TRUE; *xref=NULL; } *bDMA = (head.lambda > 0.5); if ((head.t<=-0.01) || (head.t>=0.01)) { fprintf(stderr,"WARNING: %s does not start with t=0, which should be the " "average structure. This might not be a eigenvector file. " "Some things might go wrong.\n", file); } else { fprintf(stderr, "Read %smass weighted average/minimum structure with %d atoms from %s\n", *bDMA ? "" : "non ",*natoms,file); } snew(x,*natoms); snew_size=10; snew(*eignr,snew_size); snew(*eigval,snew_size); snew(*eigvec,snew_size); *nvec=0; while (fread_trnheader(status,&head,&bOK)) { fread_htrn(status,&head,box,x,NULL,NULL); if (*nvec >= snew_size) { snew_size+=10; srenew(*eignr,snew_size); srenew(*eigval,snew_size); srenew(*eigvec,snew_size); } i=head.step; (*eigval)[*nvec]=head.t; (*eignr)[*nvec]=i-1; snew((*eigvec)[*nvec],*natoms); for(i=0; i<*natoms; i++) { copy_rvec(x[i],(*eigvec)[*nvec][i]); } (*nvec)++; } sfree(x); fprintf(stderr,"Read %d eigenvectors (for %d atoms)\n\n",*nvec,*natoms); }