use crate::geometry::Point;
use crate::mesh::{Mesh, MeshParams, mesh_section};
use crate::section::Section;
#[derive(Debug, Clone, Copy)]
pub struct Fiber {
pub area: f64,
pub centroid: Point,
pub material: usize,
}
impl Fiber {
pub fn ea(&self, e: f64) -> f64 {
self.area * e
}
}
pub fn to_fibre_section(section: &Section, params: MeshParams) -> Vec<Fiber> {
let mesh: Mesh = mesh_section(section, params);
to_fibre_from_mesh(&mesh)
}
pub fn to_fibre_from_mesh(mesh: &Mesh) -> Vec<Fiber> {
let mut fibres = Vec::with_capacity(mesh.n_elements());
for i in 0..mesh.n_elements() {
fibres.push(Fiber {
area: mesh.element_area(i),
centroid: mesh.element_centroid(i),
material: mesh.element_materials.get(i).copied().unwrap_or(0),
});
}
fibres
}
pub fn total_area(fibres: &[Fiber]) -> f64 {
fibres.iter().map(|f| f.area).sum()
}
pub fn plastic_axial(fibres: &[Fiber], fy: f64) -> f64 {
total_area(fibres) * fy
}
pub fn plastic_moment_x(fibres: &[Fiber], fy: f64) -> f64 {
let total: f64 = fibres.iter().map(|f| f.area).sum();
let half = total / 2.0;
let mut sorted: Vec<&Fiber> = fibres.iter().collect();
sorted.sort_by(|a, b| a.centroid.y.partial_cmp(&b.centroid.y).unwrap());
let mut cum = 0.0;
let mut y_na = sorted.first().map(|f| f.centroid.y).unwrap_or(0.0);
for w in sorted.windows(2) {
let (lo, hi) = (w[0].centroid.y, w[1].centroid.y);
if cum + w[0].area >= half {
y_na = lo;
break;
}
cum += w[0].area;
y_na = hi;
}
let _ = y_na;
let mut cum2 = 0.0;
let mut na = sorted.last().map(|f| f.centroid.y).unwrap_or(0.0);
for w in sorted.windows(2) {
let (a_lo, y_lo) = (w[0].area, w[0].centroid.y);
if cum2 + a_lo >= half {
let frac = (half - cum2) / a_lo.max(1e-15);
na = y_lo + frac * (w[1].centroid.y - y_lo);
break;
}
cum2 += a_lo;
na = w[1].centroid.y;
}
let _ = y_na;
let mut m = 0.0f64;
for f in fibres {
m += f.area * (f.centroid.y - na).abs();
}
m * fy
}
#[cfg(test)]
mod tests {
use super::*;
use crate::geometry::Polygon;
fn rect_section(w: f64, h: f64) -> Section {
Section::new(
Polygon::new(vec![
Point::new(-w / 2.0, -h / 2.0),
Point::new(w / 2.0, -h / 2.0),
Point::new(w / 2.0, h / 2.0),
Point::new(-w / 2.0, h / 2.0),
]),
vec![],
)
}
#[test]
fn fibre_total_area_matches() {
let sec = rect_section(0.1, 0.2);
let fibres = to_fibre_section(
&sec,
MeshParams {
target_size: 0.02,
..Default::default()
},
);
assert!(rel_err(total_area(&fibres), 0.02) < 1e-3);
}
#[test]
fn fibre_plastic_moment_rectangle() {
let sec = rect_section(0.1, 0.2);
let fy = 355.0e6;
let fibres = to_fibre_section(
&sec,
MeshParams {
target_size: 0.01,
..Default::default()
},
);
let m_pl = plastic_moment_x(&fibres, fy);
let expected = 0.1 * 0.04 / 4.0 * fy; assert!(
(m_pl - expected).abs() / expected < 0.02,
"Mpl={} expected={}",
m_pl,
expected
);
}
fn rel_err(a: f64, b: f64) -> f64 {
((a - b) / b).abs()
}
}