# MIT License
#
# Copyright (c) 2024-2026 Inverse Materials Design Group
#
# Author: Ihor Radchenko <yantar92@posteo.net>
#
# This file is a part of MatRiFFlow package
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""This module implements abstraction over Vasp input/output directory."""
import fnmatch
import typing
import hashlib
import os
import re
import warnings
import logging
import itertools
import pickle
import signal
import sys
from types import FrameType
import threading
import atexit
from pathlib import Path
from typing import cast, Collection, Any
from collections.abc import Callable, Mapping
import lmdb
import numpy as np
from monty.json import MSONable
from alive_progress import alive_it
from pymatgen.core.structure import Structure
from pymatgen.io.vasp.inputs import Poscar as pmgPoscar
from pymatgen.io.vasp.inputs import Kpoints as pmgKpoints
from pymatgen.io.vasp.inputs import Potcar as pmgPotcar
from pymatgen.io.vasp.outputs import Oszicar as pmgOszicar
from pymatgen.io.vasp.outputs import Chgcar as pmgChgcar
from pymatgen.io.vasp.outputs import Waveder as pmgWaveder
from pymatgen.io.vasp.outputs import Locpot as pmgLocpot
from pymatgen.io.vasp.outputs import Procar as pmgProcar
from pymatgen.io.vasp.outputs import Elfcar as pmgElfcar
from pymatgen.io.vasp.outputs import WSWQ as pmgWSWQ
from matrifflow.pymatgen.io.vasp.inputs import Incar
from matrifflow.pymatgen.io.vasp.outputs import (
RunTimingStats,
Vasprun,
Outcar,
Vasplog,
)
from matrifflow.pymatgen.io.vasp.diagnostics import (
VaspWarningRecord,
VaspWarnings,
)
from matrifflow.pymatgen.core.structure import (
FRAMEWORK_SYMPREC,
get_framework_elements,
get_framework_space_group,
)
logger = logging.getLogger(__name__)
[docs]
class TimeoutException(Exception):
"""Raised when a VASP directory read operation times out."""
[docs]
def timeout_handler(signum: int, frame: FrameType | None) -> None:
"""Signal handler that raises TimeoutException.
Args:
signum: Signal number.
frame: Current stack frame.
Raises:
TimeoutException: Always raised.
"""
_, _ = signum, frame # Suppress unused variable warning
raise TimeoutException
HAS_SIGALRM = sys.platform != 'win32'
if HAS_SIGALRM:
signal.signal(signal.SIGALRM, timeout_handler)
# Sentinel returned by ``_lmdb_get`` when a key is absent, so callers
# can distinguish "missing" from a stored ``None`` (a file whose parse
# failed is cached as ``None`` and must not be retried).
_MISSING = object()
# Rewriting the original VaspDir/PMGDir class to add caching, dumping,
# and other goodies.
[docs]
class MRFVaspDir(Mapping[str, Any], MSONable):
"""Dictionary-like access to all files in a VASP calculation directory.
Files are lazily parsed to minimise initialisation cost. Example::
d = MRFVaspDir(".")
print(d["INCAR"]["NELM"])
print(d["vasprun.xml"].parameters)
When file is absent but a compressed variant exists (``.gz``,
``.bz2``, ``.xz``, or ``.lzma``), it is resolved and parsed:
``d["vasprun.xml"]`` reads ``vasprun.xml.gz``.
``d["vasprun.xml.gz"]`` matches only that exact file.
Call ``refresh()`` to re-read the directory after files change.
Cached parsing results are stored in LMDB to speed up repeated
access in HPC workflows. Each directory caches two kinds of
records: individual parsed files (raw parse results, keyed by file
name) and a summary of memoized derived values (final_energy,
converged_* flags, warnings, and similar). The two are kept
separate because their invalidation needs differ; see the "Cache
namespaces" comment near ``_index_key``.
Properties that require parsing multiple files:
- ``final_energy``, ``final_energy_reliable``
- ``initial_structure``, ``final_structure``
- ``total_magnetization``
- ``converged``, ``converged_ionic``, ``converged_electronic``,
``converged_sequence``, ``converged_manual``
- ``nebp`` (whether directory is a NEB calculation)
- ``neb_dirs`` (list of NEB subdirectories, if any)
- ``mtime`` (latest modification time across all files)
- ``prev_dirs`` (chain of previous gorun_* runs)
"""
TIMEOUT = 60 * 2 # 2 minutes
# Version of the cache record schema. Bump when the structure of
# cached records changes so that entries written by older package
# versions are discarded on load instead of being misread.
CACHE_VERSION: typing.ClassVar[int] = 12
# LMDB map size in bytes. Exposed as a class attribute so tests can
# shrink it to exercise the eviction path without allocating the full
# default map. LMDB uses a sparse mmap, so the default does not
# consume physical disk until pages are actually written.
MAP_SIZE: typing.ClassVar[int] = 2**40 # 1TB
# Glob patterns for files to exclude from hash computation and
# directory iteration. Log files that change without affecting
# VASP results belong here. Individual instances can extend this
# set via the *exclude_patterns* init parameter.
EXCLUDE_PATTERNS: typing.ClassVar[frozenset[str]] = frozenset([
"mrf.log*",
])
# Every parser referenced by FILE_MAPPINGS must read through
# ``monty.io.zopen`` (or otherwise tolerate gzip/bz2/xz/lzma input):
# ``__getitem__`` hands them compressed files transparently. All
# pymatgen VASP parsers already satisfy this; keep the invariant when
# adding new FILE_MAPPINGS entries.
# Compression extensions recognised by ``monty.io.zopen``, which
# pymatgen's VASP parsers use internally. Used to auto-resolve a
# requested name like ``vasprun.xml`` to ``vasprun.xml.gz`` when the
# uncompressed file is absent. ``.z`` is intentionally omitted: it
# is deprecated in monty and gzip cannot decompress LZW data.
COMPRESSION_EXTENSIONS: typing.ClassVar[tuple[str, ...]] = (
'.gz', '.bz2', '.xz', '.lzma',
)
# Regex patterns matched with re.fullmatch against file names to
# select the parser class. Patterns (rather than fixed names) are
# needed for files with variable names, such as VASP log files.
# The values are typed loosely on purpose: the parsers share no
# common base class and not all of them provide ``from_file``, so
# dispatch in ``__getitem__`` is duck-typed.
FILE_MAPPINGS: typing.ClassVar[dict[str, type[typing.Any]]] = {
r'INCAR': Incar,
r'POSCAR': pmgPoscar,
r'CONTCAR': pmgPoscar,
r'KPOINTS': pmgKpoints,
r'POTCAR': pmgPotcar,
r'vasprun\.xml': Vasprun,
r'OUTCAR': Outcar,
# VASP log files (slurm output, stdout, vasp.out). OUTCAR
# serves as a log via the Outcar class when it is the only
# log file present.
r'slurm.+': Vasplog,
r'stdout.*': Vasplog,
r'vasp\.out.*': Vasplog,
# FIXME: Need to modify parent clases to make them dumpable
r'OSZICAR': pmgOszicar,
r'CHGCAR': pmgChgcar,
# "WAVECAR": pmgWavecar,
r'WAVEDER': pmgWaveder,
r'LOCPOT': pmgLocpot,
# "XDATCAR": pmgXdatcar,
# "EIGENVAL": pmgEigenval,
r'PROCAR': pmgProcar,
r'ELFCAR': pmgElfcar,
# "DYNMAT": pmgDynmat,
r'WSWQ': pmgWSWQ,
}
# Filenames that identify a directory as a VASP calculation during
# recursive scanning (see read_vaspdirs). Compressed variants are
# recognised via COMPRESSION_EXTENSIONS.
VASP_INDICATOR_FILES: typing.ClassVar[tuple[str, ...]] = (
'OUTCAR', 'vasprun.xml', 'POSCAR', 'OSZICAR',
)
def _should_exclude(self, filename: str) -> bool:
"""Return True if *filename* matches any exclude pattern.
Checks against the union of :attr:`EXCLUDE_PATTERNS` (class
default) and any extra patterns passed to the constructor.
"""
return any(
fnmatch.fnmatch(filename, pattern)
for pattern in self._exclude_patterns
)
# -- LMDB metadata ("meta") for eviction ----------------------------------
#
# LMDB uses a fixed-size mmap. When the map fills, we need to
# free space by evicting the oldest cache entries.
#
# Architecture: two named databases within the same environment:
#
# b"vaspdir_data" -- large pickled VaspDir cache entries
# b"vaspdir_meta" -- tiny {size, ctime} metadata dicts, keyed
# identically to the data DB.
#
# The split is critical for HPC filesystems (LUSTRE): scanning the
# meta DB only touches small in-leaf values with no overflow pages,
# avoiding mmap faults on the 32GB+ of data entries. Without the
# split, a full cursor scan for eviction can take minutes and block
# all LMDB access inside a write transaction.
#
# _lmdb_get_meta_all scans only the meta DB.
# _lmdb_evict uses ctime to sort oldest-first, deleting the
# corresponding key from both the data and meta DBs.
# -- Cache namespaces -----------------------------------------------------
#
# Each directory owns three kinds of cache records, addressed by the
# keys returned below:
#
# <md5(path)>:index small validity record {version, hash}.
# Loaded eagerly by refresh(). The hash is
# the combined mtime of all files, so it is
# cheap to compute and stable while the
# directory does not change.
#
# <md5(path)>:summary memoized derived values computed from one
# or more parsed files (final_energy,
# converged_* flags, warnings, run_stats,
# and similar). Loaded lazily by
# _ensure_summary_loaded().
#
# <md5(path)>:file:<name> the raw parse result of a single file
# (INCAR, POSCAR, OUTCAR, vasprun.xml, ...).
# Loaded lazily by __getitem__().
#
# The split between summary and per-file records matters because the
# two have different invalidation needs:
#
# * A parsed file depends only on that file's content, so the
# directory hash in the index is enough to decide whether a
# cached parse is still valid.
#
# * The summary depends on the code that derives the values, not
# just on file content. A change to the derivation logic (for
# example, how warnings are collected from vasprun.xml) can make
# a cached summary stale even when the files and their hash are
# unchanged.
[docs]
def reset(self):
"""Reset all loaded files and re-scan the directory.
Clears cached parsed files, previous-run references, and NEB
subdirectory references. Files matching
:attr:`EXCLUDE_PATTERNS` or extra patterns from the
constructor are skipped.
"""
path = Path(self.path)
self.files = sorted(
f for f in path.iterdir()
if f.is_file() and not self._should_exclude(f.name)
)
self._neb_vaspdirs = None
self._prev_vaspdirs = None
self._parsed_files = None
self._hash = None
self._summary = None
self._index_valid = False
_pending_writes: typing.ClassVar[dict[str, object]] = {}
_pending_lock: typing.ClassVar[threading.Lock] = threading.Lock()
_flush_registered: typing.ClassVar[bool] = False
_lmdb_env: typing.ClassVar[typing.Any] = None
_lmdb_db: typing.ClassVar[typing.Any] = None
_lmdb_meta_db: typing.ClassVar[typing.Any] = None
@classmethod
def _init_lmdb(cls) -> None:
if cls._lmdb_env is not None:
return
cache_dir = cls._get_cache_dir()
cache_dir.mkdir(parents=True, exist_ok=True)
db_path = cache_dir / "cache.lmdb"
# LMDB uses a sparse mmap so only written pages consume actual
# disk. Eviction logic in _lmdb_set_many reclaims pages when
# the map fills. The map size is configurable via MAP_SIZE.
try:
cls._lmdb_env = lmdb.open(
str(db_path),
map_size=cls.MAP_SIZE,
max_dbs=2,
lock=True,
subdir=False,
)
cls._lmdb_db = cls._lmdb_env.open_db(b"vaspdir_data")
cls._lmdb_meta_db = cls._lmdb_env.open_db(b"vaspdir_meta")
except Exception as e:
logger.warning("LMDB initialization failed; caching disabled: %s", e)
cls._lmdb_env = None
cls._lmdb_db = None
cls._lmdb_meta_db = None
@classmethod
def _lmdb_get(cls, key: str) -> object:
cls._init_lmdb()
if cls._lmdb_env is None:
return _MISSING
try:
with cls._lmdb_env.begin(db=cls._lmdb_db, write=False) as txn:
val = txn.get(key.encode())
if val is None:
return _MISSING
return pickle.loads(val)
except Exception as e:
logger.warning("LMDB get failed: %s", e)
return _MISSING
@classmethod
def _lmdb_set(cls, key: str, data: dict[str, Any]) -> bool:
"""Write a single entry with metadata tracking.
On MDB_MAP_FULL, evicts oldest entries and retries once.
"""
return cls._lmdb_set_many({key: data})
@classmethod
def _lmdb_get_meta_all(cls, txn: 'lmdb.Transaction[bytes]') -> dict[str, dict[str, Any]]:
"""Return all metadata as {key: meta_dict}.
Must be called inside an active LMDB transaction.
Scans only the metadata database (b"vaspdir_meta"), which
contains only small {size, ctime} dicts with no overflow
pages. This keeps the scan fast even on LUSTRE where the
data database may be tens of GB.
"""
result: dict[str, dict[str, Any]] = {}
cursor = txn.cursor(db=cls._lmdb_meta_db)
for raw_key, raw_val in cursor:
try:
result[raw_key.decode()] = pickle.loads(raw_val)
except Exception:
pass
return result
@classmethod
def _lmdb_evict(cls, txn: 'lmdb.Transaction[bytes]', needed_bytes: int) -> int:
"""Evict oldest entries to free at least *needed_bytes*.
Must be called inside an active write transaction.
Returns the total bytes freed.
"""
meta_all = cls._lmdb_get_meta_all(txn)
if not meta_all:
logger.warning("LMDB eviction requested but no metadata found")
return 0
# Evict oldest-first (by creation time)
sorted_keys = sorted(
meta_all.keys(),
key=lambda k: meta_all[k].get('ctime', 0),
)
freed = 0
evicted = 0
for key in sorted_keys:
if freed >= needed_bytes:
break
size = meta_all[key].get('size', 0)
txn.delete(key.encode(), db=cls._lmdb_db)
txn.delete(key.encode(), db=cls._lmdb_meta_db)
freed += size
evicted += 1
if evicted:
logger.info(
"LMDB evicted %d entries, freed %.2f MB",
evicted, freed / 1024**2,
)
return freed
@classmethod
def _lmdb_set_many(cls, items: dict[str, Any]) -> bool:
"""Write entries with metadata, evicting on MDB_MAP_FULL.
Pre-serializes values to avoid double pickling. On map-full
errors, evicts the oldest entries (by creation time) and
retries once before giving up.
Honours :attr:`TIMEOUT` via SIGALRM to avoid hanging on
slow filesystems (LUSTRE).
"""
cls._init_lmdb()
if cls._lmdb_env is None:
return False
import time
now = time.time()
# Pre-serialize so we can measure sizes and avoid double pickling.
# Each entry gets a companion metadata record with its size and
# creation time. The size is measured from the serialized bytes
# so it reflects what LMDB actually stores, not the Python object.
serialized: dict[str, bytes] = {}
meta_serialized: dict[str, bytes] = {}
for key, data in items.items():
serialized[key] = pickle.dumps(data, protocol=pickle.HIGHEST_PROTOCOL)
meta = {'size': len(serialized[key]), 'ctime': now}
meta_serialized[key] = pickle.dumps(meta, protocol=pickle.HIGHEST_PROTOCOL)
new_bytes = sum(len(v) + len(meta_serialized[k]) for k, v in serialized.items())
for attempt in range(2):
try:
if HAS_SIGALRM:
signal.alarm(cls.TIMEOUT)
try:
with cls._lmdb_env.begin(db=cls._lmdb_db, write=True) as txn:
for key in items:
txn.put(key.encode(), serialized[key], db=cls._lmdb_db)
txn.put(key.encode(), meta_serialized[key], db=cls._lmdb_meta_db)
finally:
if HAS_SIGALRM:
signal.alarm(0)
return True
except TimeoutException:
logger.warning(
"LMDB write timed out after %ds", cls.TIMEOUT,
)
return False
except Exception as e:
if 'MDB_MAP_FULL' not in str(e):
logger.warning("LMDB set_many failed: %s", e)
return False
if attempt == 0:
logger.warning(
"LMDB map full, evicting before retry "
"(need ~%.1f MB)", new_bytes / 1024**2,
)
try:
if HAS_SIGALRM:
signal.alarm(cls.TIMEOUT)
try:
with cls._lmdb_env.begin(
db=cls._lmdb_db, write=True,
) as txn:
# Free 2x what we need as headroom
cls._lmdb_evict(txn, new_bytes * 2)
finally:
if HAS_SIGALRM:
signal.alarm(0)
except TimeoutException:
logger.warning(
"LMDB eviction timed out after %ds", cls.TIMEOUT,
)
return False
except Exception as evict_e:
logger.error("LMDB eviction failed: %s", evict_e)
return False
else:
logger.error(
"LMDB set_many failed after eviction: %s", e,
)
return False
return False
[docs]
def __init__(
self,
dirname: str | Path,
exclude_patterns: typing.Iterable[str] | None = None,
) -> None:
"""Initialise from a directory path.
Args:
dirname: Path to the VASP calculation directory.
exclude_patterns: Extra glob patterns for files to skip
during directory scanning. These are added to the
class-level :attr:`EXCLUDE_PATTERNS`. For example,
``["*.log"]`` excludes ``mrf.log`` (already in the
class default) as well as any other ``*.log`` file.
"""
self.path = str(Path(dirname).absolute())
# This was slower: self.path = str(Path(dirname).resolve())
self._cache_key = hashlib.md5(self.path.encode('utf-8')).hexdigest()
self._parsed_files: dict[str, object] | None = None # reset() redoes it.
self._prev_vaspdirs: list['MRFVaspDir'] | None = None # reset() redoes it.
self._neb_vaspdirs: list['MRFVaspDir'] | None = None # reset() redoes it.
self._hash: str | None = None # reset() redoes it.
self._summary: dict[str | tuple[Any, ...], Any] | None = None # reset() redoes it.
self._index_valid: bool = False # reset() redoes it.
if exclude_patterns is None:
self._exclude_patterns = self.EXCLUDE_PATTERNS
else:
self._exclude_patterns = self.EXCLUDE_PATTERNS | frozenset(
exclude_patterns
)
self.reset()
# Implementation note: We cannot use SQLite to store cache
# because SQLite is not reliable on LUSTRE file system
# in HPC. LMDB is used instead.
@staticmethod
def _get_cache_dir() -> Path:
cache_dir = os.getenv("XDG_CACHE_HOME")\
or os.path.expanduser("~/.cache")
# The cache directory was renamed imdgVASPDIRcache -> mrfVASPDIRcache
# during the MatRiFFlow rename. Prefer the legacy directory when it
# already exists so previously parsed results are not discarded;
# otherwise use the new name for fresh installations.
legacy_dir = Path(cache_dir) / "imdgVASPDIRcache"
if legacy_dir.is_dir():
return legacy_dir
return Path(cache_dir) / "mrfVASPDIRcache"
def _index_key(self) -> str:
"""Cache key for the per-directory validity metadata."""
return f"{self._cache_key}:index"
def _summary_key(self) -> str:
"""Cache key for the per-directory summary (memoized values)."""
return f"{self._cache_key}:summary"
def _file_key(self, item: str) -> str:
"""Cache key for a single parsed file within this directory."""
return f"{self._cache_key}:file:{item}"
def _load_from_cache(self) -> dict[str, object] | None:
"""Load the per-directory validity metadata from LMDB.
Returns:
Cached metadata dict on success, ``None`` if no entry
exists or the LMDB environment is unavailable.
"""
data = self._lmdb_get(self._index_key())
if data is _MISSING or not isinstance(data, dict):
return None
# The per-directory index is written with string keys.
return cast(dict[str, object], data)
_PENDING_FLUSH_THRESHOLD: typing.ClassVar[int] = 200
@classmethod
def _add_pending_write(cls, key: str, data: object) -> None:
"""Add a cache entry to the pending write buffer.
Automatically flushes to LMDB when the number of pending
entries reaches :attr:`_PENDING_FLUSH_THRESHOLD`.
"""
with cls._pending_lock:
cls._pending_writes[key] = data
if not cls._flush_registered:
atexit.register(cls.flush_cache)
cls._flush_registered = True
should_flush = len(cls._pending_writes) >= cls._PENDING_FLUSH_THRESHOLD
# Flush outside the lock to avoid holding it during I/O
if should_flush:
cls.flush_cache()
[docs]
@classmethod
def flush_cache(cls) -> None:
"""Write all pending cache entries to LMDB.
On write failure (including timeout), pending entries are
restored so they can be retried on the next flush.
"""
with cls._pending_lock:
if not cls._pending_writes:
return
writes = cls._pending_writes.copy()
cls._pending_writes.clear()
# Write outside lock to avoid holding lock during I/O
if cls._lmdb_set_many(writes):
logger.debug("Flushed %d cache entries to LMDB", len(writes))
else:
logger.warning(
"LMDB flush failed, restoring %d pending entries",
len(writes),
)
with cls._pending_lock:
cls._pending_writes.update(writes)
def _dump_index(self) -> bool:
"""Dump the per-directory validity metadata (version + hash)."""
self._add_pending_write(self._index_key(), {
'version': self.CACHE_VERSION,
'hash': self._get_hash(),
})
return True
def _dump_summary(self) -> bool:
"""Dump the per-directory summary (memoized derived values)."""
summary = self._ensure_summary_loaded()
self._dump_index()
self._add_pending_write(self._summary_key(), summary)
return True
def _dump_file_to_cache(self, item: str) -> bool:
"""Dump a single parsed file to cache.
A failed parse is stored as ``None``; ``_lmdb_get`` returns the
``_MISSING`` sentinel for an absent key, so the two are
distinguishable on load.
"""
self._dump_index()
assert self._parsed_files is not None
self._add_pending_write(self._file_key(item), self._parsed_files[item])
return True
[docs]
def refresh(self):
"""Reload cache validity metadata or reset to an empty state.
Loads only the tiny per-directory index (version + hash). The
summary (memoized derived values) is loaded lazily on first
access, and parsed files are fetched lazily by
:meth:`__getitem__`.
"""
# Files may have changed since the hash was last computed, so
# invalidate the memoized hash before recomputing it.
self._hash = None
key = self._index_key()
current_hash = self._get_hash()
# First check pending writes (in-process, not yet flushed to LMDB)
with self._pending_lock:
pending = self._pending_writes.get(key)
if pending is not None:
assert isinstance(pending, dict)
if (pending.get('version') == self.CACHE_VERSION
and pending.get('hash') == current_hash):
self._parsed_files = {}
self._index_valid = True
self._summary = None
logger.debug(
"Loaded pending cache for %s",
os.path.relpath(self.path))
return
# Version or hash mismatch: stale pending entry, remove it.
logger.debug(
"Pending cache mismatch for %s "
"(version=%s, hash=%s; expected version=%s, hash=%s)",
os.path.relpath(self.path),
pending.get('version'), pending.get('hash'),
self.CACHE_VERSION, current_hash)
with self._pending_lock:
self._pending_writes.pop(key, None)
# Fall back to disk cache (LMDB)
cache_data = self._load_from_cache()
if cache_data is not None:
if cache_data.get('version') == self.CACHE_VERSION:
cached_hash = cache_data.get('hash')
if cached_hash == current_hash:
self._parsed_files = {}
self._index_valid = True
self._summary = None
logger.debug(
"Loaded disk cache for %s",
os.path.relpath(self.path))
return
logger.debug(
"Disk cache hash mismatch for %s (cached=%s, current=%s)",
os.path.relpath(self.path),
cached_hash, current_hash)
else:
logger.debug(
"Disk cache version mismatch for %s "
"(cached=%s, current=%s); discarding",
os.path.relpath(self.path),
cache_data.get('version'), self.CACHE_VERSION)
else:
logger.debug(
"No disk cache entry for %s",
os.path.relpath(self.path))
self._parsed_files = {}
self._summary = self._fresh_summary()
self._index_valid = False
logger.debug("No valid cache for %s", os.path.relpath(self.path))
def _fresh_summary(self) -> dict[str | tuple[Any, ...], Any]:
"""Return an empty summary (memo) dict."""
return {
'warnings': VaspWarnings(),
'warnings_collected': False,
}
def _ensure_summary_loaded(self) -> dict[str | tuple[Any, ...], Any]:
"""Load the summary (memo) from cache, or reset it to empty.
Triggers :meth:`refresh` first so the validity of the cached
summary is known before it is used. ``self._summary is None``
is the "not yet loaded" sentinel; the returned value is always
a dict.
"""
if self._parsed_files is None:
self.refresh()
if self._summary is not None:
return self._summary
if self._index_valid:
data = self._lmdb_get(self._summary_key())
if data is not _MISSING and isinstance(data, dict):
self._summary = data
else:
self._summary = self._fresh_summary()
else:
self._summary = self._fresh_summary()
return self._summary
def _memoized(
self,
key: str | tuple[Any, ...],
compute: Callable[[], Any]) -> Any:
"""Return ``self._summary[key]``, computing and caching on a miss."""
summary = self._ensure_summary_loaded()
if key in summary:
return summary[key]
value = compute()
summary[key] = value
self._dump_summary()
return value
[docs]
@staticmethod
def read_vaspdirs(
rootpath: Path | str | list[Path | str],
path_filter: Callable[[Path], bool] | None = None
) -> dict[str, 'MRFVaspDir']:
"""Recursively scan directories for VASP calculations.
Args:
rootpath: Root directory or list of directories to scan.
path_filter: Optional callable that returns True for paths
to include.
Returns:
dict[str, MRFVaspDir]: Mapping of ``{path: MRFVaspDir}``.
"""
if isinstance(rootpath, list):
paths = [Path(p) for p in rootpath]
else:
paths = [Path(rootpath)]
valid_paths: dict[str, MRFVaspDir] = {}
walk_it = cast(
Collection[tuple[Path, list[str], list[str]]],
itertools.chain.from_iterable(p.walk() for p in paths),
)
for parent, _, files in alive_it(
walk_it,
title=f"Scanning {list(map(str, paths))} for VASP directories",
refresh_secs=1,
):
if path_filter is not None and not path_filter(parent):
continue
if any(
MRFVaspDir._name_in_files(vaspfile, files)
for vaspfile in MRFVaspDir.VASP_INDICATOR_FILES
):
valid_paths[str(parent)] = MRFVaspDir(parent)
MRFVaspDir.flush_cache()
return valid_paths
def __fspath__(self) -> str:
return self.path
@classmethod
def _strip_compression_ext(cls, filename: str) -> str:
"""Return *filename* with a known compression suffix removed."""
for ext in cls.COMPRESSION_EXTENSIONS:
if filename.endswith(ext):
return filename[:-len(ext)]
return filename
def _resolve_filename(self, item: str) -> str | None:
"""Return the on-disk filename for *item*, or None.
Prefers the exact name; otherwise falls back to the same name
with a compression extension appended (``vasprun.xml.gz`` and
friends). Resolution checks the live filesystem, so a file
created after construction is still found.
An explicitly compressed request only matches that exact file:
``d["vasprun.xml.gz"]`` never resolves to ``vasprun.xml`` or a
double-compressed variant.
"""
path = Path(self.path)
if (path / item).exists():
return item
if self._strip_compression_ext(item) != item:
return None
for ext in self.COMPRESSION_EXTENSIONS:
candidate = item + ext
if (path / candidate).exists():
return candidate
return None
@classmethod
def _name_in_files(cls, name: str, files: Collection[str]) -> bool:
"""Return True if *name* or a compressed variant is in *files*.
Used by ``read_vaspdirs`` to recognise VASP directories whose
indicator files exist only in compressed form.
"""
if name in files:
return True
return any(name + ext in files for ext in cls.COMPRESSION_EXTENSIONS)
def __contains__(self, item: object) -> bool:
if isinstance(item, str) or isinstance(item, Path):
return self._resolve_filename(str(item)) is not None
return False
def __len__(self):
return len(self.files)
def __iter__(self):
return iter(f.name for f in self.files)
def __getitem__(self, item: str) -> typing.Any:
if self._parsed_files is None:
self.refresh()
assert self._parsed_files is not None
if item in self._parsed_files:
return self._parsed_files[item]
path = Path(self.path)
filename = self._resolve_filename(item)
if filename is None:
return None
# Try the per-file cache before parsing from disk. Only valid
# when the directory hash matched on refresh(); otherwise the
# entry may be stale.
if self._index_valid:
cached = self._lmdb_get(self._file_key(item))
if cached is not _MISSING:
self._parsed_files[item] = cached
return cached
base_item = self._strip_compression_ext(item)
for pattern, cls_ in self.FILE_MAPPINGS.items():
if re.fullmatch(pattern, base_item):
# Avoid being stuck in IO
# It can happen with Outcar
# See https://github.com/materialsproject/pymatgen/issues/4550
# or just because of IO issues on cluster
# avoid being stuck and simply signal failure then.
if HAS_SIGALRM:
signal.alarm(self.TIMEOUT)
try:
# Standard parsing for all files
try:
obj = cls_.from_file(path / filename)
except AttributeError:
obj = cls_(path / filename)
if HAS_SIGALRM:
signal.alarm(0)
self._parsed_files[item] = obj
self._dump_file_to_cache(item)
return obj
# except TimeoutException:
except Exception as e:
if HAS_SIGALRM:
signal.alarm(0)
logger.debug("Failed to read %s: %s", path / filename, e)
self._parsed_files[item] = None
self._dump_file_to_cache(item)
return None
raise RuntimeError(
f"Unable to parse {item}. "
f"Supported file patterns are {list(self.FILE_MAPPINGS.keys())}.")
[docs]
def logs(self) -> list[Vasplog | Outcar]:
"""Parse VASP log files in this directory through the file cache.
Discovers log files via :meth:`Vasplog.vasp_log_files` and
parses each through :meth:`__getitem__`, so parsed warnings
and progress are cached alongside the other files. Returns
``Vasplog`` instances for slurm/stdout/vasp.out logs and an
``Outcar`` when OUTCAR is the only log file.
Returns:
list: Parsed log objects, skipping files that fail to parse.
"""
files = Vasplog.vasp_log_files(self.path)
if files is None:
return []
logs: list[Vasplog | Outcar] = []
for f in files:
try:
log = self[f.name]
except RuntimeError:
logger.debug("No parser for log file %s", f.name)
continue
if log is not None:
logs.append(log)
return logs
def _record(self, record: VaspWarningRecord) -> None:
"""Record a directory-level warning and emit it.
Records are overwritten by name, so re-running a check is
idempotent. ``overwrite`` emits through ``warnings.warn``.
"""
summary = self._ensure_summary_loaded()
container = summary['warnings']
assert isinstance(container, VaspWarnings)
container.overwrite(record)
@property
def warnings(self) -> VaspWarnings:
"""Structured warnings for this directory.
Aggregates log-file warnings (:class:`Vasplog`/:class:`Outcar`),
:class:`Vasprun` accuracy checks, and directory-level checks
(energy reliability, displacements, framework symmetry). The
container is memoized and replayed on subsequent loads.
Returns:
VaspWarnings: Name-keyed warning records.
"""
summary = self._ensure_summary_loaded()
container = summary['warnings']
assert isinstance(container, VaspWarnings)
if not summary['warnings_collected']:
for log in self.logs():
for record in log.warnings.values():
container.add(record, emit=False)
if run := self['vasprun.xml']:
for record in run.warnings.values():
container.add(record, emit=False)
# Records energy reliability and, transitively, runs the
# displacement/framework-symmetry checks via converged_*.
self.final_energy
summary['warnings_collected'] = True
self._dump_summary()
return container
@property
def run_stats(self) -> dict[str, typing.Any] | None:
"""Cached ``Outcar.run_stats``, or None without OUTCAR."""
return self._memoized('run_stats', self._compute_run_stats)
def _compute_run_stats(self) -> dict[str, typing.Any] | None:
outcar = self['OUTCAR']
if outcar is None:
return None
return outcar.run_stats
@property
def timing_stats(self) -> RunTimingStats | None:
"""Cached :attr:`Outcar.timing_stats`, or None without OUTCAR."""
return self._memoized('timing_stats', self._compute_timing_stats)
def _compute_timing_stats(self) -> RunTimingStats | None:
outcar = self['OUTCAR']
if outcar is None:
return None
return outcar.timing_stats
@staticmethod
def _get_file_hash(filename: Path | str) -> str:
"""Get hash of FILENAME.
The hash is simply modification time."""
f = Path(filename)
return str(f.stat().st_mtime if f.exists() else "") + f.name
def _get_hash(self) -> str:
"""Get hash of all files in path.
Hashes are deterministic because ``self.files`` is sorted
in :meth:`reset`. The result is memoized per instance and
invalidated in :meth:`reset`.
"""
if self._hash is not None:
return self._hash
hashes = map(self._get_file_hash, self.files)
combined_hash =\
hashlib.md5("".join(hashes).encode('utf-8')).hexdigest()
self._hash = str(combined_hash)
return self._hash
@property
def final_energy(self) -> float:
"""Latest recorded E0 from OSZICAR, falling back to vasprun.xml/OUTCAR.
OSZICAR steps without E0 are skipped, including empty entries
in incomplete runs. Returns NaN when no energy is available.
"""
return self._memoized('final_energy', self._compute_final_energy)
def _compute_final_energy(self) -> float:
if not self.converged:
self._record(VaspWarningRecord(
name="unconverged_energy",
message="Reading final energy from unconverged run",
source=self.path,
))
if oszicar := self['OSZICAR']:
# Incomplete runs can leave empty ionic-step entries.
# FIXME: report to pymatgen upstream for
# Oszicar.final_energy
for step in reversed(oszicar.ionic_steps):
if 'E0' in step:
return step['E0']
if run := self['vasprun.xml']:
return run.final_energy
if outcar := self['OUTCAR']:
final_energy = outcar.final_energy
if not isinstance(final_energy, float):
self._record(VaspWarningRecord(
name="unparseable_energy",
message=(
f"Problems reading final energy (={final_energy})"
f" from OUTCAR."
),
source=self.path,
))
final_energy = np.nan
return final_energy
return np.nan
@property
def final_energy_reliable(self) -> str | float:
"""Like :attr:`final_energy`, but with a reliability check.
Returns:
float: The final energy when judged reliable.
str: ``"unreliable"`` when energy may be inaccurate (e.g.
volume relaxation).
str: ``"unconverged"`` when the run has not converged.
"""
if not self.converged:
return "unconverged"
incar = self['INCAR']
assert incar is not None
run = self['vasprun.xml']
assert run is not None
n_steps = len(run.ionic_steps)
if incar.get('IBRION') in Incar.IBRION_IONIC_RELAX_values and\
incar.get('ISIF') not in [
Incar.ISIF_FIX_SHAPE_VOL, Incar.ISIF_FIX_SHAPE_VOL_FAST,
Incar.ISIF_FIX_SHAPE_VOL_TRACE] and incar.get('NSW', 0) > 0 and n_steps > 1: # NSW = 0 is SCF
return "unreliable"
return self.final_energy
@property
def initial_structure(self) -> Structure | None:
"""Initial structure of the calculation.
The initial state is the earliest structure in the calculation
chain. When ``gorun_*`` subdirectories (``prev_dirs``) exist,
their initial structure is returned, because the current
directory is not the true starting geometry. Otherwise the
current directory's POSCAR, then the vasprun.xml initial
structure, is used.
Returns ``None`` when no structure is available.
"""
if prevs := self.prev_dirs():
return prevs[0].initial_structure
if poscar := self['POSCAR']:
return poscar.structure
if run := self['vasprun.xml']:
return run.initial_structure
return None
@property
def final_structure(self) -> Structure | None:
"""Final structure of the calculation in the current directory.
Unlike :attr:`initial_structure`, this never consults
``prev_dirs``: the final geometry always belongs to the current
directory. Reads CONTCAR when present, otherwise the final
structure from vasprun.xml.
Returns ``None`` when neither CONTCAR nor vasprun.xml is
available.
"""
if contcar := self['CONTCAR']:
return contcar.structure
if run := self['vasprun.xml']:
return run.final_structure
return None
@property
def structure(self) -> Structure | None:
"""Deprecated alias for :attr:`final_structure`.
Use :attr:`final_structure` instead.
"""
warnings.warn(
"MRFVaspDir.structure is deprecated; use final_structure.",
DeprecationWarning,
stacklevel=2,
)
return self.final_structure
@property
def total_magnetization(self) -> float | None:
"""Total magnetization from OSZICAR, or None if unavailable."""
if oszicar := self['OSZICAR']:
return oszicar.ionic_steps[-1].get('mag', None)\
if len(oszicar.ionic_steps) > 0 else None
return None
[docs]
def max_force(self, include_constrained: bool = False) -> float | None:
"""Maximum residual force magnitude from vasprun.xml or OUTCAR.
When ``include_constrained`` is False (default), force
components in directions constrained by selective dynamics are
ignored, so fixed atoms do not dominate the reported force.
Constraints are read from the POSCAR, with CONTCAR as a
fallback.
Args:
include_constrained: When True, include all force
components regardless of selective dynamics.
Returns:
float | None: Maximum force in eV/Angstrom, or ``None``
when the forces are unavailable or no unconstrained force
components remain.
"""
return self._memoized(
('max_force', include_constrained),
lambda: self._compute_max_force(include_constrained))
def _compute_max_force(self, include_constrained: bool) -> float | None:
forces = self._vasprun_final_forces()
if forces is None:
outcar = self['OUTCAR']
forces = outcar.final_forces if outcar is not None else None
return self._max_force_from_forces(forces, include_constrained)
def _vasprun_final_forces(self) -> np.ndarray | None:
"""Final ionic forces from vasprun.xml, or None if unavailable."""
run = self['vasprun.xml']
if run is None:
return None
steps = getattr(run, 'ionic_steps', None)
if not steps:
return None
forces = steps[-1].get('forces')
if forces is None:
return None
return np.asarray(forces, dtype=float)
def _max_force_from_forces(
self, forces: np.ndarray | None, include_constrained: bool,
) -> float | None:
"""Maximum force magnitude from *forces*, honouring constraints."""
if forces is None or len(forces) == 0:
return None
if include_constrained:
return float(np.max(np.linalg.norm(forces, axis=1)))
selective_dynamics = self._selective_dynamics()
if selective_dynamics is None:
return float(np.max(np.linalg.norm(forces, axis=1)))
magnitudes = []
for i, force in enumerate(forces):
mask = selective_dynamics[i] if i < len(selective_dynamics)\
else np.ones(3, dtype=bool)
if not np.any(mask):
continue
magnitudes.append(np.linalg.norm(force[mask]))
if not magnitudes:
return None
return float(np.max(magnitudes))
def _selective_dynamics(self) -> np.ndarray | None:
"""Selective dynamics flags for the calculation.
Read from the POSCAR, falling back to the CONTCAR when the
POSCAR is absent.
Returns:
np.ndarray | None: Boolean array of shape ``(n_atoms, 3)``
where each row marks whether that Cartesian direction is
free to move. ``None`` when neither file is available or
neither has selective dynamics constraints.
"""
for fname in ("POSCAR", "CONTCAR"):
poscar = self[fname]
if poscar is None:
continue
try:
selective = poscar.structure.site_properties.get(
'selective_dynamics')
except (OSError, ValueError, IndexError):
continue
if selective is not None:
return np.asarray(selective, dtype=bool)
return None
[docs]
def check_displacements(self) -> bool:
"""Check whether atomic displacements are below a safe threshold.
Warns and returns False when the maximum displacement exceeds
twice the average bond length.
Returns:
bool: True if displacements are acceptable.
"""
assert self.initial_structure is not None
assert self.final_structure is not None
max_displacement = 0
for i, site in enumerate(self.initial_structure):
displacement = site.distance(self.final_structure[i])
max_displacement = max(max_displacement, displacement)
vol = self.final_structure.volume
avg_bond_length = (vol / len(self.final_structure))**(1 / 3)
if max_displacement > 2.0 * avg_bond_length:
self._record(VaspWarningRecord(
name="large_displacement",
message=(
f"Large atomic displacement {max_displacement}"
),
source=self.path,
metadata={"max_displacement": float(max_displacement)},
))
return False
return True
[docs]
def check_framework_symmetry(
self,
framework_elements: Collection[str] | None = None,
symprec: float = FRAMEWORK_SYMPREC) -> bool:
"""Check whether the framework symmetry is preserved.
Reduces false positives from mobile atoms breaking symmetry.
Args:
framework_elements: List of element symbols for the
framework. Defaults to the most common element.
symprec: Symmetry tolerance for space group detection.
Returns:
bool: True if framework symmetry is preserved.
"""
assert self.initial_structure is not None
assert self.final_structure is not None
if framework_elements is None:
framework_elements = get_framework_elements(self.initial_structure)
# The result depends only on the framework element set and the
# symmetry tolerance. The summary is persisted across runs, so
# a warm load skips the spglib calls below.
key = ('symmetry', tuple(framework_elements), symprec)
return self._memoized(
key,
lambda: self._compute_framework_symmetry(
framework_elements, symprec))
def _compute_framework_symmetry(
self, framework_elements: Collection[str],
symprec: float) -> bool:
assert self.initial_structure is not None
assert self.final_structure is not None
init_sg = get_framework_space_group(
self.initial_structure, framework_elements, symprec)
final_sg = get_framework_space_group(
self.final_structure, framework_elements, symprec)
if init_sg == final_sg:
return True
self._record(VaspWarningRecord(
name="framework_symmetry",
message=(
f"Framework symmetry changed ({init_sg} to {final_sg})"
),
source=self.path,
))
return False
@property
def converged_ionic(self) -> bool:
"""Whether ionic convergence was reached.
Also checks framework symmetry and displacements.
"""
return self._memoized(
'converged_ionic', self._compute_converged_ionic)
def _compute_converged_ionic(self) -> bool:
converged_ionic = False
if run := self['vasprun.xml']:
converged_ionic = run.converged_ionic
elif 'vasprun.xml' in self:
# vasprun.xml present but cannot be parsed
converged_ionic = False
elif outcar := self['OUTCAR']:
if 'converged_ionic' not in outcar.data:
outcar.read_pattern(
{'converged_ionic':
r'(reached required accuracy - stopping structural energy minimisation|writing wavefunctions)'},
reverse=True,
terminate_on_match=True
)
converged_ionic = bool(outcar.data['converged_ionic'])
if converged_ionic:
self.check_framework_symmetry()
self.check_displacements()
return converged_ionic
@property
def converged_electronic(self) -> bool:
"""Whether electronic convergence was reached."""
return self._memoized(
'converged_electronic', self._compute_converged_electronic)
def _compute_converged_electronic(self) -> bool:
if run := self['vasprun.xml']:
return run.converged_electronic
if 'vasprun.xml' in self:
# vasprun.xml present but cannot be parsed
return False
if outcar := self['OUTCAR']:
if 'converged_electronic' not in outcar.data:
outcar.read_pattern(
{'converged_electronic':
r'aborting loop (EDIFF was not reached \(unconverged\)|because EDIFF is reached)'},
reverse=True,
terminate_on_match=True
)
converged_electronic = outcar.data['converged_electronic']
if converged_electronic:
return converged_electronic[0][0] == 'because EDIFF is reached'
return False
return False
@property
def converged_sequence(self) -> bool:
"""Whether the multi-step convergence sequence is complete.
Returns False when ``INCAR.[0-9]+`` files remain (signalling
that further convergence steps are pending).
"""
return self._memoized(
'converged_sequence', self._compute_converged_sequence)
def _compute_converged_sequence(self) -> bool:
return not any(re.match(r'INCAR\.[0-9]+', file) for file in self)
@property
def converged_manual(self) -> bool:
"""Whether the directory is explicitly marked as converged.
Returns False when an ``UNCONVERGED`` file is present.
"""
return self._memoized(
'converged_manual', self._compute_converged_manual)
def _compute_converged_manual(self) -> bool:
return not (Path(self.path) / "UNCONVERGED").is_file()
@property
def converged(self) -> bool:
"""Overall convergence status.
Returns True only when the run is electronically and ionically
converged, the convergence sequence is complete, and no
``UNCONVERGED`` marker file is present. For NEB runs, all
images must be converged.
"""
if not self.converged_manual:
return False
if self.nebp:
neb_dirs = self.neb_dirs(include_ends=False)
assert neb_dirs is not None
for image_dir in neb_dirs:
if not image_dir.converged:
return False
return True
return self.converged_electronic and self.converged_ionic\
and self.converged_sequence
@property
def nebp(self) -> bool:
"""Whether this directory contains a NEB-like calculation.
Detected by the presence of ``IMAGES`` in the INCAR.
"""
return self._memoized('nebp', self._compute_nebp)
def _compute_nebp(self) -> bool:
if incar := self['INCAR']:
return 'IMAGES' in incar
return False
[docs]
def neb_dirs(self, include_ends: bool = True) -> list['MRFVaspDir'] | None:
"""List of NEB image subdirectories.
Args:
include_ends: When False, exclude the first and last images.
Returns:
list[MRFVaspDir] | None: NEB subdirectories, or None if
this is not a NEB run.
"""
if self.nebp:
if self._neb_vaspdirs is None:
incar = self['INCAR']
assert incar is not None
dir_names = incar.image_dir_names()
self._neb_vaspdirs = [
MRFVaspDir(Path(self.path) / d) for d in dir_names
]
return self._neb_vaspdirs if include_ends\
else self._neb_vaspdirs[1:-1]
return None
[docs]
def output_mtime(self) -> float | None:
"""Modification time of the newest VASP output file, or None.
Reports the OUTCAR mtime when present, otherwise the
vasprun.xml mtime; both resolve through compression variants.
For NEB directories, returns the maximum across image
directories. Returns ``None`` when no VASP output file exists,
distinguishing "no output yet" from a zero/old timestamp.
"""
if self.nebp:
neb_dirs = self.neb_dirs()
assert neb_dirs is not None
times = [d.output_mtime() for d in neb_dirs]
times = [t for t in times if t is not None]
return None if not times else max(times)
return self._output_file_mtime()
def _output_file_mtime(self) -> float | None:
"""mtime of OUTCAR or vasprun.xml for a non-NEB directory.
ATAT and cleanup move OUTCAR away but leave vasprun.xml, so
vasprun.xml is the fallback. Returns ``None`` when neither
output file exists.
"""
path = Path(self.path)
for name in ("OUTCAR", "vasprun.xml"):
filename = self._resolve_filename(name)
if filename is not None:
return (path / filename).stat().st_mtime
return None
def _mtime_1(self) -> float:
"""Return modification time of self.
Ignore previous runs. Use NEB dirs if any.
"""
if self.nebp:
neb_dirs = self.neb_dirs()
assert neb_dirs is not None
times = [d.mtime() for d in neb_dirs]
return max(times)
mtime = self._output_file_mtime()
if mtime is not None:
return mtime
path = Path(self.path)
incar = path / "INCAR"
if incar.is_file():
return incar.stat().st_mtime
return path.stat().st_mtime
[docs]
def mtime(self) -> float:
"""Latest modification time across all relevant files.
Considers NEB subdirectories and previous-run directories.
"""
mtimes = [self._mtime_1()]
prev_dirs = self.prev_dirs()
if prev_dirs:
for prev in prev_dirs:
mtimes.append(prev.mtime())
return max(mtimes)
[docs]
def prev_dirs(self) -> list['MRFVaspDir']:
"""List of previous VASP runs in the chain.
Previous runs are assumed to reside in ``gorun_*``
subdirectories containing a ``POSCAR``.
Returns:
list[MRFVaspDir]: Sorted list of previous-run
directories. Empty when there are none.
"""
if self._prev_vaspdirs is None:
path = Path(self.path)
self._prev_vaspdirs = sorted(
[MRFVaspDir(d) for d in path.glob('gorun_*/') if d.is_dir() and (d / 'POSCAR').is_file()],
key=lambda d: d.path)
if len(self._prev_vaspdirs) > 0:
logger.info(
"%s contains previous Vasp runs",
os.path.relpath(self.path)
)
return self._prev_vaspdirs
# ---- Backward-compatibility aliases (MatRiFFlow rename) ----
from matrifflow.common import deprecated_aliases # noqa: E402
__getattr__ = deprecated_aliases(globals(), IMDGVaspDir="MRFVaspDir")