def test_spins_are_sorted(): """ Check spin sorting when using add_spins. """ from numpy import all, dot from numpy.random import randint from random import choice from itertools import permutations from pylada.ce.cluster import spin from pylada.ce import Cluster from pylada.crystal import binary lattice = binary.zinc_blende() lattice[0].type = ['Si', 'Ge'] lattice[1].type = ['Si', 'Ge', 'C'] # Trial with known result. a = Cluster(lattice) a.add_spin(lattice[1].pos) a.add_spin(lattice[0].pos + [0.5, 0, 0.5]) a.add_spin(lattice[1].pos + [-0.5, 0, 0.5]) a.add_spin(lattice[0].pos + [-2.5, -1.0, 0.5]) assert all(a.spins[0] == spin([0, 0, 0], 1)) assert all(a.spins[1] == spin([0.5, 0, 0.5])) assert all(a.spins[2] == spin([-0.5, 0, 0.5], 1)) assert all(a.spins[3] == spin([-2.5, -1.0, 0.5], 0)) spins = a.spins.copy() for p in permutations(range(len(spins))): a = Cluster(lattice) for i in p: a.add_spin(spins[i]['position'] + lattice[spins[i]['sublattice']].pos) assert all(a.spins == spins) # Trial with unknown result. for i in xrange(20): a = Cluster(lattice) for j in xrange(5): site = choice([0, 1]) pos = lattice[site].pos + dot(lattice.cell, randint(4, size=(3,))-2) try: a.add_spin(pos) except ValueError: pass if a.order < 1: continue spins = a.spins.copy() for p in permutations(range(len(spins))): a = Cluster(lattice) for i in p: a.add_spin(spins[i]['position'] + lattice[spins[i]['sublattice']].pos) assert all(a.spins == spins)
def test_addspins(): """ Test adding spins to cluster. Check failure modes. """ from numpy import all from pylada.crystal import binary from pylada.ce import Cluster from pylada.ce.cluster import spin from pylada.error import ValueError lattice = binary.zinc_blende() lattice[0].type = ['Si', 'Ge'] lattice[1].type = ['Si', 'Ge', 'C'] a = Cluster(lattice) # Wrong position try: a.add_spin(lattice[0].pos+0.1) except ValueError: pass else: raise Exception() # now add first spin a.add_spin(lattice[0].pos) assert len(a.spins) == 1 assert all(a.spins[0] == spin([0, 0, 0], 0)) # try adding it again try: a.add_spin(lattice[0].pos) except ValueError: pass else: raise Exception() # Wrong position try: a.add_spin(lattice[1].pos+[1.1, -0.5, -2.5]) except ValueError: pass else: raise Exception() # Then add a different spin a.add_spin(lattice[1].pos + [1.0, -0.5, -2.5]) assert len(a.spins) == 2 assert all(a.spins[0] == spin([0, 0, 0], 0)) assert all(a.spins[1] == spin([1.0, -0.5, -2.5], 1))
def test_cmp(): """ Test Cluster._contains function """ from numpy import all, any from pylada.crystal import binary from pylada.ce import Cluster from pylada.ce.cluster import spin lattice = binary.zinc_blende() lattice[0].type = ['Si', 'Ge'] lattice[1].type = ['Si', 'Ge', 'C'] def cmp(a,b): if len(a) != len(b): return False return all([any([all(v == s) for s in a]) for v in b]) a = Cluster(lattice) a.add_spin(lattice[0].pos) assert cmp(a.spins, [spin([0, 0, 0], 0)]) assert not cmp(a.spins, [spin([0.5, 0.5, 0.5], 0)]) assert not cmp(a.spins, [spin([0, 0, 0], 1)]) a = Cluster(lattice) a.add_spin(lattice[1].pos) assert cmp(a.spins, [spin([0, 0, 0], 1)]) assert not cmp(a.spins, [spin([0.5, 0.5, 0.5], 1)]) assert not cmp(a.spins, [spin([0, 0, 0], 0)]) a.add_spin(lattice[0].pos) assert cmp(a.spins, [spin([0, 0, 0], 1), spin([0, 0, 0])]) assert cmp(a.spins, [spin([0, 0, 0]), spin([0, 0, 0], 1)]) assert not cmp(a.spins, [spin([0, 0, 0], 1), spin([0, 0, 0]), spin([1, 0, 0])]) assert not cmp(a.spins, [spin([1, 0, 0]), spin([0, 0, 0], 1)])