oxiphysics-fem 0.1.2

Finite element method 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
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Extended contact mechanics: Hertz sphere/cylinder variants, JKR/DMT adhesion,
//! loading/unloading hysteresis, Greenwood-Williamson rough surface contact,
//! and flash temperature (Jaeger's formula).

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

// ---------------------------------------------------------------------------
// 1. Elastic material primitives
// ---------------------------------------------------------------------------

/// Reduced (combined) modulus E* from two elastic solids.
///
/// 1/E* = (1 - ν₁²)/E₁ + (1 - ν₂²)/E₂
pub fn reduced_modulus(e1: f64, nu1: f64, e2: f64, nu2: f64) -> f64 {
    let inv = (1.0 - nu1 * nu1) / e1 + (1.0 - nu2 * nu2) / e2;
    1.0 / inv
}

/// Combined radius R* from two radii (use `f64::INFINITY` for flat).
///
/// 1/R* = 1/R₁ + 1/R₂
pub fn combined_radius(r1: f64, r2: f64) -> f64 {
    1.0 / (1.0 / r1 + 1.0 / r2)
}

// ---------------------------------------------------------------------------
// 2. Hertz sphere-sphere contact
// ---------------------------------------------------------------------------

/// Hertz sphere–sphere (or sphere–flat) contact model.
///
/// Encapsulates the standard Hertz results for a circular contact patch.
#[derive(Debug, Clone)]
pub struct HertzSphereSphere {
    /// Effective radius R* (m).
    pub r_star: f64,
    /// Reduced modulus E* (Pa).
    pub e_star: f64,
}

impl HertzSphereSphere {
    /// Construct from individual sphere radii and material properties.
    pub fn new(r1: f64, nu1: f64, e1: f64, r2: f64, nu2: f64, e2: f64) -> Self {
        let r_star = combined_radius(r1, r2);
        let e_star = reduced_modulus(e1, nu1, e2, nu2);
        Self { r_star, e_star }
    }

    /// Contact radius a = (3 F R* / (4 E*))^(1/3).
    pub fn contact_radius(&self, force: f64) -> f64 {
        (3.0 * force * self.r_star / (4.0 * self.e_star)).cbrt()
    }

    /// Indentation depth δ = a² / R*.
    pub fn indentation(&self, force: f64) -> f64 {
        let a = self.contact_radius(force);
        a * a / self.r_star
    }

    /// Peak Hertz pressure p₀ = (6 F E*² / (π³ R*²))^(1/3).
    pub fn peak_pressure(&self, force: f64) -> f64 {
        (6.0 * force * self.e_star * self.e_star / (PI * PI * PI * self.r_star * self.r_star))
            .cbrt()
    }

    /// Mean Hertz pressure p_mean = F / (π a²).
    pub fn mean_pressure(&self, force: f64) -> f64 {
        let a = self.contact_radius(force);
        if a < 1e-30 {
            return 0.0;
        }
        force / (PI * a * a)
    }

    /// Hertz normal contact stiffness k_n = 2 E* a.
    pub fn contact_stiffness(&self, force: f64) -> f64 {
        2.0 * self.e_star * self.contact_radius(force)
    }

    /// Force from indentation: F = (4/3) E* √R* δ^(3/2).
    pub fn force_from_indentation(&self, delta: f64) -> f64 {
        if delta <= 0.0 {
            return 0.0;
        }
        (4.0 / 3.0) * self.e_star * self.r_star.sqrt() * delta.powf(1.5)
    }

    /// Hertz pressure distribution p(r) = p₀ √(1 - (r/a)²) for r ≤ a.
    pub fn pressure_at_radius(&self, r: f64, force: f64) -> f64 {
        let a = self.contact_radius(force);
        if r > a {
            return 0.0;
        }
        let p0 = self.peak_pressure(force);
        let ratio = r / a;
        p0 * (1.0 - ratio * ratio).max(0.0).sqrt()
    }
}

// ---------------------------------------------------------------------------
// 3. Hertz sphere-flat contact (specialised)
// ---------------------------------------------------------------------------

/// Hertz contact between a sphere and a flat surface.
///
/// Equivalent to `HertzSphereSphere` with R₂ = ∞, kept separately for clarity.
#[derive(Debug, Clone)]
pub struct HertzSphereFlat {
    /// Sphere radius (m).
    pub sphere_radius: f64,
    /// Reduced modulus E* (Pa).
    pub e_star: f64,
}

impl HertzSphereFlat {
    /// Construct from sphere geometry and bimaterial properties.
    pub fn new(r_sphere: f64, e_sphere: f64, nu_sphere: f64, e_flat: f64, nu_flat: f64) -> Self {
        let e_star = reduced_modulus(e_sphere, nu_sphere, e_flat, nu_flat);
        Self {
            sphere_radius: r_sphere,
            e_star,
        }
    }

    /// Contact radius a = (3 F R / (4 E*))^(1/3).
    pub fn contact_radius(&self, force: f64) -> f64 {
        (3.0 * force * self.sphere_radius / (4.0 * self.e_star)).cbrt()
    }

    /// Indentation depth δ = a² / R.
    pub fn indentation(&self, force: f64) -> f64 {
        let a = self.contact_radius(force);
        a * a / self.sphere_radius
    }

    /// Peak pressure p₀ = 3 F / (2 π a²).
    pub fn peak_pressure(&self, force: f64) -> f64 {
        let a = self.contact_radius(force);
        if a < 1e-30 {
            return 0.0;
        }
        3.0 * force / (2.0 * PI * a * a)
    }

    /// Elastic strain energy U = (2/5) F δ.
    pub fn strain_energy(&self, force: f64) -> f64 {
        0.4 * force * self.indentation(force)
    }
}

// ---------------------------------------------------------------------------
// 4. Hertz cylinder-flat (line contact)
// ---------------------------------------------------------------------------

/// Hertz line contact between an infinite cylinder and a flat surface.
///
/// Results are per unit length along the cylinder axis.
#[derive(Debug, Clone)]
pub struct HertzCylinderFlat {
    /// Cylinder radius (m).
    pub cylinder_radius: f64,
    /// Reduced plane-strain modulus E* (Pa).
    pub e_star: f64,
}

