oxiphysics-materials 0.1.0

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
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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Additive manufacturing material models.
//!
//! This module covers the process-structure-property (PSP) chain for key AM
//! technologies:
//!
//! - **Powder bed fusion** (SLM / DMLS): [`PbfMaterial`], [`PbfProcessParams`],
//!   [`MeltPoolGeometry`], thermal gradient and residual stress computation.
//! - **Porosity effects**: [`PorosityModel`] — Ramakrishnan-Arunachalam,
//!   Mori-Tanaka, Coble-Kingery relationships.
//! - **Microstructure evolution**: [`GrainGrowthModel`], [`MartensiticModel`].
//! - **Binder jetting**: [`BinderJettingMaterial`], sintering shrinkage.
//! - **FDM polymers**: [`FdmPolymerMaterial`] — anisotropy, layer adhesion.
//! - **Support structures**: [`SupportMaterial`].
//! - **PSP linkage**: [`PspLinkage`] combining all above.
//! - **Scan strategy effects**: [`ScanStrategyEffect`].
//! - **Surface roughness**: [`AmSurfaceRoughness`].

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

use std::f64::consts::PI;

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Boltzmann constant (J/K).
pub const KB: f64 = 1.380_649e-23;

/// Gas constant (J/(mol·K)).
pub const R_GAS: f64 = 8.314_462;

// ---------------------------------------------------------------------------
// Common enums
// ---------------------------------------------------------------------------

/// AM process technology.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AmProcess {
    /// Selective laser melting (SLM) / laser powder bed fusion (LPBF).
    Slm,
    /// Direct metal laser sintering (DMLS).
    Dmls,
    /// Electron beam melting (EBM).
    Ebm,
    /// Binder jetting (BJ).
    BinderJetting,
    /// Fused deposition modelling (FDM) / fused filament fabrication (FFF).
    Fdm,
    /// Directed energy deposition (DED).
    Ded,
}

/// Metal alloy system commonly used in powder bed fusion.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MetalAlloy {
    /// Ti-6Al-4V (Grade 5 titanium).
    Ti6Al4V,
    /// 316L stainless steel.
    Steel316L,
    /// AlSi10Mg aluminium alloy.
    AlSi10Mg,
    /// IN718 nickel superalloy.
    In718,
    /// Maraging steel (1.2709).
    MaragingSteel,
    /// Cobalt-chromium (CoCr).
    CoCr,
    /// Copper (Cu).
    Copper,
}

// ---------------------------------------------------------------------------
// Powder Bed Fusion Material
// ---------------------------------------------------------------------------

/// Intrinsic material properties relevant to PBF processing.
#[derive(Debug, Clone)]
pub struct PbfMaterial {
    /// Alloy type.
    pub alloy: MetalAlloy,
    /// Density of fully dense material (kg/m³).
    pub density: f64,
    /// Thermal conductivity at room temperature (W/(m·K)).
    pub thermal_conductivity: f64,
    /// Specific heat capacity (J/(kg·K)).
    pub specific_heat: f64,
    /// Latent heat of fusion (J/kg).
    pub latent_heat_fusion: f64,
    /// Solidus temperature (K).
    pub solidus_temp: f64,
    /// Liquidus temperature (K).
    pub liquidus_temp: f64,
    /// Absorptivity for the process laser wavelength (0–1).
    pub absorptivity: f64,
    /// Young's modulus at room temperature (Pa).
    pub elastic_modulus: f64,
    /// Poisson's ratio.
    pub poisson_ratio: f64,
    /// Yield strength at room temperature (Pa).
    pub yield_strength: f64,
    /// Tensile strength at room temperature (Pa).
    pub tensile_strength: f64,
    /// Thermal expansion coefficient (1/K).
    pub thermal_expansion: f64,
    /// Powder bed packing fraction (0–1).
    pub packing_fraction: f64,
}

impl PbfMaterial {
    /// Pre-defined Ti-6Al-4V properties for SLM.
    pub fn ti6al4v_slm() -> Self {
        Self {
            alloy: MetalAlloy::Ti6Al4V,
            density: 4_430.0,
            thermal_conductivity: 6.7,
            specific_heat: 560.0,
            latent_heat_fusion: 286_000.0,
            solidus_temp: 1878.0,
            liquidus_temp: 1928.0,
            absorptivity: 0.35,
            elastic_modulus: 114e9,
            poisson_ratio: 0.342,
            yield_strength: 930e6,
            tensile_strength: 1_000e6,
            thermal_expansion: 8.6e-6,
            packing_fraction: 0.60,
        }
    }

    /// Pre-defined 316L stainless steel properties for SLM.
    pub fn steel_316l_slm() -> Self {
        Self {
            alloy: MetalAlloy::Steel316L,
            density: 7_990.0,
            thermal_conductivity: 16.3,
            specific_heat: 500.0,
            latent_heat_fusion: 272_000.0,
            solidus_temp: 1648.0,
            liquidus_temp: 1673.0,
            absorptivity: 0.40,
            elastic_modulus: 193e9,
            poisson_ratio: 0.290,
            yield_strength: 530e6,
            tensile_strength: 680e6,
            thermal_expansion: 16.0e-6,
            packing_fraction: 0.62,
        }
    }

    /// Pre-defined AlSi10Mg properties for SLM.
    pub fn alsi10mg_slm() -> Self {
        Self {
            alloy: MetalAlloy::AlSi10Mg,
            density: 2_680.0,
            thermal_conductivity: 130.0,
            specific_heat: 910.0,
            latent_heat_fusion: 396_000.0,
            solidus_temp: 833.0,
            liquidus_temp: 868.0,
            absorptivity: 0.09,
            elastic_modulus: 72e9,
            poisson_ratio: 0.330,
            yield_strength: 240e6,
            tensile_strength: 330e6,
            thermal_expansion: 21.0e-6,
            packing_fraction: 0.62,
        }
    }

    /// Thermal diffusivity α = k / (ρ c_p) (m²/s).
    pub fn thermal_diffusivity(&self) -> f64 {
        self.thermal_conductivity / (self.density * self.specific_heat)
    }

    /// Melting range ΔT = T_liq − T_sol (K).
    pub fn melting_range(&self) -> f64 {
        (self.liquidus_temp - self.solidus_temp).max(0.0)
    }
}

// ---------------------------------------------------------------------------
// PBF Process Parameters
// ---------------------------------------------------------------------------

/// Laser / electron beam process parameters for powder bed fusion.
#[derive(Debug, Clone)]
pub struct PbfProcessParams {
    /// Laser power (W).
    pub power: f64,
    /// Scan speed (m/s).
    pub scan_speed: f64,
    /// Hatch spacing (m).
    pub hatch_spacing: f64,
    /// Layer thickness (m).
    pub layer_thickness: f64,
    /// Laser spot radius (1/e² radius) (m).
    pub spot_radius: f64,
    /// Preheat temperature of the build plate (K).
    pub preheat_temp: f64,
    /// Scan strategy rotation angle between layers (degrees).
    pub rotation_angle_deg: f64,
}

