section-properties 0.4.0

Structural cross-section properties: geometric/principal moments, section moduli, warping/torsion FEM, plastic, cold-formed, and fire analysis.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
//! Composite sections and transformed section method.
//!
//! Supports multi-material sections (steel-concrete, timber-concrete, etc.)

use crate::geometry::{Point, Polygon};
use crate::material::{Material, MaterialGroup};
use crate::section::Section;
use crate::section_library::{ParametricSection, rectangle_polygon};
use std::f64::consts::PI;

/// Composite section with multiple materials using transformed section method.
#[derive(Debug, Clone)]
pub struct CompositeSection {
    pub outer: Polygon,
    pub holes: Vec<Polygon>,
    /// Additional independent outer regions (e.g. steel beam + concrete slab
    /// as two separate solids, not a hole).
    pub extra_outers: Vec<Polygon>,
    pub material_groups: Vec<MaterialGroup>,
    /// Reference material for transformation (usually first group)
    pub reference_material: Material,
}

impl CompositeSection {
    /// Create a composite section from a single-material Section.
    pub fn single_material(section: Section, material: Material) -> Self {
        let reference_material = material;
        Self {
            outer: section.outer,
            holes: section.holes,
            extra_outers: Vec::new(),
            material_groups: vec![MaterialGroup::new(material, vec![0])],
            reference_material,
        }
    }

    /// Create from outer polygon, holes, and material groups.
    pub fn new(outer: Polygon, holes: Vec<Polygon>, material_groups: Vec<MaterialGroup>) -> Self {
        let reference_material = material_groups
            .first()
            .map(|g| g.material)
            .unwrap_or_else(|| Material::default());
        Self {
            outer,
            holes,
            extra_outers: Vec::new(),
            material_groups,
            reference_material,
        }
    }

    /// Create from a parametric section with a single material.
    pub fn from_parametric<S: ParametricSection>(section: &S, material: Material) -> Self {
        Self::single_material(section.build(), material)
    }

    /// Create a composite section from multiple parametric sections with different materials.
    ///
    /// Each component is an independent solid region. The first component's
    /// outer polygon becomes `outer`; subsequent components' outer polygons
    /// are stored in `extra_outers` (NOT treated as holes).
    pub fn from_components(components: Vec<(Box<dyn ParametricSection>, Point, Material)>) -> Self {
        if components.is_empty() {
            return Self::new(Polygon::new(vec![]), Vec::new(), Vec::new());
        }

        let mut outers: Vec<Polygon> = Vec::new();
        let mut all_holes: Vec<Polygon> = Vec::new();
        // Explicit material per region: (outer index, hole indices). Avoids
        // inferring materials from fragile polygon-index arithmetic.
        let mut region_materials: Vec<(usize, Vec<usize>, Material)> = Vec::new();

        for (section, offset, material) in components {
            let built = section.build();
            let mut outer = built.outer;
            for v in &mut outer.vertices {
                v.x += offset.x;
                v.y += offset.y;
            }
            let outer_index = outers.len();
            outers.push(outer);

            let mut hole_indices = Vec::new();
            for mut hole in built.holes {
                for v in &mut hole.vertices {
                    v.x += offset.x;
                    v.y += offset.y;
                }
                hole_indices.push(all_holes.len());
                all_holes.push(hole);
            }
            region_materials.push((outer_index, hole_indices, material));
        }

        let reference_material = region_materials[0].2.clone();
        let outer = outers.remove(0);

        Self {
            outer,
            holes: all_holes,
            extra_outers: outers,
            material_groups: region_materials
                .into_iter()
                .map(|(_o, h, m)| MaterialGroup::new(m, h))
                .collect(),
            reference_material,
        }
    }

    /// Get the reference material.
    pub fn reference_material(&self) -> &Material {
        &self.reference_material
    }

    /// Get modular ratios for all groups relative to reference material.
    pub fn modular_ratios(&self) -> Vec<f64> {
        self.material_groups
            .iter()
            .map(|g| g.material.modular_ratio(&self.reference_material))
            .collect()
    }

