Skip to content

Commit

Permalink
remove reverse readline
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Sep 15, 2024
1 parent 48422c3 commit 77518b5
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions chgnet/utils/vasp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import warnings
from typing import TYPE_CHECKING

from monty.io import reverse_readfile
from monty.io import zopen
from monty.os.path import zpath
from pymatgen.io.vasp.outputs import Oszicar, Vasprun

Expand Down Expand Up @@ -58,13 +58,11 @@ def parse_vasp_dir(
exception_on_bad_xml=False,
)

charge, mag_x, mag_y, mag_z, header, all_lines = [], [], [], [], [], []
charge, mag_x, mag_y, mag_z, header = [], [], [], [], []

for line in reverse_readfile(outcar_path):
clean = line.strip()
all_lines.append(clean)
with zopen(outcar_path, encoding="utf-8") as file:
all_lines = [line.strip() for line in file]

all_lines.reverse()
# For single atom systems, VASP doesn't print a total line, so
# reverse parsing is very difficult
# for SOC calculations only
Expand All @@ -79,23 +77,21 @@ def parse_vasp_dir(
if clean.startswith("# of ion"):
header = re.split(r"\s{2,}", clean.strip())
header.pop(0)
else:
m = re.match(r"\s*(\d+)\s+(([\d\.\-]+)\s+)+", clean)
if m:
tokens = [float(token) for token in re.findall(r"[\d\.\-]+", clean)]
tokens.pop(0)
if read_charge:
charge.append(dict(zip(header, tokens)))
elif read_mag_x:
mag_x.append(dict(zip(header, tokens)))
elif read_mag_y:
mag_y.append(dict(zip(header, tokens)))
elif read_mag_z:
mag_z.append(dict(zip(header, tokens)))
elif clean.startswith("tot"):
if ion_step_count == (len(mag_x_all) + 1):
mag_x_all.append(mag_x)
read_charge = read_mag_x = read_mag_y = read_mag_z = False
elif re.match(r"\s*(\d+)\s+(([\d\.\-]+)\s+)+", clean):
tokens = [float(token) for token in re.findall(r"[\d\.\-]+", clean)]
tokens.pop(0)
if read_charge:
charge.append(dict(zip(header, tokens)))
elif read_mag_x:
mag_x.append(dict(zip(header, tokens)))
elif read_mag_y:
mag_y.append(dict(zip(header, tokens)))
elif read_mag_z:
mag_z.append(dict(zip(header, tokens)))
elif clean.startswith("tot"):
if ion_step_count == (len(mag_x_all) + 1):
mag_x_all.append(mag_x)
read_charge = read_mag_x = read_mag_y = read_mag_z = False
if clean == "total charge":
read_charge = True
read_mag_x = read_mag_y = read_mag_z = False
Expand Down

0 comments on commit 77518b5

Please sign in to comment.