def test_b5(u): """ Test b5 space-group and equivalents """ from numpy import dot from numpy.random import randint from pylada.crystal import Structure, which_site x, y = u, 0.25-u lattice = 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") assert which_site(lattice[6].pos + [0.5, -0.5, 2], lattice) == 6 for i, atom in enumerate(lattice): assert which_site(atom.pos, lattice) == i for j in xrange(10): newpos = dot(lattice.cell, randint(10, size=(3,)) - 5) assert which_site(atom.pos + newpos, lattice) == i, (atom.pos, newpos, i)
def test_b5(u): """ Test b5 space-group and equivalents """ from numpy import dot from numpy.random import randint from pylada.crystal import Structure, which_site x, y = u, 0.25 - u lattice = 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") assert which_site(lattice[6].pos + [0.5, -0.5, 2], lattice) == 6 for i, atom in enumerate(lattice): assert which_site(atom.pos, lattice) == i for j in xrange(10): newpos = dot(lattice.cell, randint(10, size=(3, )) - 5) assert which_site(atom.pos + newpos, lattice) == i, (atom.pos, newpos, i)
def test_position_to_atomic_site_correspondence(u): from numpy import dot from numpy.random import randint from pylada.crystal import which_site lattice = get_a_b5_lattice(u) assert which_site(lattice[6].pos + [0.5, -0.5, 2], lattice) == 6 for i, atom in enumerate(lattice): assert which_site(atom.pos, lattice) == i for j in range(10): newpos = dot(lattice.cell, randint(10, size=(3,)) - 5) assert which_site(atom.pos + newpos, lattice) == i, (atom.pos, newpos, i)
def test_position_to_atomic_site_correspondence(u): from numpy import dot from numpy.random import randint from pylada.crystal import which_site lattice = get_a_b5_lattice(u) assert which_site(lattice[6].pos + [0.5, -0.5, 2], lattice) == 6 for i, atom in enumerate(lattice): assert which_site(atom.pos, lattice) == i for j in range(10): newpos = dot(lattice.cell, randint(10, size=(3, )) - 5) assert which_site(atom.pos + newpos, lattice) == i, (atom.pos, newpos, i)
def test(): from numpy import all, abs, dot, identity from pylada.dftcrystal import Crystal, External from pylada.crystal import which_site orig = Crystal(227, 5.43).add_atom(0.125, 0.125, 0.125, 'Si') b = External(copy=orig.eval()) assert all(abs(orig.eval().cell - b.initial.cell) < 1e-8) assert len(b.initial) == 2 assert repr(b) == repr( eval(repr(b), {'External': External}) ) assert len(b.print_input().splitlines()) == 2 assert b.print_input().splitlines()[0] == 'EXTERNAL' assert b.print_input().splitlines()[1] == 'END EXTERNAL' # check that symmetries as extracted are ok. for atom in b.initial: for op in orig.symmetry_operators: assert all(abs(identity(3, dtype='float64') - dot(op[:3].T, op[:3])) < 1e-8) assert which_site(dot(op[:3], atom.pos) + op[3], b.initial) != -1 # Check that running structure through crystal works c = b.eval() assert all(abs(c.cell - b.cell) < 1e-8) assert len(c) == 2 assert all(abs(abs(c[0].pos) - [0.67875]*3) < 1e-8) assert all(abs(abs(c[1].pos) - [0.67875]*3) < 1e-8) assert all(abs(c[0].pos + c[1].pos) < 1e-8)
def inequivalent_sites(lattice): """ Returns a list containing only one site index per inequivalent sub-lattice. """ from pylada.crystal import which_site result = set( i for i in range(len(lattice.sites)) ) for i, site in enumerate(lattice.sites): if i not in result: continue for op in lattice.space_group: j = which_site( op(site.pos), lattice ) if j != i and j in result: result.remove(j) return result
def _spin(self, position): """ Returns formatted spin. """ from pylada.crystal import which_site from pylada.error import ValueError # find sublattice sublattice = which_site(position, self.lattice) if sublattice == -1: raise ValueError('Could not find atomic site') # create spin and adds it to group. return spin(position - self.lattice[sublattice].pos, sublattice)
def test_rotations(): from numpy import all, dot, zeros from numpy.linalg import inv from pylada.crystal import binary, supercell, HFTransform, space_group, \ which_site from pylada.enum 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) def get_cells(n): for i in xrange(1, n): yield [[i, 0, 0], [0, 0.5, 0.5], [0, -0.5, 0.5]] for i in xrange(1, n): yield [[i, 0, 0], [0, i, 0], [0, 0, 1]] for i in xrange(1, n): yield [[i, 0, 0], [0, i, 0], [0, 0, i]] yield dot(lattice.cell, [[1, 0, 0], [0, 1, 0], [0, 0, 2]]) for cell in get_cells(8): # 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])
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])
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 and equivalents """ 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.cppwrappers import space_group, Structure, equivalent, \ transform from pylada.math import Rotation 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) assert equivalent(structure, other, cartesian=False) assert equivalent(other, structure, cartesian=False) 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) assert equivalent(structure, other, cartesian=False) assert equivalent(other, structure, cartesian=False) # try random rotation, translations, atom swap structure[0], structure[-1] = structure[-1], structure[0] for u in xrange(10): axis = random_sample((3,)) axis /= norm(axis) rotation = Rotation( pi*random(), axis) rotation[3] = random_sample((3,)) other = transform(structure, rotation) for u in xrange(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) assert equivalent(other, other2, cartesian=False) assert equivalent(other2, other, cartesian=False)