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 {
pub fn new(size: i32, diff: f32, visc: f32, dt: f32) -> FluidSquare {
FluidSquare {
size: size,
dt: dt,
diff: diff,
visc: visc,
size,
dt,
diff,
visc,
s: vec![0.0; (size * size) as usize],
density: vec![0.0; (size * size) as usize],
v_x: vec![0.0; (size * size) as usize],
@ -34,7 +34,7 @@ impl FluidSquare {
diffuse(
1,
&mut self.v_x0,
&mut self.v_x,
&self.v_x,
self.visc,
self.dt,
iter,
@ -43,7 +43,7 @@ impl FluidSquare {
diffuse(
2,
&mut self.v_y0,
&mut self.v_y,
&self.v_y,
self.visc,
self.dt,
iter,
@ -90,7 +90,7 @@ impl FluidSquare {
diffuse(
0,
&mut self.s,
&mut self.density,
&self.density,
self.diff,
self.dt,
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;
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;
for _k in 0..iter {
for j in 1..size - 1 {
@ -217,9 +217,9 @@ fn project(
fn advect(
b: i32,
d: &mut Vec<f32>,
d0: &Vec<f32>,
v_x: &Vec<f32>,
v_y: &Vec<f32>,
d0: &[f32],
v_x: &[f32],
v_y: &[f32],
dt: f32,
size: i32,
) {

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

Loading…
Cancel
Save