rust_physics_engine 0.2.0

A zero-dependency Rust library for physics, mathematics and engineering computation — 6,365 public functions across 71 modules
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
//! Lattice Boltzmann method: D2Q9 with BGK/TRT/MRT/cumulant-style
//! collisions, bounce-back solids, Zou-He open boundaries, Guo forcing,
//! D3Q19 and D3Q27 lattices, and classic benchmarks.

use crate::math::{Vec2, Vec3};

/// D2Q9 lattice velocities (0 = rest, 1-4 axis, 5-8 diagonal).
const E2: [(i64, i64); 9] = [
    (0, 0),
    (1, 0),
    (0, 1),
    (-1, 0),
    (0, -1),
    (1, 1),
    (-1, 1),
    (-1, -1),
    (1, -1),
];
const W2: [f64; 9] = [
    4.0 / 9.0,
    1.0 / 9.0,
    1.0 / 9.0,
    1.0 / 9.0,
    1.0 / 9.0,
    1.0 / 36.0,
    1.0 / 36.0,
    1.0 / 36.0,
    1.0 / 36.0,
];
/// Opposite direction of each D2Q9 velocity.
const OPP2: [usize; 9] = [0, 3, 4, 1, 2, 7, 8, 5, 6];

/// Collision operator selector.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Collision {
    /// Single-relaxation-time BGK.
    Bgk,
    /// Two-relaxation-time with the given "magic" parameter Λ
    /// (Λ = 1/4 gives optimal stability, 3/16 exact walls).
    Trt { magic: f64 },
    /// Multiple-relaxation-time (Lallemand-Luo relaxation set).
    Mrt,
    /// Cumulant-style: MRT with aggressively relaxed high-order moments.
    Cumulant,
}

fn feq2(rho: f64, u: Vec2, k: usize) -> f64 {
    let (ex, ey) = (E2[k].0 as f64, E2[k].1 as f64);
    let eu = ex * u.x + ey * u.y;
    let u2 = u.x * u.x + u.y * u.y;
    W2[k] * rho * (1.0 + 3.0 * eu + 4.5 * eu * eu - 1.5 * u2)
}

/// D2Q9 lattice Boltzmann solver.
pub struct LbmD2Q9 {
    pub nx: usize,
    pub ny: usize,
    pub f: Vec<[f64; 9]>,
    pub tau: f64,
    pub solid: Vec<bool>,
    pub force: Vec2,
    pub collision: Collision,
    /// Periodic wrap in x/y where no other boundary applies.
    pub periodic_x: bool,
    pub periodic_y: bool,
    /// Inlet velocity on the left wall (Zou-He), if set.
    inlet: Option<f64>,
    /// Outlet density on the right wall (Zou-He), if set.
    outlet: Option<f64>,
}

impl LbmD2Q9 {
    /// Quiescent unit-density fluid.
    #[must_use]
    pub fn new(nx: usize, ny: usize, tau: f64) -> Self {
        let mut s = Self {
            nx,
            ny,
            f: vec![[0.0; 9]; nx * ny],
            tau,
            solid: vec![false; nx * ny],
            force: Vec2::ZERO,
            collision: Collision::Bgk,
            periodic_x: true,
            periodic_y: true,
            inlet: None,
            outlet: None,
        };
        s.init_equilibrium(1.0, Vec2::ZERO);
        s
    }

    /// Reset every node to equilibrium at (rho, u).
    pub fn init_equilibrium(&mut self, rho: f64, u: Vec2) {
        for node in self.f.iter_mut() {
            for (k, fk) in node.iter_mut().enumerate() {
                *fk = feq2(rho, u, k);
            }
        }
    }

    /// Kinematic viscosity ν = (τ − 1/2)/3 in lattice units.
    #[must_use]
    pub fn viscosity(&self) -> f64 {
        (self.tau - 0.5) / 3.0
    }

    /// Densities at all nodes.
    #[must_use]
    pub fn density(&self) -> Vec<f64> {
        self.f.iter().map(|n| n.iter().sum()).collect()
    }

    /// Velocities at all nodes (forcing shift included).
    #[must_use]
    pub fn velocity(&self) -> Vec<Vec2> {
        self.f
            .iter()
            .map(|n| {
                let rho: f64 = n.iter().sum();
                let mut u = Vec2::ZERO;
                for k in 0..9 {
                    u.x += E2[k].0 as f64 * n[k];
                    u.y += E2[k].1 as f64 * n[k];
                }
                (u + self.force * 0.5) * (1.0 / rho.max(1e-12))
            })
            .collect()
    }

    /// Largest lattice Mach number |u|/c_s.
    #[must_use]
    pub fn mach_max(&self) -> f64 {
        let cs = (1.0_f64 / 3.0).sqrt();
        self.velocity()
            .iter()
            .map(Vec2::magnitude)
            .fold(0.0_f64, f64::max)
            / cs
    }

