use section_properties::material::presets::STEEL_S355;
use section_properties::plastic::{PlasticAnalysis, PlasticAxis};
use section_properties::section_library::ParametricSection;
use section_properties::section_library::primitive::{
CircularHollowSection, CircularSection, RectangularSection,
};
use section_properties::section_library::steel::ISection;
use section_properties::stress::{SectionLoads, StressAnalysis};
use std::f64::consts::PI;
fn rel(a: f64, b: f64) -> f64 {
((a - b) / b.abs()).abs()
}
#[test]
fn report_i_section_warping_constant() {
let ipe = ISection::from_designation("IPE300").unwrap();
let sec = ipe.build();
let fp = sec.frame_properties_full(0.3);
let dims = ipe_dims();
let (d, bf, tw, tf, _r) = dims;
let hw = d - tf;
let iw_analytic = tf * bf.powi(3) / 24.0 * hw.powi(2);
println!("IPE300 (m): d={d} bf={bf} tw={tw} tf={tf}");
println!(
"Iw computed = {:.6e}, analytic = {:.6e}, rel err = {:.3e}",
fp.iw,
iw_analytic,
rel(fp.iw, iw_analytic)
);
assert!(rel(fp.iw, iw_analytic) < 0.05, "Iw mismatch");
}
fn ipe_dims() -> (f64, f64, f64, f64, f64) {
(0.300, 0.150, 0.0071, 0.0107, 0.015)
}
#[test]
fn report_chs_torsion_constant_bredt() {
let chs = CircularHollowSection::from_dimensions(0.2191, 0.0082);
let sec = chs.build();
let props = sec.frame_properties_full(0.3);
let ro = 0.10955;
let ri = ro - 0.0082;
let rm = (ro + ri) / 2.0;
let am = PI * rm * rm;
let s = 2.0 * PI * rm;
let j_bredt = 4.0 * am * am * 0.0082 / s;
println!(
"CHS J computed = {:.6e}, Bredt = {:.6e}, rel err = {:.3e}",
props.j,
j_bredt,
rel(props.j, j_bredt)
);
assert!(rel(props.j, j_bredt) < 0.05, "J mismatch");
}
#[test]
fn report_rectangle_plastic_modulus() {
let rect = RectangularSection::new(0.1, 0.2);
let sec = rect.build();
let mat = STEEL_S355;
let pa = PlasticAnalysis::new(sec.clone(), mat);
let pp = pa.plastic_section.plastic_properties(PlasticAxis::X);
let z_expected = 0.1 * 0.2_f64.powi(4).sqrt() * 0.2; let z_true = 0.1 * 0.04 / 4.0; println!(
"Zpl computed = {:.6e}, expected = {:.6e}",
pp.plastic_section_modulus, z_true
);
let _ = z_expected;
assert!(rel(pp.plastic_section_modulus, z_true) < 0.01);
}
#[test]
fn report_stress_superposition() {
let rect = RectangularSection::new(0.1, 0.2);
let sec = rect.build();
let analysis = StressAnalysis::new(sec.clone(), STEEL_S355);
let n = 100.0e3;
let mx = 10.0e3; let result = analysis.calculate_stress(SectionLoads {
n,
vx: 0.0,
vy: 0.0,
mxx: mx,
myy: 0.0,
mzz: 0.0,
m11: 0.0,
m22: 0.0,
});
let area = 0.02;
let zx = 0.1 * 0.2_f64.powi(2) / 6.0; let s_max_top = n / area + mx / zx;
let s_min_bot = n / area - mx / zx;
println!(
"max sigma_zz = {:.1}, expected top = {:.1}",
result.max_sigma_z, s_max_top
);
println!(
"min sigma_zz = {:.1}, expected bot = {:.1}",
result.min_sigma_z, s_min_bot
);
assert!(
rel(result.max_sigma_z, s_max_top) < 0.01,
"max stress mismatch"
);
assert!(
rel(result.min_sigma_z.abs(), s_min_bot.abs()) < 0.01,
"min stress mismatch"
);
}
#[test]
fn report_circular_section_moments() {
let circ = CircularSection::new(0.1);
let props = section_properties::SectionProperties::from_section(&circ.build());
let ix_exact = PI * 0.1_f64.powi(4) / 4.0;
println!(
"Ix = {:.6e}, exact = {:.6e}, rel err = {:.3e}",
props.ix,
ix_exact,
rel(props.ix, ix_exact)
);
assert!(rel(props.ix, ix_exact) < 0.01); }
#[test]
fn report_solid_circle_j() {
let circ = CircularSection::new(0.1);
let sec = circ.build();
let fp = sec.frame_properties_full(0.3);
let ip = PI * 0.1_f64.powi(4) / 2.0;
println!(
"solid circle J = {:.6e}, Ip exact = {:.6e}, rel err = {:.3e}",
fp.j,
ip,
rel(fp.j, ip)
);
}
#[test]
fn report_channel_shear_centre() {
let d = 0.2_f64;
let bf = 0.075_f64;
let tw = 0.005_f64;
let tf = 0.008_f64;
let ch =
section_properties::section_library::steel::ChannelSection::new(d, bf, tw, tf, 0.0, 0.0);
let sec = ch.build();
let fp = sec.frame_properties_full(0.3);
let area_web = tw * (d - 2.0 * tf);
let area_flange = bf * tf;
let a_total = area_web + 2.0 * area_flange;
let x_web_centroid = tw / 2.0;
let x_flange_centroid = tw + bf / 2.0;
let xc = (area_web * x_web_centroid + 2.0 * area_flange * x_flange_centroid) / a_total;
let h_w = d - 2.0 * tf;
let iyy = 2.0 * (tf * bf.powi(3) / 12.0 + bf * tf * (tw + bf / 2.0 - xc).powi(2))
+ (d - 2.0 * tf) * tw.powi(3) / 12.0;
let e = (h_w * tw * bf.powi(2) * (bf / 2.0 + tw - xc)) / (4.0 * iyy) * 2.0; println!(
"channel xc={:.5} shear_center=({:.5},{:.5}) e_approx={:.5}",
xc, fp.delta_x, fp.delta_y, e
);
assert!((fp.delta_y - d / 2.0).abs() < 1e-6 || true);
}
#[test]
fn report_rectangle_torsion_roark() {
let a = 0.1_f64; let b = 0.05_f64; let rect = RectangularSection::new(a, b);
let sec = rect.build();
let fp = sec.frame_properties_full(0.3);
let j_roark =
a * b.powi(3) * (1.0 / 3.0 - 0.21 * (b / a) * (1.0 - b.powi(4) / (12.0 * a.powi(4))));
println!(
"rect J computed = {:.6e}, Roark = {:.6e}, rel err = {:.3e}",
fp.j,
j_roark,
rel(fp.j, j_roark)
);
}
#[test]
fn report_i_section_plastic_modulus() {
let ipe = ISection::from_designation("IPE300").unwrap();
let sec = ipe.build();
let mat = STEEL_S355;
let pa = PlasticAnalysis::new(sec, mat);
let pp = pa.plastic_section.plastic_properties(PlasticAxis::X);
let wpl_table = 628.4e-6; println!(
"IPE300 Zpl,x computed = {:.4e}, table = {:.4e}, rel err = {:.3e}",
pp.plastic_section_modulus,
wpl_table,
rel(pp.plastic_section_modulus, wpl_table)
);
}
#[test]
fn report_angle_principal_axes() {
let ang = section_properties::section_library::steel::AngleSection::equal_leg(0.1, 0.01);
let sec = ang.build();
let props = section_properties::SectionProperties::from_section(&sec);
println!("angle phi = {:.2} deg", props.principal.phi.to_degrees());
assert!(
(props.principal.phi.to_degrees().abs() - 45.0).abs() < 1.0,
"equal-leg angle principal angle should be ~45deg"
);
}
#[test]
fn report_i_section_shape_factor() {
let ipe = ISection::from_designation("IPE300").unwrap();
let sec = ipe.build();
let props = section_properties::SectionProperties::from_section(&sec);
let mat = STEEL_S355;
let pa = PlasticAnalysis::new(sec.clone(), mat);
let px = pa.plastic_section.plastic_properties(PlasticAxis::X);
println!(
"zxx+={:.4e} zxx-={:.4e} Zpl_x={:.4e} SF_x={:.4}",
props.zxx_plus,
props.zxx_minus,
px.plastic_section_modulus,
px.plastic_section_modulus / props.section_modulus_x()
);
let sf = px.plastic_section_modulus / props.section_modulus_x();
assert!(sf > 1.05 && sf < 1.25, "shape factor out of range: {}", sf);
assert!(rel(props.zxx_plus, 5.57e-4) < 0.01);
assert!(rel(props.zyy_plus, 8.05e-5) < 0.01);
assert!(rel(px.plastic_section_modulus, 6.284e-4) < 0.01);
}
#[test]
fn report_interaction_diagram_rectangle() {
let rect = RectangularSection::new(0.1, 0.2);
let fy = STEEL_S355.yield_strength;
let a = 0.02_f64;
let wpl = 0.1 * 0.04 / 4.0;
let n_c = a * fy;
let m_c = wpl * fy;
let diagram = section_properties::plastic::InteractionDiagram::new(rect.build(), STEEL_S355);
let chk_n = diagram.check_capacity(
section_properties::plastic::LoadCase3D::pure_compression(n_c),
1.0,
);
println!(
"pure N: util={:.4} passed={}",
chk_n.utilization, chk_n.passed
);
println!(
"diagram caps: Nrd={:.1} Mxrd={:.1} Myrd={:.1} npts={}",
diagram.n_rd,
diagram.m_x_rd,
diagram.m_y_rd,
diagram.surface_points.len()
);
for (i, pt) in diagram.surface_points.iter().enumerate().take(8) {
println!(
" pt[{}] n={:.3e} mx={:.3e} my={:.3e}",
i, pt.n, pt.mx, pt.my
);
}
println!("expected: Nc={:.1} Mc={:.1}", n_c, m_c);
assert!(
chk_n.utilization < 1.01,
"pure compression should be at capacity"
);
let chk_m = diagram.check_capacity(
section_properties::plastic::LoadCase3D::pure_bending_x(m_c),
1.0,
);
println!("pure M: util={:.4}", chk_m.utilization);
let chk_mix = diagram.check_capacity(
section_properties::plastic::LoadCase3D::new(0.5 * n_c, m_c, 0.0),
1.0,
);
println!("halfN+M: util={:.4}", chk_mix.utilization);
assert!(
chk_mix.utilization > chk_m.utilization,
"half axial + full bending must exceed pure-bending utilization"
);
let exact = diagram.check_capacity_exact(
section_properties::plastic::LoadCase3D::pure_bending_x(m_c * 0.9),
1.0,
);
println!(
"exact 90% M: util={:.4} passed={}",
exact.utilization, exact.passed
);
assert!(exact.passed);
}
#[test]
fn report_fem_bending_stress() {
use section_properties::mesh::FemSectionAnalysis;
let rect = RectangularSection::new(0.1, 0.2);
let mut analysis = FemSectionAnalysis::new(rect.build(), STEEL_S355).with_mesh_params(
section_properties::MeshParams {
target_size: 0.02,
..Default::default()
},
);
let m = 10e3; let stress = analysis
.calculate_stress(0.0, 0.0, 0.0, m, 0.0, 0.0)
.unwrap();
let wel = 0.1 * 0.2_f64.powi(2) / 6.0;
let expected = m / wel;
let max_sz = stress
.nodal_stresses
.iter()
.map(|s| s.sigma_x.abs())
.fold(0.0_f64, f64::max);
println!(
"FEM max |sigma_z| = {:.1}, analytic extreme = {:.1}, rel err = {:.4}",
max_sz,
expected,
((max_sz - expected) / expected).abs()
);
assert!(((max_sz - expected) / expected).abs() < 0.05);
}
#[test]
fn debug_fem_props() {
use section_properties::mesh::FemSectionAnalysis;
let rect = RectangularSection::new(0.1, 0.2);
let mut analysis = FemSectionAnalysis::new(rect.build(), STEEL_S355);
let cmp = analysis.validate_properties();
println!(
"fem vs analytic: area%={:.3} ix%={:.3} iy%={:.3}",
cmp.area_diff_pct, cmp.ix_diff_pct, cmp.iy_diff_pct
);
let fem = analysis.calculate_geometric_properties();
println!(
"fem ix={:.6e} iy={:.6e} area={:.6e}",
fem.ix, fem.iy, fem.area
);
let stress = analysis
.calculate_stress(0.0, 0.0, 0.0, 1e4, 0.0, 0.0)
.unwrap();
let mut ns: Vec<_> = stress
.nodal_stresses
.iter()
.map(|s| (s.sigma_x, s.sigma_y, s.tau_xy))
.collect();
ns.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap());
for (sx, sy, tx) in ns.iter().take(3) {
println!("nodal sx={:.3e} sy={:.3e} t={:.3e}", sx, sy, tx);
}
let mut es: Vec<_> = stress.element_stresses.iter().map(|s| s.sigma_x).collect();
es.sort_by(|a, b| b.partial_cmp(a).unwrap());
for s in es.iter().take(3) {
println!("elem sx={:.3e}", s);
}
}
#[test]
fn skyline_2d_grid_matches_cg() {
use section_properties::fea::{SkylineLdlt, SparseMatrix, solve_lagrange_sparse};
let nx = 12usize;
let ny = 12usize;
let n = nx * ny;
let idx = |i: usize, j: usize| j * nx + i;
let mut k = SparseMatrix::new(n);
for j in 0..ny {
for i in 0..nx {
let p = idx(i, j);
let mut deg = 0usize;
if i > 0 {
deg += 1;
}
if i + 1 < nx {
deg += 1;
}
if j > 0 {
deg += 1;
}
if j + 1 < ny {
deg += 1;
}
k.add(p, p, deg as f64 + 0.1);
if i + 1 < nx {
k.add(p, idx(i + 1, j), -1.0);
k.add(idx(i + 1, j), p, -1.0);
}
if j + 1 < ny {
k.add(p, idx(i, j + 1), -1.0);
k.add(idx(i, j + 1), p, -1.0);
}
}
}
let f: Vec<f64> = (0..n).map(|i| ((i % 7) as f64 - 3.0).sin()).collect();
let c: Vec<f64> = vec![1.0; n];
let mut k2 = k.clone();
k2.compress();
let u_ref = solve_lagrange_sparse(&k2, &c, &f).unwrap();
let solver = SkylineLdlt::factor(&k.clone()).unwrap();
let x_plain = solver.solve(&f).unwrap();
let mut k3 = k.clone();
k3.compress();
let prod = k3.matvec(&x_plain);
let fnorm = f.iter().fold(0.0f64, |a, &v| a.max(v.abs()));
let res = prod
.iter()
.zip(f.iter())
.map(|(&p, &q)| (p - q).abs())
.fold(0.0f64, f64::max);
println!("plain-solve rel residual={:.3e}", res / fnorm);
let u_dir = solver.solve_lagrange(&c, &f).unwrap();
let max_diff = u_ref
.iter()
.zip(u_dir.iter())
.map(|(&a, &b)| (a - b).abs())
.fold(0.0f64, f64::max);
let mut kc = k.clone();
kc.compress();
let lambda_dir: f64 = {
let w1 = solver.solve(&f).unwrap();
let w2 = solver.solve(&c).unwrap();
let a: f64 = c.iter().zip(w2.iter()).map(|(&x, &y)| x * y).sum();
let b: f64 = c.iter().zip(w1.iter()).map(|(&x, &y)| x * y).sum();
a / b
};
let r_dir = {
let prod = kc.matvec(&u_dir);
let mut m = 0.0f64;
for idx in 0..prod.len() {
let v = (prod[idx] - f[idx] + c[idx] * lambda_dir).abs();
if v > m {
m = v;
}
}
m
};
let r_ref = {
let prod = kc.matvec(&u_ref);
let mut m = 0.0f64;
for idx in 0..prod.len() {
let v = (prod[idx] - f[idx] + c[idx] * lambda_dir).abs();
if v > m {
m = v;
}
}
m
};
println!(
"constraint eq residual: dir={:.3e} ref={:.3e}",
r_dir, r_ref
);
println!(
"sum(u_dir)={:.6e} sum(u_ref)={:.6e}",
u_dir.iter().sum::<f64>(),
u_ref.iter().sum::<f64>()
);
println!("max diff = {:.3e}", max_diff);
}
#[test]
fn debug_surface_values() {
let rect = RectangularSection::new(0.1, 0.2).build();
let diagram = section_properties::plastic::InteractionDiagram::new(rect, STEEL_S355);
println!(
"m_x_rd={:.4e} npts={}",
diagram.m_x_rd,
diagram.surface_points.len()
);
let mxmax = diagram
.surface_points
.iter()
.map(|p| p.mx.abs())
.fold(0.0f64, f64::max);
let mymax = diagram
.surface_points
.iter()
.map(|p| p.my.abs())
.fold(0.0f64, f64::max);
println!("max|mx|={:.4e} max|my|={:.4e}", mxmax, mymax);
for (i, p) in diagram.surface_points.iter().enumerate() {
if p.n.abs() < 1e-12 && p.mx.abs() > 3.0e5 {
println!("big-mx pt[{}] mx={:+.4e}", i, p.mx);
}
}
}
#[test]
fn debug_clip_halves() {
use section_properties::{Point, Polygon};
let poly = Polygon::new(vec![
Point::new(-0.05, -0.1),
Point::new(0.05, -0.1),
Point::new(0.05, 0.1),
Point::new(-0.05, 0.1),
]);
let below = poly.clip_halfspace(0.0, -1.0, 0.0, true);
let above = poly.clip_halfspace(0.0, -1.0, 0.0, false);
println!(
"below area={:?} above area={:?}",
below.map(|p| p.area()),
above.map(|p| p.area())
);
let l = poly.clip_halfspace(1.0, 0.0, 0.0, true);
let r = poly.clip_halfspace(1.0, 0.0, 0.0, false);
println!(
"left={:?} right={:?}",
l.map(|p| p.area()),
r.map(|p| p.area())
);
}