/ proof-of-concept / plate-mechanics-sim
Sign in
proof-of-concept / plate-mechanics-sim
plate-mechanics-sim / packages / core / src / generate / bundle.ts
130 lines · 4.6 KBBlameHistoryRaw
1import type { ProblemSpec } from "../spec/types";
2import type { MethodDefinition } from "../methods/types";
3import type { GpuStages } from "../runner/types";
4import { generateParamsBlock } from "./params";
5import { LIBRARY_FILES, type SourceFile } from "./library";
7/** A complete, self-contained set of `.m` files plus the one to run. */
8export interface SourceBundle {
9 files: SourceFile[];
10 /** File executed as the script */
11 entry: string;
12 /** Workspace variable read back after the run */
13 resultVariable: string;
16export const MAIN_FILE = "main.m";
18/**
19 * The script a method runs: the params block followed by the solver call. With
20 * `gpu`, for a method whose loop has a WebGPU port, the call is written out as
21 * its three stages, since that is what the host then runs (see buildGpuStages).
22 */
23export function generateMain(
24 spec: ProblemSpec,
25 method: MethodDefinition,
26 opts: { gpu?: boolean } = {},
27): string {
28 const g = opts.gpu ? method.gpu : undefined;
29 const solve = g
30 ? [
31 `% Solve. With the GPU option the host runs ${g.setupFunction} and ${g.outputFunction} here in numbl and`,
32 `% ${g.loopFile} as its WebGPU port; without WebGPU, or when this script or ${g.loopFile} is edited,`,
33 `% all three run in numbl. The library files are in the other tabs.`,
34 `S = ${g.setupFunction}(params);`,
35 `R = ${g.loopFile.replace(/\.m$/, "")}(S); % replaced by the WebGPU port when the loop runs on the GPU`,
36 `result = ${g.outputFunction}(S, R);`,
37 ]
38 : [
39 `% Solve. ${method.solverFunction} and the functions it calls are the library files in the other tabs.`,
40 `result = ${method.solverFunction}(params);`,
41 ];
42 return [generateParamsBlock(spec), "", ...solve, ""].join("\n");
45/** Bundle for running one method on a spec. */
46export function buildRunBundle(
47 spec: ProblemSpec,
48 method: MethodDefinition,
49 opts: { gpu?: boolean } = {},
50): SourceBundle {
51 return {
52 files: [{ name: MAIN_FILE, source: generateMain(spec, method, opts) }, ...LIBRARY_FILES],
53 entry: MAIN_FILE,
54 resultVariable: "result",
55 };
58/**
59 * The two numbl scripts of a GPU run (see runner/executeGpu.ts): the setup,
60 * whose struct S the host reads (sparse operators as find() triplets, since
61 * a sparse matrix does not cross to the host), and the output, which takes the
62 * loop's record as the workspace variable R. Null for a method without a port.
63 */
64export function buildGpuStages(spec: ProblemSpec, method: MethodDefinition): GpuStages | null {
65 const g = method.gpu;
66 if (!g) return null;
67 const params = generateParamsBlock(spec);
68 const setup = [
69 params,
70 "",
71 `% Stage 1 of a GPU run: the setup, read by the WebGPU port of ${g.loopFile}.`,
72 `S = ${g.setupFunction}(params);`,
73 "if S.sparse",
74 " % the sparse operators cross to the host as triplets",
75 " [ki, kj, kv] = find(S.Km); S.Km_i = ki; S.Km_j = kj; S.Km_v = kv;",
76 " [li, lj, lv] = find(S.Lm); S.Lm_i = li; S.Lm_j = lj; S.Lm_v = lv;",
77 "end",
78 "",
79 ].join("\n");
80 const output = [
81 params,
82 "",
83 `% Stage 3 of a GPU run: R is the record of the WebGPU loop, passed in by the host.`,
84 `S = ${g.setupFunction}(params);`,
85 `result = ${g.outputFunction}(S, R);`,
86 "",
87 ].join("\n");
88 return { setup, output, loopFile: g.loopFile };
91/** Bundle that evaluates a method's cost plan (currently only the FD method has one). */
92export function buildPlanBundle(spec: ProblemSpec, method: MethodDefinition): SourceBundle | null {
93 if (!method.planFunction) return null;
94 const main = [generateParamsBlock(spec), "", `plan = ${method.planFunction}(params);`, ""].join(
95 "\n",
96 );
97 return {
98 files: [{ name: "plan.m", source: main }, ...LIBRARY_FILES],
99 entry: "plan.m",
100 resultVariable: "plan",
101 };
104/** Bundle comparing two results, which the runner passes in as workspace variables A and B. */
105export function buildCompareBundle(): SourceBundle {
106 const main = [
107 "% A and B are two result structs for the same problem (A: modal, B: finite differences).",
108 "cmp = plate_compare(A, B);",
109 "",
110 ].join("\n");
111 return {
112 files: [{ name: "compare.m", source: main }, ...LIBRARY_FILES],
113 entry: "compare.m",
114 resultVariable: "cmp",
115 };
118/**
119 * Replace files in a bundle with edited versions, keeping everything else. Used
120 * when the user edits source in the code view: the run then uses exactly the
121 * text on screen.
122 */
123export function withEdits(bundle: SourceBundle, edits: Record<string, string>): SourceBundle {
124 return {
125 ...bundle,
126 files: bundle.files.map((f) =>
127 edits[f.name] !== undefined ? { ...f, source: edits[f.name] } : f,
128 ),
129 };
↑↓ move↵openescclose