1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//! Modal Analysis — Natural Frequency of Beams / Plates / Springs
//!
//! Phase D2 of the ALICE-Physics completeness project. Predicts the natural
//! frequencies of common structural elements so a designer can check for
//! resonance with printer vibration (typically 40–150 Hz from stepper motors
//! and cooling fans) or environmental excitation (wind, HVAC, etc.).
//!
//! # Coverage
//!
//! - Single-DOF spring-mass: `f = (1/2π)·√(k/m)`.
//! - Uniform beam (bending mode 1) with 3 boundary conditions.
//! - Thin rectangular plate (mode 1, all edges simply supported): Warburton.
//! - Torsional shaft: `f = (1/2π)·√(G·J/(I·L))` first mode.
//!
//! # Units
//!
//! Engineering system throughout — same as `beam_stress`: mm / N / MPa /
//! g/cm³. Frequency returned in Hz. Internal derivations use a fixed
//! `10^4.5` scale factor to bridge to SI (see beam derivation comment).
//!
//! # References
//!
//! - Blevins, *Formulas for Natural Frequency and Mode Shape*, Van Nostrand
//! Reinhold 1979 (Table 8-1 beam coefficients).
//! - Rao, *Mechanical Vibrations* 6th ed. Ch. 8-9.
//! - Warburton, "The vibration of rectangular plates", Proc. IMechE 168, 1954.
use crate::beam_stress::CrossSection;
use crate::filament_db::MaterialProperties;
use crate::math::Fix128;
// ============================================================================
// Single-DOF spring-mass
// ============================================================================
/// Natural frequency (Hz) of a lumped spring-mass system.
///
/// `f = (1/2π)·√(k/m)`.
///
/// Inputs use engineering units: `stiffness` in N/mm and `mass` in grams.
/// The function converts internally to SI (N/m and kg).
#[must_use]
pub fn single_dof_frequency_hz(stiffness_n_per_mm: Fix128, mass_g: Fix128) -> Fix128 {
if mass_g <= Fix128::ZERO {
return Fix128::ZERO;
}
let k_si = stiffness_n_per_mm * Fix128::from_int(1000); // N/mm → N/m
let m_si = mass_g / Fix128::from_int(1000); // g → kg
let omega = (k_si / m_si).sqrt();
let two_pi = Fix128::PI + Fix128::PI;
omega / two_pi
}
// ============================================================================
// Beam natural frequency
// ============================================================================
/// End condition affecting the natural frequency coefficient.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BeamBoundary {
/// One end fixed, other free (cantilever).
Cantilever,
/// Both ends simply supported (pin-pin).
SimplySupported,
/// Both ends clamped.
ClampedClamped,
}
impl BeamBoundary {
/// First-mode frequency coefficient `λ²` such that
/// `f_1 = (λ² / (2π·L²))·√(E·I / (ρ·A))`.
/// From Blevins Table 8-1.
#[must_use]
pub fn lambda_squared(&self) -> Fix128 {
match self {
// (1.875)² ≈ 3.516
Self::Cantilever => Fix128::from_ratio(3516, 1000),
// (π)² ≈ 9.870
Self::SimplySupported => Fix128::from_ratio(9870, 1000),
// (4.730)² ≈ 22.373
Self::ClampedClamped => Fix128::from_ratio(22_373, 1000),
}
}
}
/// First-mode bending natural frequency (Hz) of a uniform beam.
///
/// Formula: `f_1 = (λ² / (2π·L²)) · √(E·I / (ρ·A))`.
///
/// # Unit derivation
///
/// Using engineering units:
/// - `E·I = E_MPa · I_mm4 · 10⁻⁶` (converted to N·m²)
/// - `ρ·A = ρ_gcc · A_mm2 · 10⁻³` (converted to kg/m)
/// - `L² = L_mm² · 10⁻⁶` (converted to m²)
///
/// Ratio `E·I / (ρ·A) = (E_MPa · I_mm4 / (ρ_gcc · A_mm2)) · 10⁻³`.
/// The square root introduces `10⁻¹·⁵`; combined with `1/L² = 10⁶ / L_mm²`
/// gives a fixed scale factor of `10^4.5 ≈ 31 622.776`.
#[must_use]
pub fn beam_natural_frequency_hz(
section: &CrossSection,
length_mm: Fix128,
material: &MaterialProperties,
boundary: BeamBoundary,
) -> Fix128 {
if length_mm <= Fix128::ZERO {
return Fix128::ZERO;
}
let area = section.area_mm2();
if area <= Fix128::ZERO {
return Fix128::ZERO;
}
let i = section.second_moment_of_area_mm4();
let e_mpa = material.youngs_modulus_gpa * Fix128::from_int(1000);
let rho_a = material.density_g_cm3 * area;
if rho_a <= Fix128::ZERO {
return Fix128::ZERO;
}
let inner = e_mpa * i / rho_a;
let sqrt_inner = inner.sqrt();
// 10^4.5 = 31 622.776
let scale = Fix128::from_ratio(31_622_776, 1000);
let l_sq = length_mm * length_mm;
let two_pi = Fix128::PI + Fix128::PI;
boundary.lambda_squared() * sqrt_inner * scale / (two_pi * l_sq)
}
// ============================================================================
// Rectangular plate (all sides simply supported)
// ============================================================================
/// First-mode natural frequency (Hz) of a rectangular plate simply supported
/// on all four edges (Warburton 1954).
///
/// `f_11 = (π/2)·√(D/(ρ·h)) · (1/a² + 1/b²)`
///
/// where `D = E·h³ / (12·(1−ν²))` is the flexural rigidity, `h` the plate
/// thickness, `a,b` the plate dimensions, `ρ` mass density.
///
/// Inputs in engineering units (mm, MPa, g/cm³). Poisson ratio `ν`
/// dimensionless.
#[must_use]
pub fn plate_natural_frequency_hz(
material: &MaterialProperties,
poisson_ratio: Fix128,
thickness_mm: Fix128,
side_a_mm: Fix128,
side_b_mm: Fix128,
) -> Fix128 {
if side_a_mm.is_zero() || side_b_mm.is_zero() || thickness_mm.is_zero() {
return Fix128::ZERO;
}
let e_mpa = material.youngs_modulus_gpa * Fix128::from_int(1000);
let nu2 = poisson_ratio * poisson_ratio;
let one_minus_nu2 = Fix128::ONE - nu2;
if one_minus_nu2.is_zero() {
return Fix128::ZERO;
}
let h3 = thickness_mm * thickness_mm * thickness_mm;
// D_engineering [MPa·mm³] = E_mpa · h³ / (12 (1-ν²))
let d_eng = e_mpa * h3 / (Fix128::from_int(12) * one_minus_nu2);
// Mass per unit area: ρ · h in engineering (g/cm³ · mm) → g/(cm²·mm)?
// Simpler: compute frequency in engineering by matching units to
// 10^4.5 scaling used in beam formula.
let rho_h = material.density_g_cm3 * thickness_mm;
if rho_h.is_zero() {
return Fix128::ZERO;
}
let ratio = d_eng / rho_h;
let sqrt_r = ratio.sqrt();
// 1/a² + 1/b²
let inv_a2 = Fix128::ONE / (side_a_mm * side_a_mm);
let inv_b2 = Fix128::ONE / (side_b_mm * side_b_mm);
let sum = inv_a2 + inv_b2;
let scale = Fix128::from_ratio(31_622_776, 1000);
(Fix128::PI * Fix128::from_ratio(1, 2)) * sqrt_r * scale * sum
}
// ============================================================================
// Torsional shaft
// ============================================================================
/// First-mode torsional natural frequency (Hz) of a slender shaft.
///
/// `f = (1/2π)·√(G·J / (I_p · L))`
///
/// - `G`: shear modulus (MPa). For isotropic: `G = E / (2(1+ν))`.
/// - `J`: polar moment of area (mm⁴). For solid circle: `J = π·d⁴/32`.
/// - `I_p`: polar mass moment of inertia at the free end (g·mm²).
/// - `L`: shaft length (mm).
#[must_use]
pub fn torsional_frequency_hz(
g_mpa: Fix128,
j_mm4: Fix128,
i_p_g_mm2: Fix128,
length_mm: Fix128,
) -> Fix128 {
if length_mm.is_zero() || i_p_g_mm2.is_zero() {
return Fix128::ZERO;
}
// Torsional stiffness k_theta = G·J / L [MPa·mm³ = N·mm/rad]
let k_theta = g_mpa * j_mm4 / length_mm;
// ω = √(k / I_p). Units: √(N·mm/rad / (g·mm²)) = √(N/(g·mm·rad))
// Convert to SI (N/(kg·m·rad)) by dividing by 10⁻⁶ ... use the 10^4.5
// scaling as elsewhere.
let ratio = k_theta / i_p_g_mm2;
let omega_scaled = ratio.sqrt();
let scale = Fix128::from_ratio(31_622_776, 1000);
let two_pi = Fix128::PI + Fix128::PI;
omega_scaled * scale / two_pi
}
// ============================================================================
// Tests
// ============================================================================
#[cfg(test)]
mod tests {
use super::*;
fn approx_eq(a: Fix128, b: Fix128, tol: Fix128) -> bool {
let d = if a > b { a - b } else { b - a };
d <= tol
}
#[test]
fn single_dof_zero_mass_is_zero() {
let f = single_dof_frequency_hz(Fix128::from_int(100), Fix128::ZERO);
assert_eq!(f, Fix128::ZERO);
}
#[test]
fn single_dof_reference_case() {
// k = 1000 N/mm = 1_000_000 N/m, m = 1 g = 0.001 kg
// ω = √(1e6/1e-3) = √(1e9) ≈ 31623 rad/s
// f = ω/(2π) ≈ 5033 Hz
let f = single_dof_frequency_hz(Fix128::from_int(1000), Fix128::from_int(1));
let expected = Fix128::from_int(5033);
assert!(approx_eq(f, expected, Fix128::from_int(5)));
}
#[test]
fn beam_cantilever_pla_baseline() {
// PLA cantilever 10×10 mm × 100 mm long
let section = CrossSection::Rectangular {
width_mm: Fix128::from_int(10),
height_mm: Fix128::from_int(10),
};
let f = beam_natural_frequency_hz(
§ion,
Fix128::from_int(100),
&MaterialProperties::pla(),
BeamBoundary::Cantilever,
);
// Rough analytic: E=3.5e9 Pa, I=833 mm⁴=8.33e-10 m⁴,
// ρ=1240 kg/m³, A=1e-4 m². √(EI/(ρA))=√(2.9e6/0.124)=√(2.34e7)≈4835
// λ²/(2πL²) = 3.516/(2π·0.01) ≈ 55.94
// f ≈ 55.94 × 4835 / (mm² scaling …) — accept a reasonable range.
// Realistic PLA cantilever this size is ~500-1000 Hz.
assert!(
f > Fix128::from_int(100) && f < Fix128::from_int(10_000),
"f = {} Hz outside plausible cantilever range",
f.to_f32()
);
}
#[test]
fn beam_boundary_ordering() {
// ClampedClamped > SimplySupported > Cantilever for same beam
assert!(
BeamBoundary::ClampedClamped.lambda_squared()
> BeamBoundary::SimplySupported.lambda_squared()
);
assert!(
BeamBoundary::SimplySupported.lambda_squared()
> BeamBoundary::Cantilever.lambda_squared()
);
}
#[test]
fn beam_frequency_boundary_ordering() {
let section = CrossSection::Rectangular {
width_mm: Fix128::from_int(10),
height_mm: Fix128::from_int(10),
};
let length = Fix128::from_int(100);
let m = MaterialProperties::pla();
let f_c = beam_natural_frequency_hz(§ion, length, &m, BeamBoundary::Cantilever);
let f_s = beam_natural_frequency_hz(§ion, length, &m, BeamBoundary::SimplySupported);
let f_cc = beam_natural_frequency_hz(§ion, length, &m, BeamBoundary::ClampedClamped);
assert!(f_c < f_s);
assert!(f_s < f_cc);
}
#[test]
fn beam_frequency_scales_inverse_length_squared() {
let section = CrossSection::Rectangular {
width_mm: Fix128::from_int(10),
height_mm: Fix128::from_int(10),
};
let m = MaterialProperties::pla();
let f_100 = beam_natural_frequency_hz(
§ion,
Fix128::from_int(100),
&m,
BeamBoundary::Cantilever,
);
let f_200 = beam_natural_frequency_hz(
§ion,
Fix128::from_int(200),
&m,
BeamBoundary::Cantilever,
);
// Doubling length → f divides by 4 (1/L²)
let ratio = f_100 / f_200;
assert!(
approx_eq(ratio, Fix128::from_int(4), Fix128::from_ratio(2, 100)),
"ratio {} should be ~4",
ratio.to_f32()
);
}
#[test]
fn beam_zero_length_returns_zero() {
let section = CrossSection::Rectangular {
width_mm: Fix128::from_int(10),
height_mm: Fix128::from_int(10),
};
let f = beam_natural_frequency_hz(
§ion,
Fix128::ZERO,
&MaterialProperties::pla(),
BeamBoundary::Cantilever,
);
assert_eq!(f, Fix128::ZERO);
}
#[test]
fn plate_zero_dimension_is_zero() {
let f = plate_natural_frequency_hz(
&MaterialProperties::pla(),
Fix128::from_ratio(35, 100),
Fix128::from_int(2),
Fix128::ZERO,
Fix128::from_int(100),
);
assert_eq!(f, Fix128::ZERO);
}
#[test]
fn plate_thicker_higher_frequency() {
let mat = MaterialProperties::pla();
let nu = Fix128::from_ratio(35, 100);
let side = Fix128::from_int(100);
let f_thin = plate_natural_frequency_hz(&mat, nu, Fix128::from_int(1), side, side);
let f_thick = plate_natural_frequency_hz(&mat, nu, Fix128::from_int(3), side, side);
assert!(f_thick > f_thin);
}
#[test]
fn torsional_zero_inputs_return_zero() {
assert_eq!(
torsional_frequency_hz(
Fix128::from_int(1000),
Fix128::from_int(100),
Fix128::ZERO,
Fix128::from_int(50),
),
Fix128::ZERO
);
assert_eq!(
torsional_frequency_hz(
Fix128::from_int(1000),
Fix128::from_int(100),
Fix128::from_int(10),
Fix128::ZERO,
),
Fix128::ZERO
);
}
#[test]
fn torsional_stiffer_higher_frequency() {
let f_soft = torsional_frequency_hz(
Fix128::from_int(500),
Fix128::from_int(100),
Fix128::from_int(10),
Fix128::from_int(50),
);
let f_stiff = torsional_frequency_hz(
Fix128::from_int(2000),
Fix128::from_int(100),
Fix128::from_int(10),
Fix128::from_int(50),
);
assert!(f_stiff > f_soft);
}
}