@rulecad/fea 0.2.7 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/fea.d.mts +2 -1
- package/fea.mjs +6 -2
- package/package.json +5 -1
- package/threads.d.mts +9 -9
- package/threads.mjs +16 -5
package/README.md
CHANGED
|
@@ -42,7 +42,10 @@ r.thin; // { share, cells }: how much of the part is in members under
|
|
|
42
42
|
symmetric Gauss-Seidel through those blocks.
|
|
43
43
|
- **Threads:** `import { threadsBackend } from '@rulecad/fea/threads'` (Node only) shares each product over the
|
|
44
44
|
machine's cores (`FEA_THREADS` to say how many), the rest as `jsBackend`. On a 134,000-unknown grid a product takes
|
|
45
|
-
6.8 ms on seven threads against 19 ms on one, and an optimisation step about a third of the time.
|
|
45
|
+
6.8 ms on seven threads against 19 ms on one, and an optimisation step about a third of the time. It needs Node 22
|
|
46
|
+
or later: on Node 20 a product now and then came out wrong when the workers let go of an old operator's shared
|
|
47
|
+
memory (a few solves in sixty, more with more threads, none on Node 22 or 24), so there it is plain JavaScript
|
|
48
|
+
under another name (its `name` says so).
|
|
46
49
|
|
|
47
50
|
Tested against beam theory: a cantilever's tip moves within 1% of `F L³ / 3 E I` in 2D, and within 2% in 3D.
|
|
48
51
|
Multigrid is tested against the direct solver with stiffnesses a millionfold apart.
|
package/fea.d.mts
CHANGED
|
@@ -21,7 +21,7 @@ export function assemble(g: any, pat: any, cellMatrices: any, ke: any, scale: an
|
|
|
21
21
|
* @param {{ scale: Float64Array, ke: Float64Array, fixed: Uint8Array, loads: Float64Array[], springs?: Float64Array, method?: 'direct' | 'cg' | 'mg', backend?: typeof jsBackend, tol?: number, maxIter?: number, start?: Float64Array[] }} o
|
|
22
22
|
* springs: a stiffness added on each dof (N/mm), as a mechanism's input and output springs
|
|
23
23
|
* start: a first guess for the iterative solvers (the last solution, when solving again after a small change)
|
|
24
|
-
* @returns {{ u: Float64Array[], iterations: number, again: (loads: Float64Array[]) => Float64Array[] }} again: more
|
|
24
|
+
* @returns {{ u: Float64Array[], iterations: number, again: (loads: Float64Array[]) => Float64Array[], warning?: string }} warning: multigrid on a grid it couldn't halve far (slow); again: more
|
|
25
25
|
* loads on the same matrix (an adjoint), reusing its factor or preconditioner
|
|
26
26
|
*/
|
|
27
27
|
export function solve(g: Grid, { scale, ke, fixed, loads, springs, method, backend, tol, maxIter, start }: {
|
|
@@ -39,6 +39,7 @@ export function solve(g: Grid, { scale, ke, fixed, loads, springs, method, backe
|
|
|
39
39
|
u: Float64Array[];
|
|
40
40
|
iterations: number;
|
|
41
41
|
again: (loads: Float64Array[]) => Float64Array[];
|
|
42
|
+
warning?: string;
|
|
42
43
|
};
|
|
43
44
|
/**
|
|
44
45
|
* Stress at a cell's centre from its corners' movements, for Young's modulus 1: a matrix (rows × cell dofs, row by row;
|
package/fea.mjs
CHANGED
|
@@ -274,7 +274,7 @@ export function assemble(g, pat, cellMatrices, ke, scale, into) {
|
|
|
274
274
|
* @param {{ scale: Float64Array, ke: Float64Array, fixed: Uint8Array, loads: Float64Array[], springs?: Float64Array, method?: 'direct' | 'cg' | 'mg', backend?: typeof jsBackend, tol?: number, maxIter?: number, start?: Float64Array[] }} o
|
|
275
275
|
* springs: a stiffness added on each dof (N/mm), as a mechanism's input and output springs
|
|
276
276
|
* start: a first guess for the iterative solvers (the last solution, when solving again after a small change)
|
|
277
|
-
* @returns {{ u: Float64Array[], iterations: number, again: (loads: Float64Array[]) => Float64Array[] }} again: more
|
|
277
|
+
* @returns {{ u: Float64Array[], iterations: number, again: (loads: Float64Array[]) => Float64Array[], warning?: string }} warning: multigrid on a grid it couldn't halve far (slow); again: more
|
|
278
278
|
* loads on the same matrix (an adjoint), reusing its factor or preconditioner
|
|
279
279
|
*/
|
|
280
280
|
export function solve(g, { scale, ke, fixed, loads, springs, method = 'direct', backend = jsBackend, tol = 1e-9, maxIter = 20000, start }) {
|
|
@@ -302,8 +302,12 @@ export function solve(g, { scale, ke, fixed, loads, springs, method = 'direct',
|
|
|
302
302
|
diagonal(s, d) { cells.diagonal(s, d); for (let i = 0; i < d.length; i++) d[i] += springs[i]; },
|
|
303
303
|
} : cells;
|
|
304
304
|
const precondition = method === 'mg' ? multigrid(g, { scale, ke, fixed, backend, op }) : jacobi(g, { scale, fixed, op });
|
|
305
|
+
// Multigrid halves each side while it can: a grid with an odd side stops early, and its coarsest grid, solved
|
|
306
|
+
// directly, can be most of the work (33 × 17 × 17 cells took 80 times 32 × 16 × 16). Said, with the sides to give
|
|
307
|
+
const coarse = method === 'mg' ? hierarchy(g).grids.at(-1) : null, slow = coarse && coarse.dofs > 20 * COARSEST;
|
|
308
|
+
const warning = slow ? `multigrid stopped at ${[coarse.nx, coarse.ny, coarse.nz].filter(Boolean).join(' × ')} cells (${coarse.dofs} unknowns, solved directly): a side that can't be halved again; give each side a multiple of 8 cells for a fast solve` : null;
|
|
305
309
|
const again = (ls) => conjugateGradients(g, { scale, fixed, loads: held(ls), backend, tol, maxIter, precondition, op }).u;
|
|
306
|
-
return { ...conjugateGradients(g, { scale, fixed, loads: held(loads), backend, tol, maxIter, start, precondition, op }), again };
|
|
310
|
+
return { ...conjugateGradients(g, { scale, fixed, loads: held(loads), backend, tol, maxIter, start, precondition, op }), again, ...(warning ? { warning } : {}) };
|
|
307
311
|
}
|
|
308
312
|
|
|
309
313
|
// The banded Cholesky factor of K (cells' matrices: ke × scale, or each its own), fixed dofs as identity rows. Lenient
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/fea",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"description": "How far a part bends under load: finite elements on a grid of squares (2D) or cubes (3D), with a multigrid solver, in plain JavaScript.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -38,6 +38,10 @@
|
|
|
38
38
|
"url": "git+https://github.com/JoniBach/rulecad.git",
|
|
39
39
|
"directory": "packages/fea"
|
|
40
40
|
},
|
|
41
|
+
"homepage": "https://github.com/JoniBach/rulecad/tree/main/packages/fea#readme",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/JoniBach/rulecad/issues"
|
|
44
|
+
},
|
|
41
45
|
"scripts": {
|
|
42
46
|
"test": "node --test"
|
|
43
47
|
},
|
package/threads.d.mts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
multiply(scale:
|
|
6
|
-
diagonal(scale:
|
|
1
|
+
export const threadsBackend: {
|
|
2
|
+
name: string;
|
|
3
|
+
precision: number;
|
|
4
|
+
operator: (g: import("./fea.mjs").Grid, ke: Float64Array | null, cellMatrices?: Float64Array) => {
|
|
5
|
+
multiply: (scale: Float64Array | null, x: Float64Array, y: Float64Array) => void;
|
|
6
|
+
diagonal: (scale: Float64Array | null, d: Float64Array) => void;
|
|
7
7
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
8
|
+
dot: (a: Float64Array, b: Float64Array) => number;
|
|
9
|
+
axpy: (a: number, x: Float64Array, y: Float64Array) => void;
|
|
10
|
+
};
|
package/threads.mjs
CHANGED
|
@@ -8,7 +8,10 @@ import os from 'node:os';
|
|
|
8
8
|
import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads';
|
|
9
9
|
import { jsBackend, pattern, assemble } from './fea.mjs';
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
// FEA_THREADS: a whole number of worker threads, 1 to 8; anything else is refused by name (2.5 was taken as 2.5)
|
|
12
|
+
const asked = process.env.FEA_THREADS;
|
|
13
|
+
if (asked != null && asked !== '' && !/^[1-8]$/.test(asked.trim())) throw new Error(`FEA_THREADS is "${asked}": give a whole number of threads from 1 to 8, or leave it unset`);
|
|
14
|
+
const THREADS = asked ? Number(asked) : Math.max(1, Math.min(8, os.cpus().length - 1));
|
|
12
15
|
// How many operators each worker keeps: an optimisation step registers one per multigrid level (four or so), and
|
|
13
16
|
// their arrays are large (the bin's: 160 MB), so the workers let go of all but the last few. Before this, a run
|
|
14
17
|
// kept every step's and swapped itself to a crawl.
|
|
@@ -30,7 +33,7 @@ function workers() {
|
|
|
30
33
|
Atomics.store(ctl, 3, 0);
|
|
31
34
|
for (const w of list) w.postMessage({ id, ...arrays });
|
|
32
35
|
// Wake the workers to take the message (a product with no operator), then wait for every one to hold the arrays
|
|
33
|
-
pool.multiply(0);
|
|
36
|
+
pool.multiply(0); Atomics.store(ctl, 4, 0);
|
|
34
37
|
while (Atomics.load(ctl, 3) < THREADS) Atomics.wait(ctl, 3, Atomics.load(ctl, 3), 50);
|
|
35
38
|
return id;
|
|
36
39
|
},
|
|
@@ -38,13 +41,19 @@ function workers() {
|
|
|
38
41
|
Atomics.store(ctl, 2, 0); Atomics.store(ctl, 1, id);
|
|
39
42
|
Atomics.add(ctl, 0, 1); Atomics.notify(ctl, 0);
|
|
40
43
|
while (Atomics.load(ctl, 2) < THREADS) Atomics.wait(ctl, 2, Atomics.load(ctl, 2), 50);
|
|
41
|
-
if (Atomics.load(ctl, 4)) { Atomics.store(ctl, 4, 0);
|
|
44
|
+
if (Atomics.load(ctl, 4)) { Atomics.store(ctl, 4, 0); return false; } // (an operator the workers had let go: the caller registers it again)
|
|
45
|
+
return true;
|
|
42
46
|
},
|
|
43
47
|
};
|
|
44
48
|
return pool;
|
|
45
49
|
}
|
|
46
50
|
|
|
47
|
-
|
|
51
|
+
// On Node 20 and older the products came out wrong now and then (a few solves in sixty, more with more threads):
|
|
52
|
+
// letting go of an operator's shared memory there corrupts work still running on it, and Node 20 is past its end of
|
|
53
|
+
// life. There, this backend is plain JavaScript under another name: right, only slower. (Node 22 and later: threads)
|
|
54
|
+
const NODE = Number(process.versions.node.split('.')[0]), SHARED_OK = NODE >= 22;
|
|
55
|
+
|
|
56
|
+
export const threadsBackend = !SHARED_OK ? { ...jsBackend, name: `js (threads need Node 22; this is Node ${NODE})` } : {
|
|
48
57
|
name: `threads (${THREADS})`,
|
|
49
58
|
precision: 64,
|
|
50
59
|
operator(g, ke, cellMatrices) {
|
|
@@ -57,7 +66,9 @@ export const threadsBackend = {
|
|
|
57
66
|
if (id === null) id = workers().register({ dim, nodes: n, rows: pat.rows, cols: pat.cols, values, xs, ys });
|
|
58
67
|
};
|
|
59
68
|
return {
|
|
60
|
-
|
|
69
|
+
// (a solve's again() kept past several others finds its operator let go by the workers, who keep the last few:
|
|
70
|
+
// registered again, from the arrays this side still holds, and the product taken)
|
|
71
|
+
multiply(scale, x, y) { ready(scale); xs.set(x); if (!workers().multiply(id)) { id = workers().register({ dim, nodes: n, rows: pat.rows, cols: pat.cols, values, xs, ys }); workers().multiply(id); } y.set(ys); },
|
|
61
72
|
diagonal(scale, d) {
|
|
62
73
|
ready(scale);
|
|
63
74
|
const { rows, self } = pat;
|