コード例 #1
0
def test_translations(cell):
    from numpy import abs, all
    from pylada.crystal import binary, supercell, HFTransform
    from pylada.decorations import Transforms

    lattice = binary.zinc_blende()
    lattice[0].type = ['Si', 'Ge']
    lattice[1].type = ['Si', 'Ge', 'C']

    # create random structure
    structure = supercell(lattice, cell)
    hft = HFTransform(lattice, structure)

    # these are all the translations
    translations = Transforms(lattice).translations(hft)
    assert translations.shape == (len(structure) // len(lattice) - 1,
                                  len(structure))
    # compute each translation and gets decorations
    for atom in structure:
        if atom.site != 0:
            continue
        # create translation
        trans = atom.pos - lattice[0].pos
        if all(abs(trans) < 1e-8):
            continue
        # figure out its index
        index = hft.index(trans) - 1
        for site in structure:
            pos = site.pos - lattice[site.site].pos
            i = hft.index(pos, site.site)
            j = hft.index(pos + trans, site.site)
            assert translations[index, i] == j
コード例 #2
0
def test_toarray():
    """ Tests label exchange """
    from random import choice
    from numpy import all, zeros
    from pylada.crystal import binary, supercell, HFTransform
    from pylada.decorations import Transforms

    lattice = binary.zinc_blende()
    lattice[0].type = ['Si', 'Ge']
    lattice[1].type = ['Si', 'Ge', 'C']
    transforms = Transforms(lattice)
    lattice = transforms.lattice

    for u in range(11):
        structure = supercell(lattice, get_cell())
        for atom in structure:
            atom.type = choice(atom.type)
        hft = HFTransform(lattice, structure)
        a = transforms.toarray(hft, structure)
        b = zeros(len(structure), dtype='int')
        for atom in structure:
            site = lattice[atom.site]
            b[hft.index(atom.pos - site.pos,
                        atom.site)] = site.type.index(atom.type) + 1
        assert all(a == b)
コード例 #3
0
def test_supercell_indices():
    from pytest import raises
    from random import randint
    from numpy import all, abs, dot, array
    from pylada.crystal import HFTransform, Structure, supercell

    unitcell = array([[0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]])
    lattice = Structure(unitcell).add_atom(0, 0, 0, "Si")
    supercell = supercell(
        lattice, dot(lattice.cell, [[3, 0, 5], [0, 0, -1], [-2, 1, 2]]))

    a = HFTransform(unitcell, supercell)

    assert all(abs(a.transform - [[0, 2, 0], [1, 5, -1], [-2, -4, 0]]) < 1e-8)
    assert all(abs(a.quotient - [1, 1, 3]) < 1e-8)
    all_indices = set()
    for atom in supercell:
        indices = a.indices(atom.pos)
        index = a.index(atom.pos)
        assert index not in all_indices, (index, all_indices)
        assert all(indices >= 0)
        assert all(indices <= a.quotient)
        assert index == a.flatten_indices(*indices)
        all_indices.add(index)
        for i in range(20):
            vec = dot(
                supercell.cell,
                array([randint(-20, 20),
                       randint(-20, 20),
                       randint(-20, 20)],
                      dtype="float64"))
            vec += atom.pos
            assert all(abs(a.indices(vec) - indices) < 1e-8)
            with raises(ValueError):
                a.indices(vec + [0.1, 0.1, 0])

            assert index == a.index(vec)
            with raises(ValueError):
                a.index(vec + [0.1, 0.1, 0])

    assert len(all_indices) == len(supercell)
コード例 #4
0
def test_deformed_b5(u):
    from pytest import raises
    from random import randint
    from numpy import all, abs, dot, array, concatenate
    from pylada.crystal import HFTransform, supercell

    lattice = b5(u)
    supercell = supercell(lattice,
                          dot(lattice.cell, [[2, 2, 0], [0, 2, 2], [4, 0, 4]]))

    a = HFTransform(lattice.cell, supercell)

    assert all(abs(a.transform - [[-1, 1, 1], [1, -1, 1], [5, -3, -1]]) < 1e-8)
    assert all(abs(a.quotient - [2, 2, 8]) < 1e-8)
    all_indices = set()
    others = set()
    for atom in supercell:
        indices = a.indices(atom.pos - lattice[atom.site].pos)
        index = a.index(atom.pos - lattice[atom.site].pos, atom.site)
        assert index not in all_indices, (index, all_indices)
        assert all(indices >= 0)
        assert all(indices <= a.quotient)
        all_indices.add(index)
        assert str(concatenate((indices, [atom.site]))) not in others
        others.add(str(concatenate((indices, [atom.site]))))
        for i in range(20):
            vec = dot(
                supercell.cell,
                array([randint(-20, 20),
                       randint(-20, 20),
                       randint(-20, 20)],
                      dtype="float64"))
            vec += atom.pos - lattice[atom.site].pos
            assert all(abs(a.indices(vec) - indices) < 1e-8)
            with raises(ValueError):
                a.indices(vec + [0.1, 0.1, 0])
        assert index == a.index(vec, atom.site)
        with raises(ValueError):
            a.index(vec + [0.1, 0.1, 0])

    assert len(all_indices) == len(supercell)
