Initial Code

This commit is contained in:
2023-03-02 16:33:21 +00:00
parent a8641690e6
commit 64783b7946
7 changed files with 594 additions and 0 deletions
+156
View File
@@ -0,0 +1,156 @@
use std::cmp::{max, min};
use std::fs::File;
use std::io;
use std::io::Write;
use std::ops::Add;
use nd_array::Array;
use rand::prelude::*;
mod system;
use system::walker::{Walker, LocalRandomWalker};
use system::storage::{Storage, VectorStorage};
use num_integer::Integer;
use rand::rngs::SmallRng;
use crate::system::{DIM, Position};
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() {
use rand::{SeedableRng, thread_rng};
let mut sys: DLASystem<VectorStorage, LocalRandomWalker> = DLASystem::<VectorStorage, LocalRandomWalker>::new(1, 1000, 1.0);
while sys.running {
sys.update();
}
sys.export_data();
}
+27
View File
@@ -0,0 +1,27 @@
use std::ops::Add;
pub mod walker;
pub mod storage;
pub const DIM: u32 = 2;
#[derive(Clone, Debug)]
pub struct Position(pub i32, pub i32);
impl Position {
pub fn abs(&self) -> f32 {
((self.0.pow(2) + self.0.pow(2)) as f32).powf(0.5)
}
pub fn in_direction(direction: u32, value: i32) -> Self {
if direction == 0 { Position(value, 0) } else { Position(0, value) }
}
}
impl Add for Position {
type Output = Position;
fn add(self, rhs: Self) -> Self::Output {
Position(self.0 + rhs.0, self.1 + rhs.1)
}
}
+37
View File
@@ -0,0 +1,37 @@
use nd_array::Array;
use crate::system::Position;
pub trait Storage {
fn at(&self, position: &Position) -> bool;
fn deposit(&mut self, position: &Position);
}
pub struct VectorStorage {
backing: Vec<bool>,
grid_size: u32,
dim: u32,
}
impl VectorStorage {
pub(crate) fn new(grid_size: u32, dim: u32) -> VectorStorage {
VectorStorage { grid_size, dim, backing: vec![false; grid_size.pow(dim) as usize] }
}
fn linear_index(&self, position: &Position) -> usize {
let x = (position.0 + (self.grid_size as i32) / 2) as usize;
let y = (position.1 + (self.grid_size as i32) / 2) as usize;
return self.grid_size as usize * y + x
}
}
impl Storage for VectorStorage {
fn at(&self, position: &Position) -> bool {
return self.backing[self.linear_index(position)]
}
fn deposit(&mut self, position: &Position) {
let index = self.linear_index(position);
self.backing[index] = true;
}
}
+19
View File
@@ -0,0 +1,19 @@
use num_integer::Integer;
use rand::prelude::{Rng, SmallRng};
use crate::system::{DIM, Position};
pub trait Walker {
fn walk(&self, rng: &mut SmallRng, position: &Position) -> Position;
}
pub struct LocalRandomWalker;
impl Walker for LocalRandomWalker {
fn walk(&self, rng: &mut SmallRng, position: &Position) -> Position {
let (dim, sign) = rng.gen_range(0u32..(DIM * 2)).div_rem(&DIM);
let sign = if sign == 0 { -1 } else { 1 };
let offset = Position::in_direction(dim, sign);
position.clone() + offset
}
}