|
| 1 | +"""Define common QHA flow agnostic to electronic-structure code.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import warnings |
| 6 | +from abc import ABC, abstractmethod |
| 7 | +from dataclasses import dataclass, field |
| 8 | +from typing import TYPE_CHECKING, Literal |
| 9 | + |
| 10 | +from jobflow import Flow, Maker |
| 11 | + |
| 12 | +from atomate2.common.flows.eos import CommonEosMaker |
| 13 | +from atomate2.common.jobs.qha import analyze_free_energy, get_phonon_jobs |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from pathlib import Path |
| 17 | + |
| 18 | + from pymatgen.core import Structure |
| 19 | + |
| 20 | + from atomate2.common.flows.phonons import BasePhononMaker |
| 21 | + from atomate2.forcefields.jobs import ForceFieldRelaxMaker |
| 22 | + from atomate2.vasp.jobs.core import BaseVaspMaker |
| 23 | + |
| 24 | +supported_eos = frozenset(("vinet", "birch_murnaghan", "murnaghan")) |
| 25 | + |
| 26 | + |
| 27 | +@dataclass |
| 28 | +class CommonQhaMaker(Maker, ABC): |
| 29 | + """ |
| 30 | + Use the quasi-harmonic approximation. |
| 31 | +
|
| 32 | + First relax a structure. |
| 33 | + Then we scale the relaxed structure, and |
| 34 | + then compute harmonic phonons for each scaled |
| 35 | + structure with Phonopy. |
| 36 | + Finally, we compute the Gibb's free energy and |
| 37 | + other thermodynamic properties available from |
| 38 | + the quasi-harmonic approximation. |
| 39 | +
|
| 40 | + Note: We do not consider electronic free energies so far. |
| 41 | + This might be problematic for metals (see e.g., |
| 42 | + Wolverton and Zunger, Phys. Rev. B, 52, 8813 (1994).) |
| 43 | +
|
| 44 | + Note: Magnetic Materials have never been computed with |
| 45 | + this workflow. |
| 46 | +
|
| 47 | + Parameters |
| 48 | + ---------- |
| 49 | + name: str |
| 50 | + Name of the flows produced by this maker. |
| 51 | + initial_relax_maker: .ForceFieldRelaxMaker | .BaseVaspMaker | None |
| 52 | + Maker to relax the input structure. |
| 53 | + eos_relax_maker: .ForceFieldRelaxMaker | .BaseVaspMaker | None |
| 54 | + Maker to relax deformed structures for the EOS fit. |
| 55 | + The volume has to be fixed! |
| 56 | + phonon_maker: .BasePhononMaker | None |
| 57 | + Maker to compute phonons. The volume has to be fixed! |
| 58 | + The beforehand relaxation could be switched off. |
| 59 | + linear_strain: tuple[float, float] |
| 60 | + Percentage linear strain to apply as a deformation, default = -5% to 5%. |
| 61 | + number_of_frames: int |
| 62 | + Number of strain calculations to do for EOS fit, default = 6. |
| 63 | + t_max: float | None |
| 64 | + Maximum temperature until which the QHA will be performed |
| 65 | + pressure: float | None |
| 66 | + Pressure at which the QHA will be performed (default None, no pressure) |
| 67 | + skip_analysis: bool |
| 68 | + Skips the analysis step and only performs EOS and phonon computations. |
| 69 | + ignore_imaginary_modes: bool |
| 70 | + By default, volumes where the harmonic phonon approximation shows imaginary |
| 71 | + will be ignored |
| 72 | + eos_type: str |
| 73 | + Equation of State type used for the fitting. Defaults to vinet. |
| 74 | + """ |
| 75 | + |
| 76 | + name: str = "QHA Maker" |
| 77 | + initial_relax_maker: ForceFieldRelaxMaker | BaseVaspMaker | None = None |
| 78 | + eos_relax_maker: ForceFieldRelaxMaker | BaseVaspMaker | None = None |
| 79 | + phonon_maker: BasePhononMaker | None = None |
| 80 | + linear_strain: tuple[float, float] = (-0.05, 0.05) |
| 81 | + number_of_frames: int = 6 |
| 82 | + t_max: float | None = None |
| 83 | + pressure: float | None = None |
| 84 | + ignore_imaginary_modes: bool = False |
| 85 | + skip_analysis: bool = False |
| 86 | + eos_type: Literal["vinet", "birch_murnaghan", "murnaghan"] = "vinet" |
| 87 | + analyze_free_energy_kwargs: dict = field(default_factory=dict) |
| 88 | + # TODO: implement advanced handling of |
| 89 | + # imaginary modes in phonon runs (i.e., fitting procedures) |
| 90 | + |
| 91 | + def make(self, structure: Structure, prev_dir: str | Path = None) -> Flow: |
| 92 | + """Run an EOS flow. |
| 93 | +
|
| 94 | + Parameters |
| 95 | + ---------- |
| 96 | + structure : Structure |
| 97 | + A pymatgen structure object. |
| 98 | + prev_dir : str or Path or None |
| 99 | + A previous calculation directory to copy output files from. |
| 100 | +
|
| 101 | + Returns |
| 102 | + ------- |
| 103 | + .Flow, a QHA flow |
| 104 | + """ |
| 105 | + if self.eos_type not in supported_eos: |
| 106 | + raise ValueError( |
| 107 | + "EOS not supported.", |
| 108 | + "Please choose 'vinet', 'birch_murnaghan', 'murnaghan'", |
| 109 | + ) |
| 110 | + |
| 111 | + qha_jobs = [] |
| 112 | + |
| 113 | + # In this way, one can easily exchange makers and enforce postprocessor None |
| 114 | + self.eos = CommonEosMaker( |
| 115 | + initial_relax_maker=self.initial_relax_maker, |
| 116 | + eos_relax_maker=self.eos_relax_maker, |
| 117 | + static_maker=None, |
| 118 | + postprocessor=None, |
| 119 | + number_of_frames=self.number_of_frames, |
| 120 | + ) |
| 121 | + |
| 122 | + eos_job = self.eos.make(structure) |
| 123 | + qha_jobs.append(eos_job) |
| 124 | + |
| 125 | + phonon_jobs = get_phonon_jobs( |
| 126 | + phonon_maker=self.phonon_maker, eos_output=eos_job.output |
| 127 | + ) |
| 128 | + qha_jobs.append(phonon_jobs) |
| 129 | + if not self.skip_analysis: |
| 130 | + analysis = analyze_free_energy( |
| 131 | + phonon_jobs.output, |
| 132 | + structure=structure, |
| 133 | + t_max=self.t_max, |
| 134 | + pressure=self.pressure, |
| 135 | + ignore_imaginary_modes=self.ignore_imaginary_modes, |
| 136 | + eos_type=self.eos_type, |
| 137 | + **self.analyze_free_energy_kwargs, |
| 138 | + ) |
| 139 | + qha_jobs.append(analysis) |
| 140 | + |
| 141 | + return Flow(qha_jobs) |
| 142 | + |
| 143 | + def __post_init__(self) -> None: |
| 144 | + """Test settings during the initialisation.""" |
| 145 | + if self.phonon_maker.bulk_relax_maker is not None: |
| 146 | + warnings.warn( |
| 147 | + "An additional bulk_relax_maker has been added " |
| 148 | + "to the phonon workflow. Please be aware " |
| 149 | + "that the volume needs to be kept fixed.", |
| 150 | + stacklevel=2, |
| 151 | + ) |
| 152 | + # if self.phonon_maker.symprec != self.symprec: |
| 153 | + # warnings.warn( |
| 154 | + # "You are using different symmetry precisions " |
| 155 | + # "in the phonon makers and other parts of the " |
| 156 | + # "QHA workflow.", |
| 157 | + # stacklevel=2, |
| 158 | + # ) |
| 159 | + if self.phonon_maker.static_energy_maker is None: |
| 160 | + warnings.warn( |
| 161 | + "A static energy maker " |
| 162 | + "is needed for " |
| 163 | + "this workflow." |
| 164 | + " Please add the static_energy_maker.", |
| 165 | + stacklevel=2, |
| 166 | + ) |
| 167 | + |
| 168 | + @property |
| 169 | + @abstractmethod |
| 170 | + def prev_calc_dir_argname(self) -> str | None: |
| 171 | + """Name of argument informing static maker of previous calculation directory. |
| 172 | +
|
| 173 | + As this differs between different DFT codes (e.g., VASP, CP2K), it |
| 174 | + has been left as a property to be implemented by the inheriting class. |
| 175 | +
|
| 176 | + Note: this is only applicable if a relax_maker is specified; i.e., two |
| 177 | + calculations are performed for each ordering (relax -> static) |
| 178 | + """ |
0 commit comments