Пример #1
0
 def test_shortest_vector(self):
     raise SkipTest
     # simple cases
     uc = UnitCell(numpy.identity(3,float)*3)
     self.assertArraysAlmostEqual(uc.shortest_vector([3, 0, 1]), numpy.array([0, 0, 1]))
     self.assertArraysAlmostEqual(uc.shortest_vector([-3, 0, 1]), numpy.array([0, 0, 1]))
     self.assertArraysAlmostEqual(uc.shortest_vector([-2, 0, 1]), numpy.array([1, 0, 1]))
     self.assertArraysAlmostEqual(uc.shortest_vector([-1.6, 1, 1]), numpy.array([1.4, 1, 1]))
     self.assertArraysAlmostEqual(uc.shortest_vector([-1.4, 1, 1]), numpy.array([-1.4, 1, 1]))
     # simple cases
     uc = UnitCell(numpy.identity(3,float)*3, numpy.array([True, False, False]))
     self.assertArraysAlmostEqual(uc.shortest_vector([3, 0, 1]), numpy.array([0, 0, 1]))
     self.assertArraysAlmostEqual(uc.shortest_vector([3, 0, 3]), numpy.array([0, 0, 3]))
     # random tests
     for uc_counter in xrange(1000):
         uc = self.get_random_uc(full=False)
         for r_counter in xrange(10):
             r0 = numpy.random.normal(0, 10, 3)
             r1 = uc.shortest_vector(r0)
             change = r1 - r0
             self.assert_(numpy.dot(change, r0) <= 0)
             #self.assert_(numpy.linalg.norm(r0) >= numpy.linalg.norm(r1))
             index = uc.to_fractional(r0-r1)
             self.assertArraysAlmostEqual(index, numpy.round(index), doabs=True)
             index = uc.to_fractional(r1)
             self.assert_(index.max()<0.5)
             self.assert_(index.max()>=-0.5)
         r0 = numpy.random.normal(0, 10, (10,3))
         r1 = uc.shortest_vector(r0)
         for i in xrange(10):
             r1_row_bis = uc.shortest_vector(r0[i])
             self.assertArraysAlmostEqual(r1_row_bis, r1[i], doabs=True)
Пример #2
0
def main(file_name):
    xyz_file = XYZFile(file_name)
    frames = xyz_file.geometries
    #    titles = xyz_file.titles

    # Unit cell, decide how to make this general
    matrix = np.array([[
        1.4731497044857509E+01, 3.2189795740722255E-02, 4.5577626559295564E-02
    ], [
        4.2775481701113616E-02, 2.1087874593411915E+01, -2.8531114198383896E-02
    ], [
        6.4054385616337750E-02, 1.3315840416191497E-02, 1.4683043045316882E+01
    ]])
    matrix *= angstrom
    cell = UnitCell(matrix)
    frac = UnitCell.to_fractional(cell, frames)

    xmin = np.min(frac[:, :, 0])
    ymin = np.min(frac[:, :, 1])
    zmin = np.min(frac[:, :, 2])
    frac[:, :, 0] -= -0.5  # xmin
    frac[:, :, 1] -= -0.5  # ymin
    frac[:, :, 2] -= -0.5  # zmin
    decimals = np.modf(frac)[0]
    #    decimals[:,:,0] += xmin
    #    decimals[:,:,1] += ymin
    #    decimals[:,:,2] += zmin
    frac_wrapped = np.where(decimals < 0, 1 + decimals, decimals)
    #    frac_wrapped[:,:,0] += xmin
    #    frac_wrapped[:,:,1] += ymin
    #    frac_wrapped[:,:,2] += zmin
    cart_wrapped = UnitCell.to_cartesian(cell, frac_wrapped)

    xyz_file.geometries = cart_wrapped
    xyz_file.write_to_file(file_name.rsplit(".", 1)[0] + "_wrapped.xyz")
Пример #3
0
    def _setup_grid(self, cutoff, unit_cell, grid):
        """Choose a proper grid for the binning process"""
        if grid is None:
            # automatically choose a decent grid
            if unit_cell is None:
                grid = cutoff / 2.9
            else:
                # The following would be faster, but it is not reliable
                # enough yet.
                #grid = unit_cell.get_optimal_subcell(cutoff/2.0)
                divisions = np.ceil(unit_cell.spacings / cutoff)
                divisions[divisions < 1] = 1
                grid = unit_cell / divisions

        if isinstance(grid, float):
            grid_cell = UnitCell(
                np.array([[grid, 0, 0], [0, grid, 0], [0, 0, grid]]))
        elif isinstance(grid, UnitCell):
            grid_cell = grid
        else:
            raise TypeError(
                "Grid must be None, a float or a UnitCell instance.")

        if unit_cell is not None:
            # The columns of integer_matrix are the unit cell vectors in
            # fractional coordinates of the grid cell.
            integer_matrix = grid_cell.to_fractional(
                unit_cell.matrix.transpose()).transpose()
            if abs((integer_matrix - np.round(integer_matrix)) *
                   self.unit_cell.active).max() > 1e-6:
                raise ValueError(
                    "The unit cell vectors are not an integer linear combination of grid cell vectors."
                )
            integer_matrix = integer_matrix.round()
            integer_cell = UnitCell(integer_matrix, unit_cell.active)
        else:
            integer_cell = None

        return grid_cell, integer_cell
Пример #4
0
    def _setup_grid(self, cutoff, unit_cell, grid):
        """Choose a proper grid for the binning process"""
        if grid is None:
            # automatically choose a decent grid
            if unit_cell is None:
                grid = cutoff/2.9
            else:
                # The following would be faster, but it is not reliable
                # enough yet.
                #grid = unit_cell.get_optimal_subcell(cutoff/2.0)
                divisions = numpy.ceil(unit_cell.spacings/cutoff)
                divisions[divisions<1] = 1
                grid = unit_cell/divisions

        if isinstance(grid, float):
            grid_cell = UnitCell(numpy.array([
                [grid, 0, 0],
                [0, grid, 0],
                [0, 0, grid]
            ]))
        elif isinstance(grid, UnitCell):
            grid_cell = grid
        else:
            raise TypeError("Grid must be None, a float or a UnitCell instance.")

        if unit_cell is not None:
            # The columns of integer_matrix are the unit cell vectors in
            # fractional coordinates of the grid cell.
            integer_matrix = grid_cell.to_fractional(unit_cell.matrix.transpose()).transpose()
            if abs((integer_matrix - numpy.round(integer_matrix))*self.unit_cell.active).max() > 1e-6:
                raise ValueError("The unit cell vectors are not an integer linear combination of grid cell vectors.")
            integer_matrix = integer_matrix.round()
            integer_cell = UnitCell(integer_matrix, unit_cell.active)
        else:
            integer_cell = None

        return grid_cell, integer_cell