conspire/physics/molecular/potential/harmonic/
mod.rs

1use crate::{math::Scalar, physics::molecular::potential::Potential};
2
3/// The harmonic potential.
4#[derive(Clone, Debug)]
5pub struct Harmonic {
6    /// The rest length $`x_0`$.
7    pub rest_length: Scalar,
8    /// The stiffness $`k`$.
9    pub stiffness: Scalar,
10}
11
12impl Potential for Harmonic {
13    /// ```math
14    /// u(x) = \frac{1}{2}\,k(x - x_0)^2
15    /// ```
16    fn energy(&self, length: Scalar) -> Scalar {
17        0.5 * self.stiffness * (length - self.rest_length).powi(2)
18    }
19    /// ```math
20    /// f(x) = k(x - x_0)
21    /// ```
22    fn force(&self, length: Scalar) -> Scalar {
23        self.stiffness * (length - self.rest_length)
24    }
25    /// ```math
26    /// k(x) = k
27    /// ```
28    fn stiffness(&self, _length: Scalar) -> Scalar {
29        self.stiffness
30    }
31    /// ```math
32    /// h(x) = 0.0
33    /// ```
34    fn anharmonicity(&self, _length: Scalar) -> Scalar {
35        0.0
36    }
37    /// ```math
38    /// \Delta x(f) = \frac{f}{k}
39    /// ```
40    fn extension(&self, force: Scalar) -> Scalar {
41        force / self.stiffness
42    }
43    /// ```math
44    /// c(f) = \frac{1}{k}
45    /// ```
46    fn compliance(&self, _force: Scalar) -> Scalar {
47        1.0 / self.stiffness
48    }
49    /// ```math
50    /// \text{arg max }u(x) = \infty
51    /// ```
52    fn peak(&self) -> Scalar {
53        Scalar::INFINITY
54    }
55    /// ```math
56    /// f(x_\mathrm{peak}) = \infty
57    /// ```
58    fn peak_force(&self) -> Scalar {
59        Scalar::INFINITY
60    }
61    /// ```math
62    /// \text{arg min }u(x) = x_0
63    /// ```
64    fn rest_length(&self) -> Scalar {
65        self.rest_length
66    }
67}