semiflow 0.15.0-beta

no_std Rust PDE solver: evolution equations via Chernoff approximation of operator semigroups (heat, diffusion, Schrodinger)
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
//! [`Strang2D`] — palindromic Strang composition for 2D tensor-product generators.
//!
//! Implements `Φ²ᴰ(τ) = AxisLift<X>(τ/2) ∘ AxisLift<Y>(τ) ∘ AxisLift<X>(τ/2)`
//! for separable generators `L = L_x ⊗ I + I ⊗ L_y`.
//!
//! Global order 2 (math.md §10 Theorem 7): the commutator identity
//! `[L_x ⊗ I, I ⊗ L_y] = 0` makes palindromic Strang exact at the BCH level;
//! per-axis order-2 lifts are therefore sufficient.
//!
//! `Strang2D` is a **dedicated type** (closed ADR-0012 decision) — it does NOT
//! generalise the existing `StrangSplit<D, R>` to avoid touching stable v0.2.0+ code.
//!
//! When the `parallel` feature is enabled, `apply` dispatches to an 8-thread
//! palindromic Strang kernel via `std::thread::scope` (ADR-0018).  The parallel
//! and serial paths return **bit-identical** results (see
//! `tests/strang2d_parallel_bit_equal.rs`); callers see no API change, but
//! the `ChernoffFunction` impl gains `Send + Sync` bounds on `X` and `Y`.
//!
//! ## Generic-over-Float (ADR-0025, v0.9.0 Block D Wave 4)
//!
//! `Strang2D<X, Y, F: SemiflowFloat = f64>` — the `= f64` default keeps all
//! existing call-sites compiling unchanged. The parallel path remains f64-only
//! (the parallel feature requires `X, Y: ChernoffFunction<f64>`).
//!
//! See `contracts/semiflow-core.tensor.yaml` §3 `Strang2D`,
//! `math.md` §10 Theorem 7 and Lemma 10.2,
//! `docs/adr/0012-tensor-product-2d.md`,
//! and `docs/adr/0018-parallel-strang2d.md`.

use alloc::vec::Vec;

#[cfg(feature = "parallel")]
use crate::parallel_pool::ParallelPool2D;
#[cfg(feature = "parallel")]
use crate::strang2d_parallel::{
    parallel_x_pass, parallel_y_pass, resolve_threads, MIN_ROWS_PER_THREAD,
};
use crate::{
    axis::{Axis, AxisLift},
    chernoff::{ChernoffFunction, Growth},
    error::SemiflowError,
    float::{half, SemiflowFloat},
    grid_fn::GridFn1D,
    grid_fn2d::GridFn2D,
    scratch::ScratchPool,
};

// ---------------------------------------------------------------------------
// Strang2D
// ---------------------------------------------------------------------------

