Skip to content

Commit

Permalink
Merge pull request #32 from numericalEFT/eltype
Browse files Browse the repository at this point in the history
Now AbstractGrid{T}<:AbstractArray{T,1};
  • Loading branch information
iintSjds authored Sep 23, 2022
2 parents 23f4a57 + 3994a26 commit 32531a0
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/grid/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ create Composite grid from panel and subgrids.
if the boundary grid point of two neighbor subgrids are too close, they will be combined
in the whole grid.
"""
struct Composite{T<:AbstractFloat,PG,SG} <: SimpleG.ClosedGrid
struct Composite{T<:AbstractFloat,PG,SG} <: SimpleG.ClosedGrid{T}
bound::SVector{2,T}
size::Int
grid::Vector{T}
Expand Down
158 changes: 82 additions & 76 deletions src/grid/simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const invvandermonde = BaryChebTools.invvandermonde
All Grids are derived from AbstractGrid; ClosedGrid has bound[1], bound[2] == grid[1], grid[end],
while OpenGrid has bound[1]<grid[1]<grid[end]<bound[2]
"""
abstract type AbstractGrid end
abstract type OpenGrid <: AbstractGrid end
abstract type ClosedGrid <: AbstractGrid end
abstract type AbstractGrid{T} <: AbstractArray{T,1} end
abstract type OpenGrid{T} <: AbstractGrid{T} end
abstract type ClosedGrid{T} <: AbstractGrid{T} end

"""
Expand All @@ -39,32 +39,32 @@ Arbitrary grid generated from given sorted grid.
#Constructor:
- function Arbitrary{T}(grid) where {T<:AbstractFloat}
"""
struct Arbitrary{T<:Real} <: ClosedGrid
struct Arbitrary{T<:Real} <: ClosedGrid{T}
bound::SVector{2,T}
size::Int
grid::Vector{T}
weight::Vector{Float64}

"""
function Arbitrary{T}(grid) where {T<:AbstractFloat}
"""
function Arbitrary{T}(grid) where {T<:AbstractFloat}
create Arbitrary from grid.
"""
create Arbitrary from grid.
"""
function Arbitrary{T}(grid) where {T<:Real}
bound = [grid[1],grid[end]]
bound = [grid[1], grid[end]]
size = length(grid)
weight = zeros(Float64, size)
for i in 1:size
if i==1
if size!=1
weight[1] = 0.5*(grid[2]-grid[1])
if i == 1
if size != 1
weight[1] = 0.5 * (grid[2] - grid[1])
else
weight[1] = 0 # allow arbitrary grid for 1 gridpoint, but integrate undefined
end
elseif i==size
weight[end] = 0.5*(grid[end]-grid[end-1])
elseif i == size
weight[end] = 0.5 * (grid[end] - grid[end-1])
else
weight[i] = 0.5*(grid[i+1]-grid[i-1])
weight[i] = 0.5 * (grid[i+1] - grid[i-1])
end
end
return new{T}(bound, size, grid, weight)
Expand All @@ -83,29 +83,35 @@ function Base.floor(grid::AbstractGrid, x) #where {T}
if x <= grid.grid[1]
return 1
elseif x >= grid.grid[end]
if grid.size!=1
return grid.size-1
if grid.size != 1
return grid.size - 1
else
return 1
end
end

result = searchsortedfirst(grid.grid, x)-1
result = searchsortedfirst(grid.grid, x) - 1
return Base.floor(Int, result)
end

Base.length(grid::AbstractGrid) = grid.size
Base.size(grid::AbstractGrid) = grid.size
Base.size(grid::AbstractGrid) = (grid.size,)
Base.size(grid::AbstractGrid, I::Int) = grid.size

Base.show(io::IO, grid::AbstractGrid) = print(io, grid.grid)
Base.view(grid::AbstractGrid, inds...) where {N} = Base.view(grid.grid, inds...)
# set is not allowed for grids
Base.getindex(grid::AbstractGrid, i) = grid.grid[i]
Base.firstindex(grid::AbstractGrid) = 1
Base.lastindex(grid::AbstractGrid) = grid.size
# iterator
Base.iterate(grid::AbstractGrid) = (grid.grid[1],1)
Base.iterate(grid::AbstractGrid, state) = (state>=grid.size) ? nothing : (grid.grid[state+1],state+1)
Base.iterate(grid::AbstractGrid) = (grid.grid[1], 1)
Base.iterate(grid::AbstractGrid, state) = (state >= grid.size) ? nothing : (grid.grid[state+1], state + 1)