impl HertzCylinderFlat {
    /// Construct from cylinder geometry and bimaterial properties.
    pub fn new(r_cyl: f64, e_cyl: f64, nu_cyl: f64, e_flat: f64, nu_flat: f64) -> Self {
        let e_star = reduced_modulus(e_cyl, nu_cyl, e_flat, nu_flat);
        Self {
            cylinder_radius: r_cyl,
            e_star,
        }
    }

    /// Contact half-width b = √(4 F' R / (π E*)) where F' is force per unit length.
    pub fn half_width(&self, force_per_length: f64) -> f64 {
        (4.0 * force_per_length * self.cylinder_radius / (PI * self.e_star)).sqrt()
    }

    /// Peak pressure p₀ = 2 F' / (π b).
    pub fn peak_pressure(&self, force_per_length: f64) -> f64 {
        let b = self.half_width(force_per_length);
        if b < 1e-30 {
            return 0.0;
        }
        2.0 * force_per_length / (PI * b)
    }

    /// Mean pressure p_mean = F' / (2 b).
    pub fn mean_pressure(&self, force_per_length: f64) -> f64 {
        let b = self.half_width(force_per_length);
        if b < 1e-30 {
            return 0.0;
        }
        force_per_length / (2.0 * b)
    }

    /// Ellipsoidal pressure distribution p(x) = p₀ √(1 - (x/b)²) for |x| ≤ b.
    pub fn pressure_at_position(&self, x: f64, force_per_length: f64) -> f64 {
        let b = self.half_width(force_per_length);
        if x.abs() > b {
            return 0.0;
        }
        let p0 = self.peak_pressure(force_per_length);
        p0 * (1.0 - (x / b).powi(2)).max(0.0).sqrt()
    }

    /// Subsurface maximum shear stress τ_max ≈ 0.300 p₀ at depth z ≈ 0.786 b.
    pub fn max_shear_stress(&self, force_per_length: f64) -> f64 {
        0.300 * self.peak_pressure(force_per_length)
    }

    /// Depth at which the maximum shear stress occurs: z ≈ 0.786 b.
    pub fn max_shear_depth(&self, force_per_length: f64) -> f64 {
        0.786 * self.half_width(force_per_length)
    }
}

// ---------------------------------------------------------------------------
// 5. Mindlin tangential compliance
// ---------------------------------------------------------------------------

/// Mindlin reduced shear modulus G* from two elastic solids.
///
/// 1/G* = (2 - ν₁)/(4 G₁) + (2 - ν₂)/(4 G₂)
pub fn mindlin_reduced_shear_modulus(g1: f64, nu1: f64, g2: f64, nu2: f64) -> f64 {
    1.0 / ((2.0 - nu1) / (4.0 * g1) + (2.0 - nu2) / (4.0 * g2))
}

/// Mindlin tangential compliance model.
///
/// Builds on top of a Hertz contact to provide tangential stiffness and
/// the partial-slip (no-slip) solution for a given Coulomb friction limit.
#[derive(Debug, Clone)]
pub struct MindlinCompliance {
    /// Reduced shear modulus G* (Pa).
    pub g_star: f64,
    /// Coefficient of friction μ.
    pub friction: f64,
}

impl MindlinCompliance {
    /// Construct from two elastic solids (given E, ν) and friction coefficient.
    pub fn new(e1: f64, nu1: f64, e2: f64, nu2: f64, friction: f64) -> Self {
        let g1 = e1 / (2.0 * (1.0 + nu1));
        let g2 = e2 / (2.0 * (1.0 + nu2));
        let g_star = mindlin_reduced_shear_modulus(g1, nu1, g2, nu2);
        Self { g_star, friction }
    }

    /// Tangential stiffness k_t = 8 G* a.
    pub fn tangential_stiffness(&self, contact_radius: f64) -> f64 {
        8.0 * self.g_star * contact_radius
    }

    /// Mindlin partial-slip tangential displacement (loading from 0).
    ///
    /// δ_t = (3 μ F_n)/(8 G* a) \[1 - (1 - Q/(μ F_n))^(2/3)\]
    ///
    /// Returns `None` if gross slip (Q ≥ μ F_n).
    pub fn partial_slip_displacement(
        &self,
        tangential_force: f64,
        normal_force: f64,
        contact_radius: f64,
    ) -> Option<f64> {
        let limit = self.friction * normal_force;
        if tangential_force >= limit {
            return None;
        }
        let prefactor = 3.0 * limit / (8.0 * self.g_star * contact_radius);
        let term = 1.0 - tangential_force / limit;
        Some(prefactor * (1.0 - term.powf(2.0 / 3.0)))
    }

    /// Inverse: force from displacement (partial-slip).
    pub fn force_from_displacement(
        &self,
        delta_t: f64,
        normal_force: f64,
        contact_radius: f64,
    ) -> f64 {
        let limit = self.friction * normal_force;
        let prefactor = 3.0 * limit / (8.0 * self.g_star * contact_radius);
        let ratio = (delta_t / prefactor).clamp(0.0, 1.0);
        limit * (1.0 - (1.0 - ratio).powf(1.5))
    }

    /// Stick-zone radius c = a (1 - Q/(μ F_n))^(1/3).
    pub fn stick_radius(
        &self,
        tangential_force: f64,
        normal_force: f64,
        contact_radius: f64,
    ) -> Option<f64> {
        let limit = self.friction * normal_force;
        if tangential_force >= limit || limit < 1e-30 {
            return None;
        }
        Some(contact_radius * (1.0 - tangential_force / limit).powf(1.0 / 3.0))
    }

    /// Energy dissipated per fretting cycle (Mindlin–Deresiewicz).
    ///
    /// W = (9 μ² F_n²)/(10 k_t) \[1 - (1 - Q/(μ F_n))^(5/3)\]
    pub fn fretting_energy_per_cycle(
        &self,
        tangential_amplitude: f64,
        normal_force: f64,
        contact_radius: f64,
    ) -> f64 {
        let limit = self.friction * normal_force;
        if limit < 1e-30 {
            return 0.0;
        }
        let k_t = self.tangential_stiffness(contact_radius);
        if k_t < 1e-30 {
            return 0.0;
        }
        let ratio = (tangential_amplitude / limit).min(1.0);
        (9.0 * limit * limit) / (10.0 * k_t) * (1.0 - (1.0 - ratio).powf(5.0 / 3.0))
    }
}

