dirt_granular 0.1.4

Granular physics for DIRT: Hertz normal contact, Mindlin tangential friction, rotational dynamics
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
use super::*;

/// Standard system: compute the full Hertz-Mindlin contact force in one pass.
pub fn hertz_mindlin_contact_force(
    mut atoms: ResMut<Atom>,
    neighbor: Res<Neighbor>,
    particles: ParticlesWith<
        '_,
        (
            Write<DemAtom>,
            Write<ContactHistoryStore>,
            Optional<Read<BondStore>>,
        ),
    >,
    material_table: Res<MaterialTable>,
    mut virial: Option<ResMut<VirialStress>>,
) {
    particles.with(|(mut dem, mut history, bonds)| {
        contact_force_core_views(
            &mut atoms,
            &neighbor,
            &mut dem,
            &mut history,
            bonds.as_deref(),
            &material_table,
            virial.as_deref_mut(),
            ForcePass::All,
        );
    });
}

/// Overlapped Hertz-Mindlin force (roadmap step 4): compute the interior pairs
/// (`j < nlocal`, no ghosts needed) *while the ghost halo is in flight*, then the
/// boundary pairs (`j >= nlocal`) once it lands. The interior force runs as the
/// overlap closure of [`forward_comm_overlap`], so on a multi-rank run its compute
/// hides the MPI latency of the ghost exchange. Bit-identical to
/// [`hertz_mindlin_contact_force`] (the interior/boundary split is exact — see
/// `interior_boundary_split_matches_single_pass`).
pub fn overlapped_contact_force(
    mut atoms: ResMut<Atom>,
    neighbor: Res<Neighbor>,
    registry: Res<AtomDataRegistry>,
    particles: ParticlesWith<
        '_,
        (
            Write<DemAtom>,
            Write<ContactHistoryStore>,
            Optional<Read<BondStore>>,
        ),
    >,
    material_table: Res<MaterialTable>,
    comm: Res<CommResource>,
    topo: Res<CommTopology>,
    mut buffers: ResMut<CommBuffers>,
    mut virial: Option<ResMut<VirialStress>>,
) {
    let mut pool = std::mem::take(&mut buffers.forward_scratch);
    {
        // Interior pairs need no fresh ghosts — run them during the in-flight halo.
        let mut interior = |a: &mut Atom| {
            contact_force_core_particles(
                a,
                &neighbor,
                &particles,
                &material_table,
                None,
                ForcePass::Interior,
            );
        };
        forward_comm_overlap(
            &mut atoms,
            &registry,
            &topo,
            &**comm,
            &mut pool,
            &mut interior,
        );
    }
    buffers.forward_scratch = pool;
    // Boundary pairs, now that the halo has landed.
    contact_force_core_particles(
        &mut atoms,
        &neighbor,
        &particles,
        &material_table,
        virial.as_deref_mut(),
        ForcePass::Boundary,
    );
}

/// Core force computation, parameterised by [`ForcePass`] so the interior and
/// boundary pairs can be computed in separate passes (with the halo exchange
/// between) for comm/compute overlap. `ForcePass::All` is the single-pass force.
pub fn contact_force_core(
    atoms: &mut Atom,
    neighbor: &Neighbor,
    registry: &AtomDataRegistry,
    material_table: &MaterialTable,
    virial: Option<&mut VirialStress>,
    pass: ForcePass,
) {
    let mut dem = registry.expect_mut::<DemAtom>("contact_force_core test helper");
    let mut history = registry.expect_mut::<ContactHistoryStore>("contact_force_core test helper");
    let bonds = registry.get::<BondStore>();
    contact_force_core_views(
        atoms,
        neighbor,
        &mut dem,
        &mut history,
        bonds.as_deref(),
        material_table,
        virial,
        pass,
    );
}

/// Typed scheduled-contact implementation. The legacy registry helper above is
/// retained solely for direct kernel unit tests; scheduled systems use this
/// query-backed path so required extensions are validated at preparation.
fn contact_force_core_particles(
    atoms: &mut Atom,
    neighbor: &Neighbor,
    particles: &ParticlesWith<
        '_,
        (
            Write<DemAtom>,
            Write<ContactHistoryStore>,
            Optional<Read<BondStore>>,
        ),
    >,
    material_table: &MaterialTable,
    virial: Option<&mut VirialStress>,
    pass: ForcePass,
) {
    particles.with(|(mut dem, mut history, bonds)| {
        contact_force_core_views(
            atoms,
            neighbor,
            &mut dem,
            &mut history,
            bonds.as_deref(),
            material_table,
            virial,
            pass,
        );
    });
}