# Base.IteratorSize
Base.IteratorSize(::Type{AbstractGrid{T}}) where {T} = Base.HasLength()
Base.IteratorEltype(::Type{AbstractGrid{T}}) where {T} = Base.HasEltype()
Base.eltype(::Type{AbstractGrid{T}}) where {T} = eltype(T)

"""
struct Uniform{T<:AbstractFloat} <: ClosedGrid
Expand All @@ -121,29 +127,29 @@ Uniform grid generated on [bound[1], bound[2]] with N points
#Constructor:
- function Uniform{T}(bound, size) where {T<:AbstractFloat}
"""
struct Uniform{T<:AbstractFloat} <: ClosedGrid
struct Uniform{T<:AbstractFloat} <: ClosedGrid{T}
bound::SVector{2,T}
size::Int
grid::Vector{T}
weight::Vector{T}

"""
function Uniform{T}(bound, N) where {T<:AbstractFloat}
"""
function Uniform{T}(bound, N) where {T<:AbstractFloat}
create Uniform grid.
"""
create Uniform grid.
"""
function Uniform{T}(bound, N) where {T<:AbstractFloat}
Ntot = N - 1
interval = (bound[2]-bound[1])/Ntot
grid = bound[1] .+ Vector(1:N) .* interval .- ( interval )
interval = (bound[2] - bound[1]) / Ntot
grid = bound[1] .+ Vector(1:N) .* interval .- (interval)
weight = similar(grid)
for i in 1:N
if i==1
weight[1] = 0.5*(grid[2]-grid[1])
elseif i==N
weight[end] = 0.5*(grid[end]-grid[end-1])
if i == 1
weight[1] = 0.5 * (grid[2] - grid[1])
elseif i == N
weight[end] = 0.5 * (grid[end] - grid[end-1])
else
weight[i] = 0.5*(grid[i+1]-grid[i-1])
weight[i] = 0.5 * (grid[i+1] - grid[i-1])
end
end
return new{T}(bound, N, grid, weight)
Expand All @@ -159,11 +165,11 @@ grid point smaller than x.
return 1 for x<grid[1] and grid.size-1 for x>grid[end].
"""
function Base.floor(grid::Uniform{T}, x) where {T}
result = (x-grid.grid[1])/(grid.grid[end]-grid.grid[1])*(grid.size-1)+1
if result <1
result = (x - grid.grid[1]) / (grid.grid[end] - grid.grid[1]) * (grid.size - 1) + 1
if result < 1
return 1
elseif result >= grid.size
return grid.size-1
return grid.size - 1
else
return Base.floor(Int, result)
end
Expand All @@ -184,23 +190,23 @@ BaryCheb grid generated on [bound[1], bound[2]] with order N.
#Constructor:
- function BaryCheb{T}(bound, size) where {T<:AbstractFloat}
"""
struct BaryCheb{T<:AbstractFloat} <: OpenGrid
struct BaryCheb{T<:AbstractFloat} <: OpenGrid{T}
bound::SVector{2,T}
size::Int
grid::Vector{T}
weight::Vector{T}
invVandermonde::Matrix{T}
"""
function BaryCheb{T}(bound, N) where {T<:AbstractFloat}
"""
function BaryCheb{T}(bound, N) where {T<:AbstractFloat}
create BaryCheb grid.
"""
create BaryCheb grid.
"""
function BaryCheb{T}(bound, N) where {T<:AbstractFloat}
order = N
x, w =barychebinit(order)
x, w = barychebinit(order)
grid = zeros(T, N)
a, b = bound[1], bound[2]
weight = (b - a) / 2 .* w
weight = (b - a) / 2 .* w
grid = (a + b) / 2 .+ (b - a) / 2 .* x
invVandermonde = inv(transpose(vandermonde(x)))

