Example #1
0
    def __init__(self, f, units, sub=slice(None)):
        """
           Arguments:
            | ``f``  --  a filename or a file-like object
            | ``units``  --  The units of the atom fields. The number of fields,
                             their unit and their meaning depends on the input
                             file of the LAMMPS simulation.

           Optional argumtent:
            | ``sub``  --  a slice object indicating which time frames to skip/read
        """
        SlicedReader.__init__(self, f, sub)
        # first read the number of atoms
        try:
            while True:
                line = next(self._f)
                if line == "ITEM: NUMBER OF ATOMS\n":
                    break
            try:
                line = next(self._f)
                self.num_atoms = int(line)
            except ValueError:
                raise FileFormatError("Could not read the number of atoms. Expected an integer. Got '%s'" % line)
        except StopIteration:
            raise FileFormatError("Could not find line 'ITEM: NUMBER OF ATOMS'.")
        self._f.seek(0) # go back to the beginning of the file
        self.units = units
Example #2
0
    def __init__(self, f, sub=slice(None), pos_unit=angstrom,
        vel_unit=angstrom/picosecond, frc_unit=amu*angstrom/picosecond**2,
        time_unit=picosecond, mass_unit=amu
    ):
        """
           Arguments:
             | ``f``  --  a filename or a file-like object

           Optional arguments:
             | ``sub``  --  a slice indicating the frames to be skipped/selected
             | ``pos_unit``, ``vel_unit``, ``frc_unit``, ``time_unit``,
               ``mass_unit``  --  The conversion factors for the unit conversion
                from the units in the data file to atomic units. The defaults of
                these optional arguments correspond to the defaults of dlpoly.

        """
        SlicedReader.__init__(self, f, sub)
        self._counter = 1 # make our counter compatible with dlpoly
        self.pos_unit = pos_unit
        self.vel_unit = vel_unit
        self.frc_unit = frc_unit
        self.time_unit = time_unit
        self.mass_unit = mass_unit
        try:
            self.header = self._f.next()[:-1]
            integers = tuple(int(word) for word in self._f.next().split())
            if len(integers) != 3:
                raise FileFormatError("Second line must contain three integers.")
            self.keytrj, self.imcon, self.num_atoms = integers
        except StopIteration:
            raise FileFormatError("File is too short. Could not read header.")
        except ValueError:
            raise FileFormatError("Second line must contain three integers.")
        self._frame_size = 4 + self.num_atoms*(self.keytrj+2)
Example #3
0
    def __init__(self, f, units, sub=slice(None)):
        """
           Arguments:
            | ``f``  --  a filename or a file-like object
            | ``units``  --  The units of the atom fields. The number of fields,
                             their unit and their meaning depends on the input
                             file of the LAMMPS simulation.

           Optional argumtent:
            | ``sub``  --  a slice object indicating which time frames to skip/read
        """
        SlicedReader.__init__(self, f, sub)
        # first read the number of atoms
        try:
            while True:
                line = self._f.next()
                if line == "ITEM: NUMBER OF ATOMS\n":
                    break
            try:
                line = self._f.next()
                self.num_atoms = int(line)
            except ValueError:
                raise FileFormatError("Could not read the number of atoms. Expected an integer. Got '%s'" % line)
        except StopIteration:
            raise FileFormatError("Could not find line 'ITEM: NUMBER OF ATOMS'.")
        self._f.seek(0) # go back to the beginning of the file
        self.units = units
