示例#1
0
    def test_update_isoset(self):
        c = Specie("Cu")
        t = Specie("Ti")
        m = [[-0.5, -0.5, -0.5],
             [-0.5,  0.5,  0.5],
             [ 0.5, -0.5,  0.5]]
        ele_sea = SitesGrid.sea(2, 2, 2, c)
        cell_mother_stru = CStru(m, ele_sea).get_cell()
        sym = get_symmetry(cell_mother_stru, symprec=1e-5)
        ops = [(r, t) for r, t in zip(sym['rotations'], sym['translations'])]
        sym_perm = sogen.get_permutation_cell(cell_mother_stru)

        sites_0 = [[[c, c],
                    [c, c]],

                   [[c, c],
                    [t, c]]]
        sg_0 = SitesGrid(sites_0)
        cstru01 = CStru(m, sg_0)
        number01 = cstru01.get_cell()[2]

        isoset_init = set()
        isoset_init_copy = isoset_init.copy()
        isoset_a01 = sogen._update_isoset(isoset_init, number01, sym_perm)
        self.assertNotEqual(isoset_a01, isoset_init_copy)
        self.assertIsInstance(isoset_a01, set)

        isoset_a01_copy = isoset_a01.copy()
        isoset_a02 = sogen._update_isoset(isoset_a01, number01, sym_perm)
        self.assertEqual(isoset_a02, isoset_a01_copy)
        self.assertLessEqual(len(isoset_a01), len(ops))
示例#2
0
文件: sogen.py 项目: yuansr/ababe
def gen_nodup_cstru(lattice, sea_ele, size, speckle, num):
    d, w, l = size
    ele_sea = SitesGrid.sea(d, w, l, sea_ele)
    cell_mother_stru = CStru(lattice, ele_sea).get_cell()

    sym_perm = get_permutation_cell(cell_mother_stru)

    # For testing: Show that the first unit matrix convert to range(num) perm_operator
    # print(sym_perm[0])

    gen_dup_cstrus = CStru.gen_speckle(lattice, sea_ele, size, speckle, num)

    # Add the progress bar when looping
    from scipy.special import comb
    number_of_structures = comb((d * w * l), num)
    bar = ProgressBar(max_value=number_of_structures)

    isoset = set()
    for cstru in bar(gen_dup_cstrus):
        b, pos, atom_num = cstru.get_cell()
        #id_cstru = _get_id_seq(pos, atom_num)
        id_cstru = _get_atom_seq_identifier(atom_num)
        # print(id_cstru)
        if id_cstru not in isoset:
            # print(len(sym_perm))
            # print(len(isoset))
            # print(cstru.get_array())
            yield cstru
            _update_isoset(isoset, atom_num, sym_perm)
示例#3
0
 def __init__(self, goal_num):
     self.goal_num = goal_num
     m = [[0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]]
     self.delta = 2.1  # the distance threshold to collect neighbors
     sg = SitesGrid([[[GhostSpecie() for _ in range(goal_num)]
                      for _ in range(goal_num)] for _ in range(goal_num)])
     self.bg = CStru(m, sg)
     self.start_position = self.bg.get_midpoint()
示例#4
0
    def setUp(self):
        self.m = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
        g = GhostSpecie()
        b = Specie("B")
        self.sites = [[[g, b], [g, g]], [[b, b], [b, g]]]

        self.arr = np.array([0, 5, 0, 0, 5, 5, 5, 0]).reshape([2, 2, 2])
        self.sg = SitesGrid(self.sites)
        self.s = CStru(self.m, self.sg)
示例#5
0
 def test_gen_speckle(self):
     c = Specie("Cu")
     t = Specie("Ti")
     sites = [[[c, c], [t, t]], [[c, t], [t, c]]]
     sg = SitesGrid(sites)
     gen = CStru.gen_speckle(self.m, Specie("Cu"), (2, 2, 2), Specie("Ti"),
                             4)
     from collections import Iterator
     self.assertIsInstance(gen, Iterator)
     self.assertIn(CStru(self.m, sg), gen)
     self.assertEqual(next(gen).get_array().sum(), 204)
     self.assertEqual(next(gen).get_array().sum(), 204)
示例#6
0
    def test_equal(self):
        m_0 = [[1, 1, 1], [0, 0, 1], [1, 0, 0]]
        g = GhostSpecie()
        b = Specie("B")
        sites_0 = [[[b, b], [g, g]], [[b, g], [g, b]]]
        sg_0 = SitesGrid(sites_0)

        diff_m = CStru(m_0, self.sg)
        diff_s = CStru(self.m, sg_0)
        self.assertEqual(self.s, self.s)
        self.assertNotEqual(diff_m, self.s)
        self.assertNotEqual(diff_s, self.s)
