-
Notifications
You must be signed in to change notification settings - Fork 98
/
UnstructuredGridTopologies.jl
492 lines (387 loc) · 12.7 KB
/
UnstructuredGridTopologies.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
"""
struct UnstructuredGridTopology{Dc,Dp,T,O} <: GridTopology{Dc,Dp}
# private fields
end
"""
struct UnstructuredGridTopology{Dc,Dp,T,O} <: GridTopology{Dc,Dp}
vertex_coordinates::Vector{Point{Dp,T}}
n_m_to_nface_to_mfaces::Matrix{Table{Int32,Vector{Int32},Vector{Int32}}}
cell_type::Vector{Int8}
polytopes::Vector{Polytope{Dc}}
orientation_style::O
end
# Constructors
"""
UnstructuredGridTopology(
vertex_coordinates::Vector{<:Point},
cell_vertices::Table,
cell_type::Vector{<:Integer},
polytopes::Vector{<:Polytope},
orientation_style::OrientationStyle=NonOriented())
"""
function UnstructuredGridTopology(
vertex_coordinates::Vector{<:Point},
cell_vertices::Table,
cell_type::Vector{<:Integer},
polytopes::Vector{<:Polytope},
orientation_style::OrientationStyle=NonOriented())
D = num_dims(first(polytopes))
n = D+1
n_m_to_nface_to_mfaces = Matrix{Table{Int32,Vector{Int32},Vector{Int32}}}(undef,n,n)
n_m_to_nface_to_mfaces[D+1,0+1] = cell_vertices
vertex_cells = generate_cells_around(cell_vertices,length(vertex_coordinates))
n_m_to_nface_to_mfaces[0+1,D+1] = vertex_cells
P = eltype(vertex_coordinates)
Dp = length(P)
T = eltype(P)
O = typeof(orientation_style)
UnstructuredGridTopology{D,Dp,T,O}(
vertex_coordinates,
n_m_to_nface_to_mfaces,
cell_type,
polytopes,orientation_style)
end
"""
UnstructuredGridTopology(
vertex_coordinates::Vector{<:Point},
d_to_dface_vertices::Vector{<:Table},
cell_type::Vector{<:Integer},
polytopes::Vector{<:Polytope},
orientation_style::OrientationStyle=NonOriented())
"""
function UnstructuredGridTopology(
vertex_coordinates::Vector{<:Point},
d_to_dface_vertices::Vector{<:Table},
cell_type::Vector{<:Integer},
polytopes::Vector{<:Polytope},
orientation_style::OrientationStyle=NonOriented())
D = num_dims(first(polytopes))
n = D+1
n_m_to_nface_to_mfaces = Matrix{Table{Int32,Vector{Int32},Vector{Int32}}}(undef,n,n)
nvertices = length(vertex_coordinates)
for d in 0:D
dface_to_vertices = d_to_dface_vertices[d+1]
n_m_to_nface_to_mfaces[d+1,0+1] = dface_to_vertices
vertex_to_dfaces = generate_cells_around(dface_to_vertices,nvertices)
n_m_to_nface_to_mfaces[0+1,d+1] = vertex_to_dfaces
end
P = eltype(vertex_coordinates)
Dp = length(P)
T = eltype(P)
O = typeof(orientation_style)
UnstructuredGridTopology{D,Dp,T,O}(
vertex_coordinates,
n_m_to_nface_to_mfaces,
cell_type,
polytopes,
orientation_style)
end
"""
UnstructuredGridTopology(topo::GridTopology)
"""
function UnstructuredGridTopology(topo::GridTopology)
vertex_coordinates = collect1d(get_vertex_coordinates(topo))
cell_type = collect1d(get_cell_type(topo))
polytopes = get_polytopes(topo)
D = num_cell_dims(topo)
d_to_dface_vertices = [ Table(get_faces(topo,d,0)) for d in 0:D ]
orientation = OrientationStyle(topo)
UnstructuredGridTopology(
vertex_coordinates,
d_to_dface_vertices,
cell_type,
polytopes,
orientation)
end
function UnstructuredGridTopology(topo::UnstructuredGridTopology)
topo
end
"""
UnstructuredGridTopology(grid::UnstructuredGrid)
UnstructuredGridTopology(
grid::UnstructuredGrid,
cell_to_vertices::Table,
vertex_to_node::AbstractVector)
"""
function UnstructuredGridTopology(grid::UnstructuredGrid)
cell_to_vertices, vertex_to_node, = _generate_cell_to_vertices_from_grid(grid)
_generate_grid_topology_from_grid(grid,cell_to_vertices,vertex_to_node)
end
function UnstructuredGridTopology(grid::UnstructuredGrid, cell_to_vertices::Table, vertex_to_node::AbstractVector)
_generate_grid_topology_from_grid(grid,cell_to_vertices,vertex_to_node)
end
function _generate_grid_topology_from_grid(grid::UnstructuredGrid,cell_to_vertices,vertex_to_node)
@notimplementedif (! is_regular(grid)) "Extrtacting the GridTopology form a Grid only implemented for the regular case"
node_to_coords = get_node_coordinates(grid)
if vertex_to_node == 1:num_nodes(grid)
vertex_to_coords = node_to_coords
else
vertex_to_coords = node_to_coords[vertex_to_node]
end
cell_to_type = get_cell_type(grid)
polytopes = map(get_polytope, get_reffes(grid))
UnstructuredGridTopology(
vertex_to_coords,
cell_to_vertices,
cell_to_type,
polytopes,
OrientationStyle(grid))
end
function _generate_cell_to_vertices_from_grid(grid::UnstructuredGrid)
if is_first_order(grid)
cell_to_vertices = Table(get_cell_node_ids(grid))
vertex_to_node = collect(1:num_nodes(grid))
node_to_vertex = vertex_to_node
else
cell_to_nodes = get_cell_node_ids(grid)
cell_to_cell_type = get_cell_type(grid)
reffes = get_reffes(grid)
cell_type_to_lvertex_to_lnode = map(get_vertex_node, reffes)
cell_to_vertices, vertex_to_node, node_to_vertex = _generate_cell_to_vertices(
cell_to_nodes,
cell_to_cell_type,
cell_type_to_lvertex_to_lnode,
num_nodes(grid))
end
(cell_to_vertices, vertex_to_node, node_to_vertex)
end
function _generate_cell_to_vertices(
cell_to_nodes::Table,
cell_to_cell_type::AbstractVector{<:Integer},
cell_type_to_lvertex_to_lnode::Vector{Vector{Int}},
nnodes::Int=maximum(cell_to_nodes.data))
data, ptrs, vertex_to_node, node_to_vertex = _generate_cell_to_vertices(
cell_to_nodes.data,
cell_to_nodes.ptrs,
cell_to_cell_type,
cell_type_to_lvertex_to_lnode,
nnodes)
(Table(data,ptrs), vertex_to_node, node_to_vertex)
end
function _generate_cell_to_vertices(
cell_to_nodes_data,
cell_to_nodes_ptrs,
cell_to_cell_type,
cell_type_to_lvertex_to_lnode,
nnodes)
cell_to_vertices_ptrs = similar(cell_to_nodes_ptrs)
cell_type_to_nlvertices = map(length,cell_type_to_lvertex_to_lnode)
_generate_cell_to_vertices_count!(
cell_to_vertices_ptrs,
cell_to_cell_type,
cell_type_to_nlvertices)
T = eltype(cell_to_nodes_data)
node_to_vertex = fill(T(UNSET),nnodes)
length_to_ptrs!(cell_to_vertices_ptrs)
ndata = cell_to_vertices_ptrs[end]-1
cell_to_vertices_data = zeros(T,ndata)
_generate_cell_to_vertices_fill!(
cell_to_vertices_data,
cell_to_vertices_ptrs,
cell_to_nodes_data,
cell_to_nodes_ptrs,
node_to_vertex,
cell_to_cell_type,
cell_type_to_lvertex_to_lnode)
vertex_to_node = find_inverse_index_map(node_to_vertex)
(cell_to_vertices_data, cell_to_vertices_ptrs, vertex_to_node, node_to_vertex)
end
function _generate_cell_to_vertices_count!(
cell_to_vertices_ptrs,
cell_to_cell_type,
cell_type_to_nlvertices)
cells = 1:length(cell_to_cell_type)
for cell in cells
cell_type = cell_to_cell_type[cell]
nlvertices = cell_type_to_nlvertices[cell_type]
cell_to_vertices_ptrs[1+cell] = nlvertices
end
end
function _generate_cell_to_vertices_fill!(
cell_to_vertices_data,
cell_to_vertices_ptrs,
cell_to_nodes_data,
cell_to_nodes_ptrs,
node_to_vertex,
cell_to_cell_type,
cell_type_to_lvertex_to_lnode)
cells = 1:length(cell_to_cell_type)
vertex = 1
for cell in cells
cell_type = cell_to_cell_type[cell]
a = cell_to_nodes_ptrs[cell]-1
b = cell_to_vertices_ptrs[cell]-1
lvertex_to_lnode = cell_type_to_lvertex_to_lnode[cell_type]
for (lvertex, lnode) in enumerate(lvertex_to_lnode)
node = cell_to_nodes_data[a+lnode]
if node_to_vertex[node] == UNSET
node_to_vertex[node] = vertex
vertex += 1
end
cell_to_vertices_data[b+lvertex] = node_to_vertex[node]
end
end
end
function GridTopology(::Type{<:Polytope{D}},topo::GridTopology{D}) where D
topo
end
function GridTopology(::Type{<:Polytope{D}},topo::GridTopology) where D
_topo = UnstructuredGridTopology(topo)
GridTopology(Polytope{D},_topo)
end
function GridTopology(::Type{<:Polytope{D}},topo::UnstructuredGridTopology) where D
vertex_coordinates = get_vertex_coordinates(topo)
cell_type = get_face_type(topo,D)
polytopes = get_reffaces(Polytope{D},topo)
d_to_dface_vertices = [ get_faces(topo,d,0) for d in 0:D ]
orientation = OrientationStyle(topo)
topo_d = UnstructuredGridTopology(
vertex_coordinates,
d_to_dface_vertices,
cell_type,
polytopes,
orientation)
end
# Needed, do not remove
function num_faces(g::UnstructuredGridTopology,d::Integer)
if d == 0
length(g.vertex_coordinates)
elseif d == num_cell_dims(g)
length(g.cell_type)
else
D = num_cell_dims(g)
face_to_cells = get_faces(g,d,D)
length(face_to_cells)
end
end
# Implementation of abstract API
OrientationStyle(::Type{UnstructuredGridTopology{Dc,Dp,T,O}}) where {Dc,Dp,T,O} = O()
get_vertex_coordinates(g::UnstructuredGridTopology) = g.vertex_coordinates
get_cell_type(g::UnstructuredGridTopology) = g.cell_type
get_polytopes(g::UnstructuredGridTopology) = collect1d(g.polytopes)
function get_faces(g::UnstructuredGridTopology, dimfrom::Integer, dimto::Integer)
_setup_faces!(g,dimfrom,dimto)
g.n_m_to_nface_to_mfaces[dimfrom+1,dimto+1]
end
function _setup_faces!(g,dimfrom,dimto)
if isassigned(g.n_m_to_nface_to_mfaces,dimfrom+1,dimto+1)
return nothing
end
D = num_cell_dims(g)
if dimfrom==0
if dimto==0
_setup_face_to_face!(g,dimfrom)
elseif dimto==D
nothing
else
_setup_face_to_vertices!(g,dimto)
end
elseif dimfrom == D
if dimto==0
nothing
elseif dimto==D
_setup_face_to_face!(g,dimfrom)
else
_setup_cell_to_faces!(g,dimto)
end
else
if dimto==0
_setup_face_to_vertices!(g,dimfrom)
elseif dimto==D
_setup_cell_to_faces!(g,dimfrom)
elseif dimto==dimfrom
_setup_face_to_face!(g,dimfrom)
elseif dimfrom > dimto
_setup_nface_to_mface!(g,dimfrom,dimto)
else
_setup_nface_to_mface!(g,dimto,dimfrom)
end
end
nothing
end
function _setup_cell_to_faces!(model,dimto)
D = num_cell_dims(model)
if isassigned(model.n_m_to_nface_to_mfaces,D+1,dimto+1)
return
end
if isassigned(model.n_m_to_nface_to_mfaces,dimto+1,0+1)
cell_to_vertices = get_faces(model,D,0)
vertex_to_faces = get_faces(model,0,dimto)
cell_to_cell_type = get_cell_type(model)
polytopes = get_polytopes(model)
cell_type_to_lface_to_lvertices = map( (p)->get_faces(p,dimto,0), polytopes )
cell_to_faces = find_cell_to_faces(
cell_to_vertices,
cell_type_to_lface_to_lvertices,
cell_to_cell_type,
vertex_to_faces)
else
cell_to_vertices = get_faces(model,D,0)
vertex_to_cells = get_faces(model,0,D)
cell_to_cell_type = get_cell_type(model)
polytopes = get_polytopes(model)
cell_type_to_lface_to_lvertices = map( (p)->get_faces(p,dimto,0), polytopes )
cell_to_faces = generate_cell_to_faces(
cell_to_vertices,
cell_type_to_lface_to_lvertices,
cell_to_cell_type,
vertex_to_cells)
end
faces_to_cells = generate_cells_around(cell_to_faces)
model.n_m_to_nface_to_mfaces[D+1,dimto+1] = cell_to_faces
model.n_m_to_nface_to_mfaces[dimto+1,D+1] = faces_to_cells
nothing
end
function _setup_face_to_vertices!(model,dimfrom)
if isassigned(model.n_m_to_nface_to_mfaces,dimfrom+1,0+1)
return
end
D = num_cell_dims(model)
cell_to_vertices = get_faces(model,D,0)
cell_to_faces = get_faces(model,D,dimfrom)
cell_to_ctype = get_cell_type(model)
polytopes = get_polytopes(model)
ctype_to_lface_to_lvertices = map( (p)->get_faces(p,dimfrom,0), polytopes )
nfaces = num_faces(model,dimfrom)
face_to_vertices = generate_face_to_vertices(
cell_to_vertices,
cell_to_faces,
cell_to_ctype,
ctype_to_lface_to_lvertices,
nfaces)
nvertices = num_faces(model,0)
vertex_to_faces = generate_cells_around(face_to_vertices,nvertices)
model.n_m_to_nface_to_mfaces[dimfrom+1,0+1] = face_to_vertices
model.n_m_to_nface_to_mfaces[0+1,dimfrom+1] = vertex_to_faces
nothing
end
function _setup_face_to_face!(model,d)
if isassigned(model.n_m_to_nface_to_mfaces,d+1,d+1)
return
end
nfaces = num_faces(model,d)
id = identity_table(Int32,Int32,nfaces)
model.n_m_to_nface_to_mfaces[d+1,d+1] = id
return
end
function _setup_nface_to_mface!(model,n,m)
@assert n > m
if isassigned(model.n_m_to_nface_to_mfaces,n+1,m+1)
return
end
nface_to_vertices = get_faces(model,n,0)
vertex_to_mfaces = get_faces(model,0,m)
nface_to_nftype = get_face_type(model,n)
polytopes = get_reffaces(Polytope{n},model)
nftype_to_lmface_to_lvertices = map( (p)->get_faces(p,m,0), polytopes)
nface_to_mfaces = find_cell_to_faces(
nface_to_vertices,
nftype_to_lmface_to_lvertices,
nface_to_nftype,
vertex_to_mfaces)
num_mfaces = num_faces(model,m)
mface_to_nfaces = generate_cells_around(nface_to_mfaces,num_mfaces)
model.n_m_to_nface_to_mfaces[n+1,m+1] = nface_to_mfaces
model.n_m_to_nface_to_mfaces[m+1,n+1] = mface_to_nfaces
nothing
end