refactored to be more idiomatic and robust

master
nkoch001 6 years ago
parent 3d59e57df7
commit 9b7ba4bc80
  1. 24
      src/fluid.rs
  2. 12
      src/main.rs

@ -17,10 +17,10 @@ pub struct FluidSquare {
impl FluidSquare { impl FluidSquare {
pub fn new(size: i32, diff: f32, visc: f32, dt: f32) -> FluidSquare { pub fn new(size: i32, diff: f32, visc: f32, dt: f32) -> FluidSquare {
FluidSquare { FluidSquare {
size: size, size,
dt: dt, dt,
diff: diff, diff,
visc: visc, visc,
s: vec![0.0; (size * size) as usize], s: vec![0.0; (size * size) as usize],
density: vec![0.0; (size * size) as usize], density: vec![0.0; (size * size) as usize],
v_x: vec![0.0; (size * size) as usize], v_x: vec![0.0; (size * size) as usize],
@ -34,7 +34,7 @@ impl FluidSquare {
diffuse( diffuse(
1, 1,
&mut self.v_x0, &mut self.v_x0,
&mut self.v_x, &self.v_x,
self.visc, self.visc,
self.dt, self.dt,
iter, iter,
@ -43,7 +43,7 @@ impl FluidSquare {
diffuse( diffuse(
2, 2,
&mut self.v_y0, &mut self.v_y0,
&mut self.v_y, &self.v_y,
self.visc, self.visc,
self.dt, self.dt,
iter, iter,
@ -90,7 +90,7 @@ impl FluidSquare {
diffuse( diffuse(
0, 0,
&mut self.s, &mut self.s,
&mut self.density, &self.density,
self.diff, self.diff,
self.dt, self.dt,
iter, iter,
@ -119,12 +119,12 @@ impl FluidSquare {
} }
} }
fn diffuse(b: i32, x: &mut Vec<f32>, x0: &Vec<f32>, diff: f32, dt: f32, iter: i32, size: i32) { fn diffuse(b: i32, x: &mut Vec<f32>, x0: &[f32], diff: f32, dt: f32, iter: i32, size: i32) {
let a: f32 = dt * diff * (size - 2) as f32 * (size - 2) as f32; let a: f32 = dt * diff * (size - 2) as f32 * (size - 2) as f32;
lin_solve(b, x, x0, a, 1.0 + 6.0 * a, iter, size) lin_solve(b, x, x0, a, 1.0 + 6.0 * a, iter, size)
} }
fn lin_solve(b: i32, x: &mut Vec<f32>, x0: &Vec<f32>, a: f32, c: f32, iter: i32, size: i32) { fn lin_solve(b: i32, x: &mut Vec<f32>, x0: &[f32], a: f32, c: f32, iter: i32, size: i32) {
let c_recip = 1.0 / c; let c_recip = 1.0 / c;
for _k in 0..iter { for _k in 0..iter {
for j in 1..size - 1 { for j in 1..size - 1 {
@ -217,9 +217,9 @@ fn project(
fn advect( fn advect(
b: i32, b: i32,
d: &mut Vec<f32>, d: &mut Vec<f32>,
d0: &Vec<f32>, d0: &[f32],
v_x: &Vec<f32>, v_x: &[f32],
v_y: &Vec<f32>, v_y: &[f32],
dt: f32, dt: f32,
size: i32, size: i32,
) { ) {

@ -10,7 +10,6 @@ extern crate piston_window;
use piston_window::*; use piston_window::*;
mod fluid; mod fluid;
mod tests;
fn main() { fn main() {
let size: i32 = 80; let size: i32 = 80;
@ -18,8 +17,8 @@ fn main() {
let iter: i32 = 10; let iter: i32 = 10;
let dye_amount = 1.0; let dye_amount = 1.0;
let diffusion = 0.2; let diffusion = 0.2;
let viscosity = 0.000000001; let viscosity = 0.000_000_001;
let dt = 0.000001; let dt = 0.000_001;
let mut fs: fluid::FluidSquare = fluid::FluidSquare::new(size, dt, viscosity, diffusion); let mut fs: fluid::FluidSquare = fluid::FluidSquare::new(size, dt, viscosity, diffusion);
// fs.step(iter); // fs.step(iter);
println!("Hello, world!"); println!("Hello, world!");
@ -62,7 +61,7 @@ fn main() {
// scale as f64, // scale as f64,
// ) // )
let square = let square =
rectangle::centered([x as f64, y as f64, scale as f64, scale as f64]); rectangle::centered([f64::from(x), f64::from(y), f64::from(scale), f64::from(scale)]);
let transform = _c.transform.trans(0.0, 0.0); let transform = _c.transform.trans(0.0, 0.0);
rectangle.draw(square, &_c.draw_state, transform, g); rectangle.draw(square, &_c.draw_state, transform, g);
@ -102,10 +101,7 @@ fn main() {
); );
} }
if vel { if vel {
let mut vel_add = 50.0; let vel_add = if dye {20.0} else {50.0};
if dye {
vel_add = 20.0;
}
fs.add_velocity( fs.add_velocity(
(m[0] as i32 / scale) as i32, (m[0] as i32 / scale) as i32,
(m[1] as i32 / scale) as i32, (m[1] as i32 / scale) as i32,

Loading…
Cancel
Save