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
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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! GPU-accelerated PPPM/Ewald electrostatics (universal via WGSL)
//!
//! This module provides hardware-agnostic PPPM using WGSL shaders that run
//! on any GPU/NPU/CPU via wgpu. All math is f64 precision.
//!
//! # Refactoring (Feb 2026)
//! - Shader sources in `shaders.rs`
//! - Pipeline creation in `pipelines.rs`
//! - Main logic in this file

mod kspace;
mod kspace_gpu;
pub(crate) mod pipelines;
mod shaders;

use std::sync::Arc;

use crate::device::WgpuDevice;
use crate::device::capabilities::WORKGROUP_SIZE_COMPACT;
use crate::error::{BarracudaError, Result};
use crate::linalg::sparse::SparseBuffers;

use super::{GreensFunction, PppmParams};

use pipelines::{PppmBindGroupLayouts, PppmPipelines};

/// GPU-accelerated PPPM solver
pub struct PppmGpu {
    device: wgpu::Device,
    queue: wgpu::Queue,
    wgpu_device: Arc<WgpuDevice>,
    params: PppmParams,
    greens: GreensFunction,
    pipelines: PppmPipelines,
    layouts: PppmBindGroupLayouts,
}

impl PppmGpu {
    /// Create from a `WgpuDevice` (preferred - driver-aware f64 compilation).
    /// # Errors
    /// Returns [`Err`] if shader compilation fails or the device is lost.
    pub async fn from_device(wgpu_device: &Arc<WgpuDevice>, params: PppmParams) -> Result<Self> {
        let greens = GreensFunction::new(&params);
        let bspline_module = wgpu_device.compile_shader_f64(shaders::BSPLINE, Some("pppm_bspline"));
        let charge_spread_module =
            wgpu_device.compile_shader_f64(shaders::CHARGE_SPREAD, Some("pppm_charge_spread"));
        let greens_apply_module =
            wgpu_device.compile_shader_f64(shaders::GREENS_APPLY, Some("pppm_greens_apply"));
        let force_interp_module =
            wgpu_device.compile_shader_f64(shaders::FORCE_INTERP, Some("pppm_force_interp"));
        let erfc_forces_module =
            wgpu_device.compile_shader_f64(shaders::ERFC_FORCES, Some("pppm_erfc_forces"));

        Self::build_from_modules(
            wgpu_device.device_clone(),
            wgpu_device.queue_clone(),
            Arc::clone(wgpu_device),
            params,
            greens,
            bspline_module,
            charge_spread_module,
            greens_apply_module,
            force_interp_module,
            erfc_forces_module,
        )
        .await
    }

    async fn build_from_modules(
        device: wgpu::Device,
        queue: wgpu::Queue,
        wgpu_device: Arc<WgpuDevice>,
        params: PppmParams,
        greens: GreensFunction,
        bspline_module: wgpu::ShaderModule,
        charge_spread_module: wgpu::ShaderModule,
        greens_apply_module: wgpu::ShaderModule,
        force_interp_module: wgpu::ShaderModule,
        erfc_forces_module: wgpu::ShaderModule,
    ) -> Result<Self> {
        let layouts = PppmBindGroupLayouts::new(&device);
        let pipelines = PppmPipelines::new(
            &device,
            &layouts,
            &bspline_module,
            &charge_spread_module,
            &greens_apply_module,
            &force_interp_module,
            &erfc_forces_module,
        );
        Ok(Self {
            device,
            queue,
            wgpu_device,
            params,
            greens,
            pipelines,
            layouts,
        })
    }

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

    /// Get the Greens function for PPPM.
    #[must_use]
    pub fn greens(&self) -> &GreensFunction {
        &self.greens
    }

    pub(crate) fn device(&self) -> &wgpu::Device {
        &self.device
    }

    pub(crate) fn queue(&self) -> &wgpu::Queue {
        &self.queue
    }

    pub(crate) fn wgpu_device(&self) -> &Arc<WgpuDevice> {
        &self.wgpu_device
    }

    pub(crate) fn layouts(&self) -> &PppmBindGroupLayouts {
        &self.layouts
    }

    pub(crate) fn pipelines(&self) -> &PppmPipelines {
        &self.pipelines
    }

