Merge branch 'feature/const_matrix'

Using constr generics improves performance of diffxi by about
15% without fast-float and to 30% with the fast-float flag
This commit is contained in:
Magnus Ulimoen 2021-02-10 21:25:08 +01:00
commit dc94aa8e6b
12 changed files with 1415 additions and 1089 deletions

View File

@ -10,12 +10,13 @@ approx = "0.3.2"
packed_simd = { version = "0.3.3", package = "packed_simd_2" }
rayon = { version = "1.3.0", optional = true }
sprs = { version = "0.9.0", optional = true, default-features = false }
num-traits = "0.2.11"
serde = { version = "1.0.115", optional = true, default-features = false, features = ["derive"] }
num-traits = "0.2.14"
[features]
# Use f32 as precision, default is f64
f32 = []
fast-float = []
sparse = ["sprs"]
serde1 = ["serde", "ndarray/serde"]

View File

@ -1,4 +1,7 @@
#![feature(core_intrinsics)]
#![cfg_attr(feature = "fast-float", feature(core_intrinsics))]
#![feature(array_windows)]
#![feature(array_chunks)]
#![feature(const_fn_floating_point_arithmetic)]
/// Type used for floats, configure with the `f32` feature
#[cfg(feature = "f32")]

View File

@ -78,7 +78,9 @@ pub trait SbpOperator2d: Send + Sync {
}
fn op_xi(&self) -> &dyn SbpOperator1d;
fn op_eta(&self) -> &dyn SbpOperator1d;
fn op_eta(&self) -> &dyn SbpOperator1d {
self.op_xi()
}
fn upwind(&self) -> Option<Box<dyn UpwindOperator2d>> {
None
@ -98,7 +100,9 @@ pub trait UpwindOperator2d: Send + Sync {
}
fn op_xi(&self) -> &dyn UpwindOperator1d;
fn op_eta(&self) -> &dyn UpwindOperator1d;
fn op_eta(&self) -> &dyn UpwindOperator1d {
self.op_xi()
}
}
pub trait InterpolationOperator: Send + Sync {

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,332 @@
#![allow(unused)]
use num_traits::identities::Zero;
/// A row-major matrix
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[repr(C)]
pub struct Matrix<T, const M: usize, const N: usize> {
pub data: [[T; N]; M],
}
pub type RowVector<T, const N: usize> = Matrix<T, 1, N>;
pub type ColVector<T, const N: usize> = Matrix<T, N, 1>;
impl<T: Copy + Default, const M: usize, const N: usize> Default for Matrix<T, M, N> {
fn default() -> Self {
Self {
data: [[T::default(); N]; M],
}
}
}
impl<T: Copy + Zero + PartialEq, const M: usize, const N: usize> Zero for Matrix<T, M, N> {
fn zero() -> Self {
Self {
data: [[T::zero(); N]; M],
}
}
fn is_zero(&self) -> bool {
self == &Self::zero()
}
}
impl<T, const M: usize, const N: usize> core::ops::Index<(usize, usize)> for Matrix<T, M, N> {
type Output = T;
#[inline(always)]
fn index(&self, (i, j): (usize, usize)) -> &Self::Output {
&self.data[i][j]
}
}
impl<T, const M: usize, const N: usize> core::ops::IndexMut<(usize, usize)> for Matrix<T, M, N> {
#[inline(always)]
fn index_mut(&mut self, (i, j): (usize, usize)) -> &mut Self::Output {
&mut self.data[i][j]
}
}
impl<T, const M: usize, const N: usize> core::ops::Index<usize> for Matrix<T, M, N> {
type Output = [T; N];
#[inline(always)]
fn index(&self, i: usize) -> &Self::Output {
&self.data[i]
}
}
impl<T, const M: usize, const N: usize> core::ops::IndexMut<usize> for Matrix<T, M, N> {
#[inline(always)]
fn index_mut(&mut self, i: usize) -> &mut Self::Output {
&mut self.data[i]
}
}
impl<T, const M: usize, const N: usize> Matrix<T, M, N> {
pub const fn new(data: [[T; N]; M]) -> Self {
Self { data }
}
#[inline(always)]
pub const fn nrows(&self) -> usize {
M
}
#[inline(always)]
pub const fn ncols(&self) -> usize {
N
}
#[inline(always)]
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.data.iter().flatten()
}
#[inline(always)]
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
self.data.iter_mut().flatten()
}
#[inline(always)]
pub fn iter_rows(
&self,
) -> impl ExactSizeIterator<Item = &[T; N]> + DoubleEndedIterator<Item = &[T; N]> {
self.data.iter()
}
#[inline(always)]
pub const fn row(&self, idx: usize) -> &[T; N] {
&self.data[idx]
}
}
impl<T, const N: usize> ColVector<T, N> {
#[inline(always)]
pub fn map_to_col(slice: &[T; N]) -> &ColVector<T, N> {
unsafe { std::mem::transmute::<&[T; N], &Self>(slice) }
}
#[inline(always)]
pub fn map_to_col_mut(slice: &mut [T; N]) -> &mut ColVector<T, N> {
unsafe { std::mem::transmute::<&mut [T; N], &mut Self>(slice) }
}
}
impl<T, const N: usize> RowVector<T, N> {
pub fn map_to_row(slice: &[T; N]) -> &Self {
unsafe { std::mem::transmute::<&[T; N], &Self>(slice) }
}
pub fn map_to_row_mut(slice: &mut [T; N]) -> &mut Self {
unsafe { std::mem::transmute::<&mut [T; N], &mut Self>(slice) }
}
}
impl<T, const M: usize, const P: usize> Matrix<T, M, P> {
#[inline(always)]
pub fn matmul_into<const N: usize>(&mut self, lhs: &Matrix<T, M, N>, rhs: &Matrix<T, N, P>)
where
T: Default + Copy + core::ops::Mul<Output = T> + core::ops::Add<Output = T>,
{
for i in 0..M {
for j in 0..P {
let mut t = T::default();
for k in 0..N {
t = t + lhs[(i, k)] * rhs[(k, j)];
}
self[(i, j)] = t;
}
}
}
}
macro_rules! impl_op_mul_mul {
($lhs:ty, $rhs:ty) => {
impl<T, const N: usize, const M: usize, const P: usize> core::ops::Mul<$rhs> for $lhs
where
T: Copy + Default + core::ops::Add<Output = T> + core::ops::Mul<Output = T>,
{
type Output = Matrix<T, M, P>;
fn mul(self, lhs: $rhs) -> Self::Output {
let mut out = Matrix::default();
out.matmul_into(&self, &lhs);
out
}
}
};
}
impl_op_mul_mul! { Matrix<T, M, N>, Matrix<T, N, P> }
impl_op_mul_mul! { &Matrix<T, M, N>, Matrix<T, N, P> }
impl_op_mul_mul! { Matrix<T, M, N>, &Matrix<T, N, P> }
impl_op_mul_mul! { &Matrix<T, M, N>, &Matrix<T, N, P> }
impl<T, const M: usize, const N: usize> core::ops::MulAssign<T> for Matrix<T, M, N>
where
T: Copy + core::ops::MulAssign<T>,
{
#[inline(always)]
fn mul_assign(&mut self, other: T) {
self.iter_mut().for_each(|x| *x *= other)
}
}
impl<T, const N: usize, const M: usize> core::ops::Add<Matrix<T, M, N>> for Matrix<T, M, N>
where
T: Copy + Zero + core::ops::Add<Output = T> + PartialEq,
{
type Output = Self;
fn add(self, lhs: Self) -> Self::Output {
let mut out = Matrix::zero();
for i in 0..M {
for j in 0..N {
out[(i, j)] = self[(i, j)] + lhs[(i, j)];
}
}
out
}
}
#[cfg(test)]
mod tests {
use super::{super::*, *};
#[test]
fn construct_copy_type() {
let _m0 = Matrix::<i32, 4, 3>::default();
let _m1: Matrix<u8, 8, 8> = Matrix::default();
let _m2 = Matrix::new([[1, 2], [3, 4]]);
}
#[test]
fn matmul() {
let m1 = Matrix::new([[1_u8, 2, 3], [4, 5, 6]]);
let m2 = Matrix::new([[7_u8, 8, 9, 10], [11, 12, 13, 14], [15, 16, 17, 18]]);
let m3 = m1 * m2;
assert_eq!(m3, Matrix::new([[74, 80, 86, 92], [173, 188, 203, 218]]));
}
#[test]
fn iter() {
let m = Matrix::new([[1_u8, 2, 3], [4, 5, 6]]);
let mut iter = m.iter();
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&4));
assert_eq!(iter.next(), Some(&5));
assert_eq!(iter.next(), Some(&6));
assert_eq!(iter.next(), None);
}
}
mod approx {
use super::Matrix;
use ::approx::{AbsDiffEq, RelativeEq, UlpsEq};
impl<T, const M: usize, const N: usize> AbsDiffEq for Matrix<T, M, N>
where
T: AbsDiffEq,
{
type Epsilon = T::Epsilon;
fn default_epsilon() -> Self::Epsilon {
T::default_epsilon()
}
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
self.iter()
.zip(other.iter())
.all(|(r, l)| r.abs_diff_eq(l, T::default_epsilon()))
}
}
impl<T, const M: usize, const N: usize> RelativeEq for Matrix<T, M, N>
where
T: RelativeEq,
Self::Epsilon: Copy,
{
fn default_max_relative() -> Self::Epsilon {
T::default_max_relative()
}
fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool {
self.iter()
.zip(other.iter())
.all(|(r, l)| r.relative_eq(l, epsilon, max_relative))
}
}
impl<T, const M: usize, const N: usize> UlpsEq for Matrix<T, M, N>
where
T: UlpsEq,
Self::Epsilon: Copy,
{
fn default_max_ulps() -> u32 {
T::default_max_ulps()
}
fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
self.iter()
.zip(other.iter())
.all(|(r, l)| r.ulps_eq(l, epsilon, max_ulps))
}
}
}
pub(crate) const fn flip_ud<const M: usize, const N: usize>(
mut m: Matrix<super::Float, M, N>,
) -> Matrix<super::Float, M, N> {
let mut i = 0;
while i < M / 2 {
let tmp = m.data[i];
m.data[i] = m.data[M - 1 - i];
m.data[M - 1 - i] = tmp;
i += 1;
}
m
}
pub(crate) const fn flip_lr<const M: usize, const N: usize>(
mut m: Matrix<super::Float, M, N>,
) -> Matrix<super::Float, M, N> {
let mut i = 0;
while i < M {
let mut j = 0;
while j < N / 2 {
let tmp = m.data[i][j];
m.data[i][j] = m.data[i][N - 1 - j];
m.data[i][N - 1 - j] = tmp;
j += 1;
}
i += 1;
}
m
}
/// Flip all sign bits
pub(crate) const fn flip_sign<const M: usize, const N: usize>(
mut m: Matrix<super::Float, M, N>,
) -> Matrix<super::Float, M, N> {
let mut i = 0;
while i < M {
let mut j = 0;
while j < N {
m.data[i][j] = -m.data[i][j];
j += 1;
}
i += 1;
}
m
}
mod flipping {
use super::*;
#[test]
fn flip_lr_test() {
let m = Matrix::new([[1.0, 2.0, 3.0, 4.0]]);
let flipped = flip_lr(m);
assert_eq!(flipped, Matrix::new([[4.0, 3.0, 2.0, 1.0]]));
let m = Matrix::new([[1.0, 2.0, 3.0, 4.0, 5.0]]);
let flipped = flip_lr(m);
assert_eq!(flipped, Matrix::new([[5.0, 4.0, 3.0, 2.0, 1.0]]));
}
#[test]
fn flip_ud_test() {
let m = Matrix::new([[1.0], [2.0], [3.0], [4.0]]);
let flipped = flip_ud(m);
assert_eq!(flipped, Matrix::new([[4.0], [3.0], [2.0], [1.0]]));
let m = Matrix::new([[1.0], [2.0], [3.0], [4.0], [5.0]]);
let flipped = flip_ud(m);
assert_eq!(flipped, Matrix::new([[5.0], [4.0], [3.0], [2.0], [1.0]]));
}
}

