Beispiel #1
0
def read_pdb(fileobj, index=-1):
    """Read PDB files.

    The format is assumed to follow the description given in
    http://www.wwpdb.org/documentation/format32/sect9.html."""
    if isinstance(fileobj, str):
        fileobj = open(fileobj)

    images = []
    atoms = Atoms()
    for line in fileobj.readlines():
        if line.startswith('ATOM') or line.startswith('HETATM'):
            try:
                # Atom name is arbitrary and does not necessarily contain the element symbol.
                # The specification requires the element symbol to be in columns 77+78.
                symbol = line[76:78].strip().lower().capitalize()
                words = line[30:55].split()
                position = np.array([float(words[0]), 
                                     float(words[1]),
                                     float(words[2])])
                atoms.append(Atom(symbol, position))
            except:
                pass
        if line.startswith('ENDMDL'):
            images.append(atoms)
            atoms = Atoms()
    if len(images) == 0:
        images.append(atoms)
    return images[index]
Beispiel #2
0
def read_pdb(fileobj, index=-1):
    """Read PDB files.

    The format is assumed to follow the description given in
    http://www.wwpdb.org/documentation/format32/sect9.html."""
    if isinstance(fileobj, str):
        fileobj = open(fileobj)

    atoms = Atoms()
    for line in fileobj.readlines():
        if line.startswith('ATOM'):
            try:
                symbol = line[12:16].strip()
                # we assume that the second character is a label 
                # in case that it is upper case
                if len(symbol) > 1 and symbol[1].isupper():
                    symbol = symbol[0]
                words = line[30:55].split()
                position = np.array([float(words[0]), 
                                     float(words[1]),
                                     float(words[2])])
                atoms.append(Atom(symbol, position))
            except:
                pass

    return atoms
def read_pdb(fileobj, index=-1):
    """Read PDB files.
    The format is assumed to follow the description given in
    http://www.wwpdb.org/documentation/format32/sect9.html."""
    if isinstance(fileobj, str):
        fileobj = open(fileobj)

    images = []
    atoms = Atoms()
    for line in fileobj.readlines():
        if line.startswith('ATOM') or line.startswith('HETATM'):
            try:
                # Atom name is arbitrary and does not necessarily contain the element symbol.
                # The specification requires the element symbol to be in columns 77+78.
                symbol = line[76:78].strip().lower().capitalize()
                words = line[30:55].split()
                position = np.array([float(words[0]), 
                                     float(words[1]),
                                     float(words[2])])
                atoms.append(Atom(symbol, position))
            except:
                pass
        if line.startswith('ENDMDL'):
            images.append(atoms)
            atoms = Atoms()
    if len(images) == 0:
        images.append(atoms)
    return images[index]
Beispiel #4
0
def read_pdb(fileobj, index=-1):
    """Read PDB files.

    The format is assumed to follow the description given in
    http://www.wwpdb.org/documentation/format32/sect9.html."""
    if isinstance(fileobj, str):
        fileobj = open(fileobj)

    atoms = Atoms()
    for line in fileobj.readlines():
        if line.startswith('ATOM') or line.startswith('HETATM'):
            try:
                symbol = line[12:16].strip()
                # we assume that the second character is a label
                # in case that it is upper case
                if len(symbol) > 1 and symbol[1].isupper():
                    symbol = symbol[0]
                words = line[30:55].split()
                position = np.array(
                    [float(words[0]),
                     float(words[1]),
                     float(words[2])])
                atoms.append(Atom(symbol, position))
            except:
                pass

    return atoms
Beispiel #5
0
 def colored(self, elements):
     res = Atoms()
     res.set_cell(self.get_cell())
     for atom in self:
         elem = self.types[atom.tag]
         if elem in elements:
             elem = elements[elem]
         res.append(Atom(elem, atom.position))
     return res
Beispiel #6
0
def read_gpaw_text(fileobj, index=-1):
    if isinstance(fileobj, str):
        fileobj = open(fileobj)

    lines = fileobj.readlines()
    images = []
    while True:
        try:
            i = lines.index('Unit Cell:\n')
            cell = [float(line.split()[2]) for line in lines[i + 3:i + 6]]
            pbc = [line.split()[1] == 'yes' for line in lines[i + 3:i + 6]]
        except ValueError:
            pass

        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 = 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), 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

        images.append(atoms)
        lines = lines[i:]
Beispiel #7
0
 def colored(self, elements):
     res = Atoms()
     res.set_cell(self.get_cell())
     for atom in self:
         elem = self.types[atom.tag]
         if elem in elements:
             elem = elements[elem]
         res.append(Atom(elem, atom.position))
     return res
Beispiel #8
0
def build_atoms(positions, method, cell, alat):
    """Creates the atoms for a quantum espresso in file."""
    if method != "crystal":
        raise NotImplementedError("Only supported for crystal method of " "ATOMIC_POSITIONS, not %s." % method)
    atoms = Atoms()
    for el, (x, y, z) in positions:
        atoms.append(Atom(el, (x, y, z)))
    cell *= f2f(alat) * units.Bohr
    atoms.set_cell(cell, scale_atoms=True)
    return atoms
Beispiel #9
0
def build_atoms(positions, method, cell, alat):
    """Creates the atoms for a quantum espresso in file."""
    if method != 'crystal':
        raise NotImplementedError('Only supported for crystal method of '
                                  'ATOMIC_POSITIONS, not %s.' % method)
    atoms = Atoms()
    for el, (x, y, z) in positions:
        atoms.append(Atom(el, (x, y, z)))
    cell *= f2f(alat) * units.Bohr
    atoms.set_cell(cell, scale_atoms=True)
    return atoms
Beispiel #10
0
def make_atoms(index, lines, key, cell):
    """Scan through lines to get the atomic positions."""
    atoms = Atoms()
    if key == 'Cartesian axes':
        for line in lines[index + 3:]:
            entries = line.split()
            if len(entries) == 0:
                break
            symbol = entries[1][:-1]
            x = float(entries[6])
            y = float(entries[7])
            z = float(entries[8])
            atoms.append(Atom(symbol, (x, y, z)))
        atoms.set_cell(cell)
    elif key == 'ATOMIC_POSITIONS (crystal)':
        for line in lines[index + 1:]:
            entries = line.split()
            if len(entries) == 0 or (entries[0] == 'End'):
                break
            symbol = entries[0][:-1]
            x = float(entries[1])
            y = float(entries[2])
            z = float(entries[3])
            atoms.append(Atom(symbol, (x, y, z)))
        atoms.set_cell(cell, scale_atoms=True)
    # Energy is located after positions.
    energylines = [
        number for number, line in enumerate(lines)
        if ('!' in line and 'total energy' in line)
    ]
    energyline = min([n for n in energylines if n > index])
    energy = float(lines[energyline].split()[-2]) * units.Ry
    # Forces are located after positions.
    forces = np.zeros((len(atoms), 3))
    forcelines = [
        number for number, line in enumerate(lines)
        if 'Forces acting on atoms (Ry/au):' in line
    ]
    forceline = min([n for n in forcelines if n > index])
    for line in lines[forceline + 4:]:
        words = line.split()
        if len(words) == 0:
            break
        fx = float(words[-3])
        fy = float(words[-2])
        fz = float(words[-1])
        atom_number = int(words[1]) - 1
        forces[atom_number] = (fx, fy, fz)
    forces *= units.Ry / units.Bohr
    calc = SinglePointCalculator(atoms, energy=energy, forces=forces)
    atoms.set_calculator(calc)
    return atoms
