Torch-DFTD
torch-dftd is a Python library providing a PyTorch implementation of the DFT-D2 and DFT-D3 dispersion correction models for quantum chemistry calculations. It leverages PyTorch's automatic differentiation capabilities to efficiently compute energies, forces, and virials. The library is currently at version 0.5.3, released on March 11, 2026. While it has seen recent updates, its GitHub repository indicates that it is "not actively maintained".
Common errors
-
ModuleNotFoundError: No module named 'ase'
cause The Atomic Simulation Environment (ASE) library, a core dependency for creating and manipulating atomic structures used by torch-dftd's examples and common workflows, is not installed.fixInstall ASE via pip: `pip install ase` -
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0!
cause This is a common PyTorch error indicating that tensors involved in a computation are on different devices (e.g., one on CPU, another on GPU). The `TorchDFTD3Calculator` can be initialized with a device, but input data or other tensors might not be moved accordingly.fixEnsure all relevant tensors (e.g., atomic positions if handled manually) and the `TorchDFTD3Calculator` instance are explicitly moved to the same device (e.g., `device='cuda:0'` for the calculator and `tensor.to(device)` for tensors). The quickstart handles this for the calculator. -
AttributeError: 'Atoms' object has no attribute 'get_potential_energy'
cause The `get_potential_energy()` or `get_forces()` methods are called on an `ase.Atoms` object before a calculator (like `TorchDFTD3Calculator`) has been attached to it using `atoms.set_calculator(calc)`.fixEnsure `atoms.set_calculator(calc)` is called after initializing `TorchDFTD3Calculator` and before attempting to retrieve energies or forces from the `atoms` object.
Warnings
- maintenance The official GitHub repository for torch-dftd explicitly states that the project is 'not actively maintained'. While releases might still occur, active development, bug fixes for new issues, or compatibility updates for future PyTorch/Python versions may be limited.
- gotcha The library is tested against specific Python (3.10) and CUDA (12.2) versions, and specific torch (2.0.1) and ase (3.22.1) versions. Using significantly different versions may lead to unexpected behavior or incompatibilities.
Install
-
pip install torch-dftd
Imports
- TorchDFTD3Calculator
from torch_dftd.torch_dftd3_calculator import TorchDFTD3Calculator
- TorchDFTD2Calculator
from torch_dftd.torch_dftd2_calculator import TorchDFTD2Calculator
Quickstart
import torch
import ase.build
from torch_dftd.torch_dftd3_calculator import TorchDFTD3Calculator
# Create an ASE Atoms object for methanol
atoms = ase.build.molecule("CH3OH")
# Determine device for computation
device = "cuda:0" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
# Initialize the DFTD3 calculator with the atoms object and device
# Common damping functions are 'zero', 'bj', 's6'
calc = TorchDFTD3Calculator(atoms=atoms, device=device, damping="bj")
# Attach the calculator to the atoms object
atoms.set_calculator(calc)
# Get potential energy and forces
energy = atoms.get_potential_energy()
forces = atoms.get_forces()
print(f"Total DFT-D3 energy: {energy:.4f} eV")
print(f"Forces (first atom):
{forces[0]}")