programdemos/wave-tank/wave-tank.xtl

A wave tank: the wave equation on a grid. Each step, every cell moves towards the average of its neighbors, carrying momentum from the step before: next = 2 u - previous + c^2 * (neighbors - 4 u). Walls are a mask, a source shakes up and down, and waves spread, bend through slits and interfere, all as array expressions over the whole grid. Run it with "xetal run demos/wave-tank/wave-tank.xtl"; it prints the state's shape and the surface drawn with characters (crests, troughs, the wall). The live page picks the scene (a double slit, a lens, two sources, an open tank) and lets you click for ripples: https://softwarewrighter.github.io/X_eTaL-demos/wave-tank/

source

The tank

rows : Int

value · line 19

rows, cols: the grid's size. c2: the wave speed squared (at most 0.5 for a stable step). amp, omega: the source's strength and frequency. steps: how many to run.

rows ← 36
Used in: col, row, edge, u0

cols : Int

value · line 20
cols ← 72
Used in: col, row, edge, u0

c2 : Float

value · line 21
c2 ← 0.3
Used in: ᵘs̲tep

amp : Float

value · line 22
amp ← 0.5
Used in: ᵘs̲tep

omega : Float

value · line 23
omega ← 0.5
Used in: ᵘs̲tep

steps : Int

value · line 24
steps ← 150
Used in: %2

Coordinates and the sponge

col : Int

value · line 32

col, row: each cell's position, by broadcasting. edge, damp: a cell's distance to the border; near the edges the surface is damped (a sponge), so waves leave instead of wrapping around.

col ← (o̲ffsets rows) 'r̲ight t̲able o̲ffsets cols
Used in: edge, wall, src

row : Int

value · line 33
row ← (o̲ffsets rows) 'l̲eft t̲able o̲ffsets cols
Used in: edge, slits

edge : Int

value · line 34
edge ← (row m̲in (rows − 1) − row) m̲in col m̲in (cols − 1) − col
Used in: damp

damp : Float

value · line 35
damp ← 1.0 − 0.03 × f̲loat 0 m̲ax 8 − edge
Used in: ᵘs̲tep

slits : Bool

value · line 45
slits ← ((row ≥ 13) ∧ row < 16) ∨ (row ≥ 20) ∧ row < 23
Used in: wall

wall : Float

value · line 46
wall ← f̲loat n̲ot (col = 30) ∧ n̲ot slits

src : Float

value · line 47
src ← f̲loat col = 10
Used in: ᵘs̲tep

ᵘl̲ap : Float -> Float

function · line 53

-- the Laplacian: four shifted copies minus four times the center. u:l_ap x: how much a cell differs from its four neighbors, up down left right (o_-_1, o_-_2), added and the center subtracted four times over (the edges wrap).

ᵘl̲ap ← { x → ((1 o̲-₁ x) + (-1 o̲-₁ x) + (1 o̲-₂ x) + -1 o̲-₂ x) − 4.0 × x }
Used in: ᵘs̲tep

One step for every cell at once

ᵘs̲tep : (Float, Float, Float) -> (Float, Float, Float)

function · line 63

u:s_tep (u, p, t): the state is a tuple, the surface now, the surface a step ago, and the time, taken apart by the pattern and given back as a new tuple (D7, landed); t is a plain scalar now, not a plane carrying the same number everywhere, so the source's drive reads it directly. Walls hold the surface at 0 (damp * wall both zero there).

ᵘs̲tep ← { (u, p, t) →
  drive ← amp × src × s̲in omega × t
  nxt ← damp × wall × ((2.0 × u) − p) + (c2 × ᵘl̲ap u) + drive
  (nxt, u, t + 1.0)
}
Used in: %2

u0 : Float

value · line 73

u0: the still surface. u, p, t: the state after all the steps (function power), taken apart by the same pattern the binding reads it into.

u0 ← (rows c̲at cols) r̲eshape 0.0
Used in: %2

%2 : Float

value · line 74
(u, p, t) ← steps 'ᵘs̲tep p̲ower (u0, u0, 0.0)

u : Float

value · line 74
(u, p, t) ← steps 'ᵘs̲tep p̲ower (u0, u0, 0.0)

p : Float

value · line 74
(u, p, t) ← steps 'ᵘs̲tep p̲ower (u0, u0, 0.0)
Used in: ᵘs̲tep, %2

t

value · line 74
(u, p, t) ← steps 'ᵘs̲tep p̲ower (u0, u0, 0.0)
Used in: ᵘs̲tep, %2

h

value · line 78

The surface: = and - troughs, + and * crests, # the wall.

h ← f̲loat 1 + f̲loor 2.5 + 2.0 × (u m̲ax -1.2) m̲in 1.2