/// Palindromic [`Strang2D`] composition for 2D tensor-product generators.
///
/// Composition: `Φ²ᴰ(τ) = AxisLift<X>(τ/2) ∘ AxisLift<Y>(τ) ∘ AxisLift<X>(τ/2)`.
///
/// Both `X` and `Y` type parameters must implement
/// `ChernoffFunction<F, S = GridFn1D<F>> + Clone`.
///
/// Implements `ChernoffFunction<F, S = GridFn2D<F>>` with `order = 2` (canonical
/// Strang global order for separable `L`).
///
/// When the `parallel` feature is enabled, `apply` dispatches to an 8-thread
/// palindromic Strang kernel via `std::thread::scope` (ADR-0018); the serial and
/// parallel paths return **bit-identical** results.
///
/// ## Generic-over-Float (ADR-0025, v0.9.0 Block D Wave 4)
///
/// `Strang2D<X, Y, F: SemiflowFloat = f64>` — the `= f64` default keeps all
/// existing call-sites compiling unchanged.
///
/// # Example
///
/// ```rust
/// use semiflow::{
///     chernoff::ApplyChernoffExt,
///     Grid1D, Grid2D, GridFn2D, DiffusionChernoff, Strang2D, AxisLift, Axis,
/// };
/// let gx = Grid1D::new(-4.0, 4.0, 32).unwrap();
/// let gy = Grid1D::new(-4.0, 4.0, 32).unwrap();
/// let grid2 = Grid2D::new(gx, gy);
/// let dx = DiffusionChernoff::new(|_| 0.5, |_| 0.0, |_| 0.0, 0.5, gx);
/// let dy = DiffusionChernoff::new(|_| 0.5, |_| 0.0, |_| 0.0, 0.5, gy);
/// let strang2d = Strang2D::new(dx, dy);
/// let u0 = GridFn2D::from_fn(grid2, |x, y| (-(x * x + y * y)).exp());
/// let u1 = strang2d.apply_chernoff(0.01, &u0).unwrap();
/// assert_eq!(u1.values.len(), 32 * 32);
/// ```
///
/// See `contracts/semiflow-core.tensor.yaml` §3 `Strang2D`.
#[derive(Clone, Debug)]
pub struct Strang2D<X, Y, F: SemiflowFloat = f64> {
    /// X-axis lift (`AxisLift::new(_, Axis::X)`). Applied at half-step twice.
    pub x: AxisLift<X, F>,
    /// Y-axis lift (`AxisLift::new(_, Axis::Y)`). Applied at full step once.
    pub y: AxisLift<Y, F>,
}

impl<X, Y, F: SemiflowFloat> Strang2D<X, Y, F>
where
    X: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
    Y: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
{
    /// Construct a `Strang2D` from the two inner 1D Chernoff functions.
    ///
    /// No validation — both `x_inner` and `y_inner` are already valid
    /// `ChernoffFunction<F>`s.
    ///
    /// See `contracts/semiflow-core.tensor.yaml` §3 `Strang2D::new`.
    #[must_use]
    pub fn new(x_inner: X, y_inner: Y) -> Self {
        Self {
            x: AxisLift::new(x_inner, Axis::X),
            y: AxisLift::new(y_inner, Axis::Y),
        }
    }
}

// ---------------------------------------------------------------------------
// Serial ChernoffFunction impl (no `parallel` feature)
// ---------------------------------------------------------------------------

#[cfg(not(feature = "parallel"))]
impl<X, Y, F: SemiflowFloat> ChernoffFunction<F> for Strang2D<X, Y, F>
where
    X: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
    Y: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
{
    type S = GridFn2D<F>;

    /// Wave 2 (ADR-0042): 3-leg palindromic ping-pong, 0 allocs in steady state.
    // (serial impl — no feature guard needed)
    fn apply_into(
        &self,
        tau: F,
        src: &GridFn2D<F>,
        dst: &mut GridFn2D<F>,
        scratch: &mut ScratchPool<F>,
    ) -> Result<(), SemiflowError> {
        apply_strang2d_into(tau, src, dst, &self.x, &self.y, scratch)
    }

    fn order(&self) -> u32 {
        serial_order(&self.x, &self.y)
    }

    fn growth(&self) -> Growth<F> {
        serial_growth(&self.x, &self.y)
    }
}

// ---------------------------------------------------------------------------
// Parallel ChernoffFunction impl (`parallel` feature enabled)
// ---------------------------------------------------------------------------