/// Typed core used by scheduled contact systems after `ParticlesWith` has
/// validated and borrowed the exact extension columns they require.
pub fn contact_force_core_views(
    atoms: &mut Atom,
    neighbor: &Neighbor,
    dem: &mut DemAtom,
    history: &mut ContactHistoryStore,
    bond_store: Option<&BondStore>,
    material_table: &MaterialTable,
    mut virial: Option<&mut VirialStress>,
    pass: ForcePass,
) {
    let newton = neighbor.newton;
    let dt = atoms.dt;

    let natoms = atoms.len();
    if history.contacts.len() < natoms {
        history.contacts.resize_with(natoms, Vec::new);
    }

    let nlocal = atoms.nlocal as usize;
    let mut overlap_warnings = 0usize;

    // Reset all active flags before pair loop (skipped on the Boundary pass, which
    // continues the Interior pass's history instead of clearing it).
    if pass != ForcePass::Boundary {
        for i in 0..nlocal {
            for entry in &mut history.contacts[i] {
                entry.2 = false;
            }
        }
    }

    for (i, j) in neighbor.pairs(nlocal) {
        // Interior/boundary split (step 4): boundary pairs touch a ghost (j >= nlocal).
        let is_boundary_pair = j >= nlocal;
        match pass {
            ForcePass::Interior if is_boundary_pair => continue,
            ForcePass::Boundary if !is_boundary_pair => continue,
            _ => {}
        }
        if let Some(ref bonds) = bond_store {
            if bonds.are_excluded(i, j, &atoms.tag) {
                continue;
            }
        }

        // Skip same-body pairs (sub-spheres of the same rigid body don't interact)
        if dirt_atom::same_body(&dem, i, j) {
            continue;
        }

        let r1 = dem.radius[i];
        let r2 = dem.radius[j];

        let dx = atoms.pos[j][0] as f64 - atoms.pos[i][0] as f64;
        let dy = atoms.pos[j][1] as f64 - atoms.pos[i][1] as f64;
        let dz = atoms.pos[j][2] as f64 - atoms.pos[i][2] as f64;
        let dist_sq = dx * dx + dy * dy + dz * dz;
        let sum_r = r1 + r2;

        let mat_i = atoms.atom_type[i] as usize;
        let mat_j = atoms.atom_type[j] as usize;
        let surface_energy = material_table.surface_energy_ij[mat_i][mat_j];

        let use_dmt = material_table.adhesion_model == "dmt";

        // JKR: compute pull-off distance for extended interaction range
        // DMT: no extended range (particles separate at delta = 0)
        // Effective radius: R* = R1 R2 / (R1 + R2)
        let r_eff = (r1 * r2) / sum_r;
        // Effective Young's modulus: 1/E* = (1-ν1²)/E1 + (1-ν2²)/E2
        let e_eff = material_table.e_eff_ij[mat_i][mat_j];
        let liquid_bridge_active = material_table.liquid_bridge_model == "willett2000";
        let liquid_volume = material_table.liquid_bridge_volume_ij[mat_i][mat_j];
        let liquid_surface_tension = material_table.liquid_surface_tension_ij[mat_i][mat_j];
        let liquid_contact_angle = material_table.liquid_contact_angle_ij[mat_i][mat_j];
        let liquid_rupture_distance = material_table.liquid_rupture_distance_ij[mat_i][mat_j];
        let liquid_range =
            if liquid_bridge_active && liquid_volume > 0.0 && liquid_surface_tension > 0.0 {
                if liquid_rupture_distance > 0.0 {
                    liquid_rupture_distance
                } else {
                    (1.0 + 0.5 * liquid_contact_angle) * liquid_volume.cbrt()
                }
            } else {
                0.0
            };
        // JKR pull-off distance: particles interact beyond geometric contact
        let delta_pulloff = if surface_energy > 0.0 && !use_dmt {
            let gamma = surface_energy;
            (std::f64::consts::PI * std::f64::consts::PI * gamma * gamma * r_eff
                / (4.0 * e_eff * e_eff))
                .cbrt()
        } else {
            0.0
        };

        // Check contact: geometric touch or within JKR adhesion range
        let interaction_r = sum_r + delta_pulloff.max(liquid_range);
        if dist_sq >= interaction_r * interaction_r {
            continue;
        }

        let distance = dist_sq.sqrt();

        if distance == 0.0 {
            #[cfg(debug_assertions)]
            eprintln!(
                "WARNING: zero separation between tags {} {}",
                atoms.tag[i], atoms.tag[j]
            );
            continue;
        }

        // delta > 0 means geometric overlap, delta < 0 means gap
        // Cap at half the smaller radius to keep the Hertz model numerically valid.
        let r_min = r1.min(r2);
        let delta = (sum_r - distance).min(0.5 * r_min);

        if delta > 0.0 && distance / sum_r < LARGE_OVERLAP_WARN_THRESHOLD {
            overlap_warnings += 1;
            #[cfg(debug_assertions)]
            eprintln!(
                "WARNING: large overlap tags {} {} ratio {:.3}",
                atoms.tag[i],
                atoms.tag[j],
                distance / sum_r
            );
            if overlap_warnings > MAX_OVERLAP_WARNINGS {
                panic!(
                    "Over {} excessive overlaps this step — aborting. \
                     Check timestep or initial configuration.",
                    MAX_OVERLAP_WARNINGS
                );
            }
            // Cap overlap at half the smaller radius to keep Hertz model valid,
            // but still compute the repulsive force (skipping would remove all
            // repulsion and cause runaway penetration).
        }

        let separation = (distance - sum_r).max(0.0);

        // For non-JKR/non-liquid, skip if no geometric overlap
        if delta <= 0.0 && surface_energy <= 0.0 && liquid_range <= 0.0 {
            continue;
        }

        // ── Shared quantities (computed once) ────────────────────────────
        let inv_dist = 1.0 / distance;
        let nx = dx * inv_dist;
        let ny = dy * inv_dist;
        let nz = dz * inv_dist;

        // Effective shear modulus: 1/G* = (2-ν1)/G1 + (2-ν2)/G2
        let g_eff = material_table.g_eff_ij[mat_i][mat_j];

        // Reduced mass: m_r = 1 / (1/m1 + 1/m2)
        // For clump sub-spheres inv_mass is 0 (body-integrated); use real mass.
        let inv_m_i = if atoms.inv_mass[i] as f64 > 0.0 {
            atoms.inv_mass[i] as f64
        } else {
            1.0 / atoms.mass[i] as f64
        };
        let inv_m_j = if atoms.inv_mass[j] as f64 > 0.0 {
            atoms.inv_mass[j] as f64
        } else {
            1.0 / atoms.mass[j] as f64
        };
        let m_r = 1.0 / (inv_m_i + inv_m_j);

        let beta = material_table.beta_ij[mat_i][mat_j];
        let mu = material_table.friction_ij[mat_i][mat_j];
        let mu_r = material_table.rolling_friction_ij[mat_i][mat_j];
        let mu_tw = material_table.twisting_friction_ij[mat_i][mat_j];
        let cohesion_energy = material_table.cohesion_energy_ij[mat_i][mat_j];
        let use_mdr = material_table.contact_model == "mdr";

        // JKR adhesion-only regime: gap exists but within pull-off distance
        // DMT has no adhesion-only regime (no force beyond contact)
        let jkr_adhesion_only = surface_energy > 0.0 && !use_dmt && delta <= 0.0;
        let f_liquid_bridge = if liquid_range > 0.0 {
            willett2000_liquid_bridge_force(
                separation,
                r_eff,
                liquid_volume,
                liquid_surface_tension,
                liquid_contact_angle,
                liquid_rupture_distance,
            )
        } else {
            0.0
        };
        let bridge_only = delta <= 0.0 && !jkr_adhesion_only && f_liquid_bridge > 0.0;

        // Hertz stiffness parameters (only meaningful when δ > 0)
        // S_n = 2 E* √(R* δ)  — normal stiffness parameter (used in damping)
        // k_n = 4/3 E* √(R* δ) — normal spring constant
        // k_t = 8 G* √(R* δ)  — tangential spring constant (Mindlin)
        let (s_n, k_n, k_t, contact_radius) = if delta > 0.0 {
            let sdr = (delta * r_eff).sqrt();
            let sn = 2.0 * e_eff * sdr;
            let kn = 4.0 / 3.0 * e_eff * sdr;
            let kt = 8.0 * g_eff * sdr;
            (sn, kn, kt, sdr)
        } else {
            (0.0, 0.0, 0.0, 0.0)
        };

        // Full relative velocity (including angular contributions)
        let omega_ix = dem.omega[i][0];
        let omega_iy = dem.omega[i][1];
        let omega_iz = dem.omega[i][2];
        let omega_jx = dem.omega[j][0];
        let omega_jy = dem.omega[j][1];
        let omega_jz = dem.omega[j][2];

        // v_contact_i = vel_i + omega_i × (r1 * n)
        let r1n_x = r1 * nx;
        let r1n_y = r1 * ny;
        let r1n_z = r1 * nz;
        let vc_ix = atoms.vel[i][0] as f64 + (omega_iy * r1n_z - omega_iz * r1n_y);
        let vc_iy = atoms.vel[i][1] as f64 + (omega_iz * r1n_x - omega_ix * r1n_z);
        let vc_iz = atoms.vel[i][2] as f64 + (omega_ix * r1n_y - omega_iy * r1n_x);

        // v_contact_j = vel_j + omega_j × (-r2 * n)
        let r2n_x = r2 * nx;
        let r2n_y = r2 * ny;
        let r2n_z = r2 * nz;
        let vc_jx = atoms.vel[j][0] as f64 + (-omega_jy * r2n_z + omega_jz * r2n_y);
        let vc_jy = atoms.vel[j][1] as f64 + (-omega_jz * r2n_x + omega_jx * r2n_z);
        let vc_jz = atoms.vel[j][2] as f64 + (-omega_jx * r2n_y + omega_jy * r2n_x);

        let vr_x = vc_jx - vc_ix;
        let vr_y = vc_jy - vc_iy;
        let vr_z = vc_jz - vc_iz;

        let v_n = vr_x * nx + vr_y * ny + vr_z * nz;

        let tag_i = atoms.tag[i];
        let tag_j = atoms.tag[j];
        let sign: f64 = if tag_i < tag_j { 1.0 } else { -1.0 };

        // Look up existing spring/history (single search, reused for write-back).
        let entry_idx = history.contacts[i].iter().position(|(t, _, _)| *t == tag_j);
        let mut stored = match entry_idx {
            Some(idx) => history.contacts[i][idx].1,
            None => zero_contact_history(),
        };

        // ── Normal force ─────────────────────────────────────────────────
        // F_n > 0 → repulsive (along contact normal from i to j)
        // F_n < 0 → attractive (adhesion/cohesion pulls particles together)
        let (mut f_n_mag, k_t) = if use_mdr {
            let (f, _k_mdr, kt_mdr) = mdr_normal_force(
                delta.max(0.0),
                r1,
                r2,
                e_eff,
                0.5 * (material_table.poisson_ratio[mat_i] + material_table.poisson_ratio[mat_j]),
                material_table.mdr_yield_stress_ij[mat_i][mat_j],
                surface_energy,
                material_table.mdr_damping_ij[mat_i][mat_j],
                m_r,
                v_n,
                &mut stored,
            );
            (f, kt_mdr)
        } else if surface_energy > 0.0 && use_dmt {
            // DMT: Hertz contact + constant adhesive force F_dmt = 2π γ R*
            let f_dmt = 2.0 * std::f64::consts::PI * surface_energy * r_eff;
            let f_diss_n = 2.0 * beta * SQRT_5_6 * (s_n * m_r).sqrt() * v_n;
            (k_n * delta - f_diss_n - f_dmt, k_t)
        } else if surface_energy > 0.0 {
            // JKR: adhesion force F_adh = 3/2 π γ R* (simplified explicit model)
            let f_adhesion = 1.5 * std::f64::consts::PI * surface_energy * r_eff;
            if jkr_adhesion_only {
                // Gap regime (δ ≤ 0): pure adhesion, no Hertz contact or damping
                (-f_adhesion, k_t)
            } else {
                // Contact regime (δ > 0): Hertz repulsion + damping − adhesion
                let f_diss_n = 2.0 * beta * SQRT_5_6 * (s_n * m_r).sqrt() * v_n;
                (k_n * delta - f_diss_n - f_adhesion, k_t)
            }
        } else if cohesion_energy > 0.0 {
            // SJKR: cohesion proportional to contact area A = π δ R*
            let f_diss_n = 2.0 * beta * SQRT_5_6 * (s_n * m_r).sqrt() * v_n;
            let f_cohesion = cohesion_energy * std::f64::consts::PI * delta * r_eff;
            (k_n * delta - f_diss_n - f_cohesion, k_t) // can go negative (attractive)
        } else {
            // Standard Hertz repulsion + viscoelastic damping. With
            // `limit_damping` (default) the total is clamped to ≥ 0 so damping
            // can never pull particles together; with it disabled the damping may
            // go net-attractive near separation, matching LAMMPS's default
            // `pair granular` (no tensile cutoff) — required for exact cross-code
            // COR at low restitution (see bench_hertz_rebound).
            let f_diss_n = 2.0 * beta * SQRT_5_6 * (s_n * m_r).sqrt() * v_n;
            let f_total = k_n * delta - f_diss_n;
            if material_table.limit_damping {
                (f_total.max(0.0), k_t)
            } else {
                (f_total, k_t)
            }
        };
        f_n_mag -= f_liquid_bridge;

        let fn_x = f_n_mag * nx;
        let fn_y = f_n_mag * ny;
        let fn_z = f_n_mag * nz;

        atoms.force[i][0] -= fn_x as soil_core::Accum;
        atoms.force[i][1] -= fn_y as soil_core::Accum;
        atoms.force[i][2] -= fn_z as soil_core::Accum;
        if newton {
            atoms.force[j][0] += fn_x as soil_core::Accum;
            atoms.force[j][1] += fn_y as soil_core::Accum;
            atoms.force[j][2] += fn_z as soil_core::Accum;
        }

        // ── Tangential force (skip in JKR adhesion-only regime) ──────────
        // No tangential friction when particles are not in geometric contact
        if jkr_adhesion_only || bridge_only {
            // No tangential, rolling, or spring history in gap-only attraction.
            // Virial contribution from normal only
            if let Some(ref mut v) = virial {
                if v.active {
                    let vs = if newton { 1.0 } else { 0.5 };
                    v.add_pair(dx, dy, dz, -fn_x * vs, -fn_y * vs, -fn_z * vs);
                }
            }
            continue;
        }

        let vt_x = vr_x - v_n * nx;
        let vt_y = vr_y - v_n * ny;
        let vt_z = vr_z - v_n * nz;

        // Tangential spring displacement (history model). For the history-free
        // `linear_nohistory` model the spring is identically zero, so the force
        // collapses to the velocity-Coulomb law
        //   F_t = -min(μ |F_n|, γ_t |v_t|) t̂ ,   t̂ = v_t / |v_t|
        // (LAMMPS pair_granular `tangential linear_nohistory`, and the classic
        // `pair gran/hooke`) — the force depends only on the instantaneous
        // relative tangential velocity, with NO accumulated displacement.
        let tangential_model = material_table.tangential_model.as_str();
        let nohistory = tangential_model == "linear_nohistory";
        let mindlin_rescale = is_mindlin_rescale(tangential_model);
        let mindlin_force_history = is_mindlin_force_history(tangential_model);
        let f_t_max = mu * f_n_mag.abs();
        let (sx, sy, sz) = if nohistory {
            (0.0, 0.0, 0.0)
        } else if mindlin_force_history {
            // LAMMPS `mindlin_rescale/force` stores the elastic tangential force
            // itself as history. On normal unloading it scales that force by the
            // contact-radius ratio a_n/a_{n-1} before adding the new increment.
            let mut fx = sign * stored[0];
            let mut fy = sign * stored[1];
            let mut fz = sign * stored[2];
            let prev_a = stored[7];
            if mindlin_rescale && prev_a > TANGENTIAL_EPSILON && contact_radius < prev_a {
                let scale = contact_radius / prev_a;
                fx *= scale;
                fy *= scale;
                fz *= scale;
            }
            let f_dot_n = fx * nx + fy * ny + fz * nz;
            fx -= f_dot_n * nx;
            fy -= f_dot_n * ny;
            fz -= f_dot_n * nz;
            fx += k_t * vt_x * dt;
            fy += k_t * vt_y * dt;
            fz += k_t * vt_z * dt;
            (fx, fy, fz)
        } else {
            // Convert stored spring from canonical form to local (i,j) frame
            let mut sx = sign * stored[0];
            let mut sy = sign * stored[1];
            let mut sz = sign * stored[2];
            let prev_a = stored[7];
            if mindlin_rescale && prev_a > TANGENTIAL_EPSILON && contact_radius < prev_a {
                let scale = contact_radius / prev_a;
                sx *= scale;
                sy *= scale;
                sz *= scale;
            }
            // Rotate spring into current tangent plane (remove normal component)
            let s_dot_n = sx * nx + sy * ny + sz * nz;
            sx -= s_dot_n * nx;
            sy -= s_dot_n * ny;
            sz -= s_dot_n * nz;
            // Integrate tangential velocity into spring displacement
            sx += vt_x * dt;
            sy += vt_y * dt;
            sz += vt_z * dt;

            // Coulomb cap on spring: |k_t s| ≤ μ |F_n|
            let s_mag = (sx * sx + sy * sy + sz * sz).sqrt();
            let f_t_spring_mag = k_t * s_mag;
            if f_t_spring_mag > f_t_max && f_t_spring_mag > TANGENTIAL_EPSILON {
                let scale = f_t_max / f_t_spring_mag;
                sx *= scale;
                sy *= scale;
                sz *= scale;
            }
            (sx, sy, sz)
        };

        // Tangential damping coefficient: γ_t = 2 β √(5/6) √(k_t m_r)
        let gamma_t = 2.0 * SQRT_5_6 * beta * (k_t * m_r).sqrt();
        let mut ft_x = (if mindlin_force_history { sx } else { k_t * sx }) + gamma_t * vt_x;
        let mut ft_y = (if mindlin_force_history { sy } else { k_t * sy }) + gamma_t * vt_y;
        let mut ft_z = (if mindlin_force_history { sz } else { k_t * sz }) + gamma_t * vt_z;

        // Coulomb cap on total tangential force
        let f_t_mag = (ft_x * ft_x + ft_y * ft_y + ft_z * ft_z).sqrt();
        if f_t_mag > f_t_max && f_t_mag > TANGENTIAL_EPSILON {
            let scale = f_t_max / f_t_mag;
            ft_x *= scale;
            ft_y *= scale;
            ft_z *= scale;
        }

        let (sx, sy, sz) =
            if mindlin_force_history && f_t_mag > f_t_max && f_t_mag > TANGENTIAL_EPSILON {
                (
                    ft_x - gamma_t * vt_x,
                    ft_y - gamma_t * vt_y,
                    ft_z - gamma_t * vt_z,
                )
            } else {
                (sx, sy, sz)
            };

        // Torques: τ_i = (r1 * n) × f_t, τ_j = (-r2 * n) × (-f_t) = (r2 * n) × f_t
        let ti_x = r1n_y * ft_z - r1n_z * ft_y;
        let ti_y = r1n_z * ft_x - r1n_x * ft_z;
        let ti_z = r1n_x * ft_y - r1n_y * ft_x;
        let tj_x = r2n_y * ft_z - r2n_z * ft_y;
        let tj_y = r2n_z * ft_x - r2n_x * ft_z;
        let tj_z = r2n_x * ft_y - r2n_y * ft_x;

        atoms.force[i][0] += ft_x as soil_core::Accum;
        atoms.force[i][1] += ft_y as soil_core::Accum;
        atoms.force[i][2] += ft_z as soil_core::Accum;
        if newton {
            atoms.force[j][0] -= ft_x as soil_core::Accum;
            atoms.force[j][1] -= ft_y as soil_core::Accum;
            atoms.force[j][2] -= ft_z as soil_core::Accum;
        }
        dem.torque[i][0] += ti_x;
        dem.torque[i][1] += ti_y;
        dem.torque[i][2] += ti_z;
        if newton {
            dem.torque[j][0] += tj_x;
            dem.torque[j][1] += tj_y;
            dem.torque[j][2] += tj_z;
        }

        // ── Rolling resistance torque ───────────────────────────────────
        // Relative angular velocity (rolling component)
        let or_x = omega_ix - omega_jx;
        let or_y = omega_iy - omega_jy;
        let or_z = omega_iz - omega_jz;
        let or_dot_n = or_x * nx + or_y * ny + or_z * nz;
        let roll_x = or_x - or_dot_n * nx;
        let roll_y = or_y - or_dot_n * ny;
        let roll_z = or_z - or_dot_n * nz;

        let mut roll_disp_x = sign * stored[3];
        let mut roll_disp_y = sign * stored[4];
        let mut roll_disp_z = sign * stored[5];
        let mut twist_disp = sign * stored[6];

        if mu_r > 0.0 {
            let roll_mag = (roll_x * roll_x + roll_y * roll_y + roll_z * roll_z).sqrt();
            let sds_rolling = material_table.rolling_model == "sds";
            if sds_rolling {
                // SDS rolling: spring-dashpot-slider model
                let k_roll = material_table.rolling_stiffness_ij[mat_i][mat_j];
                let gamma_roll = material_table.rolling_damping_ij[mat_i][mat_j];

                // Update rolling displacement: remove normal component, integrate
                let rd_dot_n = roll_disp_x * nx + roll_disp_y * ny + roll_disp_z * nz;
                roll_disp_x -= rd_dot_n * nx;
                roll_disp_y -= rd_dot_n * ny;
                roll_disp_z -= rd_dot_n * nz;
                roll_disp_x += roll_x * dt;
                roll_disp_y += roll_y * dt;
                roll_disp_z += roll_z * dt;

                // Spring + dashpot torque
                let mut tr_x = -k_roll * roll_disp_x - gamma_roll * roll_x;
                let mut tr_y = -k_roll * roll_disp_y - gamma_roll * roll_y;
                let mut tr_z = -k_roll * roll_disp_z - gamma_roll * roll_z;
                let tr_mag = (tr_x * tr_x + tr_y * tr_y + tr_z * tr_z).sqrt();
                let tau_max = mu_r * f_n_mag.abs() * r_eff;

                if tr_mag > tau_max && tr_mag > TANGENTIAL_EPSILON {
                    // Cap and rescale spring displacement
                    let scale = tau_max / tr_mag;
                    tr_x *= scale;
                    tr_y *= scale;
                    tr_z *= scale;
                    // Rescale spring: δ = (τ + γ·ω) / (-k)
                    if k_roll > TANGENTIAL_EPSILON {
                        roll_disp_x = (tr_x + gamma_roll * roll_x) / (-k_roll);
                        roll_disp_y = (tr_y + gamma_roll * roll_y) / (-k_roll);
                        roll_disp_z = (tr_z + gamma_roll * roll_z) / (-k_roll);
                    }
                }

                dem.torque[i][0] += tr_x;
                dem.torque[i][1] += tr_y;
                dem.torque[i][2] += tr_z;
                if newton {
                    dem.torque[j][0] -= tr_x;
                    dem.torque[j][1] -= tr_y;
                    dem.torque[j][2] -= tr_z;
                }
            } else if roll_mag > 1e-30 {
                // Constant torque model (existing behavior)
                let tau_mag = mu_r * f_n_mag.abs() * r_eff;
                let inv_roll = tau_mag / roll_mag;
                let tr_x = -inv_roll * roll_x;
                let tr_y = -inv_roll * roll_y;
                let tr_z = -inv_roll * roll_z;
                dem.torque[i][0] += tr_x;
                dem.torque[i][1] += tr_y;
                dem.torque[i][2] += tr_z;
                if newton {
                    dem.torque[j][0] -= tr_x;
                    dem.torque[j][1] -= tr_y;
                    dem.torque[j][2] -= tr_z;
                }
            }
        }

        // ── Twisting friction torque ─────────────────────────────────────
        // Three selectable models (material_table.twisting_model):
        //   "constant" / "sds"  — user-supplied coefficients (gated on μ_tw > 0);
        //   "marshall"           — coefficients DERIVED from the active tangential
        //                          (Mindlin) model, no separate twist inputs.
        if material_table.twisting_model == "marshall" {
            // Marshall (2009) twisting, per LAMMPS pair_granular `twisting marshall`
            // (doc/src/pair_granular.rst §twisting, Marshall2009 eqs 32-33). The
            // twisting stiffness/damping/friction are expressed in terms of the
            // tangential (sliding) coefficients and the Hertz contact radius
            // a = √(R* δ):
            //     k_twist = ½ k_t a²,  γ_twist = ½ γ_t a²,  μ_twist = (2/3) a μ_t
            // with k_t, γ_t the tangential spring/damping computed above and μ_t
            // the tangential friction coefficient. Below the cap the couple is
            // the spring–dashpot τ = −k_twist ξ − γ_twist Ω; it is then truncated
            // to |τ| ≤ μ_twist F_n and the angular displacement rescaled to the
            // critical value (identical bookkeeping to the SDS slider).
            if delta > 0.0 {
                let twist_vel = or_dot_n;
                let a_sq = delta * r_eff; // a² = (√(R* δ))²
                let a = a_sq.sqrt(); // Hertz contact radius
                let k_twist = 0.5 * k_t * a_sq;
                let gamma_twist = 0.5 * gamma_t * a_sq;
                let mu_twist = (2.0 / 3.0) * a * mu; // μ = tangential friction coeff

                twist_disp += twist_vel * dt;

                let mut tau_twist = -k_twist * twist_disp - gamma_twist * twist_vel;
                let tau_max = mu_twist * f_n_mag.abs();
                if tau_twist.abs() > tau_max {
                    tau_twist = tau_twist.signum() * tau_max;
                    if k_twist > TANGENTIAL_EPSILON {
                        twist_disp = (tau_twist + gamma_twist * twist_vel) / (-k_twist);
                    }
                }

                let tt_x = tau_twist * nx;
                let tt_y = tau_twist * ny;
                let tt_z = tau_twist * nz;
                dem.torque[i][0] += tt_x;
                dem.torque[i][1] += tt_y;
                dem.torque[i][2] += tt_z;
                if newton {
                    dem.torque[j][0] -= tt_x;
                    dem.torque[j][1] -= tt_y;
                    dem.torque[j][2] -= tt_z;
                }
            }
        } else if mu_tw > 0.0 {
            let twist_vel = or_dot_n; // twisting component of relative angular velocity
            let sds_twisting = material_table.twisting_model == "sds";
            if sds_twisting {
                // SDS twisting: spring-dashpot-slider model
                let k_twist = material_table.twisting_stiffness_ij[mat_i][mat_j];
                let gamma_twist = material_table.twisting_damping_ij[mat_i][mat_j];

                // Update twisting displacement
                twist_disp += twist_vel * dt;

                // Spring + dashpot torque (scalar, along contact normal)
                let mut tau_twist = -k_twist * twist_disp - gamma_twist * twist_vel;
                let tau_max = mu_tw * f_n_mag.abs() * r_eff;

                if tau_twist.abs() > tau_max {
                    // Cap and rescale spring
                    tau_twist = tau_twist.signum() * tau_max;
                    if k_twist > TANGENTIAL_EPSILON {
                        twist_disp = (tau_twist + gamma_twist * twist_vel) / (-k_twist);
                    }
                }

                let tt_x = tau_twist * nx;
                let tt_y = tau_twist * ny;
                let tt_z = tau_twist * nz;
                dem.torque[i][0] += tt_x;
                dem.torque[i][1] += tt_y;
                dem.torque[i][2] += tt_z;
                if newton {
                    dem.torque[j][0] -= tt_x;
                    dem.torque[j][1] -= tt_y;
                    dem.torque[j][2] -= tt_z;
                }
            } else if twist_vel.abs() > 1e-30 {
                // Constant torque model (existing behavior)
                let tau = mu_tw * f_n_mag.abs() * r_eff;
                let sign_tw = if twist_vel > 0.0 { -1.0 } else { 1.0 };
                let tt_x = sign_tw * tau * nx;
                let tt_y = sign_tw * tau * ny;
                let tt_z = sign_tw * tau * nz;
                dem.torque[i][0] += tt_x;
                dem.torque[i][1] += tt_y;
                dem.torque[i][2] += tt_z;
                if newton {
                    dem.torque[j][0] -= tt_x;
                    dem.torque[j][1] -= tt_y;
                    dem.torque[j][2] -= tt_z;
                }
            }
        }

        // Virial: force on i from j = (-fn + ft)
        // When newton=false, each pair is visited twice so halve virial contribution
        if let Some(ref mut v) = virial {
            if v.active {
                let vs = if newton { 1.0 } else { 0.5 };
                let vfx = (-fn_x + ft_x) * vs;
                let vfy = (-fn_y + ft_y) * vs;
                let vfz = (-fn_z + ft_z) * vs;
                v.add_pair(dx, dy, dz, vfx, vfy, vfz);
            }
        }

        // Store updated spring back (canonical form) and mark active
        let mut new_spring = stored;
        new_spring[0] = sign * sx;
        new_spring[1] = sign * sy;
        new_spring[2] = sign * sz;
        new_spring[3] = sign * roll_disp_x;
        new_spring[4] = sign * roll_disp_y;
        new_spring[5] = sign * roll_disp_z;
        new_spring[6] = sign * twist_disp;
        new_spring[7] = contact_radius;
        match entry_idx {
            Some(idx) => {
                history.contacts[i][idx].1 = new_spring;
                history.contacts[i][idx].2 = true;
            }
            None => history.contacts[i].push((tag_j, new_spring, true)),
        }
    }

    // Prune stale contacts (skipped on the Interior pass; the Boundary pass prunes
    // once after both passes have marked their active contacts).
    if pass != ForcePass::Interior {
        for i in 0..nlocal {
            history.contacts[i].retain(|(_, _, active)| *active);
        }
    }

    // Debug: check total force + torque on all atoms (local + ghost).
    // In a correct Newton's 3rd law implementation, the sum of all forces
    // from pair interactions must be zero (each pair contributes +F to one atom
    // and -F to the other). A nonzero sum means a pair was counted asymmetrically.
    // Skip this check when newton=false (forces only written to i).
    #[cfg(debug_assertions)]
    if newton {
        let total = atoms.len();
        let mut sum_fx = 0.0;
        let mut sum_fy = 0.0;
        let mut sum_fz = 0.0;
        for i in 0..total {
            sum_fx += atoms.force[i][0] as f64;
            sum_fy += atoms.force[i][1] as f64;
            sum_fz += atoms.force[i][2] as f64;
        }
        let sum_f = (sum_fx * sum_fx + sum_fy * sum_fy + sum_fz * sum_fz).sqrt();
        if sum_f > 1e-6 {
            eprintln!(
                "WARNING: nonzero net force after contact: |F|={:.6e} ({:.6e},{:.6e},{:.6e})",
                sum_f, sum_fx, sum_fy, sum_fz
            );
        }
    }
}

