diff --git a/src/pymatgen/symmetry/analyzer.py b/src/pymatgen/symmetry/analyzer.py index 7e9bf70ccaa..3bce0e81af2 100644 --- a/src/pymatgen/symmetry/analyzer.py +++ b/src/pymatgen/symmetry/analyzer.py @@ -369,7 +369,7 @@ def get_refined_structure(self, keep_site_properties: bool = False) -> Structure if keep_site_properties: site_properties = {} for k, v in self._site_props.items(): - site_properties[k] = [v[i - 1] for i in numbers] + site_properties[k] = [v[self._numbers.index(i)] for i in numbers] else: site_properties = None struct = Structure(lattice, species, scaled_positions, site_properties=site_properties) @@ -398,7 +398,7 @@ def find_primitive(self, keep_site_properties: bool = False) -> Structure: if keep_site_properties: site_properties = {} for key, val in self._site_props.items(): - site_properties[key] = [val[i - 1] for i in numbers] + site_properties[key] = [val[self._numbers.index(i)] for i in numbers] else: site_properties = None diff --git a/tests/symmetry/test_analyzer.py b/tests/symmetry/test_analyzer.py index 509c31938ed..ac100ba5450 100644 --- a/tests/symmetry/test_analyzer.py +++ b/tests/symmetry/test_analyzer.py @@ -201,6 +201,15 @@ def test_get_refined_structure(self): refined_struct = sg.get_refined_structure(keep_site_properties=False) assert refined_struct.site_properties.get("magmom") is None + structure = Structure.from_file(f"{TEST_FILES_DIR}/cif/bcc_1927.cif") + for site in structure: + site.properties["magmom"] = 1.0 if site.specie.name == "Dy" else -1.0 + sg = SpacegroupAnalyzer(structure, symprec=1e-2) + refined_struct = sg.get_refined_structure(keep_site_properties=True) + assert len(refined_struct) != len(structure), "this test is only interesting if the number of sites changes" + for site in refined_struct: + assert (1.0 if site.specie.name == "Dy" else -1.0) == site.properties["magmom"] + def test_symmetrized_structure(self): symm_struct = self.sg.get_symmetrized_structure() assert symm_struct.lattice.angles == (90, 90, 90) @@ -246,6 +255,17 @@ def test_find_primitive(self): primitive_structure = spga.find_primitive(keep_site_properties=False) assert primitive_structure.site_properties.get("magmom") is None + structure = Structure.from_spacegroup("Fm-3m", np.eye(3) * 5.6, ["Na", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) + for site in structure: + site.properties["magmom"] = 1.0 if site.specie.name == "Na" else -1.0 + sg = SpacegroupAnalyzer(structure, symprec=1e-2) + primitive_structure = sg.find_primitive(keep_site_properties=True) + assert len(primitive_structure) != len( + structure + ), "this test is only interesting if the number of sites changes" + for site in primitive_structure: + assert (1.0 if site.specie.name == "Na" else -1.0) == site.properties["magmom"] + def test_get_ir_reciprocal_mesh(self): grid = self.sg.get_ir_reciprocal_mesh() assert len(grid) == 216