// Parallel impl — generic over F: SemiflowFloat + Send + Sync + ParallelPool2D
// (ADR-0045 Wave 5). SIMD stays f64-specialised inside strang2d_parallel.rs;
// the type signature change here is zero-cost (monomorphisation).
// f64 codegen path is unchanged; f64 bit-equality contract (ADR-0018) preserved.
// `private_bounds`: ParallelPool2D is a sealed pub(crate) trait — downstream
// crates cannot name it; the bound is merely an implementation detail.
#[cfg(feature = "parallel")]
#[allow(private_bounds)]
impl<X, Y, F> ChernoffFunction<F> for Strang2D<X, Y, F>
where
    F: SemiflowFloat + Send + Sync + ParallelPool2D,
    X: ChernoffFunction<F, S = GridFn1D<F>> + Clone + Send + Sync,
    Y: ChernoffFunction<F, S = GridFn1D<F>> + Clone + Send + Sync,
{
    type S = GridFn2D<F>;

    /// Parallel palindromic ping-pong — parallel X/Y passes wire into evolve.
    ///
    /// Delegates to `apply_parallel_into` so that `ChernoffSemigroup::evolve`
    /// benefits from multi-threading. Small grids fall back to the serial
    /// scratch-pool path (zero allocation in steady state).
    fn apply_into(
        &self,
        tau: F,
        src: &GridFn2D<F>,
        dst: &mut GridFn2D<F>,
        scratch: &mut ScratchPool<F>,
    ) -> Result<(), SemiflowError> {
        self.apply_parallel_into(tau, src, dst, scratch)
    }

    fn order(&self) -> u32 {
        serial_order(&self.x, &self.y)
    }

    fn growth(&self) -> Growth<F> {
        serial_growth(&self.x, &self.y)
    }
}

// ---------------------------------------------------------------------------
// Shared order/growth helpers (both impls use the same logic)
// ---------------------------------------------------------------------------

/// Canonical Strang τ-axis order: `min(X.order(), Y.order(), 4)`.
///
/// See `contracts/semiflow-core.tensor.yaml` §3 `Strang2D::order`
/// and math.md §11.1.bis.
fn serial_order<X, Y, F: SemiflowFloat>(x: &AxisLift<X, F>, y: &AxisLift<Y, F>) -> u32
where
    X: ChernoffFunction<F, S = GridFn1D<F>>,
    Y: ChernoffFunction<F, S = GridFn1D<F>>,
{
    x.order().min(y.order()).min(4)
}

/// Composed growth bound `(M_x² · M_y, ω_x + ω_y)`.
///
/// See `contracts/semiflow-core.tensor.yaml` §3 `Strang2D::growth`.
fn serial_growth<X, Y, F: SemiflowFloat>(x: &AxisLift<X, F>, y: &AxisLift<Y, F>) -> Growth<F>
where
    X: ChernoffFunction<F, S = GridFn1D<F>>,
    Y: ChernoffFunction<F, S = GridFn1D<F>>,
{
    let gx = x.growth();
    let gy = y.growth();
    Growth {
        multiplier: gx.multiplier * gy.multiplier * gx.multiplier,
        omega: gx.omega + gy.omega,
    }
}

// ---------------------------------------------------------------------------
// Serial apply helper (v0.7.0 body — no content change)
// ---------------------------------------------------------------------------

impl<X, Y, F: SemiflowFloat> Strang2D<X, Y, F>
where
    X: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
    Y: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
{
    /// Serial palindromic Strang sandwich:
    /// `f3 = X(τ/2) ( Y(τ) ( X(τ/2) f ) )`.
    ///
    /// Identical to the v0.7.0 `apply` body. Called by both the serial impl
    /// (all builds) and the parallel impl (small-grid fallback).
    /// `pub(crate)` so `nonseparable2d` can call it directly without the
    /// `Send + Sync` bounds required by the parallel `ChernoffFunction` impl.
    // called from strang3d_helpers.rs apply_parallel fallback and nonseparable_mixed
    #[allow(dead_code)]
    pub(crate) fn apply_serial(
        &self,
        tau: F,
        f: &GridFn2D<F>,
    ) -> Result<GridFn2D<F>, SemiflowError> {
        apply_strang2d_full(tau, f, &self.x, &self.y)
    }

    /// Fused-axis path: `Y(τ) ∘ X(τ)` — 2 passes instead of 3.
    ///
    /// **ABORTED in ADR-0039 v0.13.0 Wave C**: experimental evidence shows that
    /// `‖fused(τ) − palindromic(τ)‖` scales as O(τ¹·¹), NOT O(τ²).  The
    /// palindromic structure `X(τ/2) ∘ Y(τ) ∘ X(τ/2)` is essential for
    /// achieving second-order accuracy with `DiffusionChernoff` even though
    /// `[L_x, L_y] = 0`; the half-step composition changes the FP error
    /// structure in a way that the theoretical BCH cancellation does not capture
    /// numerically.  This method is retained here solely to allow the
    /// `strang_fused_order_confirmation_2d` test to document the finding.
    ///
    /// **Do NOT wire into `apply_serial` or any production path** without
    /// re-running the slope gate `STRANG_FUSED_TAU2_PRESERVATION` (ADR-0039).
    // used by strang_fused_order_confirmation_2d test (documents aborted experiment)
    #[allow(dead_code)]
    fn apply_fused(&self, tau: F, f: &GridFn2D<F>) -> Result<GridFn2D<F>, SemiflowError> {
        use crate::chernoff::ApplyChernoffExt;
        let f1 = self.x.apply_chernoff(tau, f)?;
        let f2 = self.y.apply_chernoff(tau, &f1)?;
        Ok(f2)
    }
}

