Skip to content

Commit bd50688

Browse files
committed
Add WebGPU compute and Jacobi workers with comprehensive testing suite
- Implemented WebGPUComputeWorker for various linear algebra operations including vector addition, matrix-vector multiplication, dot product, normalization, and more. - Created WebGPUJacobiWorker for solving linear systems using the Jacobi method. - Developed a test HTML page to validate the functionality of the compute engine with multiple test cases for each operation. - Included error handling and success messages for better user feedback during tests. - Ensured compatibility with Comlink for worker communication.
1 parent 682991d commit bd50688

File tree

11 files changed

+5375
-383
lines changed

11 files changed

+5375
-383
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Heat Conduction 2D Fin - CG Solver</title>
7+
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.0.0/math.min.js"></script>
8+
<script src="https://cdnjs.cloudflare.com/ajax/libs/plotly.js/2.27.0/plotly.min.js"></script>
9+
<style>
10+
body { font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; }
11+
.result { background-color: #f5f5f5; padding: 10px; margin: 10px 0; border-radius: 3px; }
12+
.success { color: green; }
13+
.error { color: red; }
14+
</style>
15+
</head>
16+
<body>
17+
<h1>Heat Conduction in 2D Fin - Conjugate Gradient Solver</h1>
18+
<div id="solutionPlot"></div>
19+
<div id="result" class="result"></div>
20+
21+
<script type="module">
22+
import { FEAScriptModel, plotSolution } from "../../../src/index.js";
23+
import * as Comlink from '../../../src/vendor/comlink.mjs';
24+
25+
window.addEventListener("DOMContentLoaded", async () => {
26+
27+
28+
// Create a new FEAScript model
29+
const model = new FEAScriptModel();
30+
31+
// Set solver configuration
32+
model.setSolverConfig("solidHeatTransferScript");
33+
34+
// Define mesh configuration
35+
model.setMeshConfig({
36+
meshDimension: "2D",
37+
elementOrder: "quadratic",
38+
numElementsX: 40,
39+
numElementsY: 20,
40+
maxX: 4000,
41+
maxY: 2000,
42+
});
43+
44+
// Define boundary conditions
45+
model.addBoundaryCondition("0", ["constantTemp", 200]);
46+
model.addBoundaryCondition("1", ["symmetry"]);
47+
model.addBoundaryCondition("2", ["convection", 1, 20]);
48+
model.addBoundaryCondition("3", ["constantTemp", 200]);
49+
50+
// Initialize WebGPU worker
51+
const worker = new Worker('../../../src/workers/webgpuComputeWorker.js', { type: 'module' });
52+
const computeEngine = Comlink.wrap(worker);
53+
await computeEngine.initialize();
54+
55+
const resultDiv = document.getElementById('result');
56+
resultDiv.innerHTML = 'Assembling system and solving with GPU CG...';
57+
58+
try {
59+
// Solve using FEAScriptModel with WebGPU CG
60+
const { solutionVector, nodesCoordinates } = await model.solveWithWebGPU(computeEngine);
61+
62+
// Compute residual for verification
63+
const A = []; // We don't have direct access to the assembled matrix, but we can compute residual norm
64+
const b = []; // Similarly for RHS
65+
// For now, just show that we got a solution
66+
const n = solutionVector.length;
67+
68+
// Display results
69+
resultDiv.innerHTML = `
70+
<div>System size: ${n} x ${n}</div>
71+
<div>Solution computed with WebGPU CG</div>
72+
<div class="success">✓ SOLUTION COMPLETED</div>
73+
`;
74+
75+
// Plot the solution using FEAScript's plotSolution function
76+
plotSolution(
77+
solutionVector,
78+
nodesCoordinates,
79+
model.solverConfig,
80+
model.meshConfig.meshDimension,
81+
"contour",
82+
"solutionPlot"
83+
);
84+
85+
// Cleanup
86+
await computeEngine.destroy();
87+
worker.terminate();
88+
89+
} catch (error) {
90+
resultDiv.innerHTML = `<span class="error">Error: ${error.message}</span>`;
91+
console.error(error);
92+
}
93+
});
94+
</script>
95+
</body>
96+
</html>

0 commit comments

Comments
 (0)