arrayff 0.0.1__tar.gz → 0.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. {arrayff-0.0.1 → arrayff-0.0.2}/PKG-INFO +11 -6
  2. {arrayff-0.0.1 → arrayff-0.0.2}/README.md +10 -5
  3. {arrayff-0.0.1 → arrayff-0.0.2}/pyproject.toml +1 -1
  4. {arrayff-0.0.1 → arrayff-0.0.2}/pyproject.toml.orig +1 -1
  5. arrayff-0.0.2/src/arrayff/optimize/__init__.py +14 -0
  6. arrayff-0.0.2/src/arrayff/optimize/_dimer.py +122 -0
  7. arrayff-0.0.2/src/arrayff/optimize/_helper.py +179 -0
  8. arrayff-0.0.2/src/arrayff/optimize/_neb.py +229 -0
  9. {arrayff-0.0.1 → arrayff-0.0.2}/src/LICENSE +0 -0
  10. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/__init__.py +0 -0
  11. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/electrostatics/__init__.py +0 -0
  12. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/electrostatics/calculator.py +0 -0
  13. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/electrostatics/placeholders.py +0 -0
  14. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/electrostatics/qeq.py +0 -0
  15. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/neighbors/__init__.py +0 -0
  16. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/neighbors/_list.py +0 -0
  17. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/neighbors/_table.py +0 -0
  18. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/__init__.py +0 -0
  19. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/_torch.py +0 -0
  20. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/base.py +0 -0
  21. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/calculator.py +0 -0
  22. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/composite.py +0 -0
  23. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/eam/__init__.py +0 -0
  24. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/eam/_torch.py +0 -0
  25. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/eam/calculator.py +0 -0
  26. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/eam/eam.py +0 -0
  27. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/eam/io/__init__.py +0 -0
  28. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/eam/io/eam.py +0 -0
  29. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/emt/__init__.py +0 -0
  30. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/emt/_torch.py +0 -0
  31. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/emt/calculator.py +0 -0
  32. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/emt/emt.py +0 -0
  33. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/pairwise/__init__.py +0 -0
  34. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/pairwise/_pairs.py +0 -0
  35. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/pairwise/_torch.py +0 -0
  36. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/pairwise/base.py +0 -0
  37. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/pairwise/calculator.py +0 -0
  38. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/pairwise/forms.py +0 -0
  39. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/pairwise/lj.py +0 -0
  40. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/pairwise/lj93.py +0 -0
  41. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/pairwise/lj_kim.py +0 -0
  42. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/pairwise/morse.py +0 -0
  43. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/pairwise/params.py +0 -0
  44. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/placeholders.py +0 -0
  45. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/potentials/registry.py +0 -0
  46. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/py.typed +0 -0
  47. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/utils/__init__.py +0 -0
  48. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/utils/ad.py +0 -0
  49. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/utils/ase.py +0 -0
  50. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/utils/backend.py +0 -0
  51. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/utils/dispatch.py +0 -0
  52. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/utils/exceptions.py +0 -0
  53. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/utils/interp.py +0 -0
  54. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/utils/jit.py +0 -0
  55. {arrayff-0.0.1 → arrayff-0.0.2}/src/arrayff/utils/optional.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: arrayff
3
- Version: 0.0.1
3
+ Version: 0.0.2
4
4
  Summary: The Force Field Library Based on Multiple Array Backends like NumPy, PyTorch and JAX.
5
5
  Keywords: chemistry,Force Field
6
6
  Author: LiuGaoyong
@@ -32,6 +32,11 @@ Description-Content-Type: text/markdown
32
32
 
33
33
  # ArrayFF
34
34
 
