arrayff 0.0.2__tar.gz → 0.1.0__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.
- arrayff-0.1.0/PKG-INFO +229 -0
- arrayff-0.1.0/README.md +191 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/pyproject.toml +21 -12
- {arrayff-0.0.2 → arrayff-0.1.0}/pyproject.toml.orig +15 -17
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/__init__.py +6 -1
- arrayff-0.1.0/src/arrayff/electrostatics/ewald.py +78 -0
- arrayff-0.1.0/src/arrayff/electrostatics/qeq.py +579 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/neighbors/_list.py +87 -59
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/neighbors/_table.py +35 -72
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/optimize/_dimer.py +9 -5
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/optimize/_helper.py +55 -38
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/optimize/_neb.py +44 -32
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/__init__.py +2 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/_torch.py +32 -2
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/base.py +99 -90
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/composite.py +13 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/eam/_torch.py +4 -0
- arrayff-0.1.0/src/arrayff/potentials/eam/eam.py +206 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/emt/_torch.py +2 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/emt/emt.py +32 -56
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/pairwise/_pairs.py +12 -5
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/pairwise/_torch.py +14 -1
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/pairwise/base.py +8 -32
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/pairwise/lj.py +20 -5
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/pairwise/lj93.py +6 -5
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/pairwise/morse.py +5 -4
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/placeholders.py +1 -1
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/utils/ad.py +36 -27
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/utils/ase.py +7 -14
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/utils/backend.py +32 -9
- arrayff-0.1.0/src/arrayff/utils/dispatch.py +312 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/utils/exceptions.py +4 -0
- arrayff-0.1.0/src/arrayff/utils/interp.py +128 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/utils/jit.py +46 -3
- arrayff-0.1.0/src/arrayff/utils/stress.py +112 -0
- arrayff-0.0.2/PKG-INFO +0 -147
- arrayff-0.0.2/README.md +0 -115
- arrayff-0.0.2/src/arrayff/electrostatics/qeq.py +0 -498
- arrayff-0.0.2/src/arrayff/potentials/eam/eam.py +0 -233
- arrayff-0.0.2/src/arrayff/utils/dispatch.py +0 -118
- arrayff-0.0.2/src/arrayff/utils/interp.py +0 -77
- {arrayff-0.0.2 → arrayff-0.1.0}/src/LICENSE +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/electrostatics/__init__.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/electrostatics/calculator.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/electrostatics/placeholders.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/neighbors/__init__.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/optimize/__init__.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/calculator.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/eam/__init__.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/eam/calculator.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/eam/io/__init__.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/eam/io/eam.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/emt/__init__.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/emt/calculator.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/pairwise/__init__.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/pairwise/calculator.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/pairwise/forms.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/pairwise/lj_kim.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/pairwise/params.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/potentials/registry.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/py.typed +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/utils/__init__.py +0 -0
- {arrayff-0.0.2 → arrayff-0.1.0}/src/arrayff/utils/optional.py +0 -0
arrayff-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: arrayff
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: The Force Field Library Based on Multiple Array Backends like NumPy, PyTorch and JAX.
|
|
5
|
+
Keywords: chemistry,Force Field
|
|
6
|
+
Author: LiuGaoyong
|
|
7
|
+
Author-email: LiuGaoyong <liugaoyong_88@163.com>
|
|
8
|
+
License-Expression: GPL-3.0-or-later
|
|
9
|
+
License-File: src/LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Chemistry
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Requires-Dist: numpy
|
|
14
|
+
Requires-Dist: ase>=3.23
|
|
15
|
+
Requires-Dist: scipy>=1.6
|
|
16
|
+
Requires-Dist: autograd>=1.6 ; extra == 'autograd'
|
|
17
|
+
Requires-Dist: autoray>=0.6 ; extra == 'autoray'
|
|
18
|
+
Requires-Dist: vesin ; extra == 'builders'
|
|
19
|
+
Requires-Dist: matscipy ; extra == 'builders'
|
|
20
|
+
Requires-Dist: pymatgen ; extra == 'builders'
|
|
21
|
+
Requires-Dist: scikit-learn>=1.5 ; extra == 'builders'
|
|
22
|
+
Requires-Dist: jax>=0.5 ; extra == 'jax'
|
|
23
|
+
Requires-Dist: autoray>=0.6 ; extra == 'jax'
|
|
24
|
+
Requires-Dist: scikit-learn>=1.5 ; extra == 'sklearn'
|
|
25
|
+
Requires-Dist: torch>=2.3 ; extra == 'torch'
|
|
26
|
+
Requires-Dist: autoray>=0.6 ; extra == 'torch'
|
|
27
|
+
Requires-Python: >=3.12
|
|
28
|
+
Project-URL: Homepage, https://github.com/LiuGaoyong/ArrayFF
|
|
29
|
+
Project-URL: Repository, https://github.com/LiuGaoyong/ArrayFF
|
|
30
|
+
Project-URL: Issues, https://github.com/LiuGaoyong/ArrayFF/issues/
|
|
31
|
+
Provides-Extra: autograd
|
|
32
|
+
Provides-Extra: autoray
|
|
33
|
+
Provides-Extra: builders
|
|
34
|
+
Provides-Extra: jax
|
|
35
|
+
Provides-Extra: sklearn
|
|
36
|
+
Provides-Extra: torch
|
|
37
|
+
Description-Content-Type: text/markdown
|
|
38
|
+
|
|
39
|
+
# ArrayFF
|
|
40
|
+
|
|
41
|
+
[](https://anaconda.org/conda-forge/arrayff)
|
|
42
|
+
[](https://anaconda.org/conda-forge/arrayff)
|
|
43
|
+
[](https://pypi.org/project/arrayff/)
|
|
44
|
+
[](https://pepy.tech/projects/arrayff)
|
|
45
|
+
|
|
46
|
+
The Force Field Library Based on Multiple Array Backends like NumPy, PyTorch
|
|
47
|
+
and JAX.
|
|
48
|
+
|
|
49
|
+
Kernels are written once against a small dispatch layer, so the backend is
|
|
50
|
+
chosen by the arrays you pass in: NumPy (and autograd) run on a built-in
|
|
51
|
+
dispatcher, PyTorch and JAX go through
|
|
52
|
+
[`autoray`](https://github.com/jcmgray/autoray). Structural I/O uses
|
|
53
|
+
`ase.Atoms` throughout.
|
|
54
|
+
|
|
55
|
+
The only required array backend is NumPy. Automatic differentiation of NumPy
|
|
56
|
+
arrays uses [`autograd`](https://github.com/HIPS/autograd); autograd, autoray,
|
|
57
|
+
PyTorch and JAX are optional extras.
|
|
58
|
+
|
|
59
|
+
## Install
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
uv pip install -e .
|
|
63
|
+
uv pip install -e ".[autograd]" # NumPy autodiff (method="autodiff")
|
|
64
|
+
uv pip install -e ".[torch]" # PyTorch backend + nn wrappers (with autoray)
|
|
65
|
+
uv pip install -e ".[jax]" # JAX backend (with autoray)
|
|
66
|
+
uv pip install -e ".[sklearn]" # scikit-learn neighbour builder
|
|
67
|
+
uv pip install -e ".[builders]" # sklearn / vesin / matscipy / pymatgen neighbours
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The hard dependencies are NumPy, SciPy (>= 1.6) and ASE. Without autograd
|
|
71
|
+
every NumPy feature works except `method="autodiff"`; torch/JAX arrays and
|
|
72
|
+
`jit=True` on those backends need autoray (pulled in by the `torch`/`jax`
|
|
73
|
+
extras). A missing optional dependency raises `MissingBackendError` with the
|
|
74
|
+
install command.
|
|
75
|
+
|
|
76
|
+
## Quick start
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from ase.build import bulk
|
|
80
|
+
from arrayff.potentials import EMT, EMTCalculator
|
|
81
|
+
|
|
82
|
+
atoms = bulk("Cu", "fcc", a=3.6, cubic=True).repeat((2, 2, 2))
|
|
83
|
+
|
|
84
|
+
calc = EMT() # numpy backend, closed-form forces
|
|
85
|
+
energy, forces = calc.energy_and_forces(atoms, method="analytic")
|
|
86
|
+
|
|
87
|
+
atoms.calc = EMTCalculator() # ASE calculator interface
|
|
88
|
+
energy = atoms.get_potential_energy()
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Backends are selected at construction:
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
calc = EMT(backend="torch") # or "jax"
|
|
95
|
+
calc = EMT(backend="jax", jit=True)
|
|
96
|
+
calc = EMT(backend="torch", dtype=torch.float32, device="cuda")
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Neighbour tables are built with `scipy.spatial.KDTree` (`builder="scipy"`,
|
|
100
|
+
default). `sklearn` (scikit-learn `NearestNeighbors`, optional extra),
|
|
101
|
+
`vesin`, `matscipy`, `pymatgen` and `ase_kdtree` are alternatives.
|
|
102
|
+
They are built with `cutoff + skin` and reused until an atom has moved more
|
|
103
|
+
than `skin / 2`; the default `skin=0.5` Angstrom suits MD and relaxations, and
|
|
104
|
+
`skin=0.0` rebuilds whenever the positions change.
|
|
105
|
+
|
|
106
|
+
`LennardJones()` without arguments uses the OpenKIM universal shifted
|
|
107
|
+
parameters (118 elements, cutoff `4*sigma` per pair, Lorentz-Berthelot
|
|
108
|
+
mixing: arithmetic `r0`/cutoff and geometric `epsilon`). The neighbour
|
|
109
|
+
table only reaches the largest cutoff of the elements actually present.
|
|
110
|
+
|
|
111
|
+
Each potential exposes analytic and autodiff forces/stress:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from arrayff.potentials import LennardJones
|
|
115
|
+
|
|
116
|
+
calc = LennardJones(cutoff=8.0, sigma=3.405, epsilon=0.01032)
|
|
117
|
+
calc.energy_and_forces(atoms, method="analytic")
|
|
118
|
+
calc.energy_and_forces(atoms, method="autodiff")
|
|
119
|
+
calc.stress(atoms, method="analytic")
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
`jit=True` compiles the array-level kernel with `autoray.autojit` (JAX and
|
|
123
|
+
PyTorch only; NumPy runs eagerly). See
|
|
124
|
+
[Backend and derivative support](#backend-and-derivative-support).
|
|
125
|
+
|
|
126
|
+
## Charge equilibration and composite potentials
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
from arrayff.electrostatics import QEq
|
|
130
|
+
|
|
131
|
+
qeq = QEq(chi={8: 8.5, 1: 4.5}, J={8: 10.0, 1: 12.0}, shielding=1.0)
|
|
132
|
+
charges = qeq.charges(atoms) # sum == total charge
|
|
133
|
+
|
|
134
|
+
# additive composite: energy/forces/stress add, charges come from QEq
|
|
135
|
+
composite = LennardJones(cutoff=8.0, sigma=3.405, epsilon=0.01032) + qeq
|
|
136
|
+
energy, forces = composite.energy_and_forces(atoms)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
QEq supports `charge_response="frozen"` (Hellmann-Feynman, default) and
|
|
140
|
+
`"relaxed"` (differentiate through the charge solve).
|
|
141
|
+
|
|
142
|
+
Two pair kernels are available: `kernel="shielded"` (default,
|
|
143
|
+
`1/sqrt(r^2 + lambda^2)`) and `kernel="gaussian"` (`erf(r/lambda)/r`,
|
|
144
|
+
Gaussian charge clouds). Fully periodic cells are summed with Ewald
|
|
145
|
+
(`ewald_accuracy`, `ewald_cutoff`). With the Gaussian kernel this is exact to
|
|
146
|
+
`ewald_accuracy`; the shielded kernel's `r^-3` remainder is truncated at
|
|
147
|
+
`shielded_cutoff`, which is approximate (QEq emits an `AccuracyWarning`), so
|
|
148
|
+
prefer `kernel="gaussian"` for periodic systems. The real-space sum uses a
|
|
149
|
+
neighbour list, so memory grows linearly with the number of atoms (the
|
|
150
|
+
`N x N` QEq matrix itself is dense). Mixed boundary conditions are not
|
|
151
|
+
supported.
|
|
152
|
+
|
|
153
|
+
## Training (optional)
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from arrayff.potentials import LJTorch
|
|
157
|
+
|
|
158
|
+
model = LJTorch(species=[18]) # epsilon/sigma trainable
|
|
159
|
+
loss = (model(atoms) - target_energy) ** 2
|
|
160
|
+
loss.backward()
|
|
161
|
+
|
|
162
|
+
model.to(device="cuda", dtype=torch.float32) # tables and inputs follow
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The torch modules (`LJTorch`, `LJ93Torch`, `MorseTorch`, `EMTTorch`,
|
|
166
|
+
`EAMTorch`) follow `module.to(...)`: positions, neighbour tables and cached
|
|
167
|
+
parameters are rebuilt on the module's device and dtype.
|
|
168
|
+
|
|
169
|
+
## Tests and benchmarks
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
pytest -q
|
|
173
|
+
pytest -s src/tests-benchmark/bench_potentials.py
|
|
174
|
+
pytest -s src/tests-benchmark/bench_derivatives.py
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
The benchmark tables compare analytic vs autodiff forces/stress and
|
|
178
|
+
`jit=False/True` across backends.
|
|
179
|
+
|
|
180
|
+
## Status
|
|
181
|
+
|
|
182
|
+
| Potential | State |
|
|
183
|
+
|---|---|
|
|
184
|
+
| Lennard-Jones (OpenKIM universal shifted) | implemented |
|
|
185
|
+
| Lennard-Jones 9-3, Morse | implemented |
|
|
186
|
+
| EMT (matches `ase.calculators.emt`, including `asap_cutoff`) | implemented |
|
|
187
|
+
| EAM (setfl tables, cubic-spline interpolation as in `ase.calculators.eam`, continuous forces) | implemented |
|
|
188
|
+
| QEq (shielded/Gaussian kernels, Ewald for periodic cells, frozen/relaxed response) | implemented |
|
|
189
|
+
| Additive composites (`LJ + QEq`, ...) | implemented |
|
|
190
|
+
| MEAM, UF3, EEM, PQEq | declared placeholders |
|
|
191
|
+
|
|
192
|
+
## Backend and derivative support
|
|
193
|
+
|
|
194
|
+
Every implemented model runs on all three backends and provides energy,
|
|
195
|
+
forces and (for fully periodic cells) stress through both derivative paths.
|
|
196
|
+
Each combination below was checked against the NumPy analytic result.
|
|
197
|
+
|
|
198
|
+
| Model | NumPy | PyTorch | JAX | Analytic forces/stress | Autodiff forces/stress | Trainable torch module |
|
|
199
|
+
|---|---|---|---|---|---|---|
|
|
200
|
+
| `LennardJones` (KIM or explicit sigma/epsilon) | yes | yes | yes | yes | yes | `LJTorch` |
|
|
201
|
+
| `LJ93` | yes | yes | yes | yes | yes | `LJ93Torch` |
|
|
202
|
+
| `Morse` | yes | yes | yes | yes | yes | `MorseTorch` |
|
|
203
|
+
| `EMT` | yes | yes | yes | yes | yes | `EMTTorch` |
|
|
204
|
+
| `EAM` | yes | yes | yes | yes | yes | `EAMTorch` |
|
|
205
|
+
| `QEq` (shielded / Gaussian, cluster / periodic) | yes | yes | yes | yes, envelope theorem | yes, frozen or relaxed charges | - |
|
|
206
|
+
| `CompositePotential` | yes | yes | yes | sum of components | sum of components | - |
|
|
207
|
+
|
|
208
|
+
- `method="analytic"` (default) uses closed-form derivatives. For QEq, the
|
|
209
|
+
analytic forces and stress equal the relaxed-charge result to solver
|
|
210
|
+
accuracy, because the charge-response term vanishes at the QEq minimum.
|
|
211
|
+
- `method="autodiff"` uses autograd for NumPy (optional extra
|
|
212
|
+
`arrayff[autograd]`), `torch.func` for PyTorch and
|
|
213
|
+
`jax.grad` for JAX. Stress is the derivative with respect to a homogeneous
|
|
214
|
+
strain.
|
|
215
|
+
- `method="finite-difference"` is a diagnostic: forces stay analytic and only
|
|
216
|
+
the stress is obtained by central differences.
|
|
217
|
+
- `float32` and accelerators (`dtype=`, `device=`) are supported on PyTorch;
|
|
218
|
+
CPU and Apple MPS are tested.
|
|
219
|
+
|
|
220
|
+
`jit=True` compiles the following paths; everything else runs eagerly and
|
|
221
|
+
gives the same numbers:
|
|
222
|
+
|
|
223
|
+
| Backend | Analytic | Autodiff energy+forces | Autodiff stress |
|
|
224
|
+
|---|---|---|---|
|
|
225
|
+
| NumPy | eager (`jit` is ignored silently) | eager | eager |
|
|
226
|
+
| PyTorch | compiled (`torch.jit.trace`) | compiled energy kernel, then `torch.autograd` | eager |
|
|
227
|
+
| JAX | compiled (`jax.jit`) | compiled `value_and_grad` | eager |
|
|
228
|
+
|
|
229
|
+
For QEq, `jit` applies to the analytic path only.
|
arrayff-0.1.0/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# ArrayFF
|
|
2
|
+
|
|
3
|
+
[](https://anaconda.org/conda-forge/arrayff)
|
|
4
|
+
[](https://anaconda.org/conda-forge/arrayff)
|
|
5
|
+
[](https://pypi.org/project/arrayff/)
|
|
6
|
+
[](https://pepy.tech/projects/arrayff)
|
|
7
|
+
|
|
8
|
+
The Force Field Library Based on Multiple Array Backends like NumPy, PyTorch
|
|
9
|
+
and JAX.
|
|
10
|
+
|
|
11
|
+
Kernels are written once against a small dispatch layer, so the backend is
|
|
12
|
+
chosen by the arrays you pass in: NumPy (and autograd) run on a built-in
|
|
13
|
+
dispatcher, PyTorch and JAX go through
|
|
14
|
+
[`autoray`](https://github.com/jcmgray/autoray). Structural I/O uses
|
|
15
|
+
`ase.Atoms` throughout.
|
|
16
|
+
|
|
17
|
+
The only required array backend is NumPy. Automatic differentiation of NumPy
|
|
18
|
+
arrays uses [`autograd`](https://github.com/HIPS/autograd); autograd, autoray,
|
|
19
|
+
PyTorch and JAX are optional extras.
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
uv pip install -e .
|
|
25
|
+
uv pip install -e ".[autograd]" # NumPy autodiff (method="autodiff")
|
|
26
|
+
uv pip install -e ".[torch]" # PyTorch backend + nn wrappers (with autoray)
|
|
27
|
+
uv pip install -e ".[jax]" # JAX backend (with autoray)
|
|
28
|
+
uv pip install -e ".[sklearn]" # scikit-learn neighbour builder
|
|
29
|
+
uv pip install -e ".[builders]" # sklearn / vesin / matscipy / pymatgen neighbours
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The hard dependencies are NumPy, SciPy (>= 1.6) and ASE. Without autograd
|
|
33
|
+
every NumPy feature works except `method="autodiff"`; torch/JAX arrays and
|
|
34
|
+
`jit=True` on those backends need autoray (pulled in by the `torch`/`jax`
|
|
35
|
+
extras). A missing optional dependency raises `MissingBackendError` with the
|
|
36
|
+
install command.
|
|
37
|
+
|
|
38
|
+
## Quick start
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from ase.build import bulk
|
|
42
|
+
from arrayff.potentials import EMT, EMTCalculator
|
|
43
|
+
|
|
44
|
+
atoms = bulk("Cu", "fcc", a=3.6, cubic=True).repeat((2, 2, 2))
|
|
45
|
+
|
|
46
|
+
calc = EMT() # numpy backend, closed-form forces
|
|
47
|
+
energy, forces = calc.energy_and_forces(atoms, method="analytic")
|
|
48
|
+
|
|
49
|
+
atoms.calc = EMTCalculator() # ASE calculator interface
|
|
50
|
+
energy = atoms.get_potential_energy()
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Backends are selected at construction:
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
calc = EMT(backend="torch") # or "jax"
|
|
57
|
+
calc = EMT(backend="jax", jit=True)
|
|
58
|
+
calc = EMT(backend="torch", dtype=torch.float32, device="cuda")
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Neighbour tables are built with `scipy.spatial.KDTree` (`builder="scipy"`,
|
|
62
|
+
default). `sklearn` (scikit-learn `NearestNeighbors`, optional extra),
|
|
63
|
+
`vesin`, `matscipy`, `pymatgen` and `ase_kdtree` are alternatives.
|
|
64
|
+
They are built with `cutoff + skin` and reused until an atom has moved more
|
|
65
|
+
than `skin / 2`; the default `skin=0.5` Angstrom suits MD and relaxations, and
|
|
66
|
+
`skin=0.0` rebuilds whenever the positions change.
|
|
67
|
+
|
|
68
|
+
`LennardJones()` without arguments uses the OpenKIM universal shifted
|
|
69
|
+
parameters (118 elements, cutoff `4*sigma` per pair, Lorentz-Berthelot
|
|
70
|
+
mixing: arithmetic `r0`/cutoff and geometric `epsilon`). The neighbour
|
|
71
|
+
table only reaches the largest cutoff of the elements actually present.
|
|
72
|
+
|
|
73
|
+
Each potential exposes analytic and autodiff forces/stress:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from arrayff.potentials import LennardJones
|
|
77
|
+
|
|
78
|
+
calc = LennardJones(cutoff=8.0, sigma=3.405, epsilon=0.01032)
|
|
79
|
+
calc.energy_and_forces(atoms, method="analytic")
|
|
80
|
+
calc.energy_and_forces(atoms, method="autodiff")
|
|
81
|
+
calc.stress(atoms, method="analytic")
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`jit=True` compiles the array-level kernel with `autoray.autojit` (JAX and
|
|
85
|
+
PyTorch only; NumPy runs eagerly). See
|
|
86
|
+
[Backend and derivative support](#backend-and-derivative-support).
|
|
87
|
+
|
|
88
|
+
## Charge equilibration and composite potentials
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from arrayff.electrostatics import QEq
|
|
92
|
+
|
|
93
|
+
qeq = QEq(chi={8: 8.5, 1: 4.5}, J={8: 10.0, 1: 12.0}, shielding=1.0)
|
|
94
|
+
charges = qeq.charges(atoms) # sum == total charge
|
|
95
|
+
|
|
96
|
+
# additive composite: energy/forces/stress add, charges come from QEq
|
|
97
|
+
composite = LennardJones(cutoff=8.0, sigma=3.405, epsilon=0.01032) + qeq
|
|
98
|
+
energy, forces = composite.energy_and_forces(atoms)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
QEq supports `charge_response="frozen"` (Hellmann-Feynman, default) and
|
|
102
|
+
`"relaxed"` (differentiate through the charge solve).
|
|
103
|
+
|
|
104
|
+
Two pair kernels are available: `kernel="shielded"` (default,
|
|
105
|
+
`1/sqrt(r^2 + lambda^2)`) and `kernel="gaussian"` (`erf(r/lambda)/r`,
|
|
106
|
+
Gaussian charge clouds). Fully periodic cells are summed with Ewald
|
|
107
|
+
(`ewald_accuracy`, `ewald_cutoff`). With the Gaussian kernel this is exact to
|
|
108
|
+
`ewald_accuracy`; the shielded kernel's `r^-3` remainder is truncated at
|
|
109
|
+
`shielded_cutoff`, which is approximate (QEq emits an `AccuracyWarning`), so
|
|
110
|
+
prefer `kernel="gaussian"` for periodic systems. The real-space sum uses a
|
|
111
|
+
neighbour list, so memory grows linearly with the number of atoms (the
|
|
112
|
+
`N x N` QEq matrix itself is dense). Mixed boundary conditions are not
|
|
113
|
+
supported.
|
|
114
|
+
|
|
115
|
+
## Training (optional)
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from arrayff.potentials import LJTorch
|
|
119
|
+
|
|
120
|
+
model = LJTorch(species=[18]) # epsilon/sigma trainable
|
|
121
|
+
loss = (model(atoms) - target_energy) ** 2
|
|
122
|
+
loss.backward()
|
|
123
|
+
|
|
124
|
+
model.to(device="cuda", dtype=torch.float32) # tables and inputs follow
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The torch modules (`LJTorch`, `LJ93Torch`, `MorseTorch`, `EMTTorch`,
|
|
128
|
+
`EAMTorch`) follow `module.to(...)`: positions, neighbour tables and cached
|
|
129
|
+
parameters are rebuilt on the module's device and dtype.
|
|
130
|
+
|
|
131
|
+
## Tests and benchmarks
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
pytest -q
|
|
135
|
+
pytest -s src/tests-benchmark/bench_potentials.py
|
|
136
|
+
pytest -s src/tests-benchmark/bench_derivatives.py
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
The benchmark tables compare analytic vs autodiff forces/stress and
|
|
140
|
+
`jit=False/True` across backends.
|
|
141
|
+
|
|
142
|
+
## Status
|
|
143
|
+
|
|
144
|
+
| Potential | State |
|
|
145
|
+
|---|---|
|
|
146
|
+
| Lennard-Jones (OpenKIM universal shifted) | implemented |
|
|
147
|
+
| Lennard-Jones 9-3, Morse | implemented |
|
|
148
|
+
| EMT (matches `ase.calculators.emt`, including `asap_cutoff`) | implemented |
|
|
149
|
+
| EAM (setfl tables, cubic-spline interpolation as in `ase.calculators.eam`, continuous forces) | implemented |
|
|
150
|
+
| QEq (shielded/Gaussian kernels, Ewald for periodic cells, frozen/relaxed response) | implemented |
|
|
151
|
+
| Additive composites (`LJ + QEq`, ...) | implemented |
|
|
152
|
+
| MEAM, UF3, EEM, PQEq | declared placeholders |
|
|
153
|
+
|
|
154
|
+
## Backend and derivative support
|
|
155
|
+
|
|
156
|
+
Every implemented model runs on all three backends and provides energy,
|
|
157
|
+
forces and (for fully periodic cells) stress through both derivative paths.
|
|
158
|
+
Each combination below was checked against the NumPy analytic result.
|
|
159
|
+
|
|
160
|
+
| Model | NumPy | PyTorch | JAX | Analytic forces/stress | Autodiff forces/stress | Trainable torch module |
|
|
161
|
+
|---|---|---|---|---|---|---|
|
|
162
|
+
| `LennardJones` (KIM or explicit sigma/epsilon) | yes | yes | yes | yes | yes | `LJTorch` |
|
|
163
|
+
| `LJ93` | yes | yes | yes | yes | yes | `LJ93Torch` |
|
|
164
|
+
| `Morse` | yes | yes | yes | yes | yes | `MorseTorch` |
|
|
165
|
+
| `EMT` | yes | yes | yes | yes | yes | `EMTTorch` |
|
|
166
|
+
| `EAM` | yes | yes | yes | yes | yes | `EAMTorch` |
|
|
167
|
+
| `QEq` (shielded / Gaussian, cluster / periodic) | yes | yes | yes | yes, envelope theorem | yes, frozen or relaxed charges | - |
|
|
168
|
+
| `CompositePotential` | yes | yes | yes | sum of components | sum of components | - |
|
|
169
|
+
|
|
170
|
+
- `method="analytic"` (default) uses closed-form derivatives. For QEq, the
|
|
171
|
+
analytic forces and stress equal the relaxed-charge result to solver
|
|
172
|
+
accuracy, because the charge-response term vanishes at the QEq minimum.
|
|
173
|
+
- `method="autodiff"` uses autograd for NumPy (optional extra
|
|
174
|
+
`arrayff[autograd]`), `torch.func` for PyTorch and
|
|
175
|
+
`jax.grad` for JAX. Stress is the derivative with respect to a homogeneous
|
|
176
|
+
strain.
|
|
177
|
+
- `method="finite-difference"` is a diagnostic: forces stay analytic and only
|
|
178
|
+
the stress is obtained by central differences.
|
|
179
|
+
- `float32` and accelerators (`dtype=`, `device=`) are supported on PyTorch;
|
|
180
|
+
CPU and Apple MPS are tested.
|
|
181
|
+
|
|
182
|
+
`jit=True` compiles the following paths; everything else runs eagerly and
|
|
183
|
+
gives the same numbers:
|
|
184
|
+
|
|
185
|
+
| Backend | Analytic | Autodiff energy+forces | Autodiff stress |
|
|
186
|
+
|---|---|---|---|
|
|
187
|
+
| NumPy | eager (`jit` is ignored silently) | eager | eager |
|
|
188
|
+
| PyTorch | compiled (`torch.jit.trace`) | compiled energy kernel, then `torch.autograd` | eager |
|
|
189
|
+
| JAX | compiled (`jax.jit`) | compiled `value_and_grad` | eager |
|
|
190
|
+
|
|
191
|
+
For QEq, `jit` applies to the analytic path only.
|
|
@@ -4,7 +4,7 @@ build-backend = "uv_build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "arrayff"
|
|
7
|
-
version = "0.0
|
|
7
|
+
version = "0.1.0"
|
|
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"]
|
|
@@ -15,17 +15,14 @@ keywords = [
|
|
|
15
15
|
"Force Field",
|
|
16
16
|
]
|
|
17
17
|
classifiers = [
|
|
18
|
-
"Development Status ::
|
|
18
|
+
"Development Status :: 3 - Alpha",
|
|
19
19
|
"Topic :: Scientific/Engineering :: Chemistry",
|
|
20
20
|
"Intended Audience :: Science/Research",
|
|
21
21
|
]
|
|
22
22
|
dependencies = [
|
|
23
|
-
"numpy
|
|
23
|
+
"numpy",
|
|
24
24
|
"ase>=3.23",
|
|
25
|
-
"
|
|
26
|
-
"autograd>=1.6",
|
|
27
|
-
"scipy>=1.10,<2",
|
|
28
|
-
"scikit-learn>=1.5",
|
|
25
|
+
"scipy>=1.6",
|
|
29
26
|
]
|
|
30
27
|
|
|
31
28
|
[project.urls]
|
|
@@ -38,12 +35,22 @@ name = "LiuGaoyong"
|
|
|
38
35
|
email = "liugaoyong_88@163.com"
|
|
39
36
|
|
|
40
37
|
[project.optional-dependencies]
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
autograd = ["autograd>=1.6"]
|
|
39
|
+
autoray = ["autoray>=0.6"]
|
|
40
|
+
torch = [
|
|
41
|
+
"torch>=2.3",
|
|
42
|
+
"autoray>=0.6",
|
|
43
|
+
]
|
|
44
|
+
jax = [
|
|
45
|
+
"jax>=0.5",
|
|
46
|
+
"autoray>=0.6",
|
|
47
|
+
]
|
|
48
|
+
sklearn = ["scikit-learn>=1.5"]
|
|
43
49
|
builders = [
|
|
44
50
|
"vesin",
|
|
45
51
|
"matscipy",
|
|
46
52
|
"pymatgen",
|
|
53
|
+
"scikit-learn>=1.5",
|
|
47
54
|
]
|
|
48
55
|
|
|
49
56
|
[dependency-groups]
|
|
@@ -56,10 +63,12 @@ dev = [
|
|
|
56
63
|
"jupyterlab",
|
|
57
64
|
"chemiscope",
|
|
58
65
|
"autograd>=1.6",
|
|
66
|
+
"autoray>=0.6",
|
|
59
67
|
"nglview",
|
|
60
68
|
"jax>=0.5",
|
|
61
69
|
"torch>=2.10",
|
|
62
70
|
"matscipy",
|
|
71
|
+
"scikit-learn>=1.5",
|
|
63
72
|
]
|
|
64
73
|
|
|
65
74
|
[tool.uv]
|
|
@@ -137,10 +146,10 @@ array-api-compat = ">=1.15.0"
|
|
|
137
146
|
array-api-extra = ">=0.11.0"
|
|
138
147
|
array-api-strict = ">=2.5.0"
|
|
139
148
|
autoray = ">=0.1.0"
|
|
140
|
-
|
|
141
|
-
numpy = ">=2.0.0"
|
|
149
|
+
numpy = ">=1.20"
|
|
142
150
|
autograd = ">=1.6"
|
|
143
|
-
scipy = ">=1.
|
|
151
|
+
scipy = ">=1.6.0"
|
|
152
|
+
scikit-learn = ">=1.5.0"
|
|
144
153
|
jupyterlab = ">=4.5.8"
|
|
145
154
|
pytest-xdist = ">=3.8.0"
|
|
146
155
|
nglview = ">=0.14.0"
|
|
@@ -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
|
|
11
|
+
version = "0.1.0"
|
|
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" }]
|
|
@@ -16,27 +16,23 @@ license-files = ["src/LICENSE"]
|
|
|
16
16
|
license = "GPL-3.0-or-later"
|
|
17
17
|
requires-python = ">=3.12"
|
|
18
18
|
keywords = [
|
|
19
|
-
"chemistry",
|
|
19
|
+
"chemistry", #
|
|
20
20
|
"Force Field",
|
|
21
21
|
]
|
|
22
22
|
classifiers = [
|
|
23
|
-
"Development Status ::
|
|
23
|
+
"Development Status :: 3 - Alpha",
|
|
24
24
|
"Topic :: Scientific/Engineering :: Chemistry",
|
|
25
25
|
"Intended Audience :: Science/Research",
|
|
26
26
|
]
|
|
27
|
-
dependencies = [
|
|
28
|
-
"numpy>=2,<3",
|
|
29
|
-
"ase>=3.23",
|
|
30
|
-
"autoray>=0.6",
|
|
31
|
-
"autograd>=1.6",
|
|
32
|
-
"scipy>=1.10,<2",
|
|
33
|
-
"scikit-learn>=1.5",
|
|
34
|
-
]
|
|
27
|
+
dependencies = ["numpy", "ase>=3.23", "scipy>=1.6"]
|
|
35
28
|
|
|
36
29
|
[project.optional-dependencies]
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
30
|
+
autograd = ["autograd>=1.6"]
|
|
31
|
+
autoray = ["autoray>=0.6"]
|
|
32
|
+
torch = ["torch>=2.3", "autoray>=0.6"]
|
|
33
|
+
jax = ["jax>=0.5", "autoray>=0.6"]
|
|
34
|
+
sklearn = ["scikit-learn>=1.5"]
|
|
35
|
+
builders = ["vesin", "matscipy", "pymatgen", "scikit-learn>=1.5"]
|
|
40
36
|
|
|
41
37
|
[dependency-groups]
|
|
42
38
|
dev = [
|
|
@@ -49,10 +45,12 @@ dev = [
|
|
|
49
45
|
"chemiscope",
|
|
50
46
|
# "playwright>=1.52", # chemiscope [headless]
|
|
51
47
|
"autograd>=1.6",
|
|
48
|
+
"autoray>=0.6",
|
|
52
49
|
"nglview",
|
|
53
50
|
"jax>=0.5",
|
|
54
51
|
"torch>=2.10",
|
|
55
52
|
"matscipy",
|
|
53
|
+
"scikit-learn>=1.5",
|
|
56
54
|
]
|
|
57
55
|
|
|
58
56
|
# uv config
|
|
@@ -117,10 +115,10 @@ array-api-compat = ">=1.15.0"
|
|
|
117
115
|
array-api-extra = ">=0.11.0"
|
|
118
116
|
array-api-strict = ">=2.5.0"
|
|
119
117
|
autoray = ">=0.1.0"
|
|
120
|
-
|
|
121
|
-
numpy = ">=2.0.0"
|
|
118
|
+
numpy = ">=1.20"
|
|
122
119
|
autograd = ">=1.6"
|
|
123
|
-
scipy = ">=1.
|
|
120
|
+
scipy = ">=1.6.0"
|
|
121
|
+
scikit-learn = ">=1.5.0"
|
|
124
122
|
jupyterlab = ">=4.5.8"
|
|
125
123
|
pytest-xdist = ">=3.8.0"
|
|
126
124
|
nglview = ">=0.14.0"
|
|
@@ -7,6 +7,8 @@ never import a concrete array library.
|
|
|
7
7
|
|
|
8
8
|
from __future__ import annotations
|
|
9
9
|
|
|
10
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
11
|
+
|
|
10
12
|
from arrayff import utils
|
|
11
13
|
from arrayff.electrostatics import QEq
|
|
12
14
|
from arrayff.potentials import (
|
|
@@ -16,7 +18,10 @@ from arrayff.potentials import (
|
|
|
16
18
|
get_potential,
|
|
17
19
|
)
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
try:
|
|
22
|
+
__version__ = version("arrayff")
|
|
23
|
+
except PackageNotFoundError: # pragma: no cover - source tree without install
|
|
24
|
+
__version__ = "0.0.0+unknown"
|
|
20
25
|
|
|
21
26
|
__all__ = [
|
|
22
27
|
"CompositePotential",
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Ewald summation parameters for periodic charge models.
|
|
2
|
+
|
|
3
|
+
Only the splitting parameter, the real-space cutoff and the *integer*
|
|
4
|
+
reciprocal lattice indices are chosen here (in numpy, from the current cell).
|
|
5
|
+
The real-space pairs come from a neighbour list; kernels rebuild the actual
|
|
6
|
+
vectors from the (possibly traced) cell, so position and strain derivatives
|
|
7
|
+
stay exact.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import dataclasses as dc
|
|
13
|
+
from math import log, pi, sqrt
|
|
14
|
+
|
|
15
|
+
import numpy as np
|
|
16
|
+
|
|
17
|
+
__all__ = ["EwaldSetup", "ewald_setup", "reciprocal_indices"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dc.dataclass(frozen=True)
|
|
21
|
+
class EwaldSetup:
|
|
22
|
+
"""Splitting parameter, cutoffs and reciprocal indices of an Ewald sum.
|
|
23
|
+
|
|
24
|
+
Attributes
|
|
25
|
+
----------
|
|
26
|
+
alpha:
|
|
27
|
+
Ewald splitting parameter in 1/Angstrom.
|
|
28
|
+
cutoff:
|
|
29
|
+
Real-space cutoff in Angstrom (the neighbour-list radius).
|
|
30
|
+
kcut:
|
|
31
|
+
Reciprocal-space cutoff in 1/Angstrom.
|
|
32
|
+
kvectors:
|
|
33
|
+
``(K, 3)`` integer reciprocal lattice indices, origin excluded.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
alpha: float
|
|
37
|
+
cutoff: float
|
|
38
|
+
kcut: float
|
|
39
|
+
kvectors: np.ndarray
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def reciprocal_indices(cell: np.ndarray, kcut: float) -> np.ndarray:
|
|
43
|
+
"""Integer ``m`` with ``0 < |2 pi m @ inv(cell).T| <= kcut``."""
|
|
44
|
+
cell = np.asarray(cell, dtype=float)
|
|
45
|
+
extent = np.ceil(kcut * np.linalg.norm(cell, axis=1) / (2.0 * pi))
|
|
46
|
+
axes = [np.arange(-int(n), int(n) + 1) for n in extent]
|
|
47
|
+
grid = np.stack(np.meshgrid(*axes, indexing="ij"), axis=-1).reshape(-1, 3)
|
|
48
|
+
k = 2.0 * pi * grid @ np.linalg.inv(cell).T
|
|
49
|
+
norm = np.linalg.norm(k, axis=1)
|
|
50
|
+
return grid[(norm > 0.0) & (norm <= kcut)]
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def ewald_setup(
|
|
54
|
+
cell: np.ndarray,
|
|
55
|
+
*,
|
|
56
|
+
accuracy: float = 1e-6,
|
|
57
|
+
real_cutoff: float = 10.0,
|
|
58
|
+
extra_cutoff: float = 0.0,
|
|
59
|
+
) -> EwaldSetup:
|
|
60
|
+
"""Choose ``alpha`` and the k-space indices for a target accuracy.
|
|
61
|
+
|
|
62
|
+
``erfc(alpha * r_c)`` and ``exp(-k_c^2 / 4 alpha^2)`` are both about
|
|
63
|
+
``accuracy``. ``extra_cutoff`` widens the real-space radius for
|
|
64
|
+
additional short-range terms (e.g. the shielding correction).
|
|
65
|
+
"""
|
|
66
|
+
if not 0.0 < accuracy < 1.0:
|
|
67
|
+
raise ValueError("accuracy must be in (0, 1)")
|
|
68
|
+
if real_cutoff <= 0.0:
|
|
69
|
+
raise ValueError("real_cutoff must be positive")
|
|
70
|
+
s = sqrt(-log(accuracy))
|
|
71
|
+
alpha = s / real_cutoff
|
|
72
|
+
kcut = 2.0 * alpha * s
|
|
73
|
+
return EwaldSetup(
|
|
74
|
+
alpha=alpha,
|
|
75
|
+
cutoff=max(real_cutoff, extra_cutoff),
|
|
76
|
+
kcut=kcut,
|
|
77
|
+
kvectors=reciprocal_indices(cell, kcut),
|
|
78
|
+
)
|