Skip to main content

Particle-mesh based calculations of long-range interactions in PyTorch

Project description

https://raw.githubusercontent.com/lab-cosmo/torch-pme/refs/heads/main/docs/logo/torch-pme.svg

Github Actions Tests Job Status Code coverage Documentation

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:

https://contrib.rocks/image?repo=lab-cosmo/torch-pme

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_pme-0.4.0.tar.gz (47.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_pme-0.4.0-py3-none-any.whl (68.1 kB view details)

Uploaded Python 3

File details

Details for the file torch_pme-0.4.0.tar.gz.

File metadata

  • Download URL: torch_pme-0.4.0.tar.gz
  • Upload date:
  • Size: 47.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for torch_pme-0.4.0.tar.gz
Algorithm Hash digest
SHA256 df5e8233edb0a986088e6457467e4f49a42870a491b928802979448a44f62740
MD5 7e6809fc7bee7b6d54e050dd3db214a9
BLAKE2b-256 936dd09f813ee5dda8e1e3027173e576ac4334b79fab23d87b3c7b8581d82b48

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_pme-0.4.0.tar.gz:

Publisher: release.yml on lab-cosmo/torch-pme

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torch_pme-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: torch_pme-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 68.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for torch_pme-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ce9eca8ac54959381c1c899b779d03b59514485ec3c7bf55fb818c73ff1f304
MD5 93b6acf38910acac009e816e2623afd6
BLAKE2b-256 588adc7b26e854184fa67a35fc3f20fe3640bec91f137fc290bffeaf6d8b4aea

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_pme-0.4.0-py3-none-any.whl:

Publisher: release.yml on lab-cosmo/torch-pme

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page