-
Notifications
You must be signed in to change notification settings - Fork 98
/
Grids.jl
420 lines (362 loc) · 11 KB
/
Grids.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
"""
abstract type Grid{Dc,Dp}
Abstract type that represents mesh of a domain of parametric dimension `Dc` and
physical dimension `Dp`.
The interface of `Grid` is defined by overloading:
- [`get_node_coordinates(trian::Grid)`](@ref)
- [`get_cell_node_ids(trian::Grid)`](@ref)
- [`get_reffes(trian::Grid)`](@ref)
- [`get_cell_type(trian::Grid)`](@ref)
The `Grid` interface has the following traits
- [`OrientationStyle(::Type{<:Grid})`](@ref)
- [`RegularityStyle(::Type{<:Grid})`](@ref)
The interface of `Grid` is tested with
- [`test_grid`](@ref)
"""
abstract type Grid{Dc,Dp} <: GridapType end
# Traits
abstract type OrientationStyle end
struct Oriented <: OrientationStyle end
struct NonOriented <: OrientationStyle end
"""
OrientationStyle(::Type{<:Grid})
OrientationStyle(::Grid)
`Oriented()` if has oriented faces, `NonOriented()` otherwise (default).
"""
OrientationStyle(a::Grid) = OrientationStyle(typeof(a))
OrientationStyle(::Type{<:Grid}) = NonOriented()
abstract type RegularityStyle end
struct Regular <: RegularityStyle end
struct Irregular <: RegularityStyle end
"""
RegularityStyle(::Type{<:Grid})
RegularityStyle(::Grid)
`Regular()` if no hanging-nodes default), `Irregular()` otherwise.
"""
RegularityStyle(a::Grid) = RegularityStyle(typeof(a))
RegularityStyle(::Type{<:Grid}) = Regular()
# Interface
"""
get_node_coordinates(trian::Grid) -> AbstractArray{<:Point{Dp}}
"""
function get_node_coordinates(trian::Grid)
@abstractmethod
end
"""
get_cell_node_ids(trian::Grid)
"""
function get_cell_node_ids(trian::Grid)
@abstractmethod
end
"""
get_reffes(trian::Grid) -> Vector{LagrangianRefFE}
"""
function get_reffes(trian::Grid)
@abstractmethod
end
"""
get_cell_type(trian::Grid) -> AbstractVector{<:Integer}
"""
function get_cell_type(trian::Grid)
@abstractmethod
end
"""
get_facet_normal(trian::Grid)
"""
function get_facet_normal(trian::Grid)
Dp = num_point_dims(trian)
Dc = num_cell_dims(trian)
if Dp == Dc + 1
@abstractmethod
else
@unreachable "get_facet_normal does not make sense for this grid"
end
end
"""
test_grid(trian::Grid)
"""
function test_grid(trian::Grid{Dc,Dp}) where {Dc,Dp}
@test num_cell_dims(trian) == Dc
@test num_point_dims(trian) == Dp
@test num_cell_dims(typeof(trian)) == Dc
@test num_point_dims(typeof(trian)) == Dp
cell_coords = get_cell_coordinates(trian)
@test isa(cell_coords,AbstractArray{<:AbstractVector{<:Point}})
reffes = get_reffes(trian)
@test isa(reffes,AbstractVector{<:LagrangianRefFE{Dc}})
cell_types = get_cell_type(trian)
@test isa(cell_types,AbstractArray{<:Integer})
ncells = num_cells(trian)
@test ncells == length(cell_coords)
@test ncells == length(cell_types)
nodes_coords = get_node_coordinates(trian)
@test isa(nodes_coords,AbstractArray{<:Point})
cell_node_ids = get_cell_node_ids(trian)
@test isa(cell_node_ids,AbstractArray{<:AbstractArray{<:Integer}})
@test num_nodes(trian) == length(nodes_coords)
@test isa(is_oriented(trian),Bool)
@test isa(is_regular(trian),Bool)
@test OrientationStyle(trian) in (Oriented(), NonOriented())
@test RegularityStyle(trian) in (Regular(), Irregular())
@test is_oriented(trian) == (OrientationStyle(trian) == Oriented())
@test is_regular(trian) == (RegularityStyle(trian) == Regular())
end
# Some API
"""
num_cells(trian::Grid) -> Int
"""
num_cells(trian::Grid) = length(get_cell_type(trian))
"""
num_nodes(trian::Grid) -> Int
"""
num_nodes(trian::Grid) = length(get_node_coordinates(trian))
"""
num_cell_dims(::Grid) -> Int
num_cell_dims(::Type{<:Grid}) -> Int
"""
num_cell_dims(::Grid{Dc,Dp}) where {Dc,Dp} = Dc
num_cell_dims(::Type{<:Grid{Dc,Dp}}) where {Dc,Dp} = Dc
"""
num_point_dims(::Grid) -> Int
num_point_dims(::Type{<:Grid}) -> Int
"""
num_point_dims(::Grid{Dc,Dp}) where {Dc,Dp} = Dp
num_point_dims(::Type{<:Grid{Dc,Dp}}) where {Dc,Dp} = Dp
"""
num_dims(::Grid) -> Int
num_dims(::Type{<:Grid}) -> Int
Equivalent to `num_cell_dims`.
"""
num_dims(g::Grid{Dc}) where Dc = Dc
num_dims(::Type{<:Grid{Dc}}) where Dc = Dc
"""
is_first_order(trian::Grid) -> Bool
"""
function is_first_order(trian::Grid)
reffes = get_reffes(trian)
all(map(is_first_order,reffes))
end
"""
get_cell_reffe(trian::Grid) -> Vector{<:LagrangianRefFE}
It is not desirable to iterate over the resulting array
for large number of cells if the underlying reference FEs
are of different Julia type.
"""
function get_cell_reffe(trian::Grid)
type_to_reffe = get_reffes(trian)
cell_to_type = get_cell_type(trian)
expand_cell_data(type_to_reffe,cell_to_type)
end
"""
"""
function get_cell_ref_coordinates(trian::Grid)
type_to_reffe = get_reffes(trian)
type_to_coords = map(get_node_coordinates,type_to_reffe)
cell_to_type = get_cell_type(trian)
expand_cell_data(type_to_coords,cell_to_type)
end
"""
get_cell_shapefuns(trian::Grid) -> Vector{<:Field}
"""
function get_cell_shapefuns(trian::Grid)
type_to_reffes = get_reffes(trian)
cell_to_type = get_cell_type(trian)
type_to_shapefuns = map(get_shapefuns, type_to_reffes)
expand_cell_data(type_to_shapefuns,cell_to_type)
end
"""
get_cell_map(trian::Grid) -> Vector{<:Field}
"""
function get_cell_map(trian::Grid)
cell_to_coords = get_cell_coordinates(trian)
cell_to_shapefuns = get_cell_shapefuns(trian)
lazy_map(linear_combination,cell_to_coords,cell_to_shapefuns)
end
function get_cell_coordinates(trian::Grid)
node_to_coords = get_node_coordinates(trian)
cell_to_nodes = get_cell_node_ids(trian)
lazy_map(Broadcasting(Reindex(node_to_coords)),cell_to_nodes)
end
function Quadrature(trian::Grid,args...;kwargs...)
cell_ctype = get_cell_type(trian)
ctype_polytope = map(get_polytope,get_reffes(trian))
ctype_quad = map(p->Quadrature(p,args...;kwargs...),ctype_polytope)
cell_quad = expand_cell_data(ctype_quad,cell_ctype)
end
"""
is_oriented(::Type{<:Grid}) -> Bool
is_oriented(a::Grid) -> Bool
"""
is_oriented(a::Grid) = is_oriented(typeof(a))
is_oriented(a::Type{T}) where T<:Grid = OrientationStyle(T) == Oriented()
"""
is_regular(::Type{<:Grid}) -> Bool
is_regular(a::Grid) -> Bool
"""
is_regular(a::Grid) = is_regular(typeof(a))
is_regular(a::Type{T}) where T<:Grid = RegularityStyle(T) == Regular()
"""
Grid(reffe::LagrangianRefFE)
"""
function Grid(reffe::LagrangianRefFE)
UnstructuredGrid(reffe)
end
"""
compute_linear_grid(reffe::LagrangianRefFE)
"""
function compute_linear_grid(reffe::LagrangianRefFE)
p = get_polytope(reffe)
if get_order(reffe) == 0
D = num_cell_dims(reffe)
partition = tfill(1,Val{D}())
else
partition = get_orders(reffe)
end
compute_reference_grid(p,partition)
end
"""
compute_reference_grid(p::LagrangianRefFE, nelems::Integer)
"""
function compute_reference_grid(reffe::LagrangianRefFE, nelems::Integer)
p = get_polytope(reffe)
compute_reference_grid(p,nelems)
end
"""
compute_reference_grid(p::LagrangianRefFE, partition::NTuple{D,Integer})
"""
function compute_reference_grid(reffe::LagrangianRefFE{D}, partition::NTuple{D,Integer}) where D
p = get_polytope(reffe)
compute_reference_grid(p,partition)
end
"""
compute_reference_grid(p::Polytope,nelems)
"""
function compute_reference_grid(p::Polytope,nelems)
if is_n_cube(p)
_compute_linear_grid_from_n_cube(p,nelems)
else
_compute_linear_grid_from_simplex(p,nelems)
end
end
function _compute_linear_grid_from_n_cube(
p::Polytope{D},
partition::NTuple{D,Integer}) where D
@assert is_n_cube(p)
pmin, pmax = get_bounding_box(p)
desc = CartesianDescriptor(pmin,pmax,partition)
CartesianGrid(desc)
end
function _compute_linear_grid_from_n_cube(
p::Polytope{D},
nelems::Integer) where D
partition = tfill(nelems,Val{D}())
_compute_linear_grid_from_n_cube(p,partition)
end
function _compute_linear_grid_from_simplex(
p::Polytope{D},
partition::NTuple{D,Integer}) where D
@assert minimum(partition) == maximum(partition) >= 1
nelems = partition[1]
_compute_linear_grid_from_simplex(p,nelems)
end
function _compute_linear_grid_from_simplex(p::Polytope{1},nelems::Integer)
@assert is_simplex(p)
_compute_linear_grid_coords_from_n_cube(p,nelems)
end
function _compute_linear_grid_from_simplex(p::Polytope,nelems::Integer)
@assert is_simplex(p)
X,T = _compute_linear_grid_coords_from_simplex(p,nelems)
reffes = [ LagrangianRefFE(Float64,p,1) ]
cell_types = ones(Int8,length(T))
T = Table(T)
UnstructuredGrid(X,T,reffes,cell_types)
end
function _compute_linear_grid_coords_from_simplex(p::Polytope{2},n::Integer)
tri_num(n) = n*(n+1)÷2
v(n,i,j) = tri_num(n) - tri_num(n-i+1) + j
@assert is_simplex(p)
D = 2
quad_to_tris = ((1,2,3),(2,4,3))
quad = CartesianIndices( (0:1,0:1) )
Tp = eltype(get_vertex_coordinates(p))
n_verts = tri_num(n+1)
n_cells = tri_num(n)+tri_num(n-1)
n_verts_x_cell = num_vertices(p)
X = zeros(Tp,n_verts)
T = [ zeros(Int,n_verts_x_cell) for i in 1:n_cells ]
for i in 1:n+1
for j in 1:n+1-i+1
vert = v(n+1,i,j)
X[vert] = Point((i-1)/n,(j-1)/n)
end
end
for i in 1:n
for j in 1:n-(i-1)
verts = ntuple( lv-> v(n+1, (i,j).+quad[lv].I ...), Val{2^D}() )
cell = v(n,i,j)
T[cell] .= map(Reindex(verts),quad_to_tris[1])
if (i-1)+(j-1) < n-1
cell = tri_num(n) + v(n-1,i,j)
T[cell] .= map(Reindex(verts),quad_to_tris[2])
end
end
end
X,T
end
function _compute_linear_grid_coords_from_simplex(p::Polytope{3},n::Integer)
tri_num(n) = n*(n+1)÷2
tet_num(n) = n*(n+1)*(n+2)÷6
v(n,i,j) = tri_num(n) - tri_num(n-i+1) + j
v(n,i,j,k) = tet_num(n) - tet_num(n-i+1) + v(n-i+1,j,k)
@assert is_simplex(p)
D = 3
cube_to_tets = ((1,2,3,5),(2,4,3,6),(3,5,7,6),(2,3,5,6),(3,4,7,6),(4,6,7,8))
cube = CartesianIndices( (0:1,0:1,0:1) )
n_core_tets = length(cube_to_tets)-2
Tp = eltype(get_vertex_coordinates(p))
n_verts = tet_num(n+1)
n_cells = tet_num(n)+n_core_tets*tet_num(n-1)+tet_num(n-2)
n_verts_x_cell = num_vertices(p)
X = zeros(Tp,n_verts)
T = [ zeros(Int,n_verts_x_cell) for i in 1:n_cells ]
for i in 1:n+1
for j in 1:n+1-(i-1)
for k in 1:n+1-(i-1)-(j-1)
vert = v(n+1,i,j,k)
X[vert] = Point((i-1)/n,(j-1)/n,(k-1)/n)
end
end
end
for i in 1:n
for j in 1:n-(i-1)
for k in 1:n-(i-1)-(j-1)
verts = ntuple( lv-> v(n+1, (i,j,k).+cube[lv].I ...), Val{2^D}() )
cell = v(n,i,j,k)
T[cell] .= map(Reindex(verts),cube_to_tets[1])
if (i-1)+(j-1)+(k-1) < n-1
cell = tet_num(n) + (v(n-1,i,j,k)-1)*n_core_tets
for t in 1:n_core_tets
T[cell+t] .= map(Reindex(verts),cube_to_tets[t+1])
end
end
if (i-1)+(j-1)+(k-1) < n-2
cell = tet_num(n) + n_core_tets*tet_num(n-1) + v(n-2,i,j,k)
T[cell] .= map(Reindex(verts),cube_to_tets[end])
end
end
end
end
X,T
end
"""
Grid(::Type{ReferenceFE{d}},p::Polytope) where d
"""
function Grid(::Type{ReferenceFE{d}},p::Polytope) where d
UnstructuredGrid(ReferenceFE{d},p)
end
"""
simplexify(grid::Grid;kwargs...)
"""
function simplexify(grid::Grid;kwargs...)
simplexify(UnstructuredGrid(grid);kwargs...)
end