oxiphysics-materials 0.1.1

Material properties and material library for the OxiPhysics engine
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
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Metallic alloy material models: composition, mechanical properties,
//! strengthening mechanisms, phase diagrams, thermal and corrosion properties.

#![allow(dead_code)]
#![allow(clippy::too_many_arguments)]

// ─── AlloyComposition ────────────────────────────────────────────────────────

/// Chemical composition of a metallic alloy.
#[derive(Debug, Clone)]
pub struct AlloyComposition {
    /// List of (element symbol, weight fraction) pairs.
    pub elements: Vec<(String, f64)>,
    /// Bulk density in kg/m³.
    pub density: f64,
    /// (solidus, liquidus) melting range in K.
    pub melting_range: (f64, f64),
}

impl AlloyComposition {
    /// Construct a new `AlloyComposition`.
    pub fn new(elements: Vec<(String, f64)>, density: f64, melting_range: (f64, f64)) -> Self {
        Self {
            elements,
            density,
            melting_range,
        }
    }

    /// Return `true` if all weight fractions sum to approximately 1.
    pub fn validate(&self) -> bool {
        let sum: f64 = self.elements.iter().map(|(_, f)| f).sum();
        (sum - 1.0).abs() < 1e-6
    }

    /// Compute mixture density using the rule of mixtures (weight-fraction average
    /// of inverse densities — Reuss bound).
    ///
    /// `densities` is a list of `(symbol, density_kg_m3)` pairs.
    pub fn density_mixture(&self, densities: &[(String, f64)]) -> f64 {
        let mut inv_sum = 0.0;
        let mut frac_sum = 0.0;
        for (sym, frac) in &self.elements {
            if let Some((_, d)) = densities.iter().find(|(s, _)| s == sym)
                && *d > 0.0
            {
                inv_sum += frac / d;
                frac_sum += frac;
            }
        }
        if inv_sum < 1e-30 || frac_sum < 1e-12 {
            self.density
        } else {
            frac_sum / inv_sum
        }
    }
}

// ─── AlloySeries ─────────────────────────────────────────────────────────────

/// Classification of common alloy families.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AlloySeries {
    /// Aluminium 1xxx series (≥99% Al).
    Series1xxx,
    /// Aluminium 2xxx series (Cu-based).
    Series2xxx,
    /// Aluminium 3xxx series (Mn-based).
    Series3xxx,
    /// Aluminium 4xxx series (Si-based).
    Series4xxx,
    /// Aluminium 5xxx series (Mg-based).
    Series5xxx,
    /// Aluminium 6xxx series (Mg+Si).
    Series6xxx,
    /// Aluminium 7xxx series (Zn-based).
    Series7xxx,
    /// Aluminium 8xxx series (other elements).
    Series8xxx,
    /// Aluminium 9xxx series (reserved).
    Series9xxx,
    /// Austenitic stainless steel 304.
    StainlessSteel304,
    /// Austenitic stainless steel 316.
    StainlessSteel316,
    /// Nickel superalloy Inconel 718.
    Inconel718,
    /// Titanium alloy Ti-6Al-4V.
    TitaniumTi6Al4V,
    /// Alloy steel AISI 4140.
    Tool4140,
    /// Maraging steel 300 grade.
    Maraging300,
    /// Nickel-based alloy Hastelloy C-276.
    Hastelloy,
}

// ─── AlloyMechanicalProps ─────────────────────────────────────────────────────

/// Mechanical properties of a metallic alloy.
#[derive(Debug, Clone)]
pub struct AlloyMechanicalProps {
    /// 0.2% proof stress / yield strength in MPa.
    pub yield_strength: f64,
    /// Ultimate tensile strength in MPa.
    pub uts: f64,
    /// Elongation at fracture in % (gauge length = 50 mm).
    pub elongation: f64,
    /// Vickers hardness HV.
    pub hardness_hv: f64,
    /// Young's modulus in GPa.
    pub youngs_modulus: f64,
    /// Poisson's ratio (dimensionless).
    pub poisson_ratio: f64,
    /// Plane-strain fracture toughness K_IC in MPa·√m.
    pub fracture_toughness: f64,
}

