use workspaces

This commit is contained in:
Magnus Ulimoen 2020-01-30 18:28:22 +01:00
parent 4ac1ad0d2c
commit 264b483aef
23 changed files with 185 additions and 169 deletions

View File

@ -1,38 +1,15 @@
[package] [package]
name = "sbp" name = "diffsolver"
version = "0.1.1" version = "0.1.1"
authors = ["Magnus Ulimoen <flymagnus@gmail.com>"] authors = ["Magnus Ulimoen <flymagnus@gmail.com>"]
edition = "2018" edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features] [workspace]
default = ["console_error_panic_hook", "wee_alloc"] members = [
"sbp",
[dependencies] "webfront",
wasm-bindgen = "0.2.54" ]
console_error_panic_hook = { version = "0.1.6", optional = true }
wee_alloc = { version = "0.4.5", optional = true }
ndarray = { version = "0.13.0", features = ["approx"] }
approx = "0.3.2"
packed_simd = "0.3.3"
[profile.release]
opt-level = 3
lto = "thin"
debug = true
[dev-dependencies]
criterion = "0.3.0"
[[bench]]
name = "maxwell"
harness = false
[[bench]]
name = "euler"
harness = false
[profile.bench] [profile.bench]
debug = true debug = true

21
sbp/Cargo.toml Normal file
View File

@ -0,0 +1,21 @@
[package]
name = "sbp"
version = "0.1.1"
authors = ["Magnus Ulimoen <flymagnus@gmail.com>"]
edition = "2018"
[dependencies]
ndarray = { version = "0.13.0", features = ["approx"] }
approx = "0.3.2"
packed_simd = "0.3.3"
[dev-dependencies]
criterion = "0.3.0"
[[bench]]
name = "maxwell"
harness = false
[[bench]]
name = "euler"
harness = false

View File

@ -1,6 +1,6 @@
use super::grid::Grid;
use super::integrate; use super::integrate;
use super::operators::{SbpOperator, UpwindOperator}; use super::operators::{SbpOperator, UpwindOperator};
use super::Grid;
use ndarray::azip; use ndarray::azip;
use ndarray::prelude::*; use ndarray::prelude::*;

5
sbp/src/lib.rs Normal file
View File

@ -0,0 +1,5 @@
pub mod euler;
pub mod grid;
pub mod integrate;
pub mod maxwell;
pub mod operators;

View File

@ -1,6 +1,6 @@
use super::grid::Grid;
use super::integrate; use super::integrate;
use super::operators::{SbpOperator, UpwindOperator}; use super::operators::{SbpOperator, UpwindOperator};
use super::Grid;
use ndarray::azip; use ndarray::azip;
use ndarray::prelude::*; use ndarray::prelude::*;

View File

@ -1,125 +0,0 @@
use wasm_bindgen::prelude::*;
pub mod euler;
mod grid;
pub(crate) mod integrate;
pub mod maxwell;
pub mod operators;
pub(crate) use grid::Grid;
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
pub fn set_panic_hook() {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
#[wasm_bindgen]
pub struct MaxwellUniverse(maxwell::System<operators::Upwind4>);
#[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))
}
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<operators::Upwind4>);
impl EulerUniverse {
pub fn new(x: ndarray::Array2<f32>, y: ndarray::Array2<f32>) -> Self {
Self(euler::System::new(x, y))
}
}
#[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))
}
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);
}
}

1
webfront/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
publish

16
webfront/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "sbp-web"
version = "0.1.1"
authors = ["Magnus Ulimoen <flymagnus@gmail.com>"]
edition = "2018"
[lib]
crate-type = ["cdylib"]
path = "lib.rs"
[dependencies]
wasm-bindgen = "0.2.54"
console_error_panic_hook = { version = "0.1.6" }
wee_alloc = { version = "0.4.5" }
sbp = { path = "../sbp" }
ndarray = "0.13.0"

120
webfront/lib.rs Normal file
View File