View File

@ -0,0 +1,120 @@
use super::*;
#[repr(transparent)]
#[derive(Copy, Clone, Debug, PartialEq, Default)]
pub(crate) struct FastFloat(Float);
use core::intrinsics::{fadd_fast, fdiv_fast, fmul_fast, fsub_fast};
impl core::ops::Mul for FastFloat {
type Output = Self;
#[inline(always)]
fn mul(self, o: Self) -> Self::Output {
unsafe { Self(fmul_fast(self.0, o.0)) }
}
}
impl core::ops::Mul<Float> for FastFloat {
type Output = FastFloat;
#[inline(always)]
fn mul(self, o: Float) -> Self::Output {
unsafe { Self(fmul_fast(self.0, o)) }
}
}
impl core::ops::Mul<FastFloat> for Float {
type Output = FastFloat;
#[inline(always)]
fn mul(self, o: FastFloat) -> Self::Output {
unsafe { FastFloat(fmul_fast(self, o.0)) }
}
}
impl core::ops::Add<FastFloat> for FastFloat {
type Output = FastFloat;
#[inline(always)]
fn add(self, o: FastFloat) -> Self::Output {
unsafe { Self(fadd_fast(self.0, o.0)) }
}
}
impl core::ops::Add<Float> for FastFloat {
type Output = FastFloat;
#[inline(always)]
fn add(self, o: Float) -> Self::Output {
unsafe { Self(fadd_fast(self.0, o)) }
}
}
impl core::ops::Add<FastFloat> for Float {
type Output = FastFloat;
#[inline(always)]
fn add(self, o: FastFloat) -> Self::Output {
unsafe { FastFloat(fadd_fast(self, o.0)) }
}
}
impl core::ops::Sub for FastFloat {
type Output = Self;
#[inline(always)]
fn sub(self, o: FastFloat) -> Self::Output {
unsafe { Self(fsub_fast(self.0, o.0)) }
}
}
impl core::ops::Div for FastFloat {
type Output = Self;
#[inline(always)]
fn div(self, o: FastFloat) -> Self::Output {
unsafe { Self(fdiv_fast(self.0, o.0)) }
}
}
impl core::ops::MulAssign<FastFloat> for FastFloat {
#[inline(always)]
fn mul_assign(&mut self, o: FastFloat) {
unsafe {
self.0 = fmul_fast(self.0, o.0);
}
}
}
impl core::ops::Mul for &FastFloat {
type Output = FastFloat;
#[inline(always)]
fn mul(self, o: Self) -> Self::Output {
unsafe { FastFloat(fmul_fast(self.0, o.0)) }
}
}
impl From<Float> for FastFloat {
#[inline(always)]
fn from(f: Float) -> Self {
Self(f)
}
}
impl From<FastFloat> for Float {
#[inline(always)]
fn from(f: FastFloat) -> Self {
f.0
}
}
mod numt {
use super::{FastFloat, Float};
use num_traits::identities::{One, Zero};
impl One for FastFloat {
fn one() -> Self {
Self(Float::one())
}
}
impl Zero for FastFloat {
fn zero() -> Self {
Self(Float::zero())
}
fn is_zero(&self) -> bool {
self.0.is_zero()
}
}
}

View File

