torch-pme enables efficient and auto-differentiable computation of long-range interactions in PyTorch. Auto-differentiation is supported for particle positions,
charges/dipoles, and cell parameters, allowing not only the automatic computation of forces but also enabling general applications in machine learning tasks. For monopoles the library offers classes for Particle-Particle Particle-Mesh Ewald (P3M), Particle Mesh Ewald (PME), standard Ewald, and non-periodic methods. The library has the flexibility to calculate potentials beyond \(1/r\) electrostatics, including arbitrary order \(1/r^p\) potentials. For dipolar interaction we offer to calculate the \(1/r^3\) potential using the standard Ewald method.
Optimized for both CPU and GPU devices, torch-pme is fully TorchScriptable, allowing it to be converted into a format that runs independently of Python, such as in C++, making it ideal for high-performance production environments.
We also provide an experimental implementation for JAX in jax-pme.
Documentation
For details, tutorials, and examples, please have a look at our documentation.
Installation
You can install torch-pme using pip with
pip install torch-pme
or conda
conda install -c conda-forge torch-pme
and import torchpme to use it in your projects!
We also provide bindings to metatensor which can optionally be installed together and used as torchpme.metatensor via
pip install torch-pme[metatensor]
Quickstart
Here is a simple example to get started with torch-pme:
>>> import torch
>>> import torchpme
>>> # Single charge in a cubic box
>>> positions = torch.zeros((1, 3))
>>> cell = 8 * torch.eye(3)
>>> charges = torch.tensor([[1.0]])
>>> # No neighbors for a single atom; use `vesin` for neighbors if needed
>>> neighbor_indices = torch.zeros((0, 2), dtype=torch.int64)
>>> neighbor_distances = torch.zeros((0,))
>>> # Tune P3M parameters
>>> smearing, p3m_parameters, _ = torchpme.tuning.tune_p3m(
... charges=charges,
... cell=cell,
... positions=positions,
... cutoff=5.0,
... neighbor_indices=neighbor_indices,
... neighbor_distances=neighbor_distances,
... )
>>> # Initialize potential and calculator
>>> potential = torchpme.CoulombPotential(smearing)
>>> calculator = torchpme.P3MCalculator(potential, **p3m_parameters)
>>> # Start recording operations done to ``positions``
>>> _ = positions.requires_grad_()
>>> # Compute (per-atom) potentials
>>> potentials = calculator.forward(
... charges=charges,
... cell=cell,
... positions=positions,
... neighbor_indices=neighbor_indices,
... neighbor_distances=neighbor_distances,
... )
>>> # Calculate total energy and forces
>>> energy = torch.sum(charges * potentials)
>>> energy.backward()
>>> forces = -positions.grad
For more examples and details, please refer to the documentation.
Having problems or ideas?
Having a problem with torch-pme? Please let us know by submitting an issue.
Submit new features or bug fixes through a pull request.
Reference
If you use torch-pme for your work, please read and cite our publication available on JCP.
@article{10.1063/5.0251713,
title = {Fast and flexible long-range models for atomistic machine learning},
author = {Loche, Philip and Huguenin-Dumittan, Kevin K. and Honarmand, Melika and Xu, Qianjun and Rumiantsev, Egor and How, Wei Bin and Langer, Marcel F. and Ceriotti, Michele},
journal = {The Journal of Chemical Physics},
volume = {162},
number = {14},
pages = {142501},
year = {2025},
month = {04},
issn = {0021-9606},
doi = {10.1063/5.0251713},
url = {https://doi.org/10.1063/5.0251713},
}
Contributors
Thanks goes to all people that make torch-pme possible:
This project is maintained by @E-Rum, @PicoCentauri, and @sirmarcel, who will reply to issues and pull requests opened on this repository as soon as possible. You can mention them directly if you did not receive an answer after a couple of days.
Metadata
Release files for torch-pme 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| torch_pme-0.5.0.tar.gz | 48.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| torch_pme-0.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 118.4 kB
Release files / torch_pme-0.5.0.tar.gz
| Download URL | torch_pme-0.5.0.tar.gz |
|---|---|
| Size | 48.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b495f55bad25a1ed60699eb03824d6a4fe9fa60fd1684f479147b1354e81b946
|
|
BLAKE2b-256 checksum How to use checksums |
997aa57d426e5a2b950b1cfe7f5753efedc467f03fb238dcc700b8ca3557e764
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 30, 2026.
Transparency logRelease files / torch_pme-0.5.0-py3-none-any.whl
| Download URL | torch_pme-0.5.0-py3-none-any.whl |
|---|---|
| Size | 69.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
41b94968cfa232f89c7664aa9403632360d3b730aa543ddeac00d9109263873d
|
|
BLAKE2b-256 checksum How to use checksums |
adc783cad0e849c4405e01efd5941f109d1cbc46318be781734e57cd6e2015f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 30, 2026.
Transparency log