impl AlloyMechanicalProps {
    /// Construct `AlloyMechanicalProps` from individual values.
    pub fn new(
        yield_strength: f64,
        uts: f64,
        elongation: f64,
        hardness_hv: f64,
        youngs_modulus: f64,
        poisson_ratio: f64,
        fracture_toughness: f64,
    ) -> Self {
        Self {
            yield_strength,
            uts,
            elongation,
            hardness_hv,
            youngs_modulus,
            poisson_ratio,
            fracture_toughness,
        }
    }

    /// Compute safety factor = yield_strength / applied_stress.
    pub fn safety_factor(&self, applied_stress: f64) -> f64 {
        if applied_stress.abs() < 1e-15 {
            f64::INFINITY
        } else {
            self.yield_strength / applied_stress
        }
    }

    /// Return `true` if the alloy is considered brittle (elongation < 5 %).
    pub fn is_brittle(&self) -> bool {
        self.elongation < 5.0
    }
}

// ─── Hall-Petch equation ──────────────────────────────────────────────────────

/// Hall-Petch grain-boundary strengthening model.
pub struct HallPetch;

impl HallPetch {
    /// Compute yield strength:  σ_y = σ_0 + k / √(grain_size).
    ///
    /// `grain_size` is in metres; `k` is the Hall-Petch slope in MPa·√m.
    pub fn yield_strength(sigma_0: f64, k: f64, grain_size: f64) -> f64 {
        sigma_0 + k / grain_size.max(1e-30).sqrt()
    }

    /// Invert Hall-Petch to find required grain size for a target yield stress.
    pub fn grain_size_from_yield(sigma_y: f64, sigma_0: f64, k: f64) -> f64 {
        let diff = sigma_y - sigma_0;
        if diff.abs() < 1e-15 {
            return f64::INFINITY;
        }
        (k / diff).powi(2)
    }
}

// ─── Strengthening mechanisms ─────────────────────────────────────────────────

/// Collection of metallic strengthening mechanism models.
pub struct Strengthening;

impl Strengthening {
    /// Solid-solution strengthening increment: Δσ_ss = k_ss · c^(2/3).
    ///
    /// `c` is solute concentration (mol fraction); `k_ss` is a material constant (MPa).
    pub fn solid_solution_strengthening(c: f64, k_ss: f64) -> f64 {
        k_ss * c.max(0.0).powf(2.0 / 3.0)
    }

    /// Orowan precipitation-hardening increment.
    ///
    /// Δσ_ph ≈ 0.81 · G · b / (2π · λ · √(1-ν)) where ν ≈ 0.3 is absorbed.
    ///
    /// Here we use the simplified Orowan–Ashby form:
    /// Δσ ≈ 0.13 · G · b / particle_spacing
    pub fn precipitation_hardening(particle_spacing: f64, shear_modulus: f64, burgers: f64) -> f64 {
        0.13 * shear_modulus * burgers / particle_spacing.max(1e-30)
    }

    /// Hollomon work-hardening (power-law): σ = k · ε_p^n.
    ///
    /// `eps_p` is plastic strain, `k` is strength coefficient (MPa), `n` is strain-hardening exponent.
    pub fn work_hardening(eps_p: f64, k: f64, n: f64) -> f64 {
        k * eps_p.max(0.0).powf(n)
    }

    /// Combine strengthening contributions by simple summation.
    ///
    /// `ss` = solid-solution, `ph` = precipitation, `wh` = work-hardening,
    /// `gb` = grain-boundary (Hall-Petch) increment.
    pub fn combined_strengthening(ss: f64, ph: f64, wh: f64, gb: f64) -> f64 {
        ss + ph + wh + gb
    }
}

// ─── Binary phase diagram ─────────────────────────────────────────────────────

/// Phase label in a binary alloy phase diagram.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryPhase {
    /// Fully liquid.
    Liquid,
    /// Single-phase alpha solid solution.
    Alpha,
    /// Single-phase beta solid solution.
    Beta,
    /// Two-phase α + β region.
    AlphaPlusBeta,
    /// Eutectic mixture.
    Eutectic,
}

/// Simple binary phase diagram helper.
pub struct PhaseDiagram;

