Skip to content

Commit

Permalink
feat: slider for inlet velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
ssoudan committed Oct 19, 2023
1 parent e62a1dd commit e829ede
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
target/
pkg/
dist/
dist/
.DS_Store
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ <h1>Eulerian Fluid Simulation</h1>
<option value="rainbow">Rainbow</option>
<option value="grayscale">Grayscale</option>
</select>

<!-- slider for input velocity -->
<label for="in_vel">Input velocity</label>
<input type="range" id="in_vel" name="in_vel" min="0" max="5" value="0.8" step="0.01">
</div>

<div class="control_line">
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import('./pkg')
// Get the streamlines_spacing slider
const streamlines_spacing_slider = document.getElementById("streamlines_spacing");

// Get the in_vel slider
const in_vel_slider = document.getElementById("in_vel");

// Get the colormap selector
const colormap_selector = document.getElementById("colormap");

Expand Down Expand Up @@ -58,10 +61,12 @@ import('./pkg')

const overrelaxation = 1.9;

const gravity = -9.81;
const gravity = 0.;

const in_vel = 0.8;

// Create the fluid simulation
const fluid = wasm.Fluid.create(gravity, numX, numY, h, density)
const fluid = wasm.Fluid.create(gravity, in_vel, numX, numY, h, density)

// Setup the obstacles
fluid.clear_obstacles();
Expand All @@ -71,6 +76,7 @@ import('./pkg')
simu_canvas, scenario_selector,
pressure_checkbox, streamlines_checkbox,
streamlines_num_seg_slider, streamlines_spacing_slider,
in_vel_slider,
colormap_selector,
sim_to_canvas_ratio)

Expand Down
37 changes: 23 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn run_with_selector(
streamlines_checkbox: web_sys::HtmlInputElement,
streamlines_num_segs: web_sys::HtmlInputElement,
streamlines_spacing: web_sys::HtmlInputElement,
in_vel: web_sys::HtmlInputElement,
colormap_selector: web_sys::HtmlSelectElement,
sim_to_canvas_ratio: u32,
) -> Result<(), JsValue> {
Expand All @@ -47,7 +48,7 @@ pub fn run_with_selector(

let previous_frame = Rc::new(RefCell::new(Instant::now()));

let scenario = Rc::new(RefCell::new(None::<String>));
let scenario = Rc::new(RefCell::new(None::<(String, f32)>));

let context = canvas
.get_context("2d")
Expand Down Expand Up @@ -75,6 +76,8 @@ pub fn run_with_selector(
let streamlines_num_segs = streamlines_num_segs.value_as_number() as usize;
let streamlines_spacing = streamlines_spacing.value_as_number() as usize;

let in_vel = in_vel.value_as_number() as f32;

let options = DrawOptions {
pressure,
obstacle: true,
Expand All @@ -90,27 +93,33 @@ pub fn run_with_selector(
// If the scenario is not set or has changed, update the fluid.
let mut scenario = scenario.borrow_mut();
match scenario.as_ref() {
Some(sv) if sv == scenario_value.as_str() => {}
Some((sv, in_vel_)) if (sv == scenario_value.as_str()) && (*in_vel_ == in_vel) => {}
_ => {
scenario.replace(scenario_value.clone());
scenario.replace((scenario_value.clone(), in_vel));

match scenario_value.as_str() {
"rectangular" => {
fluid.clear_obstacles();
fluid.vortex_shedding(vec![ObstacleType::Rectangular {
x: 0.2,
y: 0.5,
w: 0.1,
h: 0.3,
}]);
fluid.vortex_shedding(
in_vel,
vec![ObstacleType::Rectangular {
x: 0.2,
y: 0.5,
w: 0.1,
h: 0.3,
}],
);
}
_ => {
fluid.clear_obstacles();
fluid.vortex_shedding(vec![ObstacleType::Circular {
x: 0.5,
y: 0.5,
r: 0.2,
}]);
fluid.vortex_shedding(
in_vel,
vec![ObstacleType::Circular {
x: 0.5,
y: 0.5,
r: 0.2,
}],
);
}
}
}
Expand Down
23 changes: 17 additions & 6 deletions src/simu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ use web_sys::CanvasRenderingContext2d;
pub struct Fluid {
/// gravity
gravity: f32,
/// in_vel - horizontal velocity at the inlet
in_vel: f32,

/// density of the fluid
density: f32,
Expand Down Expand Up @@ -357,10 +359,12 @@ impl Fluid {
let r = ctx.put_image_data(&data, 0.0, 0.0);

let text = format!(
"min: {:>8.1}\tmax: {:>8.1}\t{:>8.1} fps",
"min: {:>8.1}\tmax: {:>8.1}\t{:>8.1} fps\tin_vel: {:>4.2}\t gravity: {:>4.2}",
min_p,
max_p,
1. / dt
1. / dt,
self.in_vel,
self.gravity
);

let _ = ctx.fill_text(&text, 12., 12.);
Expand Down Expand Up @@ -488,12 +492,10 @@ impl Fluid {
}

/// flow in a pipe and around obstacles with no gravity
pub fn vortex_shedding(&mut self, obstacles: Vec<ObstacleType>) {
pub fn vortex_shedding(&mut self, in_vel: f32, obstacles: Vec<ObstacleType>) {
const FLUID: f32 = 1.0;
const OBSTACLE: f32 = 0.0;

let in_vel = 2.0;

let n = self.num_y;

for i in 0..self.num_x {
Expand Down Expand Up @@ -521,6 +523,7 @@ impl Fluid {
}

self.gravity = 0.;
self.in_vel = in_vel;

self.add_obstacles(obstacles.into_iter().map(|x| x.into()).collect());
}
Expand Down Expand Up @@ -596,7 +599,14 @@ impl Fluid {

#[wasm_bindgen]
impl Fluid {
pub fn create(gravity: f32, num_x: usize, num_y: usize, h: f32, density: f32) -> Fluid {
pub fn create(
gravity: f32,
in_vel: f32,
num_x: usize,
num_y: usize,
h: f32,
density: f32,
) -> Fluid {
let num_x = num_x + 2; // 2 border cells
let num_y = num_y + 2;

Expand All @@ -611,6 +621,7 @@ impl Fluid {
let new_m = vec![0.0; num_cells];
Fluid {
gravity,
in_vel,
density,
num_x,
num_y,
Expand Down

0 comments on commit e829ede

Please sign in to comment.