impl PbfProcessParams {
    /// Volumetric energy density (J/m³): `E_v = P / (v * h * t)`.
    pub fn volumetric_energy_density(&self) -> f64 {
        let denom = self.scan_speed * self.hatch_spacing * self.layer_thickness;
        if denom < 1e-30 {
            f64::INFINITY
        } else {
            self.power / denom
        }
    }

    /// Linear energy density (J/m): `E_l = P / v`.
    pub fn linear_energy_density(&self) -> f64 {
        if self.scan_speed < 1e-12 {
            f64::INFINITY
        } else {
            self.power / self.scan_speed
        }
    }

    /// Interaction time of laser with powder (s): `t_int = 2*r / v`.
    pub fn interaction_time(&self) -> f64 {
        if self.scan_speed < 1e-12 {
            f64::INFINITY
        } else {
            2.0 * self.spot_radius / self.scan_speed
        }
    }
}

// ---------------------------------------------------------------------------
// Melt Pool Geometry (Rosenthal / Goldak approximation)
// ---------------------------------------------------------------------------

/// Estimated melt pool geometry from process parameters and material properties.
///
/// Based on the Eagar-Tsai analytical model (simplified).
#[derive(Debug, Clone)]
pub struct MeltPoolGeometry {
    /// Melt pool half-width (m).
    pub half_width: f64,
    /// Melt pool half-length (m) (in scan direction).
    pub half_length: f64,
    /// Melt pool depth (m).
    pub depth: f64,
}

impl MeltPoolGeometry {
    /// Estimate melt pool geometry using the simplified Eagar-Tsai model.
    ///
    /// # Arguments
    /// * `mat` — Material properties.
    /// * `params` — Process parameters.
    /// * `ambient_temp` — Ambient/preheat temperature (K).
    pub fn eagar_tsai(mat: &PbfMaterial, params: &PbfProcessParams, ambient_temp: f64) -> Self {
        let alpha = mat.thermal_diffusivity();
        let delta_t = mat.liquidus_temp - ambient_temp;
        let q = mat.absorptivity * params.power;
        let v = params.scan_speed;
        let sigma = params.spot_radius;
        // Characteristic length scales
        let r_char = ((2.0 * q * alpha) / (PI * mat.thermal_conductivity * v * delta_t))
            .sqrt()
            .max(sigma);
        let half_width = r_char;
        let half_length = r_char * (1.0 + v * r_char / (2.0 * alpha)).min(5.0);
        let depth = r_char * 0.5; // simplified: half the width
        Self {
            half_width,
            half_length,
            depth,
        }
    }

    /// Melt pool volume (m³), approximated as an ellipsoid.
    pub fn volume(&self) -> f64 {
        (4.0 / 3.0) * PI * self.half_width * self.half_length * self.depth
    }

    /// Aspect ratio (length / width).
    pub fn aspect_ratio(&self) -> f64 {
        if self.half_width < 1e-20 {
            1.0
        } else {
            self.half_length / self.half_width
        }
    }
}

// ---------------------------------------------------------------------------
// Thermal Gradient and Cooling Rate
// ---------------------------------------------------------------------------

/// Thermal gradient and cooling rate estimates for AM.
#[derive(Debug, Clone)]
pub struct ThermalGradient {
    /// Thermal gradient magnitude at the melt pool boundary (K/m).
    pub gradient_magnitude: f64,
    /// Cooling rate at the melt pool boundary (K/s).
    pub cooling_rate: f64,
    /// Solidification rate (m/s).
    pub solidification_rate: f64,
}

impl ThermalGradient {
    /// Estimate thermal conditions from Rosenthal point-source solution.
    ///
    /// # Arguments
    /// * `mat` — Material properties.
    /// * `params` — Process parameters.
    /// * `ambient_temp` — Ambient temperature (K).
    pub fn from_rosenthal(mat: &PbfMaterial, params: &PbfProcessParams, ambient_temp: f64) -> Self {
        let alpha = mat.thermal_diffusivity();
        let v = params.scan_speed;
        let q = mat.absorptivity * params.power;
        let k = mat.thermal_conductivity;
        let r = params.spot_radius.max(1e-6);
        // Temperature at melt-pool edge (Rosenthal approximation)
        let t_peak = ambient_temp + q / (2.0 * PI * k * r) * (-v * r / (2.0 * alpha)).exp();
        let gradient_magnitude = (t_peak - ambient_temp) / r;
        let cooling_rate = gradient_magnitude * v;
        let solidification_rate = v;
        Self {
            gradient_magnitude,
            cooling_rate,
            solidification_rate,
        }
    }

    /// G·R product (K/s) — controls solidification microstructure.
    pub fn g_times_r(&self) -> f64 {
        self.gradient_magnitude * self.solidification_rate
    }

    /// G/R ratio (K·s/m²) — controls morphology (planar → cellular → dendritic).
    pub fn g_over_r(&self) -> f64 {
        if self.solidification_rate < 1e-12 {
            f64::INFINITY
        } else {
            self.gradient_magnitude / self.solidification_rate
        }
    }
}

// ---------------------------------------------------------------------------
// Residual Stress from Thermal Gradients
// ---------------------------------------------------------------------------

/// Residual stress model for PBF (temperature-gradient mechanism, TGM).
#[derive(Debug, Clone)]
pub struct ResidualStressModel {
    /// Elastic modulus (Pa).
    pub elastic_modulus: f64,
    /// Thermal expansion coefficient (1/K).
    pub thermal_expansion: f64,
    /// Yield strength at elevated temperature (Pa).
    pub yield_strength_hot: f64,
    /// Poisson's ratio.
    pub poisson_ratio: f64,
}

impl ResidualStressModel {
    /// Build from a `PbfMaterial`.
    pub fn from_material(mat: &PbfMaterial) -> Self {
        Self {
            elastic_modulus: mat.elastic_modulus,
            thermal_expansion: mat.thermal_expansion,
            yield_strength_hot: mat.yield_strength * 0.5,
            poisson_ratio: mat.poisson_ratio,
        }
    }

    /// Estimate peak residual stress using the temperature-gradient mechanism.
    ///
    /// σ_res ≈ min(E α ΔT / (1 - ν), σ_y_hot)
    ///
    /// # Arguments
    /// * `delta_t` — Temperature difference across the heated layer (K).
    pub fn peak_residual_stress(&self, delta_t: f64) -> f64 {
        let elastic_stress =
            self.elastic_modulus * self.thermal_expansion * delta_t / (1.0 - self.poisson_ratio);
        elastic_stress.min(self.yield_strength_hot)
    }