コード例 #5
0
def test_indices():
    from random import randint
    from numpy import all, abs, dot, array
    from pytest import raises
    from pylada.crystal import HFTransform

    unitcell = array([[0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]])
    supercell = array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
    a = HFTransform(unitcell, supercell)
    assert all(abs(a.transform - [[1, 1, -1], [0, 2, 0], [0, 0, 2]]) < 1e-8)
    assert all(abs(a.quotient - [1, 2, 2]) < 1e-8)
    for i in range(20):
        vec = dot(
            supercell,
            array([randint(-20, 20),
                   randint(-20, 20),
                   randint(-20, 20)],
                  dtype="float64"))
        vec += [0, -0.5, 0.5]
        assert all(abs(a.indices(vec) - [0, 1, 1]) < 1e-8)
        with raises(ValueError):
            a.indices(vec + [0.1, 0.1, 0])
コード例 #6
0
def test_labelexchange():
    """ Tests label exchange """
    from pylada.crystal import binary, supercell, HFTransform
    from pylada.decorations import Transforms

    lattice = binary.zinc_blende()
    lattice[0].type = ['Si', 'Ge']
    lattice[1].type = ['Si', 'Ge', 'C']
    transforms = Transforms(lattice)
    lattice = transforms.lattice

    structure = supercell(lattice, [[8, 0, 0], [0, 0.5, 0.5], [0, -0.5, 0.5]])
    species = [
        'Ge', 'C', 'Si', 'C', 'Si', 'C', 'Si', 'Si', 'Ge', 'Si', 'Ge', 'Si',
        'Ge', 'Si', 'Ge', 'Ge', 'Ge', 'C', 'Ge', 'Si', 'Si', 'Si', 'Si', 'Ge',
        'Si', 'Ge', 'Si', 'Si', 'Si', 'C', 'Ge', 'Si'
    ]
    for atom, s in zip(structure, species):
        atom.type = s
    hft = HFTransform(lattice, structure)
    x = transforms.toarray(hft, structure)
    results = [
        21112222221111123331111231122131,  # <- this is x
        21112222221111122221111321133121,
        21112222221111123332222132211232,
        21112222221111121112222312233212,
        21112222221111122223333123311323,
        21112222221111121113333213322313,
        12221111112222213331111231122131,
        12221111112222212221111321133121,
        12221111112222213332222132211232,
        12221111112222211112222312233212,
        12221111112222212223333123311323,
        12221111112222211113333213322313
    ]
    permutations = transforms.label_exchange(hft)
    for a, b in zip(permutations(x), results[1:]):
        assert int(str(a)[1:-1].replace(' ', '')) == b
コード例 #7
0
def test_rotations(cell):
    from numpy import all, dot, zeros
    from numpy.linalg import inv
    from pylada.crystal import binary, supercell, HFTransform, space_group,        \
        which_site
    from pylada.decorations import Transforms

    lattice = binary.zinc_blende()
    lattice[0].type = ['Si', 'Ge']
    lattice[1].type = ['Si', 'Ge', 'C']
    sg = space_group(lattice)
    invcell = inv(lattice.cell)

    # create random structure
    structure = supercell(lattice, cell)
    hft = HFTransform(lattice, structure)

    # these are all the translations
    transforms = Transforms(lattice)
    permutations = transforms.transformations(hft)
    assert permutations.shape == (len(sg) - 1, len(structure))
    operations = transforms.invariant_ops(structure)
    assert any(operations)

    # compute each translation and gets decorations
    for index, (op, isgood) in enumerate(zip(sg[1:], operations)):
        if not isgood:
            continue
        # Create rotation and figure out its index
        permutation = zeros(len(structure), dtype='int') - 1
        for atom in structure:
            pos = dot(op[:3], atom.pos) + op[3]
            newsite = which_site(pos, lattice, invcell)
            i = hft.index(atom.pos - lattice[atom.site].pos, atom.site)
            j = hft.index(pos - lattice[newsite].pos, newsite)
            permutation[i] = j
        assert all(permutation == permutations[index])