Skip to content

Commit

Permalink
Remove redundant call to get_phonopy and fix bug in common phonon s…
Browse files Browse the repository at this point in the history
…ubflow (#2105)

This PR removes the redundant call to `get_phonopy()` in
`quacc.recipes.common.phonons`. It also fixes a bug (?) where the
`Phonopy` object was constructed based on the _unrelaxed_ structure
rather than the relaxed structure.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Andrew-S-Rosen and pre-commit-ci[bot] authored May 9, 2024
1 parent 98289ae commit a095785
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 36 deletions.
64 changes: 31 additions & 33 deletions src/quacc/recipes/common/phonons.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
from quacc import Job
from quacc.schemas._aliases.phonons import PhononSchema

if has_deps:
from phonopy import Phonopy


@subflow
@requires(
Expand All @@ -30,7 +33,6 @@
def phonon_subflow(
atoms: Atoms,
force_job: Job,
relax_job: Job | None = None,
symprec: float = 1e-4,
min_lengths: float | tuple[float, float, float] | None = 20.0,
supercell_matrix: (
Expand All @@ -52,8 +54,6 @@ def phonon_subflow(
Atoms object with calculator attached.
force_job
The static job to calculate the forces.
relax_job
The job used to relax the structure before calculating the forces.
symprec
Precision for symmetry detection.
min_lengths
Expand All @@ -72,58 +72,56 @@ def phonon_subflow(
phonopy_kwargs
Additional kwargs to pass to the Phonopy class.
additional_fields
Additional fields to store in the database.
Additional fields to add to the output schema.
Returns
-------
PhononSchema
Dictionary of results from [quacc.schemas.phonons.summarize_phonopy][]
"""

phonon = get_phonopy(
atoms,
min_lengths=min_lengths,
supercell_matrix=supercell_matrix,
symprec=symprec,
displacement=displacement,
phonopy_kwargs=phonopy_kwargs,
)
supercells = [
phonopy_atoms_to_ase_atoms(s) for s in phonon.supercells_with_displacements
]

@subflow
def _get_forces_subflow(supercells: list[Atoms]) -> list[dict]:
return [
force_job(supercell) for supercell in supercells if supercell is not None
]

@job
def _thermo_job(atoms: Atoms, force_job_results: list[dict]) -> PhononSchema:
phonon = get_phonopy(
atoms,
min_lengths=min_lengths,
supercell_matrix=supercell_matrix,
symprec=symprec,
displacement=displacement,
phonopy_kwargs=phonopy_kwargs,
)
def _thermo_job(
atoms: Atoms,
phonopy: Phonopy,
force_job_results: list[dict],
t_step: float,
t_min: float,
t_max: float,
additional_fields: dict[str, Any] | None,
) -> PhononSchema:
parameters = force_job_results[-1].get("parameters")
forces = [output["results"]["forces"] for output in force_job_results]
phonon_results = run_phonopy(
phonon, forces, t_step=t_step, t_min=t_min, t_max=t_max
phonopy_results = run_phonopy(
phonopy, forces, t_step=t_step, t_min=t_min, t_max=t_max
)

return summarize_phonopy(
phonon,
phonopy,
atoms,
phonon_results.directory,
phonopy_results.directory,
parameters=parameters,
additional_fields=additional_fields,
)

if relax_job is not None:
atoms = relax_job(atoms)["atoms"]

phonopy = get_phonopy(
atoms,
min_lengths=min_lengths,
supercell_matrix=supercell_matrix,
symprec=symprec,
displacement=displacement,
phonopy_kwargs=phonopy_kwargs,
)
supercells = [
phonopy_atoms_to_ase_atoms(s) for s in phonopy.supercells_with_displacements
]
force_job_results = _get_forces_subflow(supercells)
return _thermo_job(atoms, force_job_results)
return _thermo_job(
atoms, phonopy, force_job_results, t_step, t_min, t_max, additional_fields
)
3 changes: 2 additions & 1 deletion src/quacc/recipes/emt/phonons.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ def phonon_flow(
parameters=job_params,
decorators=job_decorators,
)
if run_relax:
atoms = relax_job_(atoms)["atoms"]

return phonon_subflow(
atoms,
static_job_,
relax_job=relax_job_ if run_relax else None,
symprec=symprec,
min_lengths=min_lengths,
supercell_matrix=supercell_matrix,
Expand Down
3 changes: 2 additions & 1 deletion src/quacc/recipes/mlp/phonons.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ def phonon_flow(
parameters=job_params,
decorators=job_decorators,
)
if run_relax:
atoms = relax_job_(atoms)["atoms"]

return phonon_subflow(
atoms,
static_job_,
relax_job=relax_job_ if run_relax else None,
symprec=symprec,
min_lengths=min_lengths,
supercell_matrix=supercell_matrix,
Expand Down
3 changes: 2 additions & 1 deletion src/quacc/recipes/tblite/phonons.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ def phonon_flow(
parameters=job_params,
decorators=job_decorators,
)
if run_relax:
atoms = relax_job_(atoms)["atoms"]

return phonon_subflow(
atoms,
static_job_,
relax_job=relax_job_ if run_relax else None,
symprec=symprec,
min_lengths=min_lengths,
supercell_matrix=supercell_matrix,
Expand Down

0 comments on commit a095785

Please sign in to comment.