Beispiel #11
0
def read_cmdft(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

    yield s
Beispiel #12
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
Beispiel #13
0
def make_atoms(index, lines, key, cell):
    """Scan through lines to get the atomic positions."""
    atoms = Atoms()
    if key == 'Cartesian axes':
        for line in lines[index + 3:]:
            entries = line.split()
            if len(entries) == 0:
                break
            symbol = entries[1][:-1]
            x = float(entries[6])
            y = float(entries[7])
            z = float(entries[8])
            atoms.append(Atom(symbol, (x, y, z)))
        atoms.set_cell(cell)
    elif key == 'ATOMIC_POSITIONS (crystal)':
        for line in lines[index + 1:]:
            entries = line.split()
            if len(entries) == 0 or (entries[0] == 'End'):
                break
            symbol = entries[0][:-1]
            x = float(entries[1])
            y = float(entries[2])
            z = float(entries[3])
            atoms.append(Atom(symbol, (x, y, z)))
        atoms.set_cell(cell, scale_atoms=True)
    # Energy is located after positions.
    energylines = [number for number, line in enumerate(lines) if
                   ('!' in line and 'total energy' in line)]
    energyline = min([n for n in energylines if n > index])
    energy = float(lines[energyline].split()[-2]) * units.Ry
    # Forces are located after positions.
    forces = np.zeros((len(atoms), 3))
    forcelines = [number for number, line in enumerate(lines) if
                  'Forces acting on atoms (Ry/au):' in line]
    forceline = min([n for n in forcelines if n > index])
    for line in lines[forceline + 4:]:
        words = line.split()
        if len(words) == 0:
            break
        fx = float(words[-3])
        fy = float(words[-2])
        fz = float(words[-1])
        atom_number = int(words[1]) - 1
        forces[atom_number] = (fx, fy, fz)
    forces *= units.Ry / units.Bohr
    calc = SinglePointCalculator(atoms, energy=energy, forces=forces)
    atoms.set_calculator(calc)
    return atoms
Beispiel #14
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('"'):
            s.append(Atom(w[0].replace('"',''), 
                          [float(w[3]), float(w[4]), float(w[5])]))
        else:
            finished = True

    return s
Beispiel #15
0
def read_proteindatabank(fileobj, index=-1):
    """Read PDB files."""

    if isinstance(fileobj, basestring):
        fileobj = open(fileobj)

    images = []
    orig = np.identity(3)
    trans = np.zeros(3)
    atoms = Atoms()
    for line in fileobj.readlines():
        if line.startswith('CRYST1'):
            cellpar = [float(word) for word in line[6:54].split()]
            atoms.set_cell(cellpar_to_cell(cellpar))
            atoms.pbc = True
        for c in range(3):
            if line.startswith('ORIGX' + '123'[c]):
                pars = [float(word) for word in line[10:55].split()]
                orig[c] = pars[:3]
                trans[c] = pars[3]

        if line.startswith('ATOM') or line.startswith('HETATM'):
            try:
                # Atom name is arbitrary and does not necessarily
                # contain the element symbol.  The specification
                # requires the element symbol to be in columns 77+78.
                symbol = line[76:78].strip().lower().capitalize()
                words = line[30:55].split()
                position = np.array(
                    [float(words[0]),
                     float(words[1]),
                     float(words[2])])
                position = np.dot(orig, position) + trans
                atoms.append(Atom(symbol, position))
            except Exception as ex:
                warnings.warn(
                    'Discarding atom when reading PDB file: {}'.format(ex))
        if line.startswith('ENDMDL'):
            images.append(atoms)
            atoms = Atoms()
    if len(images) == 0:
        images.append(atoms)
    return images[index]
Beispiel #16
0
def read_proteindatabank(fileobj, index=-1):
    """Read PDB files.

    The format is assumed to follow the description given in
    http://www.wwpdb.org/documentation/format32/sect8.html and
    http://www.wwpdb.org/documentation/format32/sect9.html."""
    if isinstance(fileobj, str):
        fileobj = open(fileobj)

    images = []
    orig = np.identity(3)
    trans = np.zeros(3)
    atoms = Atoms()
    for line in fileobj.readlines():
        if line.startswith('CRYST1'):
            cellpar = [float(word) for word in line[6:54].split()]
            atoms.set_cell(cellpar_to_cell(cellpar))
        for c in range(3):
            if line.startswith('ORIGX' + '123'[c]):
                pars = [float(word) for word in line[10:55].split()]
                orig[c] = pars[:3]
                trans[c] = pars[3]

        if line.startswith('ATOM') or line.startswith('HETATM'):
            try:
                # Atom name is arbitrary and does not necessarily
                # contain the element symbol.  The specification
                # requires the element symbol to be in columns 77+78.
                symbol = line[76:78].strip().lower().capitalize()
                words = line[30:55].split()
                position = np.array([float(words[0]),
                                     float(words[1]),
                                     float(words[2])])
                position = np.dot(orig, position) + trans
                atoms.append(Atom(symbol, position))
            except:
                pass
        if line.startswith('ENDMDL'):
            images.append(atoms)
            atoms = Atoms()
    if len(images) == 0:
        images.append(atoms)
    return images[index]
Beispiel #17
0
def read_pdb(fileobj, index=-1):
    """Read PDB files.

    The format is assumed to follow the description given in
    http://www.wwpdb.org/documentation/format32/sect8.html and
    http://www.wwpdb.org/documentation/format32/sect9.html."""
    if isinstance(fileobj, str):
        fileobj = open(fileobj)

    images = []
    orig = np.identity(3)
    trans = np.zeros(3)
    atoms = Atoms()
    for line in fileobj.readlines():
        if line.startswith('CRYST1'):
            cellpar = [float(word) for word in line[6:54].split()]
            atoms.set_cell(cellpar_to_cell(cellpar))
        for c in range(3):
            if line.startswith('ORIGX' + '123'[c]):
                pars = [float(word) for word in line[10:55].split()]
                orig[c] = pars[:3]
                trans[c] = pars[3]

        if line.startswith('ATOM') or line.startswith('HETATM'):
            try:
                # Atom name is arbitrary and does not necessarily contain the element symbol.
                # The specification requires the element symbol to be in columns 77+78.
                symbol = line[76:78].strip().lower().capitalize()
                words = line[30:55].split()
                position = np.array(
                    [float(words[0]),
                     float(words[1]),
                     float(words[2])])
                position = np.dot(orig, position) + trans
                atoms.append(Atom(symbol, position))
            except:
                pass
        if line.startswith('ENDMDL'):
            images.append(atoms)
            atoms = Atoms()
    if len(images) == 0:
        images.append(atoms)
    return images[index]
Beispiel #18
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_proteindatabank(fileobj, index=-1):
    """Read PDB files."""

    if isinstance(fileobj, basestring):
        fileobj = open(fileobj)

    images = []
    orig = np.identity(3)
    trans = np.zeros(3)
    atoms = Atoms()
    for line in fileobj.readlines():
        if line.startswith('CRYST1'):
            cellpar = [float(word) for word in line[6:54].split()]
            atoms.set_cell(cellpar_to_cell(cellpar))
            atoms.pbc = True
        for c in range(3):
            if line.startswith('ORIGX' + '123'[c]):
                pars = [float(word) for word in line[10:55].split()]
                orig[c] = pars[:3]
                trans[c] = pars[3]

        if line.startswith('ATOM') or line.startswith('HETATM'):
            try:
                # Atom name is arbitrary and does not necessarily
                # contain the element symbol.  The specification
                # requires the element symbol to be in columns 77+78.
                symbol = line[76:78].strip().lower().capitalize()
                words = line[30:55].split()
                position = np.array([float(words[0]),
                                     float(words[1]),
                                     float(words[2])])
                position = np.dot(orig, position) + trans
                atoms.append(Atom(symbol, position))
            except Exception as ex:
                warnings.warn('Discarding atom when reading PDB file: {}'
                              .format(ex))
        if line.startswith('ENDMDL'):
            images.append(atoms)
            atoms = Atoms()
    if len(images) == 0:
        images.append(atoms)
    return images[index]
Beispiel #20
0
def convertXYZtoASE(filename):
    with open(filename,'r') as f:
        # Read number of atoms (line 4)
        n_atoms = string.atoi(string.split(f.readline())[0])

        # Read remaining lines
        atoms = Atoms()
        for line in f.readlines():
            if len(line)>5: # Ignore blank lines
                data = string.split(line)
                if len(data)==4:
                    symbol = data[0]
                    position = np.array([string.atof(data[1+j]) for j in range(3)])
                    atom = Atom(symbol=symbol, position=position)
                    atoms.append(atom)

    if len(atoms) != n_atoms:
        print 'SiestaIO.ReadXYZFile: Inconstency in %s detected!' %filename

    return atoms
Beispiel #21
0
def xyz_to_nuclei_fod(ase_atoms):
    # this function is from the pyflosic flosic.py routine
    # get the number of atoms + fod
    syssize = len(ase_atoms)
    # Determine, if fod is included or not.
    # A fod is included, if it is no closer then fodeps to another fod.
    fodeps = 0.1
    included = []
    nombernotincluded = 0
    dist = -1.0
    for i in range(0, syssize):
        # Cores are always included.
        if ase_atoms[i].symbol != 'X' and ase_atoms[i].symbol != 'He':
            included.append(1)
        if ase_atoms[i].symbol == 'X' or ase_atoms[i].symbol == 'He':
            distold = fodeps
            for j in range(0, i):
                dist = np.sqrt(
                    (ase_atoms[i].position[0] - ase_atoms[j].position[0])**2 +
                    (ase_atoms[i].position[1] - ase_atoms[j].position[1])**2 +
                    (ase_atoms[i].position[2] - ase_atoms[j].position[2])**2)
                if dist < distold and included[j] == 1:
                    distold = dist
            if distold < fodeps:
                included.append(0)
                nombernotincluded += 1
            else:
                included.append(1)
    # process fods and cores separetely.
    tmp = []
    nuclei = Atoms([])
    fod1 = Atoms([])
    fod2 = Atoms([])
    nrofnuclei = 0
    for i in range(0, syssize):
        if ase_atoms[i].symbol != 'X':
            nrofnuclei = nrofnuclei + 1
        if ase_atoms[i].symbol == 'X':
            break
    for i in range(0, syssize):
        if i < nrofnuclei:
            tmp.append(ase_atoms[i].symbol + ' ' +
                       str(ase_atoms[i].position[0]) + ' ' +
                       str(ase_atoms[i].position[1]) + ' ' +
                       str(ase_atoms[i].position[2]) + '\n')
            nuclei.append(ase_atoms[i])
        elif ase_atoms[i].symbol == 'X':
            fod1.append(ase_atoms[i])
            if included[i] == 1:
                tmp.append('ghost1' + ' ' + str(ase_atoms[i].position[0]) +
                           ' ' + str(ase_atoms[i].position[1]) + ' ' +
                           str(ase_atoms[i].position[2]) + '\n')
        elif ase_atoms[i].symbol == 'He':
            fod2.append(ase_atoms[i])
            if included[i] == 1:
                tmp.append('ghost2' + ' ' + str(ase_atoms[i].position[0]) +
                           ' ' + str(ase_atoms[i].position[1]) + ' ' +
                           str(ase_atoms[i].position[2]) + '\n')
    geo = ''.join(tmp)
    return geo, nuclei, fod1, fod2, included
Beispiel #22
0
def get_atoms_from_xml_output(filename, schema=None, output=None):
    """
    Returns an Atoms object constructed from an XML QE file (according to the schema).

    :param filename: Name of the XML file to read from or a file descriptor.
    :param schema: Optional XML Schema file to use (for default schema is \
    found using the schemaLocation attribute of the XML root).
    :param output: Optional dictionary containing the output tree of the XML file. \
    If provided skips file access and builds Atoms object directly from output dictionary.
    :return: An Atoms object.
    """
    if output is None:
        output = xmlschema.to_dict(filename,
                                   schema=schema,
                                   path="./qes:espresso/output")
    a1 = np.array(output["atomic_structure"]["cell"]["a1"])
    a2 = np.array(output["atomic_structure"]["cell"]["a2"])
    a3 = np.array(output["atomic_structure"]["cell"]["a3"])
    a_p = (output["atomic_structure"]["atomic_positions"]["atom"])

    atoms = Atoms()

    # First define the unit cell from a1, a2, a3 and alat
    cell = np.zeros((3, 3))
    cell[0] = a1
    cell[1] = a2
    cell[2] = a3
    atoms.set_cell(cell)

    # Now the atoms in the unit cell
    for atomx in a_p:
        # TODO: extend to all possible cases the symbol splitting (for now, only numbering up to 9 work). Not a very common case...
        symbol = split_atomic_symbol(atomx['@name'])[0]
        x = float(atomx['$'][0])
        y = float(atomx['$'][1])
        z = float(atomx['$'][2])
        atoms.append(Atom(symbol, (x, y, z)))

    return atoms
Beispiel #23
0
def read_struct(fname):
    """Read a siesta struct file"""
    from ase.atoms import Atoms, Atom

    f = open(fname, 'r')

    cell = []
    for i in range(3):
        cell.append([float(x) for x in f.readline().split()])

    natoms = int(f.readline())

    atoms = Atoms()
    for atom in f:
        Z, pos_x, pos_y, pos_z = atom.split()[1:]
        atoms.append(Atom(int(Z), position = (float(pos_x), float(pos_y), float(pos_z))))

    if len(atoms) != natoms:
        raise IOError('Badly structured input file')
    
    atoms.set_cell(cell, scale_atoms = True)

    return atoms
Beispiel #24
0
def read_struct_out(fname):
    """Read a siesta struct file"""
    from ase.atoms import Atoms, Atom

    f = fname

    cell = []
    for i in range(3):
        cell.append([float(x) for x in f.readline().split()])

    natoms = int(f.readline())

    atoms = Atoms()
    for atom in f:
        Z, pos_x, pos_y, pos_z = atom.split()[1:]
        atoms.append(Atom(int(Z),
                          position=(float(pos_x), float(pos_y), float(pos_z))))

    if len(atoms) != natoms:
        raise IOError('Badly structured input file')

    atoms.set_cell(cell, scale_atoms=True)

    return atoms
Beispiel #25
0
def get_on_opt(f_nuc,f_fod1,f_fod2):
    nuc = read(f_nuc)
    fod1 = read(f_fod1)
    fod2 = read(f_fod2)
    geo,nuclei,init_fod1,init_fod2,included =xyz_to_nuclei_fod(nuc)

    #write('final0.xyz',fod1,'xyz')
    #write('final1.xyz',fod2,'xyz')
    #print(nuclei.get_positions())
    #print(fod1.get_positions())

    comb = Atoms([])
    for n in nuclei:
        comb.append(n)
    for f1 in fod1:
        comb.append(f1)
    for f2 in fod2:
        comb.append(f2)
    write('final_ON.xyz',comb,'xyz')
Beispiel #26
0
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, 'Fermi Level:')
        except ValueError:
            eFermi = None
        else:
            try:
                eFermi = float(lines[ii].split()[2])
            except ValueError:  # we have two Fermi levels
                fields = lines[ii].split()

                def strip(string):
                    for rubbish in '[],':
                        string = string.replace(rubbish, '')
                    return string

                eFermi = [float(strip(fields[2])), float(strip(fields[3]))]
        # read Eigenvalues and occupations
        ii1 = ii2 = 1e32
        try:
            ii1 = index_startswith(lines, ' Band   Eigenvalues  Occupancy')
        except ValueError:
            pass
        try:
            ii2 = index_startswith(lines, ' Band  Eigenvalues  Occupancy')
        except ValueError:
            pass
        ii = min(ii1, ii2)
        if ii == 1e32:
            kpts = None
        else:
            ii += 1
            words = lines[ii].split()
            vals = []
            while (len(words) > 2):
                vals.append([float(word) for word in words])
                ii += 1
                words = lines[ii].split()
            vals = np.array(vals).transpose()
            kpts = [SinglePointKPoint(0, 0)]
            kpts[0].eps_n = vals[1]
            kpts[0].f_n = vals[2]
            if vals.shape[0] > 3:
                kpts.append(SinglePointKPoint(0, 1))
                kpts[1].eps_n = vals[3]
                kpts[1].f_n = vals[4]
        # read charge
        try:
            ii = index_startswith(lines, 'Total Charge:')
        except ValueError:
            q = None
        else:
            q = float(lines[ii].split()[2])
        # read dipole moment
        try:
            ii = index_startswith(lines, 'Dipole Moment:')
        except ValueError:
            dipole = None
        else:
            line = lines[ii].replace(']', '').replace('[', '')
            dipole = np.array([float(c) for c in line.split()[-3:]])

        try:
            ii = index_startswith(lines, 'Local Magnetic Moments')
        except ValueError:
            magmoms = None
        else:
            magmoms = []
            for i in range(ii + 1, ii + 1 + len(atoms)):
                iii, magmom = lines[i].split()[:2]
                magmoms.append(float(magmom))
        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), 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:
            calc = SinglePointDFTCalculator(e, f, None, magmoms, atoms, eFermi)
            if kpts is not None:
                calc.kpts = kpts
            if dipole is not None:
                calc.set_dipole_moment(dipole)
            atoms.set_calculator(calc)
        if q is not None and len(atoms) > 0:
            n = len(atoms)
            atoms.set_charges([q / n] * n)

        images.append(atoms)
        lines = lines[i:]
