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
pub trait ExternalVelocity<T, X = T> {
fn eval(&self, t: T, x: X) -> X;
}
pub trait Interaction<T, X = T> {
fn eval<P>(&self, t: T, x: X, p: P) -> X
where
P: IntoIterator<Item = X>;
}
mod zero_fields;
pub use zero_fields::{ZeroInteraction, ZeroVelocity};
pub struct Velocity<V>(V);
impl<V> Velocity<V> {
#[allow(non_snake_case)]
pub fn new(V: V) -> Self {
Self(V)
}
}
impl<T, X, V> ExternalVelocity<T, X> for Velocity<V>
where
V: Fn(T, X) -> X,
{
fn eval(&self, t: T, x: X) -> X {
self.0(t, x)
}
}
impl<T, X, V> ExternalVelocity<T, X> for V
where
V: Fn(T, X) -> X,
{
fn eval(&self, t: T, x: X) -> X {
self(t, x)
}
}
pub struct SampledInteraction<Wprime>(Wprime);
impl<Wprime> SampledInteraction<Wprime> {
#[allow(non_snake_case)]
pub fn new(Wprime: Wprime) -> Self {
Self(Wprime)
}
}
impl<T, X, Wprime> Interaction<T, X> for SampledInteraction<Wprime>
where
T: Copy,
X: Copy + num_traits::real::Real,
Wprime: Fn(T, X) -> X,
{
fn eval<P>(&self, t: T, x: X, p: P) -> X
where
P: IntoIterator<Item = X>,
{
let mut sum = X::zero();
let mut len = 0;
for y in p.into_iter() {
len += 1;
let r = x - y;
if r != X::zero() {
sum = sum + self.0(t, r);
}
}
-X::from(sum).unwrap() / X::from(len - 1).unwrap()
}
}
pub struct IntegratedInteraction<W>(W);
impl<W> IntegratedInteraction<W> {
#[allow(non_snake_case)]
pub fn new(W: W) -> Self {
Self(W)
}
}
impl<T, X, W> Interaction<T, X> for IntegratedInteraction<W>
where
T: Copy,
X: Copy + num_traits::real::Real,
W: Fn(T, X) -> X,
{
fn eval<P>(&self, t: T, x: X, p: P) -> X
where
P: IntoIterator<Item = X>,
{
let mut p = p.into_iter().peekable();
let mut total = X::zero();
let mut left_dens = X::zero();
let mut len = 0;
while let Some(y) = p.next() {
len += 1;
let right_dens = if let Some(z) = p.peek() {
(*z - y).recip()
} else {
X::zero()
};
total = total + self.0(t, x - y) * (right_dens - left_dens);
left_dens = right_dens;
}
-total / X::from(len - 1).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zero_velocity() {
assert_eq!(ZeroVelocity.eval(0.0, 0.0), 0.0);
}
#[test]
fn zero_interaction_potential() {
let p = [1., 2., 3.];
assert_eq!(ZeroInteraction.eval(0.0, 0.0, p), 0.0);
}
#[test]
fn velocity() {
let vel = Velocity(|t: f64, x: f64| t + x);
assert_eq!(vel.eval(2., 3.), 5.);
}
#[test]
fn sampled_interaction() {
let int = SampledInteraction(|_t: f64, x: f64| x);
let p = [0., 1., 2., 3.];
assert_eq!(int.eval(0., 0., p), (0. + 1. + 2. + 3.) / (4. - 1.));
}
#[test]
fn integrated_interaction() {
let int = IntegratedInteraction(|_t: f64, x: f64| x * x / 2.);
let p = [0., 1., 2., 3.];
assert_eq!(int.eval(0., 0., p), (3. * 3. / 2. - 0.) / 3.);
}
}