Repository: Quantum-Accelerators/quacc
Pull request #1584 (2024-01-22 07:06:37)

Add a safety check on `calc_cleanup`


Patch:
diff --git a/docs/user/recipes/recipes_intro.md b/docs/user/recipes/recipes_intro.md
index f41f3a72..596d574f 100644
--- a/docs/user/recipes/recipes_intro.md
+++ b/docs/user/recipes/recipes_intro.md
@@ -38,131 +38,90 @@ print(result)
 ```
 
 ??? Info "Printed Output"
-
-    ```python
-    {'atoms': Atoms(symbols='Cu', pbc=True, cell=[[0.0, 1.805, 1.805], [1.805, 0.0, 1.805], [1.805, 1.805, 0.0]], initial_magmoms=...),
-     'atoms_info': {'_id': 'f8d53f110f524872f30a5cc20e8befb1'},
-     'builder_meta': {'build_date': datetime.datetime(2023, 10, 19, 0, 46, 1, 742310),
-                      'emmet_version': '0.69.6',
-                      'pymatgen_version': '2023.10.4'},
-     'chemsys': 'Cu',
-     'composition': Composition('Cu1'),
-     'composition_reduced': Composition('Cu1'),
-     'converged': True,
-     'density': 8.971719800606017,
-     'density_atomic': 11.761470249999999,
-     'dir_name': '/home/rosen',
-     'elements': [Element Cu],
-     'fmax': 0.01,
-     'formula_anonymous': 'A',
-     'formula_pretty': 'Cu',
-     'input_atoms': {'atoms': Atoms(symbols='Cu', pbc=True, cell=[[0.0, 1.805, 1.805], [1.805, 0.0, 1.805], [1.805, 1.805, 0.0]], calculator=SinglePointCalculator(...)),
-                     'atoms_info': {},
-                     'builder_meta': {'build_date': datetime.datetime(2023, 10, 19, 0, 46, 1, 725218),
-                                      'emmet_version': '0.69.6',
-                                      'pymatgen_version': '2023.10.4'},
-                     'chemsys': 'Cu',
-                     'composition': Composition('Cu1'),
-                     'composition_reduced': Composition('Cu1'),
-                     'density': 8.971719800606017,
-                     'density_atomic': 11.761470249999999,
-                     'elements': [Element Cu],
-                     'formula_anonymous': 'A',
-                     'formula_pretty': 'Cu',
-                     'nelements': 1,
-                     'nsites': 1,
-                     'structure': Structure Summary
-    Lattice
-        abc : 2.5526554800834367 2.5526554800834367 2.5526554800834367
-     angles : 60.00000000000001 60.00000000000001 60.00000000000001
-     volume : 11.761470249999999
-          A : 0.0 1.805 1.805
-          B : 1.805 0.0 1.805
-          C : 1.805 1.805 0.0
-        pbc : True True True
-    PeriodicSite: Cu (0.0, 0.0, 0.0) [0.0, 0.0, 0.0],
-                     'symmetry': {'crystal_system': <CrystalSystem.cubic: 'Cubic'>,
-                                  'number': 225,
-                                  'point_group': 'm-3m',
-                                  'symbol': 'Fm-3m',
-                                  'symprec': 0.1,
-                                  'version': '2.1.0'},
-                     'volume': 11.761470249999999},
-     'name': 'EMT Relax',
-     'nelements': 1,
-     'nid': 'rosen.',
-     'nsites': 1,
-     'nsteps': 0,
-     'parameters': {'asap_cutoff': False},
-     'parameters_opt': {'max_steps': 1000,
-                        'maxstep': 0.2,
-                        'optimizer': 'FIRE',
-                        'type': 'optimization'},
-     'results': {'energies': array([-0.00568151]),
-                 'energy': -0.005681511358588409,
-                 'forces': array([[0., 0., 0.]]),
-                 'free_energy': -0.005681511358588409},
-     'structure': Structure Summary
-    Lattice
-        abc : 2.5526554800834367 2.5526554800834367 2.5526554800834367
-     angles : 60.00000000000001 60.00000000000001 60.00000000000001
-     volume : 11.761470249999999
-          A : 0.0 1.805 1.805
-          B : 1.805 0.0 1.805
-          C : 1.805 1.805 0.0
-        pbc : True True True
-    PeriodicSite: Cu (0.0, 0.0, 0.0) [0.0, 0.0, 0.0],
-     'symmetry': {'crystal_system': <CrystalSystem.cubic: 'Cubic'>,
-                  'number': 225,
-                  'point_group': 'm-3m',
-                  'symbol': 'Fm-3m',
-                  'symprec': 0.1,
-                  'version': '2.1.0'},
-     'trajectory': [{'atoms': Atoms(symbols='Cu', pbc=True, cell=[[0.0, 1.805, 1.805], [1.805, 0.0, 1.805], [1.805, 1.805, 0.0]], calculator=SinglePointCalculator(...)),
-                     'atoms_info': {},
-                     'builder_meta': {'build_date': datetime.datetime(2023, 10, 19, 0, 46, 1, 744287),
-                                      'emmet_version': '0.69.6',
-                                      'pymatgen_version': '2023.10.4'},
-                     'chemsys': 'Cu',
-                     'composition': Composition('Cu1'),
-                     'composition_reduced': Composition('Cu1'),
-                     'density': 8.971719800606017,
-                     'density_atomic': 11.761470249999999,
-                     'elements': [Element Cu],
-                     'formula_anonymous': 'A',
-                     'formula_pretty': 'Cu',
-                     'nelements': 1,
-                     'nsites': 1,
-                     'structure': Structure Summary
-    Lattice
-        abc : 2.5526554800834367 2.5526554800834367 2.5526554800834367
-     angles : 60.00000000000001 60.00000000000001 60.00000000000001
-     volume : 11.761470249999999
-          A : 0.0 1.805 1.805
-          B : 1.805 0.0 1.805
-          C : 1.805 1.805 0.0
-        pbc : True True True
-    PeriodicSite: Cu (0.0, 0.0, 0.0) [0.0, 0.0, 0.0],
-                     'symmetry': {'crystal_system': <CrystalSystem.cubic: 'Cubic'>,
-                                  'number': 225,
-                                  'point_group': 'm-3m',
-                                  'symbol': 'Fm-3m',
-                                  'symprec': 0.1,
-                                  'version': '2.1.0'},
-                     'volume': 11.761470249999999}],
-     'trajectory_results': [{'energies': array([-0.00568151]),
-                             'energy': -0.005681511358588409,
-                             'forces': array([[0., 0., 0.]]),
-                             'free_energy': -0.005681511358588409}],
-     'volume': 11.761470249999999}
-    ```
+{'atoms': Atoms(symbols='Cu', pbc=True, cell=[[0.0, 1.805, 1.805], [1.805, 0.0, 1.805], [1.805, 1.805, 0.0]], initial_magmoms=...),
+'atoms_info': {'\_id': 'f8d53f110f524872f30a5cc20e8befb1'},
+'builder_meta': {'build_date': datetime.datetime(2024, 1, 22, 6, 53, 30, 342389),
+'emmet_version': '0.76.2',
+'pymatgen_version': '2023.12.18'},
+'chemsys': 'Cu',
+'composition': Composition('Cu1'),
+'composition_reduced': Composition('Cu1'),
+'converged': True,
+'density': 8.971719800606017,
+'density_atomic': 11.761470249999999,
+'dir_name': '/home/rosen/test/quacc-2024-01-22-06-53-30-305561-70901',
+'elements': [Element Cu],
+'fmax': 0.01,
+'formula_anonymous': 'A',
+'formula_pretty': 'Cu',
+'input_atoms': {'atoms': Atoms(symbols='Cu', pbc=True, cell=[[0.0, 1.805, 1.805], [1.805, 0.0, 1.805], [1.805, 1.805, 0.0]], calculator=SinglePointCalculator(...)),
+'atoms_info': {},
+'builder_meta': {'build_date': datetime.datetime(2024, 1, 22, 6, 53, 30, 330220),
+'emmet_version': '0.76.2',
+'pymatgen_version': '2023.12.18'},
+'chemsys': 'Cu',
+'composition': Composition('Cu1'),
+'composition_reduced': Composition('Cu1'),
+'density': 8.971719800606017,
+'density_atomic': 11.761470249999999,
+'elements': [Element Cu],
+'formula_anonymous': 'A',
+'formula_pretty': 'Cu',
+'nelements': 1,
+'nsites': 1,
+'symmetry': {'crystal_system': <CrystalSystem.cubic: 'Cubic'>,
+'number': 225,
+'point_group': 'm-3m',
+'symbol': 'Fm-3m',
+'symprec': 0.1,
+'version': '2.2.0'},
+'volume': 11.761470249999999},
+'name': 'EMT Relax',
+'nelements': 1,
+'nid': 'rosen.',
+'nsites': 1,
+'nsteps': 0,
+'parameters': {'asap_cutoff': False},
+'parameters_opt': {'max_steps': 1000,
+'maxstep': 0.2,
+'optimizer': 'FIRE',
+'type': 'optimization'},
+'quacc_version': '0.6.2',
+'results': {'energies': array([-0.00568151]),
+'energy': -0.005681511358588409,
+'forces': array([[0., 0., 0.]]),
+'free_energy': -0.005681511358588409},
+'structure': Structure Summary
+Lattice
+abc : 2.5526554800834367 2.5526554800834367 2.5526554800834367
+angles : 60.00000000000001 60.00000000000001 60.00000000000001
+volume : 11.761470249999999
+A : 0.0 1.805 1.805
+B : 1.805 0.0 1.805
+C : 1.805 1.805 0.0
+pbc : True True True
+PeriodicSite: Cu (0.0, 0.0, 0.0) [0.0, 0.0, 0.0],
+'symmetry': {'crystal_system': <CrystalSystem.cubic: 'Cubic'>,
+'number': 225,
+'point_group': 'm-3m',
+'symbol': 'Fm-3m',
+'symprec': 0.1,
+'version': '2.2.0'},
+'trajectory': [Atoms(symbols='Cu', pbc=True, cell=[[0.0, 1.805, 1.805], [1.805, 0.0, 1.805], [1.805, 1.805, 0.0]], calculator=SinglePointCalculator(...))],
+'trajectory_results': [{'energy': -0.005681511358588409,
+'forces': array([[0., 0., 0.]]),
+'free_energy': -0.005681511358588409,
+'energies': array([-0.00568151])}],
+'volume': 11.761470249999999}
+
+````
 
 ### A Mixed-Code Workflow
 
 ```mermaid
 graph LR
   A[Input] --> B(EMT Relax) --> C(GFN2-xTB Static) --> D[Output]