Beispiel #27
0
def xyz_to_nuclei_fod(ase_atoms):

    # Get the number of atoms and fods.

    syssize = len(ase_atoms)

    # First the preparations for enabling the usage of GHOST atoms are done.
    # We need to determine if a FOD is included in the creation of the GHOST atoms or 
    # not. If two GHOST atoms are created very close to each other (or other atoms) this 
    # will result in the DFT calculation crashing. A FOD is included if it is no closer 
    # then fodeps to another FOD or a nuclei.

    fodeps = 0.1
    included = []
    numbernotincluded = 0
    dist = -1.0

    # Iterate over FODs and nuclei positions.

    for i in range(0,syssize):

        # Nuclei are always included, obviously. NOTE: FODs for spin0 are given with an 
        # X, the for spin1 with an He symbol.

        if ase_atoms[i].symbol != 'X' and ase_atoms[i].symbol != 'He':
            included.append(1)
        if ase_atoms[i].symbol == 'X' or ase_atoms[i].symbol == 'He':

        # For every FOD the distance to every included FOD and the nuclei is calculated 
        # in order to determine whether or not it has to be included.

            distold = fodeps
            for j in range(0,i):
                dist = np.sqrt((ase_atoms[i].position[0]-ase_atoms[j].position[0])**2+(ase_atoms[i].position[1]-ase_atoms[j].position[1])**2+(ase_atoms[i].position[2]-ase_atoms[j].position[2])**2)
                if dist < distold and included[j]==1:
                    distold = dist

        # If the smallest distance is smaller than fodeps, the FOD will not be included. 
        # Elsewise it is included.

            if distold < fodeps:
                included.append(0)
                numbernotincluded += 1
            else:
                included.append(1)

    # Now the actual splitting is done.
    # These arrays will hold nuclei and FODs (for each spin channel separately).


    nuclei = Atoms([])
    fod1 = Atoms([])
    fod2 = Atoms([])
    nrofnuclei = 0

    # tmp will be used to create the list that can be used to enable GHOST atoms.

    tmp = []

    # Determine where to split.

    for i in range(0,syssize):
        if ase_atoms[i].symbol != 'X':
            nrofnuclei = nrofnuclei + 1

        # If the first FOD is found, nuclei assigning will be done.

        if ase_atoms[i].symbol == 'X':
            break

    # Split everything. 

    for i in range(0,syssize):

        # Assign the nuclei.

        if i < nrofnuclei:
            tmp.append(ase_atoms[i].symbol+' '+str(ase_atoms[i].position[0])+' '+str(ase_atoms[i].position[1])+' '+str(ase_atoms[i].position[2])+'\n')
            nuclei.append(ase_atoms[i])

        # Assing FODs for spin0.    

        elif ase_atoms[i].symbol == 'X':
            fod1.append(ase_atoms[i])
            if included[i] == 1:
                tmp.append('ghost1'+' '+str(ase_atoms[i].position[0])+' '+str(ase_atoms[i].position[1])+' '+str(ase_atoms[i].position[2])+'\n')

        # Assign FODs for spin1.        

        elif ase_atoms[i].symbol == 'He':
            fod2.append(ase_atoms[i])
            if included[i] == 1:
                tmp.append('ghost2'+' '+str(ase_atoms[i].position[0])+' '+str(ase_atoms[i].position[1])+' '+str(ase_atoms[i].position[2])+'\n')

    # geo holds both nuclei and GHOST atoms at FOD positions.        

    geo = ''.join(tmp)
    # geo can now be used for calculation with GHOST atoms, nuclei and fod1/fod2 for 
    # every other calculation.

    return geo,nuclei,fod1,fod2,included
Beispiel #28
0
def xyz_to_database(xyzfile,
                    num_examples=None,
                    xyz_format='xyz',
                    verbose=True,
                    unit_to_ev=None,
                    restart=False):
    """
  Convert the xyz file to an `ase.db.core.Database`.

  Args:
    xyzfile: a `str` as the file to parse.
    num_examples: a `int` as the maximum number of examples to parse. If None,
      all examples in the given file will be saved.
    xyz_format: a `str` representing the format of the given xyz file.
    verbose: a `bool` indicating whether we should log the parsing progress.
    unit_to_ev: a `float` as the unit for converting energies to eV. Defaults
      to None so that default units will be used.
    restart: a `bool`. If True, the database will be re-built even if already
      existed.

  Returns:
    database: an `ase.db.core.Database`.
    auxdict: a `dict` as the auxiliary dict for this database.

  """
    if xyz_format.lower() == 'ase':
        formatter = _ase_xyz
    else:
        formatter = _xyz

    dbfile = "{}.db".format(splitext(xyzfile)[0])
    if isfile(dbfile):
        if restart:
            remove(dbfile)
        else:
            auxdict = _load_auxiliary_dict(dbfile)
            return connect(name=dbfile), auxdict

    unit = unit_to_ev or formatter.default_unit
    parse_forces = formatter.parse_forces
    count = 0
    ai = 0
    natoms = 0
    stage = 0
    atoms = None
    num_examples = num_examples or 0
    natoms_counter = Counter()
    y_min = np.inf
    y_max = -np.inf
    max_occurs = Counter()

    database = connect(name=dbfile)
    tic = time.time()
    if verbose:
        sys.stdout.write("Extract cartesian coordinates ...\n")
    with open(xyzfile) as f:
        for line in f:
            if num_examples and count == num_examples:
                break
            line = line.strip()
            if line == "":
                continue
            if stage == 0:
                if line.isdigit():
                    natoms = int(line)
                    atoms = Atoms(calculator=ProvidedCalculator())
                    if parse_forces:
                        atoms.info['provided_forces'] = np.zeros((natoms, 3))
                    natoms_counter[natoms] += 1
                    stage += 1
            elif stage == 1:
                m = formatter.energy_patt.search(line)
                if m:
                    if xyz_format.lower() == 'extxyz':
                        energy = float(m.group(3)) * unit
                    elif xyz_format.lower() == 'ase':
                        energy = float(m.group(2)) * unit
                        atoms.set_cell(
                            np.reshape([float(x) for x in m.group(1).split()],
                                       (3, 3)))
                        atoms.set_pbc([
                            True if x == "T" else False
                            for x in m.group(3).split()
                        ])
                    else:
                        energy = float(m.group(1)) * unit
                    atoms.info['provided_energy'] = energy
                    y_min = min(y_min, energy)
                    y_max = max(y_max, energy)
                    stage += 1
            elif stage == 2:
                m = formatter.string_patt.search(line)
                if m:
                    atoms.append(
                        Atom(symbol=m.group(1),
                             position=[float(v) for v in m.groups()[1:4]]))
                    if parse_forces:
                        atoms.info['provided_forces'][ai, :] = [
                            float(v) * unit for v in m.groups()[4:7]
                        ]
                    ai += 1
                    if ai == natoms:
                        atoms.calc.calculate()
                        database.write(atoms)
                        counter = Counter(atoms.get_chemical_symbols())
                        for symbol, n in counter.items():
                            max_occurs[symbol] = max(max_occurs[symbol], n)
                        ai = 0
                        stage = 0
                        count += 1
                        if verbose and count % 1000 == 0:
                            sys.stdout.write(
                                "\rProgress: {:7d}  /  {:7d} | Speed = {:.1f}".
                                format(count, num_examples,
                                       count / (time.time() - tic)))
        if verbose:
            print("")
            print("Total time: %.3f s\n" % (time.time() - tic))

        # Dump the auxiliary dict.
        auxdict = {
            "max_occurs": dict(max_occurs),
            "y_range": [y_min, y_max],
            "natoms_counter": dict(natoms_counter)
        }
        _save_auxiliary_dict(dbfile, auxdict)

        return database, auxdict