    /// Collision step (with Guo forcing).
    pub fn collide(&mut self) {
        let tau = self.tau;
        let omega = 1.0 / tau;
        let force = self.force;
        for (c, node) in self.f.iter_mut().enumerate() {
            if self.solid[c] {
                continue;
            }
            let rho: f64 = node.iter().sum();
            let mut u = Vec2::ZERO;
            for k in 0..9 {
                u.x += E2[k].0 as f64 * node[k];
                u.y += E2[k].1 as f64 * node[k];
            }
            u = (u + force * 0.5) * (1.0 / rho.max(1e-12));
            let feq: [f64; 9] = std::array::from_fn(|k| feq2(rho, u, k));
            match self.collision {
                Collision::Bgk => {
                    for k in 0..9 {
                        node[k] += omega * (feq[k] - node[k]);
                    }
                }
                Collision::Trt { magic } => {
                    // Symmetric part relaxes with ω⁺ = 1/τ; antisymmetric
                    // with ω⁻ chosen through the magic parameter
                    // Λ = (τ⁺ − 1/2)(τ⁻ − 1/2).
                    let lam_plus = tau - 0.5;
                    let tau_minus = magic / lam_plus + 0.5;
                    let om_m = 1.0 / tau_minus;
                    let f_old = *node;
                    for k in 0..9 {
                        let ko = OPP2[k];
                        let f_p = 0.5 * (f_old[k] + f_old[ko]);
                        let f_m = 0.5 * (f_old[k] - f_old[ko]);
                        let feq_p = 0.5 * (feq[k] + feq[ko]);
                        let feq_m = 0.5 * (feq[k] - feq[ko]);
                        node[k] = (f_p + omega * (feq_p - f_p)) + (f_m + om_m * (feq_m - f_m));
                    }
                }
                Collision::Mrt | Collision::Cumulant => {
                    // Moment-space relaxation (Lallemand-Luo D2Q9 set).
                    // Moments: rho, e, eps, jx, qx, jy, qy, pxx, pxy.
                    let f = *node;
                    let m = [
                        f.iter().sum::<f64>(),
                        -4.0 * f[0] - f[1] - f[2] - f[3] - f[4]
                            + 2.0 * (f[5] + f[6] + f[7] + f[8]),
                        4.0 * f[0] - 2.0 * (f[1] + f[2] + f[3] + f[4])
                            + f[5]
                            + f[6]
                            + f[7]
                            + f[8],
                        f[1] - f[3] + f[5] - f[6] - f[7] + f[8],
                        -2.0 * (f[1] - f[3]) + f[5] - f[6] - f[7] + f[8],
                        f[2] - f[4] + f[5] + f[6] - f[7] - f[8],
                        -2.0 * (f[2] - f[4]) + f[5] + f[6] - f[7] - f[8],
                        f[1] - f[2] + f[3] - f[4],
                        f[5] - f[6] + f[7] - f[8],
                    ];
                    let jx = m[3];
                    let jy = m[5];
                    let rho_m = m[0];
                    let meq = [
                        rho_m,
                        -2.0 * rho_m + 3.0 * (jx * jx + jy * jy) / rho_m.max(1e-12),
                        rho_m - 3.0 * (jx * jx + jy * jy) / rho_m.max(1e-12),
                        jx,
                        -jx,
                        jy,
                        -jy,
                        (jx * jx - jy * jy) / rho_m.max(1e-12),
                        jx * jy / rho_m.max(1e-12),
                    ];
                    // Relaxation rates: shear moments (pxx, pxy) use 1/τ;
                    // others fixed (cumulant relaxes bulk faster).
                    let s_bulk = if self.collision == Collision::Cumulant { 1.9 } else { 1.4 };
                    let s = [
                        0.0, s_bulk, 1.4, 0.0, 1.2, 0.0, 1.2, omega, omega,
                    ];
                    let mut m_post = [0.0; 9];
                    for i in 0..9 {
                        m_post[i] = m[i] + s[i] * (meq[i] - m[i]);
                    }
                    // Inverse moment transform (orthogonal basis norms).
                    let n0 = m_post[0] / 9.0;
                    let n1 = m_post[1] / 36.0;
                    let n2 = m_post[2] / 36.0;
                    let n3 = m_post[3] / 6.0;
                    let n4 = m_post[4] / 12.0;
                    let n5 = m_post[5] / 6.0;
                    let n6 = m_post[6] / 12.0;
                    let n7 = m_post[7] / 4.0;
                    let n8 = m_post[8] / 4.0;
                    node[0] = n0 - 4.0 * n1 + 4.0 * n2;
                    node[1] = n0 - n1 - 2.0 * n2 + n3 - 2.0 * n4 + n7;
                    node[2] = n0 - n1 - 2.0 * n2 + n5 - 2.0 * n6 - n7;
                    node[3] = n0 - n1 - 2.0 * n2 - n3 + 2.0 * n4 + n7;
                    node[4] = n0 - n1 - 2.0 * n2 - n5 + 2.0 * n6 - n7;
                    node[5] = n0 + 2.0 * n1 + n2 + n3 + n4 + n5 + n6 + n8;
                    node[6] = n0 + 2.0 * n1 + n2 - n3 - n4 + n5 + n6 - n8;
                    node[7] = n0 + 2.0 * n1 + n2 - n3 - n4 - n5 - n6 + n8;
                    node[8] = n0 + 2.0 * n1 + n2 + n3 + n4 - n5 - n6 - n8;
                }
            }
            // Guo forcing term with relaxation-matched prefactors: the
            // odd (momentum-carrying) part uses the odd rate, the even
            // part the even rate; plain BGK uses ω for both.
            if force.x != 0.0 || force.y != 0.0 {
                let (fac_odd, fac_even) = match self.collision {
                    Collision::Bgk => (1.0 - 0.5 * omega, 1.0 - 0.5 * omega),
                    Collision::Trt { magic } => {
                        let om_m = 1.0 / (magic / (tau - 0.5) + 0.5);
                        (1.0 - 0.5 * om_m, 1.0 - 0.5 * omega)
                    }
                    // Momentum moments are conserved (s = 0) in the MRT
                    // set, so their force projection enters in full; the
                    // stress projection uses ω.
                    Collision::Mrt | Collision::Cumulant => (1.0, 1.0 - 0.5 * omega),
                };
                for k in 0..9 {
                    let (ex, ey) = (E2[k].0 as f64, E2[k].1 as f64);
                    let eu = ex * u.x + ey * u.y;
                    let s_odd = 3.0 * (ex * force.x + ey * force.y);
                    let s_even = -3.0 * (u.x * force.x + u.y * force.y)
                        + 9.0 * eu * (ex * force.x + ey * force.y);
                    node[k] += W2[k] * (fac_odd * s_odd + fac_even * s_even);
                }
            }
        }
    }

