Example #1
0
 def __init__(self, filename, mode='r', atoms=None,
              backup=True, split=None, iolimit=10):
     if split is None:
         # Decide if subtype should be split based on number of atoms
         split = (atoms is not None) and atoms.get_number_of_atoms() > 1000000
     # Never use subtype split for serial simulations
     if not getattr(atoms, "parallel", False):
         split = False
     # Use the collector object to join all data on master if subtype is normal
     # and the simulation is parallel.
     if not split and atoms is not None and getattr(atoms, "parallel", False):
         atoms = asap3.Collector(atoms)
     if split:
         self.subtype = 'split'
     else:
         self.subtype = 'normal'
     # self.iolimit may be needed if reading a split bundle.
     if world.size < 1.5 * iolimit:
         self.iolimit = None
     else:
         self.iolimit = iolimit
     
     _BundleTrajectory.__init__(self, filename, mode, atoms,
                                backup=backup)
     if self.subtype == 'split':
         self.set_extra_data('ID')  # So the atoms can be sorted when read.
Example #2
0
 def close(self):
     """Clean up when closing."""
     if self.state == 'write' and self.master:
         i = self.nframes
         while True:
             fname = os.path.join(self.filename, "F" + str(i))
             if not os.path.exists(fname):
                 break
             self.log("Closing, removing empty directory "+fname)
             os.rmdir(fname)
             i += 1
     _BundleTrajectory.close(self)
Example #3
0
    def read_from_trajectory(cls, trajectory, frame=-1, atoms=None):
        """Read dynamics and atoms from trajectory (Class method).
        
        Simultaneously reads the atoms and the dynamics from a BundleTrajectory,
        including the internal data of the NPT dynamics object (automatically
        saved when attaching a BundleTrajectory to an NPT object).
        
        Arguments:
        
        trajectory 
            The filename or an open BundleTrajectory object.
        
        frame (optional)
            Which frame to read.  Default: the last.
            
        atoms (optional, internal use only)
            Pre-read atoms.  Do not use. 
        """
        if isinstance(trajectory, str):
            if trajectory.endswith("/"):
                trajectory = trajectory[:-1]
            if trajectory.endswith(".bundle"):
                from ase.io.bundletrajectory import BundleTrajectory

                trajectory = BundleTrajectory(trajectory)
            else:
                raise ValueError("Cannot open '%': unsupported file format" % trajectory)
        # trajectory is now a BundleTrajectory object (or compatible)
        if atoms is None:
            atoms = trajectory[frame]
        init_data = trajectory.read_extra_data("npt_init", 0)
        frame_data = trajectory.read_extra_data("npt_dynamics", frame)
        dyn = cls(
            atoms,
            timestep=init_data["dt"],
            temperature=init_data["temperature"],
            externalstress=init_data["externalstress"],
            ttime=init_data["ttime"],
            pfactor=init_data["pfactor_given"],
            mask=init_data["mask"],
        )
        dyn.desiredEkin = init_data["desiredEkin"]
        dyn.tfact = init_data["tfact"]
        dyn.pfact = init_data["pfact"]
        dyn.frac_traceless = init_data["frac_traceless"]
        for k, v in frame_data.items():
            setattr(dyn, k, v)
        return (dyn, atoms)
