Exemplo n.º 1
0
 def test_no_oxidation(self):
     specie = {"Cu1+": 0.5, "Au2+": 0.5}
     cuau = Structure.from_spacegroup("Fm-3m", Lattice.cubic(3.677),
                                      [specie], [[0, 0, 0]])
     trans = OrderDisorderedStructureTransformation()
     ss = trans.apply_transformation(cuau, return_ranked_list=100)
     self.assertEqual(ss[0]["structure"].composition["Cu+"], 2)
     trans = OrderDisorderedStructureTransformation(no_oxi_states=True)
     ss = trans.apply_transformation(cuau, return_ranked_list=100)
     self.assertEqual(ss[0]["structure"].composition["Cu+"], 0)
     self.assertEqual(ss[0]["structure"].composition["Cu"], 2)
Exemplo n.º 2
0
def create_crystal(cifpath, primitive=False):
    """ Convert cif to pymatgen struture """
    structure = CifParser(cifpath).get_structures(primitive=primitive)[0]
    if structure.is_ordered:
        return structure
    order_transformer = OrderDisorderedStructureTransformation()
    oxid_transformer = AutoOxiStateDecorationTransformation()
    a = oxid_transformer.apply_transformation(structure)
    b = order_transformer.apply_transformation(a)
    return b
Exemplo n.º 3
0
    def test_best_first(self):
        t = OrderDisorderedStructureTransformation(algo=2)
        coords = []
        coords.append([0, 0, 0])
        coords.append([0.75, 0.75, 0.75])
        coords.append([0.5, 0.5, 0.5])
        coords.append([0.25, 0.25, 0.25])
        lattice = Lattice([
            [3.8401979337, 0.00, 0.00],
            [1.9200989668, 3.3257101909, 0.00],
            [0.00, -2.2171384943, 3.1355090603],
        ])

        struct = Structure(
            lattice,
            [
                {
                    "Si4+": 0.5,
                    "O2-": 0.25,
                    "P5+": 0.25
                },
                {
                    "Si4+": 0.5,
                    "O2-": 0.25,
                    "P5+": 0.25
                },
                {
                    "Si4+": 0.5,
                    "O2-": 0.25,
                    "P5+": 0.25
                },
                {
                    "Si4+": 0.5,
                    "O2-": 0.25,
                    "P5+": 0.25
                },
            ],
            coords,
        )
        output = t.apply_transformation(struct, return_ranked_list=3)
        self.assertAlmostEqual(output[0]["energy"], -234.57813667648315, 4)
Exemplo n.º 4
0
 def test_symmetrized_structure(self):
     t = OrderDisorderedStructureTransformation(symmetrized_structures=True)
     c = []
     sp = []
     c.append([0.5, 0.5, 0.5])
     sp.append("Si4+")
     c.append([0.45, 0.45, 0.45])
     sp.append({"Si4+": 0.5})
     c.append([0.56, 0.56, 0.56])
     sp.append({"Si4+": 0.5})
     c.append([0.25, 0.75, 0.75])
     sp.append({"Si4+": 0.5})
     c.append([0.75, 0.25, 0.25])
     sp.append({"Si4+": 0.5})
     l = Lattice.cubic(5)
     s = Structure(l, sp, c)
     test_site = PeriodicSite("Si4+", c[2], l)
     s = SymmetrizedStructure(s, "not_real", [0, 1, 1, 2, 2],
                              ["a", "b", "b", "c", "c"])
     output = t.apply_transformation(s)
     self.assertTrue(test_site in output.sites)
