potentials 0.1.0

A lightweight Rust library for classical molecular dynamics potentials, providing modular force field components (LJ, bonds, angles, torsions) for major systems like DREIDING, AMBER, and GROMOS, with high-performance, branchless kernels in no-std scientific computing.
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
//! # Soft Sphere Potential
//!
//! A purely repulsive power-law potential commonly used for
//! soft matter simulations and as a WCA-style short-range repulsion.
//!
//! ## Formula
//!
//! ```text
//! V(r) = epsilon * (sigma / r)^n
//! ```
//!
//! where:
//! - `epsilon`: Energy scale (energy units)
//! - `sigma`: Length scale (length units)
//! - `N`: Repulsion exponent (compile-time constant)
//!
//! ## Force Factor
//!
//! ```text
//! S = -(dV/dr) / r = n * epsilon * sigma^n / r^(n+2)
//! ```
//!
//! ## Implementation Notes
//!
//! - Uses const generics for compile-time optimization of exponent
//! - Exponent computation is fully unrolled by the compiler
//! - Stores `coeff = epsilon * sigma^n` for efficient evaluation

use crate::base::Potential2;
use crate::math::Vector;

/// Soft sphere (power-law repulsion) potential.
///
/// ## Type Parameters
///
/// - `T`: Numeric type (f32, f64, or SIMD vector)
/// - `N`: Repulsion exponent (must be > 0)
///
/// ## Parameters
///
/// - `epsilon`: Energy scale (energy units)
/// - `sigma`: Length scale (length units)
///
/// ## Precomputed Values
///
/// - `coeff`: Stores `epsilon * sigma^n` for efficient evaluation
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Soft<T, const N: u32> {
    /// Precomputed coefficient: epsilon * sigma^n
    coeff: T,
}

impl<T: Vector, const N: u32> Soft<T, N> {
    /// Creates a new soft sphere potential.
    ///
    /// ## Arguments
    ///
    /// - `epsilon`: Energy scale (energy units)
    /// - `sigma`: Length scale (length units)
    ///
    /// ## Panics
    ///
    /// Panics if `N == 0` (compile-time enforced where possible).
    ///
    /// ## Example
    ///
    /// ```
    /// use potentials::pair::Soft;
    ///
    /// // Hard-sphere-like repulsion (n=12)
    /// let soft = Soft::<f64, 12>::new(1.0, 1.0);
    ///
    /// // Softer repulsion (n=6)
    /// let soft6 = Soft::<f64, 6>::new(1.0, 1.0);
    /// ```
    #[inline]
    pub fn new(epsilon: f64, sigma: f64) -> Self {
        assert!(N > 0, "Soft sphere exponent must be positive, got N={}", N);

        let sigma_n = int_pow(sigma, N);
        Self {
            coeff: T::splat(epsilon * sigma_n),
        }
    }

    /// Creates a soft sphere potential from a precomputed coefficient.
    ///
    /// ## Arguments
    ///
    /// - `coeff`: Precomputed coefficient (epsilon * sigma^n)
    ///
    /// This is useful when mixing rules produce the coefficient directly.
    #[inline]
    pub fn from_coefficient(coeff: f64) -> Self {
        Self {
            coeff: T::splat(coeff),
        }
    }

    /// Returns the coefficient (epsilon * sigma^n).
    #[inline]
    pub fn coeff(&self) -> T {
        self.coeff
    }

    /// Returns the exponent N.
    #[inline]
    pub const fn exponent(&self) -> u32 {
        N
    }
}

impl<T: Vector, const N: u32> Potential2<T> for Soft<T, N> {
    /// Computes the potential energy.
    ///
    /// ```text
    /// V(r) = coeff / r^n
    /// ```
    #[inline(always)]
    fn energy(&self, r_sq: T) -> T {
        let r_inv = r_sq.rsqrt();
        let r_neg_n = int_pow_vec::<T, N>(r_inv);
        self.coeff * r_neg_n
    }

    /// Computes the force factor.
    ///
    /// ```text
    /// dV/dr = -n * coeff / r^(n+1)
    /// S = -(dV/dr)/r = n * coeff / r^(n+2)
    /// ```
    #[inline(always)]
    fn force_factor(&self, r_sq: T) -> T {
        let r_inv = r_sq.rsqrt();
        let r_neg_n = int_pow_vec::<T, N>(r_inv);
        let r_sq_inv = r_sq.recip();
        let n = T::splat(N as f64);

        n * self.coeff * r_neg_n * r_sq_inv
    }

    /// Computes energy and force factor together (optimized).
    ///
    /// Shares the computation of r^-n.
    #[inline(always)]
    fn energy_force(&self, r_sq: T) -> (T, T) {
        let r_inv = r_sq.rsqrt();
        let r_neg_n = int_pow_vec::<T, N>(r_inv);
        let r_sq_inv = r_sq.recip();

        let coeff_rn = self.coeff * r_neg_n;
        let energy = coeff_rn;

        let n = T::splat(N as f64);
        let force = n * coeff_rn * r_sq_inv;

        (energy, force)
    }
}

