/ proof-of-concept / steelsim31
Sign in
proof-of-concept / steelsim31
steelsim31 / tests / gpu.test.ts
123 lines · 6.1 KBCodeBlameHistory
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;
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;
a2a1b68Drumstick by default, hit sequences, 170 GPa steelJeremy Magland 38 hits: { done: number; total: number } | null;
41const ARGS = ["--enable-unsafe-webgpu", "--enable-features=Vulkan", "--use-angle=vulkan", "--ignore-gpu-blocklist", "--no-sandbox"];
43/** Serves the app and opens `query` in a headless Chromium; `body` gets the page and the list of page errors. */
44async function withPage(query: string, body: (page: Page, errors: string[]) => Promise<void>): Promise<void> {
45 const server = await createServer({ root, logLevel: "silent", server: { port: 0, strictPort: false } });
46 await server.listen();
47 const url = server.resolvedUrls?.local[0];
48 assert.ok(url, "dev server URL");
49 const browser = await chromium.launch({ headless: true, args: ARGS });
50 try {
51 const page = await browser.newPage();
52 const errors: string[] = [];
53 page.on("pageerror", (e) => errors.push(e.message));
54 page.on("console", (msg) => { if (msg.type() === "error" || msg.type() === "warning") errors.push(`console.${msg.type()}: ${msg.text()}`); });
55 await page.goto(`${url}?${query}`);
56 await body(page, errors);
57 } finally {
58 await browser.close();
59 await server.close();
60 }
63test("one GPU step matches the float64 reference on flat, bowed, and elliptical sheets", { timeout: 120_000 }, async () => {
64 await withPage("selftest=1", async (page, errors) => {
65 let result: SelfTestResult = { done: false };
66 for (let i = 0; i < 120 && !result.done; i++) {
67 await page.waitForTimeout(500);
68 result = await page.evaluate<SelfTestResult>("window.steelsim ? window.steelsim.selftest() : { done: false }");
69 }
70 assert.deepEqual(errors, [], "page errors");
71 assert.ok(result.done, "self-test did not finish");
72 assert.equal(result.error, undefined, `self-test error: ${result.error}`);
73 for (const c of result.cases ?? []) {
74 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)}`);
75 }
76 assert.ok(result.passed, `self-test failed: ${JSON.stringify(result.cases)}`);
77 });
78});
80/** Runs a short simulation to completion and returns its final status. */
81async function runToCompletion(query: string): Promise<Status> {
82 let st: Status | null = null;
83 await withPage(`autostart=1&${query}`, async (page, errors) => {
84 for (let i = 0; i < 600; i++) {
85 await page.waitForTimeout(250);
8cb683aInitial shape as a list of features, with random configurationsJeremy Magland 86 st = await page.evaluate<Status | null>("window.steelsim ? { ...window.steelsim.status(), features: window.steelsim.features() } : null");
d8f9e54Clean rebuild of the steel sheet simulatorJeremy Magland 87 const noGpu = await page.evaluate<string | null>("window.steelsim.noGpu()");
88 assert.equal(noGpu, null, `no WebGPU: ${noGpu}`);
89 if (st && st.phase === "done") break;
90 }
91 assert.deepEqual(errors, [], "page errors");
92 });
93 assert.ok(st, "no status");
94 return st!;
97test("a short push-and-release run completes with finite output", { timeout: 180_000 }, async () => {
a2a1b68Drumstick by default, hit sequences, 170 GPa steelJeremy Magland 98 const st = await runToCompletion("spacing=0.012&duration=0.2&excitation=push&rampTime=0.05&holdMax=0.1&rigidProjection=1");
d8f9e54Clean rebuild of the steel sheet simulatorJeremy Magland 99 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`);
100 assert.equal(st.phase, "done");
101 assert.ok(st.samples >= 0.2 * 48000 - 1, "recorded the requested duration");
102 assert.ok(isFinite(st.wmax) && st.wmax < 0.05, "displacement stays bounded");
103 assert.ok(st.peakPressure > 1e-3 && isFinite(st.peakPressure), "the sheet radiates");
104});
8cb683aInitial shape as a list of features, with random configurationsJeremy Magland 106test("a short drumstick strike on an elliptical sheet with dents and a random field completes with finite output", { timeout: 180_000 }, async () => {
107 const st = await runToCompletion("spacing=0.012&duration=0.2&excitation=strike&shape=ellipse&features=bow:x,0.003,0.3;dent:0.5,0.5,0.03,0.002;noise:7,0.0005,0.05");
108 assert.equal(st.features, "bow:x,0.003,0.3;dent:0.5,0.5,0.03,0.002;noise:7,0.0005,0.05", "the features were read from the URL");
d8f9e54Clean rebuild of the steel sheet simulatorJeremy Magland 109 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`);
110 assert.equal(st.phase, "done");
111 assert.ok(isFinite(st.wmax) && st.wmax < 0.05, "displacement stays bounded");
112 assert.ok(st.peakPressure > 1e-3, "the sheet radiates");
113 assert.ok(st.stick && st.stick.maxForce > 1, "the stick hit the sheet");
114});
116test("a short series of two strikes completes with finite output", { timeout: 180_000 }, async () => {
117 const st = await runToCompletion("spacing=0.012&duration=0.2&hitSequence=crescendo&hitCount=2&hitInterval=0.1&strikeVelocityMin=1");
118 console.log(` series: hits ${st.hits?.done} of ${st.hits?.total}, peak ${st.peakPressure.toFixed(3)} Pa, max contact force ${st.stick?.maxForce.toFixed(1)} N`);
119 assert.equal(st.phase, "done");
120 assert.deepEqual(st.hits, { done: 2, total: 2 });
121 assert.ok(isFinite(st.wmax) && st.wmax < 0.05, "displacement stays bounded");
122 assert.ok(st.stick && st.stick.maxForce > 1, "the stick hit the sheet");
123});
↑↓ move↵openescclose