Torch-DFTD

0.5.3 · maintenance · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a DFT-D3 calculation for a methanol molecule using `torch-dftd` with `ase`. It initializes an `ase.Atoms` object, creates a `TorchDFTD3Calculator`, attaches it to the atoms, and then computes the potential energy and atomic forces. It dynamically selects between CUDA (GPU) and CPU for computation.

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]}")

view raw JSON →