From 1e84e2ddf0c39482d51c7232ed55751fd93be8a2 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Sun, 14 Jun 2020 22:20:01 +0200 Subject: [PATCH] move frontend --- webfront/Cargo.toml | 1 - webfront/{pages => frontend}/index.html | 0 webfront/{pages => frontend}/main.js | 3 +- webfront/{pages => frontend}/style.css | 0 webfront/lib.rs | 183 ------------------------ webfront/make_wasm.py | 6 +- webfront/src/euler.rs | 72 ++++++++++ webfront/src/lib.rs | 27 ++++ webfront/src/maxwell.rs | 39 +++++ webfront/src/shallow_water.rs | 55 +++++++ 10 files changed, 199 insertions(+), 187 deletions(-) rename webfront/{pages => frontend}/index.html (100%) rename webfront/{pages => frontend}/main.js (99%) rename webfront/{pages => frontend}/style.css (100%) delete mode 100644 webfront/lib.rs create mode 100644 webfront/src/euler.rs create mode 100644 webfront/src/lib.rs create mode 100644 webfront/src/maxwell.rs create mode 100644 webfront/src/shallow_water.rs diff --git a/webfront/Cargo.toml b/webfront/Cargo.toml index 13da4cd..8ccc7eb 100644 --- a/webfront/Cargo.toml +++ b/webfront/Cargo.toml @@ -6,7 +6,6 @@ edition = "2018" [lib] crate-type = ["cdylib"] -path = "lib.rs" [dependencies] wasm-bindgen = "0.2.63" diff --git a/webfront/pages/index.html b/webfront/frontend/index.html similarity index 100% rename from webfront/pages/index.html rename to webfront/frontend/index.html diff --git a/webfront/pages/main.js b/webfront/frontend/main.js similarity index 99% rename from webfront/pages/main.js rename to webfront/frontend/main.js index cece613..77e91cc 100644 --- a/webfront/pages/main.js +++ b/webfront/frontend/main.js @@ -438,7 +438,8 @@ class FieldDrawer { draw(); switch (eq_set) { case "maxwell": - universe.advance(0.2*Math.min(1/width, 1/height)); + // universe.advance(0.2*Math.min(1/width, 1/height)); + universe.advance_with_matrix(0.2*Math.min(1/width, 1/height)); break; case "euler": universe.advance_upwind(0.2*Math.min(1/width, 1/height)); diff --git a/webfront/pages/style.css b/webfront/frontend/style.css similarity index 100% rename from webfront/pages/style.css rename to webfront/frontend/style.css diff --git a/webfront/lib.rs b/webfront/lib.rs deleted file mode 100644 index 08bf3f9..0000000 --- a/webfront/lib.rs +++ /dev/null @@ -1,183 +0,0 @@ -use wasm_bindgen::prelude::*; - -use euler; -use maxwell; -use sbp::operators; - -#[cfg(feature = "wee_alloc")] -#[global_allocator] -static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; - -#[wasm_bindgen] -pub fn set_panic_hook() { - console_error_panic_hook::set_once(); -} - -#[wasm_bindgen] -pub fn set_console_logger() { - use std::sync::Once; - static LOG_INIT: Once = Once::new(); - - LOG_INIT.call_once(|| console_log::init_with_level(log::Level::Trace).unwrap()); -} - -#[wasm_bindgen] -pub struct MaxwellUniverse(maxwell::System); - -#[wasm_bindgen] -impl MaxwellUniverse { - #[wasm_bindgen(constructor)] - 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, operators::Upwind4)) - } - - pub fn init(&mut self, x0: f32, y0: f32) { - self.0.set_gaussian(x0, y0); - } - - pub fn advance(&mut self, dt: f32) { - self.0.advance(dt) - } - - pub fn advance_upwind(&mut self, dt: f32) { - self.0.advance_upwind(dt) - } - - pub fn get_ex_ptr(&self) -> *const u8 { - self.0.field().ex().as_ptr() as *const u8 - } - - pub fn get_ey_ptr(&self) -> *const u8 { - self.0.field().ey().as_ptr() as *const u8 - } - - pub fn get_hz_ptr(&self) -> *const u8 { - self.0.field().hz().as_ptr() as *const u8 - } -} - -#[wasm_bindgen] -pub struct EulerUniverse(euler::System); - -impl EulerUniverse { - pub fn new(x: ndarray::Array2, y: ndarray::Array2) -> Self { - Self(euler::System::new(x, y, operators::Upwind4)) - } -} - -#[wasm_bindgen] -impl EulerUniverse { - #[wasm_bindgen(constructor)] - pub fn new_with_slice(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(euler::System::new(x, y, operators::Upwind4)) - } - - pub fn init(&mut self, x0: f32, y0: f32) { - self.0.init_with_vortex(x0, y0) - } - - pub fn advance(&mut self, dt: f32) { - self.0.advance(dt) - } - - pub fn advance_upwind(&mut self, dt: f32) { - self.0.advance_upwind(dt) - } - - pub fn get_rho_ptr(&self) -> *const u8 { - self.0.field().rho().as_ptr() as *const u8 - } - pub fn get_rhou_ptr(&self) -> *const u8 { - self.0.field().rhou().as_ptr() as *const u8 - } - pub fn get_rhov_ptr(&self) -> *const u8 { - self.0.field().rhov().as_ptr() as *const u8 - } - pub fn get_e_ptr(&self) -> *const u8 { - self.0.field().e().as_ptr() as *const u8 - } -} - -#[test] -fn start_and_advance_euler() { - let x = ndarray::Array2::from_shape_fn((20, 20), |(_j, i)| { - 5.0 * 2.0 * ((i as f32 / (20 - 1) as f32) - 0.5) - }); - let y = ndarray::Array2::from_shape_fn((20, 20), |(j, _i)| { - 5.0 * 2.0 * ((j as f32 / (20 - 1) as f32) - 0.5) - }); - let mut universe = EulerUniverse::new(x, y); - universe.init(-1.0, 0.0); - for _ in 0..50 { - universe.advance(0.01); - } -} - -#[test] -fn start_and_advance_upwind_euler() { - let x = ndarray::Array2::from_shape_fn((20, 10), |(_j, i)| i as f32 / (10 - 1) as f32); - let y = ndarray::Array2::from_shape_fn((20, 10), |(j, _i)| j as f32 / (20 - 1) as f32); - let mut universe = EulerUniverse::new(x, y); - universe.init(0.5, 0.5); - for _ in 0..50 { - universe.advance_upwind(0.01); - } -} - -#[wasm_bindgen] -pub struct ShallowWaterUniverse(shallow_water::System); - -#[wasm_bindgen] -impl ShallowWaterUniverse { - #[wasm_bindgen(constructor)] - pub fn new(height: usize, width: usize) -> Self { - let x = (-0.5, 0.5, width); - let y = (-0.5, 0.5, height); - Self(shallow_water::System::new(x, y)) - } - - pub fn init(&mut self, x0: f32, y0: f32) { - let nx = self.0.nx(); - let ny = self.0.ny(); - let x = ndarray::Array1::linspace(-0.5, 0.5, nx); - let y = ndarray::Array1::linspace(-0.5, 0.5, ny); - - let (mut eta, mut etau, mut etav) = self.0.components_mut(); - - let sigma = 0.1; - - for j in 0..ny { - let y = y[j]; - for i in 0..nx { - let x = x[i]; - let r = f32::hypot(x - x0, y - y0); - - let f = 1.0 / (sigma * (2.0 * sbp::consts::PI).sqrt()) - * f32::exp(-0.5 * (r / sigma).powi(2)); - eta[(j, i)] = 1.0 - 0.1 * f; - etau[(j, i)] = eta[(j, i)] * 0.0; - etav[(j, i)] = eta[(j, i)] * 0.0; - } - } - } - - pub fn advance(&mut self) { - self.0.advance() - } - - pub fn get_eta_ptr(&self) -> *const u8 { - self.0.eta().as_ptr() as *const u8 - } - - pub fn get_etau_ptr(&self) -> *const u8 { - self.0.etau().as_ptr() as *const u8 - } - - pub fn get_etav_ptr(&self) -> *const u8 { - self.0.etav().as_ptr() as *const u8 - } -} diff --git a/webfront/make_wasm.py b/webfront/make_wasm.py index d087c61..57731bc 100755 --- a/webfront/make_wasm.py +++ b/webfront/make_wasm.py @@ -28,7 +28,9 @@ if __name__ == "__main__": command = ["cargo", "build", "--target", target_triple] env = os.environ.copy() if args.release: - env["RUSTFLAGS"] = "-C opt-level=3 -C codegen-units=1 -C lto=fat -C embed-bitcode" + env[ + "RUSTFLAGS" + ] = "-C opt-level=3 -C codegen-units=1 -C lto=fat -C embed-bitcode" command.append("--release") check_call(command, env=env) @@ -65,4 +67,4 @@ if __name__ == "__main__": print("wasm-opt not found, not optimising further") pass - copytree("pages", publish, dirs_exist_ok=True) + copytree("frontend", publish, dirs_exist_ok=True) diff --git a/webfront/src/euler.rs b/webfront/src/euler.rs new file mode 100644 index 0000000..0cd926f --- /dev/null +++ b/webfront/src/euler.rs @@ -0,0 +1,72 @@ +use sbp::operators; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub struct EulerUniverse(euler::System); + +impl EulerUniverse { + pub fn new(x: ndarray::Array2, y: ndarray::Array2) -> Self { + Self(euler::System::new(x, y, operators::Upwind4)) + } +} + +#[wasm_bindgen] +impl EulerUniverse { + #[wasm_bindgen(constructor)] + pub fn new_with_slice(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(euler::System::new(x, y, operators::Upwind4)) + } + + pub fn init(&mut self, x0: f32, y0: f32) { + self.0.init_with_vortex(x0, y0) + } + + pub fn advance(&mut self, dt: f32) { + self.0.advance(dt) + } + + pub fn advance_upwind(&mut self, dt: f32) { + self.0.advance_upwind(dt) + } + + pub fn get_rho_ptr(&self) -> *const u8 { + self.0.field().rho().as_ptr() as *const u8 + } + pub fn get_rhou_ptr(&self) -> *const u8 { + self.0.field().rhou().as_ptr() as *const u8 + } + pub fn get_rhov_ptr(&self) -> *const u8 { + self.0.field().rhov().as_ptr() as *const u8 + } + pub fn get_e_ptr(&self) -> *const u8 { + self.0.field().e().as_ptr() as *const u8 + } +} + +#[test] +fn start_and_advance_euler() { + let x = ndarray::Array2::from_shape_fn((20, 20), |(_j, i)| { + 5.0 * 2.0 * ((i as f32 / (20 - 1) as f32) - 0.5) + }); + let y = ndarray::Array2::from_shape_fn((20, 20), |(j, _i)| { + 5.0 * 2.0 * ((j as f32 / (20 - 1) as f32) - 0.5) + }); + let mut universe = EulerUniverse::new(x, y); + universe.init(-1.0, 0.0); + for _ in 0..50 { + universe.advance(0.01); + } +} + +#[test] +fn start_and_advance_upwind_euler() { + let x = ndarray::Array2::from_shape_fn((20, 10), |(_j, i)| i as f32 / (10 - 1) as f32); + let y = ndarray::Array2::from_shape_fn((20, 10), |(j, _i)| j as f32 / (20 - 1) as f32); + let mut universe = EulerUniverse::new(x, y); + universe.init(0.5, 0.5); + for _ in 0..50 { + universe.advance_upwind(0.01); + } +} diff --git a/webfront/src/lib.rs b/webfront/src/lib.rs new file mode 100644 index 0000000..a72e189 --- /dev/null +++ b/webfront/src/lib.rs @@ -0,0 +1,27 @@ +use wasm_bindgen::prelude::*; + +mod euler; +pub use crate::euler::EulerUniverse; + +mod maxwell; +pub use crate::maxwell::MaxwellUniverse; + +mod shallow_water; +pub use crate::shallow_water::ShallowWaterUniverse; + +#[cfg(feature = "wee_alloc")] +#[global_allocator] +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + +#[wasm_bindgen] +pub fn set_panic_hook() { + console_error_panic_hook::set_once(); +} + +#[wasm_bindgen] +pub fn set_console_logger() { + use std::sync::Once; + static LOG_INIT: Once = Once::new(); + + LOG_INIT.call_once(|| console_log::init_with_level(log::Level::Trace).unwrap()); +} diff --git a/webfront/src/maxwell.rs b/webfront/src/maxwell.rs new file mode 100644 index 0000000..5bfe760 --- /dev/null +++ b/webfront/src/maxwell.rs @@ -0,0 +1,39 @@ +use sbp::operators; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub struct MaxwellUniverse(maxwell::System); + +#[wasm_bindgen] +impl MaxwellUniverse { + #[wasm_bindgen(constructor)] + 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, operators::Upwind4)) + } + + pub fn init(&mut self, x0: f32, y0: f32) { + self.0.set_gaussian(x0, y0); + } + + pub fn advance(&mut self, dt: f32) { + self.0.advance(dt) + } + + pub fn advance_upwind(&mut self, dt: f32) { + self.0.advance_upwind(dt) + } + + pub fn get_ex_ptr(&self) -> *const u8 { + self.0.field().ex().as_ptr() as *const u8 + } + + pub fn get_ey_ptr(&self) -> *const u8 { + self.0.field().ey().as_ptr() as *const u8 + } + + pub fn get_hz_ptr(&self) -> *const u8 { + self.0.field().hz().as_ptr() as *const u8 + } +} diff --git a/webfront/src/shallow_water.rs b/webfront/src/shallow_water.rs new file mode 100644 index 0000000..f3eafcc --- /dev/null +++ b/webfront/src/shallow_water.rs @@ -0,0 +1,55 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub struct ShallowWaterUniverse(shallow_water::System); + +#[wasm_bindgen] +impl ShallowWaterUniverse { + #[wasm_bindgen(constructor)] + pub fn new(height: usize, width: usize) -> Self { + let x = (-0.5, 0.5, width); + let y = (-0.5, 0.5, height); + Self(shallow_water::System::new(x, y)) + } + + pub fn init(&mut self, x0: f32, y0: f32) { + let nx = self.0.nx(); + let ny = self.0.ny(); + let x = ndarray::Array1::linspace(-0.5, 0.5, nx); + let y = ndarray::Array1::linspace(-0.5, 0.5, ny); + + let (mut eta, mut etau, mut etav) = self.0.components_mut(); + + let sigma = 0.1; + + for j in 0..ny { + let y = y[j]; + for i in 0..nx { + let x = x[i]; + let r = f32::hypot(x - x0, y - y0); + + let f = 1.0 / (sigma * (2.0 * sbp::consts::PI).sqrt()) + * f32::exp(-0.5 * (r / sigma).powi(2)); + eta[(j, i)] = 1.0 - 0.1 * f; + etau[(j, i)] = eta[(j, i)] * 0.0; + etav[(j, i)] = eta[(j, i)] * 0.0; + } + } + } + + pub fn advance(&mut self) { + self.0.advance() + } + + pub fn get_eta_ptr(&self) -> *const u8 { + self.0.eta().as_ptr() as *const u8 + } + + pub fn get_etau_ptr(&self) -> *const u8 { + self.0.etau().as_ptr() as *const u8 + } + + pub fn get_etav_ptr(&self) -> *const u8 { + self.0.etav().as_ptr() as *const u8 + } +}