@rulecad/fea 0.2.0 → 0.2.1

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 CHANGED
@@ -29,6 +29,11 @@ r.deflection[0]; // { most, at }: the largest movement of solid material, and
29
29
  coarse cell's matrix is built from its children's exactly (Galerkin), so it stays accurate when cells'
30
30
  stiffnesses differ a millionfold, as they do in topology optimisation. A cantilever of 16,000 cubes takes 9
31
31
  iterations, where plain `cg` takes 371.
32
+ - **Solving again:** `solve(...)` returns `again(loads)`, which solves more right-hand sides on the same matrix,
33
+ reusing its factorisation or preconditioner: an adjoint, for a gradient.
34
+ - **Stress at a point:** `centreStress(h, nu, dim)` is the matrix from a cell's movements to the stress at its centre
35
+ (for Young's modulus 1), and `vonMises(s, dim, grad)` gives von Mises stress and, optionally, its gradient. Stress
36
+ limits in @rulecad/topopt use both.
32
37
  - **Backends:** the few operations `cg` repeats (the stiffness product, its diagonal, dot products) sit behind a small
33
38
  interface. `jsBackend`, plain JavaScript in 64-bit floats, is the reference; a WebGPU or TensorFlow.js backend
34
39
  implements the same and passes `backend-suite.mjs` in this repository.
package/fea.d.mts CHANGED
@@ -18,7 +18,8 @@ export function cellStiffness(nu: number, dim?: 2 | 3): Float64Array;
18
18
  * @param {Grid} g
19
19
  * @param {{ scale: Float64Array, ke: Float64Array, fixed: Uint8Array, loads: Float64Array[], method?: 'direct' | 'cg' | 'mg', backend?: typeof jsBackend, tol?: number, maxIter?: number, start?: Float64Array[] }} o
20
20
  * start: a first guess for the iterative solvers (the last solution, when solving again after a small change)
21
- * @returns {{ u: Float64Array[], iterations: number }}
21
+ * @returns {{ u: Float64Array[], iterations: number, again: (loads: Float64Array[]) => Float64Array[] }} again: more
22
+ * loads on the same matrix (an adjoint), reusing its factor or preconditioner
22
23
  */
23
24
  export function solve(g: Grid, { scale, ke, fixed, loads, method, backend, tol, maxIter, start }: {
24
25
  scale: Float64Array;
@@ -33,7 +34,21 @@ export function solve(g: Grid, { scale, ke, fixed, loads, method, backend, tol,
33
34
  }): {
34
35
  u: Float64Array[];
35
36
  iterations: number;
37
+ again: (loads: Float64Array[]) => Float64Array[];
36
38
  };
39
+ /**
40
+ * Stress at a cell's centre from its corners' movements, for Young's modulus 1: a matrix (rows × cell dofs, row by row;
41
+ * rows: σx, σy, τxy in 2D; σx, σy, σz, τxy, τyz, τzx in 3D). Times a cell's movements and its modulus, it's the stress.
42
+ * @param {number} h cell size (mm) @param {number} nu @param {2 | 3} dim
43
+ * @returns {Float64Array}
44
+ */
45
+ export function centreStress(h: number, nu: number, dim: 2 | 3): Float64Array;
46
+ /**
47
+ * Von Mises stress from a stress (as centreStress's rows); with grad, also its gradient with respect to the stress.
48
+ * @param {ArrayLike<number>} s @param {2 | 3} dim @param {Float64Array} [grad]
49
+ * @returns {number}
50
+ */
51
+ export function vonMises(s: ArrayLike<number>, dim: 2 | 3, grad?: Float64Array): number;
37
52
  /**
38
53
  * Each cell's von Mises stress (MPa) at its centre, from a solution u.
39
54
  * @param {Grid} g @param {Float64Array} u @param {Float64Array} modulus each cell's Young's modulus (MPa) @param {number} nu
package/fea.mjs CHANGED
@@ -205,19 +205,21 @@ function pattern(g) {
205
205
  * @param {Grid} g
206
206
  * @param {{ scale: Float64Array, ke: Float64Array, fixed: Uint8Array, loads: Float64Array[], method?: 'direct' | 'cg' | 'mg', backend?: typeof jsBackend, tol?: number, maxIter?: number, start?: Float64Array[] }} o
207
207
  * start: a first guess for the iterative solvers (the last solution, when solving again after a small change)
208
- * @returns {{ u: Float64Array[], iterations: number }}
208
+ * @returns {{ u: Float64Array[], iterations: number, again: (loads: Float64Array[]) => Float64Array[] }} again: more
209
+ * loads on the same matrix (an adjoint), reusing its factor or preconditioner
209
210
  */
210
211
  export function solve(g, { scale, ke, fixed, loads, method = 'direct', backend = jsBackend, tol = 1e-9, maxIter = 20000, start }) {
211
212
  if (scale.length !== g.cells) throw new Error(`scale has ${scale.length} values for ${g.cells} cells`);
212
213
  if (!fixed.some((f) => f)) throw new Error('nothing is held: fix at least one support, or the part just moves');
213
- const f = loads.map((l) => { const c = Float64Array.from(l); for (let i = 0; i < c.length; i++) if (fixed[i]) c[i] = 0; return c; });
214
+ 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; });
214
215
  if (method === 'direct') {
215
- const L = factor(g, { scale, ke }, fixed);
216
- return { u: f.map((b) => L.solve(b)), iterations: 0 };
216
+ const L = factor(g, { scale, ke }, fixed), again = (ls) => held(ls).map((b) => L.solve(b));
217
+ return { u: again(loads), iterations: 0, again };
217
218
  }
218
219
  const op = backend.operator(g, ke); // one for the solver and its preconditioner: the matrix is assembled once
219
220
  const precondition = method === 'mg' ? multigrid(g, { scale, ke, fixed, backend, op }) : jacobi(g, { scale, fixed, op });
220
- return conjugateGradients(g, { scale, fixed, loads: f, backend, tol, maxIter, start, precondition, op });
221
+ const again = (ls) => conjugateGradients(g, { scale, fixed, loads: held(ls), backend, tol, maxIter, precondition, op }).u;
222
+ return { ...conjugateGradients(g, { scale, fixed, loads: held(loads), backend, tol, maxIter, start, precondition, op }), again };
221
223
  }
222
224
 
223
225
  // The banded Cholesky factor of K (cells' matrices: ke × scale, or each its own), fixed dofs as identity rows
@@ -464,6 +466,37 @@ function transfer(fg, cg) {
464
466
  };
465
467
  }
