示例#1
0
 def test_append_transformation(self):
     t = SubstitutionTransformation({"Fe": "Mn"})
     self.trans.append_transformation(t)
     self.assertEqual(
         "NaMnPO4", self.trans.final_structure.composition.reduced_formula)
     self.assertEqual(len(self.trans.structures), 3)
     coords = list()
     coords.append([0, 0, 0])
     coords.append([0.75, 0.5, 0.75])
     lattice = [
         [3.8401979337, 0.00, 0.00],
         [1.9200989668, 3.3257101909, 0.00],
         [0.00, -2.2171384943, 3.1355090603],
     ]
     struct = Structure(lattice, ["Si4+", "Si4+"], coords)
     ts = TransformedStructure(struct, [])
     ts.append_transformation(
         SupercellTransformation.from_scaling_factors(2, 1, 1))
     alt = ts.append_transformation(
         PartialRemoveSpecieTransformation(
             "Si4+",
             0.5,
             algo=PartialRemoveSpecieTransformation.ALGO_COMPLETE),
         5,
     )
     self.assertEqual(len(alt), 2)
示例#2
0
def solid_solution_sqs(structure, elem_frac_site, elem_frac_comp, sqs_scaling):
    '''
    Use pymatgen SQSTransformation (which call ATAT mcsqs program)
    to generate disordered structure. 

    Args:
        structure: pymatgen structure
        elem_frac_site: the factional occupy site in the original structure, e.g. 'Ti'
        elem_frac_comp: solid solution composition, e.g. 'Ti0.25Zr0.25Hf0.25Nb0.25'
        sqs_scaling (int or list): (same as pymatgen scaling in SQSTransformation)
                    Scaling factor to determine supercell. Two options are possible:
                    a. (preferred) Scales number of atoms, e.g., for a structure with 8 atoms,
                       scaling=4 would lead to a 32 atom supercell
                    b. A sequence of three scaling factors, e.g., [2, 1, 1], which
                       specifies that the supercell should have dimensions 2a x b x c
    Return:
        pymatgen structure, SQS                   
    '''
    # build another pymatgen structure
    structure[elem_frac_site] = elem_frac_comp
    ts = TransformedStructure(structure, [])

    # the directory must be set in SQSTransformation, otherwise the work dir
    # will be changed by this function
    workdir = os.getcwd()
    ts.append_transformation(
        SQSTransformation(scaling=sqs_scaling,
                          search_time=1,
                          directory=workdir,
                          reduction_algo=False))
    return ts.structures[-1]
示例#3
0
 def setUp(self):
     structure_dict = {
         "lattice": {"a": 4.754150115,
                     "volume": 302.935463898643,
                     "c": 10.462573348,
                     "b": 6.090300362,
                     "matrix": [[4.754150115, 0.0, 0.0],
                                [0.0, 6.090300362, 0.0],
                                [0.0, 0.0, 10.462573348]],
                     "alpha": 90.0,
                     "beta": 90.0,
                     "gamma": 90.0},
         "sites": [{"occu": 1, "abc": [0.0, 0.0, 0.0],
                    "xyz": [0.0, 0.0, 0.0],
                    "species": [{"occu": 1, "element": "Li"}],
                    "label": "Li"}, {"occu": 1,
                                     "abc": [0.5000010396179928, 0.0,
                                             0.5000003178950235],
                                     "xyz": [2.37708, 0.0, 5.23129],
                                     "species": [{"occu": 1,
                                                  "element": "Li"}],
                                     "label": "Li"},
                                 {"occu": 1, "abc": [0.0,
                                                     0.49999997028061194,
                                                     0.0],
                                  "xyz": [0.0, 3.04515, 0.0],
                                  "species": [{"occu": 1, "element": "Li"}], "label": "Li"}, {"occu": 1, "abc": [0.5000010396179928, 0.49999997028061194, 0.5000003178950235], "xyz": [2.37708, 3.04515, 5.23129], "species": [{"occu": 1, "element": "Li"}], "label": "Li"}, {"occu": 1, "abc": [0.7885825876997996, 0.5473161916279229, 0.3339168944194627], "xyz": [3.74904, 3.33332, 3.4936300000000005], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.2114173881108085, 0.452683748933301, 0.6660827855827808], "xyz": [1.00511, 2.75698, 6.968940000000001], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7114184277288014, 0.5473161916279229, 0.8339172123144861], "xyz": [3.38219, 3.33332, 8.72492], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7885825876997996, 0.9526820772587701, 0.3339168944194627], "xyz": [3.74904, 5.8021199999999995, 3.4936300000000005], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.28858365150718424, 0.047317863302453654, 0.16608342347556082], "xyz": [1.37197, 0.28818, 1.73766], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7440972443925447, 0.25000080611787734, 0.09613791622232937], "xyz": [3.537549999999999, 1.52258, 1.00585], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.28858365150718424, 0.452683748933301, 0.16608342347556082], "xyz": [1.37197, 2.75698, 1.73766], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.2114173881108085, 0.047317863302453654, 0.6660827855827808], "xyz": [1.00511, 0.28818, 6.968940000000001], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.2559006279926859, 0.7499991344433464, 0.9038627195677177], "xyz": [1.21659, 4.56772, 9.45673], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7559016676106785, 0.25000080611787734, 0.5961372783295493], "xyz": [3.5936699999999986, 1.52258, 6.2371300000000005], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7939989080466804, 0.7499991344433464, 0.5421304884886912], "xyz": [3.77479, 4.56772, 5.67208], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.24409830819992942, 0.7499991344433464, 0.40386240167269416], "xyz": [1.16048, 4.56772, 4.22544], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7060021073819206, 0.7499991344433464, 0.04213017059366761], "xyz": [3.35644, 4.56772, 0.44079000000000007], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.2939978684286875, 0.25000080611787734, 0.9578695094085758], "xyz": [1.3977099999999996, 1.52258, 10.02178], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.20600106776392774, 0.25000080611787734, 0.4578701473013559], "xyz": [0.9793599999999998, 1.52258, 4.7905], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7114184277288014, 0.9526820772587701, 0.8339172123144861], "xyz": [3.38219, 5.8021199999999995, 8.72492], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.5793611756830275, 0.7499991344433464, 0.9051119342269868], "xyz": [2.75437, 4.56772, 9.4698], "species": [{"occu": 1, "element": "P"}], "label": "P"}, {"occu": 1, "abc": [0.9206377363201961, 0.7499991344433464, 0.40511161633196324], "xyz": [4.37685, 4.56772, 4.23851], "species": [{"occu": 1, "element": "P"}], "label": "P"}, {"occu": 1, "abc": [0.42063880012758065, 0.25000080611787734, 0.09488774577525667], "xyz": [1.9997799999999994, 1.52258, 0.99277], "species": [{"occu": 1, "element": "P"}], "label": "P"}, {"occu": 1, "abc": [0.07936223949041206, 0.25000080611787734, 0.5948880636702801], "xyz": [0.3773, 1.52258, 6.22406], "species": [{"occu": 1, "element": "P"}], "label": "P"}, {"occu": 1, "abc": [0.021860899947623972, 0.7499991344433464, 0.7185507570598875], "xyz": [0.10393, 4.56772, 7.517890000000001], "species": [{"occu": 1, "element": "Fe"}], "label": "Fe"}, {"occu": 1, "abc": [0.478135932819614, 0.7499991344433464, 0.21855043916486389], "xyz": [2.27313, 4.56772, 2.2866], "species": [{"occu": 1, "element": "Fe"}], "label": "Fe"}, {"occu": 1, "abc": [0.9781369724376069, 0.25000080611787734, 0.2814489229423561], "xyz": [4.65021, 1.52258, 2.9446800000000004], "species": [{"occu": 1, "element": "Fe"}], "label": "Fe"}, {"occu": 1, "abc": [0.5218619395656168, 0.25000080611787734, 0.7814492408373795], "xyz": [2.48101, 1.52258, 8.17597], "species": [{"occu": 1, "element": "Fe"}], "label": "Fe"}]}
     structure = Structure.from_dict(structure_dict)
     self.structure = structure
     trans = [SubstitutionTransformation({"Li": "Na"})]
     self.trans = TransformedStructure(structure, trans)
示例#4
0
 def test_append_transformation(self):
     t = SubstitutionTransformation({"Fe":"Mn"})
     self.trans.append_transformation(t)
     self.assertEqual("NaMnPO4", self.trans.final_structure.composition.reduced_formula)
     self.assertEqual(len(self.trans.structures), 3)
     coords = list()
     coords.append([0, 0, 0])
     coords.append([0.75, 0.5, 0.75])
     lattice = [[ 3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]]
     struct = Structure(lattice, ["Si4+", "Si4+"], coords)
     ts = TransformedStructure(struct, [])
     ts.append_transformation(SupercellTransformation.from_scaling_factors(2, 1, 1))
     alt = ts.append_transformation(PartialRemoveSpecieTransformation('Si4+', 0.5, algo=PartialRemoveSpecieTransformation.ALGO_COMPLETE), 5)
     self.assertEqual(len(alt), 2)
示例#5
0
    def test_snl(self):
        self.trans.set_parameter("author", "will")
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            snl = self.trans.to_snl([("will", "*****@*****.**")])
            self.assertEqual(len(w), 1, "Warning not raised on type conversion " "with other_parameters")
        ts = TransformedStructure.from_snl(snl)
        self.assertEqual(ts.history[-1]["@class"], "SubstitutionTransformation")

        h = ("testname", "testURL", {"test": "testing"})
        snl = StructureNL(ts.final_structure, [("will", "*****@*****.**")], history=[h])
        snl = TransformedStructure.from_snl(snl).to_snl([("notwill", "*****@*****.**")])
        self.assertEqual(snl.history, [h])
        self.assertEqual(snl.authors, [("notwill", "*****@*****.**")])
