Skip to content

Commit 6ee174c

Browse files
committed
Merge branch 'master' into kam/fastsparsity
2 parents ebc8037 + efdf74d commit 6ee174c

3 files changed

Lines changed: 49 additions & 16 deletions

File tree

src/Dofs/DofHandler.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,13 @@ function celldofs!(global_dofs::Vector{Int}, dh::DofHandler, i::Int)
210210
unsafe_copyto!(global_dofs, 1, dh.cell_dofs, dh.cell_dofs_offset[i], length(global_dofs))
211211
return global_dofs
212212
end
213-
function celldofs!(global_dofs::Vector{Int}, sdh::SubDofHandler, i::Int)
213+
function celldofs!(global_dofs::AbstractVector{Int}, dh::AbstractDofHandler, i::Int)
214+
@assert isclosed(dh)
215+
@assert length(global_dofs) == ndofs_per_cell(dh, i)
216+
copyto!(global_dofs, 1, dh.cell_dofs, dh.cell_dofs_offset[i], length(global_dofs))
217+
return global_dofs
218+
end
219+
function celldofs!(global_dofs::AbstractVector{Int}, sdh::SubDofHandler, i::Int)
214220
@assert i in sdh.cellset
215221
return celldofs!(global_dofs, sdh.dh, i)
216222
end
@@ -226,7 +232,7 @@ function celldofs(dh::AbstractDofHandler, i::Int)
226232
return celldofs!(zeros(Int, ndofs_per_cell(dh, i)), dh, i)
227233
end
228234

229-
function cellnodes!(global_nodes::Vector{Int}, dh::DofHandler, i::Union{Int, <:AbstractCell})
235+
function cellnodes!(global_nodes::AbstractVector{Int}, dh::DofHandler, i::Union{Int, <:AbstractCell})
230236
return cellnodes!(global_nodes, get_grid(dh), i)
231237
end
232238

src/Grid/grid.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ get_coordinate_type(::Grid{dim, C, T}) where {dim, C, T} = Vec{dim, T} # Node is
425425
This function takes the local vertex representation (a `VertexIndex`) and looks up the unique global id (an `Int`).
426426
"""
427427
toglobal(grid::AbstractGrid, vertexidx::VertexIndex) = vertices(getcells(grid, vertexidx[1]))[vertexidx[2]]
428-
toglobal(grid::AbstractGrid, vertexidx::Vector{VertexIndex}) = unique(toglobal.((grid,), vertexidx))
428+
toglobal(grid::AbstractGrid, vertexidx::AbstractVector{VertexIndex}) = unique(toglobal.((grid,), vertexidx))
429429

430430
"""
431431
Ferrite.getspatialdim(grid::AbstractGrid)
@@ -465,7 +465,7 @@ Returns either all `cells::Collection{C<:AbstractCell}` of a `<:AbstractGrid` or
465465
Whereas the last option tries to call a `cellset` of the `grid`. `Collection` can be any indexable type, for `Grid` it is `Vector{C<:AbstractCell}`.
466466
"""
467467
@inline getcells(grid::AbstractGrid) = grid.cells
468-
@inline getcells(grid::AbstractGrid, v::Union{Int, Vector{Int}}) = grid.cells[v]
468+
@inline getcells(grid::AbstractGrid, v::Union{Int, AbstractVector{Int}}) = grid.cells[v]
469469
@inline getcells(grid::AbstractGrid, setname::String) = grid.cells[collect(getcellset(grid, setname))]
470470
"Returns the number of cells in the `<:AbstractGrid`."
471471
@inline getncells(grid::AbstractGrid) = length(grid.cells)
@@ -483,7 +483,7 @@ The last option tries to call a `nodeset` of the `<:AbstractGrid`. `Collection{N
483483
to a Node.
484484
"""
485485
@inline getnodes(grid::AbstractGrid) = grid.nodes
486-
@inline getnodes(grid::AbstractGrid, v::Union{Int, Vector{Int}}) = grid.nodes[v]
486+
@inline getnodes(grid::AbstractGrid, v::Union{Int, AbstractVector{Int}}) = grid.nodes[v]
487487
@inline getnodes(grid::AbstractGrid, setname::String) = grid.nodes[collect(getnodeset(grid, setname))]
488488
"Returns the number of nodes in the grid."
489489
@inline getnnodes(grid::AbstractGrid) = length(grid.nodes)
@@ -584,14 +584,15 @@ end
584584
585585
Mutate `x` to the coordinates of the cell corresponding to `idx` or `cell`.
586586
"""
587-
@inline function getcoordinates!(x::Vector{Vec{dim, T}}, grid::AbstractGrid, cell::AbstractCell) where {dim, T}
587+
@propagate_inbounds function getcoordinates!(x::AbstractVector{Vec{dim, T}}, grid::AbstractGrid, cell::AbstractCell) where {dim, T}
588588
node_ids = get_node_ids(cell)
589-
@inbounds for i in 1:length(x)
590-
x[i] = get_node_coordinate(grid, node_ids[i])
589+
@boundscheck checkbounds(x, keys(node_ids))
590+
@inbounds for (i, node_id) in pairs(node_ids)
591+
x[i] = get_node_coordinate(grid, node_id)
591592
end
592593
return x
593594
end
594-
@inline function getcoordinates!(x::Vector{Vec{dim, T}}, grid::AbstractGrid, cellid::Int) where {dim, T}
595+
@inline function getcoordinates!(x::AbstractVector{Vec{dim, T}}, grid::AbstractGrid, cellid::Int) where {dim, T}
595596
cell = getcells(grid, cellid)
596597
return getcoordinates!(x, grid, cell)
597598
end
@@ -604,16 +605,15 @@ Return the coordinate of the `n`th node in `grid`
604605
"""
605606
get_node_coordinate(grid, n) = get_node_coordinate(getnodes(grid, n))
606607

