File size: 9,407 Bytes
e59bc30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
Defines nudged elastic band (NEB) task

This module has been modified from MatCalc
https://github.com/materialsvirtuallab/matcalc/blob/main/src/matcalc/neb.py

https://github.com/materialsvirtuallab/matcalc/blob/main/LICENSE

BSD 3-Clause License

Copyright (c) 2023, Materials Virtual Lab

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal

from prefect import task
from prefect.cache_policies import INPUTS, TASK_SOURCE
from prefect.runtime import task_run
from prefect.states import State

from ase import Atoms
from ase.filters import *  # type: ignore
from ase.mep.neb import NEB, NEBTools
from ase.optimize import *  # type: ignore
from ase.optimize.optimize import Optimizer
from ase.utils.forcecurve import fit_images
from mlip_arena.models import MLIPEnum
from mlip_arena.tasks.optimize import run as OPT
from mlip_arena.tasks.utils import get_calculator
from pymatgen.io.ase import AseAtomsAdaptor

if TYPE_CHECKING:
    pass

_valid_optimizers: dict[str, Optimizer] = {
    "MDMin": MDMin,
    "FIRE": FIRE,
    "FIRE2": FIRE2,
    "LBFGS": LBFGS,
    "LBFGSLineSearch": LBFGSLineSearch,
    "BFGS": BFGS,
    # "BFGSLineSearch": BFGSLineSearch, # NEB does not support BFGSLineSearch
    "QuasiNewton": QuasiNewton,
    "GPMin": GPMin,
    "CellAwareBFGS": CellAwareBFGS,
    "ODE12r": ODE12r,
}  # type: ignore


def _generate_task_run_name():
    task_name = task_run.task_name
    parameters = task_run.parameters

    if "images" in parameters:
        atoms = parameters["images"][0]
    elif "start" in parameters:
        atoms = parameters["start"]
    else:
        raise ValueError("No images or start atoms found in parameters")
    
    calculator_name = parameters["calculator_name"]

    return f"{task_name}: {atoms.get_chemical_formula()} - {calculator_name}"


@task(
    name="NEB from images",
    task_run_name=_generate_task_run_name,
    cache_policy=TASK_SOURCE + INPUTS,
)
def run(
    images: list[Atoms],
    calculator_name: str | MLIPEnum,
    calculator_kwargs: dict | None = None,
    dispersion: str | None = None,
    dispersion_kwargs: dict | None = None,
    device: str | None = None,
    optimizer: Optimizer | str = "MDMin",  # type: ignore
    optimizer_kwargs: dict | None = None,
    criterion: dict | None = None,
    interpolation: Literal["linear", "idpp"] = "idpp",
    climb: bool = True,
    traj_file: str | Path | None = None,
) -> dict[str, Any] | State:
    """Run the nudged elastic band (NEB) calculation.

    Args:
        images (list[Atoms]): The images.
        calculator_name (str | MLIPEnum): The calculator name.
        calculator_kwargs (dict, optional): The calculator kwargs. Defaults to None.
        dispersion (str, optional): The dispersion. Defaults to None.
        dispersion_kwargs (dict, optional): The dispersion kwargs. Defaults to None.
        device (str, optional): The device. Defaults to None.
        optimizer (Optimizer | str, optional): The optimizer. Defaults to "BFGSLineSearch".
        optimizer_kwargs (dict, optional): The optimizer kwargs. Defaults to None.
        criterion (dict, optional): The criterion. Defaults to None.
        interpolation (Literal['linear', 'idpp'], optional): The interpolation method. Defaults to "idpp".
        climb (bool, optional): Whether to use the climbing image. Defaults to True.
        traj_file (str | Path, optional): The trajectory file. Defaults to None.

    Returns:
        dict[str, Any] | State: The energy barrier.
    """

    calc = get_calculator(
        calculator_name,
        calculator_kwargs,
        dispersion=dispersion,
        dispersion_kwargs=dispersion_kwargs,
        device=device,
    )

    for image in images:
        assert isinstance(image, Atoms)
        image.calc = calc

    neb = NEB(images, climb=climb, allow_shared_calculator=True)

    neb.interpolate(method=interpolation)

    if isinstance(optimizer, str):
        if optimizer not in _valid_optimizers:
            raise ValueError(f"Invalid optimizer: {optimizer}")
        optimizer = _valid_optimizers[optimizer]

    optimizer_kwargs = optimizer_kwargs or {}
    criterion = criterion or {}

    optimizer_instance = optimizer(neb, trajectory=traj_file, **optimizer_kwargs)  # type: ignore

    optimizer_instance.run(**criterion)

    neb_tool = NEBTools(neb.images)

    return {
        "barrier": neb_tool.get_barrier(),
        "images": neb.images,
        "forcefit": fit_images(neb.images),
    }


