Compare commits

...

4 Commits

Author SHA1 Message Date
68e5d9fc3a remove unneeded functions 2025-03-24 17:04:58 -04:00
70354a4111 ThinGridData: avoid clones 2025-03-24 17:04:21 -04:00
56f3eae156 Model: rename grids field 2025-03-24 17:03:26 -04:00
5d574c9674 Model: rely on draining image data 2025-03-24 17:01:18 -04:00
3 changed files with 15 additions and 25 deletions

View File

@@ -21,8 +21,7 @@ impl ThinGridData {
}
}
#[allow(dead_code)]
pub fn new_from_grid_vec(in_grids: Vec<Grid>) -> Vec<Self> {
pub fn new_from_grid_vec(in_grids: &[Grid]) -> Vec<Self> {
in_grids
.iter()
.map(|grid| Self::new_from_grid(grid))
@@ -30,13 +29,6 @@ impl ThinGridData {
}
// from grid.rs (needed in image gen)
#[allow(dead_code)]
pub fn data(&self) -> &[f32] {
&self.data
}
// from grid.rs (needed in image gen)
#[allow(dead_code)]
pub fn quantile(&self, fraction: f32) -> f32 {
let index = if (fraction - 1.0_f32).abs() < f32::EPSILON {
self.data.len() - 1
@@ -59,7 +51,7 @@ pub struct ImgData {
}
impl ImgData {
pub fn new(in_grids: Vec<ThinGridData>, in_palette: Palette) -> Self {
pub const fn new(in_grids: Vec<ThinGridData>, in_palette: Palette) -> Self {
ImgData {
grids: in_grids,
palette: in_palette,
@@ -83,7 +75,7 @@ impl ImgData {
for (grid, max_value, color) in
multizip((&self.grids, &max_values, &self.palette.colors))
{
let mut t = (grid.data()[i] / max_value).clamp(0.0, 1.0);
let mut t = (grid.data[i] / max_value).clamp(0.0, 1.0);
t = t.powf(1.0 / 2.2); // gamma correction
r += color.0[0] as f32 * t;
g += color.0[1] as f32 * t;

View File

@@ -34,6 +34,5 @@ fn main() {
// export saved image data
println!("Rendering all saved image data....");
model.render_all_imgdata();
model.flush_image_data();
println!("Done!");
}

View File

@@ -13,8 +13,8 @@ use std::{path::Path, time::Instant};
// Top-level simulation class.
pub struct Model {
// The grid they move on.
grids: Vec<Grid>,
// per-population grid (one for each population)
population_grids: Vec<Grid>,
// Attraction table governs interaction across populations
attraction_table: Vec<Vec<f32>>,
@@ -39,7 +39,7 @@ impl Model {
const REPULSION_FACTOR_STD: f32 = 0.1;
pub fn print_configurations(&self) {
for (i, grid) in self.grids.iter().enumerate() {
for (i, grid) in self.population_grids.iter().enumerate() {
println!("Grid {}: {}", i, grid.config);
}
println!("Attraction table: {:#?}", self.attraction_table);
@@ -84,7 +84,7 @@ impl Model {
}
Model {
grids,
population_grids: grids,
attraction_table,
diffusivity,
iteration: 0,
@@ -108,14 +108,18 @@ impl Model {
let mut time_per_agent_list: Vec<f64> = Vec::new();
let mut time_per_step_list: Vec<f64> = Vec::new();
let agents_num: usize = self.grids.iter().map(|grid| grid.agents.len()).sum();
let agents_num: usize = self
.population_grids
.iter()
.map(|grid| grid.agents.len())
.sum();
(0..steps).for_each(|_| {
// Combine grids
combine(&mut self.grids, &self.attraction_table);
combine(&mut self.population_grids, &self.attraction_table);
let agents_tick_time = Instant::now();
// Tick agents
self.grids.par_iter_mut().for_each(|grid| {
self.population_grids.par_iter_mut().for_each(|grid| {
grid.tick();
grid.diffuse(self.diffusivity); // Diffuse + Decay
});
@@ -144,7 +148,7 @@ impl Model {
}
fn save_image_data(&mut self) {
let grids = ThinGridData::new_from_grid_vec(self.grids.clone());
let grids = ThinGridData::new_from_grid_vec(&self.population_grids);
let img_data = ImgData::new(grids, self.palette);
self.img_data_vec.push((self.iteration + 1, img_data));
let size: usize = std::mem::size_of_val(&self.img_data_vec);
@@ -159,14 +163,9 @@ impl Model {
self.img_data_vec.len()
);
self.render_all_imgdata();
self.flush_image_data();
}
}
pub fn flush_image_data(&mut self) {
self.img_data_vec.clear();
}
pub fn render_all_imgdata(&mut self) {
if !Path::new("./tmp").exists() {
std::fs::create_dir("./tmp").expect("could create directory");