Skip to main content

section_properties/plastic/
plastic_section.rs

1//! Plastic section analysis.
2//!
3//! Provides plastic neutral axis search, plastic section modulus,
4//! plastic moment capacity, and interaction diagrams.
5
6use crate::geometry::Point;
7use crate::material::Material;
8use crate::section::Section;
9use crate::section_properties::SectionProperties;
10
11/// Plastic section analysis for a given cross-section.
12#[derive(Debug, Clone)]
13pub struct PlasticSection {
14    pub section: Section,
15    pub material: Material,
16    /// Number of fibers for numerical integration
17    pub n_fibers: usize,
18}
19
20impl PlasticSection {
21    /// Create a new plastic section analysis.
22    pub fn new(section: Section, material: Material) -> Self {
23        Self {
24            section,
25            material,
26            n_fibers: 1000,
27        }
28    }
29
30    /// Set number of fibers for numerical integration.
31    pub fn with_fibers(mut self, n: usize) -> Self {
32        self.n_fibers = n.max(100);
33        self
34    }
35
36    /// Compute plastic properties about a given axis.
37    pub fn plastic_properties(&self, axis: PlasticAxis) -> PlasticProperties {
38        let fy = self.material.yield_strength;
39        let pna = self.find_plastic_neutral_axis(axis, fy);
40
41        let (z_pl, s_pl) = self.plastic_modulus_about_axis(axis, &pna, fy);
42
43        PlasticProperties {
44            axis,
45            plastic_neutral_axis: pna,
46            plastic_section_modulus: z_pl,
47            plastic_moment_capacity: s_pl,
48            yield_moment: self.yield_moment(axis),
49        }
50    }
51
52    /// Find plastic neutral axis (PNA) for a given bending axis.
53    ///
54    /// Uses exact polygon clipping (Sutherland-Hodgman) to compute areas,
55    /// mirroring Python's `split_section` + `brentq` approach.
56    pub fn find_plastic_neutral_axis(&self, axis: PlasticAxis, _fy: f64) -> PlasticNeutralAxis {
57        let (min_coord, max_coord) = self.section_bounds(axis);
58
59        let total_area = self.section.area();
60        let target_area = total_area / 2.0;
61
62        // Brent-like root finding using exact polygon areas
63        let mut low = min_coord;
64        let mut high = max_coord;
65        let mut pna_pos = (low + high) / 2.0;
66
67        for _ in 0..80 {
68            pna_pos = (low + high) / 2.0;
69            let area_above = exact::exact_area_on_side(&self.section, axis, pna_pos, true);
70
71            if area_above > target_area {
72                low = pna_pos;
73            } else {
74                high = pna_pos;
75            }
76
77            if (high - low).abs() < 1e-12 {
78                break;
79            }
80        }
81
82        PlasticNeutralAxis {
83            position: pna_pos,
84            axis,
85            area_above: exact::exact_area_on_side(&self.section, axis, pna_pos, true),
86            area_below: exact::exact_area_on_side(&self.section, axis, pna_pos, false),
87        }
88    }
89
90    /// Compute plastic section modulus about an axis given PNA.
91    ///
92    /// Uses exact polygon clipping (mirroring Python's `split_section` approach).
93    fn plastic_modulus_about_axis(
94        &self,
95        axis: PlasticAxis,
96        pna: &PlasticNeutralAxis,
97        fy: f64,
98    ) -> (f64, f64) {
99        let z_pl = exact::exact_plastic_modulus(&self.section, axis, pna.position);
100        let s_pl = z_pl * fy;
101        (z_pl, s_pl)
102    }
103
104    /// Elastic yield moment (first yield).
105    fn yield_moment(&self, axis: PlasticAxis) -> f64 {
106        let props = SectionProperties::from_section(&self.section);
107        let (ix, _iy) = match axis {
108            PlasticAxis::X => (props.ix, props.iy),
109            PlasticAxis::Y => (props.iy, props.ix),
110        };
111        let fy = self.material.yield_strength;
112
113        // Section modulus
114        let (cx, cy) = (props.centroid.x, props.centroid.y);
115        let max_dist_outer = match axis {
116            PlasticAxis::X => self
117                .section
118                .outer
119                .vertices
120                .iter()
121                .map(|v| (v.y - cy).abs())
122                .fold(0.0, f64::max),
123            PlasticAxis::Y => self
124                .section
125                .outer
126                .vertices
127                .iter()
128                .map(|v| (v.x - cx).abs())
129                .fold(0.0, f64::max),
130        };
131        let max_dist_holes = self
132            .section
133            .holes
134            .iter()
135            .flat_map(|h| h.vertices.iter())
136            .map(|v| match axis {
137                PlasticAxis::X => (v.y - cy).abs(),
138                PlasticAxis::Y => (v.x - cx).abs(),
139            })
140            .fold(0.0, f64::max);
141        let max_dist = max_dist_outer.max(max_dist_holes);
142
143        let z_el = ix / max_dist;
144
145        z_el * fy
146    }
147
148    /// Get section bounds along an axis.
149    pub(crate) fn section_bounds(&self, axis: PlasticAxis) -> (f64, f64) {
150        let mut min_v = f64::INFINITY;
151        let mut max_v = f64::NEG_INFINITY;
152
153        for v in &self.section.outer.vertices {
154            let coord = match axis {
155                PlasticAxis::X => v.y,
156                PlasticAxis::Y => v.x,
157            };
158            min_v = min_v.min(coord);
159            max_v = max_v.max(coord);
160        }
161
162        for hole in &self.section.holes {
163            for v in &hole.vertices {
164                let coord = match axis {
165                    PlasticAxis::X => v.y,
166                    PlasticAxis::Y => v.x,
167                };
168                min_v = min_v.min(coord);
169                max_v = max_v.max(coord);
170            }
171        }
172
173        (min_v, max_v)
174    }
175
176    /// Compute area on one side of a line.
177    pub fn area_on_side(&self, axis: PlasticAxis, position: f64, above: bool) -> f64 {
178        // Simplified: use fiber integration
179        let n = 200;
180        let (min_c, max_c) = self.section_bounds(axis);
181        let (min_o, max_o) = self.section_bounds(axis.orthogonal());
182
183        let dc = (max_c - min_c) / n as f64;
184        let do_ = (max_o - min_o) / n as f64;
185
186        let mut area = 0.0;
187
188        for i in 0..n {
189            for j in 0..n {
190                let c = min_c + (i as f64 + 0.5) * dc;
191                let o = min_o + (j as f64 + 0.5) * do_;
192
193                let point = match axis {
194                    PlasticAxis::X => Point::new(o, c),
195                    PlasticAxis::Y => Point::new(c, o),
196                };
197
198                if self.section.contains_point(point) {
199                    let on_side = match axis {
200                        PlasticAxis::X => c > position,
201                        PlasticAxis::Y => c > position,
202                    };
203
204                    if on_side == above {
205                        area += dc * do_;
206                    }
207                }
208            }
209        }
210
211        area
212    }
213
214    /// Full plastic properties (both centroidal and principal axes).
215    ///
216    /// Mirrors Python `sectionproperties.analysis.plastic_section.PlasticSection.calculate_plastic_properties`:
217    /// computes plastic moduli and shape factors about the centroidal x/y axes
218    /// and the principal 11/22 axes.
219    pub fn full_plastic_properties(&self) -> FullPlasticProperties {
220        let props_x = self.plastic_properties(PlasticAxis::X);
221        let props_y = self.plastic_properties(PlasticAxis::Y);
222
223        let shape_factor_x =
224            props_x.plastic_section_modulus / (props_x.yield_moment / self.material.yield_strength);
225        let shape_factor_y =
226            props_y.plastic_section_modulus / (props_y.yield_moment / self.material.yield_strength);
227
228        // Principal axis plastic properties
229        let sect_props = SectionProperties::from_section(&self.section);
230        let principal = sect_props.principal_properties();
231        let phi = principal.phi;
232
233        let (props_11, props_22, sf_11, sf_22) = if phi.abs() < 1e-10 {
234            // Already aligned with principal axes
235            (props_x, props_y, shape_factor_x, shape_factor_y)
236        } else {
237            // Rotate section by -phi so principal axes align with x/y
238            let rotated_outer = self.section.outer.rotate(-phi);
239            let rotated_holes: Vec<_> = self.section.holes.iter().map(|h| h.rotate(-phi)).collect();
240            let rotated_section = Section::new(rotated_outer, rotated_holes);
241            let rotated_plastic =
242                PlasticSection::new(rotated_section, self.material).with_fibers(self.n_fibers);
243
244            let p11 = rotated_plastic.plastic_properties(PlasticAxis::X);
245            let p22 = rotated_plastic.plastic_properties(PlasticAxis::Y);
246            let sf11 =
247                p11.plastic_section_modulus / (p11.yield_moment / self.material.yield_strength);
248            let sf22 =
249                p22.plastic_section_modulus / (p22.yield_moment / self.material.yield_strength);
250            (p11, p22, sf11, sf22)
251        };
252
253        // Yield moments (already computed in PlasticProperties)
254        let my_xx = props_x.yield_moment;
255        let my_yy = props_y.yield_moment;
256        let my_11 = props_11.yield_moment;
257        let my_22 = props_22.yield_moment;
258
259        // Positive/negative shape factors: S_pl / Z_el
260        let sxx = props_x.plastic_section_modulus;
261        let syy = props_y.plastic_section_modulus;
262        let s11 = props_11.plastic_section_modulus;
263        let s22 = props_22.plastic_section_modulus;
264        let sf_xx_plus = if sect_props.zxx_plus > 1e-15 {
265            sxx / sect_props.zxx_plus
266        } else {
267            0.0
268        };
269        let sf_xx_minus = if sect_props.zxx_minus > 1e-15 {
270            sxx / sect_props.zxx_minus
271        } else {
272            0.0
273        };
274        let sf_yy_plus = if sect_props.zyy_plus > 1e-15 {
275            syy / sect_props.zyy_plus
276        } else {
277            0.0
278        };
279        let sf_yy_minus = if sect_props.zyy_minus > 1e-15 {
280            syy / sect_props.zyy_minus
281        } else {
282            0.0
283        };
284        let sf_11_plus = if sect_props.principal.z11_plus > 1e-15 {
285            s11 / sect_props.principal.z11_plus
286        } else {
287            0.0
288        };
289        let sf_11_minus = if sect_props.principal.z11_minus > 1e-15 {
290            s11 / sect_props.principal.z11_minus
291        } else {
292            0.0
293        };
294        let sf_22_plus = if sect_props.principal.z22_plus > 1e-15 {
295            s22 / sect_props.principal.z22_plus
296        } else {
297            0.0
298        };
299        let sf_22_minus = if sect_props.principal.z22_minus > 1e-15 {
300            s22 / sect_props.principal.z22_minus
301        } else {
302            0.0
303        };
304
305        FullPlasticProperties {
306            x_axis: props_x,
307            y_axis: props_y,
308            shape_factor_x,
309            shape_factor_y,
310            principal_11: props_11,
311            principal_22: props_22,
312            shape_factor_11: sf_11,
313            shape_factor_22: sf_22,
314            principal_angle: phi,
315            plastic_centroid_x: props_y.plastic_neutral_axis.position,
316            plastic_centroid_y: props_x.plastic_neutral_axis.position,
317            plastic_centroid_11: props_11.plastic_neutral_axis.position,
318            plastic_centroid_22: props_22.plastic_neutral_axis.position,
319            my_xx,
320            my_yy,
321            my_11,
322            my_22,
323            sf_xx_plus,
324            sf_xx_minus,
325            sf_yy_plus,
326            sf_yy_minus,
327            sf_11_plus,
328            sf_11_minus,
329            sf_22_plus,
330            sf_22_minus,
331        }
332    }
333}
334
335/// Bending axis for plastic analysis.
336#[derive(Debug, Clone, Copy, PartialEq, Eq)]
337pub enum PlasticAxis {
338    X, // Bending about X-axis (My)
339    Y, // Bending about Y-axis (Mx)
340}
341
342impl PlasticAxis {
343    pub fn orthogonal(&self) -> Self {
344        match self {
345            PlasticAxis::X => PlasticAxis::Y,
346            PlasticAxis::Y => PlasticAxis::X,
347        }
348    }
349}
350
351/// Plastic neutral axis result.
352#[derive(Debug, Clone, Copy)]
353pub struct PlasticNeutralAxis {
354    pub position: f64, // Coordinate along the axis
355    pub axis: PlasticAxis,
356    pub area_above: f64,
357    pub area_below: f64,
358}
359
360/// Plastic properties for one axis.
361#[derive(Debug, Clone, Copy)]
362pub struct PlasticProperties {
363    pub axis: PlasticAxis,
364    pub plastic_neutral_axis: PlasticNeutralAxis,
365    pub plastic_section_modulus: f64, // Zpl
366    pub plastic_moment_capacity: f64, // Mpl = Zpl * fy
367    pub yield_moment: f64,            // My = Z_el * fy
368}
369
370/// Full plastic properties for both centroidal and principal axes.
371#[derive(Debug, Clone)]
372pub struct FullPlasticProperties {
373    pub x_axis: PlasticProperties,
374    pub y_axis: PlasticProperties,
375    pub shape_factor_x: f64, // Mpl/My
376    pub shape_factor_y: f64,
377    /// Plastic properties about the 11 (major principal) axis.
378    pub principal_11: PlasticProperties,
379    /// Plastic properties about the 22 (minor principal) axis.
380    pub principal_22: PlasticProperties,
381    /// Shape factor about the 11 axis.
382    pub shape_factor_11: f64,
383    /// Shape factor about the 22 axis.
384    pub shape_factor_22: f64,
385    /// Principal axis angle in radians.
386    pub principal_angle: f64,
387    /// Plastic centroid x-coordinate (PNA x for bending about y-axis).
388    pub plastic_centroid_x: f64,
389    /// Plastic centroid y-coordinate (PNA y for bending about x-axis).
390    pub plastic_centroid_y: f64,
391    /// Plastic centroid 11-coordinate (PNA in rotated frame for 11-axis bending).
392    pub plastic_centroid_11: f64,
393    /// Plastic centroid 22-coordinate (PNA in rotated frame for 22-axis bending).
394    pub plastic_centroid_22: f64,
395    /// Yield moment about x-axis [N·m]
396    pub my_xx: f64,
397    /// Yield moment about y-axis [N·m]
398    pub my_yy: f64,
399    /// Yield moment about 11-axis [N·m]
400    pub my_11: f64,
401    /// Yield moment about 22-axis [N·m]
402    pub my_22: f64,
403    /// Shape factor for positive bending about x-axis (Sxx / zxx_plus)
404    pub sf_xx_plus: f64,
405    /// Shape factor for negative bending about x-axis (Sxx / zxx_minus)
406    pub sf_xx_minus: f64,
407    /// Shape factor for positive bending about y-axis (Syy / zyy_plus)
408    pub sf_yy_plus: f64,
409    /// Shape factor for negative bending about y-axis (Syy / zyy_minus)
410    pub sf_yy_minus: f64,
411    /// Shape factor for positive bending about 11-axis (S11 / z11_plus)
412    pub sf_11_plus: f64,
413    /// Shape factor for negative bending about 11-axis (S11 / z11_minus)
414    pub sf_11_minus: f64,
415    /// Shape factor for positive bending about 22-axis (S22 / z22_plus)
416    pub sf_22_plus: f64,
417    /// Shape factor for negative bending about 22-axis (S22 / z22_minus)
418    pub sf_22_minus: f64,
419}
420
421impl FullPlasticProperties {
422    /// Shape factor (plastic/elastic moment ratio).
423    pub fn shape_factor(&self, axis: PlasticAxis) -> f64 {
424        match axis {
425            PlasticAxis::X => self.shape_factor_x,
426            PlasticAxis::Y => self.shape_factor_y,
427        }
428    }
429
430    /// Shape factor about principal axes.
431    pub fn shape_factor_principal(&self, axis: PlasticAxis) -> f64 {
432        match axis {
433            PlasticAxis::X => self.shape_factor_11,
434            PlasticAxis::Y => self.shape_factor_22,
435        }
436    }
437
438    /// Plastic moment capacity.
439    pub fn plastic_moment(&self, axis: PlasticAxis) -> f64 {
440        match axis {
441            PlasticAxis::X => self.x_axis.plastic_moment_capacity,
442            PlasticAxis::Y => self.y_axis.plastic_moment_capacity,
443        }
444    }
445
446    /// Plastic moment capacity about principal axes.
447    pub fn plastic_moment_principal(&self, axis: PlasticAxis) -> f64 {
448        match axis {
449            PlasticAxis::X => self.principal_11.plastic_moment_capacity,
450            PlasticAxis::Y => self.principal_22.plastic_moment_capacity,
451        }
452    }
453}
454
455/// Plastic analysis using exact polygon clipping (more accurate).
456pub mod exact {
457    use super::*;
458    use crate::geometry::{Point, Polygon};
459
460    /// Compute exact area on one side of the PNA using polygon clipping.
461    pub fn exact_area_on_side(
462        section: &Section,
463        axis: PlasticAxis,
464        pna_pos: f64,
465        above: bool,
466    ) -> f64 {
467        let mut area = 0.0;
468        if let Some(outer_part) = clip_polygon_halfspace(&section.outer, axis, pna_pos, above) {
469            area += outer_part.area();
470        }
471        for hole in &section.holes {
472            if let Some(hole_part) = clip_polygon_halfspace(hole, axis, pna_pos, above) {
473                area -= hole_part.area();
474            }
475        }
476        area
477    }
478
479    /// Compute exact plastic modulus by polygon clipping at PNA.
480    ///
481    /// Clips the section at the plastic neutral axis and computes the first moment
482    /// of area for each part: Z_pl = Σ |d_i| * A_i where d_i is the distance from
483    /// the PNA to the centroid of part i.
484    pub fn exact_plastic_modulus(section: &Section, axis: PlasticAxis, pna_pos: f64) -> f64 {
485        let (above, below) = clip_section_at_pna(section, axis, pna_pos);
486
487        let mut z_pl = 0.0;
488
489        for part in [above, below].into_iter().flatten() {
490            let area = part.area();
491            if area.abs() < 1e-15 {
492                continue;
493            }
494            let centroid = part.centroid();
495            let lever = match axis {
496                PlasticAxis::X => (centroid.y - pna_pos).abs(),
497                PlasticAxis::Y => (centroid.x - pna_pos).abs(),
498            };
499            z_pl += lever * area;
500        }
501
502        for hole in &section.holes {
503            for above in [true, false].into_iter() {
504                if let Some(hp) = clip_polygon_halfspace(hole, axis, pna_pos, above) {
505                    let area = hp.area();
506                    if area.abs() < 1e-15 {
507                        continue;
508                    }
509                    let centroid = hp.centroid();
510                    let lever = match axis {
511                        PlasticAxis::X => (centroid.y - pna_pos).abs(),
512                        PlasticAxis::Y => (centroid.x - pna_pos).abs(),
513                    };
514                    z_pl -= lever * area;
515                }
516            }
517        }
518
519        z_pl
520    }
521
522    /// Clip section at plastic neutral axis using Sutherland-Hodgman algorithm.
523    ///
524    /// Returns the portion of the section above and below the PNA line.
525    /// For axis=X, the PNA is the horizontal line y = pna_pos.
526    /// For axis=Y, the PNA is the vertical line x = pna_pos.
527    pub fn clip_section_at_pna(
528        section: &Section,
529        axis: PlasticAxis,
530        pna_pos: f64,
531    ) -> (Option<Polygon>, Option<Polygon>) {
532        let above = clip_polygon_halfspace(&section.outer, axis, pna_pos, true)
533            .map(|p| subtract_holes(p, &section.holes, axis, pna_pos, true));
534        let below = clip_polygon_halfspace(&section.outer, axis, pna_pos, false)
535            .map(|p| subtract_holes(p, &section.holes, axis, pna_pos, false));
536
537        (above, below)
538    }
539
540    /// Clip a polygon by a half-space defined by the PNA line.
541    /// `above = true` keeps the part where y > pna_pos (or x > pna_pos for axis=Y).
542    fn clip_polygon_halfspace(
543        poly: &Polygon,
544        axis: PlasticAxis,
545        pna_pos: f64,
546        above: bool,
547    ) -> Option<Polygon> {
548        let input = &poly.vertices;
549        if input.len() < 3 {
550            return None;
551        }
552
553        let is_inside = |p: &Point| -> bool {
554            match axis {
555                PlasticAxis::X => {
556                    if above {
557                        p.y >= pna_pos
558                    } else {
559                        p.y <= pna_pos
560                    }
561                }
562                PlasticAxis::Y => {
563                    if above {
564                        p.x >= pna_pos
565                    } else {
566                        p.x <= pna_pos
567                    }
568                }
569            }
570        };
571
572        let intersect = |p1: &Point, p2: &Point| -> Point {
573            match axis {
574                PlasticAxis::X => {
575                    let dy = p2.y - p1.y;
576                    if dy.abs() < 1e-15 {
577                        Point::new(p1.x, pna_pos)
578                    } else {
579                        let t = (pna_pos - p1.y) / dy;
580                        Point::new(p1.x + t * (p2.x - p1.x), pna_pos)
581                    }
582                }
583                PlasticAxis::Y => {
584                    let dx = p2.x - p1.x;
585                    if dx.abs() < 1e-15 {
586                        Point::new(pna_pos, p1.y)
587                    } else {
588                        let t = (pna_pos - p1.x) / dx;
589                        Point::new(pna_pos, p1.y + t * (p2.y - p1.y))
590                    }
591                }
592            }
593        };
594
595        let mut output = Vec::new();
596        let n = input.len();
597        let mut prev = input[n - 1];
598        let mut prev_in = is_inside(&prev);
599
600        for i in 0..n {
601            let curr = input[i];
602            let curr_in = is_inside(&curr);
603
604            if curr_in {
605                if !prev_in {
606                    output.push(intersect(&prev, &curr));
607                }
608                output.push(curr);
609            } else if prev_in {
610                output.push(intersect(&prev, &curr));
611            }
612
613            prev = curr;
614            prev_in = curr_in;
615        }
616
617        // Remove duplicate consecutive vertices
618        output = deduplicate_vertices(&output);
619
620        if output.len() < 3 {
621            None
622        } else {
623            Some(Polygon::new(output))
624        }
625    }
626
627    /// Remove duplicate consecutive vertices (and wrap-around duplicates).
628    fn deduplicate_vertices(verts: &[Point]) -> Vec<Point> {
629        if verts.is_empty() {
630            return Vec::new();
631        }
632        let mut result = Vec::with_capacity(verts.len());
633        let tol = 1e-12;
634        for v in verts {
635            let is_dup = result.last().map_or(false, |last: &Point| {
636                (last.x - v.x).abs() < tol && (last.y - v.y).abs() < tol
637            });
638            if !is_dup {
639                result.push(*v);
640            }
641        }
642        // Check wrap-around
643        if result.len() > 1 {
644            let first = result[0];
645            let last = result[result.len() - 1];
646            if (first.x - last.x).abs() < tol && (first.y - last.y).abs() < tol {
647                result.pop();
648            }
649        }
650        result
651    }
652
653    /// Subtract holes from the clipped outer polygon.
654    /// Since holes are already part of the Section (CW orientation), we simply
655    /// clip each hole and include it in the result polygon list.
656    /// For simplicity, we return the outer clipped polygon and ignore holes
657    /// in the polygon representation (area calculation handles signed areas).
658    fn subtract_holes(
659        outer: Polygon,
660        _holes: &[Polygon],
661        _axis: PlasticAxis,
662        _pna_pos: f64,
663        _above: bool,
664    ) -> Polygon {
665        // TODO: Properly subtract clipped holes from the outer polygon.
666        // For now, return the outer polygon as-is. The area and centroid
667        // will be slightly off for sections with holes, but correct for
668        // solid sections.
669        outer
670    }
671}
672
673#[cfg(test)]
674mod tests {
675    use super::*;
676    use crate::geometry::{Point, Polygon};
677    use crate::material::presets::STEEL_S355;
678    use crate::section::Section;
679    use crate::section_library::ParametricSection;
680
681    #[test]
682    fn plastic_rectangular() {
683        // Rectangular section 200x100 mm
684        let poly = Polygon::new(vec![
685            Point::new(-0.1, -0.05),
686            Point::new(0.1, -0.05),
687            Point::new(0.1, 0.05),
688            Point::new(-0.1, 0.05),
689        ]);
690        let section = Section::new(poly, vec![]);
691        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
692
693        let props_x = plastic.plastic_properties(PlasticAxis::X);
694        let props_y = plastic.plastic_properties(PlasticAxis::Y);
695
696        // For rectangle: Zpl = b*h^2/4
697        let expected_zx = 0.2 * 0.1_f64.powi(2) / 4.0; // bending about X (b=200mm, h=100mm)
698        let expected_zy = 0.1 * 0.2_f64.powi(2) / 4.0; // bending about Y (b=100mm, h=200mm)
699
700        assert!((props_x.plastic_section_modulus - expected_zx).abs() / expected_zx < 0.05);
701        assert!((props_y.plastic_section_modulus - expected_zy).abs() / expected_zy < 0.05);
702
703        // Shape factor for rectangle = 1.5
704        assert!(
705            (props_x.plastic_section_modulus / (props_x.yield_moment / STEEL_S355.yield_strength)
706                - 1.5)
707                .abs()
708                < 0.05
709        );
710    }
711
712    #[test]
713    fn plastic_circular() {
714        // Circle diameter 100mm
715        let poly = crate::section_library::circle_polygon(0.05, 64);
716        let section = Section::new(poly, vec![]);
717        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
718
719        let props = plastic.plastic_properties(PlasticAxis::X);
720
721        // For circle: Zpl = d^3/6 = 4*r^3/3
722        let r: f64 = 0.05;
723        let expected_z = 4.0 * r.powi(3) / 3.0;
724
725        assert!((props.plastic_section_modulus - expected_z).abs() / expected_z < 0.05);
726
727        // Shape factor for circle = 16/(3*pi) ≈ 1.698
728        let shape =
729            props.plastic_section_modulus / (props.yield_moment / STEEL_S355.yield_strength);
730        assert!((shape - 16.0 / (3.0 * std::f64::consts::PI)).abs() < 0.05);
731    }
732
733    #[test]
734    fn plastic_i_section() {
735        // Simple I-section
736        let i = crate::section_library::steel::ISection::new(0.3, 0.15, 0.007, 0.01, 0.012);
737        let section = i.build();
738        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
739
740        let full = plastic.full_plastic_properties();
741
742        // I-section: strong axis shape factor ~1.1-1.2, weak axis ~1.3-1.5
743        assert!(full.shape_factor_x > 1.05 && full.shape_factor_x < 1.3);
744        assert!(full.shape_factor_y > 1.1 && full.shape_factor_y < 1.6);
745
746        // Mpl > My
747        assert!(full.x_axis.plastic_moment_capacity > full.x_axis.yield_moment);
748        assert!(full.y_axis.plastic_moment_capacity > full.y_axis.yield_moment);
749    }
750
751    #[test]
752    fn plastic_principal_axes_symmetric() {
753        // Doubly-symmetric rectangle: principal axes == centroidal axes
754        let poly = Polygon::new(vec![
755            Point::new(-0.1, -0.05),
756            Point::new(0.1, -0.05),
757            Point::new(0.1, 0.05),
758            Point::new(-0.1, 0.05),
759        ]);
760        let section = Section::new(poly, vec![]);
761        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
762
763        let full = plastic.full_plastic_properties();
764
765        // For doubly-symmetric section, principal angle is 0 or pi/2
766        let angle_norm = full.principal_angle.rem_euclid(std::f64::consts::FRAC_PI_2);
767        assert!(angle_norm < 1e-6 || (angle_norm - std::f64::consts::FRAC_PI_2).abs() < 1e-6);
768
769        // Principal plastic moduli should match centroidal (possibly swapped)
770        let p11 = full.principal_11.plastic_section_modulus;
771        let p22 = full.principal_22.plastic_section_modulus;
772        let sx = full.x_axis.plastic_section_modulus;
773        let sy = full.y_axis.plastic_section_modulus;
774
775        // Either (p11≈sx and p22≈sy) or (p11≈sy and p22≈sx)
776        let match_a = (p11 - sx).abs() / sx < 0.02 && (p22 - sy).abs() / sy < 0.02;
777        let match_b = (p11 - sy).abs() / sy < 0.02 && (p22 - sx).abs() / sx < 0.02;
778        assert!(match_a || match_b);
779    }
780
781    #[test]
782    fn plastic_principal_axes_angle_section() {
783        // Unsymmetric angle section: principal axes != centroidal axes
784        let angle = crate::section_library::steel::AngleSection::new(0.1, 0.075, 0.008, 0.0, 0.0);
785        let section = angle.build();
786        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
787
788        let full = plastic.full_plastic_properties();
789
790        // Principal angle should be non-zero for unequal-leg angle
791        assert!(full.principal_angle.abs() > 1e-4);
792
793        // Principal plastic moments should be positive and meaningful
794        assert!(full.principal_11.plastic_moment_capacity > 0.0);
795        assert!(full.principal_22.plastic_moment_capacity > 0.0);
796
797        // Shape factors should be > 1.0
798        assert!(full.shape_factor_11 > 1.0);
799        assert!(full.shape_factor_22 > 1.0);
800    }
801
802    #[test]
803    fn plastic_principal_axes_channel() {
804        // Channel section: singly-symmetric (symmetric about y-axis)
805        let channel =
806            crate::section_library::steel::ChannelSection::new(0.2, 0.1, 0.008, 0.008, 0.0, 0.0);
807        let section = channel.build();
808        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
809
810        let full = plastic.full_plastic_properties();
811
812        // Channel is symmetric about y-axis, so principal angle ~ 0
813        assert!(full.principal_angle.abs() < 1e-4);
814
815        // Principal 11 (strong axis) should match x-axis
816        let ratio_11 =
817            full.principal_11.plastic_section_modulus / full.x_axis.plastic_section_modulus;
818        assert!((ratio_11 - 1.0).abs() < 0.02);
819    }
820
821    #[test]
822    fn exact_plastic_rectangular() {
823        // Rectangular section 200x100 mm
824        let poly = Polygon::new(vec![
825            Point::new(-0.1, -0.05),
826            Point::new(0.1, -0.05),
827            Point::new(0.1, 0.05),
828            Point::new(-0.1, 0.05),
829        ]);
830        let section = Section::new(poly, vec![]);
831
832        // PNA at y=0 (centroidal axis for symmetric section)
833        let z_pl = exact::exact_plastic_modulus(&section, PlasticAxis::X, 0.0);
834
835        // For rectangle: Zpl = b*h^2/4
836        let expected = 0.2 * 0.1_f64.powi(2) / 4.0;
837        assert!((z_pl - expected).abs() / expected < 0.01);
838    }
839
840    #[test]
841    fn exact_plastic_rectangular_y_axis() {
842        // Rectangular section 200x100 mm, bending about Y axis
843        let poly = Polygon::new(vec![
844            Point::new(-0.1, -0.05),
845            Point::new(0.1, -0.05),
846            Point::new(0.1, 0.05),
847            Point::new(-0.1, 0.05),
848        ]);
849        let section = Section::new(poly, vec![]);
850
851        // PNA at x=0 (centroidal axis for symmetric section)
852        let z_pl = exact::exact_plastic_modulus(&section, PlasticAxis::Y, 0.0);
853
854        // For rectangle about Y: Zpl = h*b^2/4
855        let expected = 0.1 * 0.2_f64.powi(2) / 4.0;
856        assert!((z_pl - expected).abs() / expected < 0.01);
857    }
858
859    #[test]
860    fn exact_plastic_i_section() {
861        // Simple I-section
862        let i = crate::section_library::steel::ISection::new(0.3, 0.15, 0.007, 0.01, 0.012);
863        let section = i.build();
864
865        // For symmetric I-section, PNA at centroid
866        let props = SectionProperties::from_section(&section);
867        let z_pl = exact::exact_plastic_modulus(&section, PlasticAxis::X, props.centroid.y);
868
869        // Z_pl should be positive and reasonable
870        assert!(z_pl > 0.0);
871
872        // Compare with fiber integration result
873        let plastic = PlasticSection::new(section.clone(), STEEL_S355).with_fibers(500);
874        let fiber_props = plastic.plastic_properties(PlasticAxis::X);
875        let fiber_z = fiber_props.plastic_section_modulus;
876
877        // Exact should be close to fiber (within 5%)
878        assert!((z_pl - fiber_z).abs() / fiber_z < 0.05);
879    }
880
881    #[test]
882    fn exact_plastic_clip_geometry() {
883        // Test that clipping produces valid polygons
884        let poly = Polygon::new(vec![
885            Point::new(-0.1, -0.05),
886            Point::new(0.1, -0.05),
887            Point::new(0.1, 0.05),
888            Point::new(-0.1, 0.05),
889        ]);
890        let section = Section::new(poly, vec![]);
891
892        let (above, below) = exact::clip_section_at_pna(&section, PlasticAxis::X, 0.0);
893
894        assert!(above.is_some());
895        assert!(below.is_some());
896
897        let above_poly = above.unwrap();
898        let below_poly = below.unwrap();
899
900        // Each part should have area = half of total
901        let half_area = 0.2 * 0.1 / 2.0;
902        assert!((above_poly.area() - half_area).abs() / half_area < 0.01);
903        assert!((below_poly.area() - half_area).abs() / half_area < 0.01);
904    }
905
906    #[test]
907    fn exact_plastic_hollow_rectangular() {
908        let b = 0.2;
909        let h = 0.1;
910        let t = 0.01;
911        let outer = Polygon::new(vec![
912            Point::new(-b / 2.0, -h / 2.0),
913            Point::new(b / 2.0, -h / 2.0),
914            Point::new(b / 2.0, h / 2.0),
915            Point::new(-b / 2.0, h / 2.0),
916        ]);
917        let inner = Polygon::new(vec![
918            Point::new(-(b - 2.0 * t) / 2.0, -(h - 2.0 * t) / 2.0),
919            Point::new((b - 2.0 * t) / 2.0, -(h - 2.0 * t) / 2.0),
920            Point::new((b - 2.0 * t) / 2.0, (h - 2.0 * t) / 2.0),
921            Point::new(-(b - 2.0 * t) / 2.0, (h - 2.0 * t) / 2.0),
922        ]);
923        let section = Section::new(outer, vec![inner]);
924
925        let z_pl = exact::exact_plastic_modulus(&section, PlasticAxis::X, 0.0);
926
927        let z_outer = b * h * h / 4.0;
928        let z_inner = (b - 2.0 * t) * (h - 2.0 * t).powi(2) / 4.0;
929        let expected = z_outer - z_inner;
930
931        assert!(
932            (z_pl - expected).abs() / expected < 0.01,
933            "Hollow rectangular Z_pl: got {}, expected {}",
934            z_pl,
935            expected
936        );
937    }
938
939    #[test]
940    fn plastic_centroid_symmetric_section() {
941        // Doubly-symmetric rectangle: plastic centroid == geometric centroid
942        let poly = Polygon::new(vec![
943            Point::new(-0.05, -0.05),
944            Point::new(0.05, -0.05),
945            Point::new(0.05, 0.05),
946            Point::new(-0.05, 0.05),
947        ]);
948        let section = Section::new(poly, vec![]);
949        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
950
951        let full = plastic.full_plastic_properties();
952        // Fiber integration has ~1e-3 tolerance with 200 grid points
953        assert!(full.plastic_centroid_x.abs() < 1e-3);
954        assert!(full.plastic_centroid_y.abs() < 1e-3);
955    }
956
957    #[test]
958    fn plastic_centroid_tee_section() {
959        // T-section: flange 0.2x0.02 at top, web 0.02x0.08 below.
960        // PNA for x-axis bending lies in the flange.
961        let poly = Polygon::new(vec![
962            Point::new(-0.1, 0.10),
963            Point::new(0.1, 0.10),
964            Point::new(0.1, 0.08),
965            Point::new(0.01, 0.08),
966            Point::new(0.01, 0.0),
967            Point::new(-0.01, 0.0),
968            Point::new(-0.01, 0.08),
969            Point::new(-0.1, 0.08),
970        ]);
971        let section = Section::new(poly, vec![]);
972        let _props = SectionProperties::from_section(&section);
973        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
974
975        let full = plastic.full_plastic_properties();
976
977        // Geometric centroid y
978        let area = 0.0056_f64;
979        let yc = (0.004 * 0.09 + 0.0016 * 0.04) / area;
980
981        // Plastic centroid y: PNA in flange, area above = area/2 = 0.0028
982        // 0.2 * (0.10 - y_pc) = 0.0028  =>  y_pc = 0.086
983        let y_pc_expected = 0.086_f64;
984
985        assert!(
986            (full.plastic_centroid_y - y_pc_expected).abs() < 0.005,
987            "plastic_centroid_y = {}, expected ~{}",
988            full.plastic_centroid_y,
989            y_pc_expected
990        );
991        assert!(
992            (full.plastic_centroid_y - yc).abs() > 0.005,
993            "plastic centroid should differ from geometric centroid for T-section"
994        );
995
996        // y-axis bending: section is symmetric about y-axis, so x_pc ~ 0
997        assert!(full.plastic_centroid_x.abs() < 0.005);
998    }
999
1000    #[test]
1001    fn plastic_centroid_principal_axes() {
1002        // For a symmetric section, principal plastic centroids match centroidal
1003        let poly = Polygon::new(vec![
1004            Point::new(-0.05, -0.05),
1005            Point::new(0.05, -0.05),
1006            Point::new(0.05, 0.05),
1007            Point::new(-0.05, 0.05),
1008        ]);
1009        let section = Section::new(poly, vec![]);
1010        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
1011
1012        let full = plastic.full_plastic_properties();
1013        assert!(full.plastic_centroid_11.abs() < 1e-3);
1014        assert!(full.plastic_centroid_22.abs() < 1e-3);
1015    }
1016
1017    #[test]
1018    fn yield_moments_match_plastic_properties() {
1019        let poly = Polygon::new(vec![
1020            Point::new(-0.05, -0.05),
1021            Point::new(0.05, -0.05),
1022            Point::new(0.05, 0.05),
1023            Point::new(-0.05, 0.05),
1024        ]);
1025        let section = Section::new(poly, vec![]);
1026        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
1027
1028        let full = plastic.full_plastic_properties();
1029
1030        // my_xx should match x_axis yield_moment
1031        assert!((full.my_xx - full.x_axis.yield_moment).abs() < 1e-6);
1032        assert!((full.my_yy - full.y_axis.yield_moment).abs() < 1e-6);
1033        assert!((full.my_11 - full.principal_11.yield_moment).abs() < 1e-6);
1034        assert!((full.my_22 - full.principal_22.yield_moment).abs() < 1e-6);
1035
1036        // For rectangle: My = Z * fy = (b*h²/6) * fy
1037        let b = 0.1_f64;
1038        let h = 0.1_f64;
1039        let fy = STEEL_S355.yield_strength;
1040        let z_el = b * h.powi(2) / 6.0;
1041        let my_expected = z_el * fy;
1042        assert!((full.my_xx - my_expected).abs() / my_expected < 0.02);
1043    }
1044
1045    #[test]
1046    fn shape_factors_plus_minus_rectangular() {
1047        // Rectangular section: shape factor = 1.5 for both plus and minus
1048        let poly = Polygon::new(vec![
1049            Point::new(-0.05, -0.05),
1050            Point::new(0.05, -0.05),
1051            Point::new(0.05, 0.05),
1052            Point::new(-0.05, 0.05),
1053        ]);
1054        let section = Section::new(poly, vec![]);
1055        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
1056
1057        let full = plastic.full_plastic_properties();
1058
1059        // For rectangle: S_pl / Z_el = 1.5
1060        assert!((full.sf_xx_plus - 1.5).abs() < 0.05);
1061        assert!((full.sf_xx_minus - 1.5).abs() < 0.05);
1062        assert!((full.sf_yy_plus - 1.5).abs() < 0.05);
1063        assert!((full.sf_yy_minus - 1.5).abs() < 0.05);
1064    }
1065
1066    #[test]
1067    fn shape_factors_plus_minus_tee_section() {
1068        // T-section: plus and minus shape factors differ
1069        let poly = Polygon::new(vec![
1070            Point::new(-0.1, 0.10),
1071            Point::new(0.1, 0.10),
1072            Point::new(0.1, 0.08),
1073            Point::new(0.01, 0.08),
1074            Point::new(0.01, 0.0),
1075            Point::new(-0.01, 0.0),
1076            Point::new(-0.01, 0.08),
1077            Point::new(-0.1, 0.08),
1078        ]);
1079        let section = Section::new(poly, vec![]);
1080        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
1081
1082        let full = plastic.full_plastic_properties();
1083
1084        // Shape factors should be positive
1085        assert!(full.sf_xx_plus > 0.0);
1086        assert!(full.sf_xx_minus > 0.0);
1087        // For asymmetric section, plus and minus differ
1088        assert!((full.sf_xx_plus - full.sf_xx_minus).abs() > 0.01);
1089        // Minus shape factor should be > 1 (bottom fibre farther from centroid)
1090        assert!(full.sf_xx_minus > 1.0);
1091    }
1092
1093    #[test]
1094    fn yield_moment_hollow_section() {
1095        // Box section: outer 0.2x0.1, inner 0.16x0.06 (centered)
1096        // Verify yield_moment considers both outer and inner vertices.
1097        let outer = Polygon::new(vec![
1098            Point::new(-0.1, -0.05),
1099            Point::new(0.1, -0.05),
1100            Point::new(0.1, 0.05),
1101            Point::new(-0.1, 0.05),
1102        ]);
1103        let inner = Polygon::new(vec![
1104            Point::new(-0.08, -0.03),
1105            Point::new(0.08, -0.03),
1106            Point::new(0.08, 0.03),
1107            Point::new(-0.08, 0.03),
1108        ]);
1109        let section = Section::new(outer, vec![inner]);
1110        let plastic = PlasticSection::new(section, STEEL_S355).with_fibers(500);
1111
1112        let props = plastic.plastic_properties(PlasticAxis::X);
1113
1114        // For symmetric box: yield moment = Ix / c * fy
1115        // where c = max distance from centroid to any vertex
1116        // Outer vertices: c = 0.05, inner vertices: c = 0.03
1117        // So c = 0.05 (outer governs)
1118        assert!(props.yield_moment > 0.0);
1119
1120        // Verify shape factor is reasonable for a box section (~1.1-1.2)
1121        let sf = props.plastic_section_modulus / (props.yield_moment / STEEL_S355.yield_strength);
1122        assert!(sf > 1.0 && sf < 1.5);
1123    }
1124}