@rulecad/topopt 0.2.3 → 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.
package/README.md CHANGED
@@ -18,7 +18,8 @@ const spec = discretise({
18
18
  material: { E: 2680, nu: 0.36 }, thickness: 6, cell: 1, volume: 0.5, filter: 2.5,
19
19
  });
20
20
  const result = optimise(spec, { onStep: (s) => console.log(s.step, s.compliance) });
21
- result.cases; // [{ name: 'down', deflection (mm along the load), stiffness (N/mm) }]
21
+ result.cases; // [{ name: 'down', deflection (mm along the load), stiffness (N/mm) }]; a twist (forces adding
22
+ // to nothing): { name, moment (N·mm), angle (radians), torsion (N·mm a radian) }
22
23
  const loops = outlines(spec.grid, result.density, spec.origin); // closed loops, solid on the left
23
24
  ```
24
25
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/topopt",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
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.d.mts CHANGED
@@ -31,7 +31,55 @@
31
31
  * @param {Problem} p
32
32
  * @returns {object} a spec for optimise(), with origin (the grid's lowest corner, mm) and regions
33
33
  */
34
- export function discretise(p: Problem): object;
34
+ /**
35
+ * What a case's forces add up to: their total's size (N), and if that's nothing, the moment they turn the part with
36
+ * (N·mm, about where they act, weighted by size): a twist.
37
+ * @param {{ at: number[], force: number[] }[]} loads
38
+ * @returns {{ size: number, moment: number }}
39
+ */
40
+ export function resultant(loads: {
41
+ at: number[];
42
+ force: number[];
43
+ }[]): {
44
+ size: number;
45
+ moment: number;
46
+ };
47
+ export function discretise(p: any): {
48
+ regions: {
49
+ keep: any;
50
+ avoid: any;
51
+ };
52
+ stress?: any;
53
+ overhang?: any;
54
+ grid: {
55
+ nx: number;
56
+ ny: number;
57
+ nz: number;
58
+ h: any;
59
+ } | {
60
+ nx: number;
61
+ ny: number;
62
+ h: any;
63
+ nz?: undefined;
64
+ };
65
+ origin: number[];
66
+ passive: Int8Array<ArrayBuffer>;
67
+ fixed: Uint8Array<ArrayBuffer>;
68
+ loads: {
69
+ moment?: number;
70
+ name: any;
71
+ force: Float64Array<ArrayBuffer>;
72
+ weight: any;
73
+ size: number;
74
+ }[];
75
+ material: any;
76
+ thickness: any;
77
+ volume: any;
78
+ filter: any;
79
+ penal: any;
80
+ projection: any;
81
+ steps: any;
82
+ };
35
83
  /**
36
84
  * The density filter: each cell's filtered density is the weighted average of the cells within radius (weights
37
85
  * falling linearly to 0 at the radius). apply filters densities; adjoint carries sensitivities back through it.
@@ -86,11 +134,27 @@ export function model(spec: ReturnType<typeof discretise>, { method, backend }?:
86
134
  compliance: number;
87
135
  dc: Float64Array<ArrayBuffer>;
88
136
  dv: Float64Array<ArrayBuffer>;
89
- u: any;
90
- cases: any;
137
+ u: any[];
138
+ cases: ({
139
+ name: any;
140
+ compliance: number;
141
+ deflection: number;
142
+ stiffness: number;
143
+ moment?: undefined;
144
+ angle?: undefined;
145
+ torsion?: undefined;
146
+ } | {
147
+ name: any;
148
+ compliance: number;
149
+ moment: number;
150
+ angle: number;
151
+ torsion: number;
152
+ deflection?: undefined;
153
+ stiffness?: undefined;
154
+ })[];
91
155
  };
92
156
  cells: number;
93
- passive: any;
157
+ passive: Int8Array<ArrayBuffer>;
94
158
  limits: number;
95
159
  };
96
160
  /**
@@ -112,7 +176,8 @@ export function mma({ n, m, xmin, xmax, move }: {
112
176
  * @param {ReturnType<typeof discretise>} spec
113
177
  * @param {{ onStep?: (s: { step: number, steps: number, compliance: number, change: number, beta: number, density: Float64Array }) => void, method?: 'direct' | 'cg' | 'mg', backend?: typeof jsBackend, tol?: number, move?: number }} [options]
114
178
  * @returns {{ density: Float64Array, compliance: number, cases: { name: string, compliance: number, deflection: number, stiffness: number }[], volume: number, steps: number, converged: boolean, history: number[], seconds: number }}
115
- * each case's deflection is along its load (mm, compliance over the load's size), stiffness the load over that (N/mm)
179
+ * each case's deflection is along its load (mm, compliance over the load's size), stiffness the load over that (N/mm);
180
+ * a twist (forces adding to nothing) has instead its moment (N·mm), the angle it turns (radians) and torsion (N·mm a radian)
116
181
  */
117
182
  export function optimise(spec: ReturnType<typeof discretise>, { onStep, method, backend, tol, move }?: {
118
183
  onStep?: (s: {
package/topopt.mjs CHANGED
@@ -54,6 +54,25 @@ export { jsBackend };
54
54
  * @param {Problem} p
55
55
  * @returns {object} a spec for optimise(), with origin (the grid's lowest corner, mm) and regions
56
56
  */
57
+ /**
58
+ * What a case's forces add up to: their total's size (N), and if that's nothing, the moment they turn the part with
59
+ * (N·mm, about where they act, weighted by size): a twist.
60
+ * @param {{ at: number[], force: number[] }[]} loads
61
+ * @returns {{ size: number, moment: number }}
62
+ */
63
+ export function resultant(loads) {
64
+ const w = loads.map((l) => Math.hypot(...l.force)), all = w.reduce((t, x) => t + x, 0) || 1, dim = loads[0]?.force.length ?? 3;
65
+ const p3 = (v) => [v[0], v[1], dim === 3 ? v[2] : 0], c = [0, 1, 2].map((a) => loads.reduce((t, l, i) => t + (p3(l.at)[a]) * w[i], 0) / all);
66
+ const total = [0, 0, 0], turn = [0, 0, 0];
67
+ for (const l of loads) {
68
+ const F = p3(l.force), at = p3(l.at), r = [at[0] - c[0], at[1] - c[1], at[2] - c[2]];
69
+ for (let a = 0; a < 3; a++) total[a] += F[a];
70
+ turn[0] += r[1] * F[2] - r[2] * F[1]; turn[1] += r[2] * F[0] - r[0] * F[2]; turn[2] += r[0] * F[1] - r[1] * F[0];
71
+ }
72
+ const size = Math.hypot(...total);
73
+ return { size, moment: size < 1e-9 * all ? Math.hypot(...turn) : 0 };
74
+ }
75
+
57
76
  export function discretise(p) {
58
77
  const space = [p.space].flat(), keep = p.keep || [], avoid = p.avoid || [], dim = dimension(space[0]);
59
78
  if (!(p.cell > 0)) throw new Error('the cell size must be more than 0 mm');
@@ -109,7 +128,8 @@ export function discretise(p) {
109
128
  for (const m of ns) for (let a = 0; a < dim; a++) f[dim * m + a] += l.force[a] / ns.length;
110
129
  for (let a = 0; a < dim; a++) total[a] += l.force[a];
111
130
  }
112
- return { name, force: f, weight: p.cases?.[name] ?? 1, size: Math.hypot(...total) };
131
+ const { size, moment } = resultant(mine);
132
+ return { name, force: f, weight: p.cases?.[name] ?? 1, size, ...(moment ? { moment } : {}) };
113
133
  });
114
134
  return {
115
135
  grid: size, origin, passive, fixed, loads,
@@ -268,7 +288,10 @@ export function model(spec, { method, backend = jsBackend } = {}) {
268
288
  const stress = Ss ? stressLimits(u, active, run.again, multipliers || { lam: active.map(() => new Float64Array(cells * Ss.length)), mu: 1 }) : null;
269
289
  const cases = spec.loads.map((l, c) => {
270
290
  if (!u[c]) return { name: l.name, compliance: NaN, deflection: NaN, stiffness: NaN };
271
- const ci = jsBackend.dot(l.force, u[c]), deflection = l.size ? ci / l.size : 0;
291
+ const ci = jsBackend.dot(l.force, u[c]);
292
+ // A twist turns rather than moves: the work it does is its moment times the angle it turns through
293
+ if (l.moment) { const angle = ci / l.moment; return { name: l.name, compliance: ci, moment: l.moment, angle, torsion: l.moment / angle }; }
294
+ const deflection = l.size ? ci / l.size : 0;
272
295
  return { name: l.name, compliance: ci, deflection, stiffness: deflection ? l.size / deflection : Infinity };
273
296
  });
274
297
  if (stress) { for (let e = 0; e < cells; e++) stress.d[e] *= passive[e] ? 0 : slope(made[e], beta); toDesign(stress.d); }
@@ -474,7 +497,8 @@ function gauss(A) {
474
497
  * @param {ReturnType<typeof discretise>} spec
475
498
  * @param {{ onStep?: (s: { step: number, steps: number, compliance: number, change: number, beta: number, density: Float64Array }) => void, method?: 'direct' | 'cg' | 'mg', backend?: typeof jsBackend, tol?: number, move?: number }} [options]
476
499
  * @returns {{ density: Float64Array, compliance: number, cases: { name: string, compliance: number, deflection: number, stiffness: number }[], volume: number, steps: number, converged: boolean, history: number[], seconds: number }}
477
- * each case's deflection is along its load (mm, compliance over the load's size), stiffness the load over that (N/mm)
500
+ * each case's deflection is along its load (mm, compliance over the load's size), stiffness the load over that (N/mm);
501
+ * a twist (forces adding to nothing) has instead its moment (N·mm), the angle it turns (radians) and torsion (N·mm a radian)
478
502
  */
479
503
  export function optimise(spec, { onStep, method, backend = jsBackend, tol = 0.01, move = 0.2 } = {}) {
480
504
  const started = Date.now(), m = model(spec, { method, backend }), { cells, passive } = m;