示例#6
0
 def setUp(self):
     structure_dict = {
         "lattice": {"a": 4.754150115,
                     "volume": 302.935463898643,
                     "c": 10.462573348,
                     "b": 6.090300362,
                     "matrix": [[4.754150115, 0.0, 0.0],
                                [0.0, 6.090300362, 0.0],
                                [0.0, 0.0, 10.462573348]],
                     "alpha": 90.0,
                     "beta": 90.0,
                     "gamma": 90.0},
         "sites": [{"occu": 1, "abc": [0.0, 0.0, 0.0],
                    "xyz": [0.0, 0.0, 0.0],
                    "species": [{"occu": 1, "element": "Li"}],
                    "label": "Li"}, {"occu": 1,
                                     "abc": [0.5000010396179928, 0.0,
                                             0.5000003178950235],
                                     "xyz": [2.37708, 0.0, 5.23129],
                                     "species": [{"occu": 1,
                                                  "element": "Li"}],
                                     "label": "Li"},
                                 {"occu": 1, "abc": [0.0,
                                                     0.49999997028061194,
                                                     0.0],
                                  "xyz": [0.0, 3.04515, 0.0],
                                  "species": [{"occu": 1, "element": "Li"}], "label": "Li"}, {"occu": 1, "abc": [0.5000010396179928, 0.49999997028061194, 0.5000003178950235], "xyz": [2.37708, 3.04515, 5.23129], "species": [{"occu": 1, "element": "Li"}], "label": "Li"}, {"occu": 1, "abc": [0.7885825876997996, 0.5473161916279229, 0.3339168944194627], "xyz": [3.74904, 3.33332, 3.4936300000000005], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.2114173881108085, 0.452683748933301, 0.6660827855827808], "xyz": [1.00511, 2.75698, 6.968940000000001], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7114184277288014, 0.5473161916279229, 0.8339172123144861], "xyz": [3.38219, 3.33332, 8.72492], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7885825876997996, 0.9526820772587701, 0.3339168944194627], "xyz": [3.74904, 5.8021199999999995, 3.4936300000000005], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.28858365150718424, 0.047317863302453654, 0.16608342347556082], "xyz": [1.37197, 0.28818, 1.73766], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7440972443925447, 0.25000080611787734, 0.09613791622232937], "xyz": [3.537549999999999, 1.52258, 1.00585], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.28858365150718424, 0.452683748933301, 0.16608342347556082], "xyz": [1.37197, 2.75698, 1.73766], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.2114173881108085, 0.047317863302453654, 0.6660827855827808], "xyz": [1.00511, 0.28818, 6.968940000000001], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.2559006279926859, 0.7499991344433464, 0.9038627195677177], "xyz": [1.21659, 4.56772, 9.45673], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7559016676106785, 0.25000080611787734, 0.5961372783295493], "xyz": [3.5936699999999986, 1.52258, 6.2371300000000005], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7939989080466804, 0.7499991344433464, 0.5421304884886912], "xyz": [3.77479, 4.56772, 5.67208], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.24409830819992942, 0.7499991344433464, 0.40386240167269416], "xyz": [1.16048, 4.56772, 4.22544], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7060021073819206, 0.7499991344433464, 0.04213017059366761], "xyz": [3.35644, 4.56772, 0.44079000000000007], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.2939978684286875, 0.25000080611787734, 0.9578695094085758], "xyz": [1.3977099999999996, 1.52258, 10.02178], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.20600106776392774, 0.25000080611787734, 0.4578701473013559], "xyz": [0.9793599999999998, 1.52258, 4.7905], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7114184277288014, 0.9526820772587701, 0.8339172123144861], "xyz": [3.38219, 5.8021199999999995, 8.72492], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.5793611756830275, 0.7499991344433464, 0.9051119342269868], "xyz": [2.75437, 4.56772, 9.4698], "species": [{"occu": 1, "element": "P"}], "label": "P"}, {"occu": 1, "abc": [0.9206377363201961, 0.7499991344433464, 0.40511161633196324], "xyz": [4.37685, 4.56772, 4.23851], "species": [{"occu": 1, "element": "P"}], "label": "P"}, {"occu": 1, "abc": [0.42063880012758065, 0.25000080611787734, 0.09488774577525667], "xyz": [1.9997799999999994, 1.52258, 0.99277], "species": [{"occu": 1, "element": "P"}], "label": "P"}, {"occu": 1, "abc": [0.07936223949041206, 0.25000080611787734, 0.5948880636702801], "xyz": [0.3773, 1.52258, 6.22406], "species": [{"occu": 1, "element": "P"}], "label": "P"}, {"occu": 1, "abc": [0.021860899947623972, 0.7499991344433464, 0.7185507570598875], "xyz": [0.10393, 4.56772, 7.517890000000001], "species": [{"occu": 1, "element": "Fe"}], "label": "Fe"}, {"occu": 1, "abc": [0.478135932819614, 0.7499991344433464, 0.21855043916486389], "xyz": [2.27313, 4.56772, 2.2866], "species": [{"occu": 1, "element": "Fe"}], "label": "Fe"}, {"occu": 1, "abc": [0.9781369724376069, 0.25000080611787734, 0.2814489229423561], "xyz": [4.65021, 1.52258, 2.9446800000000004], "species": [{"occu": 1, "element": "Fe"}], "label": "Fe"}, {"occu": 1, "abc": [0.5218619395656168, 0.25000080611787734, 0.7814492408373795], "xyz": [2.48101, 1.52258, 8.17597], "species": [{"occu": 1, "element": "Fe"}], "label": "Fe"}]}
     structure = Structure.from_dict(structure_dict)
     self.structure = structure
     trans = [SubstitutionTransformation({"Li": "Na"})]
     self.trans = TransformedStructure(structure, trans)
示例#7
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"]:
            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(".")
示例#8
0
    def __init__(self,
                 cif_string,
                 transformations=None,
                 primitive=True,
                 extend_collection=False):
        """
        Generates a Transmuter from a cif string, possibly
        containing multiple structures.

        Args:
            cif_string: A string containing a cif or a series of cifs
            transformations: New transformations to be applied to all
                structures
            primitive: Whether to generate the primitive cell from the cif.
            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.
        """
        transformed_structures = []
        lines = cif_string.split("\n")
        structure_data = []
        read_data = False
        for line in lines:
            if re.match("^\s*data", line):
                structure_data.append([])
                read_data = True
            if read_data:
                structure_data[-1].append(line)
        for data in structure_data:
            tstruct = TransformedStructure.from_cif_string(
                "\n".join(data), [], primitive)
            transformed_structures.append(tstruct)
        super(CifTransmuter, self).__init__(transformed_structures,
                                            transformations, extend_collection)
示例#9
0
    def __init__(self, cif_string, transformations=None, primitive=True,
                 extend_collection=False):
        """
        Generates a Transmuter from a cif string, possibly
        containing multiple structures.

        Args:
            cif_string:
                A string containing a cif or a series of cifs
            transformations:
                New transformations to be applied to all structures
            primitive:
                Whether to generate the primitive cell from the cif.
            extend_collection:
                Whether to use more than one output structure from one-to-many
                transformations.
        """
        transformed_structures = []
        lines = cif_string.split("\n")
        structure_data = []
        read_data = False
        for line in lines:
            if re.match("^\s*data", line):
                structure_data.append([])
                read_data = True
            if read_data:
                structure_data[-1].append(line)
        for data in structure_data:
            tstruct = TransformedStructure.from_cif_string("\n".join(data), [],
                                                           primitive)
            transformed_structures.append(tstruct)
        StandardTransmuter.__init__(self, transformed_structures,
                                    transformations, extend_collection)
示例#10
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(".")
    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)
示例#12
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 = MPVaspInputSet()
            ts = TransformedStructure(structure, [],
                                      history=[{
                                          "source":
                                          "file",
                                          "datetime":
                                          str(datetime.datetime.now()),
                                          "original_file":
                                          open(filename).read()
                                      }])
            ts.write_vasp_input(input_set, output_dir=out_filename)
        elif oformat == "MITVASP":
            input_set = MITVaspInputSet()
            ts = TransformedStructure(structure, [],
                                      history=[{
                                          "source":
                                          "file",
                                          "datetime":
                                          str(datetime.datetime.now()),
                                          "original_file":
                                          open(filename).read()
                                      }])
            ts.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)
示例#13
0
 def __init__(self,
              poscar_string,
              transformations=None,
              extend_collection=False):
     tstruct = TransformedStructure.from_poscar_string(poscar_string, [])
     StandardTransmuter.__init__(self, [tstruct],
                                 transformations,
                                 extend_collection=extend_collection)
示例#14
0
 def test_snl(self):
     self.trans.set_parameter('author', 'will')
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter("always")
         snl = self.trans.to_snl([('will', '*****@*****.**')])
         self.assertEqual(len(w), 1, 'Warning not raised on type conversion '
                          'with other_parameters')
     ts = TransformedStructure.from_snl(snl)
     self.assertEqual(ts.history[-1]['@class'], 'SubstitutionTransformation')
     
     h = ('testname', 'testURL', {'test' : 'testing'})
     snl = StructureNL(ts.final_structure,[('will', '*****@*****.**')], 
                       history = [h])
     snl = TransformedStructure.from_snl(snl).to_snl([('notwill', 
                                                       '*****@*****.**')])
     self.assertEqual(snl.history, [h])
     self.assertEqual(snl.authors, [('notwill', '*****@*****.**')])
示例#15
0
 def test_from_dict(self):
     d = json.load(open(os.path.join(test_dir, 'transformations.json'), 'r'))
     d['other_parameters'] = {'tags': ['test']}
     ts = TransformedStructure.from_dict(d)
     ts.set_parameter('author', 'Will')
     ts.append_transformation(SubstitutionTransformation({"Fe":"Mn"}))
     self.assertEqual("MnPO4", ts.final_structure.composition.reduced_formula)
     self.assertEqual(ts.other_parameters, {'author': 'Will', 'tags': ['test']})
