Skip to content

Commit

Permalink
fix: preserve site properties over cell transform (#4123)
Browse files Browse the repository at this point in the history
Make sure site properties are properly transferred from the original cell to
the primitive or refined one.

Co-authored-by: Shyue Ping Ong <[email protected]>
  • Loading branch information
Lattay and shyuep authored Oct 21, 2024
1 parent 52eeeea commit 7b7e61b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/pymatgen/symmetry/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions tests/symmetry/test_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7b7e61b

Please sign in to comment.