Mercurial > repos > public > sbplib_julia
changeset 1430:9fc3c1af33e5 feature/grids/curvilinear
Add testsets and a few tests
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 26 Jul 2023 21:00:44 +0200 |
parents | e87f3465b770 |
children | 6adf31ba6cfd |
files | src/Grids/curvilinear_grid.jl test/Grids/curvilinear_grid_test.jl |
diffstat | 2 files changed, 161 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/Grids/curvilinear_grid.jl Fri Aug 25 08:57:27 2023 +0200 +++ b/src/Grids/curvilinear_grid.jl Wed Jul 26 21:00:44 2023 +0200 @@ -1,1 +1,84 @@ -struct CurvilinearGrid end +struct CurvilinearGrid{T,D, GT<:Grid{<:Any,D}, CT<:AbstractArray{T,D}, JT<:AbstractArray{<:AbstractArray{<:Any, 2}, D}} <: Grid{T,D} + logicalgrid::GT + physicalcoordinates::CT + Jacobian::JT +end + + +# Indexing interface +Base.getindex(g::CurvilinearGrid, I...) = g.physicalcoordinates[I...] + +# function Base.getindex(g::TensorGrid, I...) +# szs = ndims.(g.grids) + +# Is = LazyTensors.split_tuple(I, szs) +# ps = map((g,I)->SVector(g[I...]), g.grids, Is) + +# return vcat(ps...) +# end + +# function Base.eachindex(g::TensorGrid) +# szs = LazyTensors.concatenate_tuples(size.(g.grids)...) +# return CartesianIndices(szs) +# end + +# # Iteration interface +# Base.iterate(g::TensorGrid) = iterate(Iterators.product(g.grids...)) |> _iterate_combine_coords +# Base.iterate(g::TensorGrid, state) = iterate(Iterators.product(g.grids...), state) |> _iterate_combine_coords +# _iterate_combine_coords(::Nothing) = nothing +# _iterate_combine_coords((next,state)) = combine_coordinates(next...), state + +# Base.IteratorSize(::Type{<:TensorGrid{<:Any, D}}) where D = Base.HasShape{D}() +# Base.eltype(::Type{<:TensorGrid{T}}) where T = T +# Base.length(g::TensorGrid) = sum(length, g.grids) +# Base.size(g::TensorGrid) = LazyTensors.concatenate_tuples(size.(g.grids)...) + + +# refine(g::TensorGrid, r::Int) = mapreduce(g->refine(g,r), TensorGrid, g.grids) +# coarsen(g::TensorGrid, r::Int) = mapreduce(g->coarsen(g,r), TensorGrid, g.grids) + +# """ +# TensorGridBoundary{N, BID} <: BoundaryIdentifier + +# A boundary identifier for a tensor grid. `N` Specifies which grid in the +# tensor product and `BID` which boundary on that grid. +# """ +# struct TensorGridBoundary{N, BID} <: BoundaryIdentifier end +# grid_id(::TensorGridBoundary{N, BID}) where {N, BID} = N +# boundary_id(::TensorGridBoundary{N, BID}) where {N, BID} = BID() + +# """ +# boundary_identifiers(g::TensorGrid) + +# Returns a tuple containing the boundary identifiers of `g`. +# """ +# function boundary_identifiers(g::TensorGrid) +# per_grid = map(eachindex(g.grids)) do i +# return map(bid -> TensorGridBoundary{i, typeof(bid)}(), boundary_identifiers(g.grids[i])) +# end +# return LazyTensors.concatenate_tuples(per_grid...) +# end + + +# """ +# boundary_grid(g::TensorGrid, id::TensorGridBoundary) + +# The grid for the boundary of `g` specified by `id`. +# """ +# function boundary_grid(g::TensorGrid, id::TensorGridBoundary) +# local_boundary_grid = boundary_grid(g.grids[grid_id(id)], boundary_id(id)) +# new_grids = Base.setindex(g.grids, local_boundary_grid, grid_id(id)) +# return TensorGrid(new_grids...) +# end + + + + + + + + +# Do we add a convenience function `curvilinear_grid`? It could help with +# creating the logical grid, evaluating functions and possibly calculating the +# entries in the jacobian. +
--- a/test/Grids/curvilinear_grid_test.jl Fri Aug 25 08:57:27 2023 +0200 +++ b/test/Grids/curvilinear_grid_test.jl Wed Jul 26 21:00:44 2023 +0200 @@ -1,6 +1,82 @@ using Sbplib.Grids using Test +using StaticArrays @testset "CurvilinearGrid" begin - @test CurvilinearGrid isa Any + g = equidistant_grid((10,10), (0,0), (1,1)) + x̄ = map(ξ̄ -> 2ξ̄, g) + J = map(ξ̄ -> @SArray(fill(2., 2, 2)), g) + + @test CurvilinearGrid(g, x̄, J) isa Grid{SVector{2, Float64},2} + + + @test_broken jacobian(cg) isa Array{<:AbstractVector} + @test_broken logicalgrid(cg) isa Grid + + + + @testset "Indexing Interface" begin + # cg = CurvilinearGrid(g, x̄, J) + # @test cg[1,1] == [0.0, 0.0] + # @test cg[4,2] == [3/9,1/9] + # @test cg[6,10] == [5/9, 1] + + # @test cg[begin, begin] == [0.0, 0.0] + # @test cg[end,end] == [1.0, 1.0] + # @test cg[begin,end] == [0., 1.] + + # @test eachindex(cg) == 1:101 + end + + @testset "Iterator interface" begin + # @test eltype(EquidistantGrid(0:10)) == Int + # @test eltype(EquidistantGrid(0:2:10)) == Int + # @test eltype(EquidistantGrid(0:0.1:10)) == Float64 + + # @test size(EquidistantGrid(0:10)) == (11,) + # @test size(EquidistantGrid(0:0.1:10)) == (101,) + + # @test collect(EquidistantGrid(0:0.1:0.5)) == [0.0, 0.1, 0.2, 0.3, 0.4, 0.5] + + # @test Base.IteratorSize(EquidistantGrid{Float64, StepRange{Float64}}) == Base.HasShape{1}() + end + + @testset "Base" begin + # @test ndims(EquidistantGrid(0:10)) == 1 + end + + @testset "boundary_identifiers" begin + # g = EquidistantGrid(0:0.1:10) + # @test boundary_identifiers(g) == (Lower(), Upper()) + # @inferred boundary_identifiers(g) + end + + @testset "boundary_grid" begin + # g = EquidistantGrid(0:0.1:1) + # @test boundary_grid(g, Lower()) == ZeroDimGrid(0.0) + # @test boundary_grid(g, Upper()) == ZeroDimGrid(1.0) + end + + @testset "refine" begin + # g = EquidistantGrid(0:0.1:1) + # @test refine(g, 1) == g + # @test refine(g, 2) == EquidistantGrid(0:0.05:1) + # @test refine(g, 3) == EquidistantGrid(0:(0.1/3):1) + end + + @testset "coarsen" begin + # g = EquidistantGrid(0:1:10) + # @test coarsen(g, 1) == g + # @test coarsen(g, 2) == EquidistantGrid(0:2:10) + + # g = EquidistantGrid(0:0.1:1) + # @test coarsen(g, 1) == g + # @test coarsen(g, 2) == EquidistantGrid(0:0.2:1) + + # g = EquidistantGrid(0:10) + # @test coarsen(g, 1) == EquidistantGrid(0:1:10) + # @test coarsen(g, 2) == EquidistantGrid(0:2:10) + + # @test_throws DomainError(3, "Size minus 1 must be divisible by the ratio.") coarsen(g, 3) + end end