Skip to content

Commit

Permalink
Bugfix: Parsing bugs in io.pwscf.PWInput (#4115)
Browse files Browse the repository at this point in the history
* Bugfix: Correct float parsing in io.pwscf.PWInput

* Test: Add test case related to previous commit

* Bugfix: K_POINTS card parsing in io.pwscf.PWInput

- In "automatic" mode
- Add test case to prevent regression
  • Loading branch information
jsukpark authored Oct 21, 2024
1 parent aa9bdbf commit 65e21cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/pymatgen/io/pwscf.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,10 @@ def input_mode(line):
pseudo[match[2].strip()] = match[4].strip()

elif mode[0] == "kpoints":
if match := re.match(r"(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)", line):
kpoints_grid = (int(match[1]), int(match[2]), int(match[3]))
kpoints_shift = (int(match[4]), int(match[5]), int(match[6]))
if match := re.match(r"^(\s*)(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)", line):
format_options["indent"] = len(match[1])
kpoints_grid = (int(match[2]), int(match[3]), int(match[4]))
kpoints_shift = (int(match[5]), int(match[6]), int(match[7]))
else:
kpoints_mode = mode[1]

Expand Down Expand Up @@ -537,7 +538,7 @@ def smart_int_or_float(num_str):
raise ValueError(f"{key} should be a boolean type!")

if key in float_keys:
return float(re.search(r"^-?\d*\.?\d*d?-?\d*", val.lower())[0].replace("d", "e"))
return float(re.search(r"^-?\d*\.?\d*[de]?-?\d*", val.lower())[0].replace("d", "e"))

if key in int_keys:
return int(re.match(r"^-?[0-9]+", val)[0])
Expand Down
12 changes: 10 additions & 2 deletions tests/io/test_pwscf.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def test_read_str(self):
smearing = 'cold'
/
&ELECTRONS
conv_thr = 5.3E-5,
/
&IONS
/
Expand Down Expand Up @@ -300,7 +301,8 @@ def test_read_str(self):
O 0.833534457 0.833534457 0.166465543
O 0.833411466 0.833411466 0.499764601
O 0.833411466 0.833411466 0.833411466
K_POINTS gamma
K_POINTS automatic
4 4 4 1 1 1
CELL_PARAMETERS angstrom
0.000000 6.373854 6.373854
6.373854 0.000000 6.373854
Expand Down Expand Up @@ -375,6 +377,10 @@ def test_read_str(self):

assert_allclose(lattice, pw_in.structure.lattice.matrix)
assert pw_in.sections["system"]["smearing"] == "cold"
assert pw_in.sections["electrons"]["conv_thr"] == 5.3e-5
assert pw_in.kpoints_mode == "automatic"
assert pw_in.kpoints_grid == (4, 4, 4)
assert pw_in.kpoints_shift == (1, 1, 1)

def test_write_and_read_str(self):
struct = self.get_structure("Graphite")
Expand All @@ -384,6 +390,8 @@ def test_write_and_read_str(self):
pseudo={"C": "C.pbe-n-kjpaw_psl.1.0.0.UPF"},
control={"calculation": "scf", "pseudo_dir": "./"},
system={"ecutwfc": 45},
kpoints_grid=(4, 4, 4),
kpoints_shift=(1, 1, 1),
)
pw_str = str(pw)
assert pw_str.strip() == str(PWInput.from_str(pw_str)).strip()
Expand All @@ -402,7 +410,7 @@ def test_write_and_read_str_with_oxidation(self):
pw_str = str(pw)
assert pw_str.strip() == str(PWInput.from_str(pw_str)).strip()

def test_custom_decimal_precision(self):
def test_custom_decimal_precision_and_indent(self):
struct = self.get_structure("Li2O")
pw = PWInput(
struct,
Expand Down

0 comments on commit 65e21cc

Please sign in to comment.