Swap LocalRandomWalker to GriddedPosition

This commit is contained in:
Joshua Coles 2023-03-04 12:22:17 +00:00
parent f7ac576129
commit 14b5cb4679
2 changed files with 17 additions and 23 deletions

View File

@ -9,9 +9,9 @@ use crate::system::storage::{Storage, VectorStorage};
use crate::system::walker::{LocalRandomWalker, Walker};
use serde::{Serialize, Deserialize};
pub struct DLASystem<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<R, P>> {
pub struct DLASystem<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> {
rng: R,
storage: S,
space: S,
walker: W,
stick_probability: f32,
@ -29,7 +29,7 @@ pub struct DLASystem<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<R, P>>
cluster_radius: f32,
}
impl<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<R, P>> DLASystem<R, P, S, W> {
impl<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> DLASystem<R, P, S, W> {
pub fn new(rng: R, max_particles: usize, stick_probability: f32) -> DLASystem<R, Position, VectorStorage, LocalRandomWalker> {
let mut sys: DLASystem<R, Position, VectorStorage, LocalRandomWalker> = DLASystem {
rng,
@ -37,8 +37,8 @@ impl<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<R, P>> DLASystem<R, P,
max_particles,
running: true,
storage: VectorStorage::new(1600, 2),
walker: LocalRandomWalker::new(2),
space: VectorStorage::new(1600, 2),
walker: LocalRandomWalker,
particles: vec![],
active_particle: None,
@ -76,7 +76,7 @@ impl<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<R, P>> DLASystem<R, P,
if distance > self.kill_circle {
self.active_particle = None;
} else if !self.storage.at(&next_position) {
} else if !self.space.at(&next_position) {
if self.check_stick(&next_position) {
self.deposit(&next_position);
self.active_particle = None;
@ -91,7 +91,7 @@ impl<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<R, P>> DLASystem<R, P,
position.neighbours()
.iter()
.any(|neighbour|
self.storage.at(&neighbour)
self.space.at(&neighbour)
&& self.rng.gen_range(0.0f32..=1.0) < self.stick_probability
)
}
@ -99,14 +99,14 @@ impl<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<R, P>> DLASystem<R, P,
fn spawn_particle(&mut self) {
let position = P::spawn(&mut self.rng, self.add_circle);
if !self.storage.at(&position) {
if !self.space.at(&position) {
self.active_particle = Some(position);
}
}
fn deposit(&mut self, p0: &P) {
self.particles.push(p0.clone());
self.storage.deposit(p0);
self.space.deposit(p0);
let distance = p0.abs();
if distance > self.cluster_radius {

View File

@ -2,20 +2,14 @@ use num_integer::Integer;
use rand::prelude::Rng;
use crate::system::{GriddedPosition, Position};
pub trait Walker<R: Rng, P: GriddedPosition> {
fn walk(&self, rng: &mut R, position: &P) -> P;
pub trait Walker<P: GriddedPosition> {
fn walk<R: Rng>(&self, rng: &mut R, position: &P) -> P;
}
pub struct LocalRandomWalker {
dim: u32,
}
pub struct LocalRandomWalker;
impl LocalRandomWalker {
pub(crate) fn new(dim: u32) -> LocalRandomWalker { LocalRandomWalker { dim } }
}
impl<R: Rng> Walker<R, Position> for LocalRandomWalker {
fn walk(&self, rng: &mut R, position: &Position) -> Position {
impl<Position: GriddedPosition> Walker<Position> for LocalRandomWalker {
fn walk<R: Rng>(&self, rng: &mut R, position: &Position) -> Position {
let neighbours = position.neighbours();
let index = rng.gen_range(0..(neighbours.len()));
neighbours[index].clone()
@ -25,16 +19,16 @@ impl<R: Rng> Walker<R, Position> for LocalRandomWalker {
mod test {
use rand::rngs::SmallRng;
use rand::{SeedableRng, thread_rng};
use crate::system::Position;
use crate::system::{GriddedPosition, Position};
use crate::system::walker::{LocalRandomWalker, Walker};
#[test]
fn uniformity() {
let walker = LocalRandomWalker::new(2);
let walker = LocalRandomWalker::new();
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let mut results: Vec<Position> = vec![];
let origin = &Position(0, 0);
let origin = &Position::zero();
let x: u32 = (1_000000_000);
for i in 0..x {