Example #4
0
    def __init__(
        self,
        f,
        sub=slice(None),
        pos_unit=angstrom,
        vel_unit=angstrom / picosecond,
        frc_unit=amu * angstrom / picosecond**2,
        time_unit=picosecond,
        mass_unit=amu,
        restart=False,
    ):
        """
           Arguments:
             | ``f``  --  a filename or a file-like object

           Optional arguments:
             | ``sub``  --  a slice indicating the frames to be skipped/selected
             | ``pos_unit``, ``vel_unit``, ``frc_unit``, ``time_unit``,
               ``mass_unit``  --  The conversion factors for the unit conversion
                from the units in the data file to atomic units. The defaults of
                these optional arguments correspond to the defaults of dlpoly.

           When the file starts with a line that satisfies the following
           conditions, it is assumed that this is a history restart file:

           * line consists of 6 words
           * first word equals 'timestep'
           * the following for words are integers
           * the last word is a float
        """
        SlicedReader.__init__(self, f, sub)
        self._counter = 1  # make our counter compatible with dlpoly
        self.pos_unit = pos_unit
        self.vel_unit = vel_unit
        self.frc_unit = frc_unit
        self.time_unit = time_unit
        self.mass_unit = mass_unit
        restart = self._detect_restart()
        if restart is None:
            try:
                self.header = next(self._f)[:-1]
                integers = tuple(int(word) for word in next(self._f).split())
                if len(integers) != 3:
                    raise FileFormatError(
                        "Second line must contain three integers.")
                self.keytrj, self.imcon, self.num_atoms = integers
            except StopIteration:
                raise FileFormatError(
                    "File is too short. Could not read header.")
            except ValueError:
                raise FileFormatError(
                    "Second line must contain three integers.")
        else:
            self.header = ''
            self.num_atoms, self.keytrj, self.imcon = restart
        self._frame_size = 4 + self.num_atoms * (self.keytrj + 2)
Example #5
0
    def __init__(self, f, sub=slice(None)):
        """
           Argument:
            | ``f``  --  a filename or a file-like object

           Optional argument:
            | ``sub``  --  a slice object to indicate the frames to be read/skipped.
        """
        SlicedReader.__init__(self, f, sub)
        self.num_atoms = None
        pos = self._read_frame()[1]
        self.num_atoms = len(pos)
        self._f.seek(0)
Example #6
0
    def __init__(self, f, sub=slice(None)):
        """
           Argument:
            | ``f``  --  a filename or a file-like object

           Optional argument:
            | ``sub``  --  a slice object to indicate the frames to be read/skipped.
        """
        SlicedReader.__init__(self, f, sub)
        self.num_atoms = None
        pos = self._read_frame()[1]
        self.num_atoms = len(pos)
        self._f.seek(0)
Example #7
0
    def __init__(self, f, sub=slice(None)):
        """
           Arguments:
            | ``f``  --  a filename or a file-like object
            | ``sub``  --  an optional slice object to select a subset of time
                           frames

           The number of atoms is read immediately and is available as
           ``self.num_atoms``.
        """
        SlicedReader.__init__(self, f, sub)
        self._secfile = SectionFile(self._f)
        self._read_header()
Example #8
0
    def __init__(self, f, sub=slice(None)):
        """
           Arguments:
            | ``f``  --  a filename or a file-like object
            | ``sub``  --  an optional slice object to select a subset of time
                           frames

           The number of atoms is read immediately and is available as
           ``self.num_atoms``.
        """
        SlicedReader.__init__(self, f, sub)
        self._secfile = SectionFile(self._f)
        self._read_header()
Example #9
0
    def __init__(self, f, sub=slice(None), pos_unit=angstrom,
        vel_unit=angstrom/picosecond, frc_unit=amu*angstrom/picosecond**2,
        time_unit=picosecond, mass_unit=amu, restart=False,
    ):
        """
           Arguments:
             | ``f``  --  a filename or a file-like object

           Optional arguments:
             | ``sub``  --  a slice indicating the frames to be skipped/selected
             | ``pos_unit``, ``vel_unit``, ``frc_unit``, ``time_unit``,
               ``mass_unit``  --  The conversion factors for the unit conversion
                from the units in the data file to atomic units. The defaults of
                these optional arguments correspond to the defaults of dlpoly.

           When the file starts with a line that satisfies the following
           conditions, it is assumed that this is a history restart file:

           * line consists of 6 words
           * first word equals 'timestep'
           * the following for words are integers
           * the last word is a float
        """
        SlicedReader.__init__(self, f, sub)
        self._counter = 1 # make our counter compatible with dlpoly
        self.pos_unit = pos_unit
        self.vel_unit = vel_unit
        self.frc_unit = frc_unit
        self.time_unit = time_unit
        self.mass_unit = mass_unit
        restart = self._detect_restart()
        if restart is None:
            try:
                self.header = self._f.next()[:-1]
                integers = tuple(int(word) for word in self._f.next().split())
                if len(integers) != 3:
                    raise FileFormatError("Second line must contain three integers.")
                self.keytrj, self.imcon, self.num_atoms = integers
            except StopIteration:
                raise FileFormatError("File is too short. Could not read header.")
            except ValueError:
                raise FileFormatError("Second line must contain three integers.")
        else:
            self.header = ''
            self.num_atoms, self.keytrj, self.imcon = restart
        self._frame_size = 4 + self.num_atoms*(self.keytrj+2)
