Skip to content

Commit 141d3b1

Browse files
authored
Merge pull request #3157 from mattmcdermott/magnetic-analyzer-fix
Bug fix: `CollinearMagneticAnalyzer` should not fail when `Species.spin = None`
2 parents a91da2f + 09506fd commit 141d3b1

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pymatgen/analysis/magnetism/analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def __init__(
187187
)
188188
magmoms = [m or 0 for m in structure.site_properties["magmom"]]
189189
elif has_spin:
190-
magmoms = [getattr(sp, "spin", 0) for sp in structure.species]
190+
magmoms = [sp.spin or 0 for sp in structure.species]
191191
structure.remove_spin()
192192
else:
193193
# no magmoms present, add zero magmoms for now

pymatgen/analysis/magnetism/tests/test_analyzer.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ def test_round_magmoms(self):
237237
assert msa.magnetic_species_and_magmoms["Ni"][1] == approx(5.0143)
238238
assert msa.magnetic_species_and_magmoms["O"] == approx(0.1465)
239239

240+
def test_missing_spin(self):
241+
# This test catches the case where a structure has some species with
242+
# Species.spin=None. This previously raised an error upon construction
243+
# of the analyzer).
244+
latt = Lattice([[2.085, 2.085, 0.0], [0.0, -2.085, -2.085], [-2.085, 2.085, -4.17]])
245+
species = [Species("Ni", spin=-5), Species("Ni", spin=5), Species("O", spin=None), Species("O", spin=None)]
246+
coords = [[0.5, 0, 0.5], [0, 0, 0], [0.25, 0.5, 0.25], [0.75, 0.5, 0.75]]
247+
struct = Structure(latt, species, coords)
248+
249+
msa = CollinearMagneticStructureAnalyzer(struct, round_magmoms=0.001, make_primitive=False)
250+
assert msa.structure.site_properties["magmom"] == [-5, 5, 0, 0]
251+
240252

241253
class MagneticStructureEnumeratorTest(unittest.TestCase):
242254
@unittest.skipIf(not enumlib_present, "enumlib not present")

0 commit comments

Comments
 (0)