// ---------------------------------------------------------------------------
// 6. JKR adhesion model
// ---------------------------------------------------------------------------

/// Johnson–Kendall–Roberts (JKR) adhesion model.
///
/// Extends Hertz contact to include surface energy (adhesion work W_ad).
/// The contact area is larger than Hertz and pull-off occurs at a negative
/// load F_po = -(3/2) π W_ad R*.
#[derive(Debug, Clone)]
pub struct JkrAdhesion {
    /// Effective radius R* (m).
    pub r_star: f64,
    /// Reduced modulus E* (Pa).
    pub e_star: f64,
    /// Adhesion work (Dupré energy) W_ad = γ₁ + γ₂ − γ₁₂ (J/m²).
    pub work_adhesion: f64,
}

impl JkrAdhesion {
    /// Construct JKR model.
    pub fn new(r_star: f64, e_star: f64, work_adhesion: f64) -> Self {
        Self {
            r_star,
            e_star,
            work_adhesion,
        }
    }

    /// JKR contact radius as a function of applied load F.
    ///
    /// a³ = (R*/(E*)) \[F + 3πW R* + √(6πW R* F + (3πW R*)²)\]
    /// where W = W_ad.
    pub fn contact_radius(&self, force: f64) -> f64 {
        let w = self.work_adhesion;
        let r = self.r_star;
        let e_star = self.e_star;
        let term = 3.0 * PI * w * r;
        let discriminant = 6.0 * PI * w * r * force + term * term;
        let a3 = (r / e_star) * (force + term + discriminant.max(0.0).sqrt());
        a3.cbrt()
    }

    /// JKR pull-off (adhesion) force: F_po = -(3/2) π W_ad R*.
    pub fn pull_off_force(&self) -> f64 {
        -1.5 * PI * self.work_adhesion * self.r_star
    }

    /// Indentation at zero applied load (contact due to adhesion only).
    pub fn zero_load_contact_radius(&self) -> f64 {
        self.contact_radius(0.0)
    }

    /// Elastic energy stored in the adhesive contact at load F.
    pub fn elastic_energy(&self, force: f64) -> f64 {
        let a = self.contact_radius(force);
        let delta =
            a * a / self.r_star - (8.0 * PI * self.work_adhesion * a / (3.0 * self.e_star)).sqrt();
        if delta < 0.0 {
            return 0.0;
        }
        (4.0 / 3.0) * self.e_star * self.r_star.sqrt() * delta.powf(1.5)
    }

    /// Surface energy gain −W_ad π a².
    pub fn surface_energy(&self, force: f64) -> f64 {
        let a = self.contact_radius(force);
        -self.work_adhesion * PI * a * a
    }
}

// ---------------------------------------------------------------------------
// 7. DMT adhesion model
// ---------------------------------------------------------------------------

/// Derjaguin–Muller–Toporov (DMT) adhesion model.
///
/// Valid for stiff materials / weak adhesion (small Tabor parameter).
/// The contact area follows Hertz mechanics but with an effective load
/// F_eff = F + 2 π W_ad R*.
#[derive(Debug, Clone)]
pub struct DmtAdhesion {
    /// Effective radius R* (m).
    pub r_star: f64,
    /// Reduced modulus E* (Pa).
    pub e_star: f64,
    /// Adhesion work W_ad (J/m²).
    pub work_adhesion: f64,
}

impl DmtAdhesion {
    /// Construct DMT model.
    pub fn new(r_star: f64, e_star: f64, work_adhesion: f64) -> Self {
        Self {
            r_star,
            e_star,
            work_adhesion,
        }
    }

    /// DMT pull-off force: F_po = -2 π W_ad R*.
    pub fn pull_off_force(&self) -> f64 {
        -2.0 * PI * self.work_adhesion * self.r_star
    }

    /// DMT effective load: F_eff = F + 2 π W_ad R*.
    fn effective_force(&self, force: f64) -> f64 {
        force + 2.0 * PI * self.work_adhesion * self.r_star
    }

    /// Contact radius (Hertz with effective load).
    pub fn contact_radius(&self, force: f64) -> f64 {
        let f_eff = self.effective_force(force);
        if f_eff <= 0.0 {
            return 0.0;
        }
        (3.0 * f_eff * self.r_star / (4.0 * self.e_star)).cbrt()
    }

    /// Indentation depth δ = a² / R*.
    pub fn indentation(&self, force: f64) -> f64 {
        let a = self.contact_radius(force);
        a * a / self.r_star
    }

    /// Peak pressure at given applied load.
    pub fn peak_pressure(&self, force: f64) -> f64 {
        let a = self.contact_radius(force);
        if a < 1e-30 {
            return 0.0;
        }
        let f_eff = self.effective_force(force);
        3.0 * f_eff / (2.0 * PI * a * a)
    }

    /// Tabor parameter μ_T for choosing between JKR and DMT regime.
    ///
    /// μ_T = (R* W²_ad / (E*² z₀³))^(1/3)
    /// where z₀ is the equilibrium atomic separation (typ. 0.2–0.4 nm).
    pub fn tabor_parameter(&self, z0: f64) -> f64 {
        (self.r_star * self.work_adhesion * self.work_adhesion
            / (self.e_star * self.e_star * z0 * z0 * z0))
            .cbrt()
    }
}

// ---------------------------------------------------------------------------
// 8. Contact area hysteresis (loading / unloading)
// ---------------------------------------------------------------------------

/// Contact area evolution under loading and unloading cycles.
///
/// Uses the Hertz loading branch and a Maugis-type unloading correction
/// to capture the hysteresis in the contact area vs. force curve.
#[derive(Debug, Clone)]
pub struct ContactAreaHysteresis {
    /// Effective radius R* (m).
    pub r_star: f64,
    /// Reduced modulus E* (Pa).
    pub e_star: f64,
    /// Maximum force reached during loading (for unloading branch).
    pub f_max: f64,
    /// Contact radius at maximum force (for unloading branch).
    pub a_max: f64,
}

impl ContactAreaHysteresis {
    /// Construct from geometry.
    pub fn new(r_star: f64, e_star: f64) -> Self {
        Self {
            r_star,
            e_star,
            f_max: 0.0,
            a_max: 0.0,
        }
    }

