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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Split Velocity-Verlet Integrator (f64)
//!
//! **Algorithm**: Kick-drift-kick pattern for flexible thermostating
//! **Precision**: Full f64 via `math_f64.wgsl` preamble
//! **Reference**: Standard in LAMMPS, GROMACS
//!
//! **Advantages over monolithic VV**:
//! - Thermostat can be applied between kicks
//! - Force kernel can be swapped without touching integrator
//! - Explicit PBC wrapping during drift
//!
//! **Deep Debt Compliance**:
//! - ✅ Pure WGSL shader (`velocity_verlet_split.wgsl`, `vv_half_kick_f64.wgsl`)
//! - ✅ Zero unsafe code
//! - ✅ f64 precision
use crate::device::capabilities::WORKGROUP_SIZE_COMPACT;
use crate::error::{BarracudaError, Result};
use crate::tensor::Tensor;
/// Split Velocity-Verlet Step 1: Half-kick + drift + PBC wrap
///
/// Updates velocities by half-step and positions by full step.
/// PBC wrapping applied during drift to keep positions in [0, box).
pub struct VelocityVerletKickDrift {
positions: Tensor,
velocities: Tensor,
forces: Tensor,
n_particles: usize,
dt: f64,
mass: f64,
box_size: [f64; 3],
}
impl VelocityVerletKickDrift {
/// Create a new kick-drift operation
/// # Arguments
/// * `positions` - Position tensor [N, 3] (f64)
/// * `velocities` - Velocity tensor [N, 3] (f64)
/// * `forces` - Force tensor [N, 3] (f64)
/// * `dt` - Timestep (reduced units)
/// * `mass` - Particle mass (3.0 in OCP reduced units)
/// * `box_size` - Simulation box dimensions [Lx, Ly, Lz]
/// # Errors
/// Returns error if tensor shapes don't match.
pub fn new(
positions: Tensor,
velocities: Tensor,
forces: Tensor,
dt: f64,
mass: f64,
box_size: [f64; 3],
) -> Result<Self> {
let pos_shape = positions.shape();
if pos_shape.len() != 2 || pos_shape[1] != 3 {
return Err(BarracudaError::InvalidShape {
expected: vec![0, 3],
actual: pos_shape.to_vec(),
});
}
let n_particles = pos_shape[0];
// Validate matching shapes
if velocities.shape() != pos_shape || forces.shape() != pos_shape {
return Err(BarracudaError::InvalidShape {
expected: pos_shape.to_vec(),
actual: velocities.shape().to_vec(),
});
}
if dt <= 0.0 {
return Err(BarracudaError::Device(
"Timestep dt must be positive".to_string(),
));
}
Ok(Self {
positions,
velocities,
forces,
n_particles,
dt,
mass,
box_size,
})
}
/// Execute the kick-drift step (in-place update)
/// # Returns
/// (positions, velocities) after half-kick and drift
///
/// # Errors
///
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn execute(self) -> Result<(Tensor, Tensor)> {
let device = self.positions.device();
// Params: [n, dt, mass, _, box_x, box_y, box_z, _]
let params: Vec<f64> = vec![
self.n_particles as f64,
self.dt,
self.mass,
0.0,
self.box_size[0],
self.box_size[1],
self.box_size[2],
0.0,
];
let params_bytes: &[u8] = bytemuck::cast_slice(¶ms);
let params_buffer = device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("VV KickDrift Params"),
contents: params_bytes,
usage: wgpu::BufferUsages::STORAGE,
});
let shader_body = include_str!("velocity_verlet_split.wgsl");
let shader = device.compile_shader_f64(shader_body, Some("VV KickDrift Shader"));
let bind_group_layout =
device
.device
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("VV KickDrift BGL"),
entries: &[
// Positions (read-write)
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
// Velocities (read-write)
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
// Forces (read)
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
// Params (read)
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
});
let pipeline_layout =
device
.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("VV KickDrift PL"),
bind_group_layouts: &[&bind_group_layout],
immediate_size: 0,
});
let pipeline = device
.device
.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some("VV KickDrift Pipeline"),
layout: Some(&pipeline_layout),
module: &shader,
entry_point: Some("main"),
cache: None,
compilation_options: Default::default(),
});
let bind_group = device.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("VV KickDrift BG"),
layout: &bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.positions.buffer().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.velocities.buffer().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: self.forces.buffer().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 3,
resource: params_buffer.as_entire_binding(),
},
],
});
let mut encoder = device.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("VV KickDrift Encoder"),
});
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("VV KickDrift Pass"),
timestamp_writes: None,
});
pass.set_pipeline(&pipeline);
pass.set_bind_group(0, Some(&bind_group), &[]);
let workgroups = (self.n_particles as u32).div_ceil(WORKGROUP_SIZE_COMPACT);
pass.dispatch_workgroups(workgroups, 1, 1);
}
device.submit_commands(Some(encoder.finish()));
Ok((self.positions, self.velocities))
}
}
/// Split Velocity-Verlet Step 3: Second half-kick
///
/// Completes the velocity update using NEW forces (after drift).
pub struct VelocityVerletHalfKick {
velocities: Tensor,
forces_new: Tensor,
n_particles: usize,
dt: f64,
mass: f64,
}
impl VelocityVerletHalfKick {
/// Create a new half-kick operation
/// # Arguments
/// * `velocities` - Velocity tensor [N, 3] (f64) — after kick-drift
/// * `forces_new` - Force tensor [N, 3] (f64) — recomputed after drift
/// * `dt` - Timestep (reduced units)
/// * `mass` - Particle mass (3.0 in OCP reduced units)
/// # Errors
/// Returns error if tensor shapes don't match.
pub fn new(velocities: Tensor, forces_new: Tensor, dt: f64, mass: f64) -> Result<Self> {
let vel_shape = velocities.shape();
if vel_shape.len() != 2 || vel_shape[1] != 3 {
return Err(BarracudaError::InvalidShape {
expected: vec![0, 3],
actual: vel_shape.to_vec(),
});
}
let n_particles = vel_shape[0];
if forces_new.shape() != vel_shape {
return Err(BarracudaError::InvalidShape {
expected: vel_shape.to_vec(),
actual: forces_new.shape().to_vec(),
});
}
Ok(Self {
velocities,
forces_new,
n_particles,
dt,
mass,
})
}
/// Execute the second half-kick (in-place update)
/// # Returns
/// Velocities after full VV step
///
/// # Errors
///
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn execute(self) -> Result<Tensor> {
let device = self.velocities.device();
// Params: [n, dt, mass, _]
let params: Vec<f64> = vec![self.n_particles as f64, self.dt, self.mass, 0.0];
let params_bytes: &[u8] = bytemuck::cast_slice(¶ms);
let params_buffer = device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("VV HalfKick Params"),
contents: params_bytes,
usage: wgpu::BufferUsages::STORAGE,
});
let shader = device.compile_shader_f64(
include_str!("vv_half_kick_f64.wgsl"),
Some("VV HalfKick Shader"),
);
let bind_group_layout =
device
.device
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("VV HalfKick BGL"),
entries: &[
// Velocities (read-write)
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
// Forces (read)
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
// Params (read)
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
});
let pipeline_layout =
device
.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("VV HalfKick PL"),
bind_group_layouts: &[&bind_group_layout],
immediate_size: 0,
});
let pipeline = device
.device
.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some("VV HalfKick Pipeline"),
layout: Some(&pipeline_layout),
module: &shader,
entry_point: Some("main"),
cache: None,
compilation_options: Default::default(),
});
let bind_group = device.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("VV HalfKick BG"),
layout: &bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.velocities.buffer().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.forces_new.buffer().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: params_buffer.as_entire_binding(),
},
],
});
let mut encoder = device.create_encoder_guarded(&wgpu::CommandEncoderDescriptor {
label: Some("VV HalfKick Encoder"),
});
{
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("VV HalfKick Pass"),
timestamp_writes: None,
});
pass.set_pipeline(&pipeline);
pass.set_bind_group(0, Some(&bind_group), &[]);
let workgroups = (self.n_particles as u32).div_ceil(WORKGROUP_SIZE_COMPACT);
pass.dispatch_workgroups(workgroups, 1, 1);
}
device.submit_commands(Some(encoder.finish()));
Ok(self.velocities)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_split_vv_single_particle() {
let Some(device) = crate::device::test_pool::get_test_device_if_f64_gpu_available().await
else {
return;
};
// Check for f64 support
if !device
.device
.features()
.contains(wgpu::Features::SHADER_F64)
{
return;
}
// Single particle with constant force
let positions: Vec<f64> = vec![0.0, 0.0, 0.0];
let velocities: Vec<f64> = vec![1.0, 0.0, 0.0];
let forces: Vec<f64> = vec![6.0, 0.0, 0.0]; // F = 6 → a = 6/3 = 2
let dt = 0.1;
let mass = 3.0; // OCP reduced units
let box_size = [10.0, 10.0, 10.0];
// Create tensors
let pos_bytes: &[u8] = bytemuck::cast_slice(&positions);
let pos_buffer = device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Test Positions"),
contents: pos_bytes,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
});
let pos_tensor = Tensor::from_buffer(pos_buffer, vec![1, 3], device.clone());
let vel_bytes: &[u8] = bytemuck::cast_slice(&velocities);
let vel_buffer = device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Test Velocities"),
contents: vel_bytes,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
});
let vel_tensor = Tensor::from_buffer(vel_buffer, vec![1, 3], device.clone());
let force_bytes: &[u8] = bytemuck::cast_slice(&forces);
let force_buffer = device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Test Forces"),
contents: force_bytes,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
});
let force_tensor = Tensor::from_buffer(force_buffer, vec![1, 3], device.clone());
// Step 1: Kick-drift
let kick_drift =
VelocityVerletKickDrift::new(pos_tensor, vel_tensor, force_tensor, dt, mass, box_size)
.unwrap();
let (_pos_after, vel_after) = kick_drift.execute().unwrap();
// Step 2 would be force recomputation (skipped here)
// Step 3: Second half-kick (using same forces as approximation)
let force_bytes2: &[u8] = bytemuck::cast_slice(&forces);
let force_buffer2 = device
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Test Forces 2"),
contents: force_bytes2,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
});
let force_tensor2 = Tensor::from_buffer(force_buffer2, vec![1, 3], device);
let half_kick = VelocityVerletHalfKick::new(vel_after, force_tensor2, dt, mass).unwrap();
let _vel_final = half_kick.execute().unwrap();
}
}