/// 3-pass palindromic Strang helper (shared between serial and fused dispatch).
// called from apply_serial and tests; lint fires when feature = "parallel" is on
#[allow(dead_code)]
fn apply_strang2d_full<X, Y, F: SemiflowFloat>(
    tau: F,
    f: &GridFn2D<F>,
    x: &crate::axis::AxisLift<X, F>,
    y: &crate::axis::AxisLift<Y, F>,
) -> Result<GridFn2D<F>, SemiflowError>
where
    X: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
    Y: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
{
    use crate::chernoff::ApplyChernoffExt;
    let half_tau = half::<F>() * tau;
    let f1 = x.apply_chernoff(half_tau, f)?;
    let f2 = y.apply_chernoff(tau, &f1)?;
    let f3 = x.apply_chernoff(half_tau, &f2)?;
    Ok(f3)
}

// ---------------------------------------------------------------------------
// Wave 2 (ADR-0042): 3-leg ping-pong apply_into helper
// ---------------------------------------------------------------------------

/// 3-leg palindromic ping-pong via `ScratchPool`.
///
/// Buffer parity rule (ADR-0042 §2):
///
/// - `buf_a` = copy of `src.values`; `buf_b` = empty scratch
/// - Leg 1 X(τ/2): A→B, swap → A=leg1
/// - Leg 2 Y(τ):   A→B, swap → A=leg2
/// - Leg 3 X(τ/2): A→B (no swap) → final in B
///
/// Copy B → `dst.values`.
#[inline]
pub(crate) fn apply_strang2d_into<X, Y, F: SemiflowFloat>(
    tau: F,
    src: &GridFn2D<F>,
    dst: &mut GridFn2D<F>,
    x: &AxisLift<X, F>,
    y: &AxisLift<Y, F>,
    scratch: &mut ScratchPool<F>,
) -> Result<(), SemiflowError>
where
    X: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
    Y: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
{
    let n = src.values.len();
    let half_tau = half::<F>() * tau;
    if dst.values.len() != n {
        dst.values.resize(n, F::zero());
    }
    dst.grid = src.grid;

    let mut buf_a = scratch.take_vec(n);
    buf_a.copy_from_slice(&src.values);
    let mut buf_b = scratch.take_vec(n);

    run_axislift_into_2d(x, half_tau, &buf_a, &mut buf_b, src.grid, scratch)?;
    core::mem::swap(&mut buf_a, &mut buf_b);

    run_axislift_into_2d(y, tau, &buf_a, &mut buf_b, src.grid, scratch)?;
    core::mem::swap(&mut buf_a, &mut buf_b);

    // Leg 3: A→B, no swap — final result in buf_b.
    run_axislift_into_2d(x, half_tau, &buf_a, &mut buf_b, src.grid, scratch)?;

    dst.values.copy_from_slice(&buf_b);
    scratch.return_vec(buf_a);
    scratch.return_vec(buf_b);
    Ok(())
}