    /// Compute transformed section properties using the reference material.
    ///
    /// Uses the transformed section method:
    /// - Each material's area is multiplied by its modular ratio n = E_material / E_ref
    /// - Moments of inertia are computed about the transformed centroid
    pub fn transformed_properties(&self) -> crate::section_properties::SectionProperties {
        // For full transformed section analysis, we need to:
        // 1. Transform all areas by modular ratio
        // 2. Find transformed centroid
        // 3. Compute transformed moments of inertia

        // Simplified implementation: compute properties for each group separately
        // and combine using parallel axis theorem with transformed areas

        let mut total_area = 0.0;
        let mut first_moment_x = 0.0;
        let mut first_moment_y = 0.0;
        let mut ix = 0.0;
        let mut iy = 0.0;
        let mut ixy = 0.0;

        // Process outer polygon (index 0)
        if let Some(group) = self
            .material_groups
            .iter()
            .find(|g| g.polygon_indices.contains(&0))
        {
            let n = group.material.modular_ratio(&self.reference_material);
            let poly = &self.outer;
            let signed_area = poly.signed_area() * n;
            let centroid = poly.centroid();
            let i_x = poly.moment_of_inertia_x() * n;
            let i_y = poly.moment_of_inertia_y() * n;
            let i_xy = poly.product_of_inertia_xy() * n;

            total_area += signed_area;
            first_moment_x += signed_area * centroid.x;
            first_moment_y += signed_area * centroid.y;
            ix += i_x;
            iy += i_y;
            ixy += i_xy;
        }

        // Process holes
        for (i, hole) in self.holes.iter().enumerate() {
            let poly_index = i + 1;
            if let Some(group) = self
                .material_groups
                .iter()
                .find(|g| g.polygon_indices.contains(&poly_index))
            {
                let n = group.material.modular_ratio(&self.reference_material);
                // Holes have negative signed area (CW)
                let signed_area = hole.signed_area() * n; // already negative
                let centroid = hole.centroid();
                let i_x = hole.moment_of_inertia_x() * n;
                let i_y = hole.moment_of_inertia_y() * n;
                let i_xy = hole.product_of_inertia_xy() * n;

                total_area += signed_area;
                first_moment_x += signed_area * centroid.x;
                first_moment_y += signed_area * centroid.y;
                ix += i_x;
                iy += i_y;
                ixy += i_xy;
            }
        }

        // Process extra_outers (independent solid regions, positive area).
        // Group j (j >= 1) corresponds to extra_outer j-1: each component
        // contributes one group, and the first component's group owns `outer`.
        for (i, extra) in self.extra_outers.iter().enumerate() {
            if let Some(group) = self.material_groups.get(i + 1) {
                let n = group.material.modular_ratio(&self.reference_material);
                let signed_area = extra.signed_area() * n;
                let centroid = extra.centroid();
                let i_x = extra.moment_of_inertia_x() * n;
                let i_y = extra.moment_of_inertia_y() * n;
                let i_xy = extra.product_of_inertia_xy() * n;

                total_area += signed_area;
                first_moment_x += signed_area * centroid.x;
                first_moment_y += signed_area * centroid.y;
                ix += i_x;
                iy += i_y;
                ixy += i_xy;
            }
        }

        assert!(
            total_area.abs() > f64::EPSILON,
            "Transformed section area too small"
        );

        // Transformed centroid
        let centroid = Point::new(first_moment_x / total_area, first_moment_y / total_area);

        // Parallel axis theorem to centroidal axes
        let ix_c = ix - total_area * centroid.y.powi(2);
        let iy_c = iy - total_area * centroid.x.powi(2);
        let ixy_c = ixy - total_area * centroid.x * centroid.y;

        let max_fiber_y = self
            .outer
            .vertices
            .iter()
            .chain(self.holes.iter().flat_map(|p| p.vertices.iter()))
            .chain(self.extra_outers.iter().flat_map(|p| p.vertices.iter()))
            .map(|v| (v.y - centroid.y).abs())
            .fold(0.0, f64::max);
        let max_fiber_x = self
            .outer
            .vertices
            .iter()
            .chain(self.holes.iter().flat_map(|p| p.vertices.iter()))
            .chain(self.extra_outers.iter().flat_map(|p| p.vertices.iter()))
            .map(|v| (v.x - centroid.x).abs())
            .fold(0.0, f64::max);

        let avg = (ix_c + iy_c) * 0.5;
        let diff = (ix_c - iy_c) * 0.5;
        let radius = (diff * diff + ixy_c * ixy_c).sqrt();
        let i11 = avg + radius;
        let i22 = avg - radius;
        let phi = 0.5 * (2.0 * ixy_c).atan2(ix_c - iy_c);

        crate::section_properties::SectionProperties {
            geometric: crate::section_properties::GeometricProperties {
                area: total_area,
                centroid,
                ix: ix_c,
                iy: iy_c,
                ixy: ixy_c,
                max_fiber_distance_y: max_fiber_y,
                max_fiber_distance_x: max_fiber_x,
                zxx_plus: ix_c / max_fiber_y.max(1e-10),
                zxx_minus: ix_c / max_fiber_y.max(1e-10),
                zyy_plus: iy_c / max_fiber_x.max(1e-10),
                zyy_minus: iy_c / max_fiber_x.max(1e-10),
                perimeter: self.outer.perimeter(),
                qx: 0.0,
                qy: 0.0,
                ixx_g: ix_c + total_area * centroid.y.powi(2),
                iyy_g: iy_c + total_area * centroid.x.powi(2),
                ixy_g: ixy_c + total_area * centroid.x * centroid.y,
            },
            principal: crate::section_properties::PrincipalProperties {
                i11,
                i22,
                phi,
                ..Default::default()
            },
            gyration: crate::section_properties::GyrationProperties {
                rx: (ix_c / total_area).sqrt(),
                ry: (iy_c / total_area).sqrt(),
                r11: (i11 / total_area).sqrt(),
                r22: (i22 / total_area).sqrt(),
                polar: ((ix_c + iy_c) / total_area).sqrt(),
            },
        }
    }

