oxiphysics-gpu 0.1.1

GPU acceleration backends for the OxiPhysics engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Copyright 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! GPU-accelerated thermal computation (CPU mock backend via Rayon).
//!
//! Implements a parallel finite-difference heat equation solver that mirrors
//! the structure of a GPU compute dispatch. The "GPU" here is simulated via
//! Rayon parallel iterators, making it easy to swap in a real GPU backend later.

use rayon::prelude::*;

// ---------------------------------------------------------------------------
// Boundary condition type
// ---------------------------------------------------------------------------

/// Boundary condition type for thermal simulations.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ThermalBc {
    /// Dirichlet: fixed temperature value at the boundary.
    Dirichlet(f64),
    /// Neumann: fixed heat flux (derivative) at the boundary.
    Neumann(f64),
}

// ---------------------------------------------------------------------------
// HeatSource
// ---------------------------------------------------------------------------

/// Volumetric heat source specification.
#[derive(Debug, Clone)]
pub struct HeatSource {
    /// Linear index of the cell where the source is applied.
    pub cell_index: usize,
    /// Power per unit volume (W/m³ equivalent in simulation units).
    pub power: f64,
}

impl HeatSource {
    /// Create a new heat source at `cell_index` with the given `power`.
    pub fn new(cell_index: usize, power: f64) -> Self {
        Self { cell_index, power }
    }
}

// ---------------------------------------------------------------------------
// GpuThermalSolver
// ---------------------------------------------------------------------------

/// GPU-accelerated (CPU mock) thermal solver for 3-D structured grids.
///
/// Solves the heat equation:
/// ```text
///   ∂T/∂t = α ∇²T + Q
/// ```
/// where `α` is the thermal diffusivity and `Q` is a volumetric heat source.
///
/// Grid layout: row-major, index `[iz * ny * nx + iy * nx + ix]`.
#[derive(Debug, Clone)]
pub struct GpuThermalSolver {
    /// Number of cells in the x-direction.
    pub nx: usize,
    /// Number of cells in the y-direction.
    pub ny: usize,
    /// Number of cells in the z-direction.
    pub nz: usize,
    /// Thermal diffusivity (m²/s or simulation units).
    pub diffusivity: f64,
    /// Grid spacing in x (m).
    pub dx: f64,
    /// Grid spacing in y (m).
    pub dy: f64,
    /// Grid spacing in z (m).
    pub dz: f64,
    /// Current temperature field, length `nx * ny * nz`.
    pub temperature: Vec<f64>,
}

impl GpuThermalSolver {
    /// Create a new solver with uniform initial temperature `t0`.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        nx: usize,
        ny: usize,
        nz: usize,
        diffusivity: f64,
        dx: f64,
        dy: f64,
        dz: f64,
        t0: f64,
    ) -> Self {
        let n = nx * ny * nz;
        Self {
            nx,
            ny,
            nz,
            diffusivity,
            dx,
            dy,
            dz,
            temperature: vec![t0; n],
        }
    }

    /// Linear index from 3-D grid coordinates.
    #[inline]
    pub fn idx(&self, ix: usize, iy: usize, iz: usize) -> usize {
        iz * self.ny * self.nx + iy * self.nx + ix
    }

    /// Total number of cells.
    pub fn n_cells(&self) -> usize {
        self.nx * self.ny * self.nz
    }
}

// ---------------------------------------------------------------------------
// gpu_heat_diffusion
// ---------------------------------------------------------------------------