Beispiel #29
0
def lewis2xyz(lewis,f='SMILES',aromatic=False,dim=2,addH=True):
        # Input 
        #       lewis 		... 	smiles format string 
	#	aromatic 	... 	is the system aromatic 
	#	dim		...	2d == 2 and 3d == 3 
        # Output 
        #       Bonding information 
        #       Lewis pictures
        #       sdf and xyz files 

	if f == 'pdb':
		m = Chem.MolFromPDBFile(lewis)
	if f == 'xyz':
		struct = read(lewis)
		write('lewis.pdb',struct,'proteindatabank')
		m = Chem.MolFromPDBFile(lewis)
       	if f == 'SMILES':
		m = Chem.MolFromSmiles(lewis)
	
	# Change armatic bonds to alternate single and double bonds 
	if aromatic == True:	
		if m.GetBondWithIdx(0).GetIsAromatic() == True:
       			Chem.Kekulize(m)

	if addH == True:	
		m=Chem.AddHs(m) 
	if dim == 1 or dim == 2: 
                AllChem.Compute2DCoords(m)
	if dim == 3:
                AllChem.EmbedMolecule(m)
		AllChem.UFFOptimizeMolecule(m)

	fods = Atoms()
       	# core electrons  
	for atom in m.GetAtoms():
		idx = atom.GetIdx()
		# atomic number 
		N = m.GetAtomWithIdx(idx).GetAtomicNum()
		# number of valence electrons 
		V = m.GetAtomWithIdx(idx).GetTotalValence()
		# number of radicals 
		R = atom.GetNumRadicalElectrons()
		# formal charge of the atom 
		F = atom.GetFormalCharge()
		# number of core electrons 
		C = N-V-R-F
		# lone pairs  
		#L = 0
		#bonds = atom.GetBonds()
		#for b in range(len(bonds)):
		#	bond_type = bonds[b].GetBondType()
		#	if str(bond_type) == 'SINGLE':
		#		L = L + 1 		
		#	if str(bond_type) == 'DOUBLE':
		#		L = L + 2 
		#	if str(bond_type) == 'TRIPLE':
                #                L = L + 3
		#print(V-L)
		if C > 0:
			pos = m.GetConformer().GetAtomPosition(idx)
			offset = 0.002
			if C == 1:
				fods.append(Atom('X',[pos[0],pos[1],pos[2]]))			
			if C == 2:
				fods.append(Atom('X',[pos[0],pos[1],pos[2]]))
				fods.append(Atom('He',[pos[0]+offset,pos[1]+offset,pos[2]+offset]))
			if C == 3:
				fods.append(Atom('X',[pos[0],pos[1],pos[2]]))
                                fods.append(Atom('He',[pos[0]+offset,pos[1]+offset,pos[2]+offset])) 
				fods.append(Atom('X',[pos[0]+offset,pos[1]+offset,pos[2]+4*offset]))
			if C == 4:
                                fods.append(Atom('X',[pos[0],pos[1],pos[2]]))
                                fods.append(Atom('He',[pos[0]+offset,pos[1]+offset,pos[2]+offset]))
                                fods.append(Atom('X',[pos[0]+offset,pos[1]+offset,pos[2]+40*offset]))
				fods.append(Atom('He',[pos[0]+offset,pos[1]+offset,pos[2]-40*offset]))
			if C == 5:
                                fods.append(Atom('X',[pos[0],pos[1],pos[2]]))
                                fods.append(Atom('He',[pos[0]+offset,pos[1]+offset,pos[2]+offset]))
                                fods.append(Atom('X',[pos[0]+offset,pos[1]+offset,pos[2]+40*offset]))
                                fods.append(Atom('He',[pos[0]+offset,pos[1]+offset,pos[2]-40*offset]))
				fods.append(Atom('X',[pos[0]+40*offset,pos[1]+40*offset,pos[2]+offset]))		
			if C == 6: 
                                fods.append(Atom('X',[pos[0],pos[1],pos[2]]))
                                fods.append(Atom('He',[pos[0]+offset,pos[1]+offset,pos[2]+offset]))
                                
                                fods.append(Atom('X',[pos[0]+40*offset,pos[1]+40*offset,pos[2]+40*offset]))
                                fods.append(Atom('He',[pos[0]+offset,pos[1]-40*offset,pos[2]-40*offset]))
                                fods.append(Atom('X',[pos[0]-40*offset,pos[1]+40*offset,pos[2]-40*offset]))
				fods.append(Atom('He',[pos[0]-40*offset,pos[1]-40*offset,pos[2]+40*offset]))

			if C == 7:
                                fods.append(Atom('X',[pos[0],pos[1],pos[2]]))
                                fods.append(Atom('He',[pos[0]+offset,pos[1]+offset,pos[2]+offset]))

                                fods.append(Atom('X',[pos[0]+40*offset,pos[1]+40*offset,pos[2]+40*offset]))
                                fods.append(Atom('He',[pos[0]+offset,pos[1]-40*offset,pos[2]-40*offset]))
                                fods.append(Atom('X',[pos[0]-40*offset,pos[1]+40*offset,pos[2]-40*offset]))
                                fods.append(Atom('He',[pos[0]-40*offset,pos[1]-40*offset,pos[2]+40*offset]))

				fods.append(Atom('He',[pos[0]+42*offset,pos[1]+42*offset,pos[2]+42*offset]))
			
			if C == 8:
                                fods.append(Atom('X',[pos[0],pos[1],pos[2]]))
                                fods.append(Atom('He',[pos[0]+offset,pos[1]+offset,pos[2]+offset]))

                                fods.append(Atom('X',[pos[0]+40*offset,pos[1]+40*offset,pos[2]+40*offset]))
                                fods.append(Atom('He',[pos[0]+40*offset,pos[1]-40*offset,pos[2]-40*offset]))
                                fods.append(Atom('X',[pos[0]-40*offset,pos[1]+40*offset,pos[2]-40*offset]))
                                fods.append(Atom('He',[pos[0]-40*offset,pos[1]-40*offset,pos[2]+40*offset]))

                                fods.append(Atom('He',[pos[0]+42*offset,pos[1]+42*offset,pos[2]+42*offset]))
				fods.append(Atom('X',[pos[0]+42*offset,pos[1]-42*offset,pos[2]-42*offset]))


	# find radicals 
	for atom in m.GetAtoms():
		rad = atom.GetNumRadicalElectrons()
		#print(rad)
		if rad == 1:
			idx = atom.GetIdx()
			pos = m.GetConformer().GetAtomPosition(idx)
			bonds = atom.GetBonds()
			vec_BA_x = 0
			vec_BA_y = 0 
			vec_BA_z = 0
			BA = []
			for b in range(len(bonds)):
                		ai = bonds[b].GetBeginAtomIdx()
                		aj = bonds[b].GetEndAtomIdx()
                		bond_type = bonds[b].GetBondType()

                		# Positions 
                		pos_ai = m.GetConformer().GetAtomPosition(ai)
                		pos_aj = m.GetConformer().GetAtomPosition(aj)
		
				# adding bonding vectors for 3d 	
				vec_BA_x = vec_BA_x + (pos_aj[0]-pos_ai[0])
				vec_BA_y = vec_BA_y + (pos_aj[1]-pos_ai[1])
				vec_BA_z = vec_BA_z + (pos_aj[2]-pos_ai[2]) 
				# save bonding vectors for 2d 
				BA.append(np.array([(pos_aj[0]-pos_ai[0]),(pos_aj[1]-pos_ai[1]),(pos_aj[2]-pos_ai[2])]))
			
			if dim == 1:
				fods.append(Atom('X',[-1*BA[0][0],-1*BA[0][1],-1*BA[0][2]]))	
			if dim == 2: 
				vec = np.cross(BA[0],BA[1])
				norm = np.sqrt(vec[0]**2 + vec[1]**2 + vec[2]**2)
				pos = vec/norm 
				fods.append(Atom('X',[pos[0],pos[1],pos[2]]))

			if dim == 3:
				norm = np.sqrt(vec_BA_x**2 + vec_BA_y**2 + vec_BA_z**2)	
				pos = np.array([-1*vec_BA_x,-1*vec_BA_y,-1*vec_BA_z])/norm
				fods.append(Atom('X',[pos[0],pos[1],pos[2]])) 
			


        bonds = m.GetBonds()
        for b in range(len(bonds)):
                ai = bonds[b].GetBeginAtomIdx()
                aj = bonds[b].GetEndAtomIdx()
                bond_type = bonds[b].GetBondType()

                # Positions 
                pos_ai = m.GetConformer().GetAtomPosition(ai)
		pos_aj = m.GetConformer().GetAtomPosition(aj)
                # Bondlength 
                #print('%0.5f' % np.linalg.norm([pos_ai[0]-pos_aj[0],pos_ai[1]-pos_aj[1],pos_ai[2]-pos_aj[2]]))
                # Middle of the vector 
                MP = [(pos_ai[0]+pos_aj[0])/2.,(pos_ai[1]+pos_aj[1])/2.,(pos_ai[2]+pos_aj[2])/2.]
                #print('%0.5f %0.5f %0.5f' %(MP[0],MP[1],MP[2]))
                # Print positions 
                #print('%0.5f %0.5f %0.5f' %(pos_ai[0],pos_ai[1],pos_ai[2]))
                #print('%0.5f %0.5f %0.5f' %(pos_aj[0],pos_aj[1],pos_aj[2]))

                for spin in ['He','X']:
                        if spin == 'He':
                                offset = 0.000
                        if spin == 'X':
                                #offset = 0.002
				offset = 0.02 
                        if str(bond_type) == 'SINGLE':
                                #print('%s %0.5f %0.5f %0.5f' %(spin,MP[0]+offset,MP[1]+offset,MP[2]+offset))
                                fods.append(Atom(spin,[MP[0]+offset,MP[1]+offset,MP[2]+offset]))

                        if str(bond_type) == 'DOUBLE':
                                #print('%s %0.5f %0.5f %0.5f' %(spin,MP[0]+offset,MP[1]+offset,MP[2]+0.5+offset))
                                #print('%s %0.5f %0.5f %0.5f' %(spin,MP[0]+offset,MP[1]+offset,MP[2]-0.5+offset))
                                fods.append(Atom(spin,[MP[0]+offset,MP[1]+offset,MP[2]+0.5+offset]))
                                fods.append(Atom(spin,[MP[0]+offset,MP[1]+offset,MP[2]-0.5+offset]))

                        if str(bond_type) == 'TRIPLE':
                                #print('%s %0.5f %0.5f %0.5f' %(spin,MP[0]+offset,MP[1]+offset,MP[2]+0.5+offset))
                                #print('%s %0.5f %0.5f %0.5f' %(spin,MP[0]+offset,MP[1]+offset,MP[2]-0.5+offset))
                                #print('%s %0.5f %0.5f %0.5f' %(spin,MP[0]-0.5+offset,MP[1]+offset,MP[2]+offset))
                                fods.append(Atom(spin,[MP[0]+offset,MP[1]+offset,MP[2]+0.5+offset]))
                                fods.append(Atom(spin,[MP[0]+offset,MP[1]+offset,MP[2]-0.5+offset]))
                                fods.append(Atom(spin,[MP[0]-0.5+offset,MP[1]+offset,MP[2]+offset]))

                w = Chem.SDWriter('%s.sdf' % 'pylewis')
                w.write(m)
                w.flush()

                # sdf2xyz       
                struct = read('%s.sdf' % 'pylewis')
                struct_fods = Atoms()
                struct_fods.extend(struct)
                struct_fods.extend(fods)

                #print(struct.get_chemical_symbols())
                write('%s.xyz' % 'pylewis',struct,'xyz')
                write('%s_fods.xyz' % 'pylewis',struct_fods,'xyz')
