Source code for autojob.harvest.harvesters.default
"""Default calculation harvesting utilities.
This module defines the :func:`.harvest_default_calculator_results` function
which can be used as a default harvester for calculations.
Example:
from pathlib import Path
from autojob.harvest.harvesters.espresso import harvest_default_calculator_results
outputs = harvest_default_calculator_results(Path.cwd())
"""
from __future__ import annotations
import logging
from pathlib import Path
from typing import Any
from ase import Atoms
import ase.io
from numpy import ndarray
from autojob import SETTINGS
logger = logging.getLogger(__name__)
[docs]
def harvest_default_calculator_results(src: str | Path) -> dict[str, Any]:
"""Harvest calculation results from a directory using a default method.
Args:
src: The directory from which to load the calculation results.
Returns:
A dictionary containing calculation results.
Note:
If ``SETTINGS.STRICT_MODE`` is true, this method may re-raise one of
AttributeError, FileNotFound, or KeyError.
"""
src = Path(src)
atoms_file = Path(src, SETTINGS.OUTPUT_ATOMS_FILE)
converged = atoms_file.exists()
results: dict[str, Any] = {"converged": converged}
try:
atoms: Atoms = ase.io.read(atoms_file) # type: ignore[assignment]
if atoms.calc:
results["energy"] = atoms.calc.results.pop("energy")
results["forces"] = atoms.calc.results.pop("forces")
results["calculator_results"] = {**atoms.calc.results}
for key, value in results["calculator_results"].items():
if isinstance(value, ndarray):
results["calculator_results"][key] = value.tolist()
except (AttributeError, FileNotFoundError, KeyError):
if SETTINGS.STRICT_MODE:
raise
return results