sourcedemos/wave-tank/wave-tank.xtl

1⍝!/usr/bin/env xetal 2⍝# A wave tank: the wave equation on a grid. Each step, every cell 3⍝# moves towards the average of its neighbors, carrying momentum from 4⍝# the step before: next = 2 u - previous + c^2 * (neighbors - 4 u). 5⍝# Walls are a mask, a source shakes up and down, and waves spread, 6⍝# bend through slits and interfere, all as array expressions over 7⍝# the whole grid. Run it with "xetal run 8⍝# demos/wave-tank/wave-tank.xtl"; it prints the state's shape and the 9⍝# surface drawn with characters (crests, troughs, the wall). The live 10⍝# page picks the scene (a double slit, a lens, two sources, an open 11⍝# tank) and lets you click for ripples: 12⍝# https://softwarewrighter.github.io/X_eTaL-demos/wave-tank/ 13 14⍝## The tank 15 16⍝# rows, cols: the grid's size. c2: the wave speed squared (at most 17⍝# 0.5 for a stable step). amp, omega: the source's strength and 18⍝# frequency. steps: how many to run. 19rows ← 36 20cols ← 72 21c2 ← 0.3 22amp ← 0.5 23omega ← 0.5 24steps ← 150 25 26⍝## Coordinates and the sponge 27 28⍝# col, row: each cell's position, by broadcasting. 29⍝# edge, damp: a cell's distance to the border; near the edges the 30⍝# surface is damped (a sponge), so waves leave instead of wrapping 31⍝# around. 32col ← (o̲ffsets rows) 'r̲ight t̲able o̲ffsets cols 33row ← (o̲ffsets rows) 'l̲eft t̲able o̲ffsets cols 34edge ← (row m̲in (rows − 1) − row) m̲in col m̲in (cols − 1) − col 35damp ← 1.0 − 0.03 × f̲loat 0 m̲ax 8 − edge 36 37⍝ -- the scene: a double slit, as masks (plain #: the page's web 38⍝ app finds this exact text to cut the program it runs, right up to 39⍝ it, so a ## here would leave a stray # in what it cuts). slits, 40⍝ wall: a wall down column 30 with two gaps. src: the source, the 41⍝ whole of column 10, a plane wave. The live page replaces this 42⍝ scene with the one you pick (a double slit, a single slit, two 43⍝ sources, a lens, an open tank), each its own wall and src built 44⍝ the same way, from row and col. 45slits ← ((row ≥ 13) ∧ row < 16) ∨ (row ≥ 20) ∧ row < 23 46wall ← f̲loat n̲ot (col = 30) ∧ n̲ot slits 47src ← f̲loat col = 10 48 49⍝# -- the Laplacian: four shifted copies minus four times the center. 50⍝# u:l_ap x: how much a cell differs from its four neighbors, up down 51⍝# left right (o_-_1, o_-_2), added and the center subtracted four 52⍝# times over (the edges wrap). 53ᵘl̲ap ← { x → ((1 o̲-₁ x) + (-1 o̲-₁ x) + (1 o̲-₂ x) + -1 o̲-₂ x) − 4.0 × x } 54 55⍝## One step for every cell at once 56 57⍝# u:s_tep (u, p, t): the state is a tuple, the surface now, the 58⍝# surface a step ago, and the time, taken apart by the pattern and 59⍝# given back as a new tuple (D7, landed); t is a plain scalar now, 60⍝# not a plane carrying the same number everywhere, so the source's 61⍝# drive reads it directly. Walls hold the surface at 0 (damp * wall 62⍝# both zero there). 63ᵘs̲tep ← { (u, p, t) → 64 drive ← amp × src × s̲in omega × t 65 nxt ← damp × wall × ((2.0 × u) − p) + (c2 × ᵘl̲ap u) + drive 66 (nxt, u, t + 1.0) 67} 68⍝ -- end of the core --------------------------------------------------- 69 70⍝# u0: the still surface. u, p, t: the state after all the steps 71⍝# (function power), taken apart by the same pattern the binding 72⍝# reads it into. 73u0 ← (rows c̲at cols) r̲eshape 0.0 74(u, p, t) ← steps 'ᵘs̲tep p̲ower (u0, u0, 0.0) 75s̲hape u 76 77⍝# The surface: = and - troughs, + and * crests, # the wall. 78h ← f̲loat 1 + f̲loor 2.5 + 2.0 × (u m̲ax -1.2) m̲in 1.2 79(f̲loor (wall × h) + (1.0 − wall) × 6.0) s̲elect "=- +*#"