    /// Hertz loading: update the peak state and return contact radius.
    pub fn load(&mut self, force: f64) -> f64 {
        let a = (3.0 * force * self.r_star / (4.0 * self.e_star)).cbrt();
        if force > self.f_max {
            self.f_max = force;
            self.a_max = a;
        }
        a
    }

    /// Simplified unloading branch: contact area decreases following the
    /// Maugis–Dugdale flat-punch correction.
    ///
    /// a_unload(F) = a_max * (F / F_max)^(1/3) \[Hertz\] + residual
    /// (simplified, omits detailed elastic hysteresis in full MDR).
    pub fn unload(&self, force: f64) -> f64 {
        if force <= 0.0 {
            return 0.0;
        }
        if self.f_max < 1e-30 {
            return 0.0;
        }
        let a_hertz = (3.0 * force * self.r_star / (4.0 * self.e_star)).cbrt();
        // Residual contact from elastic springback (Mindlin unloading correction).
        let a_residual = self.a_max * (1.0 - force / self.f_max).max(0.0).sqrt() * 0.05;
        a_hertz + a_residual
    }

    /// Hysteresis loop area (energy dissipated over a load-unload cycle).
    ///
    /// Approximated by numerical integration between load and unload curves.
    pub fn hysteresis_energy(&self, n_steps: usize) -> f64 {
        if self.f_max < 1e-30 || n_steps < 2 {
            return 0.0;
        }
        let df = self.f_max / (n_steps as f64 - 1.0);
        let mut energy = 0.0;
        for i in 1..n_steps {
            let f_prev = (i - 1) as f64 * df;
            let f_curr = i as f64 * df;
            let a_load_prev = (3.0 * f_prev * self.r_star / (4.0 * self.e_star)).cbrt();
            let a_load_curr = (3.0 * f_curr * self.r_star / (4.0 * self.e_star)).cbrt();
            let a_unload_prev = self.unload(f_prev);
            let a_unload_curr = self.unload(f_curr);
            let da_load = a_load_curr - a_load_prev;
            let da_unload = a_unload_curr - a_unload_prev;
            energy += 0.5 * (f_prev + f_curr) * (da_load - da_unload).abs();
        }
        energy
    }
}

// ---------------------------------------------------------------------------
// 9. Normal / tangential contact force from separation / overlap
// ---------------------------------------------------------------------------

/// Contact force model based on separation (positive = gap, negative = overlap).
///
/// Implements both linear and nonlinear (Hertz) normal force and Coulomb-limited
/// tangential force.
#[derive(Debug, Clone)]
pub struct ContactForceModel {
    /// Normal stiffness k_n (N/m) — used for linear model only.
    pub normal_stiffness: f64,
    /// Tangential stiffness k_t (N/m) — used for linear model only.
    pub tangential_stiffness: f64,
    /// Coefficient of friction.
    pub friction: f64,
    /// Damping coefficient (critical fraction), 0 = undamped.
    pub damping: f64,
}

impl ContactForceModel {
    /// Linear Hertz-like normal force from overlap δ = −gap.
    ///
    /// F_n = k_n * δ  (no adhesion).
    pub fn linear_normal_force(&self, gap: f64) -> f64 {
        if gap >= 0.0 {
            0.0
        } else {
            -gap * self.normal_stiffness
        }
    }

    /// Hertz nonlinear normal force from overlap:
    /// F_n = k_n * δ^(3/2).
    pub fn hertz_normal_force(&self, gap: f64) -> f64 {
        if gap >= 0.0 {
            0.0
        } else {
            let delta = -gap;
            self.normal_stiffness * delta.powf(1.5)
        }
    }

    /// Damped normal force: F_n + c_n * v_n where c_n = damping * 2 √(m k_n).
    pub fn damped_normal_force(&self, gap: f64, normal_velocity: f64, mass: f64) -> f64 {
        let fn_ = self.linear_normal_force(gap);
        if fn_ <= 0.0 {
            return 0.0;
        }
        let c = self.damping * 2.0 * (mass * self.normal_stiffness).sqrt();
        (fn_ - c * normal_velocity).max(0.0)
    }

    /// Coulomb-limited tangential force from tangential displacement.
    pub fn tangential_force(&self, tangential_disp: f64, normal_force: f64) -> f64 {
        let f_t_trial = self.tangential_stiffness * tangential_disp;
        let limit = self.friction * normal_force;
        if f_t_trial.abs() <= limit {
            f_t_trial
        } else {
            limit * f_t_trial.signum()
        }
    }

    /// Total contact force vector \[f_t, f_n\] for given gap and displacement.
    pub fn contact_force_vector(&self, gap: f64, tangential_disp: f64) -> [f64; 2] {
        let fn_ = self.linear_normal_force(gap);
        let ft = self.tangential_force(tangential_disp, fn_);
        [ft, fn_]
    }
}

// ---------------------------------------------------------------------------
// 10. Greenwood–Williamson rough surface contact
// ---------------------------------------------------------------------------

/// Greenwood–Williamson (GW) statistical rough-surface contact model.
///
/// Models a surface as a collection of spherical asperities with a Gaussian
/// height distribution.  Key outputs are the real contact area and total
/// normal force as a function of the mean separation.
#[derive(Debug, Clone)]
pub struct GreenwoodWilliamson {
    /// Asperity density η (number per m²).
    pub asperity_density: f64,
    /// Mean asperity tip radius β (m).
    pub asperity_radius: f64,
    /// Standard deviation of asperity heights σ_s (m).
    pub height_std: f64,
    /// Composite reduced modulus E* (Pa).
    pub e_star: f64,
}

impl GreenwoodWilliamson {
    /// Gaussian PDF φ(z) = exp(-z²/(2σ²)) / (σ √(2π)).
    fn phi(&self, z: f64) -> f64 {
        let s = self.height_std;
        (-0.5 * (z / s).powi(2)).exp() / (s * (2.0 * PI).sqrt())
    }

    /// Complementary CDF  P(Z > d) ≈ ½ erfc(d/(√2 σ)).
    fn prob_contact(&self, separation: f64) -> f64 {
        let t = separation / (self.height_std * 2.0_f64.sqrt());
        0.5 * gw_erfc(t)
    }

