Achieve the final result of entirely generic!!

This commit is contained in:
Joshua Coles 2023-03-05 10:59:08 +00:00
parent 1f5c94cb82
commit 15c2edde67
12 changed files with 219 additions and 131 deletions

View File

@ -1,7 +1,7 @@
#![feature(array_zip)]
use system::Storage;
use crate::system::grid::{Position, VectorStorage};
use crate::system::grid::{Pos2D, VectorStorage};
mod system;
@ -19,12 +19,12 @@ pub extern "C" fn storage_new(grid_size: u32) -> &'static mut CStorage {
#[no_mangle]
pub extern "C" fn storage_at(storage: &CStorage, i: i32, j: i32) -> bool {
storage.0.at(&Position { x: i, y: j })
storage.0.at(&Pos2D { x: i, y: j })
}
#[no_mangle]
pub extern "C" fn storage_deposit(storage: &mut CStorage, i: i32, j: i32, val: u8) {
storage.0.write(&Position { x: i, y: j }, val == 1);
storage.0.write(&Pos2D { x: i, y: j }, val == 1);
}
#[no_mangle]
@ -35,7 +35,7 @@ pub extern "C" fn walk(d: u32, i: i32, j: i32) -> CPosition {
mod test {
use num_integer::Integer;
use crate::CPosition;
use crate::system::grid::Position;
use crate::system::grid::Pos2D;
pub(crate) fn a(d: u32, i: i32, j: i32) -> CPosition {
match d {
@ -51,8 +51,8 @@ mod test {
let (dim, sign) = d.div_rem(&2);
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.
let offset = Position::in_direction(dim, sign);
let next = Position { x: i, y: j } + offset;
let offset = Pos2D::in_direction(dim, sign);
let next = Pos2D { x: i, y: j } + offset;
CPosition(next.x, next.y)
}

View File

@ -1,13 +1,15 @@
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::{GriddedPosition, Position, Storage};
use crate::system::spaces::grid::{Pos2D, VectorStorage};
use crate::system::spaces::nd::{NDPosition, NDVectorStorage};
use crate::system::spawner::{Spawner, UniformSpawner};
use crate::system::sticker::{ProbabilisticSticking, SimpleSticking, Sticker};
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) {
pub fn execute<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P>>(sys: &mut DLASystem<R, P, S, W, Sp, St>, csv_path: &Path) {
while sys.running {
sys.update();
}
@ -16,32 +18,35 @@ pub fn execute<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>>(sys: &mu
.expect("Failed to write");
}
pub fn initial_config(seed: u64, max_particles: usize) -> DLASystem<SmallRng, Position, VectorStorage, LocalRandomWalker> {
DLASystem::new_g(
pub fn initial_config(seed: u64, max_particles: usize) -> DLASystem<SmallRng, Pos2D, VectorStorage, LocalRandomWalker, UniformSpawner, SimpleSticking> {
DLASystem::new(
SmallRng::seed_from_u64(seed),
VectorStorage::new(1600),
LocalRandomWalker,
1.0,
UniformSpawner,
SimpleSticking,
max_particles,
)
}
pub fn stick_probability(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, Position, VectorStorage, LocalRandomWalker> {
DLASystem::new_g(
pub fn stick_probability(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, Pos2D, VectorStorage, LocalRandomWalker, UniformSpawner, ProbabilisticSticking> {
DLASystem::new(
SmallRng::seed_from_u64(seed),
VectorStorage::new(1600),
LocalRandomWalker,
stick_probability,
UniformSpawner,
ProbabilisticSticking { 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(
pub fn three_dimensional(seed: u64, max_particles: usize, stick_probability: f32) -> DLASystem<SmallRng, NDPosition<3>, NDVectorStorage<3>, LocalRandomWalker, UniformSpawner, ProbabilisticSticking> {
DLASystem::new(
SmallRng::seed_from_u64(seed),
NDVectorStorage::new(1600),
LocalRandomWalker,
stick_probability,
UniformSpawner,
ProbabilisticSticking { stick_probability },
max_particles,
)
}

View File

@ -1,4 +1,5 @@
#![feature(array_zip)]
#![feature(generic_const_exprs)]
use std::path::PathBuf;

View File

@ -3,22 +3,27 @@ use rand::Rng;
use serde::Serialize;
pub mod walker;
pub mod grid;
pub mod spawner;
pub mod sticker;
pub mod model;
pub mod nd;
mod hexagonal;
pub mod spaces;
pub trait GriddedPosition: Add<Output=Self> + Serialize + Clone {
const NEIGHBOURS: u32;
pub trait Position: Add<Output=Self> + Serialize + Clone {
const DIM: usize;
fn zero() -> Self;
fn spawn<R: Rng>(rng: &mut R, radius: f32) -> Self;
fn abs(&self) -> f32;
fn from_cartesian(cartesian: [f32; Self::DIM]) -> Self;
}
pub trait GriddedPosition: Position {
const NEIGHBOURS: u32;
fn neighbour(&self, neighbour_index: u32) -> Self;
fn linear_index(&self, grid_size: u32) -> usize;
}
pub trait Storage<P: GriddedPosition> {
pub trait Storage<P: Position> {
fn at(&self, position: &P) -> bool;
fn deposit(&mut self, position: &P);
}

View File

@ -1,19 +1,20 @@
use std::io;
use std::path::Path;
use rand::prelude::*;
use crate::system::{GriddedPosition, Storage};
use crate::system::{GriddedPosition, Position, Storage};
use crate::system::spawner::Spawner;
use crate::system::sticker::Sticker;
use crate::system::walker::Walker;
pub struct DLASystem<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> {
pub struct DLASystem<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P>> {
rng: R,
space: S,
walker: W,
stick_probability: f32,
max_particles: usize,
spawner: Sp,
sticker: St,
pub running: bool,
max_particles: usize,
particles: Vec<P>,
active_particle: Option<P>,
@ -24,13 +25,14 @@ pub struct DLASystem<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> {
cluster_radius: f32,
}
impl<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> DLASystem<R, P, S, W> {
pub fn new_g(rng: R, space: S, walker: W, stick_probability: f32, max_particles: usize) -> Self {
impl<R: Rng, P: Position, S: Storage<P>, W: Walker<P>, Sp: Spawner<P>, St: Sticker<P>> DLASystem<R, P, S, W, Sp, St> {
pub fn new(rng: R, space: S, walker: W, spawner: Sp, sticker: St, max_particles: usize) -> Self {
let mut sys = DLASystem {
rng,
stick_probability,
max_particles,
running: true,
spawner,
sticker,
space,
walker,
@ -72,7 +74,7 @@ impl<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> DLASystem<R, P, S,
if distance > self.kill_circle {
self.active_particle = None;
} else if !self.space.at(&next_position) {
if self.check_stick(&next_position) {
if self.sticker.should_stick(&mut self.rng, &self.space, &next_position) {
self.deposit(&next_position);
self.active_particle = None;
return;
@ -82,17 +84,8 @@ impl<R: Rng, P: GriddedPosition, S: Storage<P>, W: Walker<P>> DLASystem<R, P, S,
}
}
fn check_stick(&mut self, position: &P) -> bool {
(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
)
}
fn spawn_particle(&mut self) {
let position = P::spawn(&mut self.rng, self.add_circle);
let position = self.spawner.spawn(&mut self.rng, self.add_circle);
if !self.space.at(&position) {
self.active_particle = Some(position);

View File

@ -2,7 +2,7 @@ use std::f32::consts::PI;
use std::ops::Add;
use num_integer::Integer;
use rand::Rng;
use crate::system::{GriddedPosition, Storage};
use crate::system::{GriddedPosition, Position, Storage};
use serde::{Serialize, Deserialize};
pub struct VectorStorage {
@ -11,46 +11,32 @@ pub struct VectorStorage {
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Position {
pub struct Pos2D {
pub x: i32,
pub y: i32,
}
impl Position {
impl Pos2D {
pub fn in_direction(direction: u32, value: i32) -> Self {
if direction == 0 { Position { x: value, y: 0 } } else { Position { x: 0, y: value } }
if direction == 0 { Pos2D { x: value, y: 0 } } else { Pos2D { x: 0, y: value } }
}
}
impl Add for Position {
type Output = Position;
impl Add for Pos2D {
type Output = Pos2D;
fn add(self, rhs: Self) -> Self::Output {
Position { x: self.x + rhs.x, y: self.y + rhs.y }
Pos2D { x: self.x + rhs.x, y: self.y + rhs.y }
}
}
impl GriddedPosition for Position {
impl GriddedPosition for Pos2D {
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);
let offset = Pos2D::in_direction(dim, sign);
self.clone() + offset
}
@ -68,6 +54,21 @@ impl GriddedPosition for Position {
}
}
impl Position for Pos2D {
const DIM: usize = 2;
fn zero() -> Pos2D {
Pos2D { x: 0, y: 0 }
}
fn abs(&self) -> f32 {
((self.x.pow(2) + self.y.pow(2)) as f32).powf(0.5)
}
fn from_cartesian(cartesian: [f32; Self::DIM]) -> Self {
Pos2D { x: cartesian[0] as i32, y: cartesian[1] as i32 }
}
}
impl VectorStorage {
pub fn new(grid_size: u32) -> VectorStorage {
VectorStorage { grid_size, backing: vec![false; grid_size.pow(2) as usize] }
@ -76,17 +77,17 @@ impl VectorStorage {
/*
* Convenience function for c-binding
* */
pub fn write(&mut self, position: &Position, val: bool) {
pub fn write(&mut self, position: &Pos2D, 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)]
impl Storage<Pos2D> for VectorStorage {
fn at(&self, position: &Pos2D) -> bool {
return self.backing[position.linear_index(self.grid_size)];
}
fn deposit(&mut self, position: &Position) {
fn deposit(&mut self, position: &Pos2D) {
self.backing[position.linear_index(self.grid_size)] = true;
}
}

View File

@ -1,6 +1,6 @@
use std::ops::Add;
use rand::Rng;
use crate::system::GriddedPosition;
use crate::system::{GriddedPosition, Position};
use serde::{Serialize, Deserialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
@ -20,18 +20,6 @@ impl Add for HexPosition {
impl GriddedPosition for HexPosition {
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 {
let neighbour_index = neighbour_index as usize;
@ -48,3 +36,19 @@ impl GriddedPosition for HexPosition {
// ((self.q + grid_size / 2) + grid_size * self.r) as usize
}
}
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 {
todo!()
}
}

3
src/system/spaces/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod grid;
pub mod hexagonal;
pub mod nd;

View File

@ -3,7 +3,7 @@ use num_integer::Integer;
use rand::Rng;
use serde::{Serialize, Serializer};
use serde::ser::{SerializeMap, SerializeSeq, SerializeStruct};
use crate::system::GriddedPosition;
use crate::system::{GriddedPosition, Position};
use crate::system::Storage;
pub struct NDVectorStorage<const DIM: usize> {
@ -59,37 +59,6 @@ impl<const DIM: usize> Serialize for NDPosition<DIM> {
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 };
@ -105,3 +74,51 @@ impl<const DIM: usize> GriddedPosition for NDPosition<DIM> {
.sum()
}
}
impl<const DIM: usize> Position for NDPosition<DIM> {
const DIM: usize = DIM;
fn zero() -> Self {
NDPosition([0; DIM])
}
fn abs(&self) -> f32 {
let a: i32 = self.0.iter()
.map(|r| r.pow(2))
.sum();
(a as f32).powf(0.5)
}
fn from_cartesian(cartesian: [f32; Self::DIM]) -> Self {
let mut a: [i32; DIM] = [0; DIM];
for (i, x) in cartesian.iter().enumerate() {
a[i] = *x as i32;
}
Self(a)
}
}
mod test {
use rand::rngs::SmallRng;
use rand::SeedableRng;
use crate::system::{GriddedPosition, Position};
use super::NDPosition;
#[test]
fn abs() {
assert_eq!(NDPosition([1, 2]).abs(), 2.23606798);
assert_eq!(NDPosition([1, 1, 1, 1]).abs(), 2.0);
assert_eq!(NDPosition([10]).abs(), 10.0);
}
#[test]
fn spawn() {
let mut rng = SmallRng::seed_from_u64(0);
for _ in 0..10 {
println!("{:?}", NDPosition::<2>::spawn(&mut rng, 10.0));
}
}
}

29
src/system/spawner.rs Normal file
View File

@ -0,0 +1,29 @@
use std::f32::consts::PI;
use rand::Rng;
use crate::system::Position;
use crate::system::spaces::grid::Pos2D;
use crate::system::spaces::nd::NDPosition;
pub trait Spawner<P: Position> {
fn spawn<R: Rng>(&self, rng: &mut R, radius: f32) -> P;
}
pub struct UniformSpawner;
impl Spawner<Pos2D> for UniformSpawner {
fn spawn<R: Rng>(&self, rng: &mut R, radius: f32) -> Pos2D {
let theta = rng.gen_range(0f32..1.0) * 2.0 * PI;
let (x, y) = (radius * theta.cos(), radius * theta.sin());
Pos2D::from_cartesian([x, y])
}
}
impl Spawner<NDPosition<3>> for UniformSpawner {
fn spawn<R: Rng>(&self, rng: &mut R, radius: f32) -> NDPosition<3> {
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.cos(), radius * theta.sin(), radius * theta.sin());
NDPosition::<3>::from_cartesian([x, y, z])
}
}

29
src/system/sticker.rs Normal file
View File

@ -0,0 +1,29 @@
use rand::Rng;
use crate::system::{GriddedPosition, Position, Storage};
pub trait Sticker<P: Position> {
fn should_stick<R: Rng, S: Storage<P>>(&self, rng: &mut R, space: &S, position: &P) -> bool;
}
pub struct SimpleSticking;
pub struct ProbabilisticSticking {
pub stick_probability: f32
}
impl<P: GriddedPosition> Sticker<P> for SimpleSticking {
fn should_stick<R: Rng, S: Storage<P>>(&self, rng: &mut R, space: &S, position: &P) -> bool {
(0..P::NEIGHBOURS)
.map(|n| position.neighbour(n))
.any(|neighbour| space.at(&neighbour))
}
}
impl<P: GriddedPosition> Sticker<P> for ProbabilisticSticking {
fn should_stick<R: Rng, S: Storage<P>>(&self, rng: &mut R, space: &S, position: &P) -> bool {
(0..P::NEIGHBOURS)
.map(|n| position.neighbour(n))
.any(|neighbour| space.at(&neighbour) && rng.gen_range(0.0f32..=1.0) < self.stick_probability)
}
}

View File

@ -1,31 +1,32 @@
use rand::prelude::Rng;
use crate::system::GriddedPosition;
use crate::system::{GriddedPosition, Position};
pub trait Walker<P: GriddedPosition> {
pub trait Walker<P: Position> {
fn walk<R: Rng>(&self, rng: &mut R, position: &P) -> P;
}
pub struct LocalRandomWalker;
impl<Position: GriddedPosition> Walker<Position> for LocalRandomWalker {
fn walk<R: Rng>(&self, rng: &mut R, position: &Position) -> Position {
position.neighbour(rng.gen_range(0u32..Position::NEIGHBOURS))
impl<P: GriddedPosition> Walker<P> for LocalRandomWalker {
fn walk<R: Rng>(&self, rng: &mut R, position: &P) -> P {
position.neighbour(rng.gen_range(0u32..P::NEIGHBOURS))
}
}
mod test {
use rand::rngs::SmallRng;
use rand::{SeedableRng, thread_rng};
use crate::system::{GriddedPosition, grid::Position};
use crate::system::{GriddedPosition, Position};
use crate::system::spaces::grid::Pos2D;
use crate::system::walker::{LocalRandomWalker, Walker};
#[test]
fn uniformity() {
let walker = LocalRandomWalker;
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let mut results: Vec<Position> = vec![];
let mut results: Vec<Pos2D> = vec![];
let origin = &Position::zero();
let origin = &Pos2D::zero();
let x: u32 = (1_000_000);
for i in 0..x {
@ -34,22 +35,22 @@ mod test {
let a = results
.iter()
.filter(|a| **a == Position { x: 0, y: 1 })
.filter(|a| **a == Pos2D { x: 0, y: 1 })
.count();
let b = results
.iter()
.filter(|a| **a == Position { x: 0, y: -1 })
.filter(|a| **a == Pos2D { x: 0, y: -1 })
.count();
let c = results
.iter()
.filter(|a| **a == Position { x: 1, y: 0 })
.filter(|a| **a == Pos2D { x: 1, y: 0 })
.count();
let d = results
.iter()
.filter(|a| **a == Position { x: -1, y: 0 })
.filter(|a| **a == Pos2D { x: -1, y: 0 })
.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);