Skip to content

Commit

Permalink
BUG: fix setting zero magmoms (#3179)
Browse files Browse the repository at this point in the history
* BUG: fix setting zero magmoms

Signed-off-by: lbluque <lbluque@berkeley.edu>

* TST: test setting magmoms in _cell

* pre-commit auto-fixes

---------

Signed-off-by: lbluque <lbluque@berkeley.edu>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
lbluque and pre-commit-ci[bot] authored Jul 24, 2023
1 parent 5bdc1ca commit 4c7e997
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
8 changes: 3 additions & 5 deletions pymatgen/symmetry/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ def __init__(self, structure: Structure, symprec: float | None = 0.01, angle_tol
unique_species.append(species)
zs.extend([len(unique_species)] * len(tuple(group)))

has_explicit_magmoms = False
if "magmom" in structure.site_properties or any(
hasattr(specie, "spin") for specie in structure.types_of_species
):
has_explicit_magmoms = True
has_explicit_magmoms = "magmom" in structure.site_properties or any(
getattr(specie, "spin", None) is not None for specie in structure.types_of_species
)

for site in structure:
if hasattr(site, "magmom"):
Expand Down
35 changes: 35 additions & 0 deletions pymatgen/symmetry/tests/test_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
from pytest import approx

from pymatgen.core.periodic_table import Species
from pymatgen.core.sites import PeriodicSite
from pymatgen.core.structure import Molecule, Structure
from pymatgen.io.cif import CifParser
Expand Down Expand Up @@ -122,6 +123,40 @@ def test_get_symmetry_dataset(self):
ds = self.sg.get_symmetry_dataset()
assert ds["international"] == "Pnma"

def test_init_cell(self):
# see https://github.com/materialsproject/pymatgen/pull/3179
li2o = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "Li2O.cif"))

# test that magmoms are not included in spacegroup analyzer when species have no spin
# or no magmom site properties are set
assert all(species.spin is None for species in li2o.types_of_species)
assert "magmom" not in li2o.site_properties
sga = SpacegroupAnalyzer(li2o)
assert len(sga._cell) == 3 # no magmoms should be added!

# give a magmom to random Li site
li2o[0].properties["magmom"] = 0
sga = SpacegroupAnalyzer(li2o)
assert len(sga._cell) == 4 # magmoms should be added!
assert sga._cell[3] == 12 * (0,)

# now set spin for O only
li2o = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "Li2O.cif"))
li2o.replace_species({"O2-": Species("O", oxidation_state=-2, spin=1)})
assert not all(species.spin is None for species in li2o.types_of_species)
sga = SpacegroupAnalyzer(li2o)
assert len(sga._cell) == 4 # magmoms should be added!
assert sga._cell[3] == tuple(
8
* [
0,
]
+ 4
* [
1,
]
)

def test_get_symmetry(self):
# see discussion in https://github.com/materialsproject/pymatgen/pull/2724
Co8 = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "Co8.cif"))
Expand Down

0 comments on commit 4c7e997

Please sign in to comment.