Theory
The Multipole Expansion
For a given charge density \(\rho(\mathbf{r})\), the solution \(\Phi(\mathbf{r})\) of the Poisson equation \(\nabla^2\Phi=-4\pi \rho\) with vanishing Dirichlet boundary conditions at infinity is
Examples of this are the electrostatic and the Newtonian gravitational potential (see Units and Gravity for the constants). If you need to evaluate \(\Phi(\mathbf{r})\) at many points, calculating the integral for each point is computationally expensive. As a faster alternative, we can express \(\Phi\) in terms of the multipole moments \(q_{lm}\) or \(I_{lm}\) (note some literature uses the subscripts \((\cdot)_{nm}\)):
for an exterior expansion, or
for an interior expansion. Here \(r, \theta, \varphi\) are the spherical coordinates of \(\mathbf{r}\) relative to the expansion center (by default the center of charge, weighted by \(|\rho|\)), and \(Y_{lm}(\theta, \varphi)\) are the spherical harmonics.
The multipole moments for the exterior expansion are
and the multipole moments for the interior expansion are
For point charges, the integrals become sums over the charges.
This approach is usually much faster, because the contributions \(\Phi^{(l)}\) get smaller with increasing \(l\). So we only have to calculate a few integrals to obtain some \(q_{lm}\) or \(I_{lm}\), and can then evaluate the potential anywhere at the cost of a short sum.
Some literature considers the factor \(\sqrt{\frac{4\pi}{2l+1}}\) as part of the definition of \(Y_{lm}(\theta, \varphi)\) (Racah normalization).
Where the Expansion Converges
The expansion is based on
where \(r_<\) and \(r_>\) are the smaller and larger of \(r\) and \(r'\), and \(\gamma\) is the angle between
\(\mathbf{r}\) and \(\mathbf{r}'\). The exterior expansion uses \(r_> = r\) for all charges. It therefore converges
only outside the smallest sphere about the expansion center that contains all charge, with radius
\(R = \max r'\). Likewise, the interior expansion converges only inside the largest sphere that contains no
charge, with radius \(R = \min r'\). multipoles stores \(R\) as validity_radius and warns when you evaluate the
expansion on the wrong side of this sphere.
Accuracy
Since \(|P_l| \le 1\), the error of the exterior expansion truncated after \(l_{max}\) is bounded by
where \(Q = \int d^3r' |\rho(\mathbf{r}')|\) is the total absolute charge. For the interior expansion, swap \(r\) and \(R\). So the error decreases geometrically with \(l_{max}\), and the faster, the farther away from the sphere of radius \(R\) you are. Each additional order gains a factor \(R/r\) in accuracy.
For continuous charge distributions on a grid, two more sources of error come in:
- Resolution: the moments are integrated numerically with Simpson's rule, so the grid must resolve the charge density well.
- Box size: charge outside of the grid is missing, so the density must have decayed at the boundary of the grid. Also, the expansion is only valid outside of the charge distribution, so if you need the potential on the boundary of the grid, the grid must be large enough for the charge to be well inside.
The accuracy notebook shows how the error behaves for some gaussian charge distributions.
The Field
The field \(\mathbf{E} = -\nabla\Phi\) is computed analytically, using the ladder relations of the solid harmonics. With the Racah-normalized spherical harmonics \(C_{lm} = \sqrt{\frac{4\pi}{2l+1}} Y_{lm}\), the irregular solid harmonics \(I_{lm} = C_{lm} / r^{l+1}\) of the exterior expansion obey
and the regular solid harmonics \(R_{lm} = r^l C_{lm}\) of the interior expansion obey
Unlike derivatives in spherical coordinates, these relations have no singularities on the z-axis.
Cartesian Moments
The total charge \(q\), the dipole moment \(\mathbf{p}\) and the traceless quadrupole moment \(Q_{ij}\),
give the first terms of the exterior expansion in cartesian form,
They are related to the spherical moments by
All moments are taken about the expansion center. Except for the lowest non-vanishing one, they change when you choose a different center.
Numerical Methods
- Spherical harmonics are computed with the standard three-term recurrence of the associated Legendre functions, which is stable to high orders and much faster than evaluating each \(Y_{lm}\) separately.
- Grid integrals are weighted sums with the weights of the composite Simpson rule along each axis. Grid points without charge are skipped.
- Only the harmonics with \(m \ge 0\) are computed; those with negative \(m\) follow from \(C_{l,-m} = (-1)^m C_{lm}^*\).