Skip to main content

section_properties/plastic/
classification.rs

1//! Section classification per EN 1993-1-1 (Eurocode 3).
2//!
3//! Determines cross-section class (1, 2, 3, 4) based on
4// width-to-thickness ratios and stress distribution.
5// Class 1: Plastic hinge rotation capacity
6// Class 2: Plastic moment capacity
7// Class 3: Elastic moment capacity (first yield)
8// Class 4: Local buckling governs (effective section)
9
10use crate::material::Material;
11use crate::section::Section;
12use crate::section_properties::SectionProperties;
13
14/// Cross-section class per EN 1993-1-1 Table 5.2.
15/// Ordering: Class1 > Class2 > Class3 > Class4 (Class1 is most favorable)
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum SectionClass {
18    /// Class 1 - Plastic (full plastic moment + rotation capacity)
19    Class1 = 1,
20    /// Class 2 - Compact (plastic moment, limited rotation)
21    Class2 = 2,
22    /// Class 3 - Semi-compact (elastic moment, first yield)
23    Class3 = 3,
24    /// Class 4 - Slender (local buckling, effective section)
25    Class4 = 4,
26}
27
28impl PartialOrd for SectionClass {
29    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
30        Some(self.cmp(other))
31    }
32}
33
34impl Ord for SectionClass {
35    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
36        // Reverse: Class1 > Class2 > Class3 > Class4
37        (*self as u8).cmp(&(*other as u8)).reverse()
38    }
39}
40
41impl SectionClass {
42    /// Can develop plastic moment resistance?
43    pub fn can_plastic_moment(&self) -> bool {
44        matches!(self, SectionClass::Class1 | SectionClass::Class2)
45    }
46
47    /// Can develop plastic hinge rotation?
48    pub fn can_plastic_rotation(&self) -> bool {
49        matches!(self, SectionClass::Class1)
50    }
51
52    /// Must use effective section properties?
53    pub fn requires_effective_section(&self) -> bool {
54        matches!(self, SectionClass::Class4)
55    }
56
57    /// Description of the class.
58    pub fn description(&self) -> &'static str {
59        match self {
60            SectionClass::Class1 => "Class 1 - Plastic (rotation capacity)",
61            SectionClass::Class2 => "Class 2 - Compact (plastic moment)",
62            SectionClass::Class3 => "Class 3 - Semi-compact (elastic moment)",
63            SectionClass::Class4 => "Class 4 - Slender (local buckling)",
64        }
65    }
66}
67
68/// Stress distribution in the element per EN 1993-1-1 Table 5.2.
69#[derive(Debug, Clone, Copy, PartialEq)]
70pub enum StressDistribution {
71    /// Uniform compression (e.g., flange in pure compression)
72    UniformCompression,
73    /// Linear varying stress (e.g., web in bending)
74    Bending,
75    /// Web in combined bending and axial
76    BendingAndCompression { alpha: f64 }, // α = plastic compression zone ratio (0=pure tension, 0.5=pure bending, 1=pure compression)
77    /// Flange in combined bending and axial
78    FlangeBendingAndCompression { psi: f64 }, // ψ = σ_2/σ_1
79}
80
81impl StressDistribution {
82    /// For pure bending (σ varies linearly from +σ to -σ)
83    pub fn pure_bending() -> Self {
84        StressDistribution::Bending
85    }
86
87    /// For pure compression
88    pub fn pure_compression() -> Self {
89        StressDistribution::UniformCompression
90    }
91
92    /// For web with axial + bending (α parameter).
93    ///
94    /// α is the plastic compression zone ratio (0 to 1):
95    /// - α = 1.0: pure compression
96    /// - α = 0.5: pure bending (doubly-symmetric section)
97    /// - α = 0.0: pure tension (no compression zone)
98    pub fn web_bending_compression(alpha: f64) -> Self {
99        StressDistribution::BendingAndCompression { alpha }
100    }
101
102    /// For flange with axial + bending (ψ parameter)
103    pub fn flange_bending_compression(psi: f64) -> Self {
104        StressDistribution::FlangeBendingAndCompression { psi }
105    }
106}
107
108/// Class limits (ε = sqrt(235/fy) for steel).
109#[derive(Debug, Clone, Copy)]
110pub struct ClassLimit {
111    pub class1: f64,
112    pub class2: f64,
113    pub class3: f64,
114}
115
116impl ClassLimit {
117    /// Check classification for a given width-to-thickness ratio.
118    pub fn classify(&self, c_t: f64) -> SectionClass {
119        if c_t <= self.class1 {
120            SectionClass::Class1
121        } else if c_t <= self.class2 {
122            SectionClass::Class2
123        } else if c_t <= self.class3 {
124            SectionClass::Class3
125        } else {
126            SectionClass::Class4
127        }
128    }
129}
130
131/// Get class limits for internal compression parts (webs) per Table 5.2.
132fn web_limits(eps: f64, stress: StressDistribution) -> ClassLimit {
133    match stress {
134        StressDistribution::UniformCompression => ClassLimit {
135            class1: 33.0 * eps,
136            class2: 38.0 * eps,
137            class3: 42.0 * eps,
138        },
139        StressDistribution::Bending => ClassLimit {
140            class1: 72.0 * eps,
141            class2: 83.0 * eps,
142            class3: 124.0 * eps,
143        },
144        StressDistribution::BendingAndCompression { alpha } => {
145            // EN 1993-1-1 Table 5.2 (sheet 2), web subject to bending and axial compression
146            // α = plastic compression zone ratio (0=pure tension, 0.5=pure bending, 1=pure compression)
147            // ψ = stress ratio = σ_min/σ_max; for doubly-symmetric sections ψ = 4α - 3
148            let alpha = alpha.clamp(0.0, 1.0);
149            let psi = 4.0 * alpha - 3.0;
150            let (class1, class2) = if alpha > 0.5 {
151                (
152                    396.0 * eps / (13.0 * alpha - 1.0),
153                    456.0 * eps / (13.0 * alpha - 1.0),
154                )
155            } else if alpha > 0.0 {
156                (36.0 * eps / alpha, 41.5 * eps / alpha)
157            } else {
158                (f64::MAX, f64::MAX)
159            };
160            // Class 3: 42ε / (0.67 + 0.33ψ); guard against non-positive denominator
161            let class3_denom = 0.67 + 0.33 * psi;
162            let class3 = if class3_denom > 0.0 {
163                42.0 * eps / class3_denom
164            } else {
165                f64::MAX
166            };
167            ClassLimit {
168                class1,
169                class2,
170                class3,
171            }
172        }
173        StressDistribution::FlangeBendingAndCompression { .. } => {
174            // Flange limits handled separately
175            ClassLimit {
176                class1: 0.0,
177                class2: 0.0,
178                class3: 0.0,
179            }
180        }
181    }
182}
183
184/// Get class limits for outstand flanges per Table 5.2.
185fn outstand_flange_limits(eps: f64, stress: StressDistribution) -> ClassLimit {
186    match stress {
187        StressDistribution::UniformCompression => ClassLimit {
188            class1: 9.0 * eps,
189            class2: 10.0 * eps,
190            class3: 14.0 * eps,
191        },
192        StressDistribution::Bending => ClassLimit {
193            class1: 9.0 * eps,
194            class2: 10.0 * eps,
195            class3: 14.0 * eps,
196        },
197        StressDistribution::BendingAndCompression { .. } => {
198            // Use uniform compression limits for conservative estimate
199            ClassLimit {
200                class1: 9.0 * eps,
201                class2: 10.0 * eps,
202                class3: 14.0 * eps,
203            }
204        }
205        StressDistribution::FlangeBendingAndCompression { psi } => {
206            // ψ = σ_2/σ_1 (stress ratio)
207            // EN 1993-1-5 for combined stress
208            let class1 = 9.0 * eps / (1.0 + psi).sqrt().max(0.1);
209            let class2 = 10.0 * eps / (1.0 + psi).sqrt().max(0.1);
210            let class3 = 14.0 * eps / (1.0 + psi).sqrt().max(0.1);
211            ClassLimit {
212                class1,
213                class2,
214                class3,
215            }
216        }
217    }
218}
219
220/// Get class limits for internal flanges (e.g., box section flanges).
221fn internal_flange_limits(eps: f64) -> ClassLimit {
222    ClassLimit {
223        class1: 33.0 * eps,
224        class2: 38.0 * eps,
225        class3: 42.0 * eps,
226    }
227}
228
229/// Classification result for a section.
230#[derive(Debug, Clone)]
231pub struct SectionClassification {
232    /// Overall section class (governed by worst element)
233    pub overall_class: SectionClass,
234    /// Web classification
235    pub web_class: SectionClass,
236    /// Flange classification (compression flange)
237    pub flange_class: SectionClass,
238    /// Flange classification (tension flange)
239    pub tension_flange_class: SectionClass,
240    /// Individual element ratios
241    pub web_c_t: f64,
242    pub flange_c_t: f64,
243    /// Epsilon factor
244    pub epsilon: f64,
245    /// Stress distribution assumed for web
246    pub web_stress: StressDistribution,
247    /// Stress distribution assumed for flange
248    pub flange_stress: StressDistribution,
249    /// Whether section is Class 4 (requires effective section)
250    pub is_class4: bool,
251}
252
253impl SectionClassification {
254    /// Check if section can develop plastic moment.
255    pub fn can_plastic_moment(&self) -> bool {
256        self.overall_class.can_plastic_moment()
257    }
258
259    /// Check if section can develop plastic rotation.
260    pub fn can_plastic_rotation(&self) -> bool {
261        self.overall_class.can_plastic_rotation()
262    }
263
264    /// Get governing class description.
265    pub fn governing_element(&self) -> &'static str {
266        let classes = [
267            (self.web_class, "web"),
268            (self.flange_class, "compression flange"),
269            (self.tension_flange_class, "tension flange"),
270        ];
271        classes
272            .iter()
273            .max_by_key(|(c, _)| *c as u8)
274            .map(|(_, name)| *name)
275            .unwrap_or("unknown")
276    }
277
278    /// Summary string.
279    pub fn summary(&self) -> String {
280        format!(
281            "Overall: {} (governed by {}) | Web: c/t={:.1} ({}) | Flange: c/t={:.1} ({}) | ε={:.3}",
282            self.overall_class.description(),
283            self.governing_element(),
284            self.web_c_t,
285            self.web_class.description(),
286            self.flange_c_t,
287            self.flange_class.description(),
288            self.epsilon
289        )
290    }
291}
292
293/// Classify an I/H-section per EN 1993-1-1.
294pub fn classify_i_section(
295    section: &Section,
296    material: &Material,
297    stress: StressDistribution,
298) -> SectionClassification {
299    let fy = material.yield_strength;
300    let eps = (235e6 / fy).sqrt();
301
302    // Get geometric properties
303    let props = SectionProperties::from_section(section);
304    let bounds = section.bounds();
305    let _h = bounds.3 - bounds.2; // depth (max_y - min_y)
306    let b = bounds.1 - bounds.0; // width (max_x - min_x)
307
308    // Estimate web and flange dimensions from section geometry
309    // For I-section: find web thickness and flange thickness
310    let (tw, tf, hw) = estimate_i_section_dimensions(section, &props);
311
312    // Web: c/t = (h - 2*tf) / tw for internal part
313    let web_c_t = hw / tw;
314
315    // Flange: c/t = (b - tw)/2 / tf for outstand
316    let flange_c_t = (b - tw) / (2.0 * tf);
317
318    // Classify web
319    let web_limits = web_limits(eps, stress);
320    let web_class = web_limits.classify(web_c_t);
321
322    // Classify compression flange (outstand)
323    let flange_limits = outstand_flange_limits(eps, stress);
324    let flange_class = flange_limits.classify(flange_c_t);
325
326    // Tension flange (same geometry, but tension is always Class 1 for ductile steel)
327    let tension_flange_class = SectionClass::Class1;
328
329    // Overall class = min of all elements (worst class governs, with reversed Ord)
330    let overall_class = [web_class, flange_class, tension_flange_class]
331        .iter()
332        .min()
333        .copied()
334        .unwrap_or(SectionClass::Class4);
335
336    SectionClassification {
337        overall_class,
338        web_class,
339        flange_class,
340        tension_flange_class,
341        web_c_t,
342        flange_c_t,
343        epsilon: eps,
344        web_stress: stress,
345        flange_stress: stress,
346        is_class4: overall_class == SectionClass::Class4,
347    }
348}
349
350/// Classify a hollow section (CHS/RHS/SHS) per EN 1993-1-1.
351pub fn classify_hollow_section(
352    section: &Section,
353    material: &Material,
354    stress: StressDistribution,
355) -> SectionClassification {
356    let fy = material.yield_strength;
357    let eps = (235e6 / fy).sqrt();
358
359    let props = SectionProperties::from_section(section);
360    let bounds = section.bounds();
361    let h = bounds.3 - bounds.2; // height (max_y - min_y)
362    let b = bounds.1 - bounds.0; // width (max_x - min_x)
363
364    // Check if circular or rectangular
365    let is_circular = (h - b).abs() / h.max(b) < 0.05;
366
367    if is_circular {
368        // CHS - use diameter/thickness
369        let area = props.area;
370        let d = h; // diameter
371        // t = A / (π*d) for thin-walled
372        let t = area / (std::f64::consts::PI * d);
373        let d_t = d / t;
374
375        // CHS limits per EN 1993-1-1 Table 5.2 (d): 50ε², 70ε², 90ε²
376        // (same for all stress distributions)
377        let limits = ClassLimit {
378            class1: 50.0 * eps * eps,
379            class2: 70.0 * eps * eps,
380            class3: 90.0 * eps * eps,
381        };
382
383        let overall_class = limits.classify(d_t);
384
385        SectionClassification {
386            overall_class,
387            web_class: overall_class,
388            flange_class: overall_class,
389            tension_flange_class: overall_class,
390            web_c_t: d_t,
391            flange_c_t: d_t,
392            epsilon: eps,
393            web_stress: stress,
394            flange_stress: stress,
395            is_class4: overall_class == SectionClass::Class4,
396        }
397    } else {
398        // RHS/SHS
399        // For rectangular hollow sections, classify each wall
400        // c/t = (h - 2t)/t for webs, (b - 2t)/t for flanges
401        let area = props.area;
402        let peri = 2.0 * (h + b);
403        let t = area / peri; // average thickness
404
405        let web_c_t = (h - 2.0 * t) / t;
406        let flange_c_t = (b - 2.0 * t) / t;
407
408        // Internal compression parts (Table 5.2)
409        let web_limits = web_limits(eps, stress);
410        let flange_limits = internal_flange_limits(eps);
411
412        let web_class = web_limits.classify(web_c_t);
413        let flange_class = flange_limits.classify(flange_c_t);
414        let tension_flange_class = SectionClass::Class1;
415
416        let overall_class = [web_class, flange_class, tension_flange_class]
417            .iter()
418            .min()
419            .copied()
420            .unwrap_or(SectionClass::Class4);
421
422        SectionClassification {
423            overall_class,
424            web_class,
425            flange_class,
426            tension_flange_class,
427            web_c_t,
428            flange_c_t,
429            epsilon: eps,
430            web_stress: stress,
431            flange_stress: stress,
432            is_class4: overall_class == SectionClass::Class4,
433        }
434    }
435}
436
437/// Classify an angle section.
438pub fn classify_angle_section(section: &Section, material: &Material) -> SectionClassification {
439    let fy = material.yield_strength;
440    let eps = (235e6 / fy).sqrt();
441
442    let bounds = section.bounds();
443    let h = bounds.3 - bounds.2; // height (max_y - min_y)
444    let b = bounds.1 - bounds.0; // width (max_x - min_x)
445
446    // For angles, classify each leg as outstand flange
447    let props = SectionProperties::from_section(section);
448    let area = props.area;
449    let _peri = section.outer.vertices.len() as f64; // rough
450    let t = area / (h + b); // approximate
451
452    let c_t_leg1 = h / t;
453    let c_t_leg2 = b / t;
454
455    let limits = outstand_flange_limits(eps, StressDistribution::UniformCompression);
456    let class1 = limits.classify(c_t_leg1);
457    let class2 = limits.classify(c_t_leg2);
458
459    let overall_class = [class1, class2]
460        .iter()
461        .min()
462        .copied()
463        .unwrap_or(SectionClass::Class4);
464
465    SectionClassification {
466        overall_class,
467        web_class: class1,
468        flange_class: class2,
469        tension_flange_class: SectionClass::Class1,
470        web_c_t: c_t_leg1,
471        flange_c_t: c_t_leg2,
472        epsilon: eps,
473        web_stress: StressDistribution::UniformCompression,
474        flange_stress: StressDistribution::UniformCompression,
475        is_class4: overall_class == SectionClass::Class4,
476    }
477}
478
479/// Classify a channel section.
480pub fn classify_channel_section(
481    section: &Section,
482    material: &Material,
483    stress: StressDistribution,
484) -> SectionClassification {
485    // Channel: web + flanges (one side only)
486    // Web classified as internal, flange as outstand
487    let fy = material.yield_strength;
488    let eps = (235e6 / fy).sqrt();
489
490    let props = SectionProperties::from_section(section);
491    let bounds = section.bounds();
492    let _h = bounds.3 - bounds.2; // height (max_y - min_y)
493    let b = bounds.1 - bounds.0; // width (max_x - min_x)
494
495    let (tw, tf, hw) = estimate_i_section_dimensions(section, &props);
496
497    let web_c_t = hw / tw;
498    let flange_c_t = b / tf; // outstand
499
500    let web_limits = web_limits(eps, stress);
501    let flange_limits = outstand_flange_limits(eps, stress);
502
503    let web_class = web_limits.classify(web_c_t);
504    let flange_class = flange_limits.classify(flange_c_t);
505    let tension_flange_class = SectionClass::Class1;
506
507    let overall_class = [web_class, flange_class, tension_flange_class]
508        .iter()
509        .min()
510        .copied()
511        .unwrap_or(SectionClass::Class4);
512
513    SectionClassification {
514        overall_class,
515        web_class,
516        flange_class,
517        tension_flange_class,
518        web_c_t,
519        flange_c_t,
520        epsilon: eps,
521        web_stress: stress,
522        flange_stress: stress,
523        is_class4: overall_class == SectionClass::Class4,
524    }
525}
526
527/// Classify a T-section (tee).
528pub fn classify_tee_section(
529    section: &Section,
530    material: &Material,
531    stress: StressDistribution,
532) -> SectionClassification {
533    // Tee: stem (web) + flange
534    let fy = material.yield_strength;
535    let eps = (235e6 / fy).sqrt();
536
537    let props = SectionProperties::from_section(section);
538    let bounds = section.bounds();
539    let _h = bounds.3 - bounds.2; // height (max_y - min_y)
540    let b = bounds.1 - bounds.0; // width (max_x - min_x)
541
542    let (tw, tf, hw) = estimate_i_section_dimensions(section, &props);
543
544    // Stem = web
545    let web_c_t = hw / tw;
546    // Flange = outstand
547    let flange_c_t = (b - tw) / (2.0 * tf);
548
549    let web_limits = web_limits(eps, stress);
550    let flange_limits = outstand_flange_limits(eps, stress);
551
552    let web_class = web_limits.classify(web_c_t);
553    let flange_class = flange_limits.classify(flange_c_t);
554    let tension_flange_class = SectionClass::Class1;
555
556    let overall_class = [web_class, flange_class, tension_flange_class]
557        .iter()
558        .min()
559        .copied()
560        .unwrap_or(SectionClass::Class4);
561
562    SectionClassification {
563        overall_class,
564        web_class,
565        flange_class,
566        tension_flange_class,
567        web_c_t,
568        flange_c_t,
569        epsilon: eps,
570        web_stress: stress,
571        flange_stress: stress,
572        is_class4: overall_class == SectionClass::Class4,
573    }
574}
575
576/// Generic classification - auto-detect section type.
577pub fn classify_section(
578    section: &Section,
579    material: &Material,
580    stress: StressDistribution,
581) -> SectionClassification {
582    // Try to detect section type from geometry
583    let props = SectionProperties::from_section(section);
584    let bounds = section.bounds();
585    let h = bounds.3 - bounds.2; // height (max_y - min_y)
586    let b = bounds.1 - bounds.0; // width (max_x - min_x)
587    let area = props.area;
588
589    // Check if hollow (has holes)
590    if !section.holes.is_empty() {
591        return classify_hollow_section(section, material, stress);
592    }
593
594    // Check aspect ratio and area to guess type
595    let _peri = section.outer.vertices.len() as f64;
596    let _t_est = area / (2.0 * (h + b)); // rough thickness
597
598    // If very thin-walled and closed -> hollow
599    if area / (h * b) < 0.15 && section.holes.is_empty() {
600        // Might be hollow without explicit hole (thin-walled closed)
601        // Check if it's a tube-like shape
602    }
603
604    // Heuristic: count vertices for simple shapes
605    let n_verts = section.outer.vertices.len();
606
607    if n_verts <= 6 {
608        // Could be angle, tee, or simple shape
609        // Check symmetry
610        if section.is_symmetric_about_x() && section.is_symmetric_about_y() {
611            // Could be box or I-section
612            if section.holes.is_empty() && area / (h * b) > 0.3 {
613                // Solid-ish -> I-section or box
614                return classify_i_section(section, material, stress);
615            }
616        } else if section.is_symmetric_about_x() || section.is_symmetric_about_y() {
617            // Tee or channel
618            return classify_tee_section(section, material, stress);
619        } else {
620            // Angle
621            return classify_angle_section(section, material);
622        }
623    }
624
625    // Default to I-section classification
626    classify_i_section(section, material, stress)
627}
628
629/// Estimate I-section dimensions (tw, tf, hw) from geometry.
630fn estimate_i_section_dimensions(section: &Section, props: &SectionProperties) -> (f64, f64, f64) {
631    let bounds = section.bounds();
632    let h = bounds.3 - bounds.2; // height (max_y - min_y)
633    let b = bounds.1 - bounds.0; // width (max_x - min_x)
634    let _area = props.area;
635
636    // For I-section: area ≈ 2*b*tf + (h-2*tf)*tw
637    // We need to estimate tw and tf from the polygon vertices
638
639    // Find unique x and y coordinates
640    let mut x_coords: Vec<f64> = section.outer.vertices.iter().map(|v| v.x).collect();
641    let mut y_coords: Vec<f64> = section.outer.vertices.iter().map(|v| v.y).collect();
642    x_coords.sort_by(|a, b| a.partial_cmp(b).unwrap());
643    y_coords.sort_by(|a, b| a.partial_cmp(b).unwrap());
644
645    // Web thickness: find the smallest non-zero |x| among vertices near center
646    // This correctly handles root radii: web vertices have |x| = sw (smallest),
647    // radius vertices have |x| in [sw, sw+r] (larger)
648    let center_x = (bounds.0 + bounds.1) / 2.0;
649    let mut x_abs: Vec<f64> = section
650        .outer
651        .vertices
652        .iter()
653        .filter(|v| (v.x - center_x).abs() < b * 0.25)
654        .map(|v| (v.x - center_x).abs())
655        .filter(|x| *x > 1e-9)
656        .collect();
657    x_abs.sort_by(|a, b| a.partial_cmp(b).unwrap());
658
659    let tw = if !x_abs.is_empty() {
660        x_abs[0] * 2.0
661    } else {
662        b * 0.1
663    };
664
665    // Flange thickness = minimum y-distance at flange
666    let top_vertices: Vec<f64> = section
667        .outer
668        .vertices
669        .iter()
670        .filter(|v| v.y > bounds.3 - h * 0.1)
671        .map(|v| v.y)
672        .collect();
673    let tf = if top_vertices.len() >= 2 {
674        top_vertices
675            .iter()
676            .max_by(|a, b| a.partial_cmp(b).unwrap())
677            .unwrap()
678            - top_vertices
679                .iter()
680                .min_by(|a, b| a.partial_cmp(b).unwrap())
681                .unwrap()
682    } else {
683        h * 0.05
684    };
685
686    let hw = h - 2.0 * tf;
687
688    (tw.max(1e-6), tf.max(1e-6), hw.max(1e-6))
689}
690
691/// Effective section properties for Class 4 sections (EN 1993-1-5).
692pub mod effective {
693    use super::*;
694    use crate::geometry::Point;
695    use crate::section::Section;
696    use crate::section_properties::SectionProperties;
697
698    /// Effective section properties after local buckling reduction.
699    #[derive(Debug, Clone)]
700    pub struct EffectiveProperties {
701        pub area_eff: f64,
702        pub centroid_eff: Point,
703        pub ix_eff: f64,
704        pub iy_eff: f64,
705        pub ixy_eff: f64,
706        pub classification: SectionClassification,
707        /// Reduction factors for each element
708        pub reduction_factors: ElementReductions,
709    }
710
711    #[derive(Debug, Clone, Default)]
712    pub struct ElementReductions {
713        pub web_rho: f64,
714        pub flange_rho: f64,
715    }
716
717    /// Compute effective section for Class 4 sections per EN 1993-1-5.
718    pub fn effective_section(
719        section: &Section,
720        material: &Material,
721        stress: StressDistribution,
722    ) -> EffectiveProperties {
723        let classification = classify_section(section, material, stress);
724
725        if !classification.is_class4 {
726            // Not Class 4 - return gross properties
727            let props = SectionProperties::from_section(section);
728            return EffectiveProperties {
729                area_eff: props.area,
730                centroid_eff: props.centroid,
731                ix_eff: props.ix,
732                iy_eff: props.iy,
733                ixy_eff: props.ixy,
734                classification,
735                reduction_factors: ElementReductions::default(),
736            };
737        }
738
739        // For Class 4, compute reduced widths per EN 1993-1-5
740        let fy = material.yield_strength;
741        let eps = (235e6 / fy).sqrt();
742
743        // Simplified: apply reduction factor to slender elements
744        let mut rho_web = 1.0;
745        let mut rho_flange = 1.0;
746
747        // Web reduction (EN 1993-1-5 Eq 4.2)
748        if classification.web_class == SectionClass::Class4 {
749            let lambda_p = classification.web_c_t / (33.0 * eps); // normalized slenderness
750            if lambda_p > 0.673 {
751                rho_web = (lambda_p - 0.22) / lambda_p;
752                rho_web = rho_web.clamp(0.0, 1.0);
753            }
754        }
755
756        // Flange reduction (EN 1993-1-5 Eq 4.2)
757        if classification.flange_class == SectionClass::Class4 {
758            let lambda_p = classification.flange_c_t / (9.0 * eps);
759            if lambda_p > 0.673 {
760                rho_flange = (lambda_p - 0.22) / lambda_p;
761                rho_flange = rho_flange.clamp(0.0, 1.0);
762            }
763        }
764
765        // Build effective section polygon with reduced widths
766        // This is complex - for now return gross properties with warning
767        let props = SectionProperties::from_section(section);
768
769        // Apply rough reduction to properties
770        let area_red = 1.0 - (1.0 - rho_web) * 0.3 - (1.0 - rho_flange) * 0.2; // approximate
771        let ix_red = 1.0 - (1.0 - rho_web) * 0.5 - (1.0 - rho_flange) * 0.3;
772
773        EffectiveProperties {
774            area_eff: props.area * area_red,
775            centroid_eff: props.centroid,
776            ix_eff: props.ix * ix_red,
777            iy_eff: props.iy * ix_red, // approximate
778            ixy_eff: props.ixy * ix_red,
779            classification,
780            reduction_factors: ElementReductions {
781                web_rho: rho_web,
782                flange_rho: rho_flange,
783            },
784        }
785    }
786}
787
788/// Section trait for symmetry checks.
789trait SectionSymmetryCheck {
790    fn is_symmetric_about_x(&self) -> bool;
791    fn is_symmetric_about_y(&self) -> bool;
792}
793
794impl SectionSymmetryCheck for Section {
795    fn is_symmetric_about_x(&self) -> bool {
796        for v in &self.outer.vertices {
797            let found = self
798                .outer
799                .vertices
800                .iter()
801                .any(|v2| (v2.x - v.x).abs() < 1e-6 && (v2.y + v.y).abs() < 1e-6);
802            if !found {
803                return false;
804            }
805        }
806        true
807    }
808
809    fn is_symmetric_about_y(&self) -> bool {
810        for v in &self.outer.vertices {
811            let found = self
812                .outer
813                .vertices
814                .iter()
815                .any(|v2| (v2.y - v.y).abs() < 1e-6 && (v2.x + v.x).abs() < 1e-6);
816            if !found {
817                return false;
818            }
819        }
820        true
821    }
822}
823
824#[cfg(test)]
825mod tests {
826    use super::*;
827
828    use crate::material::presets::STEEL_S355;
829
830    use crate::section_library::ParametricSection;
831    use crate::section_library::steel::ISection;
832
833    #[test]
834    fn classify_i_section_class1() {
835        // Stocky I-section (IPE300 in S355)
836        let i = ISection::from_designation("IPE300").unwrap();
837        let section = i.build();
838        let classification =
839            classify_i_section(&section, &STEEL_S355, StressDistribution::pure_bending());
840
841        // IPE300 in S355 should be Class 1 or 2
842        assert!(classification.overall_class >= SectionClass::Class2);
843        assert!(classification.can_plastic_moment());
844    }
845
846    #[test]
847    fn classify_i_section_class4() {
848        // Very slender I-section
849        let i = ISection::new(1.0, 0.2, 0.003, 0.005, 0.01); // Very thin web
850        let section = i.build();
851        let classification =
852            classify_i_section(&section, &STEEL_S355, StressDistribution::pure_bending());
853
854        assert_eq!(classification.overall_class, SectionClass::Class4);
855        assert!(classification.is_class4);
856    }
857
858    #[test]
859    fn classify_chs() {
860        // CHS 219x8 in S355
861        let chs = crate::section_library::steel::CircularHollowSectionLib::new(0.219, 0.008);
862        let section = chs.build();
863        let classification =
864            classify_hollow_section(&section, &STEEL_S355, StressDistribution::pure_bending());
865
866        // d/t = 219/8 = 27.4, limit Class 1 = 50*ε² = 50*0.81² = 33.1 -> Class 1
867        assert!(classification.overall_class >= SectionClass::Class2);
868    }
869
870    #[test]
871    fn classify_rhs() {
872        // RHS 200x100x8 in S355
873        let rhs = crate::section_library::steel::RectangularHollowSection::new(
874            0.2, 0.1, 0.008, 0.012, 0.004,
875        );
876        let section = rhs.build();
877        let classification =
878            classify_hollow_section(&section, &STEEL_S355, StressDistribution::pure_bending());
879
880        // h/t = 200/8 = 25, limit Class 1 = 33*ε = 26.7 -> Class 1/2
881        assert!(classification.overall_class >= SectionClass::Class2);
882    }
883
884    #[test]
885    fn classify_angle() {
886        let angle = crate::section_library::steel::AngleSection::equal_leg(0.1, 0.01);
887        let section = angle.build();
888        let classification = classify_angle_section(&section, &STEEL_S355);
889
890        // c/t = 100/10 = 10, limit Class 1 = 9*ε = 7.3 -> Class 2 or 3
891        assert!(classification.overall_class <= SectionClass::Class1);
892    }
893
894    #[test]
895    fn epsilon_calculation() {
896        // S235: ε = 1.0
897        let eps_s235: f64 = (235.0e6_f64 / 235.0e6_f64).sqrt();
898        assert!((eps_s235 - 1.0).abs() < 1e-6);
899
900        // S355: ε = sqrt(235/355) = 0.814
901        let eps_s355: f64 = (235.0e6_f64 / 355.0e6_f64).sqrt();
902        assert!((eps_s355 - 0.814).abs() < 0.01);
903
904        // S460: ε = sqrt(235/460) = 0.715
905        let eps_s460: f64 = (235.0e6_f64 / 460.0e6_f64).sqrt();
906        assert!((eps_s460 - 0.715).abs() < 0.01);
907    }
908
909    #[test]
910    fn effective_section_class4() {
911        // Create a Class 4 section
912        let i = ISection::new(0.5, 0.15, 0.003, 0.008, 0.01);
913        let section = i.build();
914
915        let effective =
916            effective::effective_section(&section, &STEEL_S355, StressDistribution::pure_bending());
917
918        assert!(effective.classification.is_class4);
919        assert!(effective.area_eff < effective.classification.web_c_t * 0.001); // Some reduction
920        assert!(effective.reduction_factors.web_rho < 1.0);
921    }
922
923    #[test]
924    fn class_hierarchy() {
925        assert!(SectionClass::Class1 > SectionClass::Class2);
926        assert!(SectionClass::Class2 > SectionClass::Class3);
927        assert!(SectionClass::Class3 > SectionClass::Class4);
928
929        assert!(SectionClass::Class1.can_plastic_moment());
930        assert!(SectionClass::Class2.can_plastic_moment());
931        assert!(!SectionClass::Class3.can_plastic_moment());
932        assert!(!SectionClass::Class4.can_plastic_moment());
933
934        assert!(SectionClass::Class1.can_plastic_rotation());
935        assert!(!SectionClass::Class2.can_plastic_rotation());
936    }
937
938    #[test]
939    fn stress_distribution_variants() {
940        let bending = StressDistribution::pure_bending();
941        let compression = StressDistribution::pure_compression();
942        let _web_bc = StressDistribution::web_bending_compression(0.5);
943        let _flange_bc = StressDistribution::flange_bending_compression(-0.5);
944
945        assert_eq!(bending, StressDistribution::Bending);
946        assert_eq!(compression, StressDistribution::UniformCompression);
947    }
948
949    #[test]
950    fn web_bending_compression_limits_pure_compression() {
951        // α = 1.0 (pure compression) should match UniformCompression: 33ε, 38ε, 42ε
952        let eps = 1.0;
953        let bc = web_limits(eps, StressDistribution::web_bending_compression(1.0));
954        let uc = web_limits(eps, StressDistribution::pure_compression());
955        assert!((bc.class1 - uc.class1).abs() < 1e-10);
956        assert!((bc.class2 - uc.class2).abs() < 1e-10);
957        assert!((bc.class3 - uc.class3).abs() < 1e-10);
958    }
959
960    #[test]
961    fn web_bending_compression_limits_pure_bending() {
962        // α = 0.5 (pure bending) should match Bending: 72ε, 83ε, 124ε
963        let eps = 1.0;
964        let bc = web_limits(eps, StressDistribution::web_bending_compression(0.5));
965        let bending = web_limits(eps, StressDistribution::pure_bending());
966        assert!((bc.class1 - bending.class1).abs() < 1e-10);
967        assert!((bc.class2 - bending.class2).abs() < 1e-10);
968        assert!((bc.class3 - bending.class3).abs() < 1.0); // 123.5 vs 124 (rounding)
969    }
970
971    #[test]
972    fn web_bending_compression_limits_intermediate() {
973        // α = 0.75: Class 1 = 396/(13*0.75-1) = 45.26ε
974        //           Class 2 = 456/(13*0.75-1) = 52.11ε
975        //           ψ = 0, Class 3 = 42/0.67 = 62.69ε
976        let eps = 1.0;
977        let bc = web_limits(eps, StressDistribution::web_bending_compression(0.75));
978        assert!((bc.class1 - 396.0 / 8.75).abs() < 1e-10);
979        assert!((bc.class2 - 456.0 / 8.75).abs() < 1e-10);
980        assert!((bc.class3 - 42.0 / 0.67).abs() < 1e-10);
981    }
982
983    #[test]
984    fn web_bending_compression_limits_zero_alpha() {
985        // α = 0 (pure tension) should return f64::MAX (no compression zone)
986        let eps = 1.0;
987        let bc = web_limits(eps, StressDistribution::web_bending_compression(0.0));
988        assert_eq!(bc.class1, f64::MAX);
989        assert_eq!(bc.class2, f64::MAX);
990    }
991}