    /// Compute forces and total energy (short-range erfc only, no k-space).
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
    /// readback fails (e.g. device lost or out of memory).
    pub async fn compute(&self, positions: &[f64], charges: &[f64]) -> Result<(Vec<f64>, f64)> {
        let n = charges.len();
        if positions.len() != n * 3 {
            return Err(BarracudaError::InvalidInput {
                message: format!(
                    "positions length {} != charges length {} * 3",
                    positions.len(),
                    n
                ),
            });
        }
        let order = self.params.interpolation_order;
        let [kx, ky, kz] = self.params.mesh_dims;
        let o3 = order * order * order;

        let positions_buffer =
            SparseBuffers::f64_from_slice_raw(&self.device, "positions", positions);
        let charges_buffer = SparseBuffers::f64_from_slice_raw(&self.device, "charges", charges);
        let coeffs_size = n * order * 3;
        let coeffs_buffer = SparseBuffers::f64_zeros_raw(&self.device, "coeffs", coeffs_size);
        let derivs_buffer = SparseBuffers::f64_zeros_raw(&self.device, "derivs", coeffs_size);
        let base_idx_buffer = SparseBuffers::i32_zeros_raw(&self.device, "base_idx", n * 3);
        let bspline_params: Vec<f64> = vec![
            n as f64,
            order as f64,
            kx as f64,
            ky as f64,
            kz as f64,
            self.params.box_dims[0],
            self.params.box_dims[1],
            self.params.box_dims[2],
        ];
        let bspline_params_buffer =
            SparseBuffers::f64_from_slice_raw(&self.device, "bspline_params", &bspline_params);
        let per_particle_mesh_buffer =
            SparseBuffers::f64_zeros_raw(&self.device, "per_particle_mesh", n * o3);
        let spread_params: Vec<f64> = vec![n as f64, order as f64, kx as f64, ky as f64, kz as f64];
        let spread_params_buffer =
            SparseBuffers::f64_from_slice_raw(&self.device, "spread_params", &spread_params);
        let forces_buffer = SparseBuffers::f64_zeros_raw(&self.device, "forces", n * 3);
        let pe_buffer = SparseBuffers::f64_zeros_raw(&self.device, "pe", n);
        let erfc_params: Vec<f64> = vec![
            n as f64,
            self.params.alpha,
            self.params.real_cutoff * self.params.real_cutoff,
            self.params.box_dims[0],
            self.params.box_dims[1],
            self.params.box_dims[2],
            self.params.coulomb_constant,
        ];
        let erfc_params_buffer =
            SparseBuffers::f64_from_slice_raw(&self.device, "erfc_params", &erfc_params);

        let bspline_bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("bspline_bind_group"),
            layout: &self.layouts.bspline,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: positions_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: coeffs_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: derivs_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: base_idx_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: bspline_params_buffer.as_entire_binding(),
                },
            ],
        });
        let charge_spread_bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("charge_spread_bind_group"),
            layout: &self.layouts.charge_spread,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: charges_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: coeffs_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: base_idx_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: per_particle_mesh_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: spread_params_buffer.as_entire_binding(),
                },
            ],
        });
        let erfc_bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("erfc_bind_group"),
            layout: &self.layouts.erfc_forces,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: positions_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: charges_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: forces_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: pe_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: erfc_params_buffer.as_entire_binding(),
                },
            ],
        });

        let mut encoder =
            self.wgpu_device
                .create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
                    label: Some("PPPM Encoder"),
                });
        let particle_workgroups = (n as u32).div_ceil(WORKGROUP_SIZE_COMPACT);

        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("PPPM B-spline Pass"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&self.pipelines.bspline);
            pass.set_bind_group(0, Some(&bspline_bind_group), &[]);
            pass.dispatch_workgroups(particle_workgroups, 1, 1);
        }
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("PPPM Charge Spread Pass"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&self.pipelines.charge_spread);
            pass.set_bind_group(0, Some(&charge_spread_bind_group), &[]);
            pass.dispatch_workgroups(particle_workgroups, 1, 1);
        }
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("PPPM erfc Forces Pass"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&self.pipelines.erfc_forces);
            pass.set_bind_group(0, Some(&erfc_bind_group), &[]);
            pass.dispatch_workgroups(particle_workgroups, 1, 1);
        }
        {
            let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("PPPM Self Energy Pass"),
                timestamp_writes: None,
            });
            pass.set_pipeline(&self.pipelines.self_energy);
            pass.set_bind_group(0, Some(&erfc_bind_group), &[]);
            pass.dispatch_workgroups(particle_workgroups, 1, 1);
        }

        self.wgpu_device.submit_commands(Some(encoder.finish()));
        let forces =
            SparseBuffers::read_f64_raw(self.wgpu_device().as_ref(), &forces_buffer, n * 3)?;
        let pe_values = SparseBuffers::read_f64_raw(self.wgpu_device().as_ref(), &pe_buffer, n)?;
        let total_energy: f64 = pe_values.iter().sum();
        Ok((forces, total_energy))
    }

    /// Compute electrostatic forces only (short-range).
    /// # Errors
    /// Returns [`Err`] if [`compute`](PppmGpu::compute) fails.
    pub async fn compute_forces(&self, positions: &[f64], charges: &[f64]) -> Result<Vec<f64>> {
        let (forces, _) = self.compute(positions, charges).await?;
        Ok(forces)
    }

    /// Compute forces and energy with k-space (full PPPM).
    /// # Errors
    /// Returns [`Err`] if `positions.len() != charges.len() * 3`, or if GPU buffer readback fails.
    pub async fn compute_with_kspace(
        &self,
        positions: &[f64],
        charges: &[f64],
    ) -> Result<(Vec<f64>, f64)> {
        kspace::compute_with_kspace(self, positions, charges).await
    }

    /// Compute forces and energy with GPU-accelerated k-space.
    /// # Errors
    /// Returns [`Err`] if `positions.len() != charges.len() * 3`, mesh dimensions are not
    /// power-of-two, or if GPU buffer readback or FFT fails.
    pub async fn compute_with_kspace_gpu(
        &self,
        positions: &[f64],
        charges: &[f64],
    ) -> Result<(Vec<f64>, f64)> {
        kspace_gpu::compute_with_kspace_gpu(self, positions, charges).await
    }
}

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

    #[test]
    fn test_pppm_gpu_params() {
        let params = PppmParams::custom(100, [10.0, 10.0, 10.0], [16, 16, 16], 1.0, 3.0, 4);
        assert_eq!(params.mesh_dims, [16, 16, 16]);
        assert_eq!(params.alpha, 1.0);
    }

    #[tokio::test]
    async fn test_pppm_gpu_opposite_charges_energy() {
        use crate::device::test_pool::get_test_device_if_f64_gpu_available;
        let Some(device) = get_test_device_if_f64_gpu_available().await else {
            return;
        };
        let params = PppmParams::custom(2, [10.0, 10.0, 10.0], [8, 8, 8], 2.0, 3.0, 4);
        let pppm = PppmGpu::from_device(&device, params)
            .await
            .expect("Failed to create PppmGpu");
        let positions: Vec<f64> = vec![4.0, 5.0, 5.0, 6.0, 5.0, 5.0];
        let charges: Vec<f64> = vec![1.0, -1.0];
        let (_forces, energy) = pppm
            .compute_with_kspace(&positions, &charges)
            .await
            .unwrap();
        assert!(
            energy < 0.0,
            "Opposite charges should have negative energy, got {energy}"
        );
    }

    #[tokio::test]
    async fn test_pppm_gpu_newtons_third_law() {
        use crate::device::test_pool::get_test_device_if_f64_gpu_available;
        let Some(device) = get_test_device_if_f64_gpu_available().await else {
            return;
        };
        let params = PppmParams::custom(2, [10.0, 10.0, 10.0], [8, 8, 8], 2.0, 3.0, 4);
        let pppm = PppmGpu::from_device(&device, params)
            .await
            .expect("Failed to create PppmGpu");
        let positions: Vec<f64> = vec![4.0, 5.0, 5.0, 6.0, 5.0, 5.0];
        let charges: Vec<f64> = vec![1.0, -1.0];
        let (forces, _energy) = pppm
            .compute_with_kspace(&positions, &charges)
            .await
            .unwrap();
        let fx_sum = forces[0] + forces[3];
        let fy_sum = forces[1] + forces[4];
        let fz_sum = forces[2] + forces[5];
        let f1_mag = forces[2]
            .mul_add(forces[2], forces[1].mul_add(forces[1], forces[0].powi(2)))
            .sqrt();
        let relative_error = if f1_mag > 1e-14 {
            fz_sum
                .mul_add(fz_sum, fy_sum.mul_add(fy_sum, fx_sum.powi(2)))
                .sqrt()
                / f1_mag
        } else {
            0.0
        };
        assert!(
            relative_error < 1e-3,
            "Newton's 3rd law violation: |F1+F2|/|F1| = {relative_error}"
        );
    }

    #[tokio::test]
    async fn test_pppm_gpu_like_charges_repel() {
        use crate::device::test_pool::get_test_device_if_f64_gpu_available;
        let Some(device) = get_test_device_if_f64_gpu_available().await else {
            return;
        };
        let params = PppmParams::custom(2, [10.0, 10.0, 10.0], [8, 8, 8], 2.0, 3.0, 4);
        let pppm = PppmGpu::from_device(&device, params)
            .await
            .expect("Failed to create PppmGpu");
        let positions: Vec<f64> = vec![4.0, 5.0, 5.0, 6.0, 5.0, 5.0];
        let charges: Vec<f64> = vec![1.0, 1.0];
        let (forces, _energy) = pppm
            .compute_with_kspace(&positions, &charges)
            .await
            .unwrap();
        assert!(
            forces[0] < 0.0,
            "Like charges should repel: F0_x should be negative, got {}",
            forces[0]
        );
        assert!(
            forces[3] > 0.0,
            "Like charges should repel: F1_x should be positive, got {}",
            forces[3]
        );
    }

    #[tokio::test]
    #[ignore = "W-002: diagnostic - compare CPU vs GPU B-spline coeffs"]
    async fn test_pppm_gpu_bspline_vs_cpu() {
        use crate::device::test_pool::get_test_device_if_f64_gpu_available;
        use crate::linalg::sparse::SparseBuffers;
        use crate::ops::md::electrostatics::bspline::BsplineCoeffs;

        let Some(device) = get_test_device_if_f64_gpu_available().await else {
            return;
        };
        let params = PppmParams::custom(2, [10.0, 10.0, 10.0], [8, 8, 8], 2.0, 3.0, 4);
        let positions = vec![4.0, 5.0, 5.0, 6.0, 5.0, 5.0];
        let box_dims = params.box_dims;
        let mesh_dims = params.mesh_dims;
        let order = params.interpolation_order;

        let wrap_one = |x: f64, l: f64| (x / l).floor().mul_add(-l, x);
        let pos0 = [
            wrap_one(positions[0], box_dims[0]),
            wrap_one(positions[1], box_dims[1]),
            wrap_one(positions[2], box_dims[2]),
        ];
        let pos1 = [
            wrap_one(positions[3], box_dims[0]),
            wrap_one(positions[4], box_dims[1]),
            wrap_one(positions[5], box_dims[2]),
        ];

        let cpu_coeffs0 = BsplineCoeffs::compute(order, pos0, mesh_dims, box_dims);
        let cpu_coeffs1 = BsplineCoeffs::compute(order, pos1, mesh_dims, box_dims);

        let pppm = PppmGpu::from_device(&device, params)
            .await
            .expect("GPU PPPM create failed");
        let (device_ref, queue, wgpu_dev) = (pppm.device(), pppm.queue(), pppm.wgpu_device());
        let positions_buffer = SparseBuffers::f64_from_slice_raw(device_ref, "pos", &positions);
        let coeffs_buffer = SparseBuffers::f64_zeros_raw(device_ref, "coeffs", 2 * order * 3);
        let derivs_buffer = SparseBuffers::f64_zeros_raw(device_ref, "derivs", 2 * order * 3);
        let base_idx_buffer = SparseBuffers::i32_zeros_raw(device_ref, "base", 6);
        let bspline_params: Vec<f64> = vec![
            2.0,
            order as f64,
            8.0,
            8.0,
            8.0,
            box_dims[0],
            box_dims[1],
            box_dims[2],
        ];
        let params_buffer = SparseBuffers::f64_from_slice_raw(device_ref, "bp", &bspline_params);
        let layout = &pppm.layouts().bspline;
        let bg = device_ref.create_bind_group(&wgpu::BindGroupDescriptor {
            label: None,
            layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: positions_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: coeffs_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: derivs_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 3,
                    resource: base_idx_buffer.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 4,
                    resource: params_buffer.as_entire_binding(),
                },
            ],
        });
        let mut enc = wgpu_dev.create_encoder_guarded(&Default::default());
        {
            let mut pass = enc.begin_compute_pass(&Default::default());
            pass.set_pipeline(&pppm.pipelines().bspline);
            pass.set_bind_group(0, Some(&bg), &[]);
            pass.dispatch_workgroups(1, 1, 1);
        }
        queue.submit(Some(enc.finish()));

        let gpu_coeffs =
            SparseBuffers::read_f64_raw(wgpu_dev.as_ref(), &coeffs_buffer, 2 * order * 3).unwrap();
        let gpu_derivs =
            SparseBuffers::read_f64_raw(wgpu_dev.as_ref(), &derivs_buffer, 2 * order * 3).unwrap();
        let gpu_base = SparseBuffers::read_i32_raw(wgpu_dev.as_ref(), &base_idx_buffer, 6).unwrap();

        for d in 0..3 {
            for k in 0..order {
                let cpu_c = cpu_coeffs0.coeffs[d][k];
                let gpu_c = gpu_coeffs[d * order + k];
                assert!(
                    (cpu_c - gpu_c).abs() < 1e-10,
                    "coeff0 d{d} k{k}: CPU {cpu_c} GPU {gpu_c}"
                );
                let cpu_d = cpu_coeffs0.derivs[d][k];
                let gpu_d = gpu_derivs[d * order + k];
                assert!(
                    (cpu_d - gpu_d).abs() < 1e-10,
                    "deriv0 d{d} k{k}: CPU {cpu_d} GPU {gpu_d}"
                );
            }
            assert_eq!(cpu_coeffs0.base_idx[d], gpu_base[d], "base0 d{d}");
        }
        for d in 0..3 {
            for k in 0..order {
                let cpu_c = cpu_coeffs1.coeffs[d][k];
                let gpu_c = gpu_coeffs[3 * order + d * order + k];
                assert!(
                    (cpu_c - gpu_c).abs() < 1e-10,
                    "coeff1 d{d} k{k}: CPU {cpu_c} GPU {gpu_c}"
                );
            }
        }
    }

    #[tokio::test]
    #[ignore = "W-002: diagnostic - erfc short-range only"]
    async fn test_pppm_gpu_erfc_only() {
        use crate::device::test_pool::get_test_device_if_f64_gpu_available;
        use crate::ops::md::electrostatics::compute_short_range;

        // Test erfc-only path (compute without kspace) vs CPU compute_short_range
        let Some(device) = get_test_device_if_f64_gpu_available().await else {
            return;
        };
        let params = PppmParams::custom(2, [10.0, 10.0, 10.0], [8, 8, 8], 2.0, 3.0, 4);
        let positions = vec![4.0, 5.0, 5.0, 6.0, 5.0, 5.0];
        let charges = vec![1.0, -1.0];
        let pos_3d: Vec<[f64; 3]> = positions.chunks(3).map(|c| [c[0], c[1], c[2]]).collect();

        let (cpu_short_forces, _) = compute_short_range(&pos_3d, &charges, &params);
        let pppm = PppmGpu::from_device(&device, params)
            .await
            .expect("GPU PPPM create failed");
        let (gpu_forces, _gpu_energy) = pppm.compute(&positions, &charges).await.unwrap();

        // Erfc forces should attract: F0_x > 0, F1_x < 0
        assert!(
            cpu_short_forces[0][0] > 0.0,
            "CPU erfc: +q should be pulled right"
        );
        assert!(
            cpu_short_forces[1][0] < 0.0,
            "CPU erfc: -q should be pulled left"
        );
        assert!(
            gpu_forces[0] > 0.0,
            "GPU erfc: +q should be pulled right, got {}",
            gpu_forces[0]
        );
        assert!(
            gpu_forces[3] < 0.0,
            "GPU erfc: -q should be pulled left, got {}",
            gpu_forces[3]
        );
    }

    #[tokio::test]
    #[ignore = "requires f64 GPU hardware"]
    async fn test_pppm_gpu_matches_cpu_reference() {
        use crate::device::test_pool::get_test_device_if_f64_gpu_available;
        use crate::ops::md::electrostatics::Pppm;

        let Some(device) = get_test_device_if_f64_gpu_available().await else {
            return;
        };

        let positions = vec![4.0, 5.0, 5.0, 6.0, 5.0, 5.0]; // flat [x,y,z,x,y,z]
        let charges = vec![1.0, -1.0];
        let params = PppmParams::custom(2, [10.0, 10.0, 10.0], [8, 8, 8], 2.0, 3.0, 4);

        // Reference (Pppm now uses GPU FFT)
        let pos_3d: Vec<[f64; 3]> = positions.chunks(3).map(|c| [c[0], c[1], c[2]]).collect();
        let cpu_pppm = Pppm::new(device.clone(), params.clone());
        let (cpu_forces, cpu_energy) = cpu_pppm.compute(&pos_3d, &charges).unwrap();

        // GPU version
        let pppm = PppmGpu::from_device(&device, params)
            .await
            .expect("GPU PPPM create failed");
        let (gpu_forces, gpu_energy) = pppm
            .compute_with_kspace(&positions, &charges)
            .await
            .unwrap();

        // Energy should match within 10% (mesh resolution limits accuracy)
        let energy_denom = cpu_energy.abs().max(1e-14);
        let energy_rel_error = ((gpu_energy - cpu_energy) / energy_denom).abs();
        assert!(
            energy_rel_error < 0.1,
            "GPU energy {gpu_energy} vs CPU energy {cpu_energy}"
        );

        // Force directions must match (sign check)
        let cpu_fx0 = cpu_forces[0][0];
        let gpu_fx0 = gpu_forces[0];
        assert!(
            cpu_fx0 * gpu_fx0 > 0.0,
            "Force direction mismatch: CPU {cpu_fx0} GPU {gpu_fx0}"
        );
    }
}