@ -1,4 +1,6 @@
use super::{diff_op_col, diff_op_row, SbpOperator1d, SbpOperator2d};
use super::{
BlockMatrix, DiagonalMatrix, Matrix, OperatorType, RowVector, SbpOperator1d, SbpOperator2d,
};
use crate::Float;
use ndarray::{ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2};
@ -7,63 +9,61 @@ pub struct SBP4;
impl SBP4 {
#[rustfmt::skip]
const HBLOCK: &'static [Float] = &[
const H: DiagonalMatrix<4> = DiagonalMatrix::new([
17.0 / 48.0, 59.0 / 48.0, 43.0 / 48.0, 49.0 / 48.0,
];
#[rustfmt::skip]
const DIAG: &'static [Float] = &[
1.0 / 12.0, -2.0 / 3.0, 0.0, 2.0 / 3.0, -1.0 / 12.0,
];
#[rustfmt::skip]
const BLOCK: &'static [&'static [Float]] = &[
&[-24.0/17.0, 59.0/34.0, -4.0/17.0, -3.0/34.0],
&[-1.0/2.0, 0.0, 1.0/2.0],
&[4.0/43.0, -59.0/86.0, 0.0, 59.0/86.0, -4.0/43.0],
&[3.0/98.0, 0.0, -59.0/98.0, 0.0, 32.0/49.0, -4.0/49.0]
];
]);
#[rustfmt::skip]
const D2DIAG: &'static [Float] = &[
-1.0 / 12.0, 4.0 / 3.0, -5.0 / 2.0, 4.0 / 3.0, -1.0 / 12.0
];
const DIFF_DIAG: RowVector<Float, 5> = RowVector::new([[
1.0 / 12.0, -2.0 / 3.0, 0.0, 2.0 / 3.0, -1.0 / 12.0
]]);
#[rustfmt::skip]
const D2BLOCK: &'static [&'static [Float]] = &[
&[2.0, -5.0, 4.0, -1.0],
&[1.0, -2.0, 1.0],
&[-4.0/43.0, 59.0/43.0, -110.0/43.0, 59.0/43.0, -4.0/43.0],
&[-1.0/49.0, 0.0, 59.0/49.0, -118.0/49.0, 64.0/49.0, -4.0/49.0]
];
const DIFF_BLOCK: Matrix<Float, 4, 6> = Matrix::new([
[-24.0/17.0, 59.0/34.0, -4.0/17.0, -3.0/34.0, 0.0, 0.0],
[-1.0/2.0, 0.0, 1.0/2.0, 0.0, 0.0, 0.0],
[4.0/43.0, -59.0/86.0, 0.0, 59.0/86.0, -4.0/43.0, 0.0],
[3.0/98.0, 0.0, -59.0/98.0, 0.0, 32.0/49.0, -4.0/49.0]
]);
const DIFF_BLOCKEND: super::Matrix<Float, 4, 6> =
super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK)));
const DIFF: BlockMatrix<Float, 4, 6, 5> =
BlockMatrix::new(Self::DIFF_BLOCK, Self::DIFF_DIAG, Self::DIFF_BLOCKEND);
#[rustfmt::skip]
const D2DIAG: RowVector<Float, 5> = RowVector::new([[
-1.0 / 12.0, 4.0 / 3.0, -5.0 / 2.0, 4.0 / 3.0, -1.0 / 12.0
]]);
#[rustfmt::skip]
const D2BLOCK: Matrix<Float, 4, 6> = Matrix::new([
[2.0, -5.0, 4.0, -1.0, 0.0, 0.0],
[1.0, -2.0, 1.0, 0.0, 0.0, 0.0],
[-4.0/43.0, 59.0/43.0, -110.0/43.0, 59.0/43.0, -4.0/43.0, 0.0],
[-1.0/49.0, 0.0, 59.0/49.0, -118.0/49.0, 64.0/49.0, -4.0/49.0]
]);
const D2: BlockMatrix<Float, 4, 6, 5> = BlockMatrix::new(
Self::D2BLOCK,
Self::D2DIAG,
super::flip_ud(super::flip_lr(Self::D2BLOCK)),
);
}
impl SbpOperator1d for SBP4 {
fn diff(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
Self::BLOCK,
Self::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::Normal,
prev,
fut,
)
super::diff_op_1d(&Self::DIFF, super::OperatorType::Normal, prev, fut)
}
fn h(&self) -> &'static [Float] {
Self::HBLOCK
&Self::H.start
}
#[cfg(feature = "sparse")]
fn diff_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::sparse_from_block(
Self::BLOCK,
Self::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::Normal,
n,
)
super::sparse_from_block(&Self::DIFF, super::OperatorType::Normal, n)
}
#[cfg(feature = "sparse")]
fn h_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::h_matrix(Self::HBLOCK, n, self.is_h2())
super::h_matrix(&Self::H, n, self.is_h2())
}
fn d2(&self) -> Option<&dyn super::SbpOperator1d2> {
@ -72,47 +72,18 @@ impl SbpOperator1d for SBP4 {
}
impl SbpOperator2d for SBP4 {
fn diffxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
fn diffxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
assert_eq!(prev.shape(), fut.shape());
assert!(prev.shape()[1] >= 2 * SBP4::BLOCK.len());
let symmetry = super::Symmetry::AntiSymmetric;
let optype = super::OperatorType::Normal;
match (prev.strides(), fut.strides()) {
([_, 1], [_, 1]) => {
diff_op_row(SBP4::BLOCK, SBP4::DIAG, symmetry, optype)(prev, fut);
}
([1, _], [1, _]) => {
diff_op_col(SBP4::BLOCK, SBP4::DIAG, symmetry, optype)(prev, fut);
}
([_, _], [_, _]) => {
// Fallback, work row by row
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
SBP4.diff(r0, r1);
}
}
_ => unreachable!("Should only be two elements in the strides vectors"),
}
super::diff_op_2d(&Self::DIFF, OperatorType::Normal, prev, fut)
}
fn op_xi(&self) -> &dyn SbpOperator1d {
&Self
}
fn op_eta(&self) -> &dyn SbpOperator1d {
&Self
}
}
impl super::SbpOperator1d2 for SBP4 {
fn diff2(&self, prev: ArrayView1<Float>, mut fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
Self::D2BLOCK,
Self::D2DIAG,
super::Symmetry::Symmetric,
super::OperatorType::Normal,
prev,
fut.view_mut(),
);
super::diff_op_1d(&Self::D2, OperatorType::Normal, prev, fut.view_mut());
let hi = (prev.len() - 1) as Float;
fut.map_inplace(|x| *x *= hi)
}
@ -121,13 +92,7 @@ impl super::SbpOperator1d2 for SBP4 {
}
#[cfg(feature = "sparse")]
fn diff2_matrix(&self, n: usize) -> sprs::CsMat<Float> {
let mut m = super::sparse_from_block(
Self::D2BLOCK,
Self::D2DIAG,
super::Symmetry::Symmetric,
super::OperatorType::Normal,
n,
);
let mut m = super::sparse_from_block(&Self::D2, OperatorType::Normal, n);
let hi = (n - 1) as Float;
m.map_inplace(|v| v * hi);
m

View File

@ -1,4 +1,6 @@
use super::{diff_op_col, diff_op_row, SbpOperator1d, SbpOperator2d};
use super::{
BlockMatrix, DiagonalMatrix, Matrix, OperatorType, RowVector, SbpOperator1d, SbpOperator2d,
};
use crate::Float;
use ndarray::{ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2};
@ -7,88 +9,58 @@ pub struct SBP8;
impl SBP8 {
#[rustfmt::skip]
const HBLOCK: &'static [Float] = &[
const H: DiagonalMatrix<8> = DiagonalMatrix::new([
2.94890676177879e-01, 1.52572062389771e+00, 2.57452876984127e-01, 1.79811370149912e+00, 4.12708057760141e-01, 1.27848462301587e+00, 9.23295579805997e-01, 1.00933386085916e+00
];
]);
#[rustfmt::skip]
const DIAG: &'static [Float] = &[
const DIFF_DIAG: RowVector<Float, 9> = Matrix::new([[
3.57142857142857e-03, -3.80952380952381e-02, 2.00000000000000e-01, -8.00000000000000e-01, -0.00000000000000e+00, 8.00000000000000e-01, -2.00000000000000e-01, 3.80952380952381e-02, -3.57142857142857e-03
];
]]);
#[rustfmt::skip]
const BLOCK: &'static [&'static [Float]] = &[
&[-1.69554360443190e+00, 2.24741246341404e+00, -3.38931922601500e-02, -7.81028168126749e-01, 2.54881486107905e-02, 3.43865227388873e-01, -8.62858162633335e-02, -2.00150583315761e-02],
&[-4.34378988266985e-01, 0.00000000000000e+00, 9.18511925072956e-02, 4.94008626807984e-01, -2.46151762937235e-02, -1.86759403432935e-01, 5.27267838475813e-02, 7.16696483080115e-03],
&[3.88218088704253e-02, -5.44329744454984e-01, 0.00000000000000e+00, 3.89516189693211e-01, 1.36433486528546e-01, 1.03290582800845e-01, -1.79720579323281e-01, 5.59882558852296e-02],
&[1.28088632226564e-01, -4.19172130036008e-01, -5.57707021445779e-02, 0.00000000000000e+00, 1.24714160903055e-01, 2.81285212519100e-01, -3.94470423942641e-02, -1.96981310738430e-02],
&[-1.82119472519009e-02, 9.09986646154550e-02, -8.51090570277506e-02, -5.43362886365301e-01, 0.00000000000000e+00, 6.37392455438558e-01, -1.02950081118829e-01, 2.98964956216039e-02, -8.65364391190110e-03],
&[-7.93147196245203e-02, 2.22875323171502e-01, -2.07999824391436e-02, -3.95611167748401e-01, -2.05756876210586e-01, 0.00000000000000e+00, 5.45876519966127e-01, -9.42727926638298e-02, 2.97971812952850e-02, -2.79348574643297e-03],
&[2.75587615266177e-02, -8.71295642560637e-02, 5.01135077563584e-02, 7.68229253600969e-02, 4.60181213406519e-02, -7.55873581663580e-01, 0.00000000000000e+00, 8.21713248844682e-01, -2.16615355227872e-01, 4.12600676624518e-02, -3.86813134335486e-03],
&[5.84767272160451e-03, -1.08336661209337e-02, -1.42810403117803e-02, 3.50919361287023e-02, -1.22244235731112e-02, 1.19411743193552e-01, -7.51668243727123e-01, 0.00000000000000e+00, 7.92601963555477e-01, -1.98150490888869e-01, 3.77429506454989e-02, -3.53840162301552e-03],
];
const DIFF_BLOCK: Matrix<Float, 8, 12> = Matrix::new([
[-1.69554360443190e+00, 2.24741246341404e+00, -3.38931922601500e-02, -7.81028168126749e-01, 2.54881486107905e-02, 3.43865227388873e-01, -8.62858162633335e-02, -2.00150583315761e-02, 0.0, 0.0, 0.0, 0.0],
[-4.34378988266985e-01, 0.00000000000000e+00, 9.18511925072956e-02, 4.94008626807984e-01, -2.46151762937235e-02, -1.86759403432935e-01, 5.27267838475813e-02, 7.16696483080115e-03, 0.0, 0.0, 0.0, 0.0],
[3.88218088704253e-02, -5.44329744454984e-01, 0.00000000000000e+00, 3.89516189693211e-01, 1.36433486528546e-01, 1.03290582800845e-01, -1.79720579323281e-01, 5.59882558852296e-02, 0.0, 0.0, 0.0, 0.0],
[1.28088632226564e-01, -4.19172130036008e-01, -5.57707021445779e-02, 0.00000000000000e+00, 1.24714160903055e-01, 2.81285212519100e-01, -3.94470423942641e-02, -1.96981310738430e-02, 0.0, 0.0, 0.0, 0.0],
[-1.82119472519009e-02, 9.09986646154550e-02, -8.51090570277506e-02, -5.43362886365301e-01, 0.00000000000000e+00, 6.37392455438558e-01, -1.02950081118829e-01, 2.98964956216039e-02, -8.65364391190110e-03, 0.0, 0.0, 0.0],
[-7.93147196245203e-02, 2.22875323171502e-01, -2.07999824391436e-02, -3.95611167748401e-01, -2.05756876210586e-01, 0.00000000000000e+00, 5.45876519966127e-01, -9.42727926638298e-02, 2.97971812952850e-02, -2.79348574643297e-03, 0.0, 0.0],
[2.75587615266177e-02, -8.71295642560637e-02, 5.01135077563584e-02, 7.68229253600969e-02, 4.60181213406519e-02, -7.55873581663580e-01, 0.00000000000000e+00, 8.21713248844682e-01, -2.16615355227872e-01, 4.12600676624518e-02, -3.86813134335486e-03, 0.0],
[5.84767272160451e-03, -1.08336661209337e-02, -1.42810403117803e-02, 3.50919361287023e-02, -1.22244235731112e-02, 1.19411743193552e-01, -7.51668243727123e-01, 0.00000000000000e+00, 7.92601963555477e-01, -1.98150490888869e-01, 3.77429506454989e-02, -3.53840162301552e-03],
]);
const DIFF: BlockMatrix<Float, 8, 12, 9> = BlockMatrix::new(
Self::DIFF_BLOCK,
Self::DIFF_DIAG,
super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK))),
);
}
impl SbpOperator1d for SBP8 {
fn diff(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
Self::BLOCK,
Self::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::Normal,
prev,
fut,
)
super::diff_op_1d(&Self::DIFF, super::OperatorType::Normal, prev, fut)
}
fn h(&self) -> &'static [Float] {
Self::HBLOCK
&Self::H.start
}
#[cfg(feature = "sparse")]
fn diff_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::sparse_from_block(
Self::BLOCK,
Self::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::Normal,
n,
)
super::sparse_from_block(&Self::DIFF, super::OperatorType::Normal, n)
}
#[cfg(feature = "sparse")]
fn h_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::h_matrix(Self::HBLOCK, n, self.is_h2())
super::h_matrix(&Self::H, n, self.is_h2())
}
}
impl SbpOperator2d for SBP8 {
fn diffxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
fn diffxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
assert_eq!(prev.shape(), fut.shape());
assert!(prev.shape()[1] >= 2 * SBP8::BLOCK.len());
let symmetry = super::Symmetry::AntiSymmetric;
let optype = super::OperatorType::Normal;
match (prev.strides(), fut.strides()) {
([_, 1], [_, 1]) => {
diff_op_row(SBP8::BLOCK, SBP8::DIAG, symmetry, optype)(prev, fut);
}
([1, _], [1, _]) => {
diff_op_col(SBP8::BLOCK, SBP8::DIAG, symmetry, optype)(prev, fut);
}
([_, _], [_, _]) => {
// Fallback, work row by row
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
SBP8.diff(r0, r1);
}
}
_ => unreachable!("Should only be two elements in the strides vectors"),
}
super::diff_op_2d(&Self::DIFF, OperatorType::Normal, prev, fut)
}
fn op_xi(&self) -> &dyn SbpOperator1d {
&Self
}
fn op_eta(&self) -> &dyn SbpOperator1d {
&Self
}
}
#[test]

View File