/// Computes x^n using binary exponentiation for f64.
///
/// Used for precomputation of sigma^n in constructor.
#[inline]
fn int_pow(x: f64, n: u32) -> f64 {
    match n {
        0 => 1.0,
        1 => x,
        2 => x * x,
        3 => x * x * x,
        4 => {
            let x2 = x * x;
            x2 * x2
        }
        5 => {
            let x2 = x * x;
            x2 * x2 * x
        }
        6 => {
            let x2 = x * x;
            x2 * x2 * x2
        }
        7 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            x4 * x2 * x
        }
        8 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            x4 * x4
        }
        9 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            let x8 = x4 * x4;
            x8 * x
        }
        10 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            let x8 = x4 * x4;
            x8 * x2
        }
        11 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            let x8 = x4 * x4;
            x8 * x2 * x
        }
        12 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            x4 * x4 * x4
        }
        13 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            x4 * x4 * x4 * x
        }
        14 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            let x8 = x4 * x4;
            x8 * x4 * x2
        }
        _ => {
            // Binary exponentiation for large n
            let mut result = 1.0;
            let mut base = x;
            let mut exp = n;
            while exp > 0 {
                if exp & 1 == 1 {
                    result *= base;
                }
                base *= base;
                exp >>= 1;
            }
            result
        }
    }
}

/// Computes x^N for Vector types at compile time using const generics.
///
/// The compiler completely eliminates the match at compile time,
/// generating optimal code for each specific power.
#[inline(always)]
fn int_pow_vec<T: Vector, const N: u32>(x: T) -> T {
    match N {
        0 => T::splat(1.0),
        1 => x,
        2 => x * x,
        3 => x * x * x,
        4 => {
            let x2 = x * x;
            x2 * x2
        }
        5 => {
            let x2 = x * x;
            x2 * x2 * x
        }
        6 => {
            let x2 = x * x;
            x2 * x2 * x2
        }
        7 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            x4 * x2 * x
        }
        8 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            x4 * x4
        }
        9 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            let x8 = x4 * x4;
            x8 * x
        }
        10 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            let x8 = x4 * x4;
            x8 * x2
        }
        11 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            let x8 = x4 * x4;
            x8 * x2 * x
        }
        12 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            x4 * x4 * x4
        }
        13 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            x4 * x4 * x4 * x
        }
        14 => {
            let x2 = x * x;
            let x4 = x2 * x2;
            let x8 = x4 * x4;
            x8 * x4 * x2
        }
        _ => {
            // Binary exponentiation for large N
            let mut result = T::splat(1.0);
            let mut base = x;
            let mut exp = N;
            while exp > 0 {
                if exp & 1 == 1 {
                    result = result * base;
                }
                base = base * base;
                exp >>= 1;
            }
            result
        }
    }
}

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

    #[test]
    fn test_soft_at_sigma() {
        let epsilon_val = 2.0;
        let sigma = 1.5;
        let soft: Soft<f64, 12> = Soft::new(epsilon_val, sigma);

        let r_sq = sigma * sigma;
        let energy = soft.energy(r_sq);

        assert_relative_eq!(energy, epsilon_val, epsilon = 1e-10);
    }

    #[test]
    fn test_soft_repulsive() {
        let soft: Soft<f64, 12> = Soft::new(1.0, 1.0);

        for &r in &[0.5, 1.0, 2.0, 5.0] {
            let energy = soft.energy(r * r);
            assert!(energy > 0.0, "Energy at r={} should be positive", r);
        }
    }

    #[test]
    fn test_soft_decay() {
        let soft: Soft<f64, 12> = Soft::new(1.0, 1.0);

        let e1 = soft.energy(1.0);
        let e2 = soft.energy(4.0);

        assert!(e1 > e2, "Energy should decrease with distance");
    }

    #[test]
    fn test_soft_different_exponents() {
        let soft6: Soft<f64, 6> = Soft::new(1.0, 1.0);
        let soft12: Soft<f64, 12> = Soft::new(1.0, 1.0);

        let r_sq = 1.5 * 1.5;
        let e6 = soft6.energy(r_sq);
        let e12 = soft12.energy(r_sq);

        assert!(e6 > 0.0);
        assert!(e12 > 0.0);
        assert!(
            e12 < e6,
            "Higher exponent should decay faster for r > sigma"
        );
    }

    #[test]
    fn test_soft_energy_force_consistency() {
        let soft: Soft<f64, 9> = Soft::new(0.5, 2.0);
        let r_sq = 6.25;

        let (e1, f1) = soft.energy_force(r_sq);
        let e2 = soft.energy(r_sq);
        let f2 = soft.force_factor(r_sq);

        assert_relative_eq!(e1, e2, epsilon = 1e-12);
        assert_relative_eq!(f1, f2, epsilon = 1e-12);
    }

    #[test]
    fn test_soft_numerical_derivative() {
        let soft: Soft<f64, 12> = Soft::new(1.0, 1.0);
        let r = 1.2;
        let r_sq = r * r;

        let h = 1e-6;
        let v_plus = soft.energy((r + h) * (r + h));
        let v_minus = soft.energy((r - h) * (r - h));
        let dv_dr_numerical = (v_plus - v_minus) / (2.0 * h);

        let s_numerical = -dv_dr_numerical / r;
        let s_analytical = soft.force_factor(r_sq);

        assert_relative_eq!(s_analytical, s_numerical, epsilon = 1e-6);
    }
}