Пример #1
0
 def next(self):
     while True:
         # skip to the next frame
         self._secfile.get_next("Frame Number")
         if slice_match(self._sub, self._counter):
             self._counter += 1
             frame = ATRJFrame()
             # Read the time and energy
             energy_lines = self._secfile.get_next("Time/Energy")
             energy_words = energy_lines[0].split()
             frame.time = float(energy_words[0])*ps
             frame.step = int(energy_words[1])
             frame.total_energy = float(energy_words[2])*kcalmol
             # Read the coordinates
             coord_lines = self._secfile.get_next("Coordinates")
             frame.coordinates = numpy.zeros((self.num_atoms, 3), float)
             for index, line in enumerate(coord_lines):
                 words = line.split()
                 frame.coordinates[index,0] = float(words[1])
                 frame.coordinates[index,1] = float(words[2])
                 frame.coordinates[index,2] = float(words[3])
             frame.coordinates *= angstrom
             # Done
             return frame
         else:
             self._counter += 1
Пример #2
0
    def next(self):
        # skip frames as requested
        while not slice_match(self._sub, self._counter):
            self._counter += 1
            self.skip_frame()

        self._counter += 1
        return self.read_frame()
Пример #3
0
    def next(self):
        def goto_next_frame():
            marked = False
            while True:
                line = self._f.next()[:-1]
                if marked and len(line) > 0 and not line.startswith(" --------"):
                    try:
                        step = int(line[:10])
                        return step, line
                    except ValueError:
                        pass
                marked = (len(line) == 131 and line == self._marker)

        while True:
            step, line = goto_next_frame()
            if (not self.skip_equi_period or step >= self.equi_period) and \
               step != self.last_step:
                break

        # skip frames as requested
        while not slice_match(self._sub, self._counter):
            step, line = goto_next_frame()
            self._counter += 1

        # now really read these three lines
        try:
            row = [step]
            for i in xrange(9):
                row.append(float(line[10+i*12:10+(i+1)*12]))
            line = self._f.next()[:-1]
            row.append(float(line[:10]))
            for i in xrange(9):
                row.append(float(line[10+i*12:10+(i+1)*12]))
            line = self._f.next()[:-1]
            row.append(float(line[:10]))
            for i in xrange(9):
                row.append(float(line[10+i*12:10+(i+1)*12]))
        except ValueError:
            raise Error("Some numbers in the output file could not be read. (expecting floating point numbers)")

        # convert all the numbers to atomic units
        for i in xrange(30):
            row[i] *= self._conv[i]

        # done
        self.last_step = step
        return row
Пример #4
0
    def _read(self):
        def read_size():
            try:
                return int(self._file.readline().strip())
            except ValueError:
                raise StopIteration

        while not slice_match(self._sub, self._counter):
            size = read_size()
            for i in xrange(size+1):
                line = self._file.readline()
                if len(line) == 0:
                    raise StopIteration
            self._counter += 1

        size = read_size()
        title = self._file.readline()[:-1]
        symbols = []
        coordinates = numpy.zeros((size, 3), float)
        for counter in xrange(size):
            line = self._file.readline()
            if len(line) == 0:
                raise StopIteration
            words = line.split()
            if len(words) < 4:
                raise StopIteration
            symbols.append(words[0])
            try:
                coordinates[counter,0] = float(words[1])
                coordinates[counter,1] = float(words[2])
                coordinates[counter,2] = float(words[3])
            except ValueError:
                raise StopIteration
        coordinates *= self.file_unit
        self._counter += 1
        return symbols, title, coordinates
Пример #5
0
    def next(self):
        # auxiliary read function
        def read_three(msg):
            # read three words as floating point numbers
            line = self._f.next()
            try:
                return [float(line[:12]), float(line[12:24]), float(line[24:])]
            except ValueError:
                raise Error(msg)

        # skip frames as requested
        while not slice_match(self._sub, self._counter):
            for i in xrange(self._frame_size):
                self._f.next()
            self._counter += 1

        frame = {}
        # read the frame header line
        words = self._f.next().split()
        if len(words) != 6:
            raise Error("The first line of each time frame must contain 6 words. (%i'th frame)" % self._counter)
        if words[0] != "timestep":
            raise Error("The first word of the first line of each time frame must be 'timestep'. (%i'th frame)" % self._counter)
        try:
            step = int(words[1])
            frame["step"] = step
            if int(words[2]) != self.num_atoms:
                raise Error("The number of atoms has changed. (%i'th frame, %i'th step)" % (self._counter, step))
            if int(words[3]) != self.keytrj:
                raise Error("keytrj has changed. (%i'th frame, %i'th step)" % (self._counter, step))
            if int(words[4]) != self.imcon:
                raise Error("imcon has changed. (%i'th frame, %i'th step)" % (self._counter, step))
            frame["timestep"] = float(words[5])*self.time_unit
            frame["time"] = frame["timestep"]*step # this is ugly, or wait ... dlpoly is a bit ugly. we are not to blame!
        except ValueError:
            raise Error("Could not convert all numbers on the first line of the current time frame. (%i'th frame)" % self._counter)
        # the three cell lines
        cell = numpy.zeros((3,3), float)
        frame["cell"] = cell
        cell_msg = "The cell lines must consist of three floating point values. (%i'th frame, %i'th step)" % (self._counter, step)
        for i in xrange(3):
            cell[:,i] = read_three(cell_msg)
        cell *= self.pos_unit
        # the atoms
        symbols = []
        frame["symbols"] = symbols
        masses = numpy.zeros(self.num_atoms, float)
        frame["masses"] = masses
        charges = numpy.zeros(self.num_atoms, float)
        frame["charges"] = charges
        pos = numpy.zeros((self.num_atoms,3), float)
        frame["pos"] = pos
        if self.keytrj > 0:
            vel = numpy.zeros((self.num_atoms,3), float)
            frame["vel"] = vel
        if self.keytrj > 1:
            frc = numpy.zeros((self.num_atoms,3), float)
            frame["frc"] = frc
        for i in xrange(self.num_atoms):
            # the atom header line
            words = self._f.next().split()
            if len(words) != 4:
                raise Error("The atom header line must contain 4 words. (%i'th frame, %i'th step, %i'th atom)" % (self._counter, step, i+1))
            symbols.append(words[0])
            try:
                masses[i] = float(words[2])*self.mass_unit
                charges[i] = float(words[3])
            except ValueError:
                raise Error("The numbers in the atom header line could not be interpreted.")
            # the pos line
            pos_msg = "The position lines must consist of three floating point values. (%i'th frame, %i'th step, %i'th atom)" % (self._counter, step, i+1)
            pos[i] = read_three(pos_msg)
            if self.keytrj > 0:
                vel_msg = "The velocity lines must consist of three floating point values. (%i'th frame, %i'th step, %i'th atom)" % (self._counter, step, i+1)
                vel[i] = read_three(vel_msg)
            if self.keytrj > 1:
                frc_msg = "The force lines must consist of three floating point values. (%i'th frame, %i'th step, %i'th atom)" % (self._counter, step, i+1)
                frc[i] = read_three(frc_msg)
        pos *= self.pos_unit # convert to au
        if self.keytrj > 0:
            vel *= self.vel_unit # convert to au
        if self.keytrj > 1:
            frc *= self.frc_unit # convert to au
        # done
        self._counter += 1
        return frame