Multipole and Local Expansions¶
Copyright (C) 2020 Andreas Kloeckner
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt
Let's consider a potential. This one could look slightly familiar from a homework assignment.
nsources = 15
nxtgts = 400
nytgts = 400
angles = np.linspace(0, 2*np.pi, nsources, endpoint=False)
r = 1 + 0.3 * np.sin(3*angles)
sources = np.array([
r*np.cos(angles),
r*np.sin(angles),
])
np.random.seed(15)
charges = np.random.randn(nsources)
left, right, bottom, top = extent = (-2, 4, -4, 2)
targets = np.mgrid[left:right:nxtgts*1j, bottom:top:nytgts*1j]
plt.plot(sources[0], sources[1], "x")
dist_vecs = sources.reshape(2, -1, 1, 1) - targets.reshape(2, 1, targets.shape[-1], -1)
dists = np.sqrt(np.sum(dist_vecs**2, axis=0))
potentials = np.sum(charges.reshape(-1, 1, 1) * np.log(dists), axis=0)
plt.imshow(potentials.T[::-1], extent=extent)
<matplotlib.image.AxesImage at 0x7f057116ef10>
Now let's create a stash of derivatives, all about a center of 0, to make things easier:
def f(arg):
return np.log(np.sqrt(np.sum(arg**2, axis=0)))
def fdx(arg):
x, y = arg
r2 = np.sum(arg**2, axis=0)
return x/r2
def fdy(arg):
x, y = arg
r2 = np.sum(arg**2, axis=0)
return y/r2
def fdxx(arg):
x, y = arg
r2 = np.sum(arg**2, axis=0)
return 1/r2 - 2*x**2/r2**2
def fdyy(arg):
x, y = arg
r2 = np.sum(arg**2, axis=0)
return 1/r2 - 2*y**2/r2**2
def fdxy(arg):
x, y = arg
r2 = np.sum(arg**2, axis=0)
return - 2*x*y/r2**2
Local expansions¶
center = np.array([1.5, -1])
#center = np.array([2, -2])
#center = np.array([3, -3])
#center = np.array([0, 0])
Local expansion: $$\psi (\mathbf{x} - \mathbf{y}) \approx \sum _{| p | \leqslant k } \underbrace{\frac{D^p_{\mathbf{z}} \psi (\mathbf{z}) |_{\mathbf{z} = \mathbf{c}-\mathbf{y}} }{p!}}_{\text{depends on src/ctr}} \underbrace{(\mathbf{x} - \mathbf{c})^p}_{\text{dep. on ctr/tgt}} $$
$\mathbf{x}$: targets, $\mathbf{y}$: sources, and $\mathbf{z}$ is the displacement variable.
expn = 0
for isrc in range(nsources):
a = center - sources[:, isrc]
hx, hy = targets - center.reshape(-1, 1, 1)
expn += charges[isrc]*(
f(a)
+ fdx(a)*hx
+ fdy(a)*hy
+ fdxx(a)*hx**2/2
+ fdxy(a)*hx*hy
+ fdyy(a)*hy**2/2
)
err = expn - potentials
plt.plot(center[0], center[1], "o")
plt.plot(sources[0], sources[1], "x")
plt.imshow(np.log10(1e-2 + np.abs(err.T[::-1])), extent=extent)
plt.colorbar()
# Test accuracy at a point
test_y_idx = np.argmin(np.abs(center[1] - targets[1, 0, :]))
test_idx = (7*nxtgts//8, test_y_idx)
plt.plot(targets[0][test_idx], targets[1][test_idx], "ro")
print("Relative error at (red) test point:", abs(err[test_idx])/abs(potentials[test_idx]))
Relative error at (red) test point: 0.12345091017171929
- Move the center around, see how the errors change
- Reduce to linears, see how the errors change
plt.grid()
plt.xlabel("Distance from center")
plt.ylabel("Error")
plt.loglog(targets[0, :, test_y_idx]-center[0], np.abs(err[:, test_y_idx]))
[<matplotlib.lines.Line2D at 0x7f059ac36790>]
What is the slope of the error graph? What should it be?
(Disregard the close-to-center region: Center and Target points are not at exactly the same vertical position.)
Multipole expansions¶
center = np.array([0, 0])
# center = np.array([1, 0])
Now sum a multipole expansion about the center at the targets. The $(-1)^{|p|}$ factor comes from $\mathbf{z} = \mathbf{x}-\mathbf{y}$.
Multipole expansion: $$\psi (\mathbf{x} - \mathbf{y}) \approx \sum _{| p | \leqslant k } \underbrace{\frac{(-1)^{|p|} D^p_{\mathbf{z}} \psi (\mathbf{z}) |_{\mathbf{z} = \mathbf{x}-\mathbf{c}} }{p!}}_{\text{depends on ctr/tgt}} \underbrace{(\mathbf{y} - \mathbf{c})^p}_{\text{dep. on src/ctr}} . $$
$\mathbf{x}$: targets, $\mathbf{y}$: sources, and $\mathbf{z}$ is the displacement variable.
expn = 0
for isrc in range(nsources):
a = targets - center.reshape(-1, 1, 1)
hx, hy = sources[:, isrc] - center
expn += charges[isrc]*(
f(a)
# Negative sign from the chain rule
- fdx(a)*hx
- fdy(a)*hy
+ fdxx(a)*hx**2/2
+ fdxy(a)*hx*hy
+ fdyy(a)*hy**2/2
)
/home/andreas/src/env-3.7/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in log /home/andreas/src/env-3.7/lib/python3.7/site-packages/ipykernel_launcher.py:7: RuntimeWarning: invalid value encountered in true_divide import sys /home/andreas/src/env-3.7/lib/python3.7/site-packages/ipykernel_launcher.py:12: RuntimeWarning: invalid value encountered in true_divide if sys.path[0] == '': /home/andreas/src/env-3.7/lib/python3.7/site-packages/ipykernel_launcher.py:17: RuntimeWarning: divide by zero encountered in true_divide /home/andreas/src/env-3.7/lib/python3.7/site-packages/ipykernel_launcher.py:17: RuntimeWarning: invalid value encountered in true_divide /home/andreas/src/env-3.7/lib/python3.7/site-packages/ipykernel_launcher.py:27: RuntimeWarning: invalid value encountered in true_divide /home/andreas/src/env-3.7/lib/python3.7/site-packages/ipykernel_launcher.py:22: RuntimeWarning: divide by zero encountered in true_divide /home/andreas/src/env-3.7/lib/python3.7/site-packages/ipykernel_launcher.py:22: RuntimeWarning: invalid value encountered in true_divide
err = expn - potentials
imgdata = err
plt.plot(center[0], center[1], "o")
plt.plot(sources[0], sources[1], "x")
plt.imshow(np.log10(1e-2 + np.abs(imgdata.T[::-1])), extent=extent)
plt.colorbar()
# Test accuracy at a point
test_y_idx = 5*nytgts//8
test_idx = (7*nxtgts//8, test_y_idx)
plt.plot(targets[0][test_idx], targets[1][test_idx], "ro")
print("Relative error at (red) test point:", abs(err[test_idx])/abs(potentials[test_idx]))
Relative error at (red) test point: 0.001811311065482721
- Move the center around, observe convergence behavior
- Reduce to linears, observe convergence behavior
plt.grid()
plt.xlabel("Distance from center")
plt.ylabel("Error")
plt.loglog(targets[0, :, test_y_idx]-center[0], np.abs(err[:, test_y_idx]))
[<matplotlib.lines.Line2D at 0x7f057093a710>]
What is the slope in the far region? What should it be?
Look at individual basis functions:
plt.imshow(
fdx(targets).T[::-1],
extent=extent, vmin=-1, vmax=1)
/home/andreas/src/env-3.7/lib/python3.7/site-packages/ipykernel_launcher.py:7: RuntimeWarning: invalid value encountered in true_divide import sys
<matplotlib.image.AxesImage at 0x7f1072241c90>
Why is this thing called a 'multipole' expansion?