diff --git a/TODO.md b/TODO.md index f183f05..cf85d01 100644 --- a/TODO.md +++ b/TODO.md @@ -1,13 +1,20 @@ ### Todo: -- Auto create a mp4 from generate images - - Instead of using the command `ffmpeg -r 20 -i tmp/out_%d.png -vcodec libx265 -crf 25 -s 512x512 test.mp4` maybe use a rust library to do the same (more research needed) -- GPU compute +1. `grid.rs` + - Remove need for `deposit`/`deposit_all` + - Possible solution: Make it so when data is called, it generates it in a function when needed +2. GPU compute - Tried [ArrayFire-rust](https://github.com/arrayfire/arrayfire-rust) didn't work well, looking for another library - Try using [emu](https://github.com/calebwin/emu) (seems to be a very good option) -- sin and cos optimizations - - sin/cos table? -- Make colisions for walls of grid -- Add config and cmd arguments when running the binary to adjust simulation settings -- Rewrite `grid.rs` - - Move agent list to grid type - - Remove need for `deposit` function \ No newline at end of file + - emu seems to be the way to go + - May have to completely rewrite simulation to run on the gpu (that'll be fun *pain*) +3. Auto create a video from generated images + - Instead of using the command `ffmpeg -r 20 -i tmp/out_%d.png -vcodec libx265 -crf 25 -s 512x512 test.mp4` maybe use a rust library to do the same (more research needed) +4. Live output + - Might want to do after #3 ("Auto create a video from generated images") +5. Make collisions for walls of grid +6. Add config and cmd arguments when running the binary to adjust simulation settings7. +7. sin and cos optimizations + - sin/cos table + - precompute before simulation + - Cache sin/cos values in hashmap to be recalled later + - In order to peruse this route, I have to figure out how to access a hashmap in parallel \ No newline at end of file diff --git a/src/agent.rs b/src/agent.rs index e6fe509..49ecd2d 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -22,12 +22,8 @@ impl Display for Agent { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!( f, - "{{\n (x,y): ({},{})\n angle: {}\n population id: {}, i: {} }}", - self.x, - self.y, - self.angle, - self.population_id, - self.i, + "{{\n(x,y): ({},{})\nangle: {}\npopulation id: {}\ni: {}}}", + self.x, self.y, self.angle, self.population_id, self.i, ) } } diff --git a/src/grid.rs b/src/grid.rs index 73e74a2..acc3ae7 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -4,8 +4,8 @@ use crate::{ }; use rand::{distributions::Uniform, Rng}; - use std::fmt::{Display, Formatter}; +use rayon::{iter::ParallelIterator, prelude::*}; // A population configuration. #[derive(Debug)] @@ -159,6 +159,22 @@ impl Grid { ); } + #[inline] + pub fn tick(&mut self) { + let self_immutable = self.clone(); + self.agents.par_iter_mut().for_each(|agent| { + agent.tick(&self_immutable); + }); + } + + #[inline] + pub fn deposit_all(&mut self) { + let agent_list = self.agents.clone(); + for agent in agent_list.iter() { + self.deposit(agent.x, agent.y); + } + } + // No longer needed (moved to imgdata.rs) /* pub fn quantile(&self, fraction: f32) -> f32 { diff --git a/src/main.rs b/src/main.rs index 1c03d80..a86d5b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,8 +11,8 @@ fn main() { // let (width, height) = (1024, 1024); // # of agents - let n_particles = 1 << 10; - // let n_particles = 1 << 16; + // let n_particles = 1 << 10; + let n_particles = 1 << 16; println!("n_particles: {}", n_particles); let diffusivity = 1; diff --git a/src/model.rs b/src/model.rs index 0401690..f1f02f1 100644 --- a/src/model.rs +++ b/src/model.rs @@ -118,23 +118,16 @@ impl Model { // Combine grids let grids = &mut self.grids; combine(grids, &self.attraction_table); - let grids_immutable_1 = &grids.clone(); - let agents_tick_time = Instant::now(); // Tick agents for grid in grids.iter_mut() { - grid.agents.par_iter_mut().for_each(|agent| { - agent.tick(&grids_immutable_1[agent.population_id]); - }); + grid.tick(); } // Deposit - let grids_immutable_2 = &grids.clone(); - for grid in grids_immutable_2.iter() { - for agent in grid.agents.iter() { - self.grids[agent.population_id].deposit(agent.x, agent.y); - } + for grid in self.grids.iter_mut() { + grid.deposit_all(); } // Diffuse + Decay diff --git a/src/util.rs b/src/util.rs index e99d574..cc43420 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,4 @@ -#[inline(always)] +#[inline] pub fn wrap(x: f32, max: f32) -> f32 { x - max * ((x > max) as i32 as f32 - (x < 0.0_f32) as i32 as f32) }