impl PhaseDiagram {
    /// Determine the equilibrium phase for composition `x` (mol fraction of B)
    /// and temperature `temp` (K) in a simple eutectic binary system.
    ///
    /// `eutectic` = `(x_eutectic, T_eutectic)`.
    /// `liquidus` = `[T_melt_A, T_melt_B]` (melting points of pure A and B).
    pub fn phase_at_composition(
        x: f64,
        temp: f64,
        eutectic: (f64, f64),
        liquidus: [f64; 2],
    ) -> BinaryPhase {
        let (x_e, t_e) = eutectic;
        let t_a = liquidus[0];
        let t_b = liquidus[1];

        // Linear liquidus on A-rich side: T_liq_a(x) = T_A - (T_A - T_e) * x / x_e
        let t_liq_a = if x_e > 1e-12 {
            t_a - (t_a - t_e) * x / x_e
        } else {
            t_a
        };

        // Linear liquidus on B-rich side: T_liq_b(x) = T_B - (T_B - T_e) * (1-x) / (1-x_e)
        let t_liq_b = if (1.0 - x_e) > 1e-12 {
            t_b - (t_b - t_e) * (1.0 - x) / (1.0 - x_e)
        } else {
            t_b
        };

        let t_liquidus = if x <= x_e { t_liq_a } else { t_liq_b };

        if temp > t_liquidus {
            BinaryPhase::Liquid
        } else if (temp - t_e).abs() < 5.0 && (x - x_e).abs() < 0.02 {
            BinaryPhase::Eutectic
        } else if temp < t_e {
            BinaryPhase::AlphaPlusBeta
        } else if x < x_e {
            BinaryPhase::Alpha
        } else {
            BinaryPhase::Beta
        }
    }
}

// ─── Thermal properties ───────────────────────────────────────────────────────

/// Compute alloy thermal conductivity by linear rule of mixtures.
///
/// `k1`, `k2` are the component conductivities (W/m·K); `x` is the mole fraction of component 2.
pub fn alloy_thermal_conductivity(k1: f64, k2: f64, x: f64) -> f64 {
    (1.0 - x) * k1 + x * k2
}

/// Compute alloy specific heat capacity by mass-weighted mixture rule.
///
/// Each tuple is `(mass_fraction, cp)` in J/(kg·K).
pub fn alloy_specific_heat(cp_components: &[(f64, f64)]) -> f64 {
    cp_components.iter().map(|(f, cp)| f * cp).sum()
}

// ─── Corrosion ────────────────────────────────────────────────────────────────

/// Compute the Pitting Resistance Equivalent Number (PREN) for stainless steels.
///
/// PREN = %Cr + 3.3·%Mo + 16·%N
pub fn pitting_resistance_equivalent(cr: f64, mo: f64, n_pct: f64) -> f64 {
    cr + 3.3 * mo + 16.0 * n_pct
}

/// Assess galvanic corrosion risk from the electrode-potential difference.
///
/// Returns a static string describing the risk level.
pub fn galvanic_corrosion_risk(e1: f64, e2: f64) -> &'static str {
    let delta = (e1 - e2).abs();
    if delta < 0.1 {
        "negligible"
    } else if delta < 0.25 {
        "low"
    } else if delta < 0.5 {
        "moderate"
    } else {
        "high"
    }
}

// ─── AluminumAlloyT6 ──────────────────────────────────────────────────────────

/// Typical T6-temper mechanical properties for common aluminium alloys.
pub struct AluminumAlloyT6;

impl AluminumAlloyT6 {
    /// Return typical T6 mechanical properties for a given aluminium alloy series.
    pub fn properties(series: AlloySeries) -> AlloyMechanicalProps {
        match series {
            AlloySeries::Series2xxx => {
                AlloyMechanicalProps::new(324.0, 469.0, 10.0, 130.0, 73.1, 0.33, 37.0)
            }
            AlloySeries::Series6xxx => {
                AlloyMechanicalProps::new(276.0, 310.0, 12.0, 95.0, 68.9, 0.33, 29.0)
            }
            AlloySeries::Series7xxx => {
                AlloyMechanicalProps::new(503.0, 572.0, 11.0, 150.0, 71.7, 0.33, 29.0)
            }
            _ => AlloyMechanicalProps::new(100.0, 150.0, 8.0, 50.0, 69.0, 0.33, 20.0),
        }
    }
}

