@rulecad/fea 0.2.14 → 0.3.0

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/CHANGELOG.md CHANGED
@@ -2,6 +2,21 @@
2
2
 
3
3
  What changed in each release, for someone upgrading a workspace.
4
4
 
5
+ ## 0.3.0: the stress at a part's surface, and a warning where the grid is too coarse to read it
6
+
7
+ ### Added
8
+
9
+ - `analyse()` returns `surface`: each solid cell's von Mises stress at the part's surface, beside `stress`. `stress` is read at each cell's centre, half a cell inside the part, so where the part bends it reads the peak stress low by about one cell's share of the member's depth: 27 to 30% low at the four cells through that the README recommends, 10% at ten. `surface` comes within 2% of beam theory from four cells through, in 2D and 3D. Read a part's strength from `surface`.
10
+ - `surfaceStress(g, u, modulus, nu, density)` gives the same reading for any solution, with each node's value as well as each cell's.
11
+ - `analyse()` returns `findings`: a warning (`fea.coarse-stress`, in the kit's shape `{ level, text, code, values, at, box }`) for each load whose peak stress is in a member under eight cells through, where even the surface reading can be a few percent out.
12
+ - The README's grid-fineness table now says how far each stress reading is out at each number of cells through the depth.
13
+
14
+ ### Changed
15
+
16
+ - The package's tests compare a cantilever's tip with beam theory including shear (Timoshenko's), and no longer check a field (`converged`) that `solve` never returned.
17
+
18
+ Upgrading: nothing changes for existing results. `stress`, `u`, `compliance` and `deflection` keep their values; `surface` and `findings` are new. A check that judges strength from `stress` reads low: move it to `surface` (the kit's own checks move in the next release). No printed size changes.
19
+
5
20
  ## 0.2.14: motor reactions, racking and joins in frame sizing
6
21
  ### Added
7
22
 
package/README.md CHANGED
@@ -18,7 +18,9 @@ load[2 * g.node(100, 5) + 1] = -10; // 10 N down at the right en
18
18
  const r = analyse({ grid: g, density: new Float64Array(1000).fill(1), material: { E: 2680, nu: 0.36 }, thickness: 5, fixed, loads: [load] });
19
19
  r.u[0]; // each node's movement: x at 2n, y at 2n + 1
20
20
  r.compliance[0]; // the load times how far it moved (N·mm): lower is stiffer
21
- r.stress[0]; // each cell's von Mises stress (MPa)
21
+ r.stress[0]; // each cell's von Mises stress (MPa) at its centre: reads bending stress low (see How fine a grid)
22
+ r.surface[0]; // each solid cell's von Mises stress (MPa) at the part's surface: use this for strength
23
+ r.findings; // [{ level, text, code, values, at, box }]: a warning where the peak stress is in a member under 8 cells through
22
24
  r.deflection[0]; // { most, at }: the largest movement of solid material, and where
23
25
  r.thin; // { share, cells }: how much of the part is in members under 4 cells thick (thinShare), where a cell's stiffness is rough
24
26
  ```
@@ -36,6 +38,17 @@ r.thin; // { share, cells }: how much of the part is in members under
36
38
  iterations, where plain `cg` takes 371.
37
39
  - **Solving again:** `solve(...)` returns `again(loads)`, which solves more right-hand sides on the same matrix,
38
40
  reusing its factorisation or preconditioner: an adjoint, for a gradient.
41
+ - **Stress at the surface:** `stress` is read at each cell's centre, half a cell inside the part. In bending the
42
+ stress peaks at the surface, so in a member n cells through the centre stress reads low by about one cell's share:
43
+ 27 to 30% low at 4 cells, 10% at 10. `surface` (and `surfaceStress(g, u, modulus, nu, density)`, which also gives
44
+ each node's value) reads each cell at its corners and averages each stress component over the solid cells round
45
+ each node. At a corner it takes two thirds of what the cell's strains say and one third of the stress that
46
+ reproduces the forces on the cell's corners: square cells bend a little too stiffly, so their strains alone read
47
+ low (6% at 4 cells in 3D), and the second reading errs the other way by about twice as much. A cell under half
48
+ density keeps its centre stress. `analyse` adds a warning to `findings` (code `fea.coarse-stress`) for each load
49
+ whose peak surface stress is in a member under 8 cells through, counted along the directions the stress changes
50
+ fastest there (the shortest of them). At a sharp inside corner, or a curved edge stepped into cells, the peak keeps rising as cells shrink:
51
+ the stress concentrates there beyond what any grid settles on, so read it with care (a real part has a fillet).
39
52
  - **Stress at a point:** `centreStress(h, nu, dim)` is the matrix from a cell's movements to the stress at its centre
40
53
  (for Young's modulus 1), and `vonMises(s, dim, grad)` gives von Mises stress and, optionally, its gradient. Stress
41
54
  limits in @rulecad/topopt use both.
@@ -51,22 +64,28 @@ r.thin; // { share, cells }: how much of the part is in members under
51
64
  memory (a few solves in sixty, more with more threads, none on Node 22 or 24), so there it is plain JavaScript
52
65
  under another name (its `name` says so).
53
66
 
54
- Tested against beam theory: a cantilever's tip moves within 1% of `F L³ / 3 E I` in 2D, and within 2% in 3D.
67
+ Tested against beam theory with shear (Timoshenko's, `F L³ / 3 E I + F L / κ G A`): a cantilever's tip moves within
68
+ 1% of it in 2D at 10 cells through, and within 3% in 3D at 8, never more. The surface stress of a cantilever 4, 8 or
69
+ 10 cells deep is tested within 5% of `M c / I` at mid-span and a beam's depth from the support, in 2D and 3D (it
70
+ comes within 2%); right at a clamped edge the stress concentrates, and beam theory doesn't hold.
55
71
  Multigrid is tested against the direct solver with stiffnesses a millionfold apart.
56
72
 
57
73
  **How fine a grid.** The cells are exact for a part that only stretches, and converge on beam theory with shear
58
74
  (Timoshenko's) as they shrink. What matters is how many cells cross the depth of whatever bends:
59
75
 
60
- | Cells through the depth | Too stiff by |
61
- |---|---|
62
- | 1 | about 32% |
63
- | 2 | about 11% |
64
- | 4 | about 3% |
65
- | 10 | under 1% |
66
-
67
- So give every member that bends at least four cells, and check a final shape on cells half the size: if the
68
- stiffness moves by more than a few percent, the grid was too coarse. (Measured on cantilevers and a simply supported
69
- beam, in 2D and 3D.)
76
+ | Cells through the depth | Too stiff by | `stress` (cell centres) reads low by | `surface` is out by |
77
+ |---|---|---|---|
78
+ | 1 | about 32% | about 94% | about 24% low |
79
+ | 2 | about 11% | about 55% | about 5% low |
80
+ | 4 | about 3% (4% in 3D) | about 27% (29% in 3D) | under 1% |
81
+ | 8 | about 1% | about 13% | about 1% high |
82
+ | 10 | under 1% | about 10% | about 1% high |
83
+ | 20 | under 0.5% | about 5% | under 1% high |
84
+
85
+ So give every member that bends at least four cells, read its strength from `surface`, not `stress`, and check a
86
+ final shape on cells half the size: if the stiffness or the peak stress moves by more than a few percent, the grid
87
+ was too coarse. (Stiffness measured on cantilevers and a simply supported beam, in 2D and 3D; stress on a cantilever
88
+ 100 × 10 mm at mid-span, against `M c / I`, in 2D, and in 3D where they differ.)
70
89
 
71
90
  ## Frames of beams
72
91
 
package/fea.d.mts CHANGED
@@ -60,14 +60,38 @@ export function vonMises(s: ArrayLike<number>, dim: 2 | 3, grad?: Float64Array):
60
60
  * @returns {Float64Array}
61
61
  */
62
62
  export function stress(g: Grid, u: Float64Array, modulus: Float64Array, nu: number): Float64Array;
63
+ /**
64
+ * The stress at the part's surface, where bending puts its peak. `stress` reads each cell at its centre, half a cell
65
+ * inside the surface: in a member n cells through it reads the bending stress low by about one cell's share (27% at 4
66
+ * cells, 10% at 10). This reads each cell's stress at its corners instead, averages each stress component over the
67
+ * solid cells (density 0.5 or more) round each node, and gives von Mises there (patch recovery). Averaging over the
68
+ * cells round a node cancels the false shear a square cell shows at its corners when it bends. At a corner the stress
69
+ * is two thirds the cell's strains read there, and one third the stress field that reproduces the forces on its corners
70
+ * (BALANCED_SHARE): the strains alone read low where the cells bend too stiffly. A 4-cell-deep cantilever's surface
71
+ * stress is within 2% of M c / I, in 2D and 3D, away from the clamped end. At a sharp inside corner, or a curved edge stepped into cells, the
72
+ * peak keeps rising as the cells shrink: the stress there is concentrated beyond what any grid settles on. A node
73
+ * with no solid cell round it is averaged over the cells it has.
74
+ * @param {Grid} g @param {Float64Array} u @param {Float64Array} modulus each cell's Young's modulus (MPa) @param {number} nu
75
+ * @param {ArrayLike<number>} [density] each cell's density, 0 to 1 (all solid if left out)
76
+ * @returns {{ nodes: Float64Array, cells: Float64Array }} nodes: von Mises (MPa) at each node; cells: for a solid cell,
77
+ * the largest of its corners' (its surface stress), and for a cell under 0.5, its centre stress (as `stress`)
78
+ */
79
+ export function surfaceStress(g: Grid, u: Float64Array, modulus: Float64Array, nu: number, density?: ArrayLike<number>): {
80
+ nodes: Float64Array;
81
+ cells: Float64Array;
82
+ };
63
83
  /**
64
84
  * Analyse a part: each cell's density (0 empty to 1 solid; Young's modulus scales with density^penal, never below
65
85
  * E × 1e-9 so empty cells don't leave the matrix singular), supports and loads. In 2D the part is a plate of the given
66
86
  * thickness; in 3D thickness is not used.
67
87
  * @param {{ grid: Grid, density: Float64Array, material: Material, thickness?: number, fixed: Uint8Array, loads: Float64Array[], penal?: number, method?: 'direct' | 'cg' | 'mg', backend?: typeof jsBackend }} o
68
- * @returns {{ u: Float64Array[], compliance: number[], stress: Float64Array[], deflection: { most: number, at: number[] }[] }}
69
- * compliance: f · u per load (N·mm): the work the load does; lower is stiffer. deflection: the largest movement
70
- * of any node in solid material, and where.
88
+ * @returns {{ u: Float64Array[], thin: { share: number, cells: number }, compliance: number[], stress: Float64Array[], surface: Float64Array[], findings: { level: 'warn', text: string, code: string, values: { surface: number, centre: number, cells: number, need: number, load: number }, at: number[], box: { lo: number[], hi: number[] } }[], deflection: { most: number, at: number[] }[] }}
89
+ * compliance: f · u per load (N·mm): the work the load does; lower is stiffer. stress: each cell's von Mises stress
90
+ * (MPa) at its centre, which reads bending stress low by about one cell's share of the member's depth (27% at 4
91
+ * cells through, 10% at 10). surface: each solid cell's von Mises stress at its surface, the largest of its corners'
92
+ * (surfaceStress): within 2% of beam theory at 4 cells through; use it for strength. findings: a warning
93
+ * (fea.coarse-stress) for each load whose peak surface stress is in a member under 8 cells through. deflection: the
94
+ * largest movement of any node in solid material, and where.
71
95
  */
72
96
  export function analyse({ grid: g, density, material, thickness, fixed, loads, penal, method, backend }: {
73
97
  grid: Grid;
@@ -81,8 +105,30 @@ export function analyse({ grid: g, density, material, thickness, fixed, loads, p
81
105
  backend?: typeof jsBackend;
82
106
  }): {
83
107
  u: Float64Array[];
108
+ thin: {
109
+ share: number;
110
+ cells: number;
111
+ };
84
112
  compliance: number[];
85
113
  stress: Float64Array[];
114
+ surface: Float64Array[];
115
+ findings: {
116
+ level: "warn";
117
+ text: string;
118
+ code: string;
119
+ values: {
120
+ surface: number;
121
+ centre: number;
122
+ cells: number;
123
+ need: number;
124
+ load: number;
125
+ };
126
+ at: number[];
127
+ box: {
128
+ lo: number[];
129
+ hi: number[];
130
+ };
131
+ }[];
86
132
  deflection: {
87
133
  most: number;
88
134
  at: number[];
package/fea.mjs CHANGED
@@ -636,14 +636,191 @@ export function stress(g, u, modulus, nu) {
636
636
  return out;
637
637
  }
638
638
 
639
+ // The stress field a cell carries, as few terms as it has ways to deform (5 in 2D, 18 in 3D): each term a stress
640
+ // component times a product of the natural coordinates (Pian and Sumihara 1984 in 2D, Pian and Tong 1986 in 3D).
641
+ // Each term: [component, power of ξ, power of η, power of ζ].
642
+ const STRESS_TERMS = {
643
+ 2: [[0, 0, 0], [1, 0, 0], [2, 0, 0], [0, 0, 1], [1, 1, 0]].map(([c, a, b]) => [c, a, b, 0]),
644
+ 3: [[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0], [1, 0, 0, 1], [1, 1, 0, 0], [1, 1, 0, 1],
645
+ [2, 0, 0, 0], [2, 1, 0, 0], [2, 0, 1, 0], [2, 1, 1, 0], [3, 0, 0, 0], [3, 0, 0, 1], [4, 0, 0, 0], [4, 1, 0, 0], [5, 0, 0, 0], [5, 0, 1, 0]],
646
+ };
647
+ const termsAt = (p, dim) => STRESS_TERMS[dim].map(([c, ...pow]) => [c, pow.slice(0, dim).reduce((f, k, a) => f * (k ? p[a] : 1), 1)]);
648
+
649
+ // For each corner of a cell of side 1 and modulus 1, the matrix (rows × cell dofs) from its movements to the stress at
650
+ // that corner, read from the forces the cell puts on its corners (K u) rather than from its strains: the stress field
651
+ // (in STRESS_TERMS) whose work over the cell's strains gives exactly those forces. The cells' forces balance the load,
652
+ // so across a section this field carries the whole bending moment, even where square cells bend too stiffly and their
653
+ // strains read low; but it puts too much of the moment at each cell's edges, so alone it reads high (by about 9% at 4
654
+ // cells through, 4% at 10). See surfaceStress.
655
+ const balanced = new Map();
656
+ function balancedCorners(nu, dim) {
657
+ const key = `${dim} ${nu}`;
658
+ if (balanced.has(key)) return balanced.get(key);
659
+ const m = CORNERS[dim].length * dim, n = STRESS_TERMS[dim].length, ke = cellStiffness(nu, dim), gp = 1 / Math.sqrt(3), w = 1 / (1 << dim);
660
+ const points = dim === 3 ? [-gp, gp].flatMap((a) => [-gp, gp].flatMap((b) => [-gp, gp].map((c) => [a, b, c]))) : [-gp, gp].flatMap((a) => [-gp, gp].map((b) => [a, b]));
661
+ const G = new Float64Array(n * m); // ∫ Pᵀ B over the cell: term t's work over dof a's strain
662
+ for (const p of points) {
663
+ const B = strains(p, 1, dim);
664
+ termsAt(p, dim).forEach(([c, v], t) => { for (let a = 0; a < m; a++) G[t * m + a] += w * v * B[c * m + a]; });
665
+ }
666
+ // R = (G Gᵀ)⁻¹ G ke: the terms whose forces Gᵀ β are the cell's K u (exactly: K u has no rigid part, and G's rows span the rest)
667
+ const A = new Float64Array(n * n), GK = new Float64Array(n * m);
668
+ for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) { let t = 0; for (let a = 0; a < m; a++) t += G[i * m + a] * G[j * m + a]; A[i * n + j] = t; }
669
+ for (let i = 0; i < n; i++) for (let b = 0; b < m; b++) { let t = 0; for (let a = 0; a < m; a++) t += G[i * m + a] * ke[a * m + b]; GK[i * m + b] = t; }
670
+ const R = solveDense(A, GK, n, m);
671
+ const out = CORNERS[dim].map((c) => {
672
+ const S = new Float64Array((dim === 3 ? 6 : 3) * m);
673
+ termsAt(c.map((x) => 2 * x - 1), dim).forEach(([comp, v], t) => { for (let a = 0; a < m; a++) S[comp * m + a] += v * R[t * m + a]; });
674
+ return S;
675
+ });
676
+ balanced.set(key, out);
677
+ return out;
678
+ }
679
+ // A X = B for a small dense A (n × n, symmetric positive definite) and B (n × m), by Gaussian elimination
680
+ function solveDense(A, B, n, m) {
681
+ const a = Float64Array.from(A), x = Float64Array.from(B);
682
+ for (let k = 0; k < n; k++) {
683
+ let piv = k;
684
+ for (let i = k + 1; i < n; i++) if (Math.abs(a[i * n + k]) > Math.abs(a[piv * n + k])) piv = i;
685
+ if (piv !== k) { for (let j = 0; j < n; j++) [a[k * n + j], a[piv * n + j]] = [a[piv * n + j], a[k * n + j]]; for (let j = 0; j < m; j++) [x[k * m + j], x[piv * m + j]] = [x[piv * m + j], x[k * m + j]]; }
686
+ for (let i = k + 1; i < n; i++) {
687
+ const f = a[i * n + k] / a[k * n + k];
688
+ if (!f) continue;
689
+ for (let j = k; j < n; j++) a[i * n + j] -= f * a[k * n + j];
690
+ for (let j = 0; j < m; j++) x[i * m + j] -= f * x[k * m + j];
691
+ }
692
+ }
693
+ for (let k = n - 1; k >= 0; k--) {
694
+ for (let j = 0; j < m; j++) { let t = x[k * m + j]; for (let i = k + 1; i < n; i++) t -= a[k * n + i] * x[i * m + j]; x[k * m + j] = t / a[k * n + k]; }
695
+ }
696
+ return x;
697
+ }
698
+
699
+ // How much of the force-balanced reading (balancedCorners) goes into the surface stress; the rest is the strain
700
+ // reading. Square cells bend too stiffly (3% at 4 cells through in 2D, 4% in 3D), so the strains read low (4% and 6%
701
+ // at 4 cells), and the force-balanced field reads high by about twice that: a third of one and two thirds of the other
702
+ // cancel the two errors. Measured on cantilevers and a beam under pure moment, 2D and 3D, Poisson's ratio 0 to 0.45, 4
703
+ // to 20 cells through: within 2% of beam theory at mid-span and a beam's depth from the support (test/surface-
704
+ // stress.test.mjs; right at a clamped edge the stress is concentrated, and reads higher as cells shrink). Both
705
+ // readings are exact for a cell in uniform stress, so the blend is too.
706
+ const BALANCED_SHARE = 1 / 3;
707
+
708
+ /**
709
+ * The stress at the part's surface, where bending puts its peak. `stress` reads each cell at its centre, half a cell
710
+ * inside the surface: in a member n cells through it reads the bending stress low by about one cell's share (27% at 4
711
+ * cells, 10% at 10). This reads each cell's stress at its corners instead, averages each stress component over the
712
+ * solid cells (density 0.5 or more) round each node, and gives von Mises there (patch recovery). Averaging over the
713
+ * cells round a node cancels the false shear a square cell shows at its corners when it bends. At a corner the stress
714
+ * is two thirds the cell's strains read there, and one third the stress field that reproduces the forces on its corners
715
+ * (BALANCED_SHARE): the strains alone read low where the cells bend too stiffly. A 4-cell-deep cantilever's surface
716
+ * stress is within 2% of M c / I, in 2D and 3D, away from the clamped end. At a sharp inside corner, or a curved edge stepped into cells, the
717
+ * peak keeps rising as the cells shrink: the stress there is concentrated beyond what any grid settles on. A node
718
+ * with no solid cell round it is averaged over the cells it has.
719
+ * @param {Grid} g @param {Float64Array} u @param {Float64Array} modulus each cell's Young's modulus (MPa) @param {number} nu
720
+ * @param {ArrayLike<number>} [density] each cell's density, 0 to 1 (all solid if left out)
721
+ * @returns {{ nodes: Float64Array, cells: Float64Array }} nodes: von Mises (MPa) at each node; cells: for a solid cell,
722
+ * the largest of its corners' (its surface stress), and for a cell under 0.5, its centre stress (as `stress`)
723
+ */
724
+ export function surfaceStress(g, u, modulus, nu, density) { return recover(g, u, modulus, nu, density, null); }
725
+ // surfaceStress, given the centre stress where it is already worked out (analyse)
726
+ function recover(g, u, modulus, nu, density, centres) {
727
+ const dim = g.dim, m = g.cellDofs, rows = dim === 3 ? 6 : 3, corners = CORNERS[dim], C = corners.length, D = elasticity(nu, dim);
728
+ // Each corner's matrix from the cell's movements to its stress there (modulus 1): D B at the corner, blended with the
729
+ // force-balanced field there (which scales as 1 / h, as B does)
730
+ const W = BALANCED_SHARE, bal = balancedCorners(nu, dim);
731
+ const S = corners.map((c, q) => {
732
+ const B = strains(c.map((x) => 2 * x - 1), g.h, dim), out = new Float64Array(rows * m);
733
+ for (let p = 0; p < rows; p++) for (let a = 0; a < m; a++) { let t = 0; for (let r = 0; r < rows; r++) t += D[p * rows + r] * B[r * m + a]; out[p * m + a] = (1 - W) * t + W * bal[q][p * m + a] / g.h; }
734
+ return out;
735
+ });
736
+ const solid = (e) => !density || density[e] >= 0.5;
737
+ const sumSolid = new Float64Array(g.nodes * rows), nSolid = new Uint8Array(g.nodes), sumAll = new Float64Array(g.nodes * rows), nAll = new Uint8Array(g.nodes);
738
+ const ue = new Float64Array(m);
739
+ for (let e = 0; e < g.cells; e++) {
740
+ const eo = e * m, inside = solid(e);
741
+ for (let a = 0; a < m; a++) ue[a] = u[g.edof[eo + a]];
742
+ for (let q = 0; q < C; q++) {
743
+ const n = g.edof[eo + q * dim] / dim, Sq = S[q];
744
+ for (let p = 0; p < rows; p++) {
745
+ let t = 0;
746
+ for (let a = 0, o = p * m; a < m; a++) t += Sq[o + a] * ue[a];
747
+ t *= modulus[e];
748
+ sumAll[n * rows + p] += t;
749
+ if (inside) sumSolid[n * rows + p] += t;
750
+ }
751
+ nAll[n]++;
752
+ if (inside) nSolid[n]++;
753
+ }
754
+ }
755
+ const nodes = new Float64Array(g.nodes), s = new Float64Array(rows);
756
+ for (let n = 0; n < g.nodes; n++) {
757
+ const [sum, k] = nSolid[n] ? [sumSolid, nSolid[n]] : [sumAll, nAll[n]];
758
+ if (!k) continue;
759
+ for (let p = 0; p < rows; p++) s[p] = sum[n * rows + p] / k;
760
+ nodes[n] = vonMises(s, dim);
761
+ }
762
+ const centre = density ? centres || stress(g, u, modulus, nu) : null, cells = new Float64Array(g.cells);
763
+ for (let e = 0; e < g.cells; e++) {
764
+ if (!solid(e)) { cells[e] = centre[e]; continue; }
765
+ let most = 0;
766
+ for (let q = 0; q < C; q++) most = Math.max(most, nodes[g.edof[e * m + q * dim] / dim]);
767
+ cells[e] = most;
768
+ }
769
+ return { nodes, cells };
770
+ }
771
+
772
+ // Members under this many cells through, at the peak stress, are said: the grid reads stress there roughly
773
+ const STRESS_CELLS = 8;
774
+
775
+ // Where a load's peak surface stress is in a member under STRESS_CELLS cells through, a finding saying so. The member's
776
+ // thickness is counted in solid cells, from the peak cell to the first cell under 0.5 or the grid's edge each way, along
777
+ // the axes the stress changes fastest across the peak cell (through the depth, for a beam that bends): the shortest.
778
+ function coarsePeak(g, density, surface, centre, load, loads) {
779
+ const dim = g.dim, m = g.cellDofs, corners = CORNERS[dim], nz = dim === 3 ? g.nz : 1;
780
+ let peak = -1;
781
+ for (let e = 0; e < g.cells; e++) if (density[e] >= 0.5 && (peak < 0 || surface.cells[e] > surface.cells[peak])) peak = e;
782
+ if (peak < 0 || !(surface.cells[peak] > 0)) return null;
783
+ const at = [peak % g.nx, Math.floor(peak / g.nx) % g.ny, dim === 3 ? Math.floor(peak / (g.nx * g.ny)) : 0], sides = [g.nx, g.ny, nz];
784
+ const change = [...Array(dim)].map((_, a) => { // the corners' stress on the cell's far face less its near face, along each axis
785
+ let d = 0;
786
+ corners.forEach((c, q) => { d += (c[a] ? 1 : -1) * surface.nodes[g.edof[peak * m + q * dim] / dim]; });
787
+ return Math.abs(d);
788
+ });
789
+ const steepest = Math.max(...change), cell = (p) => (p[2] * g.ny + p[1]) * g.nx + p[0];
790
+ const run = (axis) => {
791
+ let n = 1;
792
+ for (const step of [-1, 1]) for (let p = at.slice(); ;) { p[axis] += step; if (p[axis] < 0 || p[axis] >= sides[axis] || !(density[cell(p)] >= 0.5)) break; n++; }
793
+ return n;
794
+ };
795
+ // At a clamped end the stress also changes fast along the member (the held face reads low), so the steepest axis
796
+ // alone can be the length: count each axis the stress changes across by at least half as much, and take the shortest
797
+ let through = Infinity;
798
+ for (let a = 0; a < dim; a++) if (change[a] >= steepest / 2) through = Math.min(through, run(a));
799
+ if (through >= STRESS_CELLS) return null;
800
+ const lo = at.slice(0, dim).map((i) => i * g.h), hi = lo.map((x) => x + g.h), r = (x) => Math.round(x * 1000) / 1000;
801
+ const most = surface.cells[peak], low = Math.max(0, Math.round((1 - centre[peak] / most) * 100));
802
+ return {
803
+ level: 'warn',
804
+ code: 'fea.coarse-stress',
805
+ text: `the highest stress${loads > 1 ? ` under load ${load + 1}` : ''}, ${r(most)} MPa at the part's surface, is in a member only ${through} cell${through === 1 ? '' : 's'} through, under the ${STRESS_CELLS} it takes to read stress closely: there the centre stress reads ${low}% lower, and even the surface stress can be some percent out. Check it on cells half the size, or with a member ${STRESS_CELLS} cells through`,
806
+ values: { surface: r(most), centre: r(centre[peak]), cells: through, need: STRESS_CELLS, load },
807
+ at: lo.map((x) => r(x + g.h / 2)),
808
+ box: { lo: lo.map(r), hi: hi.map(r) },
809
+ };
810
+ }
811
+
639
812
  /**
640
813
  * Analyse a part: each cell's density (0 empty to 1 solid; Young's modulus scales with density^penal, never below
641
814
  * E × 1e-9 so empty cells don't leave the matrix singular), supports and loads. In 2D the part is a plate of the given
642
815
  * thickness; in 3D thickness is not used.
643
816
  * @param {{ grid: Grid, density: Float64Array, material: Material, thickness?: number, fixed: Uint8Array, loads: Float64Array[], penal?: number, method?: 'direct' | 'cg' | 'mg', backend?: typeof jsBackend }} o
644
- * @returns {{ u: Float64Array[], compliance: number[], stress: Float64Array[], deflection: { most: number, at: number[] }[] }}
645
- * compliance: f · u per load (N·mm): the work the load does; lower is stiffer. deflection: the largest movement
646
- * of any node in solid material, and where.
817
+ * @returns {{ u: Float64Array[], thin: { share: number, cells: number }, compliance: number[], stress: Float64Array[], surface: Float64Array[], findings: { level: 'warn', text: string, code: string, values: { surface: number, centre: number, cells: number, need: number, load: number }, at: number[], box: { lo: number[], hi: number[] } }[], deflection: { most: number, at: number[] }[] }}
818
+ * compliance: f · u per load (N·mm): the work the load does; lower is stiffer. stress: each cell's von Mises stress
819
+ * (MPa) at its centre, which reads bending stress low by about one cell's share of the member's depth (27% at 4
820
+ * cells through, 10% at 10). surface: each solid cell's von Mises stress at its surface, the largest of its corners'
821
+ * (surfaceStress): within 2% of beam theory at 4 cells through; use it for strength. findings: a warning
822
+ * (fea.coarse-stress) for each load whose peak surface stress is in a member under 8 cells through. deflection: the
823
+ * largest movement of any node in solid material, and where.
647
824
  */
648
825
  export function analyse({ grid: g, density, material, thickness = 1, fixed, loads, penal = 1, method = g.dim === 3 ? 'mg' : 'direct', backend }) {
649
826
  if (!(material?.E > 0)) throw new Error(`the material needs a modulus E above 0, in MPa (got ${material?.E})`);
@@ -666,12 +843,14 @@ export function analyse({ grid: g, density, material, thickness = 1, fixed, load
666
843
  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; } }
667
844
  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' });
668
845
  }
669
- const position = positions(g);
846
+ const position = positions(g), centre = u.map((x) => stress(g, x, modulus, nu)), surface = u.map((x, c) => recover(g, x, modulus, nu, density, centre[c]));
670
847
  return {
671
848
  u,
672
849
  thin: thinShare(g, density),
673
850
  compliance: u.map((x, c) => jsBackend.dot(loads[c], x)),
674
- stress: u.map((x) => stress(g, x, modulus, nu)),
851
+ stress: centre,
852
+ surface: surface.map((s) => s.cells),
853
+ findings: surface.map((s, c) => coarsePeak(g, density, s, centre[c], c, loads.length)).filter(Boolean),
675
854
  deflection: u.map((x) => {
676
855
  let most = 0, at = position[0];
677
856
  for (let n = 0; n < g.nodes; n++) if (solidNode[n]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/fea",
3
- "version": "0.2.14",
3
+ "version": "0.3.0",
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, and frames of beams (static, buckling and modes), in plain JavaScript.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -22,17 +22,15 @@
22
22
  }
23
23
  },
24
24
  "files": [
25
- "fea.mjs",
26
- "fea.d.mts",
25
+ "*.mjs",
26
+ "*.d.mts",
27
27
  "README.md",
28
28
  "LICENSE",
29
- "threads.mjs",
30
- "threads.d.mts",
31
- "frame.mjs",
32
- "frame.d.mts",
33
- "layout.mjs",
34
- "layout.d.mts",
35
- "CHANGELOG.md"
29
+ "CHANGELOG.md",
30
+ "!backend-suite.mjs",
31
+ "!frame-cases.mjs",
32
+ "!layout-cases.mjs",
33
+ "!test.mjs"
36
34
  ],
37
35
  "license": "MIT",
38
36
  "keywords": [