This commit is contained in:
Simon Gardling
2021-03-26 15:11:18 +00:00
parent a7bc5599bc
commit 7965d55de9
3 changed files with 17 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
### Todo: ### Todo:
- Auto create a mp4 from generate images - Auto create a mp4 from generate images
- Instead of using the command `ffmpeg -r 20 -i tmp/out_%d.png -vcodec libx264 -crf 25 test.mp4` maybe use a rust library to do the same (more research needed) - 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 via [ArrayFire-rust](https://github.com/arrayfire/arrayfire-rust) - GPU compute via [ArrayFire-rust](https://github.com/arrayfire/arrayfire-rust)

View File

@@ -4,10 +4,13 @@ use physarum::model;
use rand::Rng; use rand::Rng;
fn main() { fn main() {
let n_iterations = 16384; // let n_iterations = 16384;
let n_iterations = 4096;
// let n_iterations = 10;
// let (width, height) = (512, 512); // let (width, height) = (512, 512);
// let (width, height) = (1024, 1024); let (width, height) = (1024, 1024);
let (width, height) = (2048, 2048); // let (width, height) = (2048, 2048);
let n_particles = 1 << 22; let n_particles = 1 << 22;
println!("n_particles: {}", n_particles); println!("n_particles: {}", n_particles);

View File

@@ -155,13 +155,11 @@ impl Model {
/// Perform a single simulation step. /// Perform a single simulation step.
pub fn step(&mut self) { pub fn step(&mut self) {
let save_image: bool = true;
// Combine grids // Combine grids
let grids = &mut self.grids; let grids = &mut self.grids;
combine(grids, &self.attraction_table); combine(grids, &self.attraction_table);
println!("Starting tick for all agents..."); // println!("Starting tick for all agents...");
let agents_tick_time = Instant::now(); let agents_tick_time = Instant::now();
self.agents.par_iter_mut().for_each(|agent| { self.agents.par_iter_mut().for_each(|agent| {
let grid = &grids[agent.population_id]; let grid = &grids[agent.population_id];
@@ -196,10 +194,11 @@ impl Model {
let direction = Model::pick_direction(trail_c, trail_l, trail_r, &mut rng); let direction = Model::pick_direction(trail_c, trail_l, trail_r, &mut rng);
agent.rotate_and_move(direction, rotation_angle, step_distance, width, height); agent.rotate_and_move(direction, rotation_angle, step_distance, width, height);
}); });
/*
let agents_tick_elapsed = agents_tick_time.elapsed().as_millis(); let agents_tick_elapsed = agents_tick_time.elapsed().as_millis();
let ms_per_agent: f64 = (agents_tick_elapsed as f64) / (self.agents.len() as f64); let ms_per_agent: f64 = (agents_tick_elapsed as f64) / (self.agents.len() as f64);
println!("Finished tick for all agents. took {}ms\nTime peragent: {}ms", agents_tick_time.elapsed().as_millis(), ms_per_agent); println!("Finished tick for all agents. took {}ms\nTime peragent: {}ms", agents_tick_time.elapsed().as_millis(), ms_per_agent);
*/
// Deposit // Deposit
for agent in self.agents.iter() { for agent in self.agents.iter() {
@@ -212,17 +211,7 @@ impl Model {
grid.diffuse(diffusivity); grid.diffuse(diffusivity);
}); });
/*
println!("Saving image...");
let image_save_time = Instant::now();
self.save_to_image(format!("./tmp/out_{}.png", self.iteration).as_str());
println!("Saved image took {}", image_save_time.elapsed().as_millis());
*/
println!("Saving imgdata...");
let image_save_time = Instant::now();
self.save_image_data(); self.save_image_data();
println!("Saved imgdata, took {}", image_save_time.elapsed().as_millis());
self.iteration += 1; self.iteration += 1;
} }
@@ -237,7 +226,7 @@ impl Model {
} }
pub fn render_all_imgdata(&self) { pub fn render_all_imgdata(&self) {
if not Path::new("./tmp").exists() { if !Path::new("./tmp").exists() {
std::fs::create_dir("./tmp"); std::fs::create_dir("./tmp");
} }
@@ -246,18 +235,18 @@ impl Model {
"{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] ({pos}/{len}, {percent}%, {per_sec})", "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] ({pos}/{len}, {percent}%, {per_sec})",
)); ));
/*
for img in &self.img_data_vec { for img in &self.img_data_vec {
Self::save_to_image(img.to_owned()); Self::save_to_image(img.to_owned());
pb.inc(1); pb.inc(1);
} }
pb.finish(); pb.finish();
/*
img_data_list.par_iter().progress_with(pb)
.foreach(|&img| {
save_to_image(img);
});
*/ */
(&self.img_data_vec).par_iter().progress_with(pb)
.for_each(|img| {
Self::save_to_image(img.to_owned());
});
} }
pub fn save_to_image(imgdata: ImgData) { pub fn save_to_image(imgdata: ImgData) {