Exemple #1
0
 def test_ovito(self):
     try:
         import ovito
     except ImportError:
         self.skipTest('missing ovito')
     N = 3
     L = 5.0
     system = System()
     system.cell = Cell([L, L, L])
     system.particle = []
     for _ in range(N):
         pos = (numpy.random.random(len(system.cell.side)) -
                0.5) * system.cell.side
         p = Particle(position=pos)
         system.particle.append(p)
     image = system.show('ovito')
    def test_write_initial_state(self):
        p = [
            PairPotential('lennard_jones', {
                'epsilon': 1.0,
                'sigma': 1.0
            }, [1, 1], CutOff('CS', 2.5))
        ]
        i = [Interaction(p, 'atomic')]
        s = System()
        s.cell = Cell([1.0, 1.0, 1.0])
        t = TrajectoryHDF5('/tmp/test_potential.h5', 'w')
        t.write_interaction(i)
        t.close()

        t = TrajectoryHDF5('/tmp/test_potential.h5', 'r')
        i = t.read_interaction()
        t.close()
Exemple #3
0
    def test_write_initial_state(self):
        p = [
            PairPotential("lennard_jones", {
                "epsilon": 1.0,
                "sigma": 1.0
            }, [1, 1], CutOff("CS", 2.5))
        ]
        i = [Interaction(p, "atomic")]
        s = System()
        s.particle = [
            Particle(position=[1.0, 1.0, 1.0], velocity=[0.0, 0.0, 0.0])
        ]
        s.cell = Cell([1.0, 1.0, 1.0])
        with TrajectoryHDF5('/tmp/test_hdf5.h5', 'w') as t:
            t.write_interaction(i)
            t.write(s, 0)

        with TrajectoryHDF5('/tmp/test_hdf5.h5', 'r') as t:
            i = t.read_interaction()
            s = t[0]
Exemple #4
0
    def read_sample(self, frame):
        system = System()
        s = self.trajectory["trajectory/realtime/sampleindex"].keys()[frame]
        system.eigenvalues = self.trajectory["trajectory/normalmodes/eigenvalues/%s" % s][:]
        system.eigenfreq = numpy.array([copysign(abs(x)**0.5, x) for x in system.eigenvalues])
        mode_idx = self.trajectory["trajectory/normalmodes/eigenvectors/index/%s" % s][:]
        eigv = {}
        for idx in mode_idx:
            # Modes are F-indexed
            omega = system.eigenfreq[idx-1]
            vect = self.trajectory["trajectory/normalmodes/eigenvectors/vector/%s_mode_%05d" % (s, idx)][:]
            eigv[omega] = vect
        system.eigenvectors = eigv

        # Add positions
        parent = os.path.splitext(self.trajectory.filename)[0]
        step = self.steps[frame]
        with TrajectoryHDF5(parent) as th:
            # The step should be there
            parent_frame = th.steps.index(step)
            system.particle = th[parent_frame].particle
            system.cell = th[parent_frame].cell
        return system
Exemple #5
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