Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store boundaries in a matrix #130

Merged
merged 2 commits into from
Apr 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 9 additions & 26 deletions src/Grid/grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,14 @@ end
Node{dim, T}(x::NTuple{dim, T}) = Node(Vec{dim, T}(x))
getcoordinates(n::Node) = n.x

#=
#A bit redundant but perhaps we want to put more in here:
immutable Face
onboundary::Bool
end
onboundary(f::Face) = f.onboundary
Face() = Face(false)
=#

"""
A `Cell` is a sub-domain defined by a collection of `Node`s as it's vertices.
"""
immutable Cell{dim, N, M}
nodes::NTuple{N, Int}
onboundary::NTuple{M, Bool}
end

onboundary(c::Cell, face::Int) = c.onboundary[face]

(::Type{Cell{dim, N, M}}){dim, N, M}(nodes::NTuple{N}) = Cell{dim,N,M}(nodes, ntuple(i->false, Val{M}))
(::Type{Cell{dim, N, M}}){dim, N, M}(nodes::NTuple{N}, arr::AbstractArray{Bool}) = Cell{dim,N,M}(nodes, ntuple(i -> arr[i], Val{M}))


nfaces(c::Cell) = nfaces(typeof(c))
nfaces{dim, N, M}(::Type{Cell{dim, N, M}}) = M

# Typealias for commonly used cells
nfaces{dim, N, M}(::Type{Cell{dim, N, M}}) = M# Typealias for commonly used cells
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should be on line below


@compat const Line = Cell{1, 2, 2}
@compat const QuadraticLine = Cell{1, 3, 2}
Expand Down Expand Up @@ -92,15 +73,17 @@ type Grid{dim, N, T <: Real, M}
cellsets::Dict{String, Set{Int}}
nodesets::Dict{String, Set{Int}}
facesets::Dict{String, Set{Tuple{Int, Int}}}
# Boundary matrix (faces per cell × cell)
boundary_matrix::SparseMatrixCSC{Bool, Int}
end

function Grid{dim, N, M, T}(cells::Vector{Cell{dim, N, M}},
nodes::Vector{Node{dim, T}};
cellsets::Dict{String, Set{Int}}=Dict{String, Set{Int}}(),
nodesets::Dict{String, Set{Int}}=Dict{String, Set{Int}}(),
facesets::Dict{String, Set{Tuple{Int, Int}}}=Dict{String, Set{Tuple{Int, Int}}}(),
)
return Grid(cells, nodes, cellsets, nodesets, facesets)
nodes::Vector{Node{dim, T}};
cellsets::Dict{String, Set{Int}}=Dict{String, Set{Int}}(),
nodesets::Dict{String, Set{Int}}=Dict{String, Set{Int}}(),
facesets::Dict{String, Set{Tuple{Int, Int}}}=Dict{String, Set{Tuple{Int, Int}}}(),
boundary_matrix::SparseMatrixCSC{Bool, Int}=spzeros(Bool, 0, 0))
return Grid(cells, nodes, cellsets, nodesets, facesets, boundary_matrix)
end

##########################
Expand Down
49 changes: 22 additions & 27 deletions src/Grid/grid_generators.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
export generate_grid

# Goes through `boundary` and updates the onboundary values for the
# cells that actually are on the boundary
function _apply_onboundary!{T}(::Type{T}, cells, boundary)
cells_new = T[]
isboundary = zeros(Bool, nfaces(T))
function boundaries_to_sparse(boundary)
I, J, V = Int[], Int[], Bool[]
for faceindex in boundary
fill!(isboundary, false)
cell, face = faceindex
c = cells[cell]
for i in 1:nfaces(T)
isboundary[i] = onboundary(c, i) | (face == i)
end
cells[cell] = T(c.nodes, isboundary)
push!(I, face)
push!(J, cell)
push!(V, true)
end
return sparse(I, J, V)
end

