Exemple #1
0
def read_cif(fileobj, index=-1):
    if isinstance(fileobj, str):
        fileobj = open(fileobj)

    def search_key(fobj, key):
        for line in fobj:
            if key in line:
                return line
        return None

    def get_key(fobj, key, pos=1):
        line = search_key(fobj, key)
        if line:
            return float(line.split()[pos].split('(')[0])
        return None

    a = get_key(fileobj, '_cell_length_a')
    b = get_key(fileobj, '_cell_length_b')
    c = get_key(fileobj, '_cell_length_c')
    alpha =  pi * get_key(fileobj, '_cell_angle_alpha') / 180
    beta = pi * get_key(fileobj, '_cell_angle_beta') / 180
    gamma =  pi * get_key(fileobj, '_cell_angle_gamma') / 180

    va = a * np.array([1, 0, 0])
    vb = b * np.array([cos(gamma), sin(gamma), 0])
    cx = cos(beta)
    cy = (cos(alpha) - cos(beta) * cos(gamma)) / sin(gamma)
    cz = sqrt(1. - cx*cx - cy*cy)
    vc = c * np.array([cx, cy, cz])
    cell = np.array([va, vb, vc])

    atoms = Atoms(cell=cell)
    read = False
    for line in fileobj:
        if not read:
            if '_atom_site_disorder_group' in line:
                read = True
        else:
            word = line.split()
            if len(word) < 5:
                break
            symbol = word[1]
            pos = (float(word[2].split('(')[0]) * va +
                   float(word[3].split('(')[0]) * vb +
                   float(word[4].split('(')[0]) * vc   )
            atoms.append(Atom(symbol, pos))

    return atoms
Exemple #2
0
def read_I_info(fileobj, index=-1):
    if isinstance(fileobj, str):
        fileobj = open(fileobj)

    lines = fileobj.readlines()

    del lines[0]

    finished = False
    s = Atoms()
    while not finished:
        w = lines.pop(0).split()
        if w[0].startswith('"'):
            position = Bohr * np.array([float(w[3]), float(w[4]), float(w[5])])
            s.append(Atom(w[0].replace('"', ''), position))
        else:
            finished = True

    return s
def read_gpaw_text(fileobj, index=-1):
    if isinstance(fileobj, str):
        fileobj = open(fileobj)

    def index_startswith(lines, string):
        for i, line in enumerate(lines):
            if line.startswith(string):
                return i
        raise ValueError

    lines = fileobj.readlines()
    images = []
    while True:
        try:
            i = lines.index('Unit Cell:\n')
        except ValueError:
            pass
        else:
            cell = []
            pbc = []
            for line in lines[i + 3:i + 6]:
                words = line.split()
                if len(words) == 5:  # old format
                    cell.append(float(words[2]))
                    pbc.append(words[1] == 'yes')
                else:  # new format with GUC
                    cell.append([float(word) for word in words[3:6]])
                    pbc.append(words[2] == 'yes')

        try:
            i = lines.index('Positions:\n')
        except ValueError:
            break

        atoms = Atoms(cell=cell, pbc=pbc)
        for line in lines[i + 1:]:
            words = line.split()
            if len(words) != 5:
                break
            n, symbol, x, y, z = words
            symbol = symbol.split('.')[0]
            atoms.append(Atom(symbol, [float(x), float(y), float(z)]))
        lines = lines[i + 5:]
        try:
            i = lines.index('-------------------------\n')
        except ValueError:
            e = None
        else:
            line = lines[i + 9]
            assert line.startswith('Zero Kelvin:')
            e = float(line.split()[-1])
        try:
            ii = index_startswith(lines, 'Total Charge:')
        except ValueError:
            q = None
        else:
            q = float(lines[ii].split()[2])
        try:
            ii = lines.index('Forces in eV/Ang:\n')
        except ValueError:
            f = None
        else:
            f = []
            for i in range(ii + 1, ii + 1 + len(atoms)):
                try:
                    x, y, z = lines[i].split()[-3:]
                    f.append((float(x), float(y), float(z)))
                except (ValueError, IndexError) as m:
                    raise IOError('Malformed GPAW log file: %s' % m)

        if len(images) > 0 and e is None:
            break

        if e is not None or f is not None:
            atoms.set_calculator(SinglePointCalculator(
                e, f, None, None, atoms))  ### Fixme magmoms
        if q is not None:
            n = len(atoms)
            atoms.set_charges([q / n] * n)

        images.append(atoms)
        lines = lines[i:]

    if len(images) == 0:
        raise IOError('Corrupted GPAW-text file!')

    return images[index]