示例#16
0
 def __init__(self,
              poscar_string,
              transformations=None,
              extend_collection=False):
     tstruct = TransformedStructure.from_poscar_string(poscar_string, [])
     super().__init__([tstruct],
                      transformations,
                      extend_collection=extend_collection)
示例#17
0
 def test_from_dict(self):
     d = json.load(open(os.path.join(test_dir, "transformations.json"), "r"))
     d["other_parameters"] = {"tags": ["test"]}
     ts = TransformedStructure.from_dict(d)
     ts.other_parameters["author"] = "Will"
     ts.append_transformation(SubstitutionTransformation({"Fe": "Mn"}))
     self.assertEqual("MnPO4", ts.final_structure.composition.reduced_formula)
     self.assertEqual(ts.other_parameters, {"author": "Will", "tags": ["test"]})
示例#18
0
 def test_from_dict(self):
     d = json.load(open(os.path.join(PymatgenTest.TEST_FILES_DIR, "transformations.json"), "r"))
     d["other_parameters"] = {"tags": ["test"]}
     ts = TransformedStructure.from_dict(d)
     ts.other_parameters["author"] = "Will"
     ts.append_transformation(SubstitutionTransformation({"Fe": "Mn"}))
     self.assertEqual("MnPO4", ts.final_structure.composition.reduced_formula)
     self.assertEqual(ts.other_parameters, {"author": "Will", "tags": ["test"]})
示例#19
0
    def test_snl(self):
        self.trans.set_parameter("author", "will")
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            snl = self.trans.to_snl([("will", "*****@*****.**")])
            self.assertEqual(
                len(w),
                1,
                "Warning not raised on type conversion " "with other_parameters",
            )
        ts = TransformedStructure.from_snl(snl)
        self.assertEqual(ts.history[-1]["@class"], "SubstitutionTransformation")

        h = ("testname", "testURL", {"test": "testing"})
        snl = StructureNL(ts.final_structure, [("will", "*****@*****.**")], history=[h])
        snl = TransformedStructure.from_snl(snl).to_snl([("notwill", "*****@*****.**")])
        self.assertEqual(snl.history, [h])
        self.assertEqual(snl.authors, [("notwill", "*****@*****.**")])
示例#20
0
 def test_undo_last_transformation_and_redo(self):
     trans = []
     trans.append(SubstitutionTransformation({"Li":"Na"}))
     trans.append(SubstitutionTransformation({"Fe":"Mn"}))
     ts = TransformedStructure(self.structure, trans)
     self.assertEqual("NaMnPO4", ts.final_structure.composition.reduced_formula)
     ts.undo_last_transformation()
     self.assertEqual("NaFePO4", ts.final_structure.composition.reduced_formula)
     ts.undo_last_transformation()
     self.assertEqual("LiFePO4", ts.final_structure.composition.reduced_formula)
     self.assertRaises(IndexError, ts.undo_last_transformation)
     ts.redo_next_transformation()
     self.assertEqual("NaFePO4", ts.final_structure.composition.reduced_formula)
     ts.redo_next_transformation()
     self.assertEqual("NaMnPO4", ts.final_structure.composition.reduced_formula)
     self.assertRaises(IndexError, ts.redo_next_transformation)
示例#21
0
 def test_from_dict(self):
     d = json.load(open(os.path.join(test_dir, 'transformations.json'),
                        'r'))
     d['other_parameters'] = {'tags': ['test']}
     ts = TransformedStructure.from_dict(d)
     ts.other_parameters['author'] = 'Will'
     ts.append_transformation(SubstitutionTransformation({"Fe": "Mn"}))
     self.assertEqual("MnPO4",
                      ts.final_structure.composition.reduced_formula)
     self.assertEqual(ts.other_parameters, {'author': 'Will',
                                            'tags': ['test']})
示例#22
0
文件: pmg.py 项目: cespejo79/pymatgen
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 == "POSCAR":
            p = Poscar.from_file(filename)
            structure = p.structure
        elif iformat == "CIF":
            r = CifParser(filename)
            structure = r.get_structures()[0]
        elif iformat == "CONVENTIONAL_CIF":
            r = CifParser(filename)
            structure = r.get_structures(primitive=False)[0]
        elif iformat == "CSSR":
            structure = Cssr.from_file(filename).structure
        else:
            structure = Structure.from_file(filename)

        if oformat == "smart":
            structure.to(filename=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":
            ts = TransformedStructure(
                structure, [],
                history=[{"source": "file",
                          "datetime": str(datetime.datetime.now()),
                          "original_file": open(filename).read()}])
            ts.write_vasp_input(MPRelaxSet, output_dir=out_filename)
        elif oformat == "MITVASP":
            ts = TransformedStructure(
                structure, [],
                history=[{"source": "file",
                          "datetime": str(datetime.datetime.now()),
                          "original_file": open(filename).read()}])
            ts.write_vasp_input(MITRelaxSet, output_dir=out_filename)

    except Exception as ex:
        print("Error converting file. Are they in the right format?")
        print(str(ex))
示例#23
0
 def __init__(self, poscar_string, transformations=None, extend_collection=False):
     """
     Args:
         poscar_string: List of POSCAR strings
         transformations: New transformations to be applied to all
             structures.
         extend_collection: Whether to use more than one output structure
             from one-to-many transformations.
     """
     tstruct = TransformedStructure.from_poscar_string(poscar_string, [])
     super().__init__(
         [tstruct], transformations, extend_collection=extend_collection
     )
示例#24
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")
示例#25
0
 def __init__(self, poscar_string, transformations=None,
              extend_collection=False):
     """
     Args:
         poscar_string:
             List of POSCAR strings
         transformations:
             New transformations to be applied to all structures.
         extend_collection:
             Whether to use more than one output structure from one-to-many
             transformations.
     """
     tstruct = TransformedStructure.from_poscar_string(poscar_string, [])
     StandardTransmuter.__init__(self, [tstruct], transformations,
                                 extend_collection=extend_collection)
示例#26
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)
示例#27
0
        def _add_metadata(structure):
            """
            For book-keeping, store useful metadata with the Structure
            object for later database ingestion including workflow
            version and a UUID for easier querying of all tasks generated
            from the workflow.

            Args:
                structure: Structure

            Returns: TransformedStructure
            """
            # this could be further improved by storing full transformation
            # history, but would require an improved transformation pipeline
            return TransformedStructure(
                structure, other_parameters={"wf_meta": self.wf_meta})
示例#28
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 = MPVaspInputSet()
            ts = TransformedStructure(
                structure,
                [],
                history=[
                    {"source": "file", "datetime": str(datetime.datetime.now()), "original_file": open(filename).read()}
                ],
            )
            ts.write_vasp_input(input_set, output_dir=out_filename)
        elif oformat == "MITVASP":
            input_set = MITVaspInputSet()
            ts = TransformedStructure(
                structure,
                [],
                history=[
                    {"source": "file", "datetime": str(datetime.datetime.now()), "original_file": open(filename).read()}
                ],
            )
            ts.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)
示例#29
0
 def __init__(self, poscar_string, transformations=[], extend_collection=False):
     """
     Generates a transmuter from a sequence of POSCARs.
     
     Args:
         poscar_string:
             List of POSCAR strings
         transformations:
             New transformations to be applied to all structures.
         primitive:
             Whether to generate the primitive cell from the cif.
         extend_collection:
             Whether to use more than one output structure from one-to-many
             transformations.
     """
     transformed_structures = []
     transformed_structures.append(TransformedStructure.from_poscar_string(poscar_string, []))
     StandardTransmuter.__init__(self, transformed_structures, transformations, extend_collection=extend_collection)
示例#30
0
    def from_filenames(poscar_filenames, transformations=None, extend_collection=False):
        """
        Convenient constructor to generates a POSCAR transmuter from a list of
        POSCAR filenames.

        Args:
            poscar_filenames: List of POSCAR filenames
            transformations: New transformations to be applied to all
                structures.
            extend_collection:
                Same meaning as in __init__.
        """
        tstructs = []
        for filename in poscar_filenames:
            with open(filename, "r") as f:
                tstructs.append(TransformedStructure.from_poscar_string(f.read(), []))
        return StandardTransmuter(
            tstructs, transformations, extend_collection=extend_collection
        )
示例#31
0
    def from_structures(structures, transformations=None, extend_collection=0):
        """
        Alternative constructor from structures rather than
        TransformedStructures.

        Args:
            structures: Sequence of structures
            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.

        Returns:
            StandardTransmuter
        """
        tstruct = [TransformedStructure(s, []) for s in structures]
        return StandardTransmuter(tstruct, transformations, extend_collection)
示例#32
0
 def test_undo_and_redo_last_change(self):
     trans = [
         SubstitutionTransformation({"Li": "Na"}),
         SubstitutionTransformation({"Fe": "Mn"})
     ]
     ts = TransformedStructure(self.structure, trans)
     self.assertEqual("NaMnPO4",
                      ts.final_structure.composition.reduced_formula)
     ts.undo_last_change()
     self.assertEqual("NaFePO4",
                      ts.final_structure.composition.reduced_formula)
     ts.undo_last_change()
     self.assertEqual("LiFePO4",
                      ts.final_structure.composition.reduced_formula)
     self.assertRaises(IndexError, ts.undo_last_change)
     ts.redo_next_change()
     self.assertEqual("NaFePO4",
                      ts.final_structure.composition.reduced_formula)
     ts.redo_next_change()
     self.assertEqual("NaMnPO4",
                      ts.final_structure.composition.reduced_formula)
     self.assertRaises(IndexError, ts.redo_next_change)
     #Make sure that this works with filters.
     f3 = ContainsSpecieFilter(['O2-'], strict_compare=True, AND=False)
     ts.append_filter(f3)
     ts.undo_last_change()
     ts.redo_next_change()
示例#33
0
 def setUp(self):
     structure = PymatgenTest.get_structure("LiFePO4")
     self.structure = structure
     trans = [SubstitutionTransformation({"Li": "Na"})]
     self.trans = TransformedStructure(structure, trans)
