This commit is contained in:
Simon Gardling 2021-04-05 12:55:06 -04:00
parent a7497d6629
commit bff0f8faf1
5 changed files with 784 additions and 38 deletions

762
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -182,6 +182,7 @@ impl Grid {
rotation_angle, step_distance, rotation_angle, step_distance,
width, height); width, height);
}); });
self.deposit_all();
} }
#[inline] #[inline]

View File

@ -57,6 +57,16 @@ impl ThinGridData {
.select_nth_unstable_by(index, |a, b| a.partial_cmp(b).unwrap()); .select_nth_unstable_by(index, |a, b| a.partial_cmp(b).unwrap());
sorted[index] sorted[index]
} }
pub fn size_of(&self) -> usize {
let mut output: usize = 0;
output = output + std::mem::size_of_val(&self.width);
output = output + std::mem::size_of_val(&self.height);
for i in self.data.iter() {
output = output + std::mem::size_of_val(&i);
}
return output;
}
} }
// Class for storing data that will be used to create images // Class for storing data that will be used to create images
@ -85,6 +95,16 @@ impl ImgData {
} }
} }
pub fn size_of(&self) -> usize {
let mut output: usize = 0;
output = output + std::mem::size_of_val(&self.iteration);
output = output + std::mem::size_of_val(&self.palette);
for grid in self.grids.iter() {
output = output + grid.size_of();
}
return output;
}
#[inline] #[inline]
pub fn save_to_image(&self) { pub fn save_to_image(&self) {
let (width, height) = (self.grids[0].width, self.grids[0].height); let (width, height) = (self.grids[0].width, self.grids[0].height);

View File

@ -4,23 +4,25 @@ fn main() {
// # of iterations to go through // # of iterations to go through
let n_iterations = 1024; let n_iterations = 1024;
// let n_iterations = 2048; // let n_iterations = 2048;
// let n_iterations = 1 << 14;
// Size of grid and pictures // Size of grid and pictures
let (width, height) = (256, 256); // let (width, height) = (256, 256);
// let (width, height) = (512, 512); // let (width, height) = (512, 512);
// let (width, height) = (1024, 1024); let (width, height) = (1024, 1024);
// # of agents // # of agents
// let n_particles = 1 << 10; // let n_particles = 1 << 10;
// let n_particles = 1 << 16; // let n_particles = 1 << 16;
let n_particles = 1 << 20; // let n_particles = 1 << 20;
let n_particles = 1 << 24;
println!("n_particles: {}", n_particles); println!("n_particles: {}", n_particles);
let diffusivity = 1; let diffusivity = 1;
// `n_populations` is the # of types of agents // `n_populations` is the # of types of agents
let n_populations = 4; // let n_populations = 4;
// let n_populations = 3; let n_populations = 1;
// let n_populations = 1 + rng.gen_range(1..4); // make # of populations between 2 and 5 // let n_populations = 1 + rng.gen_range(1..4); // make # of populations between 2 and 5
let mut model = model::Model::new(width, height, n_particles, n_populations, diffusivity); // Create the model let mut model = model::Model::new(width, height, n_particles, n_populations, diffusivity); // Create the model

View File

@ -123,19 +123,10 @@ impl Model {
let agents_tick_time = Instant::now(); let agents_tick_time = Instant::now();
// Tick agents // Tick agents
for grid in grids.iter_mut() {
grid.tick();
}
// Deposit
for grid in self.grids.iter_mut() {
grid.deposit_all();
}
// Diffuse + Decay
let diffusivity = self.diffusivity; let diffusivity = self.diffusivity;
self.grids.par_iter_mut().for_each(|grid| { self.grids.par_iter_mut().for_each(|grid| {
grid.diffuse(diffusivity); grid.tick();
grid.diffuse(diffusivity); // Diffuse + Decay
}); });
self.save_image_data(); self.save_image_data();
@ -167,11 +158,21 @@ impl Model {
); );
} }
fn size_of_imgdata_vec(&self) -> usize {
return (self.img_data_vec[0].size_of() as usize) * (self.img_data_vec.len() as usize);
}
fn save_image_data(&mut self) { fn save_image_data(&mut self) {
let grids = ThinGridData::new_from_grid_vec(self.grids.clone()); let grids = ThinGridData::new_from_grid_vec(self.grids.clone());
let img_data = ImgData::new(grids, self.palette, self.iteration); let img_data = ImgData::new(grids, self.palette, self.iteration);
self.img_data_vec.push(img_data); self.img_data_vec.push(img_data);
if self.grids[0].width > 1024 && self.grids[0].height > 1024 && self.img_data_vec.len() > 100 { let size: usize = self.size_of_imgdata_vec();
let mb = size/1024/1024;
// println!("{} B | {} KB | {} MB", size, size/1024, size/1024/1024);
let max_mb = 6000;
if mb >= max_mb {
println!("ram usage is over {} MB (and len of {}), flushing to disk\n", max_mb, self.img_data_vec.len());
self.render_all_imgdata(); self.render_all_imgdata();
self.flush_image_data(); self.flush_image_data();
} }