-
Notifications
You must be signed in to change notification settings - Fork 98
/
QGradMonomialBases.jl
483 lines (432 loc) · 11 KB
/
QGradMonomialBases.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
"""
struct QGradMonomialBasis{...} <: AbstractVector{Monomial}
This type implements a multivariate vector-valued polynomial basis
spanning the space needed for Nedelec reference elements on n-cubes.
The type parameters and fields of this `struct` are not public.
This type fully implements the [`Field`](@ref) interface, with up to first order
derivatives.
"""
struct QGradMonomialBasis{D,T} <: AbstractVector{Monomial}
order::Int
terms::CartesianIndices{D}
perms::Matrix{Int}
function QGradMonomialBasis(::Type{T},order::Int,terms::CartesianIndices{D},perms::Matrix{Int}) where {D,T}
new{D,T}(order,terms,perms)
end
end
Base.size(a::QGradMonomialBasis) = (_ndofs_qgrad(a),)
# @santiagobadia : Not sure we want to create the monomial machinery
Base.getindex(a::QGradMonomialBasis,i::Integer) = Monomial()
Base.IndexStyle(::QGradMonomialBasis) = IndexLinear()
"""
QGradMonomialBasis{D}(::Type{T},order::Int) where {D,T}
Returns a `QGradMonomialBasis` object. `D` is the dimension
of the coordinate space and `T` is the type of the components in the vector-value.
The `order` argument has the following meaning: the curl of the functions in this basis
is in the Q space of degree `order`.
"""
function QGradMonomialBasis{D}(::Type{T},order::Int) where {D,T}
@check T<:Real "T needs to be <:Real since represents the type of the components of the vector value"
_order = order + 1
_t = tfill(_order+1,Val{D-1}())
t = (_order,_t...)
terms = CartesianIndices(t)
perms = _prepare_perms(D)
QGradMonomialBasis(T,order,terms,perms)
end
"""
num_terms(f::QGradMonomialBasis{D,T}) where {D,T}
"""
num_terms(f::QGradMonomialBasis{D,T}) where {D,T} = length(f.terms)*D
get_order(f::QGradMonomialBasis) = f.order
function return_cache(f::QGradMonomialBasis{D,T},x::AbstractVector{<:Point}) where {D,T}
@check D == length(eltype(x)) "Incorrect number of point components"
np = length(x)
ndof = _ndofs_qgrad(f)
n = 1 + f.order+1
V = VectorValue{D,T}
r = CachedArray(zeros(V,(np,ndof)))
v = CachedArray(zeros(V,(ndof,)))
c = CachedArray(zeros(T,(D,n)))
(r, v, c)
end
function evaluate!(cache,f::QGradMonomialBasis{D,T},x::AbstractVector{<:Point}) where {D,T}
r, v, c = cache
np = length(x)
ndof = _ndofs_qgrad(f)
n = 1 + f.order+1
setsize!(r,(np,ndof))
setsize!(v,(ndof,))
setsize!(c,(D,n))
for i in 1:np
@inbounds xi = x[i]
_evaluate_nd_qgrad!(v,xi,f.order+1,f.terms,f.perms,c)
for j in 1:ndof
@inbounds r[i,j] = v[j]
end
end
r.array
end
function return_cache(
fg::FieldGradientArray{1,QGradMonomialBasis{D,T}},
x::AbstractVector{<:Point}) where {D,T}
f = fg.fa
@check D == length(eltype(x)) "Incorrect number of point components"
np = length(x)
ndof = _ndofs_qgrad(f)
n = 1 + f.order+1
xi = testitem(x)
V = VectorValue{D,T}
G = gradient_type(V,xi)
r = CachedArray(zeros(G,(np,ndof)))
v = CachedArray(zeros(G,(ndof,)))
c = CachedArray(zeros(T,(D,n)))
g = CachedArray(zeros(T,(D,n)))
(r, v, c, g)
end
function evaluate!(
cache,
fg::FieldGradientArray{1,QGradMonomialBasis{D,T}},
x::AbstractVector{<:Point}) where {D,T}
f = fg.fa
r, v, c, g = cache
np = length(x)
ndof = _ndofs_qgrad(f)
n = 1 + f.order+1
setsize!(r,(np,ndof))
setsize!(v,(ndof,))
setsize!(c,(D,n))
setsize!(g,(D,n))
V = VectorValue{D,T}
for i in 1:np
@inbounds xi = x[i]
_gradient_nd_qgrad!(v,xi,f.order+1,f.terms,f.perms,c,g,V)
for j in 1:ndof
@inbounds r[i,j] = v[j]
end
end
r.array
end
# Helpers
_ndofs_qgrad(f::QGradMonomialBasis{D}) where D = D*(length(f.terms))
function _prepare_perms(D)
perms = zeros(Int,D,D)
for j in 1:D
for d in j:D
perms[d,j] = d-j+1
end
for d in 1:(j-1)
perms[d,j] = d+(D-j)+1
end
end
perms
end
function _evaluate_nd_qgrad!(
v::AbstractVector{V},
x,
order,
terms::CartesianIndices{D},
perms::Matrix{Int},
c::AbstractMatrix{T}) where {V,T,D}
dim = D
for d in 1:dim
_evaluate_1d!(c,x,order,d)
end
o = one(T)
k = 1
m = zero(Mutable(V))
js = eachindex(m)
z = zero(T)
for ci in terms
for j in js
@inbounds for i in js
m[i] = z
end
s = o
@inbounds for d in 1:dim
s *= c[d,ci[perms[d,j]]]
end
m[j] = s
v[k] = m
k += 1
end
end
end
function _gradient_nd_qgrad!(
v::AbstractVector{G},
x,
order,
terms::CartesianIndices{D},
perms::Matrix{Int},
c::AbstractMatrix{T},
g::AbstractMatrix{T},
::Type{V}) where {G,T,D,V}
dim = D
for d in 1:dim
_evaluate_1d!(c,x,order,d)
_gradient_1d!(g,x,order,d)
end
z = zero(Mutable(V))
m = zero(Mutable(G))
js = eachindex(z)
mjs = eachindex(m)
o = one(T)
zi = zero(T)
k = 1
for ci in terms
for j in js
s = z
for i in js
s[i] = o
end
for q in 1:dim
for d in 1:dim
if d != q
@inbounds s[q] *= c[d,ci[perms[d,j]]]
else
@inbounds s[q] *= g[d,ci[perms[d,j]]]
end
end
end
@inbounds for i in mjs
m[i] = zi
end
for i in js
@inbounds m[i,j] = s[i]
end
@inbounds v[k] = m
k += 1
end
end
end
struct NedelecPrebasisOnSimplex{D} <: AbstractVector{Monomial}
order::Int
function NedelecPrebasisOnSimplex{D}(order::Integer) where D
new{D}(Int(order))
end
end
function Base.size(a::NedelecPrebasisOnSimplex{d}) where d
k = a.order+1
n = div(k*prod(i->(k+i),2:d),factorial(d-1))
(n,)
end
Base.getindex(a::NedelecPrebasisOnSimplex,i::Integer) = Monomial()
Base.IndexStyle(::Type{<:NedelecPrebasisOnSimplex}) = IndexLinear()
num_terms(a::NedelecPrebasisOnSimplex) = length(a)
get_order(f::NedelecPrebasisOnSimplex) = f.order
function return_cache(
f::NedelecPrebasisOnSimplex{d},x::AbstractVector{<:Point}) where d
np = length(x)
ndofs = num_terms(f)
V = eltype(x)
a = zeros(V,(np,ndofs))
k = f.order+1
P = MonomialBasis{d}(VectorValue{d,Float64},k-1,(e,order)->sum(e)<=order)
cP = return_cache(P,x)
CachedArray(a), cP, P
end
function evaluate!(
cache,f::NedelecPrebasisOnSimplex{3},x::AbstractVector{<:Point})
ca,cP,P = cache
k = f.order+1
np = length(x)
ndofs = num_terms(f)
ndofsP = length(P)
setsize!(ca,(np,ndofs))
Px = evaluate!(cP,P,x)
a = ca.array
V = eltype(x)
T = eltype(V)
z = zero(T)
u = one(T)
for (ip,p) in enumerate(x)
for j in 1:ndofsP
a[ip,j] = Px[ip,j]
end
i = ndofsP
x1,x2,x3 = x[ip]
zp = zero(x1)
for β in 1:k
for α in 1:(k+1-β)
i += 1
a[ip,i] = VectorValue(
-x1^(α-1)*x2^(k-α-β+2)*x3^(β-1),
x1^α*x2^(k-α-β+1)*x3^(β-1),
zp)
i += 1
a[ip,i] = VectorValue(
-x1^(k-α-β+1)*x2^(β-1)*x3^α,
zp,
x1^(k-α-β+2)*x2^(β-1)*x3^(α-1))
end
end
for γ in 1:k
i += 1
a[ip,i] = VectorValue(
zp,
-x2^(γ-1)*x3^(k-γ+1),
x2^γ*x3^(k-γ))
end
end
a
end
function evaluate!(
cache,f::NedelecPrebasisOnSimplex{2},x::AbstractVector{<:Point})
ca,cP,P = cache
k = f.order+1
np = length(x)
ndofs = num_terms(f)
ndofsP = length(P)
setsize!(ca,(np,ndofs))
a = ca.array
V = eltype(x)
T = eltype(V)
z = zero(T)
u = one(T)
Px = evaluate!(cP,P,x)
for (ip,p) in enumerate(x)
for j in 1:ndofsP
a[ip,j] = Px[ip,j]
end
i = ndofsP
x1,x2 = x[ip]
zp = zero(x1)
for α in 1:k
i += 1
a[ip,i] = VectorValue(-x1^(α-1)*x2^(k-α+1),x1^α*x2^(k-α))
end
#a[ip,1] = VectorValue((u,z))
#a[ip,2] = VectorValue((z,u))
#a[ip,3] = VectorValue((-p[2],p[1]))
end
a
end
function return_cache(
g::FieldGradientArray{1,<:NedelecPrebasisOnSimplex{D}},
x::AbstractVector{<:Point}) where D
f = g.fa
np = length(x)
ndofs = num_terms(f)
xi = testitem(x)
V = eltype(x)
G = gradient_type(V,xi)
a = zeros(G,(np,ndofs))
k = f.order+1
mb = MonomialBasis{D}(VectorValue{D,Float64},k-1,(e,order)->sum(e)<=order)
P = Broadcasting(∇)(mb)
cP = return_cache(P,x)
CachedArray(a), cP, P
end
function evaluate!(
cache,
g::FieldGradientArray{1,<:NedelecPrebasisOnSimplex{3}},
x::AbstractVector{<:Point})
ca,cP,P = cache
f = g.fa
k = f.order+1
np = length(x)
ndofs = num_terms(f)
setsize!(ca,(np,ndofs))
a = ca.array
fill!(a,zero(eltype(a)))
ndofsP = length(P)
Px = evaluate!(cP,P,x)
V = eltype(x)
T = eltype(V)
z = zero(T)
u = one(T)
for (ip,p) in enumerate(x)
for j in 1:ndofsP
a[ip,j] = Px[ip,j]
end
i = ndofsP
x1,x2,x3 = x[ip]
zp = zero(x1)
for β in 1:k
for α in 1:(k+1-β)
i += 1
a[ip,i] = TensorValue(
#-x1^(α-1)*x2^(k-α-β+2)*x3^(β-1),
-(α-1)*_exp(x1,α-2)*x2^(k-α-β+2)*x3^(β-1),
-x1^(α-1)*(k-α-β+2)*_exp(x2,k-α-β+1)*x3^(β-1),
-x1^(α-1)*x2^(k-α-β+2)*(β-1)*_exp(x3,β-2),
#x1^α*x2^(k-α-β+1)*x3^(β-1),
α*_exp(x1,α-1)*x2^(k-α-β+1)*x3^(β-1),
x1^α*(k-α-β+1)*_exp(x2,k-α-β)*x3^(β-1),
x1^α*x2^(k-α-β+1)*(β-1)*_exp(x3,β-2),
#zp,
zp,zp,zp)
i += 1
a[ip,i] = TensorValue(
#-x1^(k-α-β+1)*x2^(β-1)*x3^α,
-(k-α-β+1)*_exp(x1,k-α-β)*x2^(β-1)*x3^α,
-x1^(k-α-β+1)*(β-1)*_exp(x2,β-2)*x3^α,
-x1^(k-α-β+1)*x2^(β-1)*α*_exp(x3,α-1),
# zp
zp,zp,zp,
#x1^(k-α-β+2)*x2^(β-1)*x3^(α-1),
(k-α-β+2)*_exp(x1,k-α-β+1)*x2^(β-1)*x3^(α-1),
x1^(k-α-β+2)*(β-1)*_exp(x2,β-2)*x3^(α-1),
x1^(k-α-β+2)*x2^(β-1)*(α-1)*_exp(x3,α-2))
end
end
for γ in 1:k
i += 1
a[ip,i] = TensorValue(
#zp
zp,zp,zp,
#-x2^(γ-1)*x3^(k-γ+1),
-0*x2^(γ-1)*x3^(k-γ+1),
-(γ-1)*_exp(x2,γ-2)*x3^(k-γ+1),
-x2^(γ-1)*(k-γ+1)*_exp(x3,k-γ),
#x2^γ*x3^(k-γ),
0*x2^γ*x3^(k-γ),
γ*_exp(x2,γ-1)*x3^(k-γ),
x2^γ*(k-γ)*_exp(x3,k-γ-1))
end
#a[ip,4] = TensorValue((z,-u,z, u,z,z, z,z,z))
#a[ip,5] = TensorValue((z,z,-u, z,z,z, u,z,z))
#a[ip,6] = TensorValue((z,z,z, z,z,-u, z,u,z))
end
a
end
_exp(a,y) = y>0 ? a^y : one(a)
function evaluate!(
cache,
g::FieldGradientArray{1,<:NedelecPrebasisOnSimplex{2}},
x::AbstractVector{<:Point})
f = g.fa
ca,cP,P = cache
k = f.order+1
np = length(x)
ndofs = num_terms(f)
setsize!(ca,(np,ndofs))
a = ca.array
fill!(a,zero(eltype(a)))
V = eltype(x)
T = eltype(V)
z = zero(T)
u = one(T)
ndofsP = length(P)
Px = evaluate!(cP,P,x)
for (ip,p) in enumerate(x)
for j in 1:ndofsP
a[ip,j] = Px[ip,j]
end
i = ndofsP
x1,x2 = x[ip]
zp = zero(x1)
for α in 1:k
i += 1
a[ip,i] = TensorValue(
#-x1^(α-1)*x2^(k-α+1),
-(α-1)*_exp(x1,α-2)*x2^(k-α+1),
-x1^(α-1)*(k-α+1)*_exp(x2,k-α),
#x1^α*x2^(k-α),
α*_exp(x1,α-1)*x2^(k-α),
x1^α*(k-α)*_exp(x2,k-α-1))
end
#a[ip,3] = TensorValue((z,-u, u,z))
end
a
end