-```
+````
 
 Now let's return to our bulk Cu example from above and start adding on some complexity. Here, we will use EMT to run a relaxation on the bulk Cu structure and then use the output of this calculation as the input to a static calculation with the semi-empirical quantum mechanics method GFN2-xTB as implemented in [quacc.recipes.tblite.core.static_job][].
 
@@ -192,84 +151,75 @@ print(result2)
 
     ```python
     {'atoms': Atoms(symbols='Cu', pbc=True, cell=[[0.0, 1.805, 1.805], [1.805, 0.0, 1.805], [1.805, 1.805, 0.0]], initial_magmoms=...),
-    'atoms_info': {'_id': 'f8d53f110f524872f30a5cc20e8befb1',
-                    '_old_ids': ['f8d53f110f524872f30a5cc20e8befb1']},
-    'builder_meta': {'build_date': datetime.datetime(2023, 10, 18, 21, 22, 33, 352823),
-                    'emmet_version': '0.69.6',
-                    'pymatgen_version': '2023.10.4'},
-    'chemsys': 'Cu',
-    'composition': Composition('Cu1'),
-    'composition_reduced': Composition('Cu1'),
-    'density': 8.971719800606017,
-    'density_atomic': 11.761470249999999,
-    'dir_name': '/home/rosen',
-    'elements': [Element Cu],
-    'formula_anonymous': 'A',
-    'formula_pretty': 'Cu',
-    'input_atoms': {'atoms': Atoms(symbols='Cu', pbc=True, cell=[[0.0, 1.805, 1.805], [1.805, 0.0, 1.805], [1.805, 1.805, 0.0]], initial_magmoms=..., calculator=TBLite(...)),
-                    'atoms_info': {'_id': 'f8d53f110f524872f30a5cc20e8befb1'},
-                    'builder_meta': {'build_date': datetime.datetime(2023, 10, 18, 21, 22, 33, 348713),
-                                    'emmet_version': '0.69.6',
-                                    'pymatgen_version': '2023.10.4'},
-                    'chemsys': 'Cu',
-                    'composition': Composition('Cu1'),
-                    'composition_reduced': Composition('Cu1'),
-                    'density': 8.971719800606017,
-                    'density_atomic': 11.761470249999999,
-                    'elements': [Element Cu],
-                    'formula_anonymous': 'A',
-                    'formula_pretty': 'Cu',
-                    'nelements': 1,
-                    'nsites': 1,
-                    'structure': Structure Summary
-    Lattice
-        abc : 2.5526554800834367 2.5526554800834367 2.5526554800834367
-    angles : 60.00000000000001 60.00000000000001 60.00000000000001
-    volume : 11.761470249999999
-        A : 0.0 1.805 1.805
-        B : 1.805 0.0 1.805
-        C : 1.805 1.805 0.0
-        pbc : True True True
-    PeriodicSite: Cu (0.0, 0.0, 0.0) [0.0, 0.0, 0.0],
-                    'symmetry': {'crystal_system': <CrystalSystem.cubic: 'Cubic'>,
-                                'number': 225,
-                                'point_group': 'm-3m',
-                                'symbol': 'Fm-3m',
-                                'symprec': 0.1,
-                                'version': '2.1.0'},
-                    'volume': 11.761470249999999},
-    'name': 'TBLite Static',
-    'nelements': 1,
-    'nid': 'rosen.',
-    'nsites': 1,
-    'parameters': {'accuracy': 1.0,
-                    'cache_api': True,
-                    'electronic_temperature': 300.0,
-                    'max_iterations': 250,
-                    'method': 'GFN2-xTB',
-                    'verbosity': 1},
-    'results': {'charges': array([-4.63975525e-11]),
-                'dipole': array([-8.01874552e-08,  1.98737614e-07, -5.14192823e-08]),
-                'energy': -318.8584605831409,
-                'forces': array([[5.44452313e-20, 5.39007790e-19, 2.45003541e-19]]),
-                'free_energy': -318.8584605831409,
-                'stress': array([14.65181119, 14.65181119, 14.65181119,  1.40704868,  1.01943286,
-            0.63181703])},
-    'structure': Structure Summary
-    Lattice
-        abc : 2.5526554800834367 2.5526554800834367 2.5526554800834367
-    angles : 60.00000000000001 60.00000000000001 60.00000000000001
-    volume : 11.761470249999999
-        A : 0.0 1.805 1.805
-        B : 1.805 0.0 1.805
-        C : 1.805 1.805 0.0
-        pbc : True True True
-    PeriodicSite: Cu (0.0, 0.0, 0.0) [0.0, 0.0, 0.0],
-    'symmetry': {'crystal_system': <CrystalSystem.cubic: 'Cubic'>,
-                'number': 225,
-                'point_group': 'm-3m',
-                'symbol': 'Fm-3m',
-                'symprec': 0.1,
-                'version': '2.1.0'},
-    'volume': 11.761470249999999}
+     'atoms_info': {'_id': 'f8d53f110f524872f30a5cc20e8befb1',
+      '_old_ids': ['f8d53f110f524872f30a5cc20e8befb1']},
+     'builder_meta': {'build_date': datetime.datetime(2024, 1, 22, 7, 7, 50, 448307),
+      'emmet_version': '0.76.2',
+      'pymatgen_version': '2023.12.18'},
+     'chemsys': 'Cu',
+     'composition': Composition('Cu1'),
+     'composition_reduced': Composition('Cu1'),
+     'density': 8.971719800606017,
+     'density_atomic': 11.761470249999999,
+     'dir_name': '/mnt/c/Users/asros/github/quacc/quacc-2024-01-22-07-07-48-973444-54105',
+     'elements': [Element Cu],
+     'formula_anonymous': 'A',
+     'formula_pretty': 'Cu',
+     'input_atoms': {'atoms': Atoms(symbols='Cu', pbc=True, cell=[[0.0, 1.805, 1.805], [1.805, 0.0, 1.805], [1.805, 1.805, 0.0]], initial_magmoms=..., calculator=TBLite(...)),
+      'atoms_info': {'_id': 'f8d53f110f524872f30a5cc20e8befb1'},
+      'builder_meta': {'build_date': datetime.datetime(2024, 1, 22, 7, 7, 50, 444783),
+       'emmet_version': '0.76.2',
+       'pymatgen_version': '2023.12.18'},
+      'chemsys': 'Cu',
+      'composition': Composition('Cu1'),
+      'composition_reduced': Composition('Cu1'),
+      'density': 8.971719800606017,
+      'density_atomic': 11.761470249999999,
+      'elements': [Element Cu],
+      'formula_anonymous': 'A',
+      'formula_pretty': 'Cu',
+      'nelements': 1,
+      'nsites': 1,
+      'symmetry': {'crystal_system': <CrystalSystem.cubic: 'Cubic'>,
+       'number': 225,
+       'point_group': 'm-3m',
+       'symbol': 'Fm-3m',
+       'symprec': 0.1,
+       'version': '2.2.0'},
+      'volume': 11.761470249999999},
+     'name': 'TBLite Static',
+     'nelements': 1,
+     'nid': 'rosen.',
+     'nsites': 1,
+     'parameters': {'accuracy': 1.0,
+      'cache_api': True,
+      'electronic_temperature': 300.0,
+      'max_iterations': 250,
+      'method': 'GFN2-xTB',
+      'verbosity': 1},
+     'quacc_version': '0.6.2',
+     'results': {'charges': array([-4.63868943e-11]),
+      'dipole': array([-8.03135613e-08,  1.96843643e-07, -5.14599908e-08]),
+      'energy': -318.8584605831394,
+      'forces': array([[5.44452313e-20, 5.39007790e-19, 2.45003541e-19]]),
+      'free_energy': -318.8584605831394,
+      'stress': array([14.65181119, 14.65181119, 14.65181119,  1.40704868,  1.01943286,
+              0.63181703])},
+     'structure': Structure Summary
+     Lattice
+         abc : 2.5526554800834367 2.5526554800834367 2.5526554800834367
+      angles : 60.00000000000001 60.00000000000001 60.00000000000001
+      volume : 11.761470249999999
+           A : 0.0 1.805 1.805
+           B : 1.805 0.0 1.805
+           C : 1.805 1.805 0.0
+         pbc : True True True
+     PeriodicSite: Cu (0.0, 0.0, 0.0) [0.0, 0.0, 0.0],
+     'symmetry': {'crystal_system': <CrystalSystem.cubic: 'Cubic'>,
+      'number': 225,
+      'point_group': 'm-3m',
+      'symbol': 'Fm-3m',
+      'symprec': 0.1,
+      'version': '2.2.0'},
+     'volume': 11.761470249999999}
     ```
diff --git a/src/quacc/runners/ase.py b/src/quacc/runners/ase.py
index 369e0d76..569a4040 100644
--- a/src/quacc/runners/ase.py
+++ b/src/quacc/runners/ase.py
@@ -31,7 +31,7 @@ if TYPE_CHECKING:
     from ase.optimize.optimize import Optimizer
 
     class OptimizerKwargs(TypedDict, total=False):
-        restart: str | None  # default = None
+        restart: Path | str | None  # default = None
         append_trajectory: bool  # default = False
 
     class VibKwargs(TypedDict, total=False):
diff --git a/src/quacc/runners/prep.py b/src/quacc/runners/prep.py
index b98f0eca..fd3fcdbd 100644
--- a/src/quacc/runners/prep.py
+++ b/src/quacc/runners/prep.py
@@ -6,7 +6,7 @@ from pathlib import Path
 from shutil import move, rmtree
 from typing import TYPE_CHECKING
 
-from monty.shutil import gzip_dir, remove
+from monty.shutil import gzip_dir
 
 from quacc import SETTINGS
 from quacc.utils.files import (
@@ -110,6 +110,11 @@ def calc_cleanup(atoms: Atoms, tmpdir: Path | str, job_results_dir: Path | str)
 
     job_results_dir, tmpdir = Path(job_results_dir), Path(tmpdir)
 
+    # Safety check
+    if "tmp-" not in str(tmpdir):
+        msg = f"{tmpdir} does not appear to be a tmpdir... exiting for safety!"
+        raise ValueError(msg)
+
     # Reset the calculator's directory
     atoms.calc.directory = job_results_dir
 
@@ -128,8 +133,6 @@ def calc_cleanup(atoms: Atoms, tmpdir: Path | str, job_results_dir: Path | str)
 
     # Move files from tmpdir to job_results_dir
     for file_name in os.listdir(tmpdir):
-        if Path(job_results_dir / file_name).is_dir():
-            remove(job_results_dir / file_name)
         move(tmpdir / file_name, job_results_dir / file_name)
 
     # Remove symlink to tmpdir
diff --git a/src/quacc/schemas/ase.py b/src/quacc/schemas/ase.py
index ea615799..2f09f876 100644
--- a/src/quacc/schemas/ase.py
+++ b/src/quacc/schemas/ase.py
@@ -84,7 +84,11 @@ def summarize_run(
     directory = final_atoms.calc.directory
     uri = get_uri(directory)
     input_atoms_metadata = (
-        atoms_to_metadata(input_atoms, charge_and_multiplicity=charge_and_multiplicity)
+        atoms_to_metadata(
+            input_atoms,
+            charge_and_multiplicity=charge_and_multiplicity,
+            store_pmg=False,
+        )
         if input_atoms
         else None
     )
@@ -194,9 +198,14 @@ def summarize_opt_run(
         store=False,
     )
 
+    # Clean up the opt parameters
+    parameters_opt = dyn.todict()
+    parameters_opt.pop("logfile", None)
+    parameters_opt.pop("restart", None)
+
     opt_fields = {
         "fmax": getattr(dyn, "fmax", None),
-        "parameters_opt": dyn.todict(),
+        "parameters_opt": parameters_opt,
         "converged": is_converged,
         "nsteps": dyn.get_number_of_steps(),
         "trajectory": trajectory,


Test patch:
diff --git a/tests/core/runners/test_prep.py b/tests/core/runners/test_prep.py
index 386b335d..7e5bd991 100644
--- a/tests/core/runners/test_prep.py
+++ b/tests/core/runners/test_prep.py
@@ -1,6 +1,7 @@
 import os
 from pathlib import Path
 
+import pytest
 from ase.build import bulk
 from ase.calculators.emt import EMT
 
@@ -94,3 +95,6 @@ def test_calc_cleanup(tmp_path, monkeypatch):
     assert Path(atoms.calc.directory) == SETTINGS.RESULTS_DIR
 
     SETTINGS.SCRATCH_DIR = DEFAULT_SETTINGS.SCRATCH_DIR
+
+    with pytest.raises(ValueError):
+        calc_cleanup(atoms, "quacc", SETTINGS.RESULTS_DIR)