-
Notifications
You must be signed in to change notification settings - Fork 98
/
NedelecRefFEs.jl
366 lines (282 loc) · 9.86 KB
/
NedelecRefFEs.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
struct CurlConformity <: Conformity end
struct Nedelec <: ReferenceFEName end
const nedelec = Nedelec()
"""
NedelecRefFE(::Type{et},p::Polytope,order::Integer) where et
The `order` argument has the following meaning: the curl of the functions in this basis
is in the Q space of degree `order`.
"""
function NedelecRefFE(::Type{et},p::Polytope,order::Integer) where et
# @santiagobadia : Project, go to complex numbers
D = num_dims(p)
if is_n_cube(p)
prebasis = QGradMonomialBasis{D}(et,order)
elseif is_simplex(p)
prebasis = Polynomials.NedelecPrebasisOnSimplex{D}(order)
else
@unreachable "Only implemented for n-cubes and simplices"
end
nf_nodes, nf_moments = _Nedelec_nodes_and_moments(et,p,order)
face_own_dofs = _face_own_dofs_from_moments(nf_moments)
face_dofs = face_own_dofs
dof_basis = MomentBasedDofBasis(nf_nodes, nf_moments)
ndofs = num_dofs(dof_basis)
metadata = nothing
reffe = GenericRefFE{Nedelec}(
ndofs,
p,
prebasis,
dof_basis,
CurlConformity(),
metadata,
face_dofs)
reffe
end
function ReferenceFE(p::Polytope,::Nedelec, order)
NedelecRefFE(Float64,p,order)
end
function ReferenceFE(p::Polytope,::Nedelec,::Type{T}, order) where T
NedelecRefFE(T,p,order)
end
function Conformity(reffe::GenericRefFE{Nedelec},sym::Symbol)
hcurl = (:Hcurl,:HCurl)
if sym == :L2
L2Conformity()
elseif sym in hcurl
CurlConformity()
else
@unreachable """\n
It is not possible to use conformity = $sym on a Nedelec reference FE.
Possible values of conformity for this reference fe are $((:L2, hcurl...)).
"""
end
end
function get_face_own_dofs(reffe::GenericRefFE{Nedelec}, conf::CurlConformity)
reffe.face_dofs # For Nedelec, this member variable holds the face owned dofs
end
function get_face_own_dofs(reffe::GenericRefFE{Nedelec}, conf::L2Conformity)
face_own_dofs=[Int[] for i in 1:num_faces(reffe)]
face_own_dofs[end]=collect(1:num_dofs(reffe))
face_own_dofs
end
function get_face_dofs(reffe::GenericRefFE{Nedelec,Dc}) where Dc
face_dofs=[Int[] for i in 1:num_faces(reffe)]
face_own_dofs=get_face_own_dofs(reffe)
p = get_polytope(reffe)
for d=1:Dc # Starting from edges, vertices do not own DoFs for Nedelec
first_face = get_offset(p,d)
nfaces = num_faces(reffe,d)
for face=first_face+1:first_face+nfaces
for df=1:d-1
face_faces = get_faces(p,d,df)
first_cface = get_offset(p,df)
for cface in face_faces[face-first_face]
cface_own_dofs = face_own_dofs[first_cface+cface]
for dof in cface_own_dofs
push!(face_dofs[face],dof)
end
end
end
for dof in face_own_dofs[face]
push!(face_dofs[face],dof)
end
end
end
face_dofs
end
function _Nedelec_nodes_and_moments(::Type{et}, p::Polytope, order::Integer) where et
@notimplementedif !( is_n_cube(p) || (is_simplex(p) ) )
D = num_dims(p)
ft = VectorValue{D,et}
pt = Point{D,et}
nf_nodes = [ zeros(pt,0) for face in 1:num_faces(p)]
nf_moments = [ zeros(ft,0,0) for face in 1:num_faces(p)]
ecips, emoments = _Nedelec_edge_values(p,et,order)
erange = get_dimrange(p,1)
nf_nodes[erange] = ecips
nf_moments[erange] = emoments
if ( num_dims(p) == 3 && order > 0)
if is_n_cube(p)
fcips, fmoments = _Nedelec_face_values(p,et,order)
else
fcips, fmoments = _Nedelec_face_values_simplex(p,et,order)
end
frange = get_dimrange(p,D-1)
nf_nodes[frange] = fcips
nf_moments[frange] = fmoments
end
if ( is_n_cube(p) && order > 0) || ( is_simplex(p) && order > D-2)
ccips, cmoments = _Nedelec_cell_values(p,et,order)
crange = get_dimrange(p,D)
nf_nodes[crange] = ccips
nf_moments[crange] = cmoments
end
nf_nodes, nf_moments
end
function _Nedelec_edge_values(p,et,order)
# Reference facet
dim1 = 1
ep = Polytope{dim1}(p,1)
# geomap from ref face to polytope faces
egeomap = _ref_face_to_faces_geomap(p,ep)
# Compute integration points at all polynomial edges
degree = (order)*2
equad = Quadrature(ep,degree)
cips = get_coordinates(equad)
wips = get_weights(equad)
c_eips, ecips, ewips = _nfaces_evaluation_points_weights(p, egeomap, cips, wips)
# Edge moments, i.e., M(Ei)_{ab} = q_RE^a(xgp_REi^b) w_Fi^b t_Ei ⋅ ()
eshfs = MonomialBasis(et,ep,order)
emoments = _Nedelec_edge_moments(p, eshfs, c_eips, ecips, ewips)
return ecips, emoments
end
function _Nedelec_edge_moments(p, fshfs, c_fips, fcips, fwips)
ts = get_edge_tangent(p)
nc = length(c_fips)
cfshfs = fill(fshfs, nc)
cvals = lazy_map(evaluate,cfshfs,c_fips)
cvals = [fwips[i].*cvals[i] for i in 1:nc]
# @santiagobadia : Only working for oriented meshes now
cvals = [ _broadcast(typeof(t),t,b) for (t,b) in zip(ts,cvals)]
return cvals
end
function _Nedelec_face_values(p,et,order)
# Reference facet
@assert is_n_cube(p) "We are assuming that all n-faces of the same n-dim are the same."
fp = Polytope{num_dims(p)-1}(p,1)
# geomap from ref face to polytope faces
fgeomap = _ref_face_to_faces_geomap(p,fp)
# Compute integration points at all polynomial edges
degree = (order)*2
fquad = Quadrature(fp,degree)
fips = get_coordinates(fquad)
wips = get_weights(fquad)
c_fips, fcips, fwips = _nfaces_evaluation_points_weights(p, fgeomap, fips, wips)
# Face moments, i.e., M(Fi)_{ab} = w_Fi^b q_RF^a(xgp_RFi^b) (n_Fi × ())
fshfs = QGradMonomialBasis{num_dims(fp)}(et,order-1)
fmoments = _Nedelec_face_moments(p, fshfs, c_fips, fcips, fwips)
return fcips, fmoments
end
function _Nedelec_face_moments(p, fshfs, c_fips, fcips, fwips)
nc = length(c_fips)
cfshfs = fill(fshfs, nc)
cvals = lazy_map(evaluate,cfshfs,c_fips)
fvs = _nfaces_vertices(Float64,p,num_dims(p)-1)
fts = [hcat([vs[2]-vs[1]...],[vs[3]-vs[1]...]) for vs in fvs]
# Ref facet FE functions evaluated at the facet integration points (in ref facet)
cvals = [fwips[i].*cvals[i] for i in 1:nc]
fns = get_facet_normal(p)
os = get_facet_orientations(p)
# @santiagobadia : Temporary hack for making it work for structured hex meshes
ft = eltype(fns)
cvals = [ _broadcast_extend(ft,Tm,b) for (Tm,b) in zip(fts,cvals)]
cvals = [ _broadcast_cross(ft,n*o,b) for (n,o,b) in zip(fns,os,cvals)]
return cvals
end
function _Nedelec_face_values_simplex(p,et,order)
# Reference facet
@assert is_simplex(p) "We are assuming that all n-faces of the same n-dim are the same."
fp = Polytope{num_dims(p)-1}(p,1)
# geomap from ref face to polytope faces
fgeomap = _ref_face_to_faces_geomap(p,fp)
# Compute integration points at all polynomial edges
degree = (order)*2
fquad = Quadrature(fp,degree)
fips = get_coordinates(fquad)
wips = get_weights(fquad)
c_fips, fcips, fwips, fJtips = _nfaces_evaluation_points_weights_with_jac(p, fgeomap, fips, wips)
Df = num_dims(fp)
fshfs = MonomialBasis{Df}(VectorValue{Df,et},order-1,(e,k)->sum(e)<=k)
fmoments = _Nedelec_face_moments_simplex(p, fshfs, c_fips, fcips, fwips, fJtips)
return fcips, fmoments
end
function _nfaces_evaluation_points_weights_with_jac(p, fgeomap, fips, wips)
nc = length(fgeomap)
c_fips = fill(fips,nc)
c_wips = fill(wips,nc)
pquad = lazy_map(evaluate,fgeomap,c_fips)
## Must account for diagonals in simplex discretizations to get the correct
## scaling
Jt1 = lazy_map(∇,fgeomap)
Jt1_ips = lazy_map(evaluate,Jt1,c_fips)
#det_J = lazy_map(Broadcasting(meas),Jt1_ips)
#c_detwips = collect(lazy_map(Broadcasting(*),c_wips,det_J))
c_detwips = c_wips
c_fips, pquad, c_detwips, Jt1_ips
end
function _Nedelec_face_moments_simplex(p, fshfs, c_fips, fcips, fwips, fJtips)
nc = length(c_fips)
cfshfs = fill(fshfs, nc)
cfshfs_fips = lazy_map(evaluate,cfshfs,c_fips)
function weigth(qij,Jti,wi)
Ji = transpose(Jti)
Ji⋅qij*wi
end
cvals = map(Broadcasting(weigth),cfshfs_fips,fJtips,fwips)
return cvals
end
# It provides for every cell the nodes and the moments arrays
function _Nedelec_cell_values(p,et,order)
# Compute integration points at interior
degree = 2*(order)
iquad = Quadrature(p,degree)
ccips = get_coordinates(iquad)
cwips = get_weights(iquad)
# Cell moments, i.e., M(C)_{ab} = q_C^a(xgp_C^b) w_C^b ⋅ ()
if is_n_cube(p)
cbasis = QCurlGradMonomialBasis{num_dims(p)}(et,order-1)
else
D = num_dims(p)
cbasis = MonomialBasis{D}(VectorValue{D,et},order-D+1,(e,k)->sum(e)<=k)
end
cmoments = _Nedelec_cell_moments(p, cbasis, ccips, cwips )
return [ccips], [cmoments]
end
const _Nedelec_cell_moments = _RT_cell_moments
function _broadcast_cross(::Type{T},n,b) where T
c = Array{T}(undef,size(b))
for (ii, i) in enumerate(b)
c[ii] = T(cross(get_array(i),get_array(n)))# cross product
end
return c
end
function _broadcast_extend(::Type{T},Tm,b) where T
c = Array{T}(undef,size(b))
for (ii,i) in enumerate(b)
c[ii] = T(Tm*[i...])
end
return c
end
struct CoVariantPiolaMap <: Map end
function evaluate!(
cache,
::Broadcasting{typeof(∇)},
a::Fields.BroadcastOpFieldArray{CoVariantPiolaMap})
v, Jt = a.args
# Assuming J comes from an affine map
∇v = Broadcasting(∇)(v)
k = CoVariantPiolaMap()
Broadcasting(Operation(k))(∇v,Jt)
end
function lazy_map(
::Broadcasting{typeof(gradient)},
a::LazyArray{<:Fill{Broadcasting{Operation{CoVariantPiolaMap}}}})
v, Jt = a.args
∇v = lazy_map(Broadcasting(∇),v)
k = CoVariantPiolaMap()
lazy_map(Broadcasting(Operation(k)),∇v,Jt)
end
function evaluate!(cache,::CoVariantPiolaMap,v::Number,Jt::Number)
v⋅transpose(inv(Jt)) # we multiply by the right side to compute the gradient correctly
end
function evaluate!(cache,k::CoVariantPiolaMap,v::AbstractVector{<:Field},phi::Field)
Jt = ∇(phi)
Broadcasting(Operation(k))(v,Jt)
end
function lazy_map(
k::CoVariantPiolaMap,
cell_ref_shapefuns::AbstractArray{<:AbstractArray{<:Field}},
cell_map::AbstractArray{<:Field})
cell_Jt = lazy_map(∇,cell_map)
lazy_map(Broadcasting(Operation(k)),cell_ref_shapefuns,cell_Jt)
end