SbpOperator takes &self

This commit is contained in:
Magnus Ulimoen
2020-04-14 21:59:02 +02:00
parent abc9c09bd7
commit 85f3c46430
13 changed files with 285 additions and 233 deletions

View File

@@ -2,8 +2,8 @@ use super::SbpOperator;
use crate::Float;
use ndarray::{ArrayView1, ArrayViewMut1};
#[derive(Debug)]
pub struct SBP4 {}
#[derive(Debug, Copy, Clone)]
pub struct SBP4;
impl SBP4 {
#[rustfmt::skip]
@@ -24,7 +24,7 @@ impl SBP4 {
}
impl SbpOperator for SBP4 {
fn diff1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
fn diff1d(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
ndarray::arr2(Self::BLOCK).view(),
ndarray::arr1(Self::DIAG).view(),
@@ -35,7 +35,7 @@ impl SbpOperator for SBP4 {
)
}
fn h() -> &'static [Float] {
fn h(&self) -> &'static [Float] {
Self::HBLOCK
}
}
@@ -47,15 +47,24 @@ fn test_trad4() {
let nx = 20;
let ny = 13;
check_operator_on::<SBP4, _, _, _>((ny, nx), |x, y| x + 2.0 * y, |_, _| 1.0, |_, _| 2.0, 1e-4);
check_operator_on::<SBP4, _, _, _>(
check_operator_on(
SBP4,
(ny, nx),
|x, y| x + 2.0 * y,
|_, _| 1.0,
|_, _| 2.0,
1e-4,
);
check_operator_on(
SBP4,
(ny, nx),
|x, y| x * x + 2.0 * x * y + 3.0 * y * y,
|x, y| 2.0 * x + 2.0 * y,
|x, y| 2.0 * x + 6.0 * y,
1e-3,
);
check_operator_on::<SBP4, _, _, _>(
check_operator_on(
SBP4,
(ny, nx),
|x, y| x.powi(3) + 2.0 * x.powi(2) * y + 3.0 * x * y.powi(2) + 4.0 * y.powi(3),
|x, y| 3.0 * x.powi(2) + 4.0 * x * y + 3.0 * y.powi(2),

View File

@@ -2,8 +2,8 @@ use super::SbpOperator;
use crate::Float;
use ndarray::{ArrayView1, ArrayViewMut1};
#[derive(Debug)]
pub struct SBP8 {}
#[derive(Debug, Copy, Clone)]
pub struct SBP8;
impl SBP8 {
#[rustfmt::skip]
@@ -28,7 +28,7 @@ impl SBP8 {
}
impl SbpOperator for SBP8 {
fn diff1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
fn diff1d(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
ndarray::arr2(Self::BLOCK).view(),
ndarray::arr1(Self::DIAG).view(),
@@ -39,7 +39,7 @@ impl SbpOperator for SBP8 {
)
}
fn h() -> &'static [Float] {
fn h(&self) -> &'static [Float] {
Self::HBLOCK
}
}
@@ -51,7 +51,8 @@ fn test_trad8() {
let ny = 16;
// Order one polynomial
check_operator_on::<SBP8, _, _, _>(
check_operator_on(
SBP8,
(ny, nx),
|x, y| x + 2.0 * y,
|_x, _y| 1.0,
@@ -60,31 +61,35 @@ fn test_trad8() {
);
// Order two polynomial
check_operator_on::<SBP8, _, _, _>(
check_operator_on(
SBP8,
(ny, nx),
|x, y| x * x + 0.5 * y * y,
|x, _y| 2.0 * x,
|_x, y| y,
1e-4,
);
check_operator_on::<SBP8, _, _, _>((ny, nx), |x, y| x * y, |_x, y| y, |x, _y| x, 1e-4);
check_operator_on(SBP8, (ny, nx), |x, y| x * y, |_x, y| y, |x, _y| x, 1e-4);
// Order three polynomials
check_operator_on::<SBP8, _, _, _>(
check_operator_on(
SBP8,
(ny, nx),
|x, y| x * x * x + y * y * y / 6.0,
|x, _y| 3.0 * x * x,
|_x, y| y * y / 2.0,
1e-4,
);
check_operator_on::<SBP8, _, _, _>(
check_operator_on(
SBP8,
(ny, nx),
|x, y| x * x * y + x * y * y / 2.0,
|x, y| 2.0 * x * y + y * y / 2.0,
|x, y| x * x + x * y,
1e-4,
);
check_operator_on::<SBP8, _, _, _>(
check_operator_on(
SBP8,
(ny, nx),
|x, y| x.powi(3) + 2.0 * x.powi(2) * y + 3.0 * x * y.powi(2) + 4.0 * y.powi(3),
|x, y| 3.0 * x.powi(2) + 4.0 * x * y + 3.0 * y.powi(2),
@@ -93,7 +98,8 @@ fn test_trad8() {
);
// Order four polynomials
check_operator_on::<SBP8, _, _, _>(
check_operator_on(
SBP8,
(ny, nx),
|x, y| x.powi(4) + x.powi(3) * y + x.powi(2) * y.powi(2) + x * y.powi(3) + y.powi(4),
|x, y| 4.0 * x.powi(3) + 3.0 * x.powi(2) * y + 2.0 * x * y.powi(2) + y.powi(3),

View File

@@ -2,8 +2,8 @@ use super::{SbpOperator, UpwindOperator};
use crate::Float;
use ndarray::{ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2, Axis};
#[derive(Debug)]
pub struct Upwind4 {}
#[derive(Debug, Copy, Clone)]
pub struct Upwind4;
/// Simdtype used in diff_simd_col and diff_simd_row
#[cfg(feature = "f32")]
@@ -276,7 +276,7 @@ impl Upwind4 {
}
impl SbpOperator for Upwind4 {
fn diff1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
fn diff1d(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
ndarray::arr2(Self::BLOCK).view(),
ndarray::arr1(Self::DIAG).view(),
@@ -286,7 +286,7 @@ impl SbpOperator for Upwind4 {
fut,
)
}
fn diffxi(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
fn diffxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
assert_eq!(prev.shape(), fut.shape());
assert!(prev.shape()[1] >= 2 * Self::BLOCK.len());
@@ -300,14 +300,14 @@ impl SbpOperator for Upwind4 {
([_, _], [_, _]) => {
// Fallback, work row by row
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
Self::diff1d(r0, r1);
Self.diff1d(r0, r1);
}
}
_ => unreachable!("Should only be two elements in the strides vectors"),
}
}
fn h() -> &'static [Float] {
fn h(&self) -> &'static [Float] {
Self::HBLOCK
}
}
@@ -326,14 +326,14 @@ fn upwind4_test() {
target[i] = 1.0;
}
res.fill(0.0);
Upwind4::diff1d(source.view(), res.view_mut());
Upwind4.diff1d(source.view(), res.view_mut());
approx::assert_abs_diff_eq!(&res, &target, epsilon = 1e-4);
{
let source = source.to_owned().insert_axis(ndarray::Axis(0));
let mut res = res.to_owned().insert_axis(ndarray::Axis(0));
let target = target.to_owned().insert_axis(ndarray::Axis(0));
res.fill(0.0);
Upwind4::diffxi(source.view(), res.view_mut());
Upwind4.diffxi(source.view(), res.view_mut());
approx::assert_abs_diff_eq!(&res, &target, epsilon = 1e-2);
}
@@ -342,7 +342,7 @@ fn upwind4_test() {
let target = Array2::from_shape_fn((nx, 8), |(i, _)| target[i]);
let mut res = Array2::zeros((nx, 8));
res.fill(0.0);
Upwind4::diffeta(source.view(), res.view_mut());
Upwind4.diffeta(source.view(), res.view_mut());
approx::assert_abs_diff_eq!(&res.to_owned(), &target.to_owned(), epsilon = 1e-2);
}
@@ -352,14 +352,14 @@ fn upwind4_test() {
target[i] = 2.0 * x;
}
res.fill(0.0);
Upwind4::diff1d(source.view(), res.view_mut());
Upwind4.diff1d(source.view(), res.view_mut());
approx::assert_abs_diff_eq!(&res, &target, epsilon = 1e-4);
{
let source = source.to_owned().insert_axis(ndarray::Axis(0));
let mut res = res.to_owned().insert_axis(ndarray::Axis(0));
let target = target.to_owned().insert_axis(ndarray::Axis(0));
res.fill(0.0);
Upwind4::diffxi(source.view(), res.view_mut());
Upwind4.diffxi(source.view(), res.view_mut());
approx::assert_abs_diff_eq!(&res, &target, epsilon = 1e-2);
}
@@ -368,7 +368,7 @@ fn upwind4_test() {
let target = Array2::from_shape_fn((nx, 8), |(i, _)| target[i]);
let mut res = Array2::zeros((nx, 8));
res.fill(0.0);
Upwind4::diffeta(source.view(), res.view_mut());
Upwind4.diffeta(source.view(), res.view_mut());
approx::assert_abs_diff_eq!(&res.to_owned(), &target.to_owned(), epsilon = 1e-2);
}
@@ -378,7 +378,7 @@ fn upwind4_test() {
target[i] = 3.0 * x * x;
}
res.fill(0.0);
Upwind4::diff1d(source.view(), res.view_mut());
Upwind4.diff1d(source.view(), res.view_mut());
approx::assert_abs_diff_eq!(&res, &target, epsilon = 1e-2);
{
@@ -386,7 +386,7 @@ fn upwind4_test() {
let mut res = res.to_owned().insert_axis(ndarray::Axis(0));
let target = target.to_owned().insert_axis(ndarray::Axis(0));
res.fill(0.0);
Upwind4::diffxi(source.view(), res.view_mut());
Upwind4.diffxi(source.view(), res.view_mut());
approx::assert_abs_diff_eq!(&res, &target, epsilon = 1e-2);
}
@@ -395,13 +395,13 @@ fn upwind4_test() {
let target = Array2::from_shape_fn((nx, 8), |(i, _)| target[i]);
let mut res = Array2::zeros((nx, 8));
res.fill(0.0);
Upwind4::diffeta(source.view(), res.view_mut());
Upwind4.diffeta(source.view(), res.view_mut());
approx::assert_abs_diff_eq!(&res.to_owned(), &target.to_owned(), epsilon = 1e-2);
}
}
impl UpwindOperator for Upwind4 {
fn diss1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
fn diss1d(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
ndarray::arr2(Self::DISS_BLOCK).view(),
ndarray::arr1(Self::DISS_DIAG).view(),
@@ -411,7 +411,7 @@ impl UpwindOperator for Upwind4 {
fut,
)
}
fn dissxi(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
fn dissxi(&self, prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
assert_eq!(prev.shape(), fut.shape());
assert!(prev.shape()[1] >= 2 * Self::BLOCK.len());
@@ -425,7 +425,7 @@ impl UpwindOperator for Upwind4 {
([_, _], [_, _]) => {
// Fallback, work row by row
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
Self::diss1d(r0, r1);
Self.diss1d(r0, r1);
}
}
_ => unreachable!("Should only be two elements in the strides vectors"),
@@ -440,28 +440,32 @@ fn upwind4_test2() {
let nx = 32;
let ny = 16;
check_operator_on::<Upwind4, _, _, _>(
check_operator_on(
Upwind4,
(ny, nx),
|x, y| x + 2.0 * y,
|_, _| 1.0,
|_, _| 2.0,
1e-4,
);
check_operator_on::<Upwind4, _, _, _>(
check_operator_on(
Upwind4,
(ny, nx),
|x, y| x * x + 2.0 * x * y + 3.0 * y * y,
|x, y| 2.0 * x + 2.0 * y,
|x, y| 2.0 * x + 6.0 * y,
1e-3,
);
check_operator_on::<Upwind4, _, _, _>(
check_operator_on(
Upwind4,
(ny, nx),
|x, y| x.powi(3) + 2.0 * x.powi(2) * y + 3.0 * x * y.powi(2) + 4.0 * y.powi(3),
|x, y| 3.0 * x.powi(2) + 4.0 * x * y + 3.0 * y.powi(2),
|x, y| 2.0 * x.powi(2) + 6.0 * x * y + 12.0 * y.powi(2),
1e-1,
);
check_operator_on::<Upwind4, _, _, _>(
check_operator_on(
Upwind4,
(32, 32),
|x, y| x.powi(3) + 2.0 * x.powi(2) * y + 3.0 * x * y.powi(2) + 4.0 * y.powi(3),
|x, y| 3.0 * x.powi(2) + 4.0 * x * y + 3.0 * y.powi(2),

View File

@@ -2,8 +2,8 @@ use super::{SbpOperator, UpwindOperator};
use crate::Float;
use ndarray::{ArrayView1, ArrayViewMut1};
#[derive(Debug)]
pub struct Upwind4h2 {}
#[derive(Debug, Copy, Clone)]
pub struct Upwind4h2;
impl Upwind4h2 {
#[rustfmt::skip]
@@ -37,7 +37,7 @@ impl Upwind4h2 {
}
impl SbpOperator for Upwind4h2 {
fn diff1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
fn diff1d(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
ndarray::arr2(Self::BLOCK).view(),
ndarray::arr1(Self::DIAG).view(),
@@ -48,10 +48,10 @@ impl SbpOperator for Upwind4h2 {
)
}
fn h() -> &'static [Float] {
fn h(&self) -> &'static [Float] {
Self::HBLOCK
}
fn is_h2() -> bool {
fn is_h2(&self) -> bool {
true
}
}
@@ -64,25 +64,25 @@ fn upwind4h2_test() {
let mut res = ndarray::Array1::zeros(nx);
Upwind4h2::diff1d(x.view(), res.view_mut());
Upwind4h2.diff1d(x.view(), res.view_mut());
let ans = &x * 0.0 + 1.0;
approx::assert_abs_diff_eq!(&res, &ans, epsilon = 1e-4);
res.fill(0.0);
let y = &x * &x / 2.0;
Upwind4h2::diff1d(y.view(), res.view_mut());
Upwind4h2.diff1d(y.view(), res.view_mut());
let ans = &x;
approx::assert_abs_diff_eq!(&res, &ans, epsilon = 1e-4);
res.fill(0.0);
let y = &x * &x * &x / 3.0;
Upwind4h2::diff1d(y.view(), res.view_mut());
Upwind4h2.diff1d(y.view(), res.view_mut());
let ans = &x * &x;
approx::assert_abs_diff_eq!(&res, &ans, epsilon = 1e-2);
}
impl UpwindOperator for Upwind4h2 {
fn diss1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
fn diss1d(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
ndarray::arr2(Self::DISS_BLOCK).view(),
ndarray::arr1(Self::DISS_DIAG).view(),

View File

@@ -2,8 +2,8 @@ use super::{SbpOperator, UpwindOperator};
use crate::Float;
use ndarray::{ArrayView1, ArrayViewMut1};
#[derive(Debug)]
pub struct Upwind9 {}
#[derive(Debug, Copy, Clone)]
pub struct Upwind9;
impl Upwind9 {
#[rustfmt::skip]
@@ -45,7 +45,7 @@ impl Upwind9 {
}
impl SbpOperator for Upwind9 {
fn diff1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
fn diff1d(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
ndarray::arr2(Self::BLOCK).view(),
ndarray::arr1(Self::DIAG).view(),
@@ -56,13 +56,13 @@ impl SbpOperator for Upwind9 {
)
}
fn h() -> &'static [Float] {
fn h(&self) -> &'static [Float] {
Self::HBLOCK
}
}
impl UpwindOperator for Upwind9 {
fn diss1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
fn diss1d(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
ndarray::arr2(Self::DISS_BLOCK).view(),
ndarray::arr1(Self::DISS_DIAG).view(),
@@ -81,7 +81,8 @@ fn test_upwind9() {
let ny = 16;
// Order one polynomial
check_operator_on::<Upwind9, _, _, _>(
check_operator_on(
Upwind9,
(ny, nx),
|x, y| x + 2.0 * y,
|_x, _y| 1.0,
@@ -90,31 +91,35 @@ fn test_upwind9() {
);
// Order two polynomial
check_operator_on::<Upwind9, _, _, _>(
check_operator_on(
Upwind9,
(ny, nx),
|x, y| x * x + 0.5 * y * y,
|x, _y| 2.0 * x,
|_x, y| y,
1e-4,
);
check_operator_on::<Upwind9, _, _, _>((ny, nx), |x, y| x * y, |_x, y| y, |x, _y| x, 1e-4);
check_operator_on(Upwind9, (ny, nx), |x, y| x * y, |_x, y| y, |x, _y| x, 1e-4);
// Order three polynomials
check_operator_on::<Upwind9, _, _, _>(
check_operator_on(
Upwind9,
(ny, nx),
|x, y| x * x * x + y * y * y / 6.0,
|x, _y| 3.0 * x * x,
|_x, y| y * y / 2.0,
1e-4,
);
check_operator_on::<Upwind9, _, _, _>(
check_operator_on(
Upwind9,
(ny, nx),
|x, y| x * x * y + x * y * y / 2.0,
|x, y| 2.0 * x * y + y * y / 2.0,
|x, y| x * x + x * y,
1e-4,
);
check_operator_on::<Upwind9, _, _, _>(
check_operator_on(
Upwind9,
(ny, nx),
|x, y| x.powi(3) + 2.0 * x.powi(2) * y + 3.0 * x * y.powi(2) + 4.0 * y.powi(3),
|x, y| 3.0 * x.powi(2) + 4.0 * x * y + 3.0 * y.powi(2),
@@ -123,7 +128,8 @@ fn test_upwind9() {
);
// Order four polynomials
check_operator_on::<Upwind9, _, _, _>(
check_operator_on(
Upwind9,
(ny, nx),
|x, y| x.powi(4) + x.powi(3) * y + x.powi(2) * y.powi(2) + x * y.powi(3) + y.powi(4),
|x, y| 4.0 * x.powi(3) + 3.0 * x.powi(2) * y + 2.0 * x * y.powi(2) + y.powi(3),

View File

@@ -2,8 +2,8 @@ use super::{SbpOperator, UpwindOperator};
use crate::Float;
use ndarray::{ArrayView1, ArrayViewMut1};
#[derive(Debug)]
pub struct Upwind9h2 {}
#[derive(Debug, Copy, Clone)]
pub struct Upwind9h2;
impl Upwind9h2 {
#[rustfmt::skip]
@@ -45,7 +45,7 @@ impl Upwind9h2 {
}
impl SbpOperator for Upwind9h2 {
fn diff1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
fn diff1d(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
ndarray::arr2(Self::BLOCK).view(),
ndarray::arr1(Self::DIAG).view(),
@@ -56,10 +56,10 @@ impl SbpOperator for Upwind9h2 {
)
}
fn h() -> &'static [Float] {
fn h(&self) -> &'static [Float] {
Self::HBLOCK
}
fn is_h2() -> bool {
fn is_h2(&self) -> bool {
true
}
}
@@ -72,25 +72,25 @@ fn upwind9h2_test() {
let mut res = ndarray::Array1::zeros(nx);
Upwind9h2::diff1d(x.view(), res.view_mut());
Upwind9h2.diff1d(x.view(), res.view_mut());
let ans = &x * 0.0 + 1.0;
approx::assert_abs_diff_eq!(&res, &ans, epsilon = 1e-4);
res.fill(0.0);
let y = &x * &x / 2.0;
Upwind9h2::diff1d(y.view(), res.view_mut());
Upwind9h2.diff1d(y.view(), res.view_mut());
let ans = &x;
approx::assert_abs_diff_eq!(&res, &ans, epsilon = 1e-4);
res.fill(0.0);
let y = &x * &x * &x / 3.0;
Upwind9h2::diff1d(y.view(), res.view_mut());
Upwind9h2.diff1d(y.view(), res.view_mut());
let ans = &x * &x;
approx::assert_abs_diff_eq!(&res, &ans, epsilon = 1e-2);
}
impl UpwindOperator for Upwind9h2 {
fn diss1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
fn diss1d(&self, prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
super::diff_op_1d(
ndarray::arr2(Self::DISS_BLOCK).view(),
ndarray::arr1(Self::DISS_DIAG).view(),