Example #10
0
    def __init__(self, f, sub=slice(None)):
        """
           Arguments:
            | ``f``  --  a filename or a file-like object

           Optional argument:
            | ``sub``  --  a slice object indicating which time frames to skip/read
        """
        SlicedReader.__init__(self, f, sub)
        # first determine the number of atoms
        s = None
        self.num_atoms = 0
        for line in self._f:
            if s is None:
                s = line[:7]
            elif s != line[:7]:
                break
            self.num_atoms += 1
        self._f.seek(0)  # go back to the beginning of the file
Example #11
0
    def __init__(self, f, sub=slice(None)):
        """
           Arguments:
            | ``f``  --  a filename or a file-like object

           Optional argument:
            | ``sub``  --  a slice object indicating which time frames to skip/read
        """
        SlicedReader.__init__(self, f, sub)
        # first determine the number of atoms
        s = None
        self.num_atoms = 0
        for line in self._f:
            if s is None:
                s = line[:7]
            elif s != line[:7]:
                break
            self.num_atoms += 1
        self._f.seek(0) # go back to the beginning of the file
Example #12
0
    def __init__(self, f, sub=slice(None), file_unit=angstrom):
        """Initialize an XYZ reader

           Arguments:
            | ``f``  --  a filename or a file-like object

           Optional arguments:
            | ``sub``  --  a slice indicating which frames to read/skip
            | ``file_unit``  --  the conversion constant to convert data into atomic
                                  units [default=angstrom]

           After initialization, the following attributes are defined:
            | ``symbols``  --  The atom symbols
            | ``numbers``  --  The atom numbers
        """
        SlicedReader.__init__(self, f, sub)
        self.file_unit = file_unit

        try:
            self.symbols = None
            self._first = self._read_frame()
            self.numbers = numpy.zeros(len(self.symbols), int)
            for index, symbol in enumerate(self.symbols):
                try:
                    number = int(symbol)
                    symbol = periodic[number].symbol
                    self.symbols[index] = symbol
                except ValueError:
                    atom_info = periodic[symbol]
                    if atom_info is not None:
                        number = atom_info.number
                    else:
                        number = 0
                self.numbers[index] = number
            self.symbols = tuple(self.symbols)
            self._f.seek(0)
        except StopIteration:
            raise FileFormatError(
                "Could not read first frame from XYZ file. Incorrect file format."
            )
