Skip to content
Open
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
33 changes: 28 additions & 5 deletions src/atomate2/aims/schemas/calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ class Calculation(BaseModel):
aims_version: str
FHI-aims version used to perform the calculation
has_aims_completed: .TaskState
Whether FHI-aims completed the calculation successfully
Whether FHI-aims completed the calculation successfully and converged
(SCF, and geometry for relaxations)
completed: bool
Whether FHI-aims ran to completion and produced results
converged: bool
Whether the SCF cycle (and geometry, for relaxations) converged
output: .CalculationOutput
The FHI-aims calculation output
input: .CalculationInput
Expand All @@ -236,10 +241,17 @@ class Calculation(BaseModel):
None, description="FHI-aims version used to perform the calculation"
)
has_aims_completed: TaskState = Field(
None, description="Whether FHI-aims completed the calculation successfully"
None,
description="Whether FHI-aims completed the calculation successfully and "
"converged",
)
completed: bool = Field(
None, description="Whether FHI-aims completed the calculation successfully"
None, description="Whether FHI-aims ran to completion and produced results"
)
converged: bool | None = Field(
None,
description="Whether the SCF cycle (and geometry, for relaxations) converged. "
"None if unknown (e.g. outputs serialized by older versions)",
)
input: CalculationInput = Field(None, description="The FHI-aims calculation input")
output: CalculationOutput = Field(
Expand Down Expand Up @@ -346,8 +358,18 @@ def from_aims_files(

output_doc = CalculationOutput.from_aims_output(aims_output)

completed = aims_output.completed

# converged is None if unknown (e.g. outputs serialized by older versions)
converged = getattr(aims_output, "converged", None)

# As for VASP, the calculation is only successful if it converged. If the
# convergence is unknown (None), only the completion is considered. This avoid
# breaking existing workflows that rely on the completion status only.
has_aims_completed = (
TaskState.SUCCESS if aims_output.completed else TaskState.FAILED
TaskState.SUCCESS
if completed and converged is not False
else TaskState.FAILED
)

if store_trajectory:
Expand All @@ -359,7 +381,8 @@ def from_aims_files(
task_name=task_name,
aims_version=aims_output.aims_version,
has_aims_completed=has_aims_completed,
completed=has_aims_completed == TaskState.SUCCESS,
completed=completed,
converged=converged,
completed_at=completed_at,
output=output_doc,
input=input_doc,
Expand Down
8 changes: 7 additions & 1 deletion src/atomate2/aims/schemas/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def from_aims_calc_docs(cls, calc_docs: list[Calculation]) -> Self:

final_calc = calc_docs[-1]
max_force = None
if final_calc.has_aims_completed == TaskState.SUCCESS:
if final_calc.completed:
max_force = _get_max_force(final_calc)

return cls(
Expand Down Expand Up @@ -356,6 +356,8 @@ class AimsTaskDoc(BaseTaskDocument, StructureMetadata, MoleculeMetadata):
Timestamp for this task document was last updated
completed: bool
Whether this calculation completed
converged: bool or None
Whether the final calculation converged (None if unknown)
completed_at: str
Timestamp for when this task was completed
input: .InputDoc
Expand Down Expand Up @@ -401,6 +403,9 @@ class AimsTaskDoc(BaseTaskDocument, StructureMetadata, MoleculeMetadata):
description="Timestamp for this task document was last updated",
)
completed: bool = Field(None, description="Whether this calculation completed")
converged: bool | None = Field(
None, description="Whether the final calculation converged (None if unknown)"
)
completed_at: str = Field(
None, description="Timestamp for when this task was completed"
)
Expand Down Expand Up @@ -520,6 +525,7 @@ def from_directory(
"analysis": analysis,
"tags": tags,
"completed": calcs_reversed[-1].completed,
"converged": calcs_reversed[-1].converged,
"completed_at": calcs_reversed[-1].completed_at,
"input": InputDoc.from_aims_calc_doc(calcs_reversed[-1]),
"output": OutputDoc.from_aims_calc_doc(calcs_reversed[-1]),
Expand Down
Empty file.
99 changes: 99 additions & 0 deletions tests/aims/test_schemas/test_convergence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""Test that FHI-aims convergence is reflected in the task document state."""

import os

import pytest
from jobflow import run_locally
from pymatgen.io.aims.outputs import AimsOutput
from pymatgen.io.aims.sets.core import StaticSetGenerator

from atomate2.aims.jobs.core import StaticMaker
from atomate2.aims.run import should_stop_children
from atomate2.aims.schemas.calculation import TaskState

cwd = os.getcwd()

# AimsOutput.converged is not available in older pymatgen-io-aims releases
requires_converged = pytest.mark.skipif(
not hasattr(AimsOutput, "converged"),
reason="AimsOutput.converged requires a newer pymatgen-io-aims",
)


def _run_static(si, tmp_path, mock_aims, species_dir, ref_dir):
"""Run a mocked static calculation using the outputs in the given reference dir."""
mock_aims({"base": ref_dir}, {})

parameters = {
"k_grid": [2, 2, 2],
"species_dir": (species_dir / "light").as_posix(),
}
maker = StaticMaker(input_set_generator=StaticSetGenerator(user_params=parameters))
maker.name = "base"
job = maker.make(si)

os.chdir(tmp_path)
try:
responses = run_locally(job, create_folders=True, ensure_success=True)
finally:
os.chdir(cwd)
return responses[job.uuid][1]


@requires_converged
def test_converged_calculation(si, tmp_path, mock_aims, species_dir):
response = _run_static(si, tmp_path, mock_aims, species_dir, "static-si")
doc = response.output
calc = doc.calcs_reversed[-1]

assert calc.completed
assert calc.converged
assert calc.has_aims_completed == TaskState.SUCCESS
assert doc.completed
assert doc.converged
assert doc.state == TaskState.SUCCESS
assert not response.stop_children
assert not should_stop_children(doc)


@requires_converged
def test_unconverged_calculation(si, tmp_path, mock_aims, species_dir):
# same calculation as static-si, without the "SCF cycle converged" line
response = _run_static(
si, tmp_path, mock_aims, species_dir, "static-si-unconverged"
)
doc = response.output
calc = doc.calcs_reversed[-1]

# the run finished and produced results, but did not converge
assert calc.completed
assert not calc.converged
assert calc.has_aims_completed == TaskState.FAILED
assert doc.completed
assert not doc.converged
assert doc.state == TaskState.FAILED
assert response.stop_children

assert should_stop_children(doc)
assert should_stop_children(doc, handle_unsuccessful=True)
assert not should_stop_children(doc, handle_unsuccessful=False)
with pytest.raises(RuntimeError, match="not converged"):
should_stop_children(doc, handle_unsuccessful="error")


def test_unknown_convergence(si, tmp_path, mock_aims, species_dir, monkeypatch):
# convergence unknown (e.g. output serialized by an older pymatgen version):
# the state only depends on whether the calculation completed
monkeypatch.setattr(
AimsOutput, "converged", property(lambda _: None), raising=False
)
response = _run_static(si, tmp_path, mock_aims, species_dir, "static-si")
doc = response.output
calc = doc.calcs_reversed[-1]

assert calc.completed
assert calc.converged is None
assert calc.has_aims_completed == TaskState.SUCCESS
assert doc.converged is None
assert doc.state == TaskState.SUCCESS
assert not response.stop_children
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/test_data/aims/static-si-unconverged/inputs/parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"xc": "pbe",
"relativistic": "atomic_zora scalar",
"species_dir": "/home/tpurcell/git/fhi-aims-workflows/tests/test_input_generators/species_dir",
"k_grid": [
2,
2,
2
]
}
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/test_data/aims/static-si-unconverged/parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"xc": "pbe",
"relativistic": "atomic_zora scalar",
"species_dir": "/home/tpurcell/git/atomate2/tests/aims/species_dir/light",
"k_grid": [
2,
2,
2
]
}
Loading