Exemplo n.º 5
0
def _get_mc_structs(SCLst,
                    CE,
                    ecis,
                    merge_sublats=None,
                    TLst=[500, 1500, 10000],
                    compaxis=None,
                    outdir='vasp_run'):
    '''This function checks the previous calculation directories when called. If no previous calculations, it
       generates the intial pool. If there are previous calculations, add new sampled structures based on 
       structure selection rule.
       For CE sampling using MC, use three set of temperature, merge this with LocalOrdering code.
       ce_file: directory of CE Mson data file
       outdir: directory to write outputs
       SCLst: a list contaning enumerated SC's and RO pairs.
       TLst: temprature list to do MC enumeration on
       useX: a list of compounds of which we may want to calculate component.
       compaxis: a list of compound names. If specified, the program will caculate the composition in compound ratio,
                but ususally not used since we don't think its necessary nor applicable in complexed disordered 
                rocksalt systems.
    '''
    print('#### MC Initialization ####')
    Prim = CE.structure
    print('SM type:', CE.sm_type)
    calculated_structures = {}

    if os.path.isdir(outdir):
        print("Checking previously enumerated structures.")
        for root, dirs, files in os.walk(outdir):
            if _was_generated(files):
                parentdir = os.path.join(*root.split(os.sep)[0:-1])
                with open(os.path.join(parentdir,
                                       'composition_by_site')) as RO_file:
                    RO_old = json.load(RO_file)
                    RO_old_string = json.dumps(RO_old)
                if RO_old_string not in calculated_structures:
                    calculated_structures[RO_old_string] = []
                calculated_structures[RO_old_string].append(
                    Poscar.from_file(os.path.join(root, 'POSCAR')).structure)
                # struct_id = int(root.split(os.sep)[-1])
    else:
        print("No previous calculations, generating the initial pool.")

    mc_structs = {}
    if compaxis:
        ro_axis_strings = {}

    sc_ro_pair_id = 0

    if merge_sublats is None:
        sublat_list = [[i] for i in range(len(Prim))]
    else:
        sublat_list = merge_sublats

    for SC, RO, sublats_WorthToExpand in SCLst:
        #len(RO)==len(sublats_WorthToExpand)==len(merge_sublats) if merge_sublats else ==len(SC)

        print("Processing composition:\n", RO, '\nSupercell:\n', SC,
              '\nsize:\n', int(round(np.abs(np.linalg.det(SC)))))
        clusSC = CE.supercell_from_matrix(SC)

        Bits = clusSC.bits
        scs = int(round(np.abs(np.linalg.det(SC))))
        # generate a list of groups of sites to swap between! We have known which sites are partially occupied,
        # so we only need to figure out how pymatgen make a group of supercell sites from a primitive cell site.
        # Looks like it simply just replicate sites one by one!
        # indGrps=[list(range(i*scs,(i+1)*scs)) for i in range(len(RO)) if sites_WorthToExpand[i]];

        indGrps = [list(range(i*scs*len(sublat),(i+1)*scs*len(sublat))) for i,sublat in enumerate(sublat_list)\
                  if sublats_WorthToExpand[i]]

        RO_int = [{
            specie: int(round(RO[i][specie] * scs * len(sublat)))
            for specie in RO[i]
        } for i, sublat in enumerate(sublat_list)]
        #species will now be swapped within a 'sublattice', instead of the replicates of a site.

        #Note: indGrps should be generated from a clusSC supercell!!!!!
        #print('indGrps',indGrps,'RO',RO)

        # Replace species according to RO
        randSites = []
        for i, sublat in enumerate(sublat_list):
            for s_id in sublat:
                site = Prim[s_id]
                randSite = PeriodicSite(RO[i],
                                        site.frac_coords,
                                        Prim.lattice,
                                        properties=site.properties)
                randSites.append(randSite)
        randStr = Structure.from_sites(randSites)

        # Get electrostatics enumeration guess
        order = OrderDisorderedStructureTransformation(algo=2)

        randStr.make_supercell(SC)

        randStr = order.apply_transformation(randStr)

        #print('randStr:\n',randStr,'\nce prim:\n',CE.structure)
        # Simulated annealing for better guess at ground state
        # You may want to change the number of MC flips for each temperature

        init_occu = clusSC.occu_from_structure(randStr)
        #print("Starting occupation:", init_occu)
        sa_occu = simulated_anneal(ecis=ecis,
                                   cluster_supercell=clusSC,
                                   occu=init_occu,
                                   ind_groups=indGrps,
                                   n_loops=20000,
                                   init_T=5100,
                                   final_T=100,
                                   n_steps=20)
        print("MC ground state acquired, analyzing composition.")

        # Axis decomposition
        if compaxis:
            axis = _axis_decompose(compaxis, RO_int)
        #convert frac occupation back to integers.
        sp_list = []
        for sublat_occu in RO_int:
            sp_list.extend(sublat_occu.values())
        _gcd = GCD_List(sp_list)
        RO_reduced_int = [{sp: sublat_occu[sp] // _gcd
                           for sp in sublat_occu} for sublat_occu in RO_int]

        #Reduce occupation numbers by GCD.
        RO_string = json.dumps(RO_reduced_int)
        print("Reduced occupation:", RO_string)
        if RO_string not in mc_structs:
            mc_structs[RO_string] = []

        #REFERED AXIS DECOMPOSITION IS WRONG. FIX THIS!

        if compaxis:
            axis_string = json.dumps(axis)
            if RO_string not in ro_axis_strings:
                ro_axis_strings[RO_string] = axis_string
            print("Axis composition: ", axis_string)

        # Add approximate ground state to set of MC structures
        # Format as (structure, temperature) - for ground state, temperature is "0"
        # print("GS structure:",clusSC.structure_from_occu(sa_occu))
        mc_structs[RO_string].append((clusSC.structure_from_occu(sa_occu), 0))
        print("MC GS added to the preset.")

        for T in TLst:
            print("Doing MC sampling under T = {}K".format(T))
            # Equilibration run
            # Play around with the number of MC flips in the run - the current number is very arbitrary
            # We can try to implement VO et.al's sampling method here!

            occu, _, _, _ = run_T(ecis=ecis,
                                  cluster_supercell=clusSC,
                                  occu=deepcopy(sa_occu),
                                  T=T,
                                  n_loops=100000,
                                  ind_groups=indGrps,
                                  n_rand=2,
                                  check_unique=False)

            # Production run
            # Same comment about number of flips - very arbitrary right now
            occu, min_occu, min_e, rand_occu = run_T(ecis=ecis,
                                                     cluster_supercell=clusSC,
                                                     occu=deepcopy(occu),
                                                     T=T,
                                                     n_loops=200000,
                                                     ind_groups=indGrps,
                                                     n_rand=6,
                                                     check_unique=True)

            # Check that returned random structures
            # are all different
            # Save best structure and a few random structures from the production run
            mc_structs[RO_string].append(
                (clusSC.structure_from_occu(min_occu), T))
            for rand, rand_e in rand_occu:
                mc_structs[RO_string].append(
                    (clusSC.structure_from_occu(rand), SC))

        sc_ro_pair_id += 1

    # Deduplicate - first compared to previously calculated structures, then compared to structures within this run
    print('Deduplicating random structures.')

    unique_structs = {}
    unqCnt = 0
    sm = StructureMatcher(ltol=0.3,
                          stol=0.3,
                          angle_tol=5,
                          comparator=ElementComparator())
    for RO_string, structs in mc_structs.items():
        if RO_string not in unique_structs:
            unique_structs[RO_string] = []

        for struct, matrix in structs:
            unique = True
            if RO_string in calculated_structures:
                for ostruct in calculated_structures[RO_string]:
                    if sm.fit(struct, ostruct):
                        unique = False
                        break
            if unique:
                for ostruct, matrix in unique_structs[RO_string]:
                    if sm.fit(struct, ostruct):
                        unique = False
                        break
            if unique:
                try:
                    #Check if structure matcher works for this structure. If not, abandon.
                    cs = CE.supercell_from_matrix(matrix)
                    corr = cs.corr_from_structure(struct)
                    unique_structs[RO_string].append((struct, matrix))
                    unqCnt += 1
                except:
                    continue

    print('Obtained %d unique occupied random structures.' % unqCnt)

    if compaxis:
        return unique_structs, ro_axis_strings
    else:
        return unique_structs, None
