How to use the pymatgen.core.structure.Molecule function in pymatgen

To help you get started, we’ve selected a few pymatgen examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github materialsproject / pymatgen / pymatgen / io / nwchem.py View on Github external
for l in output.split("\n"):
            for e, v in error_defs.items():
                if l.find(e) != -1:
                    errors.append(v)
            if parse_time:
                m = time_patt.search(l)
                if m:
                    time = m.group(1)
                    parse_time = False
            if parse_geom:
                if l.strip() == "Atomic Mass":
                    if lattice:
                        structures.append(Structure(lattice, species, coords,
                                                    coords_are_cartesian=True))
                    else:
                        molecules.append(Molecule(species, coords))
                    species = []
                    coords = []
                    lattice = []
                    parse_geom = False
                else:
                    m = coord_patt.search(l)
                    if m:
                        species.append(m.group(1).capitalize())
                        coords.append([float(m.group(2)), float(m.group(3)),
                                       float(m.group(4))])
                    m = lat_vector_patt.search(l)
                    if m:
                        lattice.append([float(m.group(1)), float(m.group(2)),
                                        float(m.group(3))])

            if parse_force:
github materialsproject / pymatgen / pymatgen / io / qchem_deprecated.py View on Github external
else:
                        qctask = QcTask.from_string('\n'.join(qctask_lines))
                        jobtype = qctask.params["rem"]["jobtype"]
                        parse_input = False
                        continue
                qctask_lines.append(line)
            elif parse_coords:
                if "-" * 50 in line:
                    if len(coords) == 0:
                        continue
                    else:
                        if qctask and qctask.ghost_atoms:
                            if isinstance(qctask.mol, Molecule):
                                for i in qctask.ghost_atoms:
                                    species[i] = qctask.mol.sites[i].specie.symbol
                        molecules.append(Molecule(species, coords))
                        coords = []
                        species = []
                        parse_coords = False
                        continue
                if "Atom" in line:
                    continue
                m = coord_pattern.match(line)
                coords.append([float(m.group("x")), float(m.group("y")),
                              float(m.group("z"))])
                species.append(m.group("element"))
            elif parse_scf_iter:
                if "SCF time:  CPU" in line:
                    parse_scf_iter = False
                    continue
                if 'Convergence criterion met' in line and gen_scfman:
                    scf_successful = True
github materialsproject / pymatgen / pymatgen / io / abinitio / input.py View on Github external
def boxed_molecule(cls, pseudos, cart_coords, acell=3*(10,)):
        """
        Creates a molecule in a periodic box of lengths acell [Bohr]

        Args:
            pseudos:
                List of pseudopotentials
            cart_coords: 
                Cartesian coordinates
            acell:
                Lengths of the box in *Bohr*
        """
        cart_coords = np.atleast_2d(cart_coords)
                                                                                 
        molecule = Molecule([p.symbol for p in pseudos], cart_coords)
                                                                                 
        l = Bohr2Ang(acell)
                                                                                 
        structure = molecule.get_boxed_structure(l[0], l[1], l[2])
                                                                                 
        comment = structure.formula + " in a periodic box of size %s [Bohr]" % str(acell)
                                                                                 
        return cls(structure, pseudos, comment=comment)
github materialsproject / pymatgen / pymatgen / io / qchemio.py View on Github external
if self.mol != "read":
                raise ValueError('The only accept text value for mol is "read"')
        elif isinstance(self.mol, list):
            for m in self.mol:
                if not isinstance(m, Molecule):
                    raise ValueError("In case of type list, every element of mol must be a pymatgen Molecule")
            if self.charge is None or self.spin_multiplicity is None:
                raise ValueError("For fragments molecule section input, charge and spin_multiplicity "
                                 "must be specificed")
            total_charge = sum([m.charge for m in self.mol])
            total_unpaired_electron = sum([m.spin_multiplicity-1 for m in self.mol])
            if total_charge != self.charge:
                raise ValueError("The charge of the molecule doesn't equal to the sum of the fragment charges")
            if total_unpaired_electron % 2 != (self.spin_multiplicity - 1) % 2:
                raise ValueError("Spin multiplicity of molecule and fragments doesn't match")
        elif isinstance(self.mol, Molecule):
            self.charge = charge if charge is not None else self.mol.charge
            ghost_nelectrons = 0
            if ghost_atoms:
                for i in ghost_atoms:
                    site = self.mol.sites[i]
                    for sp, amt in site.species_and_occu.items():
                        ghost_nelectrons += sp.Z * amt
            nelectrons = self.mol.charge + self.mol.nelectrons - ghost_nelectrons - self.charge
            if spin_multiplicity is not None:
                self.spin_multiplicity = spin_multiplicity
                if (nelectrons + spin_multiplicity) % 2 != 1:
                    raise ValueError("Charge of {} and spin multiplicity of {} "
                                     "is not possible for this molecule"
                                     .format(self.charge, spin_multiplicity))
            else:
                self.spin_multiplicity = 1 if nelectrons % 2 == 0 else 2
