Mercurial > repos > public > sbplib_julia
changeset 823:3c1dd7692797
Merge refactor/sbp_operators_method_signatures
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 13 Jan 2022 12:43:10 +0100 |
parents | a1d556611e3c (current diff) c7af0d04efee (diff) |
children | 126e169bb0b7 5088de9b6d65 |
files | |
diffstat | 12 files changed, 154 insertions(+), 152 deletions(-) [+] |
line wrap: on
line diff
--- a/Notes.md Thu Jan 13 12:29:11 2022 +0100 +++ b/Notes.md Thu Jan 13 12:43:10 2022 +0100 @@ -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 Jan 13 12:29:11 2022 +0100 +++ b/src/SbpOperators/SbpOperators.jl Thu Jan 13 12:43:10 2022 +0100 @@ -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_operator.jl Thu Jan 13 12:29:11 2022 +0100 +++ b/src/SbpOperators/boundaryops/boundary_operator.jl Thu Jan 13 12:43:10 2022 +0100 @@ -9,7 +9,7 @@ of `IdentityMappings` in orthogonal coordinate directions, e.g for `Dim=3`, the boundary restriction operator in the y-direction direction is `Ix⊗op⊗Iz`. """ -function boundary_operator(grid::EquidistantGrid{Dim,T}, closure_stencil::Stencil{T}, boundary::CartesianBoundary) where {Dim,T} +function boundary_operator(grid::EquidistantGrid, closure_stencil, boundary::CartesianBoundary) #TODO:Check that dim(boundary) <= Dim? # Create 1D boundary operator @@ -18,8 +18,8 @@ op = BoundaryOperator(restrict(grid, d), closure_stencil, r) # Create 1D IdentityMappings for each coordinate direction - one_d_grids = restrict.(Ref(grid), Tuple(1:Dim)) - Is = IdentityMapping{T}.(size.(one_d_grids)) + one_d_grids = restrict.(Ref(grid), Tuple(1:dimension(grid))) + Is = IdentityMapping{eltype(grid)}.(size.(one_d_grids)) # Formulate the correct outer product sequence of the identity mappings and # the boundary operator
--- a/src/SbpOperators/boundaryops/boundary_restriction.jl Thu Jan 13 12:29:11 2022 +0100 +++ b/src/SbpOperators/boundaryops/boundary_restriction.jl Thu Jan 13 12:43:10 2022 +0100 @@ -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 Jan 13 12:29:11 2022 +0100 +++ b/src/SbpOperators/boundaryops/normal_derivative.jl Thu Jan 13 12:43:10 2022 +0100 @@ -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
--- a/src/SbpOperators/stencil.jl Thu Jan 13 12:29:11 2022 +0100 +++ b/src/SbpOperators/stencil.jl Thu Jan 13 12:43:10 2022 +0100 @@ -1,10 +1,10 @@ export CenteredStencil -struct Stencil{T<:Real,N} +struct Stencil{T,N} range::Tuple{Int,Int} weights::NTuple{N,T} - function Stencil(range::Tuple{Int,Int},weights::NTuple{N,T}) where {T <: Real, N} + function Stencil(range::Tuple{Int,Int},weights::NTuple{N,T}) where {T, N} @assert range[2]-range[1]+1 == N new{T,N}(range,weights) end @@ -15,7 +15,7 @@ Create a stencil with the given weights with element `center` as the center of the stencil. """ -function Stencil(weights::Vararg{Number}; center::Int) +function Stencil(weights::Vararg{T}; center::Int) where T # Type parameter T makes sure the weights are valid for the Stencil constuctors and throws an earlier, more readable, error N = length(weights) range = (1, N) .- center
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SbpOperators/volumeops/derivatives/second_derivative.jl Thu Jan 13 12:43:10 2022 +0100 @@ -0,0 +1,19 @@ +""" + second_derivative(grid::EquidistantGrid, inner_stencil, closure_stencils, direction) + +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, inner_stencil, closure_stencils, direction) + 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 Jan 13 12:29:11 2022 +0100 +++ /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/laplace/laplace.jl Thu Jan 13 12:29:11 2022 +0100 +++ b/src/SbpOperators/volumeops/laplace/laplace.jl Thu Jan 13 12:43:10 2022 +0100 @@ -11,9 +11,9 @@ multi-dimensional `grid`, `Δ` is the sum of multi-dimensional `second_derivative`s where the sum is carried out lazily. """ -function laplace(grid::EquidistantGrid{Dim}, inner_stencil, closure_stencils) where Dim +function laplace(grid::EquidistantGrid, inner_stencil, closure_stencils) Δ = second_derivative(grid, inner_stencil, closure_stencils, 1) - for d = 2:Dim + for d = 2:dimension(grid) Δ += second_derivative(grid, inner_stencil, closure_stencils, d) end return Δ
--- a/src/SbpOperators/volumeops/volume_operator.jl Thu Jan 13 12:29:11 2022 +0100 +++ b/src/SbpOperators/volumeops/volume_operator.jl Thu Jan 13 12:43:10 2022 +0100 @@ -1,21 +1,22 @@ """ - 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, inner_stencil, closure_stencils, parity, direction) #TODO: Check that direction <= Dim? # Create 1D volume operator in along coordinate direction op = VolumeOperator(restrict(grid, direction), inner_stencil, closure_stencils, parity) # Create 1D IdentityMappings for each coordinate direction - one_d_grids = restrict.(Ref(grid), Tuple(1:Dim)) - Is = IdentityMapping{T}.(size.(one_d_grids)) + one_d_grids = restrict.(Ref(grid), Tuple(1:dimension(grid))) + Is = IdentityMapping{eltype(grid)}.(size.(one_d_grids)) # Formulate the correct outer product sequence of the identity mappings and # the volume operator parts = Base.setindex(Is, op, 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 Thu Jan 13 12:43:10 2022 +0100 @@ -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 Jan 13 12:29:11 2022 +0100 +++ /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