@task(
    name="NEB from end points",
    task_run_name=_generate_task_run_name,
    cache_policy=TASK_SOURCE + INPUTS,
)
def run_from_end_points(
    start: Atoms,
    end: Atoms,
    n_images: int,
    calculator_name: str | MLIPEnum,
    calculator_kwargs: dict | None = None,
    dispersion: str | None = None,
    dispersion_kwargs: dict | None = None,
    device: str | None = None,
    optimizer: Optimizer | str = "BFGS",  # type: ignore
    optimizer_kwargs: dict | None = None,
    criterion: dict | None = None,
    relax_end_points: bool = True,
    interpolation: Literal["linear", "idpp"] = "idpp",
    climb: bool = True,
    traj_file: str | Path | None = None,
) -> dict[str, Any] | State:
    """Run the nudged elastic band (NEB) calculation from end points.

    Args:
        start (Atoms): The start image.
        end (Atoms): The end image.
        n_images (int): The number of images.
        calculator_name (str | MLIPEnum): The calculator name.
        calculator_kwargs (dict, optional): The calculator kwargs. Defaults to None.
        dispersion (str, optional): The dispersion. Defaults to None.
        dispersion_kwargs (dict, optional): The dispersion kwargs. Defaults to None.
        device (str, optional): The device. Defaults to None.
        optimizer (Optimizer | str, optional): The optimizer. Defaults to "BFGSLineSearch".
        optimizer_kwargs (dict, optional): The optimizer kwargs. Defaults to None.
        criterion (dict, optional): The criterion. Defaults to None.
        interpolation (Literal['linear', 'idpp'], optional): The interpolation method. Defaults to "idpp".
        climb (bool, optional): Whether to use the climbing image. Defaults to True.
        traj_file (str | Path, optional): The trajectory file. Defaults to None.

    Returns:
        dict[str, Any] | State: The energy barrier.
    """

    if relax_end_points:
        relax = OPT(
            atoms=start.copy(),
            calculator_name=calculator_name,
            calculator_kwargs=calculator_kwargs,
            dispersion=dispersion,
            dispersion_kwargs=dispersion_kwargs,
            device=device,
            optimizer=optimizer,
            optimizer_kwargs=optimizer_kwargs,
            criterion=criterion,
        )
        start = relax["atoms"]

        relax = OPT(
            atoms=end.copy(),
            calculator_name=calculator_name,
            calculator_kwargs=calculator_kwargs,
            dispersion=dispersion,
            dispersion_kwargs=dispersion_kwargs,
            device=device,
            optimizer=optimizer,
            optimizer_kwargs=optimizer_kwargs,
            criterion=criterion,
        )
        end = relax["atoms"]

    path = (
        AseAtomsAdaptor()
        .get_structure(start)
        .interpolate(
            AseAtomsAdaptor().get_structure(end),
            nimages=n_images - 1,
            interpolate_lattices=False,
            pbc=False,
            autosort_tol=0.5,
        )
    )

    images = [s.to_ase_atoms() for s in path]

    return run(
        images,
        calculator_name,
        calculator_kwargs=calculator_kwargs,
        dispersion=dispersion,
        dispersion_kwargs=dispersion_kwargs,
        device=device,
        optimizer=optimizer,
        optimizer_kwargs=optimizer_kwargs,
        criterion=criterion,
        interpolation=interpolation,
        climb=climb,
        traj_file=traj_file,
    )