示例#34
0
class TransformedStructureTest(PymatgenTest):
    def setUp(self):
        structure = PymatgenTest.get_structure("LiFePO4")
        self.structure = structure
        trans = [SubstitutionTransformation({"Li": "Na"})]
        self.trans = TransformedStructure(structure, trans)

    def test_append_transformation(self):
        t = SubstitutionTransformation({"Fe": "Mn"})
        self.trans.append_transformation(t)
        self.assertEqual(
            "NaMnPO4", self.trans.final_structure.composition.reduced_formula)
        self.assertEqual(len(self.trans.structures), 3)
        coords = list()
        coords.append([0, 0, 0])
        coords.append([0.75, 0.5, 0.75])
        lattice = [[3.8401979337, 0.00, 0.00],
                   [1.9200989668, 3.3257101909, 0.00],
                   [0.00, -2.2171384943, 3.1355090603]]
        struct = Structure(lattice, ["Si4+", "Si4+"], coords)
        ts = TransformedStructure(struct, [])
        ts.append_transformation(
            SupercellTransformation.from_scaling_factors(2, 1, 1))
        alt = ts.append_transformation(
            PartialRemoveSpecieTransformation(
                'Si4+',
                0.5,
                algo=PartialRemoveSpecieTransformation.ALGO_COMPLETE), 5)
        self.assertEqual(len(alt), 2)

    def test_append_filter(self):
        f3 = ContainsSpecieFilter(['O2-'], strict_compare=True, AND=False)
        self.trans.append_filter(f3)

    def test_get_vasp_input(self):
        SETTINGS["PMG_VASP_PSP_DIR"] = os.path.abspath(
            os.path.join(os.path.dirname(__file__), "..", "..", "..",
                         "test_files"))
        potcar = self.trans.get_vasp_input(MPRelaxSet)['POTCAR']
        self.assertEqual("Na_pv\nFe_pv\nP\nO",
                         "\n".join([p.symbol for p in potcar]))
        self.assertEqual(len(self.trans.structures), 2)

    def test_final_structure(self):
        self.assertEqual(
            "NaFePO4", self.trans.final_structure.composition.reduced_formula)

    def test_from_dict(self):
        d = json.load(open(os.path.join(test_dir, 'transformations.json'),
                           'r'))
        d['other_parameters'] = {'tags': ['test']}
        ts = TransformedStructure.from_dict(d)
        ts.other_parameters['author'] = 'Will'
        ts.append_transformation(SubstitutionTransformation({"Fe": "Mn"}))
        self.assertEqual("MnPO4",
                         ts.final_structure.composition.reduced_formula)
        self.assertEqual(ts.other_parameters, {
            'author': 'Will',
            'tags': ['test']
        })

    def test_undo_and_redo_last_change(self):
        trans = [
            SubstitutionTransformation({"Li": "Na"}),
            SubstitutionTransformation({"Fe": "Mn"})
        ]
        ts = TransformedStructure(self.structure, trans)
        self.assertEqual("NaMnPO4",
                         ts.final_structure.composition.reduced_formula)
        ts.undo_last_change()
        self.assertEqual("NaFePO4",
                         ts.final_structure.composition.reduced_formula)
        ts.undo_last_change()
        self.assertEqual("LiFePO4",
                         ts.final_structure.composition.reduced_formula)
        self.assertRaises(IndexError, ts.undo_last_change)
        ts.redo_next_change()
        self.assertEqual("NaFePO4",
                         ts.final_structure.composition.reduced_formula)
        ts.redo_next_change()
        self.assertEqual("NaMnPO4",
                         ts.final_structure.composition.reduced_formula)
        self.assertRaises(IndexError, ts.redo_next_change)
        #Make sure that this works with filters.
        f3 = ContainsSpecieFilter(['O2-'], strict_compare=True, AND=False)
        ts.append_filter(f3)
        ts.undo_last_change()
        ts.redo_next_change()

    def test_as_dict(self):
        self.trans.set_parameter('author', 'will')
        d = self.trans.as_dict()
        self.assertIn('last_modified', d)
        self.assertIn('history', d)
        self.assertIn('version', d)
        self.assertIn('author', d['other_parameters'])
        self.assertEqual(Structure.from_dict(d).formula, 'Na4 Fe4 P4 O16')

    def test_snl(self):
        self.trans.set_parameter('author', 'will')
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            snl = self.trans.to_snl([('will', '*****@*****.**')])
            self.assertEqual(
                len(w), 1, 'Warning not raised on type conversion '
                'with other_parameters')
        ts = TransformedStructure.from_snl(snl)
        self.assertEqual(ts.history[-1]['@class'],
                         'SubstitutionTransformation')

        h = ('testname', 'testURL', {'test': 'testing'})
        snl = StructureNL(ts.final_structure, [('will', '*****@*****.**')],
                          history=[h])
        snl = TransformedStructure.from_snl(snl).to_snl([('notwill',
                                                          '*****@*****.**')])
        self.assertEqual(snl.history, [h])
        self.assertEqual(snl.authors, [('notwill', '*****@*****.**')])
示例#35
0
文件: calc.py 项目: utf/emmet
def prep(ctx, archive, authors):
    """prep structures from an archive for submission"""
    run = ctx.obj["RUN"]
    collections = ctx.obj["COLLECTIONS"]
    snl_collection = ctx.obj["CLIENT"].db.snls
    handler = ctx.obj["MONGO_HANDLER"]
    nmax = ctx.obj["NMAX"]
    skip = ctx.obj["SKIP"]
    # TODO no_dupe_check flag

    fname, ext = os.path.splitext(os.path.basename(archive))
    tag, sec_ext = fname.rsplit(".", 1) if "." in fname else [fname, ""]
    logger.info(click.style(f"tag: {tag}", fg="cyan"))
    if sec_ext:
        ext = "".join([sec_ext, ext])
    exts = ["tar.gz", ".tgz", "bson.gz", ".zip"]
    if ext not in exts:
        raise EmmetCliError(
            f"{ext} not supported (yet)! Please use one of {exts}.")

    meta = {"authors": [Author.parse_author(a) for a in authors]}
    references = meta.get("references", "").strip()
    source_ids_scanned = handler.collection.distinct("source_id",
                                                     {"tags": tag})

    # TODO add archive of StructureNL files
    input_structures, source_total = [], None
    if ext == "bson.gz":
        input_bson = gzip.open(archive)
        source_total = count_file_documents(input_bson)
        for doc in bson.decode_file_iter(input_bson):
            if len(input_structures) >= nmax:
                break
            if skip and doc["db_id"] in source_ids_scanned:
                continue
            elements = set([
                specie["element"] for site in doc["structure"]["sites"]
                for specie in site["species"]
            ])
            for l in SETTINGS.skip_labels:
                if l in elements:
                    logger.log(
                        logging.ERROR if run else logging.INFO,
                        f'Skip structure {doc["db_id"]}: unsupported element {l}!',
                        extra={
                            "tags": [tag],
                            "source_id": doc["db_id"]
                        },
                    )
                    break
            else:
                s = TransformedStructure.from_dict(doc["structure"])
                s.source_id = doc["db_id"]
                input_structures.append(s)
    elif ext == ".zip":
        input_zip = ZipFile(archive)
        namelist = input_zip.namelist()
        source_total = len(namelist)
        for fname in namelist:
            if len(input_structures) >= nmax:
                break
            if skip and fname in source_ids_scanned:
                continue
            contents = input_zip.read(fname)
            fmt = get_format(fname)
            s = Structure.from_str(contents, fmt=fmt)
            s.source_id = fname
            input_structures.append(s)
    else:
        tar = tarfile.open(archive, "r:gz")
        members = tar.getmembers()
        source_total = len(members)
        for member in members:
            if os.path.basename(member.name).startswith("."):
                continue
            if len(input_structures) >= nmax:
                break
            fname = member.name.lower()
            if skip and fname in source_ids_scanned:
                continue
            f = tar.extractfile(member)
            if f:
                contents = f.read().decode("utf-8")
                fmt = get_format(fname)
                s = Structure.from_str(contents, fmt=fmt)
                s.source_id = fname
                input_structures.append(s)

    total = len(input_structures)
    logger.info(
        f"{total} of {source_total} structure(s) loaded "
        f"({len(source_ids_scanned)} unique structures already scanned).")

    save_logs(ctx)
    snls, index = [], None
    for istruct in input_structures:
        # number of log messages equals number of structures processed if --run
        # only logger.warning goes to DB if --run
        if run and len(handler.buffer) >= handler.buffer_size:
            insert_snls(ctx, snls)

        struct = (istruct.final_structure if isinstance(
            istruct, TransformedStructure) else istruct)
        struct.remove_oxidation_states()
        struct = struct.get_primitive_structure()
        formula = struct.composition.reduced_formula
        sg = get_sg(struct)

        if not (struct.is_ordered and struct.is_valid()):
            logger.log(
                logging.WARNING if run else logging.INFO,
                f"Skip structure {istruct.source_id}: disordered or invalid!",
                extra={
                    "formula": formula,
                    "spacegroup": sg,
                    "tags": [tag],
                    "source_id": istruct.source_id,
                },
            )
            continue

        for full_name, coll in collections.items():
            # load canonical structures in collection for current formula and
            # duplicate-check them against current structure
            load_canonical_structures(ctx, full_name, formula)
            for canonical_structure in canonical_structures[full_name][
                    formula].get(sg, []):
                if structures_match(struct, canonical_structure):
                    logger.log(
                        logging.WARNING if run else logging.INFO,
                        f"Duplicate for {istruct.source_id} ({formula}/{sg}): {canonical_structure.id}",
                        extra={
                            "formula": formula,
                            "spacegroup": sg,
                            "tags": [tag],
                            "source_id": istruct.source_id,
                            "duplicate_dbname": full_name,
                            "duplicate_id": canonical_structure.id,
                        },
                    )
                    break
            else:
                continue  # no duplicate found -> continue to next collection

            break  # duplicate found
        else:
            # no duplicates in any collection
            prefix = snl_collection.database.name
            if index is None:
                # get start index for SNL id
                snl_ids = snl_collection.distinct("snl_id")
                index = max(
                    [int(snl_id[len(prefix) + 1:]) for snl_id in snl_ids])

            index += 1
            snl_id = "{}-{}".format(prefix, index)
            kwargs = {"references": references, "projects": [tag]}
            if isinstance(istruct, TransformedStructure):
                snl = istruct.to_snl(meta["authors"], **kwargs)
            else:
                snl = StructureNL(istruct, meta["authors"], **kwargs)

            snl_dct = snl.as_dict()
            snl_dct.update(get_meta_from_structure(struct))
            snl_dct["snl_id"] = snl_id
            snls.append(snl_dct)
            logger.log(
                logging.WARNING if run else logging.INFO,
                f"SNL {snl_id} created for {istruct.source_id} ({formula}/{sg})",
                extra={
                    "formula": formula,
                    "spacegroup": sg,
                    "tags": [tag],
                    "source_id": istruct.source_id,
                },
            )

    # final save
    if run:
        insert_snls(ctx, snls)
