1use std::fmt;
5
6#[derive(Debug, Clone, Copy, PartialEq)]
7pub struct Material {
8 pub youngs_modulus: f64,
10 pub shear_modulus: f64,
12 pub poissons_ratio: f64,
14 pub density: f64,
16 pub thermal_expansion: f64,
18 pub yield_strength: f64,
20 pub ultimate_strength: f64,
22 pub name: &'static str,
24 pub color: Option<(u8, u8, u8)>,
26}
27
28impl Material {
29 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 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 pub fn with_thermal_expansion(mut self, alpha: f64) -> Self {
71 self.thermal_expansion = alpha;
72 self
73 }
74
75 pub fn with_yield_strength(mut self, fy: f64) -> Self {
77 self.yield_strength = fy;
78 self
79 }
80
81 pub fn with_ultimate_strength(mut self, fu: f64) -> Self {
83 self.ultimate_strength = fu;
84 self
85 }
86
87 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 pub fn modular_ratio(&self, ref_material: &Material) -> f64 {
95 self.youngs_modulus / ref_material.youngs_modulus
96 }
97
98 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 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
128pub mod presets {
130 use super::Material;
131
132 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 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 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 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 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 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 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 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 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 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, ultimate_strength: 30e6,
261 name: "C25/30 Concrete",
262 color: Some((180, 170, 160)),
263 };
264
265 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 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 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, ultimate_strength: 24e6,
312 name: "GL24h Timber",
313 color: Some((200, 170, 120)),
314 };
315
316 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#[derive(Debug, Clone, PartialEq)]
335pub struct MaterialGroup {
336 pub material: Material,
337 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); }
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 #[test]
387 fn timber_gl24h_orthotropic_constants() {
388 let mat = TIMBER_GL24H;
389
390 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 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 assert!(mat.is_valid(), "TIMBER_GL24H must be valid");
407 }
408}