Skip to content

Commit 4c7e997

Browse files
BUG: fix setting zero magmoms (#3179)
* BUG: fix setting zero magmoms Signed-off-by: lbluque <[email protected]> * TST: test setting magmoms in _cell * pre-commit auto-fixes --------- Signed-off-by: lbluque <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5bdc1ca commit 4c7e997

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

pymatgen/symmetry/analyzer.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,9 @@ def __init__(self, structure: Structure, symprec: float | None = 0.01, angle_tol
8585
unique_species.append(species)
8686
zs.extend([len(unique_species)] * len(tuple(group)))
8787

88-
has_explicit_magmoms = False
89-
if "magmom" in structure.site_properties or any(
90-
hasattr(specie, "spin") for specie in structure.types_of_species
91-
):
92-
has_explicit_magmoms = True
88+
has_explicit_magmoms = "magmom" in structure.site_properties or any(
89+
getattr(specie, "spin", None) is not None for specie in structure.types_of_species
90+
)
9391

9492
for site in structure:
9593
if hasattr(site, "magmom"):

pymatgen/symmetry/tests/test_analyzer.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pytest
88
from pytest import approx
99

10+
from pymatgen.core.periodic_table import Species
1011
from pymatgen.core.sites import PeriodicSite
1112
from pymatgen.core.structure import Molecule, Structure
1213
from pymatgen.io.cif import CifParser
@@ -122,6 +123,40 @@ def test_get_symmetry_dataset(self):
122123
ds = self.sg.get_symmetry_dataset()
123124
assert ds["international"] == "Pnma"
124125

126+
def test_init_cell(self):
127+
# see https://github.com/materialsproject/pymatgen/pull/3179
128+
li2o = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "Li2O.cif"))
129+
130+
# test that magmoms are not included in spacegroup analyzer when species have no spin
131+
# or no magmom site properties are set
132+
assert all(species.spin is None for species in li2o.types_of_species)
133+
assert "magmom" not in li2o.site_properties
134+
sga = SpacegroupAnalyzer(li2o)
135+
assert len(sga._cell) == 3 # no magmoms should be added!
136+
137+
# give a magmom to random Li site
138+
li2o[0].properties["magmom"] = 0
139+
sga = SpacegroupAnalyzer(li2o)
140+
assert len(sga._cell) == 4 # magmoms should be added!
141+
assert sga._cell[3] == 12 * (0,)
142+
143+
# now set spin for O only
144+
li2o = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "Li2O.cif"))
145+
li2o.replace_species({"O2-": Species("O", oxidation_state=-2, spin=1)})
146+
assert not all(species.spin is None for species in li2o.types_of_species)
147+
sga = SpacegroupAnalyzer(li2o)
148+
assert len(sga._cell) == 4 # magmoms should be added!
149+
assert sga._cell[3] == tuple(
150+
8
151+
* [
152+
0,
153+
]
154+
+ 4
155+
* [
156+
1,
157+
]
158+
)
159+
125160
def test_get_symmetry(self):
126161
# see discussion in https://github.com/materialsproject/pymatgen/pull/2724
127162
Co8 = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "Co8.cif"))

0 commit comments

Comments
 (0)