    /// Estimate part distortion (curvature) from residual stress bilayer model.
    ///
    /// Uses Stoney's formula: κ = 6 σ t_f / (E_s t_s²)
    ///
    /// # Arguments
    /// * `sigma` — Residual stress in film (Pa).
    /// * `film_thickness` — Deposited layer thickness (m).
    /// * `substrate_thickness` — Substrate/part thickness (m).
    pub fn stoney_curvature(
        &self,
        sigma: f64,
        film_thickness: f64,
        substrate_thickness: f64,
    ) -> f64 {
        let es = self.elastic_modulus / (1.0 - self.poisson_ratio * self.poisson_ratio);
        let ts2 = substrate_thickness * substrate_thickness;
        if ts2 < 1e-30 {
            0.0
        } else {
            6.0 * sigma * film_thickness / (es * ts2)
        }
    }

    /// Von Mises equivalent residual stress from in-plane biaxial state.
    ///
    /// For biaxial state σ_x = σ_y = σ:  σ_VM = σ.
    pub fn von_mises_biaxial(&self, sigma_inplane: f64) -> f64 {
        sigma_inplane.abs()
    }
}

// ---------------------------------------------------------------------------
// Porosity Effects on Mechanical Properties
// ---------------------------------------------------------------------------

/// Porosity model for relating relative density to effective properties.
#[derive(Debug, Clone)]
pub struct PorosityModel {
    /// Fully dense Young's modulus E₀ (Pa).
    pub e0: f64,
    /// Fully dense yield strength σ_y0 (Pa).
    pub yield_strength0: f64,
    /// Fully dense tensile strength σ_u0 (Pa).
    pub tensile_strength0: f64,
    /// Fully dense density ρ₀ (kg/m³).
    pub density0: f64,
}

impl PorosityModel {
    /// Effective Young's modulus using Ramakrishnan-Arunachalam model.
    ///
    /// E(p) = E₀ (1 - p)² / (1 + β p)  where β ≈ 2.
    pub fn elastic_modulus(&self, porosity: f64) -> f64 {
        let beta = 2.0;
        let p = porosity.clamp(0.0, 0.9999);
        self.e0 * (1.0 - p).powi(2) / (1.0 + beta * p)
    }

    /// Effective yield strength using power-law scaling.
    ///
    /// σ_y(p) = σ_y0 (1 - p)^n  with n ≈ 2.
    pub fn yield_strength(&self, porosity: f64) -> f64 {
        let n = 2.0;
        let p = porosity.clamp(0.0, 0.9999);
        self.yield_strength0 * (1.0 - p).powf(n)
    }

    /// Effective tensile strength.
    pub fn tensile_strength(&self, porosity: f64) -> f64 {
        let n = 1.8;
        let p = porosity.clamp(0.0, 0.9999);
        self.tensile_strength0 * (1.0 - p).powf(n)
    }

    /// Effective density.
    pub fn effective_density(&self, porosity: f64) -> f64 {
        self.density0 * (1.0 - porosity.clamp(0.0, 1.0))
    }

    /// Relative density (1 − porosity).
    pub fn relative_density(&self, porosity: f64) -> f64 {
        1.0 - porosity.clamp(0.0, 1.0)
    }

    /// Fatigue strength reduction factor due to porosity.
    ///
    /// Uses empirical Murakami √area model: Kt ≈ 1 + 2 √(πp/4)
    pub fn fatigue_strength_reduction(&self, porosity: f64) -> f64 {
        let p = porosity.clamp(0.0, 0.5);
        1.0 + 2.0 * (PI * p / 4.0).sqrt()
    }
}

// ---------------------------------------------------------------------------
// Microstructure Evolution
// ---------------------------------------------------------------------------

/// Grain growth model for AM (isothermal / anisothermal).
///
/// Uses the parabolic grain growth law: D² − D₀² = K₀ t exp(−Q / RT).
#[derive(Debug, Clone)]
pub struct GrainGrowthModel {
    /// Pre-exponential factor K₀ (m²/s).
    pub k0: f64,
    /// Activation energy for grain boundary migration Q (J/mol).
    pub activation_energy: f64,
    /// Initial grain diameter D₀ (m).
    pub initial_diameter: f64,
}

impl GrainGrowthModel {
    /// Typical values for Ti-6Al-4V β-grain growth.
    pub fn ti6al4v_beta() -> Self {
        Self {
            k0: 7.0e-8,
            activation_energy: 175_000.0,
            initial_diameter: 50e-6,
        }
    }

    /// Grain diameter after isothermal annealing for time `t` at temperature `T` (K).
    pub fn grain_diameter_isothermal(&self, temp_k: f64, time_s: f64) -> f64 {
        let keff = self.k0 * (-self.activation_energy / (R_GAS * temp_k)).exp();
        let d2 = self.initial_diameter.powi(2) + keff * time_s;
        d2.max(0.0).sqrt()
    }

    /// Mean grain aspect ratio after directional solidification.
    ///
    /// In PBF columnar grains grow with aspect ratio AR ≈ G/R × const.
    pub fn columnar_aspect_ratio(&self, g_over_r: f64) -> f64 {
        // Empirical: AR ≈ 0.01 * (G/R) + 1; clamp to reasonable range
        (0.01 * g_over_r + 1.0).min(20.0)
    }
}

/// Martensitic transformation model for Ti alloys and steels in AM.
#[derive(Debug, Clone)]
pub struct MartensiticModel {
    /// Martensite start temperature M_s (K).
    pub ms_temp: f64,
    /// Martensite finish temperature M_f (K).
    pub mf_temp: f64,
    /// Maximum volume fraction of martensite (0–1).
    pub max_martensite_fraction: f64,
    /// Koistinen-Marburger coefficient b (K⁻¹).
    pub km_coefficient: f64,
}

impl MartensiticModel {
    /// Ti-6Al-4V α' martensite (hexagonal).
    pub fn ti6al4v_martensite() -> Self {
        Self {
            ms_temp: 875.0,
            mf_temp: 500.0,
            max_martensite_fraction: 1.0,
            km_coefficient: 0.011,
        }
    }

    /// 316L steel martensite (not typical at RT, Ms < 0).
    pub fn steel_316l_martensite() -> Self {
        Self {
            ms_temp: 233.0, // −40 °C
            mf_temp: 123.0, // −150 °C
            max_martensite_fraction: 0.30,
            km_coefficient: 0.033,
        }
    }

    /// Martensite volume fraction at temperature `T` (K), Koistinen-Marburger equation.
    ///
    /// f_m = f_max * (1 − exp(−b * (M_s − T))) for T < M_s; else 0.
    pub fn martensite_fraction(&self, temp_k: f64) -> f64 {
        if temp_k >= self.ms_temp {
            return 0.0;
        }
        let delta_t = self.ms_temp - temp_k;
        let f = self.max_martensite_fraction * (1.0 - (-self.km_coefficient * delta_t).exp());
        f.min(self.max_martensite_fraction)
    }

    /// Strength contribution from martensite (Hall-Petch-like).
    ///
    /// Δσ ≈ σ_α_prime * f_m  where σ_α_prime is the intrinsic martensite strength.
    pub fn strength_contribution(&self, temp_k: f64, martensite_strength: f64) -> f64 {
        self.martensite_fraction(temp_k) * martensite_strength
    }
}

// ---------------------------------------------------------------------------
// Binder Jetting Material
// ---------------------------------------------------------------------------

