Browse Source

Drop num_particles, can be replaced by a check whether there are any children

opt
Björn Steinbrink 8 months ago
parent
commit
66ef86258c
1 changed files with 2 additions and 10 deletions
  1. +2
    -10
      src/barnes_hut.rs

+ 2
- 10
src/barnes_hut.rs View File

@ -7,7 +7,6 @@ const G: f64 = 0.00667408;
const THETA: f64 = 0.5;
pub struct Octree {
num_particles: Vec<usize>,
children: Vec<(isize, u8)>, // TODO make this an Option<[usize; 8]>
mass: Vec<f64>,
min_coord: Vec<Vector3D>,
@ -20,7 +19,6 @@ pub struct Octree {
impl Octree {
pub fn new(min: Vector3D, max: Vector3D, n: usize) -> Self {
let mut o = Octree {
num_particles: Vec::with_capacity(n),
children: Vec::with_capacity(n),
mass: Vec::with_capacity(n),
min_coord: Vec::with_capacity(n),
@ -35,7 +33,6 @@ impl Octree {
let s = if width > height { width } else { height };
let s = if depth > s { depth } else { s };
o.num_particles.push(0);
o.children.push((-1, 0));
o.mass.push(0.0);
o.min_coord.push(min);
@ -62,12 +59,10 @@ impl Octree {
particle: &mut RigidPoint,
cur_node: usize,
delta: f64) {
if self.num_particles[cur_node] == 0 { return; }
let dis_vec = self.center_of_mass[cur_node] - particle.position;
let d_squared = dis_vec.dist_squared();
if self.num_particles[cur_node] == 1 || {
if self.children[cur_node].1 == 0 || {
let s = self.s[cur_node];
let s_squared = s * s;
let ratio = s_squared / d_squared;
@ -104,7 +99,6 @@ impl Octree {
if particles.len() == 1 {
self.mass[cur_node] += particles[0].mass;
self.num_particles[cur_node] += 1;
self.avg_weighted_position[cur_node] += particles[0].position * particles[0].mass;
return;
}
@ -125,7 +119,6 @@ impl Octree {
for &particle in particles {
// Update ourself
self.mass[cur_node] += particle.mass;
self.num_particles[cur_node] += 1;
self.avg_weighted_position[cur_node] += particle.position * particle.mass;
let octant_index = Octree::get_id_from_center(center_point, particle.position);
@ -133,7 +126,7 @@ impl Octree {
}
//Recurse
let idx = self.num_particles.len();
let idx = self.mass.len();
self.children[cur_node].0 = idx as isize;
for (i, particle_oct) in particle_octs.iter().enumerate() {
if !particle_oct.is_empty() {
@ -150,7 +143,6 @@ impl Octree {
let s = if width > height { width } else { height };
let s = if depth > s { depth } else { s };
self.num_particles.push(0);
self.children.push((-1, 0));
self.mass.push(0.0);
self.min_coord.push(min);


Loading…
Cancel
Save