示例#7
0
    def test_gen_nodup_cstru(self):
        c = Specie("Cu")
        t = Specie("Ti")
        m = [[-0.5, -0.5, -0.5],
             [-0.5,  0.5,  0.5],
             [ 0.5, -0.5,  0.5]]
        ele_sea = SitesGrid.sea(2, 2, 2, c)
        cell_mother_stru = CStru(m, ele_sea).get_cell()
        sym = get_symmetry(cell_mother_stru, symprec=1e-3)
        ops = [(r, t) for r, t in zip(sym['rotations'], sym['translations'])]

        sites_0 = [[[c, c],
                    [c, c]],

                   [[c, c],
                    [t, c]]]
        sg_0 = SitesGrid(sites_0)
        cstru01 = CStru(m, sg_0)

        gen_01 = sogen.gen_nodup_cstru(m, c, (2,2,2), t, 1)
        nodup_01 = [stru for stru in gen_01]
        self.assertEqual(len(nodup_01), 1)

        gen_02 = sogen.gen_nodup_cstru(m, c, (1,2,8), t, 4)
        nodup_02 = [stru for stru in gen_02]
        # eq_(len(nodup_02), 51)

        m_tri = [[0, 0, 20],
                        [1, 0, 0],
                        [0.5, 0.8660254, 0]]
        ele_sea = SitesGrid.sea(1, 3, 3, c)
        cell_mother_stru = CStru(m, ele_sea).get_cell()
        sym = get_symmetry(cell_mother_stru, symprec=1e-3)
        ops = [(r, t) for r, t in zip(sym['rotations'], sym['translations'])]

        sites_0 = [[[c, c, c],
                    [c, c, c],
                    [c, c, c]]]
        sg_0 = SitesGrid(sites_0)
        cstru01 = CStru(m, sg_0)

        gen_01 = sogen.gen_nodup_cstru(m_tri, c, (1,3,3), t, 2)
        nodup_01 = [stru for stru in gen_01]
        self.assertEqual(len(nodup_01), 2)

        gen_02 = sogen.gen_nodup_cstru(m_tri, c, (1,3,3), t, 3)
        nodup_02 = [stru for stru in gen_02]
        self.assertEqual(len(nodup_02), 4)

        gen_03 = sogen.gen_nodup_cstru(m_tri, c, (1,5,5), t, 2)
        nodup_03 = [stru for stru in gen_03]
        self.assertEqual(len(nodup_03), 4)
示例#8
0
    def test_get_cell(self):
        c = Specie("Cu")
        t = Specie("Ti")
        m = [[-0.5, -0.5, -0.5], [-0.5, 0.5, 0.5], [0.5, -0.5, 0.5]]

        sites01 = [[[c]]]
        sites02 = [[[t, c, t], [t, t, c]]]
        sg01 = SitesGrid(sites01)
        sg02 = SitesGrid(sites02)
        cstru01 = CStru(m, sg01)
        cstru02 = CStru(m, sg02)
        lat01, pos01, num01 = cstru01.get_cell()
        lat02, pos02, num02 = cstru02.get_cell()
        self.assertTrue(
            np.allclose(
                lat01,
                np.array([[-0.5, -0.5, -0.5], [-0.5, 0.5, 0.5],
                          [0.5, -0.5, 0.5]])))
        self.assertTrue(np.allclose(pos01, np.array([[0, 0, 0]])))
        self.assertTrue(np.allclose(num01, np.array([29])))

        self.assertTrue(
            np.allclose(
                lat02,
                np.array([[-0.5, -0.5, -0.5], [-1, 1, 1], [1.5, -1.5, 1.5]])))
        self.assertTrue(
            np.allclose(
                pos02,
                np.array([[0, 0, 0], [0, 0, 1 / 3], [0, 0,
                                                     2 / 3], [0, 1 / 2, 0],
                          [0, 1 / 2, 1 / 3], [0, 1 / 2, 2 / 3]])))
        self.assertTrue(np.allclose(num02, np.array([22, 29, 22, 22, 22, 29])))
