Skip to content

Commit e7a83a5

Browse files
committed
Update HeatConduction2DFinWorker.html to use local FEAWrapperScript to resolve CORS issues
1 parent 11b3305 commit e7a83a5

File tree

3 files changed

+164
-9
lines changed

3 files changed

+164
-9
lines changed

tutorials/FEAWrapperScript.js

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// ______ ______ _____ _ _ //
2+
// | ____| ____| /\ / ____| (_) | | //
3+
// | |__ | |__ / \ | (___ ___ ____ _ ____ | |_ //
4+
// | __| | __| / /\ \ \___ \ / __| __| | _ \| __| //
5+
// | | | |____ / ____ \ ____) | (__| | | | |_) | | //
6+
// |_| |______/_/ \_\_____/ \___|_| |_| __/| | //
7+
// | | | | //
8+
// |_| | |_ //
9+
// Website: https://feascript.com/ \__| //
10+
11+
// import * as Comlink from "https://unpkg.com/comlink/dist/esm/comlink.mjs";
12+
// The Web Worker functionality now uses the local Comlink library to avoid CORS issues
13+
import * as Comlink from "./vendor/comlink.mjs";
14+
import { FEAScriptModel } from "./FEAScript.js";
15+
import { create, all } from "https://cdn.jsdelivr.net/npm/mathjs@latest/+esm";
16+
17+
const math = create(all);
18+
19+
globalThis.math = math;
20+
21+
class FEAWorkerWrapper {
22+
/**
23+
* Constructor to initialize the FEAWorkerWrapper class.
24+
* Creates an instance of the FEAScriptModel.
25+
* @throws Will throw an error if the FEAScriptModel fails to initialize.
26+
*/
27+
constructor() {
28+
try {
29+
this.model = new FEAScriptModel();
30+
} catch (error) {
31+
console.error("FEA Worker: Error initializing FEAScriptModel", error);
32+
throw error;
33+
}
34+
}
35+
36+
/**
37+
* Sets the solver configuration in the FEAScriptModel.
38+
* @param {string} solverConfig - The solver configuration to set.
39+
* @returns {boolean} Returns true if the configuration is set successfully.
40+
* @throws Will throw an error if the configuration fails to set.
41+
*/
42+
setSolverConfig(solverConfig) {
43+
try {
44+
this.model.setSolverConfig(solverConfig);
45+
return true;
46+
} catch (error) {
47+
console.error("FEA Worker: Error in setSolverConfig", error);
48+
throw error;
49+
}
50+
}
51+
52+
/**
53+
* Sets the mesh configuration in the FEAScriptModel.
54+
* @param {object} meshConfig - The mesh configuration to set.
55+
* @returns {boolean} Returns true if the configuration is set successfully.
56+
* @throws Will throw an error if the configuration fails to set.
57+
*/
58+
setMeshConfig(meshConfig) {
59+
try {
60+
this.model.setMeshConfig(meshConfig);
61+
return true;
62+
} catch (error) {
63+
console.error("FEA Worker: Error in setMeshConfig", error);
64+
throw error;
65+
}
66+
}
67+
68+
/**
69+
* Adds a boundary condition to the FEAScriptModel.
70+
* @param {string} boundaryKey - The key identifying the boundary.
71+
* @param {array} condition - The boundary condition to add.
72+
* @returns {boolean} Returns true if the boundary condition is added successfully.
73+
* @throws Will throw an error if the boundary condition fails to add.
74+
*/
75+
addBoundaryCondition(boundaryKey, condition) {
76+
try {
77+
this.model.addBoundaryCondition(boundaryKey, condition);
78+
return true;
79+
} catch (error) {
80+
console.error("FEA Worker: Error in addBoundaryCondition", error);
81+
throw error;
82+
}
83+
}
84+
85+
/**
86+
* Sets the solver method in the FEAScriptModel.
87+
* @param {string} solverMethod - The solver method to set.
88+
* @returns {boolean} Returns true if the solver method is set successfully.
89+
* @throws Will throw an error if the solver method fails to set.
90+
*/
91+
setSolverMethod(solverMethod) {
92+
try {
93+
this.model.setSolverMethod(solverMethod);
94+
return true;
95+
} catch (error) {
96+
console.error("FEA Worker: Error in setSolverMethod", error);
97+
throw error;
98+
}
99+
}
100+
101+
/**
102+
* Solves the problem using the FEAScriptModel.
103+
* @returns {object} Returns the solution result, including the solution vector, node coordinates, solver configuration, and mesh dimension.
104+
* @throws Will throw an error if the solve operation fails.
105+
*/
106+
solve() {
107+
try {
108+
const result = this.model.solve();
109+
110+
return {
111+
solutionVector: result.solutionVector,
112+
nodesCoordinates: result.nodesCoordinates,
113+
solverConfig: this.model.solverConfig,
114+
meshDimension: this.model.meshConfig.meshDimension,
115+
};
116+
} catch (error) {
117+
console.error("FEA Worker: Error in solve", error);
118+
throw error;
119+
}
120+
}
121+
122+
/**
123+
* Retrieves model information from the FEAScriptModel.
124+
* @returns {object} Returns the model information, including solver configuration, mesh configuration, boundary conditions, and solver method.
125+
* @throws Will throw an error if the model information fails to retrieve.
126+
*/
127+
getModelInfo() {
128+
try {
129+
return {
130+
solverConfig: this.model.solverConfig,
131+
meshConfig: this.model.meshConfig,
132+
boundaryConditions: this.model.boundaryConditions,
133+
solverMethod: this.model.solverMethod,
134+
};
135+
} catch (error) {
136+
console.error("FEA Worker: Error in getModelInfo", error);
137+
throw error;
138+
}
139+
}
140+
141+
/**
142+
* Simple ping method to check if the worker is responsive.
143+
* @returns {boolean} Returns true to indicate the worker is available.
144+
*/
145+
ping() {
146+
try {
147+
return true;
148+
} catch (error) {
149+
console.error("FEA Worker: Error in ping", error);
150+
throw error;
151+
}
152+
}
153+
}
154+
155+
Comlink.expose(FEAWorkerWrapper);