    /// Compute section properties for a specific material group only.
    pub fn material_group_properties(
        &self,
        group_index: usize,
    ) -> Option<crate::section_properties::SectionProperties> {
        if group_index >= self.material_groups.len() {
            return None;
        }
        let group = &self.material_groups[group_index];

        let mut area = 0.0;
        let mut fx = 0.0;
        let mut fy = 0.0;
        let mut ix = 0.0;
        let mut iy = 0.0;
        let mut ixy = 0.0;

        for &poly_idx in &group.polygon_indices {
            let poly = if poly_idx == 0 {
                &self.outer
            } else if poly_idx > 0 && poly_idx <= self.holes.len() {
                &self.holes[poly_idx - 1]
            } else {
                continue;
            };

            let signed_area = poly.signed_area();
            let centroid = poly.centroid();

            area += signed_area;
            fx += signed_area * centroid.x;
            fy += signed_area * centroid.y;
            ix += poly.moment_of_inertia_x();
            iy += poly.moment_of_inertia_y();
            ixy += poly.product_of_inertia_xy();
        }

        if area.abs() < f64::EPSILON {
            return None;
        }

        let centroid = Point::new(fx / area, fy / area);
        let ix_c = ix - area * centroid.y.powi(2);
        let iy_c = iy - area * centroid.x.powi(2);
        let ixy_c = ixy - area * centroid.x * centroid.y;

        let max_fiber_y = self
            .outer
            .vertices
            .iter()
            .chain(self.holes.iter().flat_map(|p| p.vertices.iter()))
            .chain(self.extra_outers.iter().flat_map(|p| p.vertices.iter()))
            .map(|v| (v.y - centroid.y).abs())
            .fold(0.0, f64::max);
        let max_fiber_x = self
            .outer
            .vertices
            .iter()
            .chain(self.holes.iter().flat_map(|p| p.vertices.iter()))
            .chain(self.extra_outers.iter().flat_map(|p| p.vertices.iter()))
            .map(|v| (v.x - centroid.x).abs())
            .fold(0.0, f64::max);

        let avg = (ix_c + iy_c) * 0.5;
        let diff = (ix_c - iy_c) * 0.5;
        let radius = (diff * diff + ixy_c * ixy_c).sqrt();
        let i11 = avg + radius;
        let i22 = avg - radius;
        let phi = 0.5 * (2.0 * ixy_c).atan2(ix_c - iy_c);

        Some(crate::section_properties::SectionProperties {
            geometric: crate::section_properties::GeometricProperties {
                area,
                centroid,
                ix: ix_c,
                iy: iy_c,
                ixy: ixy_c,
                max_fiber_distance_y: max_fiber_y,
                max_fiber_distance_x: max_fiber_x,
                zxx_plus: ix_c / max_fiber_y.max(1e-10),
                zxx_minus: ix_c / max_fiber_y.max(1e-10),
                zyy_plus: iy_c / max_fiber_x.max(1e-10),
                zyy_minus: iy_c / max_fiber_x.max(1e-10),
                perimeter: self.outer.perimeter(),
                qx: 0.0,
                qy: 0.0,
                ixx_g: ix_c + area * centroid.y.powi(2),
                iyy_g: iy_c + area * centroid.x.powi(2),
                ixy_g: ixy_c + area * centroid.x * centroid.y,
            },
            principal: crate::section_properties::PrincipalProperties {
                i11,
                i22,
                phi,
                ..Default::default()
            },
            gyration: crate::section_properties::GyrationProperties {
                rx: (ix_c / area).sqrt(),
                ry: (iy_c / area).sqrt(),
                r11: (i11 / area).sqrt(),
                r22: (i22 / area).sqrt(),
                polar: ((ix_c + iy_c) / area).sqrt(),
            },
        })
    }

