primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! B-spline Functions for PPPM
//!
//! Cardinal B-splines are used for charge spreading and force interpolation
//! in the PPPM algorithm. They provide smooth, local interpolation between
//! particles and mesh nodes.
//!
//! # Mathematical Background
//!
//! The cardinal B-spline of order p is defined recursively:
//! ```text
//! M_1(x) = 1 if 0 ≤ x < 1, else 0
//! M_p(x) = (x/(p-1)) M_{p-1}(x) + ((p-x)/(p-1)) M_{p-1}(x-1)
//! ```
//!
//! Properties:
//! - Non-negative: `M_p(x)` ≥ 0
//! - Compact support: `M_p(x)` = 0 for x ≤ 0 or x ≥ p
//! - Partition of unity: Σ M_p(x-n) = 1 for all x
//! - Smooth: `M_p` ∈ C^{p-2} (p-2 continuous derivatives)
//!
//! # Usage in PPPM
//!
//! 1. **Charge spreading**: Each particle contributes to p³ mesh nodes
//! 2. **Force interpolation**: Each mesh node contributes to nearby particles
//!
//! The spreading/interpolation stencil is p × p × p nodes centered on the
//! particle's mesh cell.

/// WGSL kernel for cardinal B-spline evaluation (f64).
pub const WGSL_BSPLINE_F64: &str = include_str!("bspline_f64.wgsl");

use std::f64::consts::PI;

/// Cardinal B-spline of order p evaluated at x
///
/// Uses explicit formula for stability:
/// `M_p(x)` = 1/(p-1)! × Σ_{k=0}^{p} (-1)^k × C(p,k) × max(x-k, 0)^{p-1}
///
/// # Arguments
/// * `order` - B-spline order (typically 4-7 for PPPM)
/// * `x` - Evaluation point (should be in [0, order] for non-zero result)
///
/// # Returns
/// B-spline value `M_order(x)`
#[must_use]
pub fn bspline(order: usize, x: f64) -> f64 {
    if order == 0 {
        return 0.0;
    }
    if order == 1 {
        return if (0.0..1.0).contains(&x) { 1.0 } else { 0.0 };
    }

    let order_f = order as f64;
    if x <= 0.0 || x >= order_f {
        return 0.0;
    }

    let p = order;
    let pm1 = (p - 1) as i32;

    // Compute factorial (p-1)!
    let mut factorial = 1.0;
    for i in 2..=pm1 {
        factorial *= i as f64;
    }

    // Sum over k
    let mut sum = 0.0;
    for k in 0..=p {
        let xmk = x - k as f64;
        if xmk > 0.0 {
            let sign = if k % 2 == 0 { 1.0 } else { -1.0 };
            let binom = binomial(p, k) as f64;
            sum = (sign * binom).mul_add(xmk.powi(pm1), sum);
        }
    }

    sum / factorial
}

/// Binomial coefficient C(n, k)
fn binomial(n: usize, k: usize) -> usize {
    if k > n {
        return 0;
    }
    if k == 0 || k == n {
        return 1;
    }
    let k = k.min(n - k); // Use symmetry
    let mut result = 1;
    for i in 0..k {
        result = result * (n - i) / (i + 1);
    }
    result
}

/// Derivative of cardinal B-spline
///
/// `dM_p(x)/dx` = M_{p-1}(x) - M_{p-1}(x-1)
///
/// # Arguments
/// * `order` - B-spline order
/// * `x` - Evaluation point
///
/// # Returns
/// Derivative `dM_order(x)/dx`
#[must_use]
pub fn bspline_deriv(order: usize, x: f64) -> f64 {
    if order <= 1 {
        return 0.0;
    }

    bspline(order - 1, x) - bspline(order - 1, x - 1.0)
}

/// Precomputed B-spline coefficients for a particle
///
/// Stores the B-spline values for all nodes in the interpolation stencil.
#[derive(Clone, Debug)]
pub struct BsplineCoeffs {
    /// B-spline order
    pub order: usize,

    /// Grid index of leftmost node in stencil (for each dimension)
    pub base_idx: [i32; 3],

    /// B-spline values `W_k` for k = 0..order
    /// Stored as [`x_coeffs`, `y_coeffs`, `z_coeffs`]
    pub coeffs: [Vec<f64>; 3],

    /// B-spline derivatives for force interpolation
    pub derivs: [Vec<f64>; 3],
}