示例#9
0
    def test_is_speckle_disjunct(self):
        g = GhostSpecie()
        b = Specie('B')
        m = [[0, 0, 20],
             [1, 0, 0],
             [0.5, 0.8660254, 0]]
        # ele_sea = SitesGrid.sea(4, 4, 1, b)
        # cell_mother_stru = CStru(m, ele_sea).get_cell()
        # sym = get_symmetry(cell_mother_stru, symprec=1e-3)
        # ops = [(r, t) for r, t in zip(sym['rotations'], sym['translations'])]

        sites_0 = [[[g, g, b, b],
                    [b, b, b, b],
                    [b, b, b, b],
                    [b, b, b, b]]]
        sg_0 = SitesGrid(sites_0)
        cstru00 = CStru(m, sg_0)

        self.assertFalse(sogen.is_speckle_disjunct(cstru00, g))

        sites_1 = [[[g, b, b, g],
                    [b, b, b, b],
                    [b, b, b, b],
                    [b, b, b, b]]]
        sg_1 = SitesGrid(sites_1)
        cstru01 = CStru(m, sg_1)

        self.assertFalse(sogen.is_speckle_disjunct(cstru01, g))


        sites_2 = [[[g, b, b, b],
                    [b, g, b, b],
                    [b, b, b, b],
                    [b, b, b, b]]]
        sg_2 = SitesGrid(sites_2)
        cstru02 = CStru(m, sg_2)

        self.assertTrue(sogen.is_speckle_disjunct(cstru02, g))
示例#10
0
class FCC3dArrangeProblem(arrange.ArrangeProblem):
    def __init__(self, goal_num):
        self.goal_num = goal_num
        m = [[0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]]
        self.delta = 2.1  # the distance threshold to collect neighbors
        sg = SitesGrid([[[GhostSpecie() for _ in range(goal_num)]
                         for _ in range(goal_num)] for _ in range(goal_num)])
        self.bg = CStru(m, sg)
        self.start_position = self.bg.get_midpoint()

    def get_start_position(self):
        return self.start_position

    def is_goal(self, len_path):
        is_goal = len_path == self.goal_num

        return is_goal

    def get_neighbors(self, position):
        # neighbors is a list contain positions
        neighbors = self.bg.get_neighbors(position, self.delta)

        return neighbors
示例#11
0
    def test_write_file(self):
        """.
        The function is tested by save structure
        to a POSCAR file, and then read from it.
        Compare the parameters read from to the
        origin input parameter. Using almostEqual
        """
        bcu_arr = np.array([[[5, 29, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5],
                             [5, 5, 29, 5, 5, 5]]])
        latt = [[0, 0, 20], [1, 0, 0], [0.5, sqrt(3) / 2, 0]]
        bcu_stru = CStru.from_array(latt, bcu_arr)
        poscar_bcu = VaspPOSCAR(bcu_stru.get_gcell(), zoom=4)

        tmp_file = "POSCAR.testing"
        poscar_bcu.write(tmp_file)

        with open(tmp_file, 'r') as testing_file:
            data = testing_file.read()

        self.assertEqual(data, str(poscar_bcu))
        os.remove(tmp_file)
示例#12
0
class testCStru(unittest.TestCase):
    def setUp(self):
        self.m = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
        g = GhostSpecie()
        b = Specie("B")
        self.sites = [[[g, b], [g, g]], [[b, b], [b, g]]]

        self.arr = np.array([0, 5, 0, 0, 5, 5, 5, 0]).reshape([2, 2, 2])
        self.sg = SitesGrid(self.sites)
        self.s = CStru(self.m, self.sg)

    def test_get_property(self):
        self.assertEqual(self.s.m, self.m)
        # eq_(self.s.depth, 2)
        # eq_(self.s.width, 2)
        # eq_(self.s.length, 2)

        # eq_(self.s.get_grid, self.sites)
        # arr = [[[0, 5],
        #         [0, 0]],

        #        [[5, 5],
        #         [5, 0]]]
        # eq_(self.s.get_array(), arr)

    def test_equal(self):
        m_0 = [[1, 1, 1], [0, 0, 1], [1, 0, 0]]
        g = GhostSpecie()
        b = Specie("B")
        sites_0 = [[[b, b], [g, g]], [[b, g], [g, b]]]
        sg_0 = SitesGrid(sites_0)

        diff_m = CStru(m_0, self.sg)
        diff_s = CStru(self.m, sg_0)
        self.assertEqual(self.s, self.s)
        self.assertNotEqual(diff_m, self.s)
        self.assertNotEqual(diff_s, self.s)

    def test_from_array(self):
        ss = CStru.from_array(self.m, self.arr)
        self.assertEqual(ss, self.s)

    def test_get_array(self):
        self.assertTrue(np.allclose(self.s.get_array(), self.arr))

    def test_gen_speckle(self):
        c = Specie("Cu")
        t = Specie("Ti")
        sites = [[[c, c], [t, t]], [[c, t], [t, c]]]
        sg = SitesGrid(sites)
        gen = CStru.gen_speckle(self.m, Specie("Cu"), (2, 2, 2), Specie("Ti"),
                                4)
        from collections import Iterator
        self.assertIsInstance(gen, Iterator)
        self.assertIn(CStru(self.m, sg), gen)
        self.assertEqual(next(gen).get_array().sum(), 204)
        self.assertEqual(next(gen).get_array().sum(), 204)

    def test_get_cell(self):
        c = Specie("Cu")
        t = Specie("Ti")
        m = [[-0.5, -0.5, -0.5], [-0.5, 0.5, 0.5], [0.5, -0.5, 0.5]]

        sites01 = [[[c]]]
        sites02 = [[[t, c, t], [t, t, c]]]
        sg01 = SitesGrid(sites01)
        sg02 = SitesGrid(sites02)
        cstru01 = CStru(m, sg01)
        cstru02 = CStru(m, sg02)
        lat01, pos01, num01 = cstru01.get_cell()
        lat02, pos02, num02 = cstru02.get_cell()
        self.assertTrue(
            np.allclose(
                lat01,
                np.array([[-0.5, -0.5, -0.5], [-0.5, 0.5, 0.5],
                          [0.5, -0.5, 0.5]])))
        self.assertTrue(np.allclose(pos01, np.array([[0, 0, 0]])))
        self.assertTrue(np.allclose(num01, np.array([29])))

        self.assertTrue(
            np.allclose(
                lat02,
                np.array([[-0.5, -0.5, -0.5], [-1, 1, 1], [1.5, -1.5, 1.5]])))
        self.assertTrue(
            np.allclose(
                pos02,
                np.array([[0, 0, 0], [0, 0, 1 / 3], [0, 0,
                                                     2 / 3], [0, 1 / 2, 0],
                          [0, 1 / 2, 1 / 3], [0, 1 / 2, 2 / 3]])))
        self.assertTrue(np.allclose(num02, np.array([22, 29, 22, 22, 22, 29])))
