Skip to content

Commit

Permalink
Improve serializability of recipe inputs (#895)
Browse files Browse the repository at this point in the history
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
Andrew-S-Rosen and deepsource-autofix[bot] authored Sep 7, 2023
1 parent 792620d commit 481e604
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 101 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- Re-added support for the Prefect workflow engine.

### Changed

- Removed unnecessary functions as kwargs in recipes.

## [0.2.5]

### Added
Expand Down
16 changes: 8 additions & 8 deletions docs/user/wflow_engine/wflow_engines2.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ graph LR
@flow
def workflow(atoms):
relaxed_bulk = relax_job(atoms)
relaxed_slabs = bulk_to_slabs_flow(relaxed_bulk, slab_static=None) # (1)!
relaxed_slabs = bulk_to_slabs_flow(relaxed_bulk, run_static=None) # (1)!

return relaxed_slabs

Expand All @@ -442,7 +442,7 @@ graph LR
print(result)
```

1. We didn't need to wrap `bulk_to_slabs_flow` with a decorator because it is already pre-decorated with a `#!Python @flow` decorator. We also chose to set `#!Python slab_static=None` here to disable the static calculation that is normally carried out in this workflow.
1. We didn't need to wrap `bulk_to_slabs_flow` with a decorator because it is already pre-decorated with a `#!Python @flow` decorator. We also chose to set `#!Python run_static=None` here to disable the static calculation that is normally carried out in this workflow.

=== "Parsl ⭐"

Expand All @@ -455,7 +455,7 @@ graph LR
# Define the workflow
def workflow(atoms):
relaxed_bulk = relax_job(atoms)
relaxed_slabs = bulk_to_slabs_flow(relaxed_bulk, slab_static=None) # (1)!
relaxed_slabs = bulk_to_slabs_flow(relaxed_bulk, run_static=None) # (1)!

return relaxed_slabs

Expand All @@ -470,7 +470,7 @@ graph LR
print(result)
```

1. We didn't need to wrap `bulk_to_slabs_flow` with a decorator because it is already pre-decorated with a `#!Python @flow` decorator. We also chose to set `#!Python slab_static=None` here to disable the static calculation that is normally carried out in this workflow.
1. We didn't need to wrap `bulk_to_slabs_flow` with a decorator because it is already pre-decorated with a `#!Python @flow` decorator. We also chose to set `#!Python run_static=None` here to disable the static calculation that is normally carried out in this workflow.

=== "Prefect"

Expand All @@ -485,7 +485,7 @@ graph LR
@flow
def workflow(atoms):
relaxed_bulk = relax_job(atoms)
relaxed_slabs = bulk_to_slabs_flow(relaxed_bulk, slab_static=None) # (1)!
relaxed_slabs = bulk_to_slabs_flow(relaxed_bulk, run_static=None) # (1)!

return relaxed_slabs

Expand All @@ -499,7 +499,7 @@ graph LR
print(results)
```

1. We didn't need to wrap `bulk_to_slabs_flow` with a decorator because it is already pre-decorated with a `#!Python @flow` decorator. We also chose to set `#!Python slab_static=None` here to disable the static calculation that is normally carried out in this workflow.
1. We didn't need to wrap `bulk_to_slabs_flow` with a decorator because it is already pre-decorated with a `#!Python @flow` decorator. We also chose to set `#!Python run_static=None` here to disable the static calculation that is normally carried out in this workflow.

=== "Redun"

Expand All @@ -517,7 +517,7 @@ graph LR
@flow
def workflow(atoms):
relaxed_bulk = relax_job(atoms)
relaxed_slabs = bulk_to_slabs_flow(relaxed_bulk, slab_static=None) # (1)!
relaxed_slabs = bulk_to_slabs_flow(relaxed_bulk, run_static=None) # (1)!

return relaxed_slabs

Expand All @@ -530,7 +530,7 @@ graph LR
print(result)
```

1. We didn't need to wrap `bulk_to_slabs_flow` with a decorator because it is already pre-decorated with a `#!Python @flow` decorator. We also chose to set `#!Python slab_static=None` here to disable the static calculation that is normally carried out in this workflow.
1. We didn't need to wrap `bulk_to_slabs_flow` with a decorator because it is already pre-decorated with a `#!Python @flow` decorator. We also chose to set `#!Python run_static=None` here to disable the static calculation that is normally carried out in this workflow.

=== "Jobflow"

Expand Down
27 changes: 12 additions & 15 deletions src/quacc/recipes/emt/defects.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from pymatgen.analysis.defects.generators import VacancyGenerator

from quacc import flow, job, subflow
from quacc.recipes.emt.core import relax_job as _relax_job
from quacc.recipes.emt.core import static_job as _static_job
from quacc.recipes.emt.core import relax_job, static_job
from quacc.schemas import fetch_atoms
from quacc.utils.defects import make_defects_from_bulk

Expand Down Expand Up @@ -38,8 +37,7 @@ def bulk_to_defects_flow(
) = VacancyGenerator,
defect_charge: int = 0,
make_defects_kwargs: dict | None = None,
defect_relax: Job | None = _relax_job,
defect_static: Job | None = _static_job,
run_static: bool = True,
defect_relax_kwargs: dict | None = None,
defect_static_kwargs: dict | None = None,
) -> list[RunSchema | OptSchema]:
Expand All @@ -62,10 +60,8 @@ def bulk_to_defects_flow(
Charge state of the defect
make_defects_kwargs
Keyword arguments to pass to the make_defects_from_bulk
defect_relax
Default Job to use for the relaxation of the defect structures.
defect_static
Default Job to use for the static calculation of the defect structures.
run_static
Whether to run the static calculation.
defect_relax_kwargs
Additional keyword arguments to pass to the relaxation calculation.
defect_static_kwargs
Expand Down Expand Up @@ -96,21 +92,22 @@ def _make_defects(atoms):

@subflow
def _relax_distributed(defects):
return [defect_relax(defect, **defect_relax_kwargs) for defect in defects]
return [relax_job(defect, **defect_relax_kwargs) for defect in defects]

@subflow
def _relax_and_static_distributed(defects):
return [
defect_static(
defect_relax(defect, **defect_relax_kwargs),
static_job(
relax_job(defect, **defect_relax_kwargs),
**defect_static_kwargs,
)
for defect in defects
]

defects = _make_defects(atoms)

if defect_static is None:
return _relax_distributed(defects)

return _relax_and_static_distributed(defects)
return (
_relax_and_static_distributed(defects)
if run_static
else _relax_distributed(defects)
)
28 changes: 12 additions & 16 deletions src/quacc/recipes/emt/slabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@
from typing import TYPE_CHECKING

from quacc import flow, job, subflow
from quacc.recipes.emt.core import relax_job as _relax_job
from quacc.recipes.emt.core import static_job as _static_job
from quacc.recipes.emt.core import relax_job, static_job
from quacc.schemas import fetch_atoms
from quacc.utils.slabs import make_max_slabs_from_bulk

if TYPE_CHECKING:
from ase import Atoms

from quacc.schemas.ase import OptSchema, RunSchema
from quacc.utils.wflows import Job


@flow
def bulk_to_slabs_flow(
atoms: Atoms | dict,
make_slabs_kwargs: dict | None = None,
slab_relax: Job = _relax_job,
slab_static: Job | None = _static_job,
run_static: bool = True,
slab_relax_kwargs: dict | None = None,
slab_static_kwargs: dict | None = None,
) -> list[RunSchema | OptSchema]:
Expand All @@ -42,10 +39,8 @@ def bulk_to_slabs_flow(
make_slabs_kwargs
Additional keyword arguments to pass to
`quacc.utils.slabs.make_max_slabs_from_bulk`
slab_relax
Default Job to use for the relaxation of the slab structures.
slab_static
Default Job to use for the static calculation of the slab structures.
run_static
Whether to run the static calculation.
slab_relax_kwargs
Additional keyword arguments to pass to the relaxation calculation.
slab_static_kwargs
Expand All @@ -70,21 +65,22 @@ def _make_slabs(atoms):

@subflow
def _relax_distributed(slabs):
return [slab_relax(slab, **slab_relax_kwargs) for slab in slabs]
return [relax_job(slab, **slab_relax_kwargs) for slab in slabs]

@subflow
def _relax_and_static_distributed(slabs):
return [
slab_static(
slab_relax(slab, **slab_relax_kwargs),
static_job(
relax_job(slab, **slab_relax_kwargs),
**slab_static_kwargs,
)
for slab in slabs
]

slabs = _make_slabs(atoms)

if slab_static is None:
return _relax_distributed(slabs)

return _relax_and_static_distributed(slabs)
return (
_relax_and_static_distributed(slabs)
if run_static
else _relax_distributed(slabs)
)
31 changes: 14 additions & 17 deletions src/quacc/recipes/newtonnet/ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
from monty.dev import requires

from quacc import SETTINGS, job
from quacc.recipes.newtonnet.core import _add_stdev_and_hess
from quacc.recipes.newtonnet.core import freq_job as _freq_job
from quacc.recipes.newtonnet.core import relax_job
from quacc.recipes.newtonnet.core import _add_stdev_and_hess, freq_job, relax_job
from quacc.schemas import fetch_atoms
from quacc.schemas.ase import summarize_opt_run
from quacc.utils.calc import run_ase_opt
Expand All @@ -35,7 +33,6 @@

from quacc.recipes.newtonnet.core import FreqSchema
from quacc.schemas.ase import OptSchema
from quacc.utils.wflows import Job

class TSSchema(OptSchema):
freq: FreqSchema | None
Expand All @@ -54,7 +51,7 @@ class QuasiIRCSchema(OptSchema):
def ts_job(
atoms: Atoms | dict,
use_custom_hessian: bool = False,
freq_job: Job | None = _freq_job,
run_freq: bool = True,
freq_job_kwargs: dict | None = None,
calc_swaps: dict | None = None,
opt_swaps: dict | None = None,
Expand All @@ -68,8 +65,8 @@ def ts_job(
The atoms object representing the system.
use_custom_hessian
Whether to use a custom Hessian matrix.
freq_job
Default Job to use for the frequency analysis.
run_freq
Whether to run the frequency job.
freq_job_kwargs
Keyword arguments to use for the `freq_job`.
calc_swaps
Expand Down Expand Up @@ -144,7 +141,7 @@ def ts_job(

# Run a frequency calculation
freq_summary = (
freq_job.__wrapped__(opt_ts_summary, **freq_job_kwargs) if freq_job else None
freq_job.__wrapped__(opt_ts_summary, **freq_job_kwargs) if run_freq else None
)
opt_ts_summary["freq"] = freq_summary

Expand All @@ -157,7 +154,7 @@ def ts_job(
def irc_job(
atoms: Atoms | dict,
direction: Literal["forward", "reverse"] = "forward",
freq_job: Job | None = _freq_job,
run_freq: bool = True,
freq_job_kwargs: dict | None = None,
calc_swaps: dict | None = None,
opt_swaps: dict | None = None,
Expand All @@ -172,8 +169,8 @@ def irc_job(
The atoms object representing the system.
direction
The direction of the IRC calculation ("forward" or "reverse").
freq_job
Default Job to use for the frequency analysis.
run_freq
Whether to run the frequency analysis.
freq_job_kwargs
Keyword arguments for the `freq_job`.
calc_swaps
Expand Down Expand Up @@ -261,7 +258,7 @@ def irc_job(

# Run frequency job
freq_summary = (
freq_job.__wrapped__(opt_irc_summary, **freq_job_kwargs) if freq_job else None
freq_job.__wrapped__(opt_irc_summary, **freq_job_kwargs) if run_freq else None
)
opt_irc_summary["freq"] = freq_summary

Expand All @@ -274,7 +271,7 @@ def irc_job(
def quasi_irc_job(
atoms: Atoms | dict,
direction: Literal["forward", "reverse"] = "forward",
freq_job: Job | None = _freq_job,
run_freq: bool = True,
freq_job_kwargs: dict | None = None,
irc_swaps: dict | None = None,
opt_swaps: dict | None = None,
Expand All @@ -288,8 +285,8 @@ def quasi_irc_job(
The atoms object representing the system.
direction
The direction of the IRC calculation ("forward" or "reverse").
freq_job
Default Job to use for the frequency analysis.
run_freq
Whether to run the frequency analysis.
freq_job_kwargs
Keyword arguments for `freq_job`.
irc_swaps
Expand Down Expand Up @@ -322,15 +319,15 @@ def quasi_irc_job(

# Run IRC
irc_summary = irc_job.__wrapped__(
atoms, direction=direction, opt_swaps=irc_flags, freq_job=None
atoms, direction=direction, opt_swaps=irc_flags, run_freq=None
)

# Run opt
relax_summary = relax_job.__wrapped__(irc_summary, **opt_swaps)

# Run frequency
freq_summary = (
freq_job.__wrapped__(relax_summary, **freq_job_kwargs) if freq_job else None
freq_job.__wrapped__(relax_summary, **freq_job_kwargs) if run_freq else None
)
relax_summary["freq"] = freq_summary
relax_summary["irc"] = irc_summary
Expand Down
12 changes: 4 additions & 8 deletions src/quacc/recipes/vasp/mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ def mp_relax_job(
@flow
def mp_relax_flow(
atoms: Atoms | dict,
prerelax: Callable | None = mp_prerelax_job,
relax: Callable | None = mp_relax_job,
prerelax_kwargs: dict | None = None,
relax_kwargs: dict | None = None,
) -> MPRelaxFlowSchema:
Expand All @@ -131,10 +129,6 @@ def mp_relax_flow(
----------
atoms
Atoms object for the structure.
prerelax
Default to use for the pre-relaxation.
relax
Default to use for the relaxation.
prerelax_kwargs
Additional keyword arguments to pass to the pre-relaxation calculation.
relax_kwargs
Expand All @@ -149,7 +143,7 @@ def mp_relax_flow(
relax_kwargs = relax_kwargs or {}

# Run the prerelax
prerelax_results = prerelax(atoms, **prerelax_kwargs)
prerelax_results = mp_prerelax_job(atoms, **prerelax_kwargs)

# Update KSPACING arguments
bandgap = prerelax_results["output"].get("bandgap", 0)
Expand All @@ -163,7 +157,9 @@ def mp_relax_flow(
relax_kwargs["calc_swaps"] = kspacing_swaps | relax_kwargs.get("calc_swaps", {})

# Run the relax
relax_results = relax(prerelax_results, copy_files=["WAVECAR"], **relax_kwargs)
relax_results = mp_relax_job(
prerelax_results, copy_files=["WAVECAR"], **relax_kwargs
)
relax_results["prerelax"] = prerelax_results

return relax_results
Loading

0 comments on commit 481e604

Please sign in to comment.