|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +--- |
| 4 | + |
| 5 | +# FEAScriptModel |
| 6 | + |
| 7 | +`FEAScriptModel` is the main class of FEAScript. It holds the configuration of a simulation (model, mesh, |
| 8 | +boundary conditions and solver) and solves it. |
| 9 | + |
| 10 | +```javascript |
| 11 | +import { FEAScriptModel } from "feascript"; |
| 12 | + |
| 13 | +const model = new FEAScriptModel(); |
| 14 | +model.setModelConfig("heatConductionScript"); |
| 15 | +model.setMeshConfig({ meshDimension: "1D", elementOrder: "linear", numElementsX: 10, maxX: 0.15 }); |
| 16 | +model.addBoundaryCondition("0", ["convection", 1, 25]); |
| 17 | +model.addBoundaryCondition("1", ["constantTemperature", 5]); |
| 18 | +model.setSolverMethod("lusolve"); |
| 19 | +const { solutionVector, nodesCoordinates } = model.solve(); |
| 20 | +``` |
| 21 | + |
| 22 | +## Constructor |
| 23 | + |
| 24 | +### `new FEAScriptModel()` |
| 25 | + |
| 26 | +Creates an empty model. The solver method defaults to `"lusolve"`. |
| 27 | + |
| 28 | +## Methods |
| 29 | + |
| 30 | +### `setModelConfig(modelConfig, options)` |
| 31 | + |
| 32 | +Selects the physical model and sets its options. |
| 33 | + |
| 34 | +| Parameter | Type | Description | |
| 35 | +| ------------- | -------- | ---------------------------------- | |
| 36 | +| `modelConfig` | `string` | Name of the model (see below) | |
| 37 | +| `options` | `object` | Optional model options (see below) | |
| 38 | + |
| 39 | +| `modelConfig` | Model | Dimensions | |
| 40 | +| ---------------------------- | --------------------------------------------------------- | ---------- | |
| 41 | +| `"heatConductionScript"` | [Heat conduction](../models/heat-conduction.md) | 1D, 2D | |
| 42 | +| `"generalFormPDEScript"` | [General form PDE](../models/general-form-pde.md) | 1D | |
| 43 | +| `"frontPropagationScript"` | [Front propagation](../models/front-propagation.md) | 1D, 2D | |
| 44 | +| `"creepingFlowScript"` | [Creeping flow](../models/creeping-flow.md) | 2D | |
| 45 | +| `"eulerBernoulliBeamScript"` | [Euler–Bernoulli beam](../models/euler-bernoulli-beam.md) | 1D | |
| 46 | + |
| 47 | +| Option | Type | Used by | Description | |
| 48 | +| ---------------------- | --------- | ------------------------------------------------------- | ------------------------------------------------------ | |
| 49 | +| `coefficientFunctions` | `object` | Heat conduction, general form PDE, Euler–Bernoulli beam | Coefficients of the equation; see each model page | |
| 50 | +| `nonlinear` | `boolean` | General form PDE | Solve with the Newton–Raphson method (default `false`) | |
| 51 | +| `maxIterations` | `number` | All | Maximum iterations of the iterative method in use | |
| 52 | +| `tolerance` | `number` | All | Convergence tolerance of the iterative method in use | |
| 53 | + |
| 54 | +`maxIterations` and `tolerance` apply to the Jacobi solvers in linear models and to the Newton–Raphson method in |
| 55 | +nonlinear models; see [Solvers](../fundamentals/solvers.md) for their defaults. |
| 56 | + |
| 57 | +```javascript |
| 58 | +model.setModelConfig("heatConductionScript", { |
| 59 | + coefficientFunctions: { |
| 60 | + thermalConductivity: (x, y) => (x < 2 ? 10 : 1), |
| 61 | + heatSource: 0, |
| 62 | + }, |
| 63 | +}); |
| 64 | +``` |
| 65 | + |
| 66 | +### `setMeshConfig(meshConfig)` |
| 67 | + |
| 68 | +Sets the mesh: either the parameters of a structured mesh or a mesh imported from Gmsh. |
| 69 | + |
| 70 | +| Property | Type | Description | |
| 71 | +| --------------- | -------- | ------------------------------------------------------------------------------- | |
| 72 | +| `meshDimension` | `string` | `"1D"` or `"2D"` (detected automatically for `parsedMesh`) | |
| 73 | +| `elementOrder` | `string` | `"linear"` (default) or `"quadratic"` (detected automatically for `parsedMesh`) | |
| 74 | +| `numElementsX` | `number` | Number of elements along $x$ (structured mesh) | |
| 75 | +| `numElementsY` | `number` | Number of elements along $y$ (structured 2D mesh) | |
| 76 | +| `maxX` | `number` | Length of the domain along $x$ (structured mesh) | |
| 77 | +| `maxY` | `number` | Height of the domain along $y$ (structured 2D mesh) | |
| 78 | +| `angleLeft` | `number` | Interior angle of the left side in degrees, default `90` (structured 2D mesh) | |
| 79 | +| `angleRight` | `number` | Interior angle of the right side in degrees, default `90` (structured 2D mesh) | |
| 80 | +| `parsedMesh` | `object` | Mesh returned by [`importGmshMesh`](./gmsh-import.md) | |
| 81 | + |
| 82 | +See [Mesh](../fundamentals/mesh.md) for the geometry, the boundary keys and the node numbering. |
| 83 | + |
| 84 | +### `addBoundaryCondition(boundaryKey, condition)` |
| 85 | + |
| 86 | +Adds a boundary condition, or replaces the condition of a boundary that already has one. |
| 87 | + |
| 88 | +| Parameter | Type | Description | |
| 89 | +| ------------- | -------- | ----------------------------------------------------------------------------- | |
| 90 | +| `boundaryKey` | `string` | Boundary key (`"0"`, `"1"`, ...), or node number for the Euler–Bernoulli beam | |
| 91 | +| `condition` | `array` | Condition type followed by its parameters, e.g. `["convection", 1, 20]` | |
| 92 | + |
| 93 | +See [Boundary conditions](../fundamentals/boundary-conditions.md) for the condition types of each model. |
| 94 | +Condition types that the selected model does not recognize are reported as errors when `solve()` runs (see |
| 95 | +[Checks](../fundamentals/boundary-conditions.md#checks)). |
| 96 | + |
| 97 | +### `setSolverMethod(solverMethod)` |
| 98 | + |
| 99 | +Selects the linear system solver: `"lusolve"` (default), `"jacobi"`, `"frontal"` or `"jacobi-gpu"`. See |
| 100 | +[Solvers](../fundamentals/solvers.md) for the methods supported by each model. |
| 101 | + |
| 102 | +### `solve(options)` |
| 103 | + |
| 104 | +Prepares the mesh, assembles the system, applies the boundary conditions and solves it. This method is |
| 105 | +synchronous. |
| 106 | + |
| 107 | +| Parameter | Type | Description | |
| 108 | +| --------- | -------- | ------------------------------------------------------------------------------------------- | |
| 109 | +| `options` | `object` | Optional `{ maxIterations, tolerance }`; they take precedence over `setModelConfig` options | |
| 110 | + |
| 111 | +Returns an object with: |
| 112 | + |
| 113 | +| Property | Type | Description | |
| 114 | +| ------------------------------------ | -------------- | --------------------------------------------- | |
| 115 | +| `solutionVector` | `Float64Array` | The computed unknowns (see the model pages) | |
| 116 | +| `nodesCoordinates.nodesXCoordinates` | `number[]` | $x$ coordinates of the mesh nodes | |
| 117 | +| `nodesCoordinates.nodesYCoordinates` | `number[]` | $y$ coordinates of the mesh nodes (2D meshes) | |
| 118 | + |
| 119 | +For scalar models (heat conduction, general form PDE, front propagation), `solutionVector` has one value per |
| 120 | +node, in the order of `nodesCoordinates`. The [creeping flow](../models/creeping-flow.md#results) and |
| 121 | +[Euler–Bernoulli beam](../models/euler-bernoulli-beam.md#results) models return more unknowns per node; see |
| 122 | +their pages for the layout. |
| 123 | + |
| 124 | +### `solveAsync(computeEngine, options)` |
| 125 | + |
| 126 | +Asynchronous version of `solve`, used with the `"jacobi-gpu"` solver method. It returns a `Promise` that resolves |
| 127 | +to the same object as `solve()`. |
| 128 | + |
| 129 | +```javascript |
| 130 | +model.setSolverMethod("jacobi-gpu"); |
| 131 | +const { solutionVector, nodesCoordinates } = await model.solveAsync(); |
| 132 | +``` |
| 133 | + |
| 134 | +| Parameter | Type | Description | |
| 135 | +| --------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 136 | +| `computeEngine` | `object` | Optional. An initialized WebGPU compute engine to reuse across solves. When omitted or `null`, FEAScript creates one for the solve and terminates it afterwards; a provided engine is left running | |
| 137 | +| `options` | `object` | Optional `{ maxIterations, tolerance }` | |
| 138 | + |
| 139 | +:::caution |
| 140 | +`solveAsync` currently supports only `heatConductionScript` with the `"jacobi-gpu"` solver method, in browsers |
| 141 | +with WebGPU support. For other models or solver methods, it returns an empty solution vector. |
| 142 | +::: |
| 143 | + |
| 144 | +## Error handling |
| 145 | + |
| 146 | +FEAScript reports configuration problems (for example an unsupported mesh for a model, or a solver that did not |
| 147 | +converge) as `[ERROR]` messages in the console, and in most cases continues instead of throwing an exception. |
| 148 | +Check the console output when a result looks wrong. See [Logging](./utilities.md#logsystem) to control the |
| 149 | +amount of output. |
0 commit comments