@rulecad/fea 0.2.7 → 0.2.8
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/fea.d.mts +2 -1
- package/fea.mjs +6 -2
- package/package.json +5 -1
- package/threads.mjs +10 -4
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.8",
|
|
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.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,7 +41,8 @@ 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;
|
|
@@ -57,7 +61,9 @@ export const threadsBackend = {
|
|
|
57
61
|
if (id === null) id = workers().register({ dim, nodes: n, rows: pat.rows, cols: pat.cols, values, xs, ys });
|
|
58
62
|
};
|
|
59
63
|
return {
|
|
60
|
-
|
|
64
|
+
// (a solve's again() kept past several others finds its operator let go by the workers, who keep the last few:
|
|
65
|
+
// registered again, from the arrays this side still holds, and the product taken)
|
|
66
|
+
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
67
|
diagonal(scale, d) {
|
|
62
68
|
ready(scale);
|
|
63
69
|
const { rows, self } = pat;
|