Example #4
0
File: npt.py Project: fuulish/fuase
 def read_from_trajectory(cls, trajectory, frame=-1, atoms=None):
     """Read dynamics and atoms from trajectory (Class method).
     
     Simultaneously reads the atoms and the dynamics from a BundleTrajectory,
     including the internal data of the NPT dynamics object (automatically
     saved when attaching a BundleTrajectory to an NPT object).
     
     Arguments:
     
     trajectory 
         The filename or an open BundleTrajectory object.
     
     frame (optional)
         Which frame to read.  Default: the last.
         
     atoms (optional, internal use only)
         Pre-read atoms.  Do not use. 
     """
     if isinstance(trajectory, str):
         if trajectory.endswith('/'):
             trajectory = trajectory[:-1]
         if trajectory.endswith('.bundle'):
             from ase.io.bundletrajectory import BundleTrajectory
             trajectory = BundleTrajectory(trajectory)
         else:
             raise ValueError("Cannot open '%': unsupported file format" %
                              trajectory)
     # trajectory is now a BundleTrajectory object (or compatible)
     if atoms is None:
         atoms = trajectory[frame]
     init_data = trajectory.read_extra_data('npt_init', 0)
     frame_data = trajectory.read_extra_data('npt_dynamics', frame)
     dyn = cls(atoms,
               timestep=init_data['dt'],
               temperature=init_data['temperature'],
               externalstress=init_data['externalstress'],
               ttime=init_data['ttime'],
               pfactor=init_data['pfactor_given'],
               mask=init_data['mask'])
     dyn.desiredEkin = init_data['desiredEkin']
     dyn.tfact = init_data['tfact']
     dyn.pfact = init_data['pfact']
     dyn.frac_traceless = init_data['frac_traceless']
     for k, v in frame_data.items():
         setattr(dyn, k, v)
     return (dyn, atoms)
def test_append_bundle(images, bundletraj):
    traj = BundleTrajectory(bundletraj, mode='a')
    assert len(read(bundletraj, ':')) == 2
    #write(bundletraj, images, append=True)
    for atoms in images:
        traj.write(atoms)
    traj.close()
    images1 = read(bundletraj, ':')
    assert len(images1) == 4
    # XXX Fix the magmoms/charges bug
    assert_images_equal(images * 2, images1)