/// Material model for binder jetting (BJ) process.
#[derive(Debug, Clone)]
pub struct BinderJettingMaterial {
    /// Green body porosity after printing (0–1).
    pub green_porosity: f64,
    /// Target sintered porosity after sintering (0–1).
    pub sintered_porosity: f64,
    /// Linear shrinkage during sintering (0–1).
    pub linear_shrinkage: f64,
    /// Binder content (volume fraction).
    pub binder_fraction: f64,
    /// Sintering temperature (K).
    pub sintering_temp: f64,
    /// Sintering time (s).
    pub sintering_time: f64,
    /// Fully dense yield strength (Pa).
    pub dense_yield_strength: f64,
    /// Fully dense elastic modulus (Pa).
    pub dense_elastic_modulus: f64,
}

impl BinderJettingMaterial {
    /// Typical 316L stainless steel BJ parameters.
    pub fn steel_316l_bj() -> Self {
        Self {
            green_porosity: 0.40,
            sintered_porosity: 0.02,
            linear_shrinkage: 0.18,
            binder_fraction: 0.35,
            sintering_temp: 1593.0,
            sintering_time: 3600.0 * 6.0,
            dense_yield_strength: 530e6,
            dense_elastic_modulus: 193e9,
        }
    }

    /// Volumetric shrinkage from green to sintered state.
    pub fn volumetric_shrinkage(&self) -> f64 {
        1.0 - (1.0 - self.linear_shrinkage).powi(3)
    }

    /// Relative density of the sintered part.
    pub fn relative_density(&self) -> f64 {
        1.0 - self.sintered_porosity
    }

    /// Effective yield strength using power-law porosity correction.
    pub fn effective_yield_strength(&self) -> f64 {
        let p = self.sintered_porosity;
        self.dense_yield_strength * (1.0 - p).powf(2.0)
    }

    /// Effective elastic modulus using Ramakrishnan-Arunachalam model.
    pub fn effective_elastic_modulus(&self) -> f64 {
        let beta = 2.0;
        let p = self.sintered_porosity;
        self.dense_elastic_modulus * (1.0 - p).powi(2) / (1.0 + beta * p)
    }
}

// ---------------------------------------------------------------------------
// FDM Polymer Material
// ---------------------------------------------------------------------------

/// FDM/FFF polymer material with directional (anisotropic) properties.
#[derive(Debug, Clone)]
pub struct FdmPolymerMaterial {
    /// Polymer name/grade.
    pub name: String,
    /// Density (kg/m³).
    pub density: f64,
    /// In-layer (XY) tensile strength (Pa).
    pub tensile_strength_xy: f64,
    /// Through-layer (Z) tensile strength (Pa).
    pub tensile_strength_z: f64,
    /// In-layer Young's modulus (Pa).
    pub elastic_modulus_xy: f64,
    /// Through-layer Young's modulus (Pa).
    pub elastic_modulus_z: f64,
    /// In-layer elongation at break (0–1).
    pub elongation_xy: f64,
    /// Through-layer elongation at break (0–1).
    pub elongation_z: f64,
    /// Glass transition temperature (K).
    pub glass_transition_temp: f64,
    /// Layer adhesion strength (Pa) — inter-layer bond strength.
    pub layer_adhesion_strength: f64,
    /// Layer height (m).
    pub layer_height: f64,
    /// Raster width (m).
    pub raster_width: f64,
    /// Air gap between rasters (m, can be negative for overlap).
    pub air_gap: f64,
    /// Raster angle (degrees from X-axis).
    pub raster_angle_deg: f64,
}

impl FdmPolymerMaterial {
    /// Generic PLA-like material.
    pub fn pla_generic() -> Self {
        Self {
            name: "PLA".to_string(),
            density: 1_240.0,
            tensile_strength_xy: 60e6,
            tensile_strength_z: 35e6,
            elastic_modulus_xy: 3_500e6,
            elastic_modulus_z: 2_800e6,
            elongation_xy: 0.04,
            elongation_z: 0.02,
            glass_transition_temp: 333.0,
            layer_adhesion_strength: 30e6,
            layer_height: 0.2e-3,
            raster_width: 0.4e-3,
            air_gap: 0.0,
            raster_angle_deg: 45.0,
        }
    }

    /// Generic PEEK material (high-performance polymer).
    pub fn peek_generic() -> Self {
        Self {
            name: "PEEK".to_string(),
            density: 1_310.0,
            tensile_strength_xy: 100e6,
            tensile_strength_z: 60e6,
            elastic_modulus_xy: 4_000e6,
            elastic_modulus_z: 3_200e6,
            elongation_xy: 0.03,
            elongation_z: 0.02,
            glass_transition_temp: 416.0,
            layer_adhesion_strength: 50e6,
            layer_height: 0.15e-3,
            raster_width: 0.4e-3,
            air_gap: -0.05e-3,
            raster_angle_deg: 0.0,
        }
    }

    /// Anisotropy ratio: Z-strength / XY-strength.
    pub fn anisotropy_ratio(&self) -> f64 {
        if self.tensile_strength_xy < 1e-12 {
            1.0
        } else {
            self.tensile_strength_z / self.tensile_strength_xy
        }
    }

    /// Estimate effective tensile strength at a build angle `theta_deg`
    /// using a simple cosine interpolation.
    pub fn tensile_strength_at_angle(&self, theta_deg: f64) -> f64 {
        let theta = theta_deg.to_radians();
        let cos2 = theta.cos().powi(2);
        let sin2 = theta.sin().powi(2);
        cos2 * self.tensile_strength_xy + sin2 * self.tensile_strength_z
    }

    /// Estimated void volume fraction from air gap geometry.
    pub fn void_fraction(&self) -> f64 {
        if self.raster_width < 1e-12 || self.layer_height < 1e-12 {
            return 0.0;
        }
        let effective_gap = self.air_gap / self.raster_width;
        effective_gap.clamp(0.0, 1.0)
    }
}

// ---------------------------------------------------------------------------
// Support Structure Material
// ---------------------------------------------------------------------------

/// Material model for support structures in AM.
#[derive(Debug, Clone)]
pub struct SupportMaterial {
    /// Density scaling relative to bulk (0–1).
    pub density_fraction: f64,
    /// Elastic modulus fraction relative to bulk (0–1).
    pub modulus_fraction: f64,
    /// Critical force to remove support (N/m²).
    pub removal_force: f64,
    /// Support structure type.
    pub support_type: SupportType,
}

/// Type of AM support structure.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SupportType {
    /// Solid block support.
    Solid,
    /// Lattice / sparse support.
    Lattice,
    /// Cone-tree support.
    TreeLike,
    /// Water-soluble polymer support (FDM).
    Soluble,
    /// Powder-bed support (no structure needed in PBF).
    PowderBed,
}

impl SupportMaterial {
    /// Typical metal PBF block support.
    pub fn metal_block_support() -> Self {
        Self {
            density_fraction: 0.5,
            modulus_fraction: 0.4,
            removal_force: 1e6,
            support_type: SupportType::Solid,
        }
    }