/// Wrap flat buffers in transient `GridFn2D` views and call `lift.apply_into`.
#[inline]
fn run_axislift_into_2d<C, F: SemiflowFloat>(
    lift: &AxisLift<C, F>,
    tau: F,
    src_buf: &[F],
    dst_buf: &mut Vec<F>,
    grid: crate::grid2d::Grid2D<F>,
    scratch: &mut ScratchPool<F>,
) -> Result<(), SemiflowError>
where
    C: ChernoffFunction<F, S = GridFn1D<F>> + Clone,
{
    let mut src_vals = scratch.take_vec(src_buf.len());
    src_vals.copy_from_slice(src_buf);
    let src_gf = GridFn2D {
        values: src_vals,
        grid,
    };
    let dst_vals = core::mem::take(dst_buf);
    let mut dst_gf = GridFn2D {
        values: dst_vals,
        grid,
    };
    lift.apply_into(tau, &src_gf, &mut dst_gf, scratch)?;
    *dst_buf = dst_gf.values;
    scratch.return_vec(src_gf.values);
    Ok(())
}

// ---------------------------------------------------------------------------
// Parallel apply helper — only compiled with `parallel` feature
// ---------------------------------------------------------------------------

// Parallel apply — generic over F: SemiflowFloat + Send + Sync + ParallelPool2D.
// SIMD specialisation lives inside strang2d_parallel.rs (parallel_x_pass /
// parallel_y_pass dispatch on F at the call site). ADR-0045 Wave 5.
#[cfg(feature = "parallel")]
#[allow(private_bounds)]
impl<X, Y, F> Strang2D<X, Y, F>
where
    F: SemiflowFloat + Send + Sync + ParallelPool2D,
    X: ChernoffFunction<F, S = GridFn1D<F>> + Clone + Send + Sync,
    Y: ChernoffFunction<F, S = GridFn1D<F>> + Clone + Send + Sync,
{
    /// Parallel palindromic Strang — allocation-free ping-pong path.
    ///
    /// Used by `ChernoffSemigroup::evolve` so that multi-step
    /// integration benefits from the parallel X/Y passes.
    ///
    /// For grids smaller than `2 * MIN_ROWS_PER_THREAD` rows, falls back to
    /// the serial scratch-pool path (zero allocation in steady state).
    /// For larger grids, allocates `y_scratch` locally (one Vec per call).
    fn apply_parallel_into(
        &self,
        tau: F,
        src: &GridFn2D<F>,
        dst: &mut GridFn2D<F>,
        scratch: &mut ScratchPool<F>,
    ) -> Result<(), SemiflowError> {
        let ny = src.grid.ny();

        if ny < 2 * MIN_ROWS_PER_THREAD {
            // Serial fallback — zero-alloc steady state via scratch pool.
            return apply_strang2d_into(tau, src, dst, &self.x, &self.y, scratch);
        }

        let n_threads = resolve_threads(ny);
        let half_tau = half::<F>() * tau;
        let nx = src.grid.nx();
        let mut state = src.values.clone();
        let mut y_scratch: Vec<F> = Vec::with_capacity(nx * ny);

        parallel_x_pass(&mut state, src.grid.x, n_threads, &self.x.inner, half_tau)?;
        parallel_y_pass(
            &mut state,
            src.grid.y,
            n_threads,
            &self.y.inner,
            tau,
            &mut y_scratch,
        )?;
        parallel_x_pass(&mut state, src.grid.x, n_threads, &self.x.inner, half_tau)?;

        dst.values.resize(state.len(), F::zero());
        dst.values.copy_from_slice(&state);
        dst.grid = src.grid;
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Unit tests — extracted to sibling file per ≤500-line cap.
// ---------------------------------------------------------------------------

#[cfg(test)]
#[path = "strang2d_tests.rs"]
mod tests;