Skip to content

Commit

Permalink
change subgrid, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentcp committed May 29, 2019
1 parent 1b41b1f commit 7eb29ef
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 20 deletions.
6 changes: 4 additions & 2 deletions src/intervalgrids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ size(grid::AbstractIntervalGrid) = (grid.n,)
string(g::AbstractIntervalGrid) = name(g) * " of length $(length(g)) on [$(support(g)))], ELT = $(eltype(g))"


instantiate(::Type{T}, n::Int, ::Type{ELT}) where {T<:AbstractIntervalGrid,ELT} = T(n,ELT(0),ELT(1))
instantiate(::Type{T}, n::Int, ::Type{ELT}) where {T<:AbstractIntervalGrid,ELT} = T(n,UnitInterval{ELT}())

"""
abstract type AbstractEquispacedGrid{T} <: AbstractIntervalGrid{T}
Expand Down Expand Up @@ -263,7 +263,7 @@ end

# Grids with flexible support
for GRID in (:PeriodicEquispacedGrid, :MidpointEquispacedGrid, :EquispacedGrid)
@eval $GRID(n::Int, d::AbstractInterval{T}) where {T} =
@eval $GRID(n::Int, d::AbstractInterval) =
$GRID(n, endpoints(d)...)
@eval similargrid(grid::$GRID, ::Type{T}, n::Int) where {T} =
$GRID{T}(n, map(T, endpoints(support(grid)))...)
Expand All @@ -279,6 +279,8 @@ end
for GRID in (:FourierGrid, :ChebyshevNodes, :ChebyshevExtremae, :ChebyshevUNodes, :LegendreNodes, :HermiteNodes)
@eval similargrid(g::$GRID, ::Type{T}, n::Int) where {T} = $GRID{T}(n)
@eval $GRID(n::Int) = $GRID{Float64}(n)
@eval $GRID(n::Int, d::AbstractInterval) =
$GRID(n, endpoints(d)...)
@eval $GRID(n::Int, a, b) = rescale($GRID{typeof((b-a)/n)}(n), a, b)
end

Expand Down
12 changes: 6 additions & 6 deletions src/subgrid/AbstractSubGrids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ function subgrid(grid::AbstractEquispacedGrid, domain::AbstractInterval)
idx_b = convert(Int, floor( (b-grid[1])/step(grid))+1 )
idx_a = max(idx_a, 1)
idx_b = min(idx_b, length(grid))
IndexSubGrid(grid, idx_a:idx_b)
IndexSubGrid(grid, idx_a:idx_b, domain)
end

function subgrid(grid::ScatteredGrid, domain::Domain)
mask = in.(grid, Ref(domain))
points = grid.points[mask]
ScatteredGrid(points)
end
# function subgrid(grid::ScatteredGrid, domain::Domain)
# mask = in.(grid, Ref(domain))
# points = grid.points[mask]
# ScatteredGrid(points)
# end

subgrid(grid::ProductGrid, domain::ProductDomain) =
ProductGrid(map(subgrid, elements(grid), elements(domain))...)
Expand Down
12 changes: 6 additions & 6 deletions src/subgrid/indexsubgrid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ underlying grid.
struct IndexSubGrid{G,I,T,N} <: AbstractSubGrid{T,N}
supergrid :: G
subindices :: I
domain :: Union{Nothing,Domain}

function IndexSubGrid{G,I,T,N}(supergrid::AbstractGrid{T,N}, subindices) where {G,I,T,N}
function IndexSubGrid{G,I,T,N}(supergrid::AbstractGrid{T,N}, subindices, domain=nothing) where {G,I,T,N}
@assert length(subindices) <= length(supergrid)

new(supergrid, subindices)
new(supergrid, subindices, domain)
end
end

IndexSubGrid(grid::AbstractGrid{T,N}, i) where {T,N} =
IndexSubGrid{typeof(grid),typeof(i),T,N}(grid, i)
IndexSubGrid(grid::AbstractGrid{T,N}, i, domain=nothing) where {T,N} =
IndexSubGrid{typeof(grid),typeof(i),T,N}(grid, i, domain)

name(g::IndexSubGrid) = "Index-based subgrid"

Expand All @@ -41,7 +41,7 @@ function mask(g::IndexSubGrid)
end


support(g::IndexSubGrid{G}) where G<:AbstractIntervalGrid = Interval(first(g), last(g))
support(g::IndexSubGrid{G}) where G<:AbstractIntervalGrid = g.domain == nothing ? Interval(first(g), last(g)) : g.domain



Expand Down
2 changes: 1 addition & 1 deletion src/subgrid/maskedsubgrid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ getindex(g::AbstractGrid, idx::BitArray) = MaskedGrid(g, idx)

function subgrid(grid::MaskedGrid, domain::Domain)
submask = in.(supergrid(grid), Ref(domain))
MaskedGrid(supergrid(grid), submask .& mask(grid))
MaskedGrid(supergrid(grid), submask .& mask(grid), domain)
# points = grid.points[mask]
# ScatteredGrid(points)
end
4 changes: 4 additions & 0 deletions src/test/test_grids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function test_interval_grid(grid::AbstractGrid, show_timings=false)
g2 = resize(grid, length(grid)<<1)
@test length(g2) == length(grid)<<1


g3 = resize(g1, length(grid)<<1)
@test length(g3) == length(grid)<<1

Expand All @@ -20,6 +21,9 @@ function test_interval_grid(grid::AbstractGrid, show_timings=false)
end

function test_generic_grid(grid; show_timings=false)
io = IOBuffer()
show(io, grid)
@test length(take!(io))>0
L = length(grid)
@test ndims(grid) == length(grid[1])

Expand Down
45 changes: 40 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function test_grids(T)
len = 21
a = -T(1.2)
b = T(3.5)
g1 = EquispacedGrid(len, a, b)
g2 = PeriodicEquispacedGrid(len, a-1, b+1)
g1 = EquispacedGrid(len, Interval(a, b))
g2 = PeriodicEquispacedGrid(len, Interval(a-1, b+1))
g3 = g1 × g2
g4 = g1 × g3

Expand Down Expand Up @@ -142,20 +142,23 @@ end

function test_laguerre(T)
grid = LaguerreNodes(10,rand(T))
@test infimum(support(LaguerreNodes(0.,rand(10)))) == 0
@test supremum(support(LaguerreNodes(0.,rand(10)))) == Inf
test_generic_grid(grid)
end

function test_hermite(T)
grid = HermiteNodes(10)
@test DomainSets.GeometricSpace{T}()== support(HermiteNodes(rand(T,10)))
test_generic_grid(grid)
end

function test_jacobi(T)
grid = JacobiNodes(10,rand(T),rand(T))
@test support(JacobiNodes(T(0),T(0),rand(T,10))) == ChebyshevInterval{T}()
test_generic_grid(grid)
@test JacobiNodes(10,zero(T),zero(T)) LegendreNodes(10)
@test JacobiNodes(10,T(1//2),T(1//2)) ChebyshevUNodes(10)

@test JacobiNodes(10,T(-1//2),T(-1//2)) ChebyshevTNodes(10)
end

Expand Down Expand Up @@ -192,13 +195,35 @@ for T in types
end
end

function test_generic_subgrid(grid, s)
@test support(grid) s
for x in grid
@test x support(grid)
end
for x in subindices(grid)
@test issubindex(x, grid)
end

cnt = 0
for i in eachindex(supergrid(grid))
if issubindex(i, grid)
cnt += 1
end
end
@test cnt == length(grid)
end

function test_subgrids()
delimit("Grid functionality")
@testset "SubGrids" begin
n = 20
grid1 = EquispacedGrid(n, -1.0, 1.0)
subgrid1 = MaskedGrid(grid1, -0.5..0.7)
test_generic_subgrid(subgrid1, -0.5..0.7)

subgrid2 = IndexSubGrid(grid1, 4:12)
subgrid3 = subgrid(grid1, -0.5..0.7)
test_generic_subgrid(subgrid3, -0.5..0.7)

@test subgrid1 == subgrid3
@test mask(subgrid1) == mask(subgrid3)
Expand All @@ -212,8 +237,12 @@ function test_subgrids()
ProductG = G1 × G2

C = UnitDisk()
circle_grid = MaskedGrid(ProductG, C)
@testset begin
refgrid = MaskedGrid(ProductG, C)
circle_grid = subgrid(ProductG, C)
@test circle_grid isa MaskedGrid
@test refgrid circle_grid
test_generic_subgrid(circle_grid, C)
@testset "Generic MaskedGrid" begin

@test (length(circle_grid)/length(ProductG)-pi*0.25) < 0.01

Expand All @@ -228,8 +257,10 @@ function test_subgrids()
C = UnitInterval()^2
productgrid = subgrid(ProductG, C)
refgrid = MaskedGrid(ProductG, C)

@test supergrid(productgrid) == ProductG
@test productgrid isa TensorSubGrid
test_generic_subgrid(productgrid, C)
refgrid = MaskedGrid(ProductG, C)
@test reshape(refgrid,10,10) == productgrid
@test subindices(refgrid) == subindices(productgrid)
Expand All @@ -248,6 +279,10 @@ function test_subgrids()
end
@test cnt == length(subgrid)
end

g = subgrid(ScatteredGrid(rand(10)), Interval(0,.5))
@test all( 0.0.<= g .< .5)
end
end

function test_randomgrids()
Expand Down
9 changes: 9 additions & 0 deletions test/test_modcartesianindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ end
for i in cart
@test i modcart
end

j = 0
for i in modcart
j+= 1
end
@test j==length(modcart)

@test first(modcart) == 17
@test last(modcart) == 3
end

@testset "boundary" begin
Expand Down

0 comments on commit 7eb29ef

Please sign in to comment.