Skip to content

Commit 359d86b

Browse files
Added worker (#9)
* Add FEAWorkerScript and FEAWrapperScript for finite element analysis worker functionality * Add FEAWorkerScript export to index.js --------- Co-authored-by: Sridhar.Mani <sridhar.mani@novacastindia.com>
1 parent afc067f commit 359d86b

3 files changed

Lines changed: 208 additions & 0 deletions

File tree

src/FEAWorkerScript.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import * as Comlink from "https://unpkg.com/comlink/dist/esm/comlink.mjs";
2+
import { basicLog } from "./utilities/utilitiesScript.js";
3+
4+
export class FEAWorkerScript {
5+
constructor() {
6+
this.worker = null;
7+
this.feaWorker = null;
8+
this.isReady = false;
9+
10+
this._initWorker();
11+
}
12+
13+
async _initWorker() {
14+
try {
15+
this.worker = new Worker(
16+
new URL("./FEAWrapperScript.js", import.meta.url),
17+
{
18+
type: "module",
19+
}
20+
);
21+
22+
this.worker.onerror = (event) => {
23+
console.error("FEAWorkerScript: Worker error:", event);
24+
};
25+
const FEAWorkerWrapper = Comlink.wrap(this.worker);
26+
27+
this.feaWorker = await new FEAWorkerWrapper();
28+
29+
this.isReady = true;
30+
} catch (error) {
31+
console.error("Failed to initialize worker", error);
32+
throw error;
33+
}
34+
}
35+
36+
async _ensureReady() {
37+
if (this.isReady) return Promise.resolve();
38+
39+
return new Promise((resolve, reject) => {
40+
let attempts = 0;
41+
const maxAttempts = 50; // 5 seconds max
42+
43+
const checkReady = () => {
44+
attempts++;
45+
if (this.isReady) {
46+
resolve();
47+
} else if (attempts >= maxAttempts) {
48+
reject(new Error("Timeout waiting for worker to be ready"));
49+
} else {
50+
setTimeout(checkReady, 1000);
51+
}
52+
};
53+
checkReady();
54+
});
55+
}
56+
57+
async setSolverConfig(solverConfig) {
58+
await this._ensureReady();
59+
basicLog(`FEAWorkerScript: Setting solver config to: ${solverConfig}`);
60+
return this.feaWorker.setSolverConfig(solverConfig);
61+
}
62+
63+
async setMeshConfig(meshConfig) {
64+
await this._ensureReady();
65+
basicLog(`FEAWorkerScript: Setting mesh config`);
66+
return this.feaWorker.setMeshConfig(meshConfig);
67+
}
68+
69+
async addBoundaryCondition(boundaryKey, condition) {
70+
await this._ensureReady();
71+
basicLog(
72+
`FEAWorkerScript: Adding boundary condition for boundary: ${boundaryKey}`
73+
);
74+
return this.feaWorker.addBoundaryCondition(boundaryKey, condition);
75+
}
76+
77+
async setSolverMethod(solverMethod) {
78+
await this._ensureReady();
79+
basicLog(`FEAWorkerScript: Setting solver method to: ${solverMethod}`);
80+
return this.feaWorker.setSolverMethod(solverMethod);
81+
}
82+
83+
async solve() {
84+
await this._ensureReady();
85+
basicLog("FEAWorkerScript: Requesting solution from worker...");
86+
87+
const startTime = performance.now();
88+
const result = await this.feaWorker.solve();
89+
const endTime = performance.now();
90+
91+
basicLog(
92+
`FEAWorkerScript: Solution completed in ${(
93+
(endTime - startTime) /
94+
1000
95+
).toFixed(2)}s`
96+
);
97+
return result;
98+
}
99+
100+
async getModelInfo() {
101+
await this._ensureReady();
102+
return this.feaWorker.getModelInfo();
103+
}
104+
105+
async ping() {
106+
await this._ensureReady();
107+
return this.feaWorker.ping();
108+
}
109+
110+
terminate() {
111+
if (this.worker) {
112+
this.worker.terminate();
113+
this.worker = null;
114+
this.feaWorker = null;
115+
this.isReady = false;
116+
}
117+
}
118+
}

src/FEAWrapperScript.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import * as Comlink from "https://unpkg.com/comlink/dist/esm/comlink.mjs";
2+
import { FEAScriptModel } from "./FEAScript.js";
3+
import { create, all } from "https://cdn.jsdelivr.net/npm/mathjs@latest/+esm";
4+
5+
const math = create(all);
6+
7+
globalThis.math = math;
8+
9+
class FEAWorkerWrapper {
10+
constructor() {
11+
try {
12+
this.model = new FEAScriptModel();
13+
} catch (error) {
14+
console.error("FEA Worker: Error initializing FEAScriptModel", error);
15+
throw error;
16+
}
17+
}
18+
19+
setSolverConfig(solverConfig) {
20+
try {
21+
this.model.setSolverConfig(solverConfig);
22+
return true;
23+
} catch (error) {
24+
console.error("FEA Worker: Error in setSolverConfig", error);
25+
throw error;
26+
}
27+
}
28+
29+
setMeshConfig(meshConfig) {
30+
try {
31+
this.model.setMeshConfig(meshConfig);
32+
return true;
33+
} catch (error) {
34+
console.error("FEA Worker: Error in setMeshConfig", error);
35+
throw error;
36+
}
37+
}
38+
39+
addBoundaryCondition(boundaryKey, condition) {
40+
try {
41+
this.model.addBoundaryCondition(boundaryKey, condition);
42+
return true;
43+
} catch (error) {
44+
console.error("FEA Worker: Error in addBoundaryCondition", error);
45+
throw error;
46+
}
47+
}
48+
49+
setSolverMethod(solverMethod) {
50+
try {
51+
this.model.setSolverMethod(solverMethod);
52+
return true;
53+
} catch (error) {
54+
console.error("FEA Worker: Error in setSolverMethod", error);
55+
throw error;
56+
}
57+
}
58+
59+
solve() {
60+
try {
61+
const result = this.model.solve();
62+
63+
return {
64+
solutionVector: result.solutionVector,
65+
nodesCoordinates: result.nodesCoordinates,
66+
solverConfig: this.model.solverConfig,
67+
meshDimension: this.model.meshConfig.meshDimension,
68+
};
69+
} catch (error) {
70+
console.error("FEA Worker: Error in solve", error);
71+
throw error;
72+
}
73+
}
74+
getModelInfo() {
75+
try {
76+
return {
77+
solverConfig: this.model.solverConfig,
78+
meshConfig: this.model.meshConfig,
79+
boundaryConditions: this.model.boundaryConditions,
80+
solverMethod: this.model.solverMethod,
81+
};
82+
} catch (error) {
83+
console.error("FEA Worker: Error in getModelInfo", error);
84+
throw error;
85+
}
86+
}
87+
}
88+
89+
Comlink.expose(FEAWorkerWrapper);

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
export { FEAScriptModel } from "./FEAScript.js";
1212
export { plotSolution } from "./visualization/plotSolutionScript.js";
1313
export { printVersion, logSystem } from "./utilities/utilitiesScript.js";
14+
export { FEAWorkerScript } from "./FEAWorkerScript.js";

0 commit comments

Comments
 (0)