|
| 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); |
0 commit comments