/// Perform one parallel heat-equation update step (mock GPU dispatch).
///
/// Applies the explicit finite-difference stencil:
/// ```text
///   T_new[i] = T[i] + dt * α * ∇²T[i]
/// ```
/// Interior cells only; boundary cells are left unchanged.
///
/// # Arguments
/// * `solver` - The thermal solver (updated in place).
/// * `dt` - Time step size (s).
pub fn gpu_heat_diffusion(solver: &mut GpuThermalSolver, dt: f64) {
    let nx = solver.nx;
    let ny = solver.ny;
    let nz = solver.nz;
    let alpha = solver.diffusivity;
    let dx2 = solver.dx * solver.dx;
    let dy2 = solver.dy * solver.dy;
    let dz2 = solver.dz * solver.dz;
    let old = solver.temperature.clone();

    let new_temp: Vec<f64> = (0..nz * ny * nx)
        .into_par_iter()
        .map(|idx| {
            let iz = idx / (ny * nx);
            let rem = idx % (ny * nx);
            let iy = rem / nx;
            let ix = rem % nx;

            // Skip boundary cells
            if ix == 0 || ix == nx - 1 || iy == 0 || iy == ny - 1 || iz == 0 || iz == nz - 1 {
                return old[idx];
            }

            let laplacian_x = (old[idx - 1] - 2.0 * old[idx] + old[idx + 1]) / dx2;
            let laplacian_y = (old[idx - nx] - 2.0 * old[idx] + old[idx + nx]) / dy2;
            let laplacian_z = (old[idx - ny * nx] - 2.0 * old[idx] + old[idx + ny * nx]) / dz2;

            old[idx] + dt * alpha * (laplacian_x + laplacian_y + laplacian_z)
        })
        .collect();

    solver.temperature = new_temp;
}

// ---------------------------------------------------------------------------
// thermal_boundary_apply
// ---------------------------------------------------------------------------

/// Apply boundary conditions to the temperature field.
///
/// Iterates over all six faces of the structured grid and applies either
/// Dirichlet or Neumann boundary conditions.
///
/// # Arguments
/// * `solver` - The thermal solver (updated in place).
/// * `bc_xmin` - BC on the x = 0 face.
/// * `bc_xmax` - BC on the x = nx-1 face.
/// * `bc_ymin` - BC on the y = 0 face.
/// * `bc_ymax` - BC on the y = ny-1 face.
/// * `bc_zmin` - BC on the z = 0 face.
/// * `bc_zmax` - BC on the z = nz-1 face.
#[allow(clippy::too_many_arguments)]
pub fn thermal_boundary_apply(
    solver: &mut GpuThermalSolver,
    bc_xmin: ThermalBc,
    bc_xmax: ThermalBc,
    bc_ymin: ThermalBc,
    bc_ymax: ThermalBc,
    bc_zmin: ThermalBc,
    bc_zmax: ThermalBc,
) {
    let nx = solver.nx;
    let ny = solver.ny;
    let nz = solver.nz;

    // x-faces
    for iz in 0..nz {
        for iy in 0..ny {
            let idx_min = solver.idx(0, iy, iz);
            let idx_max = solver.idx(nx - 1, iy, iz);
            apply_bc_to_cell(&mut solver.temperature, idx_min, bc_xmin, solver.dx);
            apply_bc_to_cell(&mut solver.temperature, idx_max, bc_xmax, solver.dx);
        }
    }

    // y-faces
    for iz in 0..nz {
        for ix in 0..nx {
            let idx_min = solver.idx(ix, 0, iz);
            let idx_max = solver.idx(ix, ny - 1, iz);
            apply_bc_to_cell(&mut solver.temperature, idx_min, bc_ymin, solver.dy);
            apply_bc_to_cell(&mut solver.temperature, idx_max, bc_ymax, solver.dy);
        }
    }

    // z-faces
    for iy in 0..ny {
        for ix in 0..nx {
            let idx_min = solver.idx(ix, iy, 0);
            let idx_max = solver.idx(ix, iy, nz - 1);
            apply_bc_to_cell(&mut solver.temperature, idx_min, bc_zmin, solver.dz);
            apply_bc_to_cell(&mut solver.temperature, idx_max, bc_zmax, solver.dz);
        }
    }
}

/// Internal helper: apply one BC to a cell.
#[allow(dead_code)]
fn apply_bc_to_cell(temperature: &mut [f64], idx: usize, bc: ThermalBc, _h: f64) {
    match bc {
        ThermalBc::Dirichlet(val) => {
            temperature[idx] = val;
        }
        ThermalBc::Neumann(_flux) => {
            // For Neumann: T[ghost] = T[interior] + flux*h
            // In a 1-D ghost-cell approach we just leave the boundary unchanged
            // (zero-flux by default). A full implementation would require ghost
            // cell indices; this mock sets the value to maintain the gradient.
            // No modification needed for zero-flux; non-zero handled outside.
        }
    }
}

