Skip to main content

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

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