@rulecad/fea 0.2.2 → 0.2.4

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.
Files changed (3) hide show
  1. package/fea.mjs +12 -4
  2. package/package.json +1 -1
  3. package/threads.mjs +10 -3
package/fea.mjs CHANGED
@@ -285,8 +285,10 @@ export function solve(g, { scale, ke, fixed, loads, method = 'direct', backend =
285
285
  return { ...conjugateGradients(g, { scale, fixed, loads: held(loads), backend, tol, maxIter, start, precondition, op }), again };
286
286
  }
287
287
 
288
- // The banded Cholesky factor of K (cells' matrices: ke × scale, or each its own), fixed dofs as identity rows
289
- function factor(g, { scale, ke, cellMatrices }, fixed) {
288
+ // The banded Cholesky factor of K (cells' matrices: ke × scale, or each its own), fixed dofs as identity rows. Lenient
289
+ // (a preconditioner's coarsest grid, where a coarse unknown over next to nothing can come out singular to rounding), a
290
+ // 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 } = {}) {
290
292
  const { edof, dofs, cells, cellDofs: m } = g, mm = m * m;
291
293
  let b = 0;
292
294
  for (let e = 0; e < cells; e++) {
@@ -308,16 +310,21 @@ function factor(g, { scale, ke, cellMatrices }, fixed) {
308
310
  }
309
311
  }
310
312
  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
+ const dropped = lenient ? new Uint8Array(dofs) : null;
311
314
  for (let i = 0; i < dofs; i++) {
312
315
  const from = Math.max(0, i - b), ri = i * w + b - i; // L[i][k] is at ri + k
313
316
  for (let j = from; j <= i; j++) {
317
+ if (dropped && dropped[j]) { L[ri + j] = 0; continue; }
314
318
  const rj = j * w + b - j;
315
319
  let s = L[ri + j], s1 = 0, s2 = 0, s3 = 0, k = from;
316
320
  for (; k + 3 < j; k += 4) { s -= L[ri + k] * L[rj + k]; s1 += L[ri + k + 1] * L[rj + k + 1]; s2 += L[ri + k + 2] * L[rj + k + 2]; s3 += L[ri + k + 3] * L[rj + k + 3]; } // four running sums: a little faster
317
321
  for (; k < j; k++) s -= L[ri + k] * L[rj + k];
318
322
  s -= s1 + s2 + s3;
319
323
  if (j === i) {
320
- if (!(s > 0)) throw new Error('the part is not held still: add supports, or join loose pieces to it');
324
+ if (!(s > 0)) {
325
+ if (!dropped) throw new Error('the part is not held still: add supports, or join loose pieces to it');
326
+ dropped[i] = 1; s = 1; for (let k = from; k < i; k++) L[ri + k] = 0;
327
+ }
321
328
  L[i * w + b] = Math.sqrt(s);
322
329
  } else L[ri + j] = s / L[j * w + b];
323
330
  }
@@ -335,6 +342,7 @@ function factor(g, { scale, ke, cellMatrices }, fixed) {
335
342
  for (let k = i + 1; k <= Math.min(dofs - 1, i + b); k++) s -= L[k * w + b - k + i] * x[k];
336
343
  x[i] = s / L[i * w + b];
337
344
  }
345
+ if (dropped) for (let i = 0; i < dofs; i++) if (dropped[i]) x[i] = 0;
338
346
  return x;
339
347
  },
340
348
  };
@@ -419,7 +427,7 @@ function multigrid(g, { scale, ke, fixed, backend, op }) {
419
427
  l.op.diagonal(l.scale, l.inv);
420
428
  for (let i = 0; i < l.inv.length; i++) l.inv[i] = l.fixed[i] || !(l.inv[i] > 0) ? 0 : DAMPING / l.inv[i];
421
429
  }
422
- const last = levels.at(-1), coarse = factor(last.g, { scale: last.scale, ke, cellMatrices: last.mats }, last.fixed);
430
+ const last = levels.at(-1), coarse = factor(last.g, { scale: last.scale, ke, cellMatrices: last.mats }, last.fixed, { lenient: levels.length > 1 });
423
431
  const apply = (l, x, y) => { l.op.multiply(l.scale, x, y); for (let i = 0; i < y.length; i++) if (l.fixed[i]) y[i] = 0; };
424
432
 
425
433
  const cycle = (k, r) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/fea",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
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": {
package/threads.mjs CHANGED
@@ -9,14 +9,19 @@ import { Worker, isMainThread, parentPort, workerData } from 'node:worker_thread
9
9
  import { jsBackend, pattern, assemble } from './fea.mjs';
10
10
 
11
11
  const THREADS = Math.max(1, Math.min(8, Number(process.env.FEA_THREADS) || os.cpus().length - 1));
12
+ // How many operators each worker keeps: an optimisation step registers one per multigrid level (four or so), and
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
+ // kept every step's and swapped itself to a crawl.
15
+ const KEPT = 16;
12
16
 
13
17
  // The pool: made on first use, kept for the process (unref'd, so it doesn't keep the process alive). Each operator
14
18
  // registers its arrays with every worker under an id; a product then signals the id and waits for every worker's done.
15
19
  let pool = null;
16
20
  function workers() {
17
21
  if (pool) return pool;
18
- // ctl: [0] the product generation, [1] the operator id, [2] workers done, [3] registrations acknowledged
19
- const ctl = new Int32Array(new SharedArrayBuffer(16)), list = [];
22
+ // ctl: [0] the product generation, [1] the operator id, [2] workers done, [3] registrations acknowledged, [4] a
23
+ // product asked of an operator a worker no longer holds
24
+ const ctl = new Int32Array(new SharedArrayBuffer(20)), list = [];
20
25
  for (let w = 0; w < THREADS; w++) { const worker = new Worker(new URL(import.meta.url), { workerData: { ctl, index: w, count: THREADS } }); worker.unref(); list.push(worker); }
21
26
  let nextId = 1;
22
27
  pool = {
@@ -33,6 +38,7 @@ function workers() {
33
38
  Atomics.store(ctl, 2, 0); Atomics.store(ctl, 1, id);
34
39
  Atomics.add(ctl, 0, 1); Atomics.notify(ctl, 0);
35
40
  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); throw new Error(`the threads backend was asked for a product on an operator its workers had let go (${KEPT} are kept): a solve reused too long after others`); }
36
42
  },
37
43
  };