    /// Streaming step (periodic wrap; solids handled by bounce-back).
    pub fn stream(&mut self) {
        let (nx, ny) = (self.nx, self.ny);
        let mut new_f = self.f.clone();
        for j in 0..ny as i64 {
            for i in 0..nx as i64 {
                let c = (j as usize) * nx + i as usize;
                for k in 0..9 {
                    let (di, dj) = E2[k];
                    let mut ti = i + di;
                    let mut tj = j + dj;
                    let mut blocked = false;
                    if ti < 0 || ti >= nx as i64 {
                        if self.periodic_x {
                            ti = ti.rem_euclid(nx as i64);
                        } else {
                            blocked = true;
                        }
                    }
                    if tj < 0 || tj >= ny as i64 {
                        if self.periodic_y {
                            tj = tj.rem_euclid(ny as i64);
                        } else {
                            blocked = true;
                        }
                    }
                    if blocked {
                        // Domain wall without wrap: bounce back in place.
                        new_f[c][OPP2[k]] = self.f[c][k];
                    } else {
                        new_f[(tj as usize) * nx + ti as usize][k] = self.f[c][k];
                    }
                }
            }
        }
        self.f = new_f;
    }

    /// Half-way bounce-back on solid nodes.
    pub fn bounce_back(&mut self) {
        for c in 0..self.nx * self.ny {
            if self.solid[c] {
                let node = self.f[c];
                for k in 0..9 {
                    self.f[c][k] = node[OPP2[k]];
                }
            }
        }
    }

    /// Zou-He velocity inlet on the left wall (x = 0), horizontal
    /// velocity `u`.
    pub fn zou_he_velocity_inlet(&mut self, u: f64) {
        self.inlet = Some(u);
        self.periodic_x = false;
        let nx = self.nx;
        for j in 0..self.ny {
            let c = j * nx;
            let f = &mut self.f[c];
            let rho = (f[0] + f[2] + f[4] + 2.0 * (f[3] + f[6] + f[7])) / (1.0 - u);
            f[1] = f[3] + 2.0 / 3.0 * rho * u;
            f[5] = f[7] - 0.5 * (f[2] - f[4]) + rho * u / 6.0;
            f[8] = f[6] + 0.5 * (f[2] - f[4]) + rho * u / 6.0;
        }
    }

    /// Zou-He pressure (density) outlet on the right wall.
    pub fn zou_he_pressure_outlet(&mut self, rho: f64) {
        self.outlet = Some(rho);
        self.periodic_x = false;
        let nx = self.nx;
        for j in 0..self.ny {
            let c = j * nx + (nx - 1);
            let f = &mut self.f[c];
            let u = (f[0] + f[2] + f[4] + 2.0 * (f[1] + f[5] + f[8])) / rho - 1.0;
            f[3] = f[1] - 2.0 / 3.0 * rho * u;
            f[7] = f[5] + 0.5 * (f[2] - f[4]) - rho * u / 6.0;
            f[6] = f[8] - 0.5 * (f[2] - f[4]) - rho * u / 6.0;
        }
    }

    /// Make both axes periodic (clears open boundaries).
    pub fn periodic(&mut self) {
        self.periodic_x = true;
        self.periodic_y = true;
        self.inlet = None;
        self.outlet = None;
    }

    /// One full update: collide, stream, bounce-back, boundaries.
    pub fn step(&mut self) {
        self.collide();
        self.stream();
        self.bounce_back();
        if let Some(u) = self.inlet {
            self.zou_he_velocity_inlet(u);
        }
        if let Some(rho) = self.outlet {
            self.zou_he_pressure_outlet(rho);
        }
    }

    /// Run `n` steps.
    pub fn run(&mut self, n: usize) {
        for _ in 0..n {
            self.step();
        }
    }

    /// Momentum-exchange drag on the solid set (lattice units).
    #[must_use]
    pub fn drag_on_solid(&self) -> Vec2 {
        let (nx, ny) = (self.nx, self.ny);
        let mut fx = 0.0;
        let mut fy = 0.0;
        for j in 0..ny as i64 {
            for i in 0..nx as i64 {
                let c = (j as usize) * nx + i as usize;
                if self.solid[c] {
                    continue;
                }
                for (k, &(di, dj)) in E2.iter().enumerate().skip(1) {
                    let ti = (i + di).rem_euclid(nx as i64) as usize;
                    let tj = (j + dj).rem_euclid(ny as i64) as usize;
                    if self.solid[tj * nx + ti] {
                        // Momentum transferred by the bounced population.
                        let transfer = 2.0 * self.f[c][k];
                        fx += transfer * di as f64;
                        fy += transfer * dj as f64;
                    }
                }
            }
        }
        Vec2::new(fx, fy)
    }

    /// Vorticity at interior nodes (central differences).
    #[must_use]
    pub fn vorticity(&self) -> Vec<f64> {
        let (nx, ny) = (self.nx, self.ny);
        let vel = self.velocity();
        let mut w = vec![0.0; nx * ny];
        for j in 1..ny - 1 {
            for i in 1..nx - 1 {
                let dv_dx = (vel[j * nx + i + 1].y - vel[j * nx + i - 1].y) / 2.0;
                let du_dy = (vel[(j + 1) * nx + i].x - vel[(j - 1) * nx + i].x) / 2.0;
                w[j * nx + i] = dv_dx - du_dy;
            }
        }
        w
    }
}

// --- D3Q19 ---------------------------------------------------------------

