d8f9e54Clean rebuild of the steel sheet simulatorJeremy Magland 1// GPU-versus-CPU test: serves the app with Vite's dev server, opens it in a
2// headless Chromium with WebGPU through Playwright, and reads the result of the
3// in-page self-test (src/selftest.ts), which compares one step of the WGSL
4// kernel with the float64 reference.
6import { test } from "node:test";
7import assert from "node:assert/strict";
8import { fileURLToPath } from "node:url";
9import { dirname, join } from "node:path";
10import { createServer } from "vite";
11import { chromium, type Page } from "playwright";
13const root = join(dirname(fileURLToPath(import.meta.url)), "..");
15interface SelfTestCase {
16 name: string;
17 accError: { u: number; v: number; w: number };
18 positionError: number;
19 nodes: number;
20 dt: number;
21}
22interface SelfTestResult { done: boolean; passed?: boolean; cases?: SelfTestCase[]; error?: string }
24interface Status {
25 phase: string;
26 t: number;
27 totalSteps: number;
28 wmax: number;
29 peakPressure: number;
30 samples: number;
31 frames: number;
32 stepsPerSec: number;
33 dt: number;
34 nx: number;
35 ny: number;
36 stick: { maxForce: number } | null;
37}
39const ARGS = ["--enable-unsafe-webgpu", "--enable-features=Vulkan", "--use-angle=vulkan", "--ignore-gpu-blocklist", "--no-sandbox"];
41/** Serves the app and opens `query` in a headless Chromium; `body` gets the page and the list of page errors. */
42async function withPage(query: string, body: (page: Page, errors: string[]) => Promise<void>): Promise<void> {
43 const server = await createServer({ root, logLevel: "silent", server: { port: 0, strictPort: false } });
44 await server.listen();
45 const url = server.resolvedUrls?.local[0];
46 assert.ok(url, "dev server URL");
47 const browser = await chromium.launch({ headless: true, args: ARGS });
48 try {
49 const page = await browser.newPage();
50 const errors: string[] = [];
51 page.on("pageerror", (e) => errors.push(e.message));
52 page.on("console", (msg) => { if (msg.type() === "error" || msg.type() === "warning") errors.push(`console.${msg.type()}: ${msg.text()}`); });
53 await page.goto(`${url}?${query}`);
54 await body(page, errors);
55 } finally {
56 await browser.close();
57 await server.close();
58 }
59}
61test("one GPU step matches the float64 reference on flat, bowed, and elliptical sheets", { timeout: 120_000 }, async () => {
62 await withPage("selftest=1", async (page, errors) => {
63 let result: SelfTestResult = { done: false };
64 for (let i = 0; i < 120 && !result.done; i++) {
65 await page.waitForTimeout(500);
66 result = await page.evaluate<SelfTestResult>("window.steelsim ? window.steelsim.selftest() : { done: false }");
67 }
68 assert.deepEqual(errors, [], "page errors");
69 assert.ok(result.done, "self-test did not finish");
70 assert.equal(result.error, undefined, `self-test error: ${result.error}`);
71 for (const c of result.cases ?? []) {
72 console.log(` ${c.name}: ${c.nodes} nodes, dt ${(c.dt * 1e6).toFixed(2)} us, acc errors ${c.accError.u.toExponential(1)} / ${c.accError.v.toExponential(1)} / ${c.accError.w.toExponential(1)}, position ${c.positionError.toExponential(1)}`);
73 }
74 assert.ok(result.passed, `self-test failed: ${JSON.stringify(result.cases)}`);
75 });
76});
78/** Runs a short simulation to completion and returns its final status. */
79async function runToCompletion(query: string): Promise<Status> {
80 let st: Status | null = null;
81 await withPage(`autostart=1&${query}`, async (page, errors) => {
82 for (let i = 0; i < 600; i++) {
83 await page.waitForTimeout(250);
84 st = await page.evaluate<Status | null>("window.steelsim ? window.steelsim.status() : null");
85 const noGpu = await page.evaluate<string | null>("window.steelsim.noGpu()");
86 assert.equal(noGpu, null, `no WebGPU: ${noGpu}`);
87 if (st && st.phase === "done") break;
88 }
89 assert.deepEqual(errors, [], "page errors");
90 });
91 assert.ok(st, "no status");
92 return st!;
93}
95test("a short push-and-release run completes with finite output", { timeout: 180_000 }, async () => {
96 const st = await runToCompletion("spacing=0.012&duration=0.2&rampTime=0.05&holdMax=0.1");
97 console.log(` push: ${st.nx}x${st.ny}, dt ${(st.dt * 1e6).toFixed(2)} us, ${(st.stepsPerSec / 1e3).toFixed(0)} k steps/s, peak ${st.peakPressure.toFixed(3)} Pa, max |w| ${(st.wmax * 1e3).toFixed(2)} mm`);
98 assert.equal(st.phase, "done");
99 assert.ok(st.samples >= 0.2 * 48000 - 1, "recorded the requested duration");
100 assert.ok(isFinite(st.wmax) && st.wmax < 0.05, "displacement stays bounded");
101 assert.ok(st.peakPressure > 1e-3 && isFinite(st.peakPressure), "the sheet radiates");
102});
104test("a short drumstick strike on an elliptical sheet completes with finite output", { timeout: 180_000 }, async () => {
105 const st = await runToCompletion("spacing=0.012&duration=0.2&excitation=strike&shape=ellipse");
106 console.log(` strike: ${st.nx}x${st.ny}, ${(st.stepsPerSec / 1e3).toFixed(0)} k steps/s, peak ${st.peakPressure.toFixed(3)} Pa, max contact force ${st.stick?.maxForce.toFixed(1)} N`);
107 assert.equal(st.phase, "done");
108 assert.ok(isFinite(st.wmax) && st.wmax < 0.05, "displacement stays bounded");
109 assert.ok(st.peakPressure > 1e-3, "the sheet radiates");
110 assert.ok(st.stick && st.stick.maxForce > 1, "the stick hit the sheet");
111});