    /// Mean overlap E\[max(z − d, 0)\] under Gaussian pdf.
    fn mean_overlap(&self, separation: f64) -> f64 {
        let s = self.height_std;
        let t = separation / (s * 2.0_f64.sqrt());
        s / (2.0 * PI).sqrt() * (-t * t).exp() - separation * 0.5 * gw_erfc(t)
    }

    /// Mean overlap^(3/2) ⟨max(z − d, 0)^(3/2)⟩ via numerical quadrature.
    fn mean_overlap_3_2(&self, separation: f64) -> f64 {
        let s = self.height_std;
        let n = 200usize;
        let hi = separation + 8.0 * s;
        let dz = (hi - separation) / n as f64;
        let mut sum = 0.0;
        for i in 0..n {
            let z = separation + (i as f64 + 0.5) * dz;
            sum += (z - separation).powf(1.5) * self.phi(z) * dz;
        }
        sum
    }

    /// Real contact area per unit nominal area A_real / A_nominal.
    pub fn real_contact_area_fraction(&self, separation: f64) -> f64 {
        PI * self.asperity_density * self.asperity_radius * self.mean_overlap(separation)
    }

    /// Number of contacting asperities per unit nominal area.
    pub fn contact_asperity_density(&self, separation: f64) -> f64 {
        self.asperity_density * self.prob_contact(separation)
    }

    /// Mean contact pressure (Pa) for a given separation.
    pub fn contact_pressure(&self, separation: f64) -> f64 {
        (4.0 / 3.0)
            * self.asperity_density
            * self.e_star
            * self.asperity_radius.sqrt()
            * self.mean_overlap_3_2(separation)
    }

    /// Plasticity index ψ = (E*/H) √(σ/β).
    ///
    /// ψ < 0.6 elastic, ψ > 1.0 fully plastic.
    pub fn plasticity_index(&self, hardness: f64) -> f64 {
        if hardness < 1e-30 || self.asperity_radius < 1e-30 {
            return 0.0;
        }
        (self.e_star / hardness) * (self.height_std / self.asperity_radius).sqrt()
    }

    /// Separation d for a target contact pressure via bisection.
    ///
    /// Returns `None` if the target pressure is unachievable in \[0, 10σ\].
    pub fn separation_for_pressure(&self, target_pressure: f64, tol: f64) -> Option<f64> {
        let mut lo = 0.0_f64;
        let mut hi = 10.0 * self.height_std;
        // Ensure bracket is valid.
        if self.contact_pressure(lo) < target_pressure {
            return None;
        }
        for _ in 0..80 {
            let mid = 0.5 * (lo + hi);
            if self.contact_pressure(mid) > target_pressure {
                lo = mid;
            } else {
                hi = mid;
            }
            if (hi - lo) < tol {
                break;
            }
        }
        Some(0.5 * (lo + hi))
    }
}

/// Complementary error function erfc(x) via rational approximation (A&S 7.1.26).
fn gw_erfc(x: f64) -> f64 {
    if x < 0.0 {
        return 2.0 - gw_erfc(-x);
    }
    let t = 1.0 / (1.0 + 0.3275911 * x);
    let poly = t
        * (0.254829592
            + t * (-0.284496736 + t * (1.421413741 + t * (-1.453152027 + t * 1.061405429))));
    poly * (-x * x).exp()
}

// ---------------------------------------------------------------------------
// 11. Flash temperature (Jaeger's formula)
// ---------------------------------------------------------------------------

/// Flash temperature rise at a sliding contact (Jaeger 1942).
///
/// Estimates the peak surface temperature increment due to frictional heating.
#[derive(Debug, Clone)]
pub struct FlashTemperature {
    /// Thermal conductivity of body 1 k₁ (W/(m·K)).
    pub k1: f64,
    /// Thermal conductivity of body 2 k₂ (W/(m·K)).
    pub k2: f64,
    /// Thermal diffusivity of body 1 κ₁ (m²/s).
    pub kappa1: f64,
    /// Thermal diffusivity of body 2 κ₂ (m²/s).
    pub kappa2: f64,
}

impl FlashTemperature {
    /// Construct from material thermal properties.
    pub fn new(k1: f64, kappa1: f64, k2: f64, kappa2: f64) -> Self {
        Self {
            k1,
            k2,
            kappa1,
            kappa2,
        }
    }

    /// Peclet number Pe = V a / (2 κ) where V is sliding speed, a contact radius.
    pub fn peclet_number(sliding_speed: f64, contact_radius: f64, thermal_diffusivity: f64) -> f64 {
        sliding_speed * contact_radius / (2.0 * thermal_diffusivity)
    }

    /// Partition factor β₁ (fraction of heat entering body 1).
    ///
    /// β₁ = k₁ √κ₂ / (k₁ √κ₂ + k₂ √κ₁)
    pub fn heat_partition_factor(&self) -> f64 {
        let num = self.k1 * self.kappa2.sqrt();
        let den = num + self.k2 * self.kappa1.sqrt();
        if den < 1e-30 { 0.5 } else { num / den }
    }

    /// Jaeger flash temperature rise ΔT_flash (high-speed limit, Pe >> 1).
    ///
    /// ΔT = 0.308 μ F_n V / (a (k₁ + k₂) √Pe)
    ///
    /// where Pe is the Peclet number for the faster body.
    pub fn flash_temperature_high_speed(
        &self,
        friction: f64,
        normal_force: f64,
        sliding_speed: f64,
        contact_radius: f64,
    ) -> f64 {
        if contact_radius < 1e-30 || sliding_speed < 1e-30 {
            return 0.0;
        }
        let pe = Self::peclet_number(sliding_speed, contact_radius, self.kappa1);
        if pe < 1e-10 {
            return 0.0;
        }
        let q_dot = friction * normal_force * sliding_speed; // total frictional power
        0.308 * q_dot / (contact_radius * (self.k1 + self.k2) * pe.sqrt())
    }