const E19: [(i64, i64, i64); 19] = [
    (0, 0, 0),
    (1, 0, 0),
    (-1, 0, 0),
    (0, 1, 0),
    (0, -1, 0),
    (0, 0, 1),
    (0, 0, -1),
    (1, 1, 0),
    (-1, -1, 0),
    (1, -1, 0),
    (-1, 1, 0),
    (1, 0, 1),
    (-1, 0, -1),
    (1, 0, -1),
    (-1, 0, 1),
    (0, 1, 1),
    (0, -1, -1),
    (0, 1, -1),
    (0, -1, 1),
];

fn w19(k: usize) -> f64 {
    if k == 0 {
        1.0 / 3.0
    } else if k <= 6 {
        1.0 / 18.0
    } else {
        1.0 / 36.0
    }
}

fn opp19(k: usize) -> usize {
    const OPP: [usize; 19] =
        [0, 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 16, 15, 18, 17];
    OPP[k]
}

/// D3Q19 BGK lattice Boltzmann solver (periodic + bounce-back).
pub struct LbmD3Q19 {
    pub nx: usize,
    pub ny: usize,
    pub nz: usize,
    pub f: Vec<[f64; 19]>,
    pub tau: f64,
    pub solid: Vec<bool>,
    pub force: Vec3,
}

impl LbmD3Q19 {
    /// Quiescent unit-density fluid.
    #[must_use]
    pub fn new(nx: usize, ny: usize, nz: usize, tau: f64) -> Self {
        let mut s = Self {
            nx,
            ny,
            nz,
            f: vec![[0.0; 19]; nx * ny * nz],
            tau,
            solid: vec![false; nx * ny * nz],
            force: Vec3::ZERO,
        };
        for node in s.f.iter_mut() {
            for (k, fk) in node.iter_mut().enumerate() {
                *fk = w19(k);
            }
        }
        s
    }

    /// Kinematic viscosity.
    #[must_use]
    pub fn viscosity(&self) -> f64 {
        (self.tau - 0.5) / 3.0
    }

    /// Node velocities.
    #[must_use]
    pub fn velocity(&self) -> Vec<Vec3> {
        self.f
            .iter()
            .map(|n| {
                let rho: f64 = n.iter().sum();
                let mut u = Vec3::ZERO;
                for (k, &fk) in n.iter().enumerate() {
                    u.x += E19[k].0 as f64 * fk;
                    u.y += E19[k].1 as f64 * fk;
                    u.z += E19[k].2 as f64 * fk;
                }
                (u + self.force * 0.5) * (1.0 / rho.max(1e-12))
            })
            .collect()
    }

    /// One BGK step with Guo forcing.
    pub fn step(&mut self) {
        let omega = 1.0 / self.tau;
        let force = self.force;
        // Collide.
        for (c, node) in self.f.iter_mut().enumerate() {
            if self.solid[c] {
                continue;
            }
            let rho: f64 = node.iter().sum();
            let mut u = Vec3::ZERO;
            for (k, &fk) in node.iter().enumerate() {
                u.x += E19[k].0 as f64 * fk;
                u.y += E19[k].1 as f64 * fk;
                u.z += E19[k].2 as f64 * fk;
            }
            u = (u + force * 0.5) * (1.0 / rho.max(1e-12));
            let u2 = u.magnitude_squared();
            for (k, fk) in node.iter_mut().enumerate() {
                let eu =
                    E19[k].0 as f64 * u.x + E19[k].1 as f64 * u.y + E19[k].2 as f64 * u.z;
                let feq = w19(k) * rho * (1.0 + 3.0 * eu + 4.5 * eu * eu - 1.5 * u2);
                *fk += omega * (feq - *fk);
                let ef = E19[k].0 as f64 * force.x
                    + E19[k].1 as f64 * force.y
                    + E19[k].2 as f64 * force.z;
                let uf = u.dot(&force);
                *fk += w19(k) * (1.0 - 0.5 * omega) * (3.0 * ef - 3.0 * uf + 9.0 * eu * ef);
            }
        }
        // Stream (fully periodic).
        let (nx, ny, nz) = (self.nx, self.ny, self.nz);
        let mut new_f = self.f.clone();
        for k in 0..19 {
            let (di, dj, dk) = E19[k];
            for z in 0..nz as i64 {
                for y in 0..ny as i64 {
                    for x in 0..nx as i64 {
                        let c = ((z as usize) * ny + y as usize) * nx + x as usize;
                        let tx = (x + di).rem_euclid(nx as i64) as usize;
                        let ty = (y + dj).rem_euclid(ny as i64) as usize;
                        let tz = (z + dk).rem_euclid(nz as i64) as usize;
                        new_f[(tz * ny + ty) * nx + tx][k] = self.f[c][k];
                    }
                }
            }
        }
        self.f = new_f;
        // Bounce back.
        for c in 0..nx * ny * nz {
            if self.solid[c] {
                let node = self.f[c];
                for k in 0..19 {
                    self.f[c][k] = node[opp19(k)];
                }
            }
        }
    }
}

/// D3Q27 lattice constants (velocities and weights), for custom solvers.
pub struct LbmD3Q27;

impl LbmD3Q27 {
    /// The 27 lattice velocities.
    #[must_use]
    pub fn velocities() -> Vec<(i64, i64, i64)> {
        let mut v = Vec::with_capacity(27);
        for z in -1..=1_i64 {
            for y in -1..=1_i64 {
                for x in -1..=1_i64 {
                    v.push((x, y, z));
                }
            }
        }
        v
    }

    /// Weight of velocity (x, y, z).
    #[must_use]
    pub fn weight(e: (i64, i64, i64)) -> f64 {
        match e.0.abs() + e.1.abs() + e.2.abs() {
            0 => 8.0 / 27.0,
            1 => 2.0 / 27.0,
            2 => 1.0 / 54.0,
            _ => 1.0 / 216.0,
        }
    }
}

