A first problem: charges in free space¶
This first part of the tutorial takes one problem through the whole workflow: state it, solve it, and look at the result. The problem is the electric potential of two charges with nothing around them. You need pdemagic with matplotlib, as the installation describes.
Step 1: coordinates and the unknown¶
pdemagic takes a problem as sympy writes it. The coordinates are sympy
symbols, and the unknown is an undefined function of them. Here it is the
potential phi in the plane:
import numpy as np
import sympy as sp
import pdemagic as pm
x, y = sp.symbols("x y", real=True)
phi = sp.Function("phi")(x, y)
Step 2: the equation¶
The two charges are small clouds of opposite sign, one at \(x = 1\) and one at \(x = -1\). The potential of a charge density \(\rho\) obeys Poisson’s equation, \(\nabla^2 \varphi = -\rho\):
rho = sp.exp(-((x - 1) ** 2 + y**2)) - sp.exp(-((x + 1) ** 2 + y**2))
equation = sp.Eq(pm.laplacian(phi), -rho)
pm.laplacian(phi) is the sum of the second derivatives of phi along
all coordinates. The equation can be written in any equivalent form:
sp.Eq(-phi.diff(x, 2) - phi.diff(y, 2), rho) states the same problem.
Step 3: the domain¶
A problem is an equation on a domain. The charges have the whole plane
around them, which is FreeSpace:
problem = pm.Problem(equation, domain=pm.FreeSpace(x, y))
Free space means that nothing but the charges is there: the potential is the one that the charges produce, and it fades far away from them. You do not choose a box, and you do not invent a boundary condition on it.
Step 4: solve it¶
solution = pm.solve(problem)
print(solution)
Solution(unknowns=['phi'], grid=64x54)
That is all. pdemagic recognised the equation, found where the charges are, and chose a method and a grid of 64 by 54 points. Part 4 shows how to see each of these choices, and how to change them.
Step 5: the numbers¶
Index the solution with the unknown for its values. They are a numpy
array with one axis for each coordinate, and solution.axes holds the
grid points along each axis:
potential = solution[phi]
xs, ys = solution.axes
print(potential.shape, xs.shape, ys.shape)
(64, 54) (64,) (54,)
potential[i, j] is the potential at the point (xs[i], ys[j]). With
that you can ask anything of the result. The largest value, for example,
and whether the potential changes its sign when the two charges are
swapped, as it must:
print(round(float(potential.max()), 4))
print(np.allclose(potential, -potential[::-1]))
0.5419
True
solution.mesh() returns the coordinates of every grid point, as arrays
of the shape of potential. Step 7 uses it.
Step 6: look at it¶
solution.plot()
plot draws what the problem calls for: in two dimensions the field in
colour. A field with both signs, like this one, is drawn in two colours
around zero. plot returns the matplotlib axes, so everything about the
figure can still be changed:
ax = solution.plot()
ax.set_title("Two charges of opposite sign", loc="left")
Step 7: three dimensions¶
The same code works in any number of dimensions. Only the coordinates change. This is one cloud with the total charge one in space, in units in which the equation reads \(\nabla^2 V = -4\pi\rho\):
z = sp.symbols("z", real=True)
V = sp.Function("V")(x, y, z)
cloud = sp.exp(-(x**2 + y**2 + z**2) / 2) / (2 * sp.pi) ** sp.Rational(3, 2)
in_space = pm.solve(
pm.Problem(
sp.Eq(pm.laplacian(V), -4 * sp.pi * cloud),
domain=pm.FreeSpace(x, y, z),
)
)
print(in_space)
Solution(unknowns=['V'], grid=64x64x64)
The exact potential of this cloud is known: it is \(\operatorname{erf}(r/\sqrt{2})/r\) at the distance \(r\) from the centre. So the result can be checked at every point of the grid:
from math import erf, sqrt
X, Y, Z = in_space.mesh()
r = np.sqrt(X**2 + Y**2 + Z**2)
exact = np.vectorize(erf)(r / sqrt(2)) / r
print(abs(in_space[V] - exact).max() < 1e-10)
True
The potential is right to ten digits and more, although nobody chose a grid. That is the rule with pdemagic and not the exception: most of its methods are spectral, and the planner picks the grid for this accuracy.
In three dimensions plot draws a section through the field, by default
at the middle of the box along the last coordinate, here the plane
\(z = 0\). cut chooses another section:
in_space.plot(cut={x: 1.0})
What you have learned¶
A problem is a sympy equation for an undefined function, on a domain:
pm.Problem(equation, domain=...).pm.FreeSpaceis all of space, with no box and no boundary condition to choose.pm.solvereturns a solution.solution[phi]is a numpy array, andsolution.axesandsolution.mesh()give the grid it lives on.solution.plot()draws the result, and returns the matplotlib axes.
The gallery has these two charges once more. Next: a heated plate, where the domain has walls.