Skip to main content

section_properties/plastic/
interaction.rs

1//! Interaction diagrams for combined axial + biaxial bending.
2//!
3//! Provides PMM (axial-moment-moment) interaction surfaces,
4//! section capacity checks per EN 1993, AISC 360, etc.
5
6use crate::geometry::{Point, Polygon};
7use crate::material::Material;
8use crate::plastic::plastic_section::PlasticSection;
9use crate::section::Section;
10
11/// 3D load case (axial + biaxial moments).
12#[derive(Debug, Clone, Copy, PartialEq)]
13pub struct LoadCase3D {
14    pub n: f64,  // Axial force (positive = compression)
15    pub mx: f64, // Moment about X-axis
16    pub my: f64, // Moment about Y-axis
17}
18
19impl LoadCase3D {
20    pub fn new(n: f64, mx: f64, my: f64) -> Self {
21        Self { n, mx, my }
22    }
23
24    pub fn pure_compression(n: f64) -> Self {
25        Self {
26            n,
27            mx: 0.0,
28            my: 0.0,
29        }
30    }
31
32    pub fn pure_bending_x(mx: f64) -> Self {
33        Self {
34            n: 0.0,
35            mx,
36            my: 0.0,
37        }
38    }
39
40    pub fn pure_bending_y(my: f64) -> Self {
41        Self {
42            n: 0.0,
43            mx: 0.0,
44            my,
45        }
46    }
47}
48
49/// Point on interaction diagram.
50#[derive(Debug, Clone, Copy)]
51pub struct InteractionPoint {
52    pub n: f64,
53    pub mx: f64,
54    pub my: f64,
55    /// 0 = inside (safe), 1 = on surface, -1 = outside (failed)
56    pub status: i32,
57}
58
59/// Interaction diagram/surface for a section.
60#[derive(Debug, Clone)]
61pub struct InteractionDiagram {
62    pub section: Section,
63    pub material: Material,
64    /// Points on the interaction surface
65    pub surface_points: Vec<InteractionPoint>,
66    /// Pure compression capacity
67    pub n_rd: f64,
68    /// Pure bending capacities
69    pub m_x_rd: f64,
70    pub m_y_rd: f64,
71}
72
73impl InteractionDiagram {
74    /// Create interaction diagram for a section.
75    pub fn new(section: Section, material: Material) -> Self {
76        let plastic = PlasticSection::new(section.clone(), material);
77        let full_props = plastic.full_plastic_properties();
78
79        let n_rd = section.area() * material.yield_strength;
80        let m_x_rd = full_props.x_axis.plastic_moment_capacity;
81        let m_y_rd = full_props.y_axis.plastic_moment_capacity;
82
83        let mut diagram = Self {
84            section,
85            material,
86            surface_points: Vec::new(),
87            n_rd,
88            m_x_rd,
89            m_y_rd,
90        };
91
92        diagram.generate_surface();
93        diagram
94    }
95
96    /// Generate interaction surface points.
97    fn generate_surface(&mut self) {
98        let plastic = PlasticSection::new(self.section.clone(), self.material);
99        let fy = self.material.yield_strength;
100
101        // Generate points for different axial force ratios
102        let n_ratios = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
103
104        for &n_ratio in &n_ratios {
105            let n = n_ratio * self.n_rd;
106
107            // For each n, generate moment interaction curve (Mx-My)
108            let m_points = self.moment_interaction_at_n(&plastic, n, fy);
109            self.surface_points.extend(m_points);
110        }
111
112        // Generate pure bending points (n=0) with finer resolution
113        let m_points = self.moment_interaction_at_n(&plastic, 0.0, fy);
114        self.surface_points.extend(m_points);
115    }
116
117    /// Moment interaction curve at a given axial force.
118    fn moment_interaction_at_n(
119        &self,
120        plastic: &PlasticSection,
121        n: f64,
122        fy: f64,
123    ) -> Vec<InteractionPoint> {
124        let mut points = Vec::new();
125        let area_total = self.section.area();
126
127        // Net axial force N = fy * (A_comp - A_ten) = fy * (2*A_comp - A)
128        // => compression area required for target n:
129        let a_c = (n / fy + area_total) / 2.0;
130
131        // Find PNA for this axial force
132        // For biaxial, we need to find the PNA orientation and position
133        // that gives the correct compression area and moment capacity
134
135        // Simplified: generate points by varying PNA angle
136        for i in 0..360 {
137            let theta = i as f64 * std::f64::consts::PI / 180.0; // 0 to 359 degrees
138
139            // PNA line: n_x * x + n_y * y = c
140            let n_x = theta.cos();
141            let n_y = theta.sin();
142
143            // Find PNA position c such that compression area = a_c
144            let c = self.find_pna_position(plastic, n_x, n_y, a_c, fy);
145
146            if let Some(c_val) = c {
147                // Compute moments about centroid
148                let (mx, my) = self.moments_from_pna(plastic, n_x, n_y, c_val, fy);
149
150                points.push(InteractionPoint {
151                    n,
152                    mx,
153                    my,
154                    status: 1,
155                });
156            }
157        }
158
159        points
160    }
161
162    /// Find PNA position for given normal and compression area.
163    fn find_pna_position(
164        &self,
165        plastic: &PlasticSection,
166        n_x: f64,
167        n_y: f64,
168        a_c: f64,
169        _fy: f64,
170    ) -> Option<f64> {
171        // Get bounds in PNA normal direction
172        let mut min_proj = f64::INFINITY;
173        let mut max_proj = f64::NEG_INFINITY;
174
175        for v in &self.section.outer.vertices {
176            let proj = n_x * v.x + n_y * v.y;
177            min_proj = min_proj.min(proj);
178            max_proj = max_proj.max(proj);
179        }
180
181        // Binary search for PNA position
182        let mut low = min_proj;
183        let mut high = max_proj;
184
185        for _ in 0..40 {
186            let mid = (low + high) / 2.0;
187            let area_compression = self.area_in_halfspace(plastic, n_x, n_y, mid, true);
188
189            // Compression area grows with c; if it already exceeds the
190            // target the PNA lies below mid.
191            if area_compression > a_c {
192                high = mid;
193            } else {
194                low = mid;
195            }
196
197            if (high - low).abs() < 1e-9 {
198                break;
199            }
200        }
201
202        Some((low + high) / 2.0)
203    }
204
205    /// Points inside test for half-space clipping.
206    fn inside_halfspace(p: Point, n_x: f64, n_y: f64, c: f64, below: bool) -> bool {
207        let proj = n_x * p.x + n_y * p.y;
208        if below { proj <= c } else { proj >= c }
209    }
210
211    /// Intersect segment (a, b) with line n·x = c.
212    fn line_intersection(a: Point, b: Point, n_x: f64, n_y: f64, c: f64) -> Point {
213        let proj_a = n_x * a.x + n_y * a.y;
214        let proj_b = n_x * b.x + n_y * b.y;
215        let t = (c - proj_a) / (proj_b - proj_a);
216
217        Point::new(a.x + t * (b.x - a.x), a.y + t * (b.y - a.y))
218    }
219
220    /// Clip a polygon by the half-space n·x &lt;= c (or >= c when below=false).
221    /// Sutherland–Hodgman.
222    fn clip_polygon_halfspace(
223        poly: &Polygon,
224        n_x: f64,
225        n_y: f64,
226        c: f64,
227        below: bool,
228    ) -> Option<Polygon> {
229        let mut out: Vec<Point> = Vec::new();
230        let verts = &poly.vertices;
231
232        if verts.is_empty() {
233            return None;
234        }
235
236        let s = verts.len();
237        let mut prev = verts[s - 1];
238        let mut prev_inside = Self::inside_halfspace(prev, n_x, n_y, c, below);
239
240        for i in 0..s {
241            let curr = verts[i];
242            let curr_inside = Self::inside_halfspace(curr, n_x, n_y, c, below);
243
244            if prev_inside != curr_inside {
245                out.push(Self::line_intersection(prev, curr, n_x, n_y, c));
246            }
247
248            if curr_inside {
249                out.push(curr);
250            }
251
252            prev = curr;
253            prev_inside = curr_inside;
254        }
255
256        if out.len() < 3 {
257            return None;
258        }
259        // Degenerate slivers appear when clipping at the extremes; drop them
260        // rather than tripping Polygon's zero-area assertion.
261        let pts = out;
262        let area = pts
263            .iter()
264            .enumerate()
265            .map(|(i, p)| {
266                let q = &pts[(i + 1) % pts.len()];
267                p.x * q.y - q.x * p.y
268            })
269            .sum::<f64>()
270            / 2.0;
271        if area.abs() < 1e-14 {
272            return None;
273        }
274        Some(Polygon::new(pts))
275    }
276
277    /// Compute area in half
278    /// Uses exact polygon clipping for speed.
279    fn area_in_halfspace(
280        &self,
281        _plastic: &PlasticSection,
282        n_x: f64,
283        n_y: f64,
284        c: f64,
285        below: bool,
286    ) -> f64 {
287        // Clip each polygon (outer + holes) by the half-plane
288        let mut clipped_outer = Vec::new();
289        for poly in std::iter::once(&self.section.outer).chain(self.section.holes.iter()) {
290            if let Some(clipped) = Self::clip_polygon_halfspace(poly, n_x, n_y, c, below) {
291                clipped_outer.push(clipped);
292            }
293        }
294
295        // Sum signed areas (outer CCW positive, holes CW negative)
296        clipped_outer
297            .iter()
298            .map(|p| p.signed_area())
299            .sum::<f64>()
300            .abs()
301    }
302
303    /// Compute moments from PNA definition.
304    ///
305    /// Exact: clips the section (with signed hole handling) against the two
306    /// half-planes defined by the PNA and integrates first moments from the
307    /// clipped polygon centroids. Replaces the former 200x200 grid sampling.
308    fn moments_from_pna(
309        &self,
310        _plastic: &PlasticSection,
311        n_x: f64,
312        n_y: f64,
313        c: f64,
314        fy: f64,
315    ) -> (f64, f64) {
316        let props = crate::section_properties::SectionProperties::from_section(&self.section);
317        let cx = props.centroid.x;
318        let cy = props.centroid.y;
319
320        // First moment of a polygon about the centroidal axes:
321        // M = A * (centroid - centroid_ref), accumulated with signed areas
322        // so holes subtract automatically.
323        let moment = |poly: &Polygon| -> (f64, f64) {
324            let a = poly.signed_area();
325            let cent = poly.centroid();
326            (a * (cent.y - cy), a * (cent.x - cx))
327        };
328
329        let mut mx = 0.0;
330        let mut my = 0.0;
331
332        for poly in std::iter::once(&self.section.outer).chain(self.section.holes.iter()) {
333            if let Some(below) = poly.clip_halfspace(n_x, n_y, c, true) {
334                let (mx_b, my_b) = moment(&below);
335                my += fy * my_b;
336                mx += fy * mx_b;
337            }
338            if let Some(above) = poly.clip_halfspace(n_x, n_y, c, false) {
339                let (mx_a, my_a) = moment(&above);
340                my -= fy * my_a;
341                mx -= fy * mx_a;
342            }
343        }
344
345        (mx, my)
346    }
347
348    /// Check if a load case is within the interaction surface.
349    pub fn check_capacity(&self, load: LoadCase3D, gamma_m0: f64) -> CapacityCheck {
350        let n_rd = self.n_rd / gamma_m0;
351        let m_x_rd = self.m_x_rd / gamma_m0;
352        let m_y_rd = self.m_y_rd / gamma_m0;
353
354        // Simple interaction check (conservative)
355        // For I-sections: (N/N_rd) + (Mx/M_x_rd) + (My/M_y_rd) <= 1.0
356        // More accurate: use actual interaction surface
357
358        let n_ratio = load.n.abs() / n_rd;
359        let mx_ratio = load.mx.abs() / m_x_rd;
360        let my_ratio = load.my.abs() / m_y_rd;
361
362        // Conservative linear interaction
363        let _interaction = n_ratio + mx_ratio + my_ratio;
364
365        // For biaxial bending, use ellipse approximation
366        let bending_interaction = if mx_ratio > 0.0 || my_ratio > 0.0 {
367            (mx_ratio * mx_ratio + my_ratio * my_ratio).sqrt()
368        } else {
369            0.0
370        };
371
372        let combined = n_ratio + bending_interaction;
373
374        CapacityCheck {
375            utilization: combined,
376            n_ratio,
377            mx_ratio,
378            my_ratio,
379            passed: combined <= 1.0,
380            method: "Linear + Elliptical".to_string(),
381        }
382    }
383
384    /// Check capacity using exact plastic analysis (more accurate).
385    ///
386    /// Finds the smallest scaling of the load vector that reaches the
387    /// discrete interaction surface (ray-surface test). Robust against
388    /// surface points with zero components.
389    pub fn check_capacity_exact(&self, load: LoadCase3D, gamma_m0: f64) -> CapacityCheck {
390        let ln = load.n.abs();
391        let lmx = load.mx.abs();
392        let lmy = load.my.abs();
393
394        // Smallest factor t >= 0 such that t * load lies on/outside the
395        // surface: for each surface point, the scale needed to reach it in
396        // the worst component; take the minimum over points.
397        let mut min_scale = f64::INFINITY;
398        for pt in &self.surface_points {
399            let mut scale = 0.0f64;
400            let mut covers = true;
401            for (load_c, cap_c) in [(ln, pt.n), (lmx, pt.mx), (lmy, pt.my)] {
402                // Compare magnitudes: the surface orientation (sign) depends
403                // on the PNA sweep angle and is irrelevant for capacity.
404                let cap_abs = cap_c.abs() / gamma_m0;
405                if cap_abs > 0.0 {
406                    scale = scale.max(load_c.abs() / cap_abs);
407                } else if load_c.abs() > 0.0 {
408                    // Demand in a direction this point has no capacity for.
409                    covers = false;
410                    break;
411                }
412            }
413            if covers && scale < min_scale {
414                min_scale = scale;
415            }
416        }
417
418        let utilization = if min_scale.is_finite() {
419            min_scale
420        } else {
421            f64::INFINITY
422        };
423
424        let safe = |a: f64, b: f64| if b > 0.0 { a / b } else { 0.0 };
425        CapacityCheck {
426            utilization,
427            n_ratio: safe(ln, self.n_rd / gamma_m0),
428            mx_ratio: safe(lmx, self.m_x_rd / gamma_m0),
429            my_ratio: safe(lmy, self.m_y_rd / gamma_m0),
430            passed: utilization <= 1.0,
431            method: "Exact Plastic Surface".to_string(),
432        }
433    }
434}
435
436/// Capacity check result.
437#[derive(Debug, Clone)]
438pub struct CapacityCheck {
439    pub utilization: f64,
440    pub n_ratio: f64,
441    pub mx_ratio: f64,
442    pub my_ratio: f64,
443    pub passed: bool,
444    pub method: String,
445}
446
447/// EN 1993-1-1 interaction formulas.
448pub mod en1993 {
449    use super::*;
450
451    /// Clause 6.2.9.1 - Uniform members in bending and axial compression.
452    pub fn check_combined_bending_axial(
453        section: &Section,
454        material: &Material,
455        n_ed: f64,
456        m_y_ed: f64,
457        m_z_ed: f64,
458        gamma_m0: f64,
459        _gamma_m1: f64,
460    ) -> CapacityCheck {
461        let props = crate::section_properties::SectionProperties::from_section(section);
462        let fy = material.yield_strength;
463        let area = section.area();
464
465        // Design resistances
466        let n_rd = area * fy / gamma_m0;
467        let m_y_rd = props.ix / props.max_fiber_distance_y() * fy / gamma_m0;
468        let m_z_rd = props.iy / props.max_fiber_distance_x() * fy / gamma_m0;
469
470        // Plastic moduli for Class 1/2
471        // (Would need section classification first)
472
473        let n_ratio = n_ed / n_rd;
474        let my_ratio = m_y_ed / m_y_rd;
475        let mz_ratio = m_z_ed / m_z_rd;
476
477        // Eq 6.61 & 6.62 (simplified for Class 1/2)
478        let interaction = n_ratio + my_ratio + mz_ratio;
479
480        CapacityCheck {
481            utilization: interaction,
482            n_ratio,
483            mx_ratio: my_ratio,
484            my_ratio: mz_ratio,
485            passed: interaction <= 1.0,
486            method: "EN 1993-1-1 6.2.9".to_string(),
487        }
488    }
489
490    /// Clause 6.3.3 - Biaxial bending.
491    pub fn check_biaxial_bending(
492        section: &Section,
493        material: &Material,
494        m_y_ed: f64,
495        m_z_ed: f64,
496        gamma_m0: f64,
497    ) -> CapacityCheck {
498        let props = crate::section_properties::SectionProperties::from_section(section);
499        let fy = material.yield_strength;
500
501        let m_y_rd = props.ix / props.max_fiber_distance_y() * fy / gamma_m0;
502        let m_z_rd = props.iy / props.max_fiber_distance_x() * fy / gamma_m0;
503
504        let my_ratio = m_y_ed / m_y_rd;
505        let mz_ratio = m_z_ed / m_z_rd;
506
507        // Eq 6.55 (for I-sections)
508        let alpha = 2.0; // conservative
509        let interaction = (my_ratio / alpha).powi(2) + (mz_ratio / alpha).powi(2);
510
511        CapacityCheck {
512            utilization: interaction.sqrt(),
513            n_ratio: 0.0,
514            mx_ratio: my_ratio,
515            my_ratio: mz_ratio,
516            passed: interaction <= 1.0,
517            method: "EN 1993-1-1 6.3.3".to_string(),
518        }
519    }
520}
521
522/// AISC 360 interaction equations.
523pub mod aisc360 {
524    use super::*;
525
526    /// Chapter H - Members under combined forces.
527    pub fn check_h1(
528        section: &Section,
529        material: &Material,
530        p_u: f64,   // Required axial strength
531        m_ux: f64,  // Required flexural strength about X
532        m_uy: f64,  // Required flexural strength about Y
533        phi_c: f64, // Resistance factor for compression
534        phi_b: f64, // Resistance factor for flexure
535    ) -> CapacityCheck {
536        let props = crate::section_properties::SectionProperties::from_section(section);
537        let fy = material.yield_strength;
538        let area = section.area();
539
540        // Nominal strengths
541        let p_n = area * fy;
542        let m_nx = props.ix / props.max_fiber_distance_y() * fy;
543        let m_ny = props.iy / props.max_fiber_distance_x() * fy;
544
545        let p_rd = phi_c * p_n;
546        let m_rdx = phi_b * m_nx;
547        let m_rdy = phi_b * m_ny;
548
549        let p_ratio = p_u / p_rd;
550        let mx_ratio = m_ux / m_rdx;
551        let my_ratio = m_uy / m_rdy;
552
553        // AISC 360 Eq H1-1a and H1-1b
554        let interaction = if p_ratio >= 0.2 {
555            p_ratio + 8.0 / 9.0 * (mx_ratio + my_ratio)
556        } else {
557            p_ratio / 2.0 + mx_ratio + my_ratio
558        };
559
560        CapacityCheck {
561            utilization: interaction,
562            n_ratio: p_ratio,
563            mx_ratio,
564            my_ratio,
565            passed: interaction <= 1.0,
566            method: "AISC 360 H1".to_string(),
567        }
568    }
569}
570
571#[cfg(test)]
572mod tests {
573    use super::*;
574    use crate::geometry::{Point, Polygon};
575    use crate::material::presets::STEEL_S355;
576    use crate::section::Section;
577    use crate::section_library::ParametricSection;
578
579    #[test]
580    fn interaction_rectangular() {
581        let poly = Polygon::new(vec![
582            Point::new(-0.1, -0.05),
583            Point::new(0.1, -0.05),
584            Point::new(0.1, 0.05),
585            Point::new(-0.1, 0.05),
586        ]);
587        let section = Section::new(poly, vec![]);
588        let diagram = InteractionDiagram::new(section, STEEL_S355);
589
590        assert!(diagram.n_rd > 0.0);
591        assert!(diagram.m_x_rd > 0.0);
592        assert!(diagram.m_y_rd > 0.0);
593        assert!(!diagram.surface_points.is_empty());
594    }
595
596    #[test]
597    fn capacity_check_pure_compression() {
598        let poly = Polygon::new(vec![
599            Point::new(-0.1, -0.05),
600            Point::new(0.1, -0.05),
601            Point::new(0.1, 0.05),
602            Point::new(-0.1, 0.05),
603        ]);
604        let section = Section::new(poly, vec![]);
605        let diagram = InteractionDiagram::new(section, STEEL_S355);
606
607        let load = LoadCase3D::pure_compression(diagram.n_rd * 0.5);
608        let check = diagram.check_capacity(load, 1.0);
609
610        assert!(check.passed);
611        assert!((check.utilization - 0.5).abs() < 0.1);
612    }
613
614    #[test]
615    fn capacity_check_pure_bending() {
616        let poly = Polygon::new(vec![
617            Point::new(-0.1, -0.05),
618            Point::new(0.1, -0.05),
619            Point::new(0.1, 0.05),
620            Point::new(-0.1, 0.05),
621        ]);
622        let section = Section::new(poly, vec![]);
623        let diagram = InteractionDiagram::new(section, STEEL_S355);
624
625        let load = LoadCase3D::pure_bending_x(diagram.m_x_rd * 0.8);
626        let check = diagram.check_capacity(load, 1.0);
627
628        assert!(check.passed);
629        assert!((check.utilization - 0.8).abs() < 0.1);
630    }
631
632    #[test]
633    fn capacity_check_combined() {
634        let poly = Polygon::new(vec![
635            Point::new(-0.1, -0.05),
636            Point::new(0.1, -0.05),
637            Point::new(0.1, 0.05),
638            Point::new(-0.1, 0.05),
639        ]);
640        let section = Section::new(poly, vec![]);
641        let diagram = InteractionDiagram::new(section, STEEL_S355);
642
643        // 50% compression + 60% Mx -> should fail (1.1 > 1.0)
644        let load = LoadCase3D::new(diagram.n_rd * 0.5, diagram.m_x_rd * 0.6, 0.0);
645        let check = diagram.check_capacity(load, 1.0);
646
647        assert!(!check.passed);
648        assert!(check.utilization > 1.0);
649    }
650
651    #[test]
652    fn en1993_check() {
653        let i = crate::section_library::steel::ISection::new(0.3, 0.15, 0.007, 0.01, 0.012);
654        let section = i.build();
655
656        let check = en1993::check_combined_bending_axial(
657            &section,
658            &STEEL_S355,
659            500e3, // N = 500 kN
660            50e3,  // My = 50 kNm
661            10e3,  // Mz = 10 kNm
662            1.0,
663            1.0,
664        );
665
666        assert!(check.utilization > 0.0);
667    }
668
669    #[test]
670    fn aisc360_check() {
671        let i = crate::section_library::steel::ISection::new(0.3, 0.15, 0.007, 0.01, 0.012);
672        let section = i.build();
673
674        let check = aisc360::check_h1(
675            &section,
676            &STEEL_S355,
677            500e3, // Pu = 500 kN
678            50e3,  // Mux = 50 kNm
679            10e3,  // Muy = 10 kNm
680            0.9,
681            0.9,
682        );
683
684        assert!(check.utilization > 0.0);
685    }
686}