Beispiel #30
0
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, 'Fermi Level:')
        except ValueError:
            eFermi = None
        else:
            try:
                eFermi = float(lines[ii].split()[2])
            except ValueError: # we have two Fermi levels
                fields = lines[ii].split()
                def strip(string):
                    for rubbish in '[],':
                        string = string.replace(rubbish, '')
                    return string
                eFermi = [float(strip(fields[2])),
                          float(strip(fields[3])) ]
        # read Eigenvalues and occupations
        ii1 = ii2 = 1e32
        try:
            ii1 = index_startswith(lines, ' Band   Eigenvalues  Occupancy')
        except ValueError:
            pass
        try:
            ii2 = index_startswith(lines, ' Band  Eigenvalues  Occupancy')
        except ValueError:
            pass
        ii = min(ii1, ii2)
        if ii == 1e32:
            kpts = None
        else:
            ii += 1
            words = lines[ii].split()
            vals = []
            while(len(words) > 2):
                vals.append([float(word) for word in words])
                ii += 1
                words = lines[ii].split()
            vals = np.array(vals).transpose()
            kpts = [SinglePointKPoint(0, 0)]
            kpts[0].eps_n = vals[1]
            kpts[0].f_n = vals[2]
            if vals.shape[0] > 3:
                kpts.append(SinglePointKPoint(0, 1))
                kpts[1].eps_n = vals[3]
                kpts[1].f_n = vals[4]
        # read charge
        try:
            ii = index_startswith(lines, 'Total Charge:')
        except ValueError:
            q = None
        else:
            q = float(lines[ii].split()[2])
        try:
            ii = index_startswith(lines, 'Local Magnetic Moments')
        except ValueError:
            magmoms = None
        else:
            magmoms = []
            for i in range(ii + 1, ii + 1 + len(atoms)):
                iii, magmom = lines[i].split()[:2]
                magmoms.append(float(magmom))
        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), 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:
            calc = SinglePointDFTCalculator(e, f, None, magmoms, atoms, eFermi)
            if kpts is not None:
                calc.kpts = kpts
            atoms.set_calculator(calc)
        if q is not None and len(atoms) > 0:
            n = len(atoms)
            atoms.set_charges([q / n] * n)

        images.append(atoms)
        lines = lines[i:]
Beispiel #31
0
def make_Td(names, sizes):

    #work out the expansion factor
    factor = sum(sizes)
    #factor = 3.0

    Td = Atoms()
    Td.append(Atom('F', position=(0, 0, 0)))
    Td.append(Atom('N', position=(0, 0, factor)))
    #Atom 3
    Td.append(Atom('N', position=(1.0, 0, 0.0)))
    Td.set_angle([1, 0, 2], 1.91062939)
    Td.set_distance(0, 2, factor, fix=0)
    #Atom 4
    Td.append(Atom('N', position=(1.0, 0, 0.0)))
    Td.set_distance(0, 3, factor, fix=0)
    Td.set_angle([1, 0, 3], 1.91062939, mask=None)
    Td.set_dihedral([2, 1, 0, 3], 2.0943951, mask=None)
    #print Td.get_angle([1,0,2])
    #Atom 5
    Td.append(Atom('N', position=(1.0, 0, 0)))
    Td.set_angle([1, 0, 4], 1.91062939)
    Td.set_dihedral([2, 1, 0, 4], -2.0943951)
    Td.set_distance(0, 4, factor, fix=0)

    #find side centroids
    tmp1 = Td[1:4]
    coside = tmp1.get_center_of_mass()
    Td.append(Atom('C', position=coside))
    #
    tmp1 = Td[2:5]
    coside = tmp1.get_center_of_mass()
    Td.append(Atom('C', position=coside))
    #
    tmp1 = Td[3:5]
    tmp1.append(Td[1])
    coside = tmp1.get_center_of_mass()
    Td.append(Atom('C', position=coside))
    #
    tmp1 = Td[1:3]
    tmp1.append(Td[4])
    coside = tmp1.get_center_of_mass()
    Td.append(Atom('C', position=coside))

    write('td.xyz', Td)

    # Now we make tags etc

    ## C -> triangles (faces), N => triangle caps

    eps = 0.05
    model_molecule = {}
    n_obj = -1  #initialise to -1 such that it can be indexed at start of add
    n_tags = 0

    #First detect global bonding
    cov_rad = []
    for atom in Td:
        #cov_rad.append(covalent_radii[atom.number])
        cov_rad.append(factor / 2)

    nlist = NeighborList(cov_rad,
                         skin=0.1,
                         self_interaction=False,
                         bothways=True)
    nlist.build(Td)

    nbond = 1
    bond_matrix = np.zeros((len(Td), len(Td)))
    pbc_bond_matrix = np.zeros((len(Td), len(Td)))

    bond_dict = {}

    for atom in Td:
        print "---------------------"
        indices, offsets = nlist.get_neighbors(atom.index)
        for index, offset in zip(indices, offsets):
            print atom.index, index, atom.symbol, Td[
                index].symbol, offset, Td.get_distance(atom.index,
                                                       index,
                                                       mic=True)
            if atom.index < index:
                this_bond = [atom.index, index]
                for o in offset:
                    this_bond.append(o)
                bond = tuple(this_bond)
                print bond
                bond_dict[bond] = nbond
                #Now we need to find the same bond the other direction to get the offsets right
                indices2, offsets2 = nlist.get_neighbors(index)
                for i2, o2 in zip(indices2, offsets2):
                    if i2 == atom.index:
                        #print "sum of offsets = ", offset, o2, sum(offset + o2)
                        if sum(offset + o2) == 0:  #same bond
                            this_bond = [index, atom.index]
                            for o in o2:
                                this_bond.append(o)
                            bond = tuple(this_bond)
                            print bond
                            bond_dict[bond] = nbond
                nbond += 1

    print nbond
    for k, v in bond_dict.items():
        print k, v

    #Now we look for all the things with unit*factor bondlengths
    #Start with N (rectangles)
    for atom in Td:
        if atom.symbol == 'N':
            print '======================================='
            print 'N Atom ', atom.index
            n_obj += 1
            model_molecule[n_obj] = Atoms()
            model_molecule[n_obj] += atom
            model_molecule[n_obj][0].original_index = atom.index
            model_molecule[n_obj][0].symbol = 'F'
            indices, offsets = nlist.get_neighbors(atom.index)
            #Just checking the symbols here
            print indices
            print offsets
            symbols = ([Td[index].symbol for index in indices])
            symbol_string = ''.join(
                sorted([Td[index].symbol for index in indices]))
            #print symbol_string
            #for i,o in zip(indices, offsets):
            #    print i,o
            for index, offset in zip(indices, offsets):
                print atom.index, index, offset
                this_bond = [atom.index, index]
                for o in offset:
                    this_bond.append(o)
                bond = tuple(this_bond)
                print bond_dict[bond]
                if Td[index].symbol == 'C':
                    #By definition, we can't be going over a periodic boundary (no pbc)
                    model_molecule[n_obj] += Td[index]
                    model_molecule[n_obj][-1].tag = bond_dict[bond]
                    model_molecule[n_obj].positions[-1] = Td.positions[index]

    n_centers = n_obj

    ##Now we do the  C  (triangles)
    for atom in Td:
        if atom.symbol == 'C':
            #print'======================================='
            #print 'N Atom ',atom.index, " finding squares1"
            n_obj += 1
            model_molecule[n_obj] = Atoms()
            model_molecule[n_obj] += atom
            indices, offsets = nlist.get_neighbors(atom.index)
            for index, offset in zip(indices, offsets):
                print index, offset, Td[index].symbol
                this_bond = [atom.index, index]
                for o in offset:
                    this_bond.append(o)
                bond = tuple(this_bond)
                #if not bond_dict.has_key(bond):
                #Then
                if Td[index].symbol == 'N':
                    #By definition, we can't be going over a periodic boundary (no pbc)
                    model_molecule[n_obj] += Td[index]
                    model_molecule[n_obj][-1].tag = bond_dict[bond]
                    model_molecule[n_obj].positions[-1] = Td.positions[index]

    f = open('Td.model', 'w')
    g = open('control-mofgen.txt', 'w')
    #Just for checking, now lets gather everything in the model_molecule dict into one big thing and print it
    test_mol = Atoms()
    for obj in model_molecule:
        test_mol += model_molecule[obj]

    write('test_model.xyz', test_mol)

    #print n_centers, n_model, n_obj
    #Headers and cell
    f.write('%-20s %-3d\n' % ('Number of objects =', n_obj + 1))
    f.write('%-20s\n' % ('build = systre'))
    f.write('%5s\n' % ('Cell:'))
    f.write('%8.3f %8.3f %8.3f \n' % (0.0, 0.0, 0.0))
    f.write('%8.3f %8.3f %8.3f \n' % (0.0, 0.0, 0.0))
    f.write('%8.3f %8.3f %8.3f \n' % (0.0, 0.0, 0.0))

    g.write('%-20s\n' % ('model = Td'))

    for obj in xrange(n_centers + 1):
        f.write('\n%-8s %-3d\n' % ('Center: ', obj + 1))
        f.write('%-3d\n' % (len(model_molecule[obj])))
        f.write('%-20s\n' % ('type = triangle'))
        #process positions to make it a bit more ideal
        for atom in model_molecule[obj]:
            #if atom.symbol == 'C':
            if atom.index != 0:
                model_molecule[obj].set_distance(atom.index, 0, 1.0, fix=1)
        for atom in model_molecule[obj]:
            (x, y, z) = atom.position
            #print atom.symbol, atom.position, atom.tag
            if atom.tag:
                f.write('%-2s %15.8f %15.8f %15.8f %-4s\n' %
                        ('X', x, y, z, atom.tag))
                #f.write('%-2s %15.8f %15.8f %15.8f %-4s\n' % (atom.symbol, x, y, z, atom.tag))
            else:
                f.write('%-2s %15.8f %15.8f %15.8f\n' % ('Q', x, y, z))
                #f.write('%-2s %15.8f %15.8f %15.8f\n' % (atom.symbol, x, y, z))
        g.write('%-9s %-50s\n' % ('center =', names[0]))

    for obj in xrange(n_centers + 1, n_obj + 1):
        f.write('\n%-8s %-3d\n' % ('Linker: ', obj - n_centers))
        f.write('%-3d\n' % (len(model_molecule[obj])))
        f.write('%-20s\n' % ('type = triangle'))
        #process positions to make it a bit more ideal
        for atom in model_molecule[obj]:
            #if atom.symbol == 'C':
            if atom.index != 0:
                model_molecule[obj].set_distance(atom.index, 0, 1.0, fix=1)
        for atom in model_molecule[obj]:
            (x, y, z) = atom.position
            #print atom.symbol, atom.position, atom.tag
            if atom.tag:
                f.write('%-2s %15.8f %15.8f %15.8f %-4s\n' %
                        ('X', x, y, z, atom.tag))
                #f.write('%-2s %15.8f %15.8f %15.8f %-4s\n' % (atom.symbol, x, y, z, atom.tag))
            else:
                f.write('%-2s %15.8f %15.8f %15.8f\n' % ('Q', x, y, z))
                #f.write('%-2s %15.8f %15.8f %15.8f\n' % (atom.symbol, x, y, z))
        g.write('%-9s %-50s\n' % ('linker =', names[1]))

    test_mol = Atoms()
    for obj in model_molecule:
        test_mol += model_molecule[obj]

    write('test_model2.xyz', test_mol)
