/ music / acoustic-base
Sign in
music / acoustic-base
acoustic-base / src / fe / geometry.ts
235 lines · 8.1 KBBlameHistoryRaw
1// Body geometry: outline (top view), arching, the plate grid and the f-holes.
2//
3// Coordinates: x along the body (0 = lower end, +x toward the neck), s lateral (+s toward the
4// treble side), z normal to the plates (back plate at z = 0, top at z = ribDepth + arch, +z out of
5// the top).
7import type { Params } from "../core/params.ts";
9export type FEParams = Params["fe"];
11export interface Outline {
12 L: number;
13 /** half width of the body at x (0 outside [0, L]) */
14 halfWidth: (x: number) => number;
15 maxHalf: number;
16 /** closed polygon (x, s) of the rim, for distance queries */
17 px: Float64Array;
18 ps: Float64Array;
19 area: number;
22/**
23 * Smooth symmetric violin-family outline: elliptic caps from the ends to the lower and upper bout
24 * maxima (rounded ends, vertical tangent at the end points), and half-cosine blends (zero slope at
25 * the extremes) bout -> waist -> bout in between. C1 everywhere.
26 */
27export function makeOutline(fe: FEParams): Outline {
28 const L = fe.bodyLength;
29 const xl = fe.lowerBoutPos * L, xm = fe.middleBoutPos * L, xu = fe.upperBoutPos * L;
30 const wl = fe.lowerBout / 2, wm = fe.middleBout / 2, wu = fe.upperBout / 2;
31 const halfWidth = (x: number): number => {
32 if (x <= 0 || x >= L) return 0;
33 if (x < xl) {
34 const t = (xl - x) / xl;
35 return wl * Math.sqrt(Math.max(0, 1 - t * t));
36 }
37 if (x < xm) {
38 const t = (x - xl) / (xm - xl);
39 return wl + (wm - wl) * 0.5 * (1 - Math.cos(Math.PI * t));
40 }
41 if (x < xu) {
42 const t = (x - xm) / (xu - xm);
43 return wm + (wu - wm) * 0.5 * (1 - Math.cos(Math.PI * t));
44 }
45 const t = (x - xu) / (L - xu);
46 return wu * Math.sqrt(Math.max(0, 1 - t * t));
47 };
48 const nh = 600;
49 const px = new Float64Array(2 * nh), ps = new Float64Array(2 * nh);
50 for (let k = 0; k < nh; k++) {
51 const t = k / (nh - 1);
52 const x = (L * (1 - Math.cos(Math.PI * t))) / 2;
53 px[k] = x;
54 ps[k] = halfWidth(x);
55 px[2 * nh - 1 - k] = x;
56 ps[2 * nh - 1 - k] = -halfWidth(x);
57 }
58 let area = 0;
59 for (let k = 0; k < 2 * nh; k++) {
60 const k2 = (k + 1) % (2 * nh);
61 area += px[k] * ps[k2] - px[k2] * ps[k];
62 }
63 area = Math.abs(area) / 2;
64 return { L, halfWidth, maxHalf: Math.max(wl, wm, wu), px, ps, area };
67/** Signed distance to the rim, positive inside. */
68export function rimDistance(o: Outline, x: number, s: number): number {
69 const { px, ps } = o;
70 const n = px.length;
71 let best = Infinity;
72 for (let k = 0; k < n; k++) {
73 const k2 = k + 1 === n ? 0 : k + 1;
74 const ax = px[k], as = ps[k];
75 const dx = px[k2] - ax, ds = ps[k2] - as;
76 const l2 = dx * dx + ds * ds;
77 let t = l2 > 0 ? ((x - ax) * dx + (s - as) * ds) / l2 : 0;
78 t = t < 0 ? 0 : t > 1 ? 1 : t;
79 const ex = ax + t * dx - x, es = as + t * ds - s;
80 const d2 = ex * ex + es * es;
81 if (d2 < best) best = d2;
82 }
83 const inside = Math.abs(s) < o.halfWidth(x);
84 return inside ? Math.sqrt(best) : -Math.sqrt(best);
87/**
88 * Arching of the top: z0 = topArch · g(ξ) · c(η) with ξ = x/L and η = s / halfWidth(x).
89 * Long arch g = sin(πξ)(2 − sin(πξ)) (full in the middle, linear at the ends so the curvature
90 * stays finite in the rounded end caps); cross arch c = (1 − η²)(1 − η²/2), which has a slight
91 * recurve (concave) zone near the rim and vanishes at it.
92 */
93export function makeArch(fe: FEParams, o: Outline): (x: number, s: number) => number {
94 const A = fe.topArch, L = o.L;
95 return (x: number, s: number) => {
96 const hw = o.halfWidth(x);
97 if (hw <= 0) return 0;
98 const eta = s / hw;
99 if (eta <= -1 || eta >= 1) return 0;
100 const sp = Math.sin((Math.PI * x) / L);
101 const g = sp * (2 - sp);
102 const e2 = eta * eta;
103 return A * g * (1 - e2) * (1 - 0.5 * e2);
104 };
107/** Node types on a plate grid. */
108export const OUT = 0, IN = 1, HOLE = 2;
110export interface Grid {
111 nx: number;
112 ns: number;
113 h: number;
114 x0: number;
115 s0: number;
116 /** OUT (constrained, w = u = v = 0), IN (has DOFs), HOLE (void: free edge) */
117 type: Int8Array;
118 /** arching height at the node (top) */
119 z0: Float64Array;
122export function nodeX(g: Grid, i: number): number {
123 return g.x0 + i * g.h;
125export function nodeS(g: Grid, j: number): number {
126 return g.s0 + j * g.h;
129/**
130 * Body grid: symmetric about s = 0 (s = 0 is a grid line), with a margin of constrained nodes.
131 * A node is a DOF node if it lies at least h/2 inside the rim: the plate is then constrained at
132 * the first node outside, which lies on the rim on average (simply-supported FD boundary).
133 */
134export function makeBodyGrid(fe: FEParams, o: Outline, arch: (x: number, s: number) => number, h: number): Grid {
135 const hx = Math.ceil(o.L / 2 / h) + 2, hs = Math.ceil(o.maxHalf / h) + 2;
136 const nx = 2 * hx + 1, ns = 2 * hs + 1;
137 const x0 = o.L / 2 - hx * h, s0 = -hs * h;
138 const type = new Int8Array(nx * ns), z0 = new Float64Array(nx * ns);
139 for (let i = 0; i < nx; i++)
140 for (let j = 0; j < ns; j++) {
141 const x = x0 + i * h, s = s0 + j * h;
142 const k = i * ns + j;
143 z0[k] = arch(x, s);
144 if (Math.abs(s) >= o.halfWidth(x)) continue;
145 if (rimDistance(o, x, s) >= 0.5 * h) type[k] = IN;
146 }
147 return { nx, ns, h, x0, s0, type, z0 };
150export interface FHole {
151 /** centre */
152 xc: number;
153 sc: number;
154 /** lateral drift of the slot centre line per unit x (the lower end lies further out) */
155 tilt: number;
156 length: number;
157 width: number;
158 area: number;
159 /** sample points along the slot (for the air coupling) */
160 pts: { x: number; s: number }[];
163/** The two f-holes: slots along x centred at the bridge line, at s = ±fholeSpread. */
164export function makeFHoles(fe: FEParams): FHole[] {
165 const xb = fe.bridgePos * fe.bodyLength;
166 const holes: FHole[] = [];
167 if (!(fe.fholeLength > 0 && fe.fholeWidth > 0)) return holes;
168 for (const side of [-1, 1]) {
169 const tilt = side * 0.15;
170 const sc = side * fe.fholeSpread;
171 const pts: { x: number; s: number }[] = [];
172 const np = 7;
173 for (let k = 0; k < np; k++) {
174 const x = xb + fe.fholeLength * ((k + 0.5) / np - 0.5);
175 pts.push({ x, s: sc + tilt * (xb - x) });
176 }
177 holes.push({ xc: xb, sc, tilt, length: fe.fholeLength, width: fe.fholeWidth, area: fe.fholeLength * fe.fholeWidth, pts });
178 }
179 return holes;
182/** Remove the top-plate nodes inside the f-hole slots (at least one node per grid row). */
183export function markFHoles(g: Grid, holes: FHole[]): number {
184 let n = 0;
185 for (const fh of holes) {
186 for (let i = 0; i < g.nx; i++) {
187 const x = nodeX(g, i);
188 if (Math.abs(x - fh.xc) > fh.length / 2) continue;
189 const sc = fh.sc + fh.tilt * (fh.xc - x);
190 const jn = Math.round((sc - g.s0) / g.h);
191 for (let j = 0; j < g.ns; j++) {
192 const k = i * g.ns + j;
193 if (g.type[k] !== IN) continue;
194 const s = nodeS(g, j);
195 if (j === jn || Math.abs(s - sc) < fh.width / 2) {
196 g.type[k] = HOLE;
197 n++;
198 }
199 }
200 }
201 }
202 return n;
205/** Bilinear interpolation stencil on a grid: 4 node indices and weights. */
206export function bilinear(g: Grid, x: number, s: number): { nodes: number[]; wts: number[] } {
207 const fi = (x - g.x0) / g.h, fj = (s - g.s0) / g.h;
208 const i = Math.floor(fi), j = Math.floor(fj);
209 const a = fi - i, b = fj - j;
210 return {
211 nodes: [i * g.ns + j, (i + 1) * g.ns + j, i * g.ns + j + 1, (i + 1) * g.ns + j + 1],
212 wts: [(1 - a) * (1 - b), a * (1 - b), (1 - a) * b, a * b],
213 };
216/**
217 * Conductivity C = A / l of an elliptic aperture in a thin wall (Rayleigh): C = π a / K(e),
218 * a = semi-major axis, e² = 1 − (b/a)², K = complete elliptic integral of the first kind
219 * (π / (2 AGM(1, b/a))). For a circle this gives the familiar 2a. The f-hole is replaced by the
220 * ellipse with the same length and area. Effective neck length l_eff = A / C + plate thickness.
221 */
222export function slotEffectiveLength(length: number, width: number, thickness: number): number {
223 const A = length * width;
224 const a = length / 2;
225 const b = Math.min(a, A / (Math.PI * a));
226 let x = 1, y = b / a;
227 for (let k = 0; k < 40; k++) {
228 const xn = 0.5 * (x + y);
229 y = Math.sqrt(x * y);
230 x = xn;
231 }
232 const K = Math.PI / (2 * x);
233 const C = (Math.PI * a) / K;
234 return A / C + thickness;
↑↓ move↵openescclose