    /// FDM soluble support (e.g. PVA).
    pub fn fdm_soluble() -> Self {
        Self {
            density_fraction: 0.8,
            modulus_fraction: 0.3,
            removal_force: 0.5e6,
            support_type: SupportType::Soluble,
        }
    }

    /// Estimate thermal resistance added by support block.
    ///
    /// R_thermal = h / (k_support * A)
    pub fn thermal_resistance(&self, height: f64, area: f64, bulk_conductivity: f64) -> f64 {
        let k = bulk_conductivity * self.modulus_fraction;
        if k < 1e-12 || area < 1e-20 {
            f64::INFINITY
        } else {
            height / (k * area)
        }
    }
}

// ---------------------------------------------------------------------------
// Process-Structure-Property (PSP) Linkage
// ---------------------------------------------------------------------------

/// PSP linkage: combines process parameters → structure descriptors → properties.
#[derive(Debug, Clone)]
pub struct PspLinkage {
    /// Material system.
    pub material: PbfMaterial,
    /// Process parameters.
    pub process: PbfProcessParams,
    /// Estimated porosity (0–1) from process window.
    pub estimated_porosity: f64,
}

impl PspLinkage {
    /// Construct PSP from material and process parameters.
    pub fn new(material: PbfMaterial, process: PbfProcessParams) -> Self {
        let porosity = estimate_porosity_from_energy_density(
            process.volumetric_energy_density(),
            material.density,
        );
        Self {
            material,
            process,
            estimated_porosity: porosity,
        }
    }

    /// Effective yield strength incorporating porosity.
    pub fn effective_yield_strength(&self) -> f64 {
        let p = self.estimated_porosity;
        self.material.yield_strength * (1.0 - p).powf(2.0)
    }

    /// Effective elastic modulus incorporating porosity.
    pub fn effective_elastic_modulus(&self) -> f64 {
        let beta = 2.0;
        let p = self.estimated_porosity;
        self.material.elastic_modulus * (1.0 - p).powi(2) / (1.0 + beta * p)
    }

    /// Thermal gradient estimate.
    pub fn thermal_gradient(&self, ambient_temp: f64) -> ThermalGradient {
        ThermalGradient::from_rosenthal(&self.material, &self.process, ambient_temp)
    }

    /// Melt pool geometry estimate.
    pub fn melt_pool(&self, ambient_temp: f64) -> MeltPoolGeometry {
        MeltPoolGeometry::eagar_tsai(&self.material, &self.process, ambient_temp)
    }

    /// Estimated relative density (1 − porosity).
    pub fn relative_density(&self) -> f64 {
        1.0 - self.estimated_porosity
    }
}

/// Estimate porosity from volumetric energy density using a sigmoid curve.
///
/// At optimal energy density (~60–80 J/mm³) porosity is minimal (~0.1%).
/// Too low → lack of fusion; too high → keyhole porosity.
pub fn estimate_porosity_from_energy_density(energy_density_j_per_m3: f64, _density: f64) -> f64 {
    // Convert to J/mm³
    let ev = energy_density_j_per_m3 * 1e-9;
    // Optimal energy density for most metals ≈ 70 J/mm³
    let ev_opt = 70.0_f64;
    let delta = (ev - ev_opt).abs() / ev_opt;
    // Porosity rises parabolically from ~0.1% at optimum
    let base = 0.001_f64;
    (base + 0.5 * delta * delta).min(0.30)
}

// ---------------------------------------------------------------------------
// Scan Strategy Effects
// ---------------------------------------------------------------------------

/// Effect of scan strategy on texture and residual stress.
#[derive(Debug, Clone)]
pub struct ScanStrategyEffect {
    /// Rotation angle between consecutive layers (degrees).
    pub rotation_angle_deg: f64,
    /// Estimated texture coefficient (0 = random, 1 = fully textured).
    pub texture_coefficient: f64,
    /// Residual stress scaling factor relative to unidirectional scanning.
    pub residual_stress_factor: f64,
    /// Estimated relative density achievable with this strategy.
    pub relative_density: f64,
}

impl ScanStrategyEffect {
    /// Effects for unidirectional (0° rotation) scanning.
    pub fn unidirectional() -> Self {
        Self {
            rotation_angle_deg: 0.0,
            texture_coefficient: 0.85,
            residual_stress_factor: 1.0,
            relative_density: 0.993,
        }
    }

    /// Effects for 67° rotation (common in SLM).
    pub fn rotating_67() -> Self {
        Self {
            rotation_angle_deg: 67.0,
            texture_coefficient: 0.35,
            residual_stress_factor: 0.65,
            relative_density: 0.997,
        }
    }

    /// Effects for 90° alternating scanning.
    pub fn alternating_90() -> Self {
        Self {
            rotation_angle_deg: 90.0,
            texture_coefficient: 0.50,
            residual_stress_factor: 0.75,
            relative_density: 0.995,
        }
    }

    /// Effects for island/checkerboard scanning.
    pub fn island() -> Self {
        Self {
            rotation_angle_deg: 90.0,
            texture_coefficient: 0.40,
            residual_stress_factor: 0.60,
            relative_density: 0.996,
        }
    }

    /// Estimate the number of unique scan orientations in N layers.
    pub fn unique_orientations_in_n_layers(&self, n_layers: usize) -> usize {
        if self.rotation_angle_deg < 1e-6 {
            return 1;
        }
        let period = (360.0 / self.rotation_angle_deg).round() as usize;
        period.min(n_layers)
    }
}

// ---------------------------------------------------------------------------
// Surface Roughness from AM
// ---------------------------------------------------------------------------

/// AM surface roughness model.
///
/// Covers staircase effect, spattering, and powder adhesion contributions.
#[derive(Debug, Clone)]
pub struct AmSurfaceRoughness {
    /// Layer thickness (m).
    pub layer_thickness: f64,
    /// Powder particle diameter D50 (m).
    pub powder_d50: f64,
    /// Build angle relative to horizontal (degrees, 0 = flat top, 90 = vertical wall).
    pub build_angle_deg: f64,
    /// Laser spot radius (m).
    pub spot_radius: f64,
}

impl AmSurfaceRoughness {
    /// Staircase roughness Ra from layer thickness and build angle.
    ///
    /// Ra_staircase ≈ (t / 4) |cos θ| / sin θ   for θ in (0°, 90°)
    pub fn ra_staircase(&self) -> f64 {
        let theta = self.build_angle_deg.to_radians();
        let sin_t = theta.sin().max(1e-6);
        let cos_t = theta.cos().abs();
        self.layer_thickness / 4.0 * cos_t / sin_t
    }

    /// Contribution from partially sintered/attached powder particles.
    ///
    /// Ra_powder ≈ D50 / 4
    pub fn ra_powder_adhesion(&self) -> f64 {
        self.powder_d50 / 4.0
    }

    /// Total estimated surface roughness Ra (m).
    pub fn ra_total(&self) -> f64 {
        (self.ra_staircase().powi(2) + self.ra_powder_adhesion().powi(2)).sqrt()
    }

