Exemple #1
0
def choose_structures( _howmany, _min = 2, _max = 9 ):
  import random
  import numpy as np
  from pylada import crystal, enumeration

  lattice, str_list = list_all_structures( _min, _max )

  for i in  xrange(_howmany):
    nsupercell = random.randint(0, len(str_list)-1)
    while len(str_list[nsupercell][3]) == 0: 
      nsupercell = random.randint(0, len(str_list)-1)
    nstr = 0
    if len(str_list[nsupercell][3]) > 1: 
      nstr = random.randint(0, len(str_list[nsupercell][3])-1)

    supercell, flavorbase, smith = str_list[nsupercell][:3]
    x = str_list[nsupercell][3].pop(nstr)

    # creates structure
    structure = crystal.sStructure()
    structure.cell = np.dot(lattice.cell, supercell.hermite)
    crystal.fill_structure(structure)
    structure.scale = lattice.scale
    enumeration.as_structure(structure, x, flavorbase)
    yield crystal.Structure(structure)
Exemple #2
0
def read_database(filename, withperms=True):
  from numpy import array as np_array, dot as np_dot, zeros as np_zeros
  from pylada import crystal, enumeration

  lattice = crystal.Lattice()
  with open(filename, "r") as file:
    
    # reads lattice
    for line in file:
      data = line.split()
      if len(data) == 0: continue
      if data[0] == "cell:":
        lattice.cell = np_array(data[1:], dtype="float64").reshape(3,3)
      elif data[0] == "scale:":
        lattice.scale = float(line.split()[1])
      elif data[0] == "site:":
        data = line.split()
        data.pop(0)
        site = crystal.Site()
        for i in range(3): site.pos[i] = float(data.pop(0))
        while len(data): site.type.append( data.pop(0) )
        lattice.sites.append(site)
      elif data[0] == "endlattice": break

    lattice.set_as_crystal_lattice()
    transforms = enumeration.create_transforms(lattice)
    specialized = []
    nsites = len([0 for i in lattice.sites if len(i.type) > 1])

    hermite = None
    flavorbase = None
    transform = None
    structure = crystal.Structure()
    structure.scale = lattice.scale
    for line in file:
      data = line.split()
      if len(data) == 0: continue
      if data[0] == "n:": continue
      if data[0] == "hermite:":
        hermite = np_array(data[1:], dtype="float64").reshape(3,3)
        specialized = []
      elif data[0] == "transform:": 
        transform = np_array(data[1:], dtype="float64").reshape(3,3)
      elif data[0] == "smith:":
        smith = np_array( data[1:], dtype = "int64" )
        translations = enumeration.Translation(smith, nsites)
        cell = np_dot(lattice.cell, hermite)
        structure = crystal.fill_structure(cell)
        for transformation in transforms:
          if not transformation.invariant(cell): continue
          transformation.init(transform, smith)
          if not transformation.is_trivial:
            specialized.append( transformation )
      elif data[0] == "flavorbase:":
        card, nflavors = int(data[1]),  int(data[2])
        flavorbase = enumeration.create_flavorbase(card, nflavors)
        label_exchange=enumeration.LabelExchange(card, nflavors)
      elif len(data) > 0:
        assert flavorbase is not None
        assert hermite is not None
        for x in data:
          # adds label permutations that are not equivalent by affine transforms.
          x = int(x)
          others = set([x])
          if withperms:
            for labelperm in label_exchange:
              u = labelperm(x, flavorbase)
              if u in others: continue
  
              dont = False
              for transform in specialized:
                t = transform(u, flavorbase)
                if t in others:
                  dont = True
                  break
  
                for translation in translations:
                  v = translation(t, flavorbase)
                  if v in others:
                    dont = True
                    break
              if not dont: others.add(u)

          for u in others:
            enumeration.as_structure(structure, u, flavorbase)
            yield structure