Beispiel #32
0
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, 'Fermi Level:')
        except ValueError:
            eFermi = None
        else:
            try:
                eFermi = float(lines[ii].split()[2])
            except ValueError:  # we have two Fermi levels
                fields = lines[ii].split()

                def strip(string):
                    for rubbish in '[],':
                        string = string.replace(rubbish, '')
                    return string

                eFermi = [float(strip(fields[2])), float(strip(fields[3]))]
        try:
            ii = index_startswith(lines, 'Total Charge:')
        except ValueError:
            q = None
        else:
            q = float(lines[ii].split()[2])
        try:
            ii = index_startswith(lines, 'Local Magnetic Moments')
        except ValueError:
            magmoms = None
        else:
            magmoms = []
            for i in range(ii + 1, ii + 1 + len(atoms)):
                iii, magmom = lines[i].split()[:2]
                magmoms.append(float(magmom))
        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), 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:
            calc = SinglePointDFTCalculator(e, f, None, magmoms, atoms, eFermi)
            atoms.set_calculator(calc)
        if q is not None:
            n = len(atoms)
            atoms.set_charges([q / n] * n)

        images.append(atoms)
        lines = lines[i:]
Beispiel #33
0
def make_Td(names,sizes):

    #work out the expansion factor
    factor = sum(sizes)
    #factor = 3.0
        
    Td = Atoms()
    Td.append(Atom('F', position=(0,0,0)))
    Td.append(Atom('N', position=(0,0,factor)))
    #Atom 3
    Td.append(Atom('N', position=(1.0,0,0.0)))
    Td.set_angle([1,0,2],1.91062939)
    Td.set_distance(0,2, factor, fix=0)
    #Atom 4
    Td.append(Atom('N', position=(1.0,0,0.0)))
    Td.set_distance(0,3, factor, fix=0)
    Td.set_angle([1,0,3],1.91062939, mask=None)
    Td.set_dihedral([2,1,0,3],2.0943951, mask=None)
    #print Td.get_angle([1,0,2])
    #Atom 5
    Td.append(Atom('N', position=(1.0,0,0)))
    Td.set_angle([1,0,4],1.91062939)
    Td.set_dihedral([2,1,0,4],-2.0943951)
    Td.set_distance(0,4, factor, fix=0)
    
    #find side centroids
    tmp1 = Td[1:4]
    coside = tmp1.get_center_of_mass()
    Td.append(Atom('C', position = coside))
    #
    tmp1 = Td[2:5]
    coside = tmp1.get_center_of_mass()
    Td.append(Atom('C', position = coside))
    #
    tmp1 = Td[3:5]
    tmp1.append(Td[1])
    coside = tmp1.get_center_of_mass()
    Td.append(Atom('C', position = coside))
    #
    tmp1 = Td[1:3]
    tmp1.append(Td[4])
    coside = tmp1.get_center_of_mass()
    Td.append(Atom('C', position = coside))
    
    write('td.xyz',Td)
    
    # Now we make tags etc
    
    ## C -> triangles (faces), N => triangle caps
    
    eps = 0.05
    model_molecule={}
    n_obj = -1 #initialise to -1 such that it can be indexed at start of add
    n_tags = 0
    
    #First detect global bonding
    cov_rad=[]
    for atom in Td:
        #cov_rad.append(covalent_radii[atom.number])
        cov_rad.append(factor / 2)
    
    nlist = NeighborList(cov_rad,skin=0.1,self_interaction=False,bothways=True)
    nlist.build(Td)
    
    nbond = 1
    bond_matrix = np.zeros( (len(Td),len(Td)) )
    pbc_bond_matrix = np.zeros( (len(Td),len(Td)) )
    
    bond_dict = {}
    
    for atom in Td:
        print "---------------------"
        indices, offsets = nlist.get_neighbors(atom.index)
        for index,offset in zip(indices,offsets):
            print atom.index, index, atom.symbol, Td[index].symbol, offset, Td.get_distance(atom.index,index,mic=True)
            if atom.index < index:
                this_bond = [atom.index, index]
                for o in offset:
                    this_bond.append(o)
                bond = tuple(this_bond)
                print bond
                bond_dict[bond] = nbond
                #Now we need to find the same bond the other direction to get the offsets right
                indices2,offsets2 = nlist.get_neighbors(index)
                for i2,o2 in zip(indices2,offsets2):
                    if i2 == atom.index:
                        #print "sum of offsets = ", offset, o2, sum(offset + o2)
                        if sum(offset + o2) == 0: #same bond
                            this_bond = [index, atom.index]
                            for o in o2:
                                this_bond.append(o)
                            bond = tuple(this_bond)
                            print bond
                            bond_dict[bond] = nbond
                nbond +=1
    
    print nbond
    for k,v in bond_dict.items():
        print k,v
    
    #Now we look for all the things with unit*factor bondlengths
    #Start with N (rectangles)
    for atom in Td:
        if atom.symbol == 'N':
            print'======================================='
            print 'N Atom ',atom.index
            n_obj+=1
            model_molecule[n_obj] = Atoms()
            model_molecule[n_obj] += atom
            model_molecule[n_obj][0].original_index = atom.index
            model_molecule[n_obj][0].symbol = 'F'
            indices, offsets = nlist.get_neighbors(atom.index)
            #Just checking the symbols here
            print indices
            print offsets
            symbols = ([Td[index].symbol for index in indices])
            symbol_string = ''.join(sorted([Td[index].symbol for index in indices]))
            #print symbol_string
            #for i,o in zip(indices, offsets):
            #    print i,o
            for index,offset in zip(indices,offsets):
                print atom.index, index, offset
                this_bond = [atom.index, index]
                for o in offset:
                    this_bond.append(o)
                bond = tuple(this_bond)
                print bond_dict[bond]
                if Td[index].symbol == 'C':
                    #By definition, we can't be going over a periodic boundary (no pbc)
                    model_molecule[n_obj] += Td[index]
                    model_molecule[n_obj][-1].tag = bond_dict[bond]
                    model_molecule[n_obj].positions[-1] = Td.positions[index] 
    
    n_centers = n_obj
    
    ##Now we do the  C  (triangles)
    for atom in Td:
        if atom.symbol == 'C':
            #print'======================================='
            #print 'N Atom ',atom.index, " finding squares1"
            n_obj+=1
            model_molecule[n_obj] = Atoms()
            model_molecule[n_obj] += atom
            indices, offsets = nlist.get_neighbors(atom.index)
            for index,offset in zip(indices,offsets):
                print index, offset, Td[index].symbol
                this_bond = [atom.index, index]
                for o in offset:
                    this_bond.append(o)
                bond = tuple(this_bond)
                #if not bond_dict.has_key(bond):
                    #Then
                if Td[index].symbol == 'N':
                    #By definition, we can't be going over a periodic boundary (no pbc)
                    model_molecule[n_obj] += Td[index]
                    model_molecule[n_obj][-1].tag = bond_dict[bond]
                    model_molecule[n_obj].positions[-1] = Td.positions[index] 
    
    
    f = open('Td.model','w')
    g = open('control-mofgen.txt','w')
    #Just for checking, now lets gather everything in the model_molecule dict into one big thing and print it
    test_mol = Atoms()
    for obj in model_molecule:
        test_mol += model_molecule[obj]
    
    write('test_model.xyz',test_mol)
    
    #print n_centers, n_model, n_obj
    #Headers and cell
    f.write('%-20s %-3d\n' %('Number of objects =',n_obj+1))
    f.write('%-20s\n' %('build = systre'))
    f.write('%5s\n' %('Cell:'))
    f.write('%8.3f %8.3f %8.3f \n' % (0.0, 0.0, 0.0))
    f.write('%8.3f %8.3f %8.3f \n' % (0.0, 0.0, 0.0))
    f.write('%8.3f %8.3f %8.3f \n' % (0.0, 0.0, 0.0))
    
    g.write('%-20s\n' %('model = Td'))
    
    for obj in xrange(n_centers+1):
        f.write('\n%-8s %-3d\n' %('Center: ', obj+1))
        f.write('%-3d\n' %(len(model_molecule[obj])))
        f.write('%-20s\n' %('type = triangle'))
        #process positions to make it a bit more ideal
        for atom in model_molecule[obj]:
            #if atom.symbol == 'C':
            if atom.index != 0:
                model_molecule[obj].set_distance(atom.index,0,1.0,fix=1)
        for atom in model_molecule[obj]:
            (x,y,z) = atom.position
            #print atom.symbol, atom.position, atom.tag
            if atom.tag:
                f.write('%-2s %15.8f %15.8f %15.8f %-4s\n' % ('X', x, y, z, atom.tag))
                #f.write('%-2s %15.8f %15.8f %15.8f %-4s\n' % (atom.symbol, x, y, z, atom.tag))
            else:
                f.write('%-2s %15.8f %15.8f %15.8f\n' % ('Q', x, y, z))
                #f.write('%-2s %15.8f %15.8f %15.8f\n' % (atom.symbol, x, y, z))
        g.write('%-9s %-50s\n' %('center =', names[0]))
    
    for obj in xrange(n_centers+1,n_obj+1):
        f.write('\n%-8s %-3d\n' %('Linker: ', obj-n_centers))
        f.write('%-3d\n' %(len(model_molecule[obj])))
        f.write('%-20s\n' %('type = triangle'))
        #process positions to make it a bit more ideal
        for atom in model_molecule[obj]:
            #if atom.symbol == 'C':
            if atom.index != 0:
                model_molecule[obj].set_distance(atom.index,0,1.0,fix=1)
        for atom in model_molecule[obj]:
            (x,y,z) = atom.position
            #print atom.symbol, atom.position, atom.tag
            if atom.tag:
                f.write('%-2s %15.8f %15.8f %15.8f %-4s\n' % ('X', x, y, z, atom.tag))
                #f.write('%-2s %15.8f %15.8f %15.8f %-4s\n' % (atom.symbol, x, y, z, atom.tag))
            else:
                f.write('%-2s %15.8f %15.8f %15.8f\n' % ('Q', x, y, z))
                #f.write('%-2s %15.8f %15.8f %15.8f\n' % (atom.symbol, x, y, z))
        g.write('%-9s %-50s\n' %('linker =', names[1]))
    
    
    test_mol = Atoms()
    for obj in model_molecule:
        test_mol += model_molecule[obj]
    
    write('test_model2.xyz',test_mol)
Beispiel #34
0
def read_turbomole_gradient(filename='gradient', index=-1):
    """ Method to read turbomole gradient file """

    if isinstance(filename, str):
        f = open(filename)

    # read entire file
    lines = [x.strip() for x in f.readlines()]

    # find $grad section
    start = end = -1
    for i, line in enumerate(lines):
        if not line.startswith('$'):
            continue
        if line.split()[0] == '$grad':
            start = i
        elif start >= 0:
            end = i
            break

    if end <= start:
        raise RuntimeError('File %s does not contain a valid \'$grad\' section' % (filename))

    def formatError():
        raise RuntimeError('Data format in file %s does not correspond to known Turbomole gradient format' % (filename))


    # trim lines to $grad
    del lines[:start+1]
    del lines[end-1-start:]

    # Interpret $grad section
    from ase import Atoms, Atom
    from ase.calculators.singlepoint import SinglePointCalculator
    from ase.units import Bohr
    images = []
    while len(lines): # loop over optimization cycles
        # header line
        # cycle =      1    SCF energy =     -267.6666811409   |dE/dxyz| =  0.157112
        fields = lines[0].split('=')
        try:
            cycle = int(fields[1].split()[0])
            energy = float(fields[2].split()[0])
            gradient = float(fields[3].split()[0])
        except (IndexError, ValueError):
            formatError()
        
        # coordinates/gradient
        atoms = Atoms()
        forces = []
        for line in lines[1:]:
            fields = line.split()
            if len(fields) == 4: # coordinates
                # 0.00000000000000      0.00000000000000      0.00000000000000      c
                try:
                    symbol = fields[3].lower().capitalize()
                    position = tuple([bohr2angstrom(float(x)) for x in fields[0:3] ])
                except ValueError:
                    formatError()
                atoms.append(Atom(symbol, position))
            elif len(fields) == 3: # gradients
                #  -.51654903354681D-07  -.51654903206651D-07  0.51654903169644D-07
                try:
                    grad = [float(x.replace('D', 'E')) * Bohr for x in fields[0:3] ]
                except ValueError:
                    formatError()
                forces.append(grad)
            else: # next cycle
                break

        # calculator
        calc = SinglePointCalculator(atoms, energy=energy, forces=forces)
        atoms.set_calculator(calc)

        # save frame
        images.append(atoms)

        # delete this frame from data to be handled
        del lines[:2*len(atoms)+1]

    return images[index]
