Source code for matrifflow.pymatgen.cli.mrf_status

# 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.


"""Check status of running VASP calculations."""
import argparse
import datetime
import logging
import os
import re
import shutil
import subprocess
import sys
import time
import warnings
from argparse import Namespace
from pathlib import Path
from typing import TextIO
from collections import deque
from collections.abc import Iterable
from dataclasses import dataclass

import cachetools.func
import numpy as np
from pymatgen.core import Structure
from termcolor import colored
from rich.console import Console, Group
from rich.live import Live
from rich.table import Table
from rich.text import Text
from pymatgen.io.vasp.outputs import UnconvergedVASPWarning
from matrifflow.pymatgen.io.vasp.diagnostics import VaspWarning, VaspWarnings
from matrifflow.pymatgen.core.structure import structure_distance
from matrifflow.pymatgen.io.vasp.outputs import (
    Outcar,
    RunTimingStats,
    MeanStd,
    Vasplog,
    read_outcar_timing_stats,
)
from matrifflow.pymatgen.io.vasp.vaspdir import MRFVaspDir
from matrifflow.pymatgen.io.vasp.inputs import Incar
from matrifflow.pymatgen.cli.mrf_diff import print_incar_groups

logger = logging.getLogger(__name__)
console = Console()
# rich's ``Console.is_terminal`` is not memoized: it calls ``isatty()``
# on every access (render, options, size detection), and ``mrf status``
# hits it ~800 times per run.  On slow terminals each ``isatty()`` can
# cost several milliseconds, so freeze the result once after the
# env-aware first detection (FORCE_COLOR, TTY_COMPATIBLE, jupyter, idle)
# has run.  ``_force_terminal`` is the backing field of the
# ``force_terminal=`` constructor argument and is checked first by the
# property, so every later access short-circuits without a syscall.
console._force_terminal = console.is_terminal


