Skip to main content

Torch Autodiff Multicharge

Compatibility: Python Versions PyTorch Versions
Availability: Release PyPI Conda Version Apache-2.0
Status: Test Status Ubuntu Test Status macOS (ARM) Test Status Windows Build Status Documentation Status pre-commit.ci Status Coverage

PyTorch implementation of the electronegativity equilibration (EEQ) model for atomic partial charges. This module allows to process a single structure or a batch of structures for the calculation of atom-resolved dispersion energies.

If you use this software, please cite the following publication

  • M. Friede, C. Hölzer, S. Ehlert, S. Grimme, J. Chem. Phys., 2024, 161, 062501. DOI: 10.1063/5.0216715

For details on the EEQ model, see

  • S. A. Ghasemi, A. Hofstetter, S. Saha, and S. Goedecker, Phys. Rev. B, 2015, 92, 045131. DOI: 10.1103/PhysRevB.92.045131

  • E. Caldeweyher, S. Ehlert, A. Hansen, H. Neugebauer, S. Spicher, C. Bannwarth and S. Grimme, J. Chem. Phys., 2019, 150, 154122. DOI: 10.1063/1.5090222

For alternative implementations, also check out

  • multicharge: Implementation of the EEQ model in Fortran.

Installation

pip

tad-multicharge can easily be installed with pip.

pip install tad-multicharge

conda

tad-multicharge is also available from conda.

conda install tad-multicharge

From source

This project is hosted on GitHub at tad-mctc/tad-multicharge. Obtain the source by cloning the repository with

git clone https://github.com/tad-mctc/tad-multicharge
cd tad-multicharge

We recommend using a conda environment to install the package. You can setup the environment manager using a mambaforge installer. Install the required dependencies from the conda-forge channel.

mamba env create -n torch -f environment.yaml
mamba activate torch

Install this project with pip in the environment

pip install .

The following dependencies are required

Compatibility

PyTorch \ Python 3.10 3.11 3.12 3.13 3.14
2.7.1 ✔️ ✔️ ✔️ ✅ ❌
2.8.0 ✔️ ✔️ ✔️ ✅ ❌
2.9.1 ✔️ ✔️ ✔️ ✔️ ✅
2.10.0 ✔️ ✔️ ✔️ ✔️ ✅
2.11.0 ✔️ ✔️ ✔️ ✔️ ✅
2.12.1 ✔️ ✔️ ✔️ ✔️ ✅
2.13.0 ✔️ ✔️ ✔️ ✔️ ✅
2.14.0 ✅ ✅ ✅ ✅ ✅

✅ tested in CI    ✔️ supported, but not tested in CI (should still work)    ❌ not supported

Note that only the latest bug fix version is listed, but all preceding bug fix minor versions are supported. For example, although only version 2.7.1 is listed, version 2.7.0 is also supported.

Development

For development, additionally install the following tools in your environment.

mamba install black covdefaults mypy pre-commit pylint pytest pytest-cov pytest-xdist tox
pip install pytest-random-order

With pip, add the option -e for installing in development mode, and add [dev] for the development dependencies

pip install -e .[dev]

The pre-commit hooks are initialized by running the following command in the root of the repository.

pre-commit install

For testing all Python environments, simply run tox.

tox

Note that this randomizes the order of tests but skips "large" tests. To modify this behavior, tox has to skip the optional posargs.

tox -- test

Examples

The following example shows how to calculate the EEQ partial charges and the corresponding electrostatic energy for a single structure.

import torch
from tad_mctc.io.structure import Structure
from tad_multicharge import eeq

numbers = torch.tensor([7, 7, 1, 1, 1, 1, 1, 1])

# coordinates in Bohr
positions = torch.tensor(
    [
        [-2.98334550857544, -0.08808205276728, +0.00000000000000],
        [+2.98334550857544, +0.08808205276728, +0.00000000000000],
        [-4.07920360565186, +0.25775116682053, +1.52985656261444],
        [-1.60526800155640, +1.24380481243134, +0.00000000000000],
        [-4.07920360565186, +0.25775116682053, -1.52985656261444],
        [+4.07920360565186, -0.25775116682053, -1.52985656261444],
        [+1.60526800155640, -1.24380481243134, +0.00000000000000],
        [+4.07920360565186, -0.25775116682053, +1.52985656261444],
    ]
)

# an absent `charge` means neutral
structure = Structure(numbers=numbers, positions=positions)
cn = torch.tensor([3.0, 3.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])

eeq_model = eeq.EEQModel.param2019()
qat, energy = eeq_model.solve(structure, cn, return_energy=True)

print(torch.sum(energy, -1))
# tensor(-0.1750)
print(qat)
# tensor([-0.8347, -0.8347,  0.2731,  0.2886,  0.2731,  0.2731,  0.2886,  0.2731])

The next example shows the calculation of the electrostatic energy with a simpler API for a batch of structures. The coordination number is computed internally with tad_mctc.ncoord.cn_eeq.

import torch
from tad_mctc.batch import pack
from tad_mctc.convert import symbol_to_number
from tad_mctc.io.structure import Structure
from tad_multicharge import eeq

# S22 system 4: formamide dimer
numbers = pack(
    (
        symbol_to_number("C C N N H H H H H H O O".split()),
        symbol_to_number("C O N H H H".split()),
    )
)

