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
use crate::math::{Point, Vector};
use na::{self, RealField};
use std::sync::RwLock;
pub type BoundaryHandle = usize;
pub struct Boundary<N: RealField> {
pub positions: Vec<Point<N>>,
pub velocities: Vec<Vector<N>>,
pub forces: RwLock<Vec<Vector<N>>>,
}
impl<N: RealField> Boundary<N> {
pub fn new(particle_positions: Vec<Point<N>>) -> Self {
let num_particles = particle_positions.len();
let velocities = std::iter::repeat(Vector::zeros())
.take(num_particles)
.collect();
Self {
positions: particle_positions,
velocities,
forces: RwLock::new(Vec::new()),
}
}
pub fn num_particles(&self) -> usize {
self.positions.len()
}
pub fn apply_force(&self, i: usize, f: Vector<N>) {
let mut forces = self.forces.write().unwrap();
forces[i] += f;
}
pub fn clear_forces(&mut self, resize_buffer: bool) {
let forces = self.forces.get_mut().unwrap();
if resize_buffer {
forces.resize(self.positions.len(), Vector::zeros());
}
for f in forces {
f.fill(N::zero())
}
}
}