Skip to content
Snippets Groups Projects
Commit 5f53e79c authored by Ansgar Philippsen's avatar Ansgar Philippsen
Browse files

fixed BZDNG-373 (The DCD importer segfaults when trying to import the mpor_trj_short.trj)

parent b8df0ac6
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,6 @@ class Anim(QtCore.QTimer): ...@@ -16,7 +16,6 @@ class Anim(QtCore.QTimer):
go.UpdatePositions() go.UpdatePositions()
# old-style CHARMM format, requires flag bit 0 set
eh = io.LoadCRD("mpor_trj.crd") eh = io.LoadCRD("mpor_trj.crd")
cg = io.LoadCHARMMTraj(eh,"mpor_trj_short.trj") cg = io.LoadCHARMMTraj(eh,"mpor_trj_short.trj")
go=gfx.Entity("test",eh.CreateFullView()) go=gfx.Entity("test",eh.CreateFullView())
......
...@@ -56,7 +56,7 @@ struct DCDHeader { ...@@ -56,7 +56,7 @@ struct DCDHeader {
char hdrr[4]; char hdrr[4];
int icntrl[20]; int icntrl[20];
int ntitle; int ntitle;
char title[1024]; std::string title;
int num, istep, freq,nstep; int num, istep, freq,nstep;
int t_atom_count,f_atom_count, atom_count; 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, ...@@ -78,18 +78,25 @@ bool read_dcd_header(std::istream& istream, DCDHeader& header, bool& swap_flag,
ucell_flag=false; ucell_flag=false;
if(gap_flag) istream.read(dummy,sizeof(dummy)); if(gap_flag) istream.read(dummy,sizeof(dummy));
istream.read(header.hdrr,sizeof(char)*4); 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); 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 // nonsense atom count, try swapping
swap_int(header.icntrl,20); swap_int(header.icntrl,20);
if(header.icntrl[1]<0 || header.icntrl[1]>1e8) { if(header.icntrl[1]<0 || header.icntrl[1]>1e6) {
throw(IOException("LoadCHARMMTraj: nonsense atom count in header")); std::ostringstream msg;
msg << "LoadCHARMMTraj: nonsense atom count (" << header.icntrl[1] << ") in header";
throw IOException(msg.str());
} else { } else {
LOG_VERBOSE("LoadCHARMMTraj: byte-swapping"); LOG_VERBOSE("LoadCHARMMTraj: byte-swapping");
swap_flag=true; swap_flag=true;
} }
} }
LOG_VERBOSE("LoadCHARMMTraj: found " << header.icntrl[1] << " atoms");
if(header.icntrl[19]!=0) { // CHARMM format if(header.icntrl[19]!=0) { // CHARMM format
ucell_flag=(header.icntrl[10]!=0); ucell_flag=(header.icntrl[10]!=0);
if(ucell_flag) { if(ucell_flag) {
...@@ -112,8 +119,12 @@ bool read_dcd_header(std::istream& istream, DCDHeader& header, bool& swap_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(swap_flag) swap_int(&header.ntitle,1);
if(gap_flag) istream.read(dummy,sizeof(dummy)); if(gap_flag) istream.read(dummy,sizeof(dummy));
istream.read(header.title,sizeof(char)*header.ntitle); std::vector<char> title(header.ntitle+1);
header.title[header.ntitle]='\0';
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)); if(gap_flag) istream.read(dummy,sizeof(dummy));
istream.read(reinterpret_cast<char*>(&header.t_atom_count),sizeof(int)); istream.read(reinterpret_cast<char*>(&header.t_atom_count),sizeof(int));
if(swap_flag) swap_int(&header.t_atom_count,1); if(swap_flag) swap_int(&header.t_atom_count,1);
...@@ -359,11 +370,11 @@ mol::CoordGroupHandle LoadCHARMMTraj(const mol::EntityHandle& ent, ...@@ -359,11 +370,11 @@ mol::CoordGroupHandle LoadCHARMMTraj(const mol::EntityHandle& ent,
mol::AtomHandleList alist(ent.GetAtomList()); mol::AtomHandleList alist(ent.GetAtomList());
std::sort(alist.begin(),alist.end(),less_index); std::sort(alist.begin(),alist.end(),less_index);
if (lazy_load) { if (lazy_load) {
LOG_INFO("Importing CHARMM trajectory with lazy_load=true"); LOG_VERBOSE("LoadCHARMMTraj: importing with lazy_load=true");
DCDCoordSource* source=new DCDCoordSource(alist, trj_fn, stride); DCDCoordSourcePtr source(new DCDCoordSource(alist, trj_fn, stride));
return mol::CoordGroupHandle(DCDCoordSourcePtr(source)); 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); return load_dcd(alist, trj_fn, stride);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment