Skip to main content

moldyn_core/forces/
ljp.rs

1//! This module contains the [LennardJonesForce] struct, which implements
2//! the [Force] trait according to the Lennard-Jones potential.
3
4use crate::{Particle, forces::Force};
5
6/// A struct representing a Lennard-Jones force, which implements the
7/// [Force] trait.
8pub struct LennardJonesForce {
9    // TODO document
10    cutoff_radius: f64,
11
12    // TODO document
13    epsilon: f64,
14
15    // TODO document
16    sigma: f64,
17}
18
19impl Force for LennardJonesForce {
20    fn system_name(&self) -> &str {
21        "lennard-jones"
22    }
23
24    fn potential(&self, particle: &Particle, other: &Particle) -> f64 {
25        let distance = Particle::distance(particle, other);
26
27        if distance == 0.0 || distance > self.cutoff_radius {
28            0.0
29        } else {
30            let frac = self.sigma / distance;
31            let frac6 = frac.powi(6);
32            let frac12 = frac6.powi(2);
33
34            4.0 * self.epsilon * (frac12 - frac6)
35        }
36    }
37}
38
39impl Default for LennardJonesForce {
40    /// The default instance of [LennardJonesForce]. The parameters are set
41    /// to the following.
42    ///
43    /// | Parameter | Value |
44    /// | --- | --- |
45    /// | `cutoff_radius` | `3.0` |
46    /// | `epsilon` | `5.0` |
47    /// | `sigma` | `1.0` |`
48    fn default() -> Self {
49        Self {
50            // values are taken from 'assignment 3 task 2'
51            cutoff_radius: 3.0,
52            epsilon: 5.0,
53            sigma: 1.0,
54        }
55    }
56}