Example #6
0
def filetype(filename):
    """Try to guess the type of the file."""
    if os.path.isdir(filename):
        # Potentially a BundleTrajectory
        if BundleTrajectory.is_bundle(filename):
            return 'bundle'
        elif os.path.normpath(filename) == 'states':
            return 'eon'
        else:
            raise IOError('Directory: ' + filename)

    if filename.startswith('pg://'):
        return 'postgresql'

    fileobj = open(filename, 'rU')
    s3 = fileobj.read(3)
    if len(s3) == 0:
        raise IOError('Empty file: ' + filename)

    if s3.startswith('{"'):
        return 'json'

    if filename.endswith('.db'):
        return 'db'

    if filename.lower().endswith('.cmr'):
        return 'cmr'

    if is_tarfile(filename):
        return 'gpw'

    if s3 == 'CDF':
        from ase.io.pupynere import NetCDFFile
        nc = NetCDFFile(filename)
        if 'number_of_dynamic_atoms' in nc.dimensions:
            return 'dacapo'

        history = nc.history
        if history == 'GPAW restart file':
            return 'gpw-nc'
        if history == 'ASE trajectory':
            return 'nc'
        if history == 'Dacapo':
            return 'dacapo'
        if hasattr(nc, 'file_format') and nc.file_format.startswith('ETSF'):
            return 'etsf'
        raise IOError('Unknown netCDF file!')

    if is_zipfile(filename):
        return 'vnl'

    fileobj.seek(0)
    lines = fileobj.readlines(1000)

    if lines[0].startswith('PickleTrajectory'):
        return 'traj'

    if (lines[1].startswith('OUTER LOOP:')
            or filename.lower().endswith('.cube')):
        return 'cube'

    if '  ___ ___ ___ _ _ _  \n' in lines:
        return 'gpaw-text'

    if (' &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n'
            in lines[:90]):
        return 'dacapo-text'

    for line in lines:
        if line[0] != '#':
            word = line.strip()
            if word in ['ANIMSTEPS', 'CRYSTAL', 'SLAB', 'POLYMER', 'MOLECULE']:
                return 'xsf'

    filename_v = os.path.basename(filename)
    if 'POSCAR' in filename_v or 'CONTCAR' in filename_v:
        return 'vasp'

    if 'OUTCAR' in filename_v:
        return 'vasp_out'

    if filename.lower().endswith('.exi'):
        return 'exi'

    if filename.lower().endswith('.mol'):
        return 'mol'

    if filename.lower().endswith('.pdb'):
        return 'pdb'

    if filename.lower().endswith('.cif'):
        return 'cif'

    if filename.lower().endswith('.struct'):
        return 'struct'

    if filename.lower().endswith('.struct_out'):
        return 'struct_out'

    fileobj.seek(0)
    while True:
        line = fileobj.readline()
        if not line:
            break
        if 'Invoking FHI-aims ...' in line:
            return 'aims_out'
        if 'atom' in line:
            data = line.split()
            try:
                Atoms(symbols=[data[4]],
                      positions=[[
                          float(data[1]),
                          float(data[2]),
                          float(data[3])
                      ]])
                return 'aims'
            except:
                pass

    if filename.lower().endswith('.in'):
        fileobj.seek(0)
        while True:
            line = fileobj.readline()
            if not line:
                break
            if ('&system' in line) or ('&SYSTEM' in line):
                return 'esp_in'
        return 'aims'

    if filename.lower().endswith('.cfg'):
        return 'cfg'

    if os.path.split(filename)[1] == 'atoms.dat':
        return 'iwm'

    if filename.endswith('I_info'):
        return 'Cmdft'

    if lines[0].startswith('$coord') or os.path.basename(filename) == 'coord':
        return 'tmol'

    if (lines[0].startswith('$grad')
            or os.path.basename(filename) == 'gradient'):
        return 'tmol-gradient'

    if lines[0].startswith('Geometry'):
        return 'dftb'

    if filename.lower().endswith('.geom'):
        return 'castep_geom'

    if filename.lower().endswith('.castep'):
        return 'castep'

    if filename.lower().endswith('.cell'):
        return 'castep_cell'
    if s3 == '<?x':
        from ase.io.vtkxml import probe_vtkxml
        xmltype = probe_vtkxml(filename)
        if xmltype == 'ImageData':
            return 'vti'
        elif xmltype == 'StructuredGrid':
            return 'vts'
        elif xmltype == 'UnstructuredGrid':
            return 'vtu'
        elif xmltype is not None:
            raise IOError('Unknown VTK XML file!')

    if filename.lower().endswith('.sdf'):
        return 'sdf'

    if filename.lower().endswith('.gen'):
        return 'gen'

    if filename.lower().endswith('.con'):
        return 'eon'

    if 'ITEM: TIMESTEP\n' in lines:
        return 'lammps'

    if filename.lower().endswith('.gro'):
        return 'gromacs'

    if filename.lower().endswith('.log'):
        return 'gaussian_out'

    if filename.lower().endswith('.com'):
        return 'gaussian'

    if filename.lower().endswith('.g96'):
        return 'gromos'

    if filename.lower().endswith('.out'):
        return 'esp_out'

    if filename.endswith('.nw'):
        return 'nw'

    return 'xyz'
