Skip to content

Commit

Permalink
Properly fix tests for bad vasprun dielectric.
Browse files Browse the repository at this point in the history
  • Loading branch information
shyuep committed Oct 26, 2024
1 parent 203b354 commit 77dac22
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
25 changes: 14 additions & 11 deletions src/pymatgen/io/vasp/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,8 @@ def _parse(
label = "velocity"
else:
warnings.warn(
"Additional unlabelled dielectric data in vasprun.xml are stored as unlabelled."
"Additional unlabelled dielectric data in vasprun.xml are stored as unlabelled.",
UserWarning,
)
label = "unlabelled"
# VASP 6+ has labels for the density and current
Expand Down Expand Up @@ -1522,16 +1523,18 @@ def _parse_structure(self, elem: XML_Element) -> Structure:
@staticmethod
def _parse_diel(elem: XML_Element) -> tuple[list, list, list]:
"""Parse dielectric properties."""
imag = [
[_vasprun_float(line) for line in r.text.split()] # type: ignore[union-attr]
for r in elem.find("imag").find("array").find("set").findall("r") # type: ignore[union-attr]
]
real = [
[_vasprun_float(line) for line in r.text.split()] # type: ignore[union-attr]
for r in elem.find("real").find("array").find("set").findall("r") # type: ignore[union-attr]
]
elem.clear()
return [e[0] for e in imag], [e[1:] for e in real], [e[1:] for e in imag]
if elem.find("real") and elem.find("imag"):
imag = [
[_vasprun_float(line) for line in r.text.split()] # type: ignore[union-attr]
for r in elem.find("imag").find("array").find("set").findall("r") # type: ignore[union-attr]
]
real = [
[_vasprun_float(line) for line in r.text.split()] # type: ignore[union-attr]
for r in elem.find("real").find("array").find("set").findall("r") # type: ignore[union-attr]
]
elem.clear()
return [e[0] for e in imag], [e[1:] for e in real], [e[1:] for e in imag]
return [], [], []

@staticmethod
def _parse_optical_transition(elem: XML_Element) -> tuple[NDArray, NDArray]:
Expand Down
9 changes: 5 additions & 4 deletions tests/io/vasp/test_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@ def test_BSE(self):
assert "freq_dependent" in vasp_run.dielectric_data

def test_vasprun_with_more_than_two_unlabelled_dielectric_functions(self):
with pytest.raises(
NotImplementedError,
match="This vasprun.xml has >2 unlabelled dielectric functions",
with pytest.warns(
UserWarning,
match="Additional unlabelled dielectric data in vasprun.xml are stored as unlabelled.",
):
Vasprun(f"{VASP_OUT_DIR}/vasprun.dielectric_bad.xml.gz")
vr = Vasprun(f"{VASP_OUT_DIR}/vasprun.dielectric_bad.xml.gz")
assert "unlabelled" in vr.dielectric_data

def test_bad_vasprun(self):
with pytest.raises(ET.ParseError):
Expand Down

0 comments on commit 77dac22

Please sign in to comment.