Mercurial > repos > public > sbplib_julia
changeset 780:3b29b2ff1f0e operator_storage_array_of_table
Merge in refactor/sbp_operators_method_signatures
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Mon, 19 Jul 2021 08:47:33 +0200 |
parents | bea2feebbeca (current diff) fd84ba4f8742 (diff) |
children | d2f4ac2be47f |
files | |
diffstat | 9 files changed, 145 insertions(+), 142 deletions(-) [+] |
line wrap: on
line diff
--- a/Notes.md Thu Jul 15 00:28:09 2021 +0200 +++ b/Notes.md Mon Jul 19 08:47:33 2021 +0200 @@ -330,3 +330,5 @@ A different approach would be to include it as a trait for operators so that you can specify what the adjoint for that operator is. +## Name of the `VolumeOperator` type for constant stencils +It seems that the name is too general. The name of the method `volume_operator` makes sense. It should return different types of `TensorMapping` specialized for the grid. A suggetion for a better name is `ConstantStencilVolumeOperator`
--- a/src/SbpOperators/SbpOperators.jl Thu Jul 15 00:28:09 2021 +0200 +++ b/src/SbpOperators/SbpOperators.jl Mon Jul 19 08:47:33 2021 +0200 @@ -8,7 +8,7 @@ include("d2.jl") include("readoperator.jl") include("volumeops/volume_operator.jl") -include("volumeops/derivatives/secondderivative.jl") +include("volumeops/derivatives/second_derivative.jl") include("volumeops/laplace/laplace.jl") include("volumeops/inner_products/inner_product.jl") include("volumeops/inner_products/inverse_inner_product.jl")
--- a/src/SbpOperators/boundaryops/boundary_restriction.jl Thu Jul 15 00:28:09 2021 +0200 +++ b/src/SbpOperators/boundaryops/boundary_restriction.jl Mon Jul 19 08:47:33 2021 +0200 @@ -9,7 +9,7 @@ On a one-dimensional `grid`, `e` is a `BoundaryOperator`. On a multi-dimensional `grid`, `e` is the inflation of a `BoundaryOperator`. Also see the documentation of `SbpOperators.boundary_operator(...)` for more details. """ -boundary_restriction(grid::EquidistantGrid, closure_stencil::Stencil, boundary::CartesianBoundary) = SbpOperators.boundary_operator(grid, closure_stencil, boundary) -boundary_restriction(grid::EquidistantGrid{1}, closure_stencil::Stencil, region::Region) = boundary_restriction(grid, closure_stencil, CartesianBoundary{1,typeof(region)}()) +boundary_restriction(grid::EquidistantGrid, closure_stencil, boundary::CartesianBoundary) = SbpOperators.boundary_operator(grid, closure_stencil, boundary) +boundary_restriction(grid::EquidistantGrid{1}, closure_stencil, region::Region) = boundary_restriction(grid, closure_stencil, CartesianBoundary{1,typeof(region)}()) export boundary_restriction
--- a/src/SbpOperators/boundaryops/normal_derivative.jl Thu Jul 15 00:28:09 2021 +0200 +++ b/src/SbpOperators/boundaryops/normal_derivative.jl Mon Jul 19 08:47:33 2021 +0200 @@ -9,10 +9,10 @@ On a one-dimensional `grid`, `d` is a `BoundaryOperator`. On a multi-dimensional `grid`, `d` is the inflation of a `BoundaryOperator`. Also see the documentation of `SbpOperators.boundary_operator(...)` for more details. """ -function normal_derivative(grid::EquidistantGrid, closure_stencil::Stencil, boundary::CartesianBoundary) +function normal_derivative(grid::EquidistantGrid, closure_stencil, boundary::CartesianBoundary) direction = dim(boundary) h_inv = inverse_spacing(grid)[direction] return SbpOperators.boundary_operator(grid, scale(closure_stencil,h_inv), boundary) end -normal_derivative(grid::EquidistantGrid{1}, closure_stencil::Stencil, region::Region) = normal_derivative(grid, closure_stencil, CartesianBoundary{1,typeof(region)}()) +normal_derivative(grid::EquidistantGrid{1}, closure_stencil, region::Region) = normal_derivative(grid, closure_stencil, CartesianBoundary{1,typeof(region)}()) export normal_derivative
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SbpOperators/volumeops/derivatives/second_derivative.jl Mon Jul 19 08:47:33 2021 +0200 @@ -0,0 +1,20 @@ +""" + second_derivative(grid::EquidistantGrid{Dim}, inner_stencil, closure_stencils, direction) + second_derivative(grid::EquidistantGrid{1}, inner_stencil, closure_stencils) + +Creates the second-derivative operator `D2` as a `TensorMapping` + +`D2` approximates the second-derivative d²/dξ² on `grid` along the coordinate dimension specified by +`direction`, using the stencil `inner_stencil` in the interior and a set of stencils `closure_stencils` +for the points in the closure regions. + +On a one-dimensional `grid`, `D2` is a `VolumeOperator`. On a multi-dimensional `grid`, `D2` is the outer product of the +one-dimensional operator with the `IdentityMapping`s in orthogonal coordinate dirrections. +Also see the documentation of `SbpOperators.volume_operator(...)` for more details. +""" +function second_derivative(grid::EquidistantGrid{Dim}, inner_stencil, closure_stencils, direction) where Dim + h_inv = inverse_spacing(grid)[direction] + return SbpOperators.volume_operator(grid, scale(inner_stencil,h_inv^2), scale.(closure_stencils,h_inv^2), even, direction) +end +second_derivative(grid::EquidistantGrid{1}, inner_stencil, closure_stencils) = second_derivative(grid,inner_stencil,closure_stencils,1) +export second_derivative
--- a/src/SbpOperators/volumeops/derivatives/secondderivative.jl Thu Jul 15 00:28:09 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -""" - second_derivative(grid::EquidistantGrid{Dim}, inner_stencil, closure_stencils, direction) - second_derivative(grid::EquidistantGrid{1}, inner_stencil, closure_stencils) - -Creates the second-derivative operator `D2` as a `TensorMapping` - -`D2` approximates the second-derivative d²/dξ² on `grid` along the coordinate dimension specified by -`direction`, using the stencil `inner_stencil` in the interior and a set of stencils `closure_stencils` -for the points in the closure regions. - -On a one-dimensional `grid`, `D2` is a `VolumeOperator`. On a multi-dimensional `grid`, `D2` is the outer product of the -one-dimensional operator with the `IdentityMapping`s in orthogonal coordinate dirrections. -Also see the documentation of `SbpOperators.volume_operator(...)` for more details. -""" -function second_derivative(grid::EquidistantGrid{Dim}, inner_stencil, closure_stencils, direction) where Dim - h_inv = inverse_spacing(grid)[direction] - return SbpOperators.volume_operator(grid, scale(inner_stencil,h_inv^2), scale.(closure_stencils,h_inv^2), even, direction) -end -second_derivative(grid::EquidistantGrid{1}, inner_stencil, closure_stencils) = second_derivative(grid,inner_stencil,closure_stencils,1) -export second_derivative
--- a/src/SbpOperators/volumeops/volume_operator.jl Thu Jul 15 00:28:09 2021 +0200 +++ b/src/SbpOperators/volumeops/volume_operator.jl Mon Jul 19 08:47:33 2021 +0200 @@ -1,14 +1,15 @@ """ - volume_operator(grid,inner_stencil,closure_stencils,parity,direction) + volume_operator(grid, inner_stencil, closure_stencils, parity, direction) + Creates a volume operator on a `Dim`-dimensional grid acting along the -specified coordinate `direction`. The action of the operator is determined by the -stencils `inner_stencil` and `closure_stencils`. -When `Dim=1`, the corresponding `VolumeOperator` tensor mapping is returned. -When `Dim>1`, the `VolumeOperator` `op` is inflated by the outer product -of `IdentityMappings` in orthogonal coordinate directions, e.g for `Dim=3`, -the boundary restriction operator in the y-direction direction is `Ix⊗op⊗Iz`. +specified coordinate `direction`. The action of the operator is determined by +the stencils `inner_stencil` and `closure_stencils`. When `Dim=1`, the +corresponding `VolumeOperator` tensor mapping is returned. When `Dim>1`, the +returned operator is the appropriate outer product of a one-dimensional +operators and `IdentityMapping`s, e.g for `Dim=3` the volume operator in the +y-direction is `I⊗op⊗I`. """ -function volume_operator(grid::EquidistantGrid{Dim,T}, inner_stencil::Stencil{T}, closure_stencils::NTuple{M,Stencil{T}}, parity, direction) where {Dim,T,M} +function volume_operator(grid::EquidistantGrid{Dim,T}, inner_stencil, closure_stencils, parity, direction) where {Dim,T} #TODO: Check that direction <= Dim? # Create 1D volume operator in along coordinate direction @@ -34,7 +35,7 @@ end function VolumeOperator(grid::EquidistantGrid{1}, inner_stencil, closure_stencils, parity) - return VolumeOperator(inner_stencil, closure_stencils, size(grid), parity) + return VolumeOperator(inner_stencil, Tuple(closure_stencils), size(grid), parity) end closure_size(::VolumeOperator{T,N,M}) where {T,N,M} = M
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SbpOperators/volumeops/derivatives/second_derivative_test.jl Mon Jul 19 08:47:33 2021 +0200 @@ -0,0 +1,108 @@ +using Test + +using Sbplib.SbpOperators +using Sbplib.Grids +using Sbplib.LazyTensors + +import Sbplib.SbpOperators.VolumeOperator + +@testset "SecondDerivative" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + Lx = 3.5 + Ly = 3. + g_1D = EquidistantGrid(121, 0.0, Lx) + g_2D = EquidistantGrid((121,123), (0.0, 0.0), (Lx, Ly)) + + @testset "Constructors" begin + @testset "1D" begin + Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils) + @test Dₓₓ == second_derivative(g_1D,op.innerStencil,op.closureStencils,1) + @test Dₓₓ isa VolumeOperator + end + @testset "2D" begin + Dₓₓ = second_derivative(g_2D,op.innerStencil,op.closureStencils,1) + D2 = second_derivative(g_1D,op.innerStencil,op.closureStencils) + I = IdentityMapping{Float64}(size(g_2D)[2]) + @test Dₓₓ == D2⊗I + @test Dₓₓ isa TensorMapping{T,2,2} where T + end + end + + # Exact differentiation is measured point-wise. In other cases + # the error is measured in the l2-norm. + @testset "Accuracy" begin + @testset "1D" begin + l2(v) = sqrt(spacing(g_1D)[1]*sum(v.^2)); + monomials = () + maxOrder = 4; + for i = 0:maxOrder-1 + f_i(x) = 1/factorial(i)*x^i + monomials = (monomials...,evalOn(g_1D,f_i)) + end + v = evalOn(g_1D,x -> sin(x)) + vₓₓ = evalOn(g_1D,x -> -sin(x)) + + # 2nd order interior stencil, 1nd order boundary stencil, + # implies that L*v should be exact for monomials up to order 2. + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils) + @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 + @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 + @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10 + @test Dₓₓ*v ≈ vₓₓ rtol = 5e-2 norm = l2 + end + + # 4th order interior stencil, 2nd order boundary stencil, + # implies that L*v should be exact for monomials up to order 3. + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils) + # NOTE: high tolerances for checking the "exact" differentiation + # due to accumulation of round-off errors/cancellation errors? + @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 + @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 + @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10 + @test Dₓₓ*monomials[4] ≈ monomials[2] atol = 5e-10 + @test Dₓₓ*v ≈ vₓₓ rtol = 5e-4 norm = l2 + end + end + + @testset "2D" begin + l2(v) = sqrt(prod(spacing(g_2D))*sum(v.^2)); + binomials = () + maxOrder = 4; + for i = 0:maxOrder-1 + f_i(x,y) = 1/factorial(i)*y^i + x^i + binomials = (binomials...,evalOn(g_2D,f_i)) + end + v = evalOn(g_2D, (x,y) -> sin(x)+cos(y)) + v_yy = evalOn(g_2D,(x,y) -> -cos(y)) + + # 2nd order interior stencil, 1st order boundary stencil, + # implies that L*v should be exact for binomials up to order 2. + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + Dyy = second_derivative(g_2D,op.innerStencil,op.closureStencils,2) + @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 + @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 + @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9 + @test Dyy*v ≈ v_yy rtol = 5e-2 norm = l2 + end + + # 4th order interior stencil, 2nd order boundary stencil, + # implies that L*v should be exact for binomials up to order 3. + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + Dyy = second_derivative(g_2D,op.innerStencil,op.closureStencils,2) + # NOTE: high tolerances for checking the "exact" differentiation + # due to accumulation of round-off errors/cancellation errors? + @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 + @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 + @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9 + @test Dyy*binomials[4] ≈ evalOn(g_2D,(x,y)->y) atol = 5e-9 + @test Dyy*v ≈ v_yy rtol = 5e-4 norm = l2 + end + end + end +end
--- a/test/SbpOperators/volumeops/derivatives/secondderivative_test.jl Thu Jul 15 00:28:09 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,108 +0,0 @@ -using Test - -using Sbplib.SbpOperators -using Sbplib.Grids -using Sbplib.LazyTensors - -import Sbplib.SbpOperators.VolumeOperator - -@testset "SecondDerivative" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - Lx = 3.5 - Ly = 3. - g_1D = EquidistantGrid(121, 0.0, Lx) - g_2D = EquidistantGrid((121,123), (0.0, 0.0), (Lx, Ly)) - - @testset "Constructors" begin - @testset "1D" begin - Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils) - @test Dₓₓ == second_derivative(g_1D,op.innerStencil,op.closureStencils,1) - @test Dₓₓ isa VolumeOperator - end - @testset "2D" begin - Dₓₓ = second_derivative(g_2D,op.innerStencil,op.closureStencils,1) - D2 = second_derivative(g_1D,op.innerStencil,op.closureStencils) - I = IdentityMapping{Float64}(size(g_2D)[2]) - @test Dₓₓ == D2⊗I - @test Dₓₓ isa TensorMapping{T,2,2} where T - end - end - - # Exact differentiation is measured point-wise. In other cases - # the error is measured in the l2-norm. - @testset "Accuracy" begin - @testset "1D" begin - l2(v) = sqrt(spacing(g_1D)[1]*sum(v.^2)); - monomials = () - maxOrder = 4; - for i = 0:maxOrder-1 - f_i(x) = 1/factorial(i)*x^i - monomials = (monomials...,evalOn(g_1D,f_i)) - end - v = evalOn(g_1D,x -> sin(x)) - vₓₓ = evalOn(g_1D,x -> -sin(x)) - - # 2nd order interior stencil, 1nd order boundary stencil, - # implies that L*v should be exact for monomials up to order 2. - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils) - @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 - @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 - @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10 - @test Dₓₓ*v ≈ vₓₓ rtol = 5e-2 norm = l2 - end - - # 4th order interior stencil, 2nd order boundary stencil, - # implies that L*v should be exact for monomials up to order 3. - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - Dₓₓ = second_derivative(g_1D,op.innerStencil,op.closureStencils) - # NOTE: high tolerances for checking the "exact" differentiation - # due to accumulation of round-off errors/cancellation errors? - @test Dₓₓ*monomials[1] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 - @test Dₓₓ*monomials[2] ≈ zeros(Float64,size(g_1D)...) atol = 5e-10 - @test Dₓₓ*monomials[3] ≈ monomials[1] atol = 5e-10 - @test Dₓₓ*monomials[4] ≈ monomials[2] atol = 5e-10 - @test Dₓₓ*v ≈ vₓₓ rtol = 5e-4 norm = l2 - end - end - - @testset "2D" begin - l2(v) = sqrt(prod(spacing(g_2D))*sum(v.^2)); - binomials = () - maxOrder = 4; - for i = 0:maxOrder-1 - f_i(x,y) = 1/factorial(i)*y^i + x^i - binomials = (binomials...,evalOn(g_2D,f_i)) - end - v = evalOn(g_2D, (x,y) -> sin(x)+cos(y)) - v_yy = evalOn(g_2D,(x,y) -> -cos(y)) - - # 2nd order interior stencil, 1st order boundary stencil, - # implies that L*v should be exact for binomials up to order 2. - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - Dyy = second_derivative(g_2D,op.innerStencil,op.closureStencils,2) - @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 - @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 - @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9 - @test Dyy*v ≈ v_yy rtol = 5e-2 norm = l2 - end - - # 4th order interior stencil, 2nd order boundary stencil, - # implies that L*v should be exact for binomials up to order 3. - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - Dyy = second_derivative(g_2D,op.innerStencil,op.closureStencils,2) - # NOTE: high tolerances for checking the "exact" differentiation - # due to accumulation of round-off errors/cancellation errors? - @test Dyy*binomials[1] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 - @test Dyy*binomials[2] ≈ zeros(Float64,size(g_2D)...) atol = 5e-9 - @test Dyy*binomials[3] ≈ evalOn(g_2D,(x,y)->1.) atol = 5e-9 - @test Dyy*binomials[4] ≈ evalOn(g_2D,(x,y)->y) atol = 5e-9 - @test Dyy*v ≈ v_yy rtol = 5e-4 norm = l2 - end - end - end -end