pyrucast 0.15.0

Finite-element library in Rust, inspired by cast3m, exposed to Python
Documentation
//! Mass and geometric-stiffness of the structural (bar / beam) elements,
//! exercised end-to-end through the public API against closed forms.

use pyrucast::aggregate::Aggregate;
use pyrucast::atoms::{ElementType, Interpolation, Node};
use pyrucast::containers::element_field::{ElementField, SubElementField};
use pyrucast::containers::finite_element_space::FiniteElementSpace;
use pyrucast::containers::mesh::{Mesh, SubMesh};
use pyrucast::containers::model::Model;
use pyrucast::coords::Coords;
use pyrucast::handle::Handle;
use pyrucast::ops::element_field::StrainMeasure;
use pyrucast::ops::model;
use pyrucast::Result;

/// A single SEG2 bar from the origin to `(dx, dy)`, with its FE space + two nodes.
fn bar_2d(dx: f64, dy: f64) -> Result<(FiniteElementSpace, Node, Node, f64)> {
    let coords = Handle::new(Coords::new(2)?);
    let a = Node::create_in(coords.clone(), &[0.0, 0.0])?;
    let b = Node::create_in(coords.clone(), &[dx, dy])?;
    let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    mesh.add_cell(&[a.id(), b.id()])?;
    let fes = FiniteElementSpace::new(&mesh, Interpolation::ModelEmbedded)?;
    Ok((fes, a, b, (dx * dx + dy * dy).sqrt()))
}

/// A one-component state field (`n` = axial force) on a bar's FE space.
fn axial_force(fes: &FiniteElementSpace, n: f64) -> Result<ElementField> {
    let sub = SubElementField::from_uniform_per_component(fes.get(0)?, vec!["n".into()], &[n])?;
    let mut f = ElementField::empty();
    f.add_sub(Handle::new(sub))?;
    Ok(f)
}

#[test]
fn truss_consistent_mass_matches_closed_form() -> Result<()> {
    const RHO: f64 = 2.0;
    const AREA: f64 = 3.0;
    let (fes, a, b, len) = bar_2d(3.0, 4.0)?; // L = 5
    let model = model::truss(&fes, StrainMeasure::Linear)?;
    let materials = pyrucast::ops::element_field::material_field(
        &model,
        &[("E", 1.0), ("A", AREA), ("rho", RHO)],
    )?;

    let m = pyrucast::ops::matrix::mass(&model, &materials)?;
    let tol = 1e-12;
    let base = RHO * AREA * len / 6.0; // ρAL/6
                                       // (ρAL/6)·[[2,1],[1,2]] on each translation component; block-diagonal.
    assert!((m.get(a.id(), "f_x", a.id(), "u_x").unwrap() - 2.0 * base).abs() < tol);
    assert!((m.get(a.id(), "f_x", b.id(), "u_x").unwrap() - base).abs() < tol);
    assert!((m.get(a.id(), "f_y", a.id(), "u_y").unwrap() - 2.0 * base).abs() < tol);
    assert!(m.get(a.id(), "f_x", a.id(), "u_y").unwrap().abs() < tol);

    // Whole-matrix sum = space_dim · ρAL (each direction carries the full bar mass).
    let total: f64 = m.to_dmatrix()?.sum();
    assert!(
        (total - 2.0 * RHO * AREA * len).abs() < 1e-10,
        "total = {total}"
    );
    Ok(())
}

#[test]
fn truss_geometric_stiffens_every_direction() -> Result<()> {
    const N: f64 = 7.0;
    let (fes, a, b, _l) = bar_2d(1.0, 0.0)?; // axis = x, L = 1
    let model = model::truss(&fes, StrainMeasure::Linear)?;
    let materials =
        pyrucast::ops::element_field::material_field(&model, &[("E", 1.0), ("A", 1.0)])?;
    let state = axial_force(&fes, N)?;

    let kg = pyrucast::ops::matrix::geometric(&model, &materials, &state)?;
    let tol = 1e-12;
    // (N/L)[[1,-1],[-1,1]] on each of u_x and u_y — the axial one too, as the
    // continuum's ∫ ∇N·σ∇N dv · I — and no coupling between them.
    for (f, u) in [("f_x", "u_x"), ("f_y", "u_y")] {
        assert!((kg.get(a.id(), f, a.id(), u).unwrap() - N).abs() < tol);
        assert!((kg.get(a.id(), f, b.id(), u).unwrap() + N).abs() < tol);
    }
    assert!(kg.get(a.id(), "f_x", a.id(), "u_y").unwrap().abs() < tol);
    Ok(())
}

