|
| 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 * as math from "mathjs"; |
| 12 | +global.math = math; |
| 13 | + |
| 14 | +import { FEAScriptModel } from "feascript"; |
| 15 | + |
| 16 | +const model = new FEAScriptModel(); |
| 17 | +model.setModelConfig("heatConductionScript"); |
| 18 | +model.setMeshConfig({ meshDimension: "1D", elementOrder: "linear", numElementsX: 10, maxX: 0.15 }); |
| 19 | +model.addBoundaryCondition("0", ["convection", 1, 25]); |
| 20 | +model.addBoundaryCondition("1", ["constantTemp", 5]); |
| 21 | +model.setSolverMethod("lusolve"); |
| 22 | +const { solutionVector, nodesCoordinates } = model.solve(); |
| 23 | +``` |
| 24 | + |
| 25 | +## Constructor |
| 26 | + |
| 27 | +### `new FEAScriptModel()` |
| 28 | + |
| 29 | +Creates an empty model. The solver method defaults to `"lusolve"`. |
| 30 | + |
| 31 | +## Methods |
| 32 | + |
| 33 | +### `setModelConfig(modelConfig, options)` |
| 34 | + |
| 35 | +Selects the physical model and sets its options. |
| 36 | + |
| 37 | +| Parameter | Type | Description | |
| 38 | +| ------------- | -------- | ---------------------------------- | |
| 39 | +| `modelConfig` | `string` | Name of the model (see below) | |
| 40 | +| `options` | `object` | Optional model options (see below) | |
| 41 | + |
| 42 | +| `modelConfig` | Model | Dimensions | |
| 43 | +| -------------------------- | --------------------------------------------------- | ---------- | |
| 44 | +| `"heatConductionScript"` | [Heat conduction](../models/heat-conduction.md) | 1D, 2D | |
| 45 | +| `"generalFormPDEScript"` | [General form PDE](../models/general-form-pde.md) | 1D | |
| 46 | +| `"frontPropagationScript"` | [Front propagation](../models/front-propagation.md) | 2D | |
| 47 | +| `"creepingFlowScript"` | [Creeping flow](../models/creeping-flow.md) | 2D | |
| 48 | + |
| 49 | +| Option | Type | Used by | Description | |
| 50 | +| ---------------------- | -------- | ---------------- | ---------------------------------------------------- | |
| 51 | +| `coefficientFunctions` | `object` | General form PDE | Coefficients of the equation; see the model page | |
| 52 | +| `maxIterations` | `number` | All | Maximum iterations of the iterative method in use | |
| 53 | +| `tolerance` | `number` | All | Convergence tolerance of the iterative method in use | |
| 54 | + |
| 55 | +`maxIterations` and `tolerance` apply to the Jacobi solvers in linear models and to the Newton–Raphson method in |
| 56 | +the front propagation model; see [Solvers](../fundamentals/solvers.md) for their defaults. |
| 57 | + |
| 58 | +```javascript |
| 59 | +model.setModelConfig("generalFormPDEScript", { |
| 60 | + coefficientFunctions: { |
| 61 | + A: (x) => 1, |
| 62 | + B: (x) => -10, |
| 63 | + C: (x) => 0, |
| 64 | + D: (x) => 10 * Math.exp(-200 * Math.pow(x - 0.5, 2)), |
| 65 | + }, |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +### `setSolverConfig(solverConfig, options)` |
| 70 | + |
| 71 | +Deprecated alias of `setModelConfig`, with the same parameters. It logs a deprecation warning; use |
| 72 | +`setModelConfig`. It was removed in version 0.3.0. |
| 73 | + |
| 74 | +### `setMeshConfig(meshConfig)` |
| 75 | + |
| 76 | +Sets the mesh: either the parameters of a structured mesh or a mesh imported from Gmsh. |
| 77 | + |
| 78 | +| Property | Type | Description | |
| 79 | +| --------------- | -------- | --------------------------------------------------------------------------------- | |
| 80 | +| `meshDimension` | `string` | `"1D"` or `"2D"` (required, also for `parsedMesh`) | |
| 81 | +| `elementOrder` | `string` | `"linear"` (default) or `"quadratic"` (must match the elements of a `parsedMesh`) | |
| 82 | +| `numElementsX` | `number` | Number of elements along $x$ (structured mesh) | |
| 83 | +| `numElementsY` | `number` | Number of elements along $y$ (structured 2D mesh) | |
| 84 | +| `maxX` | `number` | Length of the domain along $x$ (structured mesh) | |
| 85 | +| `maxY` | `number` | Height of the domain along $y$ (structured 2D mesh) | |
| 86 | +| `angleLeft` | `number` | Interior angle of the left side in degrees, default `90` (structured 2D mesh) | |
| 87 | +| `angleRight` | `number` | Interior angle of the right side in degrees, default `90` (structured 2D mesh) | |
| 88 | +| `parsedMesh` | `object` | Mesh returned by [`importGmshQuadTri`](./gmsh-import.md) | |
| 89 | + |
| 90 | +See [Mesh](../fundamentals/mesh.md) for the geometry, the boundary keys and the node numbering. |
| 91 | + |
| 92 | +### `addBoundaryCondition(boundaryKey, condition)` |
| 93 | + |
| 94 | +Adds a boundary condition, or replaces the condition of a boundary that already has one. |
| 95 | + |
| 96 | +| Parameter | Type | Description | |
| 97 | +| ------------- | -------- | ----------------------------------------------------------------------- | |
| 98 | +| `boundaryKey` | `string` | Boundary key (`"0"`, `"1"`, ...) | |
| 99 | +| `condition` | `array` | Condition type followed by its parameters, e.g. `["convection", 1, 20]` | |
| 100 | + |
| 101 | +See [Boundary conditions](../fundamentals/boundary-conditions.md) for the condition types of each model. |
| 102 | +Condition types that the selected model does not recognize are ignored without a message. |
| 103 | + |
| 104 | +### `setSolverMethod(solverMethod)` |
| 105 | + |
| 106 | +Selects the linear system solver: `"lusolve"` (default), `"jacobi"`, `"frontal"` or `"jacobi-gpu"`. See |
| 107 | +[Solvers](../fundamentals/solvers.md) for the methods supported by each model. |
| 108 | + |
| 109 | +### `solve(options)` |
| 110 | + |
| 111 | +Prepares the mesh, assembles the system, applies the boundary conditions and solves it. This method is |
| 112 | +synchronous. |
| 113 | + |
| 114 | +| Parameter | Type | Description | |
| 115 | +| --------- | -------- | ------------------------------------------------------------------------------------------- | |
| 116 | +| `options` | `object` | Optional `{ maxIterations, tolerance }`; they take precedence over `setModelConfig` options | |
| 117 | + |
| 118 | +Returns an object with: |
| 119 | + |
| 120 | +| Property | Type | Description | |
| 121 | +| ------------------------------------ | ---------- | --------------------------------------------- | |
| 122 | +| `solutionVector` | `number[]` | The computed unknowns (see the model pages) | |
| 123 | +| `nodesCoordinates.nodesXCoordinates` | `number[]` | $x$ coordinates of the mesh nodes | |
| 124 | +| `nodesCoordinates.nodesYCoordinates` | `number[]` | $y$ coordinates of the mesh nodes (2D meshes) | |
| 125 | + |
| 126 | +For the heat conduction, general form PDE and front propagation models, `solutionVector` has one value per node, |
| 127 | +in the order of `nodesCoordinates`. For the [creeping flow](../models/creeping-flow.md#results) model, it holds |
| 128 | +the velocity components and the pressure in consecutive blocks. |
| 129 | + |
| 130 | +### `solveAsync(computeEngine, options)` |
| 131 | + |
| 132 | +Asynchronous version of `solve`, used with the `"jacobi-gpu"` solver method. It returns a `Promise` that resolves |
| 133 | +to the same object as `solve()`. |
| 134 | + |
| 135 | +```javascript |
| 136 | +model.setSolverMethod("jacobi-gpu"); |
| 137 | +const { solutionVector, nodesCoordinates } = await model.solveAsync(); |
| 138 | +``` |
| 139 | + |
| 140 | +| Parameter | Type | Description | |
| 141 | +| --------------- | -------- | ------------------------------------------------------------------------------------ | |
| 142 | +| `computeEngine` | `object` | Not used in version 0.2.0: FEAScript creates a WebGPU compute engine for every solve | |
| 143 | +| `options` | `object` | Optional `{ maxIterations, tolerance }` | |
| 144 | + |
| 145 | +:::caution |
| 146 | +`solveAsync` supports only `heatConductionScript` with the `"jacobi-gpu"` solver method, in browsers with WebGPU |
| 147 | +support, with FEAScript imported from `src/index.js` (see |
| 148 | +[Solvers](../fundamentals/solvers.md#jacobi-method-on-the-gpu-jacobi-gpu)). For other models or solver methods, it |
| 149 | +returns an empty solution vector. |
| 150 | +::: |
| 151 | + |
| 152 | +## Error handling |
| 153 | + |
| 154 | +FEAScript reports configuration problems (for example a creeping flow model on linear elements, or a solver that |
| 155 | +did not converge) as `[ERROR]` messages in the console, and in most cases continues instead of throwing an |
| 156 | +exception. Some invalid configurations, such as a missing `meshDimension`, end in a JavaScript error later in the |
| 157 | +solution. Check the console output when a result looks wrong. |
0 commit comments