@ -1,227 +1,66 @@
use super::{
diff_op_col, diff_op_row, SbpOperator1d, SbpOperator2d, UpwindOperator1d, UpwindOperator2d,
BlockMatrix, DiagonalMatrix, Matrix, OperatorType, RowVector, SbpOperator1d, SbpOperator2d,
UpwindOperator1d, UpwindOperator2d,
};
use crate::Float;
use ndarray::{ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2, Axis};
use ndarray::{ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2};
#[derive(Debug, Copy, Clone)]
pub struct Upwind4;
/// Simdtype used in diff_simd_col
#[cfg(feature = "f32")]
type SimdT = packed_simd::f32x8;
#[cfg(not(feature = "f32"))]
type SimdT = packed_simd::f64x8;
macro_rules! diff_simd_col_7_47 {
($name: ident, $BLOCK: expr, $DIAG: expr, $symmetric: expr) => {
#[inline(never)]
fn $name(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
use std::slice;
assert_eq!(prev.shape(), fut.shape());
assert_eq!(prev.stride_of(Axis(0)), 1);
assert_eq!(fut.stride_of(Axis(0)), 1);
let ny = prev.len_of(Axis(0));
let nx = prev.len_of(Axis(1));
assert!(nx >= 2 * $BLOCK.len());
assert!(ny >= SimdT::lanes());
assert!(ny % SimdT::lanes() == 0);
let dx = 1.0 / (nx - 1) as Float;
let idx = 1.0 / dx;
for j in (0..ny).step_by(SimdT::lanes()) {
let a = unsafe {
[
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, 0)) as *const Float,
SimdT::lanes(),
)),
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, 1)) as *const Float,
SimdT::lanes(),
)),
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, 2)) as *const Float,
SimdT::lanes(),
)),
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, 3)) as *const Float,
SimdT::lanes(),
)),
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, 4)) as *const Float,
SimdT::lanes(),
)),
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, 5)) as *const Float,
SimdT::lanes(),
)),
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, 6)) as *const Float,
SimdT::lanes(),
)),
]
};
for (i, bl) in $BLOCK.iter().enumerate() {
let b = idx
* (a[0] * bl[0]
+ a[1] * bl[1]
+ a[2] * bl[2]
+ a[3] * bl[3]
+ a[4] * bl[4]
+ a[5] * bl[5]
+ a[6] * bl[6]);
unsafe {
b.write_to_slice_unaligned(slice::from_raw_parts_mut(
fut.uget_mut((j, i)) as *mut Float,
SimdT::lanes(),
));
}
}
let mut a = a;
for i in $BLOCK.len()..nx - $BLOCK.len() {
// Push a onto circular buffer
a = [a[1], a[2], a[3], a[4], a[5], a[6], unsafe {
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, i + 3)) as *const Float,
SimdT::lanes(),
))
}];
let b = idx
* (a[0] * $DIAG[0]
+ a[1] * $DIAG[1]
+ a[2] * $DIAG[2]
+ a[3] * $DIAG[3]
+ a[4] * $DIAG[4]
+ a[5] * $DIAG[5]
+ a[6] * $DIAG[6]);
unsafe {
b.write_to_slice_unaligned(slice::from_raw_parts_mut(
fut.uget_mut((j, i)) as *mut Float,
SimdT::lanes(),
));
}
}
let a = unsafe {
[
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, nx - 1)) as *const Float,
SimdT::lanes(),
)),
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, nx - 2)) as *const Float,
SimdT::lanes(),
)),
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, nx - 3)) as *const Float,
SimdT::lanes(),
)),
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, nx - 4)) as *const Float,
SimdT::lanes(),
)),
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, nx - 5)) as *const Float,
SimdT::lanes(),
)),
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, nx - 6)) as *const Float,
SimdT::lanes(),
)),
SimdT::from_slice_unaligned(slice::from_raw_parts(
prev.uget((j, nx - 7)) as *const Float,
SimdT::lanes(),
)),
]
};
for (i, bl) in $BLOCK.iter().enumerate() {
let idx = if $symmetric { idx } else { -idx };
let b = idx
* (a[0] * bl[0]
+ a[1] * bl[1]
+ a[2] * bl[2]
+ a[3] * bl[3]
+ a[4] * bl[4]
+ a[5] * bl[5]
+ a[6] * bl[6]);
unsafe {
b.write_to_slice_unaligned(slice::from_raw_parts_mut(
fut.uget_mut((j, nx - 1 - i)) as *mut Float,
SimdT::lanes(),
));
}
}
}
}
};
}
diff_simd_col_7_47!(diff_simd_col, Upwind4::BLOCK, Upwind4::DIAG, false);
diff_simd_col_7_47!(diss_simd_col, Upwind4::DISS_BLOCK, Upwind4::DISS_DIAG, true);
impl Upwind4 {
#[rustfmt::skip]
pub const HBLOCK: &'static [Float] = &[
const H: DiagonalMatrix<4> = DiagonalMatrix::new([
49.0 / 144.0, 61.0 / 48.0, 41.0 / 48.0, 149.0 / 144.0
];
]);
#[rustfmt::skip]
const DIAG: &'static [Float] = &[
const DIFF_BLOCK: Matrix<Float, 4, 7> = Matrix::new([
[ -72.0 / 49.0, 187.0 / 98.0, -20.0 / 49.0, -3.0 / 98.0, 0.0, 0.0, 0.0],
[-187.0 / 366.0, 0.0, 69.0 / 122.0, -16.0 / 183.0, 2.0 / 61.0, 0.0, 0.0],
[ 20.0 / 123.0, -69.0 / 82.0, 0.0, 227.0 / 246.0, -12.0 / 41.0, 2.0 / 41.0, 0.0],
[ 3.0 / 298.0, 16.0 / 149.0, -227.0 / 298.0, 0.0, 126.0 / 149.0, -36.0 / 149.0, 6.0 / 149.0],
]);
#[rustfmt::skip]
const DIFF_DIAG: RowVector<Float, 7> = RowVector::new([[
-1.0 / 24.0, 1.0 / 4.0, -7.0 / 8.0, 0.0, 7.0 / 8.0, -1.0 / 4.0, 1.0 / 24.0
];
#[rustfmt::skip]
const BLOCK: &'static [&'static [Float]] = &[
&[ -72.0 / 49.0, 187.0 / 98.0, -20.0 / 49.0, -3.0 / 98.0, 0.0, 0.0, 0.0],
&[-187.0 / 366.0, 0.0, 69.0 / 122.0, -16.0 / 183.0, 2.0 / 61.0, 0.0, 0.0],
&[ 20.0 / 123.0, -69.0 / 82.0, 0.0, 227.0 / 246.0, -12.0 / 41.0, 2.0 / 41.0, 0.0],
&[ 3.0 / 298.0, 16.0 / 149.0, -227.0 / 298.0, 0.0, 126.0 / 149.0, -36.0 / 149.0, 6.0 / 149.0],
];
]]);
const DIFF_BLOCKEND: Matrix<Float, 4, 7> =
super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK)));
const DIFF: BlockMatrix<Float, 4, 7, 7> =
BlockMatrix::new(Self::DIFF_BLOCK, Self::DIFF_DIAG, Self::DIFF_BLOCKEND);
#[rustfmt::skip]
const DISS_BLOCK: &'static [&'static [Float]] = &[
&[-3.0 / 49.0, 9.0 / 49.0, -9.0 / 49.0, 3.0 / 49.0, 0.0, 0.0, 0.0],
&[ 3.0 / 61.0, -11.0 / 61.0, 15.0 / 61.0, -9.0 / 61.0, 2.0 / 61.0, 0.0, 0.0],
&[-3.0 / 41.0, 15.0 / 41.0, -29.0 / 41.0, 27.0 / 41.0, -12.0 / 41.0, 2.0 / 41.0, 0.0],
&[3.0 / 149.0, -27.0 / 149.0, 81.0 / 149.0, -117.0 / 149.0, 90.0 / 149.0, -36.0 / 149.0, 6.0 / 149.0],
];
const DISS_BLOCK: Matrix<Float, 4, 7> = Matrix::new([
[-3.0 / 49.0, 9.0 / 49.0, -9.0 / 49.0, 3.0 / 49.0, 0.0, 0.0, 0.0],
[ 3.0 / 61.0, -11.0 / 61.0, 15.0 / 61.0, -9.0 / 61.0, 2.0 / 61.0, 0.0, 0.0],
[-3.0 / 41.0, 15.0 / 41.0, -29.0 / 41.0, 27.0 / 41.0, -12.0 / 41.0, 2.0 / 41.0, 0.0],
[3.0 / 149.0, -27.0 / 149.0, 81.0 / 149.0, -117.0 / 149.0, 90.0 / 149.0, -36.0 / 149.0, 6.0 / 149.0],
]);
#[rustfmt::skip]
const DISS_DIAG: &'static [Float; 7] = &[
const DISS_DIAG: RowVector<Float, 7> = Matrix::new([[
1.0 / 24.0, -1.0 / 4.0, 5.0 / 8.0, -5.0 / 6.0, 5.0 / 8.0, -1.0 / 4.0, 1.0 / 24.0
];
]]);
const DISS_BLOCKEND: Matrix<Float, 4, 7> = super::flip_ud(super::flip_lr(Self::DISS_BLOCK));
const DISS: BlockMatrix<Float, 4, 7, 7> =
BlockMatrix::new(Self::DISS_BLOCK, Self::DISS_DIAG, Self::DISS_BLOCKEND);
}
impl SbpOperator1d for Upwind4 {
fn diff(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
Self::BLOCK,
Self::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::Normal,
prev,
fut,
)
super::diff_op_1d(&Self::DIFF, super::OperatorType::Normal, prev, fut)
}
fn h(&self) -> &'static [Float] {
Self::HBLOCK
&Self::H.start
}
#[cfg(feature = "sparse")]
fn diff_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::sparse_from_block(
Self::BLOCK,
Self::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::Normal,
n,
)
super::sparse_from_block(&Self::DIFF, super::OperatorType::Normal, n)
}
#[cfg(feature = "sparse")]
fn h_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::h_matrix(Self::HBLOCK, n, self.is_h2())
super::h_matrix(&Self::H, n, self.is_h2())
}
fn upwind(&self) -> Option<&dyn UpwindOperator1d> {
@ -230,41 +69,13 @@ impl SbpOperator1d for Upwind4 {
}
impl SbpOperator2d for Upwind4 {
fn diffxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
fn diffxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
assert_eq!(prev.shape(), fut.shape());
assert!(prev.shape()[1] >= 2 * Upwind4::BLOCK.len());
match (prev.strides(), fut.strides()) {
([_, 1], [_, 1]) => diff_op_row(
Upwind4::BLOCK,
Upwind4::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::Normal,
)(prev, fut),
([1, _], [1, _]) if prev.len_of(Axis(0)) % SimdT::lanes() == 0 => {
diff_simd_col(prev, fut)
}
([1, _], [1, _]) => diff_op_col(
Upwind4::BLOCK,
Upwind4::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::Normal,
)(prev, fut),
([_, _], [_, _]) => {
// Fallback, work row by row
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
Upwind4.diff(r0, r1);
}
}
_ => unreachable!("Should only be two elements in the strides vectors"),
}
super::diff_op_2d(&Self::DIFF, OperatorType::Normal, prev, fut);
}
fn op_xi(&self) -> &dyn SbpOperator1d {
&Self
}
fn op_eta(&self) -> &dyn SbpOperator1d {
&Self
}
fn upwind(&self) -> Option<Box<dyn UpwindOperator2d>> {
Some(Box::new(Self))
}
@ -360,14 +171,7 @@ fn upwind4_test() {
impl UpwindOperator1d for Upwind4 {
fn diss(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
Self::DISS_BLOCK,
Self::DISS_DIAG,
super::Symmetry::Symmetric,
super::OperatorType::Normal,
prev,
fut,
)
super::diff_op_1d(&Self::DISS, super::OperatorType::Normal, prev, fut)
}
fn as_sbp(&self) -> &dyn SbpOperator1d {
@ -376,53 +180,20 @@ impl UpwindOperator1d for Upwind4 {
#[cfg(feature = "sparse")]
fn diss_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::sparse_from_block(
Self::DISS_BLOCK,
Self::DISS_DIAG,
super::Symmetry::Symmetric,
super::OperatorType::Normal,
n,
)
super::sparse_from_block(&Self::DISS, super::OperatorType::Normal, n)
}
}
impl UpwindOperator2d for Upwind4 {
fn dissxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
fn dissxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
assert_eq!(prev.shape(), fut.shape());
assert!(prev.shape()[1] >= 2 * Upwind4::BLOCK.len());
match (prev.strides(), fut.strides()) {
([_, 1], [_, 1]) => diff_op_row(
Upwind4::DISS_BLOCK,
Upwind4::DISS_DIAG,
super::Symmetry::Symmetric,
super::OperatorType::Normal,
)(prev, fut),
([1, _], [1, _]) if prev.len_of(Axis(0)) % SimdT::lanes() == 0 => {
diss_simd_col(prev, fut);
}
([1, _], [1, _]) => diff_op_col(
Upwind4::DISS_BLOCK,
Upwind4::DISS_DIAG,
super::Symmetry::Symmetric,
super::OperatorType::Normal,
)(prev, fut),
([_, _], [_, _]) => {
// Fallback, work row by row
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
Upwind4.diss(r0, r1);
}
}
_ => unreachable!("Should only be two elements in the strides vectors"),
}
super::diff_op_2d(&Self::DISS, OperatorType::Normal, prev, fut)
}
fn op_xi(&self) -> &dyn UpwindOperator1d {
&Self
}
fn op_eta(&self) -> &dyn UpwindOperator1d {
&Self
}
}
#[test]