示例#13
0
 def test_from_array(self):
     ss = CStru.from_array(self.m, self.arr)
     self.assertEqual(ss, self.s)
示例#14
0
    def test_get_string(self):
        boron_arr = np.array([[[5, 0, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5],
                               [5, 5, 0, 5, 5, 5]]])
        latt = [[0, 0, 20], [1, 0, 0], [0.5, sqrt(3) / 2, 0]]
        boron_stru = CStru.from_array(latt, boron_arr)
        poscar = VaspPOSCAR(boron_stru.get_gcell(), zoom=4)

        expected_str = '''B16
4
  0.000000   0.000000  20.000000
  3.000000   0.000000   0.000000
  3.000000   5.196152   0.000000
B
16
direct
  0.000000   0.000000   0.000000 B
  0.000000   0.000000   0.333333 B
  0.000000   0.000000   0.500000 B
  0.000000   0.000000   0.666667 B
  0.000000   0.000000   0.833333 B
  0.000000   0.333333   0.000000 B
  0.000000   0.333333   0.166667 B
  0.000000   0.333333   0.333333 B
  0.000000   0.333333   0.500000 B
  0.000000   0.333333   0.666667 B
  0.000000   0.333333   0.833333 B
  0.000000   0.666667   0.000000 B
  0.000000   0.666667   0.166667 B
  0.000000   0.666667   0.500000 B
  0.000000   0.666667   0.666667 B
  0.000000   0.666667   0.833333 B
'''
        self.assertEqual(str(poscar), expected_str)

        bcu_arr = np.array([[[5, 29, 5, 5, 5, 5], [5, 5, 5, 5, 5, 5],
                             [5, 5, 29, 5, 5, 5]]])
        latt = [[0, 0, 20], [1, 0, 0], [0.5, sqrt(3) / 2, 0]]
        bcu_stru = CStru.from_array(latt, bcu_arr)
        poscar_bcu = VaspPOSCAR(bcu_stru.get_gcell(), zoom=4)

        expected_str_bcu = '''B16Cu2
4
  0.000000   0.000000  20.000000
  3.000000   0.000000   0.000000
  3.000000   5.196152   0.000000
B Cu
16 2
direct
  0.000000   0.000000   0.000000 B
  0.000000   0.000000   0.333333 B
  0.000000   0.000000   0.500000 B
  0.000000   0.000000   0.666667 B
  0.000000   0.000000   0.833333 B
  0.000000   0.333333   0.000000 B
  0.000000   0.333333   0.166667 B
  0.000000   0.333333   0.333333 B
  0.000000   0.333333   0.500000 B
  0.000000   0.333333   0.666667 B
  0.000000   0.333333   0.833333 B
  0.000000   0.666667   0.000000 B
  0.000000   0.666667   0.166667 B
  0.000000   0.666667   0.500000 B
  0.000000   0.666667   0.666667 B
  0.000000   0.666667   0.833333 B
  0.000000   0.000000   0.166667 Cu
  0.000000   0.666667   0.333333 Cu
'''
        self.assertEqual(str(poscar_bcu), expected_str_bcu)