def test_zb():
    from numpy import all, abs, dot
    from pylada.crystal import space_group, transform, binary

    structure = binary.zinc_blende()
    ops = space_group(structure)
    assert len(ops) == 24
    for op in ops:
        assert op.shape == (4, 3)

        other = transform(structure, op)
        assert all(abs(dot(op[:3], structure.cell) - other.cell) < 1e-8)
        for a, atom in zip(structure, other):
            assert all(abs(dot(op[:3], a.pos) + op[3] - atom.pos) < 1e-8)
            assert a.type == atom.type

    for atom in structure:
        atom.type = ['A', 'B']
    ops = space_group(structure)
    assert len(ops) == 48
    for op in ops:
        assert op.shape == (4, 3)

        other = transform(structure, op)
        assert all(abs(dot(op[:3], structure.cell) - other.cell) < 1e-8)
        for a, atom in zip(structure, other):
            assert all(abs(dot(op[:3], a.pos) + op[3] - atom.pos) < 1e-8)
            assert a.type == atom.type
def test_zb():
    from numpy import all, abs, dot
    from pylada.crystal import space_group, transform, binary

    structure = binary.zinc_blende()
    ops = space_group(structure)
    assert len(ops) == 24
    for op in ops:
        assert op.shape == (4, 3)

        other = transform(structure, op)
        assert all(abs(dot(op[:3], structure.cell) - other.cell) < 1e-8)
        for a, atom in zip(structure, other):
            assert all(abs(dot(op[:3], a.pos) + op[3] - atom.pos) < 1e-8)
            assert a.type == atom.type

    for atom in structure:
        atom.type = ['A', 'B']
    ops = space_group(structure)
    assert len(ops) == 48
    for op in ops:
        assert op.shape == (4, 3)

        other = transform(structure, op)
        assert all(abs(dot(op[:3], structure.cell) - other.cell) < 1e-8)
        for a, atom in zip(structure, other):
            assert all(abs(dot(op[:3], a.pos) + op[3] - atom.pos) < 1e-8)
            assert a.type == atom.type
def test_fcc():
    """ Test fcc space-group """
    from numpy import all, abs, dot
    from pylada.crystal import space_group, Structure, transform

    structure = Structure([[0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]],
                          m=True).add_atom(0, 0, 0, "Si", m=True)
    ops = space_group(structure)
    assert len(ops) == 48
    for op in ops:
        assert op.shape == (4, 3)
        assert all(abs(op[3, :]) < 1e-8)

        other = transform(structure, op)
        assert all(abs(dot(op[:3], structure.cell) - other.cell) < 1e-8)
        assert getattr(other, 'm', False)
        for a, atom in zip(structure, other):
            assert all(abs(dot(op[:3], a.pos) + op[3] - atom.pos) < 1e-8)
            assert a.type == atom.type
            assert getattr(atom, 'm', False)
def test_fcc():
    """ Test fcc space-group """
    from numpy import all, abs, dot
    from pylada.crystal import space_group, Structure, transform

    structure = Structure([[0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]],
                          m=True).add_atom(0, 0, 0, "Si", m=True)
    ops = space_group(structure)
    assert len(ops) == 48
    for op in ops:
        assert op.shape == (4, 3)
        assert all(abs(op[3,:]) < 1e-8)

        other = transform(structure, op)
        assert all(abs(dot(op[:3], structure.cell) - other.cell) < 1e-8)
        assert getattr(other, 'm', False)
        for a, atom in zip(structure, other):
            assert all(abs(dot(op[:3], a.pos) + op[3] - atom.pos) < 1e-8)
            assert a.type == atom.type
            assert getattr(atom, 'm', False)