tutorials/HeatConduction2DFin.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,20 @@ <h2 id="results"><a name="Results"></a>Results</h2>
218218
>.
219219
</p>
220220

221-
<!-- Add link to worker-based implementation -->
221+
<!-- Container element where the solution plot will be rendered -->
222+
<div id="orientation-message">
223+
Cannot draw the results. Please turn your phone to horizontal position to see the results.
224+
</div>
225+
<div id="solutionPlot"></div>
226+
227+
<!-- Link to worker-based implementation -->
222228
<div class="highlight-container">
223229
<p>
224230
See also the <a href="HeatConduction2DFinWorker.html">Heat Conduction in a Two-Dimensional Fin (Web Worker version)</a>
225231
for an example using FEAScript in a separate thread for improved responsiveness.
226232
</p>
227233
</div>
228234

229-
<!-- Container element where the solution plot will be rendered -->
230-
<div id="orientation-message">
231-
Cannot draw the results. Please turn your phone to horizontal position to see the results.
232-
</div>
233-
<div id="solutionPlot"></div>
234-
235235
<ul id="menu">
236236
<li>
237237
<a href="https://feascript.com/index.html" target="_blank">Return</a>

tutorials/HeatConduction2DFinWorker.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ <h2>Web Worker Implementation</h2>
9999

100100
<pre class="prettyprint">
101101
&lt;script type="module"&gt;
102-
import { FEAWorkerScript, plotSolution, printVersion } from "https://feascript.github.io/FEAScript-core/src/index.js";
102+
import { FEAWorkerScript, plotSolution, printVersion } from "../FEAWrapperScript.js";
103103
window.addEventListener("DOMContentLoaded", async () =&gt; {
104104
// Print FEAScript version
105105
printVersion();
@@ -168,7 +168,7 @@ <h2>Results</h2>
168168
FEAWorkerScript,
169169
plotSolution,
170170
printVersion,
171-
} from "https://feascript.github.io/FEAScript-core/src/index.js";
171+
} from "../FEAWrapperScript.js";
172172
window.addEventListener("DOMContentLoaded", async () => {
173173
if (window.innerHeight > window.innerWidth) {
174174
document.getElementById("orientation-message").style.display = "block";

0 commit comments

Comments
 (0)