Initial Code

This commit is contained in:
Joshua Coles 2023-03-02 16:33:21 +00:00
parent a8641690e6
commit 64783b7946
7 changed files with 594 additions and 0 deletions

340
Cargo.lock generated Normal file
View File

@ -0,0 +1,340 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "arrayvec"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cc"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d7ae14b20b94cb02149ed21a86c423859cbe18dc7ed69845cace50e52b40a5"
dependencies = [
"bitflags",
"clap_lex",
"is-terminal",
"strsim",
"termcolor",
]
[[package]]
name = "clap_lex"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "350b9cf31731f9957399229e9b2adc51eeabdfbe9d71d9a0552275fd12710d09"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "errno"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
dependencies = [
"errno-dragonfly",
"libc",
"winapi",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hermit-abi"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]]
name = "io-lifetimes"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1abeb7a0dd0f8181267ff8adc397075586500b81b28a73e8a0208b00fc170fb3"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "is-terminal"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21b6b32576413a8e69b90e952e4a026476040d81017b80445deda5f2d3921857"
dependencies = [
"hermit-abi",
"io-lifetimes",
"rustix",
"windows-sys",
]
[[package]]
name = "libc"
version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "linux-raw-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4"
[[package]]
name = "nd_array"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd8c381bc33732b4196e2e623561d745898b6b4a90c5c76bc813fbaa12f5d7a"
dependencies = [
"arrayvec",
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "os_str_bytes"
version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "rust-codebase"
version = "0.1.0"
dependencies = [
"clap",
"nd_array",
"num-integer",
"rand",
]
[[package]]
name = "rustix"
version = "0.36.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f43abb88211988493c1abb44a70efa56ff0ce98f233b7b276146f1f3f7ba9644"
dependencies = [
"bitflags",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "termcolor"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
dependencies = [
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
[[package]]
name = "windows_i686_gnu"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
[[package]]
name = "windows_i686_msvc"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"

13
Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "rust-codebase"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "4.1.8"
#bevy = { version = "0.9.1", features = ["dynamic"] }
nd_array = "0.1.0"
num-integer = "0.1.45"
rand = { version = "0.8.5", features = ["default", "small_rng"] }

2
rust-toolchain.toml Normal file
View File

@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"

156
src/main.rs Normal file
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
src/system/mod.rs Normal file
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
src/system/storage.rs Normal file
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
src/system/walker.rs Normal file
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
}
}