github materialsproject / pymatgen / pymatgen / io / xyz.py View on Github external
num_sites = int(lines[0])
        coords = []
        sp = []
        coord_patt = re.compile(
            r"(\w+)\s+([0-9\-\+\.eEdD]+)\s+([0-9\-\+\.eEdD]+)\s+([0-9\-\+\.eEdD]+)"
        )
        for i in range(2, 2 + num_sites):
            m = coord_patt.search(lines[i])
            if m:
                sp.append(m.group(1))  # this is 1-indexed
                # this is 0-indexed
                # in case of 0.0D+00 or 0.00d+01 old double precision writing
                # replace d or D by e for ten power exponent
                xyz = [val.lower().replace("d", "e") for val in m.groups()[1:4]]
                coords.append([float(val) for val in xyz])
        return Molecule(sp, coords)
github materialsproject / pymatgen / pymatgen / io / qchemio.py View on Github external
def __init__(self, molecule=None, charge=None, spin_multiplicity=None,
                 jobtype='SP', title=None, exchange="HF", correlation=None,
                 basis_set="6-31+G*", aux_basis_set=None, ecp=None,
                 rem_params=None, optional_params=None, ghost_atoms=None):
        self.mol = copy.deepcopy(molecule) if molecule else "read"
        self.charge = charge
        self.spin_multiplicity = spin_multiplicity
        if isinstance(self.mol, six.string_types):
            self.mol = self.mol.lower()
            if self.mol != "read":
                raise ValueError('The only accept text value for mol is "read"')
        elif isinstance(self.mol, list):
            for m in self.mol:
                if not isinstance(m, Molecule):
                    raise ValueError("In case of type list, every element of mol must be a pymatgen Molecule")
            if self.charge is None or self.spin_multiplicity is None:
                raise ValueError("For fragments molecule section input, charge and spin_multiplicity "
                                 "must be specificed")
            total_charge = sum([m.charge for m in self.mol])
            total_unpaired_electron = sum([m.spin_multiplicity-1 for m in self.mol])
            if total_charge != self.charge:
                raise ValueError("The charge of the molecule doesn't equal to the sum of the fragment charges")
            if total_unpaired_electron % 2 != (self.spin_multiplicity - 1) % 2:
                raise ValueError("Spin multiplicity of molecule and fragments doesn't match")
        elif isinstance(self.mol, Molecule):
            self.charge = charge if charge is not None else self.mol.charge
            ghost_nelectrons = 0
            if ghost_atoms:
                for i in ghost_atoms:
                    site = self.mol.sites[i]
github materialsproject / pymatgen / pymatgen / io / babel.py View on Github external
def pymatgen_mol(self):
        """
        Returns pymatgen Molecule object.
        """
        sp = []
        coords = []
        for atom in ob.OBMolAtomIter(self._obmol):
            sp.append(atom.GetAtomicNum())
            coords.append([atom.GetX(), atom.GetY(), atom.GetZ()])
        return Molecule(sp, coords)
github materialsproject / pymatgen / pymatgen / io / qchem_deprecated.py View on Github external
"""
            The species specification can take many forms. E.g.,
            simple integers representing atomic numbers ("8"),
            actual species string ("C") or a labelled species ("C1").
            Sometimes, the species string is also not properly capitalized,
            e.g, ("c1"). This method should take care of these known formats.
            """
            try:
                return int(sp_str)
            except ValueError:
                sp = re.sub(r"\d", "", sp_str)
                return sp.capitalize()

        species = list(map(parse_species, species))

        return Molecule(species, coords)
github uw-cmg / MAST / libraries / pymatgen / pymatgen / io / gaussianio.py View on Github external
"""
            The species specification can take many forms. E.g.,
            simple integers representing atomic numbers ("8"),
            actual species string ("C") or a labelled species ("C1").
            Sometimes, the species string is also not properly capitalized,
            e.g, ("c1"). This method should take care of these known formats.
            """
            try:
                return int(sp_str)
            except ValueError:
                sp = re.sub("\d", "", sp_str)
                return sp.capitalize()

        species = map(parse_species, species)

        return Molecule(species, coords)
github materialsproject / pymatgen / pymatgen / io / smartio.py View on Github external
Returns:
        A Molecule object.
    """
    fname = os.path.basename(filename)
    if fnmatch(fname.lower(), "*.xyz*"):
        return XYZ.from_file(filename).molecule
    elif any([fnmatch(fname.lower(), "*.{}*".format(r))
              for r in ["gjf", "g03", "g09", "com", "inp"]]):
        return GaussianInput.from_file(filename).molecule
    elif any([fnmatch(fname.lower(), "*.{}*".format(r))
              for r in ["out", "lis", "log"]]):
        return GaussianOutput(filename).final_structure
    elif fnmatch(fname, "*.json*") or fnmatch(fname, "*.mson*"):
        with zopen(filename) as f:
            s = json.load(f, cls=MontyDecoder)
            if type(s) != Molecule:
                raise IOError("File does not contain a valid serialized "
                              "molecule")
            return s
    else:
        m = re.search("\.(pdb|mol|mdl|sdf|sd|ml2|sy2|mol2|cml|mrv)",
                      filename.lower())
        if m:
            return BabelMolAdaptor.from_file(filename,
                                             m.group(1)).pymatgen_mol

    raise ValueError("Unrecognized file extension!")