Skip to content

Commit

Permalink
Do not introduce zero magmoms in SpacegroupAnalyzer (#2727)
Browse files Browse the repository at this point in the history
* do not introduce zero magmoms in SpacegroupAnalyzer

* fix mypy error

* fix test_get_symmetry()

modify sga._cell in place to make sure ValueError is raised

Co-authored-by: Janosh Riebesell <janosh.riebesell@gmail.com>
  • Loading branch information
lbluque and janosh committed Nov 8, 2022
1 parent c70a590 commit 6b520e3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 6 additions & 4 deletions pymatgen/symmetry/analyzer.py
Expand Up @@ -21,7 +21,7 @@
from collections import defaultdict
from fractions import Fraction
from math import cos, sin
from typing import Literal
from typing import Any, Literal

import numpy as np
import spglib
Expand Down Expand Up @@ -76,12 +76,14 @@ def __init__(self, structure: Structure, symprec: float = 0.01, angle_tolerance=
magmoms.append(site.magmom)
elif site.is_ordered and hasattr(site.specie, "spin"):
magmoms.append(site.specie.spin)
else:
magmoms.append(0) # needed for spglib

self._unique_species = unique_species
self._numbers = zs
self._cell = structure.lattice.matrix, structure.frac_coords, zs, magmoms

if len(magmoms) > 0:
self._cell: tuple[Any, ...] = structure.lattice.matrix, structure.frac_coords, zs, magmoms
else: # if no magmoms given do not add to cell
self._cell = structure.lattice.matrix, structure.frac_coords, zs

self._space_group_data = spglib.get_symmetry_dataset(
self._cell, symprec=self._symprec, angle_tolerance=angle_tolerance
Expand Down
5 changes: 4 additions & 1 deletion pymatgen/symmetry/tests/test_analyzer.py
Expand Up @@ -134,7 +134,10 @@ def test_get_symmetry(self):
match=f"Symmetry detection failed for structure with formula {Co8.formula}. "
f"Try setting {symprec=} to a different value.",
):
SpacegroupAnalyzer(Co8, symprec=symprec)._get_symmetry()
sga = SpacegroupAnalyzer(Co8, symprec=symprec)
magmoms = [0] * len(Co8) # bad magmoms, see https://github.com/materialsproject/pymatgen/pull/2727
sga._cell = (*sga._cell, magmoms)
sga._get_symmetry()

def test_get_crystal_system(self):
crystal_system = self.sg.get_crystal_system()
Expand Down

0 comments on commit 6b520e3

Please sign in to comment.