Beispiel #35
0
def read_proteindatabank(fileobj, index=-1, read_arrays=True):
    """Read PDB files."""

    if isinstance(fileobj, basestring):
        fileobj = open(fileobj)

    images = []
    orig = np.identity(3)
    trans = np.zeros(3)
    atoms = Atoms()
    occ = []
    bfactor = []
    for line in fileobj.readlines():
        if line.startswith('CRYST1'):
            cellpar = [
                float(line[6:15]),  # a
                float(line[15:24]),  # b
                float(line[24:33]),  # c
                float(line[33:40]),  # alpha
                float(line[40:47]),  # beta
                float(line[47:54])
            ]  # gamma
            atoms.set_cell(cellpar_to_cell(cellpar))
            atoms.pbc = True
        for c in range(3):
            if line.startswith('ORIGX' + '123'[c]):
                orig[c] = [
                    float(line[10:20]),
                    float(line[20:30]),
                    float(line[30:40])
                ]
                trans[c] = float(line[45:55])

        if line.startswith('ATOM') or line.startswith('HETATM'):
            try:
                # Atom name is arbitrary and does not necessarily
                # contain the element symbol.  The specification
                # requires the element symbol to be in columns 77+78.
                # Fall back to Atom name for files that do not follow
                # the spec, e.g. packmol.
                try:
                    symbol = label_to_symbol(line[76:78].strip())
                except (KeyError, IndexError):
                    symbol = label_to_symbol(line[12:16].strip())
                # Don't use split() in case there are no spaces
                position = np.array([
                    float(line[30:38]),  # x
                    float(line[38:46]),  # y
                    float(line[46:54])
                ])  # z
                try:
                    occ.append(float(line[54:60]))
                    bfactor.append(float(line[60:66]))
                except (IndexError, ValueError):
                    pass
                position = np.dot(orig, position) + trans
                atoms.append(Atom(symbol, position))
            except Exception as ex:
                warnings.warn(
                    'Discarding atom when reading PDB file: {}\n{}'.format(
                        line.strip(), ex))
        if line.startswith('END'):
            # End of configuration reached
            # According to the latest PDB file format (v3.30),
            # this line should start with 'ENDMDL' (not 'END'),
            # but in this way PDB trajectories from e.g. CP2K
            # are supported (also VMD supports this format).
            if read_arrays and len(occ) == len(atoms):
                atoms.set_array('occupancy', np.array(occ))
            if read_arrays and len(bfactor) == len(atoms):
                atoms.set_array('bfactor', np.array(bfactor))
            images.append(atoms)
            atoms = Atoms()
            occ = []
            bfactor = []
    if len(images) == 0:
        # Single configuration with no 'END' or 'ENDMDL'
        if read_arrays and len(occ) == len(atoms):
            atoms.set_array('occupancy', np.array(occ))
        if read_arrays and len(bfactor) == len(atoms):
            atoms.set_array('bfactor', np.array(bfactor))
        images.append(atoms)
    return images[index]
