@rulecad/topopt 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.
- package/README.md +2 -1
- package/package.json +1 -1
- package/topopt.d.mts +70 -5
- package/topopt.mjs +39 -8
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
|
+
"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
|
-
|
|
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:
|
|
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:
|
|
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,18 +54,44 @@ 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');
|
|
60
79
|
for (const r of [...space, ...keep, ...avoid]) if (dimension(r) !== dim) throw new Error(`a ${dimension(r)}D region in a ${dim}D problem: give every region ${dim === 3 ? 'x, y and z' : 'x and y'}`);
|
|
61
|
-
const all = [...space, ...keep].map(bounds), lo = [], n = [];
|
|
80
|
+
const all = [...space, ...keep].map(bounds), lo = [], hi = [], n = [];
|
|
62
81
|
for (let a = 0; a < dim; a++) {
|
|
63
|
-
lo.push(Math.min(...all.map((b) => b[a])));
|
|
64
|
-
n.push(Math.max(1, Math.round((
|
|
82
|
+
lo.push(Math.min(...all.map((b) => b[a]))); hi.push(Math.max(...all.map((b) => b[dim + a])));
|
|
83
|
+
n.push(Math.max(1, Math.round((hi[a] - lo[a]) / p.cell)));
|
|
65
84
|
}
|
|
66
85
|
if (dim === 3) { // whole numbers of 2, 4 or 8 cells along every side, for multigrid
|
|
67
|
-
const q = Math.min(...n) >= 16 ? 8 : Math.min(...n) >= 8 ? 4 : 2;
|
|
68
|
-
for (let a = 0; a < 3; a++)
|
|
86
|
+
const q = Math.min(...n) >= 16 ? 8 : Math.min(...n) >= 8 ? 4 : 2, build = /^([+-])([xyz])$/.exec(p.overhang ?? '');
|
|
87
|
+
for (let a = 0; a < 3; a++) {
|
|
88
|
+
n[a] = Math.ceil(n[a] / q) * q;
|
|
89
|
+
// The padding split evenly round the space, so a round space sits in the middle of its grid (all on one side, a
|
|
90
|
+
// round wall ran along the grid's edge there and drew as flat blocks). Along the build direction the bed's side
|
|
91
|
+
// stays flush, so the first layer sits on the bed.
|
|
92
|
+
const pad = n[a] * p.cell - (hi[a] - lo[a]);
|
|
93
|
+
lo[a] -= build && 'xyz'.indexOf(build[2]) === a ? (build[1] === '-' ? pad : 0) : pad / 2;
|
|
94
|
+
}
|
|
69
95
|
}
|
|
70
96
|
const size = dim === 3 ? { nx: n[0], ny: n[1], nz: n[2], h: p.cell } : { nx: n[0], ny: n[1], h: p.cell };
|
|
71
97
|
const g = makeGrid(size), origin = lo, h = p.cell;
|
|
@@ -102,7 +128,8 @@ export function discretise(p) {
|
|
|
102
128
|
for (const m of ns) for (let a = 0; a < dim; a++) f[dim * m + a] += l.force[a] / ns.length;
|
|
103
129
|
for (let a = 0; a < dim; a++) total[a] += l.force[a];
|
|
104
130
|
}
|
|
105
|
-
|
|
131
|
+
const { size, moment } = resultant(mine);
|
|
132
|
+
return { name, force: f, weight: p.cases?.[name] ?? 1, size, ...(moment ? { moment } : {}) };
|
|
106
133
|
});
|
|
107
134
|
return {
|
|
108
135
|
grid: size, origin, passive, fixed, loads,
|
|
@@ -261,7 +288,10 @@ export function model(spec, { method, backend = jsBackend } = {}) {
|
|
|
261
288
|
const stress = Ss ? stressLimits(u, active, run.again, multipliers || { lam: active.map(() => new Float64Array(cells * Ss.length)), mu: 1 }) : null;
|
|
262
289
|
const cases = spec.loads.map((l, c) => {
|
|
263
290
|
if (!u[c]) return { name: l.name, compliance: NaN, deflection: NaN, stiffness: NaN };
|
|
264
|
-
const ci = jsBackend.dot(l.force, u[c])
|
|
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;
|
|
265
295
|
return { name: l.name, compliance: ci, deflection, stiffness: deflection ? l.size / deflection : Infinity };
|
|
266
296
|
});
|
|
267
297
|
if (stress) { for (let e = 0; e < cells; e++) stress.d[e] *= passive[e] ? 0 : slope(made[e], beta); toDesign(stress.d); }
|
|
@@ -467,7 +497,8 @@ function gauss(A) {
|
|
|
467
497
|
* @param {ReturnType<typeof discretise>} spec
|
|
468
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]
|
|
469
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 }}
|
|
470
|
-
* 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)
|
|
471
502
|
*/
|
|
472
503
|
export function optimise(spec, { onStep, method, backend = jsBackend, tol = 0.01, move = 0.2 } = {}) {
|
|
473
504
|
const started = Date.now(), m = model(spec, { method, backend }), { cells, passive } = m;
|