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
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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! PPPM Electrostatics Orchestration
//!
//! Combines all PPPM components into a complete long-range electrostatics solver.
//!
//! # Algorithm
//!
//! ```text
//! 1. Spread charges to mesh (CPU, B-spline interpolation)
//! 2. Forward FFT: ρ(r) → ρ̃(k)
//! 3. Apply Green's function: φ̃(k) = G(k) × ρ̃(k)
//! 4. Backward FFT: φ̃(k) → φ(r)
//! 5. Interpolate forces: F = -q∇φ (CPU, B-spline gradient)
//! 6. Add short-range: F += F_short (CPU, erfc-damped)
//! ```
//!
//! # Usage
//!
//! ```ignore
//! use barracuda::ops::md::electrostatics::{Pppm, PppmParams, PppmAccuracy};
//!
//! // Create PPPM solver
//! let params = PppmParams::auto(n_particles, box_side, PppmAccuracy::Medium);
//! let pppm = Pppm::new(params);
//!
//! // Compute forces and energy
//! let (forces, energy) = pppm.compute(&positions, &charges)?;
//! ```

use std::f64::consts::PI;
use std::sync::Arc;

use crate::device::WgpuDevice;
use crate::ops::fft::Fft3DF64;

use super::{
    ChargeMesh, GreensFunction, PotentialMesh, PppmParams, compute_short_range, dipole_correction,
    interpolate_forces, self_energy_correction, spread_charges_with_coeffs,
};

/// PPPM Electrostatics Solver
///
/// Provides efficient O(N log N) electrostatics for periodic systems.
/// Always uses GPU FFT for k-space solves.
#[derive(Clone)]
pub struct Pppm {
    /// PPPM configuration parameters
    params: PppmParams,

    /// Precomputed Green's function G(k)
    greens: GreensFunction,

    /// GPU device for FFT execution
    device: Arc<WgpuDevice>,
}

#[expect(
    dead_code,
    reason = "CPU reference path for GPU parity validation in tests"
)]
impl Pppm {
    /// Create a new PPPM solver with given parameters and GPU device
    #[must_use]
    pub fn new(device: Arc<WgpuDevice>, params: PppmParams) -> Self {
        let greens = GreensFunction::new(&params);
        Self {
            params,
            greens,
            device,
        }
    }

    /// Compute electrostatic forces and total energy
    ///
    /// # Arguments
    /// * `positions` - Particle positions [x, y, z]
    /// * `charges` - Particle charges
    ///
    /// # Returns
    /// (forces, `total_energy`) where forces[i] = [fx, fy, fz]
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
    /// readback fails (e.g. device lost or out of memory).
    pub fn compute(
        &self,
        positions: &[[f64; 3]],
        charges: &[f64],
    ) -> Result<(Vec<[f64; 3]>, f64), PppmError> {
        if positions.len() != charges.len() {
            return Err(PppmError::SizeMismatch {
                positions: positions.len(),
                charges: charges.len(),
            });
        }

        let n = positions.len();
        if n == 0 {
            return Ok((vec![], 0.0));
        }

        // 1. Spread charges to mesh (returns coefficients for force interpolation)
        let (charge_mesh, coeffs) = spread_charges_with_coeffs(positions, charges, &self.params);

        // 2. Forward FFT: ρ(r) → ρ̃(k)
        let rho_k = self.forward_fft(&charge_mesh)?;

        // 3. Apply Green's function: φ̃(k) = G(k) × ρ̃(k)
        let phi_k = self.greens.apply(&rho_k);

        // 4. Compute k-space energy
        let volume = self.params.box_dims[0] * self.params.box_dims[1] * self.params.box_dims[2];
        let e_kspace = self.greens.kspace_energy(&rho_k, volume);

        // 5. Backward FFT: φ̃(k) → φ(r)
        let potential_mesh = self.backward_fft(&phi_k)?;

        // 6. Interpolate k-space forces from mesh
        let kspace_forces = interpolate_forces(&potential_mesh, charges, &coeffs, &self.params);

        // 7. Compute short-range forces and energy
        let (short_forces, e_short) = compute_short_range(positions, charges, &self.params);

        // 8. Compute corrections
        let e_self =
            self_energy_correction(charges, self.params.alpha, self.params.coulomb_constant);
        let e_dipole = dipole_correction(
            positions,
            charges,
            self.params.box_dims,
            self.params.coulomb_constant,
        );

        // 9. Combine forces
        let mut forces = vec![[0.0, 0.0, 0.0]; n];
        for i in 0..n {
            forces[i][0] = kspace_forces[i][0] + short_forces[i][0];
            forces[i][1] = kspace_forces[i][1] + short_forces[i][1];
            forces[i][2] = kspace_forces[i][2] + short_forces[i][2];
        }

        // 10. Total energy
        let total_energy = e_kspace + e_short + e_self + e_dipole;

        Ok((forces, total_energy))
    }

