-
Notifications
You must be signed in to change notification settings - Fork 98
/
UnstructuredGrids.jl
305 lines (260 loc) · 8.33 KB
/
UnstructuredGrids.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
"""
struct UnstructuredGrid{Dc,Dp,Tp,Ti,O} <: Grid{Dc,Dp}
node_coordinates::Vector{Point{Dp,Tp}}
cell_node_ids::Table{Ti,Int32}
reffes::Vector{<:LagrangianRefFE{Dc}}
cell_types::Vector{Int8}
end
"""
struct UnstructuredGrid{Dc,Dp,Tp,O,Tn} <: Grid{Dc,Dp}
node_coordinates::Vector{Point{Dp,Tp}}
cell_node_ids::Table{Int32,Vector{Int32},Vector{Int32}}
reffes::Vector{LagrangianRefFE{Dc}}
cell_types::Vector{Int8}
orientation_style::O
facet_normal::Tn
cell_map
@doc """
function UnstructuredGrid(
node_coordinates::Vector{Point{Dp,Tp}},
cell_node_ids::Table{Ti},
reffes::Vector{<:LagrangianRefFE{Dc}},
cell_types::Vector,
orientation_style::OrientationStyle=NonOriented()) where {Dc,Dp,Tp,Ti}
end
Low-level inner constructor.
"""
function UnstructuredGrid(
node_coordinates::Vector{Point{Dp,Tp}},
cell_node_ids::Table{Ti},
reffes::Vector{<:LagrangianRefFE{Dc}},
cell_types::Vector,
orientation_style::OrientationStyle=NonOriented(),
facet_normal=nothing;
has_affine_map=nothing) where {Dc,Dp,Tp,Ti}
if has_affine_map === nothing
_has_affine_map = get_has_affine_map(reffes)
else
_has_affine_map = has_affine_map
end
cell_map = _compute_cell_map(node_coordinates,cell_node_ids,reffes,cell_types,_has_affine_map)
B = typeof(orientation_style)
Tn = typeof(facet_normal)
new{Dc,Dp,Tp,B,Tn}(
node_coordinates,
cell_node_ids,
reffes,
cell_types,
orientation_style,
facet_normal,
cell_map)
end
end
function get_has_affine_map(ctype_reffe)
ctype_poly = map(get_polytope,ctype_reffe)
has_affine_map =
all(map(is_first_order,ctype_reffe)) &&
( all(map(is_simplex,ctype_poly)) || all(map(p->num_dims(p)==1,ctype_poly)) )
end
function _compute_cell_map(node_coords,cell_node_ids,ctype_reffe,cell_ctype, has_affine_map)
cell_coords = lazy_map(Broadcasting(Reindex(node_coords)),cell_node_ids)
ctype_shapefuns = map(get_shapefuns,ctype_reffe)
cell_shapefuns = expand_cell_data(ctype_shapefuns,cell_ctype)
default_cell_map = lazy_map(linear_combination,cell_coords,cell_shapefuns)
ctype_poly = map(get_polytope,ctype_reffe)
if has_affine_map
ctype_q0 = map(p->zero(first(get_vertex_coordinates(p))),ctype_poly)
cell_q0 = expand_cell_data(ctype_q0,cell_ctype)
default_cell_grad = lazy_map(∇,default_cell_map)
origins = lazy_map(evaluate,default_cell_map,cell_q0)
gradients = lazy_map(evaluate,default_cell_grad,cell_q0)
cell_map = lazy_map(Fields.affine_map,gradients,origins)
else
cell_map = default_cell_map
end
Fields.MemoArray(cell_map)
end
"""
UnstructuredGrid(grid::Grid)
"""
function UnstructuredGrid(grid::Grid)
@assert is_regular(grid) "UnstructuredGrid constructor only for regular grids"
node_coordinates = collect1d(get_node_coordinates(grid))
cell_node_ids = Table(get_cell_node_ids(grid))
reffes = get_reffes(grid)
cell_types = collect1d(get_cell_type(grid))
orien = OrientationStyle(grid)
UnstructuredGrid(node_coordinates,cell_node_ids,reffes,cell_types,orien)
end
function UnstructuredGrid(grid::UnstructuredGrid)
grid
end
OrientationStyle(
::Type{<:UnstructuredGrid{Dc,Dp,Tp,B}}) where {Dc,Dp,Tp,B} = B()
get_reffes(g::UnstructuredGrid) = g.reffes
get_cell_type(g::UnstructuredGrid) = g.cell_types
get_node_coordinates(g::UnstructuredGrid) = g.node_coordinates
get_cell_node_ids(g::UnstructuredGrid) = g.cell_node_ids
get_cell_map(g::UnstructuredGrid) = g.cell_map
function get_facet_normal(g::UnstructuredGrid)
@assert g.facet_normal != nothing "This Grid does not have information about normals."
g.facet_normal
end
# From ReferenceFE
"""
UnstructuredGrid(reffe::LagrangianRefFE)
Build a grid with a single cell that is the given reference FE itself
"""
function UnstructuredGrid(reffe::LagrangianRefFE)
node_coordinates = get_node_coordinates(reffe)
cell_node_ids = Table([collect(1:num_nodes(reffe)),])
reffes = [reffe,]
cell_types = [1,]
UnstructuredGrid(node_coordinates,cell_node_ids,reffes,cell_types)
end
# From Polytope
function UnstructuredGrid(::Type{ReferenceFE{D}},p::Polytope{D}) where D
order = 1
reffe = LagrangianRefFE(Float64,p,order)
UnstructuredGrid(reffe)
end
"""
UnstructuredGrid(::Type{ReferenceFE{d}},p::Polytope) where d
"""
function UnstructuredGrid(::Type{ReferenceFE{d}},p::Polytope) where d
node_coordinates = get_vertex_coordinates(p)
cell_node_ids = Table(get_faces(p,d,0))
reffaces = get_reffaces(Polytope{d},p)
cell_type = get_face_type(p,d)
order = 1
reffes = map( (f)-> LagrangianRefFE(Float64,f,order), reffaces)
UnstructuredGrid(
node_coordinates,
cell_node_ids,
reffes,
cell_type)
end
# From coordinates
"""
UnstructuredGrid(x::AbstractArray{<:Point})
"""
function UnstructuredGrid(x::AbstractArray{<:Point})
np = length(x)
node_coords = collect1d(x)
cell_node_ids = identity_table(Int32,Int32,np)
cell_type = fill(1,np)
order = 1
reffes = [LagrangianRefFE(Float64,VERTEX,order),]
UnstructuredGrid(
node_coords,
cell_node_ids,
reffes,
cell_type)
end
# IO
function to_dict(grid::UnstructuredGrid)
dict = Dict{Symbol,Any}()
x = get_node_coordinates(grid)
dict[:node_coordinates] = reinterpret(eltype(eltype(x)),x)
dict[:Dp] = num_point_dims(grid)
dict[:cell_node_ids] = to_dict(get_cell_node_ids(grid))
dict[:reffes] = map(to_dict, get_reffes(grid))
dict[:cell_type] = get_cell_type(grid)
dict[:orientation] = is_oriented(grid)
dict
end
function from_dict(::Type{UnstructuredGrid},dict::Dict{Symbol,Any})
x = collect1d(dict[:node_coordinates])
T = eltype(x)
Dp = dict[:Dp]
node_coordinates::Vector{Point{Dp,T}} = reinterpret(Point{Dp,T},x)
cell_node_ids = from_dict(Table{Int32,Vector{Int32},Vector{Int32}},dict[:cell_node_ids])
reffes = [ from_dict(LagrangianRefFE,reffe) for reffe in dict[:reffes]]
cell_type::Vector{Int8} = dict[:cell_type]
O::Bool = dict[:orientation]
UnstructuredGrid(
node_coordinates,
cell_node_ids,
reffes,
cell_type,
O ? Oriented() : NonOriented())
end
function simplexify(grid::UnstructuredGrid;kwargs...)
reffes = get_reffes(grid)
@notimplementedif length(reffes) != 1
reffe = first(reffes)
order = 1
@notimplementedif get_order(reffe) != order
p = get_polytope(reffe)
ltcell_to_lpoints, simplex = simplexify(p;kwargs...)
cell_to_points = get_cell_node_ids(grid)
tcell_to_points = _refine_grid_connectivity(cell_to_points, ltcell_to_lpoints)
ctype_to_reffe = [LagrangianRefFE(Float64,simplex,order),]
tcell_to_ctype = fill(Int8(1),length(tcell_to_points))
point_to_coords = get_node_coordinates(grid)
UnstructuredGrid(
point_to_coords,
tcell_to_points,
ctype_to_reffe,
tcell_to_ctype,
Oriented())
end
function _refine_grid_connectivity(cell_to_points::Table, ltcell_to_lpoints)
data, ptrs = _refine_grid_connectivity(
cell_to_points.data,
cell_to_points.ptrs,
ltcell_to_lpoints)
Table(data,ptrs)
end
function _refine_grid_connectivity(
cell_to_points_data::AbstractVector{T},
cell_to_points_ptrs::AbstractVector{P},
ltcell_to_lpoints) where {T,P}
nltcells = length(ltcell_to_lpoints)
ncells = length(cell_to_points_ptrs) - 1
ntcells = ncells * nltcells
tcell_to_points_ptrs = zeros(P,ntcells+1)
_refine_grid_connectivity_count!(
tcell_to_points_ptrs,
ncells,
ltcell_to_lpoints)
length_to_ptrs!(tcell_to_points_ptrs)
ndata = tcell_to_points_ptrs[end]-1
tcell_to_points_data = zeros(T,ndata)
_refine_grid_connectivity!(
tcell_to_points_data,
cell_to_points_data,
cell_to_points_ptrs,
ltcell_to_lpoints )
(tcell_to_points_data, tcell_to_points_ptrs)
end
function _refine_grid_connectivity_count!(
tcell_to_points_ptrs,
ncells,
ltcell_to_lpoints)
tcell = 1
for cell in 1:ncells
for lpoints in ltcell_to_lpoints
tcell_to_points_ptrs[tcell+1] = length(lpoints)
tcell +=1
end
end
end
function _refine_grid_connectivity!(
tcell_to_points_data,
cell_to_points_data,
cell_to_points_ptrs,
ltcell_to_lpoints )
ncells = length(cell_to_points_ptrs) - 1
k = 1
for cell in 1:ncells
a = cell_to_points_ptrs[cell]-1
for lpoints in ltcell_to_lpoints
for lpoint in lpoints
point = cell_to_points_data[a+lpoint]
tcell_to_points_data[k] = point
k += 1
end
end
end
end