Compare commits
No commits in common. "7216fd561e7d1fc17ffa94aaf9914704fbedfa44" and "1f5c94cb828f077038b39aa35798b5f667f34664" have entirely different histories.
7216fd561e
...
1f5c94cb82
171
Cargo.lock
generated
171
Cargo.lock
generated
@ -1931,20 +1931,6 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "kd-tree"
|
|
||||||
version = "0.5.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f54287107c30b23cf7a32e394c68b9472220a090c897d916dfdf467caef88864"
|
|
||||||
dependencies = [
|
|
||||||
"nalgebra 0.31.4",
|
|
||||||
"num-traits",
|
|
||||||
"ordered-float",
|
|
||||||
"paste",
|
|
||||||
"pdqselect",
|
|
||||||
"typenum",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "khronos-egl"
|
name = "khronos-egl"
|
||||||
version = "4.1.0"
|
version = "4.1.0"
|
||||||
@ -1956,15 +1942,6 @@ dependencies = [
|
|||||||
"pkg-config",
|
"pkg-config",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "kiddo"
|
|
||||||
version = "0.2.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "06ced2e69cfc5f22f86ccc9ce4ecff9f19917f3083a4bac0f402bdab034d73f1"
|
|
||||||
dependencies = [
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kqueue"
|
name = "kqueue"
|
||||||
version = "1.0.7"
|
version = "1.0.7"
|
||||||
@ -2086,15 +2063,6 @@ dependencies = [
|
|||||||
"regex-automata",
|
"regex-automata",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "matrixmultiply"
|
|
||||||
version = "0.3.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "add85d4dd35074e6fedc608f8c8f513a3548619a9024b751949ef0e8e45a4d84"
|
|
||||||
dependencies = [
|
|
||||||
"rawpointer",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.5.0"
|
version = "2.5.0"
|
||||||
@ -2173,60 +2141,6 @@ dependencies = [
|
|||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "nalgebra"
|
|
||||||
version = "0.31.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "20bd243ab3dbb395b39ee730402d2e5405e448c75133ec49cc977762c4cba3d1"
|
|
||||||
dependencies = [
|
|
||||||
"approx",
|
|
||||||
"matrixmultiply",
|
|
||||||
"nalgebra-macros 0.1.0",
|
|
||||||
"num-complex",
|
|
||||||
"num-rational",
|
|
||||||
"num-traits",
|
|
||||||
"simba 0.7.3",
|
|
||||||
"typenum",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "nalgebra"
|
|
||||||
version = "0.32.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d68d47bba83f9e2006d117a9a33af1524e655516b8919caac694427a6fb1e511"
|
|
||||||
dependencies = [
|
|
||||||
"approx",
|
|
||||||
"matrixmultiply",
|
|
||||||
"nalgebra-macros 0.2.0",
|
|
||||||
"num-complex",
|
|
||||||
"num-rational",
|
|
||||||
"num-traits",
|
|
||||||
"simba 0.8.0",
|
|
||||||
"typenum",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "nalgebra-macros"
|
|
||||||
version = "0.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "01fcc0b8149b4632adc89ac3b7b31a12fb6099a0317a4eb2ebff574ef7de7218"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "nalgebra-macros"
|
|
||||||
version = "0.2.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d232c68884c0c99810a5a4d333ef7e47689cfd0edc85efc9e54e1e6bf5212766"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nd_array"
|
name = "nd_array"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
@ -2381,15 +2295,6 @@ dependencies = [
|
|||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "num-complex"
|
|
||||||
version = "0.4.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "02e0d21255c828d6f128a1e41534206671e8c3ea0c62f32291e808dc82cff17d"
|
|
||||||
dependencies = [
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-derive"
|
name = "num-derive"
|
||||||
version = "0.3.3"
|
version = "0.3.3"
|
||||||
@ -2509,15 +2414,6 @@ version = "1.17.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
|
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "ordered-float"
|
|
||||||
version = "3.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d84eb1409416d254e4a9c8fa56cc24701755025b458f0fcd8e59e1f5f40c23bf"
|
|
||||||
dependencies = [
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "os_str_bytes"
|
name = "os_str_bytes"
|
||||||
version = "6.4.1"
|
version = "6.4.1"
|
||||||
@ -2568,18 +2464,6 @@ dependencies = [
|
|||||||
"windows-sys 0.45.0",
|
"windows-sys 0.45.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "paste"
|
|
||||||
version = "1.0.12"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pdqselect"
|
|
||||||
version = "0.1.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "7778906d9321dd56cde1d1ffa69a73e59dcf5fda6d366f62727adf2bd4193aee"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "peeking_take_while"
|
name = "peeking_take_while"
|
||||||
version = "0.1.2"
|
version = "0.1.2"
|
||||||
@ -2759,12 +2643,6 @@ dependencies = [
|
|||||||
"cty",
|
"cty",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rawpointer"
|
|
||||||
version = "0.2.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rectangle-pack"
|
name = "rectangle-pack"
|
||||||
version = "0.4.2"
|
version = "0.4.2"
|
||||||
@ -2841,14 +2719,10 @@ dependencies = [
|
|||||||
"cbindgen",
|
"cbindgen",
|
||||||
"clap 4.1.8",
|
"clap 4.1.8",
|
||||||
"csv",
|
"csv",
|
||||||
"kd-tree",
|
|
||||||
"kiddo",
|
|
||||||
"nalgebra 0.32.2",
|
|
||||||
"nd_array",
|
"nd_array",
|
||||||
"num-integer",
|
"num-integer",
|
||||||
"rand",
|
"rand",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -2886,15 +2760,6 @@ version = "1.0.12"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
|
checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "safe_arch"
|
|
||||||
version = "0.6.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "794821e4ccb0d9f979512f9c1973480123f9bd62a90d74ab0f9426fcf8f4a529"
|
|
||||||
dependencies = [
|
|
||||||
"bytemuck",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "same-file"
|
name = "same-file"
|
||||||
version = "1.0.6"
|
version = "1.0.6"
|
||||||
@ -2992,32 +2857,6 @@ version = "1.1.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
|
checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "simba"
|
|
||||||
version = "0.7.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "2f3fd720c48c53cace224ae62bef1bbff363a70c68c4802a78b5cc6159618176"
|
|
||||||
dependencies = [
|
|
||||||
"approx",
|
|
||||||
"num-complex",
|
|
||||||
"num-traits",
|
|
||||||
"paste",
|
|
||||||
"wide",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "simba"
|
|
||||||
version = "0.8.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "50582927ed6f77e4ac020c057f37a268fc6aebc29225050365aacbb9deeeddc4"
|
|
||||||
dependencies = [
|
|
||||||
"approx",
|
|
||||||
"num-complex",
|
|
||||||
"num-traits",
|
|
||||||
"paste",
|
|
||||||
"wide",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "slab"
|
name = "slab"
|
||||||
version = "0.4.8"
|
version = "0.4.8"
|
||||||
@ -3575,16 +3414,6 @@ dependencies = [
|
|||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wide"
|
|
||||||
version = "0.7.8"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b689b6c49d6549434bf944e6b0f39238cf63693cb7a147e9d887507fffa3b223"
|
|
||||||
dependencies = [
|
|
||||||
"bytemuck",
|
|
||||||
"safe_arch",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.3.9"
|
version = "0.3.9"
|
||||||
|
|||||||
@ -38,10 +38,6 @@ num-integer = "0.1.45"
|
|||||||
rand = { version = "0.8.5", features = ["default", "small_rng"] }
|
rand = { version = "0.8.5", features = ["default", "small_rng"] }
|
||||||
csv = "1.1"
|
csv = "1.1"
|
||||||
serde = { version = "1.0.152", features = ["derive"] }
|
serde = { version = "1.0.152", features = ["derive"] }
|
||||||
serde_json = "1.0.93"
|
|
||||||
kd-tree = { version = "0.5.1", features = ["nalgebra"] }
|
|
||||||
nalgebra = "0.32.2"
|
|
||||||
kiddo = "0.2.5"
|
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
cbindgen = "0.24.3"
|
cbindgen = "0.24.3"
|
||||||
|
|||||||
10002
balls.json
10002
balls.json
File diff suppressed because it is too large
Load Diff
@ -1,56 +0,0 @@
|
|||||||
use std::path::PathBuf;
|
|
||||||
use clap::{Parser, Args, Subcommand, ValueEnum};
|
|
||||||
|
|
||||||
#[derive(ValueEnum, Clone, Debug, Copy)]
|
|
||||||
pub enum OutputFormat {
|
|
||||||
FullDataJson,
|
|
||||||
Positions,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
|
||||||
pub struct InitialCli {
|
|
||||||
pub grid_size: u32
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
|
||||||
pub struct StickProbabilityCli {
|
|
||||||
pub grid_size: u32,
|
|
||||||
pub stick_probability: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
|
||||||
pub struct BallsCli {
|
|
||||||
pub ball_radius: f32,
|
|
||||||
pub stick_distance: f32,
|
|
||||||
pub walk_step: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Subcommand, Debug)]
|
|
||||||
pub enum PCM {
|
|
||||||
Initial(InitialCli),
|
|
||||||
StickProbability(StickProbabilityCli),
|
|
||||||
Grid3(StickProbabilityCli),
|
|
||||||
Hex(StickProbabilityCli),
|
|
||||||
Balls(BallsCli)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
|
||||||
pub struct ModelCli {
|
|
||||||
#[command(subcommand)]
|
|
||||||
pub preconfigured_model: PCM,
|
|
||||||
|
|
||||||
#[arg(long)]
|
|
||||||
pub max_frames: Option<usize>,
|
|
||||||
|
|
||||||
#[arg(short = 'N', long)]
|
|
||||||
pub max_particles: usize,
|
|
||||||
|
|
||||||
#[arg(long, short)]
|
|
||||||
pub seed: u64,
|
|
||||||
|
|
||||||
#[arg(value_enum, short, long, default_value_t = OutputFormat::Positions)]
|
|
||||||
pub format: OutputFormat,
|
|
||||||
|
|
||||||
#[arg(value_enum, short, long)]
|
|
||||||
pub output: PathBuf,
|
|
||||||
}
|
|
||||||
@ -1,24 +0,0 @@
|
|||||||
use rand::Rng;
|
|
||||||
use crate::system::model::DLASystem;
|
|
||||||
use crate::system::{Position, Storage};
|
|
||||||
use crate::system::spawner::Spawner;
|
|
||||||
use crate::system::sticker::Sticker;
|
|
||||||
use crate::system::walker::Walker;
|
|
||||||
|
|
||||||
pub mod cli;
|
|
||||||
pub mod output;
|
|
||||||
|
|
||||||
pub fn drive_system<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P, S>>(sys: &mut DLASystem<R, P, S, W, Sp, St>, max_frames: Option<usize>) {
|
|
||||||
while sys.running {
|
|
||||||
sys.update();
|
|
||||||
|
|
||||||
match max_frames {
|
|
||||||
Some(max_frames) if max_frames <= sys.frame => {
|
|
||||||
sys.running = false;
|
|
||||||
eprintln!("System halted as it ran to {frame} frames (max_frames = {max_frames}) and did not complete", frame = sys.frame)
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ -1,49 +0,0 @@
|
|||||||
use std::fs::File;
|
|
||||||
use std::path::Path;
|
|
||||||
use rand::Rng;
|
|
||||||
use crate::cli::cli::OutputFormat;
|
|
||||||
use crate::system::model::DLASystem;
|
|
||||||
use crate::system::{Position, Storage};
|
|
||||||
use crate::system::spawner::Spawner;
|
|
||||||
use crate::system::sticker::Sticker;
|
|
||||||
use crate::system::walker::Walker;
|
|
||||||
|
|
||||||
pub fn write<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P, S>>(
|
|
||||||
sys: &DLASystem<R, P, S, W, Sp, St>,
|
|
||||||
format: OutputFormat,
|
|
||||||
output: &Path,
|
|
||||||
) {
|
|
||||||
match format {
|
|
||||||
OutputFormat::FullDataJson => write_json_full_data(sys, output),
|
|
||||||
OutputFormat::Positions => write_csv_positions(sys, output),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_csv_positions<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P, S>>(sys: &DLASystem<R, P, S, W, Sp, St>, csv_path: &Path) {
|
|
||||||
let mut wtr = csv::Writer::from_path(csv_path)
|
|
||||||
.expect("Failed to open file");
|
|
||||||
|
|
||||||
// CSVs can only store the raw positions
|
|
||||||
let positions: Vec<&P> = sys.history
|
|
||||||
.iter()
|
|
||||||
.map(|line| &line.position)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
wtr.serialize(positions)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
wtr.flush()
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_json_full_data<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P, S>>(sys: &DLASystem<R, P, S, W, Sp, St>, output_path: &Path) {
|
|
||||||
let file = File::create(output_path).expect("Failed to open file");
|
|
||||||
|
|
||||||
let vec: Vec<P> = sys.history
|
|
||||||
.iter()
|
|
||||||
.map(|l| l.position.clone())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
serde_json::to_writer(file, &vec)
|
|
||||||
.expect("Failed to write json");
|
|
||||||
}
|
|
||||||
12
src/clib.rs
12
src/clib.rs
@ -1,7 +1,7 @@
|
|||||||
#![feature(array_zip)]
|
#![feature(array_zip)]
|
||||||
|
|
||||||
use system::Storage;
|
use system::Storage;
|
||||||
use crate::system::grid::{Pos2D, VectorStorage};
|
use crate::system::grid::{Position, VectorStorage};
|
||||||
|
|
||||||
mod system;
|
mod system;
|
||||||
|
|
||||||
@ -19,12 +19,12 @@ pub extern "C" fn storage_new(grid_size: u32) -> &'static mut CStorage {
|
|||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn storage_at(storage: &CStorage, i: i32, j: i32) -> bool {
|
pub extern "C" fn storage_at(storage: &CStorage, i: i32, j: i32) -> bool {
|
||||||
storage.0.at(&Pos2D { x: i, y: j })
|
storage.0.at(&Position { x: i, y: j })
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn storage_deposit(storage: &mut CStorage, i: i32, j: i32, val: u8) {
|
pub extern "C" fn storage_deposit(storage: &mut CStorage, i: i32, j: i32, val: u8) {
|
||||||
storage.0.write(&Pos2D { x: i, y: j }, val == 1);
|
storage.0.write(&Position { x: i, y: j }, val == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -35,7 +35,7 @@ pub extern "C" fn walk(d: u32, i: i32, j: i32) -> CPosition {
|
|||||||
mod test {
|
mod test {
|
||||||
use num_integer::Integer;
|
use num_integer::Integer;
|
||||||
use crate::CPosition;
|
use crate::CPosition;
|
||||||
use crate::system::grid::Pos2D;
|
use crate::system::grid::Position;
|
||||||
|
|
||||||
pub(crate) fn a(d: u32, i: i32, j: i32) -> CPosition {
|
pub(crate) fn a(d: u32, i: i32, j: i32) -> CPosition {
|
||||||
match d {
|
match d {
|
||||||
@ -51,8 +51,8 @@ mod test {
|
|||||||
let (dim, sign) = d.div_rem(&2);
|
let (dim, sign) = d.div_rem(&2);
|
||||||
let sign = if sign == 0 { 1 } else { -1 };
|
let sign = if sign == 0 { 1 } else { -1 };
|
||||||
// HACK: Our conventin and the MVA are different, since we are trying to strangle fig this, quick hack.
|
// HACK: Our conventin and the MVA are different, since we are trying to strangle fig this, quick hack.
|
||||||
let offset = Pos2D::in_direction(dim, sign);
|
let offset = Position::in_direction(dim, sign);
|
||||||
let next = Pos2D { x: i, y: j } + offset;
|
let next = Position { x: i, y: j } + offset;
|
||||||
|
|
||||||
CPosition(next.x, next.y)
|
CPosition(next.x, next.y)
|
||||||
}
|
}
|
||||||
|
|||||||
47
src/example_systems.rs
Normal file
47
src/example_systems.rs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
use rand::rngs::SmallRng;
|
||||||
|
use rand::{Rng, SeedableRng};
|
||||||
|
use crate::system::grid::{Position, VectorStorage};
|
||||||
|
use crate::system::model::DLASystem;
|
||||||
|
use crate::system::nd::{NDPosition, NDVectorStorage};
|
||||||
|
use crate::system::{GriddedPosition, Storage};
|
||||||
|
use crate::system::walker::{LocalRandomWalker, Walker};
|
||||||
|
|
||||||
|
pub fn execute<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>>(sys: &mut DLASystem<R, P, S, W>, csv_path: &Path) {
|
||||||
|
while sys.running {
|
||||||
|
sys.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
sys.export_data(csv_path)
|
||||||
|
.expect("Failed to write");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn initial_config(seed: u64, max_particles: usize) -> DLASystem<SmallRng, Position, VectorStorage, LocalRandomWalker> {
|
||||||
|
DLASystem::new_g(
|
||||||
|
SmallRng::seed_from_u64(seed),
|
||||||
|
VectorStorage::new(1600),
|
||||||
|
LocalRandomWalker,
|
||||||
|
1.0,
|
||||||
|
max_particles,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stick_probability(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, Position, VectorStorage, LocalRandomWalker> {
|
||||||
|
DLASystem::new_g(
|
||||||
|
SmallRng::seed_from_u64(seed),
|
||||||
|
VectorStorage::new(1600),
|
||||||
|
LocalRandomWalker,
|
||||||
|
stick_probability,
|
||||||
|
max_particles,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn three_dimensional(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, NDPosition<3>, NDVectorStorage<3>, LocalRandomWalker> {
|
||||||
|
DLASystem::new_g(
|
||||||
|
SmallRng::seed_from_u64(seed),
|
||||||
|
NDVectorStorage::new(1600),
|
||||||
|
LocalRandomWalker,
|
||||||
|
stick_probability,
|
||||||
|
max_particles,
|
||||||
|
)
|
||||||
|
}
|
||||||
111
src/main.rs
111
src/main.rs
@ -1,98 +1,51 @@
|
|||||||
#![feature(array_zip)]
|
#![feature(array_zip)]
|
||||||
#![feature(generic_const_exprs)]
|
|
||||||
|
|
||||||
use clap::Parser;
|
use std::path::PathBuf;
|
||||||
use rand::prelude::*;
|
|
||||||
use crate::cli::{drive_system};
|
|
||||||
use crate::cli::cli::{StickProbabilityCli, InitialCli, BallsCli, PCM, ModelCli};
|
|
||||||
use crate::cli::output::write;
|
|
||||||
use crate::system::model::DLASystem;
|
|
||||||
use crate::system::spaces::continuous::{ContinuousSticker, ContinuousStorage, ContinuousWalker};
|
|
||||||
use crate::system::spaces::hexagonal::HexPosition;
|
|
||||||
use crate::system::spaces::square_grid::{Grid2D, Grid3D};
|
|
||||||
use crate::system::spaces::VectorStorage;
|
|
||||||
use crate::system::spawner::UniformSpawner;
|
|
||||||
use crate::system::sticker::{ProbabilisticSticking, SimpleSticking};
|
|
||||||
use crate::system::walker::LocalRandomWalker;
|
|
||||||
|
|
||||||
mod system;
|
mod system;
|
||||||
mod surface_probability_measure;
|
mod example_systems;
|
||||||
mod cli;
|
|
||||||
|
use clap::Parser;
|
||||||
|
use crate::example_systems::{execute, stick_probability, three_dimensional};
|
||||||
|
use crate::system::model::DLASystem;
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
struct Cli {
|
||||||
|
#[arg(short, long, default_value_t = 2)]
|
||||||
|
dim: u32,
|
||||||
|
|
||||||
|
seed: u64,
|
||||||
|
max_particles: usize,
|
||||||
|
stick_probability: f32,
|
||||||
|
csv_path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let cli = ModelCli::parse();
|
let cli = Cli::parse();
|
||||||
|
|
||||||
println!("Running: {:?}", cli);
|
println!("Running: {:?}", cli);
|
||||||
|
|
||||||
match cli.preconfigured_model {
|
match cli.dim {
|
||||||
PCM::Initial(InitialCli { grid_size }) => {
|
2 => {
|
||||||
let mut sys = DLASystem::<_, Grid2D, _, _, _, _>::new(
|
let mut sys = stick_probability(
|
||||||
SmallRng::seed_from_u64(cli.seed),
|
cli.seed,
|
||||||
VectorStorage::new(grid_size, 2),
|
|
||||||
LocalRandomWalker,
|
|
||||||
UniformSpawner,
|
|
||||||
SimpleSticking,
|
|
||||||
cli.max_particles,
|
cli.max_particles,
|
||||||
|
cli.stick_probability,
|
||||||
);
|
);
|
||||||
|
|
||||||
drive_system(&mut sys, cli.max_frames);
|
execute(&mut sys, &cli.csv_path);
|
||||||
write(&sys, cli.format, &cli.output);
|
},
|
||||||
}
|
|
||||||
|
|
||||||
PCM::StickProbability(StickProbabilityCli { grid_size, stick_probability }) => {
|
3 => {
|
||||||
let mut sys = DLASystem::<_, Grid2D, _, _, _, _>::new(
|
let mut sys = three_dimensional(
|
||||||
SmallRng::seed_from_u64(cli.seed),
|
cli.seed,
|
||||||
VectorStorage::new(grid_size, 2),
|
|
||||||
LocalRandomWalker,
|
|
||||||
UniformSpawner,
|
|
||||||
ProbabilisticSticking { stick_probability },
|
|
||||||
cli.max_particles,
|
cli.max_particles,
|
||||||
|
cli.stick_probability,
|
||||||
);
|
);
|
||||||
|
|
||||||
drive_system(&mut sys, cli.max_frames);
|
execute(&mut sys, &cli.csv_path);
|
||||||
write(&sys, cli.format, &cli.output);
|
},
|
||||||
}
|
|
||||||
|
|
||||||
PCM::Grid3(StickProbabilityCli { grid_size, stick_probability }) => {
|
n => panic!("Model not compiled with {n} dimensional support")
|
||||||
let mut sys = DLASystem::<_, Grid3D, _, _, _, _>::new(
|
|
||||||
SmallRng::seed_from_u64(cli.seed),
|
|
||||||
VectorStorage::new(grid_size, 3),
|
|
||||||
LocalRandomWalker,
|
|
||||||
UniformSpawner,
|
|
||||||
ProbabilisticSticking { stick_probability },
|
|
||||||
cli.max_particles,
|
|
||||||
);
|
|
||||||
|
|
||||||
drive_system(&mut sys, cli.max_frames);
|
|
||||||
write(&sys, cli.format, &cli.output);
|
|
||||||
}
|
|
||||||
|
|
||||||
PCM::Hex(StickProbabilityCli { grid_size, stick_probability }) => {
|
|
||||||
let mut sys = DLASystem::<_, HexPosition, _, _, _, _>::new(
|
|
||||||
SmallRng::seed_from_u64(cli.seed),
|
|
||||||
VectorStorage::new(grid_size, 2),
|
|
||||||
LocalRandomWalker,
|
|
||||||
UniformSpawner,
|
|
||||||
ProbabilisticSticking { stick_probability },
|
|
||||||
cli.max_particles,
|
|
||||||
);
|
|
||||||
|
|
||||||
drive_system(&mut sys, cli.max_frames);
|
|
||||||
write(&sys, cli.format, &cli.output);
|
|
||||||
}
|
|
||||||
|
|
||||||
PCM::Balls(BallsCli { ball_radius, stick_distance, walk_step }) => {
|
|
||||||
let mut sys = DLASystem::new(
|
|
||||||
SmallRng::seed_from_u64(cli.seed),
|
|
||||||
ContinuousStorage { inner: kiddo::KdTree::new(), ball_radius_sq: ball_radius * ball_radius },
|
|
||||||
ContinuousWalker { walk_step },
|
|
||||||
UniformSpawner,
|
|
||||||
ContinuousSticker { range_sq: stick_distance * stick_distance },
|
|
||||||
cli.max_particles,
|
|
||||||
);
|
|
||||||
|
|
||||||
drive_system(&mut sys, cli.max_frames);
|
|
||||||
write(&sys, cli.format, &cli.output);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,54 +0,0 @@
|
|||||||
use std::fs::File;
|
|
||||||
use std::path::Path;
|
|
||||||
use bevy::render::render_resource::AsBindGroupShaderType;
|
|
||||||
use kd_tree::KdTree;
|
|
||||||
use rand::rngs::SmallRng;
|
|
||||||
use rand::{Rng, SeedableRng};
|
|
||||||
use crate::system::model::DLASystem;
|
|
||||||
use crate::system::{Position, Storage};
|
|
||||||
use crate::system::spaces::continuous::{ContinuousSticker, ContinuousStorage, ContinuousWalker, P3};
|
|
||||||
use crate::system::spaces::hexagonal::HexPosition;
|
|
||||||
use crate::system::spaces::square_grid::{Grid2D, Grid3D};
|
|
||||||
use crate::system::spaces::VectorStorage;
|
|
||||||
use crate::system::spawner::{Spawner, UniformSpawner};
|
|
||||||
use crate::system::sticker::{ProbabilisticSticking, SimpleSticking, Sticker};
|
|
||||||
use crate::system::walker::{LocalRandomWalker, Walker};
|
|
||||||
|
|
||||||
struct LoggerSticker {
|
|
||||||
inner: SimpleSticking
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S: Storage<Grid2D>> Sticker<Grid2D, S> for LoggerSticker {
|
|
||||||
fn should_stick<R: Rng>(&self, rng: &mut R, space: &S, position: &Grid2D) -> bool {
|
|
||||||
if self.inner.should_stick(rng, space, position) {
|
|
||||||
println!("{:?}", position);
|
|
||||||
}
|
|
||||||
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ReadOnlyVectorStorage {
|
|
||||||
inner: VectorStorage,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Storage<Grid2D> for ReadOnlyVectorStorage {
|
|
||||||
fn is_occupied(&self, position: &Grid2D) -> bool {
|
|
||||||
self.inner.is_occupied(position)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deposit(&mut self, position: &Grid2D) {
|
|
||||||
eprintln!("Write ignore for space at {position:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn surface_probability_measure(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, HexPosition, VectorStorage, LocalRandomWalker, UniformSpawner, ProbabilisticSticking> {
|
|
||||||
DLASystem::new(
|
|
||||||
SmallRng::seed_from_u64(seed),
|
|
||||||
VectorStorage::new(1600, 3),
|
|
||||||
LocalRandomWalker,
|
|
||||||
UniformSpawner,
|
|
||||||
ProbabilisticSticking { stick_probability },
|
|
||||||
max_particles,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
92
src/system/grid.rs
Normal file
92
src/system/grid.rs
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
use std::f32::consts::PI;
|
||||||
|
use std::ops::Add;
|
||||||
|
use num_integer::Integer;
|
||||||
|
use rand::Rng;
|
||||||
|
use crate::system::{GriddedPosition, Storage};
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
pub struct VectorStorage {
|
||||||
|
backing: Vec<bool>,
|
||||||
|
grid_size: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub struct Position {
|
||||||
|
pub x: i32,
|
||||||
|
pub y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Position {
|
||||||
|
pub fn in_direction(direction: u32, value: i32) -> Self {
|
||||||
|
if direction == 0 { Position { x: value, y: 0 } } else { Position { x: 0, y: value } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add for Position {
|
||||||
|
type Output = Position;
|
||||||
|
|
||||||
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
|
Position { x: self.x + rhs.x, y: self.y + rhs.y }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GriddedPosition for Position {
|
||||||
|
const NEIGHBOURS: u32 = 4;
|
||||||
|
|
||||||
|
fn zero() -> Position {
|
||||||
|
Position { x: 0, y: 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn<R: Rng>(rng: &mut R, radius: f32) -> Self {
|
||||||
|
let theta = rng.gen_range(0f32..1.0) * 2.0 * PI;
|
||||||
|
let (x, y) = (radius * theta.cos(), radius * theta.sin());
|
||||||
|
Position { x: x as i32, y: y as i32 }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn abs(&self) -> f32 {
|
||||||
|
((self.x.pow(2) + self.y.pow(2)) as f32).powf(0.5)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn neighbour(&self, neighbour_index: u32) -> Self {
|
||||||
|
let (dim, sign) = neighbour_index.div_rem(&2);
|
||||||
|
let sign = if sign == 0 { 1 } else { -1 };
|
||||||
|
let offset = Position::in_direction(dim, sign);
|
||||||
|
|
||||||
|
self.clone() + offset
|
||||||
|
}
|
||||||
|
|
||||||
|
fn linear_index(&self, grid_size: u32) -> usize {
|
||||||
|
let grid_size = grid_size as i32;
|
||||||
|
|
||||||
|
assert!(self.x <= grid_size && -(grid_size) <= self.x);
|
||||||
|
assert!(self.y <= grid_size && -(grid_size) <= self.y);
|
||||||
|
|
||||||
|
let x = (self.x + (grid_size) / 2) as usize;
|
||||||
|
let y = (self.y + (grid_size) / 2) as usize;
|
||||||
|
|
||||||
|
return grid_size as usize * y + x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VectorStorage {
|
||||||
|
pub fn new(grid_size: u32) -> VectorStorage {
|
||||||
|
VectorStorage { grid_size, backing: vec![false; grid_size.pow(2) as usize] }
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Convenience function for c-binding
|
||||||
|
* */
|
||||||
|
pub fn write(&mut self, position: &Position, val: bool) {
|
||||||
|
self.backing[position.linear_index(self.grid_size)] = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Storage<Position> for VectorStorage {
|
||||||
|
fn at(&self, position: &Position) -> bool {
|
||||||
|
return self.backing[position.linear_index(self.grid_size)]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deposit(&mut self, position: &Position) {
|
||||||
|
self.backing[position.linear_index(self.grid_size)] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
use crate::system::{GriddedPosition, Position};
|
use rand::Rng;
|
||||||
|
use crate::system::GriddedPosition;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
@ -19,6 +20,18 @@ impl Add for HexPosition {
|
|||||||
impl GriddedPosition for HexPosition {
|
impl GriddedPosition for HexPosition {
|
||||||
const NEIGHBOURS: u32 = 6;
|
const NEIGHBOURS: u32 = 6;
|
||||||
|
|
||||||
|
fn zero() -> Self {
|
||||||
|
HexPosition { q: 0, r: 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn<R: Rng>(rng: &mut R, radius: f32) -> Self {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn abs(&self) -> f32 {
|
||||||
|
((self.q.pow(2) + self.r.pow(2) + self.q * self.r) as f32).sqrt()
|
||||||
|
}
|
||||||
|
|
||||||
fn neighbour(&self, neighbour_index: u32) -> Self {
|
fn neighbour(&self, neighbour_index: u32) -> Self {
|
||||||
let neighbour_index = neighbour_index as usize;
|
let neighbour_index = neighbour_index as usize;
|
||||||
|
|
||||||
@ -31,28 +44,7 @@ impl GriddedPosition for HexPosition {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn linear_index(&self, grid_size: u32) -> usize {
|
fn linear_index(&self, grid_size: u32) -> usize {
|
||||||
let q = (self.q + (grid_size as i32 / 2)) as usize;
|
todo!()
|
||||||
let r = (self.r + (grid_size as i32 / 2)) as usize;
|
// ((self.q + grid_size / 2) + grid_size * self.r) as usize
|
||||||
|
|
||||||
r * (grid_size as usize) + q
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Position for HexPosition {
|
|
||||||
const DIM: usize = 2;
|
|
||||||
|
|
||||||
fn zero() -> Self {
|
|
||||||
HexPosition { q: 0, r: 0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn abs(&self) -> f32 {
|
|
||||||
((self.q.pow(2) + self.r.pow(2) + self.q * self.r) as f32).sqrt()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_cartesian(cartesian: [f32; Self::DIM]) -> Self {
|
|
||||||
let q = (1.0f32/3.0f32).sqrt() * cartesian[0] - 1.0/3.0 * cartesian[1];
|
|
||||||
let r = 2.0/3.0 * cartesian[1];
|
|
||||||
|
|
||||||
Self { q: q as i32, r: r as i32 }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,28 +1,24 @@
|
|||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
|
use rand::Rng;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
pub mod walker;
|
pub mod walker;
|
||||||
pub mod spawner;
|
pub mod grid;
|
||||||
pub mod sticker;
|
|
||||||
pub mod model;
|
pub mod model;
|
||||||
pub mod spaces;
|
pub mod nd;
|
||||||
|
mod hexagonal;
|
||||||
|
|
||||||
pub trait Position: Add<Output=Self> + Serialize + Clone {
|
pub trait GriddedPosition: Add<Output=Self> + Serialize + Clone {
|
||||||
const DIM: usize;
|
|
||||||
|
|
||||||
fn zero() -> Self;
|
|
||||||
fn abs(&self) -> f32;
|
|
||||||
fn from_cartesian(cartesian: [f32; Self::DIM]) -> Self;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait GriddedPosition: Position {
|
|
||||||
const NEIGHBOURS: u32;
|
const NEIGHBOURS: u32;
|
||||||
|
|
||||||
|
fn zero() -> Self;
|
||||||
|
fn spawn<R: Rng>(rng: &mut R, radius: f32) -> Self;
|
||||||
|
fn abs(&self) -> f32;
|
||||||
fn neighbour(&self, neighbour_index: u32) -> Self;
|
fn neighbour(&self, neighbour_index: u32) -> Self;
|
||||||
fn linear_index(&self, grid_size: u32) -> usize;
|
fn linear_index(&self, grid_size: u32) -> usize;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Storage<P: Position> {
|
pub trait Storage<P: GriddedPosition> {
|
||||||
fn is_occupied(&self, position: &P) -> bool;
|
fn at(&self, position: &P) -> bool;
|
||||||
fn deposit(&mut self, position: &P);
|
fn deposit(&mut self, position: &P);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,44 +1,19 @@
|
|||||||
|
use std::io;
|
||||||
|
use std::path::Path;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use serde::Serialize;
|
use crate::system::{GriddedPosition, Storage};
|
||||||
use crate::system::{Position, Storage};
|
|
||||||
use crate::system::spawner::Spawner;
|
|
||||||
use crate::system::sticker::Sticker;
|
|
||||||
use crate::system::walker::Walker;
|
use crate::system::walker::Walker;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
pub struct DLASystem<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> {
|
||||||
pub struct HistoryLine<P: Position> {
|
|
||||||
pub frame: usize,
|
|
||||||
pub cluster_radius: f32,
|
|
||||||
pub fd: f32,
|
|
||||||
pub position: P,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct DLASystem<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P, S>> {
|
|
||||||
rng: R,
|
rng: R,
|
||||||
|
|
||||||
/*
|
|
||||||
* These object encapsulate the behaviour of our particular model.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* The space, along with the chosen position choose the embedding space that the cluster grows
|
|
||||||
* in.
|
|
||||||
*/
|
|
||||||
space: S,
|
space: S,
|
||||||
|
|
||||||
/*
|
|
||||||
* Walkers allow us to choose between different particle movement behaviours, eg random or
|
|
||||||
* biased walker
|
|
||||||
*/
|
|
||||||
walker: W,
|
walker: W,
|
||||||
spawner: Sp,
|
|
||||||
sticker: St,
|
|
||||||
|
|
||||||
pub frame: usize,
|
|
||||||
pub running: bool,
|
|
||||||
pub history: Vec<HistoryLine<P>>,
|
|
||||||
|
|
||||||
|
stick_probability: f32,
|
||||||
max_particles: usize,
|
max_particles: usize,
|
||||||
|
|
||||||
|
pub running: bool,
|
||||||
|
|
||||||
particles: Vec<P>,
|
particles: Vec<P>,
|
||||||
active_particle: Option<P>,
|
active_particle: Option<P>,
|
||||||
|
|
||||||
@ -49,21 +24,17 @@ pub struct DLASystem<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawn
|
|||||||
cluster_radius: f32,
|
cluster_radius: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P, S>> DLASystem<R, P, S, W, Sp, St> {
|
impl<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> DLASystem<R, P, S, W> {
|
||||||
pub fn new(rng: R, space: S, walker: W, spawner: Sp, sticker: St, max_particles: usize) -> Self {
|
pub fn new_g(rng: R, space: S, walker: W, stick_probability: f32, max_particles: usize) -> Self {
|
||||||
let mut sys = DLASystem {
|
let mut sys = DLASystem {
|
||||||
rng,
|
rng,
|
||||||
|
stick_probability,
|
||||||
max_particles,
|
max_particles,
|
||||||
running: true,
|
running: true,
|
||||||
spawner,
|
|
||||||
sticker,
|
|
||||||
|
|
||||||
space,
|
space,
|
||||||
walker,
|
walker,
|
||||||
|
|
||||||
frame: 0,
|
|
||||||
particles: vec![],
|
particles: vec![],
|
||||||
history: vec![],
|
|
||||||
active_particle: None,
|
active_particle: None,
|
||||||
|
|
||||||
add_ratio: 1.2,
|
add_ratio: 1.2,
|
||||||
@ -80,7 +51,6 @@ impl<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Stick
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self) {
|
pub fn update(&mut self) {
|
||||||
self.frame += 1;
|
|
||||||
if self.active_particle.is_some() {
|
if self.active_particle.is_some() {
|
||||||
self.move_particle();
|
self.move_particle();
|
||||||
} else if self.particles.len() < self.max_particles {
|
} else if self.particles.len() < self.max_particles {
|
||||||
@ -101,8 +71,8 @@ impl<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Stick
|
|||||||
|
|
||||||
if distance > self.kill_circle {
|
if distance > self.kill_circle {
|
||||||
self.active_particle = None;
|
self.active_particle = None;
|
||||||
} else if !self.space.is_occupied(&next_position) {
|
} else if !self.space.at(&next_position) {
|
||||||
if self.sticker.should_stick(&mut self.rng, &self.space, &next_position) {
|
if self.check_stick(&next_position) {
|
||||||
self.deposit(&next_position);
|
self.deposit(&next_position);
|
||||||
self.active_particle = None;
|
self.active_particle = None;
|
||||||
return;
|
return;
|
||||||
@ -112,10 +82,19 @@ impl<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Stick
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_particle(&mut self) {
|
fn check_stick(&mut self, position: &P) -> bool {
|
||||||
let position = self.spawner.spawn(&mut self.rng, self.add_circle);
|
(0..P::NEIGHBOURS)
|
||||||
|
.map(|n| position.neighbour(n))
|
||||||
|
.any(|neighbour|
|
||||||
|
self.space.at(&neighbour)
|
||||||
|
&& self.rng.gen_range(0.0f32..=1.0) < self.stick_probability
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if !self.space.is_occupied(&position) {
|
fn spawn_particle(&mut self) {
|
||||||
|
let position = P::spawn(&mut self.rng, self.add_circle);
|
||||||
|
|
||||||
|
if !self.space.at(&position) {
|
||||||
self.active_particle = Some(position);
|
self.active_particle = Some(position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -134,7 +113,16 @@ impl<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Stick
|
|||||||
self.kill_circle = self.kill_ratio * self.add_circle;
|
self.kill_circle = self.kill_ratio * self.add_circle;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self.history.push(HistoryLine { frame: self.frame, position: p0.clone(), cluster_radius: self.cluster_radius, fd: (self.particles.len() as f32).ln() / self.cluster_radius.ln() });
|
pub fn export_data(&self, path: &Path) -> io::Result<()> {
|
||||||
|
let mut wtr = csv::Writer::from_path(path)?;
|
||||||
|
|
||||||
|
for particle in &self.particles {
|
||||||
|
wtr.serialize(particle)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
wtr.flush()?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
107
src/system/nd.rs
Normal file
107
src/system/nd.rs
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
use std::ops::Add;
|
||||||
|
use num_integer::Integer;
|
||||||
|
use rand::Rng;
|
||||||
|
use serde::{Serialize, Serializer};
|
||||||
|
use serde::ser::{SerializeMap, SerializeSeq, SerializeStruct};
|
||||||
|
use crate::system::GriddedPosition;
|
||||||
|
use crate::system::Storage;
|
||||||
|
|
||||||
|
pub struct NDVectorStorage<const DIM: usize> {
|
||||||
|
backing: Vec<bool>,
|
||||||
|
grid_size: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const DIM: usize> NDVectorStorage<DIM> {
|
||||||
|
pub fn new(grid_size: u32) -> Self {
|
||||||
|
Self { grid_size, backing: vec![false; grid_size.pow(DIM as u32) as usize] }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const DIM: usize> Storage<NDPosition<DIM>> for NDVectorStorage<DIM> {
|
||||||
|
fn at(&self, position: &NDPosition<DIM>) -> bool {
|
||||||
|
return self.backing[position.linear_index(self.grid_size)];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deposit(&mut self, position: &NDPosition<DIM>) {
|
||||||
|
self.backing[position.linear_index(self.grid_size)] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub struct NDPosition<const DIM: usize>([i32; DIM]);
|
||||||
|
|
||||||
|
impl<const DIM: usize> NDPosition<DIM> {
|
||||||
|
pub fn in_direction(direction: usize, value: i32) -> Self {
|
||||||
|
let mut arr = [0; DIM];
|
||||||
|
arr[direction] = value;
|
||||||
|
NDPosition(arr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const DIM: usize> Add for NDPosition<DIM> {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
|
Self(self.0.zip(rhs.0).map(|(a, b)| a + b))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const DIM: usize> Serialize for NDPosition<DIM> {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
|
||||||
|
let mut ser = serializer.serialize_struct("NDPosition", DIM)?;
|
||||||
|
for (i, v) in self.0.iter().enumerate() {
|
||||||
|
ser.serialize_field(Box::leak(Box::new(format!("r{i}"))), v)?;
|
||||||
|
}
|
||||||
|
ser.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const DIM: usize> GriddedPosition for NDPosition<DIM> {
|
||||||
|
const NEIGHBOURS: u32 = { 2u32.pow(DIM as u32) } as u32;
|
||||||
|
|
||||||
|
fn zero() -> Self {
|
||||||
|
NDPosition([0; DIM])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn<R: Rng>(rng: &mut R, radius: f32) -> Self {
|
||||||
|
let mut a: [f32; DIM] = [0f32; DIM];
|
||||||
|
let mut b: [i32; DIM] = [0i32; DIM];
|
||||||
|
|
||||||
|
for i in 0..DIM {
|
||||||
|
a[i] = rng.gen_range(0f32..1f32);
|
||||||
|
}
|
||||||
|
|
||||||
|
let norm = a.iter().sum::<f32>()
|
||||||
|
.sqrt();
|
||||||
|
|
||||||
|
for i in 0..DIM {
|
||||||
|
a[i] = a[i] * radius / norm;
|
||||||
|
b[i] = a[i] as i32;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Self(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn abs(&self) -> f32 {
|
||||||
|
let a: i32 = self.0.iter()
|
||||||
|
.map(|r| r.pow(2))
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
(a as f32).powf(0.5)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn neighbour(&self, neighbour_index: u32) -> Self {
|
||||||
|
let (dim, sign) = neighbour_index.div_rem(&(DIM as u32));
|
||||||
|
let sign = if sign == 0 { 1 } else { -1 };
|
||||||
|
let offset = Self::in_direction(dim as usize, sign);
|
||||||
|
|
||||||
|
self.clone() + offset
|
||||||
|
}
|
||||||
|
|
||||||
|
fn linear_index(&self, grid_size: u32) -> usize {
|
||||||
|
self.0.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, v)| (grid_size.pow(i as u32) as usize) * (v + ((grid_size / 2) as i32)) as usize)
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,102 +0,0 @@
|
|||||||
use std::f32::consts::PI;
|
|
||||||
use std::ops::Add;
|
|
||||||
use kiddo::distance::squared_euclidean;
|
|
||||||
use rand::Rng;
|
|
||||||
use serde::Serialize;
|
|
||||||
use crate::system::sticker::Sticker;
|
|
||||||
use crate::system::{Position, Storage};
|
|
||||||
use crate::system::walker::Walker;
|
|
||||||
|
|
||||||
#[derive(Serialize, Debug, Clone)]
|
|
||||||
pub struct P3 {
|
|
||||||
x: f32,
|
|
||||||
y: f32,
|
|
||||||
z: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl P3 {
|
|
||||||
fn as_arr(&self) -> [f32; 3] {
|
|
||||||
[self.x, self.y, self.z]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn random_with_radius<R: Rng>(rng: &mut R, radius: f32) -> P3 {
|
|
||||||
let theta = rng.gen_range(0f32..1.0) * 2.0 * PI;
|
|
||||||
let phi = rng.gen_range(0f32..1.0) * 2.0 * PI;
|
|
||||||
|
|
||||||
let (x, y, z) = (
|
|
||||||
radius * theta.sin() * phi.cos(),
|
|
||||||
radius * theta.sin() * phi.sin(),
|
|
||||||
radius * theta.cos()
|
|
||||||
);
|
|
||||||
|
|
||||||
P3 { x, y, z}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Add for P3 {
|
|
||||||
type Output = P3;
|
|
||||||
|
|
||||||
fn add(self, rhs: Self) -> Self::Output {
|
|
||||||
P3 {
|
|
||||||
x: self.x + rhs.x,
|
|
||||||
y: self.y + rhs.y,
|
|
||||||
z: self.z + rhs.z,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ContinuousStorage {
|
|
||||||
pub inner: kiddo::KdTree<f32, (), 3>,
|
|
||||||
pub ball_radius_sq: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Position for P3 {
|
|
||||||
const DIM: usize = 3;
|
|
||||||
|
|
||||||
fn zero() -> Self {
|
|
||||||
P3 { x: 0f32, y: 0f32, z: 0f32 }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn abs(&self) -> f32 {
|
|
||||||
(self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).powf(0.5)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_cartesian(cartesian: [f32; Self::DIM]) -> Self {
|
|
||||||
P3 { x: cartesian[0], y: cartesian[1], z: cartesian[3] }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Storage<P3> for ContinuousStorage {
|
|
||||||
fn is_occupied(&self, position: &P3) -> bool {
|
|
||||||
let (dist_sq, _) = self.inner.nearest_one(&position.as_arr(), &squared_euclidean).unwrap();
|
|
||||||
|
|
||||||
// Is the distance of this point to the next one less than twice the ball radius
|
|
||||||
dist_sq < 2.0 * self.ball_radius_sq
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deposit(&mut self, position: &P3) {
|
|
||||||
self.inner.add(&position.as_arr(), ()).expect("Failed to write to space")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ContinuousSticker {
|
|
||||||
/// INVARIANT: THIS SHOULD BE GREATER THAN THE BALL_RADIUS_SQ value
|
|
||||||
pub range_sq: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sticker<P3, ContinuousStorage> for ContinuousSticker {
|
|
||||||
fn should_stick<R: Rng>(&self, _rng: &mut R, space: &ContinuousStorage, position: &P3) -> bool {
|
|
||||||
let (a, _) = space.inner.nearest_one(&position.as_arr(), &squared_euclidean).unwrap();
|
|
||||||
a < self.range_sq
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ContinuousWalker {
|
|
||||||
pub walk_step: f32
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Walker<P3> for ContinuousWalker {
|
|
||||||
fn walk<R: Rng>(&self, rng: &mut R, position: &P3) -> P3 {
|
|
||||||
position.clone() + P3::random_with_radius(rng, self.walk_step)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
pub mod vector_storage;
|
|
||||||
pub use vector_storage::VectorStorage;
|
|
||||||
|
|
||||||
pub mod square_grid;
|
|
||||||
pub mod hexagonal;
|
|
||||||
|
|
||||||
pub mod continuous;
|
|
||||||
@ -1,158 +0,0 @@
|
|||||||
use std::ops::Add;
|
|
||||||
use num_integer::Integer;
|
|
||||||
use crate::system::{GriddedPosition, Position};
|
|
||||||
use serde::{Serialize, Deserialize};
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
||||||
pub struct Grid2D {
|
|
||||||
pub x: i32,
|
|
||||||
pub y: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Grid2D {
|
|
||||||
pub fn in_direction(direction: u32, value: i32) -> Self {
|
|
||||||
match direction {
|
|
||||||
0 => Grid2D { x: value, y: 0 },
|
|
||||||
1 => Grid2D { x: 0, y: value },
|
|
||||||
_ => panic!("Invalid direction"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Add for Grid2D {
|
|
||||||
type Output = Grid2D;
|
|
||||||
|
|
||||||
fn add(self, rhs: Self) -> Self::Output {
|
|
||||||
Grid2D { x: self.x + rhs.x, y: self.y + rhs.y }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Position for Grid2D {
|
|
||||||
const DIM: usize = 2;
|
|
||||||
|
|
||||||
fn zero() -> Self {
|
|
||||||
Grid2D { x: 0, y: 0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn abs(&self) -> f32 {
|
|
||||||
(((self.x * self.x) + (self.y * self.y)) as f32).powf(0.5)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_cartesian(cartesian: [f32; Self::DIM]) -> Self {
|
|
||||||
Grid2D {
|
|
||||||
x: cartesian[0] as i32,
|
|
||||||
y: cartesian[1] as i32,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GriddedPosition for Grid2D {
|
|
||||||
const NEIGHBOURS: u32 = 4;
|
|
||||||
|
|
||||||
fn neighbour(&self, neighbour_index: u32) -> Self {
|
|
||||||
let (dim, sign) = neighbour_index.div_rem(&2);
|
|
||||||
let sign = if sign == 0 { 1 } else { -1 };
|
|
||||||
let offset = Self::in_direction(dim, sign);
|
|
||||||
|
|
||||||
self.clone() + offset
|
|
||||||
}
|
|
||||||
|
|
||||||
fn linear_index(&self, grid_size: u32) -> usize {
|
|
||||||
let grid_size = grid_size as i32;
|
|
||||||
|
|
||||||
assert!(self.x < grid_size && -(grid_size) < self.x);
|
|
||||||
assert!(self.y < grid_size && -(grid_size) < self.y);
|
|
||||||
|
|
||||||
let x = (self.x + (grid_size) / 2) as usize;
|
|
||||||
let y = (self.y + (grid_size) / 2) as usize;
|
|
||||||
|
|
||||||
let linear_index = grid_size as usize * y + x;
|
|
||||||
|
|
||||||
if linear_index >= (grid_size * grid_size) as usize {
|
|
||||||
eprintln!("AHHH SOMETHING WENT WRONG {:?} gives {}", self, linear_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
return linear_index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
||||||
pub struct Grid3D {
|
|
||||||
pub x: i32,
|
|
||||||
pub y: i32,
|
|
||||||
pub z: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Grid3D {
|
|
||||||
pub fn in_direction(direction: u32, value: i32) -> Self {
|
|
||||||
match direction {
|
|
||||||
0 => Grid3D { x: value, y: 0, z: 0 },
|
|
||||||
1 => Grid3D { x: 0, y: value, z: 0 },
|
|
||||||
2 => Grid3D { x: 0, y: 0, z: value },
|
|
||||||
_ => panic!("Invalid direction"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Add for Grid3D {
|
|
||||||
type Output = Grid3D;
|
|
||||||
|
|
||||||
fn add(self, rhs: Self) -> Self::Output {
|
|
||||||
Grid3D { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Position for Grid3D {
|
|
||||||
const DIM: usize = 3;
|
|
||||||
|
|
||||||
fn zero() -> Self {
|
|
||||||
Grid3D { x: 0, y: 0, z: 0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn abs(&self) -> f32 {
|
|
||||||
(((self.x * self.x) + (self.y * self.y) + (self.z * self.z)) as f32).powf(0.5)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_cartesian(cartesian: [f32; Self::DIM]) -> Self {
|
|
||||||
Self {
|
|
||||||
x: cartesian[0] as i32,
|
|
||||||
y: cartesian[1] as i32,
|
|
||||||
z: cartesian[2] as i32,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GriddedPosition for Grid3D {
|
|
||||||
const NEIGHBOURS: u32 = 6;
|
|
||||||
|
|
||||||
fn neighbour(&self, neighbour_index: u32) -> Self {
|
|
||||||
let (dim, sign) = neighbour_index.div_rem(&3);
|
|
||||||
let sign = if sign == 0 { 1 } else { -1 };
|
|
||||||
let offset = Self::in_direction(dim, sign);
|
|
||||||
|
|
||||||
self.clone() + offset
|
|
||||||
}
|
|
||||||
|
|
||||||
fn linear_index(&self, grid_size: u32) -> usize {
|
|
||||||
let grid_size = grid_size as i32;
|
|
||||||
|
|
||||||
assert!(self.x < grid_size && -(grid_size) < self.x);
|
|
||||||
assert!(self.y < grid_size && -(grid_size) < self.y);
|
|
||||||
assert!(self.z < grid_size && -(grid_size) < self.z);
|
|
||||||
|
|
||||||
let x = (self.x + (grid_size) / 2) as usize;
|
|
||||||
let y = (self.y + (grid_size) / 2) as usize;
|
|
||||||
let z = (self.z + (grid_size) / 2) as usize;
|
|
||||||
|
|
||||||
let linear_index =
|
|
||||||
(grid_size as usize * grid_size as usize) * z
|
|
||||||
+ (grid_size as usize) * y
|
|
||||||
+ x;
|
|
||||||
|
|
||||||
if linear_index >= (grid_size * grid_size * grid_size) as usize {
|
|
||||||
eprintln!("AHHH SOMETHING WENT WRONG {:?} gives {}", self, linear_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
return linear_index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,23 +0,0 @@
|
|||||||
use crate::system::{GriddedPosition, Storage};
|
|
||||||
|
|
||||||
pub struct VectorStorage {
|
|
||||||
backing: Vec<bool>,
|
|
||||||
grid_size: u32,
|
|
||||||
dimension: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl VectorStorage {
|
|
||||||
pub fn new(grid_size: u32, dimension: u32) -> VectorStorage {
|
|
||||||
VectorStorage { grid_size, dimension, backing: vec![false; grid_size.pow(dimension) as usize] }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<P: GriddedPosition> Storage<P> for VectorStorage {
|
|
||||||
fn is_occupied(&self, position: &P) -> bool {
|
|
||||||
return self.backing[position.linear_index(self.grid_size)];
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deposit(&mut self, position: &P) {
|
|
||||||
self.backing[position.linear_index(self.grid_size)] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,51 +0,0 @@
|
|||||||
use std::f32::consts::PI;
|
|
||||||
use rand::Rng;
|
|
||||||
use crate::system::Position;
|
|
||||||
use crate::system::spaces::continuous::P3;
|
|
||||||
use crate::system::spaces::hexagonal::HexPosition;
|
|
||||||
use crate::system::spaces::square_grid::{Grid2D, Grid3D};
|
|
||||||
|
|
||||||
pub trait Spawner<P: Position> {
|
|
||||||
fn spawn<R: Rng>(&self, rng: &mut R, radius: f32) -> P;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct UniformSpawner;
|
|
||||||
|
|
||||||
impl Spawner<Grid2D> for UniformSpawner {
|
|
||||||
fn spawn<R: Rng>(&self, rng: &mut R, radius: f32) -> Grid2D {
|
|
||||||
let theta = rng.gen_range(0f32..1.0) * 2.0 * PI;
|
|
||||||
let (x, y) = (radius * theta.cos(), radius * theta.sin());
|
|
||||||
|
|
||||||
Grid2D::from_cartesian([x, y])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Spawner<HexPosition> for UniformSpawner {
|
|
||||||
fn spawn<R: Rng>(&self, rng: &mut R, radius: f32) -> HexPosition {
|
|
||||||
let theta = rng.gen_range(0f32..1.0) * 2.0 * PI;
|
|
||||||
let (x, y) = (radius * theta.cos(), radius * theta.sin());
|
|
||||||
|
|
||||||
HexPosition::from_cartesian([x, y])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Spawner<Grid3D> for UniformSpawner {
|
|
||||||
fn spawn<R: Rng>(&self, rng: &mut R, radius: f32) -> Grid3D {
|
|
||||||
let theta = rng.gen_range(0f32..1.0) * 2.0 * PI;
|
|
||||||
let phi = rng.gen_range(0f32..1.0) * 2.0 * PI;
|
|
||||||
|
|
||||||
let (x, y, z) = (
|
|
||||||
radius * theta.sin() * phi.cos(),
|
|
||||||
radius * theta.sin() * phi.sin(),
|
|
||||||
radius * theta.cos()
|
|
||||||
);
|
|
||||||
|
|
||||||
Grid3D::from_cartesian([x, y, z])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Spawner<P3> for UniformSpawner {
|
|
||||||
fn spawn<R: Rng>(&self, rng: &mut R, radius: f32) -> P3 {
|
|
||||||
P3::random_with_radius(rng, radius)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,29 +0,0 @@
|
|||||||
use rand::Rng;
|
|
||||||
use crate::system::{GriddedPosition, Position, Storage};
|
|
||||||
|
|
||||||
pub trait Sticker<P: Position, S: Storage<P>> {
|
|
||||||
fn should_stick<R: Rng>(&self, rng: &mut R, space: &S, position: &P) -> bool;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SimpleSticking;
|
|
||||||
|
|
||||||
pub struct ProbabilisticSticking {
|
|
||||||
pub stick_probability: f32
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<P: GriddedPosition, S: Storage<P>> Sticker<P, S> for SimpleSticking {
|
|
||||||
fn should_stick<R: Rng>(&self, _rng: &mut R, space: &S, position: &P) -> bool {
|
|
||||||
(0..P::NEIGHBOURS)
|
|
||||||
.map(|n| position.neighbour(n))
|
|
||||||
.any(|neighbour| space.is_occupied(&neighbour))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<P: GriddedPosition, S: Storage<P>> Sticker<P, S> for ProbabilisticSticking {
|
|
||||||
fn should_stick<R: Rng>(&self, rng: &mut R, space: &S, position: &P) -> bool {
|
|
||||||
(0..P::NEIGHBOURS)
|
|
||||||
.map(|n| position.neighbour(n))
|
|
||||||
.any(|neighbour| space.is_occupied(&neighbour) && rng.gen_range(0.0f32..=1.0) < self.stick_probability)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ -1,32 +1,31 @@
|
|||||||
use rand::prelude::Rng;
|
use rand::prelude::Rng;
|
||||||
use crate::system::{GriddedPosition, Position};
|
use crate::system::GriddedPosition;
|
||||||
|
|
||||||
pub trait Walker<P: Position> {
|
pub trait Walker<P: GriddedPosition> {
|
||||||
fn walk<R: Rng>(&self, rng: &mut R, position: &P) -> P;
|
fn walk<R: Rng>(&self, rng: &mut R, position: &P) -> P;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct LocalRandomWalker;
|
pub struct LocalRandomWalker;
|
||||||
|
|
||||||
impl<P: GriddedPosition> Walker<P> for LocalRandomWalker {
|
impl<Position: GriddedPosition> Walker<Position> for LocalRandomWalker {
|
||||||
fn walk<R: Rng>(&self, rng: &mut R, position: &P) -> P {
|
fn walk<R: Rng>(&self, rng: &mut R, position: &Position) -> Position {
|
||||||
position.neighbour(rng.gen_range(0u32..P::NEIGHBOURS))
|
position.neighbour(rng.gen_range(0u32..Position::NEIGHBOURS))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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::{GriddedPosition, Position};
|
use crate::system::{GriddedPosition, grid::Position};
|
||||||
use crate::system::spaces::square_grid::Grid2D;
|
|
||||||
use crate::system::walker::{LocalRandomWalker, Walker};
|
use crate::system::walker::{LocalRandomWalker, Walker};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn uniformity() {
|
fn uniformity() {
|
||||||
let walker = LocalRandomWalker;
|
let walker = LocalRandomWalker;
|
||||||
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
|
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
|
||||||
let mut results: Vec<Grid2D> = vec![];
|
let mut results: Vec<Position> = vec![];
|
||||||
|
|
||||||
let origin = &Grid2D::zero();
|
let origin = &Position::zero();
|
||||||
|
|
||||||
let x: u32 = (1_000_000);
|
let x: u32 = (1_000_000);
|
||||||
for i in 0..x {
|
for i in 0..x {
|
||||||
@ -35,22 +34,22 @@ mod test {
|
|||||||
|
|
||||||
let a = results
|
let a = results
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|a| **a == Grid2D { x: 0, y: 1 })
|
.filter(|a| **a == Position { x: 0, y: 1 })
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
let b = results
|
let b = results
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|a| **a == Grid2D { x: 0, y: -1 })
|
.filter(|a| **a == Position { x: 0, y: -1 })
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
let c = results
|
let c = results
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|a| **a == Grid2D { x: 1, y: 0 })
|
.filter(|a| **a == Position { x: 1, y: 0 })
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
let d = results
|
let d = results
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|a| **a == Grid2D { x: -1, y: 0 })
|
.filter(|a| **a == Position { x: -1, y: 0 })
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
println!("{} {} {} {}", a as f32 / x as f32, b as f32 / x as f32, c as f32 / x as f32, d as f32 / x as f32);
|
println!("{} {} {} {}", a as f32 / x as f32, b as f32 / x as f32, c as f32 / x as f32, d as f32 / x as f32);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user