Skip to content

Commit ebea909

Browse files
committed
Document model options in FEAScriptWorker
setModelConfig and solve take the same options as FEAScriptModel. Explain that coefficient functions are sent as source code, so they must not use outer variables, and that a Content Security Policy must allow 'unsafe-eval'. Remove the limitations this fixes.
1 parent 0ca5b57 commit ebea909

1 file changed

Lines changed: 36 additions & 7 deletions

File tree

‎docs/api/web-worker.md‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,54 @@ model.terminate();
5353

5454
| Method | Returns | Description |
5555
| ---------------------------------------------- | ------------------ | ---------------------------------------------------------------- |
56-
| `setModelConfig(modelConfig)` | `Promise<boolean>` | Selects the model (name only, see the note below) |
56+
| `setModelConfig(modelConfig, options)` | `Promise<boolean>` | Selects the model and its options, as `FEAScriptModel` |
5757
| `setMeshConfig(meshConfig)` | `Promise<boolean>` | Sets the mesh, as `FEAScriptModel.setMeshConfig` |
5858
| `addBoundaryCondition(boundaryKey, condition)` | `Promise<boolean>` | Adds a boundary condition |
5959
| `setSolverMethod(solverMethod)` | `Promise<boolean>` | Selects the solver method |
60-
| `solve()` | `Promise<object>` | Solves the model in the worker |
60+
| `solve(options)` | `Promise<object>` | Solves the model in the worker (`maxIterations`, `tolerance`) |
6161
| `getModelInfo()` | `Promise<object>` | `{ solverConfig, meshConfig, boundaryConditions, solverMethod }` |
6262
| `ping()` | `Promise<boolean>` | Resolves to `true` when the worker is ready |
6363
| `terminate()` | – | Stops the worker |
6464

6565
`solve()` resolves to `{ solutionVector, nodesCoordinates, solverConfig, meshDimension }`. The object returned by
6666
`getModelInfo()` can be passed as the `model` argument of the [plotting functions](./visualization.md).
6767

68+
## Model options
69+
70+
`setModelConfig` and `solve` take the same options as in `FEAScriptModel`, including `coefficientFunctions`,
71+
`nonlinear`, `maxIterations` and `tolerance`:
72+
73+
```javascript
74+
await model.setModelConfig("generalFormPDEScript", {
75+
coefficientFunctions: {
76+
A: (x) => 1, // Diffusion coefficient
77+
B: (x) => -10, // Advection coefficient
78+
C: (x) => 0, // Reaction coefficient
79+
D: (x) => 10 * Math.exp(-200 * Math.pow(x - 0.5, 2)), // Source term
80+
},
81+
});
82+
```
83+
84+
Functions cannot be sent to a web worker, so `FEAScriptWorker` sends each coefficient function as its source code
85+
and rebuilds it in the worker. A rebuilt function does not have access to the variables around it, so write the
86+
values into the function:
87+
88+
```javascript
89+
const Da = 0.5;
90+
91+
// Does not work in the worker: Da is not defined there
92+
D: (x, u) => Da * u ** 2,
93+
94+
// Works in the worker
95+
D: (x, u) => 0.5 * u ** 2,
96+
```
97+
98+
Use functions defined in your own code: built-in functions such as `Math.sin` must be wrapped, e.g.
99+
`(x) => Math.sin(x)`. Rebuilding the functions requires `eval`, so a page with a
100+
[Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) must allow `'unsafe-eval'`.
101+
68102
## Limitations
69103

70-
- `setModelConfig` takes only the model name: model options such as `coefficientFunctions`, `nonlinear`,
71-
`maxIterations` and `tolerance` cannot be passed to the worker (functions cannot be sent to a web worker). Models
72-
that need coefficient functions (the general form PDE and the Euler–Bernoulli beam) can therefore not run in
73-
the worker yet, and heat conduction runs with the default coefficients ($k = 1$, $Q = 0$).
74-
- `solve()` takes no options, so the solvers run with their default `maxIterations` and `tolerance`.
75104
- `solveAsync` and the `"jacobi-gpu"` method are not available through the worker.
76105

77106
## Examples

0 commit comments

Comments
 (0)