// --- Benchmarks ----------------------------------------------------------

/// Body-force-driven Poiseuille channel: solid walls at j = 0 and
/// j = ny−1, periodic in x.
#[must_use]
pub fn lbm_poiseuille_2d(nx: usize, ny: usize, tau: f64, force: f64) -> LbmD2Q9 {
    let mut lbm = LbmD2Q9::new(nx, ny, tau);
    lbm.force = Vec2::new(force, 0.0);
    for i in 0..nx {
        lbm.solid[i] = true;
        lbm.solid[(ny - 1) * nx + i] = true;
    }
    lbm
}

/// Exact Poiseuille profile u(y) for channel half-width walls at y = 0
/// and y = h.
#[must_use]
pub fn poiseuille_exact(y: f64, h: f64, force: f64, nu: f64) -> f64 {
    force / (2.0 * nu) * y * (h - y)
}

/// Flow past a cylinder at Reynolds number `re` (Zou-He inlet/outlet).
#[must_use]
pub fn lbm_cylinder(nx: usize, ny: usize, re: f64) -> LbmD2Q9 {
    let u_in = 0.08;
    let d = ny as f64 / 5.0;
    let nu = u_in * d / re;
    let tau = 3.0 * nu + 0.5;
    let mut lbm = LbmD2Q9::new(nx, ny, tau);
    lbm.periodic_x = false;
    // Cylinder at (nx/4, ny/2 + tiny offset to trigger shedding).
    let (cx, cy) = (nx as f64 / 4.0, ny as f64 / 2.0 + 0.5);
    for j in 0..ny {
        for i in 0..nx {
            let dx = i as f64 - cx;
            let dy = j as f64 - cy;
            if (dx * dx + dy * dy).sqrt() < d / 2.0 {
                lbm.solid[j * nx + i] = true;
            }
        }
    }
    // Top/bottom walls.
    for i in 0..nx {
        lbm.solid[i] = true;
        lbm.solid[(ny - 1) * nx + i] = true;
    }
    lbm.periodic_y = false;
    lbm.init_equilibrium(1.0, Vec2::new(u_in, 0.0));
    lbm.inlet = Some(u_in);
    lbm.outlet = Some(1.0);
    lbm
}

/// Lid-driven cavity at Reynolds number `re` (moving top wall via
/// bounce-back with wall velocity).
#[must_use]
pub fn lbm_lid_cavity(n: usize, re: f64) -> LbmD2Q9 {
    let u_lid = 0.1;
    let nu = u_lid * n as f64 / re;
    let tau = 3.0 * nu + 0.5;
    let mut lbm = LbmD2Q9::new(n, n, tau);
    lbm.periodic_x = false;
    lbm.periodic_y = false;
    // Side and bottom walls solid; the lid handled in the driver below.
    for j in 0..n {
        lbm.solid[j * n] = true;
        lbm.solid[j * n + n - 1] = true;
    }
    for i in 0..n {
        lbm.solid[i] = true;
    }
    lbm
}

/// Apply the moving-lid boundary to a cavity solver for one step: after
/// the regular step, impose the lid velocity on the top row (Zou-He).
pub fn lbm_cavity_step(lbm: &mut LbmD2Q9, u_lid: f64) {
    lbm.step();
    let (nx, ny) = (lbm.nx, lbm.ny);
    for i in 0..nx {
        let c = (ny - 1) * nx + i;
        let f = &mut lbm.f[c];
        // Zou-He moving top wall (v = 0, u = u_lid).
        let rho = f[0] + f[1] + f[3] + 2.0 * (f[2] + f[5] + f[6]);
        f[4] = f[2];
        f[7] = f[5] + 0.5 * (f[1] - f[3]) - 0.5 * rho * u_lid;
        f[8] = f[6] - 0.5 * (f[1] - f[3]) + 0.5 * rho * u_lid;
    }
}

/// Double-distribution thermal LBM: returns (flow lattice, temperature
/// lattice); the temperature field advects with the flow and feeds back
/// as a Boussinesq force. Step both manually with `thermal_step`.
#[must_use]
pub fn lbm_thermal(nx: usize, ny: usize, tau_f: f64, tau_g: f64) -> (LbmD2Q9, LbmD2Q9) {
    let flow = LbmD2Q9::new(nx, ny, tau_f);
    let mut temp = LbmD2Q9::new(nx, ny, tau_g);
    // Initialize a hot stripe at the bottom.
    for j in 0..ny {
        for i in 0..nx {
            let t0: f64 = if j < ny / 8 { 1.0 } else { 0.0 };
            for (k, fk) in temp.f[j * nx + i].iter_mut().enumerate() {
                *fk = W2[k] * t0.max(1e-6);
            }
        }
    }
    (flow, temp)
}

/// One coupled Boussinesq step of the double-distribution system.
pub fn thermal_step(flow: &mut LbmD2Q9, temp: &mut LbmD2Q9, buoyancy: f64) {
    // Advect the temperature with the flow velocity (equilibrium built
    // from the flow's u).
    let vel = flow.velocity();
    let omega_g = 1.0 / temp.tau;
    for (c, node) in temp.f.iter_mut().enumerate() {
        let t: f64 = node.iter().sum();
        for (k, fk) in node.iter_mut().enumerate() {
            let feq = feq2(t, vel[c], k);
            *fk += omega_g * (feq - *fk);
        }
    }
    temp.stream();
    // Boussinesq: buoyancy force from the temperature.
    let t_field: Vec<f64> = temp.f.iter().map(|n| n.iter().sum()).collect();
    let t_mean = t_field.iter().sum::<f64>() / t_field.len() as f64;
    // Uniform mean force per node is applied globally (spatially varying
    // forcing needs per-node force; approximate with the mean of the
    // positive anomaly acting upward).
    let anomaly = t_field.iter().map(|t| t - t_mean).fold(0.0_f64, f64::max);
    flow.force = Vec2::new(0.0, buoyancy * anomaly);
    flow.step();
}