    /// Compute forces only (slightly faster if energy not needed)
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
    /// readback fails (e.g. device lost or out of memory).
    pub fn compute_forces(
        &self,
        positions: &[[f64; 3]],
        charges: &[f64],
    ) -> Result<Vec<[f64; 3]>, PppmError> {
        let (forces, _) = self.compute(positions, charges)?;
        Ok(forces)
    }

    /// Get the PPPM parameters
    #[must_use]
    pub fn params(&self) -> &PppmParams {
        &self.params
    }

    /// Forward FFT: convert charge mesh to k-space (GPU)
    async fn forward_fft_async(&self, mesh: &ChargeMesh) -> Result<Vec<f64>, PppmError> {
        let [kx, ky, kz] = self.params.mesh_dims;

        if !kx.is_power_of_two() || !ky.is_power_of_two() || !kz.is_power_of_two() {
            return Err(PppmError::FftError(format!(
                "Mesh dimensions must be powers of 2 for GPU FFT, got ({kx}, {ky}, {kz})"
            )));
        }

        let size = kx * ky * kz;

        let mut complex = vec![0.0f64; size * 2];
        for i in 0..size {
            complex[i * 2] = mesh.values[i];
        }

        let fft = Fft3DF64::new(self.device.clone(), kx, ky, kz)
            .map_err(|e| PppmError::FftError(e.to_string()))?;

        fft.forward(&complex)
            .await
            .map_err(|e| PppmError::FftError(e.to_string()))
    }

    fn forward_fft(&self, mesh: &ChargeMesh) -> Result<Vec<f64>, PppmError> {
        crate::runtime::tokio_block_on(self.forward_fft_async(mesh))
    }

    async fn backward_fft_async(&self, phi_k: &[f64]) -> Result<PotentialMesh, PppmError> {
        let [kx, ky, kz] = self.params.mesh_dims;

        if !kx.is_power_of_two() || !ky.is_power_of_two() || !kz.is_power_of_two() {
            return Err(PppmError::FftError(format!(
                "Mesh dimensions must be powers of 2 for GPU FFT, got ({kx}, {ky}, {kz})"
            )));
        }

        let size = kx * ky * kz;

        let fft = Fft3DF64::new(self.device.clone(), kx, ky, kz)
            .map_err(|e| PppmError::FftError(e.to_string()))?;

        let mut complex = fft
            .inverse(phi_k)
            .await
            .map_err(|e| PppmError::FftError(e.to_string()))?;

        // Normalize for inverse FFT (Fft3DF64 doesn't normalize)
        let scale = 1.0 / (kx * ky * kz) as f64;
        for v in &mut complex {
            *v *= scale;
        }

        // Extract real part as potential
        let mut values = vec![0.0; size];
        for i in 0..size {
            values[i] = complex[i * 2];
        }

        Ok(PotentialMesh::from_values(
            self.params.mesh_dims,
            values,
            self.params.box_dims,
        ))
    }

    fn backward_fft(&self, phi_k: &[f64]) -> Result<PotentialMesh, PppmError> {
        crate::runtime::tokio_block_on(self.backward_fft_async(phi_k))
    }

