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

Test monty fix for reverse readline #4068

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ jobs:

uv pip install --editable '.[${{ matrix.config.extras }}]' --resolution=${{ matrix.config.resolution }}

# TODO: test monty fix for reverse readline
uv pip install git+https://github.com/DanielYang59/monty.git@readline-line-ending

- name: Install optional Ubuntu dependencies
if: matrix.config.os == 'ubuntu-latest'
run: |
Expand Down
2 changes: 1 addition & 1 deletion dev_scripts/potcar_scrambler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from typing import TYPE_CHECKING

import numpy as np
from monty.io import zopen
from monty.os.path import zpath
from monty.serialization import zopen

from pymatgen.core import SETTINGS
from pymatgen.io.vasp import Potcar, PotcarSingle
Expand Down
22 changes: 10 additions & 12 deletions src/pymatgen/io/adf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import re
from typing import TYPE_CHECKING

from monty.io import reverse_readline
from monty.io import reverse_readfile
from monty.itertools import chunks
from monty.json import MSONable
from monty.serialization import zopen

from pymatgen.core.structure import Molecule

Expand Down Expand Up @@ -648,16 +647,15 @@ def _parse_logfile(self, logfile):
# The last non-empty line of the logfile must match the end pattern.
# Otherwise the job has some internal failure. The TAPE13 part of the
# ADF manual has a detailed explanation.
with zopen(logfile, mode="rt") as file:
for line in reverse_readline(file):
if line == "":
continue
if end_patt.search(line) is None:
self.is_internal_crash = True
self.error = "Internal crash. TAPE13 is generated!"
self.is_failed = True
return
break
for line in reverse_readfile(logfile):
if line.strip() == "":
continue
if end_patt.search(line) is None:
self.is_internal_crash = True
self.error = "Internal crash. TAPE13 is generated!"
self.is_failed = True
return
break

with open(logfile) as file:
for line in file:
Expand Down
3 changes: 2 additions & 1 deletion tests/core/test_tensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import numpy as np
import pytest
from monty.serialization import MontyDecoder, loadfn
from monty.json import MontyDecoder
from monty.serialization import loadfn
from numpy.testing import assert_allclose
from pytest import approx

Expand Down
2 changes: 1 addition & 1 deletion tests/io/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import TYPE_CHECKING

import pytest
from monty.serialization import MontyDecoder
from monty.json import MontyDecoder

from pymatgen.core.structure import Structure
from pymatgen.io.cif import CifParser, CifWriter
Expand Down
40 changes: 39 additions & 1 deletion tests/io/vasp/test_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import numpy as np
import pytest
from monty.io import zopen
from monty.shutil import decompress_file
from monty.tempfile import ScratchDir
from numpy.testing import assert_allclose
from pytest import approx

Expand Down Expand Up @@ -799,7 +801,11 @@ def test_parse_potcar_cwd_relative(self):


class TestOutcar(PymatgenTest):
def test_init(self):
def test_init_from_compressed(self):
"""Test for both compressed and uncomressed versions,
because there was a bug in monty causing different
behaviour.
"""
outcar = Outcar(f"{VASP_OUT_DIR}/OUTCAR.gz")
expected_mag = (
{"d": 0.0, "p": 0.003, "s": 0.002, "tot": 0.005},
Expand Down Expand Up @@ -845,6 +851,38 @@ def test_init(self):
toten += outcar.final_energy_contribs[k]
assert toten == approx(outcar.final_energy, abs=1e-6)

def test_init_from_uncompressed(self):
"""Test for both compressed and uncomressed versions,
because there was a bug in monty causing different
behaviour.
"""
with ScratchDir("."):
copyfile(f"{VASP_OUT_DIR}/OUTCAR.gz", "./OUTCAR.gz")
decompress_file("./OUTCAR.gz")
outcar = Outcar("./OUTCAR")

expected_mag = (
{"d": 0.0, "p": 0.003, "s": 0.002, "tot": 0.005},
{"d": 0.798, "p": 0.008, "s": 0.007, "tot": 0.813},
{"d": 0.798, "p": 0.008, "s": 0.007, "tot": 0.813},
{"d": 0.0, "p": -0.117, "s": 0.005, "tot": -0.112},
{"d": 0.0, "p": -0.165, "s": 0.004, "tot": -0.162},
{"d": 0.0, "p": -0.117, "s": 0.005, "tot": -0.112},
{"d": 0.0, "p": -0.165, "s": 0.004, "tot": -0.162},
)
expected_chg = (
{"p": 0.154, "s": 0.078, "d": 0.0, "tot": 0.232},
{"p": 0.707, "s": 0.463, "d": 8.316, "tot": 9.486},
{"p": 0.707, "s": 0.463, "d": 8.316, "tot": 9.486},
{"p": 3.388, "s": 1.576, "d": 0.0, "tot": 4.964},
{"p": 3.365, "s": 1.582, "d": 0.0, "tot": 4.947},
{"p": 3.388, "s": 1.576, "d": 0.0, "tot": 4.964},
{"p": 3.365, "s": 1.582, "d": 0.0, "tot": 4.947},
)

assert outcar.magnetization == approx(expected_mag, abs=1e-5), "Wrong magnetization read from Outcar"
assert outcar.charge == approx(expected_chg, abs=1e-5), "Wrong charge read from Outcar"

def test_stopped_old(self):
filepath = f"{VASP_OUT_DIR}/OUTCAR.stopped.gz"
outcar = Outcar(filepath)
Expand Down