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
def test_transmuter(self): tsc = PoscarTransmuter.from_filenames( [os.path.join(self.TEST_FILES_DIR, "POSCAR")]) tsc.append_transformation(RemoveSpeciesTransformation("O")) self.assertEqual(len(tsc[0].final_structure), 8) tsc.append_transformation( SubstitutionTransformation({ "Fe": { "Fe2+": 0.25, "Mn3+": 0.75 }, "P": "P5+" })) tsc.append_transformation(OrderDisorderedStructureTransformation(), extend_collection=50) self.assertEqual(len(tsc), 4) t = SuperTransformation([ SubstitutionTransformation({"Fe2+": "Mg2+"}), SubstitutionTransformation({"Fe2+": "Zn2+"}), SubstitutionTransformation({"Fe2+": "Be2+"}), ]) tsc.append_transformation(t, extend_collection=True) self.assertEqual(len(tsc), 12) for x in tsc: # should be 4 trans + starting structure self.assertEqual( len(x), 5, "something might be wrong with the number of transformations in the history", ) # test the filter tsc.apply_filter( ContainsSpecieFilter(["Zn2+", "Be2+", "Mn4+"], strict_compare=True, AND=False)) self.assertEqual(len(tsc), 8) self.assertEqual( tsc.transformed_structures[0].as_dict()["history"][-1]["@class"], "ContainsSpecieFilter", ) tsc.apply_filter(ContainsSpecieFilter(["Be2+"])) self.assertEqual(len(tsc), 4) # Test set_parameter and add_tag. tsc.set_parameter("para1", "hello") self.assertEqual( tsc.transformed_structures[0].as_dict()["other_parameters"] ["para1"], "hello", ) tsc.add_tags(["world", "universe"]) self.assertEqual( tsc.transformed_structures[0].as_dict()["other_parameters"] ["tags"], ["world", "universe"], )
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)
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)
def test_transmuter(self): tsc = PoscarTransmuter.from_filenames( [os.path.join(test_dir, "POSCAR")]) tsc.append_transformation(RemoveSpeciesTransformation('O')) self.assertEqual(len(tsc[0].final_structure), 8) tsc.append_transformation( SubstitutionTransformation({ "Fe": { "Fe2+": 0.25, "Mn3+": .75 }, "P": "P5+" })) tsc.append_transformation(OrderDisorderedStructureTransformation(), extend_collection=50) self.assertEqual(len(tsc), 4) t = SuperTransformation([ SubstitutionTransformation({"Fe2+": "Mg2+"}), SubstitutionTransformation({"Fe2+": "Zn2+"}), SubstitutionTransformation({"Fe2+": "Be2+"}) ]) tsc.append_transformation(t, extend_collection=True) self.assertEqual(len(tsc), 12) for x in tsc: self.assertEqual( len(x), 5, 'something might be wrong with the number of transformations in the history' ) #should be 4 trans + starting structure #test the filter tsc.apply_filter( ContainsSpecieFilter(['Zn2+', 'Be2+', 'Mn4+'], strict_compare=True, AND=False)) self.assertEqual(len(tsc), 8) self.assertEqual( tsc.get_transformed_structures()[0].as_dict()['history'][-1] ['@class'], 'ContainsSpecieFilter') tsc.apply_filter(ContainsSpecieFilter(['Be2+'])) self.assertEqual(len(tsc), 4) #Test set_parameter and add_tag. tsc.set_parameter("para1", "hello") self.assertEqual( tsc.transformed_structures[0].as_dict()['other_parameters'] ['para1'], 'hello') tsc.add_tags(["world", "universe"]) self.assertEqual( tsc.transformed_structures[0].as_dict()['other_parameters'] ['tags'], ["world", "universe"])
def test_apply_transformation_mult(self): # Test returning multiple structures from each transformation. disord = Structure(np.eye(3) * 4.209, [{"Cs+": 0.5, "K+": 0.5}, "Cl-"], [[0, 0, 0], [0.5, 0.5, 0.5]]) disord.make_supercell([2, 2, 1]) tl = [EnumerateStructureTransformation(), OrderDisorderedStructureTransformation()] t = SuperTransformation(tl, nstructures_per_trans=10) self.assertEqual(len(t.apply_transformation(disord, return_ranked_list=20)), 8) t = SuperTransformation(tl) self.assertEqual(len(t.apply_transformation(disord, return_ranked_list=20)), 2)
def test_too_small_cell(self): t = OrderDisorderedStructureTransformation() coords = [] coords.append([0.5, 0.5, 0.5]) lattice = Lattice([ [3.8401979337, 0.00, 0.00], [1.9200989668, 3.3257101909, 0.00], [0.00, -2.2171384943, 3.1355090603], ]) struct = Structure(lattice, [{ "X4+": 0.33, "O2-": 0.33, "P5+": 0.33 }], coords) self.assertRaises(ValueError, t.apply_transformation, struct)
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)
def apply_transformation(self, structure, return_ranked_list=False): if not return_ranked_list: raise ValueError("MultipleSubstitutionTransformation has no single" " best structure output. Must use" " return_ranked_list.") outputs = [] for charge, el_list in self.substitution_dict.items(): mapping = {} if charge > 0: sign = "+" else: sign = "-" dummy_sp = "X{}{}".format(str(charge), sign) mapping[self.sp_to_replace] = { self.sp_to_replace: 1 - self.r_fraction, dummy_sp: self.r_fraction } trans = SubstitutionTransformation(mapping) dummy_structure = trans.apply_transformation(structure) if self.charge_balance_species is not None: cbt = ChargeBalanceTransformation(self.charge_balance_species) dummy_structure = cbt.apply_transformation(dummy_structure) if self.order: trans = OrderDisorderedStructureTransformation() dummy_structure = trans.apply_transformation(dummy_structure) for el in el_list: if charge > 0: sign = "+" else: sign = "-" st = SubstitutionTransformation({ "X{}+".format(str(charge)): "{}{}{}".format(el, charge, sign) }) new_structure = st.apply_transformation(dummy_structure) outputs.append({"structure": new_structure}) return outputs
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
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))
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, )
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")