changeset 702:3cd582257072 feature/laplace_opset

Merge in default
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Mon, 15 Feb 2021 11:30:34 +0100
parents 54ce3f9771e5 (diff) 38f9894279cd (current diff)
children 988e9cfcd58d
files Notes.md src/SbpOperators/volumeops/laplace/laplace.jl src/SbpOperators/volumeops/quadratures/inverse_quadrature.jl src/SbpOperators/volumeops/quadratures/quadrature.jl test/testSbpOperators.jl
diffstat 3 files changed, 172 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Notes.md	Mon Feb 15 11:13:12 2021 +0100
+++ b/Notes.md	Mon Feb 15 11:30:34 2021 +0100
@@ -130,6 +130,7 @@
  - [ ] How do we handle mixes of periodic and non-periodic grids? Seems it should be supported on the grid level and on the 1d operator level. Between there it should be transparent.
  - [ ] Can we have a trait to tell if a TensorMapping is transposable?
  - [ ] Is it ok to have "Constructors" for abstract types which create subtypes? For example a Grids() functions that gives different kind of grids based on input?
+ - [ ] Figure out how to treat the borrowing parameters of operators. Include in into the struct? Expose via function dispatched on the operator type and grid?
 
 ## Regions and tensormappings
 - [ ] Use a trait to indicate if a TensorMapping uses indices with regions.