Exemplo n.º 6
0
    def test_apply_transformation(self):
        t = OrderDisorderedStructureTransformation()
        coords = []
        coords.append([0, 0, 0])
        coords.append([0.75, 0.75, 0.75])
        coords.append([0.5, 0.5, 0.5])
        coords.append([0.25, 0.25, 0.25])
        lattice = Lattice([
            [3.8401979337, 0.00, 0.00],
            [1.9200989668, 3.3257101909, 0.00],
            [0.00, -2.2171384943, 3.1355090603],
        ])

        struct = Structure(
            lattice,
            [
                {
                    "Si4+": 0.5,
                    "O2-": 0.25,
                    "P5+": 0.25
                },
                {
                    "Si4+": 0.5,
                    "O2-": 0.25,
                    "P5+": 0.25
                },
                {
                    "Si4+": 0.5,
                    "O2-": 0.25,
                    "P5+": 0.25
                },
                {
                    "Si4+": 0.5,
                    "O2-": 0.25,
                    "P5+": 0.25
                },
            ],
            coords,
        )
        output = t.apply_transformation(struct, return_ranked_list=50)
        self.assertEqual(len(output), 12)
        self.assertIsInstance(output[0]["structure"], Structure)

        struct = Structure(
            lattice,
            [
                {
                    "Si4+": 0.5
                },
                {
                    "Si4+": 0.5
                },
                {
                    "P5+": 0.5,
                    "O2-": 0.5
                },
                {
                    "P5+": 0.5,
                    "O2-": 0.5
                },
            ],
            coords,
        )
        output = t.apply_transformation(struct, return_ranked_list=50)
        self.assertIsInstance(output, list)
        self.assertEqual(len(output), 4)
        self.assertEqual(t.lowest_energy_structure, output[0]["structure"])

        struct = Structure(lattice, [{
            "Si4+": 0.5
        }, {
            "Si4+": 0.5
        }, {
            "O2-": 0.5
        }, {
            "O2-": 0.5
        }], coords)
        allstructs = t.apply_transformation(struct, 50)
        self.assertEqual(len(allstructs), 4)

        struct = Structure(lattice, [{
            "Si4+": 0.333
        }, {
            "Si4+": 0.333
        }, {
            "Si4+": 0.333
        }, "O2-"], coords)
        allstructs = t.apply_transformation(struct, 50)
        self.assertEqual(len(allstructs), 3)

        d = t.as_dict()
        self.assertEqual(
            type(OrderDisorderedStructureTransformation.from_dict(d)),
            OrderDisorderedStructureTransformation,
        )
Exemplo n.º 7
0
from pymatgen import Structure
from pymatgen.transformations.standard_transformations import SubstitutionTransformation
from pymatgen.transformations.standard_transformations import OrderDisorderedStructureTransformation


structure = Structure.from_file("POSCAR")

substitution = SubstitutionTransformation({"Nb3+": {"Nb3+":0.5, "Fe3+":0.5}})

result = substitution.apply_transformation(structure)

order = OrderDisorderedStructureTransformation(algo=2)

ResultOrder = order.apply_transformation(result, return_ranked_list=True)

for i, item in enumerate(ResultOrder):
    item['structure'].to(filename="POSCAR{:02d}".format(i))
#ResultOrder[0]['structure'].to(filename="POSCAR1")