Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not introduce zero magmoms in SpacegroupAnalyzer #2727

Merged
merged 3 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions pymatgen/symmetry/analyzer.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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