--- a/src/SbpOperators/volumeops/laplace/laplace.jl	Mon Feb 15 11:13:12 2021 +0100
+++ b/src/SbpOperators/volumeops/laplace/laplace.jl	Mon Feb 15 11:30:34 2021 +0100
@@ -1,3 +1,69 @@
+"""
+    Laplace{T,Dim,...}()
+    Laplace(grid::EquidistantGrid, fn; order)
+
+Implements the Laplace operator, approximating ∑d²/xᵢ² , i = 1,...,`Dim` as a
+`TensorMapping`. Additionally, `Laplace` stores the quadrature, and boundary
+operators relevant for constructing a SBP finite difference scheme as `TensorMapping`s.
+
+Laplace(grid::EquidistantGrid, fn; order) creates the Laplace operator on an
+equidistant grid, where the operators are read from a TOML. The laplace operator
+is created using laplace(grid,...).
+"""
+struct Laplace{T, Dim, Rb, TMdiffop<:TensorMapping{T,Dim,Dim}, # Differential operator tensor mapping
+                           TMqop<:TensorMapping{T,Dim,Dim}, # Quadrature operator tensor mapping
+                           TMbop<:TensorMapping{T,Rb,Dim}, # Boundary operator tensor mapping
+                           TMbqop<:TensorMapping{T,Rb,Rb}, # Boundary quadrature tensor mapping
+                           BID<:BoundaryIdentifier} <: TensorMapping{T,Dim,Dim}
+    D::TMdiffop # Difference operator
+    H::TMqop # Quadrature (norm) operator
+    H_inv::TMqop # Inverse quadrature (norm) operator
+    e::Dict{BID,TMbop} # Boundary restriction operators
+    d::Dict{BID,TMbop} # Normal derivative operators
+    H_boundary::Dict{BID,TMbqop} # Boundary quadrature operators
+end
+export Laplace
+
+function Laplace(grid::EquidistantGrid, fn; order)
+    # TODO: Removed once we can construct the volume and
+    # boundary operators by op(grid, fn; order,...).
+    # Read stencils
+    op = read_D2_operator(fn; order)
+    D_inner_stecil = op.innerStencil
+    D_closure_stencils = op.closureStencils
+    H_closure_stencils = op.quadratureClosure
+    e_closure_stencil = op.eClosure
+    d_closure_stencil = op.dClosure
+
+    # Volume operators
+    Δ =  laplace(grid, D_inner_stecil, D_closure_stencils)
+    H =  inner_product(grid, H_closure_stencils)
+    H⁻¹ =  inverse_inner_product(grid, H_closure_stencils)
+
+    # Boundary operator - id pairs
+    bids = boundary_identifiers(grid)
+    e_pairs = ntuple(i -> Pair(bids[i],boundary_restriction(grid,e_closure_stencil,bids[i])),length(bids))
+    d_pairs = ntuple(i -> Pair(bids[i],normal_derivative(grid,d_closure_stencil,bids[i])),length(bids))
+    Hᵧ_pairs = ntuple(i -> Pair(bids[i],inner_product(boundary_grid(grid,bids[i]),H_closure_stencils)),length(bids))
+
+    return Laplace(Δ, H, H⁻¹, Dict(e_pairs), Dict(d_pairs), Dict(Hᵧ_pairs))
+end
+
+LazyTensors.range_size(L::Laplace) = LazyTensors.range_size(L.D)
+LazyTensors.domain_size(L::Laplace) = LazyTensors.domain_size(L.D)
+LazyTensors.apply(L::Laplace, v::AbstractArray, I...) = LazyTensors.apply(L.D,v,I...)
+
+quadrature(L::Laplace) = L.H
+export quadrature
+inverse_quadrature(L::Laplace) = L.H_inv
+export inverse_quadrature
+boundary_restriction(L::Laplace,bid::BoundaryIdentifier) = L.e[bid]
+export boundary_restriction
+normal_derivative(L::Laplace,bid::BoundaryIdentifier) = L.d[bid]
+export normal_derivative
+boundary_quadrature(L::Laplace,bid::BoundaryIdentifier) = L.H_boundary[bid]
+export boundary_quadrature
+
 """
     laplace(grid::EquidistantGrid{Dim}, inner_stencil, closure_stencils)
 
--- a/test/testSbpOperators.jl	Mon Feb 15 11:13:12 2021 +0100
+++ b/test/testSbpOperators.jl	Mon Feb 15 11:30:34 2021 +0100
@@ -339,6 +339,95 @@
     @testset "Constructors" begin
         op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4)
         @testset "1D" begin
+            # Create all tensor mappings included in Laplace
+            Δ = laplace(g_1D, op.innerStencil, op.closureStencils)
+            H = inner_product(g_1D, op.quadratureClosure)
+            Hi = inverse_inner_product(g_1D, op.quadratureClosure)
+
+            (id_l, id_r) = boundary_identifiers(g_1D)
+
+            e_l = boundary_restriction(g_1D,op.eClosure,id_l)
+            e_r = boundary_restriction(g_1D,op.eClosure,id_r)
+            e_dict = Dict(Pair(id_l,e_l),Pair(id_r,e_r))
+
+            d_l = normal_derivative(g_1D,op.dClosure,id_l)
+            d_r = normal_derivative(g_1D,op.dClosure,id_r)
+            d_dict = Dict(Pair(id_l,d_l),Pair(id_r,d_r))
+
+            H_l = inner_product(boundary_grid(g_1D,id_l),op.quadratureClosure)
+            H_r = inner_product(boundary_grid(g_1D,id_r),op.quadratureClosure)
+            Hb_dict = Dict(Pair(id_l,H_l),Pair(id_r,H_r))
+
+            # TODO: Not sure why this doesnt work? Comparing the fields of
+            # Laplace seems to work. Reformulate below once solved.
+            @test_broken Laplace(Δ,H,Hi,e_dict,d_dict,Hb_dict) == Laplace(g_1D, sbp_operators_path()*"standard_diagonal.toml"; order=4)
+            L = Laplace(g_1D, sbp_operators_path()*"standard_diagonal.toml"; order=4)
+            @test L.D == Δ
+            @test L.H == H
+            @test L.H_inv == Hi
+            @test L.e == e_dict
+            @test L.d == d_dict
+            @test L.H_boundary == Hb_dict
+
+            @test L isa TensorMapping{T,1,1}  where T
+            @inferred Laplace(Δ,H,Hi,e_dict,d_dict,Hb_dict)
+        end
+        @testset "3D" begin
+            # Create all tensor mappings included in Laplace
+            Δ = laplace(g_3D, op.innerStencil, op.closureStencils)
+            H = inner_product(g_3D, op.quadratureClosure)
+            Hi = inverse_inner_product(g_3D, op.quadratureClosure)
+
+            (id_l, id_r, id_s, id_n, id_b, id_t) = boundary_identifiers(g_3D)
+
+            e_l = boundary_restriction(g_3D,op.eClosure,id_l)
+            e_r = boundary_restriction(g_3D,op.eClosure,id_r)
+            e_s = boundary_restriction(g_3D,op.eClosure,id_s)
+            e_n = boundary_restriction(g_3D,op.eClosure,id_n)
+            e_b = boundary_restriction(g_3D,op.eClosure,id_b)
+            e_t = boundary_restriction(g_3D,op.eClosure,id_t)
+            e_dict = Dict(Pair(id_l,e_l),Pair(id_r,e_r),
+                          Pair(id_s,e_s),Pair(id_n,e_n),
+                          Pair(id_b,e_b),Pair(id_t,e_t))
+
+            d_l = normal_derivative(g_3D,op.dClosure,id_l)
+            d_r = normal_derivative(g_3D,op.dClosure,id_r)
+            d_s = normal_derivative(g_3D,op.dClosure,id_s)
+            d_n = normal_derivative(g_3D,op.dClosure,id_n)
+            d_b = normal_derivative(g_3D,op.dClosure,id_b)
+            d_t = normal_derivative(g_3D,op.dClosure,id_t)
+            d_dict = Dict(Pair(id_l,d_l),Pair(id_r,d_r),
+                          Pair(id_s,d_s),Pair(id_n,d_n),
+                          Pair(id_b,d_b),Pair(id_t,d_t))
+
+            H_l = inner_product(boundary_grid(g_3D,id_l),op.quadratureClosure)
+            H_r = inner_product(boundary_grid(g_3D,id_r),op.quadratureClosure)
+            H_s = inner_product(boundary_grid(g_3D,id_s),op.quadratureClosure)
+            H_n = inner_product(boundary_grid(g_3D,id_n),op.quadratureClosure)
+            H_b = inner_product(boundary_grid(g_3D,id_b),op.quadratureClosure)
+            H_t = inner_product(boundary_grid(g_3D,id_t),op.quadratureClosure)
+            Hb_dict = Dict(Pair(id_l,H_l),Pair(id_r,H_r),
+                          Pair(id_s,H_s),Pair(id_n,H_n),
+                          Pair(id_b,H_b),Pair(id_t,H_t))
+
+            # TODO: Not sure why this doesnt work? Comparing the fields of
+            # Laplace seems to work. Reformulate below once solved.
+            @test_broken Laplace(Δ,H,Hi,e_dict,d_dict,Hb_dict) == Laplace(g_3D, sbp_operators_path()*"standard_diagonal.toml"; order=4)
+            L = Laplace(g_3D, sbp_operators_path()*"standard_diagonal.toml"; order=4)
+            @test L.D == Δ
+            @test L.H == H
+            @test L.H_inv == Hi
+            @test L.e == e_dict
+            @test L.d == d_dict
+            @test L.H_boundary == Hb_dict
+            @test L isa TensorMapping{T,3,3} where T
+            @inferred Laplace(Δ,H,Hi,e_dict,d_dict,Hb_dict)
+        end
+    end
+
+    @testset "laplace" begin
+        op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4)
+        @testset "1D" begin
             L = laplace(g_1D, op.innerStencil, op.closureStencils)
             @test L == second_derivative(g_1D, op.innerStencil, op.closureStencils)
             @test L isa TensorMapping{T,1,1}  where T
@@ -350,9 +439,25 @@
             Dyy = second_derivative(g_3D, op.innerStencil, op.closureStencils,2)
             Dzz = second_derivative(g_3D, op.innerStencil, op.closureStencils,3)
             @test L == Dxx + Dyy + Dzz
+            @test L isa TensorMapping{T,3,3} where T
         end
     end
 
+    @testset "quadrature" begin
+    end
+
+    @testset "inverse_quadrature" begin
+    end
+
+    @testset "boundary_restriction" begin
+    end
+
+    @testset "normal_restriction" begin
+    end
+
+    @testset "boundary_quadrature" begin
+    end
+
     # Exact differentiation is measured point-wise. In other cases
     # the error is measured in the l2-norm.
     @testset "Accuracy" begin