示例#36
0
excluded_bonding_elements = args.exclude_bonding[0].split(',') if args.exclude_bonding else []

file_format = args.format
filename = args.input_file[0]

s = None

if filename.endswith(".cif"):
    file_format = "cif"
elif filename.startswith("POSCAR"):
    file_format = "poscar"
elif re.search('\.json', filename):
    file_format = 'mpjson'


if file_format == 'poscar':
    p = Poscar.from_file(filename)
    s = p.struct
elif file_format == 'cif':
    r = CifParser(filename)
    s = r.get_structures(False)[0]
else:
    d = json.load(file_open_zip_aware(filename))
    ts = TransformedStructure.from_dict(d)
    s = ts.final_structure

if s:
    vis = StructureVis(excluded_bonding_elements=excluded_bonding_elements)
    vis.set_structure(s)
    vis.show()
示例#37
0
 def from_filenames(poscar_filenames, transformations=[], extend_collection=False):
     transformed_structures = []
     for filename in poscar_filenames:
         with open(filename, "r") as f:
             transformed_structures.append(TransformedStructure.from_poscar_string(f.read(), []))
     return StandardTransmuter(transformed_structures, transformations, extend_collection=extend_collection)
示例#38
0
 def setUp(self):
     structure = PymatgenTest.get_structure("LiFePO4")
     self.structure = structure
     trans = [SubstitutionTransformation({"Li": "Na"})]
     self.trans = TransformedStructure(structure, trans)
 def __init__(self, poscar_string, transformations=None,
              extend_collection=False):
     tstruct = TransformedStructure.from_poscar_string(poscar_string, [])
     StandardTransmuter.__init__(self, [tstruct], transformations,
                                 extend_collection=extend_collection)
示例#40
0
def list_TransformedStructure(list_struc):
    return [TransformedStructure(structure) for structure in list_struc]
示例#41
0
class TransformedStructureTest(unittest.TestCase):
    def setUp(self):
        structure_dict = {
            "lattice": {
                "a":
                4.754150115,
                "volume":
                302.935463898643,
                "c":
                10.462573348,
                "b":
                6.090300362,
                "matrix": [[4.754150115, 0.0, 0.0], [0.0, 6.090300362, 0.0],
                           [0.0, 0.0, 10.462573348]],
                "alpha":
                90.0,
                "beta":
                90.0,
                "gamma":
                90.0
            },
            "sites": [{
                "occu": 1,
                "abc": [0.0, 0.0, 0.0],
                "xyz": [0.0, 0.0, 0.0],
                "species": [{
                    "occu": 1,
                    "element": "Li"
                }],
                "label": "Li"
            }, {
                "occu": 1,
                "abc": [0.5000010396179928, 0.0, 0.5000003178950235],
                "xyz": [2.37708, 0.0, 5.23129],
                "species": [{
                    "occu": 1,
                    "element": "Li"
                }],
                "label": "Li"
            }, {
                "occu": 1,
                "abc": [0.0, 0.49999997028061194, 0.0],
                "xyz": [0.0, 3.04515, 0.0],
                "species": [{
                    "occu": 1,
                    "element": "Li"
                }],
                "label": "Li"
            }, {
                "occu":
                1,
                "abc":
                [0.5000010396179928, 0.49999997028061194, 0.5000003178950235],
                "xyz": [2.37708, 3.04515, 5.23129],
                "species": [{
                    "occu": 1,
                    "element": "Li"
                }],
                "label":
                "Li"
            }, {
                "occu":
                1,
                "abc":
                [0.7885825876997996, 0.5473161916279229, 0.3339168944194627],
                "xyz": [3.74904, 3.33332, 3.4936300000000005],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.2114173881108085, 0.452683748933301, 0.6660827855827808],
                "xyz": [1.00511, 2.75698, 6.968940000000001],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.7114184277288014, 0.5473161916279229, 0.8339172123144861],
                "xyz": [3.38219, 3.33332, 8.72492],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.7885825876997996, 0.9526820772587701, 0.3339168944194627],
                "xyz": [3.74904, 5.8021199999999995, 3.4936300000000005],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc": [
                    0.28858365150718424, 0.047317863302453654,
                    0.16608342347556082
                ],
                "xyz": [1.37197, 0.28818, 1.73766],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.7440972443925447, 0.25000080611787734, 0.09613791622232937],
                "xyz": [3.537549999999999, 1.52258, 1.00585],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.28858365150718424, 0.452683748933301, 0.16608342347556082],
                "xyz": [1.37197, 2.75698, 1.73766],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.2114173881108085, 0.047317863302453654, 0.6660827855827808],
                "xyz": [1.00511, 0.28818, 6.968940000000001],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.2559006279926859, 0.7499991344433464, 0.9038627195677177],
                "xyz": [1.21659, 4.56772, 9.45673],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.7559016676106785, 0.25000080611787734, 0.5961372783295493],
                "xyz": [3.5936699999999986, 1.52258, 6.2371300000000005],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.7939989080466804, 0.7499991344433464, 0.5421304884886912],
                "xyz": [3.77479, 4.56772, 5.67208],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.24409830819992942, 0.7499991344433464, 0.40386240167269416],
                "xyz": [1.16048, 4.56772, 4.22544],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.7060021073819206, 0.7499991344433464, 0.04213017059366761],
                "xyz": [3.35644, 4.56772, 0.44079000000000007],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.2939978684286875, 0.25000080611787734, 0.9578695094085758],
                "xyz": [1.3977099999999996, 1.52258, 10.02178],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.20600106776392774, 0.25000080611787734, 0.4578701473013559],
                "xyz": [0.9793599999999998, 1.52258, 4.7905],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.7114184277288014, 0.9526820772587701, 0.8339172123144861],
                "xyz": [3.38219, 5.8021199999999995, 8.72492],
                "species": [{
                    "occu": 1,
                    "element": "O"
                }],
                "label":
                "O"
            }, {
                "occu":
                1,
                "abc":
                [0.5793611756830275, 0.7499991344433464, 0.9051119342269868],
                "xyz": [2.75437, 4.56772, 9.4698],
                "species": [{
                    "occu": 1,
                    "element": "P"
                }],
                "label":
                "P"
            }, {
                "occu":
                1,
                "abc":
                [0.9206377363201961, 0.7499991344433464, 0.40511161633196324],
                "xyz": [4.37685, 4.56772, 4.23851],
                "species": [{
                    "occu": 1,
                    "element": "P"
                }],
                "label":
                "P"
            }, {
                "occu":
                1,
                "abc": [
                    0.42063880012758065, 0.25000080611787734,
                    0.09488774577525667
                ],
                "xyz": [1.9997799999999994, 1.52258, 0.99277],
                "species": [{
                    "occu": 1,
                    "element": "P"
                }],
                "label":
                "P"
            }, {
                "occu":
                1,
                "abc":
                [0.07936223949041206, 0.25000080611787734, 0.5948880636702801],
                "xyz": [0.3773, 1.52258, 6.22406],
                "species": [{
                    "occu": 1,
                    "element": "P"
                }],
                "label":
                "P"
            }, {
                "occu":
                1,
                "abc":
                [0.021860899947623972, 0.7499991344433464, 0.7185507570598875],
                "xyz": [0.10393, 4.56772, 7.517890000000001],
                "species": [{
                    "occu": 1,
                    "element": "Fe"
                }],
                "label":
                "Fe"
            }, {
                "occu":
                1,
                "abc":
                [0.478135932819614, 0.7499991344433464, 0.21855043916486389],
                "xyz": [2.27313, 4.56772, 2.2866],
                "species": [{
                    "occu": 1,
                    "element": "Fe"
                }],
                "label":
                "Fe"
            }, {
                "occu":
                1,
                "abc":
                [0.9781369724376069, 0.25000080611787734, 0.2814489229423561],
                "xyz": [4.65021, 1.52258, 2.9446800000000004],
                "species": [{
                    "occu": 1,
                    "element": "Fe"
                }],
                "label":
                "Fe"
            }, {
                "occu":
                1,
                "abc":
                [0.5218619395656168, 0.25000080611787734, 0.7814492408373795],
                "xyz": [2.48101, 1.52258, 8.17597],
                "species": [{
                    "occu": 1,
                    "element": "Fe"
                }],
                "label":
                "Fe"
            }]
        }
        structure = Structure.from_dict(structure_dict)
        self.structure = structure
        trans = [SubstitutionTransformation({"Li": "Na"})]
        self.trans = TransformedStructure(structure, trans)

    def test_append_transformation(self):
        t = SubstitutionTransformation({"Fe": "Mn"})
        self.trans.append_transformation(t)
        self.assertEqual(
            "NaMnPO4", self.trans.final_structure.composition.reduced_formula)
        self.assertEqual(len(self.trans.structures), 3)
        coords = list()
        coords.append([0, 0, 0])
        coords.append([0.75, 0.5, 0.75])
        lattice = [[3.8401979337, 0.00, 0.00],
                   [1.9200989668, 3.3257101909, 0.00],
                   [0.00, -2.2171384943, 3.1355090603]]
        struct = Structure(lattice, ["Si4+", "Si4+"], coords)
        ts = TransformedStructure(struct, [])
        ts.append_transformation(
            SupercellTransformation.from_scaling_factors(2, 1, 1))
        alt = ts.append_transformation(
            PartialRemoveSpecieTransformation(
                'Si4+',
                0.5,
                algo=PartialRemoveSpecieTransformation.ALGO_COMPLETE), 5)
        self.assertEqual(len(alt), 2)

    def test_append_filter(self):
        f3 = ContainsSpecieFilter(['O2-'], strict_compare=True, AND=False)
        self.trans.append_filter(f3)

    def test_get_vasp_input(self):
        vaspis = MPVaspInputSet()
        self.assertEqual(
            "Na_pv\nO\nP\nFe_pv",
            self.trans.get_vasp_input(vaspis, False)['POTCAR.spec'])
        self.assertEqual(len(self.trans.structures), 2)

    def test_final_structure(self):
        self.assertEqual(
            "NaFePO4", self.trans.final_structure.composition.reduced_formula)

    def test_from_dict(self):
        d = json.load(open(os.path.join(test_dir, 'transformations.json'),
                           'r'))
        d['other_parameters'] = {'tags': ['test']}
        ts = TransformedStructure.from_dict(d)
        ts.set_parameter('author', 'Will')
        ts.append_transformation(SubstitutionTransformation({"Fe": "Mn"}))
        self.assertEqual("MnPO4",
                         ts.final_structure.composition.reduced_formula)
        self.assertEqual(ts.other_parameters, {
            'author': 'Will',
            'tags': ['test']
        })

    def test_undo_and_redo_last_change(self):
        trans = [
            SubstitutionTransformation({"Li": "Na"}),
            SubstitutionTransformation({"Fe": "Mn"})
        ]
        ts = TransformedStructure(self.structure, trans)
        self.assertEqual("NaMnPO4",
                         ts.final_structure.composition.reduced_formula)
        ts.undo_last_change()
        self.assertEqual("NaFePO4",
                         ts.final_structure.composition.reduced_formula)
        ts.undo_last_change()
        self.assertEqual("LiFePO4",
                         ts.final_structure.composition.reduced_formula)
        self.assertRaises(IndexError, ts.undo_last_change)
        ts.redo_next_change()
        self.assertEqual("NaFePO4",
                         ts.final_structure.composition.reduced_formula)
        ts.redo_next_change()
        self.assertEqual("NaMnPO4",
                         ts.final_structure.composition.reduced_formula)
        self.assertRaises(IndexError, ts.redo_next_change)
        #Make sure that this works with filters.
        f3 = ContainsSpecieFilter(['O2-'], strict_compare=True, AND=False)
        ts.append_filter(f3)
        ts.undo_last_change()
        ts.redo_next_change()

    def test_to_dict(self):
        d = self.trans.to_dict
        self.assertIn('last_modified', d)
        self.assertIn('history', d)
        self.assertIn('version', d)
        self.assertEqual(Structure.from_dict(d).formula, 'Na4 Fe4 P4 O16')
