fc06d37Bead-and-spring percussion simulator: numbl-scripted setup, Verlet/Yoshida solver with exact damping, linear modes, browser appJeremy Magland 1// Run a numbl script with the bp_* helpers on its path, and turn its `sys`
2// struct into a BeadSystem. Shared by the browser worker and the tests.
4import { executeCode, RTV } from "numbl";
5import { parseSystem } from "./parse";
6import type { SymEig } from "./modes";
7import type { BeadSystem } from "./types";
9export interface HelperFile {
10 name: string;
11 source: string;
12}
14export function runScript(
15 source: string,
16 helpers: HelperFile[],
17 onOutput: (text: string) => void
18): { sys: BeadSystem; warnings: string[] } {
19 const result = executeCode(source, { onOutput, displayResults: true }, helpers, "script.m");
20 return parseSystem(result.variableValues.sys);
21}
23// Symmetric eigendecomposition through numbl's eig.
24export const numblEig: SymEig = (A, n) => {
25 const r = executeCode("[V, D] = eig(A); d = diag(D);", {
26 initialVariableValues: { A: RTV.tensor(A, [n, n]) },
27 });
28 const V = r.variableValues.V as unknown as { data: Float64Array };
29 const d = r.variableValues.d as unknown as { data: Float64Array } | number;
30 const values = typeof d === "number" ? Float64Array.of(d) : d.data;
31 return { values, vectors: V.data ?? Float64Array.of(1) };
32};