466
468
 
469
+ /**
470
+ * Stress at a cell's centre from its corners' movements, for Young's modulus 1: a matrix (rows × cell dofs, row by row;
471
+ * rows: σx, σy, τxy in 2D; σx, σy, σz, τxy, τyz, τzx in 3D). Times a cell's movements and its modulus, it's the stress.
472
+ * @param {number} h cell size (mm) @param {number} nu @param {2 | 3} dim
473
+ * @returns {Float64Array}
474
+ */
475
+ export function centreStress(h, nu, dim) {
476
+ const m = CORNERS[dim].length * dim, rows = dim === 3 ? 6 : 3, B = strains(new Array(dim).fill(0), h, dim), D = elasticity(nu, dim), S = new Float64Array(rows * m);
477
+ for (let p = 0; p < rows; p++) for (let a = 0; a < m; a++) { let t = 0; for (let q = 0; q < rows; q++) t += D[p * rows + q] * B[q * m + a]; S[p * m + a] = t; }
478
+ return S;
479
+ }
480
+
481
+ /**
482
+ * Von Mises stress from a stress (as centreStress's rows); with grad, also its gradient with respect to the stress.
483
+ * @param {ArrayLike<number>} s @param {2 | 3} dim @param {Float64Array} [grad]
484
+ * @returns {number}
485
+ */
486
+ export function vonMises(s, dim, grad) {
487
+ const v = dim === 2 ? Math.sqrt(Math.max(0, s[0] * s[0] - s[0] * s[1] + s[1] * s[1] + 3 * s[2] * s[2]))
488
+ : Math.sqrt(Math.max(0, ((s[0] - s[1]) ** 2 + (s[1] - s[2]) ** 2 + (s[2] - s[0]) ** 2) / 2 + 3 * (s[3] ** 2 + s[4] ** 2 + s[5] ** 2)));
489
+ if (grad) {
490
+ const k = v > 0 ? 1 / (2 * v) : 0;
491
+ if (dim === 2) { grad[0] = k * (2 * s[0] - s[1]); grad[1] = k * (2 * s[1] - s[0]); grad[2] = k * 6 * s[2]; }
492
+ else {
493
+ grad[0] = k * (2 * s[0] - s[1] - s[2]); grad[1] = k * (2 * s[1] - s[0] - s[2]); grad[2] = k * (2 * s[2] - s[0] - s[1]);
494
+ grad[3] = k * 6 * s[3]; grad[4] = k * 6 * s[4]; grad[5] = k * 6 * s[5];
495
+ }
496
+ }
497
+ return v;
498
+ }
499
+
467
500
  /**
468
501
  * Each cell's von Mises stress (MPa) at its centre, from a solution u.
469
502
  * @param {Grid} g @param {Float64Array} u @param {Float64Array} modulus each cell's Young's modulus (MPa) @param {number} nu
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/fea",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
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": {