#[test]
fn truss_geometric_inclined_is_isotropic() -> Result<()> {
    const N: f64 = 5.0;
    let (fes, a, _b, len) = bar_2d(3.0, 4.0)?; // c = (0.6, 0.8)
    let model = model::truss(&fes, StrainMeasure::Linear)?;
    let materials =
        pyrucast::ops::element_field::material_field(&model, &[("E", 1.0), ("A", 1.0)])?;
    let state = axial_force(&fes, N)?;

    let kg = pyrucast::ops::matrix::geometric(&model, &materials, &state)?;
    let k = N / len;
    let tol = 1e-12;
    // Diagonal block at A = (N/L)·I, whatever the direction of the bar.
    assert!((kg.get(a.id(), "f_x", a.id(), "u_x").unwrap() - k).abs() < tol);
    assert!(kg.get(a.id(), "f_x", a.id(), "u_y").unwrap().abs() < tol);
    assert!((kg.get(a.id(), "f_y", a.id(), "u_y").unwrap() - k).abs() < tol);
    Ok(())
}

#[test]
fn truss_mass_requires_density() -> Result<()> {
    let (fes, _a, _b, _l) = bar_2d(1.0, 0.0)?;
    let model = model::truss(&fes, StrainMeasure::Linear)?;
    let materials =
        pyrucast::ops::element_field::material_field(&model, &[("E", 1.0), ("A", 1.0)])?;
    assert!(pyrucast::ops::matrix::mass(&model, &materials).is_err());
    Ok(())
}

// ─── Frame (2-D beam-column) ────────────────────────────────────────────────

#[test]
fn frame_consistent_mass_matches_closed_form() -> Result<()> {
    const RHO: f64 = 2.0;
    const AREA: f64 = 3.0;
    const I: f64 = 0.5;
    const L: f64 = 2.0;
    let (fes, a, b, len) = bar_2d(L, 0.0)?; // horizontal ⇒ T = identity
    let model = model::timoshenko(&fes)?;
    let materials = pyrucast::ops::element_field::material_field(
        &model,
        &[
            ("E", 1.0),
            ("A", AREA),
            ("I", I),
            ("G", 1.0),
            ("A_s", 1.0),
            ("rho", RHO),
        ],
    )?;

    let m = pyrucast::ops::matrix::mass(&model, &materials)?;
    let tol = 1e-12;
    let ma = RHO * AREA * len / 6.0; // ρAL/6
                                     // The **axial** direction still carries the linear field's mass, which is
                                     // exact for what it interpolates — a bar stretches linearly.
    assert!((m.get(a.id(), "f_x", a.id(), "u_x").unwrap() - 2.0 * ma).abs() < tol);
    assert!((m.get(a.id(), "f_x", b.id(), "u_x").unwrap() - ma).abs() < tol);
    // The **transverse** direction takes the exact element's mass: a rigid
    // translation still carries exactly ρAL, and w now couples to the rotation.
    let total = m.get(a.id(), "f_y", a.id(), "u_y").unwrap()
        + m.get(a.id(), "f_y", b.id(), "u_y").unwrap()
        + m.get(b.id(), "f_y", a.id(), "u_y").unwrap()
        + m.get(b.id(), "f_y", b.id(), "u_y").unwrap();
    assert!((total - RHO * AREA * len).abs() < 1e-10 * RHO * AREA * len);
    assert!(m.get(a.id(), "f_y", a.id(), "r_z").unwrap().abs() > 1e-6);
    // No translation ↔ rotation coupling.
    assert!(m.get(a.id(), "f_x", a.id(), "r_z").unwrap().abs() < tol);

    // The x direction carries the member's mass too — the sum over the whole
    // matrix is *not* an invariant of the exact element (it mixes translations
    // with rotations, which have different units), so the check is made
    // direction by direction, where it means something.
    let total_x = m.get(a.id(), "f_x", a.id(), "u_x").unwrap()
        + m.get(a.id(), "f_x", b.id(), "u_x").unwrap()
        + m.get(b.id(), "f_x", a.id(), "u_x").unwrap()
        + m.get(b.id(), "f_x", b.id(), "u_x").unwrap();
    assert!((total_x - RHO * AREA * len).abs() < 1e-10 * RHO * AREA * len);
    Ok(())
}

