@rulecad/fea 0.2.2 → 0.2.3

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 (2) hide show
  1. package/fea.mjs +12 -4
  2. package/package.json +1 -1
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.3",
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": {