-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathCLagrangianFESpaces.jl
364 lines (331 loc) · 9.57 KB
/
CLagrangianFESpaces.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
"""
struct NodeToDofGlue{T}
free_dof_to_node::Vector{Int32}
free_dof_to_comp::Vector{Int16}
dirichlet_dof_to_node::Vector{Int32}
dirichlet_dof_to_comp::Vector{Int16}
node_and_comp_to_dof::Vector{T}
end
"""
struct NodeToDofGlue{T}
free_dof_to_node::Vector{Int32}
free_dof_to_comp::Vector{Int16}
dirichlet_dof_to_node::Vector{Int32}
dirichlet_dof_to_comp::Vector{Int16}
node_and_comp_to_dof::Vector{T}
end
"""
CLagrangianFESpace(::Type{T},grid::Triangulation) where T
"""
function CLagrangianFESpace(::Type{T},grid::Triangulation,trian::Triangulation=grid) where T
vector_type = Vector{_dof_type(T)}
node_to_tag = fill(Int8(UNSET),num_nodes(grid))
tag_to_mask = fill(_default_mask(T),0)
CLagrangianFESpace(T,grid,vector_type,node_to_tag,tag_to_mask,trian)
end
"""
CLagrangianFESpace(
::Type{T},
grid::Triangulation,
vector_type::Type,
node_to_tag::AbstractVector,
tag_to_mask::AbstractVector) where T
"""
function CLagrangianFESpace(
::Type{T},
grid::Triangulation,
vector_type::Type,
node_to_tag::AbstractVector{<:Integer},
tag_to_masks::AbstractVector,
trian::Triangulation=grid
) where T
z = zero(T)
glue, dirichlet_dof_tag = _generate_node_to_dof_glue_component_major(
z,node_to_tag,tag_to_masks)
cell_reffe = _generate_cell_reffe_clagrangian(z,grid)
cell_dofs_ids = _generate_cell_dofs_clagrangian(z,grid,glue,cell_reffe)
cell_shapefuns = lazy_map(get_shapefuns,cell_reffe)
cell_dof_basis = lazy_map(get_dof_basis,cell_reffe)
rd = ReferenceDomain()
fe_basis, fe_dof_basis = compute_cell_space(
cell_shapefuns,cell_dof_basis,rd,rd,trian)
cell_is_dirichlet = _generate_cell_is_dirichlet(cell_dofs_ids)
nfree = length(glue.free_dof_to_node)
ndirichlet = length(glue.dirichlet_dof_to_node)
ntags = length(tag_to_masks)
dirichlet_cells = collect(Int32,findall(cell_is_dirichlet))
space = UnconstrainedFESpace(
vector_type,
nfree,
ndirichlet,
cell_dofs_ids,
fe_basis,
fe_dof_basis,
cell_is_dirichlet,
dirichlet_dof_tag,
dirichlet_cells,
ntags,
glue
)
space
end
function _use_clagrangian(trian,cell_reffe,conf)
false
end
function _use_clagrangian(trian::Triangulation,cell_reffe,conf::H1Conformity)
ctype_reffe1, cell_ctype1 = compress_cell_data(cell_reffe)
ctype_reffe2 = get_reffes(trian)
if length(ctype_reffe1) != 1 || length(ctype_reffe2) != 1
return false
end
reffe1 = first(ctype_reffe1)
reffe2 = first(ctype_reffe2)
if get_orders(reffe1) != get_orders(reffe2)
return false
end
if get_order(reffe1) != 1 # This can be relaxed in the future
return false
end
return true
end
function _unsafe_clagrangian(
cell_reffe,
grid,
labels,
vector_type,
dirichlet_tags,
dirichlet_masks,
trian=grid)
ctype_reffe, cell_ctype = compress_cell_data(cell_reffe)
prebasis = get_prebasis(first(ctype_reffe))
T = return_type(prebasis)
# Next line assumes linear grids
node_to_tag = get_face_tag_index(labels,dirichlet_tags,0)
_vector_type = vector_type === nothing ? Vector{Float64} : vector_type
tag_to_mask = dirichlet_masks === nothing ? fill(_default_mask(T),length(dirichlet_tags)) : dirichlet_masks
CLagrangianFESpace(T,grid,_vector_type,node_to_tag,tag_to_mask,trian)
end
# Helpers
_default_mask(::Type) = true
_default_mask(::Type{T}) where T <: MultiValue = ntuple(i->true,Val{num_indep_components(T)}())
_dof_type(::Type{T}) where T = T
_dof_type(::Type{T}) where T<:MultiValue = eltype(T)
function _generate_cell_is_dirichlet(cell_dofs)
cell_is_dirichlet = Vector{Bool}(undef,length(cell_dofs))
cache = array_cache(cell_dofs)
isnegative(dof) = dof<0
for cell in 1:length(cell_dofs)
dofs = getindex!(cache,cell_dofs,cell)
cell_is_dirichlet[cell] = any(isnegative,dofs)
end
cell_is_dirichlet
end
function _generate_node_to_dof_glue_component_major(z,node_to_tag,tag_to_mask)
@notimplementedif length(z) != 1
nfree_dofs = 0
ndiri_dofs = 0
for tag in node_to_tag
if tag == UNSET
nfree_dofs += 1
else
mask = tag_to_mask[tag]
if mask
ndiri_dofs += 1
else
nfree_dofs += 1
end
end
end
free_dof_to_node = Vector{Int32}(undef,nfree_dofs)
free_dof_to_comp = ones(Int16,nfree_dofs)
diri_dof_to_node = Vector{Int32}(undef,ndiri_dofs)
diri_dof_to_comp = ones(Int16,ndiri_dofs)
diri_dof_to_tag = ones(Int8,ndiri_dofs)
nnodes = length(node_to_tag)
node_and_comp_to_dof = Vector{Int32}(undef,nnodes)
nfree_dofs = 0
ndiri_dofs = 0
for (node,tag) in enumerate(node_to_tag)
if tag == UNSET
nfree_dofs += 1
free_dof_to_node[nfree_dofs] = node
node_and_comp_to_dof[node] = nfree_dofs
else
mask = tag_to_mask[tag]
if mask
ndiri_dofs += 1
diri_dof_to_node[ndiri_dofs] = node
diri_dof_to_tag[ndiri_dofs] = tag
node_and_comp_to_dof[node] = -ndiri_dofs
else
nfree_dofs += 1
free_dof_to_node[nfree_dofs] = node
node_and_comp_to_dof[node] = nfree_dofs
end
end
end
glue = NodeToDofGlue(
free_dof_to_node,
free_dof_to_comp,
diri_dof_to_node,
diri_dof_to_comp,
node_and_comp_to_dof)
glue, diri_dof_to_tag
end
function _generate_node_to_dof_glue_component_major(
z::MultiValue,node_to_tag,tag_to_masks)
nfree_dofs = 0
ndiri_dofs = 0
ncomps = num_indep_components(z)
@check length(testitem(tag_to_masks)) == ncomps
for (node,tag) in enumerate(node_to_tag)
if tag == UNSET
nfree_dofs += ncomps
else
for mask in tag_to_masks[tag]
if mask
ndiri_dofs += 1
else
nfree_dofs += 1
end
end
end
end
free_dof_to_node = Vector{Int32}(undef,nfree_dofs)
free_dof_to_comp = ones(Int16,nfree_dofs)
diri_dof_to_node = Vector{Int32}(undef,ndiri_dofs)
diri_dof_to_comp = ones(Int16,ndiri_dofs)
diri_dof_to_tag = ones(Int8,ndiri_dofs)
T = change_eltype(z,Int32)
nnodes = length(node_to_tag)
node_and_comp_to_dof = Vector{T}(undef,nnodes)
nfree_dofs = 0
ndiri_dofs = 0
m = zero(MVector{ncomps, Int32})
for (node,tag) in enumerate(node_to_tag)
if tag == UNSET
for comp in 1:ncomps
nfree_dofs += 1
free_dof_to_node[nfree_dofs] = node
free_dof_to_comp[nfree_dofs] = comp
m[comp] = nfree_dofs
end
else
masks = tag_to_masks[tag]
for comp in 1:ncomps
mask = masks[comp]
if mask
ndiri_dofs += 1
diri_dof_to_node[ndiri_dofs] = node
diri_dof_to_comp[ndiri_dofs] = comp
diri_dof_to_tag[ndiri_dofs] = tag
m[comp] = -ndiri_dofs
else
nfree_dofs += 1
free_dof_to_node[nfree_dofs] = node
free_dof_to_comp[nfree_dofs] = comp
m[comp] = nfree_dofs
end
end
end
node_and_comp_to_dof[node] = Tuple(m)
end
glue = NodeToDofGlue(
free_dof_to_node,
free_dof_to_comp,
diri_dof_to_node,
diri_dof_to_comp,
node_and_comp_to_dof)
glue, diri_dof_to_tag
end
function _generate_cell_reffe_clagrangian(z,grid)
ctype_to_reffe = get_reffes(grid)
cell_to_ctype = get_cell_type(grid)
function fun(reffe)
p = get_polytope(reffe)
orders = get_orders(reffe)
LagrangianRefFE(typeof(z),p,orders)
end
# using map instead of lazy_map seems to kill the compiler.
ctype_to_reffe = collect(lazy_map(fun,ctype_to_reffe))
cell_to_reffe = expand_cell_data(ctype_to_reffe,cell_to_ctype)
end
function _generate_cell_dofs_clagrangian(z,grid,glue,cell_to_reffe)
ctype_to_reffe, cell_to_ctype = compress_cell_data(cell_to_reffe)
cell_to_nodes = get_cell_node_ids(grid)
cell_to_dofs = _generate_cell_dofs_clagrangian(
z,
cell_to_nodes,
ctype_to_reffe,
cell_to_ctype,
glue.node_and_comp_to_dof)
cell_to_dofs
end
function _generate_cell_dofs_clagrangian(
z,
cell_to_nodes,
ctype_to_reffe,
cell_to_ctype,
node_and_comp_to_dof)
cell_to_dofs = lazy_map(
Broadcasting(Reindex(node_and_comp_to_dof)),cell_to_nodes)
Table(cell_to_dofs)
end
function _generate_cell_dofs_clagrangian(
z::MultiValue,
cell_to_nodes,
ctype_to_reffe,
cell_to_ctype,
node_and_comp_to_dof)
ncomps = num_indep_components(z)
ctype_to_lnode_to_comp_to_ldof = map(get_node_and_comp_to_dof,ctype_to_reffe)
ctype_to_num_ldofs = map(num_dofs,ctype_to_reffe)
cell_to_dofs = _allocate_cell_dofs_clagrangian(
ctype_to_num_ldofs,cell_to_ctype)
cache = array_cache(cell_to_nodes)
_fill_cell_dofs_clagrangian!(
cell_to_dofs,
cache,
cell_to_nodes,
ctype_to_lnode_to_comp_to_ldof,
cell_to_ctype,
node_and_comp_to_dof,
ncomps)
cell_to_dofs
end
function _allocate_cell_dofs_clagrangian(ctype_to_num_ldofs,cell_to_ctype)
ncells = length(cell_to_ctype)
ptrs = zeros(Int32,ncells+1)
for cell in 1:ncells
ctype = cell_to_ctype[cell]
nldofs = ctype_to_num_ldofs[ctype]
ptrs[cell+1] = nldofs
end
length_to_ptrs!(ptrs)
ndata = ptrs[end]-1
data = zeros(Int32,ndata)
Table(data,ptrs)
end
function _fill_cell_dofs_clagrangian!(
cell_to_dofs,
cache,
cell_to_nodes,
ctype_to_lnode_and_comp_to_ldof,
cell_to_ctype,
node_and_comp_to_dof,
ncomps)
ncells = length(cell_to_ctype)
for cell in 1:ncells
nodes = getindex!(cache,cell_to_nodes,cell)
ctype = cell_to_ctype[cell]
lnode_and_comp_to_ldof = ctype_to_lnode_and_comp_to_ldof[ctype]
p = cell_to_dofs.ptrs[cell]-1
for (lnode, node) in enumerate(nodes)
for comp in 1:ncomps
ldof = indep_comp_getindex(lnode_and_comp_to_ldof[lnode], comp)
dof = indep_comp_getindex(node_and_comp_to_dof[node], comp)
cell_to_dofs.data[p+ldof] = dof
end
end
end
end