    /// CPU 3D FFT using dimension-wise 1D FFTs (test only)
    fn fft_3d_cpu(
        &self,
        data: &mut [f64],
        nx: usize,
        ny: usize,
        nz: usize,
        inverse: bool,
    ) -> Result<(), PppmError> {
        // FFT along Z (innermost)
        for ix in 0..nx {
            for iy in 0..ny {
                let mut pencil = vec![0.0; nz * 2];
                for iz in 0..nz {
                    let idx = ((ix * ny + iy) * nz + iz) * 2;
                    pencil[iz * 2] = data[idx];
                    pencil[iz * 2 + 1] = data[idx + 1];
                }

                self.fft_1d_cpu(&mut pencil, nz, inverse);

                for iz in 0..nz {
                    let idx = ((ix * ny + iy) * nz + iz) * 2;
                    data[idx] = pencil[iz * 2];
                    data[idx + 1] = pencil[iz * 2 + 1];
                }
            }
        }

        // FFT along Y
        for ix in 0..nx {
            for iz in 0..nz {
                let mut pencil = vec![0.0; ny * 2];
                for iy in 0..ny {
                    let idx = ((ix * ny + iy) * nz + iz) * 2;
                    pencil[iy * 2] = data[idx];
                    pencil[iy * 2 + 1] = data[idx + 1];
                }

                self.fft_1d_cpu(&mut pencil, ny, inverse);

                for iy in 0..ny {
                    let idx = ((ix * ny + iy) * nz + iz) * 2;
                    data[idx] = pencil[iy * 2];
                    data[idx + 1] = pencil[iy * 2 + 1];
                }
            }
        }

        // FFT along X (outermost)
        for iy in 0..ny {
            for iz in 0..nz {
                let mut pencil = vec![0.0; nx * 2];
                for ix in 0..nx {
                    let idx = ((ix * ny + iy) * nz + iz) * 2;
                    pencil[ix * 2] = data[idx];
                    pencil[ix * 2 + 1] = data[idx + 1];
                }

                self.fft_1d_cpu(&mut pencil, nx, inverse);

                for ix in 0..nx {
                    let idx = ((ix * ny + iy) * nz + iz) * 2;
                    data[idx] = pencil[ix * 2];
                    data[idx + 1] = pencil[ix * 2 + 1];
                }
            }
        }

        // Normalize for inverse FFT
        if inverse {
            let scale = 1.0 / (nx * ny * nz) as f64;
            for v in data.iter_mut() {
                *v *= scale;
            }
        }

        Ok(())
    }

    /// CPU 1D FFT (Cooley-Tukey radix-2)
    fn fft_1d_cpu(&self, data: &mut [f64], n: usize, inverse: bool) {
        // Bit-reversal permutation
        let mut j = 0;
        for i in 0..n {
            if i < j {
                data.swap(i * 2, j * 2);
                data.swap(i * 2 + 1, j * 2 + 1);
            }
            let mut m = n >> 1;
            while m > 0 && j >= m {
                j -= m;
                m >>= 1;
            }
            j += m;
        }

        // Cooley-Tukey butterfly
        let sign = if inverse { 1.0 } else { -1.0 };
        let mut len = 2;
        while len <= n {
            let half = len / 2;
            let angle_step = sign * 2.0 * PI / len as f64;

            for start in (0..n).step_by(len) {
                let mut angle: f64 = 0.0;
                for k in 0..half {
                    let i = start + k;
                    let j = start + k + half;

                    // Twiddle factor
                    let tw_re = angle.cos();
                    let tw_im = angle.sin();

                    // Butterfly
                    let t_re = tw_re.mul_add(data[j * 2], -(tw_im * data[j * 2 + 1]));
                    let t_im = tw_re.mul_add(data[j * 2 + 1], tw_im * data[j * 2]);

                    data[j * 2] = data[i * 2] - t_re;
                    data[j * 2 + 1] = data[i * 2 + 1] - t_im;
                    data[i * 2] += t_re;
                    data[i * 2 + 1] += t_im;

                    angle += angle_step;
                }
            }
            len *= 2;
        }
    }
}

/// PPPM computation errors.
#[derive(Debug, Clone, thiserror::Error)]
pub enum PppmError {
    /// Position and charge array sizes don't match.
    #[error("Position count ({positions}) doesn't match charge count ({charges})")]
    SizeMismatch {
        /// Number of position entries.
        positions: usize,
        /// Number of charge entries.
        charges: usize,
    },
    /// FFT computation failed.
    #[error("FFT error: {0}")]
    FftError(String),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::device::test_pool::get_test_device_if_f64_gpu_available_sync;
    use crate::ops::md::electrostatics::PppmAccuracy;

    #[test]
    fn test_pppm_two_opposite_charges() {
        let Some(device) = get_test_device_if_f64_gpu_available_sync() else {
            return;
        };
        let params = PppmParams::custom(
            2,
            [10.0, 10.0, 10.0],
            [8, 8, 8],
            2.0, // alpha
            3.0, // rc
            4,   // order
        );

        let pppm = Pppm::new(device, params);

        // Two opposite charges
        let positions = vec![[4.0, 5.0, 5.0], [6.0, 5.0, 5.0]];
        let charges = vec![1.0, -1.0];

        let (forces, energy) = pppm.compute(&positions, &charges).unwrap();

        // Energy should be negative (attractive)
        assert!(energy < 0.0, "Energy should be negative: {energy}");

        // Forces should attract charges together
        // Positive charge at x=4 should be pulled toward x=6 (positive force)
        assert!(
            forces[0][0] > 0.0,
            "Force on +q should be positive x: {}",
            forces[0][0]
        );
        // Negative charge at x=6 should be pulled toward x=4 (negative force)
        assert!(
            forces[1][0] < 0.0,
            "Force on -q should be negative x: {}",
            forces[1][0]
        );

        // Newton's third law: forces should be equal and opposite
        let force_sum = forces[0][0] + forces[1][0];
        assert!(
            force_sum.abs() < 0.1,
            "Forces should sum to ~0: {force_sum}"
        );
    }