    /// Roughness in micrometres.
    pub fn ra_total_um(&self) -> f64 {
        self.ra_total() * 1e6
    }

    /// Estimate fatigue life reduction factor due to surface roughness.
    ///
    /// Uses modified Goodman: Kf ≈ 1 + q * (Kt - 1) where q = notch sensitivity.
    pub fn fatigue_reduction_factor(&self) -> f64 {
        let ra_um = self.ra_total_um();
        // Empirical: Kf ≈ 1 + 0.06 * Ra_um for metals
        1.0 + 0.06 * ra_um
    }
}

// ---------------------------------------------------------------------------
// IN718 Specific Model
// ---------------------------------------------------------------------------

/// IN718 nickel superalloy properties for SLM/EBM.
#[derive(Debug, Clone)]
pub struct In718Properties {
    /// Gamma-prime (γ') volume fraction.
    pub gamma_prime_fraction: f64,
    /// Gamma-double-prime (γ'') volume fraction.
    pub gamma_double_prime_fraction: f64,
    /// Delta phase volume fraction.
    pub delta_fraction: f64,
    /// Creep coefficient (power-law).
    pub creep_coefficient: f64,
    /// Creep exponent n.
    pub creep_exponent: f64,
    /// Creep activation energy (J/mol).
    pub creep_activation_energy: f64,
}

impl In718Properties {
    /// Typical as-built SLM IN718 properties.
    pub fn as_built_slm() -> Self {
        Self {
            gamma_prime_fraction: 0.03,
            gamma_double_prime_fraction: 0.12,
            delta_fraction: 0.01,
            creep_coefficient: 2.5e-24,
            creep_exponent: 5.2,
            creep_activation_energy: 285_000.0,
        }
    }

    /// Typical heat-treated IN718 properties (solution annealed + aged).
    pub fn heat_treated() -> Self {
        Self {
            gamma_prime_fraction: 0.05,
            gamma_double_prime_fraction: 0.18,
            delta_fraction: 0.005,
            creep_coefficient: 1.5e-24,
            creep_exponent: 5.2,
            creep_activation_energy: 290_000.0,
        }
    }

    /// Estimate yield strength contribution from precipitates (Hall-Petch-like).
    ///
    /// Δσ ≈ M G b √ρ  simplified to ≈ C * (f_prime + f_doubleprime)^0.5
    pub fn precipitation_strengthening(&self) -> f64 {
        let c = 2000e6; // empirical constant
        c * (self.gamma_prime_fraction + self.gamma_double_prime_fraction).sqrt()
    }

    /// Minimum creep rate (s⁻¹) at stress σ and temperature T.
    ///
    /// Norton power law: ε̇ = A σⁿ exp(-Q / RT).
    pub fn creep_rate(&self, stress_pa: f64, temp_k: f64) -> f64 {
        self.creep_coefficient
            * stress_pa.powf(self.creep_exponent)
            * (-self.creep_activation_energy / (R_GAS * temp_k)).exp()
    }
}

// ---------------------------------------------------------------------------
// Energy Density Parameter Space
// ---------------------------------------------------------------------------

/// Process window analysis based on volumetric energy density.
#[derive(Debug, Clone)]
pub struct ProcessWindow {
    /// Minimum energy density for full melting (J/m³).
    pub ev_min: f64,
    /// Maximum energy density before keyhole porosity (J/m³).
    pub ev_max: f64,
    /// Optimal energy density for maximum density (J/m³).
    pub ev_optimal: f64,
}

impl ProcessWindow {
    /// Default process window for Ti-6Al-4V SLM (J/mm³ converted to J/m³).
    pub fn ti6al4v_slm() -> Self {
        Self {
            ev_min: 50e9,
            ev_max: 130e9,
            ev_optimal: 75e9,
        }
    }

    /// Default process window for 316L SLM.
    pub fn steel_316l_slm() -> Self {
        Self {
            ev_min: 45e9,
            ev_max: 120e9,
            ev_optimal: 70e9,
        }
    }

    /// Check whether given energy density is within the process window.
    pub fn is_in_window(&self, ev: f64) -> bool {
        ev >= self.ev_min && ev <= self.ev_max
    }

    /// Estimated relative density at energy density `ev`.
    pub fn estimated_relative_density(&self, ev: f64) -> f64 {
        if ev < self.ev_min {
            // Lack-of-fusion regime
            let frac = ev / self.ev_min;
            0.80 + 0.18 * frac
        } else if ev > self.ev_max {
            // Keyhole regime
            let excess = (ev - self.ev_max) / self.ev_max;
            0.999 - 0.15 * excess * excess
        } else {
            // In-window
            let x = (ev - self.ev_optimal).abs() / (self.ev_max - self.ev_min);
            0.999 - 0.05 * x * x
        }
        .clamp(0.0, 1.0)
    }
}

// ---------------------------------------------------------------------------
// Hardness Prediction
// ---------------------------------------------------------------------------

/// Vickers hardness (HV) from relative density and microstructure.
///
/// Simple linear model: HV = HV_dense * (1 − k_p * porosity)
pub fn vickers_hardness(hv_dense: f64, porosity: f64, k_p: f64) -> f64 {
    hv_dense * (1.0 - k_p * porosity.clamp(0.0, 1.0))
}

// ---------------------------------------------------------------------------
// Thermal Stress from Build Simulation
// ---------------------------------------------------------------------------

