Skip to main content

section_properties/
material.rs

1//! Isotropic linear-elastic material definition.
2//!
3//! All values in SI base units (Pa, kg/m³, /K).
4use std::fmt;
5
6#[derive(Debug, Clone, Copy, PartialEq)]
7pub struct Material {
8    /// Young's modulus `Pa`
9    pub youngs_modulus: f64,
10    /// Shear modulus `Pa`
11    pub shear_modulus: f64,
12    /// Poisson's ratio [-]
13    pub poissons_ratio: f64,
14    /// Density [kg/m³]
15    pub density: f64,
16    /// Coefficient of thermal expansion [1/K]
17    pub thermal_expansion: f64,
18    /// Yield strength `Pa` (for plastic analysis)
19    pub yield_strength: f64,
20    /// Ultimate tensile strength `Pa`
21    pub ultimate_strength: f64,
22    /// Optional: identifier/name for display
23    pub name: &'static str,
24    /// Optional: color for visualization (RGB 0-255)
25    pub color: Option<(u8, u8, u8)>,
26}
27
28impl Material {
29    /// Create a new material from E and ν (computes G = E / (2(1+ν))).
30    pub fn new(youngs_modulus: f64, poissons_ratio: f64, density: f64, name: &'static str) -> Self {
31        let shear_modulus = youngs_modulus / (2.0 * (1.0 + poissons_ratio));
32        Self {
33            youngs_modulus,
34            shear_modulus,
35            poissons_ratio,
36            density,
37            thermal_expansion: 0.0,
38            yield_strength: 0.0,
39            ultimate_strength: 0.0,
40            name,
41            color: None,
42        }
43    }
44
45    /// Create a new material with all properties specified.
46    pub fn with_all(
47        youngs_modulus: f64,
48        shear_modulus: f64,
49        poissons_ratio: f64,
50        density: f64,
51        thermal_expansion: f64,
52        yield_strength: f64,
53        ultimate_strength: f64,
54        name: &'static str,
55    ) -> Self {
56        Self {
57            youngs_modulus,
58            shear_modulus,
59            poissons_ratio,
60            density,
61            thermal_expansion,
62            yield_strength,
63            ultimate_strength,
64            name,
65            color: None,
66        }
67    }
68
69    /// Set thermal expansion coefficient.
70    pub fn with_thermal_expansion(mut self, alpha: f64) -> Self {
71        self.thermal_expansion = alpha;
72        self
73    }
74
75    /// Set yield strength.
76    pub fn with_yield_strength(mut self, fy: f64) -> Self {
77        self.yield_strength = fy;
78        self
79    }
80
81    /// Set ultimate strength.
82    pub fn with_ultimate_strength(mut self, fu: f64) -> Self {
83        self.ultimate_strength = fu;
84        self
85    }
86
87    /// Set visualization color (RGB).
88    pub fn with_color(mut self, r: u8, g: u8, b: u8) -> Self {
89        self.color = Some((r, g, b));
90        self
91    }
92
93    /// Get modular ratio relative to another material (n = E_this / E_ref).
94    pub fn modular_ratio(&self, ref_material: &Material) -> f64 {
95        self.youngs_modulus / ref_material.youngs_modulus
96    }
97
98    /// Check if material properties are physically valid.
99    pub fn is_valid(&self) -> bool {
100        self.youngs_modulus > 0.0
101            && self.shear_modulus > 0.0
102            && self.poissons_ratio >= -1.0
103            && self.poissons_ratio < 0.5
104            && self.density >= 0.0
105    }
106}
107
108impl Default for Material {
109    /// Default to generic structural steel (S355).
110    fn default() -> Self {
111        presets::STEEL_S355
112    }
113}
114
115impl fmt::Display for Material {
116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117        write!(
118            f,
119            "{} (E={:.0} GPa, ν={:.2}, ρ={:.0} kg/m³)",
120            self.name,
121            self.youngs_modulus / 1e9,
122            self.poissons_ratio,
123            self.density
124        )
125    }
126}
127
128/// Common structural materials (SI units).
129pub mod presets {
130    use super::Material;
131
132    // Steel
133    /// Generic structural steel S235 (EN 10025)
134    pub const STEEL_S235: Material = Material {
135        youngs_modulus: 200e9,
136        shear_modulus: 76.923e9,
137        poissons_ratio: 0.3,
138        density: 7850.0,
139        thermal_expansion: 12e-6,
140        yield_strength: 235e6,
141        ultimate_strength: 360e6,
142        name: "S235",
143        color: Some((120, 120, 140)),
144    };
145
146    /// Generic structural steel S275 (EN 10025)
147    pub const STEEL_S275: Material = Material {
148        youngs_modulus: 200e9,
149        shear_modulus: 76.923e9,
150        poissons_ratio: 0.3,
151        density: 7850.0,
152        thermal_expansion: 12e-6,
153        yield_strength: 275e6,
154        ultimate_strength: 430e6,
155        name: "S275",
156        color: Some((130, 130, 150)),
157    };
158
159    /// Generic structural steel S355 (EN 10025) - most common
160    pub const STEEL_S355: Material = Material {
161        youngs_modulus: 200e9,
162        shear_modulus: 76.923e9,
163        poissons_ratio: 0.3,
164        density: 7850.0,
165        thermal_expansion: 12e-6,
166        yield_strength: 355e6,
167        ultimate_strength: 510e6,
168        name: "S355",
169        color: Some((140, 140, 160)),
170    };
171
172    /// Generic structural steel S460 (EN 10025)
173    pub const STEEL_S460: Material = Material {
174        youngs_modulus: 200e9,
175        shear_modulus: 76.923e9,
176        poissons_ratio: 0.3,
177        density: 7850.0,
178        thermal_expansion: 12e-6,
179        yield_strength: 460e6,
180        ultimate_strength: 620e6,
181        name: "S460",
182        color: Some((150, 150, 170)),
183    };
184
185    /// Stainless steel 304 (EN 1.4301)
186    pub const STAINLESS_304: Material = Material {
187        youngs_modulus: 193e9,
188        shear_modulus: 74.23e9,
189        poissons_ratio: 0.3,
190        density: 7930.0,
191        thermal_expansion: 17.2e-6,
192        yield_strength: 215e6,
193        ultimate_strength: 505e6,
194        name: "304 Stainless",
195        color: Some((180, 180, 190)),
196    };
197
198    /// Stainless steel 316 (EN 1.4401)
199    pub const STAINLESS_316: Material = Material {
200        youngs_modulus: 193e9,
201        shear_modulus: 74.23e9,
202        poissons_ratio: 0.3,
203        density: 7980.0,
204        thermal_expansion: 15.9e-6,
205        yield_strength: 205e6,
206        ultimate_strength: 515e6,
207        name: "316 Stainless",
208        color: Some((185, 185, 195)),
209    };
210
211    // Aluminum
212    /// Aluminum 6061-T6
213    pub const ALUMINUM_6061_T6: Material = Material {
214        youngs_modulus: 68.9e9,
215        shear_modulus: 26.0e9,
216        poissons_ratio: 0.33,
217        density: 2700.0,
218        thermal_expansion: 23.6e-6,
219        yield_strength: 276e6,
220        ultimate_strength: 310e6,
221        name: "6061-T6 Al",
222        color: Some((200, 200, 210)),
223    };
224
225    /// Aluminum 6063-T5
226    pub const ALUMINUM_6063_T5: Material = Material {
227        youngs_modulus: 68.3e9,
228        shear_modulus: 25.7e9,
229        poissons_ratio: 0.33,
230        density: 2700.0,
231        thermal_expansion: 23.4e-6,
232        yield_strength: 145e6,
233        ultimate_strength: 186e6,
234        name: "6063-T5 Al",
235        color: Some((205, 205, 215)),
236    };
237
238    /// Aluminum 7075-T6
239    pub const ALUMINUM_7075_T6: Material = Material {
240        youngs_modulus: 71.7e9,
241        shear_modulus: 26.9e9,
242        poissons_ratio: 0.33,
243        density: 2810.0,
244        thermal_expansion: 23.6e-6,
245        yield_strength: 503e6,
246        ultimate_strength: 572e6,
247        name: "7075-T6 Al",
248        color: Some((210, 210, 220)),
249    };
250
251    // Concrete
252    /// Concrete C25/30 (EN 1992)
253    pub const CONCRETE_C25_30: Material = Material {
254        youngs_modulus: 31e9,
255        shear_modulus: 12.9e9,
256        poissons_ratio: 0.2,
257        density: 2400.0,
258        thermal_expansion: 10e-6,
259        yield_strength: 25e6, // f_ck
260        ultimate_strength: 30e6,
261        name: "C25/30 Concrete",
262        color: Some((180, 170, 160)),
263    };
264
265    /// Concrete C30/37
266    pub const CONCRETE_C30_37: Material = Material {
267        youngs_modulus: 33e9,
268        shear_modulus: 13.75e9,
269        poissons_ratio: 0.2,
270        density: 2400.0,
271        thermal_expansion: 10e-6,
272        yield_strength: 30e6,
273        ultimate_strength: 37e6,
274        name: "C30/37 Concrete",
275        color: Some((175, 165, 155)),
276    };
277
278    /// Concrete C40/50
279    pub const CONCRETE_C40_50: Material = Material {
280        youngs_modulus: 35e9,
281        shear_modulus: 14.58e9,
282        poissons_ratio: 0.2,
283        density: 2450.0,
284        thermal_expansion: 10e-6,
285        yield_strength: 40e6,
286        ultimate_strength: 50e6,
287        name: "C40/50 Concrete",
288        color: Some((170, 160, 150)),
289    };
290
291    // Timber (GL24h)
292    /// Glued laminated timber GL24h (EN 14080).
293    ///
294    /// **G is an independently specified engineering constant, NOT computed
295    /// from the isotropic relation** `G = E/(2(1+ν))`. Timber is orthotropic:
296    /// `E` is the mean modulus parallel to grain (`E₀,mean = 11.6 GPa`),
297    /// `G` is the mean shear modulus (`G,mean ≈ 0.725 GPa`), and `ν` is the
298    /// Poisson's ratio. The isotropic formula would give `G ≈ 4.30 GPa`,
299    /// which is ~6× too high and would produce incorrect torsional stiffness.
300    ///
301    /// Source: EN 14080:2013, GL24h strength class. The shear modulus value
302    /// (0.725 GPa) is consistent with the EN 14080 range (0.65–0.72 GPa)
303    /// used in timber engineering practice.
304    pub const TIMBER_GL24H: Material = Material {
305        youngs_modulus: 11.6e9,
306        shear_modulus: 0.725e9,
307        poissons_ratio: 0.35,
308        density: 420.0,
309        thermal_expansion: 5e-6,
310        yield_strength: 24e6, // f_m,k
311        ultimate_strength: 24e6,
312        name: "GL24h Timber",
313        color: Some((200, 170, 120)),
314    };
315
316    // Titanium
317    /// Titanium Grade 5 (Ti-6Al-4V)
318    pub const TITANIUM_GR5: Material = Material {
319        youngs_modulus: 113.8e9,
320        shear_modulus: 44.0e9,
321        poissons_ratio: 0.342,
322        density: 4430.0,
323        thermal_expansion: 8.6e-6,
324        yield_strength: 880e6,
325        ultimate_strength: 950e6,
326        name: "Ti-6Al-4V",
327        color: Some((160, 160, 170)),
328    };
329}
330
331/// Material group for composite sections (transformed section method).
332///
333/// Groups elements sharing the same material reference for stiffness assembly.
334#[derive(Debug, Clone, PartialEq)]
335pub struct MaterialGroup {
336    pub material: Material,
337    /// List of polygon indices belonging to this material group
338    pub polygon_indices: Vec<usize>,
339}
340
341impl MaterialGroup {
342    pub fn new(material: Material, polygon_indices: Vec<usize>) -> Self {
343        Self {
344            material,
345            polygon_indices,
346        }
347    }
348}
349
350#[cfg(test)]
351mod tests {
352    use super::presets::*;
353    use super::*;
354
355    #[test]
356    fn steel_material_properties() {
357        let mat = STEEL_S355;
358        assert!((mat.youngs_modulus - 200e9).abs() < 1e6);
359        assert!((mat.poissons_ratio - 0.3).abs() < 1e-9);
360        assert!(mat.is_valid());
361    }
362
363    #[test]
364    fn modular_ratio() {
365        let steel = STEEL_S355;
366        let concrete = CONCRETE_C30_37;
367        let n = steel.modular_ratio(&concrete);
368        assert!((n - 200.0 / 33.0).abs() < 0.1); // ≈ 6.06
369    }
370
371    #[test]
372    fn material_builder() {
373        let mat = Material::new(210e9, 0.3, 7850.0, "Custom Steel")
374            .with_yield_strength(400e6)
375            .with_color(255, 0, 0);
376        assert_eq!(mat.yield_strength, 400e6);
377        assert_eq!(mat.color, Some((255, 0, 0)));
378    }
379
380    /// Regression test: TIMBER_GL24H stores independently specified
381    /// engineering constants (orthotropic timber), NOT isotropic G.
382    ///
383    /// Timber is orthotropic — `G` is the mean shear modulus from EN 14080,
384    /// not `E/(2(1+ν))`. This test protects the intended values against
385    /// accidental "fixes" that would replace G with the isotropic relation.
386    #[test]
387    fn timber_gl24h_orthotropic_constants() {
388        let mat = TIMBER_GL24H;
389
390        // Exact values from EN 14080 GL24h.
391        assert!((mat.youngs_modulus - 11.6e9).abs() < 1e3, "E = 11.6 GPa");
392        assert!((mat.shear_modulus - 0.725e9).abs() < 1e3, "G = 0.725 GPa");
393        assert!((mat.poissons_ratio - 0.35).abs() < 1e-9, "nu = 0.35");
394        assert!((mat.density - 420.0).abs() < 1e-6, "rho = 420 kg/m³");
395
396        // G must NOT equal the isotropic relation E/(2(1+ν)).
397        let g_isotropic = mat.youngs_modulus / (2.0 * (1.0 + mat.poissons_ratio));
398        assert!(
399            (mat.shear_modulus - g_isotropic).abs() > 1e9,
400            "TIMBER_GL24H G ({:.3} GPa) must differ from isotropic G ({:.3} GPa)",
401            mat.shear_modulus / 1e9,
402            g_isotropic / 1e9
403        );
404
405        // Material must pass validation.
406        assert!(mat.is_valid(), "TIMBER_GL24H must be valid");
407    }
408}