// ─── NickelSuperalloy ─────────────────────────────────────────────────────────

/// Creep and oxidation model for nickel-based superalloys.
pub struct NickelSuperalloy;

impl NickelSuperalloy {
    /// Compute steady-state creep rate using the Norton power law:
    /// ε̇ = A · σ^n · exp(-Q / (R·T))
    ///
    /// - `sigma`: stress in MPa
    /// - `temp`: temperature in K
    /// - `a_coeff`: pre-exponential factor (s⁻¹·MPa⁻ⁿ)
    /// - `n_exp`: stress exponent
    /// - `q_activation`: activation energy in J/mol
    pub fn creep_rate(sigma: f64, temp: f64, a_coeff: f64, n_exp: f64, q_activation: f64) -> f64 {
        const R: f64 = 8.314; // J/(mol·K)
        a_coeff * sigma.powf(n_exp) * (-q_activation / (R * temp.max(1e-3))).exp()
    }

    /// Estimate parabolic oxidation mass gain: Δm² = k_p · t.
    ///
    /// Returns Δm (mg/cm²) at time `t` (s) using parabolic rate constant `k_p`.
    pub fn oxidation_mass_gain(k_p: f64, t: f64) -> f64 {
        (k_p * t.max(0.0)).sqrt()
    }
}

// ─── WeldabilityIndex ─────────────────────────────────────────────────────────

/// Carbon equivalent for steel weldability assessment.
pub struct WeldabilityIndex;

impl WeldabilityIndex {
    /// Compute the International Institute of Welding (IIW) carbon equivalent:
    ///
    /// CE = C + Mn/6 + (Cr+Mo+V)/5 + (Ni+Cu)/15
    ///
    /// All values in weight %.
    pub fn carbon_equivalent_iiw(
        c: f64,
        mn: f64,
        cr: f64,
        mo: f64,
        v: f64,
        ni: f64,
        cu: f64,
    ) -> f64 {
        c + mn / 6.0 + (cr + mo + v) / 5.0 + (ni + cu) / 15.0
    }

    /// Classify weldability from the IIW carbon equivalent.
    pub fn weldability_class(ce: f64) -> &'static str {
        if ce < 0.35 {
            "excellent"
        } else if ce < 0.45 {
            "good"
        } else if ce < 0.60 {
            "fair"
        } else {
            "poor"
        }
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn stainless_304() -> AlloyComposition {
        AlloyComposition::new(
            vec![
                ("Fe".to_string(), 0.69),
                ("Cr".to_string(), 0.19),
                ("Ni".to_string(), 0.10),
                ("Mn".to_string(), 0.02),
            ],
            7900.0,
            (1673.0, 1723.0),
        )
    }

    fn al6061_t6() -> AlloyMechanicalProps {
        AlloyMechanicalProps::new(276.0, 310.0, 12.0, 95.0, 68.9, 0.33, 29.0)
    }

    #[test]
    fn test_alloy_composition_validate_valid() {
        let alloy = stainless_304();
        assert!(alloy.validate());
    }

    #[test]
    fn test_alloy_composition_validate_invalid() {
        let alloy = AlloyComposition::new(
            vec![("Fe".to_string(), 0.5), ("Cr".to_string(), 0.3)],
            7900.0,
            (1673.0, 1723.0),
        );
        assert!(!alloy.validate());
    }

    #[test]
    fn test_density_mixture_rule_of_mixtures() {
        let alloy = AlloyComposition::new(
            vec![("A".to_string(), 0.5), ("B".to_string(), 0.5)],
            0.0,
            (1000.0, 1200.0),
        );
        let densities = vec![("A".to_string(), 2000.0), ("B".to_string(), 4000.0)];
        let d = alloy.density_mixture(&densities);
        // Reuss bound = 1 / (0.5/2000 + 0.5/4000) = 2666.67
        assert!((d - 2666.67).abs() < 1.0, "d = {:.6}", d);
    }

    #[test]
    fn test_density_mixture_missing_element() {
        let alloy = AlloyComposition::new(vec![("X".to_string(), 1.0)], 7000.0, (1000.0, 1100.0));
        let densities = vec![("Fe".to_string(), 7874.0)];
        // Falls back to alloy.density
        let d = alloy.density_mixture(&densities);
        assert!((d - 7000.0).abs() < 1e-6);
    }

