simplify Model::run

This commit is contained in:
Simon Gardling 2025-03-24 16:46:42 -04:00
parent 735a820799
commit b7f44d9ac0
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -96,8 +96,6 @@ impl Model {
// Simulates `steps` # of steps
#[inline]
pub fn run(&mut self, steps: usize) {
let debug: bool = false;
let pb = ProgressBar::new(steps as u64);
pb.set_style(
ProgressStyle::default_bar()
@ -111,21 +109,15 @@ impl Model {
let mut time_per_step_list: Vec<f64> = Vec::new();
let agents_num: usize = self.grids.iter().map(|grid| grid.agents.len()).sum();
for i in 0..steps {
if debug {
println!("Starting tick for all agents...")
};
(0..steps).for_each(|_| {
// Combine grids
let grids = &mut self.grids;
combine(grids, &self.attraction_table);
combine(&mut self.grids, &self.attraction_table);
let agents_tick_time = Instant::now();
// Tick agents
let diffusivity = self.diffusivity;
self.grids.par_iter_mut().for_each(|grid| {
grid.tick();
grid.diffuse(diffusivity); // Diffuse + Decay
grid.diffuse(self.diffusivity); // Diffuse + Decay
});
self.save_image_data();
@ -135,22 +127,16 @@ impl Model {
time_per_agent_list.push(ms_per_agent);
time_per_step_list.push(agents_tick_elapsed);
if debug {
println!(
"Finished tick for all agents. took {}ms\nTime per agent: {}ms\n",
agents_tick_elapsed, ms_per_agent
)
};
self.iteration += 1;
pb.set_position(i as u64);
}
pb.inc(1);
});
pb.finish();
let avg_per_step: f64 =
time_per_step_list.iter().sum::<f64>() / time_per_step_list.len() as f64;
let avg_per_agent: f64 =
time_per_agent_list.iter().sum::<f64>() / time_per_agent_list.len() as f64;
println!(
"Average time per step: {}ms\nAverage time per agent: {}ms",
avg_per_step, avg_per_agent