Model evaluation · examples/poisson_contours.lms

Poisson equation contours

Numerical PDE: Poisson equation ∇²φ = -f solved by SOR on a 41×41 grid; the exact cubic-per-variable solution is reproduced to 1e-9, and eight contour lines of φ are drawn over the heat map

Loading the WebAssembly host…

What runs

The page fetches examples/poisson_contours.bin, 131,427 bytes of lmcode bytecode (sha256 a9aecdd23c4e…), and hands it to the same lmplot.wasm every example uses. The bytecode calls the host's Plot functions (plot.cmd, plot.data, plot.num, plot.png …); gnuplot draws into an in-memory file system and the page shows the files the program wrote.

In SVG mode plot.png("x.png", w, h) writes x.svg with gnuplot's svg terminal; PNG mode uses lmplot's own rasterizer and fonts, the same bytes as the native binary.

The program was compiled ahead of time from the source on the right with lmscript; the lmscript lines around the _lmcode_ block (data generation, ${...} values, run_lmcode) ran at compile time.

# Poisson equation on the unit square, Laplace(phi) = -f with phi = 0 on the boundary,
# solved by successive over-relaxation on a 41 x 41 grid. The exact solution is
#   phi(x, y) = x(1-x) y(1-y) (1 + 2x + 3y),
# a cubic in each variable. The five-point Laplacian is exact for such polynomials, so the
# discrete solution must equal phi at every node; the program checks that to 1e-9.
# The left panel shows the source f, the right panel phi as a heat map with eight contour
# lines computed by gnuplot from the same grid.
# Writes images/poisson_contours.png (960x500).
mkdir("images");
mkdir("build");

let n = 41;
let h = 1.0 / 40.0;
let omega = 1.8;

# Exact field on all nodes, and the source f = -Laplace(phi) from the same stencil.
let exact = [];
let f = [];
for (j in range(0, n)) {
  for (i in range(0, n)) {
    let x = num(i) * h;
    let y = num(j) * h;
    exact = push(exact, x * (1.0 - x) * y * (1.0 - y) * (1.0 + 2.0 * x + 3.0 * y));
    f = push(f, 0.0);
  }
}
let ex = 0.0;
for (j in range(1, n - 1)) {
  for (i in range(1, n - 1)) {
    let k = j * n + i;
    let lap = exact[k + 1] + exact[k - 1] + exact[k + n] + exact[k - n] - 4.0 * exact[k];
    f[k] = -lap / (h * h);
  }
}

# SOR sweeps until the largest update is below 1e-13 (cap: 20000 sweeps).
let u = [];
for (k in range(0, n * n)) { u = push(u, 0.0); }
let sweeps = 0;
let change = 1.0;
while (change > 0.0000000000001 && sweeps < 20000) {
  change = 0.0;
  for (j in range(1, n - 1)) {
    for (i in range(1, n - 1)) {
      let k = j * n + i;
      let gs = 0.25 * (u[k + 1] + u[k - 1] + u[k + n] + u[k - n] + h * h * f[k]);
      let d = omega * (gs - u[k]);
      u[k] = u[k] + d;
      if (abs(d) > change) { change = abs(d); }
    }
  }
  sweeps = sweeps + 1;
}
let err = 0.0;
let phi_max = 0.0;
for (k in range(0, n * n)) {
  if (abs(u[k] - exact[k]) > err) { err = abs(u[k] - exact[k]); }
  if (exact[k] > phi_max) { phi_max = exact[k]; }
}
assert(err < 0.000000001);

# Gnuplot grid data: x fastest, blank line between rows of constant y.
let phi_rows = "";
let f_rows = "";
let f_max = 0.0;
for (k in range(0, n * n)) { if (abs(f[k]) > f_max) { f_max = abs(f[k]); } }
for (j in range(0, n)) {
  for (i in range(0, n)) {
    let k = j * n + i;
    phi_rows = phi_rows + str(num(i) * h) + " " + str(num(j) * h) + " " + str(u[k]) + "\n";
    f_rows = f_rows + str(num(i) * h) + " " + str(num(j) * h) + " " + str(f[k]) + "\n";
  }
  phi_rows = phi_rows + "\n";
  f_rows = f_rows + "\n";
}
let levels = "";
for (m in range(1, 9)) {
  levels = levels + (m > 1 ? "," : "") + str(phi_max * num(m) / 9.0);
}

p = _lmcode_ cli="lmplot" install="github:lmctl-ai/lmplot@v0.3.1";
plot.data("PHI", ${phi_rows});
plot.data("SRC", ${f_rows});
plot.echo(${"SOR sweeps = " + str(sweeps) + ", max |phi - exact| = " + str(err) + ", max phi = " + str(phi_max)});

# Contour lines of phi, saved to a datablock on the same grid (no surface is drawn).
plot.cmd("set contour base");
plot.cmd("set cntrparam levels discrete " + ${levels});
plot.cmd("unset surface");
plot.cmd("set view map");
plot.cmd("set table $CONT");
plot.cmd("splot $PHI using 1:2:3 with lines");
plot.cmd("unset table");
plot.cmd("unset contour");

plot.png("images/poisson_contours.png", 960, 500);
plot.cmd("set multiplot layout 1,2 title 'Poisson equation: source and potential' font ',12'");
plot.cmd("set palette defined (0 '#f8fafc', 0.5 '#fb923c', 1 '#7c2d12')");
plot.cmd("set size ratio -1");
plot.cmd("set xrange [0:1]");
plot.cmd("set yrange [0:1]");
plot.cmd("set cbrange [0:" + ${str(f_max)} + "]");
plot.cmd("set title 'source f(x, y) = -Laplace phi' font ',10'");
plot.cmd("set xlabel 'x'");
plot.cmd("set ylabel 'y'");
plot.cmd("set cblabel 'f'");
plot.cmd("plot $SRC using 1:2:3 with image notitle");
plot.cmd("set palette defined (0 '#0f172a', 0.4 '#1d4ed8', 0.7 '#0ea5e9', 1 '#fde68a')");
plot.cmd("set cbrange [0:" + ${str(phi_max)} + "]");
plot.cmd("set title 'potential phi(x, y) with 8 contour lines' font ',10'");
plot.cmd("set cblabel 'phi'");
plot.cmd("plot $PHI using 1:2:3 with image notitle, $CONT using 1:2 with lines lw 1.4 lc rgb '#f8fafc' notitle");
plot.cmd("unset multiplot");
plot.close();
end _lmcode_
r = run_lmcode(p);
assert(r.ok, json_stringify(r));
println("wrote images/poisson_contours.png");