-
Notifications
You must be signed in to change notification settings - Fork 94
/
CartesianDiscreteModels.jl
474 lines (414 loc) · 13.4 KB
/
CartesianDiscreteModels.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
"""
struct CartesianDiscreteModel{D,T,F} <: DiscreteModel{D,D}
# Private Fields
end
"""
struct CartesianDiscreteModel{D,T,F} <: DiscreteModel{D,D}
grid::CartesianGrid{D,T,F}
grid_topology::UnstructuredGridTopology{D,D,T,Oriented}
face_labeling::FaceLabeling
@doc """
CartesianDiscreteModel(desc::CartesianDescriptor)
Inner constructor
"""
function CartesianDiscreteModel(desc::CartesianDescriptor{D,T,F}) where {D,T,F}
grid = CartesianGrid(desc)
_grid = UnstructuredGrid(grid)
if any(desc.isperiodic)
topo = _cartesian_grid_topology_with_periodic_bcs(_grid, desc.isperiodic, desc.partition)
else
topo = UnstructuredGridTopology(_grid)
end
nfaces = [num_faces(topo,d) for d in 0:num_cell_dims(topo)]
labels = FaceLabeling(nfaces)
_fill_cartesian_face_labeling!(labels,topo)
new{D,T,F}(grid,topo,labels)
end
@doc """
CartesianDiscreteModel(desc::CartesianDescriptor{D,T,F},
cmin::CartesianIndex,
cmax::CartesianIndex)
Builds a CartesianDiscreteModel object which represents a subgrid of
a (larger) grid represented by desc. This subgrid is described by its
D-dimensional minimum (cmin) and maximum (cmax) CartesianIndex
identifiers.
Inner constructor
"""
function CartesianDiscreteModel(desc::CartesianDescriptor{D,T,F},
cmin::CartesianIndex,
cmax::CartesianIndex) where {D,T,F}
suborigin = Tuple(desc.origin) .+ (Tuple(cmin) .- 1) .* desc.sizes
subpartition = Tuple(cmax) .- Tuple(cmin) .+ 1
subsizes = desc.sizes
subdesc =
CartesianDescriptor(Point(suborigin), subsizes, subpartition; map=desc.map, isperiodic=desc.isperiodic)
grid = CartesianGrid(subdesc)
_grid = UnstructuredGrid(grid)
topo = UnstructuredGridTopology(_grid)
nfaces = [num_faces(topo, d) for d = 0:num_cell_dims(topo)]
labels = FaceLabeling(nfaces)
_fill_subgrid_cartesian_face_labeling!(labels,topo,subdesc,desc,cmin)
new{D,T,F}(grid, topo, labels)
end
end
"""
CartesianDiscreteModel(args...)
Same args needed to construct a `CartesianDescriptor`
"""
function CartesianDiscreteModel(args...; kwargs...)
desc = CartesianDescriptor(args...; kwargs...)
CartesianDiscreteModel(desc)
end
"""
get_cartesian_descriptor(model::CartesianDiscreteModel)
"""
function get_cartesian_descriptor(model::CartesianDiscreteModel)
get_cartesian_descriptor(model.grid)
end
# Interface
get_grid(model::CartesianDiscreteModel) = model.grid
get_grid_topology(model::CartesianDiscreteModel) = model.grid_topology
get_face_labeling(model::CartesianDiscreteModel) = model.face_labeling
# These needed to be type stable
function get_face_nodes(model::CartesianDiscreteModel,d::Integer)
face_nodes::Table{Int32,Vector{Int32},Vector{Int32}} = compute_face_nodes(model,d)
face_nodes
end
function get_face_type(model::CartesianDiscreteModel,d::Integer)
_, face_to_ftype::Vector{Int8} = compute_reffaces(ReferenceFE{d},model)
face_to_ftype
end
function get_reffaces(::Type{ReferenceFE{d}},model::CartesianDiscreteModel) where d
reffaces::Vector{LagrangianRefFE{d}},_ = compute_reffaces(ReferenceFE{d},model)
reffaces
end
# Helpers
function _fill_cartesian_face_labeling!(labels,topo)
_fill_cartesian_entities!(labels,topo)
_add_cartesian_tags!(labels,topo)
end
function _fill_cartesian_entities!(labels,topo)
D = num_cell_dims(topo)
d_to_dface_to_entity = labels.d_to_dface_to_entity
polytope = first(get_polytopes(topo))
dim_to_offset = get_offsets(polytope)
interior_id = num_faces(polytope)
boundary_id = -1
for d in 0:(D-1)
face_to_cells = get_faces(topo,d,D)
cell_to_faces = get_faces(topo,D,d)
offset = dim_to_offset[d+1]
_generate_pre_geolabel!(
d_to_dface_to_entity[d+1],
face_to_cells,
cell_to_faces,
offset,
interior_id,boundary_id,d,D)
end
_fix_geolabels(D, topo, d_to_dface_to_entity, interior_id, boundary_id)
fill!(d_to_dface_to_entity[end],interior_id)
end
function _fix_geolabels(D, topo, d_to_dface_to_entity, interior_id, boundary_id)
for d = 0:(D-2)
for j = (d+1):(D-1)
dface_to_jfaces = get_faces(topo, d, j)
dface_to_geolabel = d_to_dface_to_entity[d+1]
jface_to_geolabel = d_to_dface_to_entity[j+1]
_fix_dface_geolabels!(
dface_to_geolabel,
jface_to_geolabel,
dface_to_jfaces.data,
dface_to_jfaces.ptrs,
interior_id,
boundary_id,
)
end
end
end
function _add_cartesian_tags!(labels,topo)
D = num_cell_dims(topo)
polytope = first(get_polytopes(topo))
interior_id = num_faces(polytope)
boundary_ids = collect(1:(interior_id-1))
for i in boundary_ids
name = lpad(i,ceil(Int,log10(interior_id)),'0')
add_tag!(labels,"tag_$(name)",[i])
end
add_tag!(labels,"interior",[interior_id])
add_tag!(labels,"boundary",boundary_ids)
end
function _generate_pre_geolabel!(
face_to_geolabel,
face_to_cells,
cell_to_faces,
offset,
interior_id,
boundary_id,d,D)
nfaces = length(face_to_cells)
fill!(face_to_geolabel,interior_id)
max_ncells_around = 2^(D-d)
_generate_pre_geolabel_kernel!(
face_to_geolabel,
face_to_cells.data,
face_to_cells.ptrs,
cell_to_faces.data,
cell_to_faces.ptrs,
offset,boundary_id,max_ncells_around)
face_to_geolabel
end
function _generate_pre_geolabel_kernel!(
face_to_geolabel,
face_to_cells_data,
face_to_cells_ptrs,
cell_to_faces_data,
cell_to_faces_ptrs,
offset,boundary_id,max_ncells_around)
nfaces = length(face_to_geolabel)
for face in 1:nfaces
a = face_to_cells_ptrs[face]-1
ncells_around = face_to_cells_ptrs[face+1] - (a+1)
if ncells_around == 1
icell_around = 1
cell = face_to_cells_data[a+icell_around]
b = cell_to_faces_ptrs[cell]-1
nlfaces = cell_to_faces_ptrs[cell+1] - (b+1)
for lface in 1:nlfaces
face2 = cell_to_faces_data[b+lface]
if face == face2
face_to_geolabel[face] = lface + offset
break
end
end
elseif ncells_around != max_ncells_around
face_to_geolabel[face] = boundary_id
end
end
end
function _fix_dface_geolabels!(
dface_to_geolabel,
jface_to_geolabel,
dface_to_jfaces_data,
dface_to_jfaces_ptrs,
interior_id,boundary_id)
ndfaces = length(dface_to_jfaces_ptrs)-1
for dface in 1:ndfaces
if dface_to_geolabel[dface] != boundary_id
continue
end
a = dface_to_jfaces_ptrs[dface]
b = dface_to_jfaces_ptrs[dface+1]-1
for p in a:b
jface = dface_to_jfaces_data[p]
geolabel = jface_to_geolabel[jface]
if geolabel != interior_id && geolabel != boundary_id
dface_to_geolabel[dface] = geolabel
break
end
end
end
end
function _fill_subgrid_cartesian_face_labeling!(labels,topo,subdesc,desc,cmin)
_fill_subgrid_cartesian_entities!(labels,topo,subdesc,desc,cmin)
_add_cartesian_tags!(labels,topo)
end
function _fill_subgrid_cartesian_entities!(labels, topo, subdesc, desc, cmin)
D = num_cell_dims(topo)
d_to_dface_to_entity = labels.d_to_dface_to_entity
gcis = CartesianIndices(desc.partition)
subcis = CartesianIndices(subdesc.partition)
polytope = first(get_polytopes(topo))
face_labeling = labels
offsets = get_offsets(polytope)
interior_id = num_faces(polytope)
boundary_id = -1
minus_one_ci = CartesianIndex(tfill(-1, Val{D}()))
polytope_d_face_to_jfaces = Matrix{Vector{Vector{Int}}}(undef, (D, D))
for d = 0:(D-1)
for j = d+1:D-1
polytope_d_face_to_jfaces[d+1, j+1] = get_faces(polytope, d, j)
end
end
face_deltas = _find_ncube_face_neighbor_deltas(polytope)
for d = 0:(D-1)
face_to_cells = get_faces(topo, d, D)
cell_to_faces = get_faces(topo, D, d)
face_to_geolabel = face_labeling.d_to_dface_to_entity[d+1]
_generate_subgrid_pregeo_label!(
face_to_geolabel,
d,
D,
offsets,
polytope_d_face_to_jfaces,
minus_one_ci,
face_to_cells,
cell_to_faces,
subcis,
gcis,
face_deltas,
cmin,
interior_id,
boundary_id,
)
end
_fix_geolabels(D, topo, d_to_dface_to_entity, interior_id, boundary_id)
fill!(d_to_dface_to_entity[end], interior_id)
end
function _generate_subgrid_pregeo_label!(
face_to_geolabel,
d,
D,
offsets,
polytope_d_face_to_jfaces,
minus_one_ci,
face_to_cells,
cell_to_faces,
subcis,
gcis,
face_deltas,
cmin,
interior_id,
boundary_id,
)
for face_gid = 1:length(face_to_geolabel)
cell_gid = face_to_cells.data[face_to_cells.ptrs[face_gid]]
a = cell_to_faces.ptrs[cell_gid]
b = cell_to_faces.ptrs[cell_gid+1] - 1
gci = (subcis[cell_gid] + minus_one_ci) + cmin
face_lid = -1
for j = a:b
if (cell_to_faces.data[j] == face_gid)
face_lid = j - a + 1
break
end
end
@assert face_lid != -1
face_lid += offsets[d+1]
# Check whether cell neighbour across face face_lid belongs to the
# global grid. If yes, the current face is actually at the interior
if ((gci + face_deltas[face_lid]) in gcis)
face_to_geolabel[face_gid] = interior_id
else
cell_found = false
for j = d+1:D-1
dface_to_jfaces =
polytope_d_face_to_jfaces[d+1, j+1][face_lid-offsets[d+1]]
cell_found = _is_there_interior_cell_across_higher_dim_faces(
dface_to_jfaces,
offsets[j+1],
gcis,
gci,
face_deltas,
)
cell_found && break
end
if (cell_found)
# The current face is at least in two subgrid cells
face_to_geolabel[face_gid] = boundary_id
else
# The current face is only in one subgrid cell
face_to_geolabel[face_gid] = face_lid
end
end
end
end
function _is_there_interior_cell_across_higher_dim_faces(
dface_to_jfaces,
offset_j,
gcis,
gci,
face_deltas,
)
for k in dface_to_jfaces
jface_lid = k + offset_j
if (isassigned(face_deltas, jface_lid))
if ((gci + face_deltas[jface_lid]) in gcis)
return true
end
end
end
return false
end
"""
_find_ncube_face_neighbor_deltas(p::ExtrusionPolytope{D}) -> Vector{CartesianIndex}
Given an n-cube type ExtrusionPolytope{D}, returns V=Vector{CartesianIndex} with as many
entries as the number of faces in the boundary of the Polytope. For an entry face_lid
in this vector, V[face_lid] returns what has to be added to the CartesianIndex of a
cell in order to obtain the CartesianIndex of the cell neighbour of K across the face F
with local ID face_lid.
"""
function _find_ncube_face_neighbor_deltas(p::ExtrusionPolytope{D}) where {D}
nfaces = num_faces(p)
delta_faces = Vector{CartesianIndex{D}}(undef, nfaces - 1)
for face_lid = 1:nfaces-1
delta_faces[face_lid] = _find_ncube_face_neighbor_delta(p, face_lid)
end
delta_faces
end
function _find_ncube_face_neighbor_delta(p::ExtrusionPolytope{D}, face_lid) where {D}
@assert is_n_cube(p)
result = fill(0, D)
face = p.dface.nfaces[face_lid]
for d = 1:D
if (face.extrusion[d] == 0)
result[d] = (face.anchor[d] == 0) ? -1 : 1
end
end
return CartesianIndex(Tuple(result))
end
# Cartesian grid topology with periodic BC
function _cartesian_grid_topology_with_periodic_bcs(grid::UnstructuredGrid,
isperiodic::NTuple,
partition)
cell_to_vertices, vertex_to_node, =
_generate_cell_to_vertices_from_grid(grid, isperiodic, partition)
_generate_grid_topology_from_grid(grid,cell_to_vertices,vertex_to_node)
end
function _generate_cell_to_vertices_from_grid(grid::UnstructuredGrid,
isperiodic::NTuple, partition)
if is_first_order(grid)
nodes = get_cell_node_ids(grid)
nnodes = num_nodes(grid)
num_nodes_x_dir = [partition[i]+1 for i in 1:length(partition)]
point_to_isperiodic, slave_point_to_point, slave_point_to_master_point =
_generate_slave_to_master_point(num_nodes_x_dir,isperiodic, nnodes)
vertex_to_point = findall( .! point_to_isperiodic)
point_to_vertex = fill(-1,length(point_to_isperiodic))
point_to_vertex[vertex_to_point] = 1:length(vertex_to_point)
point_to_vertex[slave_point_to_point] = point_to_vertex[slave_point_to_master_point]
cell_to_vertices = Table(lazy_map(Broadcasting(Reindex(point_to_vertex)),nodes))
vertex_to_node = vertex_to_point
node_to_vertex = point_to_vertex
else
@notimplemented
end
(cell_to_vertices,vertex_to_node, node_to_vertex)
end
function _generate_slave_to_master_point(num_nodes_x_dir::Vector{Int},
isperiodic::NTuple, num_nodes::Int)
periodic_dirs = findall(x->x==true, isperiodic)
linear_indices = LinearIndices(Tuple(num_nodes_x_dir))
cartesian_indices = CartesianIndices(Tuple(num_nodes_x_dir))
point_to_isperiodic = fill(false,num_nodes)
for point in 1:length(point_to_isperiodic)
ci = Tuple(cartesian_indices[point])
for dir in periodic_dirs
if ci[dir] == num_nodes_x_dir[dir]
point_to_isperiodic[point] = true
end
end
end
slave_point_to_point = findall(point_to_isperiodic)
slave_point_to_master_point = Array{Int32,1}(undef,length(slave_point_to_point))
ijk = zeros(Int,length(isperiodic))
for (i,point) in enumerate(slave_point_to_point)
ijk .= Tuple(cartesian_indices[point])
for i in periodic_dirs
if ijk[i] == num_nodes_x_dir[i]
ijk[i] = 1
end
end
master_point_ijk = CartesianIndex(Tuple(ijk))
slave_point_to_master_point[i] = linear_indices[master_point_ijk]
end
point_to_isperiodic, slave_point_to_point, slave_point_to_master_point
end