#[test]
fn frame_geometric_stiffens_transverse_only() -> Result<()> {
    const N: f64 = 9.0;
    const L: f64 = 3.0;
    let (fes, a, b, len) = bar_2d(L, 0.0)?; // horizontal ⇒ transverse is u_y
    let model = model::timoshenko(&fes)?;
    let materials = pyrucast::ops::element_field::material_field(
        &model,
        &[("E", 1.0), ("A", 1.0), ("I", 1.0), ("G", 1.0), ("A_s", 1.0)],
    )?;
    let sub = SubElementField::from_uniform_per_component(fes.get(0)?, vec!["N".into()], &[N])?;
    let mut state = ElementField::empty();
    state.add_sub(Handle::new(sub))?;

    let kg = pyrucast::ops::matrix::geometric(&model, &materials, &state)?;
    let tol = 1e-12;
    let g = N / len;
    assert!((kg.get(a.id(), "f_y", a.id(), "u_y").unwrap() - g).abs() < tol);
    assert!((kg.get(a.id(), "f_y", b.id(), "u_y").unwrap() + g).abs() < tol);
    // Axial and rotation carry no geometric term.
    assert!(kg.get(a.id(), "f_x", a.id(), "u_x").unwrap().abs() < tol);
    assert!(kg.get(a.id(), "m_z", a.id(), "r_z").unwrap().abs() < tol);
    Ok(())
}

// ─── Timoshenko (1-D beam, mass only) ───────────────────────────────────────

#[test]
fn timoshenko_consistent_mass_matches_closed_form() -> Result<()> {
    const RHO: f64 = 2.0;
    const AREA: f64 = 3.0;
    const I: f64 = 0.5;
    const L: f64 = 2.0;
    let coords = Handle::new(Coords::new(1)?);
    let a = Node::create_in(coords.clone(), &[0.0])?;
    let b = Node::create_in(coords.clone(), &[L])?;
    let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    mesh.add_cell(&[a.id(), b.id()])?;
    let fes = FiniteElementSpace::new(&mesh, Interpolation::ModelEmbedded)?;
    let model = model::timoshenko(&fes)?;
    let materials = pyrucast::ops::element_field::material_field(
        &model,
        &[
            ("E", 1.0),
            ("I", I),
            ("G", 1.0),
            ("A_s", 1.0),
            ("A", AREA),
            ("rho", RHO),
        ],
    )?;

    let m = pyrucast::ops::matrix::mass(&model, &materials)?;
    // The mass is now the **exact element's**, integrated from the same shape
    // functions as its stiffness. Two properties pin it, and neither is a
    // transcribed coefficient.
    //
    // 1. A rigid translation carries exactly the mass of the member. Nothing an
    //    interpolation does may change that, whatever `Φ`.
    let total = m.get(a.id(), "f_w", a.id(), "w").unwrap()
        + m.get(a.id(), "f_w", b.id(), "w").unwrap()
        + m.get(b.id(), "f_w", a.id(), "w").unwrap()
        + m.get(b.id(), "f_w", b.id(), "w").unwrap();
    assert!(
        (total - RHO * AREA * L).abs() < 1e-10 * RHO * AREA * L,
        "rigid translation carries {total}, expected {}",
        RHO * AREA * L
    );
    // 2. Deflection and rotation are now **coupled** — the linear element's
    //    mass had no such term, and that absence is exactly what made it
    //    inconsistent with an exact stiffness.
    assert!(
        m.get(a.id(), "f_w", a.id(), "theta").unwrap().abs() > 1e-6,
        "the exact mass must couple w and theta"
    );
    Ok(())
}

// ─── Frame3d (space frame) ──────────────────────────────────────────────────

/// A single SEG2 along global x in 3-D, with its FE space + two nodes.
fn bar_3d(len: f64) -> Result<(FiniteElementSpace, Node, Node)> {
    let coords = Handle::new(Coords::new(3)?);
    let a = Node::create_in(coords.clone(), &[0.0, 0.0, 0.0])?;
    let b = Node::create_in(coords.clone(), &[len, 0.0, 0.0])?;
    let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
    mesh.add_cell(&[a.id(), b.id()])?;
    Ok((
        FiniteElementSpace::new(&mesh, Interpolation::ModelEmbedded)?,
        a,
        b,
    ))
}

