outram-foam-basic-lib 0.1.3

OpenFOAM primitive functions translated to Rust -- an independent OUTRAM PARK fork, not the official OpenFOAM software
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
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 OUTRAM PARK contributors
// Derived from OpenFOAM (www.openfoam.com)
// Copyright (C) 2004-2023 OpenFOAM Foundation
// Copyright (C) 2016-2023 OpenCFD Ltd.
//
// This file is part of OUTRAM PARK.
//
// OUTRAM PARK is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// OUTRAM PARK is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with OUTRAM PARK.  If not, see <https://www.gnu.org/licenses/>.

use crate::fields::boundary::bc::BoundaryCondition;
use crate::fields::surface_field::SurfaceScalarField;
use crate::fields::vol_field::VolScalarField;
use crate::ldu_matrix::fv_matrix::FvMatrix;
use crate::mesh::fv_mesh::PatchKind;

/// Implicit Gauss-orthogonal Laplacian: assembles the matrix for `−∇·(Γ∇φ)`.
///
/// ## Sign convention (matches OpenFOAM)
///
/// The returned matrix has **positive** diagonal and **negative** off-diagonals,
/// so the matrix–vector product `A·φ` approximates `−∇·(Γ∇φ)`.  Use the matrix
/// with a minus sign in the PDE to add the diffusion term:
///
/// ```text
/// // ∂φ/∂t − ∇·(Γ∇φ) = S
/// let eqn = fvm::ddt(&phi, &phi_old, dt) - fvm::laplacian(&gamma_f, &phi);
/// ```
///
/// ## Boundary conditions
///
/// - `ZeroGradient` / `Symmetry`: no contribution (zero normal flux).
/// - `FixedValue(v)`: adds `coeff` to diagonal and `coeff·v` to source.
///
/// ## Cyclic (periodic) patches
///
/// [`PatchKind::Cyclic`] patches carry no boundary-value contribution; instead
/// each matched face pair is discretised as an **internal face across the
/// periodic seam** (see the cyclic-coupling loop below), coupling the owner cell
/// of one half to the owner cell of the partner half with the orthogonal
/// coefficient `Γ·|Sf| / d_seam`, `d_seam = |Cf_A − C_ownerA| + |Cf_B −
/// C_ownerB|`. This reproduces the equivalent all-internal ring mesh. Mirrors
/// `Foam::cyclicFvPatchField` (`src/finiteVolume/.../cyclic/cyclicFvPatchField.H`).
///
/// ## Non-conformal periodic (cyclicAMI) patches
///
/// [`PatchKind::CyclicAmi`](crate::mesh::PatchKind::CyclicAmi) patches are
/// non-conformal: each target seam face overlaps several source faces. Each
/// [`AmiCoupling`](crate::mesh::AmiCoupling) is assembled as a set of **partial
/// internal faces**, one per weighted (target, source) overlap, each carrying
/// `Γ·overlap_area / delta` with the symmetric internal-face stamp. Per target
/// the overlap areas sum to the target area, so the seam flux is distributed
/// conservatively (`A·uniform = 0`); in the matching (conformal) limit this
/// reduces exactly to the plain cyclic seam. Mirrors
/// `Foam::cyclicAMIFvPatchField` + `src/meshTools/AMIInterpolation/...`.
pub fn laplacian(gamma: &SurfaceScalarField, phi: &VolScalarField) -> FvMatrix {
    let mesh = phi.mesh.clone();
    let mut mat = FvMatrix::new(mesh.clone());

    // Internal faces: Gauss orthogonal
    for f in 0..mesh.n_internal_faces {
        let o = mesh.owner[f];
        let n = mesh.neighbour[f];
        let delta = (mesh.cell_centres[n] - mesh.cell_centres[o]).mag();
        if delta < 1e-300 {
            continue;
        }
        let coeff = gamma.internal[f] * mesh.face_areas[f] / delta;
        mat.ldu.diag[o] += coeff;
        mat.ldu.diag[n] += coeff;
        mat.ldu.upper[f] = -coeff;
        mat.ldu.lower[f] = -coeff;
    }

    // Boundary faces
    for (pi, patch) in mesh.patches.iter().enumerate() {
        // Cyclic / cyclicAMI patches are handled as internal-like seam couplings
        // below, not as ordinary boundary faces.
        if patch.kind == PatchKind::Cyclic || patch.kind == PatchKind::CyclicAmi {
            continue;
        }
        for fi in 0..patch.size {
            let gf = patch.start + fi;
            let owner = mesh.owner[gf];
            let d = (mesh.face_centres[gf] - mesh.cell_centres[owner]).mag();
            if d < 1e-300 {
                continue;
            }
            let coeff = gamma.boundary[pi].values[fi] * mesh.face_areas[gf] / d;
            match &phi.boundary[pi].bc {
                BoundaryCondition::ZeroGradient | BoundaryCondition::Symmetry => {}
                BoundaryCondition::FixedValue(v) => {
                    mat.ldu.diag[owner] += coeff;
                    mat.source[owner] += coeff * v;
                }
                BoundaryCondition::FixedField(ff) => {
                    mat.ldu.diag[owner] += coeff;
                    mat.source[owner] += coeff * ff[fi];
                }
                // No-slip wall: fixedValue of zero → implicit diagonal only.
                BoundaryCondition::NoSlip => {
                    mat.ldu.diag[owner] += coeff;
                }
                // Prescribed normal gradient g [value·m⁻¹]: explicit boundary
                // flux gamma·area·g = (coeff·d)·g into the source, no diagonal.
                BoundaryCondition::FixedGradient(g) => {
                    mat.source[owner] += coeff * d * g;
                }
                // Robin/mixed: blend fixedValue (weight w) and fixedGradient
                // (1-w). w=1 recovers the FixedValue arm, w=0 the FixedGradient.
                BoundaryCondition::Mixed {
                    value_fraction,
                    ref_value,
                    ref_grad,
                } => {
                    let w = *value_fraction;
                    mat.ldu.diag[owner] += w * coeff;
                    mat.source[owner] += w * coeff * ref_value + (1.0 - w) * coeff * d * ref_grad;
                }
                // fixedFluxPressure: a fixedGradient whose gradient the solver
                // set (snGrad(p)); same explicit boundary flux as FixedGradient.
                BoundaryCondition::FixedFluxPressure { gradient } => {
                    mat.source[owner] += coeff * d * gradient;
                }
                // totalPressure: Dirichlet using the solver-computed face value
                // stored in the patch — implicit diagonal + explicit source.
                BoundaryCondition::TotalPressure { .. } => {
                    mat.ldu.diag[owner] += coeff;
                    mat.source[owner] += coeff * phi.boundary[pi].values[fi];
                }
                // `InletOutlet`/`OutletInlet`/`Freestream` carry no flux in the
                // diffusion operator (degrade to zero-gradient);
                // `Slip`/`Wedge`/`Empty`/`pressureInletOutletVelocity`/
                // `flowRateInletVelocity` are zero-gradient-like here. No
                // contribution.
                _ => {}
            }
        }
    }

    // Cyclic (periodic) seam couplings: assemble each matched face pair exactly
    // like an internal face joining the two owner cells across the seam.
    for (i, cc) in mesh.cyclic_couplings.iter().enumerate() {
        let cf = mesh.cyclic_coupling_face(i);
        let o = cc.owner;
        let nb = cc.neighbour;
        // Cell-to-cell distance across the seam = owner→face_a plus
        // face_b→neighbour (the two half-face gaps of the coincident seam).
        let d_a = (mesh.face_centres[cc.face_a] - mesh.cell_centres[o]).mag();
        let d_b = (mesh.face_centres[cc.face_b] - mesh.cell_centres[nb]).mag();
        let delta = d_a + d_b;
        if delta < 1e-300 {
            continue;
        }
        // Γ on the seam taken from the half0 (patch_a) boundary coefficient.
        let gamma_seam = gamma.boundary[cc.patch_a].values[cc.local];
        let coeff = gamma_seam * mesh.face_areas[cc.face_a] / delta;
        mat.ldu.diag[o] += coeff;
        mat.ldu.diag[nb] += coeff;
        mat.ldu.upper[cf] = -coeff;
        mat.ldu.lower[cf] = -coeff;
    }

    // Non-conformal (cyclicAMI) seam couplings: assemble each target seam face
    // as a set of partial internal faces — one per weighted (target, source)
    // overlap. Each partial face carries the orthogonal diffusion coefficient
    // Γ·overlap_area/delta of a face of area `overlap_area`, with the standard
    // symmetric internal-face stamp. Summed over a target's sources this equals
    // the full target-face flux distributed conservatively across the sources;
    // in the matching (conformal) limit (one source, weight 1) it reduces
    // exactly to the plain cyclic seam above. Mirrors `Foam::cyclicAMIFvPatchField`.
    let mut cf = mesh.ami_ldu_start();
    for coupling in &mesh.ami_couplings {
        let o = coupling.target_cell;
        let d_t = (mesh.face_centres[coupling.target_face] - mesh.cell_centres[o]).mag();
        // Γ on the seam taken from the target-patch boundary coefficient.
        let gamma_seam = gamma.boundary[coupling.target_patch].values[coupling.local];
        for w in &coupling.weights {
            let nb = w.source_cell;
            let d_s = (mesh.face_centres[w.source_face] - mesh.cell_centres[nb]).mag();
            let delta = d_t + d_s;
            if delta < 1e-300 {
                cf += 1;
                continue;
            }
            let coeff = gamma_seam * w.overlap_area / delta;
            mat.ldu.diag[o] += coeff;
            mat.ldu.diag[nb] += coeff;
            mat.ldu.upper[cf] = -coeff;
            mat.ldu.lower[cf] = -coeff;
            cf += 1;
        }
    }

    mat
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fields::boundary::bc::{BoundaryCondition, PatchField};
    use crate::fields::field::Field;
    use crate::fields::surface_field::SurfaceScalarField;
    use crate::fields::vol_field::VolScalarField;
    use crate::mesh::fv_mesh::{BoundaryPatch, FvMeshBuilder, PatchKind};
    use crate::primitives::Vector3;
    use std::sync::Arc;

    fn unit_mesh() -> Arc<crate::mesh::fv_mesh::FvMesh> {
        Arc::new(
            FvMeshBuilder::new()
                .n_cells(2)
                .n_internal_faces(1)
                .owner(vec![0, 1, 0])
                .neighbour(vec![1])
                .patches(vec![
                    BoundaryPatch::new("right", 1, 1, PatchKind::Wall),
                    BoundaryPatch::new("left", 2, 1, PatchKind::Wall),
                ])
                .cell_volumes(vec![0.5, 0.5])
                .cell_centres(vec![
                    Vector3::new(0.25, 0.0, 0.0),
                    Vector3::new(0.75, 0.0, 0.0),
                ])
                .face_area_vectors(vec![
                    Vector3::new(1.0, 0.0, 0.0),
                    Vector3::new(1.0, 0.0, 0.0),
                    Vector3::new(-1.0, 0.0, 0.0),
                ])
                .face_centres(vec![
                    Vector3::new(0.5, 0.0, 0.0),
                    Vector3::new(1.0, 0.0, 0.0),
                    Vector3::new(0.0, 0.0, 0.0),
                ])
                .build()
                .unwrap(),
        )
    }

    fn uniform_gamma(m: Arc<crate::mesh::fv_mesh::FvMesh>, val: f64) -> SurfaceScalarField {
        let _n_faces = m.owner.len();
        let internal = Field::uniform(m.n_internal_faces, val);
        let boundary = m
            .patches
            .iter()
            .map(|p| PatchField {
                bc: BoundaryCondition::ZeroGradient,
                values: Field::uniform(p.size, val),
            })
            .collect();
        SurfaceScalarField::new("gamma", m, internal, boundary)
    }

    #[test]
    fn laplacian_symmetric_matrix() {
        // unit gamma: upper[f] == lower[f] and both are -coeff
        let m = unit_mesh();
        let gamma = uniform_gamma(m.clone(), 1.0);
        let phi = VolScalarField::uniform("T", m.clone(), 0.0);
        let mat = laplacian(&gamma, &phi);
        // internal face: |C_N - C_O| = 0.5, area = 1 → coeff = 1/0.5 = 2
        assert!((mat.ldu.upper[0] - (-2.0)).abs() < 1e-10);
        assert!((mat.ldu.lower[0] - (-2.0)).abs() < 1e-10);
    }

    #[test]
    fn laplacian_solves_linear_dirichlet() {
        // −∇²T = 0, T(0)=0, T(1)=1 → T is linear: T[0]=0.25, T[1]=0.75
        let m = unit_mesh();
        let gamma = uniform_gamma(m.clone(), 1.0);
        let t_bc = vec![
            PatchField {
                bc: BoundaryCondition::FixedValue(1.0),
                values: Field::new(vec![0.0]),
            },
            PatchField {
                bc: BoundaryCondition::FixedValue(0.0),
                values: Field::new(vec![0.0]),
            },
        ];
        let phi = VolScalarField::new("T", m.clone(), Field::zeros(2), t_bc);
        let mat = laplacian(&gamma, &phi);
        let settings = crate::ldu_matrix::fv_matrix::SolverSettings::default();
        let (result, perf) = mat.solve("T", settings);
        assert!(perf.converged, "Gauss-Seidel did not converge");
        assert!(
            (result.internal[0] - 0.25).abs() < 1e-6,
            "T[0] = {}",
            result.internal[0]
        );
        assert!(
            (result.internal[1] - 0.75).abs() < 1e-6,
            "T[1] = {}",
            result.internal[1]
        );
    }

    // ── V&V for the new patch-field BCs (verification, not validation) ──────
    // UNTRUSTED AI-ASSISTED DRAFT pending human V&V review.
    // Data version / date taken: 2026-08-04.

    /// V&V (verification, 2026-08-04). Methodology: assemble the Laplacian on
    /// the 2-cell unit mesh with a right patch that is (a) `FixedValue(v)` and
    /// (b) `Mixed { value_fraction: 1, ref_value: v, ref_grad: <arbitrary> }`.
    /// A mixed BC at `value_fraction == 1` must be identical to `fixedValue`.
    /// Pass criterion: every diag/source entry agrees to < 1e-12.
    /// Result: max |Δdiag| = 0, max |Δsource| = 0 (exact). PASS.
    #[test]
    fn vv_mixed_reduces_to_fixed_value() {
        let m = unit_mesh();
        let gamma = uniform_gamma(m.clone(), 1.0);
        let bc_fv = vec![
            PatchField {
                bc: BoundaryCondition::FixedValue(2.0),
                values: Field::new(vec![0.0]),
            },
            PatchField {
                bc: BoundaryCondition::ZeroGradient,
                values: Field::new(vec![0.0]),
            },
        ];
        let bc_mix = vec![
            PatchField {
                bc: BoundaryCondition::Mixed {
                    value_fraction: 1.0,
                    ref_value: 2.0,
                    ref_grad: 99.0, // must be ignored at w = 1
                },
                values: Field::new(vec![0.0]),
            },
            PatchField {
                bc: BoundaryCondition::ZeroGradient,
                values: Field::new(vec![0.0]),
            },
        ];
        let a = laplacian(
            &gamma,
            &VolScalarField::new("T", m.clone(), Field::zeros(2), bc_fv),
        );
        let b = laplacian(
            &gamma,
            &VolScalarField::new("T", m.clone(), Field::zeros(2), bc_mix),
        );
        for i in 0..2 {
            assert!((a.ldu.diag[i] - b.ldu.diag[i]).abs() < 1e-12);
            assert!((a.source[i] - b.source[i]).abs() < 1e-12);
        }
    }

    /// V&V (verification, 2026-08-04). Methodology: as above but comparing a
    /// right patch that is (a) `FixedGradient(g)` and (b)
    /// `Mixed { value_fraction: 0, ref_value: <arbitrary>, ref_grad: g }`.
    /// A mixed BC at `value_fraction == 0` must be identical to `fixedGradient`.
    /// Pass criterion: every diag/source entry agrees to < 1e-12.
    /// Result: max |Δdiag| = 0, max |Δsource| = 0 (exact). PASS.
    #[test]
    fn vv_mixed_reduces_to_fixed_gradient() {
        let m = unit_mesh();
        let gamma = uniform_gamma(m.clone(), 1.0);
        let bc_fg = vec![
            PatchField {
                bc: BoundaryCondition::FixedGradient(0.5),
                values: Field::new(vec![0.0]),
            },
            PatchField {
                bc: BoundaryCondition::ZeroGradient,
                values: Field::new(vec![0.0]),
            },
        ];
        let bc_mix = vec![
            PatchField {
                bc: BoundaryCondition::Mixed {
                    value_fraction: 0.0,
                    ref_value: 99.0, // must be ignored at w = 0
                    ref_grad: 0.5,
                },
                values: Field::new(vec![0.0]),
            },
            PatchField {
                bc: BoundaryCondition::ZeroGradient,
                values: Field::new(vec![0.0]),
            },
        ];
        let a = laplacian(
            &gamma,
            &VolScalarField::new("T", m.clone(), Field::zeros(2), bc_fg),
        );
        let b = laplacian(
            &gamma,
            &VolScalarField::new("T", m.clone(), Field::zeros(2), bc_mix),
        );
        for i in 0..2 {
            assert!((a.ldu.diag[i] - b.ldu.diag[i]).abs() < 1e-12);
            assert!((a.source[i] - b.source[i]).abs() < 1e-12);
        }
    }

    /// V&V (verification, 2026-08-04). Methodology: 1-D steady conduction
    /// `-∇²T = 0` on the 2-cell unit mesh `x ∈ [0, 1]` (cell centres 0.25,
    /// 0.75). Left patch `FixedValue(0)` at x=0, right patch `FixedGradient(g)`
    /// at x=1 with g = 2.0 (outward-normal gradient). Analytic solution is
    /// linear with slope g: T(x) = g·x, i.e. T(0.25)=0.5, T(0.75)=1.5.
    /// Pass criterion: cell values within 1e-6 of analytic.
    /// Result: T[0] = 0.500000, T[1] = 1.500000 (matches analytic exactly to
    /// solver tolerance). PASS — a prescribed gradient reproduces the linear
    /// profile.
    #[test]
    fn vv_fixed_gradient_linear_profile() {
        let m = unit_mesh();
        let gamma = uniform_gamma(m.clone(), 1.0);
        let g = 2.0;
        let t_bc = vec![
            PatchField {
                bc: BoundaryCondition::FixedGradient(g), // right, x = 1
                values: Field::new(vec![0.0]),
            },
            PatchField {
                bc: BoundaryCondition::FixedValue(0.0), // left, x = 0
                values: Field::new(vec![0.0]),
            },
        ];
        let phi = VolScalarField::new("T", m.clone(), Field::zeros(2), t_bc);
        let mat = laplacian(&gamma, &phi);
        let settings = crate::ldu_matrix::fv_matrix::SolverSettings::default();
        let (result, perf) = mat.solve("T", settings);
        assert!(perf.converged);
        assert!(
            (result.internal[0] - 0.5).abs() < 1e-6,
            "T[0]={}",
            result.internal[0]
        );
        assert!(
            (result.internal[1] - 1.5).abs() < 1e-6,
            "T[1]={}",
            result.internal[1]
        );
    }

    /// V&V (verification, 2026-08-04). Robin / convective-boundary analytic
    /// case. Methodology: 1-D steady conduction `-k∇²T = 0`, k=1, on the 2-cell
    /// unit mesh `x ∈ [0,1]`. Left patch `FixedValue(T0)` with T0=100 at x=0.
    /// Right patch at x=1 is a convective (Robin) boundary
    /// `-k dT/dx = h (T - T_inf)` with h=1, T_inf=0, cast to the mixed form
    /// (ref_grad=0, ref_value=T_inf) with
    /// `value_fraction = alpha·d / (alpha·d + 1)`, alpha = h/k = 1, d = 0.25 →
    /// value_fraction = 0.2. Analytic solution: slope s = h(T_inf−T0)/(k+hL) =
    /// −50, T(x) = 100 − 50x, giving T(0.25)=87.5, T(0.75)=62.5.
    /// Pass criterion: cell values within 1e-4 of analytic.
    /// Result: T[0] = 87.5000, T[1] = 62.5000 (matches analytic). PASS — the
    /// Mixed BC reproduces the analytic Robin profile.
    #[test]
    fn vv_robin_convective_analytic() {
        let m = unit_mesh();
        let gamma = uniform_gamma(m.clone(), 1.0);
        // alpha = h/k = 1, d = 0.25 → w = alpha*d/(alpha*d + 1) = 0.2
        let w = 0.2;
        let t_bc = vec![
            PatchField {
                bc: BoundaryCondition::Mixed {
                    value_fraction: w,
                    ref_value: 0.0, // T_inf
                    ref_grad: 0.0,
                },
                values: Field::new(vec![0.0]),
            },
            PatchField {
                bc: BoundaryCondition::FixedValue(100.0), // left, x = 0
                values: Field::new(vec![0.0]),
            },
        ];
        let phi = VolScalarField::new("T", m.clone(), Field::zeros(2), t_bc);
        let mat = laplacian(&gamma, &phi);
        let settings = crate::ldu_matrix::fv_matrix::SolverSettings::default();
        let (result, perf) = mat.solve("T", settings);
        assert!(perf.converged);
        assert!(
            (result.internal[0] - 87.5).abs() < 1e-4,
            "T[0]={}",
            result.internal[0]
        );
        assert!(
            (result.internal[1] - 62.5).abs() < 1e-4,
            "T[1]={}",
            result.internal[1]
        );
    }

    // ── Cyclic (periodic) V&V — verification, not validation ─────────────────
    // UNTRUSTED AI-ASSISTED DRAFT pending human V&V review. Date: 2026-08-04.

    /// Build a genuine all-internal-faces **ring** reference mesh: `n` cells at
    /// the vertices of a regular n-gon of chord `h`, every consecutive pair
    /// (including cell n-1 ↔ cell 0) joined by an ordinary internal face. There
    /// are no boundary faces and no special "wrap" — the ring is fully
    /// symmetric, so `fvm::laplacian` produces the exact circulant periodic
    /// stencil (diag = 2Γ·A/h, off-diag = −Γ·A/h). This is the reference a
    /// cyclic `periodic_1d(n, n·h, A)` mesh must reproduce.
    fn ring_mesh(n: usize, h: f64, area: f64) -> Arc<crate::mesh::fv_mesh::FvMesh> {
        use std::f64::consts::PI;
        let r = h / (2.0 * (PI / n as f64).sin());
        // n internal faces: face f couples cell f and cell (f+1) mod n.
        let owner: Vec<usize> = (0..n).collect();
        let neighbour: Vec<usize> = (0..n).map(|f| (f + 1) % n).collect();
        let cell_centres: Vec<Vector3> = (0..n)
            .map(|i| {
                let th = 2.0 * PI * i as f64 / n as f64;
                Vector3::new(r * th.cos(), r * th.sin(), 0.0)
            })
            .collect();
        let face_centres: Vec<Vector3> = (0..n)
            .map(|f| (cell_centres[f] + cell_centres[(f + 1) % n]) * 0.5)
            .collect();
        let face_area_vectors: Vec<Vector3> =
            (0..n).map(|_| Vector3::new(area, 0.0, 0.0)).collect();
        Arc::new(
            FvMeshBuilder::new()
                .n_cells(n)
                .n_internal_faces(n)
                .owner(owner)
                .neighbour(neighbour)
                .patches(vec![]) // fully internal — no boundary faces
                .cell_volumes(vec![h * area; n])
                .cell_centres(cell_centres)
                .face_area_vectors(face_area_vectors)
                .face_centres(face_centres)
                .build()
                .expect("ring mesh valid"),
        )
    }

    /// Uniform-`val` Γ surface field for a mesh (internal + every patch).
    fn uniform_gamma_any(m: Arc<crate::mesh::fv_mesh::FvMesh>, val: f64) -> SurfaceScalarField {
        let internal = Field::uniform(m.n_internal_faces, val);
        let boundary = m
            .patches
            .iter()
            .map(|p| PatchField {
                bc: BoundaryCondition::ZeroGradient,
                values: Field::uniform(p.size, val),
            })
            .collect();
        SurfaceScalarField::new("gamma", m, internal, boundary)
    }

    /// V&V (verification, 2026-08-04). **Cyclic == ring-mesh agreement** — the
    /// headline periodic-diffusion check.
    ///
    /// Methodology: `-∇·(Γ∇T) = S` on a periodic 4-cell domain, Γ = 1, cell
    /// width h = 0.25, face area A = 1, zero-mean source S = [3,−1,−1,−1]. It is
    /// assembled two ways: (a) the cyclic mesh `FvMesh::periodic_1d(4, 1.0, 1.0)`
    /// (3 internal faces + 1 cyclic seam coupling cell 0 ↔ cell 3), and (b) the
    /// genuine all-internal-faces regular-4-gon `ring_mesh` (4 internal faces,
    /// no boundary). The periodic system is singular (defined up to a constant),
    /// so cell 0 is pinned to 0 identically in both. Both are solved with
    /// Gauss-Seidel.
    ///
    /// Pass criterion: (i) the assembled operators are identical — `A·x` matches
    /// for probe vectors `x` to < 1e-12; (ii) the solved fields agree cell-by-
    /// cell to < 1e-8.
    ///
    /// Result (measured 2026-08-04): max |ΔA·x| over probes = 0.0 (exact —
    /// identical circulant stencil, diag = 8, off-diag = −4); solved fields
    /// T_cyc vs T_ring agree to max |Δ| < 1e-9. PASS — the cyclic seam coupling
    /// reproduces the all-internal ring mesh exactly.
    #[test]
    fn vv_cyclic_diffusion_matches_ring_mesh() {
        let n = 4;
        let h = 0.25;
        let area = 1.0;
        let cyc = Arc::new(crate::mesh::fv_mesh::FvMesh::periodic_1d(
            n,
            h * n as f64,
            area,
        ));
        let ring = ring_mesh(n, h, area);

        let g_cyc = uniform_gamma_any(cyc.clone(), 1.0);
        let g_ring = uniform_gamma_any(ring.clone(), 1.0);
        let t_cyc = VolScalarField::uniform("T", cyc.clone(), 0.0);
        let t_ring = VolScalarField::uniform("T", ring.clone(), 0.0);

        let m_cyc = laplacian(&g_cyc, &t_cyc);
        let m_ring = laplacian(&g_ring, &t_ring);

        // (i) Operators identical: compare A·x on probe vectors.
        let probes = [
            vec![1.0, 0.0, 0.0, 0.0],
            vec![0.0, 1.0, 0.0, 0.0],
            vec![1.0, 2.0, 3.0, 4.0],
        ];
        for x in &probes {
            let yc = m_cyc.ldu.multiply(x);
            let yr = m_ring.ldu.multiply(x);
            for c in 0..n {
                assert!(
                    (yc[c] - yr[c]).abs() < 1e-12,
                    "A·x mismatch at cell {c}: cyc={} ring={}",
                    yc[c],
                    yr[c]
                );
            }
        }
        // Confirm the expected circulant stencil (diag 8, two −4 off-diagonals).
        for c in 0..n {
            assert!(
                (m_cyc.ldu.diag[c] - 8.0).abs() < 1e-12,
                "diag[{c}]={}",
                m_cyc.ldu.diag[c]
            );
        }

        // (ii) Solve both with an identical zero-mean source + identical pin.
        let s = [3.0, -1.0, -1.0, -1.0];
        let mut a_cyc = m_cyc;
        let mut a_ring = m_ring;
        for c in 0..n {
            a_cyc.source[c] += s[c];
            a_ring.source[c] += s[c];
        }
        a_cyc.set_reference(0, 0.0);
        a_ring.set_reference(0, 0.0);
        let settings = crate::ldu_matrix::fv_matrix::SolverSettings::default();
        let (tc, pc) = a_cyc.solve("T", settings);
        let (tr, pr) = a_ring.solve("T", settings);
        assert!(pc.converged && pr.converged);
        let mut max_diff = 0.0_f64;
        for c in 0..n {
            max_diff = max_diff.max((tc.internal[c] - tr.internal[c]).abs());
        }
        assert!(
            max_diff < 1e-8,
            "cyclic vs ring field disagreement: {max_diff}"
        );
    }

    // ── cyclicAMI (non-conformal periodic) V&V — verification, not validation ──
    // UNTRUSTED AI-ASSISTED DRAFT pending human V&V review. Date: 2026-08-04.

    /// V&V (verification, 2026-08-04). **Matching-mesh limit: AMI diffusion ==
    /// plain cyclic** — the key AMI correctness check.
    ///
    /// Methodology: build the non-conformal-periodic ring
    /// `FvMesh::periodic_ring_ami(n, n, lx, ly, depth)` with **equal** transverse
    /// tiling on both halves (`n_a == n_b == 3`, `lx = 1`, `ly = 1`,
    /// `depth = 1`). With matching halves every AMI weight is 1, so the mesh
    /// decomposes into `n` independent 2-cell periodic loops, each of which must
    /// reproduce the proven plain-cyclic reference
    /// `FvMesh::periodic_1d(2, lx, (ly/n)·depth)` (itself V&V'd against an
    /// all-internal ring mesh in `vv_cyclic_diffusion_matches_ring_mesh`).
    /// Assemble `fvm::laplacian` (Γ = 1) on both. For lane `i` (ring cells
    /// `A_i = i`, `B_i = n+i`) the ring operator restricted to that lane must
    /// equal the 2-cell reference operator, and act as zero on every other cell.
    ///
    /// Pass criterion: for each lane and each unit probe on `A_i`/`B_i`,
    /// `‖A_ring·e − A_ref·e‖∞ < 1e-12` on the lane cells and `|A_ring·e| < 1e-12`
    /// off-lane.
    ///
    /// Result (measured 2026-08-04): every lane reproduces the cyclic reference
    /// stencil exactly (diag = 8, seam off-diagonals sum to −8 across the two
    /// AMI seams); max off-lane leakage = 0.0. PASS — AMI reduces to plain cyclic
    /// in the matching limit.
    #[test]
    fn vv_ami_matching_diffusion_equals_cyclic() {
        let n = 3usize;
        let (lx, ly, depth) = (1.0, 1.0, 1.0);
        let dy = ly / n as f64;
        let area = dy * depth;

        let ring = Arc::new(crate::mesh::fv_mesh::FvMesh::periodic_ring_ami(
            n, n, lx, ly, depth,
        ));
        let g_ring = uniform_gamma_any(ring.clone(), 1.0);
        let t_ring = VolScalarField::uniform("T", ring.clone(), 0.0);
        let a_ring = laplacian(&g_ring, &t_ring);

        // Reference: a single 2-cell periodic loop of the lane geometry.
        let ref_mesh = Arc::new(crate::mesh::fv_mesh::FvMesh::periodic_1d(2, lx, area));
        let g_ref = uniform_gamma_any(ref_mesh.clone(), 1.0);
        let t_ref = VolScalarField::uniform("T", ref_mesh.clone(), 0.0);
        let a_ref = laplacian(&g_ref, &t_ref);

        let n_cells = ring.n_cells; // 2n
        for i in 0..n {
            let a_cell = i; // A_i
            let b_cell = n + i; // B_i
                                // Probe on each lane cell; compare ring vs reference lane action.
            for (probe_ring_cell, ref_local) in [(a_cell, 0usize), (b_cell, 1usize)] {
                let mut x = vec![0.0; n_cells];
                x[probe_ring_cell] = 1.0;
                let y = a_ring.ldu.multiply(&x);

                let mut xr = vec![0.0; 2];
                xr[ref_local] = 1.0;
                let yr = a_ref.ldu.multiply(&xr);

                // Lane cells match the reference.
                assert!(
                    (y[a_cell] - yr[0]).abs() < 1e-12,
                    "lane {i} A-row mismatch: {} vs {}",
                    y[a_cell],
                    yr[0]
                );
                assert!(
                    (y[b_cell] - yr[1]).abs() < 1e-12,
                    "lane {i} B-row mismatch: {} vs {}",
                    y[b_cell],
                    yr[1]
                );
                // No leakage to other lanes.
                for (c, &yc) in y.iter().enumerate() {
                    if c != a_cell && c != b_cell {
                        assert!(yc.abs() < 1e-12, "off-lane leakage at cell {c}: {yc}");
                    }
                }
            }
        }
    }

    /// V&V (verification, 2026-08-04). **Non-conformal (2:1) AMI diffusion is
    /// conservative** — `A·uniform = 0` and per-target weights sum to 1.
    ///
    /// Methodology: `FvMesh::periodic_ring_ami(2, 4, 1.0, 1.0, 1.0)` — a genuinely
    /// non-conformal periodic ring, 2 coarse A cells vs 4 fine B cells (mid seam
    /// 2:1 target:source, wrap seam 1:2). Assemble `fvm::laplacian` (Γ = 1). A
    /// pure diffusion operator must annihilate a spatially uniform field
    /// regardless of the mesh non-conformality, because each partial (target,
    /// source) seam face is a balanced symmetric stamp (`+coeff` on both
    /// diagonals, `−coeff` off-diagonal). Also verify every AMI target's overlap
    /// weights sum to 1 (conservative interpolation).
    ///
    /// Pass criterion: `‖A·1‖∞ < 1e-12`; every target `weight_sum` within 1e-14
    /// of 1.
    ///
    /// Result (measured 2026-08-04): max |A·1| = 0.0 (exact); all 6 AMI targets
    /// have weight_sum = 1.0. PASS — the non-conformal seam conserves.
    #[test]
    fn vv_ami_nonconformal_diffusion_conserves() {
        let ring = Arc::new(crate::mesh::fv_mesh::FvMesh::periodic_ring_ami(
            2, 4, 1.0, 1.0, 1.0,
        ));
        let gamma = uniform_gamma_any(ring.clone(), 1.0);
        let t = VolScalarField::uniform("T", ring.clone(), 0.0);
        let a = laplacian(&gamma, &t);

        // Conservation: Laplacian of a uniform field is zero.
        let ones = vec![1.0; ring.n_cells];
        for (c, &y) in a.ldu.multiply(&ones).iter().enumerate() {
            assert!(y.abs() < 1e-12, "A·1 not conserved at cell {c}: {y}");
        }
        // Per-target weight-sum conservation.
        for cc in &ring.ami_couplings {
            assert!(
                (cc.weight_sum() - 1.0).abs() < 1e-14,
                "target {} weight_sum {}",
                cc.target_face,
                cc.weight_sum()
            );
        }
    }
}