607-
function cellnodes!(global_nodes::Vector{Int}, grid::AbstractGrid, i::Int)
608+
function cellnodes!(global_nodes::AbstractVector{Int}, grid::AbstractGrid, i::Int)
608609
cell = getcells(grid, i)
609610
_cellnodes!(global_nodes, cell)
610611
return global_nodes
611612
end
612-
function _cellnodes!(global_nodes::Vector{Int}, cell::AbstractCell)
613+
function _cellnodes!(global_nodes::AbstractVector{Int}, cell::AbstractCell)
613614
@assert length(global_nodes) == nnodes(cell)
614-
@inbounds for i in 1:length(global_nodes)
615-
global_nodes[i] = cell.nodes[i]
616-
end
615+
node_ids = get_node_ids(cell)
616+
copyto!(global_nodes, node_ids)
617617
return global_nodes
618618
end
619619

test/test_grid_dofhandler_vtk.jl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ end
182182

183183
@test getcells(grid, "cell_set") == [getcells(grid, 1)]
184184

185+
# cellnodes via empty DofHandler
186+
nodeids = zeros(Int, 9)
187+
dh = DofHandler(grid)
188+
close!(dh)
189+
Ferrite.cellnodes!(nodeids, dh, 1)
190+
# Note that the return types typically differ (Vector vs Tuple)
191+
@test all(nodeids .== Ferrite.get_node_ids(getcells(grid, 1)))
192+
185193
# CellIterator on a grid without DofHandler
186194
grid = generate_grid(Triangle, (4, 4))
187195
n = 0
@@ -546,7 +554,6 @@ end
546554
Line((6, 7)),
547555
]
548556
nodes = [Node(coord) for coord in zeros(Vec{2, Float64}, 18)]
549-
grid = Grid(cells, nodes)
550557
@test_throws ErrorException ExclusiveTopology(grid)
551558
# topology = ExclusiveTopology(grid)
552559
# @test_throws ArgumentError Ferrite.facetskeleton(topology, grid)
@@ -585,7 +592,7 @@ end
585592
@test Set(Ferrite.getstencil(stars, quadgrid, VertexIndex(5, 4))) == Set([VertexIndex(4, 2), VertexIndex(4, 4), VertexIndex(5, 1), VertexIndex(5, 3), VertexIndex(7, 1), VertexIndex(7, 3), VertexIndex(8, 2), VertexIndex(8, 4), VertexIndex(4, 3), VertexIndex(5, 4), VertexIndex(7, 2), VertexIndex(8, 1)])
586593
@test Set(Ferrite.toglobal.((quadgrid,), Ferrite.getstencil(stars, quadgrid, VertexIndex(1, 1)))) == Set([1, 2, 5])
587594
@test Set(Ferrite.toglobal.((quadgrid,), Ferrite.getstencil(stars, quadgrid, VertexIndex(2, 1)))) == Set([2, 1, 6, 3])
588-
@test Set(Ferrite.toglobal.((quadgrid,), Ferrite.getstencil(stars, quadgrid, VertexIndex(5, 4)))) == Set([10, 6, 9, 11, 14])
595+
@test Set(Ferrite.toglobal(quadgrid, collect(Ferrite.getstencil(stars, quadgrid, VertexIndex(5, 4))))) == Set([10, 6, 9, 11, 14])
589596

590597
face_skeleton = Ferrite.facetskeleton(topology, quadgrid)
591598
@test Set(face_skeleton) == Set(
@@ -707,6 +714,16 @@ end
707714
close!(dh)
708715
@test celldofs(dh, 1) == [1, 2, 3, 4, 5, 6, 7, 9, 8, 10]
709716
@test celldofs(dh, 2) == [2, 11, 3, 12, 13, 15, 14, 7, 6, 16]
717+
# Should also agree with the remaining celldofs API
718+
dofs = zeros(Int, 10)
719+
celldofs!(dofs, dh, 1)
720+
@test dofs == celldofs(dh, 1)
721+
celldofs!(dofs, dh, 2)
722+
@test dofs == celldofs(dh, 2)
723+
celldofs!(view(dofs, :), dh, 1)
724+
@test dofs == celldofs(dh, 1)
725+
celldofs!(view(dofs, :), dh, 2)
726+
@test dofs == celldofs(dh, 2)
710727

711728
## Lagrange{RefTriangle,3}
712729
# First dof per position per triangle
@@ -723,6 +740,16 @@ end
723740
close!(dh)
724741
@test celldofs(dh, 1) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17, 18, 15, 16, 19, 20]
725742
@test celldofs(dh, 2) == [3, 4, 21, 22, 5, 6, 23, 24, 25, 26, 29, 30, 27, 28, 13, 14, 11, 12, 31, 32]
743+
# Should also agree with the remaining celldofs API
744+
dofs = zeros(Int, 20)
745+
celldofs!(dofs, dh, 1)
746+
@test dofs == celldofs(dh, 1)
747+
celldofs!(dofs, dh, 2)
748+
@test dofs == celldofs(dh, 2)
749+
celldofs!(view(dofs, :), dh, 1)
750+
@test dofs == celldofs(dh, 1)
751+
celldofs!(view(dofs, :), dh, 2)
752+
@test dofs == celldofs(dh, 2)
726753
end
727754

728755
@testset "vectorization layer compat" begin

0 commit comments

Comments
 (0)