multipoles
multipoles is a Python package for multipole expansions of the solutions of the Poisson equation (e.g. electrostatic or gravitational potentials). It can handle discrete and continuous charge or mass distributions.
Installation
Simply use pip:
pip install --upgrade multipoles
Documentation
The documentation is available here.
Theory
For a given function $\rho(x,y,z)$, the solution $\Phi(x,y,z)$ of the Poisson equation $\nabla^2\Phi=-4\pi \rho$ with vanishing Dirichlet boundary conditions at infinity is
$$\Phi(x,y,z)=\int d^3r'\frac{\rho(r')}{|r-r'|}$$
Examples of this are the electrostatic and Newtonian gravitational potential. If you need to evaluate $\Phi(x,y,z)$ at many points, calculating the integral for each point is computationally expensive. As a faster alternative, we can express $\Phi(x,y,z)$ in terms of the multipole moments $q_{lm}$ or $I_ {lm}$ (note some literature uses the subscripts $(\cdot)_{nm}$):
$$\Phi(x,y,z)=\sum_{l=0}^\infty\underbrace{\sqrt{\frac{4\pi}{2l+1}}\sum_{m=-l}^lY_{lm}(\theta, \varphi)\frac{q_ {lm}}{r^{l+1}}}_{\Phi^{(l)}}$$
for a exterior expansion, or
$$\Phi(x,y,z)=\sum_{l=0}^\infty\underbrace{\sqrt{\frac{4\pi}{2l+1}}\sum_{m=-l}^lY_{lm}(\theta, \varphi)I_{lm}r^{l}}_ {\Phi^{(l)}}$$
for an interior expansion; where $r, \theta, \varphi$ are the usual spherical coordinates corresponding to the cartesian coordinates $x, y, z$ and $Y_{lm}(\theta, \varphi)$ are the spherical harmonics.
The multipole moments for the exterior expansion are:
$$q_{lm} = \sqrt{\frac{4\pi}{2l+1}}\int d^3 r' \rho(r')r'^l Y^*_{lm}(\theta', \varphi')$$
and the multipole moments for the interior expansion are:
$$I_{lm} = \sqrt{\frac{4\pi}{2l+1}}\int d^3 r' \frac{\rho(r')}{r'^{l+1}} Y^*_{lm}(\theta', \varphi')$$
This approach is usually much faster because the contributions $\Phi^{(l)}$ are getting smaller with increasing l. So we just have to calculate a few integrals for obtaining some $q_{lm}$ or $I_{lm}$.
Some literature considers the $\sqrt{\frac{4\pi}{2l+1}}$ as part of the definition of $Y_{lm}(\theta, \varphi)$.
Examples
Discrete Charge Distribution
As example for a discrete charge distribution we model two point charges with positive and negative unit charge located on the z-axis:
from multipoles import MultipoleExpansion
# Prepare the charge distribution dict for the MultipoleExpansion object:
charge_dist = {
'discrete': True, # point charges are discrete charge distributions
'charges': [
{'q': 1, 'xyz': (0, 0, 1)},
{'q': -1, 'xyz': (0, 0, -1)},
]
}
l_max = 2 # where to stop the infinite multipole sum; here we expand up to the quadrupole (l=2)
Phi = MultipoleExpansion(charge_dist, l_max)
# We can evaluate the multipole expanded potential at a given point like this:
x, y, z = 30.5, 30.6, 30.7
value = Phi(x, y, z)
# The multipole moments are stored in a dict, where the keys are (l, m) and the values q_lm:
Phi.multipole_moments
Continuous Charge Distribution
As an example for a continuous charge distribution, we smear out the point charges from the previous example:
from multipoles import MultipoleExpansion
import numpy as np
# First we set up our grid, a cube of length 10 centered at the origin:
npoints = 101
edge = 10
x, y, z = [np.linspace(-edge / 2., edge / 2., npoints)] * 3
XYZ = np.meshgrid(x, y, z, indexing='ij')
# We model our smeared out charges as gaussian functions:
def gaussian(XYZ, xyz0, sigma):
g = np.ones_like(XYZ[0])
for k in range(3):
g *= np.exp(-(XYZ[k] - xyz0[k]) ** 2 / sigma ** 2)
g *= (sigma ** 2 * np.pi) ** -1.5
return g
sigma = 1.5 # the width of our gaussians
# Initialize the charge density rho, which is a 3D numpy array:
rho = gaussian(XYZ, (0, 0, 1), sigma) - gaussian(XYZ, (0, 0, -1), sigma)
# Prepare the charge distribution dict for the MultipoleExpansion object:
charge_dist = {
'discrete': False, # we have a continuous charge distribution here
'rho': rho,
'xyz': XYZ
}
# The rest is the same as for the discrete case:
l_max = 2 # where to stop the infinite multipole sum; here we expand up to the quadrupole (l=2)
Phi = MultipoleExpansion(charge_dist, l_max)
x, y, z = 30.5, 30.6, 30.7
value = Phi(x, y, z)
Metadata
Release files for multipoles 0.4.2
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 |
|---|---|---|---|---|
| multipoles-0.4.2.tar.gz | 145.3 kB | Without provenance | Details for multipoles-0.4.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| multipoles-0.4.2-py3-none-any.whl | Python 3 | none | any | Details for multipoles-0.4.2-py3-none-any.whl |
Total release size: 154.6 kB
Release files / multipoles-0.4.2.tar.gz
| Download URL | multipoles-0.4.2.tar.gz |
|---|---|
| Size | 145.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
685a8be989a667bf89698acd097b3a21a827cb11ae5ca4cb742a0dc890e7942d
|
|
BLAKE2b-256 checksum How to use checksums |
cd0fab120d0f2164c5fdace6cc7c3515518461a35036a3615d0e915d4a4e41c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.0
|
Release files / multipoles-0.4.2-py3-none-any.whl
| Download URL | multipoles-0.4.2-py3-none-any.whl |
|---|---|
| Size | 9.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
292566df358957a3c2f216aa471089f78714ed017e2aa5d6022111551970afd7
|
|
BLAKE2b-256 checksum How to use checksums |
0af938f5aaaab538b920d28cb17785e00a26afcc6799150c7486e8161655f0bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.0
|
Security for multipoles 0.4.2
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.