def test_interacting_system(self):
     p = PairPotential('lennard_jones', {
         'epsilon': 1.0,
         'sigma': 1.0
     }, [1, 1], CutOff('CS', 2.5))
     i = Interaction(p, 'atomic')
     s = System()
     s.interaction = i
Exemple #2
0
    def read_sample(self, frame):
        # Read number of particles
        idx, _ = self._index_db['NUMBER OF ATOMS'][frame]
        self._fh.seek(idx)
        self._fh.readline()
        data = self._fh.readline()
        npart = int(data)

        # Build the system
        system = System()
        system.particle = []
        for i in range(npart):
            if self.first_particle > 0 and i < self.first_particle:
                continue
            if self.last_particle > 0 and i >= self.last_particle:
                break
            system.particle.append(Particle())

        # Add cell
        idx, data = self._index_db['BOX BOUNDS'][frame]
        self._fh.seek(idx)
        self._fh.readline()
        ndim = len(data.split())  # line is ITEM: BOX BONDS pp pp pp
        L, center = [], []
        for i in range(ndim):
            data = [float(x) for x in self._fh.readline().split()]
            L.append(data[1] - data[0])
            center.append((data[1] + data[0]) / 2)
        system.cell = Cell(numpy.array(L), center=numpy.array(center))

        # Read atoms data
        idx, data = self._index_db['ATOMS'][frame]
        fields = data.split()  # fields on a line
        _ = self._fh.readline()

        # Add interaction if forces are present
        # In atooms, forces belong to the interaction, not to particles
        if 'fx' in fields or 'fy' in fields or 'fz' in fields:
            # TODO: this won't work with first and last particles
            system.interaction = Interaction([])  # empty list of potentials
            system.interaction.forces = numpy.ndarray((npart, ndim))
        else:
            interaction = None

        for i in range(npart):
            # Limit reading the ATOMS section if requested
            if self.first_particle > 0 and i < self.first_particle:
                continue
            if self.last_particle > 0 and i >= self.last_particle:
                break
            data = self._fh.readline().split()
            # Accept unsorted particles by parsing their id
            if 'id' in fields:
                idx = int(data[0]) - 1
            else:
                idx = i
            # Populate particle's attributes by reading fields
            for j, field in enumerate(fields):
                if field in self._cbk:
                    self._cbk[field](data[j], idx, system)
                else:
                    # We should store these fields in particle anyway
                    pass

        return system