use crate::atoms::ElementType;
use crate::containers::element_field::SubElementField;
use crate::containers::finite_element_space::SubFiniteElementSpace;
use crate::containers::matrix::DofOrdering;
use crate::containers::mesh::SubMesh;
use crate::dump::DumpOptions;
use crate::error::{PyrucastError, Result};
use crate::handle::Handle;
use crate::models::beam::{bending_4x4, mass_4x4, BeamModel};
use crate::models::owned_components;
use crate::models::{frame, frame3d, CellGeom, Domain, MatrixLayout, Physics, SubModelKind};
use serde::{Deserialize, Serialize};
fn material_of(model: BeamModel) -> &'static [&'static str] {
match model {
BeamModel::Planar1d => &["E", "I", "G", "A_s"],
BeamModel::Frame2d => &["E", "A", "I", "G", "A_s"],
BeamModel::Frame3d => &["E", "A", "I_y", "I_z", "J", "G", "A_sy", "A_sz"],
}
}
fn behavior_of(model: BeamModel) -> &'static [&'static str] {
match model {
BeamModel::Planar1d => &["M", "V"],
BeamModel::Frame2d => &["N", "M", "V"],
BeamModel::Frame3d => &["N", "M_y", "M_z", "T", "V_y", "V_z"],
}
}
#[derive(Clone, Serialize, Deserialize)]
pub struct Timoshenko {
pub(crate) fespace: Handle<SubFiniteElementSpace>,
pub(crate) support: Handle<SubMesh>,
pub(crate) model: BeamModel,
}
impl Timoshenko {
pub fn new(fespace: Handle<SubFiniteElementSpace>) -> Result<Self> {
let (submesh, space_dim, et, axisymmetric, interpolation) = {
let s = fespace.read();
(
s.submesh(),
s.space_dim(),
s.element_type()?,
s.is_axisymmetric(),
s.interpolation(),
)
};
if et != ElementType::SEG2 {
return Err(PyrucastError::Message(format!(
"Timoshenko: a beam needs SEG2 elements, got {et}"
)));
}
if !interpolation.is_model_embedded() {
return Err(PyrucastError::Message(format!(
"Timoshenko: this element is the exact beam, whose interpolation is \
cubic/quadratic and depends on the material through Φ = 12EI/(G·A_s·L²) — it is \
owned by the formulation, not by the space. Build the subspace with \
`Interpolation::ModelEmbedded`; got {interpolation}."
)));
}
if axisymmetric {
return Err(PyrucastError::Message(
"Timoshenko: a segment in a meridian plane is a shell of revolution, not a beam"
.into(),
));
}
let model = BeamModel::from_space_dim(space_dim)
.map_err(|e| PyrucastError::Message(format!("Timoshenko: {e}")))?;
let support = submesh.read().to_poi1()?;
Ok(Self {
fespace,
support,
model,
})
}
fn layout(&self) -> MatrixLayout {
MatrixLayout {
fespaces: vec![self.fespace.clone()],
support: self.support.clone(),
dual_vars: self.dual_vars(),
primal_vars: self.primal_vars(),
ordering: DofOrdering::NodesThenVars,
symmetric: true,
}
}
}
impl SubModelKind for Timoshenko {
fn primal_vars(&self) -> Vec<String> {
self.model.primal().iter().map(|s| s.to_string()).collect()
}
fn dual_vars(&self) -> Vec<String> {
self.model.dual().iter().map(|s| s.to_string()).collect()
}
fn as_domain(&self) -> Option<&dyn Domain> {
Some(self)
}
fn stiffness_layout(&self) -> Option<MatrixLayout> {
Some(self.layout())
}
fn mass_layout(&self) -> Option<MatrixLayout> {
Some(self.layout())
}
fn geometric_layout(&self) -> Option<MatrixLayout> {
match self.model {
BeamModel::Planar1d => None,
_ => Some(self.layout()),
}
}
fn element_matrix(
&self,
geoms: &[CellGeom],
material: Option<&SubElementField>,
ke: &mut [f64],
) -> Result<()> {
let geom = &geoms[0];
let mat = material.expect("Timoshenko declares a material_fespace");
match self.model {
BeamModel::Planar1d => planar_stiffness(geom, mat, ke),
BeamModel::Frame2d => frame::element_stiffness(geom, mat, ke),
BeamModel::Frame3d => frame3d::element_stiffness(geom, mat, ke),
}
}
fn element_mass(
&self,
geoms: &[CellGeom],
material: Option<&SubElementField>,
ke: &mut [f64],
) -> Result<()> {
let geom = &geoms[0];
let mat = material.expect("Timoshenko declares a material_fespace");
match self.model {
BeamModel::Planar1d => planar_mass(geom, mat, ke),
BeamModel::Frame2d => frame::element_mass(geom, mat, ke),
BeamModel::Frame3d => frame3d::element_mass(geom, mat, ke),
}
}
fn element_geometric(
&self,
geoms: &[CellGeom],
_material: Option<&SubElementField>,
state: Option<&SubElementField>,
ke: &mut [f64],
) -> Result<()> {
let geom = &geoms[0];
let stress = state.expect("the geometric stiffness requires the axial force `N`");
match self.model {
BeamModel::Planar1d => Err(PyrucastError::Message(
"Timoshenko: a pure-bending beam carries no axial force, so it has no geometric \
stiffness — use a 2-D or 3-D configuration"
.into(),
)),
BeamModel::Frame2d => frame::element_geometric(geom, stress, ke),
BeamModel::Frame3d => frame3d::element_geometric(geom, stress, ke),
}
}
fn physics(&self) -> &'static [Physics] {
&[Physics::Mechanical]
}
fn label(&self) -> &'static str {
"Timoshenko"
}
fn render(&self, _opts: &DumpOptions) -> String {
let primal = self.primal_vars().join(", ");
let dual = self.dual_vars().join(", ");
let n = self.support.read().cell_count();
format!(
"SubModel<Timoshenko({})>\n primal var(s): {primal}\n dual var(s): {dual}\n \
support: {n} node(s)",
self.model
)
}
}
impl Domain for Timoshenko {
fn material_fespace(&self) -> Handle<SubFiniteElementSpace> {
self.fespace.clone()
}
fn material_components(&self) -> Option<Vec<String>> {
Some(owned_components(material_of(self.model)))
}
fn optional_material_components(&self) -> &'static [&'static str] {
match self.model {
BeamModel::Planar1d => &["A", "rho"],
_ => &["rho"],
}
}
fn behavior_fespace(&self) -> Handle<SubFiniteElementSpace> {
self.fespace.clone()
}
fn behavior_output_components(&self) -> Result<Vec<String>> {
Ok(behavior_of(self.model)
.iter()
.map(|s| s.to_string())
.collect())
}
fn integrate_point(
&self,
geom: &CellGeom,
input: &SubElementField,
_prev: Option<&SubElementField>,
material: Option<&SubElementField>,
g: usize,
_dt: Option<f64>,
out: &mut [f64],
) -> Result<()> {
let mat = material.expect("Timoshenko declares a material_fespace");
let cell = geom.cell;
let v = |c| mat.value(cell, 0, c);
let e = v("E")?;
let gg = v("G")?;
let read_in = |name: &str| input.value(cell, g, name);
match self.model {
BeamModel::Planar1d => {
out[0] = e * v("I")? * read_in("kappa")?;
out[1] = gg * v("A_s")? * read_in("gamma")?;
}
BeamModel::Frame2d => {
out[0] = e * v("A")? * read_in("eps")?;
out[1] = e * v("I")? * read_in("kappa")?;
out[2] = gg * v("A_s")? * read_in("gamma")?;
}
BeamModel::Frame3d => {
out[0] = e * v("A")? * read_in("eps")?;
out[1] = e * v("I_y")? * read_in("kappa_y")?;
out[2] = e * v("I_z")? * read_in("kappa_z")?;
out[3] = gg * v("J")? * read_in("torsion")?;
out[4] = gg * v("A_sy")? * read_in("gamma_y")?;
out[5] = gg * v("A_sz")? * read_in("gamma_z")?;
}
}
Ok(())
}
}
fn planar_stiffness(geom: &CellGeom, material: &SubElementField, ke: &mut [f64]) -> Result<()> {
let cell = geom.cell;
let (xa, xb) = (geom.node_coord(0)?, geom.node_coord(1)?);
let l = (xb[0] - xa[0]).abs();
if l <= f64::EPSILON {
return Err(PyrucastError::Message(format!(
"Timoshenko: cell {cell} has zero length"
)));
}
let ei = material.value(cell, 0, "E")? * material.value(cell, 0, "I")?;
let gas = material.value(cell, 0, "G")? * material.value(cell, 0, "A_s")?;
let k = bending_4x4(ei, Some(gas), l);
for (r, row) in k.iter().enumerate() {
for (c, v) in row.iter().enumerate() {
ke[r * 4 + c] += v;
}
}
Ok(())
}
fn planar_mass(geom: &CellGeom, material: &SubElementField, ke: &mut [f64]) -> Result<()> {
let cell = geom.cell;
let (xa, xb) = (geom.node_coord(0)?, geom.node_coord(1)?);
let l = (xb[0] - xa[0]).abs();
let rho = material.value(cell, 0, "rho").map_err(|_| {
PyrucastError::Message(
"Timoshenko mass matrix: material component `rho` (density) is required".into(),
)
})?;
let i = material.value(cell, 0, "I")?;
let ei = material.value(cell, 0, "E")? * i;
let gas = material.value(cell, 0, "G")? * material.value(cell, 0, "A_s")?;
let m = mass_4x4(
rho * material.value(cell, 0, "A")?,
rho * i,
ei,
Some(gas),
l,
);
for (r, row) in m.iter().enumerate() {
for (c, v) in row.iter().enumerate() {
ke[r * 4 + c] += v;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::aggregate::Aggregate;
use crate::atoms::{Interpolation, Node, NodeId};
use crate::containers::field::SubField;
use crate::containers::finite_element_space::FiniteElementSpace;
use crate::containers::mesh::Mesh;
use crate::coords::Coords;
use crate::handle::Handle;
fn one_beam(a: &[f64], b: &[f64]) -> (Timoshenko, NodeId, NodeId) {
let coords = Handle::new(Coords::new(a.len() as u8).unwrap());
let na = Node::create_in(coords.clone(), a).unwrap();
let nb = Node::create_in(coords.clone(), b).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
mesh.add_cell(&[na.id(), nb.id()]).unwrap();
let fes = FiniteElementSpace::new(&mesh, Interpolation::ModelEmbedded).unwrap();
let beam = Timoshenko::new(fes.get(0).unwrap()).unwrap();
(beam, na.id(), nb.id())
}
fn material(beam: &Timoshenko, pairs: &[(&str, f64)]) -> Handle<SubElementField> {
let names: Vec<String> = pairs.iter().map(|(c, _)| (*c).to_string()).collect();
let mut m = SubElementField::new(beam.fespace.clone(), names).unwrap();
for (c, v) in pairs {
m.set_uniform(c, *v).unwrap();
}
Handle::new(m)
}
#[test]
fn the_configuration_follows_the_dimension() {
let (b1, _, _) = one_beam(&[0.0], &[2.0]);
assert_eq!(b1.primal_vars(), ["w", "theta"]);
let (b2, _, _) = one_beam(&[0.0, 0.0], &[2.0, 0.0]);
assert_eq!(b2.primal_vars(), ["u_x", "u_y", "r_z"]);
let (b3, _, _) = one_beam(&[0.0, 0.0, 0.0], &[2.0, 0.0, 0.0]);
assert_eq!(b3.primal_vars().len(), 6);
}
#[test]
fn a_lagrange_subspace_is_refused() {
let coords = Handle::new(Coords::new(1).unwrap());
let a = Node::create_in(coords.clone(), &[0.0]).unwrap();
let b = Node::create_in(coords.clone(), &[1.0]).unwrap();
let mut mesh = Mesh::from_submesh(SubMesh::new(coords, ElementType::SEG2));
mesh.add_cell(&[a.id(), b.id()]).unwrap();
let fes = FiniteElementSpace::lagrange1(&mesh).unwrap();
let err = match Timoshenko::new(fes.get(0).unwrap()) {
Err(e) => e.to_string(),
Ok(_) => panic!("a Lagrange subspace should be refused"),
};
assert!(err.contains("ModelEmbedded"), "message: {err}");
}
#[test]
fn the_tip_stiffness_is_the_two_compliances_in_series() {
let l = 2.0;
let (e, i, g, a_s) = (3.0, 2.0, 5.0, 2.0);
let (ei, gas) = (e * i, g * a_s);
let series = 1.0 / (l * l * l / (12.0 * ei) + l / gas);
let (beam, a, _) = one_beam(&[0.0], &[l]);
let mat = material(&beam, &[("E", e), ("I", i), ("G", g), ("A_s", a_s)]);
let k = &beam.build_stiffness_blocks(Some(&mat)).unwrap()[0];
assert!((k.get(a, "f_w", a, "w") - series).abs() < 1e-9);
let (frame, a2, _) = one_beam(&[0.0, 0.0], &[l, 0.0]);
let mat = material(
&frame,
&[("E", e), ("A", 4.0), ("I", i), ("G", g), ("A_s", a_s)],
);
let k = &frame.build_stiffness_blocks(Some(&mat)).unwrap()[0];
assert!((k.get(a2, "f_y", a2, "u_y") - series).abs() < 1e-9);
assert!((k.get(a2, "f_x", a2, "u_x") - e * 4.0 / l).abs() < 1e-9);
assert!(k.get(a2, "f_x", a2, "u_y").abs() < 1e-9);
}
#[test]
fn a_vertical_member_finds_its_own_axes() {
let l = 2.0;
let (e, area) = (3.0, 4.0);
let (frame, a, _) = one_beam(&[0.0, 0.0], &[0.0, l]);
let mat = material(
&frame,
&[("E", e), ("A", area), ("I", 2.0), ("G", 5.0), ("A_s", 2.0)],
);
let k = &frame.build_stiffness_blocks(Some(&mat)).unwrap()[0];
assert!((k.get(a, "f_y", a, "u_y") - e * area / l).abs() < 1e-9);
assert!(k.get(a, "f_y", a, "u_x").abs() < 1e-9);
}
#[test]
fn a_pure_bending_beam_declares_no_geometric_stiffness() {
let (b1, _, _) = one_beam(&[0.0], &[2.0]);
assert!(b1.geometric_layout().is_none());
let (b2, _, _) = one_beam(&[0.0, 0.0], &[2.0, 0.0]);
assert!(b2.geometric_layout().is_some());
}
}