/
grid.jl
881 lines (763 loc) · 39.4 KB
/
grid.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
#########################
# Main types for meshes #
#########################
"""
Node{dim, T}
A `Node` is a point in space.
# Fields
- `x::Vec{dim,T}`: stores the coordinates
"""
struct Node{dim,T}
x::Vec{dim,T}
end
Node(x::NTuple{dim,T}) where {dim,T} = Node(Vec{dim,T}(x))
getcoordinates(n::Node) = n.x
"""
Ferrite.get_coordinate_eltype(::Node)
Get the data type of the components of the nodes coordinate.
"""
get_coordinate_eltype(::Node{dim,T}) where {dim,T} = T
abstract type AbstractCell{dim,N,M} end
"""
Cell{dim,N,M} <: AbstractCell{dim,N,M}
A `Cell` is a subdomain defined by a collection of `Node`s.
The parameter `dim` refers here to the geometrical/ambient dimension, i.e. the dimension of the `nodes` in the grid and **not** the topological dimension of the cell (i.e. the dimension of the reference element obtained by default_interpolation).
A `Cell` has `N` nodes and `M` faces.
Note that a `Cell` is not defined geometrically by node coordinates, but rather topologically by node indices into the node vector of some grid.
# Fields
- `nodes::Ntuple{N,Int}`: N-tuple that stores the node ids. The ordering defines a cell's and its subentities' orientations.
"""
struct Cell{dim,N,M} <: AbstractCell{dim,N,M}
nodes::NTuple{N,Int}
end
nfaces(c::C) where {C<:AbstractCell} = nfaces(typeof(c))
nfaces(::Type{<:AbstractCell{dim,N,M}}) where {dim,N,M} = M
nedges(c::C) where {C<:AbstractCell} = length(edges(c))
nvertices(c::C) where {C<:AbstractCell} = length(vertices(c))
nnodes(c::C) where {C<:AbstractCell} = nnodes(typeof(c))
nnodes(::Type{<:AbstractCell{dim,N,M}}) where {dim,N,M} = N
"""
Ferrite.vertices(::AbstractCell)
Returns a tuple with the node indices (of the nodes in a grid) for each vertex in a given cell.
This function induces the [`VertexIndex`](@ref), where the second index
corresponds to the local index into this tuple.
"""
vertices(::Ferrite.AbstractCell)
"""
Ferrite.edges(::AbstractCell)
Returns a tuple of 2-tuples containing the ordered node indices (of the nodes in a grid) corresponding to
the vertices that define an *oriented edge*. This function induces the
[`EdgeIndex`](@ref), where the second index corresponds to the local index into this tuple.
Note that the vertices are sufficient to define an edge uniquely.
"""
edges(::Ferrite.AbstractCell)
"""
Ferrite.faces(::AbstractCell)
Returns a tuple of n-tuples containing the ordered node indices (of the nodes in a grid) corresponding to
the vertices that define an *oriented face*. This function induces the
[`FaceIndex`](@ref), where the second index corresponds to the local index into this tuple.
Note that the vertices are sufficient to define a face uniquely.
"""
faces(::Ferrite.AbstractCell)
"""
Ferrite.default_interpolation(::AbstractCell)::Interpolation
Returns the interpolation which defines the geometry of a given cell.
"""
default_interpolation(::Ferrite.AbstractCell)
# Typealias for commonly used cells
const implemented_celltypes = (
(const Line = Cell{1,2,2}),
(const Line2D = Cell{2,2,1}),
(const Line3D = Cell{3,2,0}),
(const QuadraticLine = Cell{1,3,2}),
(const Triangle = Cell{2,3,3}),
(const QuadraticTriangle = Cell{2,6,3}),
(const Quadrilateral = Cell{2,4,4}),
(const Quadrilateral3D = Cell{3,4,1}),
(const QuadraticQuadrilateral = Cell{2,9,4}),
(const Tetrahedron = Cell{3,4,4}),
(const QuadraticTetrahedron = Cell{3,10,4}),
(const Hexahedron = Cell{3,8,6}),
(Cell{2,20,6}),
(const Wedge = Cell{3,6,5})
)
struct EntityNeighborhood{T<:Union{BoundaryIndex,CellIndex}}
neighbor_info::Vector{T}
end
EntityNeighborhood(info::T) where T <: BoundaryIndex = EntityNeighborhood([info])
Base.zero(::Type{EntityNeighborhood{T}}) where T = EntityNeighborhood(T[])
Base.zero(::Type{EntityNeighborhood}) = EntityNeighborhood(BoundaryIndex[])
Base.length(n::EntityNeighborhood) = length(n.neighbor_info)
Base.getindex(n::EntityNeighborhood,i) = getindex(n.neighbor_info,i)
Base.firstindex(n::EntityNeighborhood) = 1
Base.lastindex(n::EntityNeighborhood) = length(n.neighbor_info)
Base.:(==)(n1::EntityNeighborhood, n2::EntityNeighborhood) = n1.neighbor_info == n2.neighbor_info
Base.iterate(n::EntityNeighborhood, state=1) = iterate(n.neighbor_info,state)
function Base.:+(n1::EntityNeighborhood, n2::EntityNeighborhood)
neighbor_info = [n1.neighbor_info; n2.neighbor_info]
return EntityNeighborhood(neighbor_info)
end
function Base.show(io::IO, ::MIME"text/plain", n::EntityNeighborhood)
if length(n) == 0
println(io, "No EntityNeighborhood")
elseif length(n) == 1
println(io, "$(n.neighbor_info[1])")
else
println(io, "$(n.neighbor_info...)")
end
end
"""
face_npoints(::AbstractCell{dim,N,M)
Specifies for each subtype of AbstractCell how many nodes form a face
"""
face_npoints(::Cell{2,N,M}) where {N,M} = 2
face_npoints(::Cell{3,4,1}) = 4 #not sure how to handle embedded cells e.g. Quadrilateral3D
"""
edge_npoints(::AbstractCell{dim,N,M)
Specifies for each subtype of AbstractCell how many nodes form an edge
"""
edge_npoints(::Cell{3,4,1}) = 2 #not sure how to handle embedded cells e.g. Quadrilateral3D
face_npoints(::Cell{3,N,6}) where N = 4
face_npoints(::Cell{3,N,4}) where N = 3
edge_npoints(::Cell{3,N,M}) where {N,M} = 2
getdim(::Cell{dim}) where dim = dim
abstract type AbstractTopology end
"""
ExclusiveTopology(cells::Vector{C}) where C <: AbstractCell
`ExclusiveTopology` saves topological (connectivity) data of the grid. The constructor works with an `AbstractCell`
vector for all cells that dispatch `vertices`, `faces` and in 3D `edges` as well as the utility functions
`face_npoints` and `edge_npoints`.
The struct saves the highest dimensional neighborhood, i.e. if something is connected by a face and an
edge only the face neighborhood is saved. The lower dimensional neighborhood is recomputed, if needed.
# Fields
- `vertex_to_cell::Dict{Int,Vector{Int}}`: global vertex id to all cells containing the vertex
- `cell_neighbor::Vector{EntityNeighborhood{CellIndex}}`: cellid to all connected cells
- `face_neighbor::SparseMatrixCSC{EntityNeighborhood,Int}`: `face_neighbor[cellid,local_face_id]` -> neighboring face
- `vertex_neighbor::SparseMatrixCSC{EntityNeighborhood,Int}`: `vertex_neighbor[cellid,local_vertex_id]` -> neighboring vertex
- `edge_neighbor::SparseMatrixCSC{EntityNeighborhood,Int}`: `edge_neighbor[cellid_local_vertex_id]` -> neighboring edge
- `vertex_vertex_neighbor::Dict{Int,EntityNeighborhood{VertexIndex}}`: global vertex id -> all connected vertices by edge or face
- `face_skeleton::Vector{FaceIndex}`: list of unique faces in the grid
"""
struct ExclusiveTopology <: AbstractTopology
# maps a global vertex id to all cells containing the vertex
vertex_to_cell::Dict{Int,Vector{Int}}
# index of the vector = cell id -> all other connected cells
cell_neighbor::Vector{EntityNeighborhood{CellIndex}}
# face_neighbor[cellid,local_face_id] -> exclusive connected entities (not restricted to one entity)
face_neighbor::SparseMatrixCSC{EntityNeighborhood,Int}
# vertex_neighbor[cellid,local_vertex_id] -> exclusive connected entities to the given vertex
vertex_neighbor::SparseMatrixCSC{EntityNeighborhood,Int}
# edge_neighbor[cellid,local_edge_id] -> exclusive connected entities of the given edge
edge_neighbor::SparseMatrixCSC{EntityNeighborhood,Int}
# maps global vertex id to all directly (by edge or face) connected vertices (no diagonal connection considered)
vertex_vertex_neighbor::Dict{Int,EntityNeighborhood{VertexIndex}}
# list of unique faces in the grid given as FaceIndex
face_skeleton::Vector{FaceIndex}
end
function ExclusiveTopology(cells::Vector{C}) where C <: AbstractCell
cell_vertices_table = vertices.(cells) #needs generic interface for <: AbstractCell
vertex_cell_table = Dict{Int,Vector{Int}}()
for (cellid, cell_nodes) in enumerate(cell_vertices_table)
for node in cell_nodes
if haskey(vertex_cell_table, node)
push!(vertex_cell_table[node], cellid)
else
vertex_cell_table[node] = [cellid]
end
end
end
I_face = Int[]; J_face = Int[]; V_face = EntityNeighborhood[]
I_edge = Int[]; J_edge = Int[]; V_edge = EntityNeighborhood[]
I_vertex = Int[]; J_vertex = Int[]; V_vertex = EntityNeighborhood[]
cell_neighbor_table = Vector{EntityNeighborhood{CellIndex}}(undef, length(cells))
for (cellid, cell) in enumerate(cells)
#cell neighborhood
cell_neighbors = getindex.((vertex_cell_table,), cell_vertices_table[cellid]) # cell -> vertex -> cell
cell_neighbors = unique(reduce(vcat,cell_neighbors)) # non unique list initially
filter!(x->x!=cellid, cell_neighbors) # get rid of self neighborhood
cell_neighbor_table[cellid] = EntityNeighborhood(CellIndex.(cell_neighbors))
for neighbor in cell_neighbors
neighbor_local_ids = findall(x->x in cell_vertices_table[cellid], cell_vertices_table[neighbor])
cell_local_ids = findall(x->x in cell_vertices_table[neighbor], cell_vertices_table[cellid])
# vertex neighbor
if length(cell_local_ids) == 1
_vertex_neighbor!(V_vertex, I_vertex, J_vertex, cellid, cell, neighbor_local_ids, neighbor, cells[neighbor])
# face neighbor
elseif length(cell_local_ids) == face_npoints(cell)
_face_neighbor!(V_face, I_face, J_face, cellid, cell, neighbor_local_ids, neighbor, cells[neighbor])
# edge neighbor
elseif getdim(cell) > 2 && length(cell_local_ids) == edge_npoints(cell)
_edge_neighbor!(V_edge, I_edge, J_edge, cellid, cell, neighbor_local_ids, neighbor, cells[neighbor])
end
end
end
celltype = eltype(cells)
if isconcretetype(celltype)
dim = getdim(cells[1])
_nvertices = nvertices(cells[1])
push!(V_vertex,zero(EntityNeighborhood{VertexIndex}))
push!(I_vertex,1); push!(J_vertex,_nvertices)
if dim > 1
_nfaces = nfaces(cells[1])
push!(V_face,zero(EntityNeighborhood{FaceIndex}))
push!(I_face,1); push!(J_face,_nfaces)
end
if dim > 2
_nedges = nedges(cells[1])
push!(V_edge,zero(EntityNeighborhood{EdgeIndex}))
push!(I_edge,1); push!(J_edge,_nedges)
end
else
celltypes = typeof.(cells)
for celltype in celltypes
celltypeidx = findfirst(x->typeof(x)==celltype,cells)
dim = getdim(cells[celltypeidx])
_nvertices = nvertices(cells[celltypeidx])
push!(V_vertex,zero(EntityNeighborhood{VertexIndex}))
push!(I_vertex,celltypeidx); push!(J_vertex,_nvertices)
if dim > 1
_nfaces = nfaces(cells[celltypeidx])
push!(V_face,zero(EntityNeighborhood{FaceIndex}))
push!(I_face,celltypeidx); push!(J_face,_nfaces)
end
if dim > 2
_nedges = nedges(cells[celltypeidx])
push!(V_edge,zero(EntityNeighborhood{EdgeIndex}))
push!(I_edge,celltypeidx); push!(J_edge,_nedges)
end
end
end
face_neighbor = sparse(I_face,J_face,V_face)
vertex_neighbor = sparse(I_vertex,J_vertex,V_vertex)
edge_neighbor = sparse(I_edge,J_edge,V_edge)
vertex_vertex_table = Dict{Int,EntityNeighborhood}()
vertex_vertex_global = Dict{Int,Vector{Int}}()
# Vertex Connectivity
for global_vertexid in keys(vertex_cell_table)
#Cellset that contains given vertex
cellset = vertex_cell_table[global_vertexid]
vertex_neighbors_local = VertexIndex[]
vertex_neighbors_global = Int[]
for cell in cellset
neighbor_boundary = getdim(cells[cell]) == 2 ? [faces(cells[cell])...] : [edges(cells[cell])...] #get lowest dimension boundary
neighbor_connected_faces = neighbor_boundary[findall(x->global_vertexid in x, neighbor_boundary)]
neighbor_vertices_global = getindex.(neighbor_connected_faces, findfirst.(x->x!=global_vertexid,neighbor_connected_faces))
neighbor_vertices_local= [VertexIndex(cell,local_vertex) for local_vertex in findall(x->x in neighbor_vertices_global, vertices(cells[cell]))]
append!(vertex_neighbors_local, neighbor_vertices_local)
append!(vertex_neighbors_global, neighbor_vertices_global)
end
vertex_vertex_table[global_vertexid] = EntityNeighborhood(vertex_neighbors_local)
vertex_vertex_global[global_vertexid] = vertex_neighbors_global
end
# Face Skeleton
face_skeleton_global = Set{NTuple}()
face_skeleton_local = Vector{FaceIndex}()
fs_length = length(face_skeleton_global)
for (cellid,cell) in enumerate(cells)
for (local_face_id,face) in enumerate(faces(cell))
push!(face_skeleton_global, sortface(face))
fs_length_new = length(face_skeleton_global)
if fs_length != fs_length_new
push!(face_skeleton_local, FaceIndex(cellid,local_face_id))
fs_length = fs_length_new
end
end
end
return ExclusiveTopology(vertex_cell_table,cell_neighbor_table,face_neighbor,vertex_neighbor,edge_neighbor,vertex_vertex_table,face_skeleton_local)
end
function _vertex_neighbor!(V_vertex, I_vertex, J_vertex, cellid, cell, neighbor, neighborid, neighbor_cell)
vertex_neighbor = VertexIndex((neighborid, neighbor[1]))
cell_vertex_id = findfirst(x->x==neighbor_cell.nodes[neighbor[1]], cell.nodes)
push!(V_vertex,EntityNeighborhood(vertex_neighbor))
push!(I_vertex,cellid)
push!(J_vertex,cell_vertex_id)
end
function _edge_neighbor!(V_edge, I_edge, J_edge, cellid, cell, neighbor, neighborid, neighbor_cell)
neighbor_edge = neighbor_cell.nodes[neighbor]
if getdim(neighbor_cell) < 3
neighbor_edge_id = findfirst(x->issubset(x,neighbor_edge), faces(neighbor_cell))
edge_neighbor = FaceIndex((neighborid, neighbor_edge_id))
else
neighbor_edge_id = findfirst(x->issubset(x,neighbor_edge), edges(neighbor_cell))
edge_neighbor = EdgeIndex((neighborid, neighbor_edge_id))
end
cell_edge_id = findfirst(x->issubset(x,neighbor_edge),edges(cell))
push!(V_edge, EntityNeighborhood(edge_neighbor))
push!(I_edge, cellid)
push!(J_edge, cell_edge_id)
end
function _face_neighbor!(V_face, I_face, J_face, cellid, cell, neighbor, neighborid, neighbor_cell)
neighbor_face = neighbor_cell.nodes[neighbor]
if getdim(neighbor_cell) == getdim(cell)
neighbor_face_id = findfirst(x->issubset(x,neighbor_face), faces(neighbor_cell))
face_neighbor = FaceIndex((neighborid, neighbor_face_id))
else
neighbor_face_id = findfirst(x->issubset(x,neighbor_face), edges(neighbor_cell))
face_neighbor = EdgeIndex((neighborid, neighbor_face_id))
end
cell_face_id = findfirst(x->issubset(x,neighbor_face),faces(cell))
push!(V_face, EntityNeighborhood(face_neighbor))
push!(I_face, cellid)
push!(J_face, cell_face_id)
end
getcells(neighbor::EntityNeighborhood{T}) where T <: BoundaryIndex = first.(neighbor.neighbor_info)
getcells(neighbor::EntityNeighborhood{CellIndex}) = getproperty.(neighbor.neighbor_info, :idx)
getcells(neighbors::Vector{T}) where T <: EntityNeighborhood = reduce(vcat, getcells.(neighbors))
getcells(neighbors::Vector{T}) where T <: BoundaryIndex = getindex.(neighbors,1)
abstract type AbstractGrid{dim} end
ExclusiveTopology(grid::AbstractGrid) = ExclusiveTopology(getcells(grid))
"""
Grid{dim, C<:AbstractCell, T<:Real} <: AbstractGrid}
A `Grid` is a collection of `Cells` and `Node`s which covers the computational domain, together with Sets of cells, nodes and faces.
There are multiple helper structures to apply boundary conditions or define subdomains. They are gathered in the `cellsets`, `nodesets`,
`facesets`, `edgesets` and `vertexsets`.
# Fields
- `cells::Vector{C}`: stores all cells of the grid
- `nodes::Vector{Node{dim,T}}`: stores the `dim` dimensional nodes of the grid
- `cellsets::Dict{String,Set{Int}}`: maps a `String` key to a `Set` of cell ids
- `nodesets::Dict{String,Set{Int}}`: maps a `String` key to a `Set` of global node ids
- `facesets::Dict{String,Set{FaceIndex}}`: maps a `String` to a `Set` of `Set{FaceIndex} (global_cell_id, local_face_id)`
- `edgesets::Dict{String,Set{EdgeIndex}}`: maps a `String` to a `Set` of `Set{EdgeIndex} (global_cell_id, local_edge_id`
- `vertexsets::Dict{String,Set{VertexIndex}}`: maps a `String` key to a `Set` of local vertex ids
- `boundary_matrix::SparseMatrixCSC{Bool,Int}`: optional, only needed by `onboundary` to check if a cell is on the boundary, see, e.g. Helmholtz example
"""
mutable struct Grid{dim,C<:AbstractCell,T<:Real} <: AbstractGrid{dim}
cells::Vector{C}
nodes::Vector{Node{dim,T}}
# Sets
cellsets::Dict{String,Set{Int}}
nodesets::Dict{String,Set{Int}}
facesets::Dict{String,Set{FaceIndex}}
edgesets::Dict{String,Set{EdgeIndex}}
vertexsets::Dict{String,Set{VertexIndex}}
# Boundary matrix (faces per cell × cell)
boundary_matrix::SparseMatrixCSC{Bool,Int}
end
function Grid(cells::Vector{C},
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{FaceIndex}}=Dict{String,Set{FaceIndex}}(),
edgesets::Dict{String,Set{EdgeIndex}}=Dict{String,Set{EdgeIndex}}(),
vertexsets::Dict{String,Set{VertexIndex}}=Dict{String,Set{VertexIndex}}(),
boundary_matrix::SparseMatrixCSC{Bool,Int}=spzeros(Bool, 0, 0)) where {dim,C,T}
return Grid(cells, nodes, cellsets, nodesets, facesets, edgesets, vertexsets, boundary_matrix)
end
##########################
# Grid utility functions #
##########################
"""
getneighborhood(top::ExclusiveTopology, grid::AbstractGrid, cellidx::CellIndex, include_self=false)
getneighborhood(top::ExclusiveTopology, grid::AbstractGrid, faceidx::FaceIndex, include_self=false)
getneighborhood(top::ExclusiveTopology, grid::AbstractGrid, vertexidx::VertexIndex, include_self=false)
getneighborhood(top::ExclusiveTopology, grid::AbstractGrid, edgeidx::EdgeIndex, include_self=false)
Returns all directly connected entities of the same type, i.e. calling the function with a `VertexIndex` will return
a list of directly connected vertices (connected via face/edge). If `include_self` is true, the given `*Index` is included
in the returned list.
!!! warning
This feature is highly experimental and very likely subjected to interface changes in the future.
"""
function getneighborhood(top::ExclusiveTopology, grid::AbstractGrid, cellidx::CellIndex, include_self=false)
patch = getcells(top.cell_neighbor[cellidx.idx])
if include_self
return [patch; cellidx.idx]
else
return patch
end
end
function getneighborhood(top::ExclusiveTopology, grid::AbstractGrid, faceidx::FaceIndex, include_self=false)
if include_self
return [top.face_neighbor[faceidx[1],faceidx[2]].neighbor_info; faceidx]
else
return top.face_neighbor[faceidx[1],faceidx[2]].neighbor_info
end
end
function getneighborhood(top::ExclusiveTopology, grid::AbstractGrid, vertexidx::VertexIndex, include_self=false)
cellid, local_vertexid = vertexidx[1], vertexidx[2]
cell_vertices = vertices(getcells(grid,cellid))
global_vertexid = cell_vertices[local_vertexid]
if include_self
vertex_to_cell = top.vertex_to_cell[global_vertexid]
self_reference_local = Vector{VertexIndex}(undef,length(vertex_to_cell))
for (i,cellid) in enumerate(vertex_to_cell)
local_vertex = VertexIndex(cellid,findfirst(x->x==global_vertexid,vertices(getcells(grid,cellid))))
self_reference_local[i] = local_vertex
end
return [top.vertex_vertex_neighbor[global_vertexid].neighbor_info; self_reference_local]
else
return top.vertex_vertex_neighbor[global_vertexid].neighbor_info
end
end
function getneighborhood(top::ExclusiveTopology, grid::AbstractGrid{3}, edgeidx::EdgeIndex, include_self=false)
cellid, local_edgeidx = edgeidx[1], edgeidx[2]
cell_edges = edges(getcells(grid,cellid))
nonlocal_edgeid = cell_edges[local_edgeidx]
cell_neighbors = getneighborhood(top,grid,CellIndex(cellid))
self_reference_local = EdgeIndex[]
for cellid in cell_neighbors
local_neighbor_edgeid = findfirst(x->issubset(x,nonlocal_edgeid),edges(getcells(grid,cellid)))
local_neighbor_edgeid === nothing && continue
local_edge = EdgeIndex(cellid,local_neighbor_edgeid)
push!(self_reference_local, local_edge)
end
if include_self
return unique([top.edge_neighbor[cellid, local_edgeidx].neighbor_info; self_reference_local; edgeidx])
else
return unique([top.edge_neighbor[cellid, local_edgeidx].neighbor_info; self_reference_local])
end
end
"""
faceskeleton(grid) -> Vector{FaceIndex}
Returns an iterateable face skeleton. The skeleton consists of `FaceIndex` that can be used to `reinit`
`FaceValues`.
"""
faceskeleton(top::ExclusiveTopology, grid::AbstractGrid) = top.face_skeleton
"""
toglobal(grid::AbstractGrid, vertexidx::VertexIndex) -> Int
toglobal(grid::AbstractGrid, vertexidx::Vector{VertexIndex}) -> Vector{Int}
This function takes the local vertex representation (a `VertexIndex`) and looks up the unique global id (an `Int`).
"""
toglobal(grid::AbstractGrid,vertexidx::VertexIndex) = vertices(getcells(grid,vertexidx[1]))[vertexidx[2]]
toglobal(grid::AbstractGrid,vertexidx::Vector{VertexIndex}) = unique(toglobal.((grid,),vertexidx))
@inline getdim(::AbstractGrid{dim}) where {dim} = dim
"""
getcells(grid::AbstractGrid)
getcells(grid::AbstractGrid, v::Union{Int,Vector{Int}}
getcells(grid::AbstractGrid, setname::String)
Returns either all `cells::Collection{C<:AbstractCell}` of a `<:AbstractGrid` or a subset based on an `Int`, `Vector{Int}` or `String`.
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}`.
"""
@inline getcells(grid::AbstractGrid) = grid.cells
@inline getcells(grid::AbstractGrid, v::Union{Int, Vector{Int}}) = grid.cells[v]
@inline getcells(grid::AbstractGrid, setname::String) = grid.cells[collect(getcellset(grid,setname))]
"Returns the number of cells in the `<:AbstractGrid`."
@inline getncells(grid::AbstractGrid) = length(grid.cells)
"Returns the celltype of the `<:AbstractGrid`."
@inline getcelltype(grid::AbstractGrid) = eltype(grid.cells)
@inline getcelltype(grid::AbstractGrid, i::Int) = typeof(grid.cells[i])
"""
getnodes(grid::AbstractGrid)
getnodes(grid::AbstractGrid, v::Union{Int,Vector{Int}}
getnodes(grid::AbstractGrid, setname::String)
Returns either all `nodes::Collection{N}` of a `<:AbstractGrid` or a subset based on an `Int`, `Vector{Int}` or `String`.
The last option tries to call a `nodeset` of the `<:AbstractGrid`. `Collection{N}` refers to some indexable collection where each element corresponds
to a Node.
"""
@inline getnodes(grid::AbstractGrid) = grid.nodes
@inline getnodes(grid::AbstractGrid, v::Union{Int, Vector{Int}}) = grid.nodes[v]
@inline getnodes(grid::AbstractGrid, setname::String) = grid.nodes[collect(getnodeset(grid,setname))]
"Returns the number of nodes in the grid."
@inline getnnodes(grid::AbstractGrid) = length(grid.nodes)
"Returns the number of nodes of the `i`-th cell."
@inline nnodes_per_cell(grid::AbstractGrid, i::Int=1) = nnodes(grid.cells[i])
"Return the number type of the nodal coordinates."
@inline get_coordinate_eltype(grid::AbstractGrid) = get_coordinate_eltype(first(getnodes(grid)))
"""
getcellset(grid::AbstractGrid, setname::String)
Returns all cells as cellid in a `Set` of a given `setname`.
"""
@inline getcellset(grid::AbstractGrid, setname::String) = grid.cellsets[setname]
"""
getcellsets(grid::AbstractGrid)
Returns all cellsets of the `grid`.
"""
@inline getcellsets(grid::AbstractGrid) = grid.cellsets
"""
getnodeset(grid::AbstractGrid, setname::String)
Returns all nodes as nodeid in a `Set` of a given `setname`.
"""
@inline getnodeset(grid::AbstractGrid, setname::String) = grid.nodesets[setname]
"""
getnodesets(grid::AbstractGrid)
Returns all nodesets of the `grid`.
"""
@inline getnodesets(grid::AbstractGrid) = grid.nodesets
"""
getfaceset(grid::AbstractGrid, setname::String)
Returns all faces as `FaceIndex` in a `Set` of a given `setname`.
"""
@inline getfaceset(grid::AbstractGrid, setname::String) = grid.facesets[setname]
"""
getfacesets(grid::AbstractGrid)
Returns all facesets of the `grid`.
"""
@inline getfacesets(grid::AbstractGrid) = grid.facesets
"""
getedgeset(grid::AbstractGrid, setname::String)
Returns all edges as `EdgeIndex` in a `Set` of a given `setname`.
"""
@inline getedgeset(grid::AbstractGrid, setname::String) = grid.edgesets[setname]
"""
getedgesets(grid::AbstractGrid)
Returns all edge sets of the grid.
"""
@inline getedgesets(grid::AbstractGrid) = grid.edgesets
"""
getedgeset(grid::AbstractGrid, setname::String)
Returns all vertices as `VertexIndex` in a `Set` of a given `setname`.
"""
@inline getvertexset(grid::AbstractGrid, setname::String) = grid.vertexsets[setname]
"""
getvertexsets(grid::AbstractGrid)
Returns all vertex sets of the grid.
"""
@inline getvertexsets(grid::AbstractGrid) = grid.vertexsets
n_faces_per_cell(grid::Grid) = nfaces(eltype(grid.cells))
"""
function compute_vertex_values(grid::AbstractGrid, f::Function)
function compute_vertex_values(grid::AbstractGrid, v::Vector{Int}, f::Function)
function compute_vertex_values(grid::AbstractGrid, set::String, f::Function)
Given a `grid` and some function `f`, `compute_vertex_values` computes all nodal values,
i.e. values at the nodes, of the function `f`.
The function implements two dispatches, where only a subset of the grid's node is used.
```julia
compute_vertex_values(grid, x -> sin(x[1]) + cos([2]))
compute_vertex_values(grid, [9, 6, 3], x -> sin(x[1]) + cos([2])) #compute function values at nodes with id 9,6,3
compute_vertex_values(grid, "right", x -> sin(x[1]) + cos([2])) #compute function values at nodes belonging to nodeset right
```
"""
@inline function compute_vertex_values(nodes::Vector{Node{dim,T}}, f::Function) where{dim,T}
map(n -> f(getcoordinates(n)), nodes)
end
@inline function compute_vertex_values(grid::AbstractGrid, f::Function)
compute_vertex_values(getnodes(grid), f::Function)
end
@inline function compute_vertex_values(grid::AbstractGrid, v::Vector{Int}, f::Function)
compute_vertex_values(getnodes(grid, v), f::Function)
end
@inline function compute_vertex_values(grid::AbstractGrid, set::String, f::Function)
compute_vertex_values(getnodes(grid, set), f::Function)
end
# Transformations
"""
transform!(grid::Abstractgrid, f::Function)
Transform all nodes of the `grid` based on some transformation function `f`.
"""
function transform!(g::AbstractGrid, f::Function)
c = similar(g.nodes)
for i in 1:length(c)
c[i] = Node(f(g.nodes[i].x))
end
copyto!(g.nodes, c)
g
end
# Sets
_check_setname(dict, name) = haskey(dict, name) && throw(ArgumentError("there already exists a set with the name: $name"))
_warn_emptyset(set, name) = length(set) == 0 && @warn("no entities added to the set with name: $name")
"""
addcellset!(grid::AbstractGrid, name::String, cellid::Union{Set{Int}, Vector{Int}})
addcellset!(grid::AbstractGrid, name::String, f::function; all::Bool=true)
Adds a cellset to the grid with key `name`.
Cellsets are typically used to define subdomains of the problem, e.g. two materials in the computational domain.
The `MixedDofHandler` can construct different fields which live not on the whole domain, but rather on a cellset.
`all=true` implies that `f(x)` must return `true` for all nodal coordinates `x` in the cell if the cell
should be added to the set, otherwise it suffices that `f(x)` returns `true` for one node.
```julia
addcellset!(grid, "left", Set((1,3))) #add cells with id 1 and 3 to cellset left
addcellset!(grid, "right", x -> norm(x[1]) < 2.0 ) #add cell to cellset right, if x[1] of each cell's node is smaller than 2.0
```
"""
function addcellset!(grid::AbstractGrid, name::String, cellid::Union{Set{Int},Vector{Int}})
_check_setname(grid.cellsets, name)
cells = Set(cellid)
_warn_emptyset(cells, name)
grid.cellsets[name] = cells
grid
end
function addcellset!(grid::AbstractGrid, name::String, f::Function; all::Bool=true)
_check_setname(grid.cellsets, name)
cells = Set{Int}()
for (i, cell) in enumerate(getcells(grid))
pass = all
for node_idx in cell.nodes
node = grid.nodes[node_idx]
v = f(node.x)
all ? (!v && (pass = false; break)) : (v && (pass = true; break))
end
pass && push!(cells, i)
end
_warn_emptyset(cells, name)
grid.cellsets[name] = cells
grid
end
"""
addfaceset!(grid::AbstractGrid, name::String, faceid::Union{Set{FaceIndex},Vector{FaceIndex}})
addfaceset!(grid::AbstractGrid, name::String, f::Function; all::Bool=true)
Adds a faceset to the grid with key `name`.
A faceset maps a `String` key to a `Set` of tuples corresponding to `(global_cell_id, local_face_id)`.
Facesets are used to initialize `Dirichlet` structs, that are needed to specify the boundary for the `ConstraintHandler`.
`all=true` implies that `f(x)` must return `true` for all nodal coordinates `x` on the face if the face
should be added to the set, otherwise it suffices that `f(x)` returns `true` for one node.
```julia
addfaceset!(grid, "right", Set(((2,2),(4,2))) #see grid manual example for reference
addfaceset!(grid, "clamped", x -> norm(x[1]) ≈ 0.0) #see incompressible elasticity example for reference
```
"""
addfaceset!(grid::Grid, name::String, set::Union{Set{FaceIndex},Vector{FaceIndex}}) =
_addset!(grid, name, set, grid.facesets)
addedgeset!(grid::Grid, name::String, set::Union{Set{EdgeIndex},Vector{EdgeIndex}}) =
_addset!(grid, name, set, grid.edgesets)
addvertexset!(grid::Grid, name::String, set::Union{Set{VertexIndex},Vector{VertexIndex}}) =
_addset!(grid, name, set, grid.vertexsets)
function _addset!(grid::AbstractGrid, name::String, _set, dict::Dict)
_check_setname(dict, name)
set = Set(_set)
_warn_emptyset(set, name)
dict[name] = set
grid
end
addfaceset!(grid::AbstractGrid, name::String, f::Function; all::Bool=true) =
_addset!(grid, name, f, Ferrite.faces, grid.facesets, FaceIndex; all=all)
addedgeset!(grid::AbstractGrid, name::String, f::Function; all::Bool=true) =
_addset!(grid, name, f, Ferrite.edges, grid.edgesets, EdgeIndex; all=all)
addvertexset!(grid::AbstractGrid, name::String, f::Function; all::Bool=true) =
_addset!(grid, name, f, Ferrite.vertices, grid.vertexsets, VertexIndex; all=all)
function _addset!(grid::AbstractGrid, name::String, f::Function, _ftype::Function, dict::Dict, _indextype::Type; all::Bool=true)
_check_setname(dict, name)
_set = Set{_indextype}()
for (cell_idx, cell) in enumerate(getcells(grid))
for (face_idx, face) in enumerate(_ftype(cell))
pass = all
for node_idx in face
v = f(grid.nodes[node_idx].x)
all ? (!v && (pass = false; break)) : (v && (pass = true; break))
end
pass && push!(_set, _indextype(cell_idx, face_idx))
end
end
_warn_emptyset(_set, name)
dict[name] = _set
grid
end
"""
addnodeset!(grid::AbstractGrid, name::String, nodeid::Union{Vector{Int},Set{Int}})
addnodeset!(grid::AbstractGrid, name::String, f::Function)
Adds a `nodeset::Dict{String, Set{Int}}` to the `grid` with key `name`. Has the same interface as `addcellset`.
However, instead of mapping a cell id to the `String` key, a set of node ids is returned.
"""
function addnodeset!(grid::AbstractGrid, name::String, nodeid::Union{Vector{Int},Set{Int}})
_check_setname(grid.nodesets, name)
grid.nodesets[name] = Set(nodeid)
_warn_emptyset(grid.nodesets[name], name)
grid
end
function addnodeset!(grid::AbstractGrid, name::String, f::Function)
_check_setname(grid.nodesets, name)
nodes = Set{Int}()
for (i, n) in enumerate(getnodes(grid))
f(n.x) && push!(nodes, i)
end
grid.nodesets[name] = nodes
_warn_emptyset(grid.nodesets[name], name)
grid
end
"""
getcoordinates!(x::Vector{Vec{dim,T}}, grid::AbstractGrid, cell::Int)
getcoordinates!(x::Vector{Vec{dim,T}}, grid::AbstractGrid, cell::AbstractCell)
Fills the vector `x` with the coordinates of a cell defined by either its cellid or the cell object itself.
"""
@inline function getcoordinates!(x::Vector{Vec{dim,T}}, grid::Ferrite.AbstractGrid, cellid::Int) where {dim,T}
cell = getcells(grid, cellid)
getcoordinates!(x, grid, cell)
end
@inline function getcoordinates!(x::Vector{Vec{dim,T}}, grid::Ferrite.AbstractGrid, cell::Ferrite.AbstractCell) where {dim,T}
@inbounds for i in 1:length(x)
x[i] = getcoordinates(getnodes(grid, cell.nodes[i]))
end
return x
end
@inline getcoordinates!(x::Vector{Vec{dim,T}}, grid::AbstractGrid, cell::CellIndex) where {dim, T} = getcoordinates!(x, grid, cell.idx)
@inline getcoordinates!(x::Vector{Vec{dim,T}}, grid::AbstractGrid, face::FaceIndex) where {dim, T} = getcoordinates!(x, grid, face.idx[1])
# TODO: Deprecate one of `cellcoords!` and `getcoordinates!`, as they do the same thing
cellcoords!(global_coords::Vector{Vec{dim,T}}, grid::AbstractGrid{dim}, i::Int) where {dim,T} = getcoordinates!(global_coords, grid, i)
"""
getcoordinates(grid::AbstractGrid, cell)
Return a vector with the coordinates of the vertices of cell number `cell`.
"""
@inline function getcoordinates(grid::AbstractGrid, cell::Int)
dim = getdim(grid)
T = get_coordinate_eltype(grid)
_cell = getcells(grid, cell)
N = nnodes(_cell)
x = Vector{Vec{dim, T}}(undef, N)
getcoordinates!(x, grid, _cell)
end
@inline getcoordinates(grid::AbstractGrid, cell::CellIndex) = getcoordinates(grid, cell.idx)
@inline getcoordinates(grid::AbstractGrid, face::FaceIndex) = getcoordinates(grid, face.idx[1])
function cellnodes!(global_nodes::Vector{Int}, grid::AbstractGrid, i::Int)
cell = getcells(grid, i)
_cellnodes!(global_nodes, cell)
end
function _cellnodes!(global_nodes::Vector{Int}, cell::AbstractCell)
@assert length(global_nodes) == nnodes(cell)
@inbounds for i in 1:length(global_nodes)
global_nodes[i] = cell.nodes[i]
end
return global_nodes
end
function Base.show(io::IO, ::MIME"text/plain", grid::Grid)
print(io, "$(typeof(grid)) with $(getncells(grid)) ")
if isconcretetype(eltype(grid.cells))
typestrs = [repr(eltype(grid.cells))]
else
typestrs = sort!(repr.(Set(typeof(x) for x in grid.cells)))
end
join(io, typestrs, '/')
print(io, " cells and $(getnnodes(grid)) nodes")
end
# Functions to uniquely identify vertices, edges and faces, used when distributing
# dofs over a mesh. For this we can ignore the nodes on edged, faces and inside cells,
# we only need to use the nodes that are vertices.
# 1D: vertices
faces(c::Union{Line,QuadraticLine}) = (c.nodes[1], c.nodes[2])
vertices(c::Union{Line,Line2D,Line3D,QuadraticLine}) = (c.nodes[1], c.nodes[2])
# 2D: vertices, faces
faces(c::Line2D) = ((c.nodes[1],c.nodes[2]),)
vertices(c::Union{Triangle,QuadraticTriangle}) = (c.nodes[1], c.nodes[2], c.nodes[3])
faces(c::Union{Triangle,QuadraticTriangle}) = ((c.nodes[1],c.nodes[2]), (c.nodes[2],c.nodes[3]), (c.nodes[3],c.nodes[1]))
vertices(c::Union{Quadrilateral,Quadrilateral3D,QuadraticQuadrilateral}) = (c.nodes[1], c.nodes[2], c.nodes[3], c.nodes[4])
faces(c::Union{Quadrilateral,QuadraticQuadrilateral}) = ((c.nodes[1],c.nodes[2]), (c.nodes[2],c.nodes[3]), (c.nodes[3],c.nodes[4]), (c.nodes[4],c.nodes[1]))
# 3D: vertices, edges, faces
edges(c::Line3D) = ((c.nodes[1],c.nodes[2]),)
vertices(c::Union{Tetrahedron,QuadraticTetrahedron}) = (c.nodes[1], c.nodes[2], c.nodes[3], c.nodes[4])
edges(c::Union{Tetrahedron,QuadraticTetrahedron}) = ((c.nodes[1],c.nodes[2]), (c.nodes[2],c.nodes[3]), (c.nodes[3],c.nodes[1]), (c.nodes[1],c.nodes[4]), (c.nodes[2],c.nodes[4]), (c.nodes[3],c.nodes[4]))
faces(c::Union{Tetrahedron,QuadraticTetrahedron}) = ((c.nodes[1],c.nodes[3],c.nodes[2]), (c.nodes[1],c.nodes[2],c.nodes[4]), (c.nodes[2],c.nodes[3],c.nodes[4]), (c.nodes[1],c.nodes[4],c.nodes[3]))
vertices(c::Union{Hexahedron,Cell{3,20,6}}) = (c.nodes[1], c.nodes[2], c.nodes[3], c.nodes[4], c.nodes[5], c.nodes[6], c.nodes[7], c.nodes[8])
edges(c::Union{Hexahedron,Cell{3,20,6}}) = ((c.nodes[1],c.nodes[2]), (c.nodes[2],c.nodes[3]), (c.nodes[3],c.nodes[4]), (c.nodes[4],c.nodes[1]), (c.nodes[5],c.nodes[6]), (c.nodes[6],c.nodes[7]), (c.nodes[7],c.nodes[8]), (c.nodes[8],c.nodes[5]), (c.nodes[1],c.nodes[5]), (c.nodes[2],c.nodes[6]), (c.nodes[3],c.nodes[7]), (c.nodes[4],c.nodes[8]))
faces(c::Union{Hexahedron,Cell{3,20,6}}) = ((c.nodes[1],c.nodes[4],c.nodes[3],c.nodes[2]), (c.nodes[1],c.nodes[2],c.nodes[6],c.nodes[5]), (c.nodes[2],c.nodes[3],c.nodes[7],c.nodes[6]), (c.nodes[3],c.nodes[4],c.nodes[8],c.nodes[7]), (c.nodes[1],c.nodes[5],c.nodes[8],c.nodes[4]), (c.nodes[5],c.nodes[6],c.nodes[7],c.nodes[8]))
edges(c::Union{Quadrilateral3D}) = ((c.nodes[1],c.nodes[2]), (c.nodes[2],c.nodes[3]), (c.nodes[3],c.nodes[4]), (c.nodes[4],c.nodes[1]))
faces(c::Union{Quadrilateral3D}) = ((c.nodes[1],c.nodes[2],c.nodes[3],c.nodes[4]),)
vertices(c::Wedge) = (c.nodes[1], c.nodes[2], c.nodes[3], c.nodes[4], c.nodes[5], c.nodes[6])
edges(c::Wedge) = ((c.nodes[2],c.nodes[1]), (c.nodes[1],c.nodes[3]), (c.nodes[1],c.nodes[4]), (c.nodes[3],c.nodes[2]), (c.nodes[2],c.nodes[5]), (c.nodes[3],c.nodes[6]), (c.nodes[4],c.nodes[5]), (c.nodes[4],c.nodes[6]), (c.nodes[6],c.nodes[5]))
faces(c::Wedge) = ((c.nodes[1],c.nodes[3],c.nodes[2]), (c.nodes[1],c.nodes[2],c.nodes[5],c.nodes[4]), (c.nodes[3],c.nodes[1],c.nodes[4],c.nodes[6]), (c.nodes[2],c.nodes[3],c.nodes[6],c.nodes[5]), (c.nodes[4],c.nodes[5],c.nodes[6]))
# random stuff
default_interpolation(::Union{Type{Line},Type{Line2D},Type{Line3D}}) = Lagrange{1,RefCube,1}()
default_interpolation(::Type{QuadraticLine}) = Lagrange{1,RefCube,2}()
default_interpolation(::Type{Triangle}) = Lagrange{2,RefTetrahedron,1}()
default_interpolation(::Type{QuadraticTriangle}) = Lagrange{2,RefTetrahedron,2}()
default_interpolation(::Union{Type{Quadrilateral},Type{Quadrilateral3D}}) = Lagrange{2,RefCube,1}()
default_interpolation(::Type{QuadraticQuadrilateral}) = Lagrange{2,RefCube,2}()
default_interpolation(::Type{Tetrahedron}) = Lagrange{3,RefTetrahedron,1}()
default_interpolation(::Type{QuadraticTetrahedron}) = Lagrange{3,RefTetrahedron,2}()
default_interpolation(::Type{Hexahedron}) = Lagrange{3,RefCube,1}()
default_interpolation(::Type{Cell{3,20,6}}) = Serendipity{3,RefCube,2}()
default_interpolation(::Type{Wedge}) = Lagrange{3,RefPrism,1}()
"""
boundaryfunction(::Type{<:BoundaryIndex})
Helper function to dispatch on the correct entity from a given boundary index.
"""
boundaryfunction(::Type{<:BoundaryIndex})
boundaryfunction(::Type{FaceIndex}) = Ferrite.faces
boundaryfunction(::Type{EdgeIndex}) = Ferrite.edges
boundaryfunction(::Type{VertexIndex}) = Ferrite.vertices
for INDEX in (:VertexIndex, :EdgeIndex, :FaceIndex)
@eval begin
#Constructor
($INDEX)(a::Int, b::Int) = ($INDEX)((a,b))
Base.getindex(I::($INDEX), i::Int) = I.idx[i]
#To be able to do a,b = faceidx
Base.iterate(I::($INDEX), state::Int=1) = (state==3) ? nothing : (I[state], state+1)
#For (cellid, faceidx) in faceset
Base.in(v::Tuple{Int, Int}, s::Set{$INDEX}) = in($INDEX(v), s)
end
end