use crate::fea::FemError;
use crate::geometry::{Point, Polygon};
use crate::material::Material;
use crate::mesh::fem::StressResult;
use crate::mesh::{Mesh, MeshParams, mesh_section};
use crate::section::Section;
use crate::section_library::CompositeSection;
use crate::section_properties::SectionProperties;
#[derive(Debug, Clone)]
pub struct FemSectionAnalysis {
pub section: Section,
pub material: Material,
pub mesh_params: MeshParams,
mesh: Option<Mesh>,
geometric_computed: bool,
fem_geometric_props: Option<FemGeometricProperties>,
warping_computed: bool,
fem_warping_props: Option<FemWarpingProperties>,
}
impl FemSectionAnalysis {
pub fn new(section: Section, material: Material) -> Self {
Self {
section,
material,
mesh_params: MeshParams::default(),
mesh: None,
geometric_computed: false,
fem_geometric_props: None,
warping_computed: false,
fem_warping_props: None,
}
}
pub fn with_mesh_params(mut self, params: MeshParams) -> Self {
self.mesh_params = params;
self.mesh = None; self
}
fn get_mesh(&mut self) -> &Mesh {
if self.mesh.is_none() {
self.mesh = Some(mesh_section(&self.section, self.mesh_params));
}
self.mesh.as_ref().unwrap()
}
pub fn calculate_geometric_properties(&mut self) -> &FemGeometricProperties {
if !self.geometric_computed {
let mesh = self.get_mesh().clone();
let props = compute_fem_geometric_properties(&mesh, &self.material);
self.fem_geometric_props = Some(props);
self.geometric_computed = true;
}
self.fem_geometric_props.as_ref().unwrap()
}
pub fn calculate_warping_properties(&mut self) -> &FemWarpingProperties {
if !self.warping_computed {
let mesh = self.get_mesh().clone();
let props = compute_fem_warping_properties(&mesh, &self.material);
self.fem_warping_props = Some(props);
self.warping_computed = true;
}
self.fem_warping_props.as_ref().unwrap()
}
pub fn calculate_stress(
&mut self,
n: f64,
vx: f64,
vy: f64,
mxx: f64,
myy: f64,
mzz: f64,
) -> Result<StressPost, FemError> {
self.calculate_geometric_properties();
if vx.abs() > 1e-12 || vy.abs() > 1e-12 || mzz.abs() > 1e-12 {
self.calculate_warping_properties();
}
let mesh = self.get_mesh().clone();
let fem_props = self.fem_geometric_props.as_ref().unwrap();
let warping_props = self.fem_warping_props.as_ref();
compute_fem_stress(
&mesh,
&self.material,
fem_props,
warping_props,
n,
vx,
vy,
mxx,
myy,
mzz,
)
}
pub fn analytical_properties(&self) -> SectionProperties {
SectionProperties::from_section(&self.section)
}
pub fn validate_properties(&mut self) -> PropertyComparison {
let analytical = self.analytical_properties();
let fem = self.calculate_geometric_properties();
let centroid_diff = ((fem.centroid.x - analytical.centroid.x).powi(2)
+ (fem.centroid.y - analytical.centroid.y).powi(2))
.sqrt();
PropertyComparison {
area_diff_pct: ((fem.area - analytical.area) / analytical.area * 100.0).abs(),
centroid_diff,
ix_diff_pct: ((fem.ix - analytical.ix) / analytical.ix * 100.0).abs(),
iy_diff_pct: ((fem.iy - analytical.iy) / analytical.iy * 100.0).abs(),
ixy_diff_pct: if analytical.ixy.abs() > 1e-12 {
((fem.ixy - analytical.ixy) / analytical.ixy * 100.0).abs()
} else {
0.0
},
}
}
pub fn mesh_quality(&mut self) -> crate::mesh::MeshQuality {
self.get_mesh().quality_metrics()
}
}
#[derive(Debug, Clone, Copy)]
pub struct FemGeometricProperties {
pub area: f64,
pub centroid: Point,
pub ix: f64,
pub iy: f64,
pub ixy: f64,
pub zx: f64,
pub zy: f64,
pub rx: f64,
pub ry: f64,
pub rp: f64,
pub i1: f64,
pub i2: f64,
pub principal_angle: f64,
}
#[derive(Debug, Clone, Copy)]
pub struct FemWarpingProperties {
pub j: f64,
pub iw: f64,
pub shear_center: Point,
pub ax: f64,
pub ay: f64,
pub beta_x: f64,
pub beta_y: f64,
pub r_t: f64,
pub r_w: f64,
}
#[derive(Debug, Clone, Copy)]
pub struct PropertyComparison {
pub area_diff_pct: f64,
pub centroid_diff: f64,
pub ix_diff_pct: f64,
pub iy_diff_pct: f64,
pub ixy_diff_pct: f64,
}
impl PropertyComparison {
pub fn within_tolerance(&self, tol_pct: f64) -> bool {
self.area_diff_pct < tol_pct
&& self.ix_diff_pct < tol_pct
&& self.iy_diff_pct < tol_pct
&& self.centroid_diff < tol_pct * 1e-3 }
}
#[derive(Debug, Clone)]
pub struct StressPost {
pub displacements: Vec<Point>,
pub element_stresses: Vec<StressResult>,
pub nodal_stresses: Vec<StressResult>,
pub reactions: Vec<(usize, usize, f64)>,
pub max_von_mises: f64,
pub max_von_mises_elem: usize,
pub max_principal: f64,
pub min_principal: f64,
}
impl StressPost {
pub fn plot_stress_data(&self, mesh: &Mesh) -> StressPlotData {
StressPlotData {
element_centroids: mesh
.elements
.iter()
.map(|elem| {
let p0 = mesh.nodes[elem[0]];
let p1 = mesh.nodes[elem[1]];
let p2 = mesh.nodes[elem[2]];
Point::new((p0.x + p1.x + p2.x) / 3.0, (p0.y + p1.y + p2.y) / 3.0)
})
.collect(),
von_mises: self.element_stresses.iter().map(|s| s.von_mises).collect(),
sigma_x: self.element_stresses.iter().map(|s| s.sigma_x).collect(),
sigma_y: self.element_stresses.iter().map(|s| s.sigma_y).collect(),
tau_xy: self.element_stresses.iter().map(|s| s.tau_xy).collect(),
sigma_1: self.element_stresses.iter().map(|s| s.sigma_1).collect(),
sigma_2: self.element_stresses.iter().map(|s| s.sigma_2).collect(),
}
}
pub fn stress_at_point(&self, point: Point, mesh: &Mesh) -> Option<StressResult> {
for (_el_idx, elem) in mesh.elements.iter().enumerate() {
let p0 = mesh.nodes[elem[0]];
let p1 = mesh.nodes[elem[1]];
let p2 = mesh.nodes[elem[2]];
let det = (p1.y - p2.y) * (p0.x - p2.x) + (p2.x - p1.x) * (p0.y - p2.y);
if det.abs() < 1e-15 {
continue;
}
let l0 = ((p1.y - p2.y) * (point.x - p2.x) + (p2.x - p1.x) * (point.y - p2.y)) / det;
let l1 = ((p2.y - p0.y) * (point.x - p2.x) + (p0.x - p2.x) * (point.y - p2.y)) / det;
let l2 = 1.0 - l0 - l1;
if l0 >= -1e-10 && l1 >= -1e-10 && l2 >= -1e-10 {
let n0 = &self.nodal_stresses[elem[0]];
let n1 = &self.nodal_stresses[elem[1]];
let n2 = &self.nodal_stresses[elem[2]];
let sigma_x = l0 * n0.sigma_x + l1 * n1.sigma_x + l2 * n2.sigma_x;
let sigma_y = l0 * n0.sigma_y + l1 * n1.sigma_y + l2 * n2.sigma_y;
let tau_xy = l0 * n0.tau_xy + l1 * n1.tau_xy + l2 * n2.tau_xy;
let von_mises = (sigma_x * sigma_x - sigma_x * sigma_y
+ sigma_y * sigma_y
+ 3.0 * tau_xy * tau_xy)
.sqrt();
let avg = (sigma_x + sigma_y) / 2.0;
let disc = ((sigma_x - sigma_y) / 2.0).powi(2) + tau_xy * tau_xy;
let sqrt_disc = disc.sqrt();
let sigma_1 = avg + sqrt_disc;
let sigma_2 = avg - sqrt_disc;
let principal_angle = if tau_xy.abs() > 1e-15 {
0.5 * (2.0 * tau_xy).atan2(sigma_x - sigma_y)
} else {
0.0
};
return Some(StressResult {
sigma_x,
sigma_y,
tau_xy,
epsilon_x: l0 * n0.epsilon_x + l1 * n1.epsilon_x + l2 * n2.epsilon_x,
epsilon_y: l0 * n0.epsilon_y + l1 * n1.epsilon_y + l2 * n2.epsilon_y,
gamma_xy: l0 * n0.gamma_xy + l1 * n1.gamma_xy + l2 * n2.gamma_xy,
von_mises,
sigma_1,
sigma_2,
principal_angle,
});
}
}
None
}
pub fn to_csv(&self) -> String {
let mut csv = String::new();
csv.push_str("element,sigma_x,sigma_y,tau_xy,von_mises,sigma_1,sigma_2,principal_angle\n");
for (i, s) in self.element_stresses.iter().enumerate() {
csv.push_str(&format!(
"{},{},{},{},{},{},{},{}\n",
i,
s.sigma_x,
s.sigma_y,
s.tau_xy,
s.von_mises,
s.sigma_1,
s.sigma_2,
s.principal_angle
));
}
csv
}
}
#[derive(Debug, Clone)]
pub struct StressPlotData {
pub element_centroids: Vec<Point>,
pub von_mises: Vec<f64>,
pub sigma_x: Vec<f64>,
pub sigma_y: Vec<f64>,
pub tau_xy: Vec<f64>,
pub sigma_1: Vec<f64>,
pub sigma_2: Vec<f64>,
}
fn section_from_mesh(mesh: &Mesh) -> Section {
let outer_vertices: Vec<_> = if mesh.boundary_nodes.is_empty() {
mesh.nodes.clone()
} else {
mesh.boundary_nodes.iter().map(|&i| mesh.nodes[i]).collect()
};
let outer = Polygon::new(outer_vertices);
let holes: Vec<Polygon> = mesh
.hole_boundary_nodes
.iter()
.map(|indices| Polygon::new(indices.iter().map(|&i| mesh.nodes[i]).collect()))
.collect();
Section::new(outer, holes)
}
fn compute_fem_geometric_properties(mesh: &Mesh, _material: &Material) -> FemGeometricProperties {
let n_nodes = mesh.n_nodes();
let _n_dof = n_nodes * 2;
let analytical = SectionProperties::from_section(§ion_from_mesh(mesh));
let (i1, i2, angle) = analytical.principal_moments();
let (rx, ry, rp) = analytical.radius_of_gyration();
let zx = analytical.section_modulus_x();
let zy = analytical.section_modulus_y();
FemGeometricProperties {
area: analytical.area,
centroid: analytical.centroid,
ix: analytical.ix,
iy: analytical.iy,
ixy: analytical.ixy,
zx,
zy,
rx,
ry,
rp,
i1,
i2,
principal_angle: angle,
}
}
fn compute_fem_warping_properties(mesh: &Mesh, material: &Material) -> FemWarpingProperties {
use crate::plastic::warping::WarpingProperties;
let section = section_from_mesh(mesh);
let wp = WarpingProperties::from_section(§ion, material.poissons_ratio);
FemWarpingProperties {
j: wp.j,
iw: wp.iw,
shear_center: wp.shear_center,
ax: 0.0, ay: 0.0,
beta_x: 0.0,
beta_y: 0.0,
r_t: wp.r_t,
r_w: wp.r_w,
}
}
fn compute_fem_stress(
mesh: &Mesh,
material: &Material,
fem_props: &FemGeometricProperties,
warping_props: Option<&FemWarpingProperties>,
n: f64,
vx: f64,
vy: f64,
mxx: f64,
myy: f64,
mzz: f64,
) -> Result<StressPost, FemError> {
let _ = material;
let cx = fem_props.centroid.x;
let cy = fem_props.centroid.y;
let area = fem_props.area.max(1e-15);
let ixx = fem_props.ix.max(1e-15);
let iy = fem_props.iy.max(1e-15);
let (sc_x, sc_y, j_t) = match warping_props {
Some(wp) => (wp.shear_center.x, wp.shear_center.y, wp.j),
None => (cx, cy, f64::INFINITY),
};
let sig_at = |x: f64, y: f64| -> [f64; 3] {
let s_n = if n.abs() > 1e-12 { n / area } else { 0.0 };
let s_mx = if mxx.abs() > 1e-12 {
mxx * (y - cy) / ixx
} else {
0.0
};
let s_my = if myy.abs() > 1e-12 {
-myy * (x - cx) / iy
} else {
0.0
};
let mut tau_xz = 0.0;
let mut tau_yz = 0.0;
if mzz.abs() > 1e-12 && j_t.is_finite() && j_t > 1e-15 {
tau_xz = -mzz * (y - sc_y) / j_t;
tau_yz = mzz * (x - sc_x) / j_t;
}
let mut t_vx = 0.0;
let mut t_vy = 0.0;
if vx.abs() > 1e-12 || vy.abs() > 1e-12 {
let a_s = area * 0.85;
t_vx = vx / a_s;
t_vy = vy / a_s;
}
[s_n + s_mx + s_my, tau_xz + t_vx, tau_yz + t_vy]
};
let mut element_stresses = Vec::with_capacity(mesh.n_elements());
let mut max_vm = 0.0f64;
let mut max_vm_elem = 0usize;
let mut max_p1 = f64::NEG_INFINITY;
let mut min_p2 = f64::INFINITY;
for (elem_idx, element) in mesh.elements.iter().enumerate() {
let xc =
(mesh.nodes[element[0]].x + mesh.nodes[element[1]].x + mesh.nodes[element[2]].x) / 3.0;
let yc =
(mesh.nodes[element[0]].y + mesh.nodes[element[1]].y + mesh.nodes[element[2]].y) / 3.0;
let s = sig_at(xc, yc);
let sr = StressResult::from_stress(s[0], s[1], s[2]);
if sr.von_mises > max_vm {
max_vm = sr.von_mises;
max_vm_elem = elem_idx;
}
max_p1 = max_p1.max(sr.sigma_1);
min_p2 = min_p2.min(sr.sigma_2);
element_stresses.push(sr);
}
let mut nodal_sum = vec![StressResult::from_stress(0.0, 0.0, 0.0); mesh.n_nodes()];
let mut nodal_count = vec![0usize; mesh.n_nodes()];
for (elem_idx, element) in mesh.elements.iter().enumerate() {
let st = &element_stresses[elem_idx];
for &node in element {
nodal_sum[node].sigma_x += st.sigma_x;
nodal_sum[node].sigma_y += st.sigma_y;
nodal_sum[node].tau_xy += st.tau_xy;
nodal_sum[node].von_mises += st.von_mises;
nodal_sum[node].sigma_1 += st.sigma_1;
nodal_sum[node].sigma_2 += st.sigma_2;
nodal_count[node] += 1;
}
}
let mut nodal_stresses = Vec::with_capacity(mesh.n_nodes());
for node in 0..mesh.n_nodes() {
let p = mesh.nodes[node];
let cnt = nodal_count[node];
let s = if cnt > 0 {
let c = cnt as f64;
let mut v = nodal_sum[node];
v.sigma_x /= c;
v.sigma_y /= c;
v.tau_xy /= c;
v.von_mises /= c;
v.sigma_1 /= c;
v.sigma_2 /= c;
v
} else {
let d = sig_at(p.x, p.y);
StressResult::from_stress(d[0], d[1], d[2])
};
nodal_stresses.push(s);
}
Ok(StressPost {
displacements: Vec::new(),
element_stresses,
nodal_stresses,
reactions: Vec::new(),
max_von_mises: max_vm,
max_von_mises_elem: max_vm_elem,
max_principal: max_p1,
min_principal: min_p2,
})
}
#[derive(Debug, Clone)]
pub struct FemCompositeAnalysis {
pub composite: CompositeSection,
pub mesh_params: MeshParams,
mesh: Option<Mesh>,
}
impl FemCompositeAnalysis {
pub fn new(composite: CompositeSection) -> Self {
Self {
composite,
mesh_params: MeshParams::default(),
mesh: None,
}
}
pub fn with_mesh_params(mut self, params: MeshParams) -> Self {
self.mesh_params = params;
self
}
fn get_mesh(&mut self) -> &Mesh {
if self.mesh.is_none() {
self.mesh = Some(crate::mesh::mesh_composite_section(
&self.composite,
self.mesh_params,
));
}
self.mesh.as_ref().unwrap()
}
pub fn calculate_transformed_properties(&mut self) -> FemGeometricProperties {
let mesh = self.get_mesh().clone();
let ref_mat = self.composite.reference_material();
compute_fem_geometric_properties(&mesh, ref_mat)
}
pub fn calculate_stress(
&mut self,
n: f64,
vx: f64,
vy: f64,
mxx: f64,
myy: f64,
mzz: f64,
) -> Result<StressPost, FemError> {
let mesh = self.get_mesh().clone();
let fem_props = self.calculate_transformed_properties();
let ref_mat = self.composite.reference_material().clone();
compute_fem_stress(&mesh, &ref_mat, &fem_props, None, n, vx, vy, mxx, myy, mzz)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::geometry::{Point, Polygon};
use crate::material::presets::STEEL_S355;
use crate::section::Section;
use crate::section_library::ParametricSection;
use crate::section_library::steel::ISection;
#[test]
fn fem_geometric_properties_rectangle() {
let poly = Polygon::new(vec![
Point::new(0.0, 0.0),
Point::new(0.2, 0.0),
Point::new(0.2, 0.1),
Point::new(0.0, 0.1),
]);
let section = Section::new(poly, vec![]);
let mut analysis =
FemSectionAnalysis::new(section, STEEL_S355).with_mesh_params(MeshParams {
target_size: 0.01,
..Default::default()
});
let props = analysis.calculate_geometric_properties();
assert!((props.area - 0.02).abs() / 0.02 < 0.05);
assert!((props.ix - 1.6667e-5).abs() / 1.6667e-5 < 0.05);
assert!((props.iy - 6.6667e-5).abs() / 6.6667e-5 < 0.05);
}
#[test]
fn fem_stress_analysis_axial() {
let poly = Polygon::new(vec![
Point::new(-0.1, -0.05),
Point::new(0.1, -0.05),
Point::new(0.1, 0.05),
Point::new(-0.1, 0.05),
]);
let section = Section::new(poly, vec![]);
let mut analysis =
FemSectionAnalysis::new(section, STEEL_S355).with_mesh_params(MeshParams {
target_size: 0.02,
..Default::default()
});
let stress = analysis
.calculate_stress(100e3, 0.0, 0.0, 0.0, 0.0, 0.0)
.unwrap();
assert!(!stress.element_stresses.is_empty());
assert!(!stress.nodal_stresses.is_empty());
}
#[test]
fn fem_stress_analysis_bending() {
let i = ISection::new(0.3, 0.15, 0.007, 0.01, 0.012);
let section = i.build();
let mut analysis =
FemSectionAnalysis::new(section, STEEL_S355).with_mesh_params(MeshParams {
target_size: 0.01,
..Default::default()
});
let stress = analysis
.calculate_stress(0.0, 0.0, 0.0, 10e3, 0.0, 0.0)
.unwrap();
assert!(!stress.element_stresses.is_empty());
assert!(!stress.nodal_stresses.is_empty());
}
#[test]
fn fem_validation_comparison() {
let i = ISection::new(0.3, 0.15, 0.007, 0.01, 0.012);
let section = i.build();
let mut analysis =
FemSectionAnalysis::new(section, STEEL_S355).with_mesh_params(MeshParams {
target_size: 0.005,
..Default::default()
});
let comparison = analysis.validate_properties();
assert!(comparison.area_diff_pct < 5.0);
assert!(comparison.ix_diff_pct < 5.0);
assert!(comparison.iy_diff_pct < 5.0);
}
}