Beispiel #1
0
def crystal(file='fort.34'):
  """ Reads CRYSTAL's external format. """
  from numpy import array, abs, zeros, any, dot
  from numpy.linalg import inv
  from ..crystal import which_site
  from ..misc import RelativePath
  from ..error import IOError
  from ..periodic_table import find as find_specie
  from . import Structure

  if isinstance(file, str):
    if file.find('\n') == -1:
      with open(RelativePath(file).path, 'r') as file: return crystal(file)
    else: file = file.splitlines().__iter__()
  # read first line
  try: line = file.next()
  except StopIteration: raise IOError('Premature end of stream.')
  else: dimensionality, centering, type = [int(u) for u in line.split()[:3]]
  # read cell
  try: cell = array( [file.next().split()[:3] for i in xrange(3)],
                     dtype='float64' ).T
  except StopIteration: raise IOError('Premature end of stream.')
  result = Structure( cell=cell, centering=centering,
                      dimensionality=dimensionality, type=type, scale=1e0 )
  # read symmetry operators
  result.spacegroup = []
  try: N = int(file.next())
  except StopIteration: raise IOError('Premature end of stream.')
  for i in xrange(N):
    try: op = array( [file.next().split()[:3] for j in xrange(4)],
                     dtype='float64' )
    except StopIteration: raise IOError('Premature end of stream.')
    else: op[:3] = op[:3].copy().T
    result.spacegroup.append(op)
  result.spacegroup = array(result.spacegroup)

  # read atoms.
  try: N = int(file.next())
  except StopIteration: raise IOError('Premature end of stream.')

  for i in xrange(N):
    try: line = file.next().split()
    except StopIteration: raise IOError('Premature end of stream.')
    else: type, pos = int(line[0]), array(line[1:4], dtype='float64')
    if type < 100: type = find_specie(atomic_number=type).symbol
    result.add_atom(pos=pos, type=type, asymmetric=True)

  # Adds symmetrically equivalent structures.
  identity = zeros((4, 3), dtype='float64')
  for i in xrange(3): identity[i, i] == 1
  symops = [u for u in result.spacegroup if any(abs(u - identity) > 1e-8)]
  invcell = inv(result.cell)
  for atom in [u for u in result]:
    for op in symops:
      pos = dot(op[:3], atom.pos) + op[3]
      if which_site(pos, result, invcell=invcell) == -1:
        result.add_atom(pos=pos, type=atom.type, asymmetric=False)

  return result
Beispiel #2
0
def crystal(file='fort.34'):
    """ Reads CRYSTAL's external format. """
    from six import next
    from numpy import array, abs, zeros, any, dot
    from numpy.linalg import inv
    from ..crystal import which_site
    from ..misc import RelativePath
    from .. import error
    from ..periodic_table import find as find_specie
    from . import Structure

    if isinstance(file, str):
        if file.find('\n') == -1:
            with open(RelativePath(file).path, 'r') as file:
                return crystal(file)
        else:
            file = file.splitlines().__iter__()
    # read first line
    try:
        line = next(file)
    except StopIteration:
        raise error.IOError('Premature end of stream.')
    else:
        dimensionality, centering, type = [int(u) for u in line.split()[:3]]
    # read cell
    try:
        cell = array([next(file).split()[:3] for i in range(3)],
                     dtype='float64').T
    except StopIteration:
        raise error.IOError('Premature end of stream.')
    result = Structure(cell=cell, centering=centering,
                       dimensionality=dimensionality, type=type, scale=1e0)
    # read symmetry operators
    result.spacegroup = []
    try:
        N = int(next(file))
    except StopIteration:
        raise error.IOError('Premature end of stream.')
    for i in range(N):
        try:
            op = array([next(file).split()[:3] for j in range(4)],
                       dtype='float64')
        except StopIteration:
            raise error.IOError('Premature end of stream.')
        else:
            op[:3] = op[:3].copy().T
        result.spacegroup.append(op)
    result.spacegroup = array(result.spacegroup)

    # read atoms.
    try:
        N = int(next(file))
    except StopIteration:
        raise error.IOError('Premature end of stream.')

    for i in range(N):
        try:
            line = next(file).split()
        except StopIteration:
            raise error.IOError('Premature end of stream.')
        else:
            type, pos = int(line[0]), array(line[1:4], dtype='float64')
        if type < 100:
            type = find_specie(atomic_number=type).symbol
        result.add_atom(pos=pos, type=type, asymmetric=True)

    # Adds symmetrically equivalent structures.
    identity = zeros((4, 3), dtype='float64')
    for i in range(3):
        identity[i, i] == 1
    symops = [u for u in result.spacegroup if any(abs(u - identity) > 1e-8)]
    invcell = inv(result.cell)
    for atom in [u for u in result]:
        for op in symops:
            pos = dot(op[:3], atom.pos) + op[3]
            if which_site(pos, result, invcell=invcell) == -1:
                result.add_atom(pos=pos, type=atom.type, asymmetric=False)

    return result