int write_trxframe(t_trxstatus *status,t_trxframe *fr,gmx_conect gc) { char title[STRLEN]; real prec; if (fr->bPrec) prec = fr->prec; else prec = 1000.0; switch (gmx_fio_getftp(status->fio)) { case efTRJ: case efTRR: break; default: if (!fr->bX) gmx_fatal(FARGS,"Need coordinates to write a %s trajectory", ftp2ext(gmx_fio_getftp(status->fio))); break; } switch (gmx_fio_getftp(status->fio)) { case efXTC: write_xtc(status->fio,fr->natoms,fr->step,fr->time,fr->box,fr->x,prec); break; case efTRJ: case efTRR: fwrite_trn(status->fio,fr->step,fr->time,fr->lambda,fr->box,fr->natoms, fr->bX ? fr->x:NULL,fr->bV ? fr->v:NULL ,fr->bF ? fr->f:NULL); break; case efGRO: case efPDB: case efBRK: case efENT: if (!fr->bAtoms) gmx_fatal(FARGS,"Can not write a %s file without atom names", ftp2ext(gmx_fio_getftp(status->fio))); sprintf(title,"frame t= %.3f",fr->time); if (gmx_fio_getftp(status->fio) == efGRO) write_hconf_p(gmx_fio_getfp(status->fio),title,fr->atoms, prec2ndec(prec),fr->x,fr->bV ? fr->v : NULL,fr->box); else write_pdbfile(gmx_fio_getfp(status->fio),title, fr->atoms,fr->x,fr->bPBC ? fr->ePBC : -1,fr->box, ' ',fr->step,gc,TRUE); break; case efG87: write_gms(gmx_fio_getfp(status->fio),fr->natoms,fr->x,fr->box); break; case efG96: write_g96_conf(gmx_fio_getfp(status->fio),fr,-1,NULL); break; default: gmx_fatal(FARGS,"Sorry, write_trxframe can not write %s", ftp2ext(gmx_fio_getftp(status->fio))); break; } return 0; }
void write_xtc_traj(FILE *log,t_commrec *cr, char *xtc_traj,t_nsborder *nsb,t_mdatoms *md, int step,real t,rvec *xx,matrix box,real prec) { static bool bFirst=TRUE; static rvec *x_sel; static int natoms; int i,j; if ((bFirst) && MASTER(cr)) { #ifdef DEBUG fprintf(log,"Going to open compressed trajectory file: %s\n",xtc_traj); #endif xd = open_xtc(xtc_traj,"w"); /* Count the number of atoms in the selection */ natoms=0; for(i=0; (i<md->nr); i++) if (md->cXTC[i] == 0) natoms++; if(log) fprintf(log,"There are %d atoms in your xtc output selection\n",natoms); if (natoms != md->nr) snew(x_sel,natoms); bFirst=FALSE; } if (cr->nnodes > 1) { MX(xx); } if ((xx) && MASTER(cr)) { if (natoms == md->nr) x_sel = xx; else { /* We need to copy everything into a temp array */ for(i=j=0; (i<md->nr); i++) { if (md->cXTC[i] == 0) { copy_rvec(xx[i],x_sel[j]); j++; } } } if (write_xtc(xd,natoms,step,t,box,x_sel,prec) == 0) fatal_error(0,"XTC error"); } }
/*! * This will take an arbitrary particle vector and add it * to an xtc file. No shifting is done - only modification is conversion * from aangstom to nanometers. The box dimensions for the frame must be manually * set by the ioxtc::setbox() function before calling this. */ bool FormatXTC::save(string file, const p_vec &p) { if (xd==NULL) xd=xdrfile_open(&file[0], "w"); if (xd!=NULL) { rvec *x = new rvec [p.size()]; int i=0; for (auto &pi : p) { x[i][0] = (pi.x() ) * 0.1; // AA->nm x[i][1] = (pi.y() ) * 0.1; x[i][2] = (pi.z() ) * 0.1; i++; } write_xtc(xd,p.size(),step_xtc++,time_xtc++,xdbox,x,prec_xtc); delete[] x; return true; } return false; }
int write_trxframe(t_trxstatus *status, t_trxframe *fr, gmx_conect gc) { char title[STRLEN]; real prec; if (fr->bPrec) { prec = fr->prec; } else { prec = 1000.0; } if (status->tng) { gmx_tng_set_compression_precision(status->tng, prec); write_tng_frame(status, fr); return 0; } switch (gmx_fio_getftp(status->fio)) { case efTRR: break; default: if (!fr->bX) { gmx_fatal(FARGS, "Need coordinates to write a %s trajectory", ftp2ext(gmx_fio_getftp(status->fio))); } break; } switch (gmx_fio_getftp(status->fio)) { case efXTC: write_xtc(status->fio, fr->natoms, fr->step, fr->time, fr->box, fr->x, prec); break; case efTRR: gmx_trr_write_frame(status->fio, fr->step, fr->time, fr->lambda, fr->box, fr->natoms, fr->bX ? fr->x : nullptr, fr->bV ? fr->v : nullptr, fr->bF ? fr->f : nullptr); break; case efGRO: case efPDB: case efBRK: case efENT: if (!fr->bAtoms) { gmx_fatal(FARGS, "Can not write a %s file without atom names", ftp2ext(gmx_fio_getftp(status->fio))); } sprintf(title, "frame t= %.3f", fr->time); if (gmx_fio_getftp(status->fio) == efGRO) { write_hconf_p(gmx_fio_getfp(status->fio), title, fr->atoms, fr->x, fr->bV ? fr->v : nullptr, fr->box); } else { write_pdbfile(gmx_fio_getfp(status->fio), title, fr->atoms, fr->x, fr->bPBC ? fr->ePBC : -1, fr->box, ' ', fr->step, gc, TRUE); } break; case efG96: write_g96_conf(gmx_fio_getfp(status->fio), title, fr, -1, nullptr); break; default: gmx_fatal(FARGS, "Sorry, write_trxframe can not write %s", ftp2ext(gmx_fio_getftp(status->fio))); break; } return 0; }
int write_trxframe_indexed(t_trxstatus *status, const t_trxframe *fr, int nind, const int *ind, gmx_conect gc) { char title[STRLEN]; rvec *xout = nullptr, *vout = nullptr, *fout = nullptr; int i, ftp = -1; real prec; if (fr->bPrec) { prec = fr->prec; } else { prec = 1000.0; } if (status->tng) { ftp = efTNG; } else if (status->fio) { ftp = gmx_fio_getftp(status->fio); } else { gmx_incons("No input file available"); } switch (ftp) { case efTRR: case efTNG: break; default: if (!fr->bX) { gmx_fatal(FARGS, "Need coordinates to write a %s trajectory", ftp2ext(ftp)); } break; } switch (ftp) { case efTRR: case efTNG: if (fr->bV) { snew(vout, nind); for (i = 0; i < nind; i++) { copy_rvec(fr->v[ind[i]], vout[i]); } } if (fr->bF) { snew(fout, nind); for (i = 0; i < nind; i++) { copy_rvec(fr->f[ind[i]], fout[i]); } } // fallthrough case efXTC: if (fr->bX) { snew(xout, nind); for (i = 0; i < nind; i++) { copy_rvec(fr->x[ind[i]], xout[i]); } } break; default: break; } switch (ftp) { case efTNG: gmx_write_tng_from_trxframe(status->tng, fr, nind); break; case efXTC: write_xtc(status->fio, nind, fr->step, fr->time, fr->box, xout, prec); break; case efTRR: gmx_trr_write_frame(status->fio, nframes_read(status), fr->time, fr->step, fr->box, nind, xout, vout, fout); break; case efGRO: case efPDB: case efBRK: case efENT: if (!fr->bAtoms) { gmx_fatal(FARGS, "Can not write a %s file without atom names", ftp2ext(ftp)); } sprintf(title, "frame t= %.3f", fr->time); if (ftp == efGRO) { write_hconf_indexed_p(gmx_fio_getfp(status->fio), title, fr->atoms, nind, ind, fr->x, fr->bV ? fr->v : nullptr, fr->box); } else { write_pdbfile_indexed(gmx_fio_getfp(status->fio), title, fr->atoms, fr->x, -1, fr->box, ' ', fr->step, nind, ind, gc, TRUE); } break; case efG96: sprintf(title, "frame t= %.3f", fr->time); write_g96_conf(gmx_fio_getfp(status->fio), title, fr, nind, ind); break; default: gmx_fatal(FARGS, "Sorry, write_trxframe_indexed can not write %s", ftp2ext(ftp)); break; } switch (ftp) { case efTRR: case efTNG: if (vout) { sfree(vout); } if (fout) { sfree(fout); } // fallthrough case efXTC: sfree(xout); break; default: break; } return 0; }
void mdoutf_write_to_trajectory_files(FILE *fplog, t_commrec *cr, gmx_mdoutf_t of, int mdof_flags, gmx_mtop_t *top_global, gmx_int64_t step, double t, t_state *state_local, t_state *state_global, rvec *f_local, rvec *f_global) { rvec *local_v; rvec *global_v; /* MRS -- defining these variables is to manage the difference * between half step and full step velocities, but there must be a better way . . . */ local_v = state_local->v; global_v = state_global->v; if (DOMAINDECOMP(cr)) { if (mdof_flags & MDOF_CPT) { dd_collect_state(cr->dd, state_local, state_global); } else { if (mdof_flags & (MDOF_X | MDOF_X_COMPRESSED)) { dd_collect_vec(cr->dd, state_local, state_local->x, state_global->x); } if (mdof_flags & MDOF_V) { dd_collect_vec(cr->dd, state_local, local_v, global_v); } } if (mdof_flags & MDOF_F) { dd_collect_vec(cr->dd, state_local, f_local, f_global); } } else { if (mdof_flags & MDOF_CPT) { /* All pointers in state_local are equal to state_global, * but we need to copy the non-pointer entries. */ state_global->lambda = state_local->lambda; state_global->veta = state_local->veta; state_global->vol0 = state_local->vol0; copy_mat(state_local->box, state_global->box); copy_mat(state_local->boxv, state_global->boxv); copy_mat(state_local->svir_prev, state_global->svir_prev); copy_mat(state_local->fvir_prev, state_global->fvir_prev); copy_mat(state_local->pres_prev, state_global->pres_prev); } } if (MASTER(cr)) { if (mdof_flags & MDOF_CPT) { fflush_tng(of->tng); fflush_tng(of->tng_low_prec); write_checkpoint(of->fn_cpt, of->bKeepAndNumCPT, fplog, cr, of->eIntegrator, of->simulation_part, of->bExpanded, of->elamstats, step, t, state_global); } if (mdof_flags & (MDOF_X | MDOF_V | MDOF_F)) { if (of->fp_trn) { gmx_trr_write_frame(of->fp_trn, step, t, state_local->lambda[efptFEP], state_local->box, top_global->natoms, (mdof_flags & MDOF_X) ? state_global->x : NULL, (mdof_flags & MDOF_V) ? global_v : NULL, (mdof_flags & MDOF_F) ? f_global : NULL); if (gmx_fio_flush(of->fp_trn) != 0) { gmx_file("Cannot write trajectory; maybe you are out of disk space?"); } } gmx_fwrite_tng(of->tng, FALSE, step, t, state_local->lambda[efptFEP], state_local->box, top_global->natoms, (mdof_flags & MDOF_X) ? state_global->x : NULL, (mdof_flags & MDOF_V) ? global_v : NULL, (mdof_flags & MDOF_F) ? f_global : NULL); } if (mdof_flags & MDOF_X_COMPRESSED) { rvec *xxtc = NULL; if (of->natoms_x_compressed == of->natoms_global) { /* We are writing the positions of all of the atoms to the compressed output */ xxtc = state_global->x; } else { /* We are writing the positions of only a subset of the atoms to the compressed output, so we have to make a copy of the subset of coordinates. */ int i, j; snew(xxtc, of->natoms_x_compressed); for (i = 0, j = 0; (i < of->natoms_global); i++) { if (ggrpnr(of->groups, egcCompressedX, i) == 0) { copy_rvec(state_global->x[i], xxtc[j++]); } } } if (write_xtc(of->fp_xtc, of->natoms_x_compressed, step, t, state_local->box, xxtc, of->x_compression_precision) == 0) { gmx_fatal(FARGS, "XTC error - maybe you are out of disk space?"); } gmx_fwrite_tng(of->tng_low_prec, TRUE, step, t, state_local->lambda[efptFEP], state_local->box, of->natoms_x_compressed, xxtc, NULL, NULL); if (of->natoms_x_compressed != of->natoms_global) { sfree(xxtc); } } } }
int write_trxframe_indexed(t_trxstatus *status, t_trxframe *fr, int nind, const atom_id *ind, gmx_conect gc) { char title[STRLEN]; rvec *xout = NULL, *vout = NULL, *fout = NULL; int i; real prec; if (fr->bPrec) { prec = fr->prec; } else { prec = 1000.0; } switch (gmx_fio_getftp(status->fio)) { case efTRJ: case efTRR: break; default: if (!fr->bX) { gmx_fatal(FARGS, "Need coordinates to write a %s trajectory", ftp2ext(gmx_fio_getftp(status->fio))); } break; } switch (gmx_fio_getftp(status->fio)) { case efTRJ: case efTRR: if (fr->bV) { snew(vout, nind); for (i = 0; i < nind; i++) { copy_rvec(fr->v[ind[i]], vout[i]); } } if (fr->bF) { snew(fout, nind); for (i = 0; i < nind; i++) { copy_rvec(fr->f[ind[i]], fout[i]); } } /* no break */ case efXTC: case efG87: if (fr->bX) { snew(xout, nind); for (i = 0; i < nind; i++) { copy_rvec(fr->x[ind[i]], xout[i]); } } break; default: break; } switch (gmx_fio_getftp(status->fio)) { case efXTC: write_xtc(status->fio, nind, fr->step, fr->time, fr->box, xout, prec); break; case efTRJ: case efTRR: fwrite_trn(status->fio, nframes_read(status), fr->time, fr->step, fr->box, nind, xout, vout, fout); break; case efGRO: case efPDB: case efBRK: case efENT: if (!fr->bAtoms) { gmx_fatal(FARGS, "Can not write a %s file without atom names", ftp2ext(gmx_fio_getftp(status->fio))); } sprintf(title, "frame t= %.3f", fr->time); if (gmx_fio_getftp(status->fio) == efGRO) { write_hconf_indexed_p(gmx_fio_getfp(status->fio), title, fr->atoms, nind, ind, prec2ndec(prec), fr->x, fr->bV ? fr->v : NULL, fr->box); } else { write_pdbfile_indexed(gmx_fio_getfp(status->fio), title, fr->atoms, fr->x, -1, fr->box, ' ', fr->step, nind, ind, gc, TRUE); } break; case efG87: write_gms(gmx_fio_getfp(status->fio), nind, xout, fr->box); break; case efG96: write_g96_conf(gmx_fio_getfp(status->fio), fr, nind, ind); break; default: gmx_fatal(FARGS, "Sorry, write_trxframe_indexed can not write %s", ftp2ext(gmx_fio_getftp(status->fio))); break; } switch (gmx_fio_getftp(status->fio)) { case efTRN: case efTRJ: case efTRR: if (vout) { sfree(vout); } if (fout) { sfree(fout); } /* no break */ case efXTC: case efG87: sfree(xout); break; default: break; } return 0; }
void ReadWrite(char *rfile, char *wfile, int in_xtcBool, int out_xtcBool, int in_trrBool, int out_trrBool) { XDRFILE *xd_read, *xd_write; int result_xtc, result_trr; int natoms_xtc, natoms_trr; int step_xtc, step_trr; float time_xtc, time_trr; matrix box_xtc, box_trr; rvec *x_xtc, *x_trr, *v_trr, *f_trr; float prec_xtc = 1000.0; float lambda_trr = 0.0; xd_read = xdrfile_open(rfile, "r"); if (NULL == xd_read) die("Opening xdrfile for reading"); /* Test whether output file exists */ if ((xd_write = xdrfile_open(wfile,"r")) != NULL) { xdrfile_close(xd_write); die("Output file exists."); } /* Output file does not exist. Now we can open it for writing */ xd_write = xdrfile_open(wfile, "w"); if (NULL == xd_write) die("Opening xdrfile for writing"); /* .xtc -> .xtc */ if(in_xtcBool && out_xtcBool) { result_xtc = read_xtc_natoms(rfile, &natoms_xtc); if (exdrOK != result_xtc) die_r("read_xtc_natoms",result_xtc); x_xtc = (rvec *)calloc(natoms_xtc, sizeof(x_xtc[0])); while(1) { result_xtc = read_xtc(xd_read, natoms_xtc, &step_xtc, &time_xtc, box_xtc, x_xtc, &prec_xtc); if (result_xtc == 0) // if not reach the end of file, write it to the output.xtc file { if (exdrOK != result_xtc) die_r("Reading xtc file", result_xtc); result_xtc = write_xtc(xd_write, natoms_xtc, step_xtc, time_xtc, box_xtc, x_xtc, prec_xtc); if (result_xtc != 0) die_r("Writing xtc file", result_xtc); } else break; } } /* .xtc -> .trr */ if(in_xtcBool && out_trrBool) { result_xtc = read_xtc_natoms(rfile, &natoms_xtc); if (exdrOK != result_xtc) die_r("read_xtc_natoms",result_xtc); x_xtc = (rvec *)calloc(natoms_xtc, sizeof(x_xtc[0])); while(1) { result_xtc = read_xtc(xd_read, natoms_xtc, &step_xtc, &time_xtc, box_xtc, x_xtc, &prec_xtc); if (result_xtc == 0) // if not reach the end of file, write it to the output.trr file { if (exdrOK != result_xtc) die_r("Reading xtc file", result_xtc); result_trr = write_trr(xd_write, natoms_xtc, step_xtc, time_xtc, lambda_trr, box_xtc, x_xtc, NULL, NULL); if (0 != result_trr) die_r("Writing trr file",result_trr); } else break; } } /* .trr -> .trr */ if(in_trrBool && out_trrBool) { result_trr = read_trr_natoms(rfile, &natoms_trr); if (exdrOK != result_trr) die_r("read_trr_natoms",result_trr); x_trr = (rvec *)calloc(natoms_trr, sizeof(x_trr[0])); v_trr = (rvec *)calloc(natoms_trr, sizeof(v_trr[0])); f_trr = (rvec *)calloc(natoms_trr, sizeof(f_trr[0])); while (1) { result_trr = read_trr(xd_read, natoms_trr, &step_trr, &time_trr, &lambda_trr, box_trr, x_trr, v_trr, f_trr); int ii_trr, jj_trr, x_ck=0, v_ck=0, f_ck=0; int x_ck_bool=0, v_ck_bool=0, f_ck_bool=0; for (ii_trr = 0; ii_trr < natoms_trr; ii_trr++) { for(jj_trr = 0; jj_trr < DIM; jj_trr++) { if (x_trr[ii_trr][jj_trr] == 0) x_ck++; if (v_trr[ii_trr][jj_trr] == 0) v_ck++; if (f_trr[ii_trr][jj_trr] == 0) f_ck++; } } if (x_ck == natoms_trr*DIM) x_ck_bool = 1; if (v_ck == natoms_trr*DIM) v_ck_bool = 1; if (f_ck == natoms_trr*DIM) f_ck_bool = 1; if (result_trr == 0) // if not reach the end of file, write it to the output.trr file { if (exdrOK != result_trr) die_r("Reading trr file",result_trr); if(v_ck_bool && f_ck_bool) result_trr = write_trr(xd_write, natoms_trr, step_trr, time_trr, lambda_trr, box_trr, x_trr, NULL, NULL); else if(v_ck_bool) result_trr = write_trr(xd_write, natoms_trr, step_trr, time_trr, lambda_trr, box_trr, x_trr, NULL, f_trr); else if(f_ck_bool) result_trr = write_trr(xd_write, natoms_trr, step_trr, time_trr, lambda_trr, box_trr, x_trr, v_trr, NULL); else result_trr = write_trr(xd_write, natoms_trr, step_trr, time_trr, lambda_trr, box_trr, x_trr, v_trr, f_trr); if (0 != result_trr) die_r("Writing trr file",result_trr); } else break; } } /* .trr -> .xtc */ if(in_trrBool && out_xtcBool) { result_trr = read_trr_natoms(rfile, &natoms_trr); if (exdrOK != result_trr) die_r("read_trr_natoms",result_trr); x_trr = (rvec *)calloc(natoms_trr, sizeof(x_trr[0])); v_trr = (rvec *)calloc(natoms_trr, sizeof(v_trr[0])); f_trr = (rvec *)calloc(natoms_trr, sizeof(f_trr[0])); while(1) { result_trr = read_trr(xd_read, natoms_trr, &step_trr, &time_trr, &lambda_trr, box_trr, x_trr, v_trr, f_trr); if (result_trr == 0) // if not reach the end of file, write it to the output.trr file { if (exdrOK != result_trr) die_r("Reading trr file", result_trr); result_xtc = write_xtc(xd_write, natoms_trr, step_trr, time_trr, box_trr, x_trr, prec_xtc); if (result_xtc != 0) die_r("Writing xtc file", result_xtc); } else break; } } xdrfile_close(xd_read); xdrfile_close(xd_write); }
static void test_xtc() { char *testfn = "test.xtc"; XDRFILE *xd; int result,i,j,k,nframes=13; int natoms2,natoms1=173; int step2,step1=1993; float time2,time1=1097.23; matrix box2,box1; rvec *x2,*x1; float prec2,prec1=1000; float toler=1e-3; printf("Testing xtc functionality:"); for(i=0; (i<DIM); i++) for(j=0; (j<DIM); j++) box1[i][j] = (i+1)*3.7 + (j+1); x1 = calloc(natoms1,sizeof(*x1)); if (NULL == x1) die("Allocating memory for x1 in test_xtc"); for(i=0; (i<natoms1); i++) for(j=0; (j<DIM); j++) x1[i][j] = (i+1)*3.7 + (j+1); xd = xdrfile_open(testfn,"w"); if (NULL == xd) die("Opening xdrfile for writing"); for(k=0; (k<nframes); k++) { result = write_xtc(xd,natoms1,step1+k,time1+k,box1,x1,prec1); if (0 != result) die_r("Writing xtc file",result); } xdrfile_close(xd); result = read_xtc_natoms(testfn,&natoms2); if (exdrOK != result) die_r("read_xtc_natoms",result); if (natoms2 != natoms1) die("Number of atoms incorrect when reading trr"); x2 = calloc(natoms2,sizeof(x2[0])); if (NULL == x2) die("Allocating memory for x2"); xd = xdrfile_open(testfn,"r"); if (NULL == xd) die("Opening xdrfile for reading"); k = 0; do { result = read_xtc(xd,natoms2,&step2,&time2,box2,x2,&prec2); if (exdrENDOFFILE != result) { if (exdrOK != result) die_r("read_xtc",result); if (natoms2 != natoms1) die("natoms2 != natoms1"); if (step2-step1 != k) die("incorrect step"); if (fabs(time2-time1-k) > toler) die("incorrect time"); if (fabs(prec2-prec1) > toler) die("incorrect precision"); for(i=0; (i<DIM); i++) for(j=0; (j<DIM); j++) if (fabs(box2[i][j] - box1[i][j]) > toler) die("box incorrect"); for(i=0; (i<natoms1); i++) for(j=0; (j<DIM); j++) if (fabs(x2[i][j] - x1[i][j]) > toler) die("x incorrect"); } k++; } while (result == exdrOK); xdrfile_close(xd); #ifdef HAVE_UNISTD unlink(testfn); #endif printf(" PASSED\n"); }