@yakovlev-i-a/dsh-openfoam-workbench 0.0.0-stage → 0.9.26
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/CHANGELOG.md +683 -0
- package/CORE.md +50 -0
- package/LICENSE +202 -0
- package/NOTICE +29 -0
- package/README.md +373 -2
- package/client.js +48 -0
- package/cordis.patch.yml +18 -0
- package/icon.png +0 -0
- package/lib/buildlib.js +42 -0
- package/lib/caselib.js +212 -0
- package/lib/core.js +47 -0
- package/lib/discover.js +139 -0
- package/lib/escalations.js +92 -0
- package/lib/explib.js +149 -0
- package/lib/impact.js +55 -0
- package/lib/index.js +109 -0
- package/lib/knowledge.js +108 -0
- package/lib/meshlib.js +375 -0
- package/lib/needs-input.js +32 -0
- package/lib/of-case.js +485 -0
- package/lib/of-engine.js +1061 -0
- package/lib/paperlib.js +200 -0
- package/lib/presentation.js +365 -0
- package/lib/registry.js +142 -0
- package/lib/routing-guard.js +126 -0
- package/lib/runtime.js +580 -0
- package/lib/scaffold-cht.js +318 -0
- package/lib/scaffold-compressible.js +171 -0
- package/lib/scaffold.js +355 -0
- package/lib/settings-schema.js +86 -0
- package/lib/skills.js +53 -0
- package/lib/tools/build.js +90 -0
- package/lib/tools/case.js +1082 -0
- package/lib/tools/env.js +592 -0
- package/lib/tools/experience.js +165 -0
- package/lib/tools/mesh.js +408 -0
- package/lib/tools/meta.js +95 -0
- package/lib/tools/papers.js +105 -0
- package/lib/tools/render.js +94 -0
- package/lib/tools/run.js +331 -0
- package/lib/versions.js +133 -0
- package/licenses/dsh-openfoam-doctor-MIT.txt +21 -0
- package/locale/en.json +6 -0
- package/locale/ru.json +6 -0
- package/package.json +44 -4
- package/skills/openfoam-analyze/SKILL.md +27 -0
- package/skills/openfoam-case-builder/SKILL.md +61 -0
- package/skills/openfoam-cht/SKILL.md +83 -0
- package/skills/openfoam-code-modify/SKILL.md +32 -0
- package/skills/openfoam-mesh-independence/SKILL.md +55 -0
- package/skills/openfoam-meshing/SKILL.md +30 -0
- package/skills/openfoam-paper-reproduction/SKILL.md +36 -0
- package/skills/openfoam-self-evolution/SKILL.md +21 -0
- package/skills/openfoam-solver-development/SKILL.md +61 -0
- package/skills/openfoam-stats/SKILL.md +46 -0
- package/skills/openfoam-viz/SKILL.md +35 -0
package/lib/meshlib.js
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
2
|
+
import { basename } from 'node:path'
|
|
3
|
+
import { resolveCase } from './runtime.js'
|
|
4
|
+
|
|
5
|
+
export const vsub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]]
|
|
6
|
+
|
|
7
|
+
export const vcross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export function computeFirstLayer({ U_ref, L_ref, nu = 1.5e-5, y_plus_target = 1, expansion_ratio = 1.2, coverage = 0.7 }) {
|
|
11
|
+
const U = Number(U_ref)
|
|
12
|
+
const L = Number(L_ref)
|
|
13
|
+
if (!(U > 0) || !(L > 0)) return { ok: false, error: 'U_ref and L_ref must be positive' }
|
|
14
|
+
const nuV = Number(nu) > 0 ? Number(nu) : 1.5e-5
|
|
15
|
+
const yp = Number(y_plus_target) > 0 ? Number(y_plus_target) : 1
|
|
16
|
+
const r = Number(expansion_ratio) > 1 ? Number(expansion_ratio) : 1.2
|
|
17
|
+
const cov = Math.min(Math.max(Number(coverage) || 0.7, 0.1), 1)
|
|
18
|
+
const Re = (U * L) / nuV
|
|
19
|
+
const Cf = 0.026 * Math.pow(Re, -1 / 7)
|
|
20
|
+
const uTau = U * Math.sqrt(Cf / 2)
|
|
21
|
+
const firstLayer = (yp * nuV) / uTau
|
|
22
|
+
const delta = 0.37 * L * Math.pow(Re, -0.2)
|
|
23
|
+
const total = cov * delta
|
|
24
|
+
const n = Math.max(1, Math.ceil(Math.log(1 + (total * (r - 1)) / firstLayer) / Math.log(r)))
|
|
25
|
+
const totalThickness = (firstLayer * (Math.pow(r, n) - 1)) / (r - 1)
|
|
26
|
+
return { ok: true, Re, Cf, uTau, delta, first_layer_thickness: firstLayer, n_layers: n, expansion_ratio: r, total_thickness: totalThickness }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function parseStlTris(file) {
|
|
30
|
+
const buf = readFileSync(file)
|
|
31
|
+
let isBinary = false
|
|
32
|
+
let count = 0
|
|
33
|
+
if (buf.length >= 84) {
|
|
34
|
+
const c = buf.readUInt32LE(80)
|
|
35
|
+
if (c > 0 && buf.length === 84 + c * 50) { isBinary = true; count = c }
|
|
36
|
+
}
|
|
37
|
+
const tris = []
|
|
38
|
+
if (isBinary) {
|
|
39
|
+
for (let i = 0; i < count; i++) {
|
|
40
|
+
const off = 84 + i * 50
|
|
41
|
+
const t = []
|
|
42
|
+
for (let k = 0; k < 3; k++) {
|
|
43
|
+
const o = off + 12 + k * 12
|
|
44
|
+
t.push([buf.readFloatLE(o), buf.readFloatLE(o + 4), buf.readFloatLE(o + 8)])
|
|
45
|
+
}
|
|
46
|
+
tris.push(t)
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
const text = buf.toString('utf8')
|
|
50
|
+
const vre = /vertex\s+([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)\s+([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)\s+([-+]?\d*\.?\d+(?:[eE][-+]?\d+)?)/g
|
|
51
|
+
let m
|
|
52
|
+
const vs = []
|
|
53
|
+
while ((m = vre.exec(text))) vs.push([+m[1], +m[2], +m[3]])
|
|
54
|
+
for (let i = 0; i + 2 < vs.length; i += 3) tris.push([vs[i], vs[i + 1], vs[i + 2]])
|
|
55
|
+
}
|
|
56
|
+
return { isBinary, tris }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function inspectStl(file) {
|
|
60
|
+
const { isBinary, tris } = parseStlTris(file)
|
|
61
|
+
if (!tris.length) return { ok: false, error: 'no triangles parsed (not an STL?)' }
|
|
62
|
+
const mn = [Infinity, Infinity, Infinity]
|
|
63
|
+
const mx = [-Infinity, -Infinity, -Infinity]
|
|
64
|
+
let area = 0
|
|
65
|
+
let vol = 0
|
|
66
|
+
let nv = 0
|
|
67
|
+
const ctr = [0, 0, 0]
|
|
68
|
+
for (const t of tris) {
|
|
69
|
+
for (const v of t) {
|
|
70
|
+
for (let k = 0; k < 3; k++) {
|
|
71
|
+
if (v[k] < mn[k]) mn[k] = v[k]
|
|
72
|
+
if (v[k] > mx[k]) mx[k] = v[k]
|
|
73
|
+
ctr[k] += v[k]
|
|
74
|
+
}
|
|
75
|
+
nv++
|
|
76
|
+
}
|
|
77
|
+
const cr = vcross(vsub(t[1], t[0]), vsub(t[2], t[0]))
|
|
78
|
+
area += 0.5 * Math.hypot(cr[0], cr[1], cr[2])
|
|
79
|
+
vol += (t[0][0] * (t[1][1] * t[2][2] - t[1][2] * t[2][1]) - t[0][1] * (t[1][0] * t[2][2] - t[1][2] * t[2][0]) + t[0][2] * (t[1][0] * t[2][1] - t[1][1] * t[2][0])) / 6
|
|
80
|
+
}
|
|
81
|
+
for (let k = 0; k < 3; k++) ctr[k] /= nv
|
|
82
|
+
let watertight = null
|
|
83
|
+
let openEdges = null
|
|
84
|
+
if (tris.length <= 200000) {
|
|
85
|
+
const edge = new Map()
|
|
86
|
+
const key = (a, b) => {
|
|
87
|
+
const k1 = a.map((x) => Math.round(x * 1e6)).join(',')
|
|
88
|
+
const k2 = b.map((x) => Math.round(x * 1e6)).join(',')
|
|
89
|
+
return k1 < k2 ? k1 + '|' + k2 : k2 + '|' + k1
|
|
90
|
+
}
|
|
91
|
+
for (const t of tris) {
|
|
92
|
+
for (let i = 0; i < 3; i++) {
|
|
93
|
+
const k = key(t[i], t[(i + 1) % 3])
|
|
94
|
+
edge.set(k, (edge.get(k) || 0) + 1)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
openEdges = 0
|
|
98
|
+
for (const c of edge.values()) if (c !== 2) openEdges++
|
|
99
|
+
watertight = openEdges === 0
|
|
100
|
+
}
|
|
101
|
+
return { ok: true, format: isBinary ? 'binary' : 'ascii', triangles: tris.length, bbox: { min: mn, max: mx }, centroid: ctr, volume: Math.abs(vol), area, watertight, open_edges: openEdges }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export const dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
|
|
105
|
+
|
|
106
|
+
export function closestPtTriangle(p, a, b, c) {
|
|
107
|
+
const ab = vsub(b, a), ac = vsub(c, a), ap = vsub(p, a)
|
|
108
|
+
const d1 = dot3(ab, ap), d2 = dot3(ac, ap)
|
|
109
|
+
if (d1 <= 0 && d2 <= 0) return a
|
|
110
|
+
const bp = vsub(p, b), d3 = dot3(ab, bp), d4 = dot3(ac, bp)
|
|
111
|
+
if (d3 >= 0 && d4 <= d3) return b
|
|
112
|
+
const vc = d1 * d4 - d3 * d2
|
|
113
|
+
if (vc <= 0 && d1 >= 0 && d3 <= 0) { const v = d1 / (d1 - d3); return [a[0] + v * ab[0], a[1] + v * ab[1], a[2] + v * ab[2]] }
|
|
114
|
+
const cp = vsub(p, c), d5 = dot3(ab, cp), d6 = dot3(ac, cp)
|
|
115
|
+
if (d6 >= 0 && d5 <= d6) return c
|
|
116
|
+
const vb = d5 * d2 - d1 * d6
|
|
117
|
+
if (vb <= 0 && d2 >= 0 && d6 <= 0) { const w = d2 / (d2 - d6); return [a[0] + w * ac[0], a[1] + w * ac[1], a[2] + w * ac[2]] }
|
|
118
|
+
const va = d3 * d6 - d5 * d4
|
|
119
|
+
if (va <= 0 && (d4 - d3) >= 0 && (d5 - d6) >= 0) { const w = (d4 - d3) / ((d4 - d3) + (d5 - d6)); return [b[0] + w * (c[0] - b[0]), b[1] + w * (c[1] - b[1]), b[2] + w * (c[2] - b[2])] }
|
|
120
|
+
const denom = 1 / (va + vb + vc)
|
|
121
|
+
const v = vb * denom, w = vc * denom
|
|
122
|
+
return [a[0] + ab[0] * v + ac[0] * w, a[1] + ab[1] * v + ac[1] * w, a[2] + ab[2] * v + ac[2] * w]
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Symmetric max over sampled triangle centroids of the nearest-distance to the other mesh.
|
|
126
|
+
|
|
127
|
+
export function hausdorffApprox(fromTris, toTris, maxSamples = 2000) {
|
|
128
|
+
if (!fromTris.length || !toTris.length) return null
|
|
129
|
+
const step = Math.max(1, Math.ceil(fromTris.length / maxSamples))
|
|
130
|
+
let worst = 0
|
|
131
|
+
for (let i = 0; i < fromTris.length; i += step) {
|
|
132
|
+
const t = fromTris[i]
|
|
133
|
+
const p = [(t[0][0] + t[1][0] + t[2][0]) / 3, (t[0][1] + t[1][1] + t[2][1]) / 3, (t[0][2] + t[1][2] + t[2][2]) / 3]
|
|
134
|
+
let best = Infinity
|
|
135
|
+
for (const u of toTris) {
|
|
136
|
+
const q = closestPtTriangle(p, u[0], u[1], u[2])
|
|
137
|
+
const dd = Math.hypot(p[0] - q[0], p[1] - q[1], p[2] - q[2])
|
|
138
|
+
if (dd < best) best = dd
|
|
139
|
+
}
|
|
140
|
+
if (best > worst) worst = best
|
|
141
|
+
}
|
|
142
|
+
return worst
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function stlBoxTriangles(mn, mx) {
|
|
146
|
+
const [x0, y0, z0] = mn
|
|
147
|
+
const [x1, y1, z1] = mx
|
|
148
|
+
const c = [(x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2]
|
|
149
|
+
const quads = [
|
|
150
|
+
[[x0, y0, z0], [x1, y0, z0], [x1, y1, z0], [x0, y1, z0]],
|
|
151
|
+
[[x0, y0, z1], [x0, y1, z1], [x1, y1, z1], [x1, y0, z1]],
|
|
152
|
+
[[x0, y0, z0], [x0, y0, z1], [x1, y0, z1], [x1, y0, z0]],
|
|
153
|
+
[[x0, y1, z0], [x1, y1, z0], [x1, y1, z1], [x0, y1, z1]],
|
|
154
|
+
[[x0, y0, z0], [x0, y1, z0], [x0, y1, z1], [x0, y0, z1]],
|
|
155
|
+
[[x1, y0, z0], [x1, y0, z1], [x1, y1, z1], [x1, y1, z0]],
|
|
156
|
+
]
|
|
157
|
+
const tris = []
|
|
158
|
+
for (const q of quads) {
|
|
159
|
+
const n = vcross(vsub(q[1], q[0]), vsub(q[2], q[0]))
|
|
160
|
+
const fc = q.reduce((a, v) => [a[0] + v[0] / 4, a[1] + v[1] / 4, a[2] + v[2] / 4], [0, 0, 0])
|
|
161
|
+
const out = [fc[0] - c[0], fc[1] - c[1], fc[2] - c[2]]
|
|
162
|
+
if (n[0] * out[0] + n[1] * out[1] + n[2] * out[2] < 0) q.reverse()
|
|
163
|
+
tris.push([q[0], q[1], q[2]])
|
|
164
|
+
tris.push([q[0], q[2], q[3]])
|
|
165
|
+
}
|
|
166
|
+
return tris
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function stlToAscii(name, tris) {
|
|
170
|
+
const lines = ['solid ' + name]
|
|
171
|
+
for (const t of tris) {
|
|
172
|
+
const n = vcross(vsub(t[1], t[0]), vsub(t[2], t[0]))
|
|
173
|
+
const len = Math.hypot(n[0], n[1], n[2]) || 1
|
|
174
|
+
lines.push(` facet normal ${n[0] / len} ${n[1] / len} ${n[2] / len}`, ' outer loop')
|
|
175
|
+
for (const v of t) lines.push(` vertex ${v[0]} ${v[1]} ${v[2]}`)
|
|
176
|
+
lines.push(' endloop', ' endfacet')
|
|
177
|
+
}
|
|
178
|
+
lines.push('endsolid ' + name)
|
|
179
|
+
return lines.join('\n') + '\n'
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// --- primitive STL generators ---
|
|
183
|
+
|
|
184
|
+
export function ring(r, z, sections) {
|
|
185
|
+
return Array.from({ length: sections }, (_, j) => {
|
|
186
|
+
const a = (j / sections) * 2 * Math.PI
|
|
187
|
+
return [r * Math.cos(a), r * Math.sin(a), z]
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function primBox(lx, ly, lz) {
|
|
192
|
+
return stlBoxTriangles([-lx / 2, -ly / 2, -lz / 2], [lx / 2, ly / 2, lz / 2])
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function primSphere(r, segments = 32, rings = 16) {
|
|
196
|
+
const tris = []
|
|
197
|
+
const vert = (i, j) => {
|
|
198
|
+
const th = (i / rings) * Math.PI
|
|
199
|
+
const ph = (j / segments) * 2 * Math.PI
|
|
200
|
+
return [r * Math.sin(th) * Math.cos(ph), r * Math.sin(th) * Math.sin(ph), r * Math.cos(th)]
|
|
201
|
+
}
|
|
202
|
+
for (let i = 0; i < rings; i++) {
|
|
203
|
+
for (let j = 0; j < segments; j++) {
|
|
204
|
+
const a = vert(i, j)
|
|
205
|
+
const b = vert(i, j + 1)
|
|
206
|
+
const c = vert(i + 1, j + 1)
|
|
207
|
+
const d = vert(i + 1, j)
|
|
208
|
+
tris.push([a, b, c], [a, c, d])
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return tris.filter((t) => area2(t) > 1e-18)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export const area2 = (t) => {
|
|
215
|
+
const cr = vcross(vsub(t[1], t[0]), vsub(t[2], t[0]))
|
|
216
|
+
return Math.hypot(cr[0], cr[1], cr[2])
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function primCylinder(r, h, sections = 32, rInner = 0) {
|
|
220
|
+
const tris = []
|
|
221
|
+
const z0 = -h / 2
|
|
222
|
+
const z1 = h / 2
|
|
223
|
+
const o0 = ring(r, z0, sections)
|
|
224
|
+
const o1 = ring(r, z1, sections)
|
|
225
|
+
for (let j = 0; j < sections; j++) {
|
|
226
|
+
const j2 = (j + 1) % sections
|
|
227
|
+
tris.push([o0[j], o1[j], o1[j2]], [o0[j], o1[j2], o0[j2]])
|
|
228
|
+
}
|
|
229
|
+
if (rInner > 0) {
|
|
230
|
+
const i0 = ring(rInner, z0, sections)
|
|
231
|
+
const i1 = ring(rInner, z1, sections)
|
|
232
|
+
for (let j = 0; j < sections; j++) {
|
|
233
|
+
const j2 = (j + 1) % sections
|
|
234
|
+
tris.push([i0[j], i1[j2], i1[j]], [i0[j], i0[j2], i1[j2]])
|
|
235
|
+
tris.push([o0[j], i0[j], i0[j2]], [o0[j], i0[j2], o0[j2]])
|
|
236
|
+
tris.push([o1[j], o1[j2], i1[j2]], [o1[j], i1[j2], i1[j]])
|
|
237
|
+
}
|
|
238
|
+
} else {
|
|
239
|
+
const c0 = [0, 0, z0]
|
|
240
|
+
const c1 = [0, 0, z1]
|
|
241
|
+
for (let j = 0; j < sections; j++) {
|
|
242
|
+
const j2 = (j + 1) % sections
|
|
243
|
+
tris.push([c0, o0[j2], o0[j]], [c1, o1[j], o1[j2]])
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return tris
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export function primCone(r, h, sections = 32) {
|
|
250
|
+
const tris = []
|
|
251
|
+
const z0 = -h / 2
|
|
252
|
+
const base = ring(r, z0, sections)
|
|
253
|
+
const apex = [0, 0, h / 2]
|
|
254
|
+
for (let j = 0; j < sections; j++) {
|
|
255
|
+
const j2 = (j + 1) % sections
|
|
256
|
+
tris.push([base[j], base[j2], apex], [[0, 0, z0], base[j2], base[j]])
|
|
257
|
+
}
|
|
258
|
+
return tris
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function makePrimitive(shape, p = {}) {
|
|
262
|
+
const s = String(shape)
|
|
263
|
+
if (s === 'box') return primBox(Number(p.lx), Number(p.ly), Number(p.lz))
|
|
264
|
+
if (s === 'sphere') return primSphere(Number(p.r), Number(p.segments) || 32, Number(p.rings) || 16)
|
|
265
|
+
if (s === 'cylinder') return primCylinder(Number(p.r), Number(p.h), Number(p.sections) || 32)
|
|
266
|
+
if (s === 'pipe') return primCylinder(Number(p.r_outer), Number(p.h), Number(p.sections) || 32, Number(p.r_inner))
|
|
267
|
+
if (s === 'cone') return primCone(Number(p.r), Number(p.h), Number(p.sections) || 32)
|
|
268
|
+
throw new Error('unsupported shape: ' + s)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function transformTris(tris, translate = [0, 0, 0], rotateDeg = [0, 0, 0], scale = 1) {
|
|
272
|
+
const [rx, ry, rz] = rotateDeg.map((d) => (d * Math.PI) / 180)
|
|
273
|
+
const cx = Math.cos(rx), sx = Math.sin(rx)
|
|
274
|
+
const cy = Math.cos(ry), sy = Math.sin(ry)
|
|
275
|
+
const cz = Math.cos(rz), sz = Math.sin(rz)
|
|
276
|
+
const sc = Number(scale) || 1
|
|
277
|
+
return tris.map((t) =>
|
|
278
|
+
t.map((v) => {
|
|
279
|
+
let [x, y, z] = v.map((c) => c * sc)
|
|
280
|
+
const y1 = y * cx - z * sx
|
|
281
|
+
const z1 = y * sx + z * cx
|
|
282
|
+
const x1 = x * cy + z1 * sy
|
|
283
|
+
const z2 = -x * sy + z1 * cy
|
|
284
|
+
const x2 = x1 * cz - y1 * sz
|
|
285
|
+
const y2 = x1 * sz + y1 * cz
|
|
286
|
+
return [x2 + translate[0], y2 + translate[1], z2 + translate[2]]
|
|
287
|
+
}),
|
|
288
|
+
)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function stlGenerate(spec = {}) {
|
|
292
|
+
let tris = []
|
|
293
|
+
if (Array.isArray(spec.parts) && spec.parts.length) {
|
|
294
|
+
for (const part of spec.parts) {
|
|
295
|
+
const pt = makePrimitive(part.shape, part.params || {})
|
|
296
|
+
tris.push(...transformTris(pt, part.translate || [0, 0, 0], part.rotate_deg || [0, 0, 0]))
|
|
297
|
+
}
|
|
298
|
+
} else {
|
|
299
|
+
tris = makePrimitive(spec.shape, spec.params || {})
|
|
300
|
+
}
|
|
301
|
+
tris = transformTris(tris, spec.translate || [0, 0, 0], spec.rotate_deg || [0, 0, 0], spec.scale || 1)
|
|
302
|
+
return tris
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// --- mesh plan dictionaries ---
|
|
306
|
+
|
|
307
|
+
export function fmt(n) {
|
|
308
|
+
return Number.isInteger(n) ? String(n) : String(Number(n.toPrecision(8)))
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function planMesh(cfg, args, caps = null) {
|
|
312
|
+
const stlPath = resolveCase(cfg, args.stl_path)
|
|
313
|
+
if (!stlPath || !existsSync(stlPath)) return { ok: false, error: 'stl not found: ' + stlPath }
|
|
314
|
+
const info = inspectStl(stlPath)
|
|
315
|
+
if (!info.ok) return info
|
|
316
|
+
const mn = info.bbox.min
|
|
317
|
+
const mx = info.bbox.max
|
|
318
|
+
const size = [mx[0] - mn[0], mx[1] - mn[1], mx[2] - mn[2]]
|
|
319
|
+
const L = Math.max(size[0], size[1], size[2]) || 1
|
|
320
|
+
const cell = Number(args.target_cell_size) > 0 ? Number(args.target_cell_size) : L / 30
|
|
321
|
+
const domain = args.domain || { type: 'wrap' }
|
|
322
|
+
let dmn = [mn[0], mn[1], mn[2]]
|
|
323
|
+
let dmx = [mx[0], mx[1], mx[2]]
|
|
324
|
+
const type = domain.type || 'wrap'
|
|
325
|
+
if (type === 'box') {
|
|
326
|
+
if (!Array.isArray(domain.bbox_min) || !Array.isArray(domain.bbox_max)) return { ok: false, error: 'box domain needs bbox_min/bbox_max' }
|
|
327
|
+
dmn = domain.bbox_min.map(Number)
|
|
328
|
+
dmx = domain.bbox_max.map(Number)
|
|
329
|
+
} else if (type === 'external') {
|
|
330
|
+
const up = Number(domain.upstream) > 0 ? Number(domain.upstream) : 3
|
|
331
|
+
const down = Number(domain.downstream) > 0 ? Number(domain.downstream) : 5
|
|
332
|
+
const lat = Number(domain.lateral) > 0 ? Number(domain.lateral) : 3
|
|
333
|
+
const vt = Number(domain.vertical_top) > 0 ? Number(domain.vertical_top) : 3
|
|
334
|
+
const vb = Number(domain.vertical_bottom) > 0 ? Number(domain.vertical_bottom) : 3
|
|
335
|
+
dmn = [mn[0] - up * L, mn[1] - lat * L, mn[2] - vb * L]
|
|
336
|
+
dmx = [mx[0] + down * L, mx[1] + lat * L, mx[2] + vt * L]
|
|
337
|
+
} else if (type === 'internal') {
|
|
338
|
+
dmn = [mn[0], mn[1], mn[2]]
|
|
339
|
+
dmx = [mx[0], mx[1], mx[2]]
|
|
340
|
+
} else {
|
|
341
|
+
const m = 0.1 * L
|
|
342
|
+
dmn = [mn[0] - m, mn[1] - m, mn[2] - m]
|
|
343
|
+
dmx = [mx[0] + m, mx[1] + m, mx[2] + m]
|
|
344
|
+
}
|
|
345
|
+
const nx = Math.max(1, Math.round((dmx[0] - dmn[0]) / cell))
|
|
346
|
+
const ny = Math.max(1, Math.round((dmx[1] - dmn[1]) / cell))
|
|
347
|
+
const nz = Math.max(1, Math.round((dmx[2] - dmn[2]) / cell))
|
|
348
|
+
const v = [
|
|
349
|
+
[dmn[0], dmn[1], dmn[2]], [dmx[0], dmn[1], dmn[2]], [dmx[0], dmx[1], dmn[2]], [dmn[0], dmx[1], dmn[2]],
|
|
350
|
+
[dmn[0], dmn[1], dmx[2]], [dmx[0], dmn[1], dmx[2]], [dmx[0], dmx[1], dmx[2]], [dmn[0], dmx[1], dmx[2]],
|
|
351
|
+
]
|
|
352
|
+
const name = basename(stlPath).replace(/\.stl$/i, '') || 'geometry'
|
|
353
|
+
const header = (cls, obj) => `FoamFile\n{\n format ascii;\n class ${cls};\n object ${obj};\n}\n`
|
|
354
|
+
const blockMesh = header('dictionary', 'blockMeshDict') + `\n${(caps && caps.blockMeshScale) || 'scale'} 1;\n\nvertices\n(\n${v.map((p) => ` (${p.map(fmt).join(' ')})`).join('\n')}\n);\n\nblocks\n(\n hex (0 1 2 3 4 5 6 7) (${nx} ${ny} ${nz}) simpleGrading (1 1 1)\n);\n\nboundary\n(\n inlet\n {\n type patch;\n faces ((0 4 7 3));\n }\n outlet\n {\n type patch;\n faces ((1 2 6 5));\n }\n walls\n {\n type wall;\n faces ((0 1 5 4) (3 2 6 7) (0 1 2 3) (4 5 6 7));\n }\n);\n`
|
|
355
|
+
const loc = Array.isArray(args.location_in_mesh) && args.location_in_mesh.length === 3
|
|
356
|
+
? args.location_in_mesh.map(Number)
|
|
357
|
+
: type === 'internal'
|
|
358
|
+
? [(mn[0] + mx[0]) / 2, (mn[1] + mx[1]) / 2, (mn[2] + mx[2]) / 2]
|
|
359
|
+
: [dmn[0] + 0.05 * (dmx[0] - dmn[0]), (dmn[1] + dmx[1]) / 2, (dmn[2] + dmx[2]) / 2]
|
|
360
|
+
const refMin = Number(args.refinement_level_min) || 1
|
|
361
|
+
const refMax = Number(args.refinement_level_max) || 3
|
|
362
|
+
const feat = Number(args.feature_level) || refMax
|
|
363
|
+
const nLayers = Number(args.n_layers) || 0
|
|
364
|
+
const firstLayer = Number(args.first_layer_thickness) || 0
|
|
365
|
+
const expRatio = Number(args.expansion_ratio) || 1.2
|
|
366
|
+
const snappy = header('dictionary', 'snappyHexMeshDict') + `\ncastellatedMesh true;\nsnap true;\naddLayers ${nLayers > 0 ? 'true' : 'false'};\n\ngeometry\n{\n ${name}\n {\n type triSurfaceMesh;\n file "${name}.stl";\n }\n}\n\ncastellatedMeshControls\n{\n maxLocalCells 1000000;\n maxGlobalCells 8000000;\n minRefinementCells 10;\n nCellsBetweenLevels 5;\n features\n (\n {\n file "${name}.eMesh";\n level ${feat};\n }\n )\n refinementSurfaces\n {\n ${name}\n {\n level (${refMin} ${refMax});\n patchInfo\n {\n type wall;\n }\n }\n }\n resolveFeatureAngle 30;\n refinementRegions\n {\n }\n locationInMesh (${loc.map(fmt).join(' ')});\n allowFreeStandingZoneFaces true;\n}\n\nsnapControls\n{\n nSmoothPatch 3;\n tolerance 2.0;\n nSolveIter 30;\n nRelaxIter 5;\n}\n\naddLayersControls\n{\n relativeSizes false;\n layers\n {\n ${name}\n {\n nSurfaceLayers ${nLayers};\n }\n }\n expansionRatio ${expRatio};\n ${firstLayer > 0 ? `firstLayerThickness ${firstLayer};` : 'firstLayerThickness 0.001;'}\n minThickness 1e-6;\n nGrow 0;\n featureAngle 30;\n}\n\nmeshQualityControls\n{\n${(caps && caps.snappyQualityKeys) === false ? '' : ' errorReduction 0.75;\\n nSmoothScale 4;\\n maxIter 100;\\n'} maxNonOrtho 65;\n maxBoundarySkewness 20;\n maxInternalSkewness 4;\n maxConcave 80;\n minVol 1e-13;\n minTetQuality 1e-15;\n minArea -1;\n minTwist 0.02;\n minDeterminant 0.001;\n minFaceWeight 0.05;\n minVolRatio 0.01;\n minTriangleTwist -1;\n}\n\ndebug 0;\nmergeTolerance 1e-6;\n`
|
|
367
|
+
const featureStyle = (caps && caps.featuresDictStyle) || 'extract'
|
|
368
|
+
const featureName = (caps && caps.featuresDict) || 'surfaceFeatureExtractDict'
|
|
369
|
+
const featureDict = featureStyle === 'surfaceFeatures'
|
|
370
|
+
? header('dictionary', featureName) + `\nsurfaces ("${name}.stl");\n\nincludedAngle 150;\n\nsubsetFeatures\n{\n nonManifoldEdges yes;\n openEdges yes;\n}\n`
|
|
371
|
+
: header('dictionary', featureName) + `\nsurfaces\n(\n "${name}.stl"\n {\n extractionMethod extractFromSurface;\n includedAngle 150;\n }\n);\n`
|
|
372
|
+
return { ok: true, stl: stlPath, name, bbox: { min: mn, max: mx }, domain: { type, min: dmn, max: dmx }, cells: [nx, ny, nz], target_cell_size: cell, blockMeshDict: blockMesh, snappyHexMeshDict: snappy, featureDict, featureDictName: featureName }
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// --- diagnostics + four-step + experience store ---
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Payloads for the `needs_input` protocol.
|
|
2
|
+
//
|
|
3
|
+
// The payload is language-neutral on purpose: it carries the decision (a kind,
|
|
4
|
+
// numbers and stable option ids) and one English instruction for the model.
|
|
5
|
+
// The user-facing wording is the agent's job — CORE.md tells it to ask with
|
|
6
|
+
// ask_user_question in {{language}} and to translate the option labels. This
|
|
7
|
+
// keeps one gate implementation for every language and every future gate.
|
|
8
|
+
/**
|
|
9
|
+
* Does the geometry-scale gate apply? A value exactly at the threshold must not
|
|
10
|
+
* fire because of floating-point noise (0.1 > 0.1 is false, but 0.1000000000002
|
|
11
|
+
* is not).
|
|
12
|
+
*/
|
|
13
|
+
export function scaleGateApplies(extent, askAbove) {
|
|
14
|
+
const value = Number(extent)
|
|
15
|
+
const above = Number(askAbove)
|
|
16
|
+
if (!(above > 0) || !Number.isFinite(value)) return false
|
|
17
|
+
return value > above * (1 + 1e-9)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function geometryScalePrompt({ extent, askAbove }) {
|
|
21
|
+
const value = Number(extent)
|
|
22
|
+
return {
|
|
23
|
+
kind: 'geometry-scale',
|
|
24
|
+
ask: `Confirm the model units before continuing: the geometry is ${value.toFixed(3)} m across, above the ${Number(askAbove)} m threshold.`,
|
|
25
|
+
data: { extent_m: Number(value.toFixed(3)), askAbove_m: Number(askAbove) },
|
|
26
|
+
options: [
|
|
27
|
+
{ id: 'metres-ok', label: 'metres are correct, continue' },
|
|
28
|
+
{ id: 'millimetres', label: 'millimetres: rescale by 1e-3' },
|
|
29
|
+
{ id: 'other', label: 'other (I will explain)' },
|
|
30
|
+
],
|
|
31
|
+
}
|
|
32
|
+
}
|