"""
Expand Down Expand Up @@ -56,12 +51,12 @@ function generate_grid{T}(::Type{Line}, nel::NTuple{1, Int}, left::Vec{1, T}=Vec
boundary = Vector([(1, 1),
(nel_x, 2)])

_apply_onboundary!(Line, cells, boundary)
boundary_matrix = boundaries_to_sparse(boundary)

# Cell face sets
facesets = Dict("left" => Set{Tuple{Int, Int}}([boundary[1]]),
"right" => Set{Tuple{Int, Int}}([boundary[2]]))
return Grid(cells, nodes, facesets=facesets)
return Grid(cells, nodes, facesets=facesets, boundary_matrix=boundary_matrix)
end

# QuadraticLine
Expand All @@ -86,12 +81,12 @@ function generate_grid{T}(::Type{QuadraticLine}, nel::NTuple{1, Int}, left::Vec{
boundary = Tuple{Int, Int}[(1, 1),
(nel_x, 2)]

_apply_onboundary!(QuadraticLine, cells, boundary)
boundary_matrix = boundaries_to_sparse(boundary)

# Cell face sets
facesets = Dict("left" => Set{Tuple{Int, Int}}([boundary[1]]),
"right" => Set{Tuple{Int, Int}}([boundary[2]]))
return Grid(cells, nodes, facesets=facesets)
return Grid(cells, nodes, facesets=facesets, boundary_matrix=boundary_matrix)
end

function _generate_2d_nodes!(nodes, nx, ny, LL, LR, UR, UL)
Expand Down Expand Up @@ -151,7 +146,7 @@ function generate_grid{T}(C::Type{Quadrilateral}, nel::NTuple{2, Int}, LL::Vec{2
[(cl, 3) for cl in cell_array[:,end]];
[(cl, 4) for cl in cell_array[1,:]]]

_apply_onboundary!(Quadrilateral, cells, boundary)
boundary_matrix = boundaries_to_sparse(boundary)

# Cell face sets
offset = 0
Expand All @@ -161,7 +156,7 @@ function generate_grid{T}(C::Type{Quadrilateral}, nel::NTuple{2, Int}, LL::Vec{2
facesets["top"] = Set{Tuple{Int, Int}}(boundary[(1:length(cell_array[:,end])) + offset]); offset += length(cell_array[:,end])
facesets["left"] = Set{Tuple{Int, Int}}(boundary[(1:length(cell_array[1,:])) + offset]); offset += length(cell_array[1,:])

return Grid(cells, nodes, facesets=facesets)
return Grid(cells, nodes, facesets=facesets, boundary_matrix=boundary_matrix)
end

# QuadraticQuadrilateral
Expand Down Expand Up @@ -190,7 +185,7 @@ function generate_grid{T}(::Type{QuadraticQuadrilateral}, nel::NTuple{2, Int}, L
[(cl, 3) for cl in cell_array[:,end]];
[(cl, 4) for cl in cell_array[1,:]]]

_apply_onboundary!(QuadraticQuadrilateral, cells, boundary)
boundary_matrix = boundaries_to_sparse(boundary)

# Cell face sets
offset = 0
Expand All @@ -200,7 +195,7 @@ function generate_grid{T}(::Type{QuadraticQuadrilateral}, nel::NTuple{2, Int}, L
facesets["top"] = Set{Tuple{Int, Int}}(boundary[(1:length(cell_array[:,end])) + offset]); offset += length(cell_array[:,end])
facesets["left"] = Set{Tuple{Int, Int}}(boundary[(1:length(cell_array[1,:])) + offset]); offset += length(cell_array[1,:])

return Grid(cells, nodes, facesets=facesets)
return Grid(cells, nodes, facesets=facesets, boundary_matrix=boundary_matrix)
end

# Hexahedron
Expand Down Expand Up @@ -235,7 +230,7 @@ function generate_grid{T}(::Type{Hexahedron}, nel::NTuple{3, Int}, left::Vec{3,
[(cl, 5) for cl in cell_array[1,:,:][:]];
[(cl, 6) for cl in cell_array[:,:,end][:]]]

_apply_onboundary!(Hexahedron, cells, boundary)
boundary_matrix = boundaries_to_sparse(boundary)

# Cell face sets
offset = 0
Expand All @@ -247,7 +242,7 @@ function generate_grid{T}(::Type{Hexahedron}, nel::NTuple{3, Int}, left::Vec{3,
facesets["left"] = Set{Tuple{Int, Int}}(boundary[(1:length(cell_array[1,:,:][:])) + offset]); offset += length(cell_array[1,:,:][:])
facesets["top"] = Set{Tuple{Int, Int}}(boundary[(1:length(cell_array[:,:,end][:])) + offset]); offset += length(cell_array[:,:,end][:])

return Grid(cells, nodes, facesets=facesets)
return Grid(cells, nodes, facesets=facesets, boundary_matrix=boundary_matrix)
end

# Triangle
Expand Down Expand Up @@ -275,7 +270,7 @@ function generate_grid{T}(::Type{Triangle}, nel::NTuple{2, Int}, LL::Vec{2, T},
[(cl, 2) for cl in cell_array[2,:,end]];
[(cl, 3) for cl in cell_array[1,1,:]]]

_apply_onboundary!(Triangle, cells, boundary)
boundary_matrix = boundaries_to_sparse(boundary)

# Cell face sets
offset = 0
Expand All @@ -285,7 +280,7 @@ function generate_grid{T}(::Type{Triangle}, nel::NTuple{2, Int}, LL::Vec{2, T},
facesets["top"] = Set{Tuple{Int, Int}}(boundary[(1:length(cell_array[2,:,end])) + offset]); offset += length(cell_array[2,:,end])
facesets["left"] = Set{Tuple{Int, Int}}(boundary[(1:length(cell_array[1,1,:])) + offset]); offset += length(cell_array[1,1,:])

return Grid(cells, nodes, facesets=facesets)
return Grid(cells, nodes, facesets=facesets, boundary_matrix=boundary_matrix)
end

# QuadraticTriangle
Expand Down Expand Up @@ -315,7 +310,7 @@ function generate_grid{T}(::Type{QuadraticTriangle}, nel::NTuple{2, Int}, LL::Ve
[(cl, 2) for cl in cell_array[2,:,end]];
[(cl, 3) for cl in cell_array[1,1,:]]]

_apply_onboundary!(QuadraticTriangle, cells, boundary)
boundary_matrix = boundaries_to_sparse(boundary)

# Cell face sets
offset = 0
Expand All @@ -325,7 +320,7 @@ function generate_grid{T}(::Type{QuadraticTriangle}, nel::NTuple{2, Int}, LL::Ve
facesets["top"] = Set{Tuple{Int, Int}}(boundary[(1:length(cell_array[2,:,end])) + offset]); offset += length(cell_array[2,:,end])
facesets["left"] = Set{Tuple{Int, Int}}(boundary[(1:length(cell_array[1,1,:])) + offset]); offset += length(cell_array[1,1,:])

return Grid(cells, nodes, facesets=facesets)
return Grid(cells, nodes, facesets=facesets, boundary_matrix=boundary_matrix)
end

# Tetrahedron
Expand Down Expand Up @@ -370,7 +365,7 @@ function generate_grid{T}(::Type{Tetrahedron}, nel::NTuple{3, Int}, left::Vec{3,
[(cl, 3) for cl in cell_array[4,:,:,end][:]];
[(cl, 3) for cl in cell_array[5,:,:,end][:]]]

_apply_onboundary!(Tetrahedron, cells, boundary)
boundary_matrix = boundaries_to_sparse(boundary)

# Cell face sets
offset = 0
Expand All @@ -382,5 +377,5 @@ function generate_grid{T}(::Type{Tetrahedron}, nel::NTuple{3, Int}, left::Vec{3,
facesets["left"] = Set{Tuple{Int, Int}}(boundary[(1:length([cell_array[1,1,:,:][:]; cell_array[5,1,:,:][:]])) + offset]); offset += length([cell_array[1,1,:,:][:]; cell_array[5,1,:,:][:]])
facesets["top"] = Set{Tuple{Int, Int}}(boundary[(1:length([cell_array[4,:,:,end][:]; cell_array[5,:,:,end][:]])) + offset]); offset += length([cell_array[4,:,:,end][:]; cell_array[5,:,:,end][:]])

return Grid(cells, nodes, facesets=facesets)
return Grid(cells, nodes, facesets=facesets, boundary_matrix=boundary_matrix)
end
2 changes: 1 addition & 1 deletion src/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Base.eltype{T <: CellIterator}(::Type{T}) = T
@inline getnodes(ci::CellIterator) = ci.nodes
@inline getcoordinates(ci::CellIterator) = ci.coords
@inline nfaces(ci::CellIterator) = nfaces(eltype(ci.grid.cells))
@inline onboundary(ci::CellIterator, face::Int) = onboundary(ci.grid.cells[ci.current_cellid[]], face)
@inline onboundary(ci::CellIterator, face::Int) = ci.grid.boundary_matrix[face, ci.current_cellid[]]
@inline cellid(ci::CellIterator) = ci.current_cellid[]
@inline celldofs!(v::Vector, ci::CellIterator) = celldofs!(v, ci.dh, ci.current_cellid[])

Expand Down