Some refactor to allow for other entry points
This commit is contained in:
parent
64783b7946
commit
95e328ea1f
133
src/main.rs
133
src/main.rs
@ -13,140 +13,11 @@ use system::storage::{Storage, VectorStorage};
|
|||||||
use num_integer::Integer;
|
use num_integer::Integer;
|
||||||
use rand::rngs::SmallRng;
|
use rand::rngs::SmallRng;
|
||||||
use crate::system::{DIM, Position};
|
use crate::system::{DIM, Position};
|
||||||
|
use crate::system::model::DLASystem;
|
||||||
struct DLASystem<S: Storage, W: Walker> {
|
|
||||||
rng: SmallRng,
|
|
||||||
|
|
||||||
storage: S,
|
|
||||||
walker: W,
|
|
||||||
|
|
||||||
stick_probability: f32,
|
|
||||||
max_particles: usize,
|
|
||||||
|
|
||||||
running: bool,
|
|
||||||
|
|
||||||
particles: Vec<Position>,
|
|
||||||
active_particle: Option<Position>,
|
|
||||||
|
|
||||||
add_ratio: f32,
|
|
||||||
add_circle: f32,
|
|
||||||
kill_ratio: f32,
|
|
||||||
kill_circle: f32,
|
|
||||||
cluster_radius: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: Storage, W: Walker> DLASystem<S, W> {
|
|
||||||
fn new(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<VectorStorage, LocalRandomWalker> {
|
|
||||||
let mut sys: DLASystem<VectorStorage, LocalRandomWalker> = DLASystem {
|
|
||||||
rng: SmallRng::seed_from_u64(seed),
|
|
||||||
stick_probability,
|
|
||||||
max_particles,
|
|
||||||
running: true,
|
|
||||||
|
|
||||||
storage: VectorStorage::new(1600, 2),
|
|
||||||
walker: LocalRandomWalker,
|
|
||||||
particles: vec![],
|
|
||||||
active_particle: None,
|
|
||||||
|
|
||||||
add_ratio: 1.2,
|
|
||||||
kill_ratio: 1.7,
|
|
||||||
|
|
||||||
add_circle: 10.0,
|
|
||||||
kill_circle: 20.0,
|
|
||||||
cluster_radius: 0.0,
|
|
||||||
};
|
|
||||||
|
|
||||||
sys.deposit(&Position(0, 0));
|
|
||||||
|
|
||||||
sys
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(&mut self) {
|
|
||||||
if self.active_particle.is_some() {
|
|
||||||
self.move_particle();
|
|
||||||
} else if self.particles.len() < self.max_particles {
|
|
||||||
self.spawn_particle();
|
|
||||||
} else {
|
|
||||||
self.running = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_particle(&mut self) {
|
|
||||||
let current_position = &self
|
|
||||||
.active_particle
|
|
||||||
.clone()
|
|
||||||
.expect("No active particle");
|
|
||||||
|
|
||||||
if self.check_stick(current_position) {
|
|
||||||
self.deposit(current_position);
|
|
||||||
self.active_particle = None;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let next_position = self.walker.walk(&mut self.rng, current_position);
|
|
||||||
let distance = next_position.abs();
|
|
||||||
|
|
||||||
if distance > self.kill_circle {
|
|
||||||
self.active_particle = None;
|
|
||||||
} else if !self.storage.at(&next_position) {
|
|
||||||
self.active_particle.replace(next_position);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_stick(&mut self, position: &Position) -> bool {
|
|
||||||
for direction in 0..DIM {
|
|
||||||
for sign in [-1, 1] {
|
|
||||||
let neighbour = position.clone() + Position::in_direction(direction, sign);
|
|
||||||
if self.storage.at(&neighbour) && self.rng.gen_range(0.0f32..1.0) < self.stick_probability {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn spawn_particle(&mut self) {
|
|
||||||
let theta = self.rng.gen_range(0f32..1.0);
|
|
||||||
let (x, y) = (self.add_circle * theta.cos(), self.add_circle * theta.sin());
|
|
||||||
let position = Position(x.round() as i32, y.round() as i32);
|
|
||||||
|
|
||||||
if !self.storage.at(&position) {
|
|
||||||
self.active_particle = Some(position);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deposit(&mut self, p0: &Position) {
|
|
||||||
self.particles.push(p0.clone());
|
|
||||||
self.storage.deposit(p0);
|
|
||||||
|
|
||||||
let distance = p0.abs();
|
|
||||||
if distance > self.cluster_radius {
|
|
||||||
self.cluster_radius = distance;
|
|
||||||
|
|
||||||
let new_add_circle = (self.cluster_radius * self.add_ratio).max(self.cluster_radius + 5.0);
|
|
||||||
if self.add_circle < new_add_circle {
|
|
||||||
self.add_circle = new_add_circle;
|
|
||||||
self.kill_circle = self.kill_ratio * self.add_circle;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn export_data(&self) -> io::Result<()> {
|
|
||||||
let mut file = File::create("out.csv")?;
|
|
||||||
writeln!(&mut file, "x, y")?;
|
|
||||||
|
|
||||||
for particle in &self.particles {
|
|
||||||
writeln!(&mut file, "{}, {}", particle.0, particle.1)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
use rand::{SeedableRng, thread_rng};
|
use rand::{SeedableRng, thread_rng};
|
||||||
let mut sys: DLASystem<VectorStorage, LocalRandomWalker> = DLASystem::<VectorStorage, LocalRandomWalker>::new(1, 1000, 1.0);
|
let mut sys = DLASystem::<SmallRng, VectorStorage, LocalRandomWalker>::new(1, 1000, 1.0);
|
||||||
|
|
||||||
while sys.running {
|
while sys.running {
|
||||||
sys.update();
|
sys.update();
|
||||||
|
|||||||
@ -2,6 +2,7 @@ use std::ops::Add;
|
|||||||
|
|
||||||
pub mod walker;
|
pub mod walker;
|
||||||
pub mod storage;
|
pub mod storage;
|
||||||
|
pub mod model;
|
||||||
|
|
||||||
pub const DIM: u32 = 2;
|
pub const DIM: u32 = 2;
|
||||||
|
|
||||||
|
|||||||
136
src/system/model.rs
Normal file
136
src/system/model.rs
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::io;
|
||||||
|
use rand::prelude::*;
|
||||||
|
use crate::system::{DIM, Position};
|
||||||
|
use crate::system::storage::{Storage, VectorStorage};
|
||||||
|
use crate::system::walker::{LocalRandomWalker, Walker};
|
||||||
|
|
||||||
|
pub struct DLASystem<R: Rng, S: Storage, W: Walker<R>> {
|
||||||
|
rng: R,
|
||||||
|
storage: S,
|
||||||
|
walker: W,
|
||||||
|
|
||||||
|
stick_probability: f32,
|
||||||
|
max_particles: usize,
|
||||||
|
|
||||||
|
pub running: bool,
|
||||||
|
|
||||||
|
particles: Vec<Position>,
|
||||||
|
active_particle: Option<Position>,
|
||||||
|
|
||||||
|
add_ratio: f32,
|
||||||
|
add_circle: f32,
|
||||||
|
kill_ratio: f32,
|
||||||
|
kill_circle: f32,
|
||||||
|
cluster_radius: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R: Rng, S: Storage, W: Walker<R>> DLASystem<R, S, W> {
|
||||||
|
pub fn new(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, VectorStorage, LocalRandomWalker> {
|
||||||
|
let mut sys: DLASystem<SmallRng, VectorStorage, LocalRandomWalker> = DLASystem {
|
||||||
|
rng: SmallRng::seed_from_u64(seed),
|
||||||
|
stick_probability,
|
||||||
|
max_particles,
|
||||||
|
running: true,
|
||||||
|
|
||||||
|
storage: VectorStorage::new(1600, 2),
|
||||||
|
walker: LocalRandomWalker,
|
||||||
|
particles: vec![],
|
||||||
|
active_particle: None,
|
||||||
|
|
||||||
|
add_ratio: 1.2,
|
||||||
|
kill_ratio: 1.7,
|
||||||
|
|
||||||
|
add_circle: 10.0,
|
||||||
|
kill_circle: 20.0,
|
||||||
|
cluster_radius: 0.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
sys.deposit(&Position(0, 0));
|
||||||
|
|
||||||
|
sys
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self) {
|
||||||
|
if self.active_particle.is_some() {
|
||||||
|
self.move_particle();
|
||||||
|
} else if self.particles.len() < self.max_particles {
|
||||||
|
self.spawn_particle();
|
||||||
|
} else {
|
||||||
|
self.running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_particle(&mut self) {
|
||||||
|
let current_position = &self
|
||||||
|
.active_particle
|
||||||
|
.clone()
|
||||||
|
.expect("No active particle");
|
||||||
|
|
||||||
|
if self.check_stick(current_position) {
|
||||||
|
self.deposit(current_position);
|
||||||
|
self.active_particle = None;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let next_position = self.walker.walk(&mut self.rng, current_position);
|
||||||
|
let distance = next_position.abs();
|
||||||
|
|
||||||
|
if distance > self.kill_circle {
|
||||||
|
self.active_particle = None;
|
||||||
|
} else if !self.storage.at(&next_position) {
|
||||||
|
self.active_particle.replace(next_position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_stick(&mut self, position: &Position) -> bool {
|
||||||
|
for direction in 0..DIM {
|
||||||
|
for sign in [-1, 1] {
|
||||||
|
let neighbour = position.clone() + Position::in_direction(direction, sign);
|
||||||
|
if self.storage.at(&neighbour) && self.rng.gen_range(0.0f32..1.0) < self.stick_probability {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_particle(&mut self) {
|
||||||
|
let theta = self.rng.gen_range(0f32..1.0);
|
||||||
|
let (x, y) = (self.add_circle * theta.cos(), self.add_circle * theta.sin());
|
||||||
|
let position = Position(x.round() as i32, y.round() as i32);
|
||||||
|
|
||||||
|
if !self.storage.at(&position) {
|
||||||
|
self.active_particle = Some(position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deposit(&mut self, p0: &Position) {
|
||||||
|
self.particles.push(p0.clone());
|
||||||
|
self.storage.deposit(p0);
|
||||||
|
|
||||||
|
let distance = p0.abs();
|
||||||
|
if distance > self.cluster_radius {
|
||||||
|
self.cluster_radius = distance;
|
||||||
|
|
||||||
|
let new_add_circle = (self.cluster_radius * self.add_ratio).max(self.cluster_radius + 5.0);
|
||||||
|
if self.add_circle < new_add_circle {
|
||||||
|
self.add_circle = new_add_circle;
|
||||||
|
self.kill_circle = self.kill_ratio * self.add_circle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn export_data(&self) -> io::Result<()> {
|
||||||
|
let mut file = File::create("out.csv")?;
|
||||||
|
writeln!(&mut file, "x, y")?;
|
||||||
|
|
||||||
|
for particle in &self.particles {
|
||||||
|
writeln!(&mut file, "{}, {}", particle.0, particle.1)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,14 +2,14 @@ use num_integer::Integer;
|
|||||||
use rand::prelude::{Rng, SmallRng};
|
use rand::prelude::{Rng, SmallRng};
|
||||||
use crate::system::{DIM, Position};
|
use crate::system::{DIM, Position};
|
||||||
|
|
||||||
pub trait Walker {
|
pub trait Walker<R: Rng> {
|
||||||
fn walk(&self, rng: &mut SmallRng, position: &Position) -> Position;
|
fn walk(&self, rng: &mut R, position: &Position) -> Position;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct LocalRandomWalker;
|
pub struct LocalRandomWalker;
|
||||||
|
|
||||||
impl Walker for LocalRandomWalker {
|
impl<R: Rng> Walker<R> for LocalRandomWalker {
|
||||||
fn walk(&self, rng: &mut SmallRng, position: &Position) -> Position {
|
fn walk(&self, rng: &mut R, position: &Position) -> Position {
|
||||||
let (dim, sign) = rng.gen_range(0u32..(DIM * 2)).div_rem(&DIM);
|
let (dim, sign) = rng.gen_range(0u32..(DIM * 2)).div_rem(&DIM);
|
||||||
let sign = if sign == 0 { -1 } else { 1 };
|
let sign = if sign == 0 { -1 } else { 1 };
|
||||||
let offset = Position::in_direction(dim, sign);
|
let offset = Position::in_direction(dim, sign);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user