    /// Jaeger flash temperature rise (low-speed limit, Pe << 1).
    ///
    /// ΔT = q_dot / (2 π a (k₁ + k₂))
    pub fn flash_temperature_low_speed(
        &self,
        friction: f64,
        normal_force: f64,
        sliding_speed: f64,
        contact_radius: f64,
    ) -> f64 {
        if contact_radius < 1e-30 {
            return 0.0;
        }
        let q_dot = friction * normal_force * sliding_speed;
        q_dot / (2.0 * PI * contact_radius * (self.k1 + self.k2))
    }

    /// Blok's partition-corrected flash temperature (symmetric sliding).
    ///
    /// ΔT = μ p_mean V a / (2 k₁ √(π κ₁ a / V)) + … (partition version)
    ///
    /// Simplified implementation:
    /// ΔT = β₁ * q_dot / (π a (k₁ + k₂))
    pub fn flash_temperature_partition(
        &self,
        friction: f64,
        normal_force: f64,
        sliding_speed: f64,
        contact_radius: f64,
    ) -> f64 {
        if contact_radius < 1e-30 {
            return 0.0;
        }
        let beta = self.heat_partition_factor();
        let q_dot = friction * normal_force * sliding_speed;
        beta * q_dot / (PI * contact_radius * (self.k1 + self.k2))
    }

    /// Bulk temperature rise from steady frictional heating over a sliding distance L.
    ///
    /// ΔT_bulk = μ F_n L / (m c_p)  where m c_p is the thermal mass.
    pub fn bulk_temperature_rise(
        friction: f64,
        normal_force: f64,
        sliding_distance: f64,
        thermal_mass: f64,
    ) -> f64 {
        if thermal_mass < 1e-30 {
            return 0.0;
        }
        friction * normal_force * sliding_distance / thermal_mass
    }
}

// ---------------------------------------------------------------------------
// 12. Archard wear model
// ---------------------------------------------------------------------------

/// Archard's wear law: V_wear = K * F_n * L / H.
///
/// V_wear = wear volume (m³), K = dimensionless wear coefficient,
/// F_n = normal force (N), L = sliding distance (m), H = hardness (Pa).
pub fn archard_wear_volume(
    wear_coefficient: f64,
    normal_force: f64,
    sliding_distance: f64,
    hardness: f64,
) -> f64 {
    if hardness < 1e-30 {
        return 0.0;
    }
    wear_coefficient * normal_force * sliding_distance / hardness
}

