This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

PDE solvers · Diffusion (parabolic)

The heat equation in two dimensions

Method
alternating-direction implicit (Peaceman-Rachford): one tridiagonal solve per grid line, per half-step
Checked
two decaying modes against their exact rates, the measured order, and a time step 128 times the explicit limit

In two dimensions an implicit step couples every point to its four neighbors, and the matrix is no longer tridiagonal. Peaceman and Rachford's way around it: split the step in half, and be implicit in one direction at a time.

First half: implicit in , explicit in . Second half: the other way around. Each half is a set of independent one-dimensional problems — one tridiagonal solve per grid line — so a step costs for unknowns, the same as an explicit step. It is still second order in time and stable for any time step, which the explicit scheme is not: that one needs .

The second half-step is the first with the axes exchanged, so it is written as transpose, the same sweep, transpose back.

The test field is two modes with different wavenumbers in and , , decaying at and . A field symmetric in and would not notice the two sweeps being mixed up; this one does.

What the run shows

Two modes after t = 0.02: the faster one has nearly gone
y
x
41 × 41 grid
  • both modes decay at their exact rates: the worst error is under a thousandth of the field
  • halving h and the time step together cuts the error by four: second order in both
  • a time step 128 times the explicit limit is still stable, and still accurate to a percent of the field
e1
0.00393109
e2
0.000977936
e3
0.000244455
order
2.00712
amplitude
0.422562
big_r
32
big_step_error
0.00236365

Computed when this page was built, by the same interpreter that runs it in your browser: 3 of 3 tests, 0.18 s. The numbers above are the program's show lines.

The program

101 lines, 4 functions. It is editable: change it and press Run (or ⌘/Ctrl + Enter). The tests at the bottom run too, so if your change breaks the solver, a test fails.

exact_field
The exact solution at time t on the padded (n+2) x (n+2) grid, boundary ring included.
half_step
Half a step: explicit along axis 0, implicit along axis 1. `r` is dt / h^2. Every interior row is its own tridiagonal system; the boundary rows of each are the identity.
adi_step
One Peaceman-Rachford step.
adi_error
March to t_end and return the largest error against the exact field.
Viv [1]

Open in the workbench The atlas Download heat_2d_adi.viv