Example #7
0
def filetype(filename):
    """Try to guess the type of the file."""
    if os.path.isdir(filename):
        # Potentially a BundleTrajectory
        if BundleTrajectory.is_bundle(filename):
            return 'bundle'
        elif os.path.normpath(filename) == 'states':
            return 'eon'
        else:
            raise IOError('Directory: ' + filename)

    if filename.startswith('pg://'):
        return 'postgresql'

    fileobj = open(filename, 'rb')
    s3 = fileobj.read(3)
    if len(s3) == 0:
        raise IOError('Empty file: ' + filename)

    if s3.startswith(b'{"'):
        return 'json'

    if filename.endswith('.db'):
        return 'db'

    if filename.lower().endswith('.cmr'):
        return 'cmr'

    if is_tarfile(filename):
        return 'gpw'

    if s3 == 'CDF':
        from ase.io.pupynere import NetCDFFile
        nc = NetCDFFile(filename)
        if 'number_of_dynamic_atoms' in nc.dimensions:
            return 'dacapo'

        history = nc.history
        if history == 'GPAW restart file':
            return 'gpw-nc'
        if history == 'ASE trajectory':
            return 'nc'
        if history == 'Dacapo':
            return 'dacapo'
        if hasattr(nc, 'file_format') and nc.file_format.startswith('ETSF'):
            return 'etsf'
        raise IOError('Unknown netCDF file!')

    if is_zipfile(filename):
        return 'vnl'

    fileobj.seek(0)
    lines = fileobj.readlines(1000)
    if lines[0].startswith(b'AFFormatASE-Trajectory'):
        return 'traj'
    if lines[0].startswith(b'PickleTrajectory'):
        return 'trj'

    if (lines[1].startswith(b'OUTER LOOP:') or
        filename.lower().endswith('.cube')):
        return 'cube'

    if b'  ___ ___ ___ _ _ _  \n' in lines:
        return 'gpaw-text'

    if (b' &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n'
        in lines[:90]):
        return 'dacapo-text'

    for line in lines:
        if line[0] != b'#':
            word = line.strip()
            if word in ['ANIMSTEPS', 'CRYSTAL', 'SLAB', 'POLYMER', 'MOLECULE']:
                return 'xsf'

    filename_v = os.path.basename(filename)
    if 'POSCAR' in filename_v or 'CONTCAR' in filename_v:
        return 'vasp'

    if 'OUTCAR' in filename_v:
        return 'vasp_out'

    if 'XDATCAR' in filename_v:
        return 'vasp_xdatcar'

    if filename.lower().endswith('.exi'):
        return 'exi'

    if filename.lower().endswith('.mol'):
        return 'mol'

    if filename.lower().endswith('.pdb'):
        return 'pdb'

    if filename.lower().endswith('.cif'):
        return 'cif'

    if filename.lower().endswith('.struct'):
        return 'struct'

    if filename.lower().endswith('.struct_out'):
        return 'struct_out'

    fileobj.seek(0)
    while True:
        line = fileobj.readline()
        if not line:
            break
        if b'Invoking FHI-aims ...' in line:
            return 'aims_out'
        if b'atom' in line:
            data = line.split()
            try:
                Atoms(symbols=[data[4]],
                      positions=[[float(data[1]),
                                  float(data[2]),
                                  float(data[3])]])
                return 'aims'
            except:
                pass

    if filename.lower().endswith('.in'):
        fileobj.seek(0)
        while True:
            line = fileobj.readline()
            if not line:
                break
            if ('&system' in line) or ('&SYSTEM' in line):
                return 'esp_in'
        return 'aims'

    if filename.lower().endswith('.cfg'):
        return 'cfg'

    if os.path.split(filename)[1] == 'atoms.dat':
        return 'iwm'

    if filename.endswith('I_info'):
        return 'Cmdft'

    if lines[0].startswith(b'$coord') or os.path.basename(filename) == 'coord':
        return 'tmol'

    if (lines[0].startswith(b'$grad') or
        os.path.basename(filename) == 'gradient'):
        return 'tmol-gradient'

    if lines[0].startswith(b'Geometry'):
        return 'dftb'

    if filename.lower().endswith('.geom'):
        return 'castep_geom'

    if filename.lower().endswith('.castep'):
        return 'castep'

    if filename.lower().endswith('.cell'):
        return 'castep_cell'
    if s3 == '<?x' and not filename.endswith('xsd'):
        from ase.io.vtkxml import probe_vtkxml
        xmltype = probe_vtkxml(filename)
        if xmltype == 'ImageData':
            return 'vti'
        elif xmltype == 'StructuredGrid':
            return 'vts'
        elif xmltype == 'UnstructuredGrid':
            return 'vtu'
        elif xmltype is not None:
            raise IOError('Unknown VTK XML file!')

    if filename.lower().endswith('.sdf'):
        return 'sdf'

    if filename.lower().endswith('.gen'):
        return 'gen'

    if filename.lower().endswith('.con'):
        return 'eon'

    if 'ITEM: TIMESTEP\n' in lines:
        return 'lammps'

    if filename.lower().endswith('.gro'):
        return 'gromacs'

    if filename.lower().endswith('.log'):
        return 'gaussian_out'

    if filename.lower().endswith('.com'):
        return 'gaussian'

    if filename.lower().endswith('.g96'):
        return 'gromos'

    if filename.lower().endswith('.out'):
        return 'esp_out'

    if filename.endswith('.nw'):
        return 'nw'

    if filename.endswith('xsd'):
        return 'xsd'

    return 'xyz'