[docs] def custom_showwarning( message: Warning | str, category: type[Warning], _filename: str, _lineno: int, file: TextIO | None = None, _line: str | None = None) -> None: """Print warning in nicer way. Args: message: Warning message. category: Warning category class. _filename: File where the warning originated (unused). _lineno: Line number (unused). file: Output stream (default: stderr). _line: Line context (unused). """ del _filename, _lineno, _line output = colored( f"{category.__name__}: ", "yellow", attrs=['bold']) + f'{message}' print(output, file=file or sys.stderr)
warnings.showwarning = custom_showwarning @cachetools.func.ttl_cache(maxsize=None, ttl=10) def _slurm_get_queue() -> list[bytes]: """Get the current Slurm queue as a list of byte strings. The returned list is from the output of squeue -O %Z command. """ if shutil.which("squeue") is None: return [] result = subprocess.check_output( "squeue -o %Z | tail -n +2", shell=True).split() return result
[docs] def slurm_runningp(path: str | Path) -> bool: """Check whether a Slurm job is running in the given directory. Args: path: Directory path to check. Returns: bool: True if a Slurm job is running in ``path`` or a parent NEB directory. """ result = _slurm_get_queue() if len(result) > 0 and os.path.abspath(path) in [s.decode('utf-8') for s in result]: return True # For NEB and similar calculations, vasp might be running in parent dir. # Then, current directory must be named as a number and parent # INCAR should have IMAGES tag. # See https://www.vasp.at/wiki/index.php/IMAGES if re.match(r'[0-9]+', os.path.basename(path)): parent = os.path.dirname(path) if MRFVaspDir(parent).nebp: return slurm_runningp(parent) return False
[docs] def add_args(parser: argparse.ArgumentParser) -> None: """Register subcommand arguments. Args: parser: Sub-parser from argparse. """ parser.add_argument( "dir", help="""Directories to read (recusrively). Defaults to current dir.""", type=str, nargs="*", default=["."]) parser.add_argument( "--exclude", help="Dirs matching this Python regexp pattern will be excluded", type=str, ) parser.add_argument( "--include", help="*Only* dirs matching this Python regexp pattern" " will be included", type=str, ) parser.add_argument( "--nowarn", help="Warning names to ignore (names are printed in the " "'Warnings' summary)", nargs="+", default=None ) parser.add_argument( "--problematic", help="Only show runs with warnings", action="store_true" ) parser.add_argument( "--skip-converged", help="Do not check converged runs", action="store_true" ) # Backward-compatible alias (undocumented). `--skip_converged` # was renamed to `--skip-converged` in 1.2.0. parser.add_argument( "--skip_converged", dest="skip_converged", help=argparse.SUPPRESS, action="store_true", ) parser.add_argument( "--skip-running", help="Do not check running runs", action="store_true" ) parser.add_argument( "--fast", help="Do not parse OUTCAR to get run summary", action="store_true" )
[docs] def format_duration(seconds: float | None) -> str: """Format a duration in seconds compactly. Rounds to the most significant non-zero unit so the string stays short in table cells: ``"6s"``, ``"15m"``, ``"1h"``. Args: seconds: Duration in seconds, or NaN for undefined. Returns: str: Human-readable duration, or ``"N/A"`` when seconds is NaN. """ if seconds is None or np.isnan(seconds): return "N/A" seconds = float(seconds) if seconds < 60: return f"{seconds:.0f}s" minutes = seconds / 60 if minutes < 60: return f"{minutes:.0f}m" return f"{seconds / 3600:.0f}h"
[docs] def format_timing_stats(stats: RunTimingStats) -> str: """Format RunTimingStats for display. Omits the ``+/-`` std term when fewer than two samples are available, and returns an empty string when no SCF cycle was recorded. Args: stats: RunTimingStats instance. Returns: str: e.g. ``"SCF: 6.1±2.5s relax: 14m51s"``. """ if stats.scf.n == 0: return "" def fmt(mean_std: MeanStd) -> str: mean = format_duration(mean_std.mean) if mean_std.n < 2 or np.isnan(mean_std.std): return mean return f"{mean}±{format_duration(mean_std.std)}" return f"SCF: {fmt(stats.scf)} relax: {fmt(stats.ionic)}"
[docs] def vasp_output_time(path: str | Path) -> float | None: """Return last VASP output modification time. Deprecated: use :meth:`MRFVaspDir.output_mtime` instead. Args: path: VASP directory path. Returns: float | None: Modification timestamp, or None if no VASP output file is found. For NEB calculations, the maximum timestamp across all image directories is returned. """ warnings.warn( "vasp_output_time is deprecated; " "use MRFVaspDir.output_mtime() instead", DeprecationWarning, stacklevel=2, ) return MRFVaspDir(path).output_mtime()
def _collect_warnings( warnings: VaspWarnings, ignore_list: None | list[str] = None ) -> tuple[set[str], dict[str, tuple[str, list[str], str | None]]]: """Collect warning names and their message/tips/source metadata. Args: warnings: Structured warning container for one run. ignore_list: Warning names to skip. Returns: A tuple ``(names, meta)`` where ``names`` is the set of warning names present (after ignoring) and ``meta`` maps each name to a ``(message, tips, source)`` triple for the summary block. The source is the record's ``source`` field; callers use it to print the directory/file instead of relying on the message text. """ names: set[str] = set() meta: dict[str, tuple[str, list[str], str | None]] = {} for warn_name, record in warnings.items(): if ignore_list is not None and warn_name in ignore_list: continue names.add(warn_name) if warn_name not in meta: meta[warn_name] = ( record.message, list(record.tips), record.source) return names, meta def _get_progress(logs: list[Vasplog | Outcar]) -> str: """Get formatted progress string from list of LOGS. """ progress_data = logs[-1].progress if len(progress_data.values()) > 0: return list(progress_data.values())[-1].message return "N/A" def _get_neb_summary(vaspdir: MRFVaspDir) -> str: """Return a rich-markup string summarising NEB image distances.""" neb_dirs = vaspdir.neb_dirs() assert neb_dirs is not None neb_structures = [] neb_structures_initial = [] for nebimagedir in neb_dirs: neb_structures.append( nebimagedir.final_structure or nebimagedir.initial_structure) neb_structures_initial.append(nebimagedir.initial_structure) def get_dists(structs: list[Structure | None]) -> str: dists = [ structure_distance(str1, str2, tol=0) if str1 is not None and str2 is not None else None for str1, str2 in zip(structs, structs[1:])] parts = [] for idx, dist in enumerate(dists): if dist is None: # An image dir may exist without POSCAR/CONTCAR (e.g. a # partially written input set); the distance is unknown, # so keep the position and mark it instead of crashing. parts.append(f"{idx + 1:02d}: N/A") continue value = f"{dist:.2f}\u00c5" if np.isclose(dist, 0): value = f"[red]{value}[/red]" parts.append(f"{idx + 1:02d}: {value}") return " ".join(parts) return ( "[magenta]IMAGE DISTANCES (initial) [/magenta]" + get_dists(neb_structures_initial) + "\n" + "[magenta]IMAGE DISTANCES [/magenta]" + get_dists(neb_structures)) @dataclass class _RunRow: """Structured display data for a single VASP run.""" dir_full: str when: str running: bool converged: bool is_neb: bool status_label: str status_color: str warn_names: tuple[str, ...] energy: str fmax: str per_step: str progress: str cpu_time: str n_cores: str neb_summary: str _RICH_TAG_RE = re.compile(r"\[/?[a-z]+?\]") def _strip_markup(text: str) -> str: """Remove simple rich color tags (e.g. ``[red]``) from TEXT.""" return _RICH_TAG_RE.sub("", text) def _source_label(source: str | None) -> str: """Render a warning record source as a relative path for display. Args: source: ``VaspWarningRecord.source`` value. Returns: str: The source path relative to the current directory, or an empty string when ``source`` is falsy. """ if not source: return "" return os.path.relpath(str(source)) def _details_text(row: _RunRow) -> str: """One-line plain-text details for the table column. Combines the running progress message, CPU time, and NEB image distances (markup stripped) into a single ``|``-separated string. """ parts = [] if row.progress: parts.append(row.progress) if row.cpu_time: cores = f" ({row.n_cores} cores)" if row.n_cores else "" parts.append(f"CPU {row.cpu_time}{cores}") if row.neb_summary: parts.append(_strip_markup(row.neb_summary)) return " | ".join(parts) def _add_run_row(table: Table, row: _RunRow) -> None: """Append one run's display cells to TABLE. Args: table: rich Table with the status columns already added. row: _RunRow to render. """ if row.warn_names: status_cell = Text(row.status_label, style="bold black on yellow") else: status_cell = Text(row.status_label, style=row.status_color) dir_cell = Text() if row.is_neb: dir_cell.append("IMAGES ", style="magenta") dir_cell.append(row.dir_full) warn_cell = (Text(", ".join(row.warn_names), style="yellow") if row.warn_names else Text("-", style="dim")) cells = [ row.when, status_cell, dir_cell, row.energy or "-", row.fmax or "-", row.per_step or "-", warn_cell, _details_text(row) or "-", ] table.add_row(*cells) def _build_status_table(rows: Iterable[_RunRow], dir_max: int) -> Table: """Build a fresh status table holding the given ROWS. Callers rebuild the table only when ``rows`` changes, so it always contains the most recent ``rows`` (typically the last N scanned), keeping the render short enough for ``Live`` to redraw in place. Args: rows: Iterable of _RunRow to render. dir_max: Maximum width of the DIR column in cells. Returns: Table: A populated rich Table. """ table = Table( box=None, show_header=True, header_style="bold", padding=(0, 1)) table.add_column("WHEN", justify="right", style="dim", no_wrap=True) table.add_column("STATUS", no_wrap=True) table.add_column("DIR", max_width=dir_max, overflow="fold") table.add_column("ENERGY (eV)", justify="right", no_wrap=True) table.add_column("FMAX (eV/\u00c5)", justify="right", no_wrap=True) table.add_column("PER-STEP", no_wrap=True) table.add_column("WARN", overflow="fold") table.add_column("DETAILS", overflow="ellipsis", no_wrap=True, max_width=20) for row in rows: _add_run_row(table, row) return table
[docs] def status(args: Namespace) -> int: """Run the status subcommand. Args: args: Parsed command-line arguments from argparse. Returns: int: Exit code (0 on success). """ def include_dirp(p: str | Path) -> bool: p = str(p) if args.exclude is not None and re.search(args.exclude, p): return False if args.include is not None and not re.search(args.include, p): return False return True with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=UserWarning) warnings.filterwarnings( "ignore", category=UnconvergedVASPWarning, append=True) warnings.filterwarnings( "ignore", category=VaspWarning, append=True) vaspdirs = MRFVaspDir.read_vaspdirs( args.dir, path_filter=include_dirp) paths_no_output: list[str] = [] for wdir, vaspdir in vaspdirs.items(): has_slurm = False # Assume that vaspdir is valid as long as it has slurm logs # Examples: ATAT input dir, top-level NEB input. for f in Path(wdir).iterdir(): if re.match('slurm-[0-9]+.out', f.name): has_slurm = True break # Presence check: OUTCAR.gz still counts. has_outcar = 'OUTCAR' in vaspdir if not (has_slurm or has_outcar): if not slurm_runningp(wdir): paths_no_output.append(wdir) paths = sorted(vaspdirs) logger.debug("Found VASP dirs: %s", paths) if paths_no_output: console.print( "[yellow]Directories containing VASP input but not output:[/yellow]") for wdir in sorted(paths_no_output): console.print(Text.assemble((" ", ""), (str(wdir), ""))) incars = [] # warning name -> category (running/converged/unconverged) -> list of # (dir, message, tips, source) tuples. dirs_with_warnings: dict[str, dict[str, list[tuple[str, str, list[str], str | None]]]] = {} rows: list[_RunRow] = [] n_skipped = 0 dir_max = max(30, min(48, console.width - 70)) # Keep only the last N rows in the live render so the table stays # short and Live redraws in place (auto-scroll) instead of growing # past the terminal height. visible_max = max(10, console.height or 40) visible_rows: deque[_RunRow] = deque(maxlen=visible_max) if console.is_terminal: status_table = _build_status_table([], dir_max) live = Live( Group( Text("Reading VASP outputs...", style="dim"), status_table), console=console, refresh_per_second=1) live.start() else: live = None status_table = None try: with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=VaspWarning) last_update = 0.0 for i, wdir in enumerate(paths, 1): if live is not None: assert status_table is not None live.update(Group( Text( f"Reading VASP outputs: {i}/{len(paths)}", style="dim"), status_table)) vaspdir = vaspdirs.get(wdir) assert vaspdir is not None incar = vaspdir['INCAR'] if incar is not None: incar['SYSTEM'] = wdir incars.append(incar) # Avoid overflowing memory when reading too many dirs. del vaspdirs[wdir] nebp = vaspdir.nebp if slurm_runningp(wdir): running, converged = True, False status_label, status_color = "running", "yellow" else: running = False converged = (vaspdir.converged_electronic and vaspdir.converged_ionic and vaspdir.converged_manual) if converged: status_label = ( "converged; next step pending" if not vaspdir.converged_sequence else "converged") status_color = "green" else: status_label, status_color = "unconverged", "red" logger.debug( '%s: running = %s, converged = %s', wdir, running, converged) if args.skip_running and running: logger.debug('skipping running run') n_skipped += 1 continue if args.skip_converged and converged: logger.debug('skipping converged run') n_skipped += 1 continue progress = "" warn_names: set[str] = set() if logs := (not nebp) and vaspdir.logs(): logger.debug( "Found VASP logs in %s: %s", wdir, [log.file.name for log in logs]) if not converged: progress = _get_progress(logs) nowarn_list = ['unconverged_energy'] if running else [] if args.nowarn: nowarn_list += args.nowarn warn_names, meta = _collect_warnings( vaspdir.warnings, nowarn_list) dir_norm = wdir.replace("./", "") if running: category = "running" elif converged: category = "converged" else: category = "unconverged" for warn_name in warn_names: msg, tips, source = meta[warn_name] dirs_with_warnings.setdefault( warn_name, {}).setdefault( category, []).append( (dir_norm, msg, tips, source)) if not warn_names and category == "unconverged": dirs_with_warnings.setdefault( 'UNCONVERGED', {}).setdefault( category, []).append( (dir_norm, "", [], None)) else: if not nebp: logger.debug("Slurm log file not found in %s", wdir) per_step = "" if running and not nebp and not args.fast: # A running VASP writes OUTCAR uncompressed; the # compressed variants appear only after cleanup. outcar_path = Path(wdir) / 'OUTCAR' if outcar_path.is_file(): per_step = format_timing_stats( read_outcar_timing_stats(outcar_path)) if args.problematic and not warn_names\ and (converged or running): n_skipped += 1 continue final_energy_str = "" fmax_str = "" cpu_time = "" n_cores = "" if not running: # ``vasprun.xml`` present on disk but unparseable # (``__getitem__`` returned None): incomplete. if vaspdir['vasprun.xml'] is None and\ 'vasprun.xml' in vaspdir: status_label = "incomplete vasprun.xml" status_color = "red" final_energy = vaspdir.final_energy if final_energy is None or final_energy == np.nan: final_energy_str = "" else: final_energy_str = "" if np.isnan(final_energy)\ else f"{final_energy:.4f}" if not args.fast: run_stats = vaspdir.run_stats if run_stats is not None: cpu_time_sec = run_stats.get( 'Total CPU time used (sec)') if cpu_time_sec is not None: cpu_time = str(datetime.timedelta( seconds=round(cpu_time_sec))) cores = run_stats['cores'] if cores is not None: n_cores = str(cores) max_force = vaspdir.max_force() if max_force is not None: fmax_str = f"{max_force:.4f}" timing_stats = vaspdir.timing_stats if timing_stats is not None: per_step = format_timing_stats(timing_stats) mtime = vaspdir.output_mtime() if mtime is None: continue delta = mtime - datetime.datetime.now().timestamp() dir_full = wdir.replace("./", "") row = _RunRow( dir_full=dir_full, when=print_seconds(delta), running=running, converged=converged, is_neb=nebp, status_label=status_label, status_color=status_color, warn_names=tuple(sorted(warn_names)), energy=final_energy_str, fmax=fmax_str, per_step=per_step, progress=progress, cpu_time=cpu_time, n_cores=n_cores, neb_summary=_get_neb_summary(vaspdir) if nebp else "", ) rows.append(row) visible_rows.append(row) if live is not None: now = time.monotonic() if now - last_update >= 1.0 or i == len(paths): status_table = _build_status_table( visible_rows, dir_max) last_update = now n_running = sum(1 for r in rows if r.running) n_converged = sum(1 for r in rows if r.converged) n_other = len(rows) - n_running - n_converged summary = Text.assemble( (f"{len(rows)} runs: ", "bold"), (f"{n_running} running, ", "yellow"), (f"{n_converged} converged, ", "green"), (f"{n_other} unconverged", "red")) if n_skipped: summary.append(f", {n_skipped} skipped", style="dim") # Final frame: swap the progress line for the summary and render # the complete table so the terminal ends with every row visible. if live is not None: live.update(Group( summary, _build_status_table(rows, dir_max))) finally: if live is not None: live.stop() if live is None: # Non-terminal output: emit the full table and summary once so # piping carries every row (Live would otherwise only print the # final auto-scroll window). if rows: console.print(_build_status_table(rows, dir_max)) console.print() console.print(summary) if dirs_with_warnings: category_order = ("unconverged", "running", "converged") console.print() console.print("[bold yellow]Warnings:[/bold yellow]") for warn_name, by_category in dirs_with_warnings.items(): # Tips are shared across every directory carrying the same # warning name, so print them once per name. tips: list[str] = [] for category in category_order: for _, _, entry_tips, _ in by_category.get(category, []): if entry_tips: tips = entry_tips break if tips: break for category in category_order: entries = by_category.get(category) if not entries: continue tag = "" if warn_name == "UNCONVERGED" else f" [{category}]" console.print(Text.assemble( (" ", ""), (warn_name, "yellow"), (f"{tag} ({len(entries)}):", ""))) # Directory list stays on a single line so it can be # copied straight into a command line. dir_list = Text(" ") for idx, (dir_norm, _, _, _) in enumerate(entries): if idx: dir_list.append(" ") dir_list.append(dir_norm, style="cyan") console.print(dir_list, soft_wrap=True) # Warning messages are separated from the directory list. if any(msg for _, msg, _, _ in entries): console.print() for dir_norm, msg, _, source in entries: if not msg: continue label = _source_label(source) or dir_norm console.print(Text.assemble( (" ", ""), (label, "cyan"), (": ", ""), (str(msg), ""))) if tips: for tip in tips: console.print(Text.assemble( (" TIP: ", "magenta bold"), (str(tip), ""))) # Full details are only useful for runs still in flight or that # failed; converged runs would only repeat the CPU time already # shown in the truncated DETAILS column. detail_rows = [ r for r in rows if not r.converged and (r.progress or r.cpu_time or r.neb_summary)] if detail_rows: console.print() console.print("[bold]Details:[/bold]") for r in detail_rows: head = Text(" ") head.append(r.dir_full, style="bold") console.print(head) if r.progress: console.print(Text.assemble((" ", ""), (r.progress, ""))) if r.cpu_time: cores = f" ({r.n_cores} cores)" if r.n_cores else "" console.print(Text.assemble( (" ", ""), (f"CPU {r.cpu_time}{cores}", ""))) if r.neb_summary: for line in r.neb_summary.split("\n"): console.print(Text.from_markup(" " + line)) common_incar, grouped_incars = Incar.group_incars(incars) if common_incar is not None and len(grouped_incars) > 1: console.print(Text("Multiple INCARs found: ", style="yellow")) print_incar_groups( common_incar, grouped_incars, console=console, max_names=3) return 0