diff --git a/src/atomate2/aims/schemas/calculation.py b/src/atomate2/aims/schemas/calculation.py index 1b325b2a32..005f0cf2ad 100644 --- a/src/atomate2/aims/schemas/calculation.py +++ b/src/atomate2/aims/schemas/calculation.py @@ -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 @@ -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( @@ -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: @@ -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, diff --git a/src/atomate2/aims/schemas/task.py b/src/atomate2/aims/schemas/task.py index 25dd400d92..bb98b3e761 100644 --- a/src/atomate2/aims/schemas/task.py +++ b/src/atomate2/aims/schemas/task.py @@ -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( @@ -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 @@ -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" ) @@ -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]), diff --git a/tests/aims/test_schemas/__init__.py b/tests/aims/test_schemas/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/aims/test_schemas/test_convergence.py b/tests/aims/test_schemas/test_convergence.py new file mode 100644 index 0000000000..98a5092623 --- /dev/null +++ b/tests/aims/test_schemas/test_convergence.py @@ -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 diff --git a/tests/test_data/aims/static-si-unconverged/aims.out.gz b/tests/test_data/aims/static-si-unconverged/aims.out.gz new file mode 100644 index 0000000000..f7e0bd7b9a Binary files /dev/null and b/tests/test_data/aims/static-si-unconverged/aims.out.gz differ diff --git a/tests/test_data/aims/static-si-unconverged/control.in.gz b/tests/test_data/aims/static-si-unconverged/control.in.gz new file mode 100644 index 0000000000..027bd2e2ea Binary files /dev/null and b/tests/test_data/aims/static-si-unconverged/control.in.gz differ diff --git a/tests/test_data/aims/static-si-unconverged/geometry.in.gz b/tests/test_data/aims/static-si-unconverged/geometry.in.gz new file mode 100644 index 0000000000..c2e720a036 Binary files /dev/null and b/tests/test_data/aims/static-si-unconverged/geometry.in.gz differ diff --git a/tests/test_data/aims/static-si-unconverged/inputs/control.in.gz b/tests/test_data/aims/static-si-unconverged/inputs/control.in.gz new file mode 100644 index 0000000000..3f5cdc7af5 Binary files /dev/null and b/tests/test_data/aims/static-si-unconverged/inputs/control.in.gz differ diff --git a/tests/test_data/aims/static-si-unconverged/inputs/geometry.in.gz b/tests/test_data/aims/static-si-unconverged/inputs/geometry.in.gz new file mode 100644 index 0000000000..4e9a9758c6 Binary files /dev/null and b/tests/test_data/aims/static-si-unconverged/inputs/geometry.in.gz differ diff --git a/tests/test_data/aims/static-si-unconverged/inputs/parameters.json b/tests/test_data/aims/static-si-unconverged/inputs/parameters.json new file mode 100644 index 0000000000..3b8dfe8fae --- /dev/null +++ b/tests/test_data/aims/static-si-unconverged/inputs/parameters.json @@ -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 + ] +} diff --git a/tests/test_data/aims/static-si-unconverged/outputs/aims.out.gz b/tests/test_data/aims/static-si-unconverged/outputs/aims.out.gz new file mode 100644 index 0000000000..e51639a139 Binary files /dev/null and b/tests/test_data/aims/static-si-unconverged/outputs/aims.out.gz differ diff --git a/tests/test_data/aims/static-si-unconverged/parameters.json b/tests/test_data/aims/static-si-unconverged/parameters.json new file mode 100644 index 0000000000..62b8f5feb9 --- /dev/null +++ b/tests/test_data/aims/static-si-unconverged/parameters.json @@ -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 + ] +}