# coordinates in Bohr
positions = pack(
    (
        torch.tensor(
            [
                [-3.81469488143921, +0.09993441402912, 0.00000000000000],
                [+3.81469488143921, -0.09993441402912, 0.00000000000000],
                [-2.66030049324036, -2.15898251533508, 0.00000000000000],
                [+2.66030049324036, +2.15898251533508, 0.00000000000000],
                [-0.73178529739380, -2.28237795829773, 0.00000000000000],
                [-5.89039325714111, -0.02589114569128, 0.00000000000000],
                [-3.71254944801331, -3.73605775833130, 0.00000000000000],
                [+3.71254944801331, +3.73605775833130, 0.00000000000000],
                [+0.73178529739380, +2.28237795829773, 0.00000000000000],
                [+5.89039325714111, +0.02589114569128, 0.00000000000000],
                [-2.74426102638245, +2.16115570068359, 0.00000000000000],
                [+2.74426102638245, -2.16115570068359, 0.00000000000000],
            ]
        ),
        torch.tensor(
            [
                [-0.55569743203406, +1.09030425468557, 0.00000000000000],
                [+0.51473634678469, +3.15152550263611, 0.00000000000000],
                [+0.59869690244446, -1.16861263789477, 0.00000000000000],
                [-0.45355203669134, -2.74568780438064, 0.00000000000000],
                [+2.52721209544999, -1.29200800956867, 0.00000000000000],
                [-2.63139587595376, +0.96447869452240, 0.00000000000000],
            ]
        ),
    )
)

# total charge of both systems
charge = torch.tensor([0.0, 0.0])
structure = Structure(numbers=numbers, positions=positions, charge=charge)

# calculate electrostatic energy in Hartree
energy = torch.sum(eeq.get_energy(structure), -1)

print(energy)
# tensor([-0.2087, -0.0972])
print(energy[0] - 2 * energy[1])
# tensor(-0.0143)

Periodic structures (periodic along all three lattice vectors) are handled by the same call: a Structure with a lattice is evaluated with an Ewald summation, as in multicharge. The derivatives with respect to the lattice vectors give the virial.

import torch
from tad_mctc.data.structures import get_structure
from tad_multicharge import eeq

# CO2 crystal of the X23 set, positions and lattice vectors (as rows) in Bohr
structure = get_structure("x23", "CO2", dtype=torch.double)

positions = structure.positions.clone().requires_grad_(True)
lattice = structure.lattice.clone().requires_grad_(True)
structure = structure.replace(positions=positions, lattice=lattice)

model = eeq.EEQModel.param2019(dtype=torch.double)
qat, energy = model(structure, return_energy=True)

print(energy.sum())
# tensor(-0.1245, dtype=torch.float64, grad_fn=<SumBackward0>)

energy.sum().backward()
virial = positions.mT @ positions.grad + lattice.mT @ lattice.grad
print(virial.diagonal())
# tensor([0.0378, 0.0378, 0.0378], dtype=torch.float64, grad_fn=<DiagonalBackward0>)

The model is a frozen Node (a pytree), so it works with torch.func.vmap, jacrev, jacfwd and torch.compile(fullgraph=True). For example, the derivative of the energy with respect to all parameters is a single jacrev over the model, and a different coordination number is set with replace.

import torch
from tad_mctc.ncoord import cn_eeq
from tad_multicharge import eeq

model = eeq.EEQModel.param2019().replace(cn=cn_eeq.replace(cutoff=20.0))

grad = torch.func.jacrev(
    lambda m: m(structure, return_energy=True)[1].sum()
)(model)
print(grad.chi.shape)
# torch.Size([104])

For a periodic structure under these transforms, the coordination number needs its periodic image shifts built ahead of time, which are passed to the model call: model(structure, build_periodic_shifts(lattice, periodic, cn_eeq.cutoff)) (see tad_mctc.neighbor.build_periodic_shifts).

Choosing an EEQ solver

Cholesky–Schur (solve_mode="schur") remains the recommended default. The opt-in solve_mode="cg" uses Jacobi CG with implicit derivatives and still constructs the dense Coulomb matrix, requiring quadratic memory. Pass the keyword to the model, solve, get_eeq, get_charges or get_energy. CG checks the reconstructed KKT stationarity and total charge, in addition to both auxiliary solves; failed checks raise eagerly and produce NaNs for affected systems under vmap or compilation.

Higher derivatives work in eager mode; compiled higher derivatives are unsupported, and compiled first-order jacrev requires PyTorch 2.11 or newer. Under vmap and compilation, CG runs the full upstream iteration budget (currently twice the atom count).

Contributing

This is a volunteer open source projects and contributions are always welcome. Please, take a moment to read the contributing guidelines.

License

This project is licensed under the Apache License, Version 2.0 (the "License"); you may not use this project's files except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Metadata

Release files for tad-multicharge 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tad-multicharge 0.8.0
File Size Uploaded Provenance Actions
tad_multicharge-0.8.0.tar.gz 35.6 kB With provenance Details for tad_multicharge-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tad-multicharge 0.8.0
File Interpreter ABI Platform Actions
tad_multicharge-0.8.0-py3-none-any.whl Python 3 none any Details for tad_multicharge-0.8.0-py3-none-any.whl

Total release size: 72.5 kB

Release files / tad_multicharge-0.8.0.tar.gz

Download URL tad_multicharge-0.8.0.tar.gz
Size 35.6 kB
Tags Source
SHA-256 checksum
How to use checksums
285f825193c02dd2d35170328e13dabaf420a76496188df08b0b675815a28305
BLAKE2b-256 checksum
How to use checksums
6fb3a3dd9802007882066fc03f00f689e7bd207f7144cbd36a0d2de277ca570a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / tad_multicharge-0.8.0-py3-none-any.whl

Download URL tad_multicharge-0.8.0-py3-none-any.whl
Size 36.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f7e494f4dbbffd235601e3c8767c029c4f4348055060af99b3323e1428bf634
BLAKE2b-256 checksum
How to use checksums
5e40915afc29bdfe8ea65c9ff6260d3380827720e770e366411919741e2eb1f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Security for tad-multicharge 0.8.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tad_multicharge-0.8.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page