split euler and maxwell to separate crates

This commit is contained in:
Magnus Ulimoen
2020-05-02 00:22:59 +02:00
parent 52bd4f3f8f
commit a1acf00c5a
17 changed files with 202 additions and 113 deletions

22
euler/Cargo.toml Normal file
View File

@@ -0,0 +1,22 @@
[package]
name = "euler"
version = "0.1.0"
authors = ["Magnus Ulimoen <flymagnus@gmail.com>"]
edition = "2018"
[features]
# Internal feature flag to gate the expensive tests
# which should be run only in release builds
expensive_tests = []
[dependencies]
ndarray = "0.13.1"
sbp = { path = "../sbp" }
arrayvec = "0.5.1"
[dev-dependencies]
criterion = "0.3.1"
[[bench]]
name = "bench"
harness = false

86
euler/benches/bench.rs Normal file
View File

@@ -0,0 +1,86 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use sbp::euler::System;
use sbp::operators::{SbpOperator2d, Upwind4, UpwindOperator2d, SBP4};
use sbp::Float;
fn advance_system<SBP: SbpOperator2d>(universe: &mut System<SBP>, n: usize) {
for _ in 0..n {
universe.advance(1.0 / 40.0 * 0.2);
}
}
fn advance_system_upwind<UO: UpwindOperator2d>(universe: &mut System<UO>, n: usize) {
for _ in 0..n {
universe.advance_upwind(1.0 / 40.0 * 0.2);
}
}
fn advance_embedded<UO: UpwindOperator2d>(universe: &mut System<UO>, embedded: bool) {
let dt = 0.2 / std::cmp::max(universe.nx(), universe.ny()) as Float;
let t = 1.0;
if embedded {
let mut dt = dt;
universe.advance_adaptive(t, &mut dt, 1e-2);
} else {
for _ in 0..(t / dt).round() as isize {
universe.advance_upwind(dt);
}
}
}
fn performance_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("EulerSystem");
group.sample_size(25);
let w = 40;
let h = 26;
let x = ndarray::Array1::linspace(-10.0, 10.0, w);
let x = x.broadcast((h, w)).unwrap();
let y = ndarray::Array1::linspace(-10.0, 10.0, h);
let y = y.broadcast((w, h)).unwrap().reversed_axes();
let mut universe = System::new(x.into_owned(), y.into_owned(), Upwind4);
group.bench_function("advance", |b| {
b.iter(|| {
universe.init_with_vortex(0.0, 0.0);
advance_system(&mut universe, black_box(20))
})
});
let mut universe = System::new(x.into_owned(), y.into_owned(), Upwind4);
group.bench_function("advance_upwind", |b| {
b.iter(|| {
universe.init_with_vortex(0.0, 0.0);
advance_system_upwind(&mut universe, black_box(20))
})
});
let mut universe = System::new(x.into_owned(), y.into_owned(), SBP4);
group.bench_function("advance_trad4", |b| {
b.iter(|| {
universe.init_with_vortex(0.0, 0.0);
advance_system(&mut universe, black_box(20))
})
});
group.finish();
let mut group = c.benchmark_group("adaptive integration");
group.sample_size(10);
let mut universe = System::new(x.into_owned(), y.into_owned(), Upwind4);
group.bench_function("static dt", |b| {
b.iter(|| {
universe.init_with_vortex(0.0, 0.0);
advance_embedded(&mut universe, false);
})
});
group.bench_function("adaptive dt", |b| {
b.iter(|| {
universe.init_with_vortex(0.0, 0.0);
advance_embedded(&mut universe, true);
})
});
group.finish();
}
criterion_group!(benches, performance_benchmark);
criterion_main!(benches);

1157
euler/src/lib.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,83 @@
#![cfg(feature = "expensive_tests")]
use euler::*;
use ndarray::prelude::*;
use sbp::Float;
fn run_with_size(size: usize, op: impl sbp::operators::UpwindOperator2d + Copy) -> Float {
let nx = size;
let ny = size;
let x = Array1::linspace(-5.0, 5.0, nx);
let y = Array1::linspace(-5.0, 5.0, ny);
let x = x.broadcast((ny, nx)).unwrap().to_owned();
let y = y
.reversed_axes()
.broadcast((nx, ny))
.unwrap()
.reversed_axes()
.to_owned();
let vortex_params = VortexParameters {
vortices: {
let mut v = ArrayVec::new();
v.push(Vortice {
x0: -1.0,
y0: 0.0,
rstar: 0.5,
eps: 1.0,
});
v
},
mach: 0.5,
};
let mut sys = System::new(x, y, op);
sys.vortex(0.0, vortex_params.clone());
let time = 0.2;
let dt = 0.2 * Float::min(1.0 / (nx - 1) as Float, 1.0 / (ny - 1) as Float);
let nsteps = (time / dt) as usize;
for _ in 0..nsteps {
sys.advance_upwind(dt);
}
let mut verifield = Field::new(ny, nx);
verifield.vortex(sys.x(), sys.y(), nsteps as Float * dt, &vortex_params);
verifield.h2_err(sys.field(), &op)
}
fn convergence(op: impl sbp::operators::UpwindOperator2d + Copy) {
let sizes = [25, 35, 50, 71, 100, 150, 200];
let mut prev: Option<(usize, Float)> = None;
println!("Size\tError(h2)\tq");
for size in &sizes {
print!("{:3}x{:3}", size, size);
let e = run_with_size(*size, op);
print!("\t{:.10}", e);
if let Some(prev) = prev.take() {
let m0 = size * size;
let e0 = e;
let (size1, e1) = prev;
let m1 = size1 * size1;
let q =
Float::log10(e0 / e1) / Float::log10((m0 as Float / m1 as Float).powf(1.0 / 2.0));
print!("\t{}", q);
}
println!();
prev = Some((*size, e));
}
}
#[test]
fn convergence_upwind4() {
convergence(sbp::operators::Upwind4);
}
#[test]
fn convergence_upwind9() {
convergence(sbp::operators::Upwind9);
}