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-15 15:17:48 +00:00
|
|
|
pub trait SbpOperator1d: Send + Sync {
|
2020-04-14 22:12:54 +00:00
|
|
|
fn diff(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>);
|
|
|
|
|
|
|
|
fn h(&self) -> &'static [Float];
|
|
|
|
fn is_h2(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:17:48 +00:00
|
|
|
pub trait SbpOperator2d: Send + Sync {
|
2020-04-14 22:12:54 +00:00
|
|
|
fn diffxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>);
|
|
|
|
fn diffeta(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>);
|
|
|
|
|
|
|
|
fn hxi(&self) -> &'static [Float];
|
|
|
|
fn heta(&self) -> &'static [Float];
|
|
|
|
|
|
|
|
fn is_h2xi(&self) -> bool;
|
|
|
|
fn is_h2eta(&self) -> bool;
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:17:48 +00:00
|
|
|
impl<SBPeta: SbpOperator1d, SBPxi: SbpOperator1d> SbpOperator2d for (&SBPeta, &SBPxi) {
|
2020-04-14 22:37:46 +00:00
|
|
|
default fn diffxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
2020-04-14 22:12:54 +00:00
|
|
|
assert_eq!(prev.shape(), fut.shape());
|
|
|
|
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
|
|
|
|
self.1.diff(r0, r1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn diffeta(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
|
|
|
let ba = (self.1, self.0);
|
|
|
|
ba.diffxi(prev.reversed_axes(), fut.reversed_axes())
|
|
|
|
}
|
|
|
|
fn hxi(&self) -> &'static [Float] {
|
|
|
|
self.1.h()
|
|
|
|
}
|
|
|
|
fn heta(&self) -> &'static [Float] {
|
|
|
|
self.0.h()
|
|
|
|
}
|
|
|
|
fn is_h2xi(&self) -> bool {
|
|
|
|
self.1.is_h2()
|
|
|
|
}
|
|
|
|
fn is_h2eta(&self) -> bool {
|
|
|
|
self.0.is_h2()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:17:48 +00:00
|
|
|
impl<SBP: SbpOperator1d + Copy> SbpOperator2d for SBP {
|
2020-04-14 22:37:46 +00:00
|
|
|
fn diffxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
2020-04-15 15:17:48 +00:00
|
|
|
<(&SBP, &SBP) as SbpOperator2d>::diffxi(&(self, self), prev, fut)
|
2020-04-08 21:07:14 +00:00
|
|
|
}
|
2020-04-14 19:59:02 +00:00
|
|
|
fn diffeta(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
2020-04-15 15:17:48 +00:00
|
|
|
<(&SBP, &SBP) as SbpOperator2d>::diffeta(&(self, self), prev, fut)
|
2020-04-08 21:07:14 +00:00
|
|
|
}
|
2020-04-14 22:12:54 +00:00
|
|
|
fn hxi(&self) -> &'static [Float] {
|
2020-04-15 15:17:48 +00:00
|
|
|
<(&SBP, &SBP) as SbpOperator2d>::hxi(&(self, self))
|
2020-04-14 22:12:54 +00:00
|
|
|
}
|
|
|
|
fn heta(&self) -> &'static [Float] {
|
2020-04-15 15:17:48 +00:00
|
|
|
<(&SBP, &SBP) as SbpOperator2d>::heta(&(self, self))
|
2020-04-14 22:12:54 +00:00
|
|
|
}
|
|
|
|
fn is_h2xi(&self) -> bool {
|
2020-04-15 15:17:48 +00:00
|
|
|
<(&SBP, &SBP) as SbpOperator2d>::is_h2xi(&(self, self))
|
2020-04-14 22:12:54 +00:00
|
|
|
}
|
|
|
|
fn is_h2eta(&self) -> bool {
|
2020-04-15 15:17:48 +00:00
|
|
|
<(&SBP, &SBP) as SbpOperator2d>::is_h2eta(&(self, self))
|
2020-04-14 22:12:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:17:48 +00:00
|
|
|
pub trait UpwindOperator1d: SbpOperator1d + Send + Sync {
|
2020-04-14 22:12:54 +00:00
|
|
|
fn diss(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>);
|
2020-04-15 15:36:45 +00:00
|
|
|
fn as_sbp(&self) -> &dyn SbpOperator1d;
|
2020-04-14 22:12:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 15:17:48 +00:00
|
|
|
pub trait UpwindOperator2d: SbpOperator2d + Send + Sync {
|
2020-04-14 22:12:54 +00:00
|
|
|
fn dissxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>);
|
|
|
|
fn disseta(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>);
|
2020-04-15 15:36:45 +00:00
|
|
|
fn as_sbp(&self) -> &dyn SbpOperator2d;
|
2020-04-14 22:12:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 15:17:48 +00:00
|
|
|
impl<UOeta: UpwindOperator1d, UOxi: UpwindOperator1d> UpwindOperator2d for (&UOeta, &UOxi) {
|
2020-04-14 22:37:46 +00:00
|
|
|
default fn dissxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
2020-04-14 22:12:54 +00:00
|
|
|
assert_eq!(prev.shape(), fut.shape());
|
|
|
|
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
|
|
|
|
self.1.diss(r0, r1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn disseta(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
|
|
|
let ba = (self.1, self.0);
|
|
|
|
ba.dissxi(prev.reversed_axes(), fut.reversed_axes())
|
2020-04-12 17:27:18 +00:00
|
|
|
}
|
2020-04-15 15:36:45 +00:00
|
|
|
fn as_sbp(&self) -> &dyn SbpOperator2d {
|
|
|
|
self
|
|
|
|
}
|
2019-08-09 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 15:17:48 +00:00
|
|
|
impl<UO: UpwindOperator1d + Copy> UpwindOperator2d for UO {
|
2020-04-14 22:37:46 +00:00
|
|
|
fn dissxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
2020-04-15 15:17:48 +00:00
|
|
|
<(&UO, &UO) as UpwindOperator2d>::dissxi(&(self, self), prev, fut)
|
2020-04-08 21:07:14 +00:00
|
|
|
}
|
2020-04-14 19:59:02 +00:00
|
|
|
fn disseta(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
2020-04-15 15:17:48 +00:00
|
|
|
<(&UO, &UO) as UpwindOperator2d>::disseta(&(self, self), prev, fut)
|
2020-04-08 21:07:14 +00:00
|
|
|
}
|
2020-04-15 15:36:45 +00:00
|
|
|
fn as_sbp(&self) -> &dyn SbpOperator2d {
|
|
|
|
self
|
|
|
|
}
|
2019-12-12 19:32:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 13:19:34 +00:00
|
|
|
pub trait InterpolationOperator: Send + Sync {
|
2020-04-13 18:56:29 +00:00
|
|
|
fn fine2coarse(&self, fine: ArrayView1<Float>, coarse: ArrayViewMut1<Float>);
|
|
|
|
fn coarse2fine(&self, coarse: ArrayView1<Float>, fine: ArrayViewMut1<Float>);
|
2020-04-11 13:19:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 15:42:18 +00:00
|
|
|
#[inline(always)]
|
2020-04-30 22:09:46 +00:00
|
|
|
fn diff_op_1d(
|
2020-04-21 20:15:47 +00:00
|
|
|
block: &[&[Float]],
|
|
|
|
diag: &[Float],
|
2020-04-30 22:09:46 +00:00
|
|
|
symmetry: Symmetry,
|
|
|
|
optype: OperatorType,
|
2020-04-12 15:42:18 +00:00
|
|
|
prev: ArrayView1<Float>,
|
|
|
|
mut fut: ArrayViewMut1<Float>,
|
|
|
|
) {
|
|
|
|
assert_eq!(prev.shape(), fut.shape());
|
|
|
|
let nx = prev.shape()[0];
|
2020-04-21 20:15:47 +00:00
|
|
|
assert!(nx >= 2 * block.len());
|
2020-04-12 15:42:18 +00:00
|
|
|
|
2020-04-30 22:09:46 +00:00
|
|
|
let dx = if optype == OperatorType::H2 {
|
2020-04-12 17:27:18 +00:00
|
|
|
1.0 / (nx - 2) as Float
|
|
|
|
} else {
|
|
|
|
1.0 / (nx - 1) as Float
|
|
|
|
};
|
2020-04-12 15:42:18 +00:00
|
|
|
let idx = 1.0 / dx;
|
|
|
|
|
2020-04-21 20:15:47 +00:00
|
|
|
for (bl, f) in block.iter().zip(&mut fut) {
|
|
|
|
let diff = bl
|
|
|
|
.iter()
|
|
|
|
.zip(prev.iter())
|
|
|
|
.map(|(x, y)| x * y)
|
|
|
|
.sum::<Float>();
|
2020-04-12 15:42:18 +00:00
|
|
|
*f = diff * idx;
|
|
|
|
}
|
2019-12-13 23:29:54 +00:00
|
|
|
|
2020-04-12 15:42:18 +00:00
|
|
|
// The window needs to be aligned to the diagonal elements,
|
|
|
|
// based on the block size
|
2020-04-21 20:15:47 +00:00
|
|
|
let window_elems_to_skip = block.len() - ((diag.len() - 1) / 2);
|
2020-04-12 15:42:18 +00:00
|
|
|
|
|
|
|
for (window, f) in prev
|
|
|
|
.windows(diag.len())
|
|
|
|
.into_iter()
|
|
|
|
.skip(window_elems_to_skip)
|
2020-04-21 20:15:47 +00:00
|
|
|
.zip(fut.iter_mut().skip(block.len()))
|
|
|
|
.take(nx - 2 * block.len())
|
2020-04-12 15:42:18 +00:00
|
|
|
{
|
2020-04-21 20:15:47 +00:00
|
|
|
let diff = diag.iter().zip(&window).map(|(x, y)| x * y).sum::<Float>();
|
2020-04-12 15:42:18 +00:00
|
|
|
*f = diff * idx;
|
|
|
|
}
|
2019-12-13 23:29:54 +00:00
|
|
|
|
2020-04-21 20:15:47 +00:00
|
|
|
for (bl, f) in block.iter().zip(fut.iter_mut().rev()) {
|
|
|
|
let diff = bl
|
|
|
|
.iter()
|
|
|
|
.zip(prev.iter().rev())
|
|
|
|
.map(|(x, y)| x * y)
|
|
|
|
.sum::<Float>();
|
|
|
|
|
2020-04-30 22:09:46 +00:00
|
|
|
*f = idx
|
|
|
|
* if symmetry == Symmetry::Symmetric {
|
|
|
|
diff
|
|
|
|
} else {
|
|
|
|
-diff
|
|
|
|
};
|
2020-04-12 15:42:18 +00:00
|
|
|
}
|
2019-12-13 23:29:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 22:09:46 +00:00
|
|
|
#[derive(PartialEq, Copy, Clone)]
|
|
|
|
enum Symmetry {
|
|
|
|
Symmetric,
|
|
|
|
AntiSymmetric,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Copy, Clone)]
|
|
|
|
enum OperatorType {
|
|
|
|
Normal,
|
|
|
|
H2,
|
|
|
|
}
|
|
|
|
|
2020-05-01 15:44:33 +00:00
|
|
|
#[inline(always)]
|
|
|
|
#[allow(unused)]
|
|
|
|
fn diff_op_col_naive(
|
|
|
|
block: &'static [&'static [Float]],
|
|
|
|
diag: &'static [Float],
|
|
|
|
symmetry: Symmetry,
|
|
|
|
optype: OperatorType,
|
|
|
|
) -> impl Fn(ArrayView2<Float>, ArrayViewMut2<Float>) {
|
|
|
|
#[inline(always)]
|
|
|
|
move |prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>| {
|
|
|
|
assert_eq!(prev.shape(), fut.shape());
|
|
|
|
let nx = prev.shape()[1];
|
|
|
|
assert!(nx >= 2 * block.len());
|
|
|
|
|
|
|
|
assert_eq!(prev.strides()[0], 1);
|
|
|
|
assert_eq!(fut.strides()[0], 1);
|
|
|
|
|
|
|
|
let dx = if optype == OperatorType::H2 {
|
|
|
|
1.0 / (nx - 2) as Float
|
|
|
|
} else {
|
|
|
|
1.0 / (nx - 1) as Float
|
|
|
|
};
|
|
|
|
let idx = 1.0 / dx;
|
|
|
|
|
|
|
|
fut.fill(0.0);
|
|
|
|
|
|
|
|
// First block
|
|
|
|
for (bl, mut fut) in block.iter().zip(fut.axis_iter_mut(ndarray::Axis(1))) {
|
|
|
|
debug_assert_eq!(fut.len(), prev.shape()[0]);
|
|
|
|
for (&bl, prev) in bl.iter().zip(prev.axis_iter(ndarray::Axis(1))) {
|
|
|
|
debug_assert_eq!(prev.len(), fut.len());
|
|
|
|
fut.scaled_add(idx * bl, &prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let half_diag_width = (diag.len() - 1) / 2;
|
|
|
|
assert!(half_diag_width <= block.len());
|
|
|
|
|
|
|
|
// Diagonal entries
|
|
|
|
for (ifut, mut fut) in fut
|
|
|
|
.axis_iter_mut(ndarray::Axis(1))
|
|
|
|
.enumerate()
|
|
|
|
.skip(block.len())
|
|
|
|
.take(nx - 2 * block.len())
|
|
|
|
{
|
|
|
|
for (id, &d) in diag.iter().enumerate() {
|
|
|
|
let offset = ifut - half_diag_width + id;
|
|
|
|
fut.scaled_add(idx * d, &prev.slice(ndarray::s![.., offset]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// End block
|
|
|
|
for (bl, mut fut) in block.iter().zip(fut.axis_iter_mut(ndarray::Axis(1)).rev()) {
|
|
|
|
fut.fill(0.0);
|
|
|
|
for (&bl, prev) in bl.iter().zip(prev.axis_iter(ndarray::Axis(1)).rev()) {
|
|
|
|
if symmetry == Symmetry::Symmetric {
|
|
|
|
fut.scaled_add(idx * bl, &prev);
|
|
|
|
} else {
|
|
|
|
fut.scaled_add(-idx * bl, &prev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:32:49 +00:00
|
|
|
#[inline(always)]
|
2020-04-30 22:09:46 +00:00
|
|
|
fn diff_op_col(
|
2020-04-29 17:59:48 +00:00
|
|
|
block: &'static [&'static [Float]],
|
|
|
|
diag: &'static [Float],
|
2020-04-30 22:09:46 +00:00
|
|
|
symmetry: Symmetry,
|
|
|
|
optype: OperatorType,
|
2020-05-01 15:44:33 +00:00
|
|
|
) -> impl Fn(ArrayView2<Float>, ArrayViewMut2<Float>) {
|
|
|
|
diff_op_col_simd(block, diag, symmetry, optype)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn diff_op_col_simd(
|
|
|
|
block: &'static [&'static [Float]],
|
|
|
|
diag: &'static [Float],
|
|
|
|
symmetry: Symmetry,
|
|
|
|
optype: OperatorType,
|
2020-04-29 17:59:48 +00:00
|
|
|
) -> impl Fn(ArrayView2<Float>, ArrayViewMut2<Float>) {
|
|
|
|
#[inline(always)]
|
|
|
|
move |prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>| {
|
|
|
|
assert_eq!(prev.shape(), fut.shape());
|
|
|
|
let nx = prev.shape()[1];
|
|
|
|
assert!(nx >= 2 * block.len());
|
2020-04-21 22:32:49 +00:00
|
|
|
|
2020-04-29 17:59:48 +00:00
|
|
|
assert_eq!(prev.strides()[0], 1);
|
|
|
|
assert_eq!(fut.strides()[0], 1);
|
2020-04-21 22:32:49 +00:00
|
|
|
|
2020-04-30 22:09:46 +00:00
|
|
|
let dx = if optype == OperatorType::H2 {
|
2020-04-29 17:59:48 +00:00
|
|
|
1.0 / (nx - 2) as Float
|
|
|
|
} else {
|
|
|
|
1.0 / (nx - 1) as Float
|
|
|
|
};
|
|
|
|
let idx = 1.0 / dx;
|
2020-04-21 22:32:49 +00:00
|
|
|
|
2020-04-29 21:06:19 +00:00
|
|
|
#[cfg(not(feature = "f32"))]
|
|
|
|
type SimdT = packed_simd::f64x8;
|
|
|
|
#[cfg(feature = "f32")]
|
|
|
|
type SimdT = packed_simd::f32x16;
|
|
|
|
|
|
|
|
let ny = prev.shape()[0];
|
2020-05-01 16:21:14 +00:00
|
|
|
// How many elements that can be simdified
|
|
|
|
let simdified = SimdT::lanes() * (ny / SimdT::lanes());
|
2020-04-21 22:32:49 +00:00
|
|
|
|
2020-04-29 17:59:48 +00:00
|
|
|
// First block
|
2020-05-01 16:21:14 +00:00
|
|
|
{
|
|
|
|
for (bl, mut fut) in block.iter().zip(fut.axis_iter_mut(ndarray::Axis(1))) {
|
|
|
|
fut.fill(0.0);
|
|
|
|
debug_assert_eq!(fut.len(), prev.shape()[0]);
|
|
|
|
for (&bl, prev) in bl.iter().zip(prev.axis_iter(ndarray::Axis(1))) {
|
|
|
|
debug_assert_eq!(prev.len(), fut.len());
|
|
|
|
fut.scaled_add(idx * bl, &prev);
|
|
|
|
}
|
2020-04-29 17:59:48 +00:00
|
|
|
}
|
2020-04-21 22:32:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 16:21:14 +00:00
|
|
|
// Diagonal elements
|
|
|
|
{
|
|
|
|
let half_diag_width = (diag.len() - 1) / 2;
|
|
|
|
assert!(half_diag_width <= block.len());
|
|
|
|
|
|
|
|
let fut_base_ptr = fut.as_mut_ptr();
|
|
|
|
let fut_stride = fut.strides()[1];
|
|
|
|
let fut_ptr = |j, i| {
|
|
|
|
debug_assert!(j < ny && i < nx);
|
|
|
|
unsafe { fut_base_ptr.offset(fut_stride * i as isize + j as isize) }
|
|
|
|
};
|
2020-04-29 17:59:48 +00:00
|
|
|
|
2020-05-01 16:21:14 +00:00
|
|
|
let prev_base_ptr = prev.as_ptr();
|
|
|
|
let prev_stride = prev.strides()[1];
|
|
|
|
let prev_ptr = |j, i| {
|
|
|
|
debug_assert!(j < ny && i < nx);
|
|
|
|
unsafe { prev_base_ptr.offset(prev_stride * i as isize + j as isize) }
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(fut_stride, prev_stride);
|
|
|
|
|
|
|
|
for ifut in block.len()..nx - block.len() {
|
|
|
|
for j in (0..simdified).step_by(SimdT::lanes()) {
|
|
|
|
let index_to_simd = |(j, i)| unsafe {
|
|
|
|
// j never moves past end of slice due to step_by and
|
|
|
|
// rounding down
|
|
|
|
SimdT::from_slice_unaligned(std::slice::from_raw_parts(
|
|
|
|
prev_ptr(j, i),
|
|
|
|
SimdT::lanes(),
|
|
|
|
))
|
|
|
|
};
|
|
|
|
let mut f = SimdT::splat(0.0);
|
|
|
|
for (id, &d) in diag.iter().enumerate() {
|
|
|
|
let offset = ifut - half_diag_width + id;
|
|
|
|
f = index_to_simd((j, offset)).mul_adde(SimdT::splat(d), f);
|
|
|
|
}
|
|
|
|
f = f * idx;
|
|
|
|
unsafe {
|
|
|
|
// puts simd along stride 1, j never goes past end of slice
|
|
|
|
f.write_to_slice_unaligned(std::slice::from_raw_parts_mut(
|
|
|
|
fut_ptr(j, ifut),
|
|
|
|
SimdT::lanes(),
|
|
|
|
));
|
|
|
|
}
|
2020-04-29 21:06:19 +00:00
|
|
|
}
|
2020-05-01 16:21:14 +00:00
|
|
|
for j in simdified..ny {
|
|
|
|
let mut f = 0.0;
|
|
|
|
for (id, &d) in diag.iter().enumerate() {
|
|
|
|
let offset = ifut - half_diag_width + id;
|
|
|
|
unsafe {
|
|
|
|
f += d * *prev_ptr(j, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
*fut_ptr(j, ifut) = idx * f;
|
|
|
|
}
|
2020-04-29 21:06:19 +00:00
|
|
|
}
|
2020-04-29 17:59:48 +00:00
|
|
|
}
|
2020-04-21 22:32:49 +00:00
|
|
|
}
|
2020-05-01 16:21:14 +00:00
|
|
|
|
2020-04-29 17:59:48 +00:00
|
|
|
// End block
|
2020-05-01 16:21:14 +00:00
|
|
|
{
|
|
|
|
for (bl, mut fut) in block.iter().zip(fut.axis_iter_mut(ndarray::Axis(1)).rev()) {
|
|
|
|
fut.fill(0.0);
|
|
|
|
for (&bl, prev) in bl.iter().zip(prev.axis_iter(ndarray::Axis(1)).rev()) {
|
|
|
|
if symmetry == Symmetry::Symmetric {
|
|
|
|
fut.scaled_add(idx * bl, &prev);
|
|
|
|
} else {
|
|
|
|
fut.scaled_add(-idx * bl, &prev);
|
|
|
|
}
|
2020-04-29 17:59:48 +00:00
|
|
|
}
|
2020-04-21 22:32:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 21:19:02 +00:00
|
|
|
#[inline(always)]
|
2020-04-30 22:09:46 +00:00
|
|
|
fn diff_op_row(
|
2020-04-29 17:52:55 +00:00
|
|
|
block: &'static [&'static [Float]],
|
|
|
|
diag: &'static [Float],
|
2020-04-30 22:09:46 +00:00
|
|
|
symmetry: Symmetry,
|
|
|
|
optype: OperatorType,
|
2020-04-29 17:52:55 +00:00
|
|
|
) -> impl Fn(ArrayView2<Float>, ArrayViewMut2<Float>) {
|
|
|
|
#[inline(always)]
|
|
|
|
move |prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>| {
|
|
|
|
assert_eq!(prev.shape(), fut.shape());
|
|
|
|
let nx = prev.shape()[1];
|
|
|
|
assert!(nx >= 2 * block.len());
|
|
|
|
|
|
|
|
assert_eq!(prev.strides()[1], 1);
|
|
|
|
assert_eq!(fut.strides()[1], 1);
|
|
|
|
|
2020-04-30 22:09:46 +00:00
|
|
|
let dx = if optype == OperatorType::H2 {
|
2020-04-29 17:52:55 +00:00
|
|
|
1.0 / (nx - 2) as Float
|
|
|
|
} else {
|
|
|
|
1.0 / (nx - 1) as Float
|
|
|
|
};
|
|
|
|
let idx = 1.0 / dx;
|
|
|
|
|
|
|
|
for (prev, mut fut) in prev
|
|
|
|
.axis_iter(ndarray::Axis(0))
|
|
|
|
.zip(fut.axis_iter_mut(ndarray::Axis(0)))
|
2020-04-21 21:19:02 +00:00
|
|
|
{
|
2020-04-29 17:52:55 +00:00
|
|
|
let prev = prev.as_slice().unwrap();
|
|
|
|
let fut = fut.as_slice_mut().unwrap();
|
|
|
|
|
|
|
|
for (bl, f) in block.iter().zip(fut.iter_mut()) {
|
|
|
|
let diff = bl
|
|
|
|
.iter()
|
|
|
|
.zip(prev.iter())
|
|
|
|
.map(|(x, y)| x * y)
|
|
|
|
.sum::<Float>();
|
|
|
|
*f = diff * idx;
|
|
|
|
}
|
2020-04-21 21:19:02 +00:00
|
|
|
|
2020-04-29 17:52:55 +00:00
|
|
|
// The window needs to be aligned to the diagonal elements,
|
|
|
|
// based on the block size
|
|
|
|
let window_elems_to_skip = block.len() - ((diag.len() - 1) / 2);
|
|
|
|
|
|
|
|
for (window, f) in prev
|
|
|
|
.windows(diag.len())
|
|
|
|
.skip(window_elems_to_skip)
|
|
|
|
.zip(fut.iter_mut().skip(block.len()))
|
|
|
|
.take(nx - 2 * block.len())
|
|
|
|
{
|
|
|
|
let diff = diag.iter().zip(window).map(|(&x, &y)| x * y).sum::<Float>();
|
|
|
|
*f = diff * idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (bl, f) in block.iter().zip(fut.iter_mut().rev()) {
|
|
|
|
let diff = bl
|
|
|
|
.iter()
|
|
|
|
.zip(prev.iter().rev())
|
|
|
|
.map(|(x, y)| x * y)
|
|
|
|
.sum::<Float>();
|
2020-04-21 21:19:02 +00:00
|
|
|
|
2020-04-30 22:09:46 +00:00
|
|
|
*f = idx
|
|
|
|
* if symmetry == Symmetry::Symmetric {
|
|
|
|
diff
|
|
|
|
} else {
|
|
|
|
-diff
|
|
|
|
};
|
2020-04-29 17:52:55 +00:00
|
|
|
}
|
2020-04-21 21:19:02 +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;
|
2020-04-12 17:27:18 +00:00
|
|
|
|
|
|
|
mod upwind4h2;
|
|
|
|
pub use upwind4h2::Upwind4h2;
|
2020-04-13 20:08:18 +00:00
|
|
|
mod upwind9h2;
|
|
|
|
pub use upwind9h2::Upwind9h2;
|
2020-04-12 17:27:18 +00:00
|
|
|
|
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
|
|
|
|
2020-04-11 13:19:34 +00:00
|
|
|
mod interpolation;
|
2020-04-15 21:58:39 +00:00
|
|
|
pub use interpolation::{Interpolation4, Interpolation8, Interpolation9, Interpolation9h2};
|
2020-04-11 13:19:34 +00:00
|
|
|
|
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>(
|
2020-04-14 19:59:02 +00:00
|
|
|
op: SBP,
|
2020-02-29 09:53:20 +00:00
|
|
|
n: (usize, usize),
|
|
|
|
f: F,
|
|
|
|
dfdx: FX,
|
|
|
|
dfdy: FY,
|
|
|
|
eps: Float,
|
|
|
|
) where
|
2020-04-14 22:12:54 +00:00
|
|
|
SBP: SbpOperator2d,
|
2020-02-29 09:53:20 +00:00
|
|
|
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);
|
2020-04-14 19:59:02 +00:00
|
|
|
op.diffxi(x.view(), y.view_mut());
|
2020-02-29 09:53:20 +00:00
|
|
|
approx::assert_abs_diff_eq!(&y, &grid_eval(n, dfdx), epsilon = eps);
|
|
|
|
|
|
|
|
y.fill(0.0);
|
2020-04-14 19:59:02 +00:00
|
|
|
op.diffeta(x.view(), y.view_mut());
|
2020-02-29 09:53:20 +00:00
|
|
|
approx::assert_abs_diff_eq!(&y, &grid_eval(n, dfdy), epsilon = eps);
|
|
|
|
}
|
|
|
|
}
|