Skip to main content

sim_components/
beam.rs

1//! Euler-Bernoulli beams for the structure's bending (effects.link_bending, the gantry's beam and Z axis): a tube's
2//! second moment of area, a cantilever's first bending mode as one modal coordinate (its tip's deflection), and the
3//! compliance of a simply supported span under a point load.
4//!
5//! The cantilever's first mode shape (Clough and Penzien, "Dynamics of Structures", 18.4), clamped at x = 0:
6//!
7//! ```text
8//! phi(x) = cosh bx - cos bx - s (sinh bx - sin bx),   b L = 1.875104,   s = 0.734096,   phi(L) = 2
9//! f = (b L)^2 / (2 pi) sqrt(E I / (m L^3))            m the beam's mass
10//! ```
11//!
12//! Taken to its tip (phi(L) = 1): modal mass m / 4 plus a mass at the tip, modal stiffness (b L)^4 / 4 E I / L^3 =
13//! 3.0906 E I / L^3 (the static 3 E I / L^3 within 3 %); a load spread evenly over the beam drives it with 0.3915 of its
14//! sum, a moment at the tip with 1.3765 / L of it, and the tip turns by 1.3765 / L of its deflection.
15
16/// b L of a cantilever's first mode.
17pub const CANTILEVER_BETA_L: f64 = 1.875_104_068_711_961;
18/// The share of an evenly spread load that drives the first mode taken to its tip: the integral of phi over L.
19pub const CANTILEVER_PARTICIPATION: f64 = 0.391_496;
20/// The first mode's slope at the tip times L, taken to its tip: what a moment at the tip drives it with, times 1 / L.
21pub const CANTILEVER_TIP_SLOPE: f64 = 1.376_51;
22
23/// The cantilever's first mode shape taken to its tip, phi(xi) / phi(1) at xi = x / L in [0, 1] (the module's phi).
24pub fn cantilever_shape(xi: f64) -> f64 {
25    let (b, s) = (CANTILEVER_BETA_L, 0.734_095_514);
26    let phi = |x: f64| (b * x).cosh() - (b * x).cos() - s * ((b * x).sinh() - (b * x).sin());
27    phi(xi) / phi(1.0)
28}
29
30/// A cantilever's static deflection shape under a tip load, taken to its tip: psi(xi) = (3 xi^2 - xi^3) / 2, xi = x / L.
31/// With the static stiffness 3 E I / L^3 it is the consistent single mode (Rayleigh): the tip deflects as the exact
32/// beam's under a tip load and under its own weight (int psi = 3/8: q L^4 / (8 E I)), and the first frequency is the
33/// exact one's from above, +1.5 % for the bare beam and under +0.26 % with the delta arms' elbows (the exact mode's
34/// shape with 3 E I / L^3 reads it 0.8 to 1 % low there and the sag under the tube's weight 4.4 % high,
35/// `cantilever_first_frequency`). Its mass share: int psi^2.
36pub const STATIC_SHAPE_MASS: f64 = 33.0 / 140.0;
37/// The static shape's int psi: what an evenly spread load drives it with.
38pub const STATIC_SHAPE_PARTICIPATION: f64 = 3.0 / 8.0;
39/// The static shape's int(xi psi): the first moment about the root of what moves with it, over m L.
40pub const STATIC_SHAPE_MOMENT: f64 = 11.0 / 40.0;
41/// The static shape's slope at the tip times L: the tip turns by 3/2 of its deflection over L.
42pub const STATIC_SHAPE_TIP_SLOPE: f64 = 1.5;
43
44/// The first bending frequency (rad/s) of a uniform Euler-Bernoulli cantilever, exactly: bending stiffness `ei`, its own
45/// mass `beam` over `length`, a mass `tip` and a rotary inertia `tip_inertia` (about the axis the tip turns about) at its
46/// free end. The lowest root z = b L of the clamped-free characteristic equation with the tip's shear -M w^2 w(L) and
47/// moment J w^2 w'(L), r = M / m, j = J / (m L^2):
48///
49/// ```text
50/// [(ch + c) - j z^3 (sh + s)] [(ch + c) + r z (sh - s)] - [(sh + s) - j z^3 (ch - c)] [(sh - s) + r z (ch - c)] = 0
51/// w = z^2 sqrt(E I / (m L^3))           (ch, sh, c, s of z)
52/// ```
53///
54/// by bisection below 1.875104 (the bare beam's); `beam` > 0.
55pub fn cantilever_first_frequency(ei: f64, beam: f64, tip: f64, tip_inertia: f64, length: f64) -> f64 {
56    let (r, j) = (tip / beam, tip_inertia / (beam * length * length));
57    let det = |z: f64| {
58        let (ch, sh, (s, c)) = (z.cosh(), z.sinh(), z.sin_cos());
59        let (j3, rz) = (j * z.powi(3), r * z);
60        ((ch + c) - j3 * (sh + s)) * ((ch + c) + rz * (sh - s))
61            - ((sh + s) - j3 * (ch - c)) * ((sh - s) + rz * (ch - c))
62    };
63    let (mut low, mut high) = (1e-6, CANTILEVER_BETA_L);
64    let below = det(low) > 0.0;
65    for _ in 0..200 {
66        let mid = 0.5 * (low + high);
67        if (det(mid) > 0.0) == below { low = mid } else { high = mid }
68    }
69    low * low * (ei / (beam * length.powi(3))).sqrt()
70}
71
72/// The second moment of area of a round tube, outer diameter `outer` and wall `wall` (m): pi / 64 (D^4 - d^4). A wall of
73/// half the diameter or more is a solid bar.
74pub fn tube_second_moment(outer: f64, wall: f64) -> f64 {
75    let inner = (outer - 2.0 * wall).max(0.0);
76    std::f64::consts::PI / 64.0 * (outer.powi(4) - inner.powi(4))
77}
78
79/// A cantilever's first bending mode in its tip's deflection: modal mass (kg) and stiffness (N/m), for bending
80/// stiffness `ei` (N m^2), its own mass `beam` spread evenly over `length` and `tip` at its end.
81pub fn cantilever_mode(ei: f64, beam: f64, tip: f64, length: f64) -> (f64, f64) {
82    (0.25 * beam + tip, CANTILEVER_BETA_L.powi(4) / 4.0 * ei / length.powi(3))
83}
84
85/// The deflection under a unit load at `at` along a simply supported span `span` (m/N): a^2 b^2 / (3 E I L), b = L - a.
86/// L^3 / (48 E I) in the middle.
87pub fn span_compliance(ei: f64, span: f64, at: f64) -> f64 {
88    let a = at.clamp(0.0, span);
89    let b = span - a;
90    a * a * b * b / (3.0 * ei * span)
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn a_uniform_cantilever_alone_rings_at_the_euler_bernoulli_frequency() {
99        // Steel tube 60 x 4 mm, 0.8 m, 4.5 kg: I = pi/64 (0.06^4 - 0.052^4) = 2.77264e-7 m^4, EI = 2.1e11 I = 58225 N m^2.
100        let ei = 2.1e11 * tube_second_moment(0.06, 0.004);
101        assert!((ei - 58_225.52).abs() < 0.01, "EI {ei}");
102        let (m, k) = cantilever_mode(ei, 4.5, 0.0, 0.8);
103        // f = 1.875104^2 / (2 pi) sqrt(EI / (m L^3)) = 3.51602 / 6.28319 sqrt(58225.52 / (4.5 * 0.512)) = 88.96 Hz.
104        let euler_bernoulli = 3.516_015 / std::f64::consts::TAU * (ei / (4.5 * 0.8f64.powi(3))).sqrt();
105        let f = (k / m).sqrt() / std::f64::consts::TAU;
106        assert!((f - euler_bernoulli).abs() < 1e-3, "{f} Hz against {euler_bernoulli} Hz");
107        assert!((f - 89.0).abs() < 0.1, "{f}");
108    }
109
110    #[test]
111    fn a_tip_load_bends_the_mode_as_far_as_the_static_cantilever_within_three_per_cent() {
112        let (_, k) = cantilever_mode(1000.0, 1.0, 0.0, 0.5);
113        // F L^3 / (3 EI) = 0.125 / 3000 per newton.
114        assert!(((1.0 / k) / (0.125 / 3000.0) - 1.0).abs() < 0.03);
115    }
116
117    #[test]
118    fn a_central_load_sags_a_simple_span_by_f_l3_over_48_ei() {
119        // 1.2 m span, EI = 4e5 N m^2, 500 N in the middle: 500 * 1.728 / (48 * 4e5) = 45 um.
120        assert!((500.0 * span_compliance(4.0e5, 1.2, 0.6) - 45.0e-6).abs() < 1e-12);
121        // At a quarter: a^2 b^2 / (3 EI L) = 0.09 * 0.81 / (3 * 4e5 * 1.2) per newton, 9/16 of the middle's.
122        assert!((span_compliance(4.0e5, 1.2, 0.3) / span_compliance(4.0e5, 1.2, 0.6) - 0.5625).abs() < 1e-12);
123    }
124
125    #[test]
126    fn the_mode_constants_follow_from_its_shape() {
127        let (b, s) = (CANTILEVER_BETA_L, 0.734_095_514);
128        let phi = |x: f64| (b * x).cosh() - (b * x).cos() - s * ((b * x).sinh() - (b * x).sin());
129        let tip = phi(1.0);
130        assert!((tip - 2.0).abs() < 1e-5);
131        // Integrals over x from 0 to 1 by the midpoint rule.
132        let n = 100_000;
133        let (mut area, mut square) = (0.0, 0.0);
134        for i in 0..n {
135            let x = (i as f64 + 0.5) / n as f64;
136            area += phi(x) / tip / n as f64;
137            square += (phi(x) / tip).powi(2) / n as f64;
138        }
139        assert!((area - CANTILEVER_PARTICIPATION).abs() < 1e-5, "{area}");
140        assert!((square - 0.25).abs() < 1e-5, "{square}");
141        let slope = (phi(1.0) - phi(1.0 - 1e-6)) / 1e-6 / tip;
142        assert!((slope - CANTILEVER_TIP_SLOPE).abs() < 1e-4, "{slope}");
143    }
144
145    /// The exact first frequency against the textbook's b L: 1.875104 bare, 1.247917 with a tip mass equal to the
146    /// beam's (Blevins, "Formulas for natural frequency and mode shape", table 8-8: lambda = 1.24792). And the static
147    /// shape's integrals by the midpoint rule: 3/8, 11/40, 33/140, its tip slope 3/2.
148    #[test]
149    fn the_exact_cantilever_with_a_tip_mass_has_the_textbook_frequency() {
150        let w = |tip: f64, j: f64| cantilever_first_frequency(1.0, 1.0, tip, j, 1.0);
151        assert!((w(0.0, 0.0) - CANTILEVER_BETA_L.powi(2)).abs() < 1e-9, "{}", w(0.0, 0.0));
152        assert!((w(1.0, 0.0).sqrt() - 1.247_917).abs() < 2e-6, "{}", w(1.0, 0.0).sqrt());
153        // A rotary inertia at the tip lowers it further; scaling: z^2 sqrt(EI / (m L^3)).
154        assert!(w(1.0, 0.05) < w(1.0, 0.0));
155        let scaled = cantilever_first_frequency(4.0, 2.0, 2.0, 0.0, 0.5);
156        assert!((scaled - w(1.0, 0.0) * (4.0f64 / (2.0 * 0.125)).sqrt()).abs() < 1e-9, "{scaled}");
157        let psi = |x: f64| (3.0 * x * x - x * x * x) / 2.0;
158        let n = 100_000;
159        let (mut area, mut moment, mut square) = (0.0, 0.0, 0.0);
160        for i in 0..n {
161            let x = (i as f64 + 0.5) / n as f64;
162            (area, moment, square) =
163                (area + psi(x) / n as f64, moment + x * psi(x) / n as f64, square + psi(x).powi(2) / n as f64);
164        }
165        assert!((area - STATIC_SHAPE_PARTICIPATION).abs() < 1e-9 && (moment - STATIC_SHAPE_MOMENT).abs() < 1e-9);
166        assert!((square - STATIC_SHAPE_MASS).abs() < 1e-9 && psi(1.0) == 1.0);
167        assert!(((psi(1.0) - psi(1.0 - 1e-7)) / 1e-7 - STATIC_SHAPE_TIP_SLOPE).abs() < 1e-6);
168    }
169}