What pdemagic decided: plans and accuracy¶
In the first three parts pdemagic made every choice for you: the method, the box, the grid, the times. This part shows where these choices are written down, how to change them, and how to tell whether a result can be trusted.
Step 1: a problem with a known answer¶
To judge a result you need something to hold it against. Here the problem is made from its answer: the source is chosen so that the solution in free space is the bell \(e^{-(x^2+y^2)/2}\).
import numpy as np
import sympy as sp
import pdemagic as pm
x, y, t = sp.symbols("x y t", real=True)
u = sp.Function("u")(x, y)
bell = sp.exp(-(x**2 + y**2) / 2)
problem = pm.Problem(
sp.Eq(pm.laplacian(u), pm.laplacian(bell)),
domain=pm.FreeSpace(x, y),
)
def error(solution):
X, Y = solution.mesh()
return abs(solution[u] - np.exp(-(X**2 + Y**2) / 2)).max()
error is the largest distance between a solution and the bell, over
all points of the grid.
Step 2: print the plan¶
pm.plan returns what the planner decided, without solving anything:
plan = pm.plan(problem)
print(plan)
method:
kind: fourier
grid:
lower: [-7.91919191919192, -7.91919191919192]
upper: [7.9191919191919204, 7.9191919191919204]
shape: [64, 64]
periodic: [False, False]
notes:
- The equation is a Poisson equation in 2-dimensional free space.
- Box: chosen so that the source falls to 1e-12 of the maximum at the faces.
- Grid: the coarsest candidate on which the spectrum of the source falls below 1e-10 at high frequencies.
Read it from the top. method is how the problem is solved. grid is
the box that the planner cut out of free space, from lower to upper,
and the number of points along each axis. The notes say in words what
the planner found and why it chose what it chose. They are the first
place to look when a result surprises you.
Step 3: solve, and judge the result¶
pm.solve(problem) plans and solves in one go, as in the parts before.
The solution carries its plan as solution.plan, and it carries
diagnostics:
solution = pm.solve(problem)
print(error(solution) < 1e-10)
for name, value in solution.diagnostics.items():
if value is not None:
print(name)
True
spectral_tail
source_boundary_ratio
padded_shape
removed_source_mean
The diagnostics are a dictionary. Entries that do not apply to the
method are None. Two of them are the ones to watch here:
source_boundary_ratio: the source on the faces of the box, relative to its maximum. The method assumes that the source vanishes outside the box, so this should be tiny.spectral_tail: how far the spectrum of the source has decayed at high frequencies. If it is not tiny, the grid is too coarse.
print(solution.diagnostics["source_boundary_ratio"] < 1e-11)
print(solution.diagnostics["spectral_tail"] < 1e-10)
True
True
Both are tiny, and the error is too. The diagnostics need no known answer: they are there for every problem. This is the bell as it should be:
solution.plot()
Step 4: change the grid¶
Every entry of a plan can be set. The plan is then executed exactly as given, and entries you do not touch keep the planner’s values. Ask for a grid that is too coarse:
plan.grid.shape = (16, 16)
coarse = pm.solve(problem, plan)
tail = coarse.diagnostics["spectral_tail"]
print(f"error {error(coarse):.0e}, spectral_tail {tail:.2f}")
error 9e-04, spectral_tail 0.55
Three digits are left of ten, and the diagnostic says why: at the finest waves that this grid holds, the spectrum of the source has fallen to about half only.
Step 5: change the box¶
Now keep the planner’s grid and cut the box too small, so that the source reaches its faces:
plan = pm.plan(problem)
plan.grid.lower = [-3.0, -3.0]
plan.grid.upper = [3.0, 3.0]
cramped = pm.solve(problem, plan)
ratio = cramped.diagnostics["source_boundary_ratio"]
print(f"error {error(cramped):.2f}, source_boundary_ratio {ratio:.3f}")
error 0.07, source_boundary_ratio 0.039
cramped.plot()
The picture still shows a bell, and it is wrong in the second digit. Towards the faces it dips below zero, which the true solution never does, so the plot has turned to two colours. The diagnostic gives it away without a picture. When a diagnostic is not small, the remedy is in the plan: a wider box for a source that reaches the faces, a finer grid for a spectrum that has not decayed.
Note
The notes of a plan are the planner’s. They are not written anew when you change an entry: a last note names the entries that you set.
Step 6: a plan must fit¶
Not everything can be set to anything. A method that does not suit the problem is refused:
plan = pm.plan(problem)
plan.method.kind = "collocation"
try:
pm.solve(problem, plan)
except pm.InvalidPlanError as refusal:
print(refusal)
the method Collocation does not apply to this domain
Step 7: the times of a problem in time¶
For a problem in time the plan also says when the solution is recorded: at 21 times, unless you say otherwise.
T = sp.Function("T")(x, t)
cooling = pm.Problem(
sp.Eq(T.diff(t), T.diff(x, 2) / 2),
domain=pm.FreeSpace(x),
time=(t, 0, 1),
initial={T: sp.exp(-(x**2))},
)
cooling_plan = pm.plan(cooling)
cooling_plan.times.count = 5
print(pm.solve(cooling, cooling_plan).times)
[0. 0.25 0.5 0.75 1. ]
Step 8: a plan is data¶
A plan holds numbers and words, nothing else. plan.to_json() writes it
out as text, and pm.Plan.from_json(text) reads it back. Keep the text
beside a result, and the solve can be repeated exactly:
text = pm.plan(problem).to_json()
first = pm.solve(problem, pm.Plan.from_json(text))
second = pm.solve(problem, pm.Plan.from_json(text))
print((first[u] == second[u]).all())
True
What you have learned¶
pm.plan(problem)returns the plan, andprintshows it. The notes say why the planner chose what it chose.Change an entry, such as
plan.grid.shape, and pass the plan topm.solve(problem, plan).solution.diagnosticssays how far a result can be trusted, with no known answer needed.A diagnostic that is not small points to its remedy in the plan.
The planner and its plan and Accuracy and diagnostics go deeper, and the reference lists every diagnostic. Next: a shape of your own.