Python library for differentiable electrodynamics simulations with jax.
Project description
Electromagnetics simulation library for moving point charges built on JAX
[!IMPORTANT] Major update (v2): PyCharge has been rewritten in JAX with significant API changes. The legacy NumPy-based version remains available under the
v1.1.0tag.
Key Features
- ⚡️ Point Charge Electrodynamics: Compute relativistically-correct electromagnetic potentials and fields generated by moving point charges.
- ⚛️ Self-Consistent N-Body Electrodynamics: Simulate the dynamics of electromagnetic sources—such as free charges and dipoles—interacting through their self-generated fields.
- 🔥 Powered by JAX: Leverage JAX's just-in-time (XLA) compilation for GPU/TPU acceleration, vectorization, and scalable parallel execution.
- 🛠️ Fully Differentiable: End-to-end differentiability enables gradient-based optimization, control, and inverse design workflows.
Learn more about PyCharge in our research paper published in Computational Physics Communications and available on arXiv
Installation
PyCharge is available on PyPI and can be installed with:
pip install pycharge
Documentation
Read the full documentation at pycharge.readthedocs.io.
Usage
This example shows how to simulate an oscillating charge using PyCharge, computing and visualizing the scalar potential along the x-y axis at z=0 and t=0:
import jax
import jax.numpy as jnp
import matplotlib.pyplot as plt
from scipy.constants import e
from pycharge import Charge, potentials_and_fields
# Define an oscillating point charge with charge +e.
oscillating_charge = Charge(lambda t: [2e-9 * jnp.sin(7.5e16 * t), 0.0, 0.0], e)
# Define 1D axes for the observation points (x, y) and fix z, t.
lim, grid_size = 50e-9, 1000
x = jnp.linspace(-lim, lim, grid_size)
y = jnp.linspace(-lim, lim, grid_size)
z = jnp.array([0.0])
t = jnp.array([0.0])
# Form the full 4D spacetime grid (X, Y, Z, T).
X, Y, Z, T = jnp.meshgrid(x, y, z, t, indexing="ij")
# Build and JIT-compile a function that returns potentials and fields.
quantities_fn = potentials_and_fields([oscillating_charge])
jit_quantities_fn = jax.jit(quantities_fn)
# Evaluate all quantities on the grid (returns a `Quantities` object).
quantities = jit_quantities_fn(X, Y, Z, T)
# Extract and plot the scalar potential on the x–y plane at z=0, t=0.
scalar_2d = quantities.scalar[:, :, 0, 0]
plt.imshow(scalar_2d.T, vmax=0.2)
plt.colorbar(label="Scalar Potential (V)")
For more examples and detailed usage, please refer to the documentation.
Contributing
We welcome contributions! See our Contributing Guide for details.
Citing PyCharge
If you use PyCharge in your research, please cite our paper.
@article{filipovich2022pycharge,
title={PyCharge: An open-source Python package for self-consistent electrodynamics simulations of Lorentz oscillators and moving point charges},
author={Matthew J. Filipovich and S. Hughes},
year={2022},
journaltitle = {Computer Physics Communications},
volume = {274},
pages = {108291},
issn = {00104655},
doi = {10.1016/j.cpc.2022.108291},
url={https://doi.org/10.1016/j.cpc.2022.108291},
}
License
PyCharge is distributed under the MIT License. See the LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pycharge-2.0.1.tar.gz.
File metadata
- Download URL: pycharge-2.0.1.tar.gz
- Upload date:
- Size: 4.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81969ac7c68c4b069cc8bc711bd48d8285cb3fffa4cd8cbe4f35f70e219cfde2
|
|
| MD5 |
8876ecd516ca7a3674ad694530c4f928
|
|
| BLAKE2b-256 |
851df5c9aacc4152affa84fde3c7afa0e1118c2d90fa61180c47a76e803b3f27
|
Provenance
The following attestation bundles were made for pycharge-2.0.1.tar.gz:
Publisher:
publish.yml on MatthewFilipovich/pycharge
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pycharge-2.0.1.tar.gz -
Subject digest:
81969ac7c68c4b069cc8bc711bd48d8285cb3fffa4cd8cbe4f35f70e219cfde2 - Sigstore transparency entry: 819449061
- Sigstore integration time:
-
Permalink:
MatthewFilipovich/pycharge@03b0d5b1c727768e90dcd8dcb9f56d2cecf21a3b -
Branch / Tag:
refs/tags/v2.0.1 - Owner: https://github.com/MatthewFilipovich
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@03b0d5b1c727768e90dcd8dcb9f56d2cecf21a3b -
Trigger Event:
release
-
Statement type:
File details
Details for the file pycharge-2.0.1-py3-none-any.whl.
File metadata
- Download URL: pycharge-2.0.1-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b91ae216cc00a4ad69559171e9d23be417174c555417b2329f0c01b990334c9
|
|
| MD5 |
b5e0c389b0efa91bd5890e06fdad594b
|
|
| BLAKE2b-256 |
066427995be6ad3a3b9b2b1d1a0a6a06a11f9e3fccbb2bce2cdbd8a25e8db5d3
|
Provenance
The following attestation bundles were made for pycharge-2.0.1-py3-none-any.whl:
Publisher:
publish.yml on MatthewFilipovich/pycharge
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pycharge-2.0.1-py3-none-any.whl -
Subject digest:
4b91ae216cc00a4ad69559171e9d23be417174c555417b2329f0c01b990334c9 - Sigstore transparency entry: 819449087
- Sigstore integration time:
-
Permalink:
MatthewFilipovich/pycharge@03b0d5b1c727768e90dcd8dcb9f56d2cecf21a3b -
Branch / Tag:
refs/tags/v2.0.1 - Owner: https://github.com/MatthewFilipovich
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@03b0d5b1c727768e90dcd8dcb9f56d2cecf21a3b -
Trigger Event:
release
-
Statement type: