Skip to content

Commit

Permalink
Move even more parameters into tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Masterchef365 committed Mar 5, 2024
1 parent 517304d commit c24294f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 46 deletions.
49 changes: 26 additions & 23 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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!)",
Expand All @@ -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),
Expand All @@ -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| {
Expand All @@ -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),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
44 changes: 21 additions & 23 deletions src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f32>,
}

#[derive(Clone)]
Expand All @@ -30,11 +35,7 @@ pub struct Sim {
/// Cell wall velocity, staggered grid
pub grid: Array2D<GridCell>,
/// 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)]
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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);

Expand All @@ -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,
);
}
Expand Down Expand Up @@ -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))
}
}

0 comments on commit c24294f

Please sign in to comment.