From c24294f6aaec65db7ffaee10fa728d1fa8abab1b Mon Sep 17 00:00:00 2001 From: Duncan Freeman Date: Tue, 5 Mar 2024 00:18:30 -0800 Subject: [PATCH] Move even more parameters into tweaks --- src/app.rs | 49 ++++++++++++++++++++++++++----------------------- src/sim.rs | 44 +++++++++++++++++++++----------------------- 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/src/app.rs b/src/app.rs index 2b32fd4..d1ecceb 100644 --- a/src/app.rs +++ b/src/app.rs @@ -23,7 +23,6 @@ pub struct TemplateApp { tweak: SimTweak, width: usize, height: usize, - calc_rest_density_from_radius: bool, set_inter_dist_to_radius: bool, //show_arrows: bool, //show_grid: bool, @@ -51,25 +50,25 @@ impl TemplateApp { (120, 80) }; let n_particles = 4_000; - let particle_radius = 0.28; let random_std_dev = 5.; let n_colors = 3; + let tweak = SimTweak::default(); + let life = LifeConfig::random(n_colors, random_std_dev); - let sim = Sim::new(width, height, n_particles, particle_radius, life); + let sim = Sim::new(width, height, n_particles, life); let nodes = NodeGraphWidget::new(nodegraph_fn_inputs()); let node_cfg = NodeInteractionCfg::default(); Self { - tweak: SimTweak::default(), + tweak, node_cfg, particle_mode: ParticleBehaviourMode::NodeGraph, nodes, advanced: false, n_colors, source_rate: 0, - calc_rest_density_from_radius: true, single_step: false, sim, width, @@ -184,7 +183,7 @@ impl TemplateApp { // Draw particles let painter = ui.painter_at(rect); let radius = coords - .sim_to_egui_vect(Vec2::splat(self.sim.particle_radius)) + .sim_to_egui_vect(Vec2::splat(self.tweak.particle_radius)) .length() / 2_f32.sqrt(); @@ -290,7 +289,7 @@ impl TemplateApp { } }); if self.advanced { - ui.add(Slider::new(&mut self.sim.damping, 0.0..=1.0).text("Damping")); + ui.add(Slider::new(&mut self.tweak.damping, 0.0..=1.0).text("Damping")); ui.checkbox( &mut self.tweak.enable_grid_transfer, "Grid transfer (required for incompressibility solver!)", @@ -303,7 +302,7 @@ impl TemplateApp { ui.strong("Particle collisions"); }); ui.add( - DragValue::new(&mut self.sim.particle_radius) + DragValue::new(&mut self.tweak.particle_radius) .prefix("Particle radius: ") .speed(1e-2) .clamp_range(1e-2..=5.0), @@ -314,22 +313,29 @@ impl TemplateApp { "Hard collisions", ); ui.horizontal(|ui| { - ui.add( - DragValue::new(&mut self.sim.rest_density) + let mut rest_density = self.tweak.rest_density(); + if ui.add( + DragValue::new(&mut rest_density) .prefix("Rest density: ") .speed(1e-2), - ); - ui.checkbox( - &mut self.calc_rest_density_from_radius, + ).changed() { + self.tweak.rest_density = Some(rest_density); + } + + let mut calc_rest_density_from_radius = self.tweak.rest_density.is_none(); + if ui.checkbox( + &mut calc_rest_density_from_radius, "From radius (assumes optimal packing)", - ); + ).changed() { + if calc_rest_density_from_radius { + self.tweak.rest_density = None; + } else { + self.tweak.rest_density = Some(calc_rest_density(self.tweak.particle_radius)); + } + } }); } - if self.calc_rest_density_from_radius { - self.sim.rest_density = calc_rest_density(self.sim.particle_radius); - } - if self.advanced && self.tweak.enable_grid_transfer { ui.separator(); ui.horizontal(|ui| { @@ -338,7 +344,7 @@ impl TemplateApp { }); ui.add(DragValue::new(&mut self.tweak.solver_iters).prefix("Solver iterations: ")); ui.add( - DragValue::new(&mut self.sim.over_relax) + DragValue::new(&mut self.tweak.over_relax) .prefix("Over-relaxation: ") .speed(1e-2) .clamp_range(0.0..=1.95), @@ -433,7 +439,7 @@ impl TemplateApp { }); } if self.set_inter_dist_to_radius { - behav_cfg.inter_threshold = self.sim.particle_radius * 2.; + behav_cfg.inter_threshold = self.tweak.particle_radius * 2.; } for b in self.sim.life.behaviours.data_mut() { b.max_inter_dist = behav_cfg.max_inter_dist; @@ -520,15 +526,12 @@ impl TemplateApp { */ if reset { - let damp = self.sim.damping; self.sim = Sim::new( self.width, self.height, self.sim.particles.len(), - self.sim.particle_radius, self.sim.life.clone(), ); - self.sim.damping = damp; } ui.checkbox(&mut self.advanced, "Advanced settings"); diff --git a/src/sim.rs b/src/sim.rs index dad61d3..9b6e41f 100644 --- a/src/sim.rs +++ b/src/sim.rs @@ -21,6 +21,11 @@ pub struct SimTweak { pub enable_particle_collisions: bool, pub enable_grid_transfer: bool, pub particle_mode: ParticleBehaviourMode, + pub particle_radius: f32, + pub over_relax: f32, + pub damping: f32, + /// If None, assumes hexagonal packing of particle_radius + pub rest_density: Option, } #[derive(Clone)] @@ -30,11 +35,7 @@ pub struct Sim { /// Cell wall velocity, staggered grid pub grid: Array2D, /// Rest density, in particles/unit^2 - pub rest_density: f32, - pub particle_radius: f32, - pub over_relax: f32, pub life: LifeConfig, - pub damping: f32, } #[derive(Copy, Clone, Debug, Default)] @@ -149,31 +150,17 @@ pub fn random_particle( } impl Sim { - pub fn new( - width: usize, - height: usize, - n_particles: usize, - particle_radius: f32, - life: LifeConfig, - ) -> Self { + pub fn new(width: usize, height: usize, n_particles: usize, life: LifeConfig) -> Self { // Uniformly placed, random particles let mut rng = rand::thread_rng(); let particles = (0..n_particles) .map(|_| random_particle(&mut rng, width, height, &life)) .collect(); - // Assume half-hexagonal packing density... - //let rest_density = calc_rest_density(particle_radius); - let rest_density = 2.8; - Sim { - damping: 0., life, particles, grid: Array2D::new(width, height), - rest_density, - particle_radius, - over_relax: 1.5, } } @@ -191,9 +178,9 @@ impl Sim { ParticleBehaviourMode::Off => (), } - step_particles(&mut self.particles, tweak.dt, self.damping); + step_particles(&mut self.particles, tweak.dt, tweak.damping); if tweak.enable_particle_collisions { - enforce_particle_radius(&mut self.particles, self.particle_radius); + enforce_particle_radius(&mut self.particles, tweak.particle_radius); } enforce_particle_pos(&mut self.particles, &self.grid); @@ -209,8 +196,8 @@ impl Sim { solver_fn( &mut self.grid, tweak.solver_iters, - self.rest_density, - self.over_relax, + tweak.rest_density(), + tweak.over_relax, tweak.stiffness, ); } @@ -780,6 +767,17 @@ impl Default for SimTweak { gravity: 9.8, solver: IncompressibilitySolver::GaussSeidel, particle_mode: ParticleBehaviourMode::NodeGraph, + particle_radius: 0.28, + over_relax: 1.5, + damping: 0., + rest_density: None, } } } + +impl SimTweak { + pub fn rest_density(&self) -> f32 { + self.rest_density + .unwrap_or_else(|| calc_rest_density(self.particle_radius)) + } +}