Beispiel #1
0
def create_molecule(selected_nodes, parent=None):
    numbers = []
    coordinates = []
    atoms = list(iter_atoms(selected_nodes))
    for atom in atoms:
        numbers.append(atom.number)
        if parent is None:
            coordinates.append(atom.get_absolute_frame().t)
        else:
            coordinates.append(atom.get_frame_relative_to(parent).t)
    result = Molecule(numbers, coordinates)
    result.atoms = atoms
    return result
Beispiel #2
0
    def collect_molecules(self, parent, universe=None):
        Atom = context.application.plugins.get_node("Atom")
        Bond = context.application.plugins.get_node("Bond")
        Frame = context.application.plugins.get_node("Frame")


        if universe==None:
            universe = parent

        atom_to_index = {}
        atoms_extra = {}
        counter = 0
        numbers = []
        coordinates = []
        for child in parent.children:
            if isinstance(child, Atom):
                atom_to_index[child] = counter
                if len(child.extra) > 0:
                    atoms_extra[counter] = child.extra
                counter += 1
                numbers.append(child.number)
                coordinates.append(child.get_frame_relative_to(universe).t)

        if len(numbers) > 0:
            molecule = Molecule(numbers, coordinates, parent.name)
            molecule.extra = parent.extra
            molecule.atoms_extra = atoms_extra
            molecule.bonds_extra = {}

            pairs = set([])
            for child in parent.children:
                if isinstance(child, Bond):
                    atoms = child.get_targets()
                    pair = frozenset([atom_to_index[atoms[0]], atom_to_index[atoms[1]]])
                    if len(child.extra) > 0:
                        molecule.bonds_extra[pair] = child.extra
                    pairs.add(pair)
            if len(pairs) > 0:
                molecule.graph = MolecularGraph(pairs, molecule.numbers)
            else:
                molecule.graph = None

            result = [molecule]
        else:
            result = []

        for child in parent.children:
            if isinstance(child, Frame):
                result.extend(self.collect_molecules(child, universe))

        return result
Beispiel #3
0
    def __init__(self, filename):
        """
           Arguments:
            | ``filename`` -- The file to load.
        """
        self.filename = filename

        # auxiliary skip function
        def skip_to(f, linestart):
            while True:
                line = f.readline()
                if line.startswith(linestart):
                    return line
                if len(line) == 0:
                    return

        with open(filename) as f:
            # Unit cell parameters
            line = skip_to(f, ' DIRECT LATTICE VECTOR COMPONENTS (BOHR)')
            if line is None:
                raise FileFormatError('Could not find the lattice vectors')
            f.readline()
            f.readline()
            vectors = []
            for i in range(3):
                line = f.readline()
                vectors.append([float(word) for word in line.split()[1:]])
            vectors = np.array(vectors)
            self.unit_cell = UnitCell(vectors.transpose())
            # Atomic numbers and coordinates
            line = skip_to(f, ' ATOM CARTESIAN COORDINATES (BOHR)')
            if line is None:
                raise FileFormatError('Could not find the atomic coordinates')
            f.readline()
            f.readline()
            f.readline()
            numbers = []
            symbols = []
            coordinates = []
            while True:
                line = f.readline()
                if line.startswith(' *****'):
                    break
                words = line.split()
                numbers.append(int(words[1]))
                symbols.append(words[2])
                coordinates.append(
                    [float(words[3]),
                     float(words[4]),
                     float(words[5])])
            self.mol = Molecule(np.array(numbers),
                                np.array(coordinates),
                                symbols=symbols,
                                unit_cell=self.unit_cell)
            # Basis set specification
            line = skip_to(f, ' VARIATIONAL BASIS SET')
            if line is None:
                raise FileFormatError('Could not find the basis set')
            f.readline()
            f.readline()
            f.readline()
            f.readline()
            f.readline()
            f.readline()
            self.basisset = {}
            last_basis = None
            last_contraction = None
            while True:
                line = f.readline()
                if line.startswith(' *****'):
                    break
                if line.startswith('         '):
                    # add info to the last atomic basis set
                    assert last_basis is not None
                    subshell = line[36:40].strip()
                    if len(subshell) == 0:
                        subshell = last_contraction[0]
                        # add a primitive to the contraction
                        exponent = float(line[40:50])
                        if subshell == 'S':
                            values = exponent, float(line[50:60])
                        elif subshell == 'SP':
                            values = exponent, float(line[50:60]), float(
                                line[60:70])
                        else:
                            values = exponent, float(line[70:80])
                        last_contraction[1].append(values)
                    else:
                        # define a new contraction
                        last_contraction = (subshell, [])
                        last_basis.append(last_contraction)
                else:
                    # add new atoms
                    symbol = line.split()[1]
                    if symbol not in self.basisset:
                        last_basis = []
                        self.basisset[symbol] = last_basis
            # Compute the total number of basis functions (and orbitals).
            self.num_basis = 0
            subshell_counts = {'S': 1, 'P': 3, 'SP': 4, 'D': 5, 'F': 7, 'G': 9}
            for symbol, basis in self.basisset.items():
                symbol_count = symbols.count(symbol)
                for subshell, contraction in basis:
                    self.num_basis += symbol_count * subshell_counts[subshell]
            # Density matrix.
            line = skip_to(f, ' DENSITY MATRIX DIRECT LATTICE')
            if line is None:
                raise FileFormatError('Could not find the density matrix')
            f.readline()
            f.readline()
            f.readline()
            self.density_matrix = np.zeros((self.num_basis, self.num_basis),
                                           float)
            j0 = 0
            while j0 < self.num_basis:
                f.readline()
                f.readline()
                f.readline()
                for i in range(self.num_basis):
                    line = f.readline()
                    words = line.split()[1:]
                    for j1, word in enumerate(words):
                        self.density_matrix[i, j0 + j1] = float(word)
                j0 += 10