示例#42
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
示例#43
0
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))
示例#44
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
示例#45
0
class TransformedStructureTest(unittest.TestCase):
    def setUp(self):
        structure_dict = {
            "lattice": {
                "a": 4.754150115,
                "volume": 302.935463898643,
                "c": 10.462573348,
                "b": 6.090300362,
                "matrix": [[4.754150115, 0.0, 0.0], [0.0, 6.090300362, 0.0], [0.0, 0.0, 10.462573348]],
                "alpha": 90.0,
                "beta": 90.0,
                "gamma": 90.0,
            },
            "sites": [
                {
                    "occu": 1,
                    "abc": [0.0, 0.0, 0.0],
                    "xyz": [0.0, 0.0, 0.0],
                    "species": [{"occu": 1, "element": "Li"}],
                    "label": "Li",
                },
                {
                    "occu": 1,
                    "abc": [0.5000010396179928, 0.0, 0.5000003178950235],
                    "xyz": [2.37708, 0.0, 5.23129],
                    "species": [{"occu": 1, "element": "Li"}],
                    "label": "Li",
                },
                {
                    "occu": 1,
                    "abc": [0.0, 0.49999997028061194, 0.0],
                    "xyz": [0.0, 3.04515, 0.0],
                    "species": [{"occu": 1, "element": "Li"}],
                    "label": "Li",
                },
                {
                    "occu": 1,
                    "abc": [0.5000010396179928, 0.49999997028061194, 0.5000003178950235],
                    "xyz": [2.37708, 3.04515, 5.23129],
                    "species": [{"occu": 1, "element": "Li"}],
                    "label": "Li",
                },
                {
                    "occu": 1,
                    "abc": [0.7885825876997996, 0.5473161916279229, 0.3339168944194627],
                    "xyz": [3.74904, 3.33332, 3.4936300000000005],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.2114173881108085, 0.452683748933301, 0.6660827855827808],
                    "xyz": [1.00511, 2.75698, 6.968940000000001],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.7114184277288014, 0.5473161916279229, 0.8339172123144861],
                    "xyz": [3.38219, 3.33332, 8.72492],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.7885825876997996, 0.9526820772587701, 0.3339168944194627],
                    "xyz": [3.74904, 5.8021199999999995, 3.4936300000000005],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.28858365150718424, 0.047317863302453654, 0.16608342347556082],
                    "xyz": [1.37197, 0.28818, 1.73766],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.7440972443925447, 0.25000080611787734, 0.09613791622232937],
                    "xyz": [3.537549999999999, 1.52258, 1.00585],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.28858365150718424, 0.452683748933301, 0.16608342347556082],
                    "xyz": [1.37197, 2.75698, 1.73766],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.2114173881108085, 0.047317863302453654, 0.6660827855827808],
                    "xyz": [1.00511, 0.28818, 6.968940000000001],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.2559006279926859, 0.7499991344433464, 0.9038627195677177],
                    "xyz": [1.21659, 4.56772, 9.45673],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.7559016676106785, 0.25000080611787734, 0.5961372783295493],
                    "xyz": [3.5936699999999986, 1.52258, 6.2371300000000005],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.7939989080466804, 0.7499991344433464, 0.5421304884886912],
                    "xyz": [3.77479, 4.56772, 5.67208],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.24409830819992942, 0.7499991344433464, 0.40386240167269416],
                    "xyz": [1.16048, 4.56772, 4.22544],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.7060021073819206, 0.7499991344433464, 0.04213017059366761],
                    "xyz": [3.35644, 4.56772, 0.44079000000000007],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.2939978684286875, 0.25000080611787734, 0.9578695094085758],
                    "xyz": [1.3977099999999996, 1.52258, 10.02178],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.20600106776392774, 0.25000080611787734, 0.4578701473013559],
                    "xyz": [0.9793599999999998, 1.52258, 4.7905],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.7114184277288014, 0.9526820772587701, 0.8339172123144861],
                    "xyz": [3.38219, 5.8021199999999995, 8.72492],
                    "species": [{"occu": 1, "element": "O"}],
                    "label": "O",
                },
                {
                    "occu": 1,
                    "abc": [0.5793611756830275, 0.7499991344433464, 0.9051119342269868],
                    "xyz": [2.75437, 4.56772, 9.4698],
                    "species": [{"occu": 1, "element": "P"}],
                    "label": "P",
                },
                {
                    "occu": 1,
                    "abc": [0.9206377363201961, 0.7499991344433464, 0.40511161633196324],
                    "xyz": [4.37685, 4.56772, 4.23851],
                    "species": [{"occu": 1, "element": "P"}],
                    "label": "P",
                },
                {
                    "occu": 1,
                    "abc": [0.42063880012758065, 0.25000080611787734, 0.09488774577525667],
                    "xyz": [1.9997799999999994, 1.52258, 0.99277],
                    "species": [{"occu": 1, "element": "P"}],
                    "label": "P",
                },
                {
                    "occu": 1,
                    "abc": [0.07936223949041206, 0.25000080611787734, 0.5948880636702801],
                    "xyz": [0.3773, 1.52258, 6.22406],
                    "species": [{"occu": 1, "element": "P"}],
                    "label": "P",
                },
                {
                    "occu": 1,
                    "abc": [0.021860899947623972, 0.7499991344433464, 0.7185507570598875],
                    "xyz": [0.10393, 4.56772, 7.517890000000001],
                    "species": [{"occu": 1, "element": "Fe"}],
                    "label": "Fe",
                },
                {
                    "occu": 1,
                    "abc": [0.478135932819614, 0.7499991344433464, 0.21855043916486389],
                    "xyz": [2.27313, 4.56772, 2.2866],
                    "species": [{"occu": 1, "element": "Fe"}],
                    "label": "Fe",
                },
                {
                    "occu": 1,
                    "abc": [0.9781369724376069, 0.25000080611787734, 0.2814489229423561],
                    "xyz": [4.65021, 1.52258, 2.9446800000000004],
                    "species": [{"occu": 1, "element": "Fe"}],
                    "label": "Fe",
                },
                {
                    "occu": 1,
                    "abc": [0.5218619395656168, 0.25000080611787734, 0.7814492408373795],
                    "xyz": [2.48101, 1.52258, 8.17597],
                    "species": [{"occu": 1, "element": "Fe"}],
                    "label": "Fe",
                },
            ],
        }
        structure = Structure.from_dict(structure_dict)
        self.structure = structure
        trans = [SubstitutionTransformation({"Li": "Na"})]
        self.trans = TransformedStructure(structure, trans)

    def test_append_transformation(self):
        t = SubstitutionTransformation({"Fe": "Mn"})
        self.trans.append_transformation(t)
        self.assertEqual("NaMnPO4", self.trans.final_structure.composition.reduced_formula)
        self.assertEqual(len(self.trans.structures), 3)
        coords = list()
        coords.append([0, 0, 0])
        coords.append([0.75, 0.5, 0.75])
        lattice = [[3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603]]
        struct = Structure(lattice, ["Si4+", "Si4+"], coords)
        ts = TransformedStructure(struct, [])
        ts.append_transformation(SupercellTransformation.from_scaling_factors(2, 1, 1))
        alt = ts.append_transformation(
            PartialRemoveSpecieTransformation("Si4+", 0.5, algo=PartialRemoveSpecieTransformation.ALGO_COMPLETE), 5
        )
        self.assertEqual(len(alt), 2)

    def test_append_filter(self):
        f3 = ContainsSpecieFilter(["O2-"], strict_compare=True, AND=False)
        self.trans.append_filter(f3)

    def test_get_vasp_input(self):
        SETTINGS["VASP_PSP_DIR"] = os.path.abspath(
            os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files")
        )
        potcar = self.trans.get_vasp_input(MPRelaxSet)["POTCAR"]
        self.assertEqual("Na_pv\nFe_pv\nP\nO", "\n".join([p.symbol for p in potcar]))
        self.assertEqual(len(self.trans.structures), 2)

    def test_final_structure(self):
        self.assertEqual("NaFePO4", self.trans.final_structure.composition.reduced_formula)

    def test_from_dict(self):
        d = json.load(open(os.path.join(test_dir, "transformations.json"), "r"))
        d["other_parameters"] = {"tags": ["test"]}
        ts = TransformedStructure.from_dict(d)
        ts.other_parameters["author"] = "Will"
        ts.append_transformation(SubstitutionTransformation({"Fe": "Mn"}))
        self.assertEqual("MnPO4", ts.final_structure.composition.reduced_formula)
        self.assertEqual(ts.other_parameters, {"author": "Will", "tags": ["test"]})

    def test_undo_and_redo_last_change(self):
        trans = [SubstitutionTransformation({"Li": "Na"}), SubstitutionTransformation({"Fe": "Mn"})]
        ts = TransformedStructure(self.structure, trans)
        self.assertEqual("NaMnPO4", ts.final_structure.composition.reduced_formula)
        ts.undo_last_change()
        self.assertEqual("NaFePO4", ts.final_structure.composition.reduced_formula)
        ts.undo_last_change()
        self.assertEqual("LiFePO4", ts.final_structure.composition.reduced_formula)
        self.assertRaises(IndexError, ts.undo_last_change)
        ts.redo_next_change()
        self.assertEqual("NaFePO4", ts.final_structure.composition.reduced_formula)
        ts.redo_next_change()
        self.assertEqual("NaMnPO4", ts.final_structure.composition.reduced_formula)
        self.assertRaises(IndexError, ts.redo_next_change)
        # Make sure that this works with filters.
        f3 = ContainsSpecieFilter(["O2-"], strict_compare=True, AND=False)
        ts.append_filter(f3)
        ts.undo_last_change()
        ts.redo_next_change()

    def test_as_dict(self):
        self.trans.set_parameter("author", "will")
        d = self.trans.as_dict()
        self.assertIn("last_modified", d)
        self.assertIn("history", d)
        self.assertIn("version", d)
        self.assertIn("author", d["other_parameters"])
        self.assertEqual(Structure.from_dict(d).formula, "Na4 Fe4 P4 O16")

    def test_snl(self):
        self.trans.set_parameter("author", "will")
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            snl = self.trans.to_snl([("will", "*****@*****.**")])
            self.assertEqual(len(w), 1, "Warning not raised on type conversion " "with other_parameters")
        ts = TransformedStructure.from_snl(snl)
        self.assertEqual(ts.history[-1]["@class"], "SubstitutionTransformation")

        h = ("testname", "testURL", {"test": "testing"})
        snl = StructureNL(ts.final_structure, [("will", "*****@*****.**")], history=[h])
        snl = TransformedStructure.from_snl(snl).to_snl([("notwill", "*****@*****.**")])
        self.assertEqual(snl.history, [h])
        self.assertEqual(snl.authors, [("notwill", "*****@*****.**")])