impl BsplineCoeffs {
    /// Compute B-spline coefficients for a particle at position (x, y, z)
    ///
    /// Uses the standard PPPM charge assignment scheme. For a particle at
    /// continuous position u and order p:
    /// - Base index n0 = floor(u) - (p-2)/2  (for even p)
    ///   = floor(u) - (p-1)/2  (for odd p)
    /// - Weight `W_k` = `M_p(u` - n0 - k) for k = 0, ..., p-1
    ///
    /// The weights satisfy: `Σ_k` `W_k` = 1 (partition of unity)
    ///
    /// # Arguments
    /// * `order` - B-spline order (typically 4-7)
    /// * `pos` - Particle position [x, y, z] in box coordinates
    /// * `mesh_dims` - Mesh dimensions [Kx, Ky, Kz]
    /// * `box_dims` - Box dimensions [Lx, Ly, Lz]
    #[must_use]
    pub fn compute(order: usize, pos: [f64; 3], mesh_dims: [usize; 3], box_dims: [f64; 3]) -> Self {
        let mut coeffs = [
            Vec::with_capacity(order),
            Vec::with_capacity(order),
            Vec::with_capacity(order),
        ];
        let mut derivs = [
            Vec::with_capacity(order),
            Vec::with_capacity(order),
            Vec::with_capacity(order),
        ];
        let mut base_idx = [0i32; 3];

        let p = order;

        for d in 0..3 {
            // Convert position to mesh units (scaled to [0, K))
            let u = pos[d] / box_dims[d] * mesh_dims[d] as f64;

            // Base index: first node in the p-node stencil
            // M_p(u - n) is nonzero when 0 < u - n < p, i.e., (u - p) < n < u
            // The first contributing node is ceil(u - p + epsilon) = floor(u) - p + 1
            base_idx[d] = u.floor() as i32 - p as i32 + 1;

            // Compute weights: W_k = M_p(u - (base_idx + k))
            // The argument (u - node_index) should be in (0, p) for M_p's support
            for k in 0..p {
                let node_pos = (base_idx[d] + k as i32) as f64;
                let arg = u - node_pos;
                coeffs[d].push(bspline(p, arg));
                derivs[d].push(bspline_deriv(p, arg));
            }
        }

        Self {
            order,
            base_idx,
            coeffs,
            derivs,
        }
    }

    /// Get the mesh index for stencil position (ix, iy, iz) with periodic wrapping
    ///
    /// # Arguments
    /// * `ix, iy, iz` - Stencil indices (0 to order-1)
    /// * `mesh_dims` - Mesh dimensions for periodic wrapping
    #[must_use]
    pub fn mesh_index(&self, ix: usize, iy: usize, iz: usize, mesh_dims: [usize; 3]) -> [usize; 3] {
        [
            ((self.base_idx[0] + ix as i32).rem_euclid(mesh_dims[0] as i32)) as usize,
            ((self.base_idx[1] + iy as i32).rem_euclid(mesh_dims[1] as i32)) as usize,
            ((self.base_idx[2] + iz as i32).rem_euclid(mesh_dims[2] as i32)) as usize,
        ]
    }

    /// Get the weight for stencil position (ix, iy, iz)
    ///
    /// This is the product of 1D B-spline values: w = `W_x` × `W_y` × `W_z`
    #[must_use]
    pub fn weight(&self, ix: usize, iy: usize, iz: usize) -> f64 {
        self.coeffs[0][ix] * self.coeffs[1][iy] * self.coeffs[2][iz]
    }

    /// Get the gradient weights for stencil position (ix, iy, iz)
    ///
    /// Returns [dw/dx, dw/dy, dw/dz] for force interpolation.
    /// The derivatives need to be scaled by `mesh_dims/box_dims`.
    #[must_use]
    pub fn gradient_weights(&self, ix: usize, iy: usize, iz: usize) -> [f64; 3] {
        [
            self.derivs[0][ix] * self.coeffs[1][iy] * self.coeffs[2][iz],
            self.coeffs[0][ix] * self.derivs[1][iy] * self.coeffs[2][iz],
            self.coeffs[0][ix] * self.coeffs[1][iy] * self.derivs[2][iz],
        ]
    }

    /// Sum of all weights (should be 1.0 for correctly computed coefficients)
    #[must_use]
    pub fn weight_sum(&self) -> f64 {
        let mut sum = 0.0;
        for ix in 0..self.order {
            for iy in 0..self.order {
                for iz in 0..self.order {
                    sum += self.weight(ix, iy, iz);
                }
            }
        }
        sum
    }

    /// Sum of 1D weights (should each be 1.0)
    #[must_use]
    pub fn weight_sum_1d(&self) -> [f64; 3] {
        [
            self.coeffs[0].iter().sum(),
            self.coeffs[1].iter().sum(),
            self.coeffs[2].iter().sum(),
        ]
    }
}

/// Influence function G(k) for PPPM
///
/// The influence function corrects for the finite mesh and interpolation
/// scheme to achieve accurate forces.
///
/// G(k) = 4π/k² × [reference charge / actual charge]²
///
/// where the ratio accounts for B-spline aliasing.
#[must_use]
pub fn influence_function(
    kx: f64,
    ky: f64,
    kz: f64,
    order: usize,
    box_dims: [f64; 3],
    mesh_dims: [usize; 3],
) -> f64 {
    let k_sq = kz.mul_add(kz, kx.mul_add(kx, ky * ky));

    // Handle k = 0 (no contribution)
    if k_sq < 1e-14 {
        return 0.0;
    }

    // Mesh spacing
    let hx = box_dims[0] / mesh_dims[0] as f64;
    let hy = box_dims[1] / mesh_dims[1] as f64;
    let hz = box_dims[2] / mesh_dims[2] as f64;

    // B-spline Fourier transform squared
    let bx = bspline_ft_squared(kx * hx / (2.0 * PI), order);
    let by = bspline_ft_squared(ky * hy / (2.0 * PI), order);
    let bz = bspline_ft_squared(kz * hz / (2.0 * PI), order);

    // Aliasing sum correction
    // Sum over periodic images: Σ_m |M̃(k + 2πm/h)|²
    // For most k, only m=0 contributes significantly
    let denominator = bx * by * bz;

    if denominator < 1e-14 {
        return 0.0;
    }

    // G(k) = 4π/k² × 1/|M̃(k)|²
    4.0 * PI / k_sq / denominator
}