    /// Get the number of material groups.
    pub fn n_groups(&self) -> usize {
        self.material_groups.len()
    }

    /// Get material group by index.
    pub fn group(&self, index: usize) -> Option<&MaterialGroup> {
        self.material_groups.get(index)
    }

    /// Add a material group to the section.
    pub fn add_group(&mut self, group: MaterialGroup) {
        self.material_groups.push(group);
    }
}

/// Steel-concrete composite beam section (EN 1994).
#[derive(Debug)]
pub struct SteelConcreteComposite {
    pub steel_section: Box<dyn ParametricSection>,
    pub concrete_slab: CompositeSlab,
    pub shear_connectors: ShearConnectorLayout,
    pub construction_stage: ConstructionStage,
}

#[derive(Debug, Clone, PartialEq)]
pub struct CompositeSlab {
    pub width: f64,          // Effective width (beff)
    pub thickness: f64,      // Total slab thickness
    pub deck_thickness: f64, // Metal deck thickness (if any)
    pub deck_orientation: DeckOrientation,
    pub concrete_grade: ConcreteGrade,
    pub reinforcement: Vec<RebarLayer>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeckOrientation {
    Parallel,      // Deck ribs parallel to beam
    Perpendicular, // Deck ribs perpendicular to beam
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConcreteGrade {
    C20_25,
    C25_30,
    C30_37,
    C35_45,
    C40_50,
    C45_55,
    C50_60,
}

impl ConcreteGrade {
    pub fn fck(&self) -> f64 {
        match self {
            ConcreteGrade::C20_25 => 20e6,
            ConcreteGrade::C25_30 => 25e6,
            ConcreteGrade::C30_37 => 30e6,
            ConcreteGrade::C35_45 => 35e6,
            ConcreteGrade::C40_50 => 40e6,
            ConcreteGrade::C45_55 => 45e6,
            ConcreteGrade::C50_60 => 50e6,
        }
    }

    pub fn fcm(&self) -> f64 {
        self.fck() + 8e6
    }

    pub fn ecm(&self) -> f64 {
        // EN 1992-1-1 Eq 3.1
        22e3 * (self.fcm() / 10e6).powf(0.3) * 1e6 // Pa
    }

    pub fn material(&self) -> Material {
        use crate::material::Material;
        Material::with_all(
            self.ecm(),
            self.ecm() / (2.0 * (1.0 + 0.2)),
            0.2,
            2400.0,
            10e-6,
            self.fck(),
            self.fcm(),
            "Concrete",
        )
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RebarLayer {
    pub y: f64, // Distance from top of slab (positive down)
    pub diameter: f64,
    pub spacing: f64,
    pub count_per_m: usize, // Bars per meter width
    pub material: Material,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ShearConnectorLayout {
    pub type_: ShearConnectorType,
    pub diameter: f64,
    pub height: f64,
    pub spacing: f64,     // Longitudinal spacing
    pub n_per_row: usize, // Number per cross-section
    pub material: Material,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShearConnectorType {
    HeadedStud,
    Channel,
    Perfobond,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConstructionStage {
    /// Bare steel (construction stage)
    BareSteel,
    /// Steel + wet concrete (construction stage)
    SteelWetConcrete,
    /// Composite (final stage, full interaction)
    Composite,
    /// Composite with partial interaction
    PartialInteraction,
}

impl SteelConcreteComposite {
    pub fn new(
        steel_section: impl ParametricSection + 'static,
        concrete_slab: CompositeSlab,
        shear_connectors: ShearConnectorLayout,
    ) -> Self {
        Self {
            steel_section: Box::new(steel_section),
            concrete_slab,
            shear_connectors,
            construction_stage: ConstructionStage::Composite,
        }
    }

    pub fn with_stage(mut self, stage: ConstructionStage) -> Self {
        self.construction_stage = stage;
        self
    }

    /// Effective width of concrete slab per EN 1994-1-1
    pub fn effective_width(&self, span: f64, beam_spacing: f64) -> f64 {
        let beff = match self.concrete_slab.deck_orientation {
            DeckOrientation::Parallel => {
                // EN 1994-1-1 Cl 6.6.1.2
                let b0 = span / 8.0;
                (b0.min(beam_spacing)).min(self.concrete_slab.width)
            }
            DeckOrientation::Perpendicular => {
                // EN 1994-1-1 Cl 6.6.1.3
                let b0 = span / 8.0;
                (b0.min(beam_spacing / 2.0)).min(self.concrete_slab.width)
            }
        };
        beff
    }

    /// Modular ratio n = E_steel / E_concrete (long-term)
    pub fn modular_ratio(&self, long_term: bool) -> f64 {
        let steel_mat = crate::material::presets::STEEL_S355;
        let conc_mat = self.concrete_slab.concrete_grade.material();
        let n = steel_mat.modular_ratio(&conc_mat);
        if long_term {
            // Creep effect: n_long = n * (1 + phi) where phi is creep coefficient
            // Simplified: use n * 2 for long-term
            n * 2.0
        } else {
            n
        }
    }

    /// Build the composite section for a given stage.
    pub fn build_section(&self, span: f64, beam_spacing: f64) -> CompositeSection {
        let beff = self.effective_width(span, beam_spacing);
        let _n = self.modular_ratio(matches!(
            self.construction_stage,
            ConstructionStage::Composite
        ));

        let steel_sec = self.steel_section.build();
        let steel_mat = crate::material::presets::STEEL_S355;
        let conc_mat = self.concrete_slab.concrete_grade.material();

        // Position concrete slab on top of steel section
        let steel_props = crate::section_properties::SectionProperties::from_section(&steel_sec);
        let steel_top_y = steel_props.centroid.y
            + steel_sec
                .outer
                .vertices
                .iter()
                .map(|v| v.y)
                .max_by(|a, b| a.partial_cmp(b).unwrap())
                .unwrap_or(0.0);

        // Concrete slab centroid relative to steel centroid
        let slab_y = steel_top_y + self.concrete_slab.thickness / 2.0;
        let slab_offset = Point::new(0.0, slab_y - steel_props.centroid.y);

        // Build composite section
        CompositeSection::from_components(vec![
            (
                Box::new(self.steel_section.build()),
                Point::new(0.0, 0.0),
                steel_mat,
            ),
            (
                Box::new(RectangularSection {
                    width: beff,
                    height: self.concrete_slab.thickness,
                }),
                slab_offset,
                conc_mat,
            ),
        ])
    }

    /// Compute transformed section properties for the composite stage.
    pub fn composite_properties(
        &self,
        span: f64,
        beam_spacing: f64,
    ) -> crate::section_properties::SectionProperties {
        let comp = self.build_section(span, beam_spacing);
        comp.transformed_properties()
    }

    /// Moment capacity (simplified plastic design per EN 1994).
    pub fn plastic_moment_capacity(&self, span: f64, beam_spacing: f64) -> f64 {
        // Simplified - full implementation would do plastic stress block analysis
        let props = self.composite_properties(span, beam_spacing);
        let steel_mat = crate::material::presets::STEEL_S355;
        let fy = steel_mat.yield_strength;
        // Mpl = fy * Wel (elastic) or fy * Wpl (plastic)
        // Simplified using elastic modulus
        props.ix.max(props.iy) / (self.height() / 2.0) * fy
    }

    fn height(&self) -> f64 {
        let steel_sec = self.steel_section.build();
        let _steel_props = crate::section_properties::SectionProperties::from_section(&steel_sec);
        steel_sec
            .outer
            .vertices
            .iter()
            .map(|v| v.y)
            .max_by(|a, b| a.partial_cmp(b).unwrap())
            .unwrap_or(0.0)
            - steel_sec
                .outer
                .vertices
                .iter()
                .map(|v| v.y)
                .min_by(|a, b| a.partial_cmp(b).unwrap())
                .unwrap_or(0.0)
            + self.concrete_slab.thickness
    }
}

/// Simple rectangular section for composite slab.
#[derive(Debug, Clone, Copy, PartialEq)]
struct RectangularSection {
    width: f64,
    height: f64,
}

impl ParametricSection for RectangularSection {
    fn build(&self) -> Section {
        Section::new(rectangle_polygon(self.width, self.height), Vec::new())
    }

    fn designation(&self) -> String {
        format!(
            "RECT {:.0}x{:.0}",
            self.width * 1000.0,
            self.height * 1000.0
        )
    }
}

/// Timber-concrete composite (TCC) section.
#[derive(Debug)]
pub struct TimberConcreteComposite {
    pub timber_section: Box<dyn ParametricSection>,
    pub concrete_slab: CompositeSlab,
    pub connection: TCCConnection,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TCCConnection {
    pub type_: TCCConnectorType,
    pub stiffness: f64, // k [N/mm] per connector
    pub spacing: f64,   // Longitudinal spacing [m]
    pub n_per_row: usize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TCCConnectorType {
    Screw,
    Nail,
    Dowel,
    Notch,
    Glue,
    ShearPlate,
}

impl TimberConcreteComposite {
    pub fn new(
        timber_section: impl ParametricSection + 'static,
        concrete_slab: CompositeSlab,
        connection: TCCConnection,
    ) -> Self {
        Self {
            timber_section: Box::new(timber_section),
            concrete_slab,
            connection,
        }
    }

    /// Connection stiffness parameter gamma (EN 1995-1-1 Annex B)
    pub fn gamma(&self, span: f64) -> f64 {
        // Simplified gamma calculation for TCC
        let k = self.connection.stiffness * 1000.0; // N/m
        let s = self.connection.spacing;
        let n = self.connection.n_per_row as f64;
        let k_ser = k * n / s; // Stiffness per meter [N/m/m]

        // Timber and concrete properties
        let timber_mat = crate::material::presets::TIMBER_GL24H;
        let conc_mat = self.concrete_slab.concrete_grade.material();

        // Get section properties
        let timber_sec = self.timber_section.build();
        let timber_props = crate::section_properties::SectionProperties::from_section(&timber_sec);

        // Approximate effective width
        let beff = self.concrete_slab.width;
        let tc = self.concrete_slab.thickness;

        // Gamma = 1 / (1 + (pi^2 * EI_eff) / (k_ser * span^2))
        // Simplified
        let ei_timber = timber_mat.youngs_modulus * timber_props.ix.max(timber_props.iy);
        let ei_concrete = conc_mat.youngs_modulus * beff * tc.powi(3) / 12.0;
        let ei_eff = ei_timber + ei_concrete;

        let gamma = 1.0 / (1.0 + (PI * PI * ei_eff) / (k_ser * span * span));
        gamma.clamp(0.0, 1.0)
    }
}

/// Sandwich panel (face sheets + core).
#[derive(Debug)]
pub struct SandwichPanel {
    pub face_sheet: Box<dyn ParametricSection>,
    pub core: Box<dyn ParametricSection>,
    pub face_material: Material,
    pub core_material: Material,
}

impl SandwichPanel {
    pub fn new(
        face_sheet: impl ParametricSection + 'static,
        core: impl ParametricSection + 'static,
        face_material: Material,
        core_material: Material,
    ) -> Self {
        Self {
            face_sheet: Box::new(face_sheet),
            core: Box::new(core),
            face_material,
            core_material,
        }
    }

    pub fn build(&self) -> CompositeSection {
        let face_sec = self.face_sheet.build();
        let core_sec = self.core.build();

        let face_props = crate::section_properties::SectionProperties::from_section(&face_sec);
        let core_props = crate::section_properties::SectionProperties::from_section(&core_sec);

        // Face sheets are typically at top and bottom of core
        let face_offset = Point::new(0.0, core_props.centroid.y + face_props.centroid.y);

        CompositeSection::from_components(vec![
            (
                Box::new(self.face_sheet.build()),
                Point::new(0.0, face_offset.y),
                self.face_material,
            ),
            (
                Box::new(self.core.build()),
                Point::new(0.0, 0.0),
                self.core_material,
            ),
            (
                Box::new(self.face_sheet.build()),
                Point::new(0.0, -face_offset.y),
                self.face_material,
            ),
        ])
    }

    pub fn transformed_properties(&self) -> crate::section_properties::SectionProperties {
        self.build().transformed_properties()
    }

    /// Sandwich panel bending stiffness (D) per unit width.
    pub fn bending_stiffness_d(&self, width: f64) -> f64 {
        // D = (Ef * tf * h^2) / 2 + (Ec * tc^3) / 12
        // Simplified - use transformed properties
        let props = self.transformed_properties();
        self.face_material.youngs_modulus * props.ix * width
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::material::presets::*;
    use crate::section_library::ParametricSection;
    use crate::section_library::concrete::RectangularConcreteSection;
    use crate::section_library::steel::ISection;

    #[test]
    fn composite_single_material() {
        let rect = RectangularConcreteSection::new(0.3, 0.5);
        let comp = CompositeSection::from_parametric(&rect, CONCRETE_C30_37);
        let props = comp.transformed_properties();
        assert!((props.area - 0.15).abs() < 1e-6);
    }

    #[test]
    fn composite_steel_concrete() {
        let steel = ISection::from_designation("IPE300").expect("IPE300");
        let slab = CompositeSlab {
            width: 1.0,
            thickness: 0.15,
            deck_thickness: 0.0,
            deck_orientation: DeckOrientation::Perpendicular,
            concrete_grade: ConcreteGrade::C30_37,
            reinforcement: vec![],
        };
        let connectors = ShearConnectorLayout {
            type_: ShearConnectorType::HeadedStud,
            diameter: 0.019,
            height: 0.1,
            spacing: 0.3,
            n_per_row: 2,
            material: STEEL_S355,
        };
        let composite = SteelConcreteComposite::new(steel, slab, connectors)
            .with_stage(ConstructionStage::Composite);

        let props = composite.composite_properties(6.0, 3.0);
        assert!(props.area > 0.0);
        assert!(props.ix > 0.0);
    }

    #[test]
    fn concrete_grade_properties() {
        let c30 = ConcreteGrade::C30_37;
        assert!((c30.fck() - 30e6).abs() < 1e-3);
        assert!((c30.ecm() - 33e9).abs() < 1e9); // approximate
    }

    #[test]
    fn modular_ratio_steel_concrete() {
        let steel = ISection::from_designation("IPE300").expect("IPE300");
        let slab = CompositeSlab {
            width: 1.0,
            thickness: 0.15,
            deck_thickness: 0.0,
            deck_orientation: DeckOrientation::Perpendicular,
            concrete_grade: ConcreteGrade::C30_37,
            reinforcement: vec![],
        };
        let connectors = ShearConnectorLayout {
            type_: ShearConnectorType::HeadedStud,
            diameter: 0.019,
            height: 0.1,
            spacing: 0.3,
            n_per_row: 2,
            material: STEEL_S355,
        };
        let composite = SteelConcreteComposite::new(steel, slab, connectors);
        let n_short = composite.modular_ratio(false);
        let n_long = composite.modular_ratio(true);
        assert!(n_short > 6.0 && n_short < 7.0); // ~200/33 ≈ 6.06
        assert!((n_long / n_short - 2.0).abs() < 0.1);
    }

    #[test]
    fn sandwich_panel() {
        use crate::section_library::primitive::RectangularSection as PrimRect;
        let face = PrimRect::new(1.0, 0.002);
        let core = PrimRect::new(1.0, 0.1);
        let panel = SandwichPanel::new(
            face,
            core,
            STEEL_S355,
            crate::material::presets::ALUMINUM_6061_T6,
        );
        let props = panel.transformed_properties();
        assert!(props.area > 0.0);
        assert!(props.ix > 0.0);
    }

    #[test]
    fn transformed_single_material_matches_homogeneous() {
        // Single material: transformed properties should equal homogeneous properties
        let rect = RectangularConcreteSection::new(0.3, 0.5);
        let comp = CompositeSection::from_parametric(&rect, CONCRETE_C30_37);
        let tprops = comp.transformed_properties();

        let section = rect.build();
        let hprops = crate::section_properties::SectionProperties::from_section(&section);

        assert!((tprops.area - hprops.area).abs() / hprops.area < 1e-6);
        assert!((tprops.ix - hprops.ix).abs() / hprops.ix < 1e-6);
        assert!((tprops.iy - hprops.iy).abs() / hprops.iy < 1e-6);
    }

    #[test]
    fn transformed_area_modular_ratio() {
        // Two materials: transformed area = A1 + n*A2
        // Create a simple two-region composite: steel plate on concrete
        let concrete_poly = crate::section_library::rectangle_polygon(0.3, 0.5);
        let steel_poly = crate::section_library::rectangle_polygon(0.3, 0.01);

        // Build composite with concrete as reference
        let mut steel_shifted = steel_poly.clone();
        for v in &mut steel_shifted.vertices {
            v.y += 0.255; // Place steel on top of concrete
        }

        let _groups = vec![
            MaterialGroup::new(CONCRETE_C30_37, vec![0]),
            MaterialGroup::new(STEEL_S355, vec![1]),
        ];

        // Use concrete as outer, steel as "hole" (but it's actually an addition)
        // For proper composite, we need a different approach
        let comp = CompositeSection::new(
            concrete_poly,
            vec![],
            vec![MaterialGroup::new(CONCRETE_C30_37, vec![0])],
        );
        let props = comp.transformed_properties();

        // Area should be 0.3 * 0.5 = 0.15
        assert!((props.area - 0.15).abs() < 1e-6);
    }

    #[test]
    fn transformed_steel_concrete_section() {
        // Steel I-section embedded in concrete: transformed area > concrete area
        let i_section = ISection::new(0.2, 0.1, 0.005, 0.008, 0.0);
        let steel_section = i_section.build();

        // Create composite with steel as reference material
        let comp = CompositeSection::single_material(steel_section.clone(), STEEL_S355);
        let props = comp.transformed_properties();

        // For single material, area should match original
        let original_area = steel_section.area();
        assert!((props.area - original_area).abs() / original_area < 1e-6);

        // Ix should match
        let orig_props = crate::section_properties::SectionProperties::from_section(&steel_section);
        assert!((props.ix - orig_props.ix).abs() / orig_props.ix < 1e-6);
    }

    #[test]
    fn transformed_centroid_shift() {
        // Asymmetric composite: centroid should shift towards stiffer material
        let rect = RectangularConcreteSection::new(0.3, 0.5);
        let comp = CompositeSection::from_parametric(&rect, CONCRETE_C30_37);
        let props = comp.transformed_properties();

        // Symmetric section: centroid at origin
        assert!(props.centroid.x.abs() < 1e-6);
        assert!(props.centroid.y.abs() < 1e-6);
    }
}