The planner and its plan

pdemagic makes every choice that a user of a solver would otherwise have to make: the method, the box, the grid, the times, the tolerances. The part that chooses is the planner, and a plan records what it chose and why. You can read a plan, change any entry of it, and keep it. This page says how.

import sympy as sp
import pdemagic as pm
from pdemagic import curves

x, y, t = sp.symbols("x y t", real=True)
u = sp.Function("u")(x, y)
problem = pm.Problem(
    sp.Eq(pm.laplacian(u), -sp.exp(-(x**2 + y**2) / 2)),
    domain=pm.FreeSpace(x, y),
)

What is decided

pm.solve(problem) does two things. The planner reads the problem and writes a plan, and the plan is then executed. pm.plan(problem) does the first alone, and pm.solve(problem, plan) the second with a plan that you hand in. A plan holds up to three things:

  • The method. It follows from the equation and the domain, and you seldom choose it. Some methods have settings: a tolerance, a degree.

  • The grid. Where a direction is free the planner first finds a box that holds the source. It then takes the coarsest of its candidate grids that resolves what is given, or, where it solves on one grid after another, the solution itself.

  • The times, for a problem in time: when the solution is recorded.

Read a plan

pm.plan returns the plan without solving. A solution carries the plan that produced it, as solution.plan.

plan = pm.plan(problem)
print(plan)
method:
  kind: fourier
grid:
  lower: [-7.434343434343434, -7.434343434343434]
  upper: [7.434343434343436, 7.434343434343436]
  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.

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. A note also says when the planner could not reach its aim, with the entry to set: a solution that is not resolved, for example.

Every entry is an attribute: plan.method.kind, plan.grid.shape, plan.notes[0].

A method with settings

What a plan holds depends on the problem. On a shape in the plane the method has a degree, a spacing and layers, and the grid is only where the solution is given:

shield = curves.Circle((0, 0), 1, name="shield")
core = curves.Circle((0, 0), 0.5, name="core")
cable = pm.Problem(
    sp.Eq(pm.laplacian(u), 0),
    domain=pm.Region(x, y, shield, core),
    boundary={shield: sp.Eq(u, 0), core: sp.Eq(u, 1)},
)
print(pm.plan(cable))
method:
  kind: spectral_elements
  degree: 12
  spacing: 0.5
  layers: 0
grid:
  lower: [-1.0, -1.0]
  upper: [1.0, 1.0]
  shape: [129, 129]
  periodic: []
notes:
  - The equation is a linear equation of second order with constant coefficients on a shape that 2 closed curves bound, in 2 pieces.
  - Boundary conditions: the value on `shield`, the value on `core`.
  - Mesh: 63 quadrilaterals, three from each triangle of a triangulation with sides of about 0.5000. They hold 9324 points.
  - Degree: the lowest at which the Legendre coefficients of the solution and of the source fall below 1e-10 in every element.
  - Grid: the solution is given at 129 by 129 points, equally spaced over the rectangle that holds the shape. A point outside the shape holds no number. The grid does not affect the accuracy: set grid.shape for more or fewer points.
  - Method: spectral elements. On each quadrilateral the solution is a polynomial of the degree in both directions, and the sparse system for all of them is factorized directly.

The entries that you will meet most:

Entry

Meaning

grid.lower, grid.upper, grid.shape

The box and the number of points along each axis.

method.tolerance

For an iteration and for Newton’s method: the residual at which it stops. For time stepping: the error allowed per step.

method.degree, method.spacing, method.layers

On a shape: the elements, which set the accuracy there.

method.panels

For bodies: the number of triangles on their surfaces.

times.start, times.end, times.count

For a problem in time: when the solution is recorded.

The reference lists every method with its settings.

Change a plan

Every entry can be set. The plan is then executed exactly as given, and entries you do not touch keep the planner’s values.

plan.grid.shape = (96, 96)
plan.grid.lower = [-8.0, -8.0]
plan.grid.upper = [8.0, 8.0]
finer = pm.solve(problem, plan)
assert finer[u].shape == (96, 96)

A problem in time, for example, is recorded at 21 times unless the plan says otherwise:

T = sp.Function("T")(x, t)
cooling = pm.Problem(
    sp.Eq(sp.Derivative(T, t), sp.Derivative(T, x, 2) / 2),
    domain=pm.FreeSpace(x),
    time=(t, 0.0, 1.0),
    initial={T: sp.exp(-(x**2))},
)
cooling_plan = pm.plan(cooling)
cooling_plan.times.count = 5
assert len(pm.solve(cooling, cooling_plan).times) == 5

The notes stay the planner’s: they describe what it chose. One more note, the last, names the entries that you set, as in “Changed after planning: times.count”. The method must suit the problem, and the planner’s choice is the one that does. A plan that does not fit its problem is refused with an InvalidPlanError:

wrong = pm.plan(problem)
wrong.grid.shape = (96,)
try:
    pm.solve(problem, wrong)
except pm.InvalidPlanError as error:
    print(error)
the grid needs one lower bound, upper bound and size per axis, but has 2, 2 and 1

When to change a plan is a matter of the diagnostics: see Accuracy and diagnostics.

Keep a plan

A plan is plain data. plan.to_json() writes it out, and pm.Plan.from_json(text) reads it back, so that a solve can be repeated: two solves from one plan give the same numbers. plan.copy() is a second plan that you can change without touching the first.

text = plan.to_json()
again = pm.Plan.from_json(text)
assert again.to_json() == text
assert (pm.solve(problem, again)[u] == finer[u]).all()

coarse = plan.copy()
coarse.grid.shape = (48, 48)
assert plan.grid.shape == [96, 96]

A solution that is saved to a file keeps its plan with it.

See also