Swap LocalRandomWalker to GriddedPosition
This commit is contained in:
parent
f7ac576129
commit
14b5cb4679
@ -9,9 +9,9 @@ use crate::system::storage::{Storage, VectorStorage};
|
|||||||
use crate::system::walker::{LocalRandomWalker, Walker};
|
use crate::system::walker::{LocalRandomWalker, Walker};
|
||||||
use serde::{Serialize, Deserialize};
|
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,
|
rng: R,
|
||||||
storage: S,
|
space: S,
|
||||||
walker: W,
|
walker: W,
|
||||||
|
|
||||||
stick_probability: f32,
|
stick_probability: f32,
|
||||||
@ -29,7 +29,7 @@ pub struct DLASystem<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<R, P>>
|
|||||||
cluster_radius: f32,
|
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> {
|
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 {
|
let mut sys: DLASystem<R, Position, VectorStorage, LocalRandomWalker> = DLASystem {
|
||||||
rng,
|
rng,
|
||||||
@ -37,8 +37,8 @@ impl<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<R, P>> DLASystem<R, P,
|
|||||||
max_particles,
|
max_particles,
|
||||||
running: true,
|
running: true,
|
||||||
|
|
||||||
storage: VectorStorage::new(1600, 2),
|
space: VectorStorage::new(1600, 2),
|
||||||
walker: LocalRandomWalker::new(2),
|
walker: LocalRandomWalker,
|
||||||
particles: vec![],
|
particles: vec![],
|
||||||
active_particle: None,
|
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 {
|
if distance > self.kill_circle {
|
||||||
self.active_particle = None;
|
self.active_particle = None;
|
||||||
} else if !self.storage.at(&next_position) {
|
} else if !self.space.at(&next_position) {
|
||||||
if self.check_stick(&next_position) {
|
if self.check_stick(&next_position) {
|
||||||
self.deposit(&next_position);
|
self.deposit(&next_position);
|
||||||
self.active_particle = None;
|
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()
|
position.neighbours()
|
||||||
.iter()
|
.iter()
|
||||||
.any(|neighbour|
|
.any(|neighbour|
|
||||||
self.storage.at(&neighbour)
|
self.space.at(&neighbour)
|
||||||
&& self.rng.gen_range(0.0f32..=1.0) < self.stick_probability
|
&& 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) {
|
fn spawn_particle(&mut self) {
|
||||||
let position = P::spawn(&mut self.rng, self.add_circle);
|
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);
|
self.active_particle = Some(position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deposit(&mut self, p0: &P) {
|
fn deposit(&mut self, p0: &P) {
|
||||||
self.particles.push(p0.clone());
|
self.particles.push(p0.clone());
|
||||||
self.storage.deposit(p0);
|
self.space.deposit(p0);
|
||||||
|
|
||||||
let distance = p0.abs();
|
let distance = p0.abs();
|
||||||
if distance > self.cluster_radius {
|
if distance > self.cluster_radius {
|
||||||
|
|||||||
@ -2,20 +2,14 @@ use num_integer::Integer;
|
|||||||
use rand::prelude::Rng;
|
use rand::prelude::Rng;
|
||||||
use crate::system::{GriddedPosition, Position};
|
use crate::system::{GriddedPosition, Position};
|
||||||
|
|
||||||
pub trait Walker<R: Rng, P: GriddedPosition> {
|
pub trait Walker<P: GriddedPosition> {
|
||||||
fn walk(&self, rng: &mut R, position: &P) -> P;
|
fn walk<R: Rng>(&self, rng: &mut R, position: &P) -> P;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct LocalRandomWalker {
|
pub struct LocalRandomWalker;
|
||||||
dim: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LocalRandomWalker {
|
impl<Position: GriddedPosition> Walker<Position> for LocalRandomWalker {
|
||||||
pub(crate) fn new(dim: u32) -> LocalRandomWalker { LocalRandomWalker { dim } }
|
fn walk<R: Rng>(&self, rng: &mut R, position: &Position) -> Position {
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: Rng> Walker<R, Position> for LocalRandomWalker {
|
|
||||||
fn walk(&self, rng: &mut R, position: &Position) -> Position {
|
|
||||||
let neighbours = position.neighbours();
|
let neighbours = position.neighbours();
|
||||||
let index = rng.gen_range(0..(neighbours.len()));
|
let index = rng.gen_range(0..(neighbours.len()));
|
||||||
neighbours[index].clone()
|
neighbours[index].clone()
|
||||||
@ -25,16 +19,16 @@ impl<R: Rng> Walker<R, Position> for LocalRandomWalker {
|
|||||||
mod test {
|
mod test {
|
||||||
use rand::rngs::SmallRng;
|
use rand::rngs::SmallRng;
|
||||||
use rand::{SeedableRng, thread_rng};
|
use rand::{SeedableRng, thread_rng};
|
||||||
use crate::system::Position;
|
use crate::system::{GriddedPosition, Position};
|
||||||
use crate::system::walker::{LocalRandomWalker, Walker};
|
use crate::system::walker::{LocalRandomWalker, Walker};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn uniformity() {
|
fn uniformity() {
|
||||||
let walker = LocalRandomWalker::new(2);
|
let walker = LocalRandomWalker::new();
|
||||||
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
|
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
|
||||||
let mut results: Vec<Position> = vec![];
|
let mut results: Vec<Position> = vec![];
|
||||||
|
|
||||||
let origin = &Position(0, 0);
|
let origin = &Position::zero();
|
||||||
|
|
||||||
let x: u32 = (1_000000_000);
|
let x: u32 = (1_000000_000);
|
||||||
for i in 0..x {
|
for i in 0..x {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user