Expand All @@ -209,10 +215,10 @@ create BaryCheb grid.
function BaryCheb{T}(bound, N, invVandermonde) where {T<:AbstractFloat}
# use given Vandermonde matrix, useful for composite grid that has many BaryCheb subgrids with same order
order = N
x, w =barychebinit(order)
x, w = barychebinit(order)
grid = zeros(T, N)
a, b = bound[1], bound[2]
weight = (b - a) / 2 .* w
weight = (b - a) / 2 .* w
grid = (a + b) / 2 .+ (b - a) / 2 .* x
return new{T}(bound, N, grid, weight, invVandermonde)
end
Expand All @@ -232,23 +238,23 @@ GaussLegendre grid generated on [bound[1], bound[2]] with order N.
#Constructor:
- function GaussLegendre{T}(bound, size) where {T<:AbstractFloat}
"""
struct GaussLegendre{T<:AbstractFloat} <: OpenGrid
struct GaussLegendre{T<:AbstractFloat} <: OpenGrid{T}
bound::SVector{2,T}
size::Int
grid::Vector{T}
weight::Vector{T}

"""
function GaussLegendre{T}(bound, N) where {T<:AbstractFloat}
"""
function GaussLegendre{T}(bound, N) where {T<:AbstractFloat}
create GaussLegendre grid.
"""
create GaussLegendre grid.
"""
function GaussLegendre{T}(bound, N) where {T<:AbstractFloat}
order = N
x, w = gausslegendre(order)
grid = zeros(T, N)
a, b = bound[1], bound[2]
weight = (b - a) / 2 .* w
weight = (b - a) / 2 .* w
grid = (a + b) / 2 .+ (b - a) / 2 .* x

return new{T}(bound, N, grid, weight)
Expand Down Expand Up @@ -276,7 +282,7 @@ On [0, 1], a typical d2s Log grid looks like
#Constructor:
- function Log{T}(bound, size, minterval, d2s) where {T<:AbstractFloat}
"""
struct Log{T<:AbstractFloat} <: ClosedGrid
struct Log{T<:AbstractFloat} <: ClosedGrid{T}
bound::SVector{2,T}
size::Int
grid::Vector{T}
Expand All @@ -285,39 +291,39 @@ struct Log{T<:AbstractFloat} <: ClosedGrid
λ::T
d2s::Bool

"""
function Log{T}(bound, N, minterval, d2s) where {T<:AbstractFloat}
"""
function Log{T}(bound, N, minterval, d2s) where {T<:AbstractFloat}
create Log grid.
"""
create Log grid.
"""
function Log{T}(bound, N, minterval, d2s) where {T<:AbstractFloat}
grid = zeros(T, N)
M = N-2
λ = (minterval/(bound[2]-bound[1]))^(1.0/M)
M = N - 2
λ = (minterval / (bound[2] - bound[1]))^(1.0 / M)

if d2s
for i in 1:M
grid[i+1] = bound[1] + (bound[2]-bound[1])*λ^(M+1-i)
grid[i+1] = bound[1] + (bound[2] - bound[1]) * λ^(M + 1 - i)
end
else
for i in 2:M+1
grid[i] = bound[2] - (bound[2]-bound[1])*λ^(i-1)
grid[i] = bound[2] - (bound[2] - bound[1]) * λ^(i - 1)
end
end
grid[1] = bound[1]
grid[end] = bound[2]
weight = similar(grid)
for i in 1:N
if i==1
weight[1] = 0.5*(grid[2]-grid[1])
elseif i==N
weight[end] = 0.5*(grid[end]-grid[end-1])
if i == 1
weight[1] = 0.5 * (grid[2] - grid[1])
elseif i == N
weight[end] = 0.5 * (grid[end] - grid[end-1])
else
weight[i] = 0.5*(grid[i+1]-grid[i-1])
weight[i] = 0.5 * (grid[i+1] - grid[i-1])
end
end

return new{T}(bound, N, grid,weight, λ, d2s)
return new{T}(bound, N, grid, weight, λ, d2s)
end
end

Expand All @@ -334,31 +340,31 @@ function Base.floor(grid::Log{T}, x) where {T}
if x <= grid.grid[1]
return 1
elseif x >= grid.grid[end]
return grid.size-1
return grid.size - 1
end

a,b=grid.bound[1],grid.bound[2]
a, b = grid.bound[1], grid.bound[2]
if grid.d2s
i = Base.floor( log((x-a)/(b-a))/log(grid.λ) )
if i > grid.size-2
i = Base.floor(log((x - a) / (b - a)) / log(grid.λ))
if i > grid.size - 2
result = 1
else
result = grid.size-i-1
result = grid.size - i - 1
end
else
i = Base.floor( log((b-x)/(b-a))/log(grid.λ) )
if i > grid.size-2
result = grid.size-1
i = Base.floor(log((b - x) / (b - a)) / log(grid.λ))
if i > grid.size - 2
result = grid.size - 1
else
result = i+1
result = i + 1
end
end

return Base.floor(Int, result)
end

@inline function denseindex(grid::Log)
return [(grid.d2s) ? 1 : grid.size, ]
return [(grid.d2s) ? 1 : grid.size,]
end

end

0 comments on commit 32531a0

Please sign in to comment.