087c169Physically simulated pizzicato double bass and an SF2 built from itJeremy Magland 1// Orthotropic plate / shallow-shell strain energy on a regular grid, in energy form (weighted
2// squares of finite-difference strains, so K = Bᵀ D B is symmetric PSD by construction).
3//
4// Bending (Kirchhoff): curvatures κxx = δxx w, κss = δss w at the DOF nodes (3-point second
5// differences), twist κxs = δx⁺δs⁺ w at cell centres. Energy
6// U_b = ½ Σ_nodes h² [D11 κxx² + 2 D12 κxx κss + D22 κss²] + ½ Σ_cells h² 4 D66 κxs².
7// Constrained (OUT) nodes enter the stencils with w = 0 and carry no curvature term themselves:
8// on a rectangle this is exactly the simply-supported FD plate (the sin·sin modes are discrete
9// eigenvectors). A stencil that reaches into a HOLE node is dropped and the remaining curvature
10// gets the free-edge condensed rigidity (e.g. D11 − D12²/D22 when the cross-hole curvature is
11// free), which approximates a moment-free edge.
12//
13// Membrane (shallow shell, Donnell–Marguerre): ε_xx = u_x − z0_xx w, ε_ss = v_s − z0_ss w,
14// γ = u_s + v_x − 2 z0_xs w, with bilinear (Q4) u, v and 2×2 Gauss integration (no hourglass
15// modes) and the curvature-coupling term evaluated with the cell-mean w (relieves membrane
16// locking). This is the Marguerre form ε_xx = u_x + z0_x w_x rewritten with the tangential
17// displacement ũ = u + z0_x w, identical under the rim condition u = v = w = 0.
19import { Terms } from "./sparse.ts";
20import { HOLE, IN, OUT, type Grid } from "./geometry.ts";
22export interface Ortho {
23 t: number;
24 rhoh: number;
25 D11: number;
26 D12: number;
27 D22: number;
28 D66: number;
29 A11: number;
30 A12: number;
31 A22: number;
32 A66: number;
33}
35/** Orthotropic plate rigidities, L (grain) along x. nuLR is the major Poisson ratio. */
36export function ortho(EL: number, ER: number, G: number, nuLR: number, rho: number, t: number): Ortho {
37 const nuRL = (nuLR * ER) / EL;
38 const den = 1 - nuLR * nuRL;
39 const Q11 = EL / den, Q22 = ER / den, Q12 = (nuLR * ER) / den, Q66 = G;
40 const c3 = (t * t * t) / 12;
41 return { t, rhoh: rho * t, D11: Q11 * c3, D12: Q12 * c3, D22: Q22 * c3, D66: Q66 * c3, A11: Q11 * t, A12: Q12 * t, A22: Q22 * t, A66: Q66 * t };
42}
44/** Eigen-decomposition of [[a, b], [b, c]]: [λ1, v1x, v1y, λ2, v2x, v2y]. */
45export function sym2(a: number, b: number, c: number): number[] {
46 const m = 0.5 * (a + c), d = Math.hypot(0.5 * (a - c), b);
47 const l1 = m + d, l2 = m - d;
48 let vx: number, vy: number;
49 if (Math.abs(b) < 1e-300 * (Math.abs(a) + Math.abs(c) + 1)) {
50 if (a >= c) {
51 vx = 1;
52 vy = 0;
53 } else {
54 vx = 0;
55 vy = 1;
56 }
57 } else {
58 vx = b;
59 vy = l1 - a;
60 const nn = Math.hypot(vx, vy);
61 vx /= nn;
62 vy /= nn;
63 }
64 return [l1, vx, vy, l2, -vy, vx];
65}
67function typeAt(g: Grid, i: number, j: number): number {
68 if (i < 0 || j < 0 || i >= g.nx || j >= g.ns) return OUT;
69 return g.type[i * g.ns + j];
70}
72function dofAt(g: Grid, dof: Int32Array, i: number, j: number): number {
73 if (i < 0 || j < 0 || i >= g.nx || j >= g.ns) return -1;
74 return dof[i * g.ns + j];
75}
77/**
78 * Bending energy of a plate on grid g (node types from g.type), w DOFs in wd (−1 = none).
79 * rimRotK: rotational spring per unit length on the normal slope at the rim (each link from a DOF
80 * node to a constrained neighbour contributes ½ k h (w/h)²).
81 */
82export function addBending(T: Terms, g: Grid, wd: Int32Array, P: Ortho, group: number, rimRotK: number): void {
83 const h = g.h, area = h * h, ih2 = 1 / (h * h);
84 const [l1, a1, b1, l2, a2, b2] = sym2(P.D11, P.D12, P.D22);
85 const cx = P.D11 - (P.D12 * P.D12) / P.D22;
86 const cs = P.D22 - (P.D12 * P.D12) / P.D11;
87 const idx: number[] = [], coef: number[] = [];
88 for (let i = 0; i < g.nx; i++)
89 for (let j = 0; j < g.ns; j++) {
90 const k = i * g.ns + j;
91 if (g.type[k] !== IN) continue;
92 const hasX = typeAt(g, i - 1, j) !== HOLE && typeAt(g, i + 1, j) !== HOLE;
93 const hasS = typeAt(g, i, j - 1) !== HOLE && typeAt(g, i, j + 1) !== HOLE;
94 const xI = [dofAt(g, wd, i - 1, j), wd[k], dofAt(g, wd, i + 1, j)];
95 const sI = [dofAt(g, wd, i, j - 1), wd[k], dofAt(g, wd, i, j + 1)];
96 const st = [ih2, -2 * ih2, ih2];
97 if (hasX && hasS) {
98 for (const [lam, ax, bx] of [
99 [l1, a1, b1],
100 [l2, a2, b2],
101 ]) {
102 idx.length = 0;
103 coef.length = 0;
104 for (let q = 0; q < 3; q++) {
105 idx.push(xI[q], sI[q]);
106 coef.push(ax * st[q], bx * st[q]);
107 }
108 T.add(idx, coef, lam * area, group);
109 }
110 } else if (hasX) T.add(xI, st, cx * area, group);
111 else if (hasS) T.add(sI, st, cs * area, group);
112 if (rimRotK > 0) {
113 const nb = [
114 [i - 1, j],
115 [i + 1, j],
116 [i, j - 1],
117 [i, j + 1],
118 ];
119 for (const [ii, jj] of nb) if (typeAt(g, ii, jj) === OUT) T.add([wd[k]], [1 / h], rimRotK * h, group);
120 }
121 }
122 // twist at cell centres
123 for (let i = 0; i + 1 < g.nx; i++)
124 for (let j = 0; j + 1 < g.ns; j++) {
125 const t00 = typeAt(g, i, j), t10 = typeAt(g, i + 1, j), t01 = typeAt(g, i, j + 1), t11 = typeAt(g, i + 1, j + 1);
126 if (t00 === HOLE || t10 === HOLE || t01 === HOLE || t11 === HOLE) continue;
127 if (t00 !== IN && t10 !== IN && t01 !== IN && t11 !== IN) continue;
128 T.add(
129 [dofAt(g, wd, i, j), dofAt(g, wd, i + 1, j), dofAt(g, wd, i, j + 1), dofAt(g, wd, i + 1, j + 1)],
130 [ih2, -ih2, -ih2, ih2],
131 4 * P.D66 * area,
132 group,
133 );
134 }
135}
137/** Arching curvatures at cell centres from the nodal z0 (zero outside the grid). */
138export function cellCurvatures(g: Grid): { kxx: Float64Array; kss: Float64Array; kxs: Float64Array } {
139 const { nx, ns, h } = g;
140 const z = (i: number, j: number) => (i < 0 || j < 0 || i >= nx || j >= ns ? 0 : g.z0[i * ns + j]);
141 const nc = (nx - 1) * (ns - 1);
142 const kxx = new Float64Array(nc), kss = new Float64Array(nc), kxs = new Float64Array(nc);
143 const c = 1 / (2 * h * h);
144 for (let i = 0; i + 1 < nx; i++)
145 for (let j = 0; j + 1 < ns; j++) {
146 const k = i * (ns - 1) + j;
147 let sx = 0, ss = 0;
148 for (const jj of [j, j + 1]) sx += (z(i + 2, jj) - z(i + 1, jj) - z(i, jj) + z(i - 1, jj)) * c;
149 for (const ii of [i, i + 1]) ss += (z(ii, j + 2) - z(ii, j + 1) - z(ii, j) + z(ii, j - 1)) * c;
150 kxx[k] = 0.5 * sx;
151 kss[k] = 0.5 * ss;
152 kxs[k] = (z(i + 1, j + 1) - z(i + 1, j) - z(i, j + 1) + z(i, j)) / (h * h);
153 }
154 return { kxx, kss, kxs };
155}
157/** Nodal arching curvature z0_xx (3-point). */
158export function nodeKxx(g: Grid, i: number, j: number): number {
159 const z = (ii: number) => (ii < 0 || ii >= g.nx ? 0 : g.z0[ii * g.ns + j]);
160 return (z(i + 1) - 2 * z(i) + z(i - 1)) / (g.h * g.h);
161}
163/** Membrane (shallow-shell) energy: Q4 cells with 2×2 Gauss points. */
164export function addMembrane(T: Terms, g: Grid, wd: Int32Array, ud: Int32Array, vd: Int32Array, P: Ortho, group: number): void {
165 const { nx, ns, h } = g;
166 const kap = cellCurvatures(g);
167 const [l1, a1, b1, l2, a2, b2] = sym2(P.A11, P.A12, P.A22);
168 const gp = 1 / Math.sqrt(3);
169 const xiA = [-1, 1, -1, 1], etA = [-1, -1, 1, 1];
170 const wgt = (h * h) / 4;
171 const idx = new Array<number>(12), c1 = new Array<number>(12), c2 = new Array<number>(12), cg = new Array<number>(12);
172 const ca = new Array<number>(12), cb = new Array<number>(12);
173 for (let i = 0; i + 1 < nx; i++)
174 for (let j = 0; j + 1 < ns; j++) {
175 const corners = [
176 [i, j],
177 [i + 1, j],
178 [i, j + 1],
179 [i + 1, j + 1],
180 ];
181 let anyIn = false, anyHole = false;
182 for (const [ii, jj] of corners) {
183 const t = typeAt(g, ii, jj);
184 if (t === IN) anyIn = true;
185 if (t === HOLE) anyHole = true;
186 }
187 if (!anyIn || anyHole) continue;
188 const kc = i * (ns - 1) + j;
189 const kxx = kap.kxx[kc], kss = kap.kss[kc], kxs = kap.kxs[kc];
190 for (let a = 0; a < 4; a++) {
191 const [ii, jj] = corners[a];
192 idx[a] = dofAt(g, ud, ii, jj);
193 idx[4 + a] = dofAt(g, vd, ii, jj);
194 idx[8 + a] = dofAt(g, wd, ii, jj);
195 }
196 for (const xi of [-gp, gp])
197 for (const et of [-gp, gp]) {
198 for (let a = 0; a < 4; a++) {
199 const dx = (xiA[a] * (1 + etA[a] * et)) / (2 * h);
200 const ds = (etA[a] * (1 + xiA[a] * xi)) / (2 * h);
201 // ε_xx
202 c1[a] = dx;
203 c1[4 + a] = 0;
204 c1[8 + a] = -0.25 * kxx;
205 // ε_ss
206 c2[a] = 0;
207 c2[4 + a] = ds;
208 c2[8 + a] = -0.25 * kss;
209 // γ
210 cg[a] = ds;
211 cg[4 + a] = dx;
212 cg[8 + a] = -0.5 * kxs;
213 }
214 for (let q = 0; q < 12; q++) {
215 ca[q] = a1 * c1[q] + b1 * c2[q];
216 cb[q] = a2 * c1[q] + b2 * c2[q];
217 }
218 T.add(idx, ca, l1 * wgt, group);
219 T.add(idx, cb, l2 * wgt, group);
220 T.add(idx, cg, P.A66 * wgt, group);
221 }
222 }
223}