diff --git a/examples/code_fragments/trj/test_trj.py b/examples/code_fragments/trj/test_trj.py index 43bad373bd765b73885ef056cae3302463a6aebd..a1625996c109ed4272a2d8439219b9e7d583db61 100644 --- a/examples/code_fragments/trj/test_trj.py +++ b/examples/code_fragments/trj/test_trj.py @@ -16,7 +16,6 @@ class Anim(QtCore.QTimer): go.UpdatePositions() -# old-style CHARMM format, requires flag bit 0 set eh = io.LoadCRD("mpor_trj.crd") cg = io.LoadCHARMMTraj(eh,"mpor_trj_short.trj") go=gfx.Entity("test",eh.CreateFullView()) diff --git a/modules/io/src/mol/dcd_io.cc b/modules/io/src/mol/dcd_io.cc index 5acc5bf4b79dcb0eca650ebe306f6d944cc78b68..7ec81f5ed190a9f68aed306199e56cc5cd4d9cf5 100644 --- a/modules/io/src/mol/dcd_io.cc +++ b/modules/io/src/mol/dcd_io.cc @@ -56,7 +56,7 @@ struct DCDHeader { char hdrr[4]; int icntrl[20]; int ntitle; - char title[1024]; + std::string title; int num, istep, freq,nstep; int t_atom_count,f_atom_count, atom_count; }; @@ -78,18 +78,25 @@ bool read_dcd_header(std::istream& istream, DCDHeader& header, bool& swap_flag, ucell_flag=false; if(gap_flag) istream.read(dummy,sizeof(dummy)); istream.read(header.hdrr,sizeof(char)*4); + if(header.hdrr[0]!='C' || header.hdrr[1]!='O' || header.hdrr[2]!='R' || header.hdrr[3]!='D') { + throw IOException("LoadCHARMMTraj: missing CORD magic in header"); + } istream.read(reinterpret_cast<char*>(header.icntrl),sizeof(int)*20); - if(header.icntrl[1]<0 || header.icntrl[1]>1e8) { + if(header.icntrl[1]<0 || header.icntrl[1]>1e6) { // nonsense atom count, try swapping swap_int(header.icntrl,20); - if(header.icntrl[1]<0 || header.icntrl[1]>1e8) { - throw(IOException("LoadCHARMMTraj: nonsense atom count in header")); + if(header.icntrl[1]<0 || header.icntrl[1]>1e6) { + std::ostringstream msg; + msg << "LoadCHARMMTraj: nonsense atom count (" << header.icntrl[1] << ") in header"; + throw IOException(msg.str()); } else { LOG_VERBOSE("LoadCHARMMTraj: byte-swapping"); swap_flag=true; } } + LOG_VERBOSE("LoadCHARMMTraj: found " << header.icntrl[1] << " atoms"); + if(header.icntrl[19]!=0) { // CHARMM format ucell_flag=(header.icntrl[10]!=0); if(ucell_flag) { @@ -112,8 +119,12 @@ bool read_dcd_header(std::istream& istream, DCDHeader& header, bool& swap_flag, if(swap_flag) swap_int(&header.ntitle,1); if(gap_flag) istream.read(dummy,sizeof(dummy)); - istream.read(header.title,sizeof(char)*header.ntitle); - header.title[header.ntitle]='\0'; + std::vector<char> title(header.ntitle+1); + + istream.read(&title[0],sizeof(char)*header.ntitle); + header.title=std::string(&title[0],header.ntitle); + LOG_VERBOSE("LoadCHARMMTraj: title string [" << header.title << "]") + if(gap_flag) istream.read(dummy,sizeof(dummy)); istream.read(reinterpret_cast<char*>(&header.t_atom_count),sizeof(int)); if(swap_flag) swap_int(&header.t_atom_count,1); @@ -359,11 +370,11 @@ mol::CoordGroupHandle LoadCHARMMTraj(const mol::EntityHandle& ent, mol::AtomHandleList alist(ent.GetAtomList()); std::sort(alist.begin(),alist.end(),less_index); if (lazy_load) { - LOG_INFO("Importing CHARMM trajectory with lazy_load=true"); - DCDCoordSource* source=new DCDCoordSource(alist, trj_fn, stride); - return mol::CoordGroupHandle(DCDCoordSourcePtr(source)); + LOG_VERBOSE("LoadCHARMMTraj: importing with lazy_load=true"); + DCDCoordSourcePtr source(new DCDCoordSource(alist, trj_fn, stride)); + return mol::CoordGroupHandle(source); } - LOG_INFO("Importing CHARMM trajectory with lazy_load=false"); + LOG_VERBOSE("LoadCHARMMTraj: importing with lazy_load=false"); return load_dcd(alist, trj_fn, stride); }