38
44
  return pool;
@@ -65,7 +71,7 @@ export const threadsBackend = {
65
71
 
66
72
  if (!isMainThread && workerData?.ctl) {
67
73
  const { ctl, index, count } = workerData, ops = new Map();
68
- parentPort.on('message', (m) => { ops.set(m.id, m); Atomics.add(ctl, 3, 1); Atomics.notify(ctl, 3); });
74
+ parentPort.on('message', (m) => { ops.set(m.id, m); while (ops.size > KEPT) ops.delete(ops.keys().next().value); Atomics.add(ctl, 3, 1); Atomics.notify(ctl, 3); });
69
75
  // Products: wait for the generation to change, sweep this worker's share of the nodes, say so
70
76
  let seen = 0;
71
77
  const run = () => {
@@ -75,6 +81,7 @@ if (!isMainThread && workerData?.ctl) {
75
81
  seen = Atomics.load(ctl, 0);
76
82
  const id = Atomics.load(ctl, 1), op = ops.get(id);
77
83
  if (!id) { Atomics.add(ctl, 2, 1); Atomics.notify(ctl, 2); return setImmediate(run); } // woken to take a message
84
+ if (id && !op) Atomics.store(ctl, 4, 1);
78
85
  if (op) {
79
86
  const { dim, nodes, rows, cols, values: v, xs: x, ys: y } = op, from = Math.floor((nodes * index) / count), to = Math.floor((nodes * (index + 1)) / count);
80
87
  if (dim === 3) {