示例#46
0
class TransformedStructureTest(unittest.TestCase):

    def setUp(self):
        structure_dict = {"lattice": {"a": 4.754150115,
                                      "volume": 302.935463898643,
                                      "c": 10.462573348,
                                      "b": 6.090300362,
                                      "matrix": [[4.754150115, 0.0, 0.0],
                                                 [0.0, 6.090300362, 0.0],
                                                 [0.0, 0.0, 10.462573348]],
                                      "alpha": 90.0,
                                      "beta": 90.0,
                                      "gamma": 90.0},
                          "sites": [{"occu": 1, "abc": [0.0, 0.0, 0.0],
                                     "xyz": [0.0, 0.0, 0.0],
                                     "species": [{"occu": 1, "element": "Li"}],
                                     "label": "Li"}, {"occu": 1, "abc":
                                                      [0.5000010396179928, 0.0, 0.5000003178950235],
                                                      "xyz": [2.37708, 0.0, 5.23129],
                                                      "species": [{"occu": 1, "element": "Li"}], "label": "Li"},
                                    {"occu": 1, "abc": [0.0, 0.49999997028061194, 0.0], "xyz": [0.0, 3.04515, 0.0],
                                     "species": [{"occu": 1, "element": "Li"}], "label": "Li"}, {"occu": 1, "abc": [0.5000010396179928, 0.49999997028061194, 0.5000003178950235], "xyz": [2.37708, 3.04515, 5.23129], "species": [{"occu": 1, "element": "Li"}], "label": "Li"}, {"occu": 1, "abc": [0.7885825876997996, 0.5473161916279229, 0.3339168944194627], "xyz": [3.74904, 3.33332, 3.4936300000000005], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.2114173881108085, 0.452683748933301, 0.6660827855827808], "xyz": [1.00511, 2.75698, 6.968940000000001], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7114184277288014, 0.5473161916279229, 0.8339172123144861], "xyz": [3.38219, 3.33332, 8.72492], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7885825876997996, 0.9526820772587701, 0.3339168944194627], "xyz": [3.74904, 5.8021199999999995, 3.4936300000000005], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.28858365150718424, 0.047317863302453654, 0.16608342347556082], "xyz": [1.37197, 0.28818, 1.73766], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7440972443925447, 0.25000080611787734, 0.09613791622232937], "xyz": [3.537549999999999, 1.52258, 1.00585], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.28858365150718424, 0.452683748933301, 0.16608342347556082], "xyz": [1.37197, 2.75698, 1.73766], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.2114173881108085, 0.047317863302453654, 0.6660827855827808], "xyz": [1.00511, 0.28818, 6.968940000000001], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.2559006279926859, 0.7499991344433464, 0.9038627195677177], "xyz": [1.21659, 4.56772, 9.45673], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7559016676106785, 0.25000080611787734, 0.5961372783295493], "xyz": [3.5936699999999986, 1.52258, 6.2371300000000005], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7939989080466804, 0.7499991344433464, 0.5421304884886912], "xyz": [3.77479, 4.56772, 5.67208], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.24409830819992942, 0.7499991344433464, 0.40386240167269416], "xyz": [1.16048, 4.56772, 4.22544], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7060021073819206, 0.7499991344433464, 0.04213017059366761], "xyz": [3.35644, 4.56772, 0.44079000000000007], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.2939978684286875, 0.25000080611787734, 0.9578695094085758], "xyz": [1.3977099999999996, 1.52258, 10.02178], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.20600106776392774, 0.25000080611787734, 0.4578701473013559], "xyz": [0.9793599999999998, 1.52258, 4.7905], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.7114184277288014, 0.9526820772587701, 0.8339172123144861], "xyz": [3.38219, 5.8021199999999995, 8.72492], "species": [{"occu": 1, "element": "O"}], "label": "O"}, {"occu": 1, "abc": [0.5793611756830275, 0.7499991344433464, 0.9051119342269868], "xyz": [2.75437, 4.56772, 9.4698], "species": [{"occu": 1, "element": "P"}], "label": "P"}, {"occu": 1, "abc": [0.9206377363201961, 0.7499991344433464, 0.40511161633196324], "xyz": [4.37685, 4.56772, 4.23851], "species": [{"occu": 1, "element": "P"}], "label": "P"}, {"occu": 1, "abc": [0.42063880012758065, 0.25000080611787734, 0.09488774577525667], "xyz": [1.9997799999999994, 1.52258, 0.99277], "species": [{"occu": 1, "element": "P"}], "label": "P"}, {"occu": 1, "abc": [0.07936223949041206, 0.25000080611787734, 0.5948880636702801], "xyz": [0.3773, 1.52258, 6.22406], "species": [{"occu": 1, "element": "P"}], "label": "P"}, {"occu": 1, "abc": [0.021860899947623972, 0.7499991344433464, 0.7185507570598875], "xyz": [0.10393, 4.56772, 7.517890000000001], "species": [{"occu": 1, "element": "Fe"}], "label": "Fe"}, {"occu": 1, "abc": [0.478135932819614, 0.7499991344433464, 0.21855043916486389], "xyz": [2.27313, 4.56772, 2.2866], "species": [{"occu": 1, "element": "Fe"}], "label": "Fe"}, {"occu": 1, "abc": [0.9781369724376069, 0.25000080611787734, 0.2814489229423561], "xyz": [4.65021, 1.52258, 2.9446800000000004], "species": [{"occu": 1, "element": "Fe"}], "label": "Fe"}, {"occu": 1, "abc": [0.5218619395656168, 0.25000080611787734, 0.7814492408373795], "xyz": [2.48101, 1.52258, 8.17597], "species": [{"occu": 1, "element": "Fe"}], "label": "Fe"}]}
        structure = Structure.from_dict(structure_dict)
        self.structure = structure
        trans = []
        trans.append(SubstitutionTransformation({"Li": "Na"}))
        self.trans = TransformedStructure(structure, trans)

    def test_append_transformation(self):
        t = SubstitutionTransformation({"Fe": "Mn"})
        self.trans.append_transformation(t)
        self.assertEqual("NaMnPO4",
                         self.trans.final_structure.composition
                         .reduced_formula)
        self.assertEqual(len(self.trans.structures), 3)
        coords = list()
        coords.append([0, 0, 0])
        coords.append([0.75, 0.5, 0.75])
        lattice = [[3.8401979337, 0.00, 0.00],
                   [1.9200989668, 3.3257101909, 0.00],
                   [0.00, -2.2171384943, 3.1355090603]]
        struct = Structure(lattice, ["Si4+", "Si4+"], coords)
        ts = TransformedStructure(struct, [])
        ts.append_transformation(SupercellTransformation
                                 .from_scaling_factors(2, 1, 1))
        alt = ts.append_transformation(
            PartialRemoveSpecieTransformation('Si4+', 0.5,
            algo=PartialRemoveSpecieTransformation.ALGO_COMPLETE),
            5
        )
        self.assertEqual(len(alt), 2)

    def test_append_filter(self):
        f3 = ContainsSpecieFilter(['O2-'], strict_compare=True, AND=False)
        self.trans.append_filter(f3)

    def test_get_vasp_input(self):
        vaspis = MaterialsProjectVaspInputSet()
        self.assertEqual("Na_pv\nO\nP\nFe_pv",
                         self.trans.get_vasp_input(vaspis,
                                                   False)['POTCAR.spec'])
        self.assertEqual(len(self.trans.structures), 2)

    def test_final_structure(self):
        self.assertEqual("NaFePO4", self.trans.final_structure.composition
                         .reduced_formula)

    def test_from_dict(self):
        d = json.load(open(os.path.join(test_dir, 'transformations.json'),
                           'r'))
        d['other_parameters'] = {'tags': ['test']}
        ts = TransformedStructure.from_dict(d)
        ts.set_parameter('author', 'Will')
        ts.append_transformation(SubstitutionTransformation({"Fe": "Mn"}))
        self.assertEqual("MnPO4",
                         ts.final_structure.composition.reduced_formula)
        self.assertEqual(ts.other_parameters, {'author': 'Will',
                                               'tags': ['test']})

    def test_undo_and_redo_last_change(self):
        trans = []
        trans.append(SubstitutionTransformation({"Li": "Na"}))
        trans.append(SubstitutionTransformation({"Fe": "Mn"}))
        ts = TransformedStructure(self.structure, trans)
        self.assertEqual("NaMnPO4",
                         ts.final_structure.composition.reduced_formula)
        ts.undo_last_change()
        self.assertEqual("NaFePO4",
                         ts.final_structure.composition.reduced_formula)
        ts.undo_last_change()
        self.assertEqual("LiFePO4",
                         ts.final_structure.composition.reduced_formula)
        self.assertRaises(IndexError, ts.undo_last_change)
        ts.redo_next_change()
        self.assertEqual("NaFePO4",
                         ts.final_structure.composition.reduced_formula)
        ts.redo_next_change()
        self.assertEqual("NaMnPO4",
                         ts.final_structure.composition.reduced_formula)
        self.assertRaises(IndexError, ts.redo_next_change)
        #Make sure that this works with filters.
        f3 = ContainsSpecieFilter(['O2-'], strict_compare=True, AND=False)
        ts.append_filter(f3)
        ts.undo_last_change()
        ts.redo_next_change()

    def test_to_dict(self):
        d = self.trans.to_dict
        self.assertIn('last_modified', d)
        self.assertIn('history', d)
        self.assertIn('version', d)
        self.assertEqual(Structure.from_dict(d).formula, 'Na4 Fe4 P4 O16')
