@rulecad/topopt 0.2.5 → 0.2.6

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/package.json +1 -1
  2. package/topopt.mjs +13 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/topopt",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "Topology optimisation for flat and solid parts: the stiffest layout of a fixed amount of material, printable without supports if asked, as outlines or a surface ready to print.",
5
5
  "type": "module",
6
6
  "exports": {
package/topopt.mjs CHANGED
@@ -197,19 +197,19 @@ export function overhangFilter({ nx, ny, nz = 1 }, up) {
197
197
  }
198
198
  supports[e] = Int32Array.from(list);
199
199
  }
200
- const printed = new Float64Array(cells), dSum = new Float64Array(cells), dX = new Float64Array(cells), sums = new Float64Array(cells);
200
+ const printed = new Float64Array(cells), powP = new Float64Array(cells), dSum = new Float64Array(cells), dX = new Float64Array(cells), sums = new Float64Array(cells);
201
201
  return {
202
202
  apply(x, out) {
203
203
  for (const e of order) {
204
204
  const sup = supports[e];
205
- if (!sup) { printed[e] = x[e]; dX[e] = 1; continue; }
205
+ if (!sup) { printed[e] = x[e]; powP[e] = Math.pow(x[e], P); dX[e] = 1; continue; }
206
206
  let s = 0;
207
- for (const q of sup) s += Math.pow(printed[q], P);
207
+ for (const q of sup) s += powP[q]; // (each cell's printed^P, worked out once as it's printed)
208
208
  if (s < 1e-200) s = 0; // nothing worth the name below (and no denormals for the gradient to blow up on)
209
209
  const Q = P + Math.log(sup.length) / Math.log(xi0), smax = s > 0 ? Math.pow(s, 1 / Q) : 0;
210
210
  sums[e] = s;
211
211
  const d = x[e] - smax, root = Math.sqrt(d * d + eps);
212
- printed[e] = 0.5 * (x[e] + smax - root + Math.sqrt(eps));
212
+ printed[e] = 0.5 * (x[e] + smax - root + Math.sqrt(eps)); powP[e] = Math.pow(printed[e], P);
213
213
  dX[e] = 0.5 * (1 - d / root); dSum[e] = 0.5 * (1 + d / root); // d printed / d x, d printed / d smax
214
214
  }
215
215
  out.set(printed);
@@ -222,7 +222,7 @@ export function overhangFilter({ nx, ny, nz = 1 }, up) {
222
222
  const e = order[t], sup = supports[e];
223
223
  if (!sup || !sums[e]) continue;
224
224
  const Q = P + Math.log(sup.length) / Math.log(xi0), common = lambda[e] * dSum[e] * (1 / Q) * Math.pow(sums[e], 1 / Q - 1) * P;
225
- for (const q of sup) lambda[q] += common * Math.pow(printed[q], P - 1);
225
+ for (const q of sup) if (printed[q] > 0) lambda[q] += (common * powP[q]) / printed[q];
226
226
  }
227
227
  for (let e = 0; e < cells; e++) out[e] = lambda[e] * dX[e];
228
228
  return out;
@@ -542,6 +542,7 @@ export function optimise(spec, { onStep, method, backend = jsBackend, tol = 0.01
542
542
  const multipliers = lightest ? { lam: spec.loads.filter((l) => l.weight > 0).map(() => new Float64Array(m.limits)), mu: 10 } : undefined;
543
543
  const xf = new Float64Array(n), pick = (d) => Float64Array.from(freeCells, (e) => d[e]);
544
544
  let step = 0, change = 1, converged = false, beta = 1, sinceBeta = 0, peak = null;
545
+ const tried = new Float64Array(cells); // the last design the bisection analysed, and how much it filled
545
546
  while (step < spec.steps) {
546
547
  step++; sinceBeta++;
547
548
  const { compliance, dc, dv, stress } = m.evaluate(x, beta, { multipliers });
@@ -565,16 +566,20 @@ export function optimise(spec, { onStep, method, backend = jsBackend, tol = 0.01
565
566
  history.push(compliance);
566
567
  // Optimality criteria: scale each cell by √(−dc/dv/λ), within the move limit, finding λ by bisection so the
567
568
  // physical densities fill exactly the volume
568
- let lo = 0, hi = 1e9;
569
+ let lo = 0, hi = 1e9, triedFilled = NaN;
570
+ tried.fill(-1);
569
571
  while ((hi - lo) / (hi + lo) > 1e-3) { // as the 88-line code
570
572
  const mid = (lo + hi) / 2;
573
+ let same = true;
571
574
  for (let e = 0; e < cells; e++) {
572
575
  if (passive[e]) { xnew[e] = fixedValue(passive[e]); continue; }
573
576
  const b = Math.sqrt(Math.max(0, -dc[e]) / Math.max(1e-30, dv[e]) / mid);
574
577
  xnew[e] = Math.max(0, Math.max(x[e] - limit, Math.min(1, Math.min(x[e] + limit, x[e] * b))));
578
+ if (xnew[e] !== tried[e]) same = false;
575
579
  }
576
- const filled = m.physical(xnew, beta).reduce((s, v) => s + v, 0);
577
- if (filled > target) lo = mid; else hi = mid;
580
+ // (far from the answer every cell is at its move limit, the same design as the last try: not analysed again)
581
+ if (!same) { triedFilled = m.physical(xnew, beta).reduce((s, v) => s + v, 0); tried.set(xnew); }
582
+ if (triedFilled > target) lo = mid; else hi = mid;
578
583
  }
579
584
  }
580
585
  change = 0;