View File

@ -1,5 +1,6 @@
use super::{
diff_op_col, diff_op_row, SbpOperator1d, SbpOperator2d, UpwindOperator1d, UpwindOperator2d,
BlockMatrix, DiagonalMatrix, Matrix, OperatorType, RowVector, SbpOperator1d, SbpOperator2d,
UpwindOperator1d, UpwindOperator2d,
};
use crate::Float;
use ndarray::{ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2};
@ -9,49 +10,52 @@ pub struct Upwind4h2;
impl Upwind4h2 {
#[rustfmt::skip]
const HBLOCK: &'static [Float] = &[
const H: DiagonalMatrix<4> = DiagonalMatrix::new([
0.91e2/0.720e3, 0.325e3/0.384e3, 0.595e3/0.576e3, 0.1909e4/0.1920e4,
];
]);
#[rustfmt::skip]
const DIAG: &'static [Float] = &[
const DIFF_BLOCK: Matrix<Float, 4, 7> = Matrix::new([
[-3.95604395604396e+00, 5.41758241758242e+00, -1.94505494505495e+00, 4.83516483516484e-01, 0.0, 0.0, 0.0],
[-8.09025641025641e-01, 0.00000000000000e+00, 1.03948717948718e+00, -2.47384615384615e-01, 1.69230769230769e-02, 0.0, 0.0],
[2.37983193277311e-01, -8.51680672268908e-01, 0.00000000000000e+00, 7.35966386554622e-01, -1.36134453781513e-01, 1.38655462184874e-02, 0.0],
[-6.14632442814737e-02, 2.10581456259822e-01, -7.64623712240265e-01, 0.00000000000000e+00, 7.42535358826611e-01, -1.41435306443164e-01, 1.44054478784704e-02],
]);
#[rustfmt::skip]
const DIFF_DIAG: RowVector<Float, 7> = RowVector::new([[
-1.43229166666667e-02, 1.40625000000000e-01, -7.38281250000000e-01, 0.00000000000000e+00, 7.38281250000000e-01, -1.40625000000000e-01, 1.43229166666667e-02
];
#[rustfmt::skip]
const BLOCK: &'static [&'static [Float]] = &[
&[-3.95604395604396e+00, 5.41758241758242e+00, -1.94505494505495e+00, 4.83516483516484e-01],
&[-8.09025641025641e-01, 0.00000000000000e+00, 1.03948717948718e+00, -2.47384615384615e-01, 1.69230769230769e-02],
&[2.37983193277311e-01, -8.51680672268908e-01, 0.00000000000000e+00, 7.35966386554622e-01, -1.36134453781513e-01, 1.38655462184874e-02],
&[-6.14632442814737e-02, 2.10581456259822e-01, -7.64623712240265e-01, 0.00000000000000e+00, 7.42535358826611e-01, -1.41435306443164e-01, 1.44054478784704e-02],
];
]]);
const DIFF: BlockMatrix<Float, 4, 7, 7> = BlockMatrix::new(
Self::DIFF_BLOCK,
Self::DIFF_DIAG,
super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK))),
);
#[rustfmt::skip]
const DISS_BLOCK: &'static [&'static [Float]] = &[
&[-2.76989342315976e-01, 5.19355016842454e-01, -3.46236677894969e-01, 1.03871003368491e-01],
&[7.75570158484731e-02, -1.62342481638964e-01, 1.47715500579822e-01, -7.98531117124082e-02, 1.69230769230769e-02],
&[-4.23630758836198e-02, 1.21027405937249e-01, -1.91609307039399e-01, 1.82272708078206e-01, -8.31932773109244e-02, 1.38655462184874e-02],
&[1.32037874021759e-02, -6.79734450144910e-02, 1.89370108794365e-01, -2.78654929966754e-01, 2.16081718177056e-01, -8.64326872708224e-02, 1.44054478784704e-02],
];
const DISS_BLOCK: Matrix<Float, 4, 7> = Matrix::new([
[-2.76989342315976e-01, 5.19355016842454e-01, -3.46236677894969e-01, 1.03871003368491e-01, 0.0, 0.0, 0.0],
[7.75570158484731e-02, -1.62342481638964e-01, 1.47715500579822e-01, -7.98531117124082e-02, 1.69230769230769e-02, 0.0, 0.0],
[-4.23630758836198e-02, 1.21027405937249e-01, -1.91609307039399e-01, 1.82272708078206e-01, -8.31932773109244e-02, 1.38655462184874e-02, 0.0],
[1.32037874021759e-02, -6.79734450144910e-02, 1.89370108794365e-01, -2.78654929966754e-01, 2.16081718177056e-01, -8.64326872708224e-02, 1.44054478784704e-02],
]);
#[rustfmt::skip]
const DISS_DIAG: &'static [Float; 7] = &[
const DISS_DIAG: RowVector<Float, 7> = RowVector::new([[
1.43229166666667e-02, -8.59375000000000e-02, 2.14843750000000e-01, -2.86458333333333e-01, 2.14843750000000e-01, -8.59375000000000e-02, 1.43229166666667e-02,
];
]]);
const DISS: BlockMatrix<Float, 4, 7, 7> = BlockMatrix::new(
Self::DISS_BLOCK,
Self::DISS_DIAG,
super::flip_ud(super::flip_lr(Self::DISS_BLOCK)),
);
}
impl SbpOperator1d for Upwind4h2 {
fn diff(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
Self::BLOCK,
Self::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::H2,
prev,
fut,
)
super::diff_op_1d(&Self::DIFF, OperatorType::H2, prev, fut)
}
fn h(&self) -> &'static [Float] {
Self::HBLOCK
&Self::H.start
}
fn is_h2(&self) -> bool {
true
@ -59,17 +63,11 @@ impl SbpOperator1d for Upwind4h2 {
#[cfg(feature = "sparse")]
fn diff_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::sparse_from_block(
Self::BLOCK,
Self::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::H2,
n,
)
super::sparse_from_block(&Self::DIFF, OperatorType::H2, n)
}
#[cfg(feature = "sparse")]
fn h_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::h_matrix(Self::HBLOCK, n, self.is_h2())
super::h_matrix(&Self::H, n, self.is_h2())
}
fn upwind(&self) -> Option<&dyn UpwindOperator1d> {
@ -78,80 +76,28 @@ impl SbpOperator1d for Upwind4h2 {
}
impl SbpOperator2d for Upwind4h2 {
fn diffxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
fn diffxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
assert_eq!(prev.shape(), fut.shape());
assert!(prev.shape()[1] >= 2 * Upwind4h2::BLOCK.len());
let symmetry = super::Symmetry::AntiSymmetric;
let optype = super::OperatorType::H2;
match (prev.strides(), fut.strides()) {
([_, 1], [_, 1]) => {
diff_op_row(Upwind4h2::BLOCK, Upwind4h2::DIAG, symmetry, optype)(prev, fut);
}
([1, _], [1, _]) => {
diff_op_col(Upwind4h2::BLOCK, Upwind4h2::DIAG, symmetry, optype)(prev, fut);
}
([_, _], [_, _]) => {
// Fallback, work row by row
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
Upwind4h2.diff(r0, r1);
}
}
_ => unreachable!("Should only be two elements in the strides vectors"),
}
super::diff_op_2d(&Self::DIFF, OperatorType::H2, prev, fut)
}
fn op_xi(&self) -> &dyn SbpOperator1d {
&Self
}
fn op_eta(&self) -> &dyn SbpOperator1d {
&Self
}
fn upwind(&self) -> Option<Box<dyn UpwindOperator2d>> {
Some(Box::new(Self))
}
}
impl UpwindOperator2d for Upwind4h2 {
fn dissxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
fn dissxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
assert_eq!(prev.shape(), fut.shape());
assert!(prev.shape()[1] >= 2 * Upwind4h2::BLOCK.len());
let symmetry = super::Symmetry::Symmetric;
let optype = super::OperatorType::H2;
match (prev.strides(), fut.strides()) {
([_, 1], [_, 1]) => {
diff_op_row(
Upwind4h2::DISS_BLOCK,
Upwind4h2::DISS_DIAG,
symmetry,
optype,
)(prev, fut);
}
([1, _], [1, _]) => {
diff_op_col(
Upwind4h2::DISS_BLOCK,
Upwind4h2::DISS_DIAG,
symmetry,
optype,
)(prev, fut);
}
([_, _], [_, _]) => {
// Fallback, work row by row
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
Upwind4h2.diss(r0, r1);
}
}
_ => unreachable!("Should only be two elements in the strides vectors"),
}
super::diff_op_2d(&Self::DISS, OperatorType::H2, prev, fut)
}
fn op_xi(&self) -> &dyn UpwindOperator1d {
&Self
}
fn op_eta(&self) -> &dyn UpwindOperator1d {
&Self
}
}
#[test]
@ -181,14 +127,7 @@ fn upwind4h2_test() {
impl UpwindOperator1d for Upwind4h2 {
fn diss(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
Self::DISS_BLOCK,
Self::DISS_DIAG,
super::Symmetry::Symmetric,
super::OperatorType::H2,
prev,
fut,
)
super::diff_op_1d(&Self::DISS, super::OperatorType::H2, prev, fut)
}
fn as_sbp(&self) -> &dyn SbpOperator1d {
@ -197,12 +136,6 @@ impl UpwindOperator1d for Upwind4h2 {
#[cfg(feature = "sparse")]
fn diss_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::sparse_from_block(
Self::DISS_BLOCK,
Self::DISS_DIAG,
super::Symmetry::Symmetric,
super::OperatorType::H2,
n,
)
super::sparse_from_block(&Self::DISS, super::OperatorType::H2, n)
}
}

View File

@ -1,5 +1,6 @@
use super::{
diff_op_col, diff_op_row, SbpOperator1d, SbpOperator2d, UpwindOperator1d, UpwindOperator2d,
BlockMatrix, DiagonalMatrix, Matrix, OperatorType, RowVector, SbpOperator1d, SbpOperator2d,
UpwindOperator1d, UpwindOperator2d,
};
use crate::Float;
use ndarray::{ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2};
@ -9,72 +10,69 @@ pub struct Upwind9;
impl Upwind9 {
#[rustfmt::skip]
const HBLOCK: &'static [Float] = &[
const H: DiagonalMatrix<8> = DiagonalMatrix::new([
1070017.0/3628800.0, 5537111.0/3628800.0, 103613.0/403200.0, 261115.0/145152.0, 298951.0/725760.0, 515677.0/403200.0, 3349879.0/3628800.0, 3662753.0/3628800.0
];
]);
#[rustfmt::skip]
const DIAG: &'static [Float] = &[
const DIFF_DIAG: RowVector<Float, 11> = RowVector::new([[
-1.0/1260.0, 5.0/504.0, -5.0/84.0, 5.0/21.0, -5.0/6.0, 0.0, 5.0/6.0, -5.0/21.0, 5.0/84.0, -5.0/504.0, 1.0/1260.0,
];
]]);
#[rustfmt::skip]
const BLOCK: &'static [&'static [Float]] = &[
&[-1.69567399396458e+00, 2.29023358159400e+00, -2.16473500425698e-01, -5.05879766354449e-01, -1.01161106778154e-01, 2.59147072064383e-01, 1.93922119400659e-02, -4.95844980755642e-02],
&[-4.42575354959737e-01, 0.00000000000000e+00, 1.91582959381899e-01, 2.82222626681305e-01, 1.12083989713257e-01, -1.51334868892111e-01, -2.23600502721044e-02, 3.03806983474913e-02],
&[2.48392603571843e-01, -1.13758367065272e+00, 0.00000000000000e+00, 1.95334726810969e+00, -1.58879011773212e+00, 3.93797129320378e-01, 2.52140821030291e-01, -1.21304033647356e-01],
&[8.29213105268236e-02, -2.39388470313226e-01, -2.79038666398460e-01, 0.00000000000000e+00, 3.43018053395471e-01, 1.10370852514749e-01, 1.72029988649808e-03, -2.00445645303789e-02, 4.41184918522490e-04],
&[7.24159504343116e-02, -4.15199475743626e-01, 9.91181694804303e-01, -1.49802407438608e+00, 0.00000000000000e+00, 1.30188867830442e+00, -6.03535071819214e-01, 1.73429775718218e-01, -2.40842144699299e-02, 1.92673715759439e-03],
&[-5.97470838462221e-02, 1.80551858630298e-01, -7.91241454636765e-02, -1.55240829877729e-01, -4.19298775383066e-01, 0.00000000000000e+00, 6.42287612546289e-01, -1.48833147569152e-01, 4.65407609802260e-02, -7.75679349670433e-03, 6.20543479736347e-04],
&[-6.19425252179959e-03, 3.69595678895333e-02, -7.01892820620398e-02, -3.35233082197107e-03, 2.69304373763091e-01, -8.89857974743355e-01, 0.00000000000000e+00, 8.66656645522330e-01, -2.57919763669076e-01, 6.44799409172690e-02, -1.07466568195448e-02, 8.59732545563586e-04],
&[1.44853491014330e-02, -4.59275574977554e-02, 3.08833474560615e-02, 3.57240610228828e-02, -7.07760049349999e-02, 1.88587240076292e-01, -7.92626447113877e-01, 0.00000000000000e+00, 8.25608497215073e-01, -2.35888142061449e-01, 5.89720355153623e-02, -9.82867258589373e-03, 7.86293806871498e-04],
];
const DIFF_BLOCK: Matrix<Float, 8, 13> = Matrix::new([
[-1.69567399396458e+00, 2.29023358159400e+00, -2.16473500425698e-01, -5.05879766354449e-01, -1.01161106778154e-01, 2.59147072064383e-01, 1.93922119400659e-02, -4.95844980755642e-02, 0.0, 0.0, 0.0, 0.0, 0.0],
[-4.42575354959737e-01, 0.00000000000000e+00, 1.91582959381899e-01, 2.82222626681305e-01, 1.12083989713257e-01, -1.51334868892111e-01, -2.23600502721044e-02, 3.03806983474913e-02, 0.0, 0.0, 0.0, 0.0,0.0],
[2.48392603571843e-01, -1.13758367065272e+00, 0.00000000000000e+00, 1.95334726810969e+00, -1.58879011773212e+00, 3.93797129320378e-01, 2.52140821030291e-01, -1.21304033647356e-01, 0.0, 0.0, 0.0, 0.0, 0.0],
[8.29213105268236e-02, -2.39388470313226e-01, -2.79038666398460e-01, 0.00000000000000e+00, 3.43018053395471e-01, 1.10370852514749e-01, 1.72029988649808e-03, -2.00445645303789e-02, 4.41184918522490e-04, 0.0, 0.0, 0.0, 0.0],
[7.24159504343116e-02, -4.15199475743626e-01, 9.91181694804303e-01, -1.49802407438608e+00, 0.00000000000000e+00, 1.30188867830442e+00, -6.03535071819214e-01, 1.73429775718218e-01, -2.40842144699299e-02, 1.92673715759439e-03, 0.0, 0.0, 0.0],
[-5.97470838462221e-02, 1.80551858630298e-01, -7.91241454636765e-02, -1.55240829877729e-01, -4.19298775383066e-01, 0.00000000000000e+00, 6.42287612546289e-01, -1.48833147569152e-01, 4.65407609802260e-02, -7.75679349670433e-03, 6.20543479736347e-04, 0.0, 0.0],
[-6.19425252179959e-03, 3.69595678895333e-02, -7.01892820620398e-02, -3.35233082197107e-03, 2.69304373763091e-01, -8.89857974743355e-01, 0.00000000000000e+00, 8.66656645522330e-01, -2.57919763669076e-01, 6.44799409172690e-02, -1.07466568195448e-02, 8.59732545563586e-04, 0.0],
[1.44853491014330e-02, -4.59275574977554e-02, 3.08833474560615e-02, 3.57240610228828e-02, -7.07760049349999e-02, 1.88587240076292e-01, -7.92626447113877e-01, 0.00000000000000e+00, 8.25608497215073e-01, -2.35888142061449e-01, 5.89720355153623e-02, -9.82867258589373e-03, 7.86293806871498e-04],
]);
const DIFF: BlockMatrix<Float, 8, 13, 11> = BlockMatrix::new(
Self::DIFF_BLOCK,
Self::DIFF_DIAG,
super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK))),
);
#[rustfmt::skip]
const DISS_BLOCK: &'static [&'static [Float]] = &[
&[-3.99020778658945e-04, 2.05394169917502e-03, -4.24493243399805e-03, 4.38126393542801e-03, -2.18883813216888e-03, 2.98565988131608e-04, 1.38484104084115e-04, -3.94643819928825e-05],
&[3.96913216138553e-04, -2.28230530115522e-03, 5.43069719436758e-03, -6.81086901935894e-03, 4.69064759201504e-03, -1.61429862514855e-03, 1.62083873811316e-04, 2.71310693302277e-05],
&[-4.87084939816571e-03, 3.22464611075207e-02, -9.06094757860846e-02, 1.39830191253413e-01, -1.27675500367419e-01, 6.87310321912961e-02, -2.00917702215270e-02, 2.43991122096699e-03],
&[7.18155125886276e-04, -5.77715378536685e-03, 1.99749582302141e-02, -3.87940986951101e-02, 4.62756436981388e-02, -3.46770570075288e-02, 1.59058082995305e-02, -4.06744078428648e-03, 4.41184918522490e-04],
&[-1.56687484682703e-03, 1.73758484693946e-02, -7.96515646886111e-02, 2.02094401829054e-01, -3.16098733124618e-01, 3.17999240131250e-01, -2.06522928911140e-01, 8.37112455598470e-02, -1.92673715759439e-02, 1.92673715759439e-03],
&[6.88352254356072e-05, -1.92595810396278e-03, 1.38098624496279e-02, -4.87746083763075e-02, 1.02417890394006e-01, -1.38292226669620e-01, 1.23829022892659e-01, -7.34723830823462e-02, 2.79244565881356e-02, -6.20543479736347e-03, 6.20543479736347e-04],
&[4.42345367100640e-05, 2.67913080025652e-04, -5.59301314813691e-03, 3.09954862110834e-02, -9.21529346596015e-02, 1.71559035817103e-01, -2.12738289547735e-01, 1.79835101537893e-01, -1.03167905467630e-01, 3.86879645503614e-02, -8.59732545563586e-03, 8.59732545563586e-04],
&[-1.15289127131636e-05, 4.10149803795578e-05, 6.21188131452618e-04, -7.24912245235322e-03, 3.41622279353287e-02, -9.30972311856124e-02, 1.64473506705108e-01, -1.98013074867399e-01, 1.65121699443015e-01, -9.43552568245798e-02, 3.53832213092174e-02, -7.86293806871498e-03, 7.86293806871498e-04]
];
const DISS_BLOCK: Matrix<Float, 8, 13> = Matrix::new([
[-3.99020778658945e-04, 2.05394169917502e-03, -4.24493243399805e-03, 4.38126393542801e-03, -2.18883813216888e-03, 2.98565988131608e-04, 1.38484104084115e-04, -3.94643819928825e-05, 0.0, 0.0, 0.0, 0.0, 0.0],
[3.96913216138553e-04, -2.28230530115522e-03, 5.43069719436758e-03, -6.81086901935894e-03, 4.69064759201504e-03, -1.61429862514855e-03, 1.62083873811316e-04, 2.71310693302277e-05, 0.0, 0.0, 0.0, 0.0, 0.0 ],
[-4.87084939816571e-03, 3.22464611075207e-02, -9.06094757860846e-02, 1.39830191253413e-01, -1.27675500367419e-01, 6.87310321912961e-02, -2.00917702215270e-02, 2.43991122096699e-03, 0.0, 0.0, 0.0, 0.0, 0.0],
[7.18155125886276e-04, -5.77715378536685e-03, 1.99749582302141e-02, -3.87940986951101e-02, 4.62756436981388e-02, -3.46770570075288e-02, 1.59058082995305e-02, -4.06744078428648e-03, 4.41184918522490e-04, 0.0, 0.0, 0.0, 0.0],
[-1.56687484682703e-03, 1.73758484693946e-02, -7.96515646886111e-02, 2.02094401829054e-01, -3.16098733124618e-01, 3.17999240131250e-01, -2.06522928911140e-01, 8.37112455598470e-02, -1.92673715759439e-02, 1.92673715759439e-03, 0.0, 0.0, 0.0],
[6.88352254356072e-05, -1.92595810396278e-03, 1.38098624496279e-02, -4.87746083763075e-02, 1.02417890394006e-01, -1.38292226669620e-01, 1.23829022892659e-01, -7.34723830823462e-02, 2.79244565881356e-02, -6.20543479736347e-03, 6.20543479736347e-04, 0.0, 0.0],
[4.42345367100640e-05, 2.67913080025652e-04, -5.59301314813691e-03, 3.09954862110834e-02, -9.21529346596015e-02, 1.71559035817103e-01, -2.12738289547735e-01, 1.79835101537893e-01, -1.03167905467630e-01, 3.86879645503614e-02, -8.59732545563586e-03, 8.59732545563586e-04, 0.0],
[-1.15289127131636e-05, 4.10149803795578e-05, 6.21188131452618e-04, -7.24912245235322e-03, 3.41622279353287e-02, -9.30972311856124e-02, 1.64473506705108e-01, -1.98013074867399e-01, 1.65121699443015e-01, -9.43552568245798e-02, 3.53832213092174e-02, -7.86293806871498e-03, 7.86293806871498e-04]
]);
#[rustfmt::skip]
const DISS_DIAG: &'static [Float] = &[
const DISS_DIAG: RowVector<Float, 11> = RowVector::new([[
1.0/1260.0, -1.0/126.0, 1.0/28.0, -2.0/21.0, 1.0/6.0, -1.0/5.0, 1.0/6.0, -2.0/21.0, 1.0/28.0, -1.0/126.0, 1.0/1260.0,
];
]]);
const DISS: BlockMatrix<Float, 8, 13, 11> = BlockMatrix::new(
Self::DISS_BLOCK,
Self::DISS_DIAG,
super::flip_ud(super::flip_lr(Self::DISS_BLOCK)),
);
}
impl SbpOperator1d for Upwind9 {
fn diff(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
Self::BLOCK,
Self::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::Normal,
prev,
fut,
)
super::diff_op_1d(&Self::DIFF, super::OperatorType::Normal, prev, fut)
}
fn h(&self) -> &'static [Float] {
Self::HBLOCK
&Self::H.start
}
#[cfg(feature = "sparse")]
fn diff_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::sparse_from_block(
Self::BLOCK,
Self::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::Normal,
n,
)
super::sparse_from_block(&Self::DIFF, super::OperatorType::Normal, n)
}
#[cfg(feature = "sparse")]
fn h_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::h_matrix(Self::HBLOCK, n, self.is_h2())
super::h_matrix(&Self::H, n, self.is_h2())
}
fn upwind(&self) -> Option<&dyn UpwindOperator1d> {
@ -83,35 +81,14 @@ impl SbpOperator1d for Upwind9 {
}
impl SbpOperator2d for Upwind9 {
fn diffxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
fn diffxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
assert_eq!(prev.shape(), fut.shape());
assert!(prev.shape()[1] >= 2 * Upwind9::BLOCK.len());
let symmetry = super::Symmetry::AntiSymmetric;
let optype = super::OperatorType::Normal;
match (prev.strides(), fut.strides()) {
([_, 1], [_, 1]) => {
diff_op_row(Upwind9::BLOCK, Upwind9::DIAG, symmetry, optype)(prev, fut);
}
([1, _], [1, _]) => {
diff_op_col(Upwind9::BLOCK, Upwind9::DIAG, symmetry, optype)(prev, fut);
}
([_, _], [_, _]) => {
// Fallback, work row by row
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
Upwind9.diff(r0, r1);
}
}
_ => unreachable!("Should only be two elements in the strides vectors"),
}
super::diff_op_2d(&Self::DIFF, OperatorType::Normal, prev, fut)
}
fn op_xi(&self) -> &dyn SbpOperator1d {
&Self
}
fn op_eta(&self) -> &dyn SbpOperator1d {
&Self
}
fn upwind(&self) -> Option<Box<dyn UpwindOperator2d>> {
Some(Box::new(Self))
}
@ -119,14 +96,7 @@ impl SbpOperator2d for Upwind9 {
impl UpwindOperator1d for Upwind9 {
fn diss(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
Self::DISS_BLOCK,
Self::DISS_DIAG,
super::Symmetry::Symmetric,
super::OperatorType::Normal,
prev,
fut,
)
super::diff_op_1d(&Self::DISS, super::OperatorType::Normal, prev, fut)
}
fn as_sbp(&self) -> &dyn SbpOperator1d {
@ -135,46 +105,18 @@ impl UpwindOperator1d for Upwind9 {
#[cfg(feature = "sparse")]
fn diss_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::sparse_from_block(
Self::DISS_BLOCK,
Self::DISS_DIAG,
super::Symmetry::Symmetric,
super::OperatorType::Normal,
n,
)
super::sparse_from_block(&Self::DISS, super::OperatorType::Normal, n)
}
}
impl UpwindOperator2d for Upwind9 {
fn dissxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
fn dissxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
assert_eq!(prev.shape(), fut.shape());
assert!(prev.shape()[1] >= 2 * Upwind9::BLOCK.len());
let symmetry = super::Symmetry::Symmetric;
let optype = super::OperatorType::Normal;
match (prev.strides(), fut.strides()) {
([_, 1], [_, 1]) => {
diff_op_row(Upwind9::DISS_BLOCK, Upwind9::DISS_DIAG, symmetry, optype)(prev, fut);
}
([1, _], [1, _]) => {
diff_op_col(Upwind9::DISS_BLOCK, Upwind9::DISS_DIAG, symmetry, optype)(prev, fut);
}
([_, _], [_, _]) => {
// Fallback, work row by row
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
Upwind9.diss(r0, r1);
}
}
_ => unreachable!("Should only be two elements in the strides vectors"),
}
super::diff_op_2d(&Self::DISS, OperatorType::Normal, prev, fut)
}
fn op_xi(&self) -> &dyn UpwindOperator1d {
&Self
}
fn op_eta(&self) -> &dyn UpwindOperator1d {
&Self
}
}
#[test]