/// Convert a lattice velocity to physical units given the lattice
/// spacing and time step.
#[must_use]
pub fn lbm_to_physical(u_lattice: f64, dx: f64, dt: f64) -> f64 {
    u_lattice * dx / dt
}

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

    #[test]
    fn test_conservation_and_viscosity() {
        let mut lbm = LbmD2Q9::new(24, 24, 0.8);
        // Random-ish perturbation.
        for (c, node) in lbm.f.iter_mut().enumerate() {
            node[1] += 0.01 * ((c * 7919 % 13) as f64 / 13.0 - 0.5);
        }
        let mass0: f64 = lbm.density().iter().sum();
        lbm.run(50);
        let mass: f64 = lbm.density().iter().sum();
        assert!((mass / mass0 - 1.0).abs() < 1e-12, "mass drift");
        assert!((lbm.viscosity() - 0.1).abs() < 1e-12);
        assert!(lbm.mach_max() < 0.3);
        // Momentum conserved without forces/solids.
        let vel = lbm.velocity();
        let rho = lbm.density();
        let px: f64 = vel.iter().zip(&rho).map(|(v, r)| v.x * r).sum();
        lbm.run(20);
        let vel2 = lbm.velocity();
        let rho2 = lbm.density();
        let px2: f64 = vel2.iter().zip(&rho2).map(|(v, r)| v.x * r).sum();
        assert!((px - px2).abs() < 1e-10, "momentum drift {px} vs {px2}");
    }

    #[test]
    fn test_poiseuille_matches_exact() {
        let (nx, ny) = (8, 33);
        let force = 1e-6;
        let tau = 0.9;
        let mut lbm = lbm_poiseuille_2d(nx, ny, tau, force);
        lbm.run(6000);
        let nu = lbm.viscosity();
        let vel = lbm.velocity();
        // Channel interior spans j = 1..ny-1; walls act at half-way
        // between the solid and fluid nodes.
        let h = (ny - 2) as f64; // wall planes sit half-way past the fluid
        let mut max_rel = 0.0_f64;
        let u_max_exact = poiseuille_exact(h / 2.0, h, force, nu);
        for j in 1..ny - 1 {
            let y = (j - 1) as f64 + 0.5; // distance from the lower wall plane
            let u = vel[j * nx + 4].x;
            let ue = poiseuille_exact(y, h, force, nu);
            max_rel = max_rel.max((u - ue).abs() / u_max_exact);
        }
        assert!(max_rel < 0.03, "Poiseuille profile error {max_rel}");
        // Center beats near-wall velocity.
        let u_c = vel[(ny / 2) * nx + 4].x;
        let u_w = vel[nx + 4].x;
        assert!(u_c > 3.0 * u_w, "no parabola: {u_c} vs {u_w}");
    }

    #[test]
    fn test_collision_operators_stable() {
        for coll in [
            Collision::Bgk,
            Collision::Trt { magic: 0.25 },
            Collision::Mrt,
            Collision::Cumulant,
        ] {
            let mut lbm = lbm_poiseuille_2d(8, 17, 0.7, 1e-6);
            lbm.collision = coll;
            lbm.run(6000);
            let vel = lbm.velocity();
            assert!(
                vel.iter().all(|v| v.x.is_finite() && v.x.abs() < 0.3),
                "{coll:?} unstable"
            );
            // Profile still parabola-ish.
            let u_c = vel[8 * 8 + 4].x;
            let u_w = vel[8 + 4].x;
            assert!(u_c > u_w, "{coll:?} profile");
            // TRT/MRT match BGK viscosity closely on this laminar flow.
            let nu = lbm.viscosity();
            let h = 15.0; // ny - 2 with half-way wall planes
            let ue = poiseuille_exact(h / 2.0, h, 1e-6, nu);
            assert!((u_c / ue - 1.0).abs() < 0.1, "{coll:?} center {u_c} vs {ue}");
        }
    }

    #[test]
    fn test_cylinder_and_drag() {
        let mut lbm = lbm_cylinder(80, 40, 60.0);
        lbm.run(800);
        let vel = lbm.velocity();
        assert!(vel.iter().all(|v| v.magnitude().is_finite()));
        // Drag pushes the cylinder downstream (+x).
        let drag = lbm.drag_on_solid();
        assert!(drag.x > 0.0, "drag {drag:?}");
        // Wake: velocity right behind the cylinder is below the inlet.
        let (cx, cy) = (80 / 4, 20);
        let behind = vel[cy * 80 + cx + 8].x;
        assert!(behind < 0.08, "no wake deficit: {behind}");
        // Vorticity of opposite signs above/below the cylinder.
        let w = lbm.vorticity();
        let above = w[(cy + 4) * 80 + cx + 4];
        let below = w[(cy - 4) * 80 + cx + 4];
        assert!(above * below < 0.0, "no shear layers: {above} vs {below}");
    }

    #[test]
    fn test_lid_cavity_circulation() {
        let n = 32;
        let mut lbm = lbm_lid_cavity(n, 100.0);
        for _ in 0..3000 {
            lbm_cavity_step(&mut lbm, 0.1);
        }
        let vel = lbm.velocity();
        // Flow near the lid follows it; return flow at the bottom is
        // opposite.
        let top = vel[(n - 2) * n + n / 2].x;
        let bottom = vel[2 * n + n / 2].x;
        assert!(top > 0.01, "no lid drag: {top}");
        assert!(bottom < 0.0, "no return flow: {bottom}");
        // A single primary vortex: net vorticity is single-signed.
        let w = lbm.vorticity();
        let sum: f64 = w.iter().sum();
        assert!(sum.abs() > 1e-3);
    }

    #[test]
    fn test_d3q19_and_d3q27() {
        // Weights normalize.
        let vs = LbmD3Q27::velocities();
        assert_eq!(vs.len(), 27);
        let total: f64 = vs.iter().map(|&e| LbmD3Q27::weight(e)).sum();
        assert!((total - 1.0).abs() < 1e-12);
        let w_sum: f64 = (0..19).map(w19).sum();
        assert!((w_sum - 1.0).abs() < 1e-12);
        // 3D body-driven channel flow between z walls.
        let (nx, ny, nz) = (4, 4, 17);
        let mut lbm = LbmD3Q19::new(nx, ny, nz, 0.8);
        lbm.force = Vec3::new(1e-6, 0.0, 0.0);
        for j in 0..ny {
            for i in 0..nx {
                lbm.solid[j * nx + i] = true;
                lbm.solid[((nz - 1) * ny + j) * nx + i] = true;
            }
        }
        for _ in 0..2000 {
            lbm.step();
        }
        let vel = lbm.velocity();
        let center = vel[((nz / 2) * ny + 2) * nx + 2].x;
        let wall = vel[(ny + 2) * nx + 2].x;
        assert!(center > 2.0 * wall.max(1e-9), "3D channel: {center} vs {wall}");
        assert!(vel.iter().all(|v| v.magnitude().is_finite()));
        // Mass conserved.
        let mass: f64 = lbm.f.iter().map(|n| n.iter().sum::<f64>()).sum();
        assert!((mass / (nx * ny * nz) as f64 - 1.0).abs() < 1e-9);
    }

    #[test]
    fn test_periodic_restores_wraparound() {
        // A lattice configured with Zou-He open boundaries is closed in x;
        // `periodic()` must put the torus back and drop the open-boundary
        // bookkeeping.
        let (nx, ny) = (16, 8);
        let u0 = Vec2::new(0.05, 0.0);

        let mut torus = LbmD2Q9::new(nx, ny, 0.8);
        torus.periodic_x = false;
        torus.periodic_y = false;
        torus.periodic();
        assert!(torus.periodic_x && torus.periodic_y);
        torus.init_equilibrium(1.0, u0);

        // On a torus with no solids and no forcing, a uniform equilibrium
        // state is an exact fixed point: collision leaves f = f_eq and
        // streaming just permutes identical values. Mass and momentum are
        // conserved to round-off.
        let mass0: f64 = torus.density().iter().sum();
        let px0: f64 = torus
            .velocity()
            .iter()
            .zip(torus.density())
            .map(|(v, r)| v.x * r)
            .sum();
        torus.run(60);
        for (c, (rho, v)) in torus.density().iter().zip(torus.velocity()).enumerate() {
            assert!((rho - 1.0).abs() < 1e-12, "density {rho} at {c}");
            assert!((v.x - u0.x).abs() < 1e-12, "u {v:?} at {c}");
            assert!(v.y.abs() < 1e-12, "v {v:?} at {c}");
        }
        let mass: f64 = torus.density().iter().sum();
        let px: f64 = torus
            .velocity()
            .iter()
            .zip(torus.density())
            .map(|(v, r)| v.x * r)
            .sum();
        assert!((mass - mass0).abs() < 1e-12, "mass drift {mass} vs {mass0}");
        assert!((px - px0).abs() < 1e-12, "momentum drift {px} vs {px0}");

        // The same uniform state on a closed lattice does not survive:
        // the stream bounces off the walls, so this is not a vacuous
        // check of the torus.
        let mut closed = LbmD2Q9::new(nx, ny, 0.8);
        closed.periodic_x = false;
        closed.periodic_y = false;
        closed.init_equilibrium(1.0, u0);
        closed.run(60);
        let closed_dev = closed
            .velocity()
            .iter()
            .map(|v| (v.x - u0.x).abs())
            .fold(0.0_f64, f64::max);
        assert!(closed_dev > 1e-3, "closed lattice was undisturbed: {closed_dev}");

        // `periodic()` also drops the Zou-He inlet/outlet: a uniform
        // ρ = 1.2 lattice at rest is then an exact fixed point, whereas a
        // live pressure outlet would drive the last column back to ρ = 1.
        let mut cleared = LbmD2Q9::new(nx, ny, 0.8);
        cleared.zou_he_velocity_inlet(0.05);
        cleared.zou_he_pressure_outlet(1.0);
        cleared.periodic();
        cleared.init_equilibrium(1.2, Vec2::ZERO);
        cleared.run(30);
        let rho_dev = cleared
            .density()
            .iter()
            .map(|r| (r - 1.2).abs())
            .fold(0.0_f64, f64::max);
        assert!(rho_dev < 1e-12, "open boundaries still active: {rho_dev}");
        let mut still_open = LbmD2Q9::new(nx, ny, 0.8);
        still_open.zou_he_velocity_inlet(0.05);
        still_open.zou_he_pressure_outlet(1.0);
        still_open.init_equilibrium(1.2, Vec2::ZERO);
        still_open.run(30);
        let open_dev = still_open
            .density()
            .iter()
            .map(|r| (r - 1.2).abs())
            .fold(0.0_f64, f64::max);
        assert!(open_dev > 1e-3, "the outlet should have pulled ρ down: {open_dev}");

        // Wrap-around distance: with the collision switched off (τ → ∞,
        // ω ≈ 0) a population injected in the +x direction advects one
        // node per step and must return to its origin after exactly nx
        // steps.
        let mut ballistic = LbmD2Q9::new(nx, ny, 1e9);
        ballistic.periodic();
        let (i0, j0) = (3_usize, 4_usize);
        let delta = 0.02;
        let base = ballistic.f[j0 * nx + i0][1];
        ballistic.f[j0 * nx + i0][1] += delta;
        let mass_b: f64 = ballistic.density().iter().sum();
        for s in 1..=nx {
            ballistic.step();
            let here = (i0 + s) % nx;
            let carried = ballistic.f[j0 * nx + here][1] - base;
            assert!(
                (carried - delta).abs() < 1e-6,
                "step {s}: perturbation {carried} not at column {here}"
            );
        }
        // Back where it started after a full lap.
        assert!(
            (ballistic.f[j0 * nx + i0][1] - base - delta).abs() < 1e-6,
            "no wrap-around after {nx} steps"
        );
        assert!(
            (ballistic.density().iter().sum::<f64>() - mass_b).abs() < 1e-12,
            "mass lost during the lap"
        );
    }

    #[test]
    fn test_d3q19_viscosity_matches_shear_wave_decay() {
        // ν = (τ − 1/2)/3 in lattice units (c_s² = 1/3, dt = 1).
        for &tau in &[0.6_f64, 0.8, 1.0, 1.5] {
            let lbm = LbmD3Q19::new(2, 2, 2, tau);
            assert!(
                (lbm.viscosity() - (tau - 0.5) / 3.0).abs() < 1e-15,
                "tau {tau}: nu {}",
                lbm.viscosity()
            );
        }
        // Physical check: a transverse shear wave u_x = U sin(k z) on a
        // fully periodic lattice decays as exp(−ν k² t). Measuring the
        // decay recovers the viscosity the formula reports.
        let (nx, ny, nz) = (4_usize, 4_usize, 16_usize);
        let tau = 0.8;
        let mut lbm = LbmD3Q19::new(nx, ny, nz, tau);
        let k = 2.0 * std::f64::consts::PI / nz as f64;
        let amp = 0.01;
        for z in 0..nz {
            let ux = amp * (k * z as f64).sin();
            for y in 0..ny {
                for x in 0..nx {
                    let c = (z * ny + y) * nx + x;
                    let u2 = ux * ux;
                    for k19 in 0..19 {
                        let eu = E19[k19].0 as f64 * ux;
                        lbm.f[c][k19] =
                            w19(k19) * (1.0 + 3.0 * eu + 4.5 * eu * eu - 1.5 * u2);
                    }
                }
            }
        }
        // Fourier amplitude of the sin(kz) mode of the plane-averaged u_x.
        let mode = |lbm: &LbmD3Q19| -> f64 {
            let vel = lbm.velocity();
            let mut a = 0.0;
            for z in 0..nz {
                let mut mean = 0.0;
                for y in 0..ny {
                    for x in 0..nx {
                        mean += vel[(z * ny + y) * nx + x].x;
                    }
                }
                mean /= (nx * ny) as f64;
                a += mean * (k * z as f64).sin();
            }
            2.0 * a / nz as f64
        };
        // Skip a short start-up transient, then measure the decay rate
        // over a window.
        for _ in 0..20 {
            lbm.step();
        }
        let a0 = mode(&lbm);
        let window = 100;
        for _ in 0..window {
            lbm.step();
        }
        let a1 = mode(&lbm);
        assert!(a0 > 0.5 * amp && a1 > 0.0, "mode collapsed: {a0} -> {a1}");
        let nu_measured = -(a1 / a0).ln() / (k * k * window as f64);
        let nu_formula = lbm.viscosity();
        // The lattice-Boltzmann shear mode decays at exactly ν k² up to
        // O((k dx)²) lattice corrections; here k dx = 0.39, so a few
        // percent is the expected accuracy.
        assert!(
            (nu_measured / nu_formula - 1.0).abs() < 0.05,
            "measured nu {nu_measured} vs (tau-0.5)/3 = {nu_formula}"
        );
        // Doubling (τ − 1/2) doubles the viscosity and so doubles the
        // decay rate: an independent confirmation of the linear relation.
        let tau2 = 0.5 + 2.0 * (tau - 0.5);
        let mut fast = LbmD3Q19::new(nx, ny, nz, tau2);
        for z in 0..nz {
            let ux = amp * (k * z as f64).sin();
            for y in 0..ny {
                for x in 0..nx {
                    let c = (z * ny + y) * nx + x;
                    let u2 = ux * ux;
                    for k19 in 0..19 {
                        let eu = E19[k19].0 as f64 * ux;
                        fast.f[c][k19] =
                            w19(k19) * (1.0 + 3.0 * eu + 4.5 * eu * eu - 1.5 * u2);
                    }
                }
            }
        }
        assert!((fast.viscosity() - 2.0 * nu_formula).abs() < 1e-15);
        for _ in 0..20 {
            fast.step();
        }
        let b0 = mode(&fast);
        for _ in 0..window {
            fast.step();
        }
        let b1 = mode(&fast);
        let rate_ratio = (b0 / b1).ln() / (a0 / a1).ln();
        assert!(
            (rate_ratio - 2.0).abs() < 0.1,
            "decay-rate ratio {rate_ratio} for doubled viscosity"
        );
    }

    #[test]
    fn test_thermal_plume_and_units() {
        let (mut flow, mut temp) = lbm_thermal(24, 24, 0.7, 0.7);
        for _ in 0..200 {
            thermal_step(&mut flow, &mut temp, 1e-4);
        }
        let vel = flow.velocity();
        // Buoyancy drives net upward motion.
        let vy_mean: f64 = vel.iter().map(|v| v.y).sum::<f64>() / vel.len() as f64;
        assert!(vy_mean > 0.0, "no buoyant rise: {vy_mean}");
        assert!(vel.iter().all(|v| v.magnitude().is_finite()));
        assert!((lbm_to_physical(0.1, 1e-3, 1e-5) - 10.0).abs() < 1e-12);
    }
}