def test_to_dict_and_from_dict(self): sm = StructureMatcher(ltol=0.1, stol=0.2, angle_tol=2, primitive_cell=False, scale=False, comparator=FrameworkComparator()) d = sm.to_dict sm2 = StructureMatcher.from_dict(d) self.assertEqual(sm2.to_dict, d)
def test_fit(self): """ Take two known matched structures 1) Ensure match 2) Ensure match after translation and rotations 3) Ensure no-match after large site translation 4) Ensure match after site shuffling """ sm = StructureMatcher() self.assertTrue(sm.fit(self.struct_list[0], self.struct_list[1])) # Test rotational/translational invariance op = SymmOp.from_axis_angle_and_translation([0, 0, 1], 30, False, np.array([0.4, 0.7, 0.9])) self.struct_list[1].apply_operation(op) self.assertTrue(sm.fit(self.struct_list[0], self.struct_list[1])) #Test failure under large atomic translation self.struct_list[1].translate_sites([0], [.4, .4, .2], frac_coords=True) self.assertFalse(sm.fit(self.struct_list[0], self.struct_list[1])) self.struct_list[1].translate_sites([0], [-.4, -.4, -.2], frac_coords=True) # random.shuffle(editor._sites) self.assertTrue(sm.fit(self.struct_list[0], self.struct_list[1])) #Test FrameworkComporator sm2 = StructureMatcher(comparator=FrameworkComparator()) lfp = read_structure(os.path.join(test_dir, "LiFePO4.cif")) nfp = read_structure(os.path.join(test_dir, "NaFePO4.cif")) self.assertTrue(sm2.fit(lfp, nfp)) self.assertFalse(sm.fit(lfp, nfp)) #Test anonymous fit. self.assertEqual(sm.fit_anonymous(lfp, nfp), {Composition("Li"): Composition("Na")}) self.assertAlmostEqual(sm.get_minimax_rms_anonymous(lfp, nfp)[0], 0.096084154118549828) #Test partial occupancies. s1 = Structure([[3, 0, 0], [0, 3, 0], [0, 0, 3]], [{"Fe": 0.5}, {"Fe": 0.5}, {"Fe": 0.5}, {"Fe": 0.5}], [[0, 0, 0], [0.25, 0.25, 0.25], [0.5, 0.5, 0.5], [0.75, 0.75, 0.75]]) s2 = Structure([[3, 0, 0], [0, 3, 0], [0, 0, 3]], [{"Fe": 0.25}, {"Fe": 0.5}, {"Fe": 0.5}, {"Fe": 0.75}], [[0, 0, 0], [0.25, 0.25, 0.25], [0.5, 0.5, 0.5], [0.75, 0.75, 0.75]]) self.assertFalse(sm.fit(s1, s2)) self.assertFalse(sm.fit(s2, s1)) s2 = Structure([[3, 0, 0], [0, 3, 0], [0, 0, 3]], [{"Fe": 0.25}, {"Fe": 0.25}, {"Fe": 0.25}, {"Fe": 0.25}], [[0, 0, 0], [0.25, 0.25, 0.25], [0.5, 0.5, 0.5], [0.75, 0.75, 0.75]]) self.assertEqual(sm.fit_anonymous(s1, s2), {Composition("Fe0.5"): Composition("Fe0.25")}) self.assertAlmostEqual(sm.get_minimax_rms_anonymous(s1, s2)[0], 0)
def test_fit(self): """ Take two known matched structures 1) Ensure match 2) Ensure match after translation and rotations 3) Ensure no-match after large site translation 4) Ensure match after site shuffling """ sm = StructureMatcher() self.assertTrue(sm.fit(self.struct_list[0], self.struct_list[1])) # Test rotational/translational invariance op = SymmOp.from_axis_angle_and_translation([0, 0, 1], 30, False, np.array([0.4, 0.7, 0.9])) self.struct_list[1].apply_operation(op) self.assertTrue(sm.fit(self.struct_list[0], self.struct_list[1])) # Test failure under large atomic translation self.struct_list[1].translate_sites([0], [.4, .4, .2], frac_coords=True) self.assertFalse(sm.fit(self.struct_list[0], self.struct_list[1])) self.struct_list[1].translate_sites([0], [-.4, -.4, -.2], frac_coords=True) # random.shuffle(editor._sites) self.assertTrue(sm.fit(self.struct_list[0], self.struct_list[1])) # Test FrameworkComporator sm2 = StructureMatcher(comparator=FrameworkComparator()) lfp = self.get_structure("LiFePO4") nfp = self.get_structure("NaFePO4") self.assertTrue(sm2.fit(lfp, nfp)) self.assertFalse(sm.fit(lfp, nfp)) # Test anonymous fit. self.assertEqual(sm.fit_anonymous(lfp, nfp), True) self.assertAlmostEqual(sm.get_rms_anonymous(lfp, nfp)[0], 0.060895871160262717) # Test partial occupancies. s1 = Structure(Lattice.cubic(3), [{"Fe": 0.5}, {"Fe": 0.5}, {"Fe": 0.5}, {"Fe": 0.5}], [[0, 0, 0], [0.25, 0.25, 0.25], [0.5, 0.5, 0.5], [0.75, 0.75, 0.75]]) s2 = Structure(Lattice.cubic(3), [{"Fe": 0.25}, {"Fe": 0.5}, {"Fe": 0.5}, {"Fe": 0.75}], [[0, 0, 0], [0.25, 0.25, 0.25], [0.5, 0.5, 0.5], [0.75, 0.75, 0.75]]) self.assertFalse(sm.fit(s1, s2)) self.assertFalse(sm.fit(s2, s1)) s2 = Structure(Lattice.cubic(3), [{"Mn": 0.5}, {"Mn": 0.5}, {"Mn": 0.5}, {"Mn": 0.5}], [[0, 0, 0], [0.25, 0.25, 0.25], [0.5, 0.5, 0.5], [0.75, 0.75, 0.75]]) self.assertEqual(sm.fit_anonymous(s1, s2), True) self.assertAlmostEqual(sm.get_rms_anonymous(s1, s2)[0], 0)
def group_structures(s_l): """group_structures Applies a structure grouping algorithm to a list of structures. :param s_l: List of Structure objects. :return: List of lists of grouped structures. """ sm = StructureMatcher(scale=True, attempt_supercell=True, comparator=FrameworkComparator()) return sm.group_structures(s_l)
def __init__(self, Asite_struct, Bspecie): super(observer_spinel, self).__init__() self.Asite_struct = Asite_struct self.Bspecie = Bspecie self.site_matcher = StructureMatcher( ltol=0.1, primitive_cell=False, allow_subset=True, comparator=FrameworkComparator(), ignored_species=["O"], )
def __init__( self, base_structure, defect_sublattices, num_defects, cellsize=[1, 1, 1], perf_structure=None, ): try: num_defect_sublat = len(defect_sublattices) except TypeError: num_defect_sublat = 1 defect_sublattices = [defect_sublattices] num_defects = [num_defects] self.matcher_base = StructureMatcher(primitive_cell=False, allow_subset=True) self.matcher_frame = StructureMatcher( stol=0.4, primitive_cell=False, allow_subset=True, comparator=FrameworkComparator(), ) self.calc_history = [] self.cellsize = cellsize self.base_structure = base_structure if self.base_structure.num_sites == 0: # we need at least one site for make_supercell self.base_structure.append("H", np.array([0, 0, 0])) self.base_structure.make_supercell( [cellsize[0], cellsize[1], cellsize[2]]) self.base_structure.remove_sites( range(self.base_structure.num_sites)) else: self.base_structure.make_supercell( [cellsize[0], cellsize[1], cellsize[2]]) if perf_structure: self.perf_structure = perf_structure self.perf_structure.make_supercell( [cellsize[0], cellsize[1], cellsize[2]]) self.supercell = self.base_structure.lattice.matrix self.n_sublat = num_defect_sublat invSuper = np.linalg.inv(self.supercell) # add supercell information to defect_sites num_sites = 0 ntot_defects = 0 sublat_id = 0 for defect_sublattice in defect_sublattices: site_centers = defect_sublattice.site_centers defect_sublattice.site_centers_sc = np.zeros( (np.prod(cellsize) * site_centers.shape[0], 3), dtype=float) idx = 0 for i in range(cellsize[0]): for j in range(cellsize[1]): for k in range(cellsize[2]): for l in range(site_centers.shape[0]): defect_sublattice.site_centers_sc[ idx] = site_centers[l] + np.array([i, j, k]) idx += 1 defect_sublattice.site_centers_sc /= np.array(cellsize) num_sites += len(defect_sublattice.site_centers_sc) # change cartesian coordinates to fractional for group in defect_sublattice.groups: for i in range(group.orientations): for j in range(group.natoms): group.coords[i][j] = np.dot(group.coords[i][j], invSuper) # fill the lattice gas representation list latgas_rep = [] for group in num_defects[sublat_id].keys(): latgas_rep.extend( [[group, 0] for i in range(num_defects[sublat_id][group])]) ntot_defects += num_defects[sublat_id][group] defect_sublattice.latgas_rep = latgas_rep sublat_id += 1 self.defect_sublattices = defect_sublattices assert num_sites == ntot_defects self.set_latgas()
throwout = 1 tomlfile = sys.argv[1] if len(sys.argv) > 1 else "input.toml" rxparams = RXParams.from_toml(tomlfile) nreplicas = rxparams.nreplicas comm = MPI.COMM_WORLD spinel_struct = Structure.from_file("MgAl2O4.vasp") Asite_struct = spinel_struct.copy() Asite_struct.remove_species(["Al", "O"]) matcher = StructureMatcher( ltol=0.1, primitive_cell=False, allow_subset=True, comparator=FrameworkComparator(), ignored_species=["O"], ) def calc_DOI(structure): asites = matcher.get_mapping(structure, Asite_struct) x = 0 species = [str(sp) for sp in structure.species] for i in asites: if species[i] == "Al": x += 1 x /= float(len(asites)) return x
import numpy as np import re from pymatgen.analysis.structure_matcher import StructureMatcher, FrameworkComparator from pymatgen.io.cif import CifParser f = open('results.csv', 'r') g = open('structuretypes_1D.txt', 'w') content = f.read() f.close() lines = content.split('\n')[1:-1] pattern = re.compile('[0-9]') dim = [line.split(';')[2] for line in lines] icsd_codes = [line.split(';')[1] for line in lines] number_index = [line.split(';')[11] for line in lines] structure_types = [] sm = StructureMatcher(scale=True, comparator=FrameworkComparator()) for i in range(len(icsd_codes)): print(i) if dim[i] == "1": structure1 = CifParser( "cif_files/data_" + icsd_codes[i] + "-ICSD.cif", occupancy_tolerance=100).get_structures(primitive=False)[0] for j in range(i): if dim[j] == "1" and number_index[i] == number_index[j]: """if icsd_codes[i] == icsd_codes[j]: structure_types.append(structure_types[j]) g.write(str(structure_types[-1])+'\n') break""" structure2 = CifParser( "cif_files/data_" + icsd_codes[j] + "-ICSD.cif", occupancy_tolerance=100).get_structures(primitive=False)[0]