@rulecad/fea 0.2.8 → 0.2.10
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 +159 -1
- package/frame.d.mts +367 -0
- package/frame.mjs +660 -0
- package/package.json +14 -4
- package/threads.d.mts +9 -9
- package/threads.mjs +6 -1
package/frame.mjs
ADDED
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
// Frames of beams joined at nodes: how far a frame moves under load, what each beam carries, the load at which it
|
|
2
|
+
// buckles and the frequencies it rings at. Three-dimensional beam elements with six freedoms a node (three movements,
|
|
3
|
+
// three rotations) and shear deformation (Timoshenko's beam, which becomes Euler-Bernoulli's when shear is stiff).
|
|
4
|
+
// Plain numbers in typed arrays, no dependencies: it runs in browsers and Node alike.
|
|
5
|
+
//
|
|
6
|
+
// Units: millimetres, newtons, MPa (N/mm²), radians for rotations, N·mm for moments, N/mm and N·mm/rad for springs.
|
|
7
|
+
// Masses are in kg, densities in g/cm³ (as the kit's material rule sets give them) and gravity in N/kg (the same
|
|
8
|
+
// number as m/s²: [0, 0, -9.81]), so a mass times gravity is newtons with no factor. In the modal solve stiffness is
|
|
9
|
+
// N/mm and mass kg, and N/mm ÷ kg = 1000 s⁻² (a newton is a kg·m/s² and the lengths are mm), so the angular
|
|
10
|
+
// frequency squared is 1000 times the eigenvalue of K against M in kg: frequency = √(1000 λ) / 2π, in Hz.
|
|
11
|
+
//
|
|
12
|
+
// An element's local axes: x runs from its node `a` to its node `b`; z is the part of `up` square to x (`up` is
|
|
13
|
+
// global +Z, or global +X for an element along Z, unless the element gives one); y = z × x. `roll` then turns y and z
|
|
14
|
+
// about x, right-handed, in degrees. Iy is the second moment about local y (it resists bending that moves the beam
|
|
15
|
+
// along z) and Iz about local z.
|
|
16
|
+
//
|
|
17
|
+
// Matrices (sources in the README): the stiffness with the shear parameter Φ = 12 E I / (G As L²) is
|
|
18
|
+
// Przemieniecki's (1968, eq. 5.116); the geometric stiffness and the consistent mass are integrals of the same
|
|
19
|
+
// element's shape functions (Friedman and Kosmatka 1993), which for Φ = 0 are the familiar 6/5, L/10, 2L²/15 and
|
|
20
|
+
// 156, 22L, 4L² tables. An end's rotational spring or release is condensed out of the element, K − K_s (K_ss + k)⁻¹
|
|
21
|
+
// K_sᵀ, which is exact for the stiffness and the loads; the mass and geometric stiffness are carried through the same
|
|
22
|
+
// reduction (Guyan's), so buckling and modes want each member cut into several elements (`subdivide`).
|
|
23
|
+
|
|
24
|
+
/** @typedef {[number, number, number]} Vec3 */
|
|
25
|
+
/**
|
|
26
|
+
* @typedef {object} FrameSection A beam's cross-section, in its local axes
|
|
27
|
+
* @property {number} A area, mm²
|
|
28
|
+
* @property {number} Iy second moment about local y, mm⁴ (bending that moves the beam along local z)
|
|
29
|
+
* @property {number} Iz second moment about local z, mm⁴ (bending that moves the beam along local y)
|
|
30
|
+
* @property {number} J torsion constant, mm⁴
|
|
31
|
+
* @property {number} [Asy] shear area for shear along local y, mm² (left out: no shear deformation that way)
|
|
32
|
+
* @property {number} [Asz] shear area for shear along local z, mm²
|
|
33
|
+
* @property {number} [cy] distance from the centroid to the furthest fibre along local y, mm (for stress)
|
|
34
|
+
* @property {number} [cz] distance to the furthest fibre along local z, mm
|
|
35
|
+
* @property {number} [Wy] section modulus for bending about y, mm³ (Iy / cz, if cz is not given)
|
|
36
|
+
* @property {number} [Wz] section modulus for bending about z, mm³ (Iz / cy)
|
|
37
|
+
*/
|
|
38
|
+
/**
|
|
39
|
+
* @typedef {object} FrameMaterial
|
|
40
|
+
* @property {number} E Young's modulus, MPa
|
|
41
|
+
* @property {number} [G] shear modulus, MPa (or give nu)
|
|
42
|
+
* @property {number} [nu] Poisson's ratio, for G = E / 2(1 + nu)
|
|
43
|
+
* @property {number} [density] g/cm³ (for gravity and modes; left out: weightless)
|
|
44
|
+
*/
|
|
45
|
+
/** @typedef {'rigid' | 'free' | number} Fixity how one rotation at an element's end is joined to its node: rigidly, not at all (a hinge), or through a spring of that many N·mm/rad */
|
|
46
|
+
/** @typedef {'rigid' | 'pinned' | 'ball' | { rx?: Fixity, ry?: Fixity, rz?: Fixity }} EndJoin 'pinned' frees both bending rotations (ry, rz) and carries twist; 'ball' frees all three; or say each, about the element's local axes */
|
|
47
|
+
/**
|
|
48
|
+
* @typedef {object} FrameElement
|
|
49
|
+
* @property {string | number} [id] its name in the results (its place in the list if left out)
|
|
50
|
+
* @property {string | number} a the node it starts at
|
|
51
|
+
* @property {string | number} b the node it ends at
|
|
52
|
+
* @property {FrameSection | string} section the section, or its name in the model's `sections`
|
|
53
|
+
* @property {FrameMaterial | string} material the material, or its name in the model's `materials`
|
|
54
|
+
* @property {number} [roll] degrees about its own axis, turning local y towards local z
|
|
55
|
+
* @property {Vec3} [up] the direction local z leans towards (global +Z, or +X for an element along Z)
|
|
56
|
+
* @property {{ a?: EndJoin, b?: EndJoin }} [ends] how each end is joined to its node ('rigid' if left out)
|
|
57
|
+
* @property {string | number} [of] the member this is a piece of (set by `subdivide`, handed back in the results)
|
|
58
|
+
*/
|
|
59
|
+
/** @typedef {boolean | number | 'fixed' | 'free'} Hold one freedom of a support: held (true), free, or a spring to ground of that many N/mm (movements) or N·mm/rad (rotations) */
|
|
60
|
+
/** @typedef {'fixed' | 'pinned' | { x?: Hold, y?: Hold, z?: Hold, rx?: Hold, ry?: Hold, rz?: Hold }} FrameSupport 'fixed' holds all six freedoms, 'pinned' the three movements */
|
|
61
|
+
/** @typedef {{ node: string | number, force?: Vec3, moment?: Vec3 } | { element: string | number, spread: Vec3, axes?: 'global' | 'local' }} FrameLoad a force (N) and moment (N·mm) at a node, in global axes; or a load spread evenly along an element, N per mm of its length, in global axes or its own */
|
|
62
|
+
/**
|
|
63
|
+
* @typedef {object} FrameModel
|
|
64
|
+
* @property {Record<string, Vec3> | Vec3[]} nodes each node's position in mm, by name (or a list, named by place: 0, 1, ...)
|
|
65
|
+
* @property {FrameElement[]} elements
|
|
66
|
+
* @property {Record<string, FrameSection>} [sections] sections by name
|
|
67
|
+
* @property {Record<string, FrameMaterial>} [materials] materials by name
|
|
68
|
+
* @property {Record<string, FrameSupport>} supports by node
|
|
69
|
+
* @property {FrameLoad[]} [loads]
|
|
70
|
+
* @property {Vec3} [gravity] N/kg (m/s²), acting on every element's mass and every lumped mass: [0, 0, -9.81]
|
|
71
|
+
* @property {Record<string, number>} [masses] lumped masses by node, kg (weight under gravity, and inertia in modes)
|
|
72
|
+
*/
|
|
73
|
+
/** @typedef {[number, number, number, number, number, number]} Six x, y, z, then about x, y, z */
|
|
74
|
+
/**
|
|
75
|
+
* @typedef {object} ElementResult
|
|
76
|
+
* @property {string | number} id
|
|
77
|
+
* @property {string | number} [of] the member it is a piece of, when the model came from `subdivide`
|
|
78
|
+
* @property {number} length mm
|
|
79
|
+
* @property {[Vec3, Vec3, Vec3]} axes its local x, y and z in global axes
|
|
80
|
+
* @property {{ a: Six, b: Six }} ends the forces (N) and moments (N·mm) its nodes put on each end, in local axes
|
|
81
|
+
* @property {{ x: number[], N: number[], Vy: number[], Vz: number[], T: number[], My: number[], Mz: number[], d: Vec3[] }} along
|
|
82
|
+
* at each station x (mm from a): axial force (tension positive), shear along local y and z, torque, bending moments
|
|
83
|
+
* about local y and z (each as the part of the beam beyond x acts on the part before it, so at b they are the end
|
|
84
|
+
* forces), and the movement there in global axes (mm). The stations are evenly spaced, plus where each moment peaks.
|
|
85
|
+
* @property {{ N: Peak, Vy: Peak, Vz: Peak, T: Peak, My: Peak, Mz: Peak }} peak each one's largest value by size, signed, and where
|
|
86
|
+
* @property {number} axial the mean axial force, N (tension positive): what buckling reads
|
|
87
|
+
* @property {{ peak: number, bending: number, axial: number, x: number }} [stress] MPa, when the section has cy and cz (or
|
|
88
|
+
* Wy and Wz): the largest |N|/A + |My|/Wy + |Mz|/Wz along the element and where; the largest bending part alone;
|
|
89
|
+
* and N/A at the peak
|
|
90
|
+
*/
|
|
91
|
+
/** @typedef {{ value: number, x: number }} Peak */
|
|
92
|
+
/**
|
|
93
|
+
* @typedef {object} StaticResult
|
|
94
|
+
* @property {Record<string, Six>} displacements each node's movement (mm) and rotation (rad), in global axes
|
|
95
|
+
* @property {Record<string, Six>} reactions at each supported node: the force (N) and moment (N·mm) its support gives the frame
|
|
96
|
+
* @property {ElementResult[]} elements in the model's order
|
|
97
|
+
* @property {{ most: number, node: string }} deflection the largest movement of any node, and which
|
|
98
|
+
*/
|
|
99
|
+
/** @typedef {{ factor: number, shape: Record<string, Six> }} BucklingMode the loads times `factor` buckle the frame into `shape` (largest movement 1) */
|
|
100
|
+
/** @typedef {{ frequency: number, shape: Record<string, Six> }} VibrationMode Hz, and the shape (largest movement 1) */
|
|
101
|
+
|
|
102
|
+
const FREEDOMS = ['x', 'y', 'z', 'rx', 'ry', 'rz'];
|
|
103
|
+
/** @returns {never} */
|
|
104
|
+
const refuse = (code, text, more) => { throw Object.assign(new Error(text), { code: `fea.frame.${code}`, ...more }); };
|
|
105
|
+
const isVec3 = (v) => (Array.isArray(v) || ArrayBuffer.isView(v)) && v.length === 3 && Number.isFinite(v[0]) && Number.isFinite(v[1]) && Number.isFinite(v[2]);
|
|
106
|
+
const said = (v) => (typeof v === 'string' ? `"${v}"` : JSON.stringify(v) ?? String(v));
|
|
107
|
+
|
|
108
|
+
// ---- one element, in its own axes: [u, v, w, θx, θy, θz] at a, then the same at b ----
|
|
109
|
+
|
|
110
|
+
// 4-point Gauss on 0..1: exact to degree 7 (the mass matrix's integrand is degree 6)
|
|
111
|
+
const GAUSS = [[0.5 - 0.4305681557970263, 0.1739274225687269], [0.5 - 0.16999052179242816, 0.3260725774312731], [0.5 + 0.16999052179242816, 0.3260725774312731], [0.5 + 0.4305681557970263, 0.1739274225687269]];
|
|
112
|
+
const XY = [1, 5, 7, 11], XZ = [2, 4, 8, 10], FLIP = [1, -1, 1, -1]; // bending in the x-y plane (v, θz) and in x-z (w, θy = −dw/dx: its rotations change sign)
|
|
113
|
+
|
|
114
|
+
// The Timoshenko element's shape functions in one bending plane at ξ = x / L, for [v1, θ1, v2, θ2]: the deflection N,
|
|
115
|
+
// and with `all` its slope D and the section's rotation R (Friedman and Kosmatka 1993). Φ = 0 gives the Hermite cubics.
|
|
116
|
+
function shape(xi, L, phi, all) {
|
|
117
|
+
const f = 1 / (1 + phi), x2 = xi * xi, x3 = x2 * xi;
|
|
118
|
+
const N = [f * (2 * x3 - 3 * x2 - phi * xi + 1 + phi), f * L * (x3 - (2 + phi / 2) * x2 + (1 + phi / 2) * xi), -f * (2 * x3 - 3 * x2 - phi * xi), f * L * (x3 - (1 - phi / 2) * x2 - (phi / 2) * xi)];
|
|
119
|
+
if (!all) return { N };
|
|
120
|
+
const D = [f * (6 * x2 - 6 * xi - phi) / L, f * (3 * x2 - (4 + phi) * xi + 1 + phi / 2), -f * (6 * x2 - 6 * xi - phi) / L, f * (3 * x2 - (2 - phi) * xi - phi / 2)];
|
|
121
|
+
const R = [f * 6 * (x2 - xi) / L, f * (3 * x2 - (4 + phi) * xi + 1 + phi), -f * 6 * (x2 - xi) / L, f * (3 * x2 - (2 - phi) * xi)];
|
|
122
|
+
return { N, D, R };
|
|
123
|
+
}
|
|
124
|
+
// ∫ DᵀD (geometric stiffness for a tension of 1), ∫ NᵀN (mass, for 1 a length) and ∫ RᵀR (rotary inertia), each 4 × 4
|
|
125
|
+
function planeForms(L, phi) {
|
|
126
|
+
const g = new Float64Array(16), m = new Float64Array(16), r = new Float64Array(16);
|
|
127
|
+
for (const [xi, wt] of GAUSS) {
|
|
128
|
+
const { N, D, R } = shape(xi, L, phi, true), w = wt * L;
|
|
129
|
+
for (let i = 0; i < 4; i++) for (let j = 0; j < 4; j++) { g[4 * i + j] += w * D[i] * D[j]; m[4 * i + j] += w * N[i] * N[j]; r[4 * i + j] += w * R[i] * R[j]; }
|
|
130
|
+
}
|
|
131
|
+
return { g, m, r };
|
|
132
|
+
}
|
|
133
|
+
// One bending plane's stiffness for [v1, θ1, v2, θ2]: Przemieniecki (1968) eq. 5.116
|
|
134
|
+
function planeStiffness(EI, L, phi) {
|
|
135
|
+
const c = EI / ((1 + phi) * L ** 3), a = 12 * c, b = 6 * L * c, d = (4 + phi) * L * L * c, e = (2 - phi) * L * L * c;
|
|
136
|
+
return [a, b, -a, b, b, d, -b, e, -a, -b, a, -b, b, e, -b, d];
|
|
137
|
+
}
|
|
138
|
+
const put = (K, m4, idx, flip, scale) => { for (let i = 0; i < 4; i++) for (let j = 0; j < 4; j++) K[idx[i] * 12 + idx[j]] += scale * m4[4 * i + j] * (flip ? FLIP[i] * FLIP[j] : 1); };
|
|
139
|
+
const bar = (K, i, j, s, t = -s) => { K[i * 12 + i] += s; K[j * 12 + j] += s; K[i * 12 + j] += t; K[j * 12 + i] += t; };
|
|
140
|
+
const shearModulus = (material) => material.G ?? material.E / (2 * (1 + material.nu));
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* One beam element's matrices in its own axes, both ends rigid: 12 × 12, row by row, for [u, v, w, θx, θy, θz] at its
|
|
144
|
+
* start then its end. `k` is its stiffness (N/mm, N, N·mm), `kg` its geometric stiffness for a tension of 1 N, and
|
|
145
|
+
* `m` its consistent mass in kg (kg·mm for the cross terms, kg·mm² for rotations), with rotary inertia.
|
|
146
|
+
* @param {FrameSection} section @param {FrameMaterial} material @param {number} L length, mm
|
|
147
|
+
* @param {{ shear?: boolean }} [o] shear: false ignores the shear areas (Euler-Bernoulli)
|
|
148
|
+
* @returns {{ k: Float64Array, kg: Float64Array, m: Float64Array, phi: [number, number] }} phi: the shear parameters
|
|
149
|
+
* 12 E Iz / (G Asy L²) and 12 E Iy / (G Asz L²)
|
|
150
|
+
*/
|
|
151
|
+
export function elementMatrices(section, material, L, { shear = true } = {}) {
|
|
152
|
+
const { A, Iy, Iz, J } = section, { E } = material, G = shearModulus(material), rho = (material.density || 0) * 1e-6; // kg/mm³
|
|
153
|
+
const phiY = shear && section.Asy ? (12 * E * Iz) / (G * section.Asy * L * L) : 0, phiZ = shear && section.Asz ? (12 * E * Iy) / (G * section.Asz * L * L) : 0;
|
|
154
|
+
const k = new Float64Array(144), kg = new Float64Array(144), m = new Float64Array(144), fy = planeForms(L, phiY), fz = planeForms(L, phiZ);
|
|
155
|
+
bar(k, 0, 6, (E * A) / L); bar(k, 3, 9, (G * J) / L);
|
|
156
|
+
put(k, planeStiffness(E * Iz, L, phiY), XY, false, 1); put(k, planeStiffness(E * Iy, L, phiZ), XZ, true, 1);
|
|
157
|
+
put(kg, fy.g, XY, false, 1); put(kg, fz.g, XZ, true, 1);
|
|
158
|
+
bar(m, 0, 6, (rho * A * L) / 3, (rho * A * L) / 6); bar(m, 3, 9, (rho * (Iy + Iz) * L) / 3, (rho * (Iy + Iz) * L) / 6);
|
|
159
|
+
put(m, fy.m, XY, false, rho * A); put(m, fy.r, XY, false, rho * Iz); put(m, fz.m, XZ, true, rho * A); put(m, fz.r, XZ, true, rho * Iy);
|
|
160
|
+
return { k, kg, m, phi: [phiY, phiZ] };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// The inverse of a small matrix (n ≤ 6) by Gauss-Jordan with pivoting; null if it has none
|
|
164
|
+
function invert(A, n) {
|
|
165
|
+
const a = Float64Array.from(A), inv = new Float64Array(n * n);
|
|
166
|
+
let big = 0;
|
|
167
|
+
for (let i = 0; i < n; i++) { inv[i * n + i] = 1; big = Math.max(big, Math.abs(a[i * n + i])); }
|
|
168
|
+
for (let c = 0; c < n; c++) {
|
|
169
|
+
let p = c;
|
|
170
|
+
for (let r = c + 1; r < n; r++) if (Math.abs(a[r * n + c]) > Math.abs(a[p * n + c])) p = r;
|
|
171
|
+
if (!(Math.abs(a[p * n + c]) > 1e-12 * big)) return null;
|
|
172
|
+
if (p !== c) for (let j = 0; j < n; j++) { let t = a[c * n + j]; a[c * n + j] = a[p * n + j]; a[p * n + j] = t; t = inv[c * n + j]; inv[c * n + j] = inv[p * n + j]; inv[p * n + j] = t; }
|
|
173
|
+
const d = a[c * n + c];
|
|
174
|
+
for (let j = 0; j < n; j++) { a[c * n + j] /= d; inv[c * n + j] /= d; }
|
|
175
|
+
for (let r = 0; r < n; r++) if (r !== c) { const f = a[r * n + c]; if (f) for (let j = 0; j < n; j++) { a[r * n + j] -= f * a[c * n + j]; inv[r * n + j] -= f * inv[c * n + j]; } }
|
|
176
|
+
}
|
|
177
|
+
return inv;
|
|
178
|
+
}
|
|
179
|
+
const mul12 = (A, B) => { const C = new Float64Array(144); for (let i = 0; i < 12; i++) for (let k = 0; k < 12; k++) { const a = A[i * 12 + k]; if (a) for (let j = 0; j < 12; j++) C[i * 12 + j] += a * B[k * 12 + j]; } return C; };
|
|
180
|
+
const transpose12 = (A) => { const C = new Float64Array(144); for (let i = 0; i < 12; i++) for (let j = 0; j < 12; j++) C[j * 12 + i] = A[i * 12 + j]; return C; };
|
|
181
|
+
const symmetric12 = (A) => { for (let i = 0; i < 12; i++) for (let j = 0; j < i; j++) A[i * 12 + j] = A[j * 12 + i] = (A[i * 12 + j] + A[j * 12 + i]) / 2; return A; };
|
|
182
|
+
|
|
183
|
+
// Springs and releases at an element's ends, condensed out. `springs` has a stiffness for each of the 12 freedoms:
|
|
184
|
+
// Infinity where the end is rigid, 0 where it is released. With s the sprung freedoms and H = (K_ss + k)⁻¹, the
|
|
185
|
+
// beam's own end freedoms follow its nodes' as d_beam = T d + H f_s, with T = I − E_s H K[s, :], and the condensed
|
|
186
|
+
// stiffness is K T = K − K_s H K_sᵀ: nothing is subtracted from a large number however stiff the spring.
|
|
187
|
+
function condense({ k, kg, m }, springs) {
|
|
188
|
+
const s = [];
|
|
189
|
+
for (let i = 0; i < 12; i++) if (springs[i] !== Infinity) s.push(i);
|
|
190
|
+
if (!s.length) return { k, kg, m, T: null, H: null, s };
|
|
191
|
+
const n = s.length, A = new Float64Array(n * n);
|
|
192
|
+
for (let p = 0; p < n; p++) for (let q = 0; q < n; q++) A[p * n + q] = k[s[p] * 12 + s[q]] + (p === q ? springs[s[p]] : 0);
|
|
193
|
+
const H = invert(A, n);
|
|
194
|
+
if (!H) return null;
|
|
195
|
+
const T = new Float64Array(144);
|
|
196
|
+
for (let i = 0; i < 12; i++) T[i * 13] = 1;
|
|
197
|
+
for (let p = 0; p < n; p++) for (let j = 0; j < 12; j++) { let sum = 0; for (let q = 0; q < n; q++) sum += H[p * n + q] * k[s[q] * 12 + j]; T[s[p] * 12 + j] -= sum; }
|
|
198
|
+
const Tt = transpose12(T);
|
|
199
|
+
return { k: symmetric12(mul12(k, T)), kg: symmetric12(mul12(Tt, mul12(kg, T))), m: symmetric12(mul12(Tt, mul12(m, T))), T, H, s };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Λᵀ K Λ: a 12 × 12 matrix from local to global axes, R's rows being the local axes
|
|
203
|
+
function toGlobal(K, R) {
|
|
204
|
+
const t = new Float64Array(144), out = new Float64Array(144);
|
|
205
|
+
for (let i = 0; i < 12; i++) for (let q = 0; q < 12; q += 3) for (let c = 0; c < 3; c++) t[i * 12 + q + c] = K[i * 12 + q] * R[c] + K[i * 12 + q + 1] * R[3 + c] + K[i * 12 + q + 2] * R[6 + c];
|
|
206
|
+
for (let p = 0; p < 12; p += 3) for (let r = 0; r < 3; r++) for (let j = 0; j < 12; j++) out[(p + r) * 12 + j] = R[r] * t[p * 12 + j] + R[3 + r] * t[(p + 1) * 12 + j] + R[6 + r] * t[(p + 2) * 12 + j];
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
const toLocal3 = (R, v, o = 0) => [R[0] * v[o] + R[1] * v[o + 1] + R[2] * v[o + 2], R[3] * v[o] + R[4] * v[o + 1] + R[5] * v[o + 2], R[6] * v[o] + R[7] * v[o + 1] + R[8] * v[o + 2]];
|
|
210
|
+
const toGlobal3 = (R, v, o = 0) => [R[0] * v[o] + R[3] * v[o + 1] + R[6] * v[o + 2], R[1] * v[o] + R[4] * v[o + 1] + R[7] * v[o + 2], R[2] * v[o] + R[5] * v[o + 1] + R[8] * v[o + 2]];
|
|
211
|
+
|
|
212
|
+
// ---- the frame's matrix: symmetric, each row kept from its first non-zero column to the diagonal ----
|
|
213
|
+
|
|
214
|
+
// Nodes in an order that keeps joined nodes close (reverse Cuthill-McKee), so rows are short
|
|
215
|
+
function ordering(n, pairs) {
|
|
216
|
+
const adj = Array.from({ length: n }, () => []);
|
|
217
|
+
for (const [a, b] of pairs) { adj[a].push(b); adj[b].push(a); }
|
|
218
|
+
const seen = new Uint8Array(n), out = [];
|
|
219
|
+
const sweep = (start, mark) => { const q = [start]; mark[start] = 1; for (let h = 0; h < q.length; h++) for (const m of adj[q[h]].filter((v) => !mark[v]).sort((a, b) => adj[a].length - adj[b].length)) { mark[m] = 1; q.push(m); } return q; };
|
|
220
|
+
for (const s of [...adj.keys()].sort((a, b) => adj[a].length - adj[b].length)) {
|
|
221
|
+
if (seen[s]) continue;
|
|
222
|
+
const far = sweep(s, new Uint8Array(n)).at(-1); // start from an end of the frame, not its middle
|
|
223
|
+
out.push(...sweep(far, seen));
|
|
224
|
+
}
|
|
225
|
+
return out.reverse();
|
|
226
|
+
}
|
|
227
|
+
function profile(first) {
|
|
228
|
+
const n = first.length, start = new Int32Array(n + 1);
|
|
229
|
+
for (let i = 0; i < n; i++) start[i + 1] = start[i] + i - first[i] + 1;
|
|
230
|
+
return { n, first, start, v: new Float64Array(start[n]) };
|
|
231
|
+
}
|
|
232
|
+
const multiply = (P, x, y = new Float64Array(P.n)) => {
|
|
233
|
+
const { n, first, start, v } = P;
|
|
234
|
+
y.fill(0);
|
|
235
|
+
for (let i = 0; i < n; i++) { const o = start[i] - first[i], xi = x[i]; let s = 0; for (let j = first[i]; j < i; j++) { s += v[o + j] * x[j]; y[j] += v[o + j] * xi; } y[i] += s + v[o + i] * xi; }
|
|
236
|
+
return y;
|
|
237
|
+
};
|
|
238
|
+
// Cholesky, L Lᵀ, in the same rows. A pivot that all but vanishes is a freedom nothing holds: it is noted, held,
|
|
239
|
+
// and the factorisation goes on, so every loose freedom is found in one pass.
|
|
240
|
+
function factorise(P) {
|
|
241
|
+
const { n, first, start } = P, v = Float64Array.from(P.v), loose = [];
|
|
242
|
+
for (let i = 0; i < n; i++) {
|
|
243
|
+
const fi = first[i], ri = start[i] - fi;
|
|
244
|
+
for (let j = fi; j < i; j++) {
|
|
245
|
+
const fj = first[j], rj = start[j] - fj;
|
|
246
|
+
let s = v[ri + j];
|
|
247
|
+
for (let k = fi > fj ? fi : fj; k < j; k++) s -= v[ri + k] * v[rj + k];
|
|
248
|
+
v[ri + j] = s / v[rj + j];
|
|
249
|
+
}
|
|
250
|
+
const a = v[ri + i];
|
|
251
|
+
let d = a;
|
|
252
|
+
for (let k = fi; k < i; k++) d -= v[ri + k] * v[ri + k];
|
|
253
|
+
if (!(d > 1e-11 * a)) { loose.push(i); d = Math.max(a, 1) * 1e20; }
|
|
254
|
+
v[ri + i] = Math.sqrt(d);
|
|
255
|
+
}
|
|
256
|
+
const solve = (b, x = new Float64Array(n)) => {
|
|
257
|
+
for (let i = 0; i < n; i++) { const fi = first[i], ri = start[i] - fi; let s = b[i]; for (let k = fi; k < i; k++) s -= v[ri + k] * x[k]; x[i] = s / v[ri + i]; }
|
|
258
|
+
for (let i = n - 1; i >= 0; i--) { const fi = first[i], ri = start[i] - fi, xi = (x[i] /= v[ri + i]); for (let k = fi; k < i; k++) x[k] -= v[ri + k] * xi; }
|
|
259
|
+
return x;
|
|
260
|
+
};
|
|
261
|
+
return { solve, loose };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Eigenvalues and vectors of a small symmetric matrix by Jacobi rotations (A is used up): values[i] goes with column i of V
|
|
265
|
+
function jacobi(A, n) {
|
|
266
|
+
const V = new Float64Array(n * n);
|
|
267
|
+
for (let i = 0; i < n; i++) V[i * n + i] = 1;
|
|
268
|
+
for (let sweep = 0; sweep < 80; sweep++) {
|
|
269
|
+
let off = 0, all = 0;
|
|
270
|
+
for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) { const a = A[i * n + j] * A[i * n + j]; all += a; if (i !== j) off += a; }
|
|
271
|
+
if (!(off > 1e-26 * all)) break; // (the off-diagonal part is under 1e-13 of the whole)
|
|
272
|
+
for (let p = 0; p < n - 1; p++) for (let q = p + 1; q < n; q++) {
|
|
273
|
+
const apq = A[p * n + q];
|
|
274
|
+
if (apq === 0) continue;
|
|
275
|
+
const theta = (A[q * n + q] - A[p * n + p]) / (2 * apq), t = (theta < 0 ? -1 : 1) / (Math.abs(theta) + Math.sqrt(theta * theta + 1)), c = 1 / Math.sqrt(t * t + 1), s = t * c;
|
|
276
|
+
for (let k = 0; k < n; k++) { const kp = A[k * n + p], kq = A[k * n + q]; A[k * n + p] = c * kp - s * kq; A[k * n + q] = s * kp + c * kq; }
|
|
277
|
+
for (let k = 0; k < n; k++) { const pk = A[p * n + k], qk = A[q * n + k]; A[p * n + k] = c * pk - s * qk; A[q * n + k] = s * pk + c * qk; }
|
|
278
|
+
for (let k = 0; k < n; k++) { const kp = V[k * n + p], kq = V[k * n + q]; V[k * n + p] = c * kp - s * kq; V[k * n + q] = s * kp + c * kq; }
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return { values: Float64Array.from({ length: n }, (_, i) => A[i * n + i]), V };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// The largest μ of B x = μ K x (K positive definite and factorised, B symmetric: the mass, or minus the geometric
|
|
285
|
+
// stiffness) and their vectors, by subspace iteration (Bathe 2014, chapter 11): a block of vectors is put through
|
|
286
|
+
// K⁻¹ B again and again, and the small problem in the block (Rayleigh-Ritz) is solved by Jacobi rotations. The block
|
|
287
|
+
// is made orthonormal in K, not in B, so a B of low rank (one lumped mass, one beam in compression) is no trouble:
|
|
288
|
+
// vectors the block cannot tell apart are dropped. Only positive μ are wanted, `want` of them; if the block settles
|
|
289
|
+
// with fewer (negative ones crowd it: the frame would buckle more readily with its loads reversed), or has not
|
|
290
|
+
// settled in 30 steps, it doubles, up to the whole problem, where one step is exact. Buckling starts with a wider
|
|
291
|
+
// block than modes do: a frame's columns buckle at loads close together, and close values settle slowly in a
|
|
292
|
+
// block that holds only some of them.
|
|
293
|
+
function lowest(solveK, B, want, block = Math.max(2 * want, want + 8), tol = 1e-9, limit = 400) {
|
|
294
|
+
const n = B.n;
|
|
295
|
+
let seed = 12345;
|
|
296
|
+
const random = () => { let t = (seed += 0x6D2B79F5); t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 2147483648 - 1; };
|
|
297
|
+
const fresh = () => multiply(B, Float64Array.from({ length: n }, random));
|
|
298
|
+
let q = Math.min(n, block), Y = Array.from({ length: q }, fresh), last = null, exhausted = false;
|
|
299
|
+
for (let step = 0, since = 1; ; step++, since++) {
|
|
300
|
+
const offered = Y.length;
|
|
301
|
+
let Z = Y.map((y) => solveK(y)), BZ = Z.map((z) => multiply(B, z));
|
|
302
|
+
// each vector's size in K (zᵀ K z = zᵀ y): scaled to 1, or dropped if B gave it nothing
|
|
303
|
+
const keep = [];
|
|
304
|
+
Z.forEach((z, i) => { let e = 0; for (let k = 0; k < n; k++) e += z[k] * Y[i][k]; if (e > 0 && Number.isFinite(e)) { const s = 1 / Math.sqrt(e); for (let k = 0; k < n; k++) { z[k] *= s; BZ[i][k] *= s; Y[i][k] *= s; } keep.push(i); } });
|
|
305
|
+
Z = keep.map((i) => Z[i]); BZ = keep.map((i) => BZ[i]); Y = keep.map((i) => Y[i]);
|
|
306
|
+
const m = Z.length;
|
|
307
|
+
if (!m) return { values: [], vectors: [] };
|
|
308
|
+
const Kr = new Float64Array(m * m), Br = new Float64Array(m * m);
|
|
309
|
+
for (let i = 0; i < m; i++) for (let j = i; j < m; j++) { let a = 0, b = 0; const zi = Z[i], yj = Y[j], bj = BZ[j]; for (let k = 0; k < n; k++) { a += zi[k] * yj[k]; b += zi[k] * bj[k]; } Kr[i * m + j] = Kr[j * m + i] = a; Br[i * m + j] = Br[j * m + i] = b; }
|
|
310
|
+
// an orthonormal basis of the block in K: Kr = V Λ Vᵀ, W = V Λ^-½, leaving out directions the block repeats
|
|
311
|
+
const kr = jacobi(Kr, m), top = Math.max(...kr.values), cols = [...kr.values.keys()].filter((i) => kr.values[i] > 1e-12 * top), r = cols.length;
|
|
312
|
+
const W = new Float64Array(m * r), BW = new Float64Array(m * r), C = new Float64Array(r * r);
|
|
313
|
+
cols.forEach((c, j) => { const s = 1 / Math.sqrt(kr.values[c]); for (let i = 0; i < m; i++) W[i * r + j] = kr.V[i * m + c] * s; });
|
|
314
|
+
for (let i = 0; i < m; i++) for (let j = 0; j < r; j++) { let s = 0; for (let k = 0; k < m; k++) s += Br[i * m + k] * W[k * r + j]; BW[i * r + j] = s; }
|
|
315
|
+
for (let i = 0; i < r; i++) for (let j = 0; j < r; j++) { let s = 0; for (let k = 0; k < m; k++) s += W[k * r + i] * BW[k * r + j]; C[i * r + j] = s; }
|
|
316
|
+
const ritz = jacobi(C, r), by = [...ritz.values.keys()].sort((a, b) => ritz.values[b] - ritz.values[a]);
|
|
317
|
+
// the Ritz vectors (or their B products) from the block: column c = Σ_i S_i (W U)[i][c]
|
|
318
|
+
const mix = (S, count = r) => by.slice(0, count).map((c) => { const x = new Float64Array(n); for (let i = 0; i < m; i++) { let w = 0; for (let j = 0; j < r; j++) w += W[i * r + j] * ritz.V[j * r + c]; if (w) { const si = S[i]; for (let k = 0; k < n; k++) x[k] += w * si[k]; } } return x; });
|
|
319
|
+
const values = by.map((c) => ritz.values[c]), size = Math.max(Math.abs(values[0]), Math.abs(values[r - 1])), wanted = values.filter((v) => v > 1e-12 * size).slice(0, want);
|
|
320
|
+
if (since === 1 && r < offered) exhausted = true; // fresh vectors added nothing new: the block already holds all B can reach
|
|
321
|
+
const settled = !!last && last.length === wanted.length && wanted.every((v, i) => Math.abs(v - last[i]) <= tol * v);
|
|
322
|
+
if (r >= n || (settled && (wanted.length >= want || exhausted || q >= n)) || step + 1 >= limit) return { values: wanted, vectors: mix(Z, wanted.length) };
|
|
323
|
+
last = wanted;
|
|
324
|
+
Y = mix(BZ);
|
|
325
|
+
if ((settled || since >= 30) && q < n) { const more = Math.min(n, 2 * q) - q; for (let i = 0; i < more; i++) Y.push(fresh()); q += more; since = 0; last = null; }
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// ---- the model, read and refused by name ----
|
|
330
|
+
|
|
331
|
+
function read(model, { shear = true } = {}) {
|
|
332
|
+
if (!model || typeof model !== 'object') refuse('input', 'a frame is { nodes, elements, supports, loads }: got ' + said(model));
|
|
333
|
+
const { nodes, elements } = model;
|
|
334
|
+
if (!nodes || typeof nodes !== 'object') refuse('input', 'a frame needs nodes: positions in mm by name, { a: [0, 0, 0], b: [0, 0, 300] }, or a list of positions');
|
|
335
|
+
const ids = Array.isArray(nodes) ? nodes.map((_, i) => String(i)) : Object.keys(nodes), at = Array.isArray(nodes) ? nodes : Object.values(nodes), index = new Map(ids.map((id, i) => [id, i]));
|
|
336
|
+
if (!ids.length) refuse('input', 'a frame needs nodes: positions in mm by name, { a: [0, 0, 0], b: [0, 0, 300] }, or a list of positions');
|
|
337
|
+
at.forEach((p, i) => { if (!isVec3(p)) refuse('node', `node ${said(ids[i])} is at ${said(p)}: a node needs a position [x, y, z] in mm`, { node: ids[i] }); });
|
|
338
|
+
if (!Array.isArray(elements) || !elements.length) refuse('input', 'a frame needs elements: a list of { a, b, section, material }, each between two nodes');
|
|
339
|
+
const nodeOf = (name, what) => index.get(String(name)) ?? refuse('node', `${what} names node ${said(name)}, which is not in nodes (there ${ids.length === 1 ? 'is' : 'are'} ${ids.slice(0, 12).join(', ')}${ids.length > 12 ? ', ...' : ''})`, { node: name });
|
|
340
|
+
const named = (v, table, what, whose) => (typeof v === 'string' ? table?.[v] ?? refuse(what, `${whose} names ${what} "${v}", which is not in the model's ${what}s${table ? ` (there ${Object.keys(table).length === 1 ? 'is' : 'are'} ${Object.keys(table).join(', ')})` : ''}`) : v);
|
|
341
|
+
const N = ids.length, elementIds = new Map();
|
|
342
|
+
const els = elements.map((e, i) => {
|
|
343
|
+
const id = e?.id ?? i, whose = `element ${said(id)}`;
|
|
344
|
+
if (!e || typeof e !== 'object') refuse('input', `element ${i} is ${said(e)}: an element is { a, b, section, material }`);
|
|
345
|
+
if (elementIds.has(String(id))) refuse('input', `two elements are called ${said(id)}: each needs its own id`);
|
|
346
|
+
elementIds.set(String(id), i);
|
|
347
|
+
const a = nodeOf(e.a, whose), b = nodeOf(e.b, whose), pa = at[a], pb = at[b], dx = [pb[0] - pa[0], pb[1] - pa[1], pb[2] - pa[2]], L = Math.hypot(...dx);
|
|
348
|
+
if (a === b || !(L > 1e-9)) refuse('length', `${whose} has no length: it runs from node ${said(e.a)} to node ${said(e.b)}${a === b ? '' : ', which are at the same place'}. An element joins two nodes some way apart`, { element: id });
|
|
349
|
+
const section = named(e.section, model.sections, 'section', whose), material = named(e.material, model.materials, 'material', whose);
|
|
350
|
+
if (!section || typeof section !== 'object') refuse('section', `${whose} needs a section: { A, Iy, Iz, J } in mm² and mm⁴`, { element: id });
|
|
351
|
+
for (const key of ['A', 'Iy', 'Iz', 'J']) if (!(section[key] > 0) || !Number.isFinite(section[key])) refuse('section', `${whose}'s section has ${key} = ${said(section[key])}: it needs ${key} above 0 (A in mm²; Iy, Iz and J in mm⁴)`, { element: id, property: key });
|
|
352
|
+
for (const key of ['Asy', 'Asz', 'cy', 'cz', 'Wy', 'Wz']) if (section[key] !== undefined && (!(section[key] > 0) || !Number.isFinite(section[key]))) refuse('section', `${whose}'s section has ${key} = ${said(section[key])}: leave it out, or give it above 0`, { element: id, property: key });
|
|
353
|
+
if (!material || typeof material !== 'object') refuse('material', `${whose} needs a material: { E, nu or G, density }`, { element: id });
|
|
354
|
+
if (!(material.E > 0) || !Number.isFinite(material.E)) refuse('material', `${whose}'s material has E = ${said(material.E)}: it needs a Young's modulus above 0, in MPa`, { element: id, property: 'E' });
|
|
355
|
+
if (material.G !== undefined ? !(material.G > 0 && Number.isFinite(material.G)) : !(material.nu > -1 && material.nu < 0.5)) refuse('material', `${whose}'s material has G = ${said(material.G)} and nu = ${said(material.nu)}: it needs a shear modulus G above 0 in MPa, or a Poisson's ratio nu between -1 and 0.5`, { element: id, property: 'G' });
|
|
356
|
+
if (material.density !== undefined && !(material.density >= 0 && Number.isFinite(material.density))) refuse('material', `${whose}'s material has density = ${said(material.density)}: leave it out (weightless), or give it in g/cm³, 0 or more`, { element: id, property: 'density' });
|
|
357
|
+
// local axes
|
|
358
|
+
const x = dx.map((c) => c / L);
|
|
359
|
+
if (e.up !== undefined && !isVec3(e.up)) refuse('axes', `${whose}'s up is ${said(e.up)}: it is a direction [x, y, z] that local z leans towards`, { element: id });
|
|
360
|
+
if (e.roll !== undefined && !Number.isFinite(e.roll)) refuse('axes', `${whose}'s roll is ${said(e.roll)}: it is an angle in degrees about the element's own axis`, { element: id });
|
|
361
|
+
const up = e.up ?? (Math.abs(x[2]) > 0.999 ? [1, 0, 0] : [0, 0, 1]), along = up[0] * x[0] + up[1] * x[1] + up[2] * x[2];
|
|
362
|
+
let z = [up[0] - along * x[0], up[1] - along * x[1], up[2] - along * x[2]];
|
|
363
|
+
const zl = Math.hypot(...z);
|
|
364
|
+
if (!(zl > 1e-6 * Math.hypot(...up))) refuse('axes', `${whose}'s up, ${said(Array.from(up))}, runs along the element, so it does not say which way local z points: give a direction across it`, { element: id });
|
|
365
|
+
z = z.map((c) => c / zl);
|
|
366
|
+
let y = [z[1] * x[2] - z[2] * x[1], z[2] * x[0] - z[0] * x[2], z[0] * x[1] - z[1] * x[0]];
|
|
367
|
+
if (e.roll) { const c = Math.cos((e.roll * Math.PI) / 180), s = Math.sin((e.roll * Math.PI) / 180), y0 = y, z0 = z; y = y0.map((v, k) => c * v + s * z0[k]); z = z0.map((v, k) => c * z0[k] - s * y0[k]); }
|
|
368
|
+
// how each end is joined
|
|
369
|
+
const springs = new Float64Array(12).fill(Infinity);
|
|
370
|
+
for (const [end, o] of [['a', 3], ['b', 9]]) {
|
|
371
|
+
const j = e.ends?.[end] ?? 'rigid', each = j === 'rigid' ? {} : j === 'pinned' ? { ry: 'free', rz: 'free' } : j === 'ball' ? { rx: 'free', ry: 'free', rz: 'free' } : j;
|
|
372
|
+
if (!each || typeof each !== 'object') refuse('join', `${whose}'s end ${end} is joined ${said(j)}: 'rigid', 'pinned' (both bending rotations free), 'ball' (all three free), or { rx, ry, rz } each 'rigid', 'free' or a spring in N·mm/rad`, { element: id });
|
|
373
|
+
for (const [key, f] of Object.entries(each)) {
|
|
374
|
+
const d = ['rx', 'ry', 'rz'].indexOf(key);
|
|
375
|
+
if (d < 0) refuse('join', `${whose}'s end ${end} has "${key}": an end's rotations are rx (twist), ry and rz, about the element's own axes`, { element: id });
|
|
376
|
+
if (f === 'rigid' || f === Infinity || f === undefined) continue;
|
|
377
|
+
if (f !== 'free' && !(f >= 0)) refuse('join', `${whose}'s end ${end} has ${key} = ${said(f)}: 'rigid', 'free', or a spring's stiffness in N·mm/rad, 0 or more`, { element: id });
|
|
378
|
+
springs[o + d] = f === 'free' ? 0 : f;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
const R = Float64Array.of(...x, ...y, ...z), raw = elementMatrices(section, material, L, { shear });
|
|
382
|
+
// free to twist at both ends (a strut on two ball joints): it carries no torque, and its own spin is nobody's business
|
|
383
|
+
if (springs[3] === 0 && springs[9] === 0) { springs[3] = springs[9] = Infinity; for (const i of [3, 9]) for (const j of [3, 9]) raw.k[i * 12 + j] = raw.m[i * 12 + j] = 0; }
|
|
384
|
+
const c = condense(raw, springs);
|
|
385
|
+
if (!c) refuse('join', `${whose}'s end joints leave it loose: carry more of its rotations`, { element: id });
|
|
386
|
+
const G = shearModulus(material), Wy = section.Wy ?? (section.cz ? section.Iy / section.cz : undefined), Wz = section.Wz ?? (section.cy ? section.Iz / section.cy : undefined);
|
|
387
|
+
return { id, of: e.of, a, b, L, R, raw, ...c, phi: raw.phi, EA: material.E * section.A, EIy: material.E * section.Iy, EIz: material.E * section.Iz, GAy: shear && section.Asy ? G * section.Asy : 0, GAz: shear && section.Asz ? G * section.Asz : 0,
|
|
388
|
+
A: section.A, Wy, Wz, perLength: (material.density || 0) * 1e-6 * section.A, K: toGlobal(c.k, R) };
|
|
389
|
+
});
|
|
390
|
+
// supports
|
|
391
|
+
const held = new Uint8Array(6 * N), ground = new Float64Array(6 * N), supported = [];
|
|
392
|
+
if (model.supports !== undefined && (typeof model.supports !== 'object' || !model.supports)) refuse('support', `supports is ${said(model.supports)}: give each supported node 'fixed', 'pinned' or { x, y, z, rx, ry, rz }`);
|
|
393
|
+
for (const [name, s] of Object.entries(model.supports || {})) {
|
|
394
|
+
const n = nodeOf(name, 'a support'), each = s === 'fixed' ? Object.fromEntries(FREEDOMS.map((f) => [f, true])) : s === 'pinned' ? { x: true, y: true, z: true } : s;
|
|
395
|
+
if (!each || typeof each !== 'object') refuse('support', `node ${said(name)}'s support is ${said(s)}: 'fixed' (all six freedoms), 'pinned' (its three movements), or { x, y, z, rx, ry, rz } each true (held), false, or a spring to ground in N/mm or N·mm/rad`, { node: name });
|
|
396
|
+
let any = false;
|
|
397
|
+
for (const [key, h] of Object.entries(each)) {
|
|
398
|
+
const d = FREEDOMS.indexOf(key);
|
|
399
|
+
if (d < 0) refuse('support', `node ${said(name)}'s support has "${key}": a node's freedoms are ${FREEDOMS.join(', ')}`, { node: name });
|
|
400
|
+
if (h === true || h === 'fixed' || h === Infinity) { held[6 * n + d] = 1; any = true; } else if (typeof h === 'number' && h > 0) { ground[6 * n + d] = h; any = true; } else if (!(h === false || h === 'free' || h === 0 || h === undefined)) refuse('support', `node ${said(name)}'s support has ${key} = ${said(h)}: true (held), false (free), or a spring to ground of 0 or more`, { node: name });
|
|
401
|
+
}
|
|
402
|
+
if (any) supported.push(n);
|
|
403
|
+
}
|
|
404
|
+
if (!supported.length) refuse('unheld', 'nothing is held: give at least one node a support (supports: { a: \'fixed\' }), or the frame just moves');
|
|
405
|
+
const span = Math.max(...[0, 1, 2].map((c) => Math.max(...at.map((p) => p[c])) - Math.min(...at.map((p) => p[c]))));
|
|
406
|
+
return { ids, at, index, N, els, elementIds, held, ground, supported, nodeOf, span };
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// The loads of one case: forces on every freedom, and each element's spread load in its own axes
|
|
410
|
+
function loadCase(f, { loads = [], gravity, masses = {} }) {
|
|
411
|
+
const { N, els, elementIds, nodeOf } = f, nodal = new Float64Array(6 * N), spread = new Float64Array(3 * els.length), lumped = new Float64Array(N);
|
|
412
|
+
if (!Array.isArray(loads)) refuse('load', `loads is ${said(loads)}: a list of { node, force, moment } and { element, spread }`);
|
|
413
|
+
if (gravity !== undefined && !isVec3(gravity)) refuse('load', `gravity is ${said(gravity)}: a direction and size [x, y, z] in N/kg (m/s²), [0, 0, -9.81] for down`);
|
|
414
|
+
if (!masses || typeof masses !== 'object') refuse('mass', `masses is ${said(masses)}: lumped masses in kg by node, { head: 0.4 }`);
|
|
415
|
+
for (const [name, kg] of Object.entries(masses)) {
|
|
416
|
+
const n = nodeOf(name, 'a lumped mass');
|
|
417
|
+
if (!(kg >= 0) || !Number.isFinite(kg)) refuse('mass', `the lumped mass at node ${said(name)} is ${said(kg)}: a mass in kg, 0 or more`, { node: name });
|
|
418
|
+
lumped[n] += kg;
|
|
419
|
+
if (gravity) for (let c = 0; c < 3; c++) nodal[6 * n + c] += kg * gravity[c];
|
|
420
|
+
}
|
|
421
|
+
loads.forEach((l, i) => {
|
|
422
|
+
if (l && l.node !== undefined) {
|
|
423
|
+
const n = nodeOf(l.node, `load ${i + 1}`);
|
|
424
|
+
for (const [key, o] of [['force', 0], ['moment', 3]]) if (l[key] !== undefined) { if (!isVec3(l[key])) refuse('load', `load ${i + 1}'s ${key} is ${said(l[key])}: [x, y, z] in ${o ? 'N·mm' : 'N'}, in global axes`); for (let c = 0; c < 3; c++) nodal[6 * n + o + c] += l[key][c]; }
|
|
425
|
+
if (l.force === undefined && l.moment === undefined) refuse('load', `load ${i + 1}, at node ${said(l.node)}, has neither a force nor a moment`);
|
|
426
|
+
} else if (l && l.element !== undefined) {
|
|
427
|
+
const e = elementIds.get(String(l.element)) ?? refuse('load', `load ${i + 1} names element ${said(l.element)}, which is not in elements`);
|
|
428
|
+
if (!isVec3(l.spread)) refuse('load', `load ${i + 1}'s spread is ${said(l.spread)}: [x, y, z] in N per mm of the element's length`);
|
|
429
|
+
if (l.axes !== undefined && l.axes !== 'global' && l.axes !== 'local') refuse('load', `load ${i + 1}'s axes is ${said(l.axes)}: 'global' or 'local' (the element's own)`);
|
|
430
|
+
const w = l.axes === 'local' ? l.spread : toLocal3(els[e].R, l.spread);
|
|
431
|
+
for (let c = 0; c < 3; c++) spread[3 * e + c] += w[c];
|
|
432
|
+
} else refuse('load', `load ${i + 1} is ${said(l)}: { node, force, moment } or { element, spread }`);
|
|
433
|
+
});
|
|
434
|
+
if (gravity) els.forEach((el, e) => { const w = toLocal3(el.R, gravity); for (let c = 0; c < 3; c++) spread[3 * e + c] += el.perLength * w[c]; });
|
|
435
|
+
return { nodal, spread, lumped };
|
|
436
|
+
}
|
|
437
|
+
// A spread load's forces at the element's (rigid) ends, in its own axes: w L / 2 each, and moments of w L² / 12
|
|
438
|
+
function spreadEnds(L, wx, wy, wz) {
|
|
439
|
+
const fb = new Float64Array(12), h = L / 2, m = (L * L) / 12;
|
|
440
|
+
fb[0] = fb[6] = wx * h; fb[1] = fb[7] = wy * h; fb[2] = fb[8] = wz * h; fb[5] = wy * m; fb[11] = -wy * m; fb[4] = -wz * m; fb[10] = wz * m;
|
|
441
|
+
return fb;
|
|
442
|
+
}
|
|
443
|
+
const condensedLoad = (el, fb) => { if (!el.T) return fb; const fc = new Float64Array(12); for (let i = 0; i < 12; i++) { const v = fb[i]; if (v) for (let j = 0; j < 12; j++) fc[j] += el.T[i * 12 + j] * v; } return fc; };
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* A frame, read, checked and factorised once, ready for its solves. What is wrong with the model is said here, by
|
|
447
|
+
* name, before any solve: each refusal is an Error with a `code` ("fea.frame." then node, length, section, material,
|
|
448
|
+
* axes, join, support, load, mass, input, unheld or mechanism; buckling adds compression).
|
|
449
|
+
* @param {FrameModel} model
|
|
450
|
+
* @param {{ shear?: boolean }} [o] shear: false ignores the sections' shear areas (Euler-Bernoulli beams)
|
|
451
|
+
* @returns {{
|
|
452
|
+
* statics: (o?: { loads?: FrameLoad[], gravity?: Vec3, masses?: Record<string, number>, samples?: number }) => StaticResult,
|
|
453
|
+
* buckling: (o?: { count?: number, loads?: FrameLoad[], gravity?: Vec3, masses?: Record<string, number> }) => { factors: number[], modes: BucklingMode[], statics: StaticResult },
|
|
454
|
+
* modes: (o?: { count?: number, masses?: Record<string, number> }) => { frequencies: number[], modes: VibrationMode[] },
|
|
455
|
+
* freedoms: number, nodes: string[] }}
|
|
456
|
+
* statics, buckling and modes use the model's own loads, gravity and masses unless given others (another load case
|
|
457
|
+
* on the same factorisation). samples: stations along each element (11). count: how many modes (1 for buckling, 6
|
|
458
|
+
* for vibration; fewer come back if the frame has fewer). freedoms: how many unknowns were solved for.
|
|
459
|
+
*/
|
|
460
|
+
export function frame(model, o = {}) {
|
|
461
|
+
const f = read(model, o), { ids, N, els, held, ground } = f;
|
|
462
|
+
// free freedoms, numbered node by node in an order that keeps the matrix's rows short
|
|
463
|
+
const free = new Int32Array(6 * N).fill(-1), back = [];
|
|
464
|
+
for (const n of ordering(N, els.map((e) => [e.a, e.b]))) for (let d = 0; d < 6; d++) if (!held[6 * n + d]) { free[6 * n + d] = back.length; back.push(6 * n + d); }
|
|
465
|
+
const n = back.length, first = Int32Array.from({ length: n }, (_, i) => i);
|
|
466
|
+
for (const el of els) {
|
|
467
|
+
el.dofs = Int32Array.from({ length: 12 }, (_, i) => free[6 * (i < 6 ? el.a : el.b) + (i % 6)]);
|
|
468
|
+
let lo = n;
|
|
469
|
+
for (const d of el.dofs) if (d >= 0 && d < lo) lo = d;
|
|
470
|
+
for (const d of el.dofs) if (d >= 0 && lo < first[d]) first[d] = lo;
|
|
471
|
+
}
|
|
472
|
+
const assemble = (matrix, scale) => {
|
|
473
|
+
const P = profile(first), { start, v } = P;
|
|
474
|
+
els.forEach((el, e) => { const s = scale ? scale[e] : 1, G = matrix(el), dofs = el.dofs; if (s) for (let i = 0; i < 12; i++) { const di = dofs[i]; if (di >= 0) for (let j = 0; j < 12; j++) { const dj = dofs[j]; if (dj >= 0 && dj <= di) v[start[di] + dj - first[di]] += s * G[i * 12 + j]; } } });
|
|
475
|
+
return P;
|
|
476
|
+
};
|
|
477
|
+
const diagonal = (P, i) => P.start[i] + i - P.first[i];
|
|
478
|
+
const K = assemble((el) => el.K);
|
|
479
|
+
for (let i = 0; i < n; i++) K.v[diagonal(K, i)] += ground[back[i]];
|
|
480
|
+
// A rotation of a node that no element end there carries (every end at it pinned that way) leaves the matrix
|
|
481
|
+
// singular though nothing is wrong with the frame: nothing turns the node, and nothing depends on it. Such a
|
|
482
|
+
// rotation is held still (and reported as 0); a moment put on it is refused, as nothing would carry it.
|
|
483
|
+
const idle = [], turn = new Float64Array(9 * N), stiff = new Float64Array(N);
|
|
484
|
+
for (const el of els) for (const [p, o] of [[el.a, 3], [el.b, 9]]) for (let i = 0; i < 3; i++) { stiff[p] = Math.max(stiff[p], el.raw.k[(o + i) * 13]); for (let j = 0; j < 3; j++) turn[9 * p + 3 * i + j] += el.K[(o + i) * 12 + o + j]; }
|
|
485
|
+
for (let p = 0; p < N; p++) {
|
|
486
|
+
const fr = [3, 4, 5].filter((d) => !held[6 * p + d]), m = fr.length;
|
|
487
|
+
if (!m || !(stiff[p] > 0)) continue;
|
|
488
|
+
const S = Float64Array.from({ length: m * m }, (_, q) => { const i = Math.floor(q / m), j = q % m; return turn[9 * p + 3 * (fr[i] - 3) + fr[j] - 3] + (i === j ? ground[6 * p + fr[i]] : 0); }), { values, V } = jacobi(S, m);
|
|
489
|
+
for (let c = 0; c < m; c++) if (values[c] < 1e-9 * stiff[p]) {
|
|
490
|
+
const v = [0, 0, 0];
|
|
491
|
+
fr.forEach((d, i) => { v[d - 3] = V[i * m + c]; });
|
|
492
|
+
idle.push({ node: p, v });
|
|
493
|
+
for (const di of fr) for (const dj of fr) { const a = free[6 * p + di], b = free[6 * p + dj]; if (b <= a) K.v[K.start[a] + b - first[a]] += stiff[p] * v[di - 3] * v[dj - 3]; }
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
const factor = n ? factorise(K) : { solve: (b) => new Float64Array(0), loose: [] };
|
|
497
|
+
if (factor.loose.length) {
|
|
498
|
+
const loose = factor.loose.map((i) => ({ node: ids[Math.floor(back[i] / 6)], freedom: FREEDOMS[back[i] % 6] }));
|
|
499
|
+
const by = new Map(); for (const l of loose) by.set(l.node, [...(by.get(l.node) || []), l.freedom]);
|
|
500
|
+
const list = [...by].slice(0, 8).map(([node, fr]) => `node ${said(node)} (${fr.join(', ')})`).join('; ');
|
|
501
|
+
refuse('mechanism', `the frame is a mechanism: it can still move with nothing resisting, at ${list}${by.size > 8 ? `, and ${by.size - 8} more nodes` : ''}. Hold those freedoms with a support, or join the elements there more stiffly (a pinned end frees its bending rotations and still carries twist, so a strut pinned at both ends can spin: make one end 'ball')`, { loose });
|
|
502
|
+
}
|
|
503
|
+
const shapeOf = (x) => { // a vector over the free freedoms as each node's six: its largest movement 1, and that one positive
|
|
504
|
+
const u = new Float64Array(6 * N);
|
|
505
|
+
for (let i = 0; i < n; i++) u[back[i]] = x[i];
|
|
506
|
+
let most = 0, lead = 0;
|
|
507
|
+
for (let p = 0; p < N; p++) most = Math.max(most, Math.hypot(u[6 * p], u[6 * p + 1], u[6 * p + 2]));
|
|
508
|
+
const moves = most > 0; // (a shape that only turns, such as a shaft's twist, is scaled by its largest rotation)
|
|
509
|
+
for (let i = 0; i < 6 * N; i++) if ((!moves || i % 6 < 3) && Math.abs(u[i]) > Math.abs(lead) * (1 + 1e-9)) lead = u[i];
|
|
510
|
+
const scale = (lead < 0 ? -1 : 1) / ((moves ? most : Math.abs(lead)) || 1);
|
|
511
|
+
return Object.fromEntries(ids.map((id, p) => [id, /** @type {Six} */ (Array.from(u.subarray(6 * p, 6 * p + 6), (v) => v * scale))]));
|
|
512
|
+
};
|
|
513
|
+
const caseOf = (c = {}) => ({ loads: c.loads ?? model.loads, gravity: c.gravity ?? model.gravity, masses: c.masses ?? model.masses });
|
|
514
|
+
|
|
515
|
+
function statics(c = {}) {
|
|
516
|
+
const samples = c.samples ?? 11;
|
|
517
|
+
if (!(Number.isInteger(samples) && samples >= 2)) refuse('input', `samples is ${said(samples)}: a whole number of stations along each element, 2 or more`);
|
|
518
|
+
const { nodal, spread } = loadCase(f, caseOf(c)), p = Float64Array.from(nodal);
|
|
519
|
+
for (const { node, v } of idle) { const m = [nodal[6 * node + 3], nodal[6 * node + 4], nodal[6 * node + 5]]; if (Math.abs(m[0] * v[0] + m[1] * v[1] + m[2] * v[2]) > 1e-9 * Math.hypot(...m)) refuse('load', `a moment is put on node ${said(ids[node])} about a direction no element there carries (every end at it is pinned that way): join an end to it rigidly or through a spring, or hold that rotation with a support`, { node: ids[node] }); }
|
|
520
|
+
const ends = els.map((el, e) => {
|
|
521
|
+
const fb = spreadEnds(el.L, spread[3 * e], spread[3 * e + 1], spread[3 * e + 2]), fc = condensedLoad(el, fb);
|
|
522
|
+
for (let q = 0; q < 4; q++) { const g = toGlobal3(el.R, fc, 3 * q), at = 6 * (q < 2 ? el.a : el.b) + 3 * (q % 2); for (let k = 0; k < 3; k++) p[at + k] += g[k]; }
|
|
523
|
+
return { fb, fc };
|
|
524
|
+
});
|
|
525
|
+
const x = factor.solve(Float64Array.from(back, (d) => p[d])), u = new Float64Array(6 * N), residual = Float64Array.from(nodal, (v) => -v);
|
|
526
|
+
for (let i = 0; i < n; i++) u[back[i]] = x[i];
|
|
527
|
+
for (const { node, v } of idle) { const o = 6 * node + 3, along = u[o] * v[0] + u[o + 1] * v[1] + u[o + 2] * v[2]; for (let c = 0; c < 3; c++) u[o + c] = held[o + c] || Math.abs(v[c]) === 1 ? 0 : u[o + c] - along * v[c]; }
|
|
528
|
+
let most = 0, mostAt = 0;
|
|
529
|
+
for (let q = 0; q < N; q++) { const d = Math.hypot(u[6 * q], u[6 * q + 1], u[6 * q + 2]); if (d > most) { most = d; mostAt = q; } }
|
|
530
|
+
if (!x.every(Number.isFinite) || most > 1e6 * (f.span || 1)) refuse('mechanism', 'the frame is barely held: under this load it moves many times its own size. Add supports, or join its elements more stiffly', { loose: [] });
|
|
531
|
+
const elements = els.map((el, e) => {
|
|
532
|
+
const { L, R } = el, { fb, fc } = ends[e], w = [spread[3 * e], spread[3 * e + 1], spread[3 * e + 2]], d = new Float64Array(12), F = new Float64Array(12);
|
|
533
|
+
for (let q = 0; q < 4; q++) d.set(toLocal3(R, u, 6 * (q < 2 ? el.a : el.b) + 3 * (q % 2)), 3 * q);
|
|
534
|
+
for (let i = 0; i < 12; i++) { let s = -fc[i]; for (let j = 0; j < 12; j++) s += el.k[i * 12 + j] * d[j]; F[i] = s; }
|
|
535
|
+
for (let q = 0; q < 4; q++) { const g = toGlobal3(R, F, 3 * q), at = 6 * (q < 2 ? el.a : el.b) + 3 * (q % 2); for (let k = 0; k < 3; k++) residual[at + k] += g[k]; }
|
|
536
|
+
// the beam's own end freedoms, behind any springs: d_beam = T d + H f_s
|
|
537
|
+
let db = d;
|
|
538
|
+
if (el.T) { db = new Float64Array(12); for (let i = 0; i < 12; i++) { let s = 0; for (let j = 0; j < 12; j++) s += el.T[i * 12 + j] * d[j]; db[i] = s; } el.s.forEach((sp, a) => { el.s.forEach((sq, b) => { db[sp] += el.H[a * el.s.length + b] * fb[sq]; }); }); }
|
|
539
|
+
// N, V and M as polynomials in x, from the forces on end a and the spread load between
|
|
540
|
+
const poly = { N: [-F[0], -w[0], 0], Vy: [-F[1], -w[1], 0], Vz: [-F[2], -w[2], 0], T: [-F[3], 0, 0], My: [-F[4], -F[2], -w[2] / 2], Mz: [-F[5], F[1], w[1] / 2] };
|
|
541
|
+
const value = (c, s) => c[0] + s * (c[1] + s * c[2]), inside = (s) => s > 1e-9 * L && s < L * (1 - 1e-9);
|
|
542
|
+
const xs = Array.from({ length: samples }, (_, i) => (L * i) / (samples - 1));
|
|
543
|
+
for (const c of [poly.My, poly.Mz]) if (c[2] !== 0 && inside(-c[1] / (2 * c[2]))) xs.push(-c[1] / (2 * c[2]));
|
|
544
|
+
xs.sort((a, b) => a - b);
|
|
545
|
+
const along = { x: xs, N: [], Vy: [], Vz: [], T: [], My: [], Mz: [], d: [] }, peak = {};
|
|
546
|
+
for (const key of ['N', 'Vy', 'Vz', 'T', 'My', 'Mz']) { along[key] = xs.map((s) => value(poly[key], s)); let b = 0; along[key].forEach((v, i) => { if (Math.abs(v) > Math.abs(along[key][b])) b = i; }); peak[key] = { value: along[key][b], x: xs[b] }; }
|
|
547
|
+
along.d = xs.map((s) => {
|
|
548
|
+
const xi = s / L, ny = shape(xi, L, el.phi[0]).N, nz = shape(xi, L, el.phi[1]).N, bend = (s * s * (L - s) * (L - s)) / 24, sh = (s * (L - s)) / 2;
|
|
549
|
+
const local = [(1 - xi) * db[0] + xi * db[6] + (w[0] * sh) / el.EA,
|
|
550
|
+
ny[0] * db[1] + ny[1] * db[5] + ny[2] * db[7] + ny[3] * db[11] + (w[1] * bend) / el.EIz + (el.GAy ? (w[1] * sh) / el.GAy : 0),
|
|
551
|
+
nz[0] * db[2] - nz[1] * db[4] + nz[2] * db[8] - nz[3] * db[10] + (w[2] * bend) / el.EIy + (el.GAz ? (w[2] * sh) / el.GAz : 0)];
|
|
552
|
+
return /** @type {Vec3} */ (toGlobal3(R, local));
|
|
553
|
+
});
|
|
554
|
+
const result = { id: el.id, ...(el.of !== undefined ? { of: el.of } : {}), length: L, axes: /** @type {[Vec3, Vec3, Vec3]} */ ([0, 3, 6].map((o) => Array.from(R.subarray(o, o + 3)))),
|
|
555
|
+
ends: { a: /** @type {Six} */ (Array.from(F.subarray(0, 6))), b: /** @type {Six} */ (Array.from(F.subarray(6, 12))) }, along, peak: /** @type {ElementResult['peak']} */ (peak), axial: value(poly.N, L / 2) };
|
|
556
|
+
if (el.Wy && el.Wz) {
|
|
557
|
+
// |N|/A + |My|/Wy + |Mz|/Wz is a quadratic wherever no term changes sign: its peak is at an end, where a
|
|
558
|
+
// moment peaks (both already stations), or where one of the signed sums is level
|
|
559
|
+
const terms = [[poly.N, 1 / el.A], [poly.My, 1 / el.Wy], [poly.Mz, 1 / el.Wz]], at = (s) => terms.reduce((sum, [c, k]) => sum + Math.abs(value(c, s)) * k, 0), tries = [...xs];
|
|
560
|
+
for (const sy of [1, -1]) for (const sz of [1, -1]) { const c1 = poly.N[1] / el.A + (sy * poly.My[1]) / el.Wy + (sz * poly.Mz[1]) / el.Wz, c2 = (sy * poly.My[2]) / el.Wy + (sz * poly.Mz[2]) / el.Wz; if (c2 !== 0 && inside(-c1 / (2 * c2))) tries.push(-c1 / (2 * c2)); }
|
|
561
|
+
let best = 0, bending = 0;
|
|
562
|
+
for (const s of tries) { if (at(s) > at(best)) best = s; bending = Math.max(bending, Math.abs(value(poly.My, s)) / el.Wy + Math.abs(value(poly.Mz, s)) / el.Wz); }
|
|
563
|
+
result.stress = { peak: at(best), bending, axial: value(poly.N, best) / el.A, x: best };
|
|
564
|
+
}
|
|
565
|
+
return result;
|
|
566
|
+
});
|
|
567
|
+
const six = (v, q) => /** @type {Six} */ (Array.from(v.subarray(6 * q, 6 * q + 6)));
|
|
568
|
+
return { displacements: Object.fromEntries(ids.map((id, q) => [id, six(u, q)])), reactions: Object.fromEntries(f.supported.map((q) => [ids[q], six(residual, q).map((v, d) => (held[6 * q + d] || ground[6 * q + d] ? v : 0))])),
|
|
569
|
+
elements, deflection: { most, node: ids[mostAt] } };
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
function buckling(c = {}) {
|
|
573
|
+
const count = c.count ?? 1;
|
|
574
|
+
if (!(Number.isInteger(count) && count >= 1)) refuse('input', `count is ${said(count)}: how many buckling modes, a whole number from 1`);
|
|
575
|
+
const s = statics(c), axial = s.elements.map((e) => e.axial), size = Math.max(...axial.map(Math.abs));
|
|
576
|
+
if (!axial.some((a) => a < -1e-9 * Math.max(size, 1e-300))) refuse('compression', 'no element is in compression under these loads, so nothing can buckle: buckling needs a load that squeezes at least one element along its length');
|
|
577
|
+
const B = assemble((el) => toGlobal(el.kg, el.R), axial.map((a) => -a)), block = 2 * count + 16;
|
|
578
|
+
// K φ = λ B φ, B = −Kg, for the smallest λ above 0. B is indefinite: with the loads reversed, whatever is in
|
|
579
|
+
// tension would buckle, and those (negative) factors can be far smaller in size and crowd the iteration. So it
|
|
580
|
+
// is shifted to just under the first factor: K − σ B has a Cholesky factor exactly when σ is under it (and over
|
|
581
|
+
// the first negative one), which brackets it between a σ that factorises and one that does not, and the
|
|
582
|
+
// iteration on (K − σ B)⁻¹ B then finds 1 / (λ − σ), where the first factor leads every negative one at least
|
|
583
|
+
// fourfold. (Factors more than about twice the first still have to outlast the negative ones, by a wider block:
|
|
584
|
+
// the first is always quick, the rest only when little is in tension.)
|
|
585
|
+
let high = Infinity, low = 0, shifted = factor;
|
|
586
|
+
for (let i = 0; i < n; i++) { const b = B.v[diagonal(B, i)]; if (b > 0) high = Math.min(high, K.v[diagonal(K, i)] / b); } // one freedom moved alone: no frame buckles later than that
|
|
587
|
+
const rough = lowest(factor.solve, B, 1, block, 1e-3, 4);
|
|
588
|
+
if (rough.values.length) high = Math.min(high, 1 / rough.values[0]); // (a Ritz value is never under the true one)
|
|
589
|
+
if (Number.isFinite(high)) for (let tries = 0, sigma = 0.9 * high; tries < 60 && low < 0.8 * high; tries++, sigma = low ? (low + high) / 2 : high / 2) {
|
|
590
|
+
const A = profile(first);
|
|
591
|
+
for (let i = 0; i < A.v.length; i++) A.v[i] = K.v[i] - sigma * B.v[i];
|
|
592
|
+
const fa = factorise(A);
|
|
593
|
+
if (fa.loose.length) high = sigma; else { low = sigma; shifted = fa; }
|
|
594
|
+
}
|
|
595
|
+
const { values, vectors } = lowest(shifted.solve, B, count, block);
|
|
596
|
+
const modes = values.map((mu, i) => ({ factor: low + 1 / mu, shape: shapeOf(vectors[i]) }));
|
|
597
|
+
return { factors: modes.map((m) => m.factor), modes, statics: s };
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function modes(c = {}) {
|
|
601
|
+
const count = c.count ?? 6;
|
|
602
|
+
if (!(Number.isInteger(count) && count >= 1)) refuse('input', `count is ${said(count)}: how many modes, a whole number from 1`);
|
|
603
|
+
const { lumped } = loadCase(f, { masses: c.masses ?? model.masses });
|
|
604
|
+
if (!els.some((el) => el.perLength > 0) && !lumped.some((m) => m > 0)) refuse('mass', 'nothing in the frame has mass, so it has no frequencies: give the materials a density (g/cm³), or lump masses (kg) at nodes');
|
|
605
|
+
const M = assemble((el) => toGlobal(el.m, el.R));
|
|
606
|
+
for (let i = 0; i < n; i++) if (back[i] % 6 < 3) M.v[diagonal(M, i)] += lumped[Math.floor(back[i] / 6)];
|
|
607
|
+
const { values, vectors } = lowest(factor.solve, M, count);
|
|
608
|
+
// K in N/mm against M in kg: ω² = 1000 / μ (a newton is a kg·m/s², and the lengths are mm)
|
|
609
|
+
const found = values.map((mu, i) => ({ frequency: Math.sqrt(1000 / mu) / (2 * Math.PI), shape: shapeOf(vectors[i]) }));
|
|
610
|
+
return { frequencies: found.map((m) => m.frequency), modes: found };
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
return { statics, buckling, modes, freedoms: n, nodes: ids };
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* How far a frame moves under its loads, and what each element carries. The same as `frame(model).statics()`.
|
|
618
|
+
* @param {FrameModel} model @param {{ shear?: boolean, samples?: number }} [o]
|
|
619
|
+
* @returns {StaticResult}
|
|
620
|
+
*/
|
|
621
|
+
export const statics = (model, o = {}) => frame(model, o).statics(o);
|
|
622
|
+
/**
|
|
623
|
+
* The factors by which a frame's loads must grow to buckle it, lowest first, and the shapes it buckles into: linear
|
|
624
|
+
* buckling, from the geometric stiffness of each element's axial force in the static solve. The first factor is
|
|
625
|
+
* always quick to find; asking for more costs more where slender members are in tension.
|
|
626
|
+
* @param {FrameModel} model @param {{ shear?: boolean, count?: number }} [o] count: how many modes (1)
|
|
627
|
+
* @returns {{ factors: number[], modes: BucklingMode[], statics: StaticResult }}
|
|
628
|
+
*/
|
|
629
|
+
export const buckling = (model, o = {}) => frame(model, o).buckling(o);
|
|
630
|
+
/**
|
|
631
|
+
* A frame's lowest natural frequencies, in Hz, and their shapes, from the elements' consistent mass and the lumped masses.
|
|
632
|
+
* @param {FrameModel} model @param {{ shear?: boolean, count?: number }} [o] count: how many modes (6)
|
|
633
|
+
* @returns {{ frequencies: number[], modes: VibrationMode[] }}
|
|
634
|
+
*/
|
|
635
|
+
export const modes = (model, o = {}) => frame(model, o).modes(o);
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* The same frame with every element cut into `pieces` equal lengths, for buckling and modes: one element cannot bow
|
|
639
|
+
* between its own ends, so a column's Euler load is 22% out with one element, 0.8% with two and 0.05% with four. New
|
|
640
|
+
* nodes are named "<element>/<k>" (or added to the end of a list of nodes), new elements "<element>/<k>" with `of`
|
|
641
|
+
* naming the element they came from; end joints stay at the member's own ends, and spread loads follow their pieces.
|
|
642
|
+
* @param {FrameModel} model @param {number} [pieces] 4
|
|
643
|
+
* @returns {FrameModel}
|
|
644
|
+
*/
|
|
645
|
+
export function subdivide(model, pieces = 4) {
|
|
646
|
+
if (!(Number.isInteger(pieces) && pieces >= 1)) refuse('input', `pieces is ${said(pieces)}: a whole number of pieces for each element, 1 or more`);
|
|
647
|
+
if (!model || typeof model !== 'object' || !model.nodes || !Array.isArray(model.elements)) refuse('input', 'a frame is { nodes, elements, supports, loads }');
|
|
648
|
+
const list = Array.isArray(model.nodes), nodes = list ? [...model.nodes] : { ...model.nodes }, parts = new Map();
|
|
649
|
+
const elements = model.elements.flatMap((e, i) => {
|
|
650
|
+
const id = e.id ?? i, pa = model.nodes[e.a], pb = model.nodes[e.b];
|
|
651
|
+
for (const [end, p] of [[e.a, pa], [e.b, pb]]) if (!isVec3(p)) refuse('node', `element ${said(id)} names node ${said(end)}, which is not in nodes`, { node: end });
|
|
652
|
+
const mid = Array.from({ length: pieces - 1 }, (_, k) => { const t = (k + 1) / pieces, p = /** @type {Vec3} */ (pa.map((v, c) => v + t * (pb[c] - v))); if (list) return nodes.push(p) - 1; nodes[`${id}/${k + 1}`] = p; return `${id}/${k + 1}`; });
|
|
653
|
+
const made = Array.from({ length: pieces }, (_, k) => ({ ...e, id: pieces === 1 ? id : `${id}/${k + 1}`, of: e.of ?? id, a: k ? mid[k - 1] : e.a, b: k < pieces - 1 ? mid[k] : e.b,
|
|
654
|
+
ends: { a: k ? 'rigid' : e.ends?.a ?? 'rigid', b: k < pieces - 1 ? 'rigid' : e.ends?.b ?? 'rigid' } }));
|
|
655
|
+
parts.set(String(id), made.map((m) => m.id));
|
|
656
|
+
return made;
|
|
657
|
+
});
|
|
658
|
+
const loads = (model.loads || []).flatMap((l) => (l && 'element' in l && parts.has(String(l.element)) ? parts.get(String(l.element)).map((element) => ({ ...l, element })) : [l]));
|
|
659
|
+
return { ...model, nodes, elements, ...(model.loads ? { loads } : {}) };
|
|
660
|
+
}
|