fn frame3d_materials(model: &Model, iy: f64, iz: f64, area: f64, rho: f64) -> Result<ElementField> {
    pyrucast::ops::element_field::material_field(
        model,
        &[
            ("E", 1.0),
            ("A", area),
            ("I_y", iy),
            ("I_z", iz),
            ("J", 1.0),
            ("G", 1.0),
            ("A_sy", 1.0),
            ("A_sz", 1.0),
            ("rho", rho),
        ],
    )
}

#[test]
fn frame3d_consistent_mass_matches_closed_form() -> Result<()> {
    const RHO: f64 = 2.0;
    const AREA: f64 = 3.0;
    const IY: f64 = 0.4;
    const IZ: f64 = 0.7;
    const L: f64 = 2.0;
    let (fes, a, b) = bar_3d(L)?; // along x ⇒ local axes = global ⇒ T = identity
    let model = model::timoshenko(&fes)?;
    let materials = frame3d_materials(&model, IY, IZ, AREA, RHO)?;

    let m = pyrucast::ops::matrix::mass(&model, &materials)?;
    let tol = 1e-12;
    let diag = |sec: f64| 2.0 * RHO * sec * L / 6.0;
    // Axial and torsion keep the linear field's mass — exact for what those two
    // degrees of freedom actually interpolate.
    assert!((m.get(a.id(), "f_x", a.id(), "u_x").unwrap() - diag(AREA)).abs() < tol); // u'
    assert!((m.get(a.id(), "m_x", a.id(), "r_x").unwrap() - diag(IY + IZ)).abs() < tol); // torsion I_p
                                                                                         // The two bending planes take the exact element's mass. Each still carries
                                                                                         // exactly the member's mass under a rigid translation…
    for (dual, primal) in [("f_y", "u_y"), ("f_z", "u_z")] {
        let total = m.get(a.id(), dual, a.id(), primal).unwrap()
            + m.get(a.id(), dual, b.id(), primal).unwrap()
            + m.get(b.id(), dual, a.id(), primal).unwrap()
            + m.get(b.id(), dual, b.id(), primal).unwrap();
        assert!(
            (total - RHO * AREA * L).abs() < 1e-10 * RHO * AREA * L,
            "{dual}: {total} vs {}",
            RHO * AREA * L
        );
    }
    // …and now couples the deflection to the rotation of its own plane, which
    // the linear mass never did.
    assert!(m.get(a.id(), "f_y", a.id(), "r_z").unwrap().abs() > 1e-6);
    assert!(m.get(a.id(), "f_z", a.id(), "r_y").unwrap().abs() > 1e-6);
    Ok(())
}

#[test]
fn frame3d_geometric_stiffens_both_transverse() -> Result<()> {
    const N: f64 = 8.0;
    const L: f64 = 4.0;
    let (fes, a, b) = bar_3d(L)?;
    let model = model::timoshenko(&fes)?;
    let materials = frame3d_materials(&model, 1.0, 1.0, 1.0, 1.0)?;
    let sub = SubElementField::from_uniform_per_component(fes.get(0)?, vec!["N".into()], &[N])?;
    let mut state = ElementField::empty();
    state.add_sub(Handle::new(sub))?;

    let kg = pyrucast::ops::matrix::geometric(&model, &materials, &state)?;
    let tol = 1e-12;
    let g = N / L;
    // Both transverse translations (u_y = v', u_z = w') are stiffened.
    assert!((kg.get(a.id(), "f_y", a.id(), "u_y").unwrap() - g).abs() < tol);
    assert!((kg.get(a.id(), "f_y", b.id(), "u_y").unwrap() + g).abs() < tol);
    assert!((kg.get(a.id(), "f_z", a.id(), "u_z").unwrap() - g).abs() < tol);
    // Axial and torsion carry no geometric term.
    assert!(kg.get(a.id(), "f_x", a.id(), "u_x").unwrap().abs() < tol);
    assert!(kg.get(a.id(), "m_x", a.id(), "r_x").unwrap().abs() < tol);
    Ok(())
}