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
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
//! # Generalized Mie Potential (n-m)
//!
//! The Mie potential generalizes Lennard-Jones with arbitrary integer exponents.
//!
//! ## Formula
//!
//! ```text
//! V(r) = C * epsilon * [(sigma/r)^n - (sigma/r)^m]
//!
//! where:
//!     C = (n / (n - m)) * (n / m)^(m / (n - m))
//! ```
//!
//! The prefactor C ensures that:
//! - The minimum energy is exactly -epsilon
//! - r_min = (n/m)^(1/(n-m)) * sigma
//!
//! ## Special Cases
//!
//! | N  | M | Name |
//! |----|---|------|
//! | 12 | 6 | Lennard-Jones |
//! | 9  | 6 | Soft-core LJ |
//! | 14 | 7 | Harder repulsion |
//!
//! ## Implementation Notes
//!
//! - Uses const generics for compile-time optimization of exponents
//! - Exponent computations are fully unrolled by the compiler
//! - Zero runtime overhead for exponent handling

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

/// Generalized Mie (n-m) potential.
///
/// ## Type Parameters
///
/// - `T`: Numeric type (f32, f64, or SIMD vector)
/// - `N`: Repulsive exponent (must be > M)
/// - `M`: Attractive exponent (must be > 0)
///
/// ## Parameters
///
/// - `epsilon`: Well depth (energy)
/// - `sigma`: Characteristic length (length)
///
/// ## Precomputed Values
///
/// Stores `cn = C * epsilon * sigma^n` and `cm = C * epsilon * sigma^m`
/// for efficient evaluation.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Mie<T, const N: u32, const M: u32> {
    /// Coefficient for r^-n term: C * epsilon * sigma^n
    cn: T,
    /// Coefficient for r^-m term: C * epsilon * sigma^m
    cm: T,
}

impl<T: Vector, const N: u32, const M: u32> Mie<T, N, M> {
    /// Creates a new Mie potential.
    ///
    /// ## Arguments
    ///
    /// - `epsilon`: Well depth (energy units)
    /// - `sigma`: Characteristic length (length units)
    ///
    /// ## Panics
    ///
    /// Panics if `N <= M` or `M == 0`.
    ///
    /// ## Example
    ///
    /// ```
    /// use potentials::pair::Mie;
    ///
    /// // Standard LJ 12-6
    /// let lj = Mie::<f64, 12, 6>::new(1.0, 1.0);
    ///
    /// // Softer 9-6 potential
    /// let soft = Mie::<f64, 9, 6>::new(1.0, 1.0);
    /// ```
    #[inline]
    pub fn new(epsilon: f64, sigma: f64) -> Self {
        assert!(N > M, "Mie potential requires N > M, got N={}, M={}", N, M);
        assert!(M > 0, "Mie potential requires M > 0, got M={}", M);

        let n = N as f64;
        let m = M as f64;

        // Prefactor: C = (n/(n-m)) * (n/m)^(m/(n-m))
        let n_minus_m = n - m;
        let prefactor = (n / n_minus_m) * (n / m).powf(m / n_minus_m);
        let c_eps = prefactor * epsilon;

        // Precompute sigma^n and sigma^m using integer powers
        let sigma_n = int_pow(sigma, N);
        let sigma_m = int_pow(sigma, M);

        Self {
            cn: T::splat(c_eps * sigma_n),
            cm: T::splat(c_eps * sigma_m),
        }
    }

    /// Creates a new Mie potential from precomputed coefficients.
    ///
    /// ## Arguments
    ///
    /// - `cn`: Coefficient for r^-n term (C * epsilon * sigma^n)
    /// - `cm`: Coefficient for r^-m term (C * epsilon * sigma^m)
    ///
    /// This is useful when mixing rules produce cn/cm directly.
    #[inline]
    pub fn from_coefficients(cn: f64, cm: f64) -> Self {
        Self {
            cn: T::splat(cn),
            cm: T::splat(cm),
        }
    }

