Mercurial > repos > public > sbplib_julia
changeset 769:0158c3fd521c operator_storage_array_of_table
Merge in default
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 15 Jul 2021 00:06:16 +0200 |
parents | 7c87a33963c5 (current diff) d7d030f8f708 (diff) |
children | 80d5717ad2f9 |
files | test/SbpOperators/readoperator_test.jl test/testSbpOperators.jl |
diffstat | 41 files changed, 2195 insertions(+), 1817 deletions(-) [+] |
line wrap: on
line diff
--- a/Manifest.toml Wed Jul 14 23:40:10 2021 +0200 +++ b/Manifest.toml Thu Jul 15 00:06:16 2021 +0200 @@ -1,13 +1,27 @@ # This file is machine-generated - editing it directly is not advised +[[Adapt]] +deps = ["LinearAlgebra"] +git-tree-sha1 = "84918055d15b3114ede17ac6a7182f68870c16f7" +uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" +version = "3.3.1" + [[Dates]] deps = ["Printf"] uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" +[[Libdl]] +uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb" + +[[LinearAlgebra]] +deps = ["Libdl"] +uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" + [[OffsetArrays]] -git-tree-sha1 = "9011c7c98769c451f83869a4d66461e2f23bc80b" +deps = ["Adapt"] +git-tree-sha1 = "2bf78c5fd7fa56d2bbf1efbadd45c1b8789e6f57" uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" -version = "1.2.1" +version = "1.10.2" [[Printf]] deps = ["Unicode"] @@ -15,15 +29,13 @@ [[TOML]] deps = ["Dates"] -git-tree-sha1 = "d0ac7eaad0fb9f6ba023a1d743edca974ae637c4" uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" -version = "1.0.0" [[TiledIteration]] deps = ["OffsetArrays"] -git-tree-sha1 = "98693daea9bb49aba71eaad6b168b152d2310358" +git-tree-sha1 = "52c5f816857bfb3291c7d25420b1f4aca0a74d18" uuid = "06e1c1a7-607b-532d-9fad-de7d9aa2abac" -version = "0.2.4" +version = "0.3.0" [[Unicode]] uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
--- a/Notes.md Wed Jul 14 23:40:10 2021 +0200 +++ b/Notes.md Thu Jul 15 00:06:16 2021 +0200 @@ -298,3 +298,35 @@ component(gf,:,2) # Andra kolumnen av en matris @ourview gf[:,:][2] ``` + +## Grids embedded in higher dimensions + +For grids generated by asking for boundary grids for a regular grid, it would +make sense if these grids knew they were embedded in a higher dimension. They +would return coordinates in the full room. This would make sense when +drawing points for example, or when evaluating functions on the boundary. + +Implementation of this is an issue that requires some thought. Adding an extra +"Embedded" type for each grid would make it easy to understand each type but +contribute to "type bloat". On the other hand adapting existing types to +handle embeddedness would complicate the now very simple grid types. Are there +other ways of doing the implentation? + +## Performance measuring +We should be measuring performance early. How does our effective cpu and memory bandwidth utilization compare to peak performance? + +We should make these test simple to run for any solver. + +See [this talk](https://www.youtube.com/watch?v=vPsfZUqI4_0) for some simple ideas for defining effecive memory usage and some comparison with peak performance. + + +## Adjoint as a trait on the sbp_operator level? + +It would be nice to have a way of refering to adjoints with resepct to the sbp-inner-product. +If it was possible you could reduce the number of times you have to deal with the inner product matrix. + +Since the LazyOperators package is sort of implementing matrix-free matrices there is no concept of inner products there at the moment. It seems to complicate large parts of the package if this was included there. + +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. + +
--- a/Project.toml Wed Jul 14 23:40:10 2021 +0200 +++ b/Project.toml Thu Jul 15 00:06:16 2021 +0200 @@ -1,7 +1,6 @@ name = "Sbplib" uuid = "5a373a26-915f-4769-bcab-bf03835de17b" -authors = ["Jonatan Werpers <jonatan@werpers.com>", - "Vidar Stiernström <vidar.stiernstrom@it.uu.se>, and contributors"] +authors = ["Jonatan Werpers <jonatan@werpers.com>", "Vidar Stiernström <vidar.stiernstrom@it.uu.se>, and contributors"] version = "0.1.0" [deps]
--- a/README.md Wed Jul 14 23:40:10 2021 +0200 +++ b/README.md Thu Jul 15 00:06:16 2021 +0200 @@ -10,6 +10,20 @@ If you want to run tests from a specific file in `test/`, you can do ``` julia> using Pkg -julia> Pkg.test(test_args=["testLazyTensors"]) +julia> Pkg.test(test_args=["[glob pattern]"]) +``` +For example +``` +julia> Pkg.test(test_args=["SbpOperators/*"]) +``` +to run all test in the `SbpOperators` folder, or ``` -This works by using the `@includetests` macro from the [TestSetExtensions](https://github.com/ssfrr/TestSetExtensions.jl) package. For more information, see their documentation. +julia> Pkg.test(test_args=["*/readoperators.jl"]) +``` +to run only the tests in files named `readoperators.jl`. +Multiple filters are allowed and will cause files matching any of the provided +filters to be run. For example +``` +Pkg.test(test_args=["*/lazy_tensor_operations_test.jl", "Grids/*"]) +``` +will run any file named `lazy_tensor_operations_test.jl` and all the files in the `Grids` folder.
--- a/src/Grids/EquidistantGrid.jl Wed Jul 14 23:40:10 2021 +0200 +++ b/src/Grids/EquidistantGrid.jl Thu Jul 15 00:06:16 2021 +0200 @@ -1,11 +1,19 @@ """ - EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T} + EquidistantGrid(size::NTuple{Dim, Int}, limit_lower::NTuple{Dim, T}, limit_upper::NTuple{Dim, T}) + EquidistantGrid{T}() + +`EquidistantGrid` is a grid with equidistant grid spacing per coordinat direction. -EquidistantGrid is a grid with equidistant grid spacing per coordinat direction. -The domain is defined through the two points P1 = x̄₁, P2 = x̄₂ by the exterior -product of the vectors obtained by projecting (x̄₂-x̄₁) onto the coordinate -directions. E.g for a 2D grid with x̄₁=(-1,0) and x̄₂=(1,2) the domain is defined -as (-1,1)x(0,2). The side lengths of the grid are not allowed to be negative +`EquidistantGrid(size, limit_lower, limit_upper)` construct the grid with the +domain defined by the two points P1, and P2 given by `limit_lower` and +`limit_upper`. The length of the domain sides are given by the components of +(P2-P1). E.g for a 2D grid with P1=(-1,0) and P2=(1,2) the domain is defined +as (-1,1)x(0,2). The side lengths of the grid are not allowed to be negative. +The number of equidistantly spaced points in each coordinate direction are given +by `size`. + +`EquidistantGrid{T}()` constructs a 0-dimensional grid. + """ struct EquidistantGrid{Dim,T<:Real} <: AbstractGrid size::NTuple{Dim, Int} @@ -22,6 +30,9 @@ end return new{Dim,T}(size, limit_lower, limit_upper) end + + # Specialized constructor for 0-dimensional grid + EquidistantGrid{T}() where T = new{0,T}((),(),()) end export EquidistantGrid @@ -35,9 +46,9 @@ return EquidistantGrid((size,),(limit_lower,),(limit_upper,)) end -function Base.eachindex(grid::EquidistantGrid) - CartesianIndices(grid.size) -end +Base.eltype(grid::EquidistantGrid{Dim,T}) where {Dim,T} = T + +Base.eachindex(grid::EquidistantGrid) = CartesianIndices(grid.size) Base.size(g::EquidistantGrid) = g.size @@ -104,3 +115,23 @@ """ boundary_identifiers(g::EquidistantGrid) = (((ntuple(i->(CartesianBoundary{i,Lower}(),CartesianBoundary{i,Upper}()),dimension(g)))...)...,) export boundary_identifiers + + +""" + boundary_grid(grid::EquidistantGrid,id::CartesianBoundary) + boundary_grid(::EquidistantGrid{1},::CartesianBoundary{1}) + +Creates the lower-dimensional restriciton of `grid` spanned by the dimensions +orthogonal to the boundary specified by `id`. The boundary grid of a 1-dimensional +grid is a zero-dimensional grid. +""" +function boundary_grid(grid::EquidistantGrid,id::CartesianBoundary) + dims = collect(1:dimension(grid)) + orth_dims = dims[dims .!= dim(id)] + if orth_dims == dims + throw(DomainError("boundary identifier not matching grid")) + end + return restrict(grid,orth_dims) +end +export boundary_grid +boundary_grid(::EquidistantGrid{1,T},::CartesianBoundary{1}) where T = EquidistantGrid{T}()
--- a/src/SbpOperators/SbpOperators.jl Wed Jul 14 23:40:10 2021 +0200 +++ b/src/SbpOperators/SbpOperators.jl Thu Jul 15 00:06:16 2021 +0200 @@ -10,8 +10,8 @@ include("volumeops/volume_operator.jl") include("volumeops/derivatives/secondderivative.jl") include("volumeops/laplace/laplace.jl") -include("volumeops/quadratures/quadrature.jl") -include("volumeops/quadratures/inverse_quadrature.jl") +include("volumeops/inner_products/inner_product.jl") +include("volumeops/inner_products/inverse_inner_product.jl") include("boundaryops/boundary_operator.jl") include("boundaryops/boundary_restriction.jl") include("boundaryops/normal_derivative.jl")
--- a/src/SbpOperators/boundaryops/boundary_restriction.jl Wed Jul 14 23:40:10 2021 +0200 +++ b/src/SbpOperators/boundaryops/boundary_restriction.jl Thu Jul 15 00:06:16 2021 +0200 @@ -1,6 +1,6 @@ """ - BoundaryRestriction(grid::EquidistantGrid, closure_stencil::Stencil, boundary::CartesianBoundary) - BoundaryRestriction(grid::EquidistantGrid{1}, closure_stencil::Stencil, region::Region) + boundary_restriction(grid::EquidistantGrid, closure_stencil::Stencil, boundary::CartesianBoundary) + boundary_restriction(grid::EquidistantGrid{1}, closure_stencil::Stencil, region::Region) Creates the boundary restriction operator `e` as a `TensorMapping` @@ -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. """ -BoundaryRestriction(grid::EquidistantGrid, closure_stencil::Stencil, boundary::CartesianBoundary) = SbpOperators.boundary_operator(grid, closure_stencil, boundary) -BoundaryRestriction(grid::EquidistantGrid{1}, closure_stencil::Stencil, region::Region) = BoundaryRestriction(grid, closure_stencil, CartesianBoundary{1,typeof(region)}()) +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)}()) -export BoundaryRestriction +export boundary_restriction
--- a/src/SbpOperators/boundaryops/normal_derivative.jl Wed Jul 14 23:40:10 2021 +0200 +++ b/src/SbpOperators/boundaryops/normal_derivative.jl Thu Jul 15 00:06:16 2021 +0200 @@ -1,6 +1,6 @@ """ - NormalDerivative(grid::EquidistantGrid, closure_stencil::Stencil, boundary::CartesianBoundary) - NormalDerivative(grid::EquidistantGrid{1}, closure_stencil::Stencil, region::Region) + normal_derivative(grid::EquidistantGrid, closure_stencil::Stencil, boundary::CartesianBoundary) + normal_derivative(grid::EquidistantGrid{1}, closure_stencil::Stencil, region::Region) Creates the normal derivative boundary operator `d` as a `TensorMapping` @@ -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 NormalDerivative(grid::EquidistantGrid, closure_stencil::Stencil, boundary::CartesianBoundary) +function normal_derivative(grid::EquidistantGrid, closure_stencil::Stencil, boundary::CartesianBoundary) direction = dim(boundary) h_inv = inverse_spacing(grid)[direction] return SbpOperators.boundary_operator(grid, scale(closure_stencil,h_inv), boundary) end -NormalDerivative(grid::EquidistantGrid{1}, closure_stencil::Stencil, region::Region) = NormalDerivative(grid, closure_stencil, CartesianBoundary{1,typeof(region)}()) -export NormalDerivative +normal_derivative(grid::EquidistantGrid{1}, closure_stencil::Stencil, region::Region) = normal_derivative(grid, closure_stencil, CartesianBoundary{1,typeof(region)}()) +export normal_derivative
--- a/src/SbpOperators/volumeops/derivatives/secondderivative.jl Wed Jul 14 23:40:10 2021 +0200 +++ b/src/SbpOperators/volumeops/derivatives/secondderivative.jl Thu Jul 15 00:06:16 2021 +0200 @@ -1,6 +1,6 @@ """ - SecondDerivative(grid::EquidistantGrid{Dim}, inner_stencil, closure_stencils, direction) - SecondDerivative(grid::EquidistantGrid{1}, inner_stencil, closure_stencils) + 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` @@ -12,9 +12,9 @@ one-dimensional operator with the `IdentityMapping`s in orthogonal coordinate dirrections. Also see the documentation of `SbpOperators.volume_operator(...)` for more details. """ -function SecondDerivative(grid::EquidistantGrid{Dim}, inner_stencil, closure_stencils, direction) where Dim +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 -SecondDerivative(grid::EquidistantGrid{1}, inner_stencil, closure_stencils) = SecondDerivative(grid,inner_stencil,closure_stencils,1) -export SecondDerivative +second_derivative(grid::EquidistantGrid{1}, inner_stencil, closure_stencils) = second_derivative(grid,inner_stencil,closure_stencils,1) +export second_derivative
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SbpOperators/volumeops/inner_products/inner_product.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,29 @@ +""" + inner_product(grid::EquidistantGrid, closure_stencils, inner_stencil) + +Creates the discrete inner product operator `H` as a `TensorMapping` on an equidistant +grid, defined as `(u,v) = u'Hv` for grid functions `u,v`. + +`inner_product(grid::EquidistantGrid, closure_stencils, inner_stencil)` creates +`H` on `grid` the using a set of stencils `closure_stencils` for the points in +the closure regions and the stencil and `inner_stencil` in the interior. If +`inner_stencil` is omitted a central interior stencil with weight 1 is used. + +On a 1-dimensional `grid`, `H` is a `VolumeOperator`. On a N-dimensional +`grid`, `H` is the outer product of the 1-dimensional inner product operators in +each coordinate direction. Also see the documentation of +`SbpOperators.volume_operator(...)` for more details. On a 0-dimensional `grid`, +`H` is a 0-dimensional `IdentityMapping`. +""" +function inner_product(grid::EquidistantGrid, closure_stencils, inner_stencil = CenteredStencil(one(eltype(grid)))) + h = spacing(grid) + H = SbpOperators.volume_operator(grid, scale(inner_stencil,h[1]), scale.(closure_stencils,h[1]), even, 1) + for i ∈ 2:dimension(grid) + Hᵢ = SbpOperators.volume_operator(grid, scale(inner_stencil,h[i]), scale.(closure_stencils,h[i]), even, i) + H = H∘Hᵢ + end + return H +end +export inner_product + +inner_product(grid::EquidistantGrid{0}, closure_stencils, inner_stencil) = IdentityMapping{eltype(grid)}()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SbpOperators/volumeops/inner_products/inverse_inner_product.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,43 @@ +""" + inverse_inner_product(grid::EquidistantGrid, inv_inner_stencil, inv_closure_stencils) + inverse_inner_product(grid::EquidistantGrid, closure_stencils::NTuple{M,Stencil{T,1}}) + +Creates the inverse inner product operator `H⁻¹` as a `TensorMapping` on an +equidistant grid. `H⁻¹` is defined implicitly by `H⁻¹∘H = I`, where +`H` is the corresponding inner product operator and `I` is the `IdentityMapping`. + +`inverse_inner_product(grid::EquidistantGrid, inv_inner_stencil, inv_closure_stencils)` +constructs `H⁻¹` using a set of stencils `inv_closure_stencils` for the points +in the closure regions and the stencil `inv_inner_stencil` in the interior. If +`inv_closure_stencils` is omitted, a central interior stencil with weight 1 is used. + +`inverse_inner_product(grid::EquidistantGrid, closure_stencils::NTuple{M,Stencil{T,1}})` +constructs a diagonal inverse inner product operator where `closure_stencils` are the +closure stencils of `H` (not `H⁻¹`!). + +On a 1-dimensional `grid`, `H⁻¹` is a `VolumeOperator`. On a N-dimensional +`grid`, `H⁻¹` is the outer product of the 1-dimensional inverse inner product +operators in each coordinate direction. Also see the documentation of +`SbpOperators.volume_operator(...)` for more details. On a 0-dimensional `grid`, +`H⁻¹` is a 0-dimensional `IdentityMapping`. +""" +function inverse_inner_product(grid::EquidistantGrid, inv_closure_stencils, inv_inner_stencil = CenteredStencil(one(eltype(grid)))) + h⁻¹ = inverse_spacing(grid) + H⁻¹ = SbpOperators.volume_operator(grid,scale(inv_inner_stencil,h⁻¹[1]),scale.(inv_closure_stencils,h⁻¹[1]),even,1) + for i ∈ 2:dimension(grid) + Hᵢ⁻¹ = SbpOperators.volume_operator(grid,scale(inv_inner_stencil,h⁻¹[i]),scale.(inv_closure_stencils,h⁻¹[i]),even,i) + H⁻¹ = H⁻¹∘Hᵢ⁻¹ + end + return H⁻¹ +end +export inverse_inner_product + +inverse_inner_product(grid::EquidistantGrid{0}, inv_closure_stencils, inv_inner_stencil) = IdentityMapping{eltype(grid)}() + +function inverse_inner_product(grid::EquidistantGrid, closure_stencils::NTuple{M,Stencil{T,1}}) where {M,T} + inv_closure_stencils = reciprocal_stencil.(closure_stencils) + inv_inner_stencil = CenteredStencil(one(T)) + return inverse_inner_product(grid, inv_closure_stencils, inv_inner_stencil) +end + +reciprocal_stencil(s::Stencil{T}) where T = Stencil(s.range,one(T)./s.weights)
--- a/src/SbpOperators/volumeops/laplace/laplace.jl Wed Jul 14 23:40:10 2021 +0200 +++ b/src/SbpOperators/volumeops/laplace/laplace.jl Thu Jul 15 00:06:16 2021 +0200 @@ -1,5 +1,5 @@ """ - Laplace(grid::EquidistantGrid{Dim}, inner_stencil, closure_stencils) + laplace(grid::EquidistantGrid{Dim}, inner_stencil, closure_stencils) Creates the Laplace operator operator `Δ` as a `TensorMapping` @@ -7,14 +7,15 @@ 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`, `Δ` is a `SecondDerivative`. On a multi-dimensional `grid`, `Δ` is the sum of -multi-dimensional `SecondDerivative`s where the sum is carried out lazily. +On a one-dimensional `grid`, `Δ` is equivalent to `second_derivative`. On a +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 - Δ = SecondDerivative(grid, inner_stencil, closure_stencils, 1) +function laplace(grid::EquidistantGrid{Dim}, inner_stencil, closure_stencils) where Dim + Δ = second_derivative(grid, inner_stencil, closure_stencils, 1) for d = 2:Dim - Δ += SecondDerivative(grid, inner_stencil, closure_stencils, d) + Δ += second_derivative(grid, inner_stencil, closure_stencils, d) end return Δ end -export Laplace +export laplace
--- a/src/SbpOperators/volumeops/quadratures/inverse_quadrature.jl Wed Jul 14 23:40:10 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ - -""" - InverseQuadrature(grid::EquidistantGrid, inv_inner_stencil, inv_closure_stencils) - -Creates the inverse `H⁻¹` of the quadrature operator as a `TensorMapping` - -The inverse quadrature approximates the integral operator on the grid using -`inv_inner_stencil` in the interior and a set of stencils `inv_closure_stencils` -for the points in the closure regions. - -On a one-dimensional `grid`, `H⁻¹` is a `VolumeOperator`. On a multi-dimensional -`grid`, `H` is the outer product of the 1-dimensional inverse quadrature operators in -each coordinate direction. Also see the documentation of -`SbpOperators.volume_operator(...)` for more details. -""" -function InverseQuadrature(grid::EquidistantGrid{Dim}, inv_inner_stencil, inv_closure_stencils) where Dim - h⁻¹ = inverse_spacing(grid) - H⁻¹ = SbpOperators.volume_operator(grid,scale(inv_inner_stencil,h⁻¹[1]),scale.(inv_closure_stencils,h⁻¹[1]),even,1) - for i ∈ 2:Dim - Hᵢ⁻¹ = SbpOperators.volume_operator(grid,scale(inv_inner_stencil,h⁻¹[i]),scale.(inv_closure_stencils,h⁻¹[i]),even,i) - H⁻¹ = H⁻¹∘Hᵢ⁻¹ - end - return H⁻¹ -end -export InverseQuadrature - -""" - InverseDiagonalQuadrature(grid::EquidistantGrid, closure_stencils) - -Creates the inverse of the diagonal quadrature operator defined by the inner stencil -1/h and a set of 1-element closure stencils in `closure_stencils`. Note that -the closure stencils are those of the quadrature operator (and not the inverse). -""" -function InverseDiagonalQuadrature(grid::EquidistantGrid, closure_stencils::NTuple{M,Stencil{T,1}}) where {T,M} - inv_inner_stencil = Stencil(one(T), center=1) - inv_closure_stencils = reciprocal_stencil.(closure_stencils) - return InverseQuadrature(grid, inv_inner_stencil, inv_closure_stencils) -end -export InverseDiagonalQuadrature - -reciprocal_stencil(s::Stencil{T}) where T = Stencil(s.range,one(T)./s.weights)
--- a/src/SbpOperators/volumeops/quadratures/quadrature.jl Wed Jul 14 23:40:10 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -""" - Quadrature(grid::EquidistantGrid, inner_stencil, closure_stencils) - -Creates the quadrature operator `H` as a `TensorMapping` - -The quadrature approximates the integral operator on the grid using -`inner_stencil` in the interior and a set of stencils `closure_stencils` -for the points in the closure regions. - -On a one-dimensional `grid`, `H` is a `VolumeOperator`. On a multi-dimensional -`grid`, `H` is the outer product of the 1-dimensional quadrature operators in -each coordinate direction. Also see the documentation of -`SbpOperators.volume_operator(...)` for more details. -""" -function Quadrature(grid::EquidistantGrid{Dim}, inner_stencil, closure_stencils) where Dim - h = spacing(grid) - H = SbpOperators.volume_operator(grid, scale(inner_stencil,h[1]), scale.(closure_stencils,h[1]), even, 1) - for i ∈ 2:Dim - Hᵢ = SbpOperators.volume_operator(grid, scale(inner_stencil,h[i]), scale.(closure_stencils,h[i]), even, i) - H = H∘Hᵢ - end - return H -end -export Quadrature - -""" - DiagonalQuadrature(grid::EquidistantGrid, closure_stencils) - -Creates the quadrature operator with the inner stencil 1/h and 1-element sized -closure stencils (i.e the operator is diagonal) -""" -function DiagonalQuadrature(grid::EquidistantGrid, closure_stencils::NTuple{M,Stencil{T,1}}) where {M,T} - inner_stencil = Stencil(one(T), center=1) - return Quadrature(grid, inner_stencil, closure_stencils) -end -export DiagonalQuadrature
--- a/src/Sbplib.jl Wed Jul 14 23:40:10 2021 +0200 +++ b/src/Sbplib.jl Thu Jul 15 00:06:16 2021 +0200 @@ -1,5 +1,6 @@ module Sbplib +include("StaticDicts/StaticDicts.jl") include("RegionIndices/RegionIndices.jl") include("LazyTensors/LazyTensors.jl") include("Grids/Grids.jl")
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/StaticDicts/StaticDicts.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,82 @@ +module StaticDicts + +export StaticDict + +""" + StaticDict{K,V,N} <: AbstractDict{K,V} + +A static dictionary implementing the interface for an `AbstractDict`. A +`StaticDict` is fully immutable and after creation no changes can be made. + +The immutable nature means that `StaticDict` can be compared with `===`, in +constrast to regular `Dict` or `ImmutableDict` which can not. (See +https://github.com/JuliaLang/julia/issues/4648 for details) One important +aspect of this is that `StaticDict` can be used in a struct while still +allowing the struct to be comared using the default implementation of `==` for +structs. + +Lookups are done by linear search. + +Duplicate keys are not allowed and an error will be thrown if they are passed +to the constructor. +""" +struct StaticDict{K,V,N} <: AbstractDict{K,V} + pairs::NTuple{N,Pair{K,V}} + + function StaticDict{K,V}(pairs::Vararg{Pair,N}) where {K,V,N} + if !allunique(first.(pairs)) + throw(DomainError(pairs, "keys must be unique")) + end + return new{K,V,N}(pairs) + end +end + +function StaticDict(pairs::Vararg{Pair}) + K = typejoin(firsttype.(pairs)...) + V = typejoin(secondtype.(pairs)...) + return StaticDict{K,V}(pairs...) +end + +StaticDict(pairs::NTuple{N,Pair} where N) = StaticDict(pairs...) + +function Base.get(d::StaticDict, key, default) + for p ∈ d.pairs + if key == p.first + return p.second + end + end + + return default +end + +Base.iterate(d::StaticDict) = iterate(d.pairs) +Base.iterate(d::StaticDict, state) = iterate(d.pairs,state) +Base.length(d::StaticDict) = length(d.pairs) + + +""" + merge(d1::StaticDict, d2::StaticDict) + +Merge two `StaticDict`. Repeating keys is considered and error. This may +change in a future version. +""" +function Base.merge(d1::StaticDict, d2::StaticDict) + return StaticDict(d1.pairs..., d2.pairs...) +end + + +""" + firsttype(::Pair{T1,T2}) + +The type of the first element in the pair. +""" +firsttype(::Pair{T1,T2}) where {T1,T2} = T1 + +""" + secondtype(::Pair{T1,T2}) + +The type of the secondtype element in the pair. +""" +secondtype(::Pair{T1,T2}) where {T1,T2} = T2 + +end # module
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/DiffOps/DiffOps_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,195 @@ +using Test +using Sbplib.DiffOps +using Sbplib.Grids +using Sbplib.SbpOperators +using Sbplib.RegionIndices +using Sbplib.LazyTensors + +# +# @testset "BoundaryValue" begin +# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) +# g = EquidistantGrid((4,5), (0.0, 0.0), (1.0,1.0)) +# +# e_w = BoundaryValue(op, g, CartesianBoundary{1,Lower}()) +# e_e = BoundaryValue(op, g, CartesianBoundary{1,Upper}()) +# e_s = BoundaryValue(op, g, CartesianBoundary{2,Lower}()) +# e_n = BoundaryValue(op, g, CartesianBoundary{2,Upper}()) +# +# v = zeros(Float64, 4, 5) +# v[:,5] = [1, 2, 3,4] +# v[:,4] = [1, 2, 3,4] +# v[:,3] = [4, 5, 6, 7] +# v[:,2] = [7, 8, 9, 10] +# v[:,1] = [10, 11, 12, 13] +# +# @test e_w isa TensorMapping{T,2,1} where T +# @test e_w' isa TensorMapping{T,1,2} where T +# +# @test domain_size(e_w, (3,2)) == (2,) +# @test domain_size(e_e, (3,2)) == (2,) +# @test domain_size(e_s, (3,2)) == (3,) +# @test domain_size(e_n, (3,2)) == (3,) +# +# @test size(e_w'*v) == (5,) +# @test size(e_e'*v) == (5,) +# @test size(e_s'*v) == (4,) +# @test size(e_n'*v) == (4,) +# +# @test collect(e_w'*v) == [10,7,4,1.0,1] +# @test collect(e_e'*v) == [13,10,7,4,4.0] +# @test collect(e_s'*v) == [10,11,12,13.0] +# @test collect(e_n'*v) == [1,2,3,4.0] +# +# g_x = [1,2,3,4.0] +# g_y = [5,4,3,2,1.0] +# +# G_w = zeros(Float64, (4,5)) +# G_w[1,:] = g_y +# +# G_e = zeros(Float64, (4,5)) +# G_e[4,:] = g_y +# +# G_s = zeros(Float64, (4,5)) +# G_s[:,1] = g_x +# +# G_n = zeros(Float64, (4,5)) +# G_n[:,5] = g_x +# +# @test size(e_w*g_y) == (UnknownDim,5) +# @test size(e_e*g_y) == (UnknownDim,5) +# @test size(e_s*g_x) == (4,UnknownDim) +# @test size(e_n*g_x) == (4,UnknownDim) +# +# # These tests should be moved to where they are possible (i.e we know what the grid should be) +# @test_broken collect(e_w*g_y) == G_w +# @test_broken collect(e_e*g_y) == G_e +# @test_broken collect(e_s*g_x) == G_s +# @test_broken collect(e_n*g_x) == G_n +# end +# +# @testset "NormalDerivative" begin +# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) +# g = EquidistantGrid((5,6), (0.0, 0.0), (4.0,5.0)) +# +# d_w = NormalDerivative(op, g, CartesianBoundary{1,Lower}()) +# d_e = NormalDerivative(op, g, CartesianBoundary{1,Upper}()) +# d_s = NormalDerivative(op, g, CartesianBoundary{2,Lower}()) +# d_n = NormalDerivative(op, g, CartesianBoundary{2,Upper}()) +# +# +# v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y) +# v∂x = evalOn(g, (x,y)-> 2*x + y) +# v∂y = evalOn(g, (x,y)-> 2*(y-1) + x) +# +# @test d_w isa TensorMapping{T,2,1} where T +# @test d_w' isa TensorMapping{T,1,2} where T +# +# @test domain_size(d_w, (3,2)) == (2,) +# @test domain_size(d_e, (3,2)) == (2,) +# @test domain_size(d_s, (3,2)) == (3,) +# @test domain_size(d_n, (3,2)) == (3,) +# +# @test size(d_w'*v) == (6,) +# @test size(d_e'*v) == (6,) +# @test size(d_s'*v) == (5,) +# @test size(d_n'*v) == (5,) +# +# @test collect(d_w'*v) ≈ v∂x[1,:] +# @test collect(d_e'*v) ≈ v∂x[5,:] +# @test collect(d_s'*v) ≈ v∂y[:,1] +# @test collect(d_n'*v) ≈ v∂y[:,6] +# +# +# d_x_l = zeros(Float64, 5) +# d_x_u = zeros(Float64, 5) +# for i ∈ eachindex(d_x_l) +# d_x_l[i] = op.dClosure[i-1] +# d_x_u[i] = -op.dClosure[length(d_x_u)-i] +# end +# +# d_y_l = zeros(Float64, 6) +# d_y_u = zeros(Float64, 6) +# for i ∈ eachindex(d_y_l) +# d_y_l[i] = op.dClosure[i-1] +# d_y_u[i] = -op.dClosure[length(d_y_u)-i] +# end +# +# function prod_matrix(x,y) +# G = zeros(Float64, length(x), length(y)) +# for I ∈ CartesianIndices(G) +# G[I] = x[I[1]]*y[I[2]] +# end +# +# return G +# end +# +# g_x = [1,2,3,4.0,5] +# g_y = [5,4,3,2,1.0,11] +# +# G_w = prod_matrix(d_x_l, g_y) +# G_e = prod_matrix(d_x_u, g_y) +# G_s = prod_matrix(g_x, d_y_l) +# G_n = prod_matrix(g_x, d_y_u) +# +# +# @test size(d_w*g_y) == (UnknownDim,6) +# @test size(d_e*g_y) == (UnknownDim,6) +# @test size(d_s*g_x) == (5,UnknownDim) +# @test size(d_n*g_x) == (5,UnknownDim) +# +# # These tests should be moved to where they are possible (i.e we know what the grid should be) +# @test_broken collect(d_w*g_y) ≈ G_w +# @test_broken collect(d_e*g_y) ≈ G_e +# @test_broken collect(d_s*g_x) ≈ G_s +# @test_broken collect(d_n*g_x) ≈ G_n +# end +# +# @testset "BoundaryQuadrature" begin +# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) +# g = EquidistantGrid((10,11), (0.0, 0.0), (1.0,1.0)) +# +# H_w = BoundaryQuadrature(op, g, CartesianBoundary{1,Lower}()) +# H_e = BoundaryQuadrature(op, g, CartesianBoundary{1,Upper}()) +# H_s = BoundaryQuadrature(op, g, CartesianBoundary{2,Lower}()) +# H_n = BoundaryQuadrature(op, g, CartesianBoundary{2,Upper}()) +# +# v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y) +# +# function get_quadrature(N) +# qc = op.quadratureClosure +# q = (qc..., ones(N-2*closuresize(op))..., reverse(qc)...) +# @assert length(q) == N +# return q +# end +# +# v_w = v[1,:] +# v_e = v[10,:] +# v_s = v[:,1] +# v_n = v[:,11] +# +# q_x = spacing(g)[1].*get_quadrature(10) +# q_y = spacing(g)[2].*get_quadrature(11) +# +# @test H_w isa TensorOperator{T,1} where T +# +# @test domain_size(H_w, (3,)) == (3,) +# @test domain_size(H_n, (3,)) == (3,) +# +# @test range_size(H_w, (3,)) == (3,) +# @test range_size(H_n, (3,)) == (3,) +# +# @test size(H_w*v_w) == (11,) +# @test size(H_e*v_e) == (11,) +# @test size(H_s*v_s) == (10,) +# @test size(H_n*v_n) == (10,) +# +# @test collect(H_w*v_w) ≈ q_y.*v_w +# @test collect(H_e*v_e) ≈ q_y.*v_e +# @test collect(H_s*v_s) ≈ q_x.*v_s +# @test collect(H_n*v_n) ≈ q_x.*v_n +# +# @test collect(H_w'*v_w) == collect(H_w'*v_w) +# @test collect(H_e'*v_e) == collect(H_e'*v_e) +# @test collect(H_s'*v_s) == collect(H_s'*v_s) +# @test collect(H_n'*v_n) == collect(H_n'*v_n) +# end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/Grids/EquidistantGrid_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,101 @@ +using Sbplib.Grids +using Test +using Sbplib.RegionIndices + + +@testset "EquidistantGrid" begin + @test EquidistantGrid(4,0.0,1.0) isa EquidistantGrid + @test EquidistantGrid(4,0.0,8.0) isa EquidistantGrid + # constuctor + @test_throws DomainError EquidistantGrid(0,0.0,1.0) + @test_throws DomainError EquidistantGrid(1,1.0,1.0) + @test_throws DomainError EquidistantGrid(1,1.0,-1.0) + @test EquidistantGrid(4,0.0,1.0) == EquidistantGrid((4,),(0.0,),(1.0,)) + + @testset "Base" begin + @test eltype(EquidistantGrid(4,0.0,1.0)) == Float64 + @test eltype(EquidistantGrid((4,3),(0,0),(1,3))) == Int + @test size(EquidistantGrid(4,0.0,1.0)) == (4,) + @test size(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3) + end + + # dimension + @test dimension(EquidistantGrid(4,0.0,1.0)) == 1 + @test dimension(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == 2 + + # spacing + @test [spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(1. /3,)...] atol=5e-13 + @test [spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(0.5, 1.)...] atol=5e-13 + + # inverse_spacing + @test [inverse_spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(3.,)...] atol=5e-13 + @test [inverse_spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(2, 1.)...] atol=5e-13 + + # points + g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11)) + gp = points(g); + p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11); + (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11); + (-0.5,0.) (-0.5,7.11/2) (-0.5,7.11); + (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11); + (0.,0.) (0.,7.11/2) (0.,7.11)] + for i ∈ eachindex(gp) + @test [gp[i]...] ≈ [p[i]...] atol=5e-13 + end + + # restrict + g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0)) + @test restrict(g, 1) == EquidistantGrid(5,0.0,2.0) + @test restrict(g, 2) == EquidistantGrid(3,0.0,1.0) + + g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) + @test restrict(g, 1) == EquidistantGrid(2,0.0,2.0) + @test restrict(g, 2) == EquidistantGrid(5,0.0,1.0) + @test restrict(g, 3) == EquidistantGrid(3,0.0,3.0) + @test restrict(g, 1:2) == EquidistantGrid((2,5),(0.0,0.0),(2.0,1.0)) + @test restrict(g, 2:3) == EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0)) + @test restrict(g, [1,3]) == EquidistantGrid((2,3),(0.0,0.0),(2.0,3.0)) + @test restrict(g, [2,1]) == EquidistantGrid((5,2),(0.0,0.0),(1.0,2.0)) + + @testset "boundary_identifiers" begin + g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) + bids = (CartesianBoundary{1,Lower}(),CartesianBoundary{1,Upper}(), + CartesianBoundary{2,Lower}(),CartesianBoundary{2,Upper}(), + CartesianBoundary{3,Lower}(),CartesianBoundary{3,Upper}()) + @test boundary_identifiers(g) == bids + @inferred boundary_identifiers(g) + end + + @testset "boundary_grid" begin + @testset "1D" begin + g = EquidistantGrid(5,0.0,2.0) + (id_l, id_r) = boundary_identifiers(g) + @test boundary_grid(g,id_l) == EquidistantGrid{Float64}() + @test boundary_grid(g,id_r) == EquidistantGrid{Float64}() + @test_throws DomainError boundary_grid(g,CartesianBoundary{2,Lower}()) + @test_throws DomainError boundary_grid(g,CartesianBoundary{0,Lower}()) + end + @testset "2D" begin + g = EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0)) + (id_w, id_e, id_s, id_n) = boundary_identifiers(g) + @test boundary_grid(g,id_w) == restrict(g,2) + @test boundary_grid(g,id_e) == restrict(g,2) + @test boundary_grid(g,id_s) == restrict(g,1) + @test boundary_grid(g,id_n) == restrict(g,1) + @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}()) + end + @testset "3D" begin + g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) + (id_w, id_e, + id_s, id_n, + id_t, id_b) = boundary_identifiers(g) + @test boundary_grid(g,id_w) == restrict(g,[2,3]) + @test boundary_grid(g,id_e) == restrict(g,[2,3]) + @test boundary_grid(g,id_s) == restrict(g,[1,3]) + @test boundary_grid(g,id_n) == restrict(g,[1,3]) + @test boundary_grid(g,id_t) == restrict(g,[1,2]) + @test boundary_grid(g,id_b) == restrict(g,[1,2]) + @test_throws DomainError boundary_grid(g,CartesianBoundary{4,Lower}()) + end + end +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/LazyTensors/lazy_array_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,102 @@ +using Test +using Sbplib.LazyTensors +using Sbplib.RegionIndices + + +@testset "LazyArray" begin + @testset "LazyConstantArray" begin + @test LazyTensors.LazyConstantArray(3,(3,2)) isa LazyArray{Int,2} + + lca = LazyTensors.LazyConstantArray(3.0,(3,2)) + @test eltype(lca) == Float64 + @test ndims(lca) == 2 + @test size(lca) == (3,2) + @test lca[2] == 3.0 + end + struct DummyArray{T,D, T1<:AbstractArray{T,D}} <: LazyArray{T,D} + data::T1 + end + Base.size(v::DummyArray) = size(v.data) + Base.getindex(v::DummyArray{T,D}, I::Vararg{Int,D}) where {T,D} = v.data[I...] + + # Test lazy operations + v1 = [1, 2.3, 4] + v2 = [1., 2, 3] + s = 3.4 + r_add_v = v1 .+ v2 + r_sub_v = v1 .- v2 + r_times_v = v1 .* v2 + r_div_v = v1 ./ v2 + r_add_s = v1 .+ s + r_sub_s = v1 .- s + r_times_s = v1 .* s + r_div_s = v1 ./ s + @test isa(v1 +̃ v2, LazyArray) + @test isa(v1 -̃ v2, LazyArray) + @test isa(v1 *̃ v2, LazyArray) + @test isa(v1 /̃ v2, LazyArray) + @test isa(v1 +̃ s, LazyArray) + @test isa(v1 -̃ s, LazyArray) + @test isa(v1 *̃ s, LazyArray) + @test isa(v1 /̃ s, LazyArray) + @test isa(s +̃ v1, LazyArray) + @test isa(s -̃ v1, LazyArray) + @test isa(s *̃ v1, LazyArray) + @test isa(s /̃ v1, LazyArray) + for i ∈ eachindex(v1) + @test (v1 +̃ v2)[i] == r_add_v[i] + @test (v1 -̃ v2)[i] == r_sub_v[i] + @test (v1 *̃ v2)[i] == r_times_v[i] + @test (v1 /̃ v2)[i] == r_div_v[i] + @test (v1 +̃ s)[i] == r_add_s[i] + @test (v1 -̃ s)[i] == r_sub_s[i] + @test (v1 *̃ s)[i] == r_times_s[i] + @test (v1 /̃ s)[i] == r_div_s[i] + @test (s +̃ v1)[i] == r_add_s[i] + @test (s -̃ v1)[i] == -r_sub_s[i] + @test (s *̃ v1)[i] == r_times_s[i] + @test (s /̃ v1)[i] == 1/r_div_s[i] + end + @test_throws BoundsError (v1 +̃ v2)[4] + v2 = [1., 2, 3, 4] + # Test that size of arrays is asserted when not specified inbounds + # TODO: Replace these errors with SizeMismatch + @test_throws DimensionMismatch v1 +̃ v2 + + # Test operations on LazyArray + v1 = DummyArray([1, 2.3, 4]) + v2 = [1., 2, 3] + @test isa(v1 + v2, LazyArray) + @test isa(v2 + v1, LazyArray) + @test isa(v1 - v2, LazyArray) + @test isa(v2 - v1, LazyArray) + for i ∈ eachindex(v2) + @test (v1 + v2)[i] == (v2 + v1)[i] == r_add_v[i] + @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub_v[i] + end + @test_throws BoundsError (v1 + v2)[4] + v2 = [1., 2, 3, 4] + # Test that size of arrays is asserted when not specified inbounds + # TODO: Replace these errors with SizeMismatch + @test_throws DimensionMismatch v1 + v2 +end + + +@testset "LazyFunctionArray" begin + @test LazyFunctionArray(i->i^2, (3,)) == [1,4,9] + @test LazyFunctionArray((i,j)->i*j, (3,2)) == [ + 1 2; + 2 4; + 3 6; + ] + + @test size(LazyFunctionArray(i->i^2, (3,))) == (3,) + @test size(LazyFunctionArray((i,j)->i*j, (3,2))) == (3,2) + + @inferred LazyFunctionArray(i->i^2, (3,))[2] + + @test_throws BoundsError LazyFunctionArray(i->i^2, (3,))[4] + @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[4,2] + @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[2,3] + +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/LazyTensors/lazy_tensor_operations_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,469 @@ +using Test +using Sbplib.LazyTensors +using Sbplib.RegionIndices + +using Tullio + +@testset "Mapping transpose" begin + struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end + + LazyTensors.apply(m::DummyMapping{T,R}, v, I::Vararg{Any,R}) where {T,R} = :apply + LazyTensors.apply_transpose(m::DummyMapping{T,R,D}, v, I::Vararg{Any,D}) where {T,R,D} = :apply_transpose + + LazyTensors.range_size(m::DummyMapping) = :range_size + LazyTensors.domain_size(m::DummyMapping) = :domain_size + + m = DummyMapping{Float64,2,3}() + @test m' isa TensorMapping{Float64, 3,2} + @test m'' == m + @test apply(m',zeros(Float64,(0,0)), 0, 0, 0) == :apply_transpose + @test apply(m'',zeros(Float64,(0,0,0)), 0, 0) == :apply + @test apply_transpose(m', zeros(Float64,(0,0,0)), 0, 0) == :apply + + @test range_size(m') == :domain_size + @test domain_size(m') == :range_size +end + +@testset "TensorApplication" begin + struct SizeDoublingMapping{T,R,D} <: TensorMapping{T,R,D} + domain_size::NTuple{D,Int} + end + + LazyTensors.apply(m::SizeDoublingMapping{T,R}, v, i::Vararg{Any,R}) where {T,R} = (:apply,v,i) + LazyTensors.range_size(m::SizeDoublingMapping) = 2 .* m.domain_size + LazyTensors.domain_size(m::SizeDoublingMapping) = m.domain_size + + + m = SizeDoublingMapping{Int, 1, 1}((3,)) + v = [0,1,2] + @test m*v isa AbstractVector{Int} + @test size(m*v) == 2 .*size(v) + @test (m*v)[0] == (:apply,v,(0,)) + @test m*m*v isa AbstractVector{Int} + @test (m*m*v)[1] == (:apply,m*v,(1,)) + @test (m*m*v)[3] == (:apply,m*v,(3,)) + @test (m*m*v)[6] == (:apply,m*v,(6,)) + @test_broken BoundsError == (m*m*v)[0] + @test_broken BoundsError == (m*m*v)[7] + @test_throws MethodError m*m + + m = SizeDoublingMapping{Int, 2, 1}((3,)) + @test_throws MethodError m*ones(Int,2,2) + @test_throws MethodError m*m*v + + m = SizeDoublingMapping{Float64, 2, 2}((3,3)) + v = ones(3,3) + @test size(m*v) == 2 .*size(v) + @test (m*v)[1,2] == (:apply,v,(1,2)) + + struct ScalingOperator{T,D} <: TensorMapping{T,D,D} + λ::T + size::NTuple{D,Int} + end + + LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Any,D}) where {T,D} = m.λ*v[I...] + LazyTensors.range_size(m::ScalingOperator) = m.size + LazyTensors.domain_size(m::ScalingOperator) = m.size + + m = ScalingOperator{Int,1}(2,(3,)) + v = [1,2,3] + @test m*v isa AbstractVector + @test m*v == [2,4,6] + + m = ScalingOperator{Int,2}(2,(2,2)) + v = [[1 2];[3 4]] + @test m*v == [[2 4];[6 8]] + @test (m*v)[2,1] == 6 +end + +@testset "TensorMapping binary operations" begin + struct ScalarMapping{T,R,D} <: TensorMapping{T,R,D} + λ::T + range_size::NTuple{R,Int} + domain_size::NTuple{D,Int} + end + + LazyTensors.apply(m::ScalarMapping{T,R}, v, I::Vararg{Any,R}) where {T,R} = m.λ*v[I...] + LazyTensors.range_size(m::ScalarMapping) = m.domain_size + LazyTensors.domain_size(m::ScalarMapping) = m.range_size + + A = ScalarMapping{Float64,1,1}(2.0, (3,), (3,)) + B = ScalarMapping{Float64,1,1}(3.0, (3,), (3,)) + + v = [1.1,1.2,1.3] + for i ∈ eachindex(v) + @test ((A+B)*v)[i] == 2*v[i] + 3*v[i] + end + + for i ∈ eachindex(v) + @test ((A-B)*v)[i] == 2*v[i] - 3*v[i] + end + + @test range_size(A+B) == range_size(A) == range_size(B) + @test domain_size(A+B) == domain_size(A) == domain_size(B) +end + + +@testset "TensorMappingComposition" begin + A = rand(2,3) + B = rand(3,4) + + à = LazyLinearMap(A, (1,), (2,)) + B̃ = LazyLinearMap(B, (1,), (2,)) + + @test Ã∘B̃ isa TensorMappingComposition + @test range_size(Ã∘B̃) == (2,) + @test domain_size(Ã∘B̃) == (4,) + @test_throws SizeMismatch B̃∘à + + # @test @inbounds B̃∘à # Should not error even though dimensions don't match. (Since ]test runs with forced boundschecking this is currently not testable 2020-10-16) + + v = rand(4) + @test Ã∘B̃*v ≈ A*B*v rtol=1e-14 + + v = rand(2) + @test (Ã∘B̃)'*v ≈ B'*A'*v rtol=1e-14 +end + +@testset "LazyLinearMap" begin + # Test a standard matrix-vector product + # mapping vectors of size 4 to vectors of size 3. + A = rand(3,4) + à = LazyLinearMap(A, (1,), (2,)) + v = rand(4) + w = rand(3) + + @test à isa LazyLinearMap{T,1,1} where T + @test à isa TensorMapping{T,1,1} where T + @test range_size(Ã) == (3,) + @test domain_size(Ã) == (4,) + + @test Ã*ones(4) ≈ A*ones(4) atol=5e-13 + @test Ã*v ≈ A*v atol=5e-13 + @test Ã'*w ≈ A'*w + + A = rand(2,3,4) + @test_throws DomainError LazyLinearMap(A, (3,1), (2,)) + + # Test more exotic mappings + B = rand(3,4,2) + # Map vectors of size 2 to matrices of size (3,4) + B̃ = LazyLinearMap(B, (1,2), (3,)) + v = rand(2) + + @test range_size(B̃) == (3,4) + @test domain_size(B̃) == (2,) + @test B̃ isa TensorMapping{T,2,1} where T + @test B̃*ones(2) ≈ B[:,:,1] + B[:,:,2] atol=5e-13 + @test B̃*v ≈ B[:,:,1]*v[1] + B[:,:,2]*v[2] atol=5e-13 + + # Map matrices of size (3,2) to vectors of size 4 + B̃ = LazyLinearMap(B, (2,), (1,3)) + v = rand(3,2) + + @test range_size(B̃) == (4,) + @test domain_size(B̃) == (3,2) + @test B̃ isa TensorMapping{T,1,2} where T + @test B̃*ones(3,2) ≈ B[1,:,1] + B[2,:,1] + B[3,:,1] + + B[1,:,2] + B[2,:,2] + B[3,:,2] atol=5e-13 + @test B̃*v ≈ B[1,:,1]*v[1,1] + B[2,:,1]*v[2,1] + B[3,:,1]*v[3,1] + + B[1,:,2]v[1,2] + B[2,:,2]*v[2,2] + B[3,:,2]*v[3,2] atol=5e-13 + + + # TODO: + # @inferred (B̃*v)[2] +end + + +@testset "IdentityMapping" begin + @test IdentityMapping{Float64}((4,5)) isa IdentityMapping{T,2} where T + @test IdentityMapping{Float64}((4,5)) isa TensorMapping{T,2,2} where T + @test IdentityMapping{Float64}((4,5)) == IdentityMapping{Float64}(4,5) + + @test IdentityMapping(3,2) isa IdentityMapping{Float64,2} + + for sz ∈ [(4,5),(3,),(5,6,4)] + I = IdentityMapping{Float64}(sz) + v = rand(sz...) + @test I*v == v + @test I'*v == v + + @test range_size(I) == sz + @test domain_size(I) == sz + end + + I = IdentityMapping{Float64}((4,5)) + v = rand(4,5) + @inferred (I*v)[3,2] + @inferred (I'*v)[3,2] + @inferred range_size(I) + + @inferred range_dim(I) + @inferred domain_dim(I) + + à = rand(4,2) + A = LazyLinearMap(Ã,(1,),(2,)) + I1 = IdentityMapping{Float64}(2) + I2 = IdentityMapping{Float64}(4) + @test A∘I1 == A + @test I2∘A == A + @test I1∘I1 == I1 + @test_throws SizeMismatch I1∘A + @test_throws SizeMismatch A∘I2 + @test_throws SizeMismatch I1∘I2 +end + +@testset "InflatedTensorMapping" begin + I(sz...) = IdentityMapping(sz...) + + à = rand(4,2) + B̃ = rand(4,2,3) + C̃ = rand(4,2,3) + + A = LazyLinearMap(Ã,(1,),(2,)) + B = LazyLinearMap(B̃,(1,2),(3,)) + C = LazyLinearMap(C̃,(1,),(2,3)) + + @testset "Constructors" begin + @test InflatedTensorMapping(I(3,2), A, I(4)) isa TensorMapping{Float64, 4, 4} + @test InflatedTensorMapping(I(3,2), B, I(4)) isa TensorMapping{Float64, 5, 4} + @test InflatedTensorMapping(I(3), C, I(2,3)) isa TensorMapping{Float64, 4, 5} + @test InflatedTensorMapping(C, I(2,3)) isa TensorMapping{Float64, 3, 4} + @test InflatedTensorMapping(I(3), C) isa TensorMapping{Float64, 2, 3} + @test InflatedTensorMapping(I(3), I(2,3)) isa TensorMapping{Float64, 3, 3} + end + + @testset "Range and domain size" begin + @test range_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,4,4) + @test domain_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,2,4) + + @test range_size(InflatedTensorMapping(I(3,2), B, I(4))) == (3,2,4,2,4) + @test domain_size(InflatedTensorMapping(I(3,2), B, I(4))) == (3,2,3,4) + + @test range_size(InflatedTensorMapping(I(3), C, I(2,3))) == (3,4,2,3) + @test domain_size(InflatedTensorMapping(I(3), C, I(2,3))) == (3,2,3,2,3) + + @inferred range_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,4,4) + @inferred domain_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,2,4) + end + + @testset "Application" begin + # Testing regular application and transposed application with inflation "before", "after" and "before and after". + # The inflated tensor mappings are chosen to preserve, reduce and increase the dimension of the result compared to the input. + tests = [ + ( + InflatedTensorMapping(I(3,2), A, I(4)), + (v-> @tullio res[a,b,c,d] := Ã[c,i]*v[a,b,i,d]), # Expected result of apply + (v-> @tullio res[a,b,c,d] := Ã[i,c]*v[a,b,i,d]), # Expected result of apply_transpose + ), + ( + InflatedTensorMapping(I(3,2), B, I(4)), + (v-> @tullio res[a,b,c,d,e] := B̃[c,d,i]*v[a,b,i,e]), + (v-> @tullio res[a,b,c,d] := B̃[i,j,c]*v[a,b,i,j,d]), + ), + ( + InflatedTensorMapping(I(3,2), C, I(4)), + (v-> @tullio res[a,b,c,d] := C̃[c,i,j]*v[a,b,i,j,d]), + (v-> @tullio res[a,b,c,d,e] := C̃[i,c,d]*v[a,b,i,e]), + ), + ( + InflatedTensorMapping(I(3,2), A), + (v-> @tullio res[a,b,c] := Ã[c,i]*v[a,b,i]), + (v-> @tullio res[a,b,c] := Ã[i,c]*v[a,b,i]), + ), + ( + InflatedTensorMapping(I(3,2), B), + (v-> @tullio res[a,b,c,d] := B̃[c,d,i]*v[a,b,i]), + (v-> @tullio res[a,b,c] := B̃[i,j,c]*v[a,b,i,j]), + ), + ( + InflatedTensorMapping(I(3,2), C), + (v-> @tullio res[a,b,c] := C̃[c,i,j]*v[a,b,i,j]), + (v-> @tullio res[a,b,c,d] := C̃[i,c,d]*v[a,b,i]), + ), + ( + InflatedTensorMapping(A,I(4)), + (v-> @tullio res[a,b] := Ã[a,i]*v[i,b]), + (v-> @tullio res[a,b] := Ã[i,a]*v[i,b]), + ), + ( + InflatedTensorMapping(B,I(4)), + (v-> @tullio res[a,b,c] := B̃[a,b,i]*v[i,c]), + (v-> @tullio res[a,b] := B̃[i,j,a]*v[i,j,b]), + ), + ( + InflatedTensorMapping(C,I(4)), + (v-> @tullio res[a,b] := C̃[a,i,j]*v[i,j,b]), + (v-> @tullio res[a,b,c] := C̃[i,a,b]*v[i,c]), + ), + ] + + @testset "apply" begin + for i ∈ 1:length(tests) + tm = tests[i][1] + v = rand(domain_size(tm)...) + true_value = tests[i][2](v) + @test tm*v ≈ true_value rtol=1e-14 + end + end + + @testset "apply_transpose" begin + for i ∈ 1:length(tests) + tm = tests[i][1] + v = rand(range_size(tm)...) + true_value = tests[i][3](v) + @test tm'*v ≈ true_value rtol=1e-14 + end + end + + @testset "Inference of application" begin + struct ScalingOperator{T,D} <: TensorMapping{T,D,D} + λ::T + size::NTuple{D,Int} + end + + LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Any,D}) where {T,D} = m.λ*v[I...] + LazyTensors.range_size(m::ScalingOperator) = m.size + LazyTensors.domain_size(m::ScalingOperator) = m.size + + tm = InflatedTensorMapping(I(2,3),ScalingOperator(2.0, (3,2)),I(3,4)) + v = rand(domain_size(tm)...) + + @inferred apply(tm,v,1,2,3,2,2,4) + @inferred (tm*v)[1,2,3,2,2,4] + end + end + + @testset "InflatedTensorMapping of InflatedTensorMapping" begin + A = ScalingOperator(2.0,(2,3)) + itm = InflatedTensorMapping(I(3,2), A, I(4)) + @test InflatedTensorMapping(I(4), itm, I(2)) == InflatedTensorMapping(I(4,3,2), A, I(4,2)) + @test InflatedTensorMapping(itm, I(2)) == InflatedTensorMapping(I(3,2), A, I(4,2)) + @test InflatedTensorMapping(I(4), itm) == InflatedTensorMapping(I(4,3,2), A, I(4)) + + @test InflatedTensorMapping(I(2), I(2), I(2)) isa InflatedTensorMapping # The constructor should always return its type. + end +end + +@testset "split_index" begin + @test LazyTensors.split_index(Val(2),Val(1),Val(2),Val(2),1,2,3,4,5,6) == ((1,2,:,5,6),(3,4)) + @test LazyTensors.split_index(Val(2),Val(3),Val(2),Val(2),1,2,3,4,5,6) == ((1,2,:,:,:,5,6),(3,4)) + @test LazyTensors.split_index(Val(3),Val(1),Val(1),Val(2),1,2,3,4,5,6) == ((1,2,3,:,5,6),(4,)) + @test LazyTensors.split_index(Val(3),Val(2),Val(1),Val(2),1,2,3,4,5,6) == ((1,2,3,:,:,5,6),(4,)) + @test LazyTensors.split_index(Val(1),Val(1),Val(2),Val(3),1,2,3,4,5,6) == ((1,:,4,5,6),(2,3)) + @test LazyTensors.split_index(Val(1),Val(2),Val(2),Val(3),1,2,3,4,5,6) == ((1,:,:,4,5,6),(2,3)) + + @test LazyTensors.split_index(Val(0),Val(1),Val(3),Val(3),1,2,3,4,5,6) == ((:,4,5,6),(1,2,3)) + @test LazyTensors.split_index(Val(3),Val(1),Val(3),Val(0),1,2,3,4,5,6) == ((1,2,3,:),(4,5,6)) + + @inferred LazyTensors.split_index(Val(2),Val(3),Val(2),Val(2),1,2,3,2,2,4) +end + +@testset "slice_tuple" begin + @test LazyTensors.slice_tuple((1,2,3),Val(1), Val(3)) == (1,2,3) + @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(2), Val(5)) == (2,3,4,5) + @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(1), Val(3)) == (1,2,3) + @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(4), Val(6)) == (4,5,6) +end + +@testset "split_tuple" begin + @testset "2 parts" begin + @test LazyTensors.split_tuple((),Val(0)) == ((),()) + @test LazyTensors.split_tuple((1,),Val(0)) == ((),(1,)) + @test LazyTensors.split_tuple((1,),Val(1)) == ((1,),()) + + @test LazyTensors.split_tuple((1,2,3,4),Val(0)) == ((),(1,2,3,4)) + @test LazyTensors.split_tuple((1,2,3,4),Val(1)) == ((1,),(2,3,4)) + @test LazyTensors.split_tuple((1,2,3,4),Val(2)) == ((1,2),(3,4)) + @test LazyTensors.split_tuple((1,2,3,4),Val(3)) == ((1,2,3),(4,)) + @test LazyTensors.split_tuple((1,2,3,4),Val(4)) == ((1,2,3,4),()) + + @test LazyTensors.split_tuple((1,2,true,4),Val(3)) == ((1,2,true),(4,)) + + @inferred LazyTensors.split_tuple((1,2,3,4),Val(3)) + @inferred LazyTensors.split_tuple((1,2,true,4),Val(3)) + end + + @testset "3 parts" begin + @test LazyTensors.split_tuple((),Val(0),Val(0)) == ((),(),()) + @test LazyTensors.split_tuple((1,2,3),Val(1), Val(1)) == ((1,),(2,),(3,)) + @test LazyTensors.split_tuple((1,true,3),Val(1), Val(1)) == ((1,),(true,),(3,)) + + @test LazyTensors.split_tuple((1,2,3,4,5,6),Val(1),Val(2)) == ((1,),(2,3),(4,5,6)) + @test LazyTensors.split_tuple((1,2,3,4,5,6),Val(3),Val(2)) == ((1,2,3),(4,5),(6,)) + + @inferred LazyTensors.split_tuple((1,2,3,4,5,6),Val(3),Val(2)) + @inferred LazyTensors.split_tuple((1,true,3),Val(1), Val(1)) + end +end + +@testset "flatten_tuple" begin + @test LazyTensors.flatten_tuple((1,)) == (1,) + @test LazyTensors.flatten_tuple((1,2,3,4,5,6)) == (1,2,3,4,5,6) + @test LazyTensors.flatten_tuple((1,2,(3,4),5,6)) == (1,2,3,4,5,6) + @test LazyTensors.flatten_tuple((1,2,(3,(4,5)),6)) == (1,2,3,4,5,6) + @test LazyTensors.flatten_tuple(((1,2),(3,4),(5,),6)) == (1,2,3,4,5,6) +end + + +@testset "LazyOuterProduct" begin + struct ScalingOperator{T,D} <: TensorMapping{T,D,D} + λ::T + size::NTuple{D,Int} + end + + LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Any,D}) where {T,D} = m.λ*v[I...] + LazyTensors.range_size(m::ScalingOperator) = m.size + LazyTensors.domain_size(m::ScalingOperator) = m.size + + A = ScalingOperator(2.0, (5,)) + B = ScalingOperator(3.0, (3,)) + C = ScalingOperator(5.0, (3,2)) + + AB = LazyOuterProduct(A,B) + @test AB isa TensorMapping{T,2,2} where T + @test range_size(AB) == (5,3) + @test domain_size(AB) == (5,3) + + v = rand(range_size(AB)...) + @test AB*v == 6*v + + ABC = LazyOuterProduct(A,B,C) + + @test ABC isa TensorMapping{T,4,4} where T + @test range_size(ABC) == (5,3,3,2) + @test domain_size(ABC) == (5,3,3,2) + + @test A⊗B == AB + @test A⊗B⊗C == ABC + + A = rand(3,2) + B = rand(2,4,3) + + v₁ = rand(2,4,3) + v₂ = rand(4,3,2) + + à = LazyLinearMap(A,(1,),(2,)) + B̃ = LazyLinearMap(B,(1,),(2,3)) + + ÃB̃ = LazyOuterProduct(Ã,B̃) + @tullio ABv[i,k] := A[i,j]*B[k,l,m]*v₁[j,l,m] + @test ÃB̃*v₁ ≈ ABv + + B̃à = LazyOuterProduct(B̃,Ã) + @tullio BAv[k,i] := A[i,j]*B[k,l,m]*v₂[l,m,j] + @test B̃Ã*v₂ ≈ BAv + + @testset "Indentity mapping arguments" begin + @test LazyOuterProduct(IdentityMapping(3,2), IdentityMapping(1,2)) == IdentityMapping(3,2,1,2) + + à = LazyLinearMap(A,(1,),(2,)) + @test LazyOuterProduct(IdentityMapping(3,2), Ã) == InflatedTensorMapping(IdentityMapping(3,2),Ã) + @test LazyOuterProduct(Ã, IdentityMapping(3,2)) == InflatedTensorMapping(Ã,IdentityMapping(3,2)) + + I1 = IdentityMapping(3,2) + I2 = IdentityMapping(4) + @test I1⊗Ã⊗I2 == InflatedTensorMapping(I1, Ã, I2) + end + +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/LazyTensors/tensor_mapping_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,12 @@ +using Test +using Sbplib.LazyTensors + +@testset "Generic Mapping methods" begin + struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end + LazyTensors.apply(m::DummyMapping{T,R,D}, v, I::Vararg{Any,R}) where {T,R,D} = :apply + @test range_dim(DummyMapping{Int,2,3}()) == 2 + @test domain_dim(DummyMapping{Int,2,3}()) == 3 + @test apply(DummyMapping{Int,2,3}(), zeros(Int, (0,0,0)),0,0) == :apply + @test eltype(DummyMapping{Int,2,3}()) == Int + @test eltype(DummyMapping{Float64,2,3}()) == Float64 +end
--- a/test/Manifest.toml Wed Jul 14 23:40:10 2021 +0200 +++ b/test/Manifest.toml Thu Jul 15 00:06:16 2021 +0200 @@ -34,6 +34,11 @@ deps = ["Random", "Serialization", "Sockets"] uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" +[[Glob]] +git-tree-sha1 = "4df9f7e06108728ebf00a0a11edee4b29a482bb2" +uuid = "c27321d9-0574-5035-807b-f59d2c89b15c" +version = "1.3.0" + [[InteractiveUtils]] deps = ["Markdown"] uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
--- a/test/Project.toml Wed Jul 14 23:40:10 2021 +0200 +++ b/test/Project.toml Thu Jul 15 00:06:16 2021 +0200 @@ -1,4 +1,5 @@ [deps] +Glob = "c27321d9-0574-5035-807b-f59d2c89b15c" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/RegionIndices/RegionIndices_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,3 @@ +using Sbplib.RegionIndices +using Test +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SbpOperators/boundaryops/boundary_operator_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,152 @@ +using Test + +using Sbplib.LazyTensors +using Sbplib.SbpOperators +using Sbplib.Grids +using Sbplib.RegionIndices +import Sbplib.SbpOperators.Stencil +import Sbplib.SbpOperators.BoundaryOperator +import Sbplib.SbpOperators.boundary_operator + +@testset "BoundaryOperator" begin + closure_stencil = Stencil((0,2), (2.,1.,3.)) + g_1D = EquidistantGrid(11, 0.0, 1.0) + g_2D = EquidistantGrid((11,15), (0.0, 0.0), (1.0,1.0)) + + @testset "Constructors" begin + @testset "1D" begin + op_l = BoundaryOperator{Lower}(closure_stencil,size(g_1D)[1]) + @test op_l == BoundaryOperator(g_1D,closure_stencil,Lower()) + @test op_l == boundary_operator(g_1D,closure_stencil,CartesianBoundary{1,Lower}()) + @test op_l isa TensorMapping{T,0,1} where T + + op_r = BoundaryOperator{Upper}(closure_stencil,size(g_1D)[1]) + @test op_r == BoundaryOperator(g_1D,closure_stencil,Upper()) + @test op_r == boundary_operator(g_1D,closure_stencil,CartesianBoundary{1,Upper}()) + @test op_r isa TensorMapping{T,0,1} where T + end + + @testset "2D" begin + e_w = boundary_operator(g_2D,closure_stencil,CartesianBoundary{1,Upper}()) + @test e_w isa InflatedTensorMapping + @test e_w isa TensorMapping{T,1,2} where T + end + end + + op_l = boundary_operator(g_1D, closure_stencil, CartesianBoundary{1,Lower}()) + op_r = boundary_operator(g_1D, closure_stencil, CartesianBoundary{1,Upper}()) + + op_w = boundary_operator(g_2D, closure_stencil, CartesianBoundary{1,Lower}()) + op_e = boundary_operator(g_2D, closure_stencil, CartesianBoundary{1,Upper}()) + op_s = boundary_operator(g_2D, closure_stencil, CartesianBoundary{2,Lower}()) + op_n = boundary_operator(g_2D, closure_stencil, CartesianBoundary{2,Upper}()) + + @testset "Sizes" begin + @testset "1D" begin + @test domain_size(op_l) == (11,) + @test domain_size(op_r) == (11,) + + @test range_size(op_l) == () + @test range_size(op_r) == () + end + + @testset "2D" begin + @test domain_size(op_w) == (11,15) + @test domain_size(op_e) == (11,15) + @test domain_size(op_s) == (11,15) + @test domain_size(op_n) == (11,15) + + @test range_size(op_w) == (15,) + @test range_size(op_e) == (15,) + @test range_size(op_s) == (11,) + @test range_size(op_n) == (11,) + end + end + + @testset "Application" begin + @testset "1D" begin + v = evalOn(g_1D,x->1+x^2) + u = fill(3.124) + @test (op_l*v)[] == 2*v[1] + v[2] + 3*v[3] + @test (op_r*v)[] == 2*v[end] + v[end-1] + 3*v[end-2] + @test (op_r*v)[1] == 2*v[end] + v[end-1] + 3*v[end-2] + @test op_l'*u == [2*u[]; u[]; 3*u[]; zeros(8)] + @test op_r'*u == [zeros(8); 3*u[]; u[]; 2*u[]] + end + + @testset "2D" begin + v = rand(size(g_2D)...) + u = fill(3.124) + @test op_w*v ≈ 2*v[1,:] + v[2,:] + 3*v[3,:] rtol = 1e-14 + @test op_e*v ≈ 2*v[end,:] + v[end-1,:] + 3*v[end-2,:] rtol = 1e-14 + @test op_s*v ≈ 2*v[:,1] + v[:,2] + 3*v[:,3] rtol = 1e-14 + @test op_n*v ≈ 2*v[:,end] + v[:,end-1] + 3*v[:,end-2] rtol = 1e-14 + + + g_x = rand(size(g_2D)[1]) + g_y = rand(size(g_2D)[2]) + + G_w = zeros(Float64, size(g_2D)...) + G_w[1,:] = 2*g_y + G_w[2,:] = g_y + G_w[3,:] = 3*g_y + + G_e = zeros(Float64, size(g_2D)...) + G_e[end,:] = 2*g_y + G_e[end-1,:] = g_y + G_e[end-2,:] = 3*g_y + + G_s = zeros(Float64, size(g_2D)...) + G_s[:,1] = 2*g_x + G_s[:,2] = g_x + G_s[:,3] = 3*g_x + + G_n = zeros(Float64, size(g_2D)...) + G_n[:,end] = 2*g_x + G_n[:,end-1] = g_x + G_n[:,end-2] = 3*g_x + + @test op_w'*g_y == G_w + @test op_e'*g_y == G_e + @test op_s'*g_x == G_s + @test op_n'*g_x == G_n + end + + @testset "Regions" begin + u = fill(3.124) + @test (op_l'*u)[Index(1,Lower)] == 2*u[] + @test (op_l'*u)[Index(2,Lower)] == u[] + @test (op_l'*u)[Index(6,Interior)] == 0 + @test (op_l'*u)[Index(10,Upper)] == 0 + @test (op_l'*u)[Index(11,Upper)] == 0 + + @test (op_r'*u)[Index(1,Lower)] == 0 + @test (op_r'*u)[Index(2,Lower)] == 0 + @test (op_r'*u)[Index(6,Interior)] == 0 + @test (op_r'*u)[Index(10,Upper)] == u[] + @test (op_r'*u)[Index(11,Upper)] == 2*u[] + end + end + + @testset "Inferred" begin + v = ones(Float64, 11) + u = fill(1.) + + @inferred apply(op_l, v) + @inferred apply(op_r, v) + + @inferred apply_transpose(op_l, u, 4) + @inferred apply_transpose(op_l, u, Index(1,Lower)) + @inferred apply_transpose(op_l, u, Index(2,Lower)) + @inferred apply_transpose(op_l, u, Index(6,Interior)) + @inferred apply_transpose(op_l, u, Index(10,Upper)) + @inferred apply_transpose(op_l, u, Index(11,Upper)) + + @inferred apply_transpose(op_r, u, 4) + @inferred apply_transpose(op_r, u, Index(1,Lower)) + @inferred apply_transpose(op_r, u, Index(2,Lower)) + @inferred apply_transpose(op_r, u, Index(6,Interior)) + @inferred apply_transpose(op_r, u, Index(10,Upper)) + @inferred apply_transpose(op_r, u, Index(11,Upper)) + end +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SbpOperators/boundaryops/boundary_restriction_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,65 @@ +using Test + +using Sbplib.SbpOperators +using Sbplib.Grids +using Sbplib.RegionIndices +using Sbplib.LazyTensors + +import Sbplib.SbpOperators.BoundaryOperator + +@testset "boundary_restriction" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + g_1D = EquidistantGrid(11, 0.0, 1.0) + g_2D = EquidistantGrid((11,15), (0.0, 0.0), (1.0,1.0)) + + @testset "boundary_restriction" begin + @testset "1D" begin + e_l = boundary_restriction(g_1D,op.eClosure,Lower()) + @test e_l == boundary_restriction(g_1D,op.eClosure,CartesianBoundary{1,Lower}()) + @test e_l == BoundaryOperator(g_1D,op.eClosure,Lower()) + @test e_l isa BoundaryOperator{T,Lower} where T + @test e_l isa TensorMapping{T,0,1} where T + + e_r = boundary_restriction(g_1D,op.eClosure,Upper()) + @test e_r == boundary_restriction(g_1D,op.eClosure,CartesianBoundary{1,Upper}()) + @test e_r == BoundaryOperator(g_1D,op.eClosure,Upper()) + @test e_r isa BoundaryOperator{T,Upper} where T + @test e_r isa TensorMapping{T,0,1} where T + end + + @testset "2D" begin + e_w = boundary_restriction(g_2D,op.eClosure,CartesianBoundary{1,Upper}()) + @test e_w isa InflatedTensorMapping + @test e_w isa TensorMapping{T,1,2} where T + end + end + + @testset "Application" begin + @testset "1D" begin + e_l = boundary_restriction(g_1D, op.eClosure, CartesianBoundary{1,Lower}()) + e_r = boundary_restriction(g_1D, op.eClosure, CartesianBoundary{1,Upper}()) + + v = evalOn(g_1D,x->1+x^2) + u = fill(3.124) + + @test (e_l*v)[] == v[1] + @test (e_r*v)[] == v[end] + @test (e_r*v)[1] == v[end] + end + + @testset "2D" begin + e_w = boundary_restriction(g_2D, op.eClosure, CartesianBoundary{1,Lower}()) + e_e = boundary_restriction(g_2D, op.eClosure, CartesianBoundary{1,Upper}()) + e_s = boundary_restriction(g_2D, op.eClosure, CartesianBoundary{2,Lower}()) + e_n = boundary_restriction(g_2D, op.eClosure, CartesianBoundary{2,Upper}()) + + v = rand(11, 15) + u = fill(3.124) + + @test e_w*v == v[1,:] + @test e_e*v == v[end,:] + @test e_s*v == v[:,1] + @test e_n*v == v[:,end] + end + end +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SbpOperators/boundaryops/normal_derivative_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,66 @@ +using Test + +using Sbplib.SbpOperators +using Sbplib.Grids +using Sbplib.RegionIndices +using Sbplib.LazyTensors + +import Sbplib.SbpOperators.BoundaryOperator + +@testset "normal_derivative" begin + g_1D = EquidistantGrid(11, 0.0, 1.0) + g_2D = EquidistantGrid((11,12), (0.0, 0.0), (1.0,1.0)) + @testset "normal_derivative" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + @testset "1D" begin + d_l = normal_derivative(g_1D, op.dClosure, Lower()) + @test d_l == normal_derivative(g_1D, op.dClosure, CartesianBoundary{1,Lower}()) + @test d_l isa BoundaryOperator{T,Lower} where T + @test d_l isa TensorMapping{T,0,1} where T + end + @testset "2D" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + d_w = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Lower}()) + d_n = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Upper}()) + Ix = IdentityMapping{Float64}((size(g_2D)[1],)) + Iy = IdentityMapping{Float64}((size(g_2D)[2],)) + d_l = normal_derivative(restrict(g_2D,1),op.dClosure,Lower()) + d_r = normal_derivative(restrict(g_2D,2),op.dClosure,Upper()) + @test d_w == d_l⊗Iy + @test d_n == Ix⊗d_r + @test d_w isa TensorMapping{T,1,2} where T + @test d_n isa TensorMapping{T,1,2} where T + end + end + @testset "Accuracy" begin + v = evalOn(g_2D, (x,y)-> x^2 + (y-1)^2 + x*y) + v∂x = evalOn(g_2D, (x,y)-> 2*x + y) + v∂y = evalOn(g_2D, (x,y)-> 2*(y-1) + x) + # TODO: Test for higher order polynomials? + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + d_w = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Lower}()) + d_e = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Upper}()) + d_s = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Lower}()) + d_n = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Upper}()) + + @test d_w*v ≈ v∂x[1,:] atol = 1e-13 + @test d_e*v ≈ -v∂x[end,:] atol = 1e-13 + @test d_s*v ≈ v∂y[:,1] atol = 1e-13 + @test d_n*v ≈ -v∂y[:,end] atol = 1e-13 + end + + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + d_w = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Lower}()) + d_e = normal_derivative(g_2D, op.dClosure, CartesianBoundary{1,Upper}()) + d_s = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Lower}()) + d_n = normal_derivative(g_2D, op.dClosure, CartesianBoundary{2,Upper}()) + + @test d_w*v ≈ v∂x[1,:] atol = 1e-13 + @test d_e*v ≈ -v∂x[end,:] atol = 1e-13 + @test d_s*v ≈ v∂y[:,1] atol = 1e-13 + @test d_n*v ≈ -v∂y[:,end] atol = 1e-13 + end + end +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SbpOperators/readoperator_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,115 @@ +using Test + +using TOML +using Sbplib.SbpOperators + +import Sbplib.SbpOperators.Stencil + + +@testset "parse_rational" begin + @test SbpOperators.parse_rational("1") isa Rational + @test SbpOperators.parse_rational("1") == 1//1 + @test SbpOperators.parse_rational("1/2") isa Rational + @test SbpOperators.parse_rational("1/2") == 1//2 + @test SbpOperators.parse_rational("37/13") isa Rational + @test SbpOperators.parse_rational("37/13") == 37//13 +end + +@testset "readoperator" begin + toml_str = """ + [meta] + authors = "Ken Mattson" + description = "Standard operators for equidistant grids" + type = "equidistant" + cite = "A paper a long time ago in a galaxy far far away." + + [[stencil_set]] + + order = 2 + test = 2 + + H.inner = ["1"] + H.closure = ["1/2"] + + D1.inner_stencil = ["-1/2", "0", "1/2"] + D1.closure_stencils = [ + {s = ["-1", "1"], c = 1}, + ] + + D2.inner_stencil = ["1", "-2", "1"] + D2.closure_stencils = [ + {s = ["1", "-2", "1"], c = 1}, + ] + + e.closure = ["1"] + d1.closure = {s = ["-3/2", "2", "-1/2"], c = 1} + + [[stencil_set]] + + order = 4 + test = 1 + H.inner = ["1"] + H.closure = ["17/48", "59/48", "43/48", "49/48"] + + D2.inner_stencil = ["-1/12","4/3","-5/2","4/3","-1/12"] + D2.closure_stencils = [ + {s = [ "2", "-5", "4", "-1", "0", "0"], c = 1}, + {s = [ "1", "-2", "1", "0", "0", "0"], c = 2}, + {s = [ "-4/43", "59/43", "-110/43", "59/43", "-4/43", "0"], c = 3}, + {s = [ "-1/49", "0", "59/49", "-118/49", "64/49", "-4/49"], c = 4}, + ] + + e.closure = ["1"] + d1.closure = {s = ["-11/6", "3", "-3/2", "1/3"], c = 1} + + [[stencil_set]] + order = 4 + test = 2 + + H.closure = ["-1/49", "0", "59/49", "-118/49", "64/49", "-4/49"] + """ + + parsed_toml = TOML.parse(toml_str) + + @testset "get_stencil_set" begin + @test get_stencil_set(parsed_toml; order = 2) isa Dict + @test get_stencil_set(parsed_toml; order = 2) == parsed_toml["stencil_set"][1] + @test get_stencil_set(parsed_toml; test = 1) == parsed_toml["stencil_set"][2] + @test get_stencil_set(parsed_toml; order = 4, test = 2) == parsed_toml["stencil_set"][3] + + @test_throws ArgumentError get_stencil_set(parsed_toml; test = 2) + @test_throws ArgumentError get_stencil_set(parsed_toml; order = 4) + end + + @testset "parse_stencil" begin + toml = """ + s1 = ["-1/12","4/3","-5/2","4/3","-1/12"] + s2 = {s = ["2", "-5", "4", "-1", "0", "0"], c = 1} + s3 = {s = ["1", "-2", "1", "0", "0", "0"], c = 2} + s4 = "not a stencil" + s5 = [-1, 4, 3] + s6 = {k = ["1", "-2", "1", "0", "0", "0"], c = 2} + s7 = {s = [-1, 4, 3], c = 2} + s8 = {s = ["1", "-2", "1", "0", "0", "0"], c = [2,2]} + """ + + @test parse_stencil(TOML.parse(toml)["s1"]) == CenteredStencil(-1/12, 4/3, -5/2, 4/3, -1/12) + @test parse_stencil(TOML.parse(toml)["s2"]) == Stencil(2., -5., 4., -1., 0., 0.; center=1) + @test parse_stencil(TOML.parse(toml)["s3"]) == Stencil(1., -2., 1., 0., 0., 0.; center=2) + + @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s4"]) + @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s5"]) + @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s6"]) + @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s7"]) + @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s8"]) + + stencil_set = get_stencil_set(parsed_toml; order = 4, test = 1) + + @test parse_stencil.(stencil_set["D2"]["closure_stencils"]) == [ + Stencil( 2., -5., 4., -1., 0., 0.; center=1), + Stencil( 1., -2., 1., 0., 0., 0.; center=2), + Stencil(-4/43, 59/43, -110/43, 59/43, -4/43, 0.; center=3), + Stencil(-1/49, 0., 59/49, -118/49, 64/49, -4/49; center=4), + ] + end +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SbpOperators/stencil_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,18 @@ +using Test +using Sbplib.SbpOperators +import Sbplib.SbpOperators.Stencil + +@testset "Stencil" begin + s = Stencil((-2,2), (1.,2.,2.,3.,4.)) + @test s isa Stencil{Float64, 5} + + @test eltype(s) == Float64 + @test SbpOperators.scale(s, 2) == Stencil((-2,2), (2.,4.,4.,6.,8.)) + + @test Stencil(1,2,3,4; center=1) == Stencil((0, 3),(1,2,3,4)) + @test Stencil(1,2,3,4; center=2) == Stencil((-1, 2),(1,2,3,4)) + @test Stencil(1,2,3,4; center=4) == Stencil((-3, 0),(1,2,3,4)) + + @test CenteredStencil(1,2,3,4,5) == Stencil((-2, 2), (1,2,3,4,5)) + @test_throws ArgumentError CenteredStencil(1,2,3,4) +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SbpOperators/volumeops/derivatives/secondderivative_test.jl Thu Jul 15 00:06:16 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SbpOperators/volumeops/inner_products/inner_product_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,98 @@ +using Test + +using Sbplib.SbpOperators +using Sbplib.Grids +using Sbplib.LazyTensors + + +@testset "Diagonal-stencil inner_product" begin + Lx = π/2. + Ly = Float64(π) + Lz = 1. + g_1D = EquidistantGrid(77, 0.0, Lx) + g_2D = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly)) + g_3D = EquidistantGrid((10,10, 10), (0.0, 0.0, 0.0), (Lx,Ly,Lz)) + integral(H,v) = sum(H*v) + @testset "inner_product" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + @testset "0D" begin + H = inner_product(EquidistantGrid{Float64}(),op.quadratureClosure) + @test H == IdentityMapping{Float64}() + @test H isa TensorMapping{T,0,0} where T + end + @testset "1D" begin + H = inner_product(g_1D,op.quadratureClosure) + inner_stencil = CenteredStencil(1.) + @test H == inner_product(g_1D,op.quadratureClosure,inner_stencil) + @test H isa TensorMapping{T,1,1} where T + end + @testset "2D" begin + H = inner_product(g_2D,op.quadratureClosure) + H_x = inner_product(restrict(g_2D,1),op.quadratureClosure) + H_y = inner_product(restrict(g_2D,2),op.quadratureClosure) + @test H == H_x⊗H_y + @test H isa TensorMapping{T,2,2} where T + end + end + + @testset "Sizes" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + @testset "1D" begin + H = inner_product(g_1D,op.quadratureClosure) + @test domain_size(H) == size(g_1D) + @test range_size(H) == size(g_1D) + end + @testset "2D" begin + H = inner_product(g_2D,op.quadratureClosure) + @test domain_size(H) == size(g_2D) + @test range_size(H) == size(g_2D) + end + end + + @testset "Accuracy" begin + @testset "1D" begin + v = () + for i = 0:4 + f_i(x) = 1/factorial(i)*x^i + v = (v...,evalOn(g_1D,f_i)) + end + u = evalOn(g_1D,x->sin(x)) + + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + H = inner_product(g_1D,op.quadratureClosure) + for i = 1:2 + @test integral(H,v[i]) ≈ v[i+1][end] - v[i+1][1] rtol = 1e-14 + end + @test integral(H,u) ≈ 1. rtol = 1e-4 + end + + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + H = inner_product(g_1D,op.quadratureClosure) + for i = 1:4 + @test integral(H,v[i]) ≈ v[i+1][end] - v[i+1][1] rtol = 1e-14 + end + @test integral(H,u) ≈ 1. rtol = 1e-8 + end + end + + @testset "2D" begin + b = 2.1 + v = b*ones(Float64, size(g_2D)) + u = evalOn(g_2D,(x,y)->sin(x)+cos(y)) + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + H = inner_product(g_2D,op.quadratureClosure) + @test integral(H,v) ≈ b*Lx*Ly rtol = 1e-13 + @test integral(H,u) ≈ π rtol = 1e-4 + end + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + H = inner_product(g_2D,op.quadratureClosure) + @test integral(H,v) ≈ b*Lx*Ly rtol = 1e-13 + @test integral(H,u) ≈ π rtol = 1e-8 + end + end + end +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SbpOperators/volumeops/inner_products/inverse_inner_product_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,92 @@ +using Test + +using Sbplib.SbpOperators +using Sbplib.Grids +using Sbplib.LazyTensors + +import Sbplib.SbpOperators.Stencil + +@testset "Diagonal-stencil inverse_inner_product" begin + Lx = π/2. + Ly = Float64(π) + g_1D = EquidistantGrid(77, 0.0, Lx) + g_2D = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly)) + @testset "inverse_inner_product" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + @testset "0D" begin + Hi = inverse_inner_product(EquidistantGrid{Float64}(),op.quadratureClosure) + @test Hi == IdentityMapping{Float64}() + @test Hi isa TensorMapping{T,0,0} where T + end + @testset "1D" begin + Hi = inverse_inner_product(g_1D, op.quadratureClosure); + inner_stencil = CenteredStencil(1.) + closures = () + for i = 1:length(op.quadratureClosure) + closures = (closures...,Stencil(op.quadratureClosure[i].range,1.0./op.quadratureClosure[i].weights)) + end + @test Hi == inverse_inner_product(g_1D,closures,inner_stencil) + @test Hi isa TensorMapping{T,1,1} where T + end + @testset "2D" begin + Hi = inverse_inner_product(g_2D,op.quadratureClosure) + Hi_x = inverse_inner_product(restrict(g_2D,1),op.quadratureClosure) + Hi_y = inverse_inner_product(restrict(g_2D,2),op.quadratureClosure) + @test Hi == Hi_x⊗Hi_y + @test Hi isa TensorMapping{T,2,2} where T + end + end + + @testset "Sizes" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + @testset "1D" begin + Hi = inverse_inner_product(g_1D,op.quadratureClosure) + @test domain_size(Hi) == size(g_1D) + @test range_size(Hi) == size(g_1D) + end + @testset "2D" begin + Hi = inverse_inner_product(g_2D,op.quadratureClosure) + @test domain_size(Hi) == size(g_2D) + @test range_size(Hi) == size(g_2D) + end + end + + @testset "Accuracy" begin + @testset "1D" begin + v = evalOn(g_1D,x->sin(x)) + u = evalOn(g_1D,x->x^3-x^2+1) + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + H = inner_product(g_1D,op.quadratureClosure) + Hi = inverse_inner_product(g_1D,op.quadratureClosure) + @test Hi*H*v ≈ v rtol = 1e-15 + @test Hi*H*u ≈ u rtol = 1e-15 + end + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + H = inner_product(g_1D,op.quadratureClosure) + Hi = inverse_inner_product(g_1D,op.quadratureClosure) + @test Hi*H*v ≈ v rtol = 1e-15 + @test Hi*H*u ≈ u rtol = 1e-15 + end + end + @testset "2D" begin + v = evalOn(g_2D,(x,y)->sin(x)+cos(y)) + u = evalOn(g_2D,(x,y)->x*y + x^5 - sqrt(y)) + @testset "2nd order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) + H = inner_product(g_2D,op.quadratureClosure) + Hi = inverse_inner_product(g_2D,op.quadratureClosure) + @test Hi*H*v ≈ v rtol = 1e-15 + @test Hi*H*u ≈ u rtol = 1e-15 + end + @testset "4th order" begin + op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) + H = inner_product(g_2D,op.quadratureClosure) + Hi = inverse_inner_product(g_2D,op.quadratureClosure) + @test Hi*H*v ≈ v rtol = 1e-15 + @test Hi*H*u ≈ u rtol = 1e-15 + end + end + end +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SbpOperators/volumeops/laplace/laplace_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,65 @@ +using Test + +using Sbplib.SbpOperators +using Sbplib.Grids +using Sbplib.LazyTensors + +@testset "Laplace" begin + g_1D = EquidistantGrid(101, 0.0, 1.) + g_3D = EquidistantGrid((51,101,52), (0.0, -1.0, 0.0), (1., 1., 1.)) + @testset "Constructors" 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 + end + @testset "3D" begin + L = laplace(g_3D, op.innerStencil, op.closureStencils) + @test L isa TensorMapping{T,3,3} where T + Dxx = second_derivative(g_3D, op.innerStencil, op.closureStencils,1) + 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 + end + end + + # Exact differentiation is measured point-wise. In other cases + # the error is measured in the l2-norm. + @testset "Accuracy" begin + l2(v) = sqrt(prod(spacing(g_3D))*sum(v.^2)); + polynomials = () + maxOrder = 4; + for i = 0:maxOrder-1 + f_i(x,y,z) = 1/factorial(i)*(y^i + x^i + z^i) + polynomials = (polynomials...,evalOn(g_3D,f_i)) + end + v = evalOn(g_3D, (x,y,z) -> sin(x) + cos(y) + exp(z)) + Δv = evalOn(g_3D,(x,y,z) -> -sin(x) - cos(y) + exp(z)) + + # 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) + L = laplace(g_3D,op.innerStencil,op.closureStencils) + @test L*polynomials[1] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 + @test L*polynomials[2] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 + @test L*polynomials[3] ≈ polynomials[1] atol = 5e-9 + @test L*v ≈ Δv 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) + L = laplace(g_3D,op.innerStencil,op.closureStencils) + # NOTE: high tolerances for checking the "exact" differentiation + # due to accumulation of round-off errors/cancellation errors? + @test L*polynomials[1] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 + @test L*polynomials[2] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 + @test L*polynomials[3] ≈ polynomials[1] atol = 5e-9 + @test L*polynomials[4] ≈ polynomials[2] atol = 5e-9 + @test L*v ≈ Δv rtol = 5e-4 norm = l2 + end + end +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/SbpOperators/volumeops/volume_operator_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,124 @@ +using Test + +using Sbplib.SbpOperators +using Sbplib.Grids +using Sbplib.RegionIndices +using Sbplib.LazyTensors + +import Sbplib.SbpOperators.Stencil +import Sbplib.SbpOperators.VolumeOperator +import Sbplib.SbpOperators.volume_operator +import Sbplib.SbpOperators.odd +import Sbplib.SbpOperators.even + +@testset "VolumeOperator" begin + inner_stencil = CenteredStencil(1/4, 2/4, 1/4) + closure_stencils = (Stencil(1/2, 1/2; center=1), Stencil(0.,1.; center=2)) + g_1D = EquidistantGrid(11,0.,1.) + g_2D = EquidistantGrid((11,12),(0.,0.),(1.,1.)) + g_3D = EquidistantGrid((11,12,10),(0.,0.,0.),(1.,1.,1.)) + @testset "Constructors" begin + @testset "1D" begin + op = VolumeOperator(inner_stencil,closure_stencils,(11,),even) + @test op == VolumeOperator(g_1D,inner_stencil,closure_stencils,even) + @test op == volume_operator(g_1D,inner_stencil,closure_stencils,even,1) + @test op isa TensorMapping{T,1,1} where T + end + @testset "2D" begin + op_x = volume_operator(g_2D,inner_stencil,closure_stencils,even,1) + op_y = volume_operator(g_2D,inner_stencil,closure_stencils,even,2) + Ix = IdentityMapping{Float64}((11,)) + Iy = IdentityMapping{Float64}((12,)) + @test op_x == VolumeOperator(inner_stencil,closure_stencils,(11,),even)⊗Iy + @test op_y == Ix⊗VolumeOperator(inner_stencil,closure_stencils,(12,),even) + @test op_x isa TensorMapping{T,2,2} where T + @test op_y isa TensorMapping{T,2,2} where T + end + @testset "3D" begin + op_x = volume_operator(g_3D,inner_stencil,closure_stencils,even,1) + op_y = volume_operator(g_3D,inner_stencil,closure_stencils,even,2) + op_z = volume_operator(g_3D,inner_stencil,closure_stencils,even,3) + Ix = IdentityMapping{Float64}((11,)) + Iy = IdentityMapping{Float64}((12,)) + Iz = IdentityMapping{Float64}((10,)) + @test op_x == VolumeOperator(inner_stencil,closure_stencils,(11,),even)⊗Iy⊗Iz + @test op_y == Ix⊗VolumeOperator(inner_stencil,closure_stencils,(12,),even)⊗Iz + @test op_z == Ix⊗Iy⊗VolumeOperator(inner_stencil,closure_stencils,(10,),even) + @test op_x isa TensorMapping{T,3,3} where T + @test op_y isa TensorMapping{T,3,3} where T + @test op_z isa TensorMapping{T,3,3} where T + end + end + + @testset "Sizes" begin + @testset "1D" begin + op = volume_operator(g_1D,inner_stencil,closure_stencils,even,1) + @test range_size(op) == domain_size(op) == size(g_1D) + end + + @testset "2D" begin + op_x = volume_operator(g_2D,inner_stencil,closure_stencils,even,1) + op_y = volume_operator(g_2D,inner_stencil,closure_stencils,even,2) + @test range_size(op_y) == domain_size(op_y) == + range_size(op_x) == domain_size(op_x) == size(g_2D) + end + @testset "3D" begin + op_x = volume_operator(g_3D,inner_stencil,closure_stencils,even,1) + op_y = volume_operator(g_3D,inner_stencil,closure_stencils,even,2) + op_z = volume_operator(g_3D,inner_stencil,closure_stencils,even,3) + @test range_size(op_z) == domain_size(op_z) == + range_size(op_y) == domain_size(op_y) == + range_size(op_x) == domain_size(op_x) == size(g_3D) + end + end + + op_x = volume_operator(g_2D,inner_stencil,closure_stencils,even,1) + op_y = volume_operator(g_2D,inner_stencil,closure_stencils,odd,2) + v = zeros(size(g_2D)) + Nx = size(g_2D)[1] + Ny = size(g_2D)[2] + for i = 1:Nx + v[i,:] .= i + end + rx = copy(v) + rx[1,:] .= 1.5 + rx[Nx,:] .= (2*Nx-1)/2 + ry = copy(v) + ry[:,Ny-1:Ny] = -v[:,Ny-1:Ny] + + @testset "Application" begin + @test op_x*v ≈ rx rtol = 1e-14 + @test op_y*v ≈ ry rtol = 1e-14 + end + + @testset "Regions" begin + @test (op_x*v)[Index(1,Lower),Index(3,Interior)] ≈ rx[1,3] rtol = 1e-14 + @test (op_x*v)[Index(2,Lower),Index(3,Interior)] ≈ rx[2,3] rtol = 1e-14 + @test (op_x*v)[Index(6,Interior),Index(3,Interior)] ≈ rx[6,3] rtol = 1e-14 + @test (op_x*v)[Index(10,Upper),Index(3,Interior)] ≈ rx[10,3] rtol = 1e-14 + @test (op_x*v)[Index(11,Upper),Index(3,Interior)] ≈ rx[11,3] rtol = 1e-14 + + @test_throws BoundsError (op_x*v)[Index(3,Lower),Index(3,Interior)] + @test_throws BoundsError (op_x*v)[Index(9,Upper),Index(3,Interior)] + + @test (op_y*v)[Index(3,Interior),Index(1,Lower)] ≈ ry[3,1] rtol = 1e-14 + @test (op_y*v)[Index(3,Interior),Index(2,Lower)] ≈ ry[3,2] rtol = 1e-14 + @test (op_y*v)[Index(3,Interior),Index(6,Interior)] ≈ ry[3,6] rtol = 1e-14 + @test (op_y*v)[Index(3,Interior),Index(11,Upper)] ≈ ry[3,11] rtol = 1e-14 + @test (op_y*v)[Index(3,Interior),Index(12,Upper)] ≈ ry[3,12] rtol = 1e-14 + + @test_throws BoundsError (op_y*v)[Index(3,Interior),Index(10,Upper)] + @test_throws BoundsError (op_y*v)[Index(3,Interior),Index(3,Lower)] + end + + @testset "Inferred" begin + @test_skip @inferred apply(op_x, v,1,1) + @inferred apply(op_x, v, Index(1,Lower),Index(1,Lower)) + @inferred apply(op_x, v, Index(6,Interior),Index(1,Lower)) + @inferred apply(op_x, v, Index(11,Upper),Index(1,Lower)) + @test_skip @inferred apply(op_y, v,1,1) + @inferred apply(op_y, v, Index(1,Lower),Index(1,Lower)) + @inferred apply(op_y, v, Index(1,Lower),Index(6,Interior)) + @inferred apply(op_y, v, Index(1,Lower),Index(11,Upper)) + end +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/StaticDicts/StaticDicts_test.jl Thu Jul 15 00:06:16 2021 +0200 @@ -0,0 +1,68 @@ +using Test +using Sbplib.StaticDicts + +@testset "StaticDicts" begin + +@testset "StaticDict" begin + @testset "constructor" begin + @test (StaticDict{Int,Int,N} where N) <: AbstractDict + + d = StaticDict(1=>2, 3=>4) + @test d isa StaticDict{Int,Int} + @test d[1] == 2 + @test d[3] == 4 + + @test StaticDict((1=>2, 3=>4)) == d + + @test StaticDict() isa StaticDict + @test StaticDict{Int,String}() isa StaticDict{Int,String,0} + + @test StaticDict(1=>3, 2=>4.) isa StaticDict{Int,Real} + @test StaticDict(1. =>3, 2=>4) isa StaticDict{Real,Int} + @test StaticDict(1. =>3, 2=>4.) isa StaticDict{Real,Real} + + @test_throws DomainError StaticDict(1=>3, 1=>3) + end + + @testset "length" begin + @test length(StaticDict()) == 0 + @test length(StaticDict(1=>1)) == 1 + @test length(StaticDict(1=>1, 2=>2)) == 2 + end + + @testset "equality" begin + @test StaticDict(1=>1) == StaticDict(1=>1) + @test StaticDict(2=>1) != StaticDict(1=>1) + @test StaticDict(1=>2) != StaticDict(1=>1) + + @test StaticDict(1=>1) === StaticDict(1=>1) #not true for a regular Dict + @test StaticDict(2=>1) !== StaticDict(1=>1) + @test StaticDict(1=>2) !== StaticDict(1=>1) + end + + @testset "get" begin + d = StaticDict(1=>2, 3=>4) + + @test get(d,1,6) == 2 + @test get(d,3,6) == 4 + @test get(d,5,6) == 6 + end + + @testset "iterate" begin + pairs = [1=>2, 3=>4, 5=>6] + + d = StaticDict(pairs...) + @test collect(d) == pairs + end + + @testset "merge" begin + @test merge( + StaticDict(1=>3, 2=> 4), + StaticDict(3=>5,4=>6)) == StaticDict( + 1=>3, 2=>4, 3=>5, 4=>6 + ) + @test_throws DomainError merge(StaticDict(1=>3),StaticDict(1=>3)) + end +end + +end
--- a/test/runtests.jl Wed Jul 14 23:40:10 2021 +0200 +++ b/test/runtests.jl Thu Jul 15 00:06:16 2021 +0200 @@ -1,6 +1,52 @@ using Test -using TestSetExtensions +using Glob + +""" + run_testfiles() + run_testfiles(path, globs) + +Find and run all files with filenames ending with "_test.jl". If `path` is omitted the test folder is assumed. +The argument `globs` can optionally be supplied to filter which test files are run. +""" +function run_testfiles(args) + if isempty(args) + globs = [fn"./*"] + else + globs = Glob.FilenameMatch.("./".*args) + end + + run_testfiles(".", globs) +end + +function run_testfiles(path, globs) + for name ∈ readdir(path) + filepath = joinpath(path, name) -@testset "All" begin - @includetests ARGS + if isdir(filepath) + @testset "$name" begin + run_testfiles(filepath, globs) + end + end + + if endswith(name, "_test.jl") && any(occursin.(globs, filepath)) + printstyled("Running "; bold=true, color=:green) + print(filepath) + + t_start = time() + @testset "$name" begin + include(filepath) + end + t_end = time() + + Δt = t_end - t_start + printstyled(" ($(round(Δt, digits=2)) s)"; color=:light_black) + println() + end + end end + +testsetname = isempty(ARGS) ? "Sbplib.jl" : "["*join(ARGS, ", ")*"]" + +@testset "$testsetname" begin + run_testfiles(ARGS) +end
--- a/test/testDiffOps.jl Wed Jul 14 23:40:10 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,198 +0,0 @@ -using Test -using Sbplib.DiffOps -using Sbplib.Grids -using Sbplib.SbpOperators -using Sbplib.RegionIndices -using Sbplib.LazyTensors - -@testset "DiffOps" begin -# -# @testset "BoundaryValue" begin -# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) -# g = EquidistantGrid((4,5), (0.0, 0.0), (1.0,1.0)) -# -# e_w = BoundaryValue(op, g, CartesianBoundary{1,Lower}()) -# e_e = BoundaryValue(op, g, CartesianBoundary{1,Upper}()) -# e_s = BoundaryValue(op, g, CartesianBoundary{2,Lower}()) -# e_n = BoundaryValue(op, g, CartesianBoundary{2,Upper}()) -# -# v = zeros(Float64, 4, 5) -# v[:,5] = [1, 2, 3,4] -# v[:,4] = [1, 2, 3,4] -# v[:,3] = [4, 5, 6, 7] -# v[:,2] = [7, 8, 9, 10] -# v[:,1] = [10, 11, 12, 13] -# -# @test e_w isa TensorMapping{T,2,1} where T -# @test e_w' isa TensorMapping{T,1,2} where T -# -# @test domain_size(e_w, (3,2)) == (2,) -# @test domain_size(e_e, (3,2)) == (2,) -# @test domain_size(e_s, (3,2)) == (3,) -# @test domain_size(e_n, (3,2)) == (3,) -# -# @test size(e_w'*v) == (5,) -# @test size(e_e'*v) == (5,) -# @test size(e_s'*v) == (4,) -# @test size(e_n'*v) == (4,) -# -# @test collect(e_w'*v) == [10,7,4,1.0,1] -# @test collect(e_e'*v) == [13,10,7,4,4.0] -# @test collect(e_s'*v) == [10,11,12,13.0] -# @test collect(e_n'*v) == [1,2,3,4.0] -# -# g_x = [1,2,3,4.0] -# g_y = [5,4,3,2,1.0] -# -# G_w = zeros(Float64, (4,5)) -# G_w[1,:] = g_y -# -# G_e = zeros(Float64, (4,5)) -# G_e[4,:] = g_y -# -# G_s = zeros(Float64, (4,5)) -# G_s[:,1] = g_x -# -# G_n = zeros(Float64, (4,5)) -# G_n[:,5] = g_x -# -# @test size(e_w*g_y) == (UnknownDim,5) -# @test size(e_e*g_y) == (UnknownDim,5) -# @test size(e_s*g_x) == (4,UnknownDim) -# @test size(e_n*g_x) == (4,UnknownDim) -# -# # These tests should be moved to where they are possible (i.e we know what the grid should be) -# @test_broken collect(e_w*g_y) == G_w -# @test_broken collect(e_e*g_y) == G_e -# @test_broken collect(e_s*g_x) == G_s -# @test_broken collect(e_n*g_x) == G_n -# end -# -# @testset "NormalDerivative" begin -# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) -# g = EquidistantGrid((5,6), (0.0, 0.0), (4.0,5.0)) -# -# d_w = NormalDerivative(op, g, CartesianBoundary{1,Lower}()) -# d_e = NormalDerivative(op, g, CartesianBoundary{1,Upper}()) -# d_s = NormalDerivative(op, g, CartesianBoundary{2,Lower}()) -# d_n = NormalDerivative(op, g, CartesianBoundary{2,Upper}()) -# -# -# v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y) -# v∂x = evalOn(g, (x,y)-> 2*x + y) -# v∂y = evalOn(g, (x,y)-> 2*(y-1) + x) -# -# @test d_w isa TensorMapping{T,2,1} where T -# @test d_w' isa TensorMapping{T,1,2} where T -# -# @test domain_size(d_w, (3,2)) == (2,) -# @test domain_size(d_e, (3,2)) == (2,) -# @test domain_size(d_s, (3,2)) == (3,) -# @test domain_size(d_n, (3,2)) == (3,) -# -# @test size(d_w'*v) == (6,) -# @test size(d_e'*v) == (6,) -# @test size(d_s'*v) == (5,) -# @test size(d_n'*v) == (5,) -# -# @test collect(d_w'*v) ≈ v∂x[1,:] -# @test collect(d_e'*v) ≈ v∂x[5,:] -# @test collect(d_s'*v) ≈ v∂y[:,1] -# @test collect(d_n'*v) ≈ v∂y[:,6] -# -# -# d_x_l = zeros(Float64, 5) -# d_x_u = zeros(Float64, 5) -# for i ∈ eachindex(d_x_l) -# d_x_l[i] = op.dClosure[i-1] -# d_x_u[i] = -op.dClosure[length(d_x_u)-i] -# end -# -# d_y_l = zeros(Float64, 6) -# d_y_u = zeros(Float64, 6) -# for i ∈ eachindex(d_y_l) -# d_y_l[i] = op.dClosure[i-1] -# d_y_u[i] = -op.dClosure[length(d_y_u)-i] -# end -# -# function prod_matrix(x,y) -# G = zeros(Float64, length(x), length(y)) -# for I ∈ CartesianIndices(G) -# G[I] = x[I[1]]*y[I[2]] -# end -# -# return G -# end -# -# g_x = [1,2,3,4.0,5] -# g_y = [5,4,3,2,1.0,11] -# -# G_w = prod_matrix(d_x_l, g_y) -# G_e = prod_matrix(d_x_u, g_y) -# G_s = prod_matrix(g_x, d_y_l) -# G_n = prod_matrix(g_x, d_y_u) -# -# -# @test size(d_w*g_y) == (UnknownDim,6) -# @test size(d_e*g_y) == (UnknownDim,6) -# @test size(d_s*g_x) == (5,UnknownDim) -# @test size(d_n*g_x) == (5,UnknownDim) -# -# # These tests should be moved to where they are possible (i.e we know what the grid should be) -# @test_broken collect(d_w*g_y) ≈ G_w -# @test_broken collect(d_e*g_y) ≈ G_e -# @test_broken collect(d_s*g_x) ≈ G_s -# @test_broken collect(d_n*g_x) ≈ G_n -# end -# -# @testset "BoundaryQuadrature" begin -# op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) -# g = EquidistantGrid((10,11), (0.0, 0.0), (1.0,1.0)) -# -# H_w = BoundaryQuadrature(op, g, CartesianBoundary{1,Lower}()) -# H_e = BoundaryQuadrature(op, g, CartesianBoundary{1,Upper}()) -# H_s = BoundaryQuadrature(op, g, CartesianBoundary{2,Lower}()) -# H_n = BoundaryQuadrature(op, g, CartesianBoundary{2,Upper}()) -# -# v = evalOn(g, (x,y)-> x^2 + (y-1)^2 + x*y) -# -# function get_quadrature(N) -# qc = op.quadratureClosure -# q = (qc..., ones(N-2*closuresize(op))..., reverse(qc)...) -# @assert length(q) == N -# return q -# end -# -# v_w = v[1,:] -# v_e = v[10,:] -# v_s = v[:,1] -# v_n = v[:,11] -# -# q_x = spacing(g)[1].*get_quadrature(10) -# q_y = spacing(g)[2].*get_quadrature(11) -# -# @test H_w isa TensorOperator{T,1} where T -# -# @test domain_size(H_w, (3,)) == (3,) -# @test domain_size(H_n, (3,)) == (3,) -# -# @test range_size(H_w, (3,)) == (3,) -# @test range_size(H_n, (3,)) == (3,) -# -# @test size(H_w*v_w) == (11,) -# @test size(H_e*v_e) == (11,) -# @test size(H_s*v_s) == (10,) -# @test size(H_n*v_n) == (10,) -# -# @test collect(H_w*v_w) ≈ q_y.*v_w -# @test collect(H_e*v_e) ≈ q_y.*v_e -# @test collect(H_s*v_s) ≈ q_x.*v_s -# @test collect(H_n*v_n) ≈ q_x.*v_n -# -# @test collect(H_w'*v_w) == collect(H_w'*v_w) -# @test collect(H_e'*v_e) == collect(H_e'*v_e) -# @test collect(H_s'*v_s) == collect(H_s'*v_s) -# @test collect(H_n'*v_n) == collect(H_n'*v_n) -# end - -end
--- a/test/testGrids.jl Wed Jul 14 23:40:10 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -using Sbplib.Grids -using Test -using Sbplib.RegionIndices - -@testset "Grids" begin - -@testset "EquidistantGrid" begin - @test EquidistantGrid(4,0.0,1.0) isa EquidistantGrid - @test EquidistantGrid(4,0.0,8.0) isa EquidistantGrid - # constuctor - @test_throws DomainError EquidistantGrid(0,0.0,1.0) - @test_throws DomainError EquidistantGrid(1,1.0,1.0) - @test_throws DomainError EquidistantGrid(1,1.0,-1.0) - @test EquidistantGrid(4,0.0,1.0) == EquidistantGrid((4,),(0.0,),(1.0,)) - - # size - @test size(EquidistantGrid(4,0.0,1.0)) == (4,) - @test size(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == (5,3) - - # dimension - @test dimension(EquidistantGrid(4,0.0,1.0)) == 1 - @test dimension(EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0))) == 2 - - # spacing - @test [spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(1. /3,)...] atol=5e-13 - @test [spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(0.5, 1.)...] atol=5e-13 - - # inverse_spacing - @test [inverse_spacing(EquidistantGrid(4,0.0,1.0))...] ≈ [(3.,)...] atol=5e-13 - @test [inverse_spacing(EquidistantGrid((5,3), (0.0,-1.0), (2.0,1.0)))...] ≈ [(2, 1.)...] atol=5e-13 - - # points - g = EquidistantGrid((5,3), (-1.0,0.0), (0.0,7.11)) - gp = points(g); - p = [(-1.,0.) (-1.,7.11/2) (-1.,7.11); - (-0.75,0.) (-0.75,7.11/2) (-0.75,7.11); - (-0.5,0.) (-0.5,7.11/2) (-0.5,7.11); - (-0.25,0.) (-0.25,7.11/2) (-0.25,7.11); - (0.,0.) (0.,7.11/2) (0.,7.11)] - for i ∈ eachindex(gp) - @test [gp[i]...] ≈ [p[i]...] atol=5e-13 - end - - # restrict - g = EquidistantGrid((5,3), (0.0,0.0), (2.0,1.0)) - @test restrict(g, 1) == EquidistantGrid(5,0.0,2.0) - @test restrict(g, 2) == EquidistantGrid(3,0.0,1.0) - - g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) - @test restrict(g, 1) == EquidistantGrid(2,0.0,2.0) - @test restrict(g, 2) == EquidistantGrid(5,0.0,1.0) - @test restrict(g, 3) == EquidistantGrid(3,0.0,3.0) - @test restrict(g, 1:2) == EquidistantGrid((2,5),(0.0,0.0),(2.0,1.0)) - @test restrict(g, 2:3) == EquidistantGrid((5,3),(0.0,0.0),(1.0,3.0)) - @test restrict(g, [1,3]) == EquidistantGrid((2,3),(0.0,0.0),(2.0,3.0)) - @test restrict(g, [2,1]) == EquidistantGrid((5,2),(0.0,0.0),(1.0,2.0)) - - @testset "boundary_identifiers" begin - g = EquidistantGrid((2,5,3), (0.0,0.0,0.0), (2.0,1.0,3.0)) - bids = (CartesianBoundary{1,Lower}(),CartesianBoundary{1,Upper}(), - CartesianBoundary{2,Lower}(),CartesianBoundary{2,Upper}(), - CartesianBoundary{3,Lower}(),CartesianBoundary{3,Upper}()) - @test boundary_identifiers(g) == bids - @inferred boundary_identifiers(g) - end -end - -end
--- a/test/testLazyTensors.jl Wed Jul 14 23:40:10 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,580 +0,0 @@ -using Test -using Sbplib.LazyTensors -using Sbplib.RegionIndices - -using Tullio - -@testset "LazyTensors" begin - -@testset "Generic Mapping methods" begin - struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end - LazyTensors.apply(m::DummyMapping{T,R,D}, v, I::Vararg{Any,R}) where {T,R,D} = :apply - @test range_dim(DummyMapping{Int,2,3}()) == 2 - @test domain_dim(DummyMapping{Int,2,3}()) == 3 - @test apply(DummyMapping{Int,2,3}(), zeros(Int, (0,0,0)),0,0) == :apply - @test eltype(DummyMapping{Int,2,3}()) == Int - @test eltype(DummyMapping{Float64,2,3}()) == Float64 -end - -@testset "Mapping transpose" begin - struct DummyMapping{T,R,D} <: TensorMapping{T,R,D} end - - LazyTensors.apply(m::DummyMapping{T,R}, v, I::Vararg{Any,R}) where {T,R} = :apply - LazyTensors.apply_transpose(m::DummyMapping{T,R,D}, v, I::Vararg{Any,D}) where {T,R,D} = :apply_transpose - - LazyTensors.range_size(m::DummyMapping) = :range_size - LazyTensors.domain_size(m::DummyMapping) = :domain_size - - m = DummyMapping{Float64,2,3}() - @test m' isa TensorMapping{Float64, 3,2} - @test m'' == m - @test apply(m',zeros(Float64,(0,0)), 0, 0, 0) == :apply_transpose - @test apply(m'',zeros(Float64,(0,0,0)), 0, 0) == :apply - @test apply_transpose(m', zeros(Float64,(0,0,0)), 0, 0) == :apply - - @test range_size(m') == :domain_size - @test domain_size(m') == :range_size -end - -@testset "TensorApplication" begin - struct SizeDoublingMapping{T,R,D} <: TensorMapping{T,R,D} - domain_size::NTuple{D,Int} - end - - LazyTensors.apply(m::SizeDoublingMapping{T,R}, v, i::Vararg{Any,R}) where {T,R} = (:apply,v,i) - LazyTensors.range_size(m::SizeDoublingMapping) = 2 .* m.domain_size - LazyTensors.domain_size(m::SizeDoublingMapping) = m.domain_size - - - m = SizeDoublingMapping{Int, 1, 1}((3,)) - v = [0,1,2] - @test m*v isa AbstractVector{Int} - @test size(m*v) == 2 .*size(v) - @test (m*v)[0] == (:apply,v,(0,)) - @test m*m*v isa AbstractVector{Int} - @test (m*m*v)[1] == (:apply,m*v,(1,)) - @test (m*m*v)[3] == (:apply,m*v,(3,)) - @test (m*m*v)[6] == (:apply,m*v,(6,)) - @test_broken BoundsError == (m*m*v)[0] - @test_broken BoundsError == (m*m*v)[7] - @test_throws MethodError m*m - - m = SizeDoublingMapping{Int, 2, 1}((3,)) - @test_throws MethodError m*ones(Int,2,2) - @test_throws MethodError m*m*v - - m = SizeDoublingMapping{Float64, 2, 2}((3,3)) - v = ones(3,3) - @test size(m*v) == 2 .*size(v) - @test (m*v)[1,2] == (:apply,v,(1,2)) - - struct ScalingOperator{T,D} <: TensorMapping{T,D,D} - λ::T - size::NTuple{D,Int} - end - - LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Any,D}) where {T,D} = m.λ*v[I...] - LazyTensors.range_size(m::ScalingOperator) = m.size - LazyTensors.domain_size(m::ScalingOperator) = m.size - - m = ScalingOperator{Int,1}(2,(3,)) - v = [1,2,3] - @test m*v isa AbstractVector - @test m*v == [2,4,6] - - m = ScalingOperator{Int,2}(2,(2,2)) - v = [[1 2];[3 4]] - @test m*v == [[2 4];[6 8]] - @test (m*v)[2,1] == 6 -end - -@testset "TensorMapping binary operations" begin - struct ScalarMapping{T,R,D} <: TensorMapping{T,R,D} - λ::T - range_size::NTuple{R,Int} - domain_size::NTuple{D,Int} - end - - LazyTensors.apply(m::ScalarMapping{T,R}, v, I::Vararg{Any,R}) where {T,R} = m.λ*v[I...] - LazyTensors.range_size(m::ScalarMapping) = m.domain_size - LazyTensors.domain_size(m::ScalarMapping) = m.range_size - - A = ScalarMapping{Float64,1,1}(2.0, (3,), (3,)) - B = ScalarMapping{Float64,1,1}(3.0, (3,), (3,)) - - v = [1.1,1.2,1.3] - for i ∈ eachindex(v) - @test ((A+B)*v)[i] == 2*v[i] + 3*v[i] - end - - for i ∈ eachindex(v) - @test ((A-B)*v)[i] == 2*v[i] - 3*v[i] - end - - @test range_size(A+B) == range_size(A) == range_size(B) - @test domain_size(A+B) == domain_size(A) == domain_size(B) -end - -@testset "LazyArray" begin - @testset "LazyConstantArray" begin - @test LazyTensors.LazyConstantArray(3,(3,2)) isa LazyArray{Int,2} - - lca = LazyTensors.LazyConstantArray(3.0,(3,2)) - @test eltype(lca) == Float64 - @test ndims(lca) == 2 - @test size(lca) == (3,2) - @test lca[2] == 3.0 - end - struct DummyArray{T,D, T1<:AbstractArray{T,D}} <: LazyArray{T,D} - data::T1 - end - Base.size(v::DummyArray) = size(v.data) - Base.getindex(v::DummyArray{T,D}, I::Vararg{Int,D}) where {T,D} = v.data[I...] - - # Test lazy operations - v1 = [1, 2.3, 4] - v2 = [1., 2, 3] - s = 3.4 - r_add_v = v1 .+ v2 - r_sub_v = v1 .- v2 - r_times_v = v1 .* v2 - r_div_v = v1 ./ v2 - r_add_s = v1 .+ s - r_sub_s = v1 .- s - r_times_s = v1 .* s - r_div_s = v1 ./ s - @test isa(v1 +̃ v2, LazyArray) - @test isa(v1 -̃ v2, LazyArray) - @test isa(v1 *̃ v2, LazyArray) - @test isa(v1 /̃ v2, LazyArray) - @test isa(v1 +̃ s, LazyArray) - @test isa(v1 -̃ s, LazyArray) - @test isa(v1 *̃ s, LazyArray) - @test isa(v1 /̃ s, LazyArray) - @test isa(s +̃ v1, LazyArray) - @test isa(s -̃ v1, LazyArray) - @test isa(s *̃ v1, LazyArray) - @test isa(s /̃ v1, LazyArray) - for i ∈ eachindex(v1) - @test (v1 +̃ v2)[i] == r_add_v[i] - @test (v1 -̃ v2)[i] == r_sub_v[i] - @test (v1 *̃ v2)[i] == r_times_v[i] - @test (v1 /̃ v2)[i] == r_div_v[i] - @test (v1 +̃ s)[i] == r_add_s[i] - @test (v1 -̃ s)[i] == r_sub_s[i] - @test (v1 *̃ s)[i] == r_times_s[i] - @test (v1 /̃ s)[i] == r_div_s[i] - @test (s +̃ v1)[i] == r_add_s[i] - @test (s -̃ v1)[i] == -r_sub_s[i] - @test (s *̃ v1)[i] == r_times_s[i] - @test (s /̃ v1)[i] == 1/r_div_s[i] - end - @test_throws BoundsError (v1 +̃ v2)[4] - v2 = [1., 2, 3, 4] - # Test that size of arrays is asserted when not specified inbounds - # TODO: Replace these errors with SizeMismatch - @test_throws DimensionMismatch v1 +̃ v2 - - # Test operations on LazyArray - v1 = DummyArray([1, 2.3, 4]) - v2 = [1., 2, 3] - @test isa(v1 + v2, LazyArray) - @test isa(v2 + v1, LazyArray) - @test isa(v1 - v2, LazyArray) - @test isa(v2 - v1, LazyArray) - for i ∈ eachindex(v2) - @test (v1 + v2)[i] == (v2 + v1)[i] == r_add_v[i] - @test (v1 - v2)[i] == -(v2 - v1)[i] == r_sub_v[i] - end - @test_throws BoundsError (v1 + v2)[4] - v2 = [1., 2, 3, 4] - # Test that size of arrays is asserted when not specified inbounds - # TODO: Replace these errors with SizeMismatch - @test_throws DimensionMismatch v1 + v2 -end - - -@testset "LazyFunctionArray" begin - @test LazyFunctionArray(i->i^2, (3,)) == [1,4,9] - @test LazyFunctionArray((i,j)->i*j, (3,2)) == [ - 1 2; - 2 4; - 3 6; - ] - - @test size(LazyFunctionArray(i->i^2, (3,))) == (3,) - @test size(LazyFunctionArray((i,j)->i*j, (3,2))) == (3,2) - - @inferred LazyFunctionArray(i->i^2, (3,))[2] - - @test_throws BoundsError LazyFunctionArray(i->i^2, (3,))[4] - @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[4,2] - @test_throws BoundsError LazyFunctionArray((i,j)->i*j, (3,2))[2,3] - -end - -@testset "TensorMappingComposition" begin - A = rand(2,3) - B = rand(3,4) - - à = LazyLinearMap(A, (1,), (2,)) - B̃ = LazyLinearMap(B, (1,), (2,)) - - @test Ã∘B̃ isa TensorMappingComposition - @test range_size(Ã∘B̃) == (2,) - @test domain_size(Ã∘B̃) == (4,) - @test_throws SizeMismatch B̃∘à - - # @test @inbounds B̃∘à # Should not error even though dimensions don't match. (Since ]test runs with forced boundschecking this is currently not testable 2020-10-16) - - v = rand(4) - @test Ã∘B̃*v ≈ A*B*v rtol=1e-14 - - v = rand(2) - @test (Ã∘B̃)'*v ≈ B'*A'*v rtol=1e-14 -end - -@testset "LazyLinearMap" begin - # Test a standard matrix-vector product - # mapping vectors of size 4 to vectors of size 3. - A = rand(3,4) - à = LazyLinearMap(A, (1,), (2,)) - v = rand(4) - w = rand(3) - - @test à isa LazyLinearMap{T,1,1} where T - @test à isa TensorMapping{T,1,1} where T - @test range_size(Ã) == (3,) - @test domain_size(Ã) == (4,) - - @test Ã*ones(4) ≈ A*ones(4) atol=5e-13 - @test Ã*v ≈ A*v atol=5e-13 - @test Ã'*w ≈ A'*w - - A = rand(2,3,4) - @test_throws DomainError LazyLinearMap(A, (3,1), (2,)) - - # Test more exotic mappings - B = rand(3,4,2) - # Map vectors of size 2 to matrices of size (3,4) - B̃ = LazyLinearMap(B, (1,2), (3,)) - v = rand(2) - - @test range_size(B̃) == (3,4) - @test domain_size(B̃) == (2,) - @test B̃ isa TensorMapping{T,2,1} where T - @test B̃*ones(2) ≈ B[:,:,1] + B[:,:,2] atol=5e-13 - @test B̃*v ≈ B[:,:,1]*v[1] + B[:,:,2]*v[2] atol=5e-13 - - # Map matrices of size (3,2) to vectors of size 4 - B̃ = LazyLinearMap(B, (2,), (1,3)) - v = rand(3,2) - - @test range_size(B̃) == (4,) - @test domain_size(B̃) == (3,2) - @test B̃ isa TensorMapping{T,1,2} where T - @test B̃*ones(3,2) ≈ B[1,:,1] + B[2,:,1] + B[3,:,1] + - B[1,:,2] + B[2,:,2] + B[3,:,2] atol=5e-13 - @test B̃*v ≈ B[1,:,1]*v[1,1] + B[2,:,1]*v[2,1] + B[3,:,1]*v[3,1] + - B[1,:,2]v[1,2] + B[2,:,2]*v[2,2] + B[3,:,2]*v[3,2] atol=5e-13 - - - # TODO: - # @inferred (B̃*v)[2] -end - - -@testset "IdentityMapping" begin - @test IdentityMapping{Float64}((4,5)) isa IdentityMapping{T,2} where T - @test IdentityMapping{Float64}((4,5)) isa TensorMapping{T,2,2} where T - @test IdentityMapping{Float64}((4,5)) == IdentityMapping{Float64}(4,5) - - @test IdentityMapping(3,2) isa IdentityMapping{Float64,2} - - for sz ∈ [(4,5),(3,),(5,6,4)] - I = IdentityMapping{Float64}(sz) - v = rand(sz...) - @test I*v == v - @test I'*v == v - - @test range_size(I) == sz - @test domain_size(I) == sz - end - - I = IdentityMapping{Float64}((4,5)) - v = rand(4,5) - @inferred (I*v)[3,2] - @inferred (I'*v)[3,2] - @inferred range_size(I) - - @inferred range_dim(I) - @inferred domain_dim(I) - - à = rand(4,2) - A = LazyLinearMap(Ã,(1,),(2,)) - I1 = IdentityMapping{Float64}(2) - I2 = IdentityMapping{Float64}(4) - @test A∘I1 == A - @test I2∘A == A - @test I1∘I1 == I1 - @test_throws SizeMismatch I1∘A - @test_throws SizeMismatch A∘I2 - @test_throws SizeMismatch I1∘I2 -end - -@testset "InflatedTensorMapping" begin - I(sz...) = IdentityMapping(sz...) - - à = rand(4,2) - B̃ = rand(4,2,3) - C̃ = rand(4,2,3) - - A = LazyLinearMap(Ã,(1,),(2,)) - B = LazyLinearMap(B̃,(1,2),(3,)) - C = LazyLinearMap(C̃,(1,),(2,3)) - - @testset "Constructors" begin - @test InflatedTensorMapping(I(3,2), A, I(4)) isa TensorMapping{Float64, 4, 4} - @test InflatedTensorMapping(I(3,2), B, I(4)) isa TensorMapping{Float64, 5, 4} - @test InflatedTensorMapping(I(3), C, I(2,3)) isa TensorMapping{Float64, 4, 5} - @test InflatedTensorMapping(C, I(2,3)) isa TensorMapping{Float64, 3, 4} - @test InflatedTensorMapping(I(3), C) isa TensorMapping{Float64, 2, 3} - @test InflatedTensorMapping(I(3), I(2,3)) isa TensorMapping{Float64, 3, 3} - end - - @testset "Range and domain size" begin - @test range_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,4,4) - @test domain_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,2,4) - - @test range_size(InflatedTensorMapping(I(3,2), B, I(4))) == (3,2,4,2,4) - @test domain_size(InflatedTensorMapping(I(3,2), B, I(4))) == (3,2,3,4) - - @test range_size(InflatedTensorMapping(I(3), C, I(2,3))) == (3,4,2,3) - @test domain_size(InflatedTensorMapping(I(3), C, I(2,3))) == (3,2,3,2,3) - - @inferred range_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,4,4) - @inferred domain_size(InflatedTensorMapping(I(3,2), A, I(4))) == (3,2,2,4) - end - - @testset "Application" begin - # Testing regular application and transposed application with inflation "before", "after" and "before and after". - # The inflated tensor mappings are chosen to preserve, reduce and increase the dimension of the result compared to the input. - tests = [ - ( - InflatedTensorMapping(I(3,2), A, I(4)), - (v-> @tullio res[a,b,c,d] := Ã[c,i]*v[a,b,i,d]), # Expected result of apply - (v-> @tullio res[a,b,c,d] := Ã[i,c]*v[a,b,i,d]), # Expected result of apply_transpose - ), - ( - InflatedTensorMapping(I(3,2), B, I(4)), - (v-> @tullio res[a,b,c,d,e] := B̃[c,d,i]*v[a,b,i,e]), - (v-> @tullio res[a,b,c,d] := B̃[i,j,c]*v[a,b,i,j,d]), - ), - ( - InflatedTensorMapping(I(3,2), C, I(4)), - (v-> @tullio res[a,b,c,d] := C̃[c,i,j]*v[a,b,i,j,d]), - (v-> @tullio res[a,b,c,d,e] := C̃[i,c,d]*v[a,b,i,e]), - ), - ( - InflatedTensorMapping(I(3,2), A), - (v-> @tullio res[a,b,c] := Ã[c,i]*v[a,b,i]), - (v-> @tullio res[a,b,c] := Ã[i,c]*v[a,b,i]), - ), - ( - InflatedTensorMapping(I(3,2), B), - (v-> @tullio res[a,b,c,d] := B̃[c,d,i]*v[a,b,i]), - (v-> @tullio res[a,b,c] := B̃[i,j,c]*v[a,b,i,j]), - ), - ( - InflatedTensorMapping(I(3,2), C), - (v-> @tullio res[a,b,c] := C̃[c,i,j]*v[a,b,i,j]), - (v-> @tullio res[a,b,c,d] := C̃[i,c,d]*v[a,b,i]), - ), - ( - InflatedTensorMapping(A,I(4)), - (v-> @tullio res[a,b] := Ã[a,i]*v[i,b]), - (v-> @tullio res[a,b] := Ã[i,a]*v[i,b]), - ), - ( - InflatedTensorMapping(B,I(4)), - (v-> @tullio res[a,b,c] := B̃[a,b,i]*v[i,c]), - (v-> @tullio res[a,b] := B̃[i,j,a]*v[i,j,b]), - ), - ( - InflatedTensorMapping(C,I(4)), - (v-> @tullio res[a,b] := C̃[a,i,j]*v[i,j,b]), - (v-> @tullio res[a,b,c] := C̃[i,a,b]*v[i,c]), - ), - ] - - @testset "apply" begin - for i ∈ 1:length(tests) - tm = tests[i][1] - v = rand(domain_size(tm)...) - true_value = tests[i][2](v) - @test tm*v ≈ true_value rtol=1e-14 - end - end - - @testset "apply_transpose" begin - for i ∈ 1:length(tests) - tm = tests[i][1] - v = rand(range_size(tm)...) - true_value = tests[i][3](v) - @test tm'*v ≈ true_value rtol=1e-14 - end - end - - @testset "Inference of application" begin - struct ScalingOperator{T,D} <: TensorMapping{T,D,D} - λ::T - size::NTuple{D,Int} - end - - LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Any,D}) where {T,D} = m.λ*v[I...] - LazyTensors.range_size(m::ScalingOperator) = m.size - LazyTensors.domain_size(m::ScalingOperator) = m.size - - tm = InflatedTensorMapping(I(2,3),ScalingOperator(2.0, (3,2)),I(3,4)) - v = rand(domain_size(tm)...) - - @inferred apply(tm,v,1,2,3,2,2,4) - @inferred (tm*v)[1,2,3,2,2,4] - end - end - - @testset "InflatedTensorMapping of InflatedTensorMapping" begin - A = ScalingOperator(2.0,(2,3)) - itm = InflatedTensorMapping(I(3,2), A, I(4)) - @test InflatedTensorMapping(I(4), itm, I(2)) == InflatedTensorMapping(I(4,3,2), A, I(4,2)) - @test InflatedTensorMapping(itm, I(2)) == InflatedTensorMapping(I(3,2), A, I(4,2)) - @test InflatedTensorMapping(I(4), itm) == InflatedTensorMapping(I(4,3,2), A, I(4)) - - @test InflatedTensorMapping(I(2), I(2), I(2)) isa InflatedTensorMapping # The constructor should always return its type. - end -end - -@testset "split_index" begin - @test LazyTensors.split_index(Val(2),Val(1),Val(2),Val(2),1,2,3,4,5,6) == ((1,2,:,5,6),(3,4)) - @test LazyTensors.split_index(Val(2),Val(3),Val(2),Val(2),1,2,3,4,5,6) == ((1,2,:,:,:,5,6),(3,4)) - @test LazyTensors.split_index(Val(3),Val(1),Val(1),Val(2),1,2,3,4,5,6) == ((1,2,3,:,5,6),(4,)) - @test LazyTensors.split_index(Val(3),Val(2),Val(1),Val(2),1,2,3,4,5,6) == ((1,2,3,:,:,5,6),(4,)) - @test LazyTensors.split_index(Val(1),Val(1),Val(2),Val(3),1,2,3,4,5,6) == ((1,:,4,5,6),(2,3)) - @test LazyTensors.split_index(Val(1),Val(2),Val(2),Val(3),1,2,3,4,5,6) == ((1,:,:,4,5,6),(2,3)) - - @test LazyTensors.split_index(Val(0),Val(1),Val(3),Val(3),1,2,3,4,5,6) == ((:,4,5,6),(1,2,3)) - @test LazyTensors.split_index(Val(3),Val(1),Val(3),Val(0),1,2,3,4,5,6) == ((1,2,3,:),(4,5,6)) - - @inferred LazyTensors.split_index(Val(2),Val(3),Val(2),Val(2),1,2,3,2,2,4) -end - -@testset "slice_tuple" begin - @test LazyTensors.slice_tuple((1,2,3),Val(1), Val(3)) == (1,2,3) - @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(2), Val(5)) == (2,3,4,5) - @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(1), Val(3)) == (1,2,3) - @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(4), Val(6)) == (4,5,6) -end - -@testset "split_tuple" begin - @testset "2 parts" begin - @test LazyTensors.split_tuple((),Val(0)) == ((),()) - @test LazyTensors.split_tuple((1,),Val(0)) == ((),(1,)) - @test LazyTensors.split_tuple((1,),Val(1)) == ((1,),()) - - @test LazyTensors.split_tuple((1,2,3,4),Val(0)) == ((),(1,2,3,4)) - @test LazyTensors.split_tuple((1,2,3,4),Val(1)) == ((1,),(2,3,4)) - @test LazyTensors.split_tuple((1,2,3,4),Val(2)) == ((1,2),(3,4)) - @test LazyTensors.split_tuple((1,2,3,4),Val(3)) == ((1,2,3),(4,)) - @test LazyTensors.split_tuple((1,2,3,4),Val(4)) == ((1,2,3,4),()) - - @test LazyTensors.split_tuple((1,2,true,4),Val(3)) == ((1,2,true),(4,)) - - @inferred LazyTensors.split_tuple((1,2,3,4),Val(3)) - @inferred LazyTensors.split_tuple((1,2,true,4),Val(3)) - end - - @testset "3 parts" begin - @test LazyTensors.split_tuple((),Val(0),Val(0)) == ((),(),()) - @test LazyTensors.split_tuple((1,2,3),Val(1), Val(1)) == ((1,),(2,),(3,)) - @test LazyTensors.split_tuple((1,true,3),Val(1), Val(1)) == ((1,),(true,),(3,)) - - @test LazyTensors.split_tuple((1,2,3,4,5,6),Val(1),Val(2)) == ((1,),(2,3),(4,5,6)) - @test LazyTensors.split_tuple((1,2,3,4,5,6),Val(3),Val(2)) == ((1,2,3),(4,5),(6,)) - - @inferred LazyTensors.split_tuple((1,2,3,4,5,6),Val(3),Val(2)) - @inferred LazyTensors.split_tuple((1,true,3),Val(1), Val(1)) - end -end - -@testset "flatten_tuple" begin - @test LazyTensors.flatten_tuple((1,)) == (1,) - @test LazyTensors.flatten_tuple((1,2,3,4,5,6)) == (1,2,3,4,5,6) - @test LazyTensors.flatten_tuple((1,2,(3,4),5,6)) == (1,2,3,4,5,6) - @test LazyTensors.flatten_tuple((1,2,(3,(4,5)),6)) == (1,2,3,4,5,6) - @test LazyTensors.flatten_tuple(((1,2),(3,4),(5,),6)) == (1,2,3,4,5,6) -end - - -@testset "LazyOuterProduct" begin - struct ScalingOperator{T,D} <: TensorMapping{T,D,D} - λ::T - size::NTuple{D,Int} - end - - LazyTensors.apply(m::ScalingOperator{T,D}, v, I::Vararg{Any,D}) where {T,D} = m.λ*v[I...] - LazyTensors.range_size(m::ScalingOperator) = m.size - LazyTensors.domain_size(m::ScalingOperator) = m.size - - A = ScalingOperator(2.0, (5,)) - B = ScalingOperator(3.0, (3,)) - C = ScalingOperator(5.0, (3,2)) - - AB = LazyOuterProduct(A,B) - @test AB isa TensorMapping{T,2,2} where T - @test range_size(AB) == (5,3) - @test domain_size(AB) == (5,3) - - v = rand(range_size(AB)...) - @test AB*v == 6*v - - ABC = LazyOuterProduct(A,B,C) - - @test ABC isa TensorMapping{T,4,4} where T - @test range_size(ABC) == (5,3,3,2) - @test domain_size(ABC) == (5,3,3,2) - - @test A⊗B == AB - @test A⊗B⊗C == ABC - - A = rand(3,2) - B = rand(2,4,3) - - v₁ = rand(2,4,3) - v₂ = rand(4,3,2) - - à = LazyLinearMap(A,(1,),(2,)) - B̃ = LazyLinearMap(B,(1,),(2,3)) - - ÃB̃ = LazyOuterProduct(Ã,B̃) - @tullio ABv[i,k] := A[i,j]*B[k,l,m]*v₁[j,l,m] - @test ÃB̃*v₁ ≈ ABv - - B̃à = LazyOuterProduct(B̃,Ã) - @tullio BAv[k,i] := A[i,j]*B[k,l,m]*v₂[l,m,j] - @test B̃Ã*v₂ ≈ BAv - - @testset "Indentity mapping arguments" begin - @test LazyOuterProduct(IdentityMapping(3,2), IdentityMapping(1,2)) == IdentityMapping(3,2,1,2) - - à = LazyLinearMap(A,(1,),(2,)) - @test LazyOuterProduct(IdentityMapping(3,2), Ã) == InflatedTensorMapping(IdentityMapping(3,2),Ã) - @test LazyOuterProduct(Ã, IdentityMapping(3,2)) == InflatedTensorMapping(Ã,IdentityMapping(3,2)) - - I1 = IdentityMapping(3,2) - I2 = IdentityMapping(4) - @test I1⊗Ã⊗I2 == InflatedTensorMapping(I1, Ã, I2) - end - -end - -end
--- a/test/testRegionIndices.jl Wed Jul 14 23:40:10 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -using Sbplib.RegionIndices -using Test - -@testset "RegionIndices" begin - @test_broken false -end
--- a/test/testSbpOperators.jl Wed Jul 14 23:40:10 2021 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,842 +0,0 @@ -using Test -using Sbplib.SbpOperators -using Sbplib.Grids -using Sbplib.RegionIndices -using Sbplib.LazyTensors -using LinearAlgebra -using TOML - -import Sbplib.SbpOperators.Stencil -import Sbplib.SbpOperators.VolumeOperator -import Sbplib.SbpOperators.volume_operator -import Sbplib.SbpOperators.BoundaryOperator -import Sbplib.SbpOperators.boundary_operator -import Sbplib.SbpOperators.even -import Sbplib.SbpOperators.odd - - -@testset "SbpOperators" begin - -@testset "Stencil" begin - s = Stencil((-2,2), (1.,2.,2.,3.,4.)) - @test s isa Stencil{Float64, 5} - - @test eltype(s) == Float64 - @test SbpOperators.scale(s, 2) == Stencil((-2,2), (2.,4.,4.,6.,8.)) - - @test Stencil(1,2,3,4; center=1) == Stencil((0, 3),(1,2,3,4)) - @test Stencil(1,2,3,4; center=2) == Stencil((-1, 2),(1,2,3,4)) - @test Stencil(1,2,3,4; center=4) == Stencil((-3, 0),(1,2,3,4)) - - @test CenteredStencil(1,2,3,4,5) == Stencil((-2, 2), (1,2,3,4,5)) - @test_throws ArgumentError CenteredStencil(1,2,3,4) -end - -@testset "parse_rational" begin - @test SbpOperators.parse_rational("1") isa Rational - @test SbpOperators.parse_rational("1") == 1//1 - @test SbpOperators.parse_rational("1/2") isa Rational - @test SbpOperators.parse_rational("1/2") == 1//2 - @test SbpOperators.parse_rational("37/13") isa Rational - @test SbpOperators.parse_rational("37/13") == 37//13 -end - -@testset "readoperator" begin - toml_str = """ - [meta] - authors = "Ken Mattson" - description = "Standard operators for equidistant grids" - type = "equidistant" - cite = "A paper a long time ago in a galaxy far far away." - - [[stencil_set]] - - order = 2 - test = 2 - - H.inner = ["1"] - H.closure = ["1/2"] - - D1.inner_stencil = ["-1/2", "0", "1/2"] - D1.closure_stencils = [ - {s = ["-1", "1"], c = 1}, - ] - - D2.inner_stencil = ["1", "-2", "1"] - D2.closure_stencils = [ - {s = ["1", "-2", "1"], c = 1}, - ] - - e.closure = ["1"] - d1.closure = {s = ["-3/2", "2", "-1/2"], c = 1} - - [[stencil_set]] - - order = 4 - test = 1 - H.inner = ["1"] - H.closure = ["17/48", "59/48", "43/48", "49/48"] - - D2.inner_stencil = ["-1/12","4/3","-5/2","4/3","-1/12"] - D2.closure_stencils = [ - {s = [ "2", "-5", "4", "-1", "0", "0"], c = 1}, - {s = [ "1", "-2", "1", "0", "0", "0"], c = 2}, - {s = [ "-4/43", "59/43", "-110/43", "59/43", "-4/43", "0"], c = 3}, - {s = [ "-1/49", "0", "59/49", "-118/49", "64/49", "-4/49"], c = 4}, - ] - - e.closure = ["1"] - d1.closure = {s = ["-11/6", "3", "-3/2", "1/3"], c = 1} - - [[stencil_set]] - order = 4 - test = 2 - - H.closure = ["-1/49", "0", "59/49", "-118/49", "64/49", "-4/49"] - """ - - parsed_toml = TOML.parse(toml_str) - - @testset "get_stencil_set" begin - @test get_stencil_set(parsed_toml; order = 2) isa Dict - @test get_stencil_set(parsed_toml; order = 2) == parsed_toml["stencil_set"][1] - @test get_stencil_set(parsed_toml; test = 1) == parsed_toml["stencil_set"][2] - @test get_stencil_set(parsed_toml; order = 4, test = 2) == parsed_toml["stencil_set"][3] - - @test_throws ArgumentError get_stencil_set(parsed_toml; test = 2) - @test_throws ArgumentError get_stencil_set(parsed_toml; order = 4) - end - - @testset "parse_stencil" begin - toml = """ - s1 = ["-1/12","4/3","-5/2","4/3","-1/12"] - s2 = {s = ["2", "-5", "4", "-1", "0", "0"], c = 1} - s3 = {s = ["1", "-2", "1", "0", "0", "0"], c = 2} - s4 = "not a stencil" - s5 = [-1, 4, 3] - s6 = {k = ["1", "-2", "1", "0", "0", "0"], c = 2} - s7 = {s = [-1, 4, 3], c = 2} - s8 = {s = ["1", "-2", "1", "0", "0", "0"], c = [2,2]} - """ - - @test parse_stencil(TOML.parse(toml)["s1"]) == CenteredStencil(-1/12, 4/3, -5/2, 4/3, -1/12) - @test parse_stencil(TOML.parse(toml)["s2"]) == Stencil(2., -5., 4., -1., 0., 0.; center=1) - @test parse_stencil(TOML.parse(toml)["s3"]) == Stencil(1., -2., 1., 0., 0., 0.; center=2) - - @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s4"]) - @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s5"]) - @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s6"]) - @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s7"]) - @test_throws ArgumentError parse_stencil(TOML.parse(toml)["s8"]) - - stencil_set = get_stencil_set(parsed_toml; order = 4, test = 1) - - @test parse_stencil.(stencil_set["D2"]["closure_stencils"]) == [ - Stencil( 2., -5., 4., -1., 0., 0.; center=1), - Stencil( 1., -2., 1., 0., 0., 0.; center=2), - Stencil(-4/43, 59/43, -110/43, 59/43, -4/43, 0.; center=3), - Stencil(-1/49, 0., 59/49, -118/49, 64/49, -4/49; center=4), - ] - end -end - -@testset "VolumeOperator" begin - inner_stencil = CenteredStencil(1/4, 2/4, 1/4) - closure_stencils = (Stencil(1/2, 1/2; center=1), Stencil(0.,1.; center=2)) - g_1D = EquidistantGrid(11,0.,1.) - g_2D = EquidistantGrid((11,12),(0.,0.),(1.,1.)) - g_3D = EquidistantGrid((11,12,10),(0.,0.,0.),(1.,1.,1.)) - @testset "Constructors" begin - @testset "1D" begin - op = VolumeOperator(inner_stencil,closure_stencils,(11,),even) - @test op == VolumeOperator(g_1D,inner_stencil,closure_stencils,even) - @test op == volume_operator(g_1D,inner_stencil,closure_stencils,even,1) - @test op isa TensorMapping{T,1,1} where T - end - @testset "2D" begin - op_x = volume_operator(g_2D,inner_stencil,closure_stencils,even,1) - op_y = volume_operator(g_2D,inner_stencil,closure_stencils,even,2) - Ix = IdentityMapping{Float64}((11,)) - Iy = IdentityMapping{Float64}((12,)) - @test op_x == VolumeOperator(inner_stencil,closure_stencils,(11,),even)⊗Iy - @test op_y == Ix⊗VolumeOperator(inner_stencil,closure_stencils,(12,),even) - @test op_x isa TensorMapping{T,2,2} where T - @test op_y isa TensorMapping{T,2,2} where T - end - @testset "3D" begin - op_x = volume_operator(g_3D,inner_stencil,closure_stencils,even,1) - op_y = volume_operator(g_3D,inner_stencil,closure_stencils,even,2) - op_z = volume_operator(g_3D,inner_stencil,closure_stencils,even,3) - Ix = IdentityMapping{Float64}((11,)) - Iy = IdentityMapping{Float64}((12,)) - Iz = IdentityMapping{Float64}((10,)) - @test op_x == VolumeOperator(inner_stencil,closure_stencils,(11,),even)⊗Iy⊗Iz - @test op_y == Ix⊗VolumeOperator(inner_stencil,closure_stencils,(12,),even)⊗Iz - @test op_z == Ix⊗Iy⊗VolumeOperator(inner_stencil,closure_stencils,(10,),even) - @test op_x isa TensorMapping{T,3,3} where T - @test op_y isa TensorMapping{T,3,3} where T - @test op_z isa TensorMapping{T,3,3} where T - end - end - - @testset "Sizes" begin - @testset "1D" begin - op = volume_operator(g_1D,inner_stencil,closure_stencils,even,1) - @test range_size(op) == domain_size(op) == size(g_1D) - end - - @testset "2D" begin - op_x = volume_operator(g_2D,inner_stencil,closure_stencils,even,1) - op_y = volume_operator(g_2D,inner_stencil,closure_stencils,even,2) - @test range_size(op_y) == domain_size(op_y) == - range_size(op_x) == domain_size(op_x) == size(g_2D) - end - @testset "3D" begin - op_x = volume_operator(g_3D,inner_stencil,closure_stencils,even,1) - op_y = volume_operator(g_3D,inner_stencil,closure_stencils,even,2) - op_z = volume_operator(g_3D,inner_stencil,closure_stencils,even,3) - @test range_size(op_z) == domain_size(op_z) == - range_size(op_y) == domain_size(op_y) == - range_size(op_x) == domain_size(op_x) == size(g_3D) - end - end - - op_x = volume_operator(g_2D,inner_stencil,closure_stencils,even,1) - op_y = volume_operator(g_2D,inner_stencil,closure_stencils,odd,2) - v = zeros(size(g_2D)) - Nx = size(g_2D)[1] - Ny = size(g_2D)[2] - for i = 1:Nx - v[i,:] .= i - end - rx = copy(v) - rx[1,:] .= 1.5 - rx[Nx,:] .= (2*Nx-1)/2 - ry = copy(v) - ry[:,Ny-1:Ny] = -v[:,Ny-1:Ny] - - @testset "Application" begin - @test op_x*v ≈ rx rtol = 1e-14 - @test op_y*v ≈ ry rtol = 1e-14 - end - - @testset "Regions" begin - @test (op_x*v)[Index(1,Lower),Index(3,Interior)] ≈ rx[1,3] rtol = 1e-14 - @test (op_x*v)[Index(2,Lower),Index(3,Interior)] ≈ rx[2,3] rtol = 1e-14 - @test (op_x*v)[Index(6,Interior),Index(3,Interior)] ≈ rx[6,3] rtol = 1e-14 - @test (op_x*v)[Index(10,Upper),Index(3,Interior)] ≈ rx[10,3] rtol = 1e-14 - @test (op_x*v)[Index(11,Upper),Index(3,Interior)] ≈ rx[11,3] rtol = 1e-14 - - @test_throws BoundsError (op_x*v)[Index(3,Lower),Index(3,Interior)] - @test_throws BoundsError (op_x*v)[Index(9,Upper),Index(3,Interior)] - - @test (op_y*v)[Index(3,Interior),Index(1,Lower)] ≈ ry[3,1] rtol = 1e-14 - @test (op_y*v)[Index(3,Interior),Index(2,Lower)] ≈ ry[3,2] rtol = 1e-14 - @test (op_y*v)[Index(3,Interior),Index(6,Interior)] ≈ ry[3,6] rtol = 1e-14 - @test (op_y*v)[Index(3,Interior),Index(11,Upper)] ≈ ry[3,11] rtol = 1e-14 - @test (op_y*v)[Index(3,Interior),Index(12,Upper)] ≈ ry[3,12] rtol = 1e-14 - - @test_throws BoundsError (op_y*v)[Index(3,Interior),Index(10,Upper)] - @test_throws BoundsError (op_y*v)[Index(3,Interior),Index(3,Lower)] - end - - @testset "Inferred" begin - @inferred apply(op_x, v,1,1) - @inferred apply(op_x, v, Index(1,Lower),Index(1,Lower)) - @inferred apply(op_x, v, Index(6,Interior),Index(1,Lower)) - @inferred apply(op_x, v, Index(11,Upper),Index(1,Lower)) - - @inferred apply(op_y, v,1,1) - @inferred apply(op_y, v, Index(1,Lower),Index(1,Lower)) - @inferred apply(op_y, v, Index(1,Lower),Index(6,Interior)) - @inferred apply(op_y, v, Index(1,Lower),Index(11,Upper)) - end - -end - -@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ₓₓ = SecondDerivative(g_1D,op.innerStencil,op.closureStencils) - @test Dₓₓ == SecondDerivative(g_1D,op.innerStencil,op.closureStencils,1) - @test Dₓₓ isa VolumeOperator - end - @testset "2D" begin - Dₓₓ = SecondDerivative(g_2D,op.innerStencil,op.closureStencils,1) - D2 = SecondDerivative(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ₓₓ = SecondDerivative(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ₓₓ = SecondDerivative(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 = SecondDerivative(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 = SecondDerivative(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 - -@testset "Laplace" begin - g_1D = EquidistantGrid(101, 0.0, 1.) - g_3D = EquidistantGrid((51,101,52), (0.0, -1.0, 0.0), (1., 1., 1.)) - @testset "Constructors" 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 == SecondDerivative(g_1D, op.innerStencil, op.closureStencils) - @test L isa TensorMapping{T,1,1} where T - end - @testset "3D" begin - L = Laplace(g_3D, op.innerStencil, op.closureStencils) - @test L isa TensorMapping{T,3,3} where T - Dxx = SecondDerivative(g_3D, op.innerStencil, op.closureStencils,1) - Dyy = SecondDerivative(g_3D, op.innerStencil, op.closureStencils,2) - Dzz = SecondDerivative(g_3D, op.innerStencil, op.closureStencils,3) - @test L == Dxx + Dyy + Dzz - end - end - - # Exact differentiation is measured point-wise. In other cases - # the error is measured in the l2-norm. - @testset "Accuracy" begin - l2(v) = sqrt(prod(spacing(g_3D))*sum(v.^2)); - polynomials = () - maxOrder = 4; - for i = 0:maxOrder-1 - f_i(x,y,z) = 1/factorial(i)*(y^i + x^i + z^i) - polynomials = (polynomials...,evalOn(g_3D,f_i)) - end - v = evalOn(g_3D, (x,y,z) -> sin(x) + cos(y) + exp(z)) - Δv = evalOn(g_3D,(x,y,z) -> -sin(x) - cos(y) + exp(z)) - - # 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) - L = Laplace(g_3D,op.innerStencil,op.closureStencils) - @test L*polynomials[1] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 - @test L*polynomials[2] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 - @test L*polynomials[3] ≈ polynomials[1] atol = 5e-9 - @test L*v ≈ Δv 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) - L = Laplace(g_3D,op.innerStencil,op.closureStencils) - # NOTE: high tolerances for checking the "exact" differentiation - # due to accumulation of round-off errors/cancellation errors? - @test L*polynomials[1] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 - @test L*polynomials[2] ≈ zeros(Float64, size(g_3D)...) atol = 5e-9 - @test L*polynomials[3] ≈ polynomials[1] atol = 5e-9 - @test L*polynomials[4] ≈ polynomials[2] atol = 5e-9 - @test L*v ≈ Δv rtol = 5e-4 norm = l2 - end - end -end - -@testset "DiagonalQuadrature" begin - Lx = π/2. - Ly = Float64(π) - g_1D = EquidistantGrid(77, 0.0, Lx) - g_2D = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly)) - integral(H,v) = sum(H*v) - @testset "Constructors" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - @testset "1D" begin - H = DiagonalQuadrature(g_1D,op.quadratureClosure) - inner_stencil = CenteredStencil(1.) - @test H == Quadrature(g_1D,inner_stencil,op.quadratureClosure) - @test H isa TensorMapping{T,1,1} where T - end - @testset "1D" begin - H = DiagonalQuadrature(g_2D,op.quadratureClosure) - H_x = DiagonalQuadrature(restrict(g_2D,1),op.quadratureClosure) - H_y = DiagonalQuadrature(restrict(g_2D,2),op.quadratureClosure) - @test H == H_x⊗H_y - @test H isa TensorMapping{T,2,2} where T - end - end - - @testset "Sizes" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - @testset "1D" begin - H = DiagonalQuadrature(g_1D,op.quadratureClosure) - @test domain_size(H) == size(g_1D) - @test range_size(H) == size(g_1D) - end - @testset "2D" begin - H = DiagonalQuadrature(g_2D,op.quadratureClosure) - @test domain_size(H) == size(g_2D) - @test range_size(H) == size(g_2D) - end - end - - @testset "Accuracy" begin - @testset "1D" begin - v = () - for i = 0:4 - f_i(x) = 1/factorial(i)*x^i - v = (v...,evalOn(g_1D,f_i)) - end - u = evalOn(g_1D,x->sin(x)) - - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - H = DiagonalQuadrature(g_1D,op.quadratureClosure) - for i = 1:2 - @test integral(H,v[i]) ≈ v[i+1][end] - v[i+1][1] rtol = 1e-14 - end - @test integral(H,u) ≈ 1. rtol = 1e-4 - end - - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - H = DiagonalQuadrature(g_1D,op.quadratureClosure) - for i = 1:4 - @test integral(H,v[i]) ≈ v[i+1][end] - v[i+1][1] rtol = 1e-14 - end - @test integral(H,u) ≈ 1. rtol = 1e-8 - end - end - - @testset "2D" begin - b = 2.1 - v = b*ones(Float64, size(g_2D)) - u = evalOn(g_2D,(x,y)->sin(x)+cos(y)) - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - H = DiagonalQuadrature(g_2D,op.quadratureClosure) - @test integral(H,v) ≈ b*Lx*Ly rtol = 1e-13 - @test integral(H,u) ≈ π rtol = 1e-4 - end - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - H = DiagonalQuadrature(g_2D,op.quadratureClosure) - @test integral(H,v) ≈ b*Lx*Ly rtol = 1e-13 - @test integral(H,u) ≈ π rtol = 1e-8 - end - end - end -end - -@testset "InverseDiagonalQuadrature" begin - Lx = π/2. - Ly = Float64(π) - g_1D = EquidistantGrid(77, 0.0, Lx) - g_2D = EquidistantGrid((77,66), (0.0, 0.0), (Lx,Ly)) - @testset "Constructors" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - @testset "1D" begin - Hi = InverseDiagonalQuadrature(g_1D, op.quadratureClosure); - inner_stencil = CenteredStencil(1.) - closures = () - for i = 1:length(op.quadratureClosure) - closures = (closures...,Stencil(op.quadratureClosure[i].range,1.0./op.quadratureClosure[i].weights)) - end - @test Hi == InverseQuadrature(g_1D,inner_stencil,closures) - @test Hi isa TensorMapping{T,1,1} where T - end - @testset "2D" begin - Hi = InverseDiagonalQuadrature(g_2D,op.quadratureClosure) - Hi_x = InverseDiagonalQuadrature(restrict(g_2D,1),op.quadratureClosure) - Hi_y = InverseDiagonalQuadrature(restrict(g_2D,2),op.quadratureClosure) - @test Hi == Hi_x⊗Hi_y - @test Hi isa TensorMapping{T,2,2} where T - end - end - - @testset "Sizes" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - @testset "1D" begin - Hi = InverseDiagonalQuadrature(g_1D,op.quadratureClosure) - @test domain_size(Hi) == size(g_1D) - @test range_size(Hi) == size(g_1D) - end - @testset "2D" begin - Hi = InverseDiagonalQuadrature(g_2D,op.quadratureClosure) - @test domain_size(Hi) == size(g_2D) - @test range_size(Hi) == size(g_2D) - end - end - - @testset "Accuracy" begin - @testset "1D" begin - v = evalOn(g_1D,x->sin(x)) - u = evalOn(g_1D,x->x^3-x^2+1) - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - H = DiagonalQuadrature(g_1D,op.quadratureClosure) - Hi = InverseDiagonalQuadrature(g_1D,op.quadratureClosure) - @test Hi*H*v ≈ v rtol = 1e-15 - @test Hi*H*u ≈ u rtol = 1e-15 - end - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - H = DiagonalQuadrature(g_1D,op.quadratureClosure) - Hi = InverseDiagonalQuadrature(g_1D,op.quadratureClosure) - @test Hi*H*v ≈ v rtol = 1e-15 - @test Hi*H*u ≈ u rtol = 1e-15 - end - end - @testset "2D" begin - v = evalOn(g_2D,(x,y)->sin(x)+cos(y)) - u = evalOn(g_2D,(x,y)->x*y + x^5 - sqrt(y)) - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - H = DiagonalQuadrature(g_2D,op.quadratureClosure) - Hi = InverseDiagonalQuadrature(g_2D,op.quadratureClosure) - @test Hi*H*v ≈ v rtol = 1e-15 - @test Hi*H*u ≈ u rtol = 1e-15 - end - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - H = DiagonalQuadrature(g_2D,op.quadratureClosure) - Hi = InverseDiagonalQuadrature(g_2D,op.quadratureClosure) - @test Hi*H*v ≈ v rtol = 1e-15 - @test Hi*H*u ≈ u rtol = 1e-15 - end - end - end -end - -@testset "BoundaryOperator" begin - closure_stencil = Stencil((0,2), (2.,1.,3.)) - g_1D = EquidistantGrid(11, 0.0, 1.0) - g_2D = EquidistantGrid((11,15), (0.0, 0.0), (1.0,1.0)) - - @testset "Constructors" begin - @testset "1D" begin - op_l = BoundaryOperator{Lower}(closure_stencil,size(g_1D)[1]) - @test op_l == BoundaryOperator(g_1D,closure_stencil,Lower()) - @test op_l == boundary_operator(g_1D,closure_stencil,CartesianBoundary{1,Lower}()) - @test op_l isa TensorMapping{T,0,1} where T - - op_r = BoundaryOperator{Upper}(closure_stencil,size(g_1D)[1]) - @test op_r == BoundaryRestriction(g_1D,closure_stencil,Upper()) - @test op_r == boundary_operator(g_1D,closure_stencil,CartesianBoundary{1,Upper}()) - @test op_r isa TensorMapping{T,0,1} where T - end - - @testset "2D" begin - e_w = boundary_operator(g_2D,closure_stencil,CartesianBoundary{1,Upper}()) - @test e_w isa InflatedTensorMapping - @test e_w isa TensorMapping{T,1,2} where T - end - end - - op_l = boundary_operator(g_1D, closure_stencil, CartesianBoundary{1,Lower}()) - op_r = boundary_operator(g_1D, closure_stencil, CartesianBoundary{1,Upper}()) - - op_w = boundary_operator(g_2D, closure_stencil, CartesianBoundary{1,Lower}()) - op_e = boundary_operator(g_2D, closure_stencil, CartesianBoundary{1,Upper}()) - op_s = boundary_operator(g_2D, closure_stencil, CartesianBoundary{2,Lower}()) - op_n = boundary_operator(g_2D, closure_stencil, CartesianBoundary{2,Upper}()) - - @testset "Sizes" begin - @testset "1D" begin - @test domain_size(op_l) == (11,) - @test domain_size(op_r) == (11,) - - @test range_size(op_l) == () - @test range_size(op_r) == () - end - - @testset "2D" begin - @test domain_size(op_w) == (11,15) - @test domain_size(op_e) == (11,15) - @test domain_size(op_s) == (11,15) - @test domain_size(op_n) == (11,15) - - @test range_size(op_w) == (15,) - @test range_size(op_e) == (15,) - @test range_size(op_s) == (11,) - @test range_size(op_n) == (11,) - end - end - - @testset "Application" begin - @testset "1D" begin - v = evalOn(g_1D,x->1+x^2) - u = fill(3.124) - @test (op_l*v)[] == 2*v[1] + v[2] + 3*v[3] - @test (op_r*v)[] == 2*v[end] + v[end-1] + 3*v[end-2] - @test (op_r*v)[1] == 2*v[end] + v[end-1] + 3*v[end-2] - @test op_l'*u == [2*u[]; u[]; 3*u[]; zeros(8)] - @test op_r'*u == [zeros(8); 3*u[]; u[]; 2*u[]] - end - - @testset "2D" begin - v = rand(size(g_2D)...) - u = fill(3.124) - @test op_w*v ≈ 2*v[1,:] + v[2,:] + 3*v[3,:] rtol = 1e-14 - @test op_e*v ≈ 2*v[end,:] + v[end-1,:] + 3*v[end-2,:] rtol = 1e-14 - @test op_s*v ≈ 2*v[:,1] + v[:,2] + 3*v[:,3] rtol = 1e-14 - @test op_n*v ≈ 2*v[:,end] + v[:,end-1] + 3*v[:,end-2] rtol = 1e-14 - - - g_x = rand(size(g_2D)[1]) - g_y = rand(size(g_2D)[2]) - - G_w = zeros(Float64, size(g_2D)...) - G_w[1,:] = 2*g_y - G_w[2,:] = g_y - G_w[3,:] = 3*g_y - - G_e = zeros(Float64, size(g_2D)...) - G_e[end,:] = 2*g_y - G_e[end-1,:] = g_y - G_e[end-2,:] = 3*g_y - - G_s = zeros(Float64, size(g_2D)...) - G_s[:,1] = 2*g_x - G_s[:,2] = g_x - G_s[:,3] = 3*g_x - - G_n = zeros(Float64, size(g_2D)...) - G_n[:,end] = 2*g_x - G_n[:,end-1] = g_x - G_n[:,end-2] = 3*g_x - - @test op_w'*g_y == G_w - @test op_e'*g_y == G_e - @test op_s'*g_x == G_s - @test op_n'*g_x == G_n - end - - @testset "Regions" begin - u = fill(3.124) - @test (op_l'*u)[Index(1,Lower)] == 2*u[] - @test (op_l'*u)[Index(2,Lower)] == u[] - @test (op_l'*u)[Index(6,Interior)] == 0 - @test (op_l'*u)[Index(10,Upper)] == 0 - @test (op_l'*u)[Index(11,Upper)] == 0 - - @test (op_r'*u)[Index(1,Lower)] == 0 - @test (op_r'*u)[Index(2,Lower)] == 0 - @test (op_r'*u)[Index(6,Interior)] == 0 - @test (op_r'*u)[Index(10,Upper)] == u[] - @test (op_r'*u)[Index(11,Upper)] == 2*u[] - end - end - - @testset "Inferred" begin - v = ones(Float64, 11) - u = fill(1.) - - @inferred apply(op_l, v) - @inferred apply(op_r, v) - - @inferred apply_transpose(op_l, u, 4) - @inferred apply_transpose(op_l, u, Index(1,Lower)) - @inferred apply_transpose(op_l, u, Index(2,Lower)) - @inferred apply_transpose(op_l, u, Index(6,Interior)) - @inferred apply_transpose(op_l, u, Index(10,Upper)) - @inferred apply_transpose(op_l, u, Index(11,Upper)) - - @inferred apply_transpose(op_r, u, 4) - @inferred apply_transpose(op_r, u, Index(1,Lower)) - @inferred apply_transpose(op_r, u, Index(2,Lower)) - @inferred apply_transpose(op_r, u, Index(6,Interior)) - @inferred apply_transpose(op_r, u, Index(10,Upper)) - @inferred apply_transpose(op_r, u, Index(11,Upper)) - end - -end - -@testset "BoundaryRestriction" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - g_1D = EquidistantGrid(11, 0.0, 1.0) - g_2D = EquidistantGrid((11,15), (0.0, 0.0), (1.0,1.0)) - - @testset "Constructors" begin - @testset "1D" begin - e_l = BoundaryRestriction(g_1D,op.eClosure,Lower()) - @test e_l == BoundaryRestriction(g_1D,op.eClosure,CartesianBoundary{1,Lower}()) - @test e_l == BoundaryOperator(g_1D,op.eClosure,Lower()) - @test e_l isa BoundaryOperator{T,Lower} where T - @test e_l isa TensorMapping{T,0,1} where T - - e_r = BoundaryRestriction(g_1D,op.eClosure,Upper()) - @test e_r == BoundaryRestriction(g_1D,op.eClosure,CartesianBoundary{1,Upper}()) - @test e_r == BoundaryOperator(g_1D,op.eClosure,Upper()) - @test e_r isa BoundaryOperator{T,Upper} where T - @test e_r isa TensorMapping{T,0,1} where T - end - - @testset "2D" begin - e_w = BoundaryRestriction(g_2D,op.eClosure,CartesianBoundary{1,Upper}()) - @test e_w isa InflatedTensorMapping - @test e_w isa TensorMapping{T,1,2} where T - end - end - - @testset "Application" begin - @testset "1D" begin - e_l = BoundaryRestriction(g_1D, op.eClosure, CartesianBoundary{1,Lower}()) - e_r = BoundaryRestriction(g_1D, op.eClosure, CartesianBoundary{1,Upper}()) - - v = evalOn(g_1D,x->1+x^2) - u = fill(3.124) - - @test (e_l*v)[] == v[1] - @test (e_r*v)[] == v[end] - @test (e_r*v)[1] == v[end] - end - - @testset "2D" begin - e_w = BoundaryRestriction(g_2D, op.eClosure, CartesianBoundary{1,Lower}()) - e_e = BoundaryRestriction(g_2D, op.eClosure, CartesianBoundary{1,Upper}()) - e_s = BoundaryRestriction(g_2D, op.eClosure, CartesianBoundary{2,Lower}()) - e_n = BoundaryRestriction(g_2D, op.eClosure, CartesianBoundary{2,Upper}()) - - v = rand(11, 15) - u = fill(3.124) - - @test e_w*v == v[1,:] - @test e_e*v == v[end,:] - @test e_s*v == v[:,1] - @test e_n*v == v[:,end] - end - end -end - -@testset "NormalDerivative" begin - g_1D = EquidistantGrid(11, 0.0, 1.0) - g_2D = EquidistantGrid((11,12), (0.0, 0.0), (1.0,1.0)) - @testset "Constructors" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - @testset "1D" begin - d_l = NormalDerivative(g_1D, op.dClosure, Lower()) - @test d_l == NormalDerivative(g_1D, op.dClosure, CartesianBoundary{1,Lower}()) - @test d_l isa BoundaryOperator{T,Lower} where T - @test d_l isa TensorMapping{T,0,1} where T - end - @testset "2D" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - d_w = NormalDerivative(g_2D, op.dClosure, CartesianBoundary{1,Lower}()) - d_n = NormalDerivative(g_2D, op.dClosure, CartesianBoundary{2,Upper}()) - Ix = IdentityMapping{Float64}((size(g_2D)[1],)) - Iy = IdentityMapping{Float64}((size(g_2D)[2],)) - d_l = NormalDerivative(restrict(g_2D,1),op.dClosure,Lower()) - d_r = NormalDerivative(restrict(g_2D,2),op.dClosure,Upper()) - @test d_w == d_l⊗Iy - @test d_n == Ix⊗d_r - @test d_w isa TensorMapping{T,1,2} where T - @test d_n isa TensorMapping{T,1,2} where T - end - end - @testset "Accuracy" begin - v = evalOn(g_2D, (x,y)-> x^2 + (y-1)^2 + x*y) - v∂x = evalOn(g_2D, (x,y)-> 2*x + y) - v∂y = evalOn(g_2D, (x,y)-> 2*(y-1) + x) - # TODO: Test for higher order polynomials? - @testset "2nd order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=2) - d_w = NormalDerivative(g_2D, op.dClosure, CartesianBoundary{1,Lower}()) - d_e = NormalDerivative(g_2D, op.dClosure, CartesianBoundary{1,Upper}()) - d_s = NormalDerivative(g_2D, op.dClosure, CartesianBoundary{2,Lower}()) - d_n = NormalDerivative(g_2D, op.dClosure, CartesianBoundary{2,Upper}()) - - @test d_w*v ≈ v∂x[1,:] atol = 1e-13 - @test d_e*v ≈ -v∂x[end,:] atol = 1e-13 - @test d_s*v ≈ v∂y[:,1] atol = 1e-13 - @test d_n*v ≈ -v∂y[:,end] atol = 1e-13 - end - - @testset "4th order" begin - op = read_D2_operator(sbp_operators_path()*"standard_diagonal.toml"; order=4) - d_w = NormalDerivative(g_2D, op.dClosure, CartesianBoundary{1,Lower}()) - d_e = NormalDerivative(g_2D, op.dClosure, CartesianBoundary{1,Upper}()) - d_s = NormalDerivative(g_2D, op.dClosure, CartesianBoundary{2,Lower}()) - d_n = NormalDerivative(g_2D, op.dClosure, CartesianBoundary{2,Upper}()) - - @test d_w*v ≈ v∂x[1,:] atol = 1e-13 - @test d_e*v ≈ -v∂x[end,:] atol = 1e-13 - @test d_s*v ≈ v∂y[:,1] atol = 1e-13 - @test d_n*v ≈ -v∂y[:,end] atol = 1e-13 - end - end -end - -end