pdemagic at a glance

pdemagic solves partial differential equations that you state as you would write them down. It chooses the method, the grid and every setting, solves the problem, and tells you what it did. This page shows the whole idea with one example.

A problem is equations on a domain

A problem is a set of sympy equations on a domain. The unknown is an undefined function of the coordinates. Here a source in the plane sends out waves of one frequency, which is the Helmholtz equation \(\nabla^2 w + k^2 w = -f\) with \(k = 6\):

import sympy as sp
import pdemagic as pm

x, y = sp.symbols("x y", real=True)
w = sp.Function("w")(x, y)

source = sp.exp(-8 * (x**2 + y**2))
problem = pm.Problem(
    sp.Eq(pm.laplacian(w) + 36 * w, -source),
    domain=pm.FreeSpace(x, y),
)

FreeSpace is the whole plane. Nothing in the problem says how to solve it: there is no grid, no box, and no artificial boundary that would have to absorb the waves.

The planner chooses method and grid

pm.solve hands the problem to the planner, which recognises the equation and decides how to solve it, and then solves it. The solution holds numpy arrays, with the grid they live on.

solution = pm.solve(problem)
solution.plot(part="real")
The figure that the example above draws. The figure that the example above draws.

The waves leave the source and do not come back, as they do in free space.

The plan is data

What the planner decided is a plan. You can print it before anything is solved:

plan = pm.plan(problem)
print(plan)
method:
  kind: fourier
grid:
  lower: [-3.952980960979054, -3.952980960979054]
  upper: [3.9529809609790543, 3.9529809609790543]
  shape: [128, 128]
  periodic: [False, False]
notes:
  - The equation is a Helmholtz equation with the wavelength 1.0472, in 2-dimensional free space.
  - Box: chosen so that the source falls to 1e-12 of the maximum at the faces.
  - The box is 2 wavelengths wider than the source on every free side, and the grid has at least 6 points per wavelength, so that the waves around the source can be seen. The solution is the outgoing one, and is complex.
  - 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. Every entry can be changed, and the plan is then executed exactly as given:

plan.grid.shape = (192, 192)
finer = pm.solve(problem, plan)
assert finer[w].shape == (192, 192)

Accurate by default

Most methods are spectral, and linear equations in time are propagated exactly. Ten digits on a modest grid are common. Every solution carries diagnostics that say how far it can be trusted:

assert solution.diagnostics["spectral_tail"] < 1e-10

The numerics are written in Rust. Python is the first frontend.

What it solves

pdemagic is young, and it does not solve everything. In short:

A problem lives on one of these domains:

Not every equation is solved on every domain. What is solved where has the table, and Domains helps you choose.

Where to go from here

Install pdemagic, and then take the tutorial, which starts with a first problem.