Example #13
0
    def __init__(self, f, sub=slice(None), skip_equi_period=True,
        pos_unit=angstrom, time_unit=picosecond, angle_unit=deg,
        e_unit=amu/(angstrom/picosecond)**2
    ):
        """
           Arguments:
            | ``f``  --  a filename or a file-like object

           Optional arguments:
            | ``sub``  --  a slice indicating the frames to be skipped/selected
            | ``skip_equi_period``  -- When True, the equilibration period is
                                       not read [default=True]
            | ``pos_unit``, ``time_unit``, ``angle_unit``, ``e_unit`` --  The
               conversion factors for the unit conversion from the units in the
               data file to atomic units. The defaults of these optional
               arguments correspond to the defaults of dlpoly.

        """
        SlicedReader.__init__(self, f, sub)
        self._counter = 1 # make our counter compatible with dlpoly
        self.skip_equi_period = skip_equi_period

        self._conv = [
            1,         e_unit,      1, e_unit, e_unit, e_unit,     e_unit,     e_unit,     e_unit, e_unit,
            time_unit, e_unit,      1, e_unit, e_unit, e_unit,     e_unit,     e_unit,     e_unit, e_unit,
            1,         pos_unit**3, 1, e_unit, e_unit, angle_unit, angle_unit, angle_unit, e_unit, 1000*atm,
        ]

        # find the line that gives the number of equilibration steps:
        try:
            while True:
                line = self._f.next()
                if line.startswith(" equilibration period"):
                    self.equi_period = int(line[30:])
                    break
        except StopIteration:
            raise FileFormatError("DL_POLY OUTPUT file is too short. Could not find line with the number of equilibration steps.")
        except ValueError:
            raise FileFormatError("Could not read the number of equilibration steps. (expecting an integer)")
Example #14
0
    def __init__(self, f, sub=slice(None), skip_equi_period=True,
        pos_unit=angstrom, time_unit=picosecond, angle_unit=deg,
        e_unit=amu/(angstrom/picosecond)**2
    ):
        """
           Arguments:
            | ``f``  --  a filename or a file-like object

           Optional arguments:
            | ``sub``  --  a slice indicating the frames to be skipped/selected
            | ``skip_equi_period``  -- When True, the equilibration period is
                                       not read [default=True]
            | ``pos_unit``, ``time_unit``, ``angle_unit``, ``e_unit`` --  The
               conversion factors for the unit conversion from the units in the
               data file to atomic units. The defaults of these optional
               arguments correspond to the defaults of dlpoly.

        """
        SlicedReader.__init__(self, f, sub)
        self._counter = 1 # make our counter compatible with dlpoly
        self.skip_equi_period = skip_equi_period

        self._conv = [
            1,         e_unit,      1, e_unit, e_unit, e_unit,     e_unit,     e_unit,     e_unit, e_unit,
            time_unit, e_unit,      1, e_unit, e_unit, e_unit,     e_unit,     e_unit,     e_unit, e_unit,
            1,         pos_unit**3, 1, e_unit, e_unit, angle_unit, angle_unit, angle_unit, e_unit, 1000*atm,
        ]

        # find the line that gives the number of equilibration steps:
        try:
            while True:
                line = next(self._f)
                if line.startswith(" equilibration period"):
                    self.equi_period = int(line[30:])
                    break
        except StopIteration:
            raise FileFormatError("DL_POLY OUTPUT file is too short. Could not find line with the number of equilibration steps.")
        except ValueError:
            raise FileFormatError("Could not read the number of equilibration steps. (expecting an integer)")
Example #15
0
    def __init__(self, f, sub=slice(None), file_unit=angstrom):
        """Initialize an XYZ reader

           Arguments:
            | ``f``  --  a filename or a file-like object

           Optional arguments:
            | ``sub``  --  a slice indicating which frames to read/skip
            | ``file_unit``  --  the conversion constant to convert data into atomic
                                  units [default=angstrom]

           After initialization, the following attributes are defined:
            | ``symbols``  --  The atom symbols
            | ``numbers``  --  The atom numbers
        """
        SlicedReader.__init__(self, f, sub)
        self.file_unit = file_unit

        try:
            self.symbols = None
            self._first = self._read_frame()
            self.numbers = numpy.zeros(len(self.symbols), int)
            for index, symbol in enumerate(self.symbols):
                try:
                    number = int(symbol)
                    symbol = periodic[number].symbol
                    self.symbols[index] = symbol
                except ValueError:
                    atom_info = periodic[symbol]
                    if atom_info is not None:
                        number = atom_info.number
                    else:
                        number = 0
                self.numbers[index] = number
            self.symbols = tuple(self.symbols)
            self._f.seek(0)
        except StopIteration:
            raise FileFormatError("Could not read first frame from XYZ file. Incorrect file format.")