Beispiel #36
0
def make_soc(names,sizes):

    #work out the expansion factor
    factor = sum(sizes) #

    a = 2.8284 * factor
    
    soc = crystal(['C', 'In'], [(0.25, 0, 0), (0.25, 0.25, 0.25)], spacegroup=229,  #229
                   cellpar=[a, a, a, 90, 90, 90])
    
    #Use this purely to get the COM wrt In only
    shadow_soc = crystal(['C', 'In'], [(0.25, 0, 0), (0.25, 0.25, 0.25)], spacegroup=229,
                   cellpar=[a, a, a, 90, 90, 90])
    del shadow_soc[[atom.index for atom in shadow_soc if atom.symbol=='C']]
    coIn = Atom('X', position = shadow_soc.get_center_of_mass())
    #shadow_soc +=  coIn
    #write('In_only.xyz',shadow_soc)
    del shadow_soc[[atom.index for atom in shadow_soc if atom.symbol=='In']]
    #print "shadow_soc",shadow_soc
    
    #Now let's sort our In atoms into two sets of diagonals
    first_In = 0
    for atom in soc:
        if atom.symbol !='In':
            first_In +=1
        else:
            break
    
    #print first_In
    intra_In_distances = {}
    for atom in soc:
        if atom.symbol == 'In':
            this_dist = soc.get_distance(first_In,atom.index)
            intra_In_distances[atom.index] = this_dist
    #print intra_In_distances
    sorted_IID = sorted(intra_In_distances, key=intra_In_distances.get)
    #print sorted_IID
    #Now assign into two sets of diagonals
    cw_set = [first_In,sorted_IID[4],sorted_IID[5],sorted_IID[6]]
    acw_set = [sorted_IID[1],sorted_IID[2],sorted_IID[3],sorted_IID[7]] 
    
    
    #write('test.xyz',soc)
    
    eps = 0.01
    model_molecule={}
    n_obj = -1 #initialise to -1 such that it can be indexed at start of add
    n_tags = 0
    
    #First detect global bonding
    cov_rad=[]
    for atom in soc:
        #cov_rad.append(covalent_radii[atom.number])
        cov_rad.append(factor / 2)
    
    nlist = NeighborList(cov_rad,self_interaction=False,bothways=True)
    nlist.build(soc)
    
    #To sort out tags, we need to label each bond
    nbond = 1
    bond_matrix = np.zeros( (len(soc),len(soc)) )
    
    for atom in soc:
        indices, offsets = nlist.get_neighbors(atom.index)
        for index in indices:
            if atom.index < index:
                bond_matrix[atom.index,index] = nbond
                bond_matrix[index,atom.index] = nbond
                print atom.index, index, nbond
                nbond +=1
    
    print nbond
    print bond_matrix 
    
    #Now we look for all the things with 2unit bondlengths
    #Starting with In (6 connected things)
    for atom in soc:
        if atom.symbol == 'In':
            print'======================================='
            print 'In Atom ',atom.index
            n_obj+=1
            model_molecule[n_obj] = Atoms()
            model_molecule[n_obj] += atom
            model_molecule[n_obj][0].original_index = atom.index
            #Find the oxygens with three In bound to it
            indices, offsets = nlist.get_neighbors(atom.index)
            symbols = ([soc[index].symbol for index in indices])
            symbol_string = ''.join(sorted([soc[index].symbol for index in indices]))
            print symbol_string
            #for i,o in zip(indices, offsets):
            #    print i,o
            for index,offset in zip(indices,offsets):
                if soc[index].symbol == 'C':
                    dist_mic = soc.get_distance(atom.index,index,mic=True)
                    dist_no_mic = soc.get_distance(atom.index,index,mic=False)
                    print index, dist_no_mic
                    if (abs(dist_mic - dist_no_mic) <= eps) and (abs(dist_mic - factor) < eps): #Cell expands by the factor. Default systre bondlength is 1.0
                         model_molecule[n_obj] += soc[index]
                         model_molecule[n_obj][-1].tag = bond_matrix[atom.index,index]
                    elif abs(dist_mic - factor) < eps:
                        #If we're going over a periodic boundary, we need to negate the tag
                        #print "Tag, ", soc[index].tag, " goes over pbc"
                        #soc[index].tag = -(soc[index].tag)
                        model_molecule[n_obj] += soc[index]
                        model_molecule[n_obj][-1].tag = -bond_matrix[atom.index,index]
                        print model_molecule[n_obj].positions[-1]
                        model_molecule[n_obj].positions[-1] = soc.positions[index] + np.dot(offset, soc.get_cell())
                        print model_molecule[n_obj].positions[-1]
            model_molecule[n_obj] +=  coIn
            #print model_molecule[n_obj].original_indices
    n_centers = n_obj
    
    #Assigning In atoms to 'sets'
    intra_In_distances = {}
    for atom in soc:
        if atom.symbol == 'In':
            this_dist = soc.get_distance(first_In,atom.index)
            intra_In_distances[atom.index] = this_dist
    print intra_In_distances
    sorted_IID = sorted(intra_In_distances, key=intra_In_distances.get)
    print sorted_IID
    #Now assign into two sets of diagonals
    cw_set = [first_In,sorted_IID[4],sorted_IID[5],sorted_IID[6]]
    acw_set = [sorted_IID[1],sorted_IID[2],sorted_IID[3],sorted_IID[7]] 
    
    angle = 30.0 * pi /180.0
    
    ##Now we need to rotate these carbons around
    for obj in range(n_centers+1): 
        #First work out the two sets of C atoms
        print "Looking at model object, ",obj,model_molecule[obj][0].original_index
        cx_distances = {}
        for atom in model_molecule[obj]:
            if atom.symbol == 'C':
                cx_distances[atom.index] = model_molecule[obj].get_distance(atom.index,-1)
        sorted_CXD = sorted(cx_distances, key = cx_distances.get)    
        inner_set = [sorted_CXD[0],sorted_CXD[1],sorted_CXD[2]]
        outer_set = [sorted_CXD[3],sorted_CXD[4],sorted_CXD[5]]
        rotation_axis = model_molecule[obj][0].position - model_molecule[obj][-1].position #In-X
        #Now copy each set of C and rotate, then replace the original
        inner_mol = Atoms()
        for a in inner_set:
            this_atom = Atom('N',position=model_molecule[obj][a].position, tag=model_molecule[obj][a].tag)
            inner_mol.append(this_atom)
        outer_mol = Atoms()
        for a in outer_set:
            this_atom = Atom('N',position=model_molecule[obj][a].position, tag=model_molecule[obj][a].tag)
            outer_mol.append(this_atom)
        if model_molecule[obj][0].original_index in cw_set:
            #inner_mol goes cw
            inner_mol.rotate(rotation_axis,angle,center='COM')
            outer_mol.rotate(rotation_axis,-angle,center='COM')
        elif model_molecule[obj][0].original_index in acw_set:
            #inner_mol goes acw
            inner_mol.rotate(rotation_axis,-angle,center='COM')
            outer_mol.rotate(rotation_axis,angle,center='COM')
        model_molecule[obj] += inner_mol
        model_molecule[obj] += outer_mol
        soc += inner_mol
        soc += outer_mol
        del model_molecule[obj][[atom.index for atom in model_molecule[obj] if atom.symbol=='C' or atom.symbol == 'X']]
        print "=============="
    
    #Now look for C (4 connected things)
    for atom in soc:
        if atom.symbol == 'C':
            print'======================================='
            print 'C Atom ',atom.index
            n_obj+=1
            model_molecule[n_obj] = Atoms()
            model_molecule[n_obj] += atom
            #Find the oxygens with three In bound to it
            indices, offsets = nlist.get_neighbors(atom.index)
            symbols = ([soc[index].symbol for index in indices])
            symbol_string = ''.join(sorted([soc[index].symbol for index in indices]))
            print symbol_string
            #for i,o in zip(indices, offsets):
            #    print i,o
            for index,offset in zip(indices,offsets):
                if soc[index].symbol == 'In':
                    dist_mic = soc.get_distance(atom.index,index,mic=True)
                    dist_no_mic = soc.get_distance(atom.index,index,mic=False)
                    print index, dist_no_mic
                    print soc[index].original_index
                    if (abs(dist_mic - dist_no_mic) <= eps) and (abs(dist_mic - factor) < eps):
                        this_tag = bond_matrix[atom.index,index]
                        print "Searching for tag ",this_tag
                        for atom2 in soc:
                            if atom2.symbol == 'N' and abs(atom2.tag) == this_tag:
                                model_molecule[n_obj] += Atom('O', position=atom2.position)
                                model_molecule[n_obj][-1].tag = this_tag
                        #model_molecule[n_obj] += soc[index]
                        #model_molecule[n_obj][-1].tag = bond_matrix[atom.index,index]
                    elif abs(dist_mic - factor) < eps:
                        #If we're going over a periodic boundary, we need to mark the tag
                        #print "Tag, ", soc[index].tag, " goes over pbc"
                        this_tag = bond_matrix[atom.index,index]
                        for atom2 in soc:
                            if atom2.symbol == 'N' and abs(atom2.tag) == this_tag:
                                model_molecule[n_obj] += Atom('O', position=atom2.position) 
                                model_molecule[n_obj][-1].tag = -this_tag
                                model_molecule[n_obj].positions[-1] = soc.positions[atom2.index] + np.dot(offset, soc.get_cell())
    
                        #model_molecule[n_obj] += soc[index]
                        #model_molecule[n_obj][-1].tag = -bond_matrix[atom.index,index]
                        #print model_molecule[n_obj].positions[-1]
                        #model_molecule[n_obj].positions[-1] = soc.positions[index] + np.dot(offset, soc.get_cell())
    
    for obj in xrange(n_centers+1,n_obj+1):
        tmp_mol = Atoms()
        tmp_mol = model_molecule[obj].copy()
        del tmp_mol[[atom.index for atom in tmp_mol if atom.symbol!='O']]
        #process positions correct COM 
        model_molecule[obj][0].position = tmp_mol.get_center_of_mass()
    
    #Now most of the tags have switched
    
    for obj in xrange(n_centers+1):
        #process positions to make it a bit more ideal
        for atom in model_molecule[obj]:
            if atom.symbol == 'C' or atom.symbol == 'N':
                model_molecule[obj].set_distance(atom.index,0,1.0,fix=1)
                shadow_soc += Atom('N', position=atom.position,tag=atom.tag)  #Keep these tags
    
    for obj in xrange(n_centers+1,n_obj+1):
        #process positions to make it a bit more ideal
        for atom in model_molecule[obj]:
            if atom.symbol == 'O':
                model_molecule[obj].set_distance(atom.index,0,1.0,fix=1)
                shadow_soc += Atom('O', position=atom.position)
                #Now find the nearest N
                rON={}
                for atom2 in shadow_soc:
                    if atom2.symbol == 'N':
                        this_dist = shadow_soc.get_distance(-1,atom2.index,mic=True)
                        rON[atom2.index] = this_dist
                        print atom.index, atom2.index, this_dist
                min_index = min(rON, key=rON.get)
                atom.tag = shadow_soc[min_index].tag
                shadow_soc[-1].tag = shadow_soc[min_index].tag
    
    #write('shadow_soc.xyz',shadow_soc)
    #Tags get horribly messed up
    t = open('tags_check','w')
    for obj in xrange(n_obj+1):
        #process positions to make it a bit more ideal
        for atom in model_molecule[obj]:
            (x,y,z) = atom.position
            #print atom.symbol, atom.position, atom.tag
            if atom.tag:
                t.write('%-2s %15.8f %15.8f %15.8f %-4s\n' % (atom.symbol, x, y, z, atom.tag))
            else:
                t.write('%-2s %15.8f %15.8f %15.8f\n' % (atom.symbol, x, y, z))
    
    
    
    
    f = open('soc.model','w')
    g = open('control-mofgen.txt','w')
    #Just for checking, now lets gather everything in the model_molecule dict into one big thing and print it
    #test_mol = Atoms()
    #for obj in model_molecule:
    #    test_mol += model_molecule[obj]
    #
    #write('test_model.xyz',test_mol)
    
    #print n_centers, n_model, n_obj
    #Headers and cell
    f.write('%-20s %-3d\n' %('Number of objects =',n_obj+1))
    f.write('%-20s\n' %('build = systre'))
    f.write('%5s\n' %('Cell:'))
    f.write('%8.3f %8.3f %8.3f \n' %
              (soc.get_cell()[0][0],
               soc.get_cell()[0][1],
               soc.get_cell()[0][2]))
    f.write('%8.3f %8.3f %8.3f \n' %
              (soc.get_cell()[1][0],
               soc.get_cell()[1][1],
               soc.get_cell()[1][2]))
    f.write('%8.3f %8.3f %8.3f \n' %
              (soc.get_cell()[2][0],
               soc.get_cell()[2][1],
               soc.get_cell()[2][2])) 
   
    g.write('%-20s\n' %('model = soc')) 
    
    for obj in xrange(n_centers+1):
        f.write('\n%-8s %-3d\n' %('Centre: ', obj+1))
        f.write('%-3d\n' %(len(model_molecule[obj])))
        f.write('%-20s\n' %('type = tri_prism'))
        #process positions to make it a bit more ideal
        #for atom in model_molecule[obj]:
        #    if atom.symbol == 'C' or atom.symbol == 'N':
        #        pass#model_molecule[obj].set_distance(atom.index,0,1.0,fix=1)
        for atom in model_molecule[obj]:
            (x,y,z) = atom.position
            #print atom.symbol, atom.position, atom.tag
            if atom.tag:
                f.write('%-2s %15.8f %15.8f %15.8f %-4s\n' % ('X', x, y, z, atom.tag))
            else:
                f.write('%-2s %15.8f %15.8f %15.8f\n' % ('Q', x, y, z))
        g.write('%-9s %-50s\n' %('center =', names[0]))
    
    for obj in xrange(n_centers+1,n_obj+1):
        f.write('\n%-8s %-3d\n' %('Linker: ', obj-n_centers))
        f.write('%-3d\n' %(len(model_molecule[obj])))
        f.write('%-20s\n' %('type = rectangle'))
        #process positions to make it a bit more ideal
        #for atom in model_molecule[obj]:
        #    if atom.symbol == 'In':
        #        pass#model_molecule[obj].set_distance(atom.index,0,1.0,fix=1)
        for atom in model_molecule[obj]:
            (x,y,z) = atom.position
            #print atom.symbol, atom.position, atom.tag
            if atom.tag:
                f.write('%-2s %15.8f %15.8f %15.8f %-4s\n' % ('X', x, y, z, atom.tag))
            else:
                f.write('%-2s %15.8f %15.8f %15.8f\n' % ('Q', x, y, z))
        g.write('%-9s %-50s\n' %('linker =', names[1]))
    
    
    test_mol = Atoms()
    for obj in model_molecule:
        test_mol += model_molecule[obj]
    
    write('test_model2.xyz',test_mol)
    write('test_model2.cif',soc)
Beispiel #37
0
def read_turbomole_gradient(filename='gradient', index=-1):
    """ Method to read turbomole gradient file """

    if isinstance(filename, str):
        f = open(filename)

    # read entire file
    lines = [x.strip() for x in f.readlines()]

    # find $grad section
    start = end = -1
    for i, line in enumerate(lines):
        if not line.startswith('$'):
            continue
        if line.split()[0] == '$grad':
            start = i
        elif start >= 0:
            end = i
            break

    if end <= start:
        raise RuntimeError(
            'File %s does not contain a valid \'$grad\' section' % (filename))

    def formatError():
        raise RuntimeError(
            'Data format in file %s does not correspond to known Turbomole gradient format'
            % (filename))

    # trim lines to $grad
    del lines[:start + 1]
    del lines[end - 1 - start:]

    # Interpret $grad section
    from ase import Atoms, Atom
    from ase.calculators.singlepoint import SinglePointCalculator
    from ase.units import Bohr
    images = []
    while len(lines):  # loop over optimization cycles
        # header line
        # cycle =      1    SCF energy =     -267.6666811409   |dE/dxyz| =  0.157112
        fields = lines[0].split('=')
        try:
            cycle = int(fields[1].split()[0])
            energy = float(fields[2].split()[0])
            gradient = float(fields[3].split()[0])
        except (IndexError, ValueError):
            formatError()

        # coordinates/gradient
        atoms = Atoms()
        forces = []
        for line in lines[1:]:
            fields = line.split()
            if len(fields) == 4:  # coordinates
                # 0.00000000000000      0.00000000000000      0.00000000000000      c
                try:
                    symbol = fields[3].lower().capitalize()
                    position = tuple(
                        [bohr2angstrom(float(x)) for x in fields[0:3]])
                except ValueError:
                    formatError()
                atoms.append(Atom(symbol, position))
            elif len(fields) == 3:  # gradients
                #  -.51654903354681D-07  -.51654903206651D-07  0.51654903169644D-07
                try:
                    grad = [
                        float(x.replace('D', 'E')) * Bohr for x in fields[0:3]
                    ]
                except ValueError:
                    formatError()
                forces.append(grad)
            else:  # next cycle
                break

        # calculator
        calc = SinglePointCalculator(energy, forces, None, None, atoms)
        atoms.set_calculator(calc)

        # save frame
        images.append(atoms)

        # delete this frame from data to be handled
        del lines[:2 * len(atoms) + 1]

    return images[index]
Beispiel #38
0
atoms = read(glob.glob('final.xyz')[0])
atoms.set_cell(np.eye(3) * l)
print atoms
up = Atoms()
up.set_cell(atoms.get_cell())
up_target = 'X'
dn = Atoms()
dn.set_cell(atoms.get_cell())
dn_target = 'He'
nuclei = Atoms()
nuclei.set_cell(atoms.get_cell())

for a in range(len(atoms)):
    if atoms[a].symbol == up_target:
        up.append(atoms[a])
    if atoms[a].symbol == dn_target:
        dn.append(atoms[a])
    if atoms[a].symbol != up_target and atoms[a].symbol != dn_target:
        nuclei.append(atoms[a])

data_up = get_rdf(up, l, nmax)
data_dn = get_rdf(dn, l, nmax)
data_nuclei = get_rdf(nuclei, l, nmax)
data_all = get_rdf(atoms, l, nmax)
# data_up[0]/max(data_up[0])*len(up)
# data_dn[0]/max(data_dn[0])*len(dn)
bar(data_up[1], data_up[0], label='up', width=0.02)
bar(data_dn[1], data_dn[0], label='dn', width=0.01)
bar(data_nuclei[1], data_nuclei[0], label='nuclei', width=0.02)
#bar(data_all[1],data_all[0],label='all',width=0.05)