// Body geometry: outline (top view), arching, the plate grid and the f-holes. // // Coordinates: x along the body (0 = lower end, +x toward the neck), s lateral (+s toward the // treble side), z normal to the plates (back plate at z = 0, top at z = ribDepth + arch, +z out of // the top). import type { Params } from "../core/params.ts"; export type FEParams = Params["fe"]; export interface Outline { L: number; /** half width of the body at x (0 outside [0, L]) */ halfWidth: (x: number) => number; maxHalf: number; /** closed polygon (x, s) of the rim, for distance queries */ px: Float64Array; ps: Float64Array; area: number; } /** * Smooth symmetric violin-family outline: elliptic caps from the ends to the lower and upper bout * maxima (rounded ends, vertical tangent at the end points), and half-cosine blends (zero slope at * the extremes) bout -> waist -> bout in between. C1 everywhere. */ export function makeOutline(fe: FEParams): Outline { const L = fe.bodyLength; const xl = fe.lowerBoutPos * L, xm = fe.middleBoutPos * L, xu = fe.upperBoutPos * L; const wl = fe.lowerBout / 2, wm = fe.middleBout / 2, wu = fe.upperBout / 2; const halfWidth = (x: number): number => { if (x <= 0 || x >= L) return 0; if (x < xl) { const t = (xl - x) / xl; return wl * Math.sqrt(Math.max(0, 1 - t * t)); } if (x < xm) { const t = (x - xl) / (xm - xl); return wl + (wm - wl) * 0.5 * (1 - Math.cos(Math.PI * t)); } if (x < xu) { const t = (x - xm) / (xu - xm); return wm + (wu - wm) * 0.5 * (1 - Math.cos(Math.PI * t)); } const t = (x - xu) / (L - xu); return wu * Math.sqrt(Math.max(0, 1 - t * t)); }; const nh = 600; const px = new Float64Array(2 * nh), ps = new Float64Array(2 * nh); for (let k = 0; k < nh; k++) { const t = k / (nh - 1); const x = (L * (1 - Math.cos(Math.PI * t))) / 2; px[k] = x; ps[k] = halfWidth(x); px[2 * nh - 1 - k] = x; ps[2 * nh - 1 - k] = -halfWidth(x); } let area = 0; for (let k = 0; k < 2 * nh; k++) { const k2 = (k + 1) % (2 * nh); area += px[k] * ps[k2] - px[k2] * ps[k]; } area = Math.abs(area) / 2; return { L, halfWidth, maxHalf: Math.max(wl, wm, wu), px, ps, area }; } /** Signed distance to the rim, positive inside. */ export function rimDistance(o: Outline, x: number, s: number): number { const { px, ps } = o; const n = px.length; let best = Infinity; for (let k = 0; k < n; k++) { const k2 = k + 1 === n ? 0 : k + 1; const ax = px[k], as = ps[k]; const dx = px[k2] - ax, ds = ps[k2] - as; const l2 = dx * dx + ds * ds; let t = l2 > 0 ? ((x - ax) * dx + (s - as) * ds) / l2 : 0; t = t < 0 ? 0 : t > 1 ? 1 : t; const ex = ax + t * dx - x, es = as + t * ds - s; const d2 = ex * ex + es * es; if (d2 < best) best = d2; } const inside = Math.abs(s) < o.halfWidth(x); return inside ? Math.sqrt(best) : -Math.sqrt(best); } /** * Arching of the top: z0 = topArch · g(ξ) · c(η) with ξ = x/L and η = s / halfWidth(x). * Long arch g = sin(πξ)(2 − sin(πξ)) (full in the middle, linear at the ends so the curvature * stays finite in the rounded end caps); cross arch c = (1 − η²)(1 − η²/2), which has a slight * recurve (concave) zone near the rim and vanishes at it. */ export function makeArch(fe: FEParams, o: Outline): (x: number, s: number) => number { const A = fe.topArch, L = o.L; return (x: number, s: number) => { const hw = o.halfWidth(x); if (hw <= 0) return 0; const eta = s / hw; if (eta <= -1 || eta >= 1) return 0; const sp = Math.sin((Math.PI * x) / L); const g = sp * (2 - sp); const e2 = eta * eta; return A * g * (1 - e2) * (1 - 0.5 * e2); }; } /** Node types on a plate grid. */ export const OUT = 0, IN = 1, HOLE = 2; export interface Grid { nx: number; ns: number; h: number; x0: number; s0: number; /** OUT (constrained, w = u = v = 0), IN (has DOFs), HOLE (void: free edge) */ type: Int8Array; /** arching height at the node (top) */ z0: Float64Array; } export function nodeX(g: Grid, i: number): number { return g.x0 + i * g.h; } export function nodeS(g: Grid, j: number): number { return g.s0 + j * g.h; } /** * Body grid: symmetric about s = 0 (s = 0 is a grid line), with a margin of constrained nodes. * A node is a DOF node if it lies at least h/2 inside the rim: the plate is then constrained at * the first node outside, which lies on the rim on average (simply-supported FD boundary). */ export function makeBodyGrid(fe: FEParams, o: Outline, arch: (x: number, s: number) => number, h: number): Grid { const hx = Math.ceil(o.L / 2 / h) + 2, hs = Math.ceil(o.maxHalf / h) + 2; const nx = 2 * hx + 1, ns = 2 * hs + 1; const x0 = o.L / 2 - hx * h, s0 = -hs * h; const type = new Int8Array(nx * ns), z0 = new Float64Array(nx * ns); for (let i = 0; i < nx; i++) for (let j = 0; j < ns; j++) { const x = x0 + i * h, s = s0 + j * h; const k = i * ns + j; z0[k] = arch(x, s); if (Math.abs(s) >= o.halfWidth(x)) continue; if (rimDistance(o, x, s) >= 0.5 * h) type[k] = IN; } return { nx, ns, h, x0, s0, type, z0 }; } export interface FHole { /** centre */ xc: number; sc: number; /** lateral drift of the slot centre line per unit x (the lower end lies further out) */ tilt: number; length: number; width: number; area: number; /** sample points along the slot (for the air coupling) */ pts: { x: number; s: number }[]; } /** The two f-holes: slots along x centred at the bridge line, at s = ±fholeSpread. */ export function makeFHoles(fe: FEParams): FHole[] { const xb = fe.bridgePos * fe.bodyLength; const holes: FHole[] = []; if (!(fe.fholeLength > 0 && fe.fholeWidth > 0)) return holes; for (const side of [-1, 1]) { const tilt = side * 0.15; const sc = side * fe.fholeSpread; const pts: { x: number; s: number }[] = []; const np = 7; for (let k = 0; k < np; k++) { const x = xb + fe.fholeLength * ((k + 0.5) / np - 0.5); pts.push({ x, s: sc + tilt * (xb - x) }); } holes.push({ xc: xb, sc, tilt, length: fe.fholeLength, width: fe.fholeWidth, area: fe.fholeLength * fe.fholeWidth, pts }); } return holes; } /** Remove the top-plate nodes inside the f-hole slots (at least one node per grid row). */ export function markFHoles(g: Grid, holes: FHole[]): number { let n = 0; for (const fh of holes) { for (let i = 0; i < g.nx; i++) { const x = nodeX(g, i); if (Math.abs(x - fh.xc) > fh.length / 2) continue; const sc = fh.sc + fh.tilt * (fh.xc - x); const jn = Math.round((sc - g.s0) / g.h); for (let j = 0; j < g.ns; j++) { const k = i * g.ns + j; if (g.type[k] !== IN) continue; const s = nodeS(g, j); if (j === jn || Math.abs(s - sc) < fh.width / 2) { g.type[k] = HOLE; n++; } } } } return n; } /** Bilinear interpolation stencil on a grid: 4 node indices and weights. */ export function bilinear(g: Grid, x: number, s: number): { nodes: number[]; wts: number[] } { const fi = (x - g.x0) / g.h, fj = (s - g.s0) / g.h; const i = Math.floor(fi), j = Math.floor(fj); const a = fi - i, b = fj - j; return { nodes: [i * g.ns + j, (i + 1) * g.ns + j, i * g.ns + j + 1, (i + 1) * g.ns + j + 1], wts: [(1 - a) * (1 - b), a * (1 - b), (1 - a) * b, a * b], }; } /** * Conductivity C = A / l of an elliptic aperture in a thin wall (Rayleigh): C = π a / K(e), * a = semi-major axis, e² = 1 − (b/a)², K = complete elliptic integral of the first kind * (π / (2 AGM(1, b/a))). For a circle this gives the familiar 2a. The f-hole is replaced by the * ellipse with the same length and area. Effective neck length l_eff = A / C + plate thickness. */ export function slotEffectiveLength(length: number, width: number, thickness: number): number { const A = length * width; const a = length / 2; const b = Math.min(a, A / (Math.PI * a)); let x = 1, y = b / a; for (let k = 0; k < 40; k++) { const xn = 0.5 * (x + y); y = Math.sqrt(x * y); x = xn; } const K = Math.PI / (2 * x); const C = (Math.PI * a) / K; return A / C + thickness; }