@ -0,0 +1,120 @@
use wasm_bindgen::prelude::*;
use sbp::{euler, maxwell, operators};
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
pub fn set_panic_hook() {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
#[wasm_bindgen]
pub struct MaxwellUniverse(maxwell::System<operators::Upwind4>);
#[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))
}
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<operators::Upwind4>);
impl EulerUniverse {
pub fn new(x: ndarray::Array2<f32>, y: ndarray::Array2<f32>) -> Self {
Self(euler::System::new(x, y))
}
}
#[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))
}
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);
}
}

View File

@ -24,17 +24,18 @@ if __name__ == "__main__":
publish.mkdir(exist_ok=True) publish.mkdir(exist_ok=True)
target_triple = "wasm32-unknown-unknown" target_triple = "wasm32-unknown-unknown"
command = ["cargo", "build", "--target", target_triple] command = ["env", "RUSTFLAGS=-Clto=thin", "cargo", "build", "--target", target_triple]
target = (
pathlib.Path("target")
.joinpath(target_triple)
.joinpath("release" if args.release else "debug")
.joinpath("sbp.wasm")
)
if args.release: if args.release:
command.append("--release") command.append("--release")
check_call(command) check_call(command)
target = (
pathlib.Path("../target")
.joinpath(target_triple)
.joinpath("release" if args.release else "debug")
.joinpath("sbp_web.wasm")
)
assert target.exists() assert target.exists()
check_call( check_call(
@ -53,7 +54,7 @@ if __name__ == "__main__":
try: try:
with tempfile.TemporaryDirectory() as d_: with tempfile.TemporaryDirectory() as d_:
d = pathlib.Path(d_) d = pathlib.Path(d_)
wasm_bg = publish.joinpath("sbp_bg.wasm") wasm_bg = publish.joinpath("sbp_web_bg.wasm")
wasm_to_opt = d.joinpath("before-wasm-opt.wasm") wasm_to_opt = d.joinpath("before-wasm-opt.wasm")
copyfile(wasm_bg, wasm_to_opt) copyfile(wasm_bg, wasm_to_opt)
check_call(["wasm-opt", "-O4", str(wasm_to_opt), "-o", str(wasm_bg)]) check_call(["wasm-opt", "-O4", str(wasm_to_opt), "-o", str(wasm_bg)])
@ -61,4 +62,4 @@ if __name__ == "__main__":
print("wasm-opt not found, not optimising further") print("wasm-opt not found, not optimising further")
pass pass
copytree("webfront", publish, dirs_exist_ok=True) copytree("pages", publish, dirs_exist_ok=True)

View File

@ -1,11 +1,11 @@
import { EulerUniverse, default as init, set_panic_hook as setPanicHook } from "../sbp.js"; import { EulerUniverse, default as init, set_panic_hook as setPanicHook } from "../sbp_web.js";
/** /**
* Initialises and runs the Euler solver, * Initialises and runs the Euler solver,
* plotting the solution to a canvas using webgl * plotting the solution to a canvas using webgl
*/ */
(async function run() { (async function run() {
const wasm = await init("../sbp_bg.wasm"); const wasm = await init("../sbp_web_bg.wasm");
setPanicHook(); setPanicHook();
const DIAMOND = false; const DIAMOND = false;
const UPWIND = true; const UPWIND = true;

View File

@ -1,11 +1,11 @@
import { MaxwellUniverse, default as init, set_panic_hook as setPanicHook } from "../sbp.js"; import { MaxwellUniverse, default as init, set_panic_hook as setPanicHook } from "../sbp_web.js";
/** /**
* Initialises and runs the Maxwell solver, * Initialises and runs the Maxwell solver,
* plotting the solution to a canvas using webgl * plotting the solution to a canvas using webgl
*/ */
(async function run() { (async function run() {
const wasm = await init("../sbp_bg.wasm"); const wasm = await init("../sbp_web_bg.wasm");
setPanicHook(); setPanicHook();
const DIAMOND = false; const DIAMOND = false;