示例#47
0
 def __init__(self, poscar_string, transformations=None,
              extend_collection=False):
     tstruct = TransformedStructure.from_poscar_string(poscar_string, [])
     super(PoscarTransmuter, self).__init__([tstruct], transformations,
                                 extend_collection=extend_collection)
示例#48
0
class TransformedStructureTest(PymatgenTest):

    def setUp(self):
        structure = PymatgenTest.get_structure("LiFePO4")
        self.structure = structure
        trans = [SubstitutionTransformation({"Li": "Na"})]
        self.trans = TransformedStructure(structure, trans)

    def test_append_transformation(self):
        t = SubstitutionTransformation({"Fe": "Mn"})
        self.trans.append_transformation(t)
        self.assertEqual("NaMnPO4",
                         self.trans.final_structure.composition
                         .reduced_formula)
        self.assertEqual(len(self.trans.structures), 3)
        coords = list()
        coords.append([0, 0, 0])
        coords.append([0.75, 0.5, 0.75])
        lattice = [[3.8401979337, 0.00, 0.00],
                   [1.9200989668, 3.3257101909, 0.00],
                   [0.00, -2.2171384943, 3.1355090603]]
        struct = Structure(lattice, ["Si4+", "Si4+"], coords)
        ts = TransformedStructure(struct, [])
        ts.append_transformation(SupercellTransformation
                                 .from_scaling_factors(2, 1, 1))
        alt = ts.append_transformation(
            PartialRemoveSpecieTransformation(
                'Si4+', 0.5,
                algo=PartialRemoveSpecieTransformation.ALGO_COMPLETE), 5)
        self.assertEqual(len(alt), 2)

    def test_append_filter(self):
        f3 = ContainsSpecieFilter(['O2-'], strict_compare=True, AND=False)
        self.trans.append_filter(f3)

    def test_get_vasp_input(self):
        SETTINGS["PMG_VASP_PSP_DIR"] = os.path.abspath(
                os.path.join(os.path.dirname(__file__), "..", "..", "..",
                             "test_files"))
        potcar = self.trans.get_vasp_input(MPRelaxSet)['POTCAR']
        self.assertEqual("Na_pv\nFe_pv\nP\nO",
                         "\n".join([p.symbol for p in potcar]))
        self.assertEqual(len(self.trans.structures), 2)

    def test_final_structure(self):
        self.assertEqual("NaFePO4", self.trans.final_structure.composition
                         .reduced_formula)

    def test_from_dict(self):
        d = json.load(open(os.path.join(test_dir, 'transformations.json'),
                           'r'))
        d['other_parameters'] = {'tags': ['test']}
        ts = TransformedStructure.from_dict(d)
        ts.other_parameters['author'] = 'Will'
        ts.append_transformation(SubstitutionTransformation({"Fe": "Mn"}))
        self.assertEqual("MnPO4",
                         ts.final_structure.composition.reduced_formula)
        self.assertEqual(ts.other_parameters, {'author': 'Will',
                                               'tags': ['test']})

    def test_undo_and_redo_last_change(self):
        trans = [SubstitutionTransformation({"Li": "Na"}),
                 SubstitutionTransformation({"Fe": "Mn"})]
        ts = TransformedStructure(self.structure, trans)
        self.assertEqual("NaMnPO4",
                         ts.final_structure.composition.reduced_formula)
        ts.undo_last_change()
        self.assertEqual("NaFePO4",
                         ts.final_structure.composition.reduced_formula)
        ts.undo_last_change()
        self.assertEqual("LiFePO4",
                         ts.final_structure.composition.reduced_formula)
        self.assertRaises(IndexError, ts.undo_last_change)
        ts.redo_next_change()
        self.assertEqual("NaFePO4",
                         ts.final_structure.composition.reduced_formula)
        ts.redo_next_change()
        self.assertEqual("NaMnPO4",
                         ts.final_structure.composition.reduced_formula)
        self.assertRaises(IndexError, ts.redo_next_change)
        #Make sure that this works with filters.
        f3 = ContainsSpecieFilter(['O2-'], strict_compare=True, AND=False)
        ts.append_filter(f3)
        ts.undo_last_change()
        ts.redo_next_change()

    def test_as_dict(self):
        self.trans.set_parameter('author', 'will')
        d = self.trans.as_dict()
        self.assertIn('last_modified', d)
        self.assertIn('history', d)
        self.assertIn('version', d)
        self.assertIn('author', d['other_parameters'])
        self.assertEqual(Structure.from_dict(d).formula, 'Na4 Fe4 P4 O16')

    def test_snl(self):
        self.trans.set_parameter('author', 'will')
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            snl = self.trans.to_snl([('will', '*****@*****.**')])
            self.assertEqual(len(w), 1, 'Warning not raised on type conversion '
                             'with other_parameters')
        ts = TransformedStructure.from_snl(snl)
        self.assertEqual(ts.history[-1]['@class'], 'SubstitutionTransformation')
        
        h = ('testname', 'testURL', {'test' : 'testing'})
        snl = StructureNL(ts.final_structure,[('will', '*****@*****.**')], 
                          history = [h])
        snl = TransformedStructure.from_snl(snl).to_snl([('notwill', 
                                                          '*****@*****.**')])
        self.assertEqual(snl.history, [h])
        self.assertEqual(snl.authors, [('notwill', '*****@*****.**')])
示例#49
0
 def test_undo_and_redo_last_change(self):
     trans = [SubstitutionTransformation({"Li": "Na"}), SubstitutionTransformation({"Fe": "Mn"})]
     ts = TransformedStructure(self.structure, trans)
     self.assertEqual("NaMnPO4", ts.final_structure.composition.reduced_formula)
     ts.undo_last_change()
     self.assertEqual("NaFePO4", ts.final_structure.composition.reduced_formula)
     ts.undo_last_change()
     self.assertEqual("LiFePO4", ts.final_structure.composition.reduced_formula)
     self.assertRaises(IndexError, ts.undo_last_change)
     ts.redo_next_change()
     self.assertEqual("NaFePO4", ts.final_structure.composition.reduced_formula)
     ts.redo_next_change()
     self.assertEqual("NaMnPO4", ts.final_structure.composition.reduced_formula)
     self.assertRaises(IndexError, ts.redo_next_change)
     # Make sure that this works with filters.
     f3 = ContainsSpecieFilter(["O2-"], strict_compare=True, AND=False)
     ts.append_filter(f3)
     ts.undo_last_change()
     ts.redo_next_change()