|
| 1 | +"""Create NEB jobs with ASE.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass, field |
| 6 | +from typing import TYPE_CHECKING |
| 7 | + |
| 8 | +from ase.mep.neb import idpp_interpolate, interpolate |
| 9 | +from emmet.core.neb import NebResult |
| 10 | +from jobflow import Flow, Maker, Response, job |
| 11 | +from pymatgen.core import Molecule, Structure |
| 12 | +from pymatgen.io.ase import AseAtomsAdaptor |
| 13 | + |
| 14 | +from atomate2.ase.jobs import _ASE_DATA_OBJECTS, AseMaker |
| 15 | +from atomate2.ase.utils import AseNebInterface |
| 16 | +from atomate2.common.jobs.neb import NebInterpolation, _get_images_from_endpoints |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from pathlib import Path |
| 20 | + from typing import Literal |
| 21 | + |
| 22 | + from ase.atoms import Atoms |
| 23 | + from ase.calculators.calculator import Calculator |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class AseNebFromImagesMaker(AseMaker): |
| 28 | + """Define scheme for performing ASE NEB calculations.""" |
| 29 | + |
| 30 | + name: str = "ASE NEB maker" |
| 31 | + neb_kwargs: dict = field(default_factory=dict) |
| 32 | + fix_symmetry: bool = False |
| 33 | + symprec: float | None = 1e-2 |
| 34 | + steps: int = 500 |
| 35 | + relax_kwargs: dict = field(default_factory=dict) |
| 36 | + optimizer_kwargs: dict = field(default_factory=dict) |
| 37 | + traj_file: str | None = None |
| 38 | + traj_file_fmt: Literal["pmg", "ase", "xdatcar"] = "ase" |
| 39 | + traj_interval: int = 1 |
| 40 | + neb_doc_kwargs: dict = field(default_factory=dict) |
| 41 | + |
| 42 | + def run_ase( |
| 43 | + self, |
| 44 | + images: list[Atoms | Structure | Molecule], |
| 45 | + prev_dir: str | Path | None = None, |
| 46 | + ) -> NebResult: |
| 47 | + """ |
| 48 | + Run an ASE NEB job from a list of images. |
| 49 | +
|
| 50 | + Parameters |
| 51 | + ---------- |
| 52 | + images: list of pymatgen .Molecule or .Structure |
| 53 | + pymatgen molecule or structure images |
| 54 | + prev_dir : str or Path or None |
| 55 | + A previous calculation directory to copy output files from. Unused, just |
| 56 | + added to match the method signature of other makers. |
| 57 | + """ |
| 58 | + return AseNebInterface( |
| 59 | + calculator=self.calculator, |
| 60 | + fix_symmetry=self.fix_symmetry, |
| 61 | + symprec=self.symprec, |
| 62 | + ).run_neb( |
| 63 | + images, |
| 64 | + steps=self.steps, |
| 65 | + traj_file=self.traj_file, |
| 66 | + traj_file_fmt=self.traj_file_fmt, |
| 67 | + interval=self.traj_interval, |
| 68 | + neb_doc_kwargs=self.neb_doc_kwargs, |
| 69 | + neb_kwargs=self.neb_kwargs, |
| 70 | + optimizer_kwargs=self.optimizer_kwargs, |
| 71 | + **self.relax_kwargs, |
| 72 | + ) |
| 73 | + |
| 74 | + @job(data=_ASE_DATA_OBJECTS, schema=NebResult) |
| 75 | + def make( |
| 76 | + self, |
| 77 | + images: list[Structure | Molecule], |
| 78 | + prev_dir: str | Path | None = None, |
| 79 | + ) -> NebResult: |
| 80 | + """ |
| 81 | + Run an ASE NEB job from a list of images. |
| 82 | +
|
| 83 | + Parameters |
| 84 | + ---------- |
| 85 | + images: list of pymatgen .Molecule or .Structure |
| 86 | + pymatgen molecule or structure images |
| 87 | + prev_dir : str or Path or None |
| 88 | + A previous calculation directory to copy output files from. Unused, just |
| 89 | + added to match the method signature of other makers. |
| 90 | + """ |
| 91 | + # Note that images are copied to prevent them from being overwritten |
| 92 | + # by ASE during the NEB run |
| 93 | + return self.run_ase([image.copy() for image in images], prev_dir=prev_dir) |
| 94 | + |
| 95 | + |
| 96 | +@dataclass |
| 97 | +class AseNebFromEndpointsMaker(AseNebFromImagesMaker): |
| 98 | + """Maker to create ASE NEB jobs from two endpoints. |
| 99 | +
|
| 100 | + Optionally relax the two endpoints and return a full NEB hop analysis. |
| 101 | + If a maker to relax the endpoints is not specified, this job |
| 102 | + interpolates the provided endpoints and performs NEB on the |
| 103 | + interpolated images. |
| 104 | +
|
| 105 | + Parameters |
| 106 | + ---------- |
| 107 | + endpoint_relax_maker : Maker or None (default) |
| 108 | + Optional maker to initially relax the endpoints. |
| 109 | + """ |
| 110 | + |
| 111 | + endpoint_relax_maker: Maker | None = None |
| 112 | + |
| 113 | + @job |
| 114 | + def interpolate_endpoints( |
| 115 | + self, |
| 116 | + endpoints: tuple[Structure | Molecule, Structure | Molecule], |
| 117 | + num_images: int, |
| 118 | + interpolation_method: NebInterpolation | str = NebInterpolation.LINEAR, |
| 119 | + **interpolation_kwargs, |
| 120 | + ) -> list[Atoms]: |
| 121 | + """ |
| 122 | + Interpolate between two endpoints using ASE's methods, as a job. |
| 123 | +
|
| 124 | + Note that `num_images` specifies the number of intermediate images |
| 125 | + between two endpoints. Thus, specifying `num_images = 5` will return |
| 126 | + the endpoints and 5 intermediate images. |
| 127 | +
|
| 128 | + Parameters |
| 129 | + ---------- |
| 130 | + endpoints : tuple[Structure,Structure] or list[Structure] |
| 131 | + A set of two endpoints to interpolate NEB images from. |
| 132 | + num_images : int |
| 133 | + The number of images to include in the interpolation. |
| 134 | + interpolation_method : .NebInterpolation |
| 135 | + The method to use to interpolate between images. |
| 136 | + **interpolation_kwargs |
| 137 | + kwargs to pass to the interpolation function. |
| 138 | + """ |
| 139 | + # return interpolate_endpoints_ase( |
| 140 | + # endpoints, num_images, interpolation_method, **interpolation_kwargs |
| 141 | + # ) |
| 142 | + interpolated = _get_images_from_endpoints( |
| 143 | + endpoints, |
| 144 | + num_images, |
| 145 | + interpolation_method=interpolation_method, |
| 146 | + **interpolation_kwargs, |
| 147 | + ) |
| 148 | + adaptor = AseAtomsAdaptor() |
| 149 | + return [adaptor.get_atoms(image) for image in interpolated] |
| 150 | + |
| 151 | + @job(data=_ASE_DATA_OBJECTS) |
| 152 | + def make( |
| 153 | + self, |
| 154 | + endpoints: tuple[Structure | Molecule, Structure | Molecule] |
| 155 | + | list[Structure | Molecule], |
| 156 | + num_images: int, |
| 157 | + prev_dir: str | Path = None, |
| 158 | + interpolation_method: NebInterpolation | str = NebInterpolation.LINEAR, |
| 159 | + **interpolation_kwargs, |
| 160 | + ) -> Flow: |
| 161 | + """ |
| 162 | + Make an NEB job from a set of endpoints. |
| 163 | +
|
| 164 | + Parameters |
| 165 | + ---------- |
| 166 | + endpoints : tuple[Structure,Structure] or list[Structure] |
| 167 | + A set of two endpoints to interpolate NEB images from. |
| 168 | + num_images : int |
| 169 | + The number of images to include in the interpolation. |
| 170 | + prev_dir : str or Path or None (default) |
| 171 | + A previous directory to copy outputs from. |
| 172 | + interpolation_method : .NebInterpolation |
| 173 | + The method to use to interpolate between images. |
| 174 | + **interpolation_kwargs |
| 175 | + kwargs to pass to the interpolation function. |
| 176 | + """ |
| 177 | + if len(endpoints) != 2: |
| 178 | + raise ValueError("Please specify exactly two endpoint structures.") |
| 179 | + |
| 180 | + endpoint_jobs = [] |
| 181 | + if self.endpoint_relax_maker is not None: |
| 182 | + endpoint_jobs += [ |
| 183 | + self.endpoint_relax_maker.make(endpoint, prev_dir=prev_dir) |
| 184 | + for endpoint in endpoints |
| 185 | + ] |
| 186 | + for idx in range(2): |
| 187 | + endpoint_jobs[idx].append_name(f" endpoint {idx + 1}") |
| 188 | + endpoints = [relax_job.output.structure for relax_job in endpoint_jobs] |
| 189 | + |
| 190 | + get_images = self.interpolate_endpoints( |
| 191 | + endpoints, |
| 192 | + num_images, |
| 193 | + interpolation_method=interpolation_method, |
| 194 | + **interpolation_kwargs, |
| 195 | + ) |
| 196 | + |
| 197 | + neb_from_images = super().make(get_images.output) |
| 198 | + |
| 199 | + flow = Flow( |
| 200 | + [*endpoint_jobs, get_images, neb_from_images], |
| 201 | + output=neb_from_images.output, |
| 202 | + ) |
| 203 | + |
| 204 | + return Response(replace=flow, output=neb_from_images.output) |
| 205 | + |
| 206 | + |
| 207 | +def interpolate_endpoints_ase( |
| 208 | + endpoints: tuple[Structure | Molecule | Atoms, Structure | Molecule | Atoms], |
| 209 | + num_images: int, |
| 210 | + interpolation_method: NebInterpolation | str = NebInterpolation.LINEAR, |
| 211 | + **interpolation_kwargs, |
| 212 | +) -> list[Atoms]: |
| 213 | + """ |
| 214 | + Interpolate between two endpoints using ASE's methods. |
| 215 | +
|
| 216 | + Note that `num_images` specifies the number of intermediate images |
| 217 | + between two endpoints. Thus, specifying `num_images = 5` will return |
| 218 | + the endpoints and 5 intermediate images. |
| 219 | +
|
| 220 | + Parameters |
| 221 | + ---------- |
| 222 | + endpoints : tuple[Structure,Structure] or list[Structure] |
| 223 | + A set of two endpoints to interpolate NEB images from. |
| 224 | + num_images : int |
| 225 | + The number of images to include in the interpolation. |
| 226 | + interpolation_method : .NebInterpolation |
| 227 | + The method to use to interpolate between images. |
| 228 | + **interpolation_kwargs |
| 229 | + kwargs to pass to the interpolation function. |
| 230 | +
|
| 231 | + Returns |
| 232 | + ------- |
| 233 | + list of Atoms : the atoms interpolated between endpoints. |
| 234 | + """ |
| 235 | + endpoint_atoms = [ |
| 236 | + AseAtomsAdaptor().get_atoms(ions) |
| 237 | + if isinstance(ions, Structure | Molecule) |
| 238 | + else ions.copy() |
| 239 | + for ions in endpoints |
| 240 | + ] |
| 241 | + images = [ |
| 242 | + endpoint_atoms[0], |
| 243 | + *[endpoint_atoms[0].copy() for _ in range(num_images)], |
| 244 | + endpoint_atoms[1], |
| 245 | + ] |
| 246 | + |
| 247 | + interp_method = NebInterpolation(interpolation_method) |
| 248 | + if interp_method == NebInterpolation.LINEAR: |
| 249 | + interpolate(images, **interpolation_kwargs) |
| 250 | + elif interp_method == NebInterpolation.IDPP: |
| 251 | + idpp_interpolate(images, **interpolation_kwargs) |
| 252 | + return images |
| 253 | + |
| 254 | + |
| 255 | +class EmtNebFromImagesMaker(AseNebFromImagesMaker): |
| 256 | + """EMT NEB from images maker.""" |
| 257 | + |
| 258 | + name: str = "EMT NEB from images maker" |
| 259 | + |
| 260 | + @property |
| 261 | + def calculator(self) -> Calculator: |
| 262 | + """EMT calculator.""" |
| 263 | + from ase.calculators.emt import EMT |
| 264 | + |
| 265 | + return EMT(**self.calculator_kwargs) |
0 commit comments