use crate::error::{PyrucastError, Result};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum BeamModel {
Planar1d,
Frame2d,
Frame3d,
}
impl BeamModel {
pub fn from_space_dim(space_dim: usize) -> Result<Self> {
match space_dim {
1 => Ok(Self::Planar1d),
2 => Ok(Self::Frame2d),
3 => Ok(Self::Frame3d),
d => Err(PyrucastError::Message(format!(
"a beam lives in a 1-, 2- or 3-D configuration, got {d}-D"
))),
}
}
pub fn to_tag(self) -> &'static str {
match self {
Self::Planar1d => "planar_1d",
Self::Frame2d => "frame_2d",
Self::Frame3d => "frame_3d",
}
}
pub fn primal(self) -> &'static [&'static str] {
match self {
Self::Planar1d => &["w", "theta"],
Self::Frame2d => &["u_x", "u_y", "r_z"],
Self::Frame3d => &["u_x", "u_y", "u_z", "r_x", "r_y", "r_z"],
}
}
pub fn dual(self) -> &'static [&'static str] {
match self {
Self::Planar1d => &["f_w", "m_theta"],
Self::Frame2d => &["f_x", "f_y", "m_z"],
Self::Frame3d => &["f_x", "f_y", "f_z", "m_x", "m_y", "m_z"],
}
}
pub fn dofs_per_node(self) -> usize {
self.primal().len()
}
}
impl std::fmt::Display for BeamModel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.to_tag())
}
}
pub fn bending_4x4(ei: f64, gas: Option<f64>, l: f64) -> Vec<Vec<f64>> {
let phi = phi(ei, gas, l);
let c = ei / (l * l * l * (1.0 + phi));
let (l2, k1, k2) = (l * l, 12.0 * c, 6.0 * l * c);
let k3 = (4.0 + phi) * l2 * c;
let k4 = (2.0 - phi) * l2 * c;
vec![
vec![k1, k2, -k1, k2],
vec![k2, k3, -k2, k4],
vec![-k1, -k2, k1, -k2],
vec![k2, k4, -k2, k3],
]
}
fn shape_functions(phi: f64, l: f64, xi: f64) -> ([f64; 4], [f64; 4]) {
let (x2, x3) = (xi * xi, xi * xi * xi);
let d = 1.0 / (1.0 + phi);
let n_w = [
d * (2.0 * x3 - 3.0 * x2 - phi * xi + 1.0 + phi),
d * l * (x3 - (2.0 + phi / 2.0) * x2 + (1.0 + phi / 2.0) * xi),
d * (-2.0 * x3 + 3.0 * x2 + phi * xi),
d * l * (x3 - (1.0 - phi / 2.0) * x2 - (phi / 2.0) * xi),
];
let n_t = [
d * (6.0 / l) * (x2 - xi),
d * (3.0 * x2 - (4.0 + phi) * xi + 1.0 + phi),
d * (6.0 / l) * (xi - x2),
d * (3.0 * x2 - (2.0 - phi) * xi),
];
(n_w, n_t)
}
const GAUSS_01: [(f64, f64); 4] = [
(0.069_431_844_202_973_71, 0.173_927_422_568_726_9),
(0.330_009_478_207_571_9, 0.326_072_577_431_273_1),
(0.669_990_521_792_428_1, 0.326_072_577_431_273_1),
(0.930_568_155_797_026_3, 0.173_927_422_568_726_9),
];
pub fn mass_4x4(rho_a: f64, rho_i: f64, ei: f64, gas: Option<f64>, l: f64) -> Vec<Vec<f64>> {
let phi = phi(ei, gas, l);
let mut m = vec![vec![0.0_f64; 4]; 4];
for (xi, w) in GAUSS_01 {
let (n_w, n_t) = shape_functions(phi, l, xi);
let dx = w * l; for a in 0..4 {
for b in 0..4 {
m[a][b] += (rho_a * n_w[a] * n_w[b] + rho_i * n_t[a] * n_t[b]) * dx;
}
}
}
m
}
pub fn section_strains(phi: f64, l: f64, d: &[f64; 4], xi: f64) -> (f64, f64) {
let dd = 1.0 / (1.0 + phi);
let dn_t = [
dd * (6.0 / l) * (2.0 * xi - 1.0),
dd * (6.0 * xi - (4.0 + phi)),
dd * (6.0 / l) * (1.0 - 2.0 * xi),
dd * (6.0 * xi - (2.0 - phi)),
];
let (x2, _) = (xi * xi, ());
let dn_w = [
dd * (6.0 * x2 - 6.0 * xi - phi),
dd * l * (3.0 * x2 - (4.0 + phi) * xi + 1.0 + phi / 2.0),
dd * (-6.0 * x2 + 6.0 * xi + phi),
dd * l * (3.0 * x2 - (2.0 - phi) * xi - phi / 2.0),
];
let (_, n_t) = shape_functions(phi, l, xi);
let kappa: f64 = (0..4).map(|i| dn_t[i] * d[i]).sum::<f64>() / l;
let gamma: f64 = (0..4).map(|i| (dn_w[i] / l - n_t[i]) * d[i]).sum();
(kappa, gamma)
}
pub fn phi(ei: f64, gas: Option<f64>, l: f64) -> f64 {
match gas {
Some(g) if g.abs() > f64::MIN_POSITIVE => 12.0 * ei / (g * l * l),
_ => 0.0,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_shear_compliance_recovers_euler_bernoulli() {
let (ei, l) = (2.5, 1.7);
let k = bending_4x4(ei, None, l);
let c = ei / (l * l * l);
let want = [
[12.0 * c, 6.0 * l * c, -12.0 * c, 6.0 * l * c],
[6.0 * l * c, 4.0 * l * l * c, -6.0 * l * c, 2.0 * l * l * c],
[-12.0 * c, -6.0 * l * c, 12.0 * c, -6.0 * l * c],
[6.0 * l * c, 2.0 * l * l * c, -6.0 * l * c, 4.0 * l * l * c],
];
for i in 0..4 {
for j in 0..4 {
assert!(
(k[i][j] - want[i][j]).abs() < 1e-12,
"[{i}][{j}]: {} vs {}",
k[i][j],
want[i][j]
);
}
}
}
#[test]
fn a_stiff_shear_converges_to_the_shear_free_matrix() {
let (ei, l) = (2.5, 1.7);
let free = bending_4x4(ei, None, l);
let stiff = bending_4x4(ei, Some(1e12), l);
for i in 0..4 {
for j in 0..4 {
assert!((stiff[i][j] - free[i][j]).abs() < 1e-6 * free[0][0].abs());
}
}
}
#[test]
fn shear_compliance_softens_the_element() {
let (ei, l) = (2.5, 1.7);
let free = bending_4x4(ei, None, l);
let soft = bending_4x4(ei, Some(0.5), l);
assert!(soft[0][0] < free[0][0], "the deflection term should soften");
assert!(soft[1][1] < free[1][1], "the rotation term should soften");
}
#[test]
fn rigid_body_modes_are_free() {
let (ei, l, gas) = (2.5, 1.7, 3.0);
let k = bending_4x4(ei, Some(gas), l);
for mode in [[1.0, 0.0, 1.0, 0.0], [-l / 2.0, 1.0, l / 2.0, 1.0]] {
for row in k.iter() {
let f: f64 = (0..4).map(|j| row[j] * mode[j]).sum();
assert!(f.abs() < 1e-9 * k[0][0].abs(), "rigid mode carries {f}");
}
}
}
#[test]
fn without_shear_the_rotation_is_the_slope() {
let l = 1.7;
for k in 0..=10 {
let xi = k as f64 / 10.0;
let (n_w, n_t) = shape_functions(0.0, l, xi);
let h = 1e-6;
let (plus, _) = shape_functions(0.0, l, xi + h);
let (minus, _) = shape_functions(0.0, l, xi - h);
for i in 0..4 {
let dn_dx = (plus[i] - minus[i]) / (2.0 * h * l);
assert!(
(dn_dx - n_t[i]).abs() < 1e-6,
"N{}'({xi}) = {dn_dx} vs Nθ = {}",
i + 1,
n_t[i]
);
}
let _ = n_w;
}
}
#[test]
fn without_shear_the_mass_is_the_classical_table() {
let (rho_a, l) = (2.5, 1.7);
let m = mass_4x4(rho_a, 0.0, 1.0, None, l);
let c = rho_a * l / 420.0;
let (l2, ll) = (l * l, l);
let want = [
[156.0 * c, 22.0 * ll * c, 54.0 * c, -13.0 * ll * c],
[22.0 * ll * c, 4.0 * l2 * c, 13.0 * ll * c, -3.0 * l2 * c],
[54.0 * c, 13.0 * ll * c, 156.0 * c, -22.0 * ll * c],
[-13.0 * ll * c, -3.0 * l2 * c, -22.0 * ll * c, 4.0 * l2 * c],
];
for i in 0..4 {
for j in 0..4 {
assert!(
(m[i][j] - want[i][j]).abs() < 1e-10 * want[0][0],
"[{i}][{j}]: {} vs {}",
m[i][j],
want[i][j]
);
}
}
}
#[test]
fn a_rigid_translation_carries_the_whole_mass() {
let (rho_a, rho_i, ei, l) = (2.5, 0.4, 3.0, 1.7);
for gas in [None, Some(1e-2), Some(1.0), Some(1e6)] {
let m = mass_4x4(rho_a, rho_i, ei, gas, l);
let unit = [1.0, 0.0, 1.0, 0.0]; let total: f64 = (0..4)
.map(|a| (0..4).map(|b| unit[a] * m[a][b] * unit[b]).sum::<f64>())
.sum();
assert!(
(total - rho_a * l).abs() < 1e-10 * rho_a * l,
"gas={gas:?}: {total} vs {}",
rho_a * l
);
}
}
#[test]
fn the_mass_is_symmetric_and_positive() {
let m = mass_4x4(2.5, 0.4, 3.0, Some(1.0), 1.7);
for i in 0..4 {
for j in 0..4 {
assert!(
(m[i][j] - m[j][i]).abs() < 1e-12,
"asymmetric at [{i}][{j}]"
);
}
assert!(m[i][i] > 0.0, "non-positive diagonal at {i}");
}
for v in [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[1.0, -1.0, 1.0, -1.0],
[0.3, 0.7, -0.2, 0.9],
] {
let q: f64 = (0..4)
.map(|a| (0..4).map(|b| v[a] * m[a][b] * v[b]).sum::<f64>())
.sum();
assert!(q > 0.0, "non-positive quadratic form: {q}");
}
}
#[test]
fn shear_compliance_changes_the_rotational_terms() {
let (rho_a, rho_i, ei, l) = (2.5, 0.4, 3.0, 1.7);
let stiff = mass_4x4(rho_a, rho_i, ei, Some(1e6), l);
let soft = mass_4x4(rho_a, rho_i, ei, Some(0.5), l);
assert!(
(stiff[1][1] - soft[1][1]).abs() > 1e-3 * stiff[1][1],
"Φ leaves the rotational mass untouched: {} vs {}",
stiff[1][1],
soft[1][1]
);
}
#[test]
fn the_shear_is_constant_and_the_curvature_linear() {
let (l, ph) = (1.7, 0.8);
let d = [0.0, 0.0, 0.5, 0.2]; let sample: Vec<(f64, f64)> = (0..=10)
.map(|k| section_strains(ph, l, &d, k as f64 / 10.0))
.collect();
for (_, g) in &sample {
assert!(
(g - sample[0].1).abs() < 1e-12,
"shear varies: {g} vs {}",
sample[0].1
);
}
for w in sample.windows(3) {
let d2 = w[0].0 - 2.0 * w[1].0 + w[2].0;
assert!(d2.abs() < 1e-12, "curvature is not linear: {d2}");
}
assert!((sample[0].0 - sample[10].0).abs() > 1e-6);
}
#[test]
fn without_shear_the_strain_is_bernoullis() {
let l = 1.7;
let d = [0.1, -0.2, 0.5, 0.3];
for k in 0..=10 {
let xi = k as f64 / 10.0;
let (kappa, gamma) = section_strains(0.0, l, &d, xi);
assert!(gamma.abs() < 1e-12, "shear-free γ = {gamma}");
let h = 1e-4;
let w_of = |x: f64| -> f64 {
let (n_w, _) = shape_functions(0.0, l, x);
(0..4).map(|i| n_w[i] * d[i]).sum()
};
let fd = (w_of(xi + h) - 2.0 * w_of(xi) + w_of(xi - h)) / (h * h * l * l);
assert!((kappa - fd).abs() < 1e-4, "κ({xi}) = {kappa} vs w'' = {fd}");
}
}
}