/// Wear depth from wear volume, assuming circular contact of radius a.
pub fn wear_depth_from_volume(wear_volume: f64, contact_radius: f64) -> f64 {
    if contact_radius < 1e-30 {
        return 0.0;
    }
    wear_volume / (PI * contact_radius * contact_radius)
}

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

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

    // ── helper ──────────────────────────────────────────────────────────────

    fn steel_steel() -> HertzSphereSphere {
        // E = 200 GPa, ν = 0.3 for both spheres of R = 10 mm
        HertzSphereSphere::new(0.01, 0.3, 200e9, 0.01, 0.3, 200e9)
    }

    fn steel_flat() -> HertzSphereFlat {
        HertzSphereFlat::new(0.01, 200e9, 0.3, 200e9, 0.3)
    }

    fn steel_cyl_flat() -> HertzCylinderFlat {
        HertzCylinderFlat::new(0.01, 200e9, 0.3, 200e9, 0.3)
    }

    // ── reduced_modulus ────────────────────────────────────────────────────

    #[test]
    fn test_reduced_modulus_symmetric() {
        let e = 200e9_f64;
        let nu = 0.3_f64;
        let e_star = reduced_modulus(e, nu, e, nu);
        let expected = 0.5 * e / (1.0 - nu * nu);
        assert!((e_star - expected).abs() / expected < 1e-10);
    }

    #[test]
    fn test_combined_radius_equal() {
        let r = combined_radius(0.01, 0.01);
        assert!((r - 0.005).abs() < 1e-12);
    }

    #[test]
    fn test_combined_radius_flat() {
        let r = combined_radius(0.01, f64::INFINITY);
        assert!((r - 0.01).abs() < 1e-12);
    }

    // ── HertzSphereSphere ─────────────────────────────────────────────────

    #[test]
    fn test_hertz_ss_contact_radius_positive() {
        let h = steel_steel();
        assert!(h.contact_radius(100.0) > 0.0);
    }

    #[test]
    fn test_hertz_ss_indentation_positive() {
        let h = steel_steel();
        assert!(h.indentation(100.0) > 0.0);
    }

    #[test]
    fn test_hertz_ss_peak_pressure_positive() {
        let h = steel_steel();
        assert!(h.peak_pressure(100.0) > 0.0);
    }

    #[test]
    fn test_hertz_ss_mean_pressure_less_than_peak() {
        let h = steel_steel();
        let f = 100.0;
        assert!(h.mean_pressure(f) < h.peak_pressure(f));
    }

    #[test]
    fn test_hertz_ss_force_roundtrip() {
        let h = steel_steel();
        let f_in = 500.0;
        let delta = h.indentation(f_in);
        let f_out = h.force_from_indentation(delta);
        assert!((f_in - f_out).abs() / f_in < 1e-6);
    }

    #[test]
    fn test_hertz_ss_stiffness_positive() {
        let h = steel_steel();
        assert!(h.contact_stiffness(100.0) > 0.0);
    }

    #[test]
    fn test_hertz_ss_pressure_outside_contact_zero() {
        let h = steel_steel();
        let a = h.contact_radius(100.0);
        assert_eq!(h.pressure_at_radius(a * 2.0, 100.0), 0.0);
    }

    #[test]
    fn test_hertz_ss_pressure_at_center_equals_peak() {
        let h = steel_steel();
        let f = 200.0;
        let p0 = h.peak_pressure(f);
        let p_center = h.pressure_at_radius(0.0, f);
        assert!((p_center - p0).abs() / p0 < 1e-10);
    }

    // ── HertzSphereFlat ───────────────────────────────────────────────────

    #[test]
    fn test_sphere_flat_contact_radius_positive() {
        let h = steel_flat();
        assert!(h.contact_radius(100.0) > 0.0);
    }

    #[test]
    fn test_sphere_flat_indentation_positive() {
        let h = steel_flat();
        assert!(h.indentation(200.0) > 0.0);
    }

    #[test]
    fn test_sphere_flat_peak_pressure_positive() {
        let h = steel_flat();
        assert!(h.peak_pressure(100.0) > 0.0);
    }

    #[test]
    fn test_sphere_flat_strain_energy_positive() {
        let h = steel_flat();
        assert!(h.strain_energy(100.0) > 0.0);
    }

    // ── HertzCylinderFlat ─────────────────────────────────────────────────

    #[test]
    fn test_cyl_flat_half_width_positive() {
        let h = steel_cyl_flat();
        assert!(h.half_width(1e4) > 0.0);
    }

    #[test]
    fn test_cyl_flat_peak_pressure_positive() {
        let h = steel_cyl_flat();
        assert!(h.peak_pressure(1e4) > 0.0);
    }

    #[test]
    fn test_cyl_flat_mean_less_than_peak() {
        let h = steel_cyl_flat();
        let fpl = 1e5;
        assert!(h.mean_pressure(fpl) < h.peak_pressure(fpl));
    }

    #[test]
    fn test_cyl_flat_pressure_outside_zero() {
        let h = steel_cyl_flat();
        let fpl = 1e4;
        let b = h.half_width(fpl);
        assert_eq!(h.pressure_at_position(b * 2.0, fpl), 0.0);
    }

    #[test]
    fn test_cyl_flat_max_shear_stress_positive() {
        let h = steel_cyl_flat();
        assert!(h.max_shear_stress(1e5) > 0.0);
    }

    #[test]
    fn test_cyl_flat_max_shear_depth_proportional() {
        let h = steel_cyl_flat();
        let fpl = 1e4;
        let b = h.half_width(fpl);
        let z_max = h.max_shear_depth(fpl);
        assert!((z_max - 0.786 * b).abs() < 1e-15);
    }

    // ── MindlinCompliance ─────────────────────────────────────────────────

    fn make_mindlin() -> MindlinCompliance {
        MindlinCompliance::new(200e9, 0.3, 200e9, 0.3, 0.4)
    }

    #[test]
    fn test_mindlin_tangential_stiffness_positive() {
        let m = make_mindlin();
        assert!(m.tangential_stiffness(1e-4) > 0.0);
    }

    #[test]
    fn test_mindlin_partial_slip_zero_force() {
        let m = make_mindlin();
        let disp = m.partial_slip_displacement(0.0, 100.0, 1e-4).unwrap();
        assert!(disp.abs() < 1e-20);
    }

    #[test]
    fn test_mindlin_gross_slip_returns_none() {
        let m = make_mindlin();
        // tangential = limit
        let result = m.partial_slip_displacement(40.0, 100.0, 1e-4);
        assert!(result.is_none());
    }

    #[test]
    fn test_mindlin_stick_radius_less_than_contact() {
        let m = make_mindlin();
        let a = 1e-4;
        let c = m.stick_radius(5.0, 100.0, a).unwrap();
        assert!(c < a);
    }

    #[test]
    fn test_mindlin_fretting_energy_positive() {
        let m = make_mindlin();
        let e = m.fretting_energy_per_cycle(5.0, 100.0, 1e-4);
        assert!(e > 0.0);
    }

    #[test]
    fn test_mindlin_fretting_energy_zero_amplitude() {
        let m = make_mindlin();
        let e = m.fretting_energy_per_cycle(0.0, 100.0, 1e-4);
        assert!(e.abs() < 1e-30);
    }

    // ── JKR adhesion ─────────────────────────────────────────────────────

    fn make_jkr() -> JkrAdhesion {
        JkrAdhesion::new(1e-6, 1e10, 0.05)
    }

    #[test]
    fn test_jkr_pull_off_negative() {
        let j = make_jkr();
        assert!(j.pull_off_force() < 0.0);
    }

    #[test]
    fn test_jkr_zero_load_contact_positive() {
        let j = make_jkr();
        // Adhesion creates contact even at zero load.
        assert!(j.zero_load_contact_radius() > 0.0);
    }

    #[test]
    fn test_jkr_contact_radius_increases_with_force() {
        let j = make_jkr();
        let a0 = j.contact_radius(0.0);
        let a1 = j.contact_radius(1e-3);
        assert!(a1 > a0);
    }

    #[test]
    fn test_jkr_surface_energy_negative() {
        let j = make_jkr();
        assert!(j.surface_energy(1e-3) < 0.0);
    }

    // ── DMT adhesion ─────────────────────────────────────────────────────

    fn make_dmt() -> DmtAdhesion {
        DmtAdhesion::new(1e-6, 1e10, 0.05)
    }

    #[test]
    fn test_dmt_pull_off_negative() {
        let d = make_dmt();
        assert!(d.pull_off_force() < 0.0);
    }

    #[test]
    fn test_dmt_pull_off_stronger_than_jkr() {
        // JKR: F_po = -1.5 π W R, DMT: F_po = -2 π W R → |DMT| > |JKR|
        let j = make_jkr();
        let d = make_dmt();
        assert!(d.pull_off_force().abs() > j.pull_off_force().abs());
    }

    #[test]
    fn test_dmt_contact_radius_zero_at_large_negative_force() {
        let d = make_dmt();
        // Below pull-off effective force → no contact
        let f_below = d.pull_off_force() * 1.1;
        let a = d.contact_radius(f_below);
        assert_eq!(a, 0.0);
    }

    #[test]
    fn test_dmt_tabor_parameter_positive() {
        let d = make_dmt();
        let tabor = d.tabor_parameter(3e-10);
        assert!(tabor > 0.0);
    }

    #[test]
    fn test_dmt_peak_pressure_positive() {
        let d = make_dmt();
        assert!(d.peak_pressure(1e-3) > 0.0);
    }

    // ── ContactAreaHysteresis ─────────────────────────────────────────────

    #[test]
    fn test_hysteresis_loading_increases_area() {
        let mut h = ContactAreaHysteresis::new(5e-3, 1e11);
        let a0 = h.load(100.0);
        let a1 = h.load(500.0);
        assert!(a1 > a0);
    }

    #[test]
    fn test_hysteresis_unload_less_or_equal_load() {
        let mut h = ContactAreaHysteresis::new(5e-3, 1e11);
        h.load(500.0);
        let a_load = h.load(200.0);
        let a_unload = h.unload(200.0);
        // Unloading branch ≥ Hertz loading due to residual
        assert!(a_unload >= a_load * 0.99);
    }

    #[test]
    fn test_hysteresis_energy_positive() {
        let mut h = ContactAreaHysteresis::new(5e-3, 1e11);
        h.load(500.0);
        let e = h.hysteresis_energy(50);
        assert!(e >= 0.0);
    }

    // ── ContactForceModel ─────────────────────────────────────────────────

    fn make_cfm() -> ContactForceModel {
        ContactForceModel {
            normal_stiffness: 1e8,
            tangential_stiffness: 5e7,
            friction: 0.3,
            damping: 0.05,
        }
    }

    #[test]
    fn test_cfm_no_force_open_gap() {
        let c = make_cfm();
        assert_eq!(c.linear_normal_force(0.001), 0.0);
    }

    #[test]
    fn test_cfm_linear_normal_force_overlap() {
        let c = make_cfm();
        let fn_ = c.linear_normal_force(-1e-4);
        assert!((fn_ - 1e4).abs() < 1.0);
    }

    #[test]
    fn test_cfm_hertz_normal_force_positive() {
        let c = make_cfm();
        assert!(c.hertz_normal_force(-1e-5) > 0.0);
    }

    #[test]
    fn test_cfm_tangential_force_coulomb_limited() {
        let c = make_cfm();
        let fn_ = 1000.0;
        let limit = c.friction * fn_;
        let ft = c.tangential_force(1.0, fn_); // very large displacement
        assert!(ft.abs() <= limit + 1e-10);
    }

    #[test]
    fn test_cfm_damped_normal_force_approach() {
        let c = make_cfm();
        let fn_ = c.damped_normal_force(-1e-4, -0.001, 0.1);
        assert!(fn_ >= 0.0);
    }

    // ── Greenwood–Williamson ──────────────────────────────────────────────

    fn make_gw() -> GreenwoodWilliamson {
        GreenwoodWilliamson {
            asperity_density: 1e10,
            asperity_radius: 1e-6,
            height_std: 1e-7,
            e_star: 1e11,
        }
    }

    #[test]
    fn test_gw_contact_area_fraction_positive() {
        let gw = make_gw();
        let frac = gw.real_contact_area_fraction(0.0);
        assert!(frac > 0.0);
    }

    #[test]
    fn test_gw_contact_area_decreases_with_separation() {
        let gw = make_gw();
        let a0 = gw.real_contact_area_fraction(0.0);
        let a1 = gw.real_contact_area_fraction(5e-7);
        assert!(a0 > a1);
    }

    #[test]
    fn test_gw_pressure_positive_at_zero_separation() {
        let gw = make_gw();
        assert!(gw.contact_pressure(0.0) > 0.0);
    }

    #[test]
    fn test_gw_plasticity_index_positive() {
        let gw = make_gw();
        let psi = gw.plasticity_index(2e9);
        assert!(psi > 0.0);
    }

    #[test]
    fn test_gw_asperity_density_positive() {
        let gw = make_gw();
        let n = gw.contact_asperity_density(0.0);
        assert!(n > 0.0);
    }

    #[test]
    fn test_gw_erfc_at_zero() {
        // erfc(0) = 1
        assert!((gw_erfc(0.0) - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_gw_erfc_large_arg() {
        // erfc(large) ≈ 0
        assert!(gw_erfc(5.0) < 1e-6);
    }

    #[test]
    fn test_gw_separation_for_pressure() {
        let gw = make_gw();
        let p0 = gw.contact_pressure(0.0);
        let target = p0 * 0.5;
        let sep = gw.separation_for_pressure(target, 1e-12);
        assert!(sep.is_some());
        let sep = sep.unwrap();
        let p_check = gw.contact_pressure(sep);
        assert!((p_check - target).abs() / target < 0.01);
    }

    // ── FlashTemperature ──────────────────────────────────────────────────

    fn make_flash() -> FlashTemperature {
        FlashTemperature::new(50.0, 1.4e-5, 50.0, 1.4e-5)
    }

    #[test]
    fn test_flash_partition_factor_symmetric() {
        let ft = make_flash();
        assert!((ft.heat_partition_factor() - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_flash_peclet_positive() {
        let pe = FlashTemperature::peclet_number(1.0, 1e-3, 1e-5);
        assert!(pe > 0.0);
    }

    #[test]
    fn test_flash_high_speed_positive() {
        let ft = make_flash();
        let dt = ft.flash_temperature_high_speed(0.3, 100.0, 1.0, 1e-3);
        assert!(dt > 0.0);
    }

    #[test]
    fn test_flash_low_speed_positive() {
        let ft = make_flash();
        let dt = ft.flash_temperature_low_speed(0.3, 100.0, 0.01, 1e-3);
        assert!(dt > 0.0);
    }

    #[test]
    fn test_flash_partition_positive() {
        let ft = make_flash();
        let dt = ft.flash_temperature_partition(0.3, 100.0, 1.0, 1e-3);
        assert!(dt > 0.0);
    }

    #[test]
    fn test_flash_bulk_temperature_rise_positive() {
        let dt = FlashTemperature::bulk_temperature_rise(0.3, 100.0, 1.0, 500.0);
        assert!(dt > 0.0);
    }

    #[test]
    fn test_flash_zero_speed_gives_zero() {
        let ft = make_flash();
        let dt = ft.flash_temperature_high_speed(0.3, 100.0, 0.0, 1e-3);
        assert_eq!(dt, 0.0);
    }

    // ── Archard wear ──────────────────────────────────────────────────────

    #[test]
    fn test_archard_wear_volume_positive() {
        let v = archard_wear_volume(1e-4, 1000.0, 0.5, 5e9);
        assert!(v > 0.0);
    }

    #[test]
    fn test_archard_zero_hardness() {
        let v = archard_wear_volume(1e-4, 1000.0, 0.5, 0.0);
        assert_eq!(v, 0.0);
    }

    #[test]
    fn test_wear_depth_from_volume() {
        let depth = wear_depth_from_volume(1e-12, 1e-3);
        assert!(depth > 0.0);
    }

    #[test]
    fn test_wear_depth_zero_radius() {
        let depth = wear_depth_from_volume(1e-12, 0.0);
        assert_eq!(depth, 0.0);
    }
}