From 4ac1ad0d2c69ca599475392cc08935ebfcf2b0b5 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Wed, 29 Jan 2020 22:01:12 +0100 Subject: [PATCH] follow nx/ny conventions --- benches/maxwell.rs | 6 +++--- src/grid.rs | 11 ----------- src/lib.rs | 6 ++++-- src/maxwell.rs | 24 +++++++++++++----------- webfront/maxwell/main.js | 2 +- 5 files changed, 21 insertions(+), 28 deletions(-) diff --git a/benches/maxwell.rs b/benches/maxwell.rs index 9636466..5521cc3 100644 --- a/benches/maxwell.rs +++ b/benches/maxwell.rs @@ -23,7 +23,7 @@ fn performance_benchmark(c: &mut Criterion) { let x = ndarray::Array2::from_shape_fn((h, w), |(_, i)| i as f32 / (w - 1) as f32); let y = ndarray::Array2::from_shape_fn((h, w), |(j, _)| j as f32 / (h - 1) as f32); - let mut universe = System::::new(w, h, x.as_slice().unwrap(), y.as_slice().unwrap()); + let mut universe = System::::new(x.clone(), y.clone()); group.bench_function("advance", |b| { b.iter(|| { universe.set_gaussian(0.5, 0.5); @@ -31,7 +31,7 @@ fn performance_benchmark(c: &mut Criterion) { }) }); - let mut universe = System::::new(w, h, x.as_slice().unwrap(), y.as_slice().unwrap()); + let mut universe = System::::new(x.clone(), y.clone()); group.bench_function("advance_upwind", |b| { b.iter(|| { universe.set_gaussian(0.5, 0.5); @@ -39,7 +39,7 @@ fn performance_benchmark(c: &mut Criterion) { }) }); - let mut universe = System::::new(w, h, x.as_slice().unwrap(), y.as_slice().unwrap()); + let mut universe = System::::new(x, y); group.bench_function("advance_trad4", |b| { b.iter(|| { universe.set_gaussian(0.5, 0.5); diff --git a/src/grid.rs b/src/grid.rs index e3f049c..126ab0e 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -66,17 +66,6 @@ impl Grid { operator: std::marker::PhantomData, }) } - pub fn new_from_slice( - ny: usize, - nx: usize, - x: &[f32], - y: &[f32], - ) -> Result { - let x = Array2::from_shape_vec((ny, nx), x.to_vec())?; - let y = Array2::from_shape_vec((ny, nx), y.to_vec())?; - - Self::new(x, y) - } pub fn nx(&self) -> usize { self.x.shape()[1] } diff --git a/src/lib.rs b/src/lib.rs index c5faa28..6a1ed01 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,8 +23,10 @@ pub struct MaxwellUniverse(maxwell::System); #[wasm_bindgen] impl MaxwellUniverse { #[wasm_bindgen(constructor)] - pub fn new(width: usize, height: usize, x: &[f32], y: &[f32]) -> Self { - Self(maxwell::System::new(width as usize, height as usize, x, y)) + pub fn new(height: usize, width: usize, x: &[f32], y: &[f32]) -> Self { + let x = ndarray::Array2::from_shape_vec((height, width), x.to_vec()).unwrap(); + let y = ndarray::Array2::from_shape_vec((height, width), y.to_vec()).unwrap(); + Self(maxwell::System::new(x, y)) } pub fn init(&mut self, x0: f32, y0: f32) { diff --git a/src/maxwell.rs b/src/maxwell.rs index a123523..e021d99 100644 --- a/src/maxwell.rs +++ b/src/maxwell.rs @@ -21,7 +21,7 @@ impl std::ops::DerefMut for Field { } impl Field { - pub fn new(width: usize, height: usize) -> Self { + pub fn new(height: usize, width: usize) -> Self { let field = Array3::zeros((3, height, width)); Self(field) @@ -68,6 +68,7 @@ impl Field { } } +#[derive(Debug, Clone)] pub struct System { sys: (Field, Field), wb: WorkBuffers, @@ -75,17 +76,17 @@ pub struct System { } impl System { - pub fn new(width: usize, height: usize, x: &[f32], y: &[f32]) -> Self { - assert_eq!((width * height), x.len()); - assert_eq!((width * height), y.len()); + pub fn new(x: Array2, y: Array2) -> Self { + assert_eq!(x.shape(), y.shape()); + let ny = x.shape()[0]; + let nx = x.shape()[1]; + + let grid = Grid::new(x, y).unwrap(); - let grid = Grid::new_from_slice(height, width, x, y).expect( - "Could not create grid. Different number of elements compared to width*height?", - ); Self { - sys: (Field::new(width, height), Field::new(width, height)), + sys: (Field::new(ny, nx), Field::new(ny, nx)), grid, - wb: WorkBuffers::new(width, height), + wb: WorkBuffers::new(ny, nx), } } @@ -556,15 +557,16 @@ fn SAT_characteristics( } } +#[derive(Clone, Debug)] pub struct WorkBuffers { k: [Field; 4], tmp: (Array2, Array2, Array2, Array2), } impl WorkBuffers { - pub fn new(nx: usize, ny: usize) -> Self { + pub fn new(ny: usize, nx: usize) -> Self { let arr2 = Array2::zeros((ny, nx)); - let arr3 = Field::new(nx, ny); + let arr3 = Field::new(ny, nx); Self { k: [arr3.clone(), arr3.clone(), arr3.clone(), arr3], tmp: (arr2.clone(), arr2.clone(), arr2.clone(), arr2), diff --git a/webfront/maxwell/main.js b/webfront/maxwell/main.js index 3ec8939..15941f0 100644 --- a/webfront/maxwell/main.js +++ b/webfront/maxwell/main.js @@ -116,7 +116,7 @@ import { MaxwellUniverse, default as init, set_panic_hook as setPanicHook } from } } - const universe = new MaxwellUniverse(width, height, x, y); + const universe = new MaxwellUniverse(height, width, x, y); // Transfer x, y to cpu, prepare fBuffer