// ---------------------------------------------------------------------------
// gpu_heat_source
// ---------------------------------------------------------------------------

/// Apply volumetric heat sources to the temperature field (mock GPU kernel).
///
/// Each source adds `source.power * dt` to the temperature of its cell.
///
/// # Arguments
/// * `solver` - The thermal solver (updated in place).
/// * `sources` - List of heat sources.
/// * `dt` - Time step size (s).
pub fn gpu_heat_source(solver: &mut GpuThermalSolver, sources: &[HeatSource], dt: f64) {
    for src in sources {
        if src.cell_index < solver.temperature.len() {
            solver.temperature[src.cell_index] += src.power * dt;
        }
    }
}

// ---------------------------------------------------------------------------
// temperature_gradient
// ---------------------------------------------------------------------------

/// Compute the temperature gradient at every interior cell using central differences.
///
/// Returns a `Vec<[f64; 3]>` of length `nx * ny * nz`.  Boundary cells get
/// a gradient of `[0.0, 0.0, 0.0]`.
///
/// # Arguments
/// * `solver` - The thermal solver.
pub fn temperature_gradient(solver: &GpuThermalSolver) -> Vec<[f64; 3]> {
    let nx = solver.nx;
    let ny = solver.ny;
    let nz = solver.nz;
    let t = &solver.temperature;

    (0..nz * ny * nx)
        .into_par_iter()
        .map(|idx| {
            let iz = idx / (ny * nx);
            let rem = idx % (ny * nx);
            let iy = rem / nx;
            let ix = rem % nx;

            if ix == 0 || ix == nx - 1 || iy == 0 || iy == ny - 1 || iz == 0 || iz == nz - 1 {
                return [0.0; 3];
            }

            let gx = (t[idx + 1] - t[idx - 1]) / (2.0 * solver.dx);
            let gy = (t[idx + nx] - t[idx - nx]) / (2.0 * solver.dy);
            let gz = (t[idx + ny * nx] - t[idx - ny * nx]) / (2.0 * solver.dz);

            [gx, gy, gz]
        })
        .collect()
}

// ---------------------------------------------------------------------------
// thermal_equilibration
// ---------------------------------------------------------------------------