Example #8
0
def filetype(filename):
    """Try to guess the type of the file."""
    if os.path.isdir(filename):
        # Potentially a BundleTrajectory
        if BundleTrajectory.is_bundle(filename):
            return 'bundle'
        else:
            raise IOError('Directory: ' + filename)

    fileobj = open(filename)
    s3 = fileobj.read(3)
    if len(s3) == 0:
        raise IOError('Empty file: ' + filename)

    if is_tarfile(filename):
        return 'gpw'

    if s3 == 'CDF':
        from ase.io.pupynere import NetCDFFile
        nc = NetCDFFile(filename)
        if 'number_of_dynamic_atoms' in nc.dimensions:
            return 'dacapo'

        history = nc.history
        if history == 'GPAW restart file':
            return 'gpw-nc'
        if history == 'ASE trajectory':
            return 'nc'
        if history == 'Dacapo':
            return 'dacapo'
        if hasattr(nc, 'file_format') and nc.file_format.startswith('ETSF'):
            return 'etsf'
        raise IOError('Unknown netCDF file!')

    if is_zipfile(filename):
        return 'vnl'

    fileobj.seek(0)
    lines = fileobj.readlines(1000)

    if lines[0].startswith('PickleTrajectory'):
        return 'traj'

    if lines[1].startswith('OUTER LOOP:') or filename.lower().endswith(
            '.cube'):
        return 'cube'

    if '  ___ ___ ___ _ _ _  \n' in lines:
        return 'gpaw-text'

    if (' &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n'
            in lines[:90]):
        return 'dacapo-text'

    for line in lines:
        if line[0] != '#':
            word = line.strip()
            if word in ['ANIMSTEPS', 'CRYSTAL', 'SLAB', 'POLYMER', 'MOLECULE']:
                return 'xsf'

    filename_v = basename(filename)
    if 'POSCAR' in filename_v or 'CONTCAR' in filename_v:
        return 'vasp'

    if 'OUTCAR' in filename_v:
        return 'vasp_out'

    if filename.lower().endswith('.exi'):
        return 'exi'

    if filename.lower().endswith('.mol'):
        return 'mol'

    if filename.lower().endswith('.pdb'):
        return 'pdb'

    if filename.lower().endswith('.cif'):
        return 'cif'

    if filename.lower().endswith('.struct'):
        return 'struct'

    if filename.lower().endswith('.in'):
        return 'aims'

    if filename.lower().endswith('.out'):
        return 'aims_out'

    if filename.lower().endswith('.cfg'):
        return 'cfg'

    if os.path.split(filename)[1] == 'atoms.dat':
        return 'iwm'

    if filename.endswith('I_info'):
        return 'Cmdft'

    if lines[0].startswith('$coord'):
        return 'tmol'

    if lines[0].startswith('Geometry'):
        return 'dftb'

    if s3 == '<?x':
        from ase.io.vtkxml import probe_vtkxml
        xmltype = probe_vtkxml(filename)
        if xmltype == 'ImageData':
            return 'vti'
        elif xmltype == 'StructuredGrid':
            return 'vts'
        elif xmltype == 'UnstructuredGrid':
            return 'vtu'
        elif xmltype is not None:
            raise IOError('Unknown VTK XML file!')

    if filename.lower().endswith('.sdf'):
        return 'sdf'

    return 'xyz'
Example #9
0
 def _set_defaults(self):
     subtype = self.subtype   # Preserve it
     _BundleTrajectory._set_defaults(self)
     self.subtype = subtype
     self.datatypes['forces'] = False
Example #10
0
 def write(self, atoms=None):
     if self.subtype == 'normal' and atoms is not None and getattr(atoms, "parallel", False):
         atoms = asap3.Collector(atoms)
     _BundleTrajectory.write(self, atoms)