def run_task(self, fw_spec):
        db = SPStructuresMongoAdapter.auto_load()
        tstructs = []
        species = fw_spec['species']
        t = fw_spec['threshold']
        for p in SubstitutionPredictor(threshold=t).list_prediction(species):
            subs = p['substitutions']
            if len(set(subs.values())) < len(species):
                continue
            st = SubstitutionTransformation(subs)
            target = map(str, subs.keys())
            for snl in db.get_snls(target):
                ts = TransformedStructure.from_snl(snl)
                ts.append_transformation(st)
                if ts.final_structure.charge == 0:
                    tstructs.append(ts)

        transmuter = StandardTransmuter(tstructs)
        f = RemoveDuplicatesFilter(structure_matcher=StructureMatcher(
            comparator=ElementComparator(), primitive_cell=False))
        transmuter.apply_filter(f)
        results = []
        for ts in transmuter.transformed_structures:
            results.append(ts.to_snl([]).to_dict)
        submissions = SPSubmissionsMongoAdapter.auto_load()
        submissions.insert_results(fw_spec['submission_id'], results)
Beispiel #2
0
    def __init__(self,
                 queryengine,
                 criteria,
                 transformations,
                 extend_collection=0,
                 ncores=None):
        """Constructor.

        Args:
            queryengine:
                QueryEngine object for database access
            criteria:
                A criteria to search on, which is passed to queryengine's
                get_entries method.
            transformations:
                New transformations to be applied to all structures
            extend_collection:
                Whether to use more than one output structure from one-to-many
                transformations. extend_collection can be a number, which
                determines the maximum branching for each transformation.
            ncores:
                Number of cores to use for applying transformations.
                Uses multiprocessing.Pool
        """
        entries = queryengine.get_entries(criteria, inc_structure=True)

        source = "{}:{}/{}/{}".format(queryengine.host, queryengine.port,
                                      queryengine.database_name,
                                      queryengine.collection_name)

        def get_history(entry):
            return [{
                "source": source,
                "criteria": criteria,
                "entry": entry.as_dict(),
                "datetime": datetime.datetime.utcnow()
            }]

        transformed_structures = [
            TransformedStructure(entry.structure, [],
                                 history=get_history(entry))
            for entry in entries
        ]
        StandardTransmuter.__init__(self,
                                    transformed_structures,
                                    transformations=transformations,
                                    extend_collection=extend_collection,
                                    ncores=ncores)
    def run_task(self, fw_spec):

        transformations = []
        transformation_params = self.get(
            "transformation_params",
            [{} for i in range(len(self["transformations"]))])
        for t in self["transformations"]:
            for m in [
                    "advanced_transformations", "defect_transformations",
                    "site_transformations", "standard_transformations"
            ]:
                mod = __import__("pymatgen.transformations." + m, globals(),
                                 locals(), [t], -1)
                try:
                    t_cls = getattr(mod, t)
                except AttributeError:
                    continue
                t_obj = t_cls(**transformation_params.pop(0))
                transformations.append(t_obj)

        structure = self['structure'] if 'prev_calc_dir' not in self else \
                Poscar.from_file(os.path.join(self['prev_calc_dir'], 'POSCAR')).structure
        ts = TransformedStructure(structure)
        transmuter = StandardTransmuter([ts], transformations)
        final_structure = transmuter.transformed_structures[
            -1].final_structure.copy()

        vis_orig = self["vasp_input_set"]
        vis_dict = vis_orig.as_dict()
        vis_dict["structure"] = final_structure.as_dict()
        vis_dict.update(self.get("override_default_vasp_params", {}) or {})
        vis = vis_orig.__class__.from_dict(vis_dict)
        vis.write_input(".")
 def test_filter(self):
     transmuter = StandardTransmuter.from_structures(self._struct_list)
     fil = RemoveDuplicatesFilter()
     transmuter.apply_filter(fil)
     out = self._sm.group_structures(transmuter.transformed_structures)
     self.assertEqual(self._sm.find_indexes(transmuter.transformed_structures, out),
         [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Beispiel #5
0
    def run_task(self, fw_spec):

        transformations = []
        transformation_params = self.get("transformation_params",
                                         [{} for i in range(len(self["transformations"]))])
        for t in self["transformations"]:
            found = False
            for m in ["advanced_transformations", "defect_transformations",
                      "site_transformations", "standard_transformations"]:
                mod = import_module("pymatgen.transformations.{}".format(m))
                try:
                    t_cls = getattr(mod, t)
                except AttributeError:
                    continue
                t_obj = t_cls(**transformation_params.pop(0))
                transformations.append(t_obj)
                found = True
            if not found:
                raise ValueError("Could not find transformation: {}".format(t))
        
        # TODO: @matk86 - should prev_calc_dir use CONTCAR instead of POSCAR? Note that if
        # current dir, maybe it is POSCAR indeed best ... -computron
        structure = self['structure'] if not self.get('prev_calc_dir', None) else \
                Poscar.from_file(os.path.join(self['prev_calc_dir'], 'POSCAR')).structure
        ts = TransformedStructure(structure)
        transmuter = StandardTransmuter([ts], transformations)
        final_structure = transmuter.transformed_structures[-1].final_structure.copy()
        vis_orig = self["vasp_input_set"]
        vis_dict = vis_orig.as_dict()
        vis_dict["structure"] = final_structure.as_dict()
        vis_dict.update(self.get("override_default_vasp_params", {}) or {})
        vis = vis_orig.__class__.from_dict(vis_dict)
        vis.write_input(".")
Beispiel #6
0
 def test_filter(self):
     fil = RemoveExistingFilter(self._exisiting_structures)
     transmuter = StandardTransmuter.from_structures(self._struct_list)
     transmuter.apply_filter(fil)
     self.assertEqual(len(transmuter.transformed_structures), 1)
     self.assertTrue(
         self._sm.fit(self._struct_list[-1],
                      transmuter.transformed_structures[-1].final_structure))
Beispiel #7
0
 def test_filter(self):
     fil = RemoveExistingFilter(self._exisiting_structures)
     transmuter = StandardTransmuter.from_structures(self._struct_list)
     transmuter.apply_filter(fil)
     self.assertEqual(len(transmuter.transformed_structures), 1)
     self.assertTrue(
         self._sm.fit(self._struct_list[-1],
                      transmuter.transformed_structures[-1].final_structure))
Beispiel #8
0
 def test_filter(self):
     transmuter = StandardTransmuter.from_structures(self._struct_list)
     fil = RemoveDuplicatesFilter()
     transmuter.apply_filter(fil)
     out = self._sm.group_structures(transmuter.transformed_structures)
     self.assertEqual(
         self._sm.find_indexes(transmuter.transformed_structures, out),
         [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Beispiel #9
0
    def __init__(self, queryengine, criteria, transformations,
                 extend_collection=0, ncores=None):
        """Constructor.

        Args:
            queryengine:
                QueryEngine object for database access
            criteria:
                A criteria to search on, which is passed to queryengine's
                get_entries method.
            transformations:
                New transformations to be applied to all structures
            extend_collection:
                Whether to use more than one output structure from one-to-many
                transformations. extend_collection can be a number, which
                determines the maximum branching for each transformation.
            ncores:
                Number of cores to use for applying transformations.
                Uses multiprocessing.Pool
        """
        entries = queryengine.get_entries(criteria, inc_structure=True)

        source = "{}:{}/{}/{}".format(queryengine.host, queryengine.port,
                                      queryengine.database_name,
                                      queryengine.collection_name)

        def get_history(entry):
            return [{"source": source,
                     "criteria": criteria,
                     "entry": entry.as_dict(),
                     "datetime": datetime.datetime.utcnow()}]

        transformed_structures = [TransformedStructure(
            entry.structure, [], history=get_history(entry))
            for entry in entries]
        StandardTransmuter.__init__(self, transformed_structures,
                                    transformations=transformations,
                                    extend_collection=extend_collection,
                                    ncores=ncores)
Beispiel #10
0
    def run_task(self, fw_spec):

        transformations = []
        transformation_params = self.get(
            "transformation_params",
            [{} for _ in range(len(self["transformations"]))],
        )
        for t in self["transformations"]:
            found = False
            t_cls = None
            for m in [
                    "advanced_transformations",
                    "defect_transformations",
                    "site_transformations",
                    "standard_transformations",
            ]:
                mod = import_module(f"pymatgen.transformations.{m}")

                try:
                    t_cls = getattr(mod, t)
                    found = True
                    continue
                except AttributeError:
                    pass

            if not found:
                raise ValueError(f"Could not find transformation: {t}")

            t_obj = t_cls(**transformation_params.pop(0))
            transformations.append(t_obj)

        # TODO: @matk86 - should prev_calc_dir use CONTCAR instead of POSCAR?
        #  Note that if current dir, maybe POSCAR is indeed best ... -computron
        structure = (self["structure"] if not self.get("prev_calc_dir", None)
                     else Poscar.from_file(
                         os.path.join(self["prev_calc_dir"],
                                      "POSCAR")).structure)
        ts = TransformedStructure(structure)
        transmuter = StandardTransmuter([ts], transformations)
        final_structure = transmuter.transformed_structures[
            -1].final_structure.copy()
        vis_orig = self["vasp_input_set"]
        vis_dict = vis_orig.as_dict()
        vis_dict["structure"] = final_structure.as_dict()
        vis_dict.update(self.get("override_default_vasp_params", {}) or {})
        vis = vis_orig.__class__.from_dict(vis_dict)

        potcar_spec = self.get("potcar_spec", False)
        vis.write_input(".", potcar_spec=potcar_spec)

        dumpfn(transmuter.transformed_structures[-1], "transformations.json")
Beispiel #11
0
def convert_fmt(args):
    iformat = args.input_format[0]
    oformat = args.output_format[0]
    filename = args.input_filename[0]
    out_filename = args.output_filename[0]

    try:
        if iformat == "smart":
            structure = read_structure(filename)
        if iformat == "POSCAR":
            p = Poscar.from_file(filename)
            structure = p.structure
        elif iformat == "CIF":
            r = CifParser(filename)
            structure = r.get_structures()[0]
        elif iformat == "CSSR":
            structure = Cssr.from_file(filename).structure

        if oformat == "smart":
            write_structure(structure, out_filename)
        elif oformat == "POSCAR":
            p = Poscar(structure)
            p.write_file(out_filename)
        elif oformat == "CIF":
            w = CifWriter(structure)
            w.write_file(out_filename)
        elif oformat == "CSSR":
            c = Cssr(structure)
            c.write_file(out_filename)
        elif oformat == "VASP":
            input_set = MaterialsProjectVaspInputSet()
            transmuter = StandardTransmuter.from_structures([structure], [])
            transmuter.write_vasp_input(input_set, output_dir=out_filename)

    except Exception as ex:
        print "Error converting file. Are they in the right format?"
        print str(ex)
Beispiel #12
0
    def pred_from_structures(self,
                             target_species,
                             structures_list,
                             remove_duplicates=True,
                             remove_existing=False):
        """
        performs a structure prediction targeting compounds containing all of
        the target_species, based on a list of structure (those structures
        can for instance come from a database like the ICSD). It will return
        all the structures formed by ionic substitutions with a probability
        higher than the threshold

        Notes:
        If the default probability model is used, input structures must
        be oxidation state decorated. See AutoOxiStateDecorationTransformation

        This method does not change the number of species in a structure. i.e
        if the number of target species is 3, only input structures containing
        3 species will be considered.

        Args:
            target_species:
                a list of species with oxidation states
                e.g., [Specie('Li',1),Specie('Ni',2), Specie('O',-2)]

            structures_list:
                a list of dictionnary of the form {'structure':Structure object
                ,'id':some id where it comes from}
                the id can for instance refer to an ICSD id.

            remove_duplicates:
                if True, the duplicates in the predicted structures will
                be removed

            remove_existing:
                if True, the predicted structures that already exist in the
                structures_list will be removed

        Returns:
            a list of TransformedStructure objects.
        """
        target_species = get_el_sp(target_species)
        result = []
        transmuter = StandardTransmuter([])
        if len(list(set(target_species) & set(self.get_allowed_species()))) \
                != len(target_species):
            raise ValueError("the species in target_species are not allowed " +
                             "for the probability model you are using")

        for permut in itertools.permutations(target_species):
            for s in structures_list:
                # check if: species are in the domain,
                # and the probability of subst. is above the threshold
                els = s['structure'].composition.elements
                if len(els) == len(permut) and len(list(set(els) & set(self.get_allowed_species()))) == \
                        len(els) and self._sp.cond_prob_list(permut, els) > self._threshold:

                    clean_subst = {
                        els[i]: permut[i]
                        for i in range(0, len(els)) if els[i] != permut[i]
                    }

                    if len(clean_subst) == 0:
                        continue

                    transf = SubstitutionTransformation(clean_subst)

                    if Substitutor._is_charge_balanced(
                            transf.apply_transformation(s['structure'])):
                        ts = TransformedStructure(s['structure'], [transf],
                                                  history=[{
                                                      "source": s['id']
                                                  }],
                                                  other_parameters={
                                                      'type':
                                                      'structure_prediction',
                                                      'proba':
                                                      self._sp.cond_prob_list(
                                                          permut, els)
                                                  })
                        result.append(ts)
                        transmuter.append_transformed_structures([ts])

        if remove_duplicates:
            transmuter.apply_filter(
                RemoveDuplicatesFilter(symprec=self._symprec))
        if remove_existing:
            # Make the list of structures from structures_list that corresponds to the
            # target species
            chemsys = list(set([sp.symbol for sp in target_species]))
            structures_list_target = [
                st['structure'] for st in structures_list
                if Substitutor._is_from_chemical_system(
                    chemsys, st['structure'])
            ]
            transmuter.apply_filter(
                RemoveExistingFilter(structures_list_target,
                                     symprec=self._symprec))
        return transmuter.transformed_structures
Beispiel #13
0
 def test_filter(self):
     transmuter = StandardTransmuter.from_structures(self._struct_list)
     fil = RemoveDuplicatesFilter()
     transmuter.apply_filter(fil)
     self.assertEqual(len(transmuter.transformed_structures), 11)
Beispiel #14
0
 def test_filter(self):
     transmuter = StandardTransmuter.from_structures(self._struct_list)
     fil = RemoveDuplicatesFilter()
     transmuter.apply_filter(fil)
     self.assertEqual(len(transmuter.transformed_structures), 11)
Beispiel #15
0
    def pred_from_structures(self, target_species, structures_list,
                             remove_duplicates=True):
        """
        performs a structure prediction targeting compounds containing the
        target_species and based on a list of structure (those structures
        can for instance come from a database like the ICSD). It will return
        all the structures formed by ionic substitutions with a probability
        higher than the threshold

        Args:
            target_species:
                a list of species with oxidation states
                e.g., [Specie('Li',1),Specie('Ni',2), Specie('O',-2)]

            structures_list:
                a list of dictionnary of the form {'structure':Structure object
                ,'id':some id where it comes from}
                the id can for instance refer to an ICSD id

        Returns:
            a list of TransformedStructure objects.
        """
        result = []
        transmuter = StandardTransmuter([])
        if len(list(set(target_species) & set(self.get_allowed_species()))) \
                != len(target_species):
            raise ValueError("the species in target_species are not allowed"
                              + "for the probability model you are using")

        for permut in itertools.permutations(target_species):
            for s in structures_list:
                #check if: species are in the domain,
                #and the probability of subst. is above the threshold
                els = s['structure'].composition.elements
                if len(els) == len(permut) and \
                    len(list(set(els) & set(self.get_allowed_species()))) == \
                        len(els) and self._sp.cond_prob_list(permut, els) > \
                        self._threshold:

                    clean_subst = {els[i]: permut[i]
                                   for i in xrange(0, len(els))
                                   if els[i] != permut[i]}

                    if len(clean_subst) == 0:
                        continue

                    transf = SubstitutionTransformation(clean_subst)

                    if Substitutor._is_charge_balanced(
                            transf.apply_transformation(s['structure'])):
                        ts = TransformedStructure(
                            s['structure'], [transf], history=[s['id']],
                            other_parameters={
                                'type': 'structure_prediction',
                                'proba': self._sp.cond_prob_list(permut, els)}
                        )
                        result.append(ts)
                        transmuter.append_transformed_structures([ts])

        if remove_duplicates:
            transmuter.apply_filter(RemoveDuplicatesFilter(
                symprec=self._symprec))
        return transmuter.transformed_structures
def generate_ewald_orderings(path, choose_file, oxidation_states,
                             num_structures):
    """
    DESCRIPTION: Given a disordered CIF structure with at least one crystallographic site that is shared by more than one element, all permutations
                 will have their electrostatic energy calculated via an Ewald summation, given that all ion charges are specified. Ordered CIF structures will 
                 be generated, postpended with a number that indicates the stability ranking of the structure. For example, if a CIF file called 
                 "Na2Mn2Fe(VO4)3.cif" is inputted with num_structures=3, then the function will generate 3 ordered output files, 
                 "Na2Mn2Fe(VO4)3-ewald-1", "Na2Mn2Fe(VO4)3-ewald-2", and "Na2Mn2Fe(VO4)3-ewald-3", with "Na2Mn2Fe(VO4)3-ewald-1" being the most stable and
                 "Na2Mn2Fe(VO4)3-ewald-3" being the least stable. Note that this function does not take into account the symmetry of the crystal, and thus
                 it may give several structures which are symmetrically identical under a space group. Use "find_unique_structures" to isolate unique orderings.
    PARAMETERS:
        path: string
            The file path to the CIF file. The CIF file must be a disordered structure, or else an error will occur. A disordered structure will have one
            site that is occupied by more than one element, with occupancies less than 1: i.e. Fe at 0,0,0.5 with an occupancy of 0.75, and Mn at the same 
            site 0,0,0.5 with an occupancy of 0.25. This function cannot handle multivalent elements, for example it cannot handle a structure that has Mn
            in both the 2+ and 3+ redox state.
        choose_file: boolean
            Setting this parameter to True brings up the file explorer dialog for the user to manually select the CIF file
        oxidation_states: dictionary
            A dictionary that maps each element in the structure to a particular oxidation state. E.g. {"Fe": 3, "Mn": 2, "O": -2, "V": 5, "Na": 1, "Al":3}
            Make sure that all elements in the structure is assigned an oxidation state. It is ok to add more elements than needed.
        num_structures: int
            The number of strcutures to be outputted by the function. There can be hundreds or thousands of candidate structures, however in practice only
            the first few (or even only the first, most stable) structures are needed.
    RETURNS: None
    """
    # open file dialog if file is to be manually chosen
    if choose_file:
        root = tk.Tk()
        root.withdraw()
        path = filedialog.askopenfilename()
    #Read cif file
    cryst = Structure.from_file(path)
    analyzer = SpacegroupAnalyzer(cryst)
    space_group = analyzer.get_space_group_symbol()
    print(space_group)
    symm_struct = analyzer.get_symmetrized_structure()
    cryst = TransformedStructure(symm_struct)

    #Create Oxidation State Transform
    oxidation_transform = OxidationStateDecorationTransformation(
        oxidation_states)

    #Create Ewald Ordering Transform object which will be passed into the transmuter
    ordering_transform = OrderDisorderedStructureTransformation(
        symmetrized_structures=True)

    # apply the order-disorder transform on the structure for any site that has fractional occupancies
    transmuter = StandardTransmuter([cryst],
                                    [oxidation_transform, ordering_transform],
                                    extend_collection=num_structures)
    print("Ewald optimization successful!")
    num_structures = len(transmuter.transformed_structures)
    for i in range(num_structures):
        newCryst = transmuter.transformed_structures[i].final_structure
        #Save to CIF
        structure_name = os.path.splitext(os.path.basename(path))[0]
        save_directory = structure_name
        if not os.path.isdir(save_directory):
            os.mkdir(save_directory)
        filename = structure_name + '/' + structure_name + '-ewald' + '-%i' % (
            i + 1)
        w = CifWriter(newCryst)
        w.write_file(filename + '.cif')
        print("Cif file saved to {}.cif".format(filename))
        #Save to POSCAR
        poscar = Poscar(newCryst)
        poscar.write_file(filename)
        print("POSCAR file saved to {}".format(filename))
Beispiel #17
0
    def pred_from_structures(self, target_species, structures_list, remove_duplicates=True, remove_existing=False):
        """
        performs a structure prediction targeting compounds containing all of 
        the target_species, based on a list of structure (those structures
        can for instance come from a database like the ICSD). It will return
        all the structures formed by ionic substitutions with a probability
        higher than the threshold
        
        Notes:
        If the default probability model is used, input structures must
        be oxidation state decorated.
        
        This method does not change the number of species in a structure. i.e
        if the number of target species is 3, only input structures containing
        3 species will be considered.

        Args:
            target_species:
                a list of species with oxidation states
                e.g., [Specie('Li',1),Specie('Ni',2), Specie('O',-2)]

            structures_list:
                a list of dictionnary of the form {'structure':Structure object
                ,'id':some id where it comes from}
                the id can for instance refer to an ICSD id.

            remove_duplicates:
                if True, the duplicates in the predicted structures will
                be removed

            remove_existing:
                if True, the predicted structures that already exist in the
                structures_list will be removed

        Returns:
            a list of TransformedStructure objects.
        """
        result = []
        transmuter = StandardTransmuter([])
        if len(list(set(target_species) & set(self.get_allowed_species()))) != len(target_species):
            raise ValueError(
                "the species in target_species are not allowed" + "for the probability model you are using"
            )

        for permut in itertools.permutations(target_species):
            for s in structures_list:
                # check if: species are in the domain,
                # and the probability of subst. is above the threshold
                els = s["structure"].composition.elements
                if (
                    len(els) == len(permut)
                    and len(list(set(els) & set(self.get_allowed_species()))) == len(els)
                    and self._sp.cond_prob_list(permut, els) > self._threshold
                ):

                    clean_subst = {els[i]: permut[i] for i in xrange(0, len(els)) if els[i] != permut[i]}

                    if len(clean_subst) == 0:
                        continue

                    transf = SubstitutionTransformation(clean_subst)

                    if Substitutor._is_charge_balanced(transf.apply_transformation(s["structure"])):
                        ts = TransformedStructure(
                            s["structure"],
                            [transf],
                            history=[{"source": s["id"]}],
                            other_parameters={
                                "type": "structure_prediction",
                                "proba": self._sp.cond_prob_list(permut, els),
                            },
                        )
                        result.append(ts)
                        transmuter.append_transformed_structures([ts])

        if remove_duplicates:
            transmuter.apply_filter(RemoveDuplicatesFilter(symprec=self._symprec))
        if remove_existing:
            # Make the list of structures from structures_list that corresponds to the
            # target species
            chemsys = list(set([sp.symbol for sp in target_species]))
            structures_list_target = [
                st["structure"]
                for st in structures_list
                if Substitutor._is_from_chemical_system(chemsys, st["structure"])
            ]
            transmuter.apply_filter(RemoveExistingFilter(structures_list_target, symprec=self._symprec))
        return transmuter.transformed_structures