/// B-spline Fourier transform magnitude squared
///
/// |`M̃_p(ξ)|²` = (sin(πξ)/(πξ))^(2p) for ξ ≠ 0
///           = 1 for ξ = 0
fn bspline_ft_squared(xi: f64, order: usize) -> f64 {
    if xi.abs() < 1e-10 {
        return 1.0;
    }

    let sinc = (PI * xi).sin() / (PI * xi);
    sinc.powi(2 * order as i32)
}

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

    #[test]
    fn test_bspline_order_1() {
        // Box function
        assert!((bspline(1, 0.5) - 1.0).abs() < 1e-10);
        assert!(bspline(1, -0.1).abs() < 1e-10);
        assert!(bspline(1, 1.1).abs() < 1e-10);
    }

    #[test]
    fn test_bspline_order_2() {
        // Linear (tent) function, peak at x=1
        assert!((bspline(2, 1.0) - 1.0).abs() < 1e-10); // Peak at center
        assert!(bspline(2, 0.0).abs() < 1e-10);
        assert!(bspline(2, 2.0).abs() < 1e-10);
        // Linear interpolation
        assert!((bspline(2, 0.5) - 0.5).abs() < 1e-10);
        assert!((bspline(2, 1.5) - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_bspline_order_4() {
        // Cubic B-spline should be symmetric around center (x=2)
        let mid = 2.0;
        let v1 = bspline(4, mid - 0.5);
        let v2 = bspline(4, mid + 0.5);
        assert!((v1 - v2).abs() < 1e-10);

        // Peak at center
        assert!(bspline(4, mid) > bspline(4, mid + 0.5));
    }

    #[test]
    fn test_bspline_partition_of_unity() {
        // Sum of B-splines over integer shifts should equal 1
        for order in 2..=6 {
            let x = 2.37; // Arbitrary point
            let mut sum = 0.0;
            for n in -5..15 {
                sum += bspline(order, x - n as f64);
            }
            assert!(
                (sum - 1.0).abs() < 1e-10,
                "Partition of unity failed for order {order}: sum = {sum}"
            );
        }
    }

    #[test]
    fn test_bspline_coeffs_sum_to_one() {
        // Test that 1D weights sum to 1 for various positions
        for order in 2..=6 {
            for i in 0..10 {
                let pos = [
                    (i as f64).mul_add(0.73, 0.1),
                    (i as f64).mul_add(0.91, 0.2),
                    (i as f64).mul_add(0.67, 0.3),
                ];
                let mesh_dims = [16, 16, 16];
                let box_dims = [10.0, 10.0, 10.0];

                let coeffs = BsplineCoeffs::compute(order, pos, mesh_dims, box_dims);
                let sums = coeffs.weight_sum_1d();

                for (d, s) in sums.iter().enumerate() {
                    assert!(
                        (s - 1.0).abs() < 1e-10,
                        "Order {order} dim {d} at {pos:?}: 1D sum = {s} (expected 1)"
                    );
                }
            }
        }
    }

    #[test]
    fn test_bspline_coeffs_3d_sum() {
        let pos = [5.0, 5.0, 5.0];
        let mesh_dims = [16, 16, 16];
        let box_dims = [10.0, 10.0, 10.0];

        for order in 2..=6 {
            let coeffs = BsplineCoeffs::compute(order, pos, mesh_dims, box_dims);
            let sum = coeffs.weight_sum();

            assert!(
                (sum - 1.0).abs() < 1e-10,
                "Order {order}: 3D sum = {sum} (expected 1)"
            );
        }
    }

    #[test]
    fn test_influence_function() {
        let order = 4;
        let box_dims = [10.0, 10.0, 10.0];
        let mesh_dims = [16, 16, 16];

        // k = 0 should give 0
        assert!(influence_function(0.0, 0.0, 0.0, order, box_dims, mesh_dims).abs() < 1e-10);

        // Non-zero k should give positive value
        let g = influence_function(1.0, 0.0, 0.0, order, box_dims, mesh_dims);
        assert!(g > 0.0);

        // Should decrease with increasing k (roughly as 1/k²)
        let g1 = influence_function(1.0, 0.0, 0.0, order, box_dims, mesh_dims);
        let g2 = influence_function(2.0, 0.0, 0.0, order, box_dims, mesh_dims);
        assert!(g2 < g1);
    }
}