/// Hooke (linear spring) contact force — alternative to Hertz-Mindlin.
///
/// Normal: `f_n = kn * delta`, tangential uses `kt` directly.
/// Damping: `gamma = 2 * beta * sqrt(kn_ij * m_r)`.
/// All other features (friction, rolling, twisting, cohesion, JKR) reused.
pub fn hooke_contact_force(
    mut atoms: ResMut<Atom>,
    neighbor: Res<Neighbor>,
    particles: ParticlesWith<
        '_,
        (
            Write<DemAtom>,
            Write<ContactHistoryStore>,
            Optional<Read<BondStore>>,
        ),
    >,
    material_table: Res<MaterialTable>,
    mut virial: Option<ResMut<VirialStress>>,
) {
    particles.with(|(mut dem, mut history, bond_store)| {
        let newton = neighbor.newton;
        let dt = atoms.dt;

        while history.contacts.len() < atoms.len() {
            history.contacts.push(Vec::new());
        }

        let nlocal = atoms.nlocal as usize;
        let mut overlap_warnings = 0usize;

        for i in 0..nlocal {
            for entry in &mut history.contacts[i] {
                entry.2 = false;
            }
        }

        for (i, j) in neighbor.pairs(nlocal) {
            if let Some(ref bonds) = bond_store {
                if bonds.are_excluded(i, j, &atoms.tag) {
                    continue;
                }
            }

            // Skip same-body pairs (sub-spheres of the same rigid body don't interact)
            if dirt_atom::same_body(&dem, i, j) {
                continue;
            }

            let r1 = dem.radius[i];
            let r2 = dem.radius[j];

            let dx = atoms.pos[j][0] as f64 - atoms.pos[i][0] as f64;
            let dy = atoms.pos[j][1] as f64 - atoms.pos[i][1] as f64;
            let dz = atoms.pos[j][2] as f64 - atoms.pos[i][2] as f64;
            let dist_sq = dx * dx + dy * dy + dz * dz;
            let sum_r = r1 + r2;

            if dist_sq >= sum_r * sum_r {
                continue;
            }

            let distance = dist_sq.sqrt();
            if distance == 0.0 {
                continue;
            }

            let r_min = r1.min(r2);
            let delta = (sum_r - distance).min(0.5 * r_min);
            if delta <= 0.0 {
                continue;
            }

            if distance / sum_r < LARGE_OVERLAP_WARN_THRESHOLD {
                overlap_warnings += 1;
                if overlap_warnings > MAX_OVERLAP_WARNINGS {
                    panic!(
                        "Over {} excessive overlaps this step — aborting.",
                        MAX_OVERLAP_WARNINGS
                    );
                }
                // Still compute force (don't skip) — removing repulsion causes runaway.
            }

            let inv_dist = 1.0 / distance;
            let nx = dx * inv_dist;
            let ny = dy * inv_dist;
            let nz = dz * inv_dist;

            let mat_i = atoms.atom_type[i] as usize;
            let mat_j = atoms.atom_type[j] as usize;
            let r_eff = (r1 * r2) / sum_r;
            // For clump sub-spheres inv_mass is 0 (body-integrated); use real mass.
            let inv_m_i = if atoms.inv_mass[i] as f64 > 0.0 {
                atoms.inv_mass[i] as f64
            } else {
                1.0 / atoms.mass[i] as f64
            };
            let inv_m_j = if atoms.inv_mass[j] as f64 > 0.0 {
                atoms.inv_mass[j] as f64
            } else {
                1.0 / atoms.mass[j] as f64
            };
            let m_r = 1.0 / (inv_m_i + inv_m_j);
            let beta = material_table.beta_ij[mat_i][mat_j];
            let mu = material_table.friction_ij[mat_i][mat_j];
            let mu_r = material_table.rolling_friction_ij[mat_i][mat_j];
            let mu_tw = material_table.twisting_friction_ij[mat_i][mat_j];
            let cohesion_energy = material_table.cohesion_energy_ij[mat_i][mat_j];

            let kn = material_table.kn_ij[mat_i][mat_j];
            let kt = material_table.kt_ij[mat_i][mat_j];
            let contact_radius = (r_eff * delta).sqrt();

            // Hooke normal: f_n = kn * delta
            // Damping: gamma_n = 2 * beta * sqrt(kn * m_r)
            let gamma_n = 2.0 * beta * (kn * m_r).sqrt();

            // Relative velocity
            let omega_ix = dem.omega[i][0];
            let omega_iy = dem.omega[i][1];
            let omega_iz = dem.omega[i][2];
            let omega_jx = dem.omega[j][0];
            let omega_jy = dem.omega[j][1];
            let omega_jz = dem.omega[j][2];

            let r1n_x = r1 * nx;
            let r1n_y = r1 * ny;
            let r1n_z = r1 * nz;
            let vc_ix = atoms.vel[i][0] as f64 + (omega_iy * r1n_z - omega_iz * r1n_y);
            let vc_iy = atoms.vel[i][1] as f64 + (omega_iz * r1n_x - omega_ix * r1n_z);
            let vc_iz = atoms.vel[i][2] as f64 + (omega_ix * r1n_y - omega_iy * r1n_x);

            let r2n_x = r2 * nx;
            let r2n_y = r2 * ny;
            let r2n_z = r2 * nz;
            let vc_jx = atoms.vel[j][0] as f64 + (-omega_jy * r2n_z + omega_jz * r2n_y);
            let vc_jy = atoms.vel[j][1] as f64 + (-omega_jz * r2n_x + omega_jx * r2n_z);
            let vc_jz = atoms.vel[j][2] as f64 + (-omega_jx * r2n_y + omega_jy * r2n_x);

            let vr_x = vc_jx - vc_ix;
            let vr_y = vc_jy - vc_iy;
            let vr_z = vc_jz - vc_iz;
            let v_n = vr_x * nx + vr_y * ny + vr_z * nz;

            // Normal force
            let f_n_mag = if cohesion_energy > 0.0 {
                let f_cohesion = cohesion_energy * std::f64::consts::PI * delta * r_eff;
                kn * delta - gamma_n * v_n - f_cohesion
            } else {
                // See the Hertz path: `limit_damping` (default) clamps to repulsive-
                // only; disabling it matches LAMMPS's default (no tensile cutoff).
                let f_total = kn * delta - gamma_n * v_n;
                if material_table.limit_damping {
                    f_total.max(0.0)
                } else {
                    f_total
                }
            };

            let fn_x = f_n_mag * nx;
            let fn_y = f_n_mag * ny;
            let fn_z = f_n_mag * nz;

            atoms.force[i][0] -= fn_x as soil_core::Accum;
            atoms.force[i][1] -= fn_y as soil_core::Accum;
            atoms.force[i][2] -= fn_z as soil_core::Accum;
            if newton {
                atoms.force[j][0] += fn_x as soil_core::Accum;
                atoms.force[j][1] += fn_y as soil_core::Accum;
                atoms.force[j][2] += fn_z as soil_core::Accum;
            }

            // Tangential force
            let vt_x = vr_x - v_n * nx;
            let vt_y = vr_y - v_n * ny;
            let vt_z = vr_z - v_n * nz;

            let tag_i = atoms.tag[i];
            let tag_j = atoms.tag[j];
            let sign: f64 = if tag_i < tag_j { 1.0 } else { -1.0 };

            let entry_idx = history.contacts[i].iter().position(|(t, _, _)| *t == tag_j);
            let stored = match entry_idx {
                Some(idx) => history.contacts[i][idx].1,
                None => zero_contact_history(),
            };

            // History-free `linear_nohistory` tangential model → zero spring (see the
            // Hertz path above); the force reduces to velocity-Coulomb with no
            // accumulated displacement. "history" keeps the incremental Hooke spring.
            let tangential_model = material_table.tangential_model.as_str();
            let nohistory = tangential_model == "linear_nohistory";
            let mindlin_rescale = is_mindlin_rescale(tangential_model);
            let mindlin_force_history = is_mindlin_force_history(tangential_model);
            let f_t_max = mu * f_n_mag.abs();
            let (sx, sy, sz) = if nohistory {
                (0.0, 0.0, 0.0)
            } else if mindlin_force_history {
                let mut fx = sign * stored[0];
                let mut fy = sign * stored[1];
                let mut fz = sign * stored[2];
                let prev_a = stored[7];
                if mindlin_rescale && prev_a > TANGENTIAL_EPSILON && contact_radius < prev_a {
                    let scale = contact_radius / prev_a;
                    fx *= scale;
                    fy *= scale;
                    fz *= scale;
                }
                let f_dot_n = fx * nx + fy * ny + fz * nz;
                fx -= f_dot_n * nx;
                fy -= f_dot_n * ny;
                fz -= f_dot_n * nz;
                fx += kt * vt_x * dt;
                fy += kt * vt_y * dt;
                fz += kt * vt_z * dt;
                (fx, fy, fz)
            } else {
                let mut sx = sign * stored[0];
                let mut sy = sign * stored[1];
                let mut sz = sign * stored[2];
                let prev_a = stored[7];
                if mindlin_rescale && prev_a > TANGENTIAL_EPSILON && contact_radius < prev_a {
                    let scale = contact_radius / prev_a;
                    sx *= scale;
                    sy *= scale;
                    sz *= scale;
                }
                let s_dot_n = sx * nx + sy * ny + sz * nz;
                sx -= s_dot_n * nx;
                sy -= s_dot_n * ny;
                sz -= s_dot_n * nz;
                sx += vt_x * dt;
                sy += vt_y * dt;
                sz += vt_z * dt;

                let s_mag = (sx * sx + sy * sy + sz * sz).sqrt();
                let f_t_spring_mag = kt * s_mag;
                if f_t_spring_mag > f_t_max && f_t_spring_mag > TANGENTIAL_EPSILON {
                    let scale = f_t_max / f_t_spring_mag;
                    sx *= scale;
                    sy *= scale;
                    sz *= scale;
                }
                (sx, sy, sz)
            };

            let gamma_t = 2.0 * SQRT_5_6 * beta * (kt * m_r).sqrt();
            let mut ft_x = (if mindlin_force_history { sx } else { kt * sx }) + gamma_t * vt_x;
            let mut ft_y = (if mindlin_force_history { sy } else { kt * sy }) + gamma_t * vt_y;
            let mut ft_z = (if mindlin_force_history { sz } else { kt * sz }) + gamma_t * vt_z;

            let f_t_mag = (ft_x * ft_x + ft_y * ft_y + ft_z * ft_z).sqrt();
            if f_t_mag > f_t_max && f_t_mag > TANGENTIAL_EPSILON {
                let scale = f_t_max / f_t_mag;
                ft_x *= scale;
                ft_y *= scale;
                ft_z *= scale;
            }

            let (sx, sy, sz) =
                if mindlin_force_history && f_t_mag > f_t_max && f_t_mag > TANGENTIAL_EPSILON {
                    (
                        ft_x - gamma_t * vt_x,
                        ft_y - gamma_t * vt_y,
                        ft_z - gamma_t * vt_z,
                    )
                } else {
                    (sx, sy, sz)
                };

            // Torques
            let ti_x = r1n_y * ft_z - r1n_z * ft_y;
            let ti_y = r1n_z * ft_x - r1n_x * ft_z;
            let ti_z = r1n_x * ft_y - r1n_y * ft_x;
            let tj_x = r2n_y * ft_z - r2n_z * ft_y;
            let tj_y = r2n_z * ft_x - r2n_x * ft_z;
            let tj_z = r2n_x * ft_y - r2n_y * ft_x;

            atoms.force[i][0] += ft_x as soil_core::Accum;
            atoms.force[i][1] += ft_y as soil_core::Accum;
            atoms.force[i][2] += ft_z as soil_core::Accum;
            if newton {
                atoms.force[j][0] -= ft_x as soil_core::Accum;
                atoms.force[j][1] -= ft_y as soil_core::Accum;
                atoms.force[j][2] -= ft_z as soil_core::Accum;
            }
            dem.torque[i][0] += ti_x;
            dem.torque[i][1] += ti_y;
            dem.torque[i][2] += ti_z;
            if newton {
                dem.torque[j][0] += tj_x;
                dem.torque[j][1] += tj_y;
                dem.torque[j][2] += tj_z;
            }

            // Rolling/twisting relative angular velocity
            let or_x = omega_ix - omega_jx;
            let or_y = omega_iy - omega_jy;
            let or_z = omega_iz - omega_jz;
            let or_dot_n = or_x * nx + or_y * ny + or_z * nz;
            let roll_x = or_x - or_dot_n * nx;
            let roll_y = or_y - or_dot_n * ny;
            let roll_z = or_z - or_dot_n * nz;

            let mut roll_disp_x = sign * stored[3];
            let mut roll_disp_y = sign * stored[4];
            let mut roll_disp_z = sign * stored[5];
            let mut twist_disp = sign * stored[6];

            // Rolling resistance
            if mu_r > 0.0 {
                let roll_mag = (roll_x * roll_x + roll_y * roll_y + roll_z * roll_z).sqrt();
                let sds_rolling = material_table.rolling_model == "sds";
                if sds_rolling {
                    let k_roll = material_table.rolling_stiffness_ij[mat_i][mat_j];
                    let gamma_roll = material_table.rolling_damping_ij[mat_i][mat_j];

                    let rd_dot_n = roll_disp_x * nx + roll_disp_y * ny + roll_disp_z * nz;
                    roll_disp_x -= rd_dot_n * nx;
                    roll_disp_y -= rd_dot_n * ny;
                    roll_disp_z -= rd_dot_n * nz;
                    roll_disp_x += roll_x * dt;
                    roll_disp_y += roll_y * dt;
                    roll_disp_z += roll_z * dt;

                    let mut tr_x = -k_roll * roll_disp_x - gamma_roll * roll_x;
                    let mut tr_y = -k_roll * roll_disp_y - gamma_roll * roll_y;
                    let mut tr_z = -k_roll * roll_disp_z - gamma_roll * roll_z;
                    let tr_mag = (tr_x * tr_x + tr_y * tr_y + tr_z * tr_z).sqrt();
                    let tau_max = mu_r * f_n_mag.abs() * r_eff;

                    if tr_mag > tau_max && tr_mag > TANGENTIAL_EPSILON {
                        let scale = tau_max / tr_mag;
                        tr_x *= scale;
                        tr_y *= scale;
                        tr_z *= scale;
                        if k_roll > TANGENTIAL_EPSILON {
                            roll_disp_x = (tr_x + gamma_roll * roll_x) / (-k_roll);
                            roll_disp_y = (tr_y + gamma_roll * roll_y) / (-k_roll);
                            roll_disp_z = (tr_z + gamma_roll * roll_z) / (-k_roll);
                        }
                    }

                    dem.torque[i][0] += tr_x;
                    dem.torque[i][1] += tr_y;
                    dem.torque[i][2] += tr_z;
                    if newton {
                        dem.torque[j][0] -= tr_x;
                        dem.torque[j][1] -= tr_y;
                        dem.torque[j][2] -= tr_z;
                    }
                } else if roll_mag > 1e-30 {
                    let tau_mag = mu_r * f_n_mag.abs() * r_eff;
                    let inv_roll = tau_mag / roll_mag;
                    let tr_x = -inv_roll * roll_x;
                    let tr_y = -inv_roll * roll_y;
                    let tr_z = -inv_roll * roll_z;
                    dem.torque[i][0] += tr_x;
                    dem.torque[i][1] += tr_y;
                    dem.torque[i][2] += tr_z;
                    if newton {
                        dem.torque[j][0] -= tr_x;
                        dem.torque[j][1] -= tr_y;
                        dem.torque[j][2] -= tr_z;
                    }
                }
            }

            // Twisting friction (see the Hertz-Mindlin path for model semantics).
            if material_table.twisting_model == "marshall" {
                // Marshall (2009) derived-coefficient twisting on the linear (Hooke)
                // tangential model: k_twist = ½ k_t a², γ_twist = ½ γ_t a²,
                // μ_twist = (2/3) a μ_t with a = √(R* δ) and k_t = kt, γ_t the Hooke
                // tangential spring/damping computed above.
                let twist_vel = or_dot_n;
                let a_sq = delta * r_eff;
                let a = a_sq.sqrt();
                let k_twist = 0.5 * kt * a_sq;
                let gamma_twist = 0.5 * gamma_t * a_sq;
                let mu_twist = (2.0 / 3.0) * a * mu;

                twist_disp += twist_vel * dt;

                let mut tau_twist = -k_twist * twist_disp - gamma_twist * twist_vel;
                let tau_max = mu_twist * f_n_mag.abs();
                if tau_twist.abs() > tau_max {
                    tau_twist = tau_twist.signum() * tau_max;
                    if k_twist > TANGENTIAL_EPSILON {
                        twist_disp = (tau_twist + gamma_twist * twist_vel) / (-k_twist);
                    }
                }

                let tt_x = tau_twist * nx;
                let tt_y = tau_twist * ny;
                let tt_z = tau_twist * nz;
                dem.torque[i][0] += tt_x;
                dem.torque[i][1] += tt_y;
                dem.torque[i][2] += tt_z;
                if newton {
                    dem.torque[j][0] -= tt_x;
                    dem.torque[j][1] -= tt_y;
                    dem.torque[j][2] -= tt_z;
                }
            } else if mu_tw > 0.0 {
                let twist_vel = or_dot_n;
                let sds_twisting = material_table.twisting_model == "sds";
                if sds_twisting {
                    let k_twist = material_table.twisting_stiffness_ij[mat_i][mat_j];
                    let gamma_twist = material_table.twisting_damping_ij[mat_i][mat_j];

                    twist_disp += twist_vel * dt;

                    let mut tau_twist = -k_twist * twist_disp - gamma_twist * twist_vel;
                    let tau_max = mu_tw * f_n_mag.abs() * r_eff;

                    if tau_twist.abs() > tau_max {
                        tau_twist = tau_twist.signum() * tau_max;
                        if k_twist > TANGENTIAL_EPSILON {
                            twist_disp = (tau_twist + gamma_twist * twist_vel) / (-k_twist);
                        }
                    }

                    let tt_x = tau_twist * nx;
                    let tt_y = tau_twist * ny;
                    let tt_z = tau_twist * nz;
                    dem.torque[i][0] += tt_x;
                    dem.torque[i][1] += tt_y;
                    dem.torque[i][2] += tt_z;
                    if newton {
                        dem.torque[j][0] -= tt_x;
                        dem.torque[j][1] -= tt_y;
                        dem.torque[j][2] -= tt_z;
                    }
                } else if twist_vel.abs() > 1e-30 {
                    let tau = mu_tw * f_n_mag.abs() * r_eff;
                    let sign_tw = if twist_vel > 0.0 { -1.0 } else { 1.0 };
                    let tt_x = sign_tw * tau * nx;
                    let tt_y = sign_tw * tau * ny;
                    let tt_z = sign_tw * tau * nz;
                    dem.torque[i][0] += tt_x;
                    dem.torque[i][1] += tt_y;
                    dem.torque[i][2] += tt_z;
                    if newton {
                        dem.torque[j][0] -= tt_x;
                        dem.torque[j][1] -= tt_y;
                        dem.torque[j][2] -= tt_z;
                    }
                }
            }

            // Virial
            if let Some(ref mut v) = virial {
                if v.active {
                    let vs = if newton { 1.0 } else { 0.5 };
                    let vfx = (-fn_x + ft_x) * vs;
                    let vfy = (-fn_y + ft_y) * vs;
                    let vfz = (-fn_z + ft_z) * vs;
                    v.add_pair(dx, dy, dz, vfx, vfy, vfz);
                }
            }

            let mut new_spring = stored;
            new_spring[0] = sign * sx;
            new_spring[1] = sign * sy;
            new_spring[2] = sign * sz;
            new_spring[3] = sign * roll_disp_x;
            new_spring[4] = sign * roll_disp_y;
            new_spring[5] = sign * roll_disp_z;
            new_spring[6] = sign * twist_disp;
            new_spring[7] = contact_radius;
            match entry_idx {
                Some(idx) => {
                    history.contacts[i][idx].1 = new_spring;
                    history.contacts[i][idx].2 = true;
                }
                None => history.contacts[i].push((tag_j, new_spring, true)),
            }
        }

        for i in 0..nlocal {
            history.contacts[i].retain(|(_, _, active)| *active);
        }
    });
}