static gmx_bool pdb_next_x(t_trxstatus *status, FILE *fp, t_trxframe *fr) { t_atoms atoms; t_symtab *symtab; matrix boxpdb; // Initiate model_nr to -1 rather than NOTSET. // It is not worthwhile introducing extra variables in the // read_pdbfile call to verify that a model_nr was read. int ePBC, model_nr = -1, na; char title[STRLEN], *time, *step; double dbl; atoms.nr = fr->natoms; atoms.atom = nullptr; atoms.pdbinfo = nullptr; /* the other pointers in atoms should not be accessed if these are NULL */ snew(symtab, 1); open_symtab(symtab); na = read_pdbfile(fp, title, &model_nr, &atoms, symtab, fr->x, &ePBC, boxpdb, TRUE, nullptr); free_symtab(symtab); sfree(symtab); set_trxframe_ePBC(fr, ePBC); if (nframes_read(status) == 0) { fprintf(stderr, " '%s', %d atoms\n", title, fr->natoms); } fr->bPrec = TRUE; fr->prec = 10000; fr->bX = TRUE; fr->bBox = (boxpdb[XX][XX] != 0.0); if (fr->bBox) { copy_mat(boxpdb, fr->box); } fr->step = 0; step = std::strstr(title, " step= "); fr->bStep = ((step != nullptr) && sscanf(step+7, "%" SCNd64, &fr->step) == 1); dbl = 0.0; time = std::strstr(title, " t= "); fr->bTime = ((time != nullptr) && sscanf(time+4, "%lf", &dbl) == 1); fr->time = dbl; if (na == 0) { return FALSE; } else { if (na != fr->natoms) { gmx_fatal(FARGS, "Number of atoms in pdb frame %d is %d instead of %d", nframes_read(status), na, fr->natoms); } return TRUE; } }
static gmx_bool pdb_next_x(t_trxstatus *status, FILE *fp,t_trxframe *fr) { t_atoms atoms; matrix boxpdb; int ePBC,model_nr,na; char title[STRLEN],*time; double dbl; atoms.nr = fr->natoms; atoms.atom=NULL; atoms.pdbinfo=NULL; /* the other pointers in atoms should not be accessed if these are NULL */ model_nr=NOTSET; na=read_pdbfile(fp,title,&model_nr,&atoms,fr->x,&ePBC,boxpdb,TRUE,NULL); set_trxframe_ePBC(fr,ePBC); if (nframes_read(status)==0) fprintf(stderr," '%s', %d atoms\n",title, fr->natoms); fr->bPrec = TRUE; fr->prec = 10000; fr->bX = TRUE; fr->bBox = (boxpdb[XX][XX] != 0.0); if (fr->bBox) { copy_mat(boxpdb,fr->box); } if (model_nr!=NOTSET) { fr->bStep = TRUE; fr->step = model_nr; } time=strstr(title," t= "); if (time) { fr->bTime = TRUE; sscanf(time+4,"%lf",&dbl); fr->time=(real)dbl; } else { fr->bTime = FALSE; /* this is a bit dirty, but it will work: if no time is read from comment line in pdb file, set time to current frame number */ if (fr->bStep) fr->time=(real)fr->step; else fr->time=(real)nframes_read(status); } if (na == 0) { return FALSE; } else { if (na != fr->natoms) gmx_fatal(FARGS,"Number of atoms in pdb frame %d is %d instead of %d", nframes_read(status),na,fr->natoms); return TRUE; } }
/*! * \param[in,out] d Trajectory analysis data structure. * \returns 0 on success, a non-zero error code on error. */ static int init_first_frame(gmx_ana_traj_t *d) { int i; /* Return if we have already initialized the trajectory */ if (d->fr) { return 0; } d->frflags |= TRX_NEED_X; snew(d->fr, 1); if (!read_first_frame(&d->status, d->trjfile, d->fr, d->frflags)) { gmx_input("could not read coordinates from trajectory"); return EIO; } if (d->top && d->fr->natoms > d->top->atoms.nr) { gmx_fatal(FARGS, "Trajectory (%d atoms) does not match topology (%d atoms)", d->fr->natoms, d->top->atoms.nr); return -1; } /* check index groups */ for (i = 0; i < d->ngrps; ++i) { gmx_ana_index_check(d->sel[i]->g, d->fr->natoms); } set_trxframe_ePBC(d->fr, d->ePBC); return 0; }
void TrajectoryAnalysisRunnerCommon::initFirstFrame() { // Return if we have already initialized the trajectory. if (impl_->fr) { return; } time_unit_t time_unit = static_cast<time_unit_t>(impl_->settings_.timeUnit() + 1); output_env_init(&impl_->oenv_, 0, NULL, time_unit, FALSE, exvgNONE, 0, 0); int frflags = impl_->settings_.frflags(); frflags |= TRX_NEED_X; snew(impl_->fr, 1); const TopologyInformation &top = impl_->topInfo_; if (hasTrajectory()) { if (!read_first_frame(impl_->oenv_, &impl_->status_, impl_->trjfile_.c_str(), impl_->fr, frflags)) { GMX_THROW(FileIOError("Could not read coordinates from trajectory")); } impl_->bTrajOpen_ = true; if (top.hasTopology() && impl_->fr->natoms > top.topology()->atoms.nr) { GMX_THROW(InconsistentInputError(formatString( "Trajectory (%d atoms) does not match topology (%d atoms)", impl_->fr->natoms, top.topology()->atoms.nr))); } // Check index groups if they have been initialized based on the topology. /* if (top) { for (int i = 0; i < impl_->sel->nr(); ++i) { gmx_ana_index_check(impl_->sel->sel(i)->indexGroup(), impl_->fr->natoms); } } */ } else { // Prepare a frame from topology information. // TODO: Initialize more of the fields. if (frflags & (TRX_NEED_V)) { GMX_THROW(NotImplementedError("Velocity reading from a topology not implemented")); } if (frflags & (TRX_NEED_F)) { GMX_THROW(InvalidInputError("Forces cannot be read from a topology")); } impl_->fr->flags = frflags; impl_->fr->natoms = top.topology()->atoms.nr; impl_->fr->bX = TRUE; snew(impl_->fr->x, impl_->fr->natoms); memcpy(impl_->fr->x, top.xtop_, sizeof(*impl_->fr->x) * impl_->fr->natoms); impl_->fr->bBox = TRUE; copy_mat(const_cast<rvec *>(top.boxtop_), impl_->fr->box); } set_trxframe_ePBC(impl_->fr, top.ePBC()); if (top.hasTopology() && impl_->settings_.hasRmPBC()) { impl_->gpbc_ = gmx_rmpbc_init(&top.topology()->idef, top.ePBC(), impl_->fr->natoms, impl_->fr->box); } }
static gmx_bool pdb_next_x(t_trxstatus *status, FILE *fp, t_trxframe *fr) { t_atoms atoms; t_symtab *symtab; matrix boxpdb; // Initiate model_nr to -1 rather than NOTSET. // It is not worthwhile introducing extra variables in the // read_pdbfile call to verify that a model_nr was read. int ePBC, model_nr = -1, na; char title[STRLEN], *time; double dbl; atoms.nr = fr->natoms; atoms.atom = NULL; atoms.pdbinfo = NULL; /* the other pointers in atoms should not be accessed if these are NULL */ snew(symtab, 1); open_symtab(symtab); na = read_pdbfile(fp, title, &model_nr, &atoms, symtab, fr->x, &ePBC, boxpdb, TRUE, NULL); free_symtab(symtab); sfree(symtab); set_trxframe_ePBC(fr, ePBC); if (nframes_read(status) == 0) { fprintf(stderr, " '%s', %d atoms\n", title, fr->natoms); } fr->bPrec = TRUE; fr->prec = 10000; fr->bX = TRUE; fr->bBox = (boxpdb[XX][XX] != 0.0); if (fr->bBox) { copy_mat(boxpdb, fr->box); } if (model_nr != -1) { fr->bStep = TRUE; fr->step = model_nr; } time = std::strstr(title, " t= "); if (time) { fr->bTime = TRUE; sscanf(time+4, "%lf", &dbl); fr->time = (real)dbl; } else { fr->bTime = FALSE; /* this is a bit dirty, but it will work: if no time is read from comment line in pdb file, set time to current frame number */ if (fr->bStep) { fr->time = (real)fr->step; } else { fr->time = (real)nframes_read(status); } } if (na == 0) { return FALSE; } else { if (na != fr->natoms) { gmx_fatal(FARGS, "Number of atoms in pdb frame %d is %d instead of %d", nframes_read(status), na, fr->natoms); } return TRUE; } }
int gmx_insert_dummy_atom(int argc, char *argv[]) { const char *desc[] = { "\tAdd a dummy atom between atoms -a1 and -a2", }; gmx_bool bVerbose = FALSE; int a1=-1,a2=-1; int insert_at=-1; const char *tpr_file, *traj_file, *out_file; t_pargs pa[] = { { "-a1", TRUE, etINT, {&a1}, "Starting atom for bond vector--ie: CD in CNC"}, { "-a2", TRUE, etINT, {&a2}, "Ending atom for bond vector--ie: NE in CNC"}, { "-insert_at", TRUE, etINT, {&insert_at}, "Desired index for dummy atom. Default = 0"}, { "-v", FALSE, etBOOL, {&bVerbose}, "Be slightly more verbose"} }; t_filenm fnm[] = { {efTPS, NULL, NULL, ffREAD}, {efTRX, NULL, NULL, ffREAD}, {efTRO, "-o","tilt",ffWRITE}, }; #define NFILE asize(fnm) #define NPA asize(pa) output_env_t oenv; int ngrps, nrefgrps; t_topology top; t_atoms *atoms=NULL; t_trxframe fr,frout; t_trxstatus *status; rvec *xtop; matrix box; int ePBC; int flags=TRX_READ_X; char buffer[1024]; int ftp; FILE *out_gro = NULL; t_trxstatus *trxout = NULL; CopyRight(stderr,argv[0]); parse_common_args(&argc, argv, PCA_CAN_BEGIN | PCA_CAN_END | PCA_CAN_VIEW | PCA_TIME_UNIT | PCA_BE_NICE | PCA_CAN_TIME, NFILE, fnm, NPA, pa, asize(desc), desc, 0, NULL, &oenv); /* Get inputs */ tpr_file = ftp2fn(efTPS, NFILE, fnm); traj_file = opt2fn( "-f", NFILE, fnm); out_file = opt2fn("-o", NFILE, fnm); ftp = fn2ftp(out_file); std::cout << "\n\n" << out_file << " " << ftp << std::endl; /* Open inputs */ read_tps_conf(tpr_file, buffer, &top, &ePBC, &xtop, NULL, box, TRUE); sfree(xtop); atoms = &top.atoms; gmx_conect gc = NULL; gc = gmx_conect_generate(&top); /* Make sure -a1 and -a2 are included and increment by -1 to match internal numbering */ if ( a1<1 || a2<1 || a1==a2 || a1>top.atoms.nr || a2>top.atoms.nr ) { gmx_fatal(FARGS, "\nAtom numbers -a1 and -a2 defining the bond vector must be specified and different\n"); } a1--; a2--; /* Read first frame */ gmx_bool bHaveFirstFrame = read_first_frame(oenv, &status, traj_file, &fr, flags); if (bHaveFirstFrame) { set_trxframe_ePBC(&fr,ePBC); } if (ftp == efGRO) { out_gro = ffopen(out_file,"w"); } else if (ftp == efXTC) { trxout = open_trx(out_file,"w"); } /* read file and loop through frames */ int frameN = 0; do { if (ftp == efGRO) { fprintf(out_gro,"Dummy atom inserted into %s, FRAME %i\n",traj_file,frameN); fprintf(out_gro,"%i\n",top.atoms.nr+1); float CD[3] = { fr.x[a1][0], fr.x[a1][1], fr.x[a1][2] }; float NE[3] = { fr.x[a2][0], fr.x[a2][1], fr.x[a2][2] }; float MP[3] ; for (int i=0;i<3;i++) { MP[i] = (CD[i]+NE[i])*0.5; } // GRO format: // RESID, RESNAME, ATOM, INDEX, X, Y, Z, vX, vY, vZ //"%5d%-5s%5s%5d%8.3f%8.3f%8.3f%8.4f%8.4f%8.4f" int index = 1; if (insert_at <= 0) { fprintf(out_gro,"%5d%-5s%5s%5d%8.4f%8.4f%8.4f%8.4f%8.4f%8.4f\n", 0,"TCHG","TCHG",0,MP[0],MP[1],MP[2],0.0f,0.0f,0.0f); index++; } /* Loop over atoms */ int i; int resid_offset = 0; for (i=0;i<top.atoms.nr;i++){ if (insert_at == index) { fprintf(out_gro,"%5d%-5s%5s%5d%8.4f%8.4f%8.4f%8.4f%8.4f%8.4f\n", top.atoms.atom[i-1].resind+2,"TCHG","TCHG",index,MP[0],MP[1],MP[2],0.0f,0.0f,0.0f); index++; resid_offset++; } // Ignoring velocities since I'm using this with mdrun -rerun for // force calculations only, which don't care about velocities fprintf(out_gro,"%5d%-5s%5s%5d%8.4f%8.4f%8.4f%8.4f%8.4f%8.4f\n", top.atoms.atom[i].resind+1 + resid_offset, *top.atoms.resinfo[top.atoms.atom[i].resind].name, *top.atoms.atomname[i], index, fr.x[i][0], fr.x[i][1], fr.x[i][2], 0.0f,0.0f,0.0f); index++; if (index > 99999) { index = 0; } } /* Get box information */ write_hconf_box(out_gro,1,box); } else if (ftp == efXTC) { float CD[3] = { fr.x[a1][0], fr.x[a1][1], fr.x[a1][2] }; float NE[3] = { fr.x[a2][0], fr.x[a2][1], fr.x[a2][2] }; rvec MP ; for (int i=0;i<3;i++) { MP[i] = (CD[i]+NE[i])*0.5; } rvec * newX = new rvec [top.atoms.nr+1]; int i = 0; int offset = 0; if (insert_at <= 0) { for (int j=0;j<3;j++) { newX[i][j] = MP[j]; } offset++; } for (i=0; i<top.atoms.nr; i++) { if (insert_at == i) { for (int j=0;j<3;j++) { newX[i][j] = MP[j]; } offset++; } for (int j=0;j<3;j++) { newX[i+offset][j] = fr.x[i][j]; } } frout = fr; frout.x = newX; frout.natoms++; write_trxframe(trxout,&frout,gc); delete[] newX; } frameN++; } while(read_next_frame(oenv, status, &fr)); if (trxout) { close_trx(trxout); } if (out_gro) { ffclose(out_gro); } return 0; }
void TrajectoryAnalysisRunnerCommon::Impl::initFirstFrame() { // Return if we have already initialized the trajectory. if (fr != NULL) { return; } time_unit_t time_unit = static_cast<time_unit_t>(settings_.timeUnit() + 1); output_env_init(&oenv_, getProgramContext(), time_unit, FALSE, exvgNONE, 0); int frflags = settings_.frflags(); frflags |= TRX_NEED_X; snew(fr, 1); if (hasTrajectory()) { if (!read_first_frame(oenv_, &status_, trjfile_.c_str(), fr, frflags)) { GMX_THROW(FileIOError("Could not read coordinates from trajectory")); } bTrajOpen_ = true; if (topInfo_.hasTopology()) { const int topologyAtomCount = topInfo_.topology()->atoms.nr; if (fr->natoms > topologyAtomCount) { const std::string message = formatString("Trajectory (%d atoms) does not match topology (%d atoms)", fr->natoms, topologyAtomCount); GMX_THROW(InconsistentInputError(message)); } } } else { // Prepare a frame from topology information. // TODO: Initialize more of the fields. if (frflags & (TRX_NEED_V)) { GMX_THROW(NotImplementedError("Velocity reading from a topology not implemented")); } if (frflags & (TRX_NEED_F)) { GMX_THROW(InvalidInputError("Forces cannot be read from a topology")); } fr->natoms = topInfo_.topology()->atoms.nr; fr->bX = TRUE; snew(fr->x, fr->natoms); memcpy(fr->x, topInfo_.xtop_, sizeof(*fr->x) * fr->natoms); fr->bBox = TRUE; copy_mat(topInfo_.boxtop_, fr->box); } set_trxframe_ePBC(fr, topInfo_.ePBC()); if (topInfo_.hasTopology() && settings_.hasRmPBC()) { gpbc_ = gmx_rmpbc_init(&topInfo_.topology()->idef, topInfo_.ePBC(), fr->natoms); } }