/// Layer-by-layer thermal stress accumulation.
///
/// Each deposited layer imposes a thermal strain `α ΔT` on the underlying body.
/// The cumulative residual stress is computed via a simple analytical laminate model.
pub fn cumulative_residual_stress(
    layer_temps: &[f64],
    ambient_temp: f64,
    thermal_expansion: f64,
    elastic_modulus: f64,
    poisson_ratio: f64,
) -> Vec<f64> {
    layer_temps
        .iter()
        .map(|&t| {
            let delta_t = (t - ambient_temp).abs();

            elastic_modulus * thermal_expansion * delta_t / (1.0 - poisson_ratio)
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // --- PbfMaterial ---

    #[test]
    fn test_ti6al4v_thermal_diffusivity_positive() {
        let m = PbfMaterial::ti6al4v_slm();
        assert!(m.thermal_diffusivity() > 0.0);
    }

    #[test]
    fn test_ti6al4v_melting_range_positive() {
        let m = PbfMaterial::ti6al4v_slm();
        assert!(m.melting_range() > 0.0);
    }

    #[test]
    fn test_steel_density_reasonable() {
        let m = PbfMaterial::steel_316l_slm();
        assert!(m.density > 7_000.0 && m.density < 9_000.0);
    }

    #[test]
    fn test_alsi10mg_absorptivity_range() {
        let m = PbfMaterial::alsi10mg_slm();
        assert!(m.absorptivity > 0.0 && m.absorptivity <= 1.0);
    }

    // --- PbfProcessParams ---

    #[test]
    fn test_volumetric_energy_density_positive() {
        let p = PbfProcessParams {
            power: 200.0,
            scan_speed: 0.8,
            hatch_spacing: 110e-6,
            layer_thickness: 30e-6,
            spot_radius: 35e-6,
            preheat_temp: 300.0,
            rotation_angle_deg: 67.0,
        };
        let ev = p.volumetric_energy_density();
        assert!(ev > 0.0 && ev.is_finite());
    }

    #[test]
    fn test_linear_energy_density_consistent() {
        let p = PbfProcessParams {
            power: 200.0,
            scan_speed: 1.0,
            hatch_spacing: 100e-6,
            layer_thickness: 30e-6,
            spot_radius: 35e-6,
            preheat_temp: 300.0,
            rotation_angle_deg: 67.0,
        };
        assert!((p.linear_energy_density() - 200.0).abs() < 1e-6);
    }

    #[test]
    fn test_interaction_time_positive() {
        let p = PbfProcessParams {
            power: 200.0,
            scan_speed: 1.0,
            hatch_spacing: 100e-6,
            layer_thickness: 30e-6,
            spot_radius: 35e-6,
            preheat_temp: 300.0,
            rotation_angle_deg: 67.0,
        };
        assert!(p.interaction_time() > 0.0);
    }

    // --- MeltPoolGeometry ---

    #[test]
    fn test_melt_pool_volume_positive() {
        let mat = PbfMaterial::ti6al4v_slm();
        let params = PbfProcessParams {
            power: 200.0,
            scan_speed: 0.5,
            hatch_spacing: 110e-6,
            layer_thickness: 30e-6,
            spot_radius: 35e-6,
            preheat_temp: 300.0,
            rotation_angle_deg: 67.0,
        };
        let mp = MeltPoolGeometry::eagar_tsai(&mat, &params, 300.0);
        assert!(mp.volume() > 0.0);
    }

    #[test]
    fn test_melt_pool_aspect_ratio_ge_1() {
        let mat = PbfMaterial::ti6al4v_slm();
        let params = PbfProcessParams {
            power: 200.0,
            scan_speed: 0.5,
            hatch_spacing: 110e-6,
            layer_thickness: 30e-6,
            spot_radius: 35e-6,
            preheat_temp: 300.0,
            rotation_angle_deg: 67.0,
        };
        let mp = MeltPoolGeometry::eagar_tsai(&mat, &params, 300.0);
        assert!(mp.aspect_ratio() >= 1.0);
    }

    // --- ThermalGradient ---

    #[test]
    fn test_thermal_gradient_positive() {
        let mat = PbfMaterial::ti6al4v_slm();
        let params = PbfProcessParams {
            power: 200.0,
            scan_speed: 0.5,
            hatch_spacing: 110e-6,
            layer_thickness: 30e-6,
            spot_radius: 35e-6,
            preheat_temp: 300.0,
            rotation_angle_deg: 67.0,
        };
        let tg = ThermalGradient::from_rosenthal(&mat, &params, 300.0);
        assert!(tg.gradient_magnitude > 0.0);
        assert!(tg.cooling_rate > 0.0);
    }

    #[test]
    fn test_g_times_r_positive() {
        let mat = PbfMaterial::ti6al4v_slm();
        let params = PbfProcessParams {
            power: 200.0,
            scan_speed: 0.5,
            hatch_spacing: 110e-6,
            layer_thickness: 30e-6,
            spot_radius: 35e-6,
            preheat_temp: 300.0,
            rotation_angle_deg: 67.0,
        };
        let tg = ThermalGradient::from_rosenthal(&mat, &params, 300.0);
        assert!(tg.g_times_r() > 0.0);
    }

    // --- ResidualStressModel ---

    #[test]
    fn test_residual_stress_nonnegative() {
        let mat = PbfMaterial::ti6al4v_slm();
        let rs = ResidualStressModel::from_material(&mat);
        let sigma = rs.peak_residual_stress(500.0);
        assert!(sigma >= 0.0);
    }

    #[test]
    fn test_residual_stress_capped_at_yield() {
        let mat = PbfMaterial::ti6al4v_slm();
        let rs = ResidualStressModel::from_material(&mat);
        let sigma = rs.peak_residual_stress(10_000.0); // very large delta T
        assert!(sigma <= rs.yield_strength_hot + 1.0);
    }

    #[test]
    fn test_stoney_curvature_increases_with_stress() {
        let mat = PbfMaterial::ti6al4v_slm();
        let rs = ResidualStressModel::from_material(&mat);
        let k1 = rs.stoney_curvature(100e6, 30e-6, 10e-3);
        let k2 = rs.stoney_curvature(200e6, 30e-6, 10e-3);
        assert!(k2 > k1);
    }

    // --- PorosityModel ---

    #[test]
    fn test_porosity_zero_gives_full_properties() {
        let pm = PorosityModel {
            e0: 114e9,
            yield_strength0: 930e6,
            tensile_strength0: 1000e6,
            density0: 4430.0,
        };
        assert!((pm.elastic_modulus(0.0) - pm.e0).abs() < 1e-3 * pm.e0);
        assert!((pm.yield_strength(0.0) - pm.yield_strength0).abs() < 1e-6);
    }

    #[test]
    fn test_porosity_modulus_decreasing() {
        let pm = PorosityModel {
            e0: 114e9,
            yield_strength0: 930e6,
            tensile_strength0: 1000e6,
            density0: 4430.0,
        };
        let e1 = pm.elastic_modulus(0.01);
        let e2 = pm.elastic_modulus(0.05);
        assert!(e1 > e2);
    }

    #[test]
    fn test_fatigue_reduction_ge_1() {
        let pm = PorosityModel {
            e0: 114e9,
            yield_strength0: 930e6,
            tensile_strength0: 1000e6,
            density0: 4430.0,
        };
        assert!(pm.fatigue_strength_reduction(0.02) >= 1.0);
    }

    // --- GrainGrowthModel ---

    #[test]
    fn test_grain_growth_increases_with_time() {
        let gg = GrainGrowthModel::ti6al4v_beta();
        let d1 = gg.grain_diameter_isothermal(1200.0, 60.0);
        let d2 = gg.grain_diameter_isothermal(1200.0, 3600.0);
        assert!(d2 > d1);
    }

    #[test]
    fn test_grain_growth_initial_diameter_nonnegative() {
        let gg = GrainGrowthModel::ti6al4v_beta();
        let d = gg.grain_diameter_isothermal(300.0, 0.0);
        assert!(d >= 0.0);
    }

    // --- MartensiticModel ---

    #[test]
    fn test_martensite_below_ms() {
        let m = MartensiticModel::ti6al4v_martensite();
        let f = m.martensite_fraction(600.0);
        assert!(f > 0.0 && f <= 1.0);
    }

    #[test]
    fn test_martensite_above_ms_is_zero() {
        let m = MartensiticModel::ti6al4v_martensite();
        let f = m.martensite_fraction(1000.0);
        assert_eq!(f, 0.0);
    }

    #[test]
    fn test_martensite_increases_cooling() {
        let m = MartensiticModel::ti6al4v_martensite();
        let f1 = m.martensite_fraction(800.0);
        let f2 = m.martensite_fraction(600.0);
        assert!(f2 > f1);
    }

    // --- BinderJettingMaterial ---

    #[test]
    fn test_bj_volumetric_shrinkage_positive() {
        let bj = BinderJettingMaterial::steel_316l_bj();
        assert!(bj.volumetric_shrinkage() > 0.0);
    }

    #[test]
    fn test_bj_relative_density_high() {
        let bj = BinderJettingMaterial::steel_316l_bj();
        assert!(bj.relative_density() > 0.95);
    }

    #[test]
    fn test_bj_effective_yield_strength_positive() {
        let bj = BinderJettingMaterial::steel_316l_bj();
        assert!(bj.effective_yield_strength() > 0.0);
    }

    // --- FdmPolymerMaterial ---

    #[test]
    fn test_fdm_anisotropy_ratio_range() {
        let fdm = FdmPolymerMaterial::pla_generic();
        let r = fdm.anisotropy_ratio();
        assert!(r > 0.0 && r <= 1.0);
    }

    #[test]
    fn test_fdm_tensile_at_0_equals_xy() {
        let fdm = FdmPolymerMaterial::pla_generic();
        let sigma = fdm.tensile_strength_at_angle(0.0);
        assert!((sigma - fdm.tensile_strength_xy).abs() < 1.0);
    }

    #[test]
    fn test_fdm_tensile_at_90_equals_z() {
        let fdm = FdmPolymerMaterial::pla_generic();
        let sigma = fdm.tensile_strength_at_angle(90.0);
        assert!((sigma - fdm.tensile_strength_z).abs() < 1.0);
    }

    // --- ScanStrategyEffect ---

    #[test]
    fn test_scan_67_lower_texture_than_unidirectional() {
        let uni = ScanStrategyEffect::unidirectional();
        let rot = ScanStrategyEffect::rotating_67();
        assert!(rot.texture_coefficient < uni.texture_coefficient);
    }

    #[test]
    fn test_scan_67_lower_stress_than_unidirectional() {
        let uni = ScanStrategyEffect::unidirectional();
        let rot = ScanStrategyEffect::rotating_67();
        assert!(rot.residual_stress_factor < uni.residual_stress_factor);
    }

    #[test]
    fn test_unique_orientations_unidirectional() {
        let uni = ScanStrategyEffect::unidirectional();
        assert_eq!(uni.unique_orientations_in_n_layers(100), 1);
    }

    // --- AmSurfaceRoughness ---

    #[test]
    fn test_surface_roughness_ra_positive() {
        let r = AmSurfaceRoughness {
            layer_thickness: 30e-6,
            powder_d50: 30e-6,
            build_angle_deg: 45.0,
            spot_radius: 35e-6,
        };
        assert!(r.ra_total() > 0.0);
    }

    #[test]
    fn test_surface_roughness_increases_with_layer_thickness() {
        let r1 = AmSurfaceRoughness {
            layer_thickness: 30e-6,
            powder_d50: 30e-6,
            build_angle_deg: 45.0,
            spot_radius: 35e-6,
        };
        let r2 = AmSurfaceRoughness {
            layer_thickness: 60e-6,
            powder_d50: 30e-6,
            build_angle_deg: 45.0,
            spot_radius: 35e-6,
        };
        assert!(r2.ra_total() > r1.ra_total());
    }

    #[test]
    fn test_surface_fatigue_reduction_ge_1() {
        let r = AmSurfaceRoughness {
            layer_thickness: 30e-6,
            powder_d50: 30e-6,
            build_angle_deg: 45.0,
            spot_radius: 35e-6,
        };
        assert!(r.fatigue_reduction_factor() >= 1.0);
    }

    // --- PSP linkage ---

    #[test]
    fn test_psp_relative_density_in_range() {
        let mat = PbfMaterial::ti6al4v_slm();
        let params = PbfProcessParams {
            power: 200.0,
            scan_speed: 0.7,
            hatch_spacing: 110e-6,
            layer_thickness: 30e-6,
            spot_radius: 35e-6,
            preheat_temp: 300.0,
            rotation_angle_deg: 67.0,
        };
        let psp = PspLinkage::new(mat, params);
        let rd = psp.relative_density();
        assert!(rd > 0.0 && rd <= 1.0);
    }

    #[test]
    fn test_psp_effective_yield_positive() {
        let mat = PbfMaterial::ti6al4v_slm();
        let params = PbfProcessParams {
            power: 200.0,
            scan_speed: 0.7,
            hatch_spacing: 110e-6,
            layer_thickness: 30e-6,
            spot_radius: 35e-6,
            preheat_temp: 300.0,
            rotation_angle_deg: 67.0,
        };
        let psp = PspLinkage::new(mat, params);
        assert!(psp.effective_yield_strength() > 0.0);
    }

    // --- ProcessWindow ---

    #[test]
    fn test_process_window_in_range() {
        let pw = ProcessWindow::ti6al4v_slm();
        assert!(pw.is_in_window(pw.ev_optimal));
    }

    #[test]
    fn test_process_window_out_of_range_low() {
        let pw = ProcessWindow::ti6al4v_slm();
        assert!(!pw.is_in_window(pw.ev_min * 0.5));
    }

    #[test]
    fn test_relative_density_at_optimal_near_1() {
        let pw = ProcessWindow::ti6al4v_slm();
        let rd = pw.estimated_relative_density(pw.ev_optimal);
        assert!(rd > 0.99);
    }

    // --- In718 ---

    #[test]
    fn test_in718_precipitation_strengthening_positive() {
        let props = In718Properties::as_built_slm();
        assert!(props.precipitation_strengthening() > 0.0);
    }

    #[test]
    fn test_in718_creep_rate_positive() {
        let props = In718Properties::heat_treated();
        let cr = props.creep_rate(500e6, 923.0);
        assert!(cr > 0.0);
    }

    // --- Miscellaneous ---

    #[test]
    fn test_vickers_hardness_decreases_with_porosity() {
        let hv1 = vickers_hardness(350.0, 0.0, 3.0);
        let hv2 = vickers_hardness(350.0, 0.05, 3.0);
        assert!(hv1 > hv2);
    }

    #[test]
    fn test_cumulative_residual_stress_count() {
        let temps = vec![1000.0, 900.0, 800.0];
        let stresses = cumulative_residual_stress(&temps, 300.0, 8.6e-6, 114e9, 0.342);
        assert_eq!(stresses.len(), 3);
    }

    #[test]
    fn test_estimate_porosity_in_window_is_low() {
        let p = estimate_porosity_from_energy_density(70e9, 4430.0);
        assert!(p < 0.05);
    }

    #[test]
    fn test_support_thermal_resistance_finite() {
        let s = SupportMaterial::metal_block_support();
        let r = s.thermal_resistance(5e-3, 1e-4, 20.0);
        assert!(r.is_finite() && r > 0.0);
    }
}