    #[test]
    fn test_safety_factor_normal() {
        let props = al6061_t6();
        let sf = props.safety_factor(138.0);
        assert!((sf - 2.0).abs() < 0.01);
    }

    #[test]
    fn test_safety_factor_zero_stress() {
        let props = al6061_t6();
        assert!(props.safety_factor(0.0).is_infinite());
    }

    #[test]
    fn test_is_brittle_ductile() {
        let props = al6061_t6();
        assert!(!props.is_brittle());
    }

    #[test]
    fn test_is_brittle_true() {
        let props = AlloyMechanicalProps::new(600.0, 700.0, 2.0, 700.0, 210.0, 0.28, 50.0);
        assert!(props.is_brittle());
    }

    #[test]
    fn test_hall_petch_yield_strength() {
        // σ_0 = 50 MPa, k = 0.5 MPa·mm^0.5, grain_size = 0.25 mm² → k/√d = 1.0
        let sy = HallPetch::yield_strength(50.0, 0.5, 0.25);
        assert!((sy - 51.0).abs() < 1e-9, "sy = {:.6}", sy);
    }

    #[test]
    fn test_hall_petch_grain_size_inversion() {
        let sigma_0 = 50.0;
        let k = 0.5;
        let d0 = 1.0e-6;
        let sy = HallPetch::yield_strength(sigma_0, k, d0);
        let d_back = HallPetch::grain_size_from_yield(sy, sigma_0, k);
        assert!((d_back - d0).abs() < 1e-18, "d_back = {:.6e}", d_back);
    }

    #[test]
    fn test_hall_petch_grain_size_same_yield() {
        let d = HallPetch::grain_size_from_yield(50.0, 50.0, 0.5);
        assert!(d.is_infinite());
    }