def test_b5(u):
    """ Test b5 space-group """
    from random import random, randint
    from numpy import all, abs, dot, pi
    from numpy.linalg import inv, norm
    from numpy.random import random_sample
    from pylada.crystal import space_group, Structure, transform
    from pylada.crystal import which_site

    x, y = u, 0.25 - u
    structure = Structure([[0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]]) \
        .add_atom(5.000000e-01, 5.000000e-01, 5.000000e-01, "A") \
        .add_atom(5.000000e-01, 2.500000e-01, 2.500000e-01, "A") \
        .add_atom(2.500000e-01, 5.000000e-01, 2.500000e-01, "A") \
        .add_atom(2.500000e-01, 2.500000e-01, 5.000000e-01, "A") \
        .add_atom(8.750000e-01, 8.750000e-01, 8.750000e-01, "B") \
        .add_atom(1.250000e-01, 1.250000e-01, 1.250000e-01, "B") \
        .add_atom(     x,     x,     x, "X") \
        .add_atom(     x,     y,     y, "X") \
        .add_atom(     y,     x,     y, "X") \
        .add_atom(     y,     y,     x, "X") \
        .add_atom(    -x,    -x,    -x, "X") \
        .add_atom(    -x,    -y,    -y, "X") \
        .add_atom(    -y,    -x,    -y, "X") \
        .add_atom(-y,    -y,    -x, "X")
    ops = space_group(structure)
    assert len(ops) == 48
    invcell = inv(structure.cell)
    for op in ops:
        assert op.shape == (4, 3)

        other = transform(structure, op)
        assert all(abs(dot(op[:3], structure.cell) - other.cell) < 1e-8)
        for a, atom in zip(structure, other):
            assert all(abs(dot(op[:3], a.pos) + op[3] - atom.pos) < 1e-8)
            assert a.type == atom.type
        sites = []
        for i, atom in enumerate(structure):
            pos = dot(op[:3], atom.pos) + op[3]
            j = which_site(pos, structure, invcell)
            assert j != -1
            sites.append(j)
        assert len(set(sites)) == len(structure)

    structure[0], structure[-1] = structure[-1], structure[0]
    ops = space_group(structure)
    assert len(ops) == 48
    for op in ops:
        assert op.shape == (4, 3)

        other = transform(structure, op)
        assert all(abs(dot(op[:3], structure.cell) - other.cell) < 1e-8)
        for a, atom in zip(structure, other):
            assert all(abs(dot(op[:3], a.pos) + op[3] - atom.pos) < 1e-8)
            assert a.type == atom.type
        sites = []
        for i, atom in enumerate(structure):
            pos = dot(op[:3], atom.pos) + op[3]
            j = which_site(pos, structure, invcell)
            assert j != -1, (i, atom, op)
            sites.append(j)
        assert len(set(sites)) == len(structure)

    # try random rotation, translations, atom swap
    structure[0], structure[-1] = structure[-1], structure[0]
    for u in range(10):
        axis = random_sample((3, ))
        axis /= norm(axis)
        rotation = rotation_matrix(pi * random(), axis)
        translation = random_sample((3, ))
        other = transform(structure, rotation, translation)
        for u in range(10):
            l, m = randint(0,
                           len(structure) - 1), randint(0,
                                                        len(structure) - 1)
            a, b = other[l], other[m]
            other[l], other[m] = b, a
        invcell = inv(other.cell)
        ops = space_group(other)
        for z, op in enumerate(ops):
            assert op.shape == (4, 3)

            other2 = transform(other, op)
            assert all(abs(dot(op[:3], other.cell) - other2.cell) < 1e-8)
            for a, atom in zip(other, other2):
                assert all(abs(dot(op[:3], a.pos) + op[3] - atom.pos) < 1e-8)
                assert a.type == atom.type
            sites = []
            for i, atom in enumerate(other):
                pos = dot(op[:3], atom.pos) + op[3]
                j = which_site(pos, other, invcell)
                if j == -1:
                    print((i, z))
                    print(atom)
                    print(op)
                    print(pos)
                    print(other)
                    raise Exception()
                sites.append(j)
            assert len(set(sites)) == len(other)
