2020-01-29 18:36:16 +00:00
|
|
|
#![allow(clippy::excessive_precision)]
|
|
|
|
#![allow(clippy::unreadable_literal)]
|
|
|
|
|
2020-02-27 19:26:43 +00:00
|
|
|
use crate::Float;
|
|
|
|
|
2020-04-08 21:07:14 +00:00
|
|
|
use ndarray::{ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2};
|
2019-08-09 14:49:19 +00:00
|
|
|
|
2020-04-02 21:36:20 +00:00
|
|
|
pub trait SbpOperator: Send + Sync {
|
2020-04-08 21:07:14 +00:00
|
|
|
fn diff1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>);
|
|
|
|
fn diffxi(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
|
|
|
assert_eq!(prev.shape(), fut.shape());
|
|
|
|
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
|
|
|
|
Self::diff1d(r0, r1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn diffeta(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
|
|
|
Self::diffxi(prev.reversed_axes(), fut.reversed_axes())
|
|
|
|
}
|
2020-02-27 19:26:43 +00:00
|
|
|
fn h() -> &'static [Float];
|
2019-08-09 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 19:32:38 +00:00
|
|
|
pub trait UpwindOperator: SbpOperator {
|
2020-04-08 21:07:14 +00:00
|
|
|
fn diss1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>);
|
|
|
|
fn dissxi(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
|
|
|
assert_eq!(prev.shape(), fut.shape());
|
|
|
|
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
|
|
|
|
Self::diss1d(r0, r1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn disseta(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
|
|
|
Self::dissxi(prev.reversed_axes(), fut.reversed_axes())
|
|
|
|
}
|
2019-12-12 19:32:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 23:29:54 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! diff_op_1d {
|
2020-04-08 21:07:14 +00:00
|
|
|
($name: ident, $BLOCK: expr, $DIAG: expr, $symmetric: expr) => {
|
|
|
|
fn $name(prev: ArrayView1<Float>, mut fut: ArrayViewMut1<Float>) {
|
|
|
|
assert_eq!(prev.shape(), fut.shape());
|
|
|
|
let nx = prev.shape()[0];
|
|
|
|
assert!(nx >= 2 * $BLOCK.len());
|
2019-12-13 23:29:54 +00:00
|
|
|
|
2020-04-08 21:07:14 +00:00
|
|
|
let dx = 1.0 / (nx - 1) as Float;
|
|
|
|
let idx = 1.0 / dx;
|
2019-12-13 23:29:54 +00:00
|
|
|
|
2020-04-08 21:07:14 +00:00
|
|
|
let block = ::ndarray::arr2($BLOCK);
|
|
|
|
let diag = ::ndarray::arr1($DIAG);
|
2019-12-13 23:29:54 +00:00
|
|
|
|
|
|
|
|
2020-04-08 21:07:14 +00:00
|
|
|
let first_elems = prev.slice(::ndarray::s!(..block.len_of(::ndarray::Axis(1))));
|
|
|
|
for (bl, f) in block.outer_iter().zip(&mut fut) {
|
|
|
|
let diff = first_elems.dot(&bl);
|
|
|
|
*f = diff * idx;
|
|
|
|
}
|
2019-12-13 23:29:54 +00:00
|
|
|
|
2020-04-08 21:07:14 +00:00
|
|
|
// The window needs to be aligned to the diagonal elements,
|
|
|
|
// based on the block size
|
|
|
|
let window_elems_to_skip =
|
|
|
|
block.len_of(::ndarray::Axis(0)) - ((diag.len() - 1) / 2);
|
2019-12-13 23:29:54 +00:00
|
|
|
|
2020-04-08 21:07:14 +00:00
|
|
|
for (window, f) in prev
|
|
|
|
.windows(diag.len())
|
|
|
|
.into_iter()
|
|
|
|
.skip(window_elems_to_skip)
|
|
|
|
.zip(fut.iter_mut().skip(block.len_of(::ndarray::Axis(0))))
|
|
|
|
.take(nx - 2 * block.len_of(::ndarray::Axis(0)))
|
|
|
|
{
|
|
|
|
let diff = diag.dot(&window);
|
|
|
|
*f = diff * idx;
|
|
|
|
}
|
2019-12-13 23:29:54 +00:00
|
|
|
|
2020-04-08 21:07:14 +00:00
|
|
|
let last_elems = prev.slice(::ndarray::s!(nx - block.len_of(::ndarray::Axis(1))..;-1));
|
|
|
|
for (bl, f) in block.outer_iter()
|
|
|
|
.zip(&mut fut.slice_mut(s![nx - block.len_of(::ndarray::Axis(0))..;-1]))
|
|
|
|
{
|
|
|
|
let diff = if $symmetric {
|
|
|
|
bl.dot(&last_elems)
|
|
|
|
} else {
|
|
|
|
-bl.dot(&last_elems)
|
|
|
|
};
|
|
|
|
*f = diff * idx;
|
2019-12-13 23:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-03 17:41:49 +00:00
|
|
|
mod upwind4;
|
|
|
|
pub use upwind4::Upwind4;
|
2019-12-15 16:49:41 +00:00
|
|
|
mod upwind9;
|
|
|
|
pub use upwind9::Upwind9;
|
2019-12-15 16:27:31 +00:00
|
|
|
mod traditional4;
|
|
|
|
pub use traditional4::SBP4;
|
2019-12-15 16:49:41 +00:00
|
|
|
mod traditional8;
|
|
|
|
pub use traditional8::SBP8;
|
2020-02-29 09:53:20 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod testing {
|
|
|
|
use super::*;
|
|
|
|
use ndarray::prelude::*;
|
|
|
|
pub(crate) fn grid_eval<F: Fn(Float, Float) -> Float>(
|
|
|
|
n: (usize, usize),
|
|
|
|
f: F,
|
|
|
|
) -> Array2<Float> {
|
|
|
|
let nx = n.1;
|
|
|
|
let dx = 1.0 / (nx - 1) as Float;
|
|
|
|
let ny = n.0;
|
|
|
|
let dy = 1.0 / (ny - 1) as Float;
|
|
|
|
Array2::from_shape_fn(n, |(j, i)| {
|
|
|
|
let x = dx * i as Float;
|
|
|
|
let y = dy * j as Float;
|
|
|
|
f(x, y)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn check_operator_on<SBP, F, FX, FY>(
|
|
|
|
n: (usize, usize),
|
|
|
|
f: F,
|
|
|
|
dfdx: FX,
|
|
|
|
dfdy: FY,
|
|
|
|
eps: Float,
|
|
|
|
) where
|
|
|
|
SBP: SbpOperator,
|
|
|
|
F: Fn(Float, Float) -> Float,
|
|
|
|
FX: Fn(Float, Float) -> Float,
|
|
|
|
FY: Fn(Float, Float) -> Float,
|
|
|
|
{
|
|
|
|
let mut y = Array2::zeros(n);
|
|
|
|
let x = grid_eval(n, f);
|
|
|
|
|
|
|
|
y.fill(0.0);
|
|
|
|
SBP::diffxi(x.view(), y.view_mut());
|
|
|
|
approx::assert_abs_diff_eq!(&y, &grid_eval(n, dfdx), epsilon = eps);
|
|
|
|
|
|
|
|
y.fill(0.0);
|
|
|
|
SBP::diffeta(x.view(), y.view_mut());
|
|
|
|
approx::assert_abs_diff_eq!(&y, &grid_eval(n, dfdy), epsilon = eps);
|
|
|
|
}
|
|
|
|
}
|