    #[test]
    fn test_solid_solution_strengthening_zero() {
        assert!((Strengthening::solid_solution_strengthening(0.0, 100.0) - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_solid_solution_strengthening_positive() {
        let ds = Strengthening::solid_solution_strengthening(0.01, 500.0);
        assert!(ds > 0.0);
    }

    #[test]
    fn test_precipitation_hardening_positive() {
        let ds = Strengthening::precipitation_hardening(1e-7, 26e3, 2.86e-10);
        assert!(ds > 0.0);
    }

    #[test]
    fn test_work_hardening_hollomon() {
        // σ = 500 · 0.2^0.2
        let expected = 500.0 * 0.2_f64.powf(0.2);
        let result = Strengthening::work_hardening(0.2, 500.0, 0.2);
        assert!((result - expected).abs() < 1e-9);
    }

    #[test]
    fn test_combined_strengthening() {
        let total = Strengthening::combined_strengthening(50.0, 30.0, 20.0, 10.0);
        assert!((total - 110.0).abs() < 1e-10);
    }

    #[test]
    fn test_phase_diagram_liquid() {
        // Above liquidus → Liquid
        let phase = PhaseDiagram::phase_at_composition(0.3, 1500.0, (0.5, 800.0), [1200.0, 1400.0]);
        assert_eq!(phase, BinaryPhase::Liquid);
    }

    #[test]
    fn test_phase_diagram_alpha() {
        let phase = PhaseDiagram::phase_at_composition(0.1, 900.0, (0.5, 600.0), [1200.0, 1100.0]);
        assert_eq!(phase, BinaryPhase::Alpha);
    }

    #[test]
    fn test_phase_diagram_beta() {
        let phase = PhaseDiagram::phase_at_composition(0.8, 900.0, (0.5, 600.0), [1200.0, 1100.0]);
        assert_eq!(phase, BinaryPhase::Beta);
    }

    #[test]
    fn test_phase_diagram_two_phase() {
        let phase = PhaseDiagram::phase_at_composition(0.3, 500.0, (0.5, 600.0), [1200.0, 1100.0]);
        assert_eq!(phase, BinaryPhase::AlphaPlusBeta);
    }

    #[test]
    fn test_alloy_thermal_conductivity_pure_components() {
        let k = alloy_thermal_conductivity(15.0, 400.0, 0.0);
        assert!((k - 15.0).abs() < 1e-10);
        let k2 = alloy_thermal_conductivity(15.0, 400.0, 1.0);
        assert!((k2 - 400.0).abs() < 1e-10);
    }

    #[test]
    fn test_alloy_thermal_conductivity_midpoint() {
        let k = alloy_thermal_conductivity(10.0, 20.0, 0.5);
        assert!((k - 15.0).abs() < 1e-10);
    }

    #[test]
    fn test_alloy_specific_heat_single() {
        let cp = alloy_specific_heat(&[(1.0, 500.0)]);
        assert!((cp - 500.0).abs() < 1e-10);
    }

    #[test]
    fn test_alloy_specific_heat_mixture() {
        let cp = alloy_specific_heat(&[(0.7, 500.0), (0.3, 900.0)]);
        assert!((cp - 620.0).abs() < 1e-10);
    }

    #[test]
    fn test_pren_calculation() {
        // 316L: Cr≈17, Mo≈2.5, N≈0.03
        let pren = pitting_resistance_equivalent(17.0, 2.5, 0.03);
        let expected = 17.0 + 3.3 * 2.5 + 16.0 * 0.03;
        assert!((pren - expected).abs() < 1e-10);
    }

    #[test]
    fn test_galvanic_corrosion_negligible() {
        assert_eq!(galvanic_corrosion_risk(0.0, 0.05), "negligible");
    }

    #[test]
    fn test_galvanic_corrosion_high() {
        assert_eq!(galvanic_corrosion_risk(0.0, 1.0), "high");
    }

    #[test]
    fn test_galvanic_corrosion_low() {
        assert_eq!(galvanic_corrosion_risk(0.1, 0.3), "low");
    }

    #[test]
    fn test_aluminum_t6_6xxx() {
        let props = AluminumAlloyT6::properties(AlloySeries::Series6xxx);
        assert!((props.yield_strength - 276.0).abs() < 1e-6);
    }

    #[test]
    fn test_aluminum_t6_7xxx_high_strength() {
        let p7 = AluminumAlloyT6::properties(AlloySeries::Series7xxx);
        let p6 = AluminumAlloyT6::properties(AlloySeries::Series6xxx);
        assert!(p7.yield_strength > p6.yield_strength);
    }

    #[test]
    fn test_nickel_creep_rate_positive() {
        let rate = NickelSuperalloy::creep_rate(200.0, 1073.0, 1e-15, 4.0, 290_000.0);
        assert!(rate > 0.0);
    }

    #[test]
    fn test_nickel_creep_rate_increases_with_stress() {
        let r1 = NickelSuperalloy::creep_rate(100.0, 1073.0, 1e-15, 4.0, 290_000.0);
        let r2 = NickelSuperalloy::creep_rate(200.0, 1073.0, 1e-15, 4.0, 290_000.0);
        assert!(r2 > r1);
    }

    #[test]
    fn test_oxidation_mass_gain_zero_time() {
        let dm = NickelSuperalloy::oxidation_mass_gain(1e-12, 0.0);
        assert!((dm - 0.0).abs() < 1e-20);
    }

    #[test]
    fn test_oxidation_mass_gain_positive() {
        let dm = NickelSuperalloy::oxidation_mass_gain(1e-12, 3600.0);
        assert!(dm > 0.0);
    }

    #[test]
    fn test_carbon_equivalent_low_alloy() {
        let ce = WeldabilityIndex::carbon_equivalent_iiw(0.15, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0);
        assert!((ce - 0.3167).abs() < 0.001, "ce = {:.6}", ce);
    }

    #[test]
    fn test_weldability_class_excellent() {
        assert_eq!(WeldabilityIndex::weldability_class(0.30), "excellent");
    }

    #[test]
    fn test_weldability_class_poor() {
        assert_eq!(WeldabilityIndex::weldability_class(0.65), "poor");
    }

    #[test]
    fn test_alloy_series_debug() {
        let s = format!("{:?}", AlloySeries::Inconel718);
        assert!(s.contains("Inconel718"));
    }

    #[test]
    fn test_binary_phase_debug() {
        let s = format!("{:?}", BinaryPhase::AlphaPlusBeta);
        assert!(s.contains("Alpha"));
    }
}