Skip to content

Commit 9cfef44

Browse files
Clearer handling of magmom flag
Clarify docstring of MAGMOM flag, only print warning for Co if magmom <= 1, allow for integer magmom properties; also, check Incar() for LASPH not settings.
1 parent dd37370 commit 9cfef44

File tree

2 files changed

+46
-45
lines changed

2 files changed

+46
-45
lines changed

pymatgen/io/vasp/sets.py

+27-31
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,10 @@ class DictSet(VaspInputSet):
281281
structure and the configuration settings. The order in which the magmom is
282282
determined is as follows:
283283
284-
1. If the site itself has a magmom setting (`site.magmom`), that is used.
285-
2. If the species on the site has a spin setting (`site.spin`), that is used.
284+
1. If the site itself has a magmom setting (i.e. site.properties["magmom"] = float),
285+
that is used. This can be set with structure.add_site_property().
286+
2. If the species of the site has a spin setting, that is used. This can be set
287+
with structure.add_spin_by_element().
286288
3. If the species itself has a particular setting in the config file, that
287289
is used, e.g., Mn3+ may have a different magmom than Mn4+.
288290
4. Lastly, the element symbol itself is checked in the config file. If
@@ -374,7 +376,7 @@ def __init__(
374376
international_monoclinic (bool): Whether to use international convention
375377
(vs Curtarolo) for monoclinic. Defaults True.
376378
validate_magmom (bool): Ensure that the missing magmom values are filled
377-
in with the vasp default value of 1.0
379+
in with the VASP default value of 1.0
378380
"""
379381
if reduce_structure:
380382
structure = structure.get_reduced_structure(reduce_structure)
@@ -503,38 +505,30 @@ def incar(self) -> Incar:
503505
if k == "MAGMOM":
504506
mag = []
505507
for site in structure:
508+
if v and not isinstance(v, dict):
509+
raise TypeError(
510+
"MAGMOM must be supplied in a dictionary format, e.g. {'Fe': 5}. "
511+
"If you want site-specific magnetic moments, set them in the site magmom properties "
512+
"of the site objects in the structure."
513+
)
506514
if hasattr(site, "magmom"):
507515
mag.append(site.magmom)
508516
elif hasattr(site.specie, "spin"):
509517
mag.append(site.specie.spin)
510518
elif str(site.specie) in v:
511-
if not isinstance(v, dict):
512-
raise TypeError(
513-
"MAGMOM must be supplied in a dictionary format, e.g. {'Fe': 5}. "
514-
"If you want site-specific magnetic moments, set them in the site.magmom properties "
515-
"of the site objects in the structure"
516-
)
517-
if site.specie.symbol == "Co":
519+
if site.specie.symbol == "Co" and v[str(site.specie)] <= 1.0:
518520
warnings.warn(
519-
"Co without an oxidation state is initialized as low spin by default in Pymatgen "
520-
"(unless you have provided a MAGMOM dictionary specifying otherwise). If this default "
521-
"behavior is not desired, please set the spin on the magmom on the site directly to "
522-
"ensure correct initialization"
521+
"Co without an oxidation state is initialized as low spin by default in Pymatgen. "
522+
"If this default behavior is not desired, please set the spin on the magmom on the "
523+
"site directly to ensure correct initialization."
523524
)
524525
mag.append(v.get(str(site.specie)))
525526
else:
526-
if not isinstance(v, dict):
527-
raise TypeError(
528-
"MAGMOM must be supplied in a dictionary format, e.g. {'Fe': 5}. "
529-
"If you want site-specific magnetic moments, set them in the site.magmom properties "
530-
"of the site objects in the structure"
531-
)
532527
if site.specie.symbol == "Co":
533528
warnings.warn(
534-
"Co without an oxidation state is initialized as low spin by default in Pymatgen "
535-
"(unless you have provided a MAGMOM dictionary specifying otherwise). If this default "
536-
"behavior is not desired, please set the spin on the magmom on the site directly to "
537-
"ensure correct initialization"
529+
"Co without an oxidation state is initialized as low spin by default in Pymatgen. "
530+
"If this default behavior is not desired, please set the spin on the magmom on the "
531+
"site directly to ensure correct initialization."
538532
)
539533
mag.append(v.get(site.specie.symbol, 0.6))
540534
incar[k] = mag
@@ -582,11 +576,11 @@ def incar(self) -> Incar:
582576
incar["LMAXMIX"] = 4
583577

584578
# Warn user about LASPH for +U, meta-GGAs, hybrids, and vdW-DF
585-
if not settings.get("LASPH", False) and (
586-
settings.get("METAGGA")
587-
or settings.get("LHFCALC", False)
588-
or settings.get("LDAU", False)
589-
or settings.get("LUSE_VDW", False)
579+
if not incar.get("LASPH", False) and (
580+
incar.get("METAGGA")
581+
or incar.get("LHFCALC", False)
582+
or incar.get("LDAU", False)
583+
or incar.get("LUSE_VDW", False)
590584
):
591585
warnings.warn(
592586
"LASPH = True should be set for +U, meta-GGAs, hybrids, and vdW-DFT",
@@ -3076,7 +3070,7 @@ def batch_write_input(
30763070

30773071
def get_valid_magmom_struct(structure, inplace=True, spin_mode="auto"):
30783072
"""
3079-
Make sure that the structure is valid magmoms based on the kind of caculation
3073+
Make sure that the structure has valid magmoms based on the kind of caculation
30803074
Fill in missing Magmom values
30813075
30823076
Args:
@@ -3099,10 +3093,12 @@ def get_valid_magmom_struct(structure, inplace=True, spin_mode="auto"):
30993093
for isite in structure.sites:
31003094
if "magmom" not in isite.properties or isite.properties["magmom"] is None:
31013095
pass
3102-
elif isinstance(isite.properties["magmom"], float):
3096+
elif isinstance(isite.properties["magmom"], (float, int)):
31033097
if mode == "v":
31043098
raise TypeError("Magmom type conflict")
31053099
mode = "s"
3100+
if isinstance(isite.properties["magmom"], int):
3101+
isite.properties["magmom"] = float(isite.properties["magmom"])
31063102
elif len(isite.properties["magmom"]) == 3:
31073103
if mode == "s":
31083104
raise TypeError("Magmom type conflict")

pymatgen/io/vasp/tests/test_sets.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -520,20 +520,6 @@ def test_init(self):
520520
self.assertEqual(vis.incar["ENCUT"], 600)
521521
self.assertEqual(vis.kpoints.style, Kpoints.supported_modes.Monkhorst)
522522

523-
# Check warning if LASPH is set to False for meta-GGAs/hybrids/+U/vdW
524-
with pytest.warns(BadInputSetWarning, match=r"LASPH"):
525-
vis = MPStaticSet(vis.structure, user_incar_settings={"METAGGA": "M06L", "LASPH": False})
526-
vis.incar.items()
527-
with pytest.warns(BadInputSetWarning, match=r"LASPH"):
528-
vis = MPStaticSet(vis.structure, user_incar_settings={"LHFCALC": True, "LASPH": False})
529-
vis.incar.items()
530-
with pytest.warns(BadInputSetWarning, match=r"LASPH"):
531-
vis = MPStaticSet(vis.structure, user_incar_settings={"LDAU": True, "LASPH": False})
532-
vis.incar.items()
533-
with pytest.warns(BadInputSetWarning, match=r"LASPH"):
534-
vis = MPStaticSet(vis.structure, user_incar_settings={"LUSE_VDW": True, "LASPH": False})
535-
vis.incar.items()
536-
537523
non_prev_vis = MPStaticSet(vis.structure, user_incar_settings={"LORBIT": 12, "LWAVE": True})
538524
self.assertEqual(non_prev_vis.incar["NSW"], 0)
539525
# Check that the ENCUT and Kpoints style has NOT been inherited.
@@ -560,6 +546,25 @@ def test_init(self):
560546
lcalcpol_vis = MPStaticSet.from_prev_calc(prev_calc_dir=prev_run, lcalcpol=True)
561547
self.assertTrue(lcalcpol_vis.incar["LCALCPOL"])
562548

549+
# Check warning if LASPH is set to False for meta-GGAs/hybrids/+U/vdW
550+
with pytest.warns(BadInputSetWarning, match=r"LASPH"):
551+
vis = MPStaticSet(vis.structure, user_incar_settings={"METAGGA": "M06L", "LASPH": False})
552+
vis.incar.items()
553+
with pytest.warns(BadInputSetWarning, match=r"LASPH"):
554+
vis = MPStaticSet(vis.structure, user_incar_settings={"LHFCALC": True, "LASPH": False})
555+
vis.incar.items()
556+
with pytest.warns(BadInputSetWarning, match=r"LASPH"):
557+
vis = MPStaticSet(vis.structure, user_incar_settings={"LUSE_VDW": True, "LASPH": False})
558+
vis.incar.items()
559+
with pytest.warns(BadInputSetWarning, match=r"LASPH"):
560+
dummy_struc = Structure(
561+
lattice=[[0, 2, 2], [2, 0, 2], [2, 2, 0]],
562+
species=["Fe", "O"],
563+
coords=[[0, 0, 0], [0.5, 0.5, 0.5]],
564+
)
565+
vis = MPStaticSet(dummy_struc, user_incar_settings={"LDAU": True, "LASPH": False})
566+
vis.incar.items()
567+
563568
def test_user_incar_kspacing(self):
564569
# Make sure user KSPACING settings properly overrides KPOINTS.
565570
si = self.get_structure("Si")

0 commit comments

Comments
 (0)