torch-calculate-electrostatic-potential
Differentiable 2D projected and 3D electrostatic potentials from Peng 1996 electron-scattering factors.
The high-level API consumes
torch_structure_manipulation.AtomicStructure. The tensor-only
calculate_scattering_potential_2d and calculate_scattering_potential_3d
kernels remain public and support arbitrary leading batch dimensions.
Coordinates and spacing are in Angstroms. Axis order is ZYX in 3D and YX in 2D.
Units and normalization
peng1996_element_params.json contains Peng et al. (1996) elastic electron
scattering factors, not X-ray form factors:
f_e(s) = sum_i a_i exp(-b_i s^2), s = sin(theta) / wavelength
The amplitudes a_i and f_e are in Angstroms and b_i is in Angstroms
squared. The X-ray-to-electron Mott-Bethe conversion
f_e(s) = 0.023934 (Z - f_X(s)) / s^2 is therefore already incorporated in the
tabulated coefficients and must not be applied again.
An electron scattering factor is not itself a real-space potential in volts. The package converts it to the Fourier transform of the electrostatic potential using
V_tilde(g) = C f_e(g / 2), g = 2s
C = 2 pi hbar^2 / (m_e e) = 47.877647... V Angstrom^2
The inverse transform returned by calculate_scattering_potential_3d and
potential_from_structure_3d is therefore in volts. The 2D functions
analytically integrate the 3D potential over the omitted spatial axis and
return a projected potential in volt-Angstroms.
The bonded coefficients come from
Shtyrov et al. (2026)
and use the equivalent convention f_e(g) = sum_i a_i exp(-b_i g^2 / 4).
Protein and RNA currently share the same coefficient table because RNA-specific
factors have not yet been measured.
Installation
# From PyPI (after first release)
pip install torch-calculate-electrostatic-potential
# Development install from the monorepo
pip install -e packages/primitives/torch-calculate-electrostatic-potential
With uv: uv pip install torch-calculate-electrostatic-potential.
Usage
from torch_calculate_electrostatic_potential import (
GridConfig,
potential_from_structure_2d,
potential_from_structure_3d,
)
from torch_structure_manipulation import AtomicStructure
structure = AtomicStructure.from_dataframe(atoms, device="cuda")
grid_3d = GridConfig.from_grid_shape_and_voxel_size(
grid_shape=(128, 128, 128),
voxel_size=(1.0, 1.0, 1.0),
center_zyx=(0.0, 0.0, 0.0),
sublattice_radius=5.0,
)
volume = potential_from_structure_3d(
structure,
grid_3d,
scattering_factors="peng_bonded",
bonded_fallback="elemental",
)
grid_2d = GridConfig.from_grid_shape_and_voxel_size(
grid_shape=(128, 128),
voxel_size=(1.0, 1.0),
center_yx=(0.0, 0.0),
)
projected = potential_from_structure_2d(structure, grid_2d)
scattering_factors="peng_elemental" is the default and ignores bonding
metadata. "peng_bonded" must be selected explicitly and requires
bonded_environments and per-atom molecule_types. Unsupported other
molecules and absent keys either emit one warning and use elemental values
(bonded_fallback="elemental") or raise (bonded_fallback="error").
The molecule type is the scattering-factor provider key, not merely descriptive metadata. Custom providers can therefore supply different tables for protein, RNA, or any additional molecule type:
Batched structures and bonded factors
AtomicStructure may carry broadcast-compatible batch dimensions on positions
and other numerical fields. The tensor kernels and elemental Peng lookup support
that directly.
Bonded factors are different:
bonded_environmentsandmolecule_typesare flat tuples (one string per atom), shared across the whole batch.resolve_scattering_parameters(..., scattering_factors="peng_bonded")requires one-dimensionalatomic_numberswith shape(n_atoms,).
Practical guidance:
| Use case | Elemental | Bonded |
|---|---|---|
| Single structure | yes | yes |
Multiple poses, same chemistry (positions batched, atomic_numbers (n,)) |
yes | yes |
Batched atomic_numbers with shape (batch, n_atoms) |
yes | no — raises |
| Different chemistry per batch member | N/A | no — not representable |
For different structures, call potential_from_structure_3d once per
AtomicStructure (or loop over batch indices).
from torch_calculate_electrostatic_potential import BondedScatteringFactorTable
custom_factors = {
"protein": BondedScatteringFactorTable(
parameters_a=protein_parameters_a,
parameters_b=protein_parameters_b,
),
"rna": BondedScatteringFactorTable(
parameters_a=rna_parameters_a,
parameters_b=rna_parameters_b,
),
}
volume = potential_from_structure_3d(
structure,
grid_3d,
scattering_factors=custom_factors,
bonded_fallback="error",
)
Each parameter mapping is keyed by the structure's bonded_environments
strings. The low-level tensor API remains available for callers that have
already resolved arbitrary per-atom a and b tensors.
The lower-level route exposes parameter tensors directly:
from torch_calculate_electrostatic_potential import (
calculate_scattering_potential_3d,
get_peng_scattering_parameters,
)
atom_params_a, atom_params_b = get_peng_scattering_parameters(atomic_numbers)
potential_volume = calculate_scattering_potential_3d(
atom_pos_zyx,
atom_bfactors,
atom_params_a,
atom_params_b,
grid_3d,
atom_occupancies=occupancies,
)
Positions, B-factors, occupancies, and explicit parameter tensors remain
differentiable. sublattice_radius controls the finite local stencil; increase
it for broad Gaussians.
Testing
Install the package together with test dependencies:
pip install "torch-calculate-electrostatic-potential[test]" @ git+https://github.com/teamtomo/torch-calculate-electrostatic-potential.git
pytest
With coverage: pytest --cov=torch_calculate_electrostatic_potential --cov-report=html.
Requirements
- Python >= 3.11
- PyTorch >= 2.0
- torch-structure-manipulation, numpy, einops, tqdm
License
BSD 3-Clause License
Metadata
Release files for torch-calculate-electrostatic-potential 0.6.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 | Provenance | Actions |
|---|---|---|---|---|
| torch_calculate_electrostatic_potential-0.6.0.tar.gz | 34.8 kB | With provenance | Details for torch_calculate_electrostatic_potential-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_calculate_electrostatic_potential-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for torch_calculate_electrostatic_potential-0.6.0-py3-none-any.whl |
Total release size: 67.9 kB
Release files / torch_calculate_electrostatic_potential-0.6.0.tar.gz
| Download URL | torch_calculate_electrostatic_potential-0.6.0.tar.gz |
|---|---|
| Size | 34.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0ed79a865daec48b77b2003798230349a23830ba509a353f1f045959c64223f5
|
|
BLAKE2b-256 checksum How to use checksums |
007a1cc2d6d4a9d3db71a5f9513a152ca05d763dc9782655d8b902c4c23d5501
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 11, 2026.
Transparency logRelease files / torch_calculate_electrostatic_potential-0.6.0-py3-none-any.whl
| Download URL | torch_calculate_electrostatic_potential-0.6.0-py3-none-any.whl |
|---|---|
| Size | 33.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
66350df095a2ff09ddb49a407403c763c78d9ebb62f4dbc33cf69c64f2270b77
|
|
BLAKE2b-256 checksum How to use checksums |
31eb506dabeaf8d575979a9bf0a58ae1309528556f971b4984dd9daee8cd9063
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 11, 2026.
Transparency logSecurity for torch-calculate-electrostatic-potential 0.6.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.
Attestation for torch_calculate_electrostatic_potential-0.6.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 Sep 11, 2026.
Transparency logA 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.