Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LNONCOLLINEAR match in Outcar parser #4034

Merged
merged 8 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/pymatgen/io/vasp/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2106,19 +2106,14 @@ def __init__(self, filename: PathLike) -> None:
self.drift = self.data.get("drift", [])

# Check if calculation is spin polarized
self.spin = False
self.read_pattern({"spin": "ISPIN = 2"})
if self.data.get("spin", []):
self.spin = True
self.read_pattern({"spin": r"ISPIN\s*=\s*2"}, terminate_on_match=True)
self.spin = bool(self.data.get("spin", []))

# Check if calculation is non-collinear
self.noncollinear = False
self.read_pattern({"noncollinear": "LNONCOLLINEAR = T"})
if self.data.get("noncollinear", []):
self.noncollinear = False
self.read_pattern({"noncollinear": r"LNONCOLLINEAR\s*=\s*T"}, terminate_on_match=True)
Copy link
Member

@janosh janosh Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these lines suggest that the matches dict can contain less keys if terminate_on_match=True and so you might get a different self.data after self.data.update(matches). but given all tests pass, this may not happen in practice

matches = regrep(
self.filename,
patterns,
reverse=reverse,
terminate_on_match=terminate_on_match,
postprocess=postprocess,
)
self.data.update(matches)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any concerns @mkhorton?

Copy link
Contributor Author

@DanielYang59 DanielYang59 Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be safer for me to revert those changes in terminate_on_match until I'm 100% percent it wouldn't cause any breakage.

Currently there must have been some part of Outcar not covered by unit test, because the reverse reading in #4033 seems completely broken in Windows, but tests are still passing.

I would keep this into my TODO list, and have a closer look later.

self.noncollinear = bool(self.data.get("noncollinear", []))

# Check if the calculation type is DFPT
self.dfpt = False
self.read_pattern(
{"ibrion": r"IBRION =\s+([\-\d]+)"},
terminate_on_match=True,
Expand All @@ -2127,24 +2122,28 @@ def __init__(self, filename: PathLike) -> None:
if self.data.get("ibrion", [[0]])[0][0] > 6:
self.dfpt = True
self.read_internal_strain_tensor()
else:
self.dfpt = False

# Check if LEPSILON is True and read piezo data if so
self.lepsilon = False
self.read_pattern({"epsilon": "LEPSILON= T"})
self.read_pattern({"epsilon": r"LEPSILON\s*=\s*T"}, terminate_on_match=True)
if self.data.get("epsilon", []):
self.lepsilon = True
self.read_lepsilon()
# Only read ionic contribution if DFPT is turned on
if self.dfpt:
self.read_lepsilon_ionic()
else:
self.lepsilon = False

# Check if LCALCPOL is True and read polarization data if so
self.lcalcpol = False
self.read_pattern({"calcpol": "LCALCPOL = T"})
self.read_pattern({"calcpol": r"LCALCPOL\s*=\s*T"}, terminate_on_match=True)
if self.data.get("calcpol", []):
self.lcalcpol = True
self.read_lcalcpol()
self.read_pseudo_zval()
else:
self.lcalcpol = False

# Read electrostatic potential
self.electrostatic_potential: list[float] | None = None
Expand All @@ -2154,30 +2153,33 @@ def __init__(self, filename: PathLike) -> None:
if self.data.get("electrostatic", []):
self.read_electrostatic_potential()

self.nmr_cs = False
self.read_pattern({"nmr_cs": r"LCHIMAG = (T)"})
self.read_pattern({"nmr_cs": r"LCHIMAG\s*=\s*(T)"}, terminate_on_match=True)
if self.data.get("nmr_cs"):
self.nmr_cs = True
self.read_chemical_shielding()
self.read_cs_g0_contribution()
self.read_cs_core_contribution()
self.read_cs_raw_symmetrized_tensors()
else:
self.nmr_cs = False

self.nmr_efg = False
self.read_pattern({"nmr_efg": r"NMR quadrupolar parameters"})
if self.data.get("nmr_efg"):
self.nmr_efg = True
self.read_nmr_efg()
self.read_nmr_efg_tensor()
else:
self.nmr_efg = False

self.has_onsite_density_matrices = False
self.read_pattern(
{"has_onsite_density_matrices": r"onsite density matrix"},
terminate_on_match=True,
)
if "has_onsite_density_matrices" in self.data:
self.has_onsite_density_matrices = True
self.read_onsite_density_matrices()
else:
self.has_onsite_density_matrices = False

# Store the individual contributions to the final total energy
final_energy_contribs = {}
Expand Down
DanielYang59 marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
4 changes: 4 additions & 0 deletions tests/io/vasp/test_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,10 @@ def test_polarization(self):
assert outcar.p_ion == approx([0.0, 0.0, -5.56684])
assert outcar.p_elec == approx([0.00024, 0.00019, 3.61674])

def test_noncollinear(self):
outcar_ncl = Outcar(f"{VASP_OUT_DIR}/OUTCAR.noncollinear.gz")
assert outcar_ncl.noncollinear is True

def test_pseudo_zval(self):
filepath = f"{VASP_OUT_DIR}/OUTCAR.BaTiO3.polar"
outcar = Outcar(filepath)
Expand Down