Skip to content

Commit

Permalink
add duplicate check in check_params
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Oct 18, 2024
1 parent f0ebb34 commit f94d43e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
8 changes: 8 additions & 0 deletions src/pymatgen/io/vasp/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import re
import subprocess
import warnings
from collections import Counter
from enum import Enum, unique
from glob import glob
from hashlib import sha256
Expand Down Expand Up @@ -1022,6 +1023,13 @@ def check_params(self) -> None:
If a tag doesn't exist, calculation will still run, however VASP
will ignore the tag and set it as default without letting you know.
"""
# Check for duplicate
key_counts = Counter(key.upper() for key in self)
duplicates = [key for key, count in key_counts.items() if count > 1]

if duplicates:
warnings.warn(f"Duplicate keys found: {', '.join(duplicates)} in INCAR", BadIncarWarning, stacklevel=2)

# Load INCAR tag/value check reference file
with open(os.path.join(MODULE_DIR, "incar_parameters.json"), encoding="utf-8") as json_file:
incar_params = json.loads(json_file.read())
Expand Down
48 changes: 30 additions & 18 deletions tests/io/vasp/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,25 +773,25 @@ def test_proc_types(self):

def test_check_params(self):
# Triggers warnings when running into invalid parameters
incar = Incar(
{
"ADDGRID": True,
"ALGO": "Normal",
"AMIN": 0.01,
"ICHARG": 1,
"MAGMOM": [1, 2, 4, 5],
"ENCUT": 500, # make sure float key is casted
"GGA": "PS", # test string case insensitivity
"LREAL": True, # special case: Union type
"NBAND": 250, # typo in tag
"METAGGA": "SCAM", # typo in value
"EDIFF": 5 + 1j, # value should be a float
"ISIF": 9, # value out of range
"LASPH": 5, # value should be bool
"PHON_TLIST": "is_a_str", # value should be a list
}
)
with pytest.warns(BadIncarWarning) as record:
incar = Incar(
{
"ADDGRID": True,
"ALGO": "Normal",
"AMIN": 0.01,
"ICHARG": 1,
"MAGMOM": [1, 2, 4, 5],
"ENCUT": 500, # make sure float key is casted
"GGA": "PS", # test string case insensitivity
"LREAL": True, # special case: Union type
"NBAND": 250, # typo in tag
"METAGGA": "SCAM", # typo in value
"EDIFF": 5 + 1j, # value should be a float
"ISIF": 9, # value out of range
"LASPH": 5, # value should be bool
"PHON_TLIST": "is_a_str", # value should be a list
}
)
incar.check_params()

assert record[0].message.args[0] == "Cannot find NBAND in the list of INCAR tags"
Expand All @@ -801,6 +801,18 @@ def test_check_params(self):
assert record[4].message.args[0] == "LASPH: 5 is not a bool"
assert record[5].message.args[0] == "PHON_TLIST: is_a_str is not a list"

def test_check_params_check_duplicate(self):
incar = Incar(
{
"ENCUT": 500,
"encut": 400,
}
)
with pytest.warns(BadIncarWarning) as record:
incar.check_params()

assert record[0].message.args[0] == "Duplicate keys found: ENCUT in INCAR"


class TestKpointsSupportedModes:
def test_from_str(self):
Expand Down

0 comments on commit f94d43e

Please sign in to comment.