Repository navigation
Expand file tree
/
Copy pathumd.test.html
More file actions
212 lines (203 loc) · 9.51 KB
/
Copy pathumd.test.html
File metadata and controls
212 lines (203 loc) · 9.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<!--
* ════════════════════════════════════════════════════════════════
* FEAScript Core Library
* Finite Element Simulation in JavaScript
* Version: 0.4.1 | https://feascript.com
* MIT License © 2023–2026 FEAScript
* ════════════════════════════════════════════════════════════════
-->
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>FEAScript UMD test</title>
<!--
Test of the UMD bundle loaded with a classic <script> tag (no import map, no type="module").
Automatically in headless Chromium and Firefox: npm run test:browser (see README.md).
By hand: npm run build, serve the FEAScript-core folder and open this page, e.g.
npx http-server . -p 8080 -> http://localhost:8080/tests/umd/umd.test.html
The page lives in a different folder from dist/ on purpose: FEAScriptWorker must find
feascript-worker.esm.js next to feascript.umd.js, not next to this page.
-->
<style>
body {
font-family: system-ui, sans-serif;
margin: 16px;
background: #fff;
color: #111;
}
li.pass {
color: #137333;
}
li.fail {
color: #b3261e;
}
.plot {
width: 100%;
max-width: 640px;
height: 360px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/plotly.js/2.35.3/plotly.min.js"></script>
<script src="../../dist/feascript.umd.js"></script>
</head>
<body>
<h1>FEAScript UMD test</h1>
<ul id="results"></ul>
<div id="mainThreadPlot" class="plot"></div>
<div id="workerPlot" class="plot"></div>
<script>
const results = document.getElementById("results");
function report(name, ok, detail) {
const li = document.createElement("li");
li.className = ok ? "pass" : "fail";
li.textContent = `${ok ? "PASS" : "FAIL"}: ${name}${detail ? ` (${detail})` : ""}`;
results.appendChild(li);
console[ok ? "log" : "error"](li.textContent);
}
// Same 1D wall as examples/heatConduction/heatConduction1DWall: convection on the left, 5 °C on the right
const geometry = { line: 0.15 };
const mesh = { elementOrder: "linear", divisions: 10 };
const conditions = [
["left", { type: "convection", h: 1, ambientTemperature: 25 }],
["right", { type: "constantTemperature", temperature: 5 }],
];
const expectedRight = 5;
async function run() {
// 1. The bundle defines a FEAScript global
const hasGlobal = typeof FEAScript === "object" && typeof FEAScript.FEAScriptModel === "function";
report("window.FEAScript is defined", hasGlobal, hasGlobal ? `version ${FEAScript.version}` : "");
if (!hasGlobal) return;
const { FEAScriptModel, FEAScriptWorker, plotSolution } = FEAScript;
// 2. Solve on the main thread and plot with the Plotly global
let mainSolution;
try {
const model = new FEAScriptModel();
model.setModelConfig("heatConduction", { solver: { method: "lusolve" } });
model.setGeometry(geometry);
model.setMeshConfig(mesh);
for (const [boundary, condition] of conditions) model.addBoundaryCondition(boundary, condition);
const result = model.solve();
mainSolution = Array.from(result.solutionVector);
const right = mainSolution[mainSolution.length - 1];
report("Main-thread solve", Math.abs(right - expectedRight) < 1e-9, `T(right) = ${right}`);
plotSolution(model, result, "line", "mainThreadPlot");
report("Plotly plot", document.querySelector("#mainThreadPlot .plot-container") !== null);
} catch (error) {
report("Main-thread solve", false, error.message);
}
// 3. Solve in the worker: checks that feascript-worker.esm.js is found next to the bundle
try {
const worker = new FEAScriptWorker();
await worker.ping();
await worker.setModelConfig("heatConduction", { solver: { method: "lusolve" } });
await worker.setGeometry(geometry);
await worker.setMeshConfig(mesh);
for (const [boundary, condition] of conditions)
await worker.addBoundaryCondition(boundary, condition);
const result = await worker.solve();
const workerSolution = Array.from(result.solutionVector);
const maxDiff = mainSolution
? Math.max(...workerSolution.map((value, i) => Math.abs(value - mainSolution[i])))
: NaN;
report("Worker solve matches main thread", maxDiff < 1e-12, `max difference ${maxDiff}`);
report(
"Worker run report",
result.info?.method === "lusolve" && result.info.residual < 1e-12 && result.info.solveTime >= 0,
JSON.stringify(result.info),
);
plotSolution(await worker.getModelInfo(), result, "line", "workerPlot");
worker.terminate();
} catch (error) {
report("Worker solve", false, error.message);
}
// 3b. Triangles with a size function in the worker: the function is sent as its source, and the
// model information sent back plots the generated mesh
try {
const shape = {
start: [0, 0],
boundaries: [
{ name: "bottom", to: [4, 0] },
{ name: "right", to: [4, 2] },
{ name: "top", to: [0, 2] },
{ name: "left" },
],
};
const meshConfig = {
elements: "triangles",
elementOrder: "quadratic",
size: (x, y) => 0.3 + 0.1 * y,
};
const setUp = async (model) => {
await model.setModelConfig("heatConduction");
await model.setGeometry({ shape });
await model.setMeshConfig(meshConfig);
await model.addBoundaryCondition("bottom", { type: "constantTemperature", temperature: 200 });
await model.addBoundaryCondition("top", { type: "convection", h: 1, ambientTemperature: 20 });
};
const mainModel = new FEAScriptModel();
await setUp(mainModel);
const mainResult = mainModel.solve();
const worker = new FEAScriptWorker();
await worker.ping();
await setUp(worker);
const workerResult = await worker.solve();
const maxDiff = Math.max(
...Array.from(workerResult.solutionVector, (value, i) =>
Math.abs(value - mainResult.solutionVector[i]),
),
);
report(
"Worker triangles with a size function match the main thread",
workerResult.solutionVector.length === mainResult.solutionVector.length && maxDiff < 1e-12,
`${workerResult.solutionVector.length} nodes, max difference ${maxDiff}`,
);
plotSolution(await worker.getModelInfo(), workerResult, "contour", "workerPlot");
report("Plot from the worker model information", true);
worker.terminate();
} catch (error) {
report("Worker triangles with a size function", false, error.message);
}
// 4. Solve with "jacobi-gpu": checks that the WebGPU worker is found in src/workers/ beside dist/.
// Plain Jacobi converges slowly on the wall above (the convection node is weakly coupled), so this uses
// 4 elements with both temperatures fixed. Taichi.js works in 32-bit floats: the relative residual cannot
// go much below 1e-7, hence the tolerance and the looser match with LU
if (!navigator.gpu || !(await navigator.gpu.requestAdapter().catch(() => null))) {
report("WebGPU solve", true, "SKIP: no WebGPU adapter in this browser");
} else {
try {
const createModel = (solverMethod) => {
const model = new FEAScriptModel();
model.setModelConfig("heatConduction", { solver: { method: solverMethod } });
model.setGeometry(geometry);
model.setMeshConfig({ ...mesh, divisions: 4 });
model.addBoundaryCondition("left", { type: "constantTemperature", temperature: 25 });
model.addBoundaryCondition("right", { type: "constantTemperature", temperature: 5 });
return model;
};
const reference = Array.from(createModel("lusolve").solve().solutionVector);
const result = await createModel("jacobi-gpu").solveAsync(null, {
maxIterations: 500,
tolerance: 1e-6,
});
const gpuSolution = Array.from(result.solutionVector);
const maxDiff = Math.max(...gpuSolution.map((value, i) => Math.abs(value - reference[i])));
report("WebGPU solve matches LU", maxDiff < 1e-3, `max difference ${maxDiff}`);
report(
"WebGPU run report",
result.info?.method === "jacobi-gpu" &&
result.info.iterations > 0 &&
result.info.residual < 1e-6,
JSON.stringify(result.info),
);
} catch (error) {
report("WebGPU solve", false, error.message);
}
}
document.title = results.querySelector(".fail") ? "FAIL" : "PASS";
}
run();
</script>
</body>
</html>