コード例 #1
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)
コード例 #2
0
ファイル: test_transform.py プロジェクト: pylada/pylada-light
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
コード例 #3
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
コード例 #4
0
ファイル: transform.py プロジェクト: georgeyumnam/pylada
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])
コード例 #5
0
ファイル: cluster.py プロジェクト: georgeyumnam/pylada
def fhmap(lattice, supercell):
  """ Creates a map from Forkade-Hart indices to atom indices """
  from pylada.error import ValueError
  from pylada.crystal import HFTransform
  transform = HFTransform(lattice, supercell)
  results = {}
  for i, atom in enumerate(supercell):
    if not hasattr(atom, 'site') and len(lattice) > 0: 
      raise ValueError( 'Atoms in supercell were not mapped to lattice. '      \
                        'Please use pylada.crystal.map_sites.' )
    site = getattr(atom, 'site', 0)
    try: index = transform.index(atom.pos - lattice[site].pos, site)
    except ValueError:
      print supercell.cell
      print atom.pos, lattice[site].pos, atom.pos-lattice[site].pos
      raise
    results[index] = i
  return results
コード例 #6
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])
コード例 #7
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)
コード例 #8
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])
コード例 #9
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])
コード例 #10
0
ファイル: test_transform.py プロジェクト: pylada/pylada-light
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)
コード例 #11
0
ファイル: cluster.py プロジェクト: georgeyumnam/pylada
  def __call__(self, structure, mapping=None, transform=None, fhmapping=None):
    """ Returns PI of a particular structure. """
    from numpy import zeros
    from .cppwrappers import outer_sum
    from ..crystal import HFTransform

    if self.spins is None or len(self.spins) == 0:
      return float(len(structure))
    if not hasattr(self, '_symmetrized'): self._create_symmetrized()
    if mapping is None: mapping = self.occupation_mapping()
    if transform is None: transform = HFTransform(self.lattice, structure)
    if fhmapping is None: fhmapping = fhmap(self.lattice, structure)
    
    
    # computes shape of the result
    nshape = []
    for spin in self.spins:
      nshape.append(len(self.lattice[spin['sublattice']].type)-1)
    # creates result and intermediate vectors.
    result = zeros(nshape, dtype='float64')

    # loop over possible origin of cluster
    for origin in structure:
      pos = origin.pos - self.lattice[origin.site].pos
      # loop over symmetrically equivalent clusters
      for spins in self._symmetrized:
        # no need to double count
        if spins['sublattice'][0] != origin.site: continue
        args = []
        # loop over each spin in cluster
        for spin in spins: 
          index = transform.index(pos + spin['position'], spin['sublattice'])
          atom  = structure[ fhmapping[index] ]
          args.append(mapping[atom.site][atom.type])
        outer_sum(result, *args)
    return result
コード例 #12
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)
コード例 #13
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)
コード例 #14
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
コード例 #15
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)