def test_b5(u):
    """ Test b5 space-group """
    from random import random, randint
    from numpy import all, abs, dot, pi
    from numpy.linalg import inv, norm
    from numpy.random import random_sample
    from pylada.crystal import space_group, Structure, transform
    from pylada.crystal import which_site

    x, y = u, 0.25 - u
    structure = Structure([[0, 0.5, 0.5], [0.5, 0, 0.5], [0.5, 0.5, 0]]) \
        .add_atom(5.000000e-01, 5.000000e-01, 5.000000e-01, "A") \
        .add_atom(5.000000e-01, 2.500000e-01, 2.500000e-01, "A") \
        .add_atom(2.500000e-01, 5.000000e-01, 2.500000e-01, "A") \
        .add_atom(2.500000e-01, 2.500000e-01, 5.000000e-01, "A") \
        .add_atom(8.750000e-01, 8.750000e-01, 8.750000e-01, "B") \
        .add_atom(1.250000e-01, 1.250000e-01, 1.250000e-01, "B") \
        .add_atom(     x,     x,     x, "X") \
        .add_atom(     x,     y,     y, "X") \
        .add_atom(     y,     x,     y, "X") \
        .add_atom(     y,     y,     x, "X") \
        .add_atom(    -x,    -x,    -x, "X") \
        .add_atom(    -x,    -y,    -y, "X") \
        .add_atom(    -y,    -x,    -y, "X") \
        .add_atom(-y,    -y,    -x, "X")
    ops = space_group(structure)
    assert len(ops) == 48
    invcell = inv(structure.cell)
    for op in ops:
        assert op.shape == (4, 3)

        other = transform(structure, op)
        assert all(abs(dot(op[:3], structure.cell) - other.cell) < 1e-8)
        for a, atom in zip(structure, other):
            assert all(abs(dot(op[:3], a.pos) + op[3] - atom.pos) < 1e-8)
            assert a.type == atom.type
        sites = []
        for i, atom in enumerate(structure):
            pos = dot(op[:3], atom.pos) + op[3]
            j = which_site(pos, structure, invcell)
            assert j != -1
            sites.append(j)
        assert len(set(sites)) == len(structure)

    structure[0], structure[-1] = structure[-1], structure[0]
    ops = space_group(structure)
    assert len(ops) == 48
    for op in ops:
        assert op.shape == (4, 3)

        other = transform(structure, op)
        assert all(abs(dot(op[:3], structure.cell) - other.cell) < 1e-8)
        for a, atom in zip(structure, other):
            assert all(abs(dot(op[:3], a.pos) + op[3] - atom.pos) < 1e-8)
            assert a.type == atom.type
        sites = []
        for i, atom in enumerate(structure):
            pos = dot(op[:3], atom.pos) + op[3]
            j = which_site(pos, structure, invcell)
            assert j != -1, (i, atom, op)
            sites.append(j)
        assert len(set(sites)) == len(structure)

    # try random rotation, translations, atom swap
    structure[0], structure[-1] = structure[-1], structure[0]
    for u in range(10):
        axis = random_sample((3,))
        axis /= norm(axis)
        rotation = rotation_matrix(pi * random(), axis)
        translation = random_sample((3,))
        other = transform(structure, rotation, translation)
        for u in range(10):
            l, m = randint(0, len(structure) - 1), randint(0, len(structure) - 1)
            a, b = other[l], other[m]
            other[l], other[m] = b, a
        invcell = inv(other.cell)
        ops = space_group(other)
        for z, op in enumerate(ops):
            assert op.shape == (4, 3)

            other2 = transform(other, op)
            assert all(abs(dot(op[:3], other.cell) - other2.cell) < 1e-8)
            for a, atom in zip(other, other2):
                assert all(abs(dot(op[:3], a.pos) + op[3] - atom.pos) < 1e-8)
                assert a.type == atom.type
            sites = []
            for i, atom in enumerate(other):
                pos = dot(op[:3], atom.pos) + op[3]
                j = which_site(pos, other, invcell)
                if j == -1:
                    print((i, z))
                    print(atom)
                    print(op)
                    print(pos)
                    print(other)
                    raise Exception()
                sites.append(j)
            assert len(set(sites)) == len(other)