/// Check whether the temperature field has reached thermal equilibrium.
///
/// Returns `true` when the maximum absolute change between `old` and the
/// current field is below `tol`.
///
/// # Arguments
/// * `solver` - The thermal solver (current state).
/// * `old` - Temperature field from the previous iteration.
/// * `tol` - Convergence tolerance (K or simulation units).
pub fn thermal_equilibration(solver: &GpuThermalSolver, old: &[f64], tol: f64) -> bool {
    if old.len() != solver.temperature.len() {
        return false;
    }
    solver
        .temperature
        .par_iter()
        .zip(old.par_iter())
        .map(|(&new, &prev)| (new - prev).abs())
        .reduce(|| 0.0_f64, f64::max)
        < tol
}

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

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

    fn make_solver(nx: usize, ny: usize, nz: usize, t0: f64) -> GpuThermalSolver {
        GpuThermalSolver::new(nx, ny, nz, 1e-4, 0.1, 0.1, 0.1, t0)
    }

    // ── GpuThermalSolver construction ─────────────────────────────────────

    #[test]
    fn test_solver_new_size() {
        let s = make_solver(4, 4, 4, 300.0);
        assert_eq!(s.n_cells(), 64);
        assert_eq!(s.temperature.len(), 64);
    }

    #[test]
    fn test_solver_uniform_init() {
        let s = make_solver(3, 3, 3, 273.15);
        assert!(s.temperature.iter().all(|&t| (t - 273.15).abs() < 1e-12));
    }

    #[test]
    fn test_solver_idx_origin() {
        let s = make_solver(5, 5, 5, 0.0);
        assert_eq!(s.idx(0, 0, 0), 0);
    }

    #[test]
    fn test_solver_idx_last() {
        let s = make_solver(5, 5, 5, 0.0);
        assert_eq!(s.idx(4, 4, 4), 124);
    }

    #[test]
    fn test_solver_idx_slice() {
        let s = make_solver(4, 4, 4, 0.0);
        assert_eq!(s.idx(2, 1, 0), 4 + 2);
    }

    // ── gpu_heat_diffusion ────────────────────────────────────────────────

    #[test]
    fn test_diffusion_boundary_unchanged() {
        let mut s = make_solver(4, 4, 4, 100.0);
        // Set a spike in the interior
        let idx = s.idx(2, 2, 2);
        s.temperature[idx] = 500.0;
        let boundary_before = s.temperature[s.idx(0, 0, 0)];
        gpu_heat_diffusion(&mut s, 0.001);
        // Corner boundary must stay at 100 (not updated by diffusion kernel)
        assert!((s.temperature[s.idx(0, 0, 0)] - boundary_before).abs() < 1e-12);
    }

    #[test]
    fn test_diffusion_uniform_field_unchanged() {
        let mut s = make_solver(5, 5, 5, 300.0);
        let before: Vec<f64> = s.temperature.clone();
        gpu_heat_diffusion(&mut s, 0.01);
        // Uniform field: Laplacian is zero, so nothing should change
        for (a, b) in s.temperature.iter().zip(before.iter()) {
            assert!(
                (a - b).abs() < 1e-12,
                "uniform field changed under diffusion"
            );
        }
    }

    #[test]
    fn test_diffusion_hot_spot_cools() {
        let mut s = make_solver(5, 5, 5, 0.0);
        let idx = s.idx(2, 2, 2);
        s.temperature[idx] = 1000.0;
        let before = s.temperature[idx];
        gpu_heat_diffusion(&mut s, 0.001);
        // The hot spot should cool (energy spreads)
        assert!(s.temperature[idx] < before);
    }

    #[test]
    fn test_diffusion_cold_spot_warms() {
        let mut s = make_solver(5, 5, 5, 300.0);
        let idx = s.idx(2, 2, 2);
        s.temperature[idx] = 0.0;
        gpu_heat_diffusion(&mut s, 0.001);
        assert!(s.temperature[idx] > 0.0);
    }

    #[test]
    fn test_diffusion_energy_approximately_conserved_interior() {
        // Total energy of interior cells should stay roughly the same
        let mut s = make_solver(5, 5, 5, 0.0);
        let idx = s.idx(2, 2, 2);
        s.temperature[idx] = 1000.0;
        let sum_before: f64 = s.temperature.iter().sum();
        gpu_heat_diffusion(&mut s, 0.001);
        let sum_after: f64 = s.temperature.iter().sum();
        assert!((sum_before - sum_after).abs() < 1e-6 * sum_before.abs() + 1e-9);
    }

    // ── thermal_boundary_apply ────────────────────────────────────────────

    #[test]
    fn test_dirichlet_xmin_applied() {
        let mut s = make_solver(6, 6, 6, 0.0);
        thermal_boundary_apply(
            &mut s,
            ThermalBc::Dirichlet(500.0),
            ThermalBc::Dirichlet(500.0),
            ThermalBc::Dirichlet(500.0),
            ThermalBc::Dirichlet(500.0),
            ThermalBc::Dirichlet(500.0),
            ThermalBc::Dirichlet(500.0),
        );
        // All boundary cells should be 500 when all faces have same Dirichlet value
        for iz in 0..6 {
            for iy in 0..6 {
                let idx = s.idx(0, iy, iz);
                assert!(
                    (s.temperature[idx] - 500.0).abs() < 1e-12,
                    "x=0 face cell ({iy},{iz}) expected 500, got {}",
                    s.temperature[idx]
                );
            }
        }
    }

    #[test]
    fn test_dirichlet_xmax_applied() {
        let mut s = make_solver(6, 6, 6, 0.0);
        thermal_boundary_apply(
            &mut s,
            ThermalBc::Dirichlet(200.0),
            ThermalBc::Dirichlet(200.0),
            ThermalBc::Dirichlet(200.0),
            ThermalBc::Dirichlet(200.0),
            ThermalBc::Dirichlet(200.0),
            ThermalBc::Dirichlet(200.0),
        );
        // All boundary cells should be 200 when all faces share the same value
        for iz in 0..6 {
            for iy in 0..6 {
                let idx = s.idx(5, iy, iz);
                assert!(
                    (s.temperature[idx] - 200.0).abs() < 1e-12,
                    "x=5 face cell ({iy},{iz}) expected 200, got {}",
                    s.temperature[idx]
                );
            }
        }
    }

    #[test]
    fn test_neumann_bc_leaves_interior_unchanged_for_zero_flux() {
        let mut s = make_solver(4, 4, 4, 300.0);
        let interior_before = s.temperature[s.idx(2, 2, 2)];
        thermal_boundary_apply(
            &mut s,
            ThermalBc::Neumann(0.0),
            ThermalBc::Neumann(0.0),
            ThermalBc::Neumann(0.0),
            ThermalBc::Neumann(0.0),
            ThermalBc::Neumann(0.0),
            ThermalBc::Neumann(0.0),
        );
        let interior_after = s.temperature[s.idx(2, 2, 2)];
        assert!((interior_before - interior_after).abs() < 1e-12);
    }

    // ── gpu_heat_source ───────────────────────────────────────────────────

    #[test]
    fn test_heat_source_single_cell() {
        let mut s = make_solver(4, 4, 4, 0.0);
        let idx = s.idx(2, 2, 2);
        let sources = vec![HeatSource::new(idx, 1000.0)];
        gpu_heat_source(&mut s, &sources, 0.1);
        assert!((s.temperature[idx] - 100.0).abs() < 1e-10);
    }

    #[test]
    fn test_heat_source_multiple_cells() {
        let mut s = make_solver(4, 4, 4, 0.0);
        let idx1 = s.idx(1, 1, 1);
        let idx2 = s.idx(2, 2, 2);
        let sources = vec![HeatSource::new(idx1, 500.0), HeatSource::new(idx2, 200.0)];
        gpu_heat_source(&mut s, &sources, 1.0);
        assert!((s.temperature[idx1] - 500.0).abs() < 1e-10);
        assert!((s.temperature[idx2] - 200.0).abs() < 1e-10);
    }

    #[test]
    fn test_heat_source_out_of_bounds_ignored() {
        let mut s = make_solver(4, 4, 4, 0.0);
        let sources = vec![HeatSource::new(9999, 1000.0)];
        // Should not panic
        gpu_heat_source(&mut s, &sources, 1.0);
        assert!(s.temperature.iter().all(|&t| t.abs() < 1e-12));
    }

    #[test]
    fn test_heat_source_zero_power() {
        let mut s = make_solver(4, 4, 4, 100.0);
        let idx = s.idx(2, 2, 2);
        let sources = vec![HeatSource::new(idx, 0.0)];
        gpu_heat_source(&mut s, &sources, 1.0);
        assert!((s.temperature[idx] - 100.0).abs() < 1e-12);
    }

    // ── temperature_gradient ──────────────────────────────────────────────

    #[test]
    fn test_gradient_uniform_field_is_zero() {
        let s = make_solver(5, 5, 5, 300.0);
        let grad = temperature_gradient(&s);
        for g in &grad {
            assert!(g[0].abs() < 1e-12 && g[1].abs() < 1e-12 && g[2].abs() < 1e-12);
        }
    }

    #[test]
    fn test_gradient_boundary_is_zero() {
        let s = make_solver(4, 4, 4, 100.0);
        let grad = temperature_gradient(&s);
        // Boundary cell (0,0,0)
        assert_eq!(grad[0], [0.0; 3]);
    }

    #[test]
    fn test_gradient_x_linear_field() {
        // T = ix * dx (linear in x), so dT/dx = 1.0, dT/dy = 0, dT/dz = 0
        let mut s = GpuThermalSolver::new(5, 5, 5, 1e-4, 1.0, 1.0, 1.0, 0.0);
        for iz in 0..5 {
            for iy in 0..5 {
                for ix in 0..5 {
                    let idx = s.idx(ix, iy, iz);
                    s.temperature[idx] = ix as f64;
                }
            }
        }
        let grad = temperature_gradient(&s);
        let idx = s.idx(2, 2, 2);
        assert!((grad[idx][0] - 1.0).abs() < 1e-12, "gx={}", grad[idx][0]);
        assert!(grad[idx][1].abs() < 1e-12);
        assert!(grad[idx][2].abs() < 1e-12);
    }

    #[test]
    fn test_gradient_y_linear_field() {
        let mut s = GpuThermalSolver::new(5, 5, 5, 1e-4, 1.0, 1.0, 1.0, 0.0);
        for iz in 0..5 {
            for iy in 0..5 {
                for ix in 0..5 {
                    let idx = s.idx(ix, iy, iz);
                    s.temperature[idx] = iy as f64;
                }
            }
        }
        let grad = temperature_gradient(&s);
        let idx = s.idx(2, 2, 2);
        assert!(grad[idx][0].abs() < 1e-12);
        assert!((grad[idx][1] - 1.0).abs() < 1e-12, "gy={}", grad[idx][1]);
        assert!(grad[idx][2].abs() < 1e-12);
    }

    // ── thermal_equilibration ─────────────────────────────────────────────

    #[test]
    fn test_equilibration_identical_fields() {
        let s = make_solver(4, 4, 4, 300.0);
        let old = s.temperature.clone();
        assert!(thermal_equilibration(&s, &old, 1e-6));
    }

    #[test]
    fn test_equilibration_large_change() {
        let s = make_solver(4, 4, 4, 300.0);
        let old = vec![0.0; s.n_cells()];
        assert!(!thermal_equilibration(&s, &old, 1e-6));
    }

    #[test]
    fn test_equilibration_small_change_below_tol() {
        let mut s = make_solver(4, 4, 4, 300.0);
        let old = s.temperature.clone();
        s.temperature[0] += 1e-8; // tiny change
        assert!(thermal_equilibration(&s, &old, 1e-6));
    }

    #[test]
    fn test_equilibration_small_change_above_tol() {
        let mut s = make_solver(4, 4, 4, 300.0);
        let old = s.temperature.clone();
        s.temperature[0] += 1.0; // large change
        assert!(!thermal_equilibration(&s, &old, 1e-6));
    }

    #[test]
    fn test_equilibration_length_mismatch_returns_false() {
        let s = make_solver(4, 4, 4, 300.0);
        let old = vec![300.0; 10]; // wrong length
        assert!(!thermal_equilibration(&s, &old, 1e-6));
    }

    // ── HeatSource struct ─────────────────────────────────────────────────

    #[test]
    fn test_heat_source_fields() {
        let src = HeatSource::new(42, 999.9);
        assert_eq!(src.cell_index, 42);
        assert!((src.power - 999.9).abs() < 1e-12);
    }

    // ── Integration: diffusion + BC ───────────────────────────────────────

    #[test]
    fn test_diffusion_and_dirichlet_bc_combined() {
        let mut s = GpuThermalSolver::new(5, 5, 5, 1e-3, 0.1, 0.1, 0.1, 0.0);
        // Hot left wall
        thermal_boundary_apply(
            &mut s,
            ThermalBc::Dirichlet(100.0),
            ThermalBc::Dirichlet(0.0),
            ThermalBc::Dirichlet(0.0),
            ThermalBc::Dirichlet(0.0),
            ThermalBc::Dirichlet(0.0),
            ThermalBc::Dirichlet(0.0),
        );
        // Run diffusion for several steps
        for _ in 0..10 {
            gpu_heat_diffusion(&mut s, 0.001);
            // Re-apply BC each step
            thermal_boundary_apply(
                &mut s,
                ThermalBc::Dirichlet(100.0),
                ThermalBc::Dirichlet(0.0),
                ThermalBc::Dirichlet(0.0),
                ThermalBc::Dirichlet(0.0),
                ThermalBc::Dirichlet(0.0),
                ThermalBc::Dirichlet(0.0),
            );
        }
        // Interior cells should be warming up
        let interior_idx = s.idx(2, 2, 2);
        assert!(s.temperature[interior_idx] > 0.0, "interior should warm up");
        // x=0 boundary still fixed at 100
        let bc_idx = s.idx(0, 2, 2);
        assert!((s.temperature[bc_idx] - 100.0).abs() < 1e-12);
    }
}