-
Notifications
You must be signed in to change notification settings - Fork 98
/
ReferenceFEInterfaces.jl
509 lines (416 loc) · 13.6 KB
/
ReferenceFEInterfaces.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
abstract type Conformity end
struct L2Conformity <: Conformity end
"""
abstract type ReferenceFE{D} <: GridapType
Abstract type representing a Reference finite element. `D` is the underlying coordinate space dimension.
We follow the Ciarlet definition. A reference finite element
is defined by a polytope (cell topology), a basis of an interpolation space
of top of this polytope (denoted here as the prebasis), and a basis of the dual of this space
(i.e. the degrees of freedom). From this information one can compute the shape functions
(i.e, the canonical basis of w.r.t. the degrees of freedom) with a simple change of basis.
In addition, we also encode in this type information about how the interpolation space
in a reference finite element is "glued" with neighbors in order to build conforming
cell-wise spaces.
The `ReferenceFE` interface is defined by overloading these methods:
- [`num_dofs(reffe::ReferenceFE)`](@ref)
- [`get_polytope(reffe::ReferenceFE)`](@ref)
- [`get_prebasis(reffe::ReferenceFE)`](@ref)
- [`get_dof_basis(reffe::ReferenceFE)`](@ref)
- [`Conformity(reffe::ReferenceFE)`](@ref)
- [`get_face_own_dofs(reffe::ReferenceFE,conf::Conformity)`](@ref)
- [`get_face_own_dofs_permutations(reffe::ReferenceFE,conf::Conformity)`](@ref)
- [`get_face_dofs(reffe::ReferenceFE)`](@ref)
The interface is tested with
- [`test_reference_fe(reffe::ReferenceFE)`](@ref)
"""
abstract type ReferenceFE{D} <: GridapType end
abstract type ReferenceFEName end
# Extensible factory function
function ReferenceFE(p::Polytope,basis::ReferenceFEName,args...;kwargs...)
@unreachable """\n
Undefined factory function ReferenceFE for basis $basis and the given arguments.
"""
end
ReferenceFE(basis::ReferenceFEName,args...;kwargs...) = (basis, args, kwargs)
"""
num_dofs(reffe::ReferenceFE) -> Int
Returns the number of DOFs.
"""
function num_dofs(reffe::ReferenceFE)
@abstractmethod
end
"""
get_polytope(reffe::ReferenceFE) -> Polytope
Returns the underlying polytope object.
"""
function get_polytope(reffe::ReferenceFE)
@abstractmethod
end
"""
get_prebasis(reffe::ReferenceFE) -> Field
Returns the underlying prebasis encoded as a `Field` object.
"""
function get_prebasis(reffe::ReferenceFE)
@abstractmethod
end
get_order(reffe::ReferenceFE) = get_order(get_prebasis(reffe))
"""
get_dof_basis(reffe::ReferenceFE) -> Dof
Returns the underlying dof basis encoded in a `Dof` object.
"""
function get_dof_basis(reffe::ReferenceFE)
@abstractmethod
end
"""
Conformity(reffe::ReferenceFE) -> Conformity
"""
function Conformity(reffe::ReferenceFE)
@abstractmethod
end
"""
get_face_own_dofs(reffe::ReferenceFE,conf::Conformity) -> Vector{Vector{Int}}
"""
function get_face_own_dofs(reffe::ReferenceFE,conf::Conformity)
@abstractmethod
end
function Conformity(reffe::ReferenceFE,conf::Conformity)
conf
end
function Conformity(reffe::ReferenceFE,conf::Nothing)
Conformity(reffe)
end
function Conformity(reffe::ReferenceFE,sym::Symbol)
@abstractmethod
end
"""
get_face_own_dofs(reffe::ReferenceFE) -> Vector{Vector{Int}}
"""
function get_face_own_dofs(reffe::ReferenceFE)
conf = Conformity(reffe)
get_face_own_dofs(reffe,conf)
end
function get_face_own_dofs(reffe::ReferenceFE,conf::Nothing)
get_face_own_dofs(reffe)
end
function get_face_own_dofs(reffe::ReferenceFE,conf::L2Conformity)
_get_face_own_dofs_l2(reffe)
end
function _get_face_own_dofs_l2(reffe::ReferenceFE)
p = get_polytope(reffe)
r = [Int[] for i in 1:num_faces(p)]
r[end] = collect(1:num_dofs(reffe))
r
end
"""
get_face_own_dofs_permutations(reffe::ReferenceFE,conf::Conformity) -> Vector{Vector{Vector{Int}}}
"""
function get_face_own_dofs_permutations(reffe::ReferenceFE,conf::Conformity)
face_own_dofs = get_face_own_dofs(reffe,conf)
_trivial_face_own_dofs_permutations(face_own_dofs)
end
function _trivial_face_own_dofs_permutations(face_own_dofs)
[ [collect(Int,1:length(dofs)),] for dofs in face_own_dofs ]
end
"""
get_face_own_dofs_permutations(reffe::ReferenceFE) -> Vector{Vector{Vector{Int}}}
"""
function get_face_own_dofs_permutations(reffe::ReferenceFE)
conf = Conformity(reffe)
get_face_own_dofs_permutations(reffe,conf)
end
function get_face_own_dofs_permutations(reffe::ReferenceFE,conf::Nothing)
get_face_own_dofs_permutations(reffe)
end
"""
get_face_dofs(reffe::ReferenceFE) -> Vector{Vector{Int}}
Returns a vector of vector that, for each face, stores the
dofids in the closure of the face.
"""
function get_face_dofs(reffe::ReferenceFE)
@abstractmethod
end
function get_dof_to_comp(reffe::ReferenceFE)
fill(0,num_dofs(reffe))
end
"""
"""
function expand_cell_data(type_to_data, cell_to_type)
CompressedArray(type_to_data,cell_to_type)
end
function expand_cell_data(type_to_data, cell_to_type::Fill)
ncells = length(cell_to_type)
@assert length(type_to_data) == 1 "Only one reference element expected"
@assert cell_to_type.value == 1 "Only one type of reference element expected"
data = first(type_to_data)
Fill(data,ncells)
end
function compress_cell_data(cell_data::AbstractArray)
@unreachable """\n
The given cell data cannot be compressed. Describe your data with
a CompressedArray or Fill array types.
"""
end
function compress_cell_data(a::CompressedArray)
a.values, a.ptrs
end
function compress_cell_data(a::Fill)
fill(a.value,1), Fill(1,length(a))
end
# Test
"""
test_reference_fe(reffe::ReferenceFE{D}) where D
Test if the methods in the `ReferenceFE` interface are defined for the object `reffe`.
"""
function test_reference_fe(reffe::ReferenceFE{D}) where D
conf = Conformity(reffe)
@test isa(conf,Conformity)
test_reference_fe(reffe,conf)
end
function test_reference_fe(reffe::ReferenceFE{D},conf::Conformity) where D
@test D == num_dims(reffe)
p = get_polytope(reffe)
@test isa(p,Polytope{D})
basis = get_prebasis(reffe)
@test isa(basis,AbstractArray{<:Field})
dofs = get_dof_basis(reffe)
@test isa(dofs,AbstractArray{<:Dof})
facedofs = get_face_own_dofs(reffe,conf)
@test isa(facedofs,Vector{Vector{Int}})
@test length(facedofs) == num_faces(p)
facedofs_perms = get_face_own_dofs_permutations(reffe,conf)
@test isa(facedofs_perms,Vector{Vector{Vector{Int}}})
@test length(facedofs_perms) == num_faces(p)
facedofs = get_face_dofs(reffe)
@test isa(facedofs,Vector{Vector{Int}})
@test length(facedofs) == num_faces(p)
shapefuns = get_shapefuns(reffe)
@test isa(shapefuns,AbstractVector{<:Field})
ndofs = num_dofs(reffe)
m = evaluate(dofs,basis)
@test ndofs == size(m,1)
@test ndofs == size(m,2)
end
"""
Constant of type `Int` used to signal that a permutation is not valid.
"""
const INVALID_PERM = 0
# API
"""
num_dims(::Type{<:ReferenceFE{D}}) where D
num_dims(reffe::ReferenceFE{D}) where D
Returns `D`.
"""
num_dims(reffe::ReferenceFE) = num_dims(typeof(reffe))
num_dims(::Type{<:ReferenceFE{D}}) where D = D
"""
num_cell_dims(::Type{<:ReferenceFE{D}}) where D
num_cell_dims(reffe::ReferenceFE{D}) where D
Returns `D`.
"""
num_cell_dims(reffe::ReferenceFE) = num_dims(typeof(reffe))
num_cell_dims(::Type{<:ReferenceFE{D}}) where D = D
"""
num_point_dims(::Type{<:ReferenceFE{D}}) where D
num_point_dims(reffe::ReferenceFE{D}) where D
Returns `D`.
"""
num_point_dims(reffe::ReferenceFE) = num_dims(typeof(reffe))
num_point_dims(::Type{<:ReferenceFE{D}}) where D = D
"""
num_faces(reffe::ReferenceFE)
num_faces(reffe::ReferenceFE,d::Integer)
"""
num_faces(reffe::ReferenceFE) = num_faces(get_polytope(reffe))
num_faces(reffe::ReferenceFE,d::Integer) = num_faces(get_polytope(reffe),d)
"""
num_vertices(reffe::ReferenceFE)
"""
num_vertices(reffe::ReferenceFE) = num_vertices(get_polytope(reffe))
"""
num_edges(reffe::ReferenceFE)
"""
num_edges(reffe::ReferenceFE) = num_edges(get_polytope(reffe))
"""
num_facets(reffe::ReferenceFE)
"""
num_facets(reffe::ReferenceFE) = num_facets(get_polytope(reffe))
"""
get_face_own_dofs(reffe::ReferenceFE,conf::Conformity,d::Integer)
"""
function get_face_own_dofs(reffe::ReferenceFE,conf::Conformity,d::Integer)
p = get_polytope(reffe)
range = get_dimrange(p,d)
get_face_own_dofs(reffe,conf)[range]
end
"""
get_face_own_dofs(reffe::ReferenceFE,d::Integer)
"""
function get_face_own_dofs(reffe::ReferenceFE,d::Integer)
conf = Conformity(reffe)
get_face_own_dofs(reffe,conf,d)
end
"""
get_face_dofs(reffe::ReferenceFE,d::Integer)
"""
function get_face_dofs(reffe::ReferenceFE,d::Integer)
p = get_polytope(reffe)
range = get_dimrange(p,d)
get_face_dofs(reffe)[range]
end
"""
get_face_own_dofs_permutations(reffe::ReferenceFE,conf::Conformity,d::Integer)
"""
function get_face_own_dofs_permutations(reffe::ReferenceFE,conf::Conformity,d::Integer)
p = get_polytope(reffe)
range = get_dimrange(p,d)
get_face_own_dofs_permutations(reffe,conf)[range]
end
"""
get_face_own_dofs_permutations(reffe::ReferenceFE,d::Integer)
"""
function get_face_own_dofs_permutations(reffe::ReferenceFE,d::Integer)
conf = Conformity(reffe)
get_face_own_dofs_permutations(reffe,conf,d)
end
"""
get_own_dofs_permutations(reffe::ReferenceFE,conf::Conformity)
"""
function get_own_dofs_permutations(reffe::ReferenceFE,conf::Conformity)
n = num_faces(get_polytope(reffe))
get_face_own_dofs_permutations(reffe,conf)[n]
end
"""
get_own_dofs_permutations(reffe::ReferenceFE)
"""
function get_own_dofs_permutations(reffe::ReferenceFE)
conf = Conformity(reffe)
get_own_dofs_permutations(reffe,conf)
end
"""
get_shapefuns(reffe::ReferenceFE) -> Field
Returns the basis of shape functions (i.e. the canonical basis)
associated with the reference FE. The result is encoded as a `Field` object.
"""
function get_shapefuns(reffe::ReferenceFE)
dofs = get_dof_basis(reffe)
prebasis = get_prebasis(reffe)
compute_shapefuns(dofs,prebasis)
end
"""
compute_shapefuns(dofs,prebasis)
Helper function used to compute the shape function basis
associated with the dof basis `dofs` and the basis `prebasis`.
It is equivalent to
change = inv(evaluate(dofs,prebasis))
linear_combination(change,prebasis) # i.e. transpose(change)*prebasis
"""
function compute_shapefuns(dofs,prebasis)
change = inv(evaluate(dofs,prebasis))
linear_combination(change,prebasis)
end
"""
compute_dofs(predofs,shapefuns)
Helper function used to compute the dof basis
associated with the dof basis `predofs` and the basis `shapefuns`.
It is equivalent to
change = inv(evaluate(predofs,shapefuns))
linear_combination(change,predofs) # i.e. transpose(change)*predofs
"""
function compute_dofs(predofs,shapefuns)
change = inv(evaluate(predofs,shapefuns))
linear_combination(change,predofs)
end
# Concrete implementation
"""
struct GenericRefFE{T,D} <: ReferenceFE{D}
# + private fields
end
This type is a *materialization* of the `ReferenceFE` interface. That is, it is a
`struct` that stores the values of all abstract methods in the `ReferenceFE` interface.
This type is useful to build reference FEs from the underlying ingredients without
the need to create a new type.
Note that some fields in this `struct` are type unstable deliberately in order to simplify the
type signature. Don't access them in computationally expensive functions,
instead extract the required fields before and pass them to the computationally expensive function.
"""
struct GenericRefFE{T,D} <: ReferenceFE{D}
ndofs::Int
polytope::Polytope{D}
prebasis::AbstractVector{<:Field}
dofs::AbstractVector{<:Dof}
conformity::Conformity
metadata
face_dofs::Vector{Vector{Int}}
shapefuns::AbstractVector{<:Field}
@doc """
GenericRefFE{T}(
ndofs::Int,
polytope::Polytope{D},
prebasis::AbstractVector{<:Field},
dofs::AbstractVector{<:Dof},
conformity::Conformity,
metadata,
face_dofs::Vector{Vector{Int}},
shapefuns::AbstractVector{<:Field}=compute_shapefuns(dofs,prebasis)) where {T,D}
Constructs a `GenericRefFE` object with the provided data.
"""
function GenericRefFE{T}(
ndofs::Int,
polytope::Polytope{D},
prebasis::AbstractVector{<:Field},
dofs::AbstractVector{<:Dof},
conformity::Conformity,
metadata,
face_dofs::Vector{Vector{Int}},
shapefuns::AbstractVector{<:Field}=compute_shapefuns(dofs,prebasis)) where {T,D}
new{T,D}(
ndofs,
polytope,
prebasis,
dofs,
conformity,
metadata,
face_dofs,
shapefuns)
end
@doc """
GenericRefFE{T}(
ndofs::Int,
polytope::Polytope{D},
prebasis::AbstractVector{<:Field},
predofs::AbstractVector{<:Dof},
conformity::Conformity,
metadata,
face_dofs::Vector{Vector{Int}},
shapefuns::AbstractVector{<:Field},
dofs::AbstractVector{<:Dof}=compute_dofs(predofs,shapefuns)) where {T,D}
Constructs a `GenericRefFE` object with the provided data.
"""
function GenericRefFE{T}(
ndofs::Int,
polytope::Polytope{D},
predofs::AbstractVector{<:Dof},
conformity::Conformity,
metadata,
face_dofs::Vector{Vector{Int}},
shapefuns::AbstractVector{<:Field},
dofs::AbstractVector{<:Dof}=compute_dofs(predofs,shapefuns)) where {T,D}
new{T,D}(
ndofs,
polytope,
shapefuns,
dofs,
conformity,
metadata,
face_dofs,
linear_combination(Eye{Int}(ndofs),shapefuns)) # Trick to be able to eval dofs af shapefuns in physical space
end
end
num_dofs(reffe::GenericRefFE) = reffe.ndofs
get_polytope(reffe::GenericRefFE) = reffe.polytope
get_prebasis(reffe::GenericRefFE) = reffe.prebasis
get_dof_basis(reffe::GenericRefFE) = reffe.dofs
Conformity(reffe::GenericRefFE) = reffe.conformity
get_face_dofs(reffe::GenericRefFE) = reffe.face_dofs
get_shapefuns(reffe::GenericRefFE) = reffe.shapefuns
get_metadata(reffe::GenericRefFE) = reffe.metadata