xnn.common.models.dispersion_fast.cpu#

D3 and D4 on CPUs: optional C++ and OpenMP kernels (off by default, built on first use).

simple-dftd3 and dftd4 evaluate the dispersion energy in loops over the atoms and their neighbors, energy and gradient in one pass, without tensors per pair or triangle. The reference (DFTD3, DFTD4) evaluates the same formulas as tensor operations; on a CPU its many operations over gathered copies take longer than those loops. These kernels do what the Fortran codes do:

  • the edges are sorted by center, then neighbor (a counting sort and a sort per center);

  • D3: one loop over the centers forms the coordination numbers, the Gaussian reference weights and their derivatives, and with a three-body term the pair C6 and their derivatives with respect to both coordination numbers fill dense (N, N) arrays inside each structure;

  • one loop over the centers sums the two-body energies of their edges (D3: C6 from the weights; D4: from the dynamic polarizabilities), and one sums the three-body energies of their neighbor pairs: a triangle of three distinct atoms once, from its smallest-index corner, with a third of its energy to each atom (the reference’s bookkeeping), and a triangle with a repeated atom (periodic images) from every corner, a third each;

  • the gradient with respect to the edge vectors adds into the center’s own edges (no two threads write the same memory); the derivatives through the C6 collect per thread on the coordination numbers. D3 carries them to the edges in a last loop over the centers; D4 returns them, with the derivatives in the dynamic polarizabilities, to the charges, weights and coordination numbers that the reference code forms.

The forward pass forms the gradient for unit weights on every atom’s energy, which is what a sum of energies (forces, stress) asks for; another weighting evaluates again with those weights. The gradient is not differentiable again, so the kernels serve evaluation. They compute in float64 for float32 inputs as well (scalar code is no slower in it, and the sums of a periodic three-body term cancel to a few digits). The source is compiled with torch.utils.cpp_extension.load_inline() on first use (xnn.common.models.cpu_extension).

Module Attributes

MAX_DENSE_ATOMS

it reads the pair C6 and their derivatives from dense (N, N) arrays (four of them, 2 GB at this size).

Functions

available()

Whether the C++ kernels build here (checked once per process).

d3_direct(term, data)

D3 of a batched graph on the C++ kernels with its forces, without autograd: the energies per atom and per structure (B, 3) (total, two-body, three-body; eV) and the features in the model dtype, the forces (N, 3) (eV / A) and dE / d(strain) (B, 3, 3) (eV) in the dtype of the positions.

d4_direct(term, data)

D4 of a batch (molecules and cells of the dense EEQ regime) on the C++ kernels with its forces and stress: the energies per atom and per structure (B, 4) (total, two-body, three-body, tail = 0; eV), the D4 coordination numbers, the EEQ charges and the dynamic polarizabilities (N, 23) in the model dtype, the forces (N, 3) (eV / A) and dE / d(strain) (B, 3, 3) (eV) in the dtype of the positions.

d4_terms(term, z, edge_index, edge_vec, ...)

The two-body and three-body energies per atom (hartree) and the dynamic polarizabilities (N, 23) of DFTD4 term on the C++ kernels (the reference's values), differentiable once in edge_vec, cn (the D4 coordination numbers) and q (the charges).

evaluate_d3(term, z, edge_index, edge_vec, batch)

The energies per atom (total, two-body, three-body; eV) and the features (N, 2) (coordination number, homoatomic C6) of DFTD3 term on the C++ kernels (the reference's values), in the dtype of edge_vec; raises ValueError for elements without D3 reference data.

Classes

D3CpuFunction(*args, **kwargs)

Energies per atom (total, two-body, three-body; eV) and the features (coordination number, homoatomic C6) from the edge vectors (Angstrom); the gradient is not differentiable again.

D4CpuFunction(*args, **kwargs)

D4's two-body and three-body energies per atom (hartree) and dynamic polarizabilities (N, 23) from the edge vectors (Angstrom), the coordination numbers and the charges; the gradient is not differentiable again.