35
+ [![Conda Version](https://img.shields.io/conda/vn/conda-forge/arrayff.svg)](https://anaconda.org/conda-forge/arrayff)
36
+ [![Conda Downloads](https://img.shields.io/conda/dn/conda-forge/arrayff.svg)](https://anaconda.org/conda-forge/arrayff)
37
+ [![Pypi version](https://img.shields.io/pypi/v/arrayff)](https://pypi.org/project/arrayff/)
38
+ [![PyPI Downloads](https://static.pepy.tech/badge/arrayff)](https://pepy.tech/projects/arrayff)
39
+
35
40
  The Force Field Library Based on Multiple Array Backends like NumPy, PyTorch
36
41
  and JAX.
37
42
 
@@ -60,17 +65,17 @@ from arrayff.potentials import EMT, EMTCalculator
60
65
 
61
66
  atoms = bulk("Cu", "fcc", a=3.6, cubic=True).repeat((2, 2, 2))
62
67
 
63
- calc = EMT() # numpy backend + autograd for forces
68
+ calc = EMT() # numpy backend + autograd for forces
64
69
  energy, forces = calc.energy_and_forces(atoms, method="analytic")
65
70
 
66
- atoms.calc = EMTCalculator() # ASE calculator interface
71
+ atoms.calc = EMTCalculator() # ASE calculator interface
67
72
  energy = atoms.get_potential_energy()
68
73
  ```
69
74
 
70
75
  Backends are selected at construction:
71
76
 
72
77
  ```python
73
- calc = EMT(backend="torch") # or "jax"
78
+ calc = EMT(backend="torch") # or "jax"
74
79
  calc = EMT(backend="jax", jit=True)
75
80
  ```
76
81
 
@@ -98,7 +103,7 @@ PyTorch are the useful backends; NumPy's compiler is best-effort).
98
103
  from arrayff.electrostatics import QEq
99
104
 
100
105
  qeq = QEq(chi={8: 8.5, 1: 4.5}, J={8: 10.0, 1: 12.0}, shielding=1.0)
101
- charges = qeq.charges(atoms) # sum == total charge
106
+ charges = qeq.charges(atoms) # sum == total charge
102
107
 
103
108
  # additive composite: energy/forces/stress add, charges come from QEq
104
109
  composite = LennardJones(cutoff=8.0, sigma=3.405, epsilon=0.01032) + qeq
@@ -113,7 +118,7 @@ QEq supports `charge_response="frozen"` (Hellmann-Feynman, default) and
113
118
  ```python
114
119
  from arrayff.potentials import LJTorch
115
120
 
116
- model = LJTorch(species=[18]) # epsilon/sigma trainable
121
+ model = LJTorch(species=[18]) # epsilon/sigma trainable
117
122
  loss = (model(atoms) - target_energy) ** 2
118
123
  loss.backward()
119
124
  ```
@@ -1,5 +1,10 @@
1
1
  # ArrayFF
2
2
 
3
+ [![Conda Version](https://img.shields.io/conda/vn/conda-forge/arrayff.svg)](https://anaconda.org/conda-forge/arrayff)
4
+ [![Conda Downloads](https://img.shields.io/conda/dn/conda-forge/arrayff.svg)](https://anaconda.org/conda-forge/arrayff)
5
+ [![Pypi version](https://img.shields.io/pypi/v/arrayff)](https://pypi.org/project/arrayff/)
6
+ [![PyPI Downloads](https://static.pepy.tech/badge/arrayff)](https://pepy.tech/projects/arrayff)
7
+
3
8
  The Force Field Library Based on Multiple Array Backends like NumPy, PyTorch
4
9
  and JAX.
5
10
 
@@ -28,17 +33,17 @@ from arrayff.potentials import EMT, EMTCalculator
28
33
 
29
34
  atoms = bulk("Cu", "fcc", a=3.6, cubic=True).repeat((2, 2, 2))
30
35
 
31
- calc = EMT() # numpy backend + autograd for forces
36
+ calc = EMT() # numpy backend + autograd for forces
32
37
  energy, forces = calc.energy_and_forces(atoms, method="analytic")
33
38
 
34
- atoms.calc = EMTCalculator() # ASE calculator interface
39
+ atoms.calc = EMTCalculator() # ASE calculator interface
35
40
  energy = atoms.get_potential_energy()
36
41
  ```
37
42
 
38
43
  Backends are selected at construction:
39
44
 
40
45
  ```python
41
- calc = EMT(backend="torch") # or "jax"
46
+ calc = EMT(backend="torch") # or "jax"
42
47
  calc = EMT(backend="jax", jit=True)
43
48
  ```
44
49
 
@@ -66,7 +71,7 @@ PyTorch are the useful backends; NumPy's compiler is best-effort).
66
71
  from arrayff.electrostatics import QEq
67
72
 
68
73
  qeq = QEq(chi={8: 8.5, 1: 4.5}, J={8: 10.0, 1: 12.0}, shielding=1.0)
69
- charges = qeq.charges(atoms) # sum == total charge
74
+ charges = qeq.charges(atoms) # sum == total charge
70
75
 
71
76
  # additive composite: energy/forces/stress add, charges come from QEq
72
77
  composite = LennardJones(cutoff=8.0, sigma=3.405, epsilon=0.01032) + qeq
@@ -81,7 +86,7 @@ QEq supports `charge_response="frozen"` (Hellmann-Feynman, default) and
81
86
  ```python
82
87
  from arrayff.potentials import LJTorch
83
88
 
84
- model = LJTorch(species=[18]) # epsilon/sigma trainable
89
+ model = LJTorch(species=[18]) # epsilon/sigma trainable
85
90
  loss = (model(atoms) - target_energy) ** 2
86
91
  loss.backward()
87
92
  ```
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "arrayff"
7
- version = "0.0.1"
7
+ version = "0.0.2"
8
8
  readme = "README.md"
9
9
  description = "The Force Field Library Based on Multiple Array Backends like NumPy, PyTorch and JAX."
10
10
  license-files = ["src/LICENSE"]
@@ -8,7 +8,7 @@ Repository = "https://github.com/LiuGaoyong/ArrayFF"
8
8
  Issues = "https://github.com/LiuGaoyong/ArrayFF/issues/"
9
9
  [project]
10
10
  name = "arrayff"
11
- version = "0.0.1"
11
+ version = "0.0.2"
12
12
  readme = "README.md"
13
13
  description = "The Force Field Library Based on Multiple Array Backends like NumPy, PyTorch and JAX."
14
14
  authors = [{ name = "LiuGaoyong", email = "liugaoyong_88@163.com" }]
@@ -0,0 +1,14 @@
1
+ from ._dimer import DimerResult, call_dimer, call_dimer_displace
2
+ from ._helper import OptimizationResult, call_optimization, call_vib
3
+ from ._neb import NEBResult, call_neb
4
+
5
+ __all__ = [
6
+ "call_dimer",
7
+ "call_dimer_displace",
8
+ "call_optimization",
9
+ "call_vib",
10
+ "call_neb",
11
+ "NEBResult",
12
+ "DimerResult",
13
+ "OptimizationResult",
14
+ ]
@@ -0,0 +1,122 @@
1
+ from typing import Any
2
+
3
+ import numpy as np
4
+ from ase import Atoms
5
+ from ase.calculators.calculator import Calculator
6
+ from ase.calculators.singlepoint import SinglePointCalculator as SPC
7
+ from ase.constraints import FixAtoms
8
+ from ase.mep import DimerControl, MinModeAtoms
9
+ from ase.mep.dimer import MinModeTranslate
10
+
11
+ from ._helper import _ERR_MSG, OptimizationResult, _trajectory_record
12
+
13
+
14
+ class DimerResult(OptimizationResult):
15
+ pass
16
+
17
+
18
+ def call_dimer_displace(
19
+ atoms: Atoms,
20
+ calc: Calculator,
21
+ *,
22
+ parse_mask_from_atoms: bool = True,
23
+ mask: list[bool] | np.ndarray | None = None,
24
+ **kwargs,
25
+ ) -> np.ndarray:
26
+ """Call dimer method to search transition state."""
27
+ res = call_dimer(
28
+ atoms=atoms,
29
+ calc=calc,
30
+ mask=mask,
31
+ parse_mask_from_atoms=parse_mask_from_atoms,
32
+ max_steps=2,
33
+ **kwargs,
34
+ )
35
+ return res.lst[1].positions - res.lst[0].positions
36
+
37
+
38
+ def call_dimer(
39
+ atoms: Atoms,
40
+ calc: Calculator,
41
+ *,
42
+ displacement: np.ndarray | None = None,
43
+ mask: list[bool] | np.ndarray | None = None,
44
+ parse_mask_from_atoms: bool = True,
45
+ max_steps: int = 1000,
46
+ fmax: float = 0.02,
47
+ **kwargs,
48
+ ) -> DimerResult:
49
+ """Call dimer method to search transition state.
50
+
51
+ Parameters
52
+ ----------
53
+ atoms : Atoms
54
+ The atoms to optimize.
55
+ calc : Calculator
56
+ The calculator to use.
57
+ displacement : np.ndarray | None, optional
58
+ The displacement vector. Defaults to None.
59
+ mask : list[bool] | np.ndarray | None, optional
60
+ Which atoms will be moved during displacement. Defaults to None.
61
+ parse_mask_from_atoms : bool, optional
62
+ Whether to parse mask from atoms. Defaults to True.
63
+ max_steps : int, optional
64
+ The maximum number of steps. Defaults to 1000.
65
+ fmax : float, optional
66
+ The maximum force. Defaults to 0.01.
67
+ debug : bool, optional
68
+ Whether to print debug information. Defaults to False.
69
+ If True, the optimization will print log information to stdout.
70
+
71
+ Returns:
72
+ -------
73
+ DimerResult
74
+ The optimized atoms trajectory and whether the optimization converged.
75
+
76
+ """
77
+ if mask is None and parse_mask_from_atoms:
78
+ fixed_indices = []
79
+ for constr in atoms.constraints:
80
+ if isinstance(constr, FixAtoms):
81
+ fixed_indices.extend(constr.get_indices())
82
+ fixed_indices = list(set(fixed_indices))
83
+ mask = [i not in fixed_indices for i in range(len(atoms))]
84
+ else:
85
+ mask = np.asarray(mask, dtype=bool).tolist()
86
+ if calc is not None:
87
+ atoms.calc = calc
88
+ assert atoms.calc is not None, "Please set calculator."
89
+
90
+ param: dict[str, Any] = kwargs.copy()
91
+ param.setdefault("logfile", "-")
92
+ param.update({"mask": mask})
93
+ if displacement is not None:
94
+ param["initial_eigenmode_method"] = "displacement"
95
+ param["displacement_method"] = "vector"
96
+ displacement = np.asarray(displacement, copy=True)
97
+
98
+ # record initial energy
99
+ atoms.calc.reset()
100
+ atoms.get_potential_energy()
101
+ target = atoms.copy()
102
+ target.calc = SPC(target, **atoms.calc.results)
103
+ target.info["dimer_curvature"] = np.inf
104
+ data: list[Atoms] = [target]
105
+ converged = False
106
+
107
+ # run dimer
108
+ with DimerControl(**param) as d_control:
109
+ d_atoms = MinModeAtoms(atoms, d_control)
110
+ d_atoms.displace(displacement_vector=displacement)
111
+ with MinModeTranslate(d_atoms, logfile=param["logfile"]) as dim_rlx:
112
+ for _ in range(max_steps):
113
+ _trajectory_record(container=data, object=dim_rlx)
114
+ converged = dim_rlx.run(fmax=fmax, steps=1)
115
+ if np.any(np.isinf(data[-1].positions)):
116
+ raise RuntimeError(_ERR_MSG)
117
+ if np.any(np.isnan(atoms.positions)):
118
+ raise RuntimeError(_ERR_MSG)
119
+ if converged:
120
+ _trajectory_record(container=data, object=dim_rlx)
121
+ break
122
+ return DimerResult(lst=data, converged=converged)
@@ -0,0 +1,179 @@
1
+ import dataclasses as dc
2
+ from tempfile import TemporaryDirectory
3
+
4
+ import ase.optimize
5
+ import numpy as np
6
+ from ase import Atoms
7
+ from ase.calculators.calculator import Calculator
8
+ from ase.calculators.singlepoint import SinglePointCalculator as SPC
9
+ from ase.mep import MinModeAtoms
10
+ from ase.mep.dimer import DimerTranslate, MinModeTranslate
11
+ from ase.mep.neb import NEB, BaseNEB, DyNEB
12
+ from ase.optimize.optimize import Dynamics, Optimizer
13
+ from ase.units import invcm
14
+ from ase.vibrations import Vibrations, VibrationsData
15
+
16
+ _ERR_MSG = "Optimization failed because of infinite position."
17
+ OPTIMIZE_METHODS: dict[str, type[Optimizer]] = {}
18
+ for k in ase.optimize.__all__:
19
+ v = getattr(ase.optimize, k)
20
+ if isinstance(v, type) and issubclass(v, Optimizer):
21
+ OPTIMIZE_METHODS[k] = v
22
+
23
+
24
+ @dc.dataclass(frozen=True)
25
+ class OptimizationResult:
26
+ lst: list[Atoms]
27
+ converged: bool
28
+
29
+ @property
30
+ def latest(self) -> Atoms:
31
+ return self.lst[-1]
32
+
33
+
34
+ def call_vib(
35
+ atoms: Atoms,
36
+ calc: Calculator,
37
+ *,
38
+ ignore_fqmin: float = 10.0,
39
+ **kwargs,
40
+ ) -> tuple[np.ndarray, np.ndarray]:
41
+ """Vibration atoms (frequencies in cm^-1 & harmonic modes).
42
+
43
+ Parameters
44
+ ----------
45
+ atoms : Atoms
46
+ The atoms to analyze.
47
+ calc : Calculator
48
+ The calculator to use.
49
+ ignore_fqmin : float, optional
50
+ The minimum frequency to ignore. Defaults to 10.0.
51
+ Effect: the element frequency will be set to 1e-5 if the
52
+ absolute value of element frequency is smaller than this value.
53
+
54
+ Returns
55
+ -------
56
+ tuple[np.ndarray, np.ndarray]
57
+ The frequencies in cm^-1 and the harmonic modes.
58
+ """
59
+ if calc is not None:
60
+ atoms.calc = calc
61
+ assert atoms.calc is not None, "Please set calculator."
62
+ atoms.calc.reset()
63
+
64
+ with TemporaryDirectory() as tmpdir:
65
+ vib = Vibrations(atoms, name=tmpdir)
66
+ vib.run()
67
+ vibdata: VibrationsData = vib.get_vibrations()
68
+ eng, modes = vibdata.get_energies_and_modes(all_atoms=True)
69
+ freq = np.asarray(eng / invcm, dtype=complex)
70
+ freq = np.real(freq) - np.imag(freq) # complex to real
71
+ freq[np.abs(freq) < abs(ignore_fqmin)] = 1e-5
72
+ return freq, modes
73
+
74
+
75
+ def call_optimization(
76
+ atoms: Atoms,
77
+ calc: Calculator,
78
+ *,
79
+ method: str = "LBFGS",
80
+ max_steps: int = 100,
81
+ fmax: float = 0.01,
82
+ ) -> OptimizationResult:
83
+ """Optimize the `ase.Atoms`.
84
+
85
+ Parameters
86
+ ----------
87
+ atoms : Atoms
88
+ The atoms to optimize.
89
+ calc : Calculator
90
+ The calculator to use.
91
+ method : str, optional
92
+ The method to use. Defaults to "LBFGS".
93
+ max_steps : int, optional
94
+ The maximum number of steps. Defaults to 100.
95
+ fmax : float, optional
96
+ The maximum force. Defaults to 1e-4.
97
+
98
+ Returns:
99
+ -------
100
+ OptimizationResult
101
+ The optimized atoms trajectory and whether the optimization converged.
102
+ """
103
+ assert method in OPTIMIZE_METHODS, (
104
+ f"Method {method} not found in `ase.optimize`."
105
+ )
106
+ OPT_CLS: type[Optimizer] = OPTIMIZE_METHODS[method]
107
+ result_lst: list[Atoms] = []
108
+ converged = False
109
+
110
+ atoms.calc = calc
111
+ atoms.calc.reset()
112
+ optimizer: Optimizer = OPT_CLS(atoms)
113
+ optimizer.attach(
114
+ _trajectory_record,
115
+ container=result_lst,
116
+ object=optimizer,
117
+ interval=1,
118
+ )
119
+ try:
120
+ for _ in range(max_steps):
121
+ converged = optimizer.run(steps=1, fmax=fmax)
122
+ # Check for infinite/NaN positions (handle both Atoms and NEB)
123
+ if hasattr(atoms, "images"):
124
+ arr = [img.positions for img in atoms.images] # type: ignore
125
+ positions = np.concatenate(arr)
126
+ else:
127
+ positions = atoms.positions
128
+ if np.any(np.isinf(positions)):
129
+ raise RuntimeError(_ERR_MSG)
130
+ if np.any(np.isnan(positions)):
131
+ raise RuntimeError(_ERR_MSG)
132
+ if converged:
133
+ break
134
+ except RuntimeError as e:
135
+ converged = False
136
+ raise e
137
+ return OptimizationResult(
138
+ converged=converged,
139
+ lst=result_lst,
140
+ )
141
+
142
+
143
+ def _trajectory_record(container: list[Atoms], object: Dynamics) -> None:
144
+ """Record the trajectory of the dynamics object.
145
+
146
+ Parameters
147
+ ----------
148
+ container : list[Atoms]
149
+ The container to store the trajectory.
150
+ object : Dynamics
151
+ The dynamics object to record.
152
+
153
+ Returns: None
154
+ -------------
155
+ """
156
+ if isinstance(object, (DimerTranslate, MinModeTranslate)):
157
+ d_atoms = object.atoms
158
+ assert isinstance(d_atoms, MinModeAtoms)
159
+ atoms0: Atoms = d_atoms.get_atoms()
160
+ assert isinstance(atoms0.calc, Calculator)
161
+ d_atoms.get_potential_energy()
162
+ target = atoms0.copy()
163
+ target.calc = SPC(target, **atoms0.calc.results)
164
+ target.info["dimer_curvature"] = d_atoms.get_curvature()
165
+ container.append(target)
166
+ elif isinstance(object, Dynamics):
167
+ if isinstance(object.atoms, (NEB, DyNEB, BaseNEB)):
168
+ for atoms in object.atoms.images:
169
+ target = atoms.copy()
170
+ assert isinstance(atoms.calc, Calculator)
171
+ target.calc = SPC(target, **atoms.calc.results)
172
+ container.append(target)
173
+ else:
174
+ target = object.atoms.copy()
175
+ assert isinstance(object.atoms.calc, Calculator)
176
+ target.calc = SPC(target, **object.atoms.calc.results)
177
+ container.append(target)
178
+ else:
179
+ raise ValueError(f"Unknown type {type(object)}.")
@@ -0,0 +1,229 @@
1
+ import dataclasses as dc
2
+ from copy import deepcopy
3
+
4
+ from ase import Atoms
5
+ from ase.calculators.calculator import Calculator
6
+ from ase.mep.neb import DyNEB
7
+
8
+ from ._helper import OptimizationResult, call_optimization
9
+
10
+
11
+ @dc.dataclass(frozen=True)
12
+ class NEBResult(OptimizationResult):
13
+ nimages: int
14
+
15
+ @property
16
+ def latest(self) -> list[Atoms]: # type: ignore
17
+ """The latest images list."""
18
+ assert self.converged, "Optimization did not converge."
19
+ return self.lst[-abs(self.nimages) :]
20
+
21
+
22
+ def call_neb_raw(
23
+ images: list[Atoms],
24
+ calc: Calculator,
25
+ *,
26
+ climb: bool = True,
27
+ k4spring: float = 0.1,
28
+ dynamic_relaxation: bool = True,
29
+ method4spring: str = "improvedtangent",
30
+ method4interpolate: str | None = None,
31
+ method4opt: str = "FIRE",
32
+ max_steps: int = 100,
33
+ fmax: float = 0.05,
34
+ ) -> NEBResult:
35
+ """Call NEB method to search transition state.
36
+
37
+ Parameters
38
+ ----------
39
+ images : list[Atoms]
40
+ The images to optimize.
41
+ calc : Calculator
42
+ The calculator to use.
43
+ climb : bool, optional
44
+ Whether to climb. Defaults to True.
45
+ k4spring : float, optional
46
+ The spring constant. Defaults to 0.1.
47
+ dynamic_relaxation : bool, optional
48
+ Whether to use dynamic relaxation. Defaults to True.
49
+ True skips images with forces below the convergence criterion.
50
+ This is updated after each force call; if a previously converged
51
+ image goes out of tolerance (due to spring adjustments between
52
+ the image and its neighbors), it will be optimized again.
53
+ False reverts to the default NEB implementation.
54
+ method4spring : str | None, optional
55
+ The method to use for spring optimization. Defaults to None.
56
+ Choice betweeen five methods:
57
+
58
+ * aseneb: legacy ase NEB implementation
59
+ * improvedtangent: Paper I NEB implementation (default)
60
+ * eb: Paper III full spring force implementation
61
+ * spline: Paper IV spline interpolation (supports precon)
62
+ * string: Paper IV string method (supports precon)
63
+
64
+ method4interpolate : str, optional
65
+ The method to use for interpolation. Defaults to "FIRE".
66
+ Method by which to interpolate: 'linear' or 'idpp'.
67
+ linear provides a standard straight-line interpolation, while
68
+ idpp uses an image-dependent pair potential.
69
+
70
+
71
+ Paper I:
72
+ G. Henkelman and H. Jonsson, Chem. Phys, 113, 9978 (2000).
73
+ :doi:`10.1063/1.1323224`
74
+ Paper II:
75
+ G. Henkelman, B. P. Uberuaga, and H. Jonsson, Chem. Phys,
76
+ 113, 9901 (2000).
77
+ :doi:`10.1063/1.1329672`
78
+ Paper III:
79
+ E. L. Kolsbjerg, M. N. Groves, and B. Hammer, J. Chem. Phys,
80
+ 145, 094107 (2016)
81
+ :doi:`10.1063/1.4961868`
82
+ Paper IV:
83
+ S. Makri, C. Ortner and J. R. Kermode, J. Chem. Phys.
84
+ 150, 094109 (2019)
85
+ https://dx.doi.org/10.1063/1.5064465
86
+
87
+
88
+ Returns:
89
+ -------
90
+ NEBResult
91
+ The optimized images trajectory and whether the optimization converged.
92
+ """
93
+ for at in images:
94
+ at.calc = deepcopy(calc)
95
+ at.calc.reset()
96
+ at.get_potential_energy()
97
+ neb = DyNEB(
98
+ images,
99
+ method=method4spring,
100
+ dynamic_relaxation=dynamic_relaxation,
101
+ climb=climb,
102
+ k=k4spring,
103
+ )
104
+ if method4interpolate is not None:
105
+ neb.interpolate(method=method4interpolate)
106
+ result = call_optimization(
107
+ neb, # type: ignore
108
+ calc,
109
+ method=method4opt,
110
+ max_steps=max_steps,
111
+ fmax=fmax,
112
+ )
113
+ return NEBResult(
114
+ lst=result.lst,
115
+ converged=result.converged,
116
+ nimages=len(images),
117
+ )
118
+
119
+
120
+ def call_neb(
121
+ atoms: Atoms,
122
+ calc: Calculator,
123
+ final_atoms: Atoms,
124
+ *,
125
+ nimages: int = 5,
126
+ climb: bool = True,
127
+ k4spring: float = 0.1,
128
+ dynamic_relaxation: bool = True,
129
+ method4spring: str = "improvedtangent",
130
+ method4interpolate: str = "linear",
131
+ method4opt: str = "FIRE",
132
+ max_steps: int = 100,
133
+ fmax: float = 0.05,
134
+ ) -> NEBResult:
135
+ """Call NEB method to search transition state.
136
+
137
+ Parameters
138
+ ----------
139
+ atoms : Atoms
140
+ The atoms to optimize.
141
+ calc : Calculator
142
+ The calculator to use.
143
+ final_atoms : Atoms
144
+ The final atoms to optimize.
145
+ nimages : int, optional
146
+ The number of images. Defaults to 5.
147
+ climb : bool, optional
148
+ Whether to climb. Defaults to True.
149
+ k4spring : float, optional
150
+ The spring constant. Defaults to 0.1.
151
+ dynamic_relaxation : bool, optional
152
+ Whether to use dynamic relaxation. Defaults to True.
153
+ True skips images with forces below the convergence criterion.
154
+ This is updated after each force call; if a previously converged
155
+ image goes out of tolerance (due to spring adjustments between
156
+ the image and its neighbors), it will be optimized again.
157
+ False reverts to the default NEB implementation.
158
+ method4spring : str | None, optional
159
+ The method to use for spring optimization. Defaults to None.
160
+ Choice betweeen five methods:
161
+
162
+ * aseneb: legacy ase NEB implementation
163
+ * improvedtangent: Paper I NEB implementation (default)
164
+ * eb: Paper III full spring force implementation
165
+ * spline: Paper IV spline interpolation (supports precon)
166
+ * string: Paper IV string method (supports precon)
167
+
168
+ method4interpolate : str, optional
169
+ The method to use for interpolation. Defaults to "FIRE".
170
+ Method by which to interpolate: 'linear' or 'idpp'.
171
+ linear provides a standard straight-line interpolation, while
172
+ idpp uses an image-dependent pair potential.
173
+
174
+
175
+ Paper I:
176
+ G. Henkelman and H. Jonsson, Chem. Phys, 113, 9978 (2000).
177
+ :doi:`10.1063/1.1323224`
178
+
179
+ Paper II:
180
+ G. Henkelman, B. P. Uberuaga, and H. Jonsson, Chem. Phys,
181
+ 113, 9901 (2000).
182
+ :doi:`10.1063/1.1329672`
183
+
184
+ Paper III:
185
+ E. L. Kolsbjerg, M. N. Groves, and B. Hammer, J. Chem. Phys,
186
+ 145, 094107 (2016)
187
+ :doi:`10.1063/1.4961868`
188
+
189
+ Paper IV:
190
+ S. Makri, C. Ortner and J. R. Kermode, J. Chem. Phys.
191
+ 150, 094109 (2019)
192
+ https://dx.doi.org/10.1063/1.5064465
193
+
194
+
195
+ Returns:
196
+ -------
197
+ tuple[list[Atoms], bool]
198
+ The optimized atoms trajectory and whether the optimization converged.
199
+ """
200
+ images: list[Atoms] = []
201
+ assert nimages >= 3, "nimages must be at least 3."
202
+ for k, at in zip(["first", "final"], [atoms, final_atoms]):
203
+ res = call_optimization(
204
+ at,
205
+ calc,
206
+ method=method4opt,
207
+ max_steps=max_steps,
208
+ fmax=fmax,
209
+ )
210
+ if not res.converged:
211
+ raise RuntimeError(f"Optimization did not converge for {k} image.")
212
+ images.append(res.latest)
213
+ assert len(images) == 2
214
+ for _ in range(nimages - 2):
215
+ target = images[0].copy()
216
+ images.insert(0, target)
217
+ assert len(images) == nimages
218
+ return call_neb_raw(
219
+ images=images,
220
+ calc=calc,
221
+ climb=climb,
222
+ method4spring=method4spring,
223
+ dynamic_relaxation=dynamic_relaxation,
224
+ method4interpolate=method4interpolate,
225
+ method4opt=method4opt,
226
+ max_steps=max_steps,
227
+ k4spring=k4spring,
228
+ fmax=fmax,
229
+ )
File without changes
File without changes
File without changes
File without changes