Skip to main content

beam_rotated_mixed_loading/
beam_rotated_mixed_loading.rs

1//! Rotated (45°) cantilever with combined axial + transverse tip loads and a
2//! tip applied moment — showing the local/global transformation.
3//!
4//! Demonstrates: a non-axis-aligned element, local tip loads expressed in
5//! global coordinates, an applied moment, and the relationship between global
6//! displacement and the local element response.
7//!
8//! Unit properties (`E = A = I = 1`, `L = 1`). Local tip loads `fx = 50`
9//! (tension), `fy = -100` (transverse) and a tip applied moment `M = +20` give
10//! the analytical local tip response
11//!
12//! ```text
13//! u_local = fx L / (E A)                             = +50
14//! v_local = fy L^3 / (3 E I) + M L^2 / (2 E I)       = -23.333333
15//! theta   = fy L^2 / (2 E I) + M L / (E I)           = -30
16//! ```
17
18use section_properties::beam_fem::{
19    BeamElement, BeamModel, BeamNode, BeamSection, BeamSolver, Dof,
20};
21use section_properties::material::Material;
22
23fn main() -> Result<(), Box<dyn std::error::Error>> {
24    let (e, a, i, l) = (1.0, 1.0, 1.0, 1.0);
25    let (fx, fy, m) = (50.0, -100.0, 20.0);
26    let angle: f64 = 45.0_f64.to_radians();
27    let (c, s) = (angle.cos(), angle.sin());
28
29    // --- model (element along direction (cos, sin)) --------------------------
30    let mut model = BeamModel::new();
31    model.add_node(BeamNode::new(0, 0.0, 0.0));
32    model.add_node(BeamNode::new(1, l * c, l * s));
33    model.add_element(BeamElement::new(
34        0,
35        1,
36        Material::new(e, 0.3, 1.0, "unit"),
37        BeamSection::new(a, i),
38    )?);
39    model.fix_node(0);
40
41    // Local tip force (fx, fy) expressed in global coordinates: Fg = R(theta)·F.
42    let gx = c * fx - s * fy;
43    let gy = s * fx + c * fy;
44    model.add_nodal_force(1, 0, gx);
45    model.add_nodal_force(1, 1, gy);
46    model.add_applied_moment(1, m)?; // applied moment is a global theta load
47
48    // --- solve ---------------------------------------------------------------
49    let mut solver = BeamSolver::from_model(&model)?;
50    solver.solve_configured()?;
51    let res = solver.results();
52
53    // --- global results ------------------------------------------------------
54    // Typed DOF accessors on the solver (equivalent to the raw-index API).
55    let (dgx, dgy) = (
56        solver.displacement_dof(1, Dof::Ux)?,
57        solver.displacement_dof(1, Dof::Uy)?,
58    );
59    println!("solver backend : {:?}", res.solver_name());
60    println!("global tip ux,uy : {:+.6}, {:+.6}", dgx, dgy);
61
62    // Local tip displacement: u_local = T · u_global.
63    let el = &model.elements[0];
64    let ni = model.nodes[0].point();
65    let nj = model.nodes[1].point();
66    let t = el.transformation_matrix(ni, nj);
67    let ug = [
68        solver.displacements()[3],
69        solver.displacements()[4],
70        solver.displacements()[5],
71    ];
72    let ul = [
73        t[0][0] * ug[0] + t[0][1] * ug[1],
74        t[1][0] * ug[0] + t[1][1] * ug[1],
75        ug[2],
76    ];
77    println!(
78        "local tip  u,v   : {:+.6}, {:+.6}   (analytical {:+.6}, {:+.6})",
79        ul[0],
80        ul[1],
81        fx * l / (e * a),
82        fy * l.powi(3) / (3.0 * e * i) + m * l * l / (2.0 * e * i)
83    );
84    println!(
85        "local tip theta  : {:+.6}   (analytical {:+.6})",
86        ul[2],
87        fy * l.powi(2) / (2.0 * e * i) + m * l / (e * i)
88    );
89
90    // --- reactions and equilibrium ------------------------------------------
91    let r0 = res.reaction(0)?;
92    println!("reaction Rx,Ry   : {:+.6}, {:+.6}", r0.fx, r0.fy);
93    println!("reaction Rz      : {:+.6}", r0.mz);
94    println!("ΣFx residual     : {:+.3e}", r0.fx + gx);
95    println!("ΣFy residual     : {:+.3e}", r0.fy + gy);
96    println!(
97        "ΣMz residual     : {:+.3e}   (about the fixed node)",
98        r0.mz + m + l * c * gy - l * s * gx
99    );
100
101    // --- element end forces --------------------------------------------------
102    println!("local end forces : {:?}", res.element_end_forces(0)?);
103    println!(
104        "global end forces: {:?}",
105        solver.element_end_forces_global()?[0]
106    );
107
108    Ok(())
109}