Modelresearch
Behler-Parrinello neural network potential
Also called high-dimensional neural network potential.
Writes the energy as a sum of atomic energies, each a neural network of symmetry functions of the atom's neighborhood, trained on quantum calculations.
The Behler-Parrinello potential is a machine-learned interatomic potential. It writes the energy of atoms as a sum of atomic energies, , and computes each with a small neural network from a fixed-length vector of symmetry functions that describe the neighbors of atom within a cutoff. The weights of the networks are fitted to energies and forces from quantum calculations, usually density functional theory. The result can be close to the quantum energy surface for configurations like those in the training set, at a cost that grows as , but its accuracy is a measurement on a test set, not a property of the model.
The machine
Unknowns. The positions , with and the angle at atom between the directions to and .
Cutoff function. With a cutoff radius ,
It goes to zero with zero slope at , so the energy and the forces are continuous.
Symmetry functions. Each atom gets a vector of functions of its neighborhood that do not change under translation, rotation or exchange of neighbors of the same element. The radial functions are
with a width (in inverse length squared) and a center for each ; with the function is a smooth count of neighbors. The angular functions are
with and , summed over pairs of neighbors of atom . Papers differ on whether each pair is counted once or twice, which changes these functions by a factor 2; an implementation must state its choice, and the network must be trained with the same one.
Atomic networks. For each element there is one feed-forward network , the same for every atom of that element. With one hidden layer of units,
and in practice two hidden layers of 10 to 50 units each are common. The energy is .
Forces. By the chain rule, . The derivatives come from backpropagation through each network, and is known in closed form. The forces are the exact gradient of the model energy, so the energy is conserved in molecular dynamics up to the integrator error.
Parameters, fitted and derived.
- Fitted: all network weights and biases, often thousands per element. They minimize the squared error between the model and the reference energies, often with a weighted squared error of the forces added, on a training set of configurations.
- Chosen, not fitted: the cutoff , the set of symmetry functions and their , , and , and the size of the networks. They are hyperparameters, set by trial and by validation.
- Derived: nothing about the energy itself. The structure of the model, a sum of local atomic energies with the right symmetries, is a design choice motivated by the locality of the quantum energy in many materials.
Standing assumptions. The energy is a sum of local contributions, so any interaction beyond is missing unless a separate term adds it. The model is only as good as its reference method and its training set.
BP-ENERGY-FORCES(r_1..r_N, network N, radial functions g_1..g_M, R_c)- 1
G_i^μ ← 0 for each atom i and each μ - 2
for each pair i < j with r = |r_ij| < R_c: for each μ: G_i^μ ← G_i^μ + g_μ(r); G_j^μ ← G_j^μ + g_μ(r) - 3
for each atom i: E_i ← N(G_i); a_i ← ∂N/∂G at G_i ▷ by backpropagation - 4
U ← Σ_i E_i - 5
for each pair i < j with r < R_c: - 6
g ← Σ_μ (a_i^μ + a_j^μ) g_μ'(r) ▷ dU/dr for this pair - 7
F_i ← F_i - g r_ij / r; F_j ← F_j + g r_ij / r - 8
return U, (E_i), F
The pseudocode and the implementation use radial functions only, as a small toy of the model with given weights; a real potential adds the angular functions, whose forces involve triples, and is trained on quantum data. The structure of the force loop is the same as for the embedded atom method, with the vector in place of the scalar density.
What it counts
The model gives the energy and the forces for any configuration of the elements it was trained on, and through molecular dynamics and Monte Carlo, the properties that follow from them: phase diagrams, melting, defects, diffusion, and reactions that break and form bonds, which a molecular mechanics force field cannot describe. The atomic energies themselves are not measurable quantities; only their sum is fitted.
Cost. At fixed density each atom has a bounded number of neighbors, so computing all , all networks and all forces costs (more precisely with neighbors per atom, because of the angular functions). Per atom this is much more than for a pair potential or EAM, and much less than for density functional theory, whose cost grows about as in standard implementations. This gap is what makes it worth fitting the model.
Relation to other models
- Born-Oppenheimer molecular dynamics computes the same energy surface from the electrons at each step. The Behler-Parrinello potential is a fit of that surface.
- Embedded atom method. Also a sum of atomic energies that depend on the neighborhood, with in place of a network of many symmetry functions. EAM has a few fitted functions; the network has thousands of weights and needs many more reference data.
- Kernel methods. Gaussian approximation potentials and related models use the same decomposition into atomic energies but fit each one by Gaussian process regression or kernel ridge regression on descriptors of the neighborhood.
- Later generations. Behler's review calls this model the second generation. The third generation adds long-range electrostatics with atomic charges that a second set of networks predicts, and the fourth adds charges that are equilibrated over the whole system, for cases where a charge transfer is not local. Message-passing neural networks learn the descriptors instead of fixing them.
Known results in the model
Hand computation of a radial function. Take , , , and an atom with neighbors at distances 1 and 2. Then and , so
With the same neighbors give the smooth count , less than 2 because the cutoff function weights far neighbors less. The tests check these numbers.
Exact properties (theorems about the form).
- Invariance. does not change under translations, rotations and exchanges of atoms of the same element, because each does not and every atom of an element uses the same network. The total force is zero.
- Extensivity and locality. Two copies of a system farther apart than have twice the energy of one, and does not change when an atom moves outside the cutoff sphere of atom . The tests check both. The price is that interactions longer than , such as Coulomb energies between distant charges, are absent.
- Radial functions alone are incomplete. An atom with two neighbors at distance 1 has the same radial functions whether the angle between them is or , so a model with radial functions only gives both the same energy. The angular functions remove this case. Even with angles, a fixed finite set of symmetry functions does not separate every pair of different neighborhoods.
- Approximation. A network with one hidden layer and enough units can approximate any continuous function of on a bounded set to any accuracy (the universal approximation theorem). This says nothing about how many data or units are needed, and nothing about the error of the decomposition into atomic energies itself.
Accuracy (measurements). Behler and Parrinello fitted their 2007 silicon potential to density functional energies and compared its energies and structural properties with those of the reference method. Errors of later potentials are reported as root mean square errors of energy per atom and of force components on test sets held out from training. Such numbers hold only for configurations like the training set. Outside it, at a new pressure, a new phase or an unseen defect, the network extrapolates without warning, which is why training sets are built by repeated sampling of the configurations that the model itself visits.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/behler-parrinello-potential/. The authors are listed in prov/JVQRJ5.jsonl.
"""A small Behler-Parrinello potential for one element, with radial symmetry functions only.
This is a toy of the model: a real potential also uses angular symmetry
functions and is trained on quantum energies and forces. Here the weights are
given, and the code computes the energy and the exact forces.
"""
import math
import random
Point = list[float]
def cutoff(r: float, rc: float) -> float:
return 0.5 * (math.cos(math.pi * r / rc) + 1) if r < rc else 0.0
def dcutoff(r: float, rc: float) -> float:
return -0.5 * math.pi / rc * math.sin(math.pi * r / rc) if r < rc else 0.0
def g2(r: float, eta: float, rs: float, rc: float) -> float:
"""One term exp(-eta (r - rs)^2) f_c(r) of the radial symmetry function G2."""
return math.exp(-eta * (r - rs) ** 2) * cutoff(r, rc)
def dg2(r: float, eta: float, rs: float, rc: float) -> float:
e = math.exp(-eta * (r - rs) ** 2)
return e * (dcutoff(r, rc) - 2 * eta * (r - rs) * cutoff(r, rc))
class AtomicNetwork:
"""E_i = b2 + sum_h w2[h] tanh(b1[h] + sum_m w1[h][m] G[m]), one hidden layer."""
def __init__(self, w1: list[list[float]], b1: list[float], w2: list[float], b2: float):
self.w1, self.b1, self.w2, self.b2 = w1, b1, w2, b2
@classmethod
def random(cls, inputs: int, hidden: int, seed: int) -> "AtomicNetwork":
rng = random.Random(seed)
w1 = [[rng.gauss(0, 1) for _ in range(inputs)] for _ in range(hidden)]
b1 = [rng.gauss(0, 0.5) for _ in range(hidden)]
w2 = [rng.gauss(0, 1) for _ in range(hidden)]
return cls(w1, b1, w2, rng.gauss(0, 0.5))
def energy_and_gradient(self, g: list[float]) -> tuple[float, list[float]]:
"""The output and its gradient with respect to the inputs, by backpropagation."""
t = [math.tanh(b + sum(w * x for w, x in zip(row, g))) for row, b in zip(self.w1, self.b1)]
e = self.b2 + sum(w * x for w, x in zip(self.w2, t))
grad = [0.0] * len(g)
for h, row in enumerate(self.w1):
c = self.w2[h] * (1 - t[h] * t[h])
for m, w in enumerate(row):
grad[m] += c * w
return e, grad
def bp_energy_forces(
positions: list[Point], net: AtomicNetwork, params: list[tuple[float, float]], rc: float
) -> tuple[float, list[float], list[list[float]]]:
"""The energy U = sum_i E_i(G_i) and the forces, with G_i[m] = sum_j g2(r_ij, eta_m, rs_m, rc).
params lists the pairs (eta, rs) of the radial symmetry functions. Returns
(U, atomic energies, forces).
"""
n, m = len(positions), len(params)
gs = [[0.0] * m for _ in range(n)] # @line L1
close = []
for i in range(n): # @line L2
for j in range(i + 1, n): # @line L2
d = [a - b for a, b in zip(positions[i], positions[j])]
r = math.sqrt(sum(x * x for x in d))
if r >= rc: # @line L2
continue
close.append((i, j, r, d))
for k, (eta, rs) in enumerate(params): # @line L2
v = g2(r, eta, rs, rc)
gs[i][k] += v # @line L2
gs[j][k] += v # @line L2
atomic, grads = [], []
for i in range(n): # @line L3
e, a = net.energy_and_gradient(gs[i]) # @line L3
atomic.append(e)
grads.append(a)
u = sum(atomic) # @line L4
forces = [[0.0] * len(positions[0]) for _ in range(n)]
for i, j, r, d in close: # @line L5
g = sum((grads[i][k] + grads[j][k]) * dg2(r, eta, rs, rc) for k, (eta, rs) in enumerate(params)) # @line L6
for q in range(len(d)):
c = g * d[q] / r
forces[i][q] -= c # @line L7
forces[j][q] += c # @line L7
return u, atomic, forces # @line L8
def symmetry_functions(positions: list[Point], i: int, params: list[tuple[float, float]], rc: float) -> list[float]:
"""The vector G_i of atom i, computed directly."""
out = []
for eta, rs in params:
out.append(sum(g2(math.dist(positions[i], p), eta, rs, rc) for j, p in enumerate(positions) if j != i))
return out
test_behler_parrinello_potential.py 113 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/behler-parrinello-potential/. The authors are listed in prov/JVQRJ5.jsonl.
import math
import random
import pytest
from behler_parrinello_potential import AtomicNetwork, bp_energy_forces, cutoff, g2, symmetry_functions
RC = 4.0
PARAMS = [(0.5, 0.0), (1.0, 1.0), (2.0, 1.5), (0.2, 2.5)]
NET = AtomicNetwork.random(len(PARAMS), 5, seed=11)
def cluster(rng, n, side, dmin):
out = []
while len(out) < n:
p = [rng.uniform(0, side) for _ in range(3)]
if all(math.dist(p, q) >= dmin for q in out):
out.append(p)
return out
def energy(pos):
return bp_energy_forces(pos, NET, PARAMS, RC)[0]
def rotate(p, axis_angle):
a, b = axis_angle
ca, sa, cb, sb = math.cos(a), math.sin(a), math.cos(b), math.sin(b)
x, y, z = p
x, y = ca * x - sa * y, sa * x + ca * y
y, z = cb * y - sb * z, sb * y + cb * z
return [x, y, z]
def test_page_example_g2():
assert cutoff(1, RC) == pytest.approx(0.853553, abs=1e-6)
assert cutoff(2, RC) == pytest.approx(0.5)
assert cutoff(4, RC) == 0.0
pos = [[0, 0, 0], [1, 0, 0], [0, 2, 0]]
g = symmetry_functions(pos, 0, [(0.5, 0.0)], RC)[0]
assert g == pytest.approx(0.585374, abs=1e-6)
assert g2(1, 0.5, 0, RC) == pytest.approx(0.517706, abs=1e-6)
assert g2(2, 0.5, 0, RC) == pytest.approx(0.067668, abs=1e-6)
assert symmetry_functions(pos, 0, [(0.0, 0.0)], RC)[0] == pytest.approx(1.353553, abs=1e-6)
def test_forces_match_finite_differences():
rng = random.Random(1)
pos = cluster(rng, 8, 3.5, 0.9)
_, _, f = bp_energy_forces(pos, NET, PARAMS, RC)
eps = 1e-6
for i in range(len(pos)):
for q in range(3):
plus = [p[:] for p in pos]
minus = [p[:] for p in pos]
plus[i][q] += eps
minus[i][q] -= eps
num = -(energy(plus) - energy(minus)) / (2 * eps)
assert f[i][q] == pytest.approx(num, abs=1e-6)
def test_invariance_translation_rotation_permutation():
rng = random.Random(2)
pos = cluster(rng, 9, 3.5, 0.9)
u0 = energy(pos)
shifted = [[x + 0.37, y - 1.2, z + 2.5] for x, y, z in pos]
assert energy(shifted) == pytest.approx(u0, abs=1e-12)
turned = [rotate(p, (0.7, -1.1)) for p in pos]
assert energy(turned) == pytest.approx(u0, abs=1e-12)
perm = pos[:]
rng.shuffle(perm)
assert energy(perm) == pytest.approx(u0, abs=1e-12)
def test_total_force_is_zero():
rng = random.Random(3)
pos = cluster(rng, 10, 3.5, 0.9)
_, _, f = bp_energy_forces(pos, NET, PARAMS, RC)
for q in range(3):
assert abs(sum(row[q] for row in f)) < 1e-10
def test_atomic_energy_is_local():
rng = random.Random(4)
pos = cluster(rng, 6, 3.0, 0.9)
_, e0, _ = bp_energy_forces(pos, NET, PARAMS, RC)
i = 0
c = [sum(p[q] for p in pos) / len(pos) for q in range(3)]
u = [c[q] - pos[i][q] for q in range(3)]
norm = math.sqrt(sum(x * x for x in u))
# a new atom just beyond the cutoff of atom i, past the center of the cluster
extra = [pos[i][q] + (RC + 0.01) * u[q] / norm for q in range(3)]
_, e1, _ = bp_energy_forces(pos + [extra], NET, PARAMS, RC)
assert e1[i] == pytest.approx(e0[i], abs=1e-12)
close = [k for k, p in enumerate(pos) if math.dist(p, extra) < RC]
assert close # some other atom does see it, so the check is not empty
assert any(abs(e1[k] - e0[k]) > 1e-9 for k in close)
def test_energy_is_extensive():
rng = random.Random(5)
pos = cluster(rng, 6, 3.0, 0.9)
u = energy(pos)
copy = [[x + 50, y, z] for x, y, z in pos]
assert energy(pos + copy) == pytest.approx(2 * u, abs=1e-12)
def test_isolated_atom_energy_is_network_at_zero():
e, _ = NET.energy_and_gradient([0.0] * len(PARAMS))
assert energy([[0.0, 0.0, 0.0]]) == pytest.approx(e)
References
- Jörg Behler and Michele Parrinello, Generalized neural-network representation of high-dimensional potential-energy surfaces, Physical Review Letters 98(14), 2007, 146401, the energy as a sum of atomic networks of symmetry functions, and the application to silicon
- Jörg Behler, Atom-centered symmetry functions for constructing high-dimensional neural network potentials, Journal of Chemical Physics 134(7), 2011, 074106, the cutoff function and the radial and angular symmetry functions
- Jörg Behler, Four generations of high-dimensional neural network potentials, Chemical Reviews 121(16), 2021, 10037-10072, the four generations, the training procedure and the treatment of long-range electrostatics
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag JVQRJ5 Edit this page History Report an error Markdown