Skip to main content

beam_cantilever_tip_load/
beam_cantilever_tip_load.rs

1//! Cantilever with a transverse tip load — the simplest end-to-end workflow.
2//!
3//! Demonstrates: build model → add nodes/elements → fix a support → apply a
4//! load → solve → read displacement, reactions and element end forces.
5//!
6//! Unit properties (`E = A = I = 1`, `L = 1`) are used so the results are easy
7//! to verify by hand:
8//!
9//! ```text
10//! tip deflection  delta = -P L^3 / (3 E I) = -33.333333
11//! tip rotation    theta = -P L^2 / (2 E I) = -50.0
12//! reaction        Ry = +P = 100,  Rz = +P L = 100
13//! ```
14
15use section_properties::beam_fem::{
16    BeamElement, BeamModel, BeamNode, BeamSection, BeamSolver, Dof,
17};
18use section_properties::material::Material;
19
20fn main() -> Result<(), Box<dyn std::error::Error>> {
21    let (e, a, i, l, p) = (1.0, 1.0, 1.0, 1.0, 100.0);
22
23    // --- model ---------------------------------------------------------------
24    let mut model = BeamModel::new();
25    model.add_node(BeamNode::new(0, 0.0, 0.0));
26    model.add_node(BeamNode::new(1, l, 0.0));
27    model.add_element(BeamElement::new(
28        0,
29        1,
30        Material::new(e, 0.3, 1.0, "unit"),
31        BeamSection::new(a, i),
32    )?);
33    model.fix_node(0); // clamp node 0 (ux = uy = rz = 0)
34    model.add_nodal_force(1, 1, -p); // global uy = -P at the free tip
35
36    // --- solve ---------------------------------------------------------------
37    let mut solver = BeamSolver::from_model(&model)?;
38    solver.solve_configured()?; // default SolverSelection::Auto
39    let res = solver.results();
40
41    // --- results -------------------------------------------------------------
42    // Typed DOF accessors on the solver (equivalent to the raw-index API):
43    //   solver.displacement_dof(1, Dof::Uy) == solver.displacement(1, 1).unwrap()
44    let uy = solver.displacement_dof(1, Dof::Uy)?;
45    let rz = solver.displacement_dof(1, Dof::Rz)?;
46    let ry = solver.reaction_dof(0, Dof::Uy)?;
47    let moment = solver.reaction_dof(0, Dof::Rz)?;
48
49    println!("solver backend : {:?}", res.solver_name());
50    println!(
51        "tip uy         : {:+.6}   (analytical {:+.6})",
52        uy,
53        -p * l.powi(3) / (3.0 * e * i)
54    );
55    println!(
56        "tip rz         : {:+.6}   (analytical {:+.6})",
57        rz,
58        -p * l.powi(2) / (2.0 * e * i)
59    );
60    println!("reaction Ry    : {:+.6}   (analytical {:+.6})", ry, p);
61    println!(
62        "reaction Rz    : {:+.6}   (analytical {:+.6})",
63        moment,
64        p * l
65    );
66
67    let end = res.element_end_forces(0)?;
68    println!("element end forces [N_i,V_i,M_i,N_j,V_j,M_j] = {end:?}");
69    println!(
70        "internal section moment at the fixed end M(0) = {:+.6} (analytical -P L = {:+.6})",
71        res.section_forces(0, 0.0)?.moment,
72        -p * l
73    );
74
75    Ok(())
76}