View File

@ -1,5 +1,6 @@
use super::{
diff_op_col, diff_op_row, SbpOperator1d, SbpOperator2d, UpwindOperator1d, UpwindOperator2d,
BlockMatrix, DiagonalMatrix, Matrix, OperatorType, RowVector, SbpOperator1d, SbpOperator2d,
UpwindOperator1d, UpwindOperator2d,
};
use crate::Float;
use ndarray::{ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2};
@ -9,57 +10,60 @@ pub struct Upwind9h2;
impl Upwind9h2 {
#[rustfmt::skip]
const HBLOCK: &'static [Float] = &[
const H: DiagonalMatrix<8> = DiagonalMatrix::new([
0.13528301e8/0.97297200e8, 0.189103813e9/0.232243200e9, 0.125336387e9/0.116121600e9, 0.24412231e8/0.25804800e8, 0.11976149e8/0.11612160e8, 0.27510113e8/0.27869184e8, 0.142384289e9/0.141926400e9, 0.3018054133e10/0.3019161600e10
];
]);
#[rustfmt::skip]
const DIAG: &'static [Float] = &[
const DIFF_DIAG: RowVector<Float, 11> = RowVector::new([[
-7.93650793650794e-04, 9.92063492063492e-03, -5.95238095238095e-02, 2.38095238095238e-01, -8.33333333333333e-01, 0.00000000000000e+00, 8.33333333333333e-01, -2.38095238095238e-01, 5.95238095238095e-02, -9.92063492063492e-03, 7.93650793650794e-04
];
]]);
#[rustfmt::skip]
const BLOCK: &'static [&'static [Float]] = &[
&[-3.59606132359119e+00, 4.89833536312447e+00, -1.88949383693789e+00, 5.47742030095865e-01, 1.98323891961440e-01, -1.81919774755160e-01, 9.75536252790282e-03, 1.33182875745630e-02],
&[-8.36438764514407e-01, 0.00000000000000e+00, 1.13444534067338e+00, -2.98589126517177e-01, -5.64158604558766e-02, 6.76718494319307e-02, -7.11434545077044e-03, -3.55909316707803e-03],
&[2.43402051702672e-01, -8.55808694890095e-01, 0.00000000000000e+00, 7.10023434316597e-01, -8.34706840757189e-02, -2.34098045062645e-02, 9.87675724649922e-03, -6.13059793689851e-04],
&[-8.05029896098980e-02, 2.56994775122436e-01, -8.10083655220697e-01, 0.00000000000000e+00, 7.86933049074895e-01, -1.76732206311904e-01, 2.52232469669152e-02, -2.67114375632805e-03, 8.38923734582063e-04],
&[-2.67370674924706e-02, 4.45404208225958e-02, 8.73562441688813e-02, -7.21839392529084e-01, 0.00000000000000e+00, 7.74603212862825e-01, -2.00161722480411e-01, 5.10878939438559e-02, -9.61911880020865e-03, 7.69529504016692e-04],
&[2.56244589039153e-02, -5.58209474181657e-02, 2.55972803937942e-02, 1.69377044771715e-01, -8.09310829929974e-01, 0.00000000000000e+00, 8.33417128898551e-01, -2.39938756878570e-01, 6.03007337701593e-02, -1.00501222950266e-02, 8.04009783602125e-04],
&[-1.35203347497451e-03, 5.77422023528126e-03, -1.06262408660325e-02, -2.37853245409338e-02, 2.05772021953463e-01, -8.20033622612654e-01, 0.00000000000000e+00, 8.31345778788959e-01, -2.37329555369694e-01, 5.93323888424235e-02, -9.88873147373725e-03, 7.91098517898980e-04],
&[-1.85246767034256e-03, 2.89905176240824e-03, 6.61951741227212e-04, 2.52792141498726e-03, -5.27086038822990e-02, 2.36934258275492e-01, -8.34333946306806e-01, 0.00000000000000e+00, 8.33639122800983e-01, -2.38182606514566e-01, 5.95456516286416e-02, -9.92427527144027e-03, 7.93942021715222e-04]
];
const DIFF_BLOCK: Matrix<Float, 8, 13> = Matrix::new([
[-3.59606132359119e+00, 4.89833536312447e+00, -1.88949383693789e+00, 5.47742030095865e-01, 1.98323891961440e-01, -1.81919774755160e-01, 9.75536252790282e-03, 1.33182875745630e-02, 0.0, 0.0, 0.0, 0.0, 0.0],
[-8.36438764514407e-01, 0.00000000000000e+00, 1.13444534067338e+00, -2.98589126517177e-01, -5.64158604558766e-02, 6.76718494319307e-02, -7.11434545077044e-03, -3.55909316707803e-03, 0.0, 0.0, 0.0, 0.0, 0.0],
[2.43402051702672e-01, -8.55808694890095e-01, 0.00000000000000e+00, 7.10023434316597e-01, -8.34706840757189e-02, -2.34098045062645e-02, 9.87675724649922e-03, -6.13059793689851e-04, 0.0, 0.0, 0.0, 0.0, 0.0],
[-8.05029896098980e-02, 2.56994775122436e-01, -8.10083655220697e-01, 0.00000000000000e+00, 7.86933049074895e-01, -1.76732206311904e-01, 2.52232469669152e-02, -2.67114375632805e-03, 8.38923734582063e-04, 0.0, 0.0, 0.0, 0.0],
[-2.67370674924706e-02, 4.45404208225958e-02, 8.73562441688813e-02, -7.21839392529084e-01, 0.00000000000000e+00, 7.74603212862825e-01, -2.00161722480411e-01, 5.10878939438559e-02, -9.61911880020865e-03, 7.69529504016692e-04, 0.0, 0.0, 0.0],
[2.56244589039153e-02, -5.58209474181657e-02, 2.55972803937942e-02, 1.69377044771715e-01, -8.09310829929974e-01, 0.00000000000000e+00, 8.33417128898551e-01, -2.39938756878570e-01, 6.03007337701593e-02, -1.00501222950266e-02, 8.04009783602125e-04, 0.0, 0.0],
[-1.35203347497451e-03, 5.77422023528126e-03, -1.06262408660325e-02, -2.37853245409338e-02, 2.05772021953463e-01, -8.20033622612654e-01, 0.00000000000000e+00, 8.31345778788959e-01, -2.37329555369694e-01, 5.93323888424235e-02, -9.88873147373725e-03, 7.91098517898980e-04, 0.0],
[-1.85246767034256e-03, 2.89905176240824e-03, 6.61951741227212e-04, 2.52792141498726e-03, -5.27086038822990e-02, 2.36934258275492e-01, -8.34333946306806e-01, 0.00000000000000e+00, 8.33639122800983e-01, -2.38182606514566e-01, 5.95456516286416e-02, -9.92427527144027e-03, 7.93942021715222e-04]
]);
const DIFF: BlockMatrix<Float, 8, 13, 11> = BlockMatrix::new(
Self::DIFF_BLOCK,
Self::DIFF_DIAG,
super::flip_sign(super::flip_ud(super::flip_lr(Self::DIFF_BLOCK))),
);
#[rustfmt::skip]
const DISS_BLOCK: &'static [&'static [Float]] = &[
&[-9.67684432055993e-03, 2.46709290489728e-02, -3.59750445192099e-02, 3.68391266633410e-02, -2.15642540957128e-02, 6.31810399460400e-03, -5.50815969583136e-04, -6.12008018523449e-05],
&[4.21280289799980e-03, -1.11651391003720e-02, 1.78139108670387e-02, -2.04287733194061e-02, 1.39229889120575e-02, -5.16220078628933e-03, 8.08589544070426e-04, -2.17901509897605e-06],
&[-4.63425679136442e-03, 1.34385494509234e-02, -2.60541352669482e-02, 3.74164435120410e-02, -3.36394001786921e-02, 1.82199905150995e-02, -5.42550576921788e-03, 6.78314528158575e-04],
&[5.41433680102586e-03, -1.75829845731036e-02, 4.26893646894443e-02, -7.75663958780684e-02, 9.06986463369770e-02, -6.74666360933304e-02, 3.07997352073168e-02, -7.82499022484376e-03, 8.38923734582063e-04],
&[-2.90718839510249e-03, 1.09922241766815e-02, -3.52053141560315e-02, 8.31962208882433e-02, -1.27244413706703e-01, 1.27078393791315e-01, -8.23947157593937e-02, 3.34105586971409e-02, -7.69529504016692e-03, 7.69529504016692e-04],
&[8.89941714023592e-04, -4.25818033750240e-03, 1.99225160493104e-02, -6.46588399513875e-02, 1.32772390609268e-01, -1.78508501143865e-01, 1.59999344843642e-01, -9.51030239931668e-02, 3.61804402620956e-02, -8.04009783602125e-03, 8.04009783602125e-04],
&[-7.63397185185940e-05, 6.56275990492236e-04, -5.83721252683346e-03, 2.90439093207059e-02, -8.47041434795334e-02, 1.57429980520300e-01, -1.95480070025582e-01, 1.65419875422483e-01, -9.49318221478776e-02, 3.55994333054541e-02, -7.91098517898980e-03, 7.91098517898980e-04],
&[-8.51254383837181e-06, -1.77491211003807e-06, 7.32410586432031e-04, -7.40542710012764e-03, 3.44704736857857e-02, -9.39121496781828e-02, 1.66014456295034e-01, -1.99926171069111e-01, 1.66727824560197e-01, -9.52730426058266e-02, 3.57273909771850e-02, -7.93942021715222e-03, 7.93942021715222e-04]
];
const DISS_BLOCK: Matrix<Float, 8, 13> = Matrix::new([
[-9.67684432055993e-03, 2.46709290489728e-02, -3.59750445192099e-02, 3.68391266633410e-02, -2.15642540957128e-02, 6.31810399460400e-03, -5.50815969583136e-04, -6.12008018523449e-05, 0.0, 0.0, 0.0, 0.0, 0.0],
[4.21280289799980e-03, -1.11651391003720e-02, 1.78139108670387e-02, -2.04287733194061e-02, 1.39229889120575e-02, -5.16220078628933e-03, 8.08589544070426e-04, -2.17901509897605e-06, 0.0, 0.0, 0.0, 0.0, 0.0],
[-4.63425679136442e-03, 1.34385494509234e-02, -2.60541352669482e-02, 3.74164435120410e-02, -3.36394001786921e-02, 1.82199905150995e-02, -5.42550576921788e-03, 6.78314528158575e-04, 0.0, 0.0, 0.0, 0.0, 0.0],
[5.41433680102586e-03, -1.75829845731036e-02, 4.26893646894443e-02, -7.75663958780684e-02, 9.06986463369770e-02, -6.74666360933304e-02, 3.07997352073168e-02, -7.82499022484376e-03, 8.38923734582063e-04, 0.0, 0.0, 0.0, 0.0],
[-2.90718839510249e-03, 1.09922241766815e-02, -3.52053141560315e-02, 8.31962208882433e-02, -1.27244413706703e-01, 1.27078393791315e-01, -8.23947157593937e-02, 3.34105586971409e-02, -7.69529504016692e-03, 7.69529504016692e-04, 0.0, 0.0, 0.0],
[8.89941714023592e-04, -4.25818033750240e-03, 1.99225160493104e-02, -6.46588399513875e-02, 1.32772390609268e-01, -1.78508501143865e-01, 1.59999344843642e-01, -9.51030239931668e-02, 3.61804402620956e-02, -8.04009783602125e-03, 8.04009783602125e-04, 0.0, 0.0],
[-7.63397185185940e-05, 6.56275990492236e-04, -5.83721252683346e-03, 2.90439093207059e-02, -8.47041434795334e-02, 1.57429980520300e-01, -1.95480070025582e-01, 1.65419875422483e-01, -9.49318221478776e-02, 3.55994333054541e-02, -7.91098517898980e-03, 7.91098517898980e-04, 0.0],
[-8.51254383837181e-06, -1.77491211003807e-06, 7.32410586432031e-04, -7.40542710012764e-03, 3.44704736857857e-02, -9.39121496781828e-02, 1.66014456295034e-01, -1.99926171069111e-01, 1.66727824560197e-01, -9.52730426058266e-02, 3.57273909771850e-02, -7.93942021715222e-03, 7.93942021715222e-04]
]);
#[rustfmt::skip]
const DISS_DIAG: &'static [Float; 11] = &[
const DISS_DIAG: RowVector<Float, 11> = RowVector::new([[
7.93650793650794e-04, -7.93650793650794e-03, 3.57142857142857e-02, -9.52380952380952e-02, 1.66666666666667e-01, -2.00000000000000e-01, 1.66666666666667e-01, -9.52380952380952e-02, 3.57142857142857e-02, -7.93650793650794e-03, 7.93650793650794e-04
];
]]);
const DISS: BlockMatrix<Float, 8, 13, 11> = BlockMatrix::new(
Self::DISS_BLOCK,
Self::DISS_DIAG,
super::flip_ud(super::flip_lr(Self::DISS_BLOCK)),
);
}
impl SbpOperator1d for Upwind9h2 {
fn diff(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
Self::BLOCK,
Self::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::H2,
prev,
fut,
)
super::diff_op_1d(&Self::DIFF, super::OperatorType::H2, prev, fut)
}
fn h(&self) -> &'static [Float] {
Self::HBLOCK
&Self::H.start
}
fn is_h2(&self) -> bool {
true
@ -67,17 +71,11 @@ impl SbpOperator1d for Upwind9h2 {
#[cfg(feature = "sparse")]
fn diff_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::sparse_from_block(
Self::BLOCK,
Self::DIAG,
super::Symmetry::AntiSymmetric,
super::OperatorType::H2,
n,
)
super::sparse_from_block(&Self::DIFF, super::OperatorType::H2, n)
}
#[cfg(feature = "sparse")]
fn h_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::h_matrix(Self::HBLOCK, n, self.is_h2())
super::h_matrix(&Self::H, n, self.is_h2())
}
fn upwind(&self) -> Option<&dyn UpwindOperator1d> {
@ -86,35 +84,14 @@ impl SbpOperator1d for Upwind9h2 {
}
impl SbpOperator2d for Upwind9h2 {
fn diffxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
fn diffxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
assert_eq!(prev.shape(), fut.shape());
assert!(prev.shape()[1] >= 2 * Upwind9h2::BLOCK.len());
let symmetry = super::Symmetry::AntiSymmetric;
let optype = super::OperatorType::H2;
match (prev.strides(), fut.strides()) {
([_, 1], [_, 1]) => {
diff_op_row(Upwind9h2::BLOCK, Upwind9h2::DIAG, symmetry, optype)(prev, fut);
}
([1, _], [1, _]) => {
diff_op_col(Upwind9h2::BLOCK, Upwind9h2::DIAG, symmetry, optype)(prev, fut);
}
([_, _], [_, _]) => {
// Fallback, work row by row
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
Upwind9h2.diff(r0, r1);
}
}
_ => unreachable!("Should only be two elements in the strides vectors"),
}
super::diff_op_2d(&Self::DIFF, OperatorType::H2, prev, fut)
}
fn op_xi(&self) -> &dyn SbpOperator1d {
&Self
}
fn op_eta(&self) -> &dyn SbpOperator1d {
&Self
}
fn upwind(&self) -> Option<Box<dyn UpwindOperator2d>> {
Some(Box::new(Self))
}
@ -147,14 +124,7 @@ fn upwind9h2_test() {
impl UpwindOperator1d for Upwind9h2 {
fn diss(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
Self::DISS_BLOCK,
Self::DISS_DIAG,
super::Symmetry::Symmetric,
super::OperatorType::H2,
prev,
fut,
)
super::diff_op_1d(&Self::DISS, super::OperatorType::H2, prev, fut)
}
fn as_sbp(&self) -> &dyn SbpOperator1d {
self
@ -162,54 +132,17 @@ impl UpwindOperator1d for Upwind9h2 {
#[cfg(feature = "sparse")]
fn diss_matrix(&self, n: usize) -> sprs::CsMat<Float> {
super::sparse_from_block(
Self::DISS_BLOCK,
Self::DISS_DIAG,
super::Symmetry::Symmetric,
super::OperatorType::H2,
n,
)
super::sparse_from_block(&Self::DISS, OperatorType::H2, n)
}
}
impl UpwindOperator2d for Upwind9h2 {
fn dissxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
fn dissxi(&self, prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
assert_eq!(prev.shape(), fut.shape());
assert!(prev.shape()[1] >= 2 * Upwind9h2::BLOCK.len());
let symmetry = super::Symmetry::Symmetric;
let optype = super::OperatorType::H2;
match (prev.strides(), fut.strides()) {
([_, 1], [_, 1]) => {
diff_op_row(
Upwind9h2::DISS_BLOCK,
Upwind9h2::DISS_DIAG,
symmetry,
optype,
)(prev, fut);
}
([1, _], [1, _]) => {
diff_op_col(
Upwind9h2::DISS_BLOCK,
Upwind9h2::DISS_DIAG,
symmetry,
optype,
)(prev, fut);
}
([_, _], [_, _]) => {
// Fallback, work row by row
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
Upwind9h2.diss(r0, r1);
}
}
_ => unreachable!("Should only be two elements in the strides vectors"),
}
super::diff_op_2d(&Self::DISS, OperatorType::H2, prev, fut)
}
fn op_xi(&self) -> &dyn UpwindOperator1d {
&Self
}
fn op_eta(&self) -> &dyn UpwindOperator1d {
&Self
}
}