@rulecad/fea 0.2.4 → 0.2.7
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 +15 -0
- package/fea.d.mts +16 -2
- package/fea.mjs +75 -7
- package/package.json +1 -1
- package/threads.d.mts +10 -0
- package/threads.mjs +1 -1
package/README.md
CHANGED
|
@@ -16,6 +16,7 @@ r.u[0]; // each node's movement: x at 2n, y at 2n + 1
|
|
|
16
16
|
r.compliance[0]; // the load times how far it moved (N·mm): lower is stiffer
|
|
17
17
|
r.stress[0]; // each cell's von Mises stress (MPa)
|
|
18
18
|
r.deflection[0]; // { most, at }: the largest movement of solid material, and where
|
|
19
|
+
r.thin; // { share, cells }: how much of the part is in members under 4 cells thick (thinShare), where a cell's stiffness is rough
|
|
19
20
|
```
|
|
20
21
|
|
|
21
22
|
- **3D:** give the grid `nz` too (`grid({ nx, ny, nz, h })`) and every node has x, y and z.
|
|
@@ -46,6 +47,20 @@ r.deflection[0]; // { most, at }: the largest movement of solid material, and
|
|
|
46
47
|
Tested against beam theory: a cantilever's tip moves within 1% of `F L³ / 3 E I` in 2D, and within 2% in 3D.
|
|
47
48
|
Multigrid is tested against the direct solver with stiffnesses a millionfold apart.
|
|
48
49
|
|
|
50
|
+
**How fine a grid.** The cells are exact for a part that only stretches, and converge on beam theory with shear
|
|
51
|
+
(Timoshenko's) as they shrink. What matters is how many cells cross the depth of whatever bends:
|
|
52
|
+
|
|
53
|
+
| Cells through the depth | Too stiff by |
|
|
54
|
+
|---|---|
|
|
55
|
+
| 1 | about 32% |
|
|
56
|
+
| 2 | about 11% |
|
|
57
|
+
| 4 | about 3% |
|
|
58
|
+
| 10 | under 1% |
|
|
59
|
+
|
|
60
|
+
So give every member that bends at least four cells, and check a final shape on cells half the size: if the
|
|
61
|
+
stiffness moves by more than a few percent, the grid was too coarse. (Measured on cantilevers and a simply supported
|
|
62
|
+
beam, in 2D and 3D.)
|
|
63
|
+
|
|
49
64
|
## Sources
|
|
50
65
|
|
|
51
66
|
- Amir, Aage, Lazarov (2014). On multigrid-CG for efficient topology optimization. *Structural and Multidisciplinary
|
package/fea.d.mts
CHANGED
|
@@ -18,16 +18,18 @@ export function assemble(g: any, pat: any, cellMatrices: any, ke: any, scale: an
|
|
|
18
18
|
/**
|
|
19
19
|
* Solve K u = f for each load. K is every cell's stiffness (cellStiffness) times scale[cell]; fixed dofs don't move.
|
|
20
20
|
* @param {Grid} g
|
|
21
|
-
* @param {{ scale: Float64Array, ke: Float64Array, fixed: Uint8Array, loads: Float64Array[], method?: 'direct' | 'cg' | 'mg', backend?: typeof jsBackend, tol?: number, maxIter?: number, start?: Float64Array[] }} o
|
|
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
|
+
* springs: a stiffness added on each dof (N/mm), as a mechanism's input and output springs
|
|
22
23
|
* start: a first guess for the iterative solvers (the last solution, when solving again after a small change)
|
|
23
24
|
* @returns {{ u: Float64Array[], iterations: number, again: (loads: Float64Array[]) => Float64Array[] }} again: more
|
|
24
25
|
* loads on the same matrix (an adjoint), reusing its factor or preconditioner
|
|
25
26
|
*/
|
|
26
|
-
export function solve(g: Grid, { scale, ke, fixed, loads, method, backend, tol, maxIter, start }: {
|
|
27
|
+
export function solve(g: Grid, { scale, ke, fixed, loads, springs, method, backend, tol, maxIter, start }: {
|
|
27
28
|
scale: Float64Array;
|
|
28
29
|
ke: Float64Array;
|
|
29
30
|
fixed: Uint8Array;
|
|
30
31
|
loads: Float64Array[];
|
|
32
|
+
springs?: Float64Array;
|
|
31
33
|
method?: "direct" | "cg" | "mg";
|
|
32
34
|
backend?: typeof jsBackend;
|
|
33
35
|
tol?: number;
|
|
@@ -85,6 +87,18 @@ export function analyse({ grid: g, density, material, thickness, fixed, loads, p
|
|
|
85
87
|
at: number[];
|
|
86
88
|
}[];
|
|
87
89
|
};
|
|
90
|
+
/**
|
|
91
|
+
* How much of the part lies in members too thin for the grid to carry truthfully: the share of its solid cells (density
|
|
92
|
+
* 0.5 or more) that an opening of `k` cells takes away (eroded k times, then grown back: a member under 2k cells thick
|
|
93
|
+
* vanishes, a corner loses a little). A cell's stiffness across a member two or three cells thick is a rough
|
|
94
|
+
* figure (the READMEs' tables); four cells and up it settles. { share, cells: 2k }.
|
|
95
|
+
* @param {Grid} g @param {ArrayLike<number>} density @param {number} [k] erosions, 2 (members under 4 cells)
|
|
96
|
+
* @returns {{ share: number, cells: number }}
|
|
97
|
+
*/
|
|
98
|
+
export function thinShare(g: Grid, density: ArrayLike<number>, k?: number): {
|
|
99
|
+
share: number;
|
|
100
|
+
cells: number;
|
|
101
|
+
};
|
|
88
102
|
/**
|
|
89
103
|
* Each node's position in mm, [x, y] (or [x, y, z]), by node number.
|
|
90
104
|
* @param {Grid} g
|
package/fea.mjs
CHANGED
|
@@ -129,7 +129,12 @@ export const jsBackend = {
|
|
|
129
129
|
for (let q = 0; q < dim; q++) if (fixed[dim * i + q]) { for (let f = 0; f < dim; f++) { D[q * dim + f] = 0; D[f * dim + q] = 0; } }
|
|
130
130
|
if (dim === 2) {
|
|
131
131
|
const det = D[0] * D[3] - D[1] * D[2];
|
|
132
|
-
if (!(Math.abs(det) > 0))
|
|
132
|
+
if (!(Math.abs(det) > 0)) {
|
|
133
|
+
// One dof fixed (a roller, a symmetry edge): the block is singular, so invert the free dof on its own,
|
|
134
|
+
// as the 3D branch does. (Skipped, the node was never smoothed, and multigrid never converged)
|
|
135
|
+
for (let q = 0; q < dim; q++) if (!fixed[dim * i + q] && D[q * dim + q] > 0) inv[b + q * dim + q] = 1 / D[q * dim + q];
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
133
138
|
inv[b] = D[3] / det; inv[b + 1] = -D[1] / det; inv[b + 2] = -D[2] / det; inv[b + 3] = D[0] / det;
|
|
134
139
|
} else {
|
|
135
140
|
const c0 = D[4] * D[8] - D[5] * D[7], c1 = D[5] * D[6] - D[3] * D[8], c2 = D[3] * D[7] - D[4] * D[6], det = D[0] * c0 + D[1] * c1 + D[2] * c2;
|
|
@@ -266,20 +271,36 @@ export function assemble(g, pat, cellMatrices, ke, scale, into) {
|
|
|
266
271
|
/**
|
|
267
272
|
* Solve K u = f for each load. K is every cell's stiffness (cellStiffness) times scale[cell]; fixed dofs don't move.
|
|
268
273
|
* @param {Grid} g
|
|
269
|
-
* @param {{ scale: Float64Array, ke: Float64Array, fixed: Uint8Array, loads: Float64Array[], method?: 'direct' | 'cg' | 'mg', backend?: typeof jsBackend, tol?: number, maxIter?: number, start?: Float64Array[] }} o
|
|
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
|
+
* springs: a stiffness added on each dof (N/mm), as a mechanism's input and output springs
|
|
270
276
|
* start: a first guess for the iterative solvers (the last solution, when solving again after a small change)
|
|
271
277
|
* @returns {{ u: Float64Array[], iterations: number, again: (loads: Float64Array[]) => Float64Array[] }} again: more
|
|
272
278
|
* loads on the same matrix (an adjoint), reusing its factor or preconditioner
|
|
273
279
|
*/
|
|
274
|
-
export function solve(g, { scale, ke, fixed, loads, method = 'direct', backend = jsBackend, tol = 1e-9, maxIter = 20000, start }) {
|
|
280
|
+
export function solve(g, { scale, ke, fixed, loads, springs, method = 'direct', backend = jsBackend, tol = 1e-9, maxIter = 20000, start }) {
|
|
281
|
+
// What's wrong with what was handed in is said here, by name: further in, it comes out as a part "not held still",
|
|
282
|
+
// or as an answer that looks like one
|
|
283
|
+
if (!['direct', 'cg', 'mg'].includes(method)) throw new Error(`no solver "${method}": direct, cg or mg`);
|
|
275
284
|
if (scale.length !== g.cells) throw new Error(`scale has ${scale.length} values for ${g.cells} cells`);
|
|
285
|
+
if (fixed.length !== g.dofs) throw new Error(`fixed has ${fixed.length} values for ${g.dofs} unknowns (${g.dim} a node)`);
|
|
286
|
+
loads.forEach((l, c) => { if (l.length !== g.dofs) throw new Error(`load ${c + 1} has ${l.length} values for ${g.dofs} unknowns (${g.dim} a node)`); });
|
|
287
|
+
if (springs && springs.length !== g.dofs) throw new Error(`springs has ${springs.length} values for ${g.dofs} unknowns`);
|
|
288
|
+
for (let e = 0; e < scale.length; e++) if (!(scale[e] > 0) || !Number.isFinite(scale[e])) throw new Error(`cell ${e}'s stiffness is ${scale[e]}: every cell needs a stiffness above 0 (a density, modulus or thickness is 0, negative or not a number)`);
|
|
289
|
+
for (let i = 0; i < ke.length; i++) if (!Number.isFinite(ke[i])) throw new Error("the cell's stiffness matrix isn't numbers: Poisson's ratio must be between -1 and 0.5");
|
|
290
|
+
loads.forEach((l, c) => { for (let i = 0; i < l.length; i++) if (!Number.isFinite(l[i])) throw new Error(`load ${c + 1} has a force that isn't a number`); });
|
|
276
291
|
if (!fixed.some((f) => f)) throw new Error('nothing is held: fix at least one support, or the part just moves');
|
|
277
292
|
const held = (ls) => ls.map((l) => { const c = Float64Array.from(l); for (let i = 0; i < c.length; i++) if (fixed[i]) c[i] = 0; return c; });
|
|
278
293
|
if (method === 'direct') {
|
|
279
|
-
const L = factor(g, { scale, ke }, fixed), again = (ls) => held(ls).map((b) => L.solve(b));
|
|
294
|
+
const L = factor(g, { scale, ke, springs }, fixed), again = (ls) => held(ls).map((b) => L.solve(b));
|
|
280
295
|
return { u: again(loads), iterations: 0, again };
|
|
281
296
|
}
|
|
282
|
-
const
|
|
297
|
+
const cells = backend.operator(g, ke); // one for the solver and its preconditioner: the matrix is assembled once
|
|
298
|
+
// Springs on dofs (a mechanism's input and output): added to the product and the diagonal; the smoother, where the
|
|
299
|
+
// backend has one, doesn't know them, so Jacobi then (the preconditioner needn't be exact)
|
|
300
|
+
const op = springs ? {
|
|
301
|
+
multiply(s, x, y) { cells.multiply(s, x, y); for (let i = 0; i < y.length; i++) if (springs[i]) y[i] += springs[i] * x[i]; },
|
|
302
|
+
diagonal(s, d) { cells.diagonal(s, d); for (let i = 0; i < d.length; i++) d[i] += springs[i]; },
|
|
303
|
+
} : cells;
|
|
283
304
|
const precondition = method === 'mg' ? multigrid(g, { scale, ke, fixed, backend, op }) : jacobi(g, { scale, fixed, op });
|
|
284
305
|
const again = (ls) => conjugateGradients(g, { scale, fixed, loads: held(ls), backend, tol, maxIter, precondition, op }).u;
|
|
285
306
|
return { ...conjugateGradients(g, { scale, fixed, loads: held(loads), backend, tol, maxIter, start, precondition, op }), again };
|
|
@@ -288,7 +309,7 @@ export function solve(g, { scale, ke, fixed, loads, method = 'direct', backend =
|
|
|
288
309
|
// The banded Cholesky factor of K (cells' matrices: ke × scale, or each its own), fixed dofs as identity rows. Lenient
|
|
289
310
|
// (a preconditioner's coarsest grid, where a coarse unknown over next to nothing can come out singular to rounding), a
|
|
290
311
|
// pivot that isn't positive is dropped: an identity row with no couplings, so the unknown stays put.
|
|
291
|
-
function factor(g, { scale, ke, cellMatrices }, fixed, { lenient = false } = {}) {
|
|
312
|
+
function factor(g, { scale, ke, cellMatrices, springs }, fixed, { lenient = false } = {}) {
|
|
292
313
|
const { edof, dofs, cells, cellDofs: m } = g, mm = m * m;
|
|
293
314
|
let b = 0;
|
|
294
315
|
for (let e = 0; e < cells; e++) {
|
|
@@ -309,6 +330,7 @@ function factor(g, { scale, ke, cellMatrices }, fixed, { lenient = false } = {})
|
|
|
309
330
|
}
|
|
310
331
|
}
|
|
311
332
|
}
|
|
333
|
+
if (springs) for (let i = 0; i < dofs; i++) if (!fixed[i]) L[i * w + b] += springs[i];
|
|
312
334
|
for (let i = 0; i < dofs; i++) if (fixed[i] || L[i * w + b] === 0) L[i * w + b] = 1; // fixed, or touching no cell
|
|
313
335
|
const dropped = lenient ? new Uint8Array(dofs) : null;
|
|
314
336
|
for (let i = 0; i < dofs; i++) {
|
|
@@ -372,7 +394,8 @@ function conjugateGradients(g, { scale, fixed, loads, backend, tol, maxIter, sta
|
|
|
372
394
|
let rz = backend.dot(r, z), it = 0;
|
|
373
395
|
while (Math.sqrt(backend.dot(r, r)) > limit && it < maxIter) {
|
|
374
396
|
apply(p, q);
|
|
375
|
-
const
|
|
397
|
+
const pq = backend.dot(p, q), a = rz / pq;
|
|
398
|
+
if (!(pq > 0) || !Number.isFinite(a)) throw new Error('the part is not held still (it can slide or turn under this load): add supports, or join loose pieces to it'); // (a direction with no stiffness: the search would go on for ever, or stop on nonsense)
|
|
376
399
|
backend.axpy(a, p, x); backend.axpy(-a, q, r);
|
|
377
400
|
precondition(r, z);
|
|
378
401
|
const next = backend.dot(r, z);
|
|
@@ -619,15 +642,30 @@ export function stress(g, u, modulus, nu) {
|
|
|
619
642
|
* of any node in solid material, and where.
|
|
620
643
|
*/
|
|
621
644
|
export function analyse({ grid: g, density, material, thickness = 1, fixed, loads, penal = 1, method = g.dim === 3 ? 'mg' : 'direct', backend }) {
|
|
645
|
+
if (!(material?.E > 0)) throw new Error(`the material needs a modulus E above 0, in MPa (got ${material?.E})`);
|
|
646
|
+
if (!(material.nu > -1 && material.nu < 0.5)) throw new Error(`the material needs a Poisson's ratio nu between -1 and 0.5 (got ${material.nu}): about 0.35 for printed plastics`);
|
|
647
|
+
if (g.dim === 2 && !(thickness > 0)) throw new Error(`a flat part needs a thickness above 0 mm (got ${thickness})`);
|
|
648
|
+
if (density.length !== g.cells) throw new Error(`density has ${density.length} values for ${g.cells} cells`);
|
|
649
|
+
for (let e = 0; e < density.length; e++) if (!(density[e] >= 0 && density[e] <= 1)) throw new Error(`cell ${e}'s density is ${density[e]}: each is a share from 0 (empty) to 1 (solid)`);
|
|
622
650
|
const { E, nu } = material, Emin = E * 1e-9, ke = cellStiffness(nu, g.dim);
|
|
623
651
|
const modulus = Float64Array.from(density, (x) => Emin + Math.pow(x, penal) * (E - Emin));
|
|
624
652
|
const scale = modulus.map((mod) => mod * (g.dim === 3 ? g.h : thickness));
|
|
625
653
|
const { u } = solve(g, { scale, ke, fixed, loads, method, backend });
|
|
626
654
|
const solidNode = new Uint8Array(g.nodes), corners = CORNERS[g.dim].length;
|
|
627
655
|
for (let e = 0; e < density.length; e++) if (density[e] >= 0.5) for (let k = 0; k < corners; k++) solidNode[g.edof[e * g.cellDofs + g.dim * k] / g.dim] = 1;
|
|
656
|
+
// A part that can still slide or turn is solved all the same, by a hair's stiffness of rounding: it then "moves"
|
|
657
|
+
// many times its own size. No real part does, so that is said, not handed back as a deflection. (Most of the
|
|
658
|
+
// part, that is: a loose piece beside a held part, such as a disc lying in a mechanism, is the caller's business.)
|
|
659
|
+
const span = Math.max(g.nx, g.ny, g.nz || 1) * g.h;
|
|
660
|
+
for (const x of u) {
|
|
661
|
+
let solid = 0, adrift = 0;
|
|
662
|
+
for (let n = 0; n < g.nodes; n++) if (solidNode[n]) { solid++; for (let d = 0; d < g.dim; d++) if (!(Math.abs(x[g.dim * n + d]) < 1e4 * span)) { adrift++; break; } }
|
|
663
|
+
if (adrift > solid / 2) throw Object.assign(new Error('the part is not held still (it can slide or turn under this load): fix more of it, in each direction it could move'), { code: 'fea.unheld' });
|
|
664
|
+
}
|
|
628
665
|
const position = positions(g);
|
|
629
666
|
return {
|
|
630
667
|
u,
|
|
668
|
+
thin: thinShare(g, density),
|
|
631
669
|
compliance: u.map((x, c) => jsBackend.dot(loads[c], x)),
|
|
632
670
|
stress: u.map((x) => stress(g, x, modulus, nu)),
|
|
633
671
|
deflection: u.map((x) => {
|
|
@@ -642,6 +680,36 @@ export function analyse({ grid: g, density, material, thickness = 1, fixed, load
|
|
|
642
680
|
};
|
|
643
681
|
}
|
|
644
682
|
|
|
683
|
+
/**
|
|
684
|
+
* How much of the part lies in members too thin for the grid to carry truthfully: the share of its solid cells (density
|
|
685
|
+
* 0.5 or more) that an opening of `k` cells takes away (eroded k times, then grown back: a member under 2k cells thick
|
|
686
|
+
* vanishes, a corner loses a little). A cell's stiffness across a member two or three cells thick is a rough
|
|
687
|
+
* figure (the READMEs' tables); four cells and up it settles. { share, cells: 2k }.
|
|
688
|
+
* @param {Grid} g @param {ArrayLike<number>} density @param {number} [k] erosions, 2 (members under 4 cells)
|
|
689
|
+
* @returns {{ share: number, cells: number }}
|
|
690
|
+
*/
|
|
691
|
+
export function thinShare(g, density, k = 2) {
|
|
692
|
+
const nx = g.nx, ny = g.ny, nz = g.dim === 3 ? g.nz : 1, N = nx * ny * nz, at = (i, j, l) => (l * ny + j) * nx + i;
|
|
693
|
+
let mask = new Uint8Array(N);
|
|
694
|
+
for (let e = 0; e < N; e++) mask[e] = density[e] >= 0.5 ? 1 : 0;
|
|
695
|
+
const solid = mask.reduce((a, b) => a + b, 0);
|
|
696
|
+
if (!solid) return { share: 0, cells: 2 * k };
|
|
697
|
+
const pass = (m, keep) => { // keep: every face-neighbour solid (erode) or any (dilate); outside the grid counts as the cell itself
|
|
698
|
+
const out = new Uint8Array(N);
|
|
699
|
+
for (let l = 0; l < nz; l++) for (let j = 0; j < ny; j++) for (let i = 0; i < nx; i++) {
|
|
700
|
+
const e = at(i, j, l), n = [i > 0 ? m[at(i - 1, j, l)] : m[e], i + 1 < nx ? m[at(i + 1, j, l)] : m[e], j > 0 ? m[at(i, j - 1, l)] : m[e], j + 1 < ny ? m[at(i, j + 1, l)] : m[e], ...(nz > 1 ? [l > 0 ? m[at(i, j, l - 1)] : m[e], l + 1 < nz ? m[at(i, j, l + 1)] : m[e]] : [])];
|
|
701
|
+
out[e] = keep === 'all' ? (m[e] && n.every((x) => x) ? 1 : 0) : (m[e] || n.some((x) => x) ? 1 : 0);
|
|
702
|
+
}
|
|
703
|
+
return out;
|
|
704
|
+
};
|
|
705
|
+
let m = mask;
|
|
706
|
+
for (let i = 0; i < k; i++) m = pass(m, 'all');
|
|
707
|
+
for (let i = 0; i < k; i++) m = pass(m, 'any');
|
|
708
|
+
let lost = 0;
|
|
709
|
+
for (let e = 0; e < N; e++) if (mask[e] && !m[e]) lost++;
|
|
710
|
+
return { share: lost / solid, cells: 2 * k };
|
|
711
|
+
}
|
|
712
|
+
|
|
645
713
|
/**
|
|
646
714
|
* Each node's position in mm, [x, y] (or [x, y, z]), by node number.
|
|
647
715
|
* @param {Grid} g
|
package/package.json
CHANGED
package/threads.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export namespace threadsBackend {
|
|
2
|
+
let name: string;
|
|
3
|
+
let precision: number;
|
|
4
|
+
function operator(g: any, ke: any, cellMatrices: any): {
|
|
5
|
+
multiply(scale: any, x: any, y: any): void;
|
|
6
|
+
diagonal(scale: any, d: any): void;
|
|
7
|
+
};
|
|
8
|
+
let dot: (a: Float64Array, b: Float64Array) => number;
|
|
9
|
+
let axpy: (a: number, x: Float64Array, y: Float64Array) => void;
|
|
10
|
+
}
|
package/threads.mjs
CHANGED
|
@@ -12,7 +12,7 @@ const THREADS = Math.max(1, Math.min(8, Number(process.env.FEA_THREADS) || os.cp
|
|
|
12
12
|
// How many operators each worker keeps: an optimisation step registers one per multigrid level (four or so), and
|
|
13
13
|
// 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
14
|
// kept every step's and swapped itself to a crawl.
|
|
15
|
-
const KEPT =
|
|
15
|
+
const KEPT = 8;
|
|
16
16
|
|
|
17
17
|
// The pool: made on first use, kept for the process (unref'd, so it doesn't keep the process alive). Each operator
|
|
18
18
|
// registers its arrays with every worker under an id; a product then signals the id and waits for every worker's done.
|