def __init__(self, filename, plugin=None, x_factor=0.1, t_factor=1.0): if plugin is None: suffix = filename.rsplit('.', 1)[-1] plugin = self.suggest_plugin(suffix) if plugin is None: raise RuntimeError( 'molfile_reader: no suitable plugin known for file %s' % filename) self.x_factor = x_factor self.t_factor = t_factor self.v_factor = x_factor / t_factor self._N = c_int() suffix = filename.rsplit('.', 1)[-1] self._mfp = MolfilePlugin(plugin) p = self._mfp.plugin self._fh = p.open_file_read(filename, suffix, byref(self._N)) if not self._fh: raise RuntimeError( 'molfile_reader: failed to open file %s with plugin %s.' % (filename, plugin)) N = self._N.value if p.read_structure: # for e.g. lammpsplugin, read_structure needs to be called first so # that molfile_reader knows (internally) which coordinates and # velocites to map to which atoms. self._atoms_arr = (molfile_atom_t * N)() self._optflags = c_int() rc = p.read_structure(self._fh, byref(self._optflags), self._atoms_arr) if rc: raise IOError('molfile_reader: read structure failed for ' 'file %s (plugin %s, rc %i)' % (filename, plugin, rc)) else: self._atoms_arr = None self._v = None self._x = np.require(zeros((3, N)), molfile_float_np, ['F_CONTIGUOUS', 'ALIGNED']) self._x.flags.writeable = False if p.read_timestep_metadata: # It seems only lammpsplugin offers this (but other formats could # include velocity information. how to test for that!?) tsm = self._timestep_metadata = molfile_timestep_metadata_t() rc = p.read_timestep_metadata(self._fh, byref(tsm)) if rc: raise IOError( 'molfile_reader: read timestep metadata failed for ' 'file %s (plugin %s, rc %i)' % (filename, plugin, rc)) if tsm.has_velocities: self._v = np.require(zeros((3, N)), molfile_float_np, ['F_CONTIGUOUS', 'ALIGNED']) self._v.flags.writeable = False else: self._timestep_metadata = None # Now, set up the timestep structure self._ts = molfile_timestep_t() self._ts.coords = self._x.ctypes.data_as(POINTER(molfile_float_ct)) if self._v is not None: self._ts.velocities = self._v.ctypes.data_as( POINTER(molfile_float_ct)) else: # Set velocities to a NULL pointer self._ts.velocities = POINTER(molfile_float_ct)() # Set frame counter self._index = count(1)
def __init__(self, filename, plugin=None, x_factor=0.1, t_factor=1.0): if plugin is None: suffix = filename.rsplit('.', 1)[-1] plugin = self.suggest_plugin(suffix) if plugin is None: raise RuntimeError('molfile_reader: no suitable plugin known for file %s' % filename) self.x_factor = x_factor self.t_factor = t_factor self.v_factor = x_factor/t_factor self._N = c_int() suffix = filename.rsplit('.',1)[-1] self._mfp = MolfilePlugin(plugin) p = self._mfp.plugin self._fh = p.open_file_read(filename, suffix, byref(self._N)) if not self._fh: raise RuntimeError('molfile_reader: failed to open file %s with plugin %s.' % ( filename, plugin)) N = self._N.value if p.read_structure: # for e.g. lammpsplugin, read_structure needs to be called first so # that molfile_reader knows (internally) which coordinates and # velocites to map to which atoms. self._atoms_arr = (molfile_atom_t*N)() self._optflags = c_int() rc = p.read_structure(self._fh, byref(self._optflags), self._atoms_arr) if rc: raise IOError('molfile_reader: read structure failed for ' 'file %s (plugin %s, rc %i)' % (filename, plugin, rc)) else: self._atoms_arr = None self._v = None self._x = np.require(zeros((3,N)), molfile_float_np, ['F_CONTIGUOUS', 'ALIGNED']) self._x.flags.writeable = False if p.read_timestep_metadata: # It seems only lammpsplugin offers this (but other formats could # include velocity information. how to test for that!?) tsm = self._timestep_metadata = molfile_timestep_metadata_t() rc = p.read_timestep_metadata(self._fh, byref(tsm)) if rc: raise IOError('molfile_reader: read timestep metadata failed for ' 'file %s (plugin %s, rc %i)' % (filename, plugin, rc)) if tsm.has_velocities: self._v = np.require(zeros((3,N)), molfile_float_np, ['F_CONTIGUOUS', 'ALIGNED']) self._v.flags.writeable = False else: self._timestep_metadata = None # Now, set up the timestep structure self._ts = molfile_timestep_t() self._ts.coords = self._x.ctypes.data_as(POINTER(molfile_float_ct)) if self._v is not None: self._ts.velocities = self._v.ctypes.data_as(POINTER(molfile_float_ct)) else: # Set velocities to a NULL pointer self._ts.velocities = POINTER(molfile_float_ct)() # Set frame counter self._index = count(1)
class molfile_reader(abstract_trajectory_reader): """Read a trajectory using the molfile_plugin package molfile_plugin is a part of VMD, and consists of plugins for a fairly large number of different trajectory formats (see molfile_plugin.TRAJECTORY_PLUGIN_MAPPING). filename - string, filename of trajectory file. index_file - string, filename of ini-style index file. plugin - string, name of plugin to use. If None, guess pluginname by looking at filename suffix. """ @classmethod def reader_available(cls): return MOLFILE_PLUGIN_DIR is not None @classmethod def suggest_plugin(cls, filename_suffix): for _, _, sfx, plugin in TRAJECTORY_PLUGIN_MAPPING: if sfx == filename_suffix: return plugin return None def __init__(self, filename, plugin=None, x_factor=0.1, t_factor=1.0): if plugin is None: suffix = filename.rsplit('.', 1)[-1] plugin = self.suggest_plugin(suffix) if plugin is None: raise RuntimeError( 'molfile_reader: no suitable plugin known for file %s' % filename) self.x_factor = x_factor self.t_factor = t_factor self.v_factor = x_factor / t_factor self._N = c_int() suffix = filename.rsplit('.', 1)[-1] self._mfp = MolfilePlugin(plugin) p = self._mfp.plugin self._fh = p.open_file_read(filename, suffix, byref(self._N)) if not self._fh: raise RuntimeError( 'molfile_reader: failed to open file %s with plugin %s.' % (filename, plugin)) N = self._N.value if p.read_structure: # for e.g. lammpsplugin, read_structure needs to be called first so # that molfile_reader knows (internally) which coordinates and # velocites to map to which atoms. self._atoms_arr = (molfile_atom_t * N)() self._optflags = c_int() rc = p.read_structure(self._fh, byref(self._optflags), self._atoms_arr) if rc: raise IOError('molfile_reader: read structure failed for ' 'file %s (plugin %s, rc %i)' % (filename, plugin, rc)) else: self._atoms_arr = None self._v = None self._x = np.require(zeros((3, N)), molfile_float_np, ['F_CONTIGUOUS', 'ALIGNED']) self._x.flags.writeable = False if p.read_timestep_metadata: # It seems only lammpsplugin offers this (but other formats could # include velocity information. how to test for that!?) tsm = self._timestep_metadata = molfile_timestep_metadata_t() rc = p.read_timestep_metadata(self._fh, byref(tsm)) if rc: raise IOError( 'molfile_reader: read timestep metadata failed for ' 'file %s (plugin %s, rc %i)' % (filename, plugin, rc)) if tsm.has_velocities: self._v = np.require(zeros((3, N)), molfile_float_np, ['F_CONTIGUOUS', 'ALIGNED']) self._v.flags.writeable = False else: self._timestep_metadata = None # Now, set up the timestep structure self._ts = molfile_timestep_t() self._ts.coords = self._x.ctypes.data_as(POINTER(molfile_float_ct)) if self._v is not None: self._ts.velocities = self._v.ctypes.data_as( POINTER(molfile_float_ct)) else: # Set velocities to a NULL pointer self._ts.velocities = POINTER(molfile_float_ct)() # Set frame counter self._index = count(1) def __iter__(self): return self def next(self): if not self._mfp.plugin.read_next_timestep: raise StopIteration ts = self._ts rc = self._mfp.plugin.read_next_timestep(self._fh, self._N, byref(ts)) if rc: self._mfp.close() raise StopIteration res = dict(index=self._index.next(), box=to_box(ts.A, ts.B, ts.C, ts.alpha, ts.beta, ts.gamma) * self.x_factor, N=self._N.value, time=ts.physical_time * self.t_factor, x=self._x * self.x_factor) if self._v is not None: res['v'] = self._v * self.v_factor return res def close(self): self._mfp.close()
class molfile_reader(abstract_trajectory_reader): """Read a trajectory using the molfile_plugin package molfile_plugin is a part of VMD, and consists of plugins for a fairly large number of different trajectory formats (see molfile_plugin.TRAJECTORY_PLUGIN_MAPPING). filename - string, filename of trajectory file. index_file - string, filename of ini-style index file. plugin - string, name of plugin to use. If None, guess pluginname by looking at filename suffix. """ @classmethod def reader_available(cls): return MOLFILE_PLUGIN_DIR is not None @classmethod def suggest_plugin(cls, filename_suffix): for _,_,sfx,plugin in TRAJECTORY_PLUGIN_MAPPING: if sfx == filename_suffix: return plugin return None def __init__(self, filename, plugin=None, x_factor=0.1, t_factor=1.0): if plugin is None: suffix = filename.rsplit('.', 1)[-1] plugin = self.suggest_plugin(suffix) if plugin is None: raise RuntimeError('molfile_reader: no suitable plugin known for file %s' % filename) self.x_factor = x_factor self.t_factor = t_factor self.v_factor = x_factor/t_factor self._N = c_int() suffix = filename.rsplit('.',1)[-1] self._mfp = MolfilePlugin(plugin) p = self._mfp.plugin self._fh = p.open_file_read(filename, suffix, byref(self._N)) if not self._fh: raise RuntimeError('molfile_reader: failed to open file %s with plugin %s.' % ( filename, plugin)) N = self._N.value if p.read_structure: # for e.g. lammpsplugin, read_structure needs to be called first so # that molfile_reader knows (internally) which coordinates and # velocites to map to which atoms. self._atoms_arr = (molfile_atom_t*N)() self._optflags = c_int() rc = p.read_structure(self._fh, byref(self._optflags), self._atoms_arr) if rc: raise IOError('molfile_reader: read structure failed for ' 'file %s (plugin %s, rc %i)' % (filename, plugin, rc)) else: self._atoms_arr = None self._v = None self._x = np.require(zeros((3,N)), molfile_float_np, ['F_CONTIGUOUS', 'ALIGNED']) self._x.flags.writeable = False if p.read_timestep_metadata: # It seems only lammpsplugin offers this (but other formats could # include velocity information. how to test for that!?) tsm = self._timestep_metadata = molfile_timestep_metadata_t() rc = p.read_timestep_metadata(self._fh, byref(tsm)) if rc: raise IOError('molfile_reader: read timestep metadata failed for ' 'file %s (plugin %s, rc %i)' % (filename, plugin, rc)) if tsm.has_velocities: self._v = np.require(zeros((3,N)), molfile_float_np, ['F_CONTIGUOUS', 'ALIGNED']) self._v.flags.writeable = False else: self._timestep_metadata = None # Now, set up the timestep structure self._ts = molfile_timestep_t() self._ts.coords = self._x.ctypes.data_as(POINTER(molfile_float_ct)) if self._v is not None: self._ts.velocities = self._v.ctypes.data_as(POINTER(molfile_float_ct)) else: # Set velocities to a NULL pointer self._ts.velocities = POINTER(molfile_float_ct)() # Set frame counter self._index = count(1) def __iter__(self): return self def next(self): if not self._mfp.plugin.read_next_timestep: raise StopIteration ts = self._ts rc = self._mfp.plugin.read_next_timestep(self._fh, self._N, byref(ts)) if rc: self._mfp.close() raise StopIteration res = dict( index = self._index.next(), box = to_box(ts.A, ts.B, ts.C, ts.alpha, ts.beta, ts.gamma)*self.x_factor, N = self._N.value, time = ts.physical_time*self.t_factor, x = self._x*self.x_factor ) if self._v is not None: res['v'] = self._v*self.v_factor return res def close(self): self._mfp.close()