    #[test]
    fn test_pppm_two_like_charges() {
        let Some(device) = get_test_device_if_f64_gpu_available_sync() else {
            return;
        };
        let params = PppmParams::custom(2, [10.0, 10.0, 10.0], [8, 8, 8], 2.0, 3.0, 4);

        let pppm = Pppm::new(device, params);

        // Two positive charges
        let positions = vec![[4.0, 5.0, 5.0], [6.0, 5.0, 5.0]];
        let charges = vec![1.0, 1.0];

        let (forces, _energy) = pppm.compute(&positions, &charges).unwrap();

        // Note: Total energy includes negative self-energy correction (-α/√π × Σq²)
        // which can dominate for small systems. The important physical test is
        // that forces are repulsive.

        // Forces should repel: first charge pushed left, second pushed right
        assert!(
            forces[0][0] < 0.0,
            "First charge should be pushed left: {}",
            forces[0][0]
        );
        assert!(
            forces[1][0] > 0.0,
            "Second charge should be pushed right: {}",
            forces[1][0]
        );

        // Newton's third law
        let force_sum = forces[0][0] + forces[1][0];
        assert!(
            force_sum.abs() < 0.1,
            "Forces should sum to ~0: {force_sum}"
        );
    }

    #[test]
    fn test_pppm_neutral_system() {
        let Some(device) = get_test_device_if_f64_gpu_available_sync() else {
            return;
        };
        let params = PppmParams::auto(4, 10.0, PppmAccuracy::Medium);
        let pppm = Pppm::new(device, params);

        // Neutral system: +1 +1 -1 -1
        let positions = vec![
            [2.0, 5.0, 5.0],
            [4.0, 5.0, 5.0],
            [6.0, 5.0, 5.0],
            [8.0, 5.0, 5.0],
        ];
        let charges = vec![1.0, 1.0, -1.0, -1.0];

        let (forces, _energy) = pppm.compute(&positions, &charges).unwrap();

        // Total momentum should be conserved (sum of forces = 0)
        let total_fx: f64 = forces.iter().map(|f| f[0]).sum();
        let total_fy: f64 = forces.iter().map(|f| f[1]).sum();
        let total_fz: f64 = forces.iter().map(|f| f[2]).sum();

        assert!(total_fx.abs() < 0.1, "Total Fx should be ~0: {total_fx}");
        assert!(total_fy.abs() < 0.1, "Total Fy should be ~0: {total_fy}");
        assert!(total_fz.abs() < 0.1, "Total Fz should be ~0: {total_fz}");
    }

    #[test]
    fn test_pppm_empty_system() {
        let Some(device) = get_test_device_if_f64_gpu_available_sync() else {
            return;
        };
        let params = PppmParams::auto(0, 10.0, PppmAccuracy::Low);
        let pppm = Pppm::new(device, params);

        let (forces, energy) = pppm.compute(&[], &[]).unwrap();

        assert!(forces.is_empty());
        assert!((energy - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_pppm_size_mismatch() {
        let Some(device) = get_test_device_if_f64_gpu_available_sync() else {
            return;
        };
        let params = PppmParams::auto(2, 10.0, PppmAccuracy::Low);
        let pppm = Pppm::new(device, params);

        let result = pppm.compute(&[[0.0, 0.0, 0.0]], &[1.0, 2.0]);

        assert!(result.is_err());
        if let Err(PppmError::SizeMismatch { positions, charges }) = result {
            assert_eq!(positions, 1);
            assert_eq!(charges, 2);
        }
    }

    #[test]
    fn test_fft_1d_roundtrip() {
        let Some(device) = get_test_device_if_f64_gpu_available_sync() else {
            return;
        };
        let params = PppmParams::auto(10, 10.0, PppmAccuracy::Medium);
        let pppm = Pppm::new(device, params);

        // Simple complex signal
        let mut data = vec![1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 4.0, 0.0]; // n=4

        let original = data.clone();

        // Forward FFT
        pppm.fft_1d_cpu(&mut data, 4, false);

        // Inverse FFT
        pppm.fft_1d_cpu(&mut data, 4, true);

        // Normalize
        for v in &mut data {
            *v /= 4.0;
        }

        // Should match original
        for (a, b) in original.iter().zip(data.iter()) {
            assert!((a - b).abs() < 1e-10, "FFT roundtrip failed: {a} != {b}");
        }
    }
}