    /// Returns the cn coefficient.
    #[inline]
    pub fn cn(&self) -> T {
        self.cn
    }

    /// Returns the cm coefficient.
    #[inline]
    pub fn cm(&self) -> T {
        self.cm
    }

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

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

impl<T: Vector, const N: u32, const M: u32> Potential2<T> for Mie<T, N, M> {
    /// Computes the potential energy.
    ///
    /// ```text
    /// V(r) = cn/r^n - cm/r^m
    /// ```
    #[inline(always)]
    fn energy(&self, r_sq: T) -> T {
        let r_inv = r_sq.rsqrt();

        // Compute r^-n and r^-m using optimized integer powers
        let r_neg_n = int_pow_vec::<T, N>(r_inv);
        let r_neg_m = int_pow_vec::<T, M>(r_inv);

        self.cn * r_neg_n - self.cm * r_neg_m
    }

    /// Computes the force factor.
    ///
    /// ```text
    /// dV/dr = -n*cn/r^(n+1) + m*cm/r^(m+1)
    /// S = -(dV/dr)/r = n*cn/r^(n+2) - m*cm/r^(m+2)
    /// ```
    #[inline(always)]
    fn force_factor(&self, r_sq: T) -> T {
        let r_inv = r_sq.rsqrt();
        let r_sq_inv = r_sq.recip();

        let r_neg_n = int_pow_vec::<T, N>(r_inv);
        let r_neg_m = int_pow_vec::<T, M>(r_inv);

        let n = T::splat(N as f64);
        let m = T::splat(M as f64);

        (n * self.cn * r_neg_n - m * self.cm * r_neg_m) * r_sq_inv
    }

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

        let r_neg_n = int_pow_vec::<T, N>(r_inv);
        let r_neg_m = int_pow_vec::<T, M>(r_inv);

        let cn_rn = self.cn * r_neg_n;
        let cm_rm = self.cm * r_neg_m;

        let energy = cn_rn - cm_rm;

        let n = T::splat(N as f64);
        let m = T::splat(M as f64);
        let force = (n * cn_rn - m * cm_rm) * 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_mie_reduces_to_lj() {
        let mie: Mie<f64, 12, 6> = Mie::new(1.0, 1.0);
        let lj = crate::pair::Lj::<f64>::new(1.0, 1.0);

        let r_sq = 1.5 * 1.5;
        let e_mie = mie.energy(r_sq);
        let e_lj = lj.energy(r_sq);

        assert_relative_eq!(e_mie, e_lj, epsilon = 1e-10);
    }

    #[test]
    fn test_mie_at_minimum() {
        let epsilon = 1.5;
        let sigma = 2.0;
        let mie: Mie<f64, 10, 5> = Mie::new(epsilon, sigma);

        let r_min = 2.0_f64.powf(1.0 / 5.0) * sigma;
        let r_sq = r_min * r_min;
        let energy = mie.energy(r_sq);

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

    #[test]
    fn test_mie_force_at_minimum() {
        let mie: Mie<f64, 12, 6> = Mie::new(1.0, 1.0);

        let r_min = 2.0_f64.powf(1.0 / 6.0);
        let r_sq = r_min * r_min;
        let force = mie.force_factor(r_sq);

        assert_relative_eq!(force, 0.0, epsilon = 1e-10);
    }

    #[test]
    fn test_mie_energy_force_consistency() {
        let mie: Mie<f64, 9, 6> = Mie::new(0.5, 2.0);
        let r_sq = 6.25;

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

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

    #[test]
    fn test_mie_numerical_derivative() {
        let mie: Mie<f64, 9, 6> = Mie::new(1.0, 1.0);
        let r = 1.2;
        let r_sq = r * r;

        let h = 1e-6;
        let v_plus = mie.energy((r + h) * (r + h));
        let v_minus = mie.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 = mie.force_factor(r_sq);

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