-
Notifications
You must be signed in to change notification settings - Fork 9
/
spectralelement.jl
2254 lines (2037 loc) · 70.6 KB
/
spectralelement.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
abstract type AbstractSpectralStyle <: Fields.AbstractFieldStyle end
"""
SpectralStyle()
Broadcasting requires use of spectral-element operations.
"""
struct SpectralStyle <: AbstractSpectralStyle end
"""
SlabBlockSpectralStyle()
Applies spectral-element operations using by making use of intermediate
temporaries for each operator. This is used for CPU kernels.
"""
struct SlabBlockSpectralStyle <: AbstractSpectralStyle end
"""
CUDASpectralStyle()
Applies spectral-element operations by using threads for each node, and
synchronizing when they occur. This is used for GPU kernels.
"""
struct CUDASpectralStyle <: AbstractSpectralStyle end
import ClimaComms
AbstractSpectralStyle(::ClimaComms.AbstractCPUDevice) = SlabBlockSpectralStyle
AbstractSpectralStyle(::ClimaComms.CUDADevice) = CUDASpectralStyle
"""
SpectralElementOperator
Represents an operation that is applied to each element.
Subtypes `Op` of this should define the following:
- [`operator_return_eltype(::Op, ElTypes...)`](@ref)
- [`allocate_work(::Op, args...)`](@ref)
- [`apply_operator(::Op, work, args...)`](@ref)
Additionally, the result type `OpResult <: OperatorSlabResult` of `apply_operator` should define `get_node(::OpResult, ij, slabidx)`.
"""
abstract type SpectralElementOperator <: AbstractOperator end
"""
operator_axes(space)
Return a tuple of the axis indicies a given field operator works over.
"""
function operator_axes end
operator_axes(space::Spaces.AbstractSpace) = ()
operator_axes(space::Spaces.SpectralElementSpace1D) = (1,)
operator_axes(space::Spaces.SpectralElementSpace2D) = (1, 2)
operator_axes(space::Spaces.SpectralElementSpaceSlab1D) = (1,)
operator_axes(space::Spaces.SpectralElementSpaceSlab2D) = (1, 2)
operator_axes(space::Spaces.ExtrudedFiniteDifferenceSpace) =
operator_axes(Spaces.horizontal_space(space))
function node_indices(space::Spaces.SpectralElementSpace1D)
QS = Spaces.quadrature_style(space)
Nq = Quadratures.degrees_of_freedom(QS)
CartesianIndices((Nq,))
end
function node_indices(space::Spaces.SpectralElementSpace2D)
QS = Spaces.quadrature_style(space)
Nq = Quadratures.degrees_of_freedom(QS)
CartesianIndices((Nq, Nq))
end
node_indices(space::Spaces.ExtrudedFiniteDifferenceSpace) =
node_indices(Spaces.horizontal_space(space))
"""
SpectralBroadcasted{Style}(op, args[,axes[, work]])
This is similar to a `Base.Broadcast.Broadcasted` object, except it contains space for an intermediate `work` storage.
This is returned by `Base.Broadcast.broadcasted(op::SpectralElementOperator)`.
"""
struct SpectralBroadcasted{Style, Op, Args, Axes, Work} <:
OperatorBroadcasted{Style}
op::Op
args::Args
axes::Axes
work::Work
end
SpectralBroadcasted{Style}(
op::Op,
args::Args,
axes::Axes = nothing,
work::Work = nothing,
) where {Style, Op, Args, Axes, Work} =
SpectralBroadcasted{Style, Op, Args, Axes, Work}(op, args, axes, work)
Adapt.adapt_structure(to, sbc::SpectralBroadcasted{Style}) where {Style} =
SpectralBroadcasted{Style}(
sbc.op,
Adapt.adapt(to, sbc.args),
Adapt.adapt(to, sbc.axes),
)
return_space(::SpectralElementOperator, space) = space
function Base.Broadcast.broadcasted(op::SpectralElementOperator, args...)
args′ = map(Base.Broadcast.broadcastable, args)
style = Base.Broadcast.result_style(
SpectralStyle(),
Base.Broadcast.combine_styles(args′...),
)
Base.Broadcast.broadcasted(style, op, args′...)
end
Base.Broadcast.broadcasted(
::SpectralStyle,
op::SpectralElementOperator,
args...,
) = SpectralBroadcasted{SpectralStyle}(op, args)
Base.eltype(sbc::SpectralBroadcasted) =
operator_return_eltype(sbc.op, map(eltype, sbc.args)...)
function Base.Broadcast.instantiate(sbc::SpectralBroadcasted)
op = sbc.op
# recursively instantiate the arguments to allocate intermediate work arrays
args = instantiate_args(sbc.args)
# axes: same logic as Broadcasted
if sbc.axes isa Nothing # Not done via dispatch to make it easier to extend instantiate(::Broadcasted{Style})
axes = Base.axes(sbc)
else
axes = sbc.axes
Base.Broadcast.check_broadcast_axes(axes, args...)
end
op = typeof(op)(axes)
Style = AbstractSpectralStyle(ClimaComms.device(axes))
return SpectralBroadcasted{Style}(op, args, axes)
end
function Base.Broadcast.instantiate(
bc::Base.Broadcast.Broadcasted{<:AbstractSpectralStyle},
)
# recursively instantiate the arguments to allocate intermediate work arrays
args = instantiate_args(bc.args)
# axes: same logic as Broadcasted
if bc.axes isa Nothing # Not done via dispatch to make it easier to extend instantiate(::Broadcasted{Style})
axes = Base.Broadcast.combine_axes(args...)
else
axes = bc.axes
Base.Broadcast.check_broadcast_axes(axes, args...)
end
Style = AbstractSpectralStyle(ClimaComms.device(axes))
return Base.Broadcast.Broadcasted{Style}(bc.f, args, axes)
end
function Base.similar(
bc::Base.Broadcast.Broadcasted{<:AbstractSpectralStyle},
::Type{Eltype},
) where {Eltype}
space = axes(bc)
return Field(Eltype, space)
end
# Functions for SlabBlockSpectralStyle
function Base.copyto!(
out::Field,
sbc::Union{
SpectralBroadcasted{SlabBlockSpectralStyle},
Broadcasted{SlabBlockSpectralStyle},
},
)
Fields.byslab(axes(out)) do slabidx
Base.@_inline_meta
@inbounds copyto_slab!(out, sbc, slabidx)
end
return out
end
"""
copyto_slab!(out, bc, slabidx)
Copy the slab indexed by `slabidx` from `bc` to `out`.
"""
Base.@propagate_inbounds function copyto_slab!(out, bc, slabidx)
space = axes(out)
QS = Spaces.quadrature_style(space)
Nq = Quadratures.degrees_of_freedom(QS)
rbc = resolve_operator(bc, slabidx)
@inbounds for ij in node_indices(axes(out))
set_node!(space, out, ij, slabidx, get_node(space, rbc, ij, slabidx))
end
return nothing
end
"""
resolve_operator(bc, slabidx)
Recursively evaluate any operators in `bc` at `slabidx`, replacing any
`SpectralBroadcasted` objects.
- if `bc` is a regular `Broadcasted` object, return a new `Broadcasted` with `resolve_operator` called on each `arg`
- if `bc` is a regular `SpectralBroadcasted` object:
- call `resolve_operator` called on each `arg`
- call `apply_operator`, returning the resulting "pseudo Field": a `Field` with an
`IF`/`IJF` data object.
- if `bc` is a `Field`, return that
"""
Base.@propagate_inbounds function resolve_operator(
bc::SpectralBroadcasted{SlabBlockSpectralStyle},
slabidx,
)
args = _resolve_operator_args(slabidx, bc.args...)
apply_operator(bc.op, bc.axes, slabidx, args...)
end
Base.@propagate_inbounds function resolve_operator(
bc::Base.Broadcast.Broadcasted{SlabBlockSpectralStyle},
slabidx,
)
args = _resolve_operator_args(slabidx, bc.args...)
Base.Broadcast.Broadcasted{SlabBlockSpectralStyle}(bc.f, args, bc.axes)
end
@inline resolve_operator(x, slabidx) = x
"""
_resolve_operator(slabidx, args...)
Calls `resolve_operator(arg, slabidx)` for each `arg` in `args`
"""
@inline _resolve_operator_args(slabidx) = ()
Base.@propagate_inbounds _resolve_operator_args(slabidx, arg, xargs...) = (
resolve_operator(arg, slabidx),
_resolve_operator_args(slabidx, xargs...)...,
)
function strip_space(bc::SpectralBroadcasted{Style}, parent_space) where {Style}
current_space = axes(bc)
new_space = placeholder_space(current_space, parent_space)
return SpectralBroadcasted{Style}(
bc.op,
strip_space_args(bc.args, current_space),
new_space,
)
end
function Base.copyto!(
out::Field,
sbc::Union{
SpectralBroadcasted{CUDASpectralStyle},
Broadcasted{CUDASpectralStyle},
},
)
space = axes(out)
QS = Spaces.quadrature_style(space)
Nq = Quadratures.degrees_of_freedom(QS)
Nh = Topologies.nlocalelems(Spaces.topology(space))
Nv = Spaces.nlevels(space)
max_threads = 256
@assert Nq * Nq ≤ max_threads
Nvthreads = fld(max_threads, Nq * Nq)
Nvblocks = cld(Nv, Nvthreads)
# executed
@cuda always_inline = true threads = (Nq, Nq, Nvthreads) blocks =
(Nh, Nvblocks) copyto_spectral_kernel!(
strip_space(out, space),
strip_space(sbc, space),
space,
Val(Nvthreads),
)
return out
end
function copyto_spectral_kernel!(
out::Fields.Field,
sbc,
space,
::Val{Nvt},
) where {Nvt}
@inbounds begin
i = threadIdx().x
j = threadIdx().y
k = threadIdx().z
h = blockIdx().x
vid = k + (blockIdx().y - 1) * blockDim().z
# allocate required shmem
sbc_reconstructed = reconstruct_placeholder_broadcasted(space, sbc)
sbc_shmem = allocate_shmem(Val(Nvt), sbc_reconstructed)
# can loop over blocks instead?
if space isa Spaces.AbstractSpectralElementSpace
v = nothing
elseif space isa Spaces.FaceExtrudedFiniteDifferenceSpace
v = vid - half
elseif space isa Spaces.CenterExtrudedFiniteDifferenceSpace
v = vid
else
error("Invalid space")
end
ij = CartesianIndex((i, j))
slabidx = Fields.SlabIndex(v, h)
# v may potentially be out-of-range: any time memory is accessed, it
# should be checked by a call to is_valid_index(space, ij, slabidx)
# resolve_shmem! needs to be called even when out of range, so that
# sync_threads() is invoked collectively
resolve_shmem!(sbc_shmem, ij, slabidx)
isactive = is_valid_index(space, ij, slabidx)
if isactive
result = get_node(space, sbc_shmem, ij, slabidx)
set_node!(space, out, ij, slabidx, result)
end
end
return nothing
end
"""
reconstruct_placeholder_broadcasted(space, obj)
Recurively reconstructs objects that have been stripped via `strip_space`.
"""
@inline reconstruct_placeholder_broadcasted(parent_space, obj) = obj
@inline function reconstruct_placeholder_broadcasted(
parent_space::Spaces.AbstractSpace,
field::Fields.Field,
)
space = reconstruct_placeholder_space(axes(field), parent_space)
return Fields.Field(Fields.field_values(field), space)
end
@inline function reconstruct_placeholder_broadcasted(
parent_space::Spaces.AbstractSpace,
bc::Broadcasted{Style},
) where {Style}
space = reconstruct_placeholder_space(axes(bc), parent_space)
args = _reconstruct_placeholder_broadcasted(space, bc.args...)
return Broadcasted{Style}(bc.f, args, space)
end
@inline function reconstruct_placeholder_broadcasted(
parent_space::Spaces.AbstractSpace,
sbc::SpectralBroadcasted{Style},
) where {Style}
space = reconstruct_placeholder_space(axes(sbc), parent_space)
args = _reconstruct_placeholder_broadcasted(space, sbc.args...)
return SpectralBroadcasted{Style}(sbc.op, args, space, sbc.work)
end
@inline _reconstruct_placeholder_broadcasted(parent_space) = ()
@inline _reconstruct_placeholder_broadcasted(parent_space, arg, xargs...) = (
reconstruct_placeholder_broadcasted(parent_space, arg),
_reconstruct_placeholder_broadcasted(parent_space, xargs...)...,
)
"""
is_valid_index(space, ij, slabidx)::Bool
Returns `true` if the node indices `ij` and slab indices `slabidx` are valid for
`space`.
"""
@inline function is_valid_index(space, ij, slabidx)
# if we want to support interpolate/restrict, we would need to check i <= Nq && j <= Nq
is_valid_index(space, slabidx)
end
# assumes h is always in a valid range
@inline function is_valid_index(
space::Spaces.AbstractSpectralElementSpace,
slabidx,
)
return true
end
@inline function is_valid_index(
space::Spaces.CenterExtrudedFiniteDifferenceSpace,
slabidx,
)
Nv = Spaces.nlevels(space)
return slabidx.v <= Nv
end
@inline function is_valid_index(
space::Spaces.FaceExtrudedFiniteDifferenceSpace,
slabidx,
)
Nv = Spaces.nlevels(space)
return slabidx.v + half <= Nv
end
"""
allocate_shmem(Val(Nvt), b)
Create a new broadcasted object with necessary share memory allocated,
using `Nvt` slabs per block.
"""
@inline function allocate_shmem(::Val{Nvt}, obj) where {Nvt}
obj
end
@inline function allocate_shmem(
::Val{Nvt},
bc::Broadcasted{Style},
) where {Nvt, Style}
Broadcasted{Style}(bc.f, _allocate_shmem(Val(Nvt), bc.args...), bc.axes)
end
@inline function allocate_shmem(
::Val{Nvt},
sbc::SpectralBroadcasted{Style},
) where {Nvt, Style}
args = _allocate_shmem(Val(Nvt), sbc.args...)
work = operator_shmem(sbc.axes, Val(Nvt), sbc.op, args...)
SpectralBroadcasted{Style}(sbc.op, args, sbc.axes, work)
end
@inline _allocate_shmem(::Val{Nvt}) where {Nvt} = ()
@inline _allocate_shmem(::Val{Nvt}, arg, xargs...) where {Nvt} =
(allocate_shmem(Val(Nvt), arg), _allocate_shmem(Val(Nvt), xargs...)...)
"""
resolve_shmem!(obj, ij, slabidx)
Recursively stores the arguments to all operators into shared memory, at the
given indices (if they are valid).
As this calls `sync_threads()`, it should be called collectively on all threads
at the same time.
"""
Base.@propagate_inbounds function resolve_shmem!(
sbc::SpectralBroadcasted,
ij,
slabidx,
)
space = axes(sbc)
isactive = is_valid_index(space, ij, slabidx)
_resolve_shmem!(ij, slabidx, sbc.args...)
# we could reuse shmem if we split this up
#==
if isactive
temp = compute thing to store in shmem
end
CUDA.sync_threads()
if isactive
shmem[i,j] = temp
end
CUDA.sync_threads()
===#
if isactive
operator_fill_shmem!(
sbc.op,
sbc.work,
space,
ij,
slabidx,
_get_node(space, ij, slabidx, sbc.args...)...,
)
end
CUDA.sync_threads()
return nothing
end
@inline _resolve_shmem!(ij, slabidx) = nothing
@inline function _resolve_shmem!(ij, slabidx, arg, xargs...)
resolve_shmem!(arg, ij, slabidx)
_resolve_shmem!(ij, slabidx, xargs...)
end
Base.@propagate_inbounds function resolve_shmem!(bc::Broadcasted, ij, slabidx)
_resolve_shmem!(ij, slabidx, bc.args...)
return nothing
end
Base.@propagate_inbounds function resolve_shmem!(obj, ij, slabidx)
nothing
end
Base.@propagate_inbounds function get_node(
space,
sbc::SpectralBroadcasted{CUDASpectralStyle},
ij,
slabidx,
)
operator_evaluate(sbc.op, sbc.work, sbc.axes, ij, slabidx)
end
@inline _get_node(space, ij, slabidx) = ()
Base.@propagate_inbounds _get_node(space, ij, slabidx, arg, xargs...) = (
get_node(space, arg, ij, slabidx),
_get_node(space, ij, slabidx, xargs...)...,
)
Base.@propagate_inbounds function get_node(space, scalar, ij, slabidx)
scalar[]
end
Base.@propagate_inbounds function get_node(
space,
scalar::Tuple{<:Any},
ij,
slabidx,
)
scalar[1]
end
Base.@propagate_inbounds function get_node(
parent_space,
field::Fields.Field,
ij::CartesianIndex{1},
slabidx,
)
space = reconstruct_placeholder_space(axes(field), parent_space)
i, = Tuple(ij)
if space isa Spaces.FaceExtrudedFiniteDifferenceSpace
v = slabidx.v + half
elseif space isa Spaces.CenterExtrudedFiniteDifferenceSpace ||
space isa Spaces.AbstractSpectralElementSpace
v = slabidx.v
else
error("invalid space")
end
h = slabidx.h
fv = Fields.field_values(field)
return fv[i, nothing, nothing, v, h]
end
Base.@propagate_inbounds function get_node(
parent_space,
field::Fields.Field,
ij::CartesianIndex{2},
slabidx,
)
space = reconstruct_placeholder_space(axes(field), parent_space)
i, j = Tuple(ij)
if space isa Spaces.FaceExtrudedFiniteDifferenceSpace
v = slabidx.v + half
elseif space isa Spaces.CenterExtrudedFiniteDifferenceSpace ||
space isa Spaces.AbstractSpectralElementSpace
v = slabidx.v
else
error("invalid space")
end
h = slabidx.h
fv = Fields.field_values(field)
return fv[i, j, nothing, v, h]
end
Base.@propagate_inbounds function get_node(
parent_space,
bc::Base.Broadcast.Broadcasted,
ij,
slabidx,
)
space = reconstruct_placeholder_space(axes(bc), parent_space)
bc.f(_get_node(space, ij, slabidx, bc.args...)...)
end
Base.@propagate_inbounds function get_node(
space,
data::Union{DataLayouts.IJF, DataLayouts.IF},
ij,
slabidx,
)
data[ij]
end
Base.@propagate_inbounds function get_node(
space,
data::StaticArrays.SArray,
ij,
slabidx,
)
data[ij]
end
dont_limit = (args...) -> true
for m in methods(get_node)
m.recursion_relation = dont_limit
end
Base.@propagate_inbounds function get_local_geometry(
space::Union{
Spaces.AbstractSpectralElementSpace,
Spaces.ExtrudedFiniteDifferenceSpace,
},
ij::CartesianIndex{1},
slabidx,
)
i, = Tuple(ij)
h = slabidx.h
if space isa Spaces.FaceExtrudedFiniteDifferenceSpace
v = slabidx.v + half
else
v = slabidx.v
end
lgd = Spaces.local_geometry_data(space)
return lgd[i, nothing, nothing, v, h]
end
Base.@propagate_inbounds function get_local_geometry(
space::Union{
Spaces.AbstractSpectralElementSpace,
Spaces.ExtrudedFiniteDifferenceSpace,
},
ij::CartesianIndex{2},
slabidx,
)
i, j = Tuple(ij)
h = slabidx.h
if space isa Spaces.FaceExtrudedFiniteDifferenceSpace
v = slabidx.v + half
else
v = slabidx.v
end
lgd = Spaces.local_geometry_data(space)
return lgd[i, j, nothing, v, h]
end
Base.@propagate_inbounds function set_node!(
space,
field::Fields.Field,
ij::CartesianIndex{1},
slabidx,
val,
)
i, = Tuple(ij)
if space isa Spaces.FaceExtrudedFiniteDifferenceSpace
v = slabidx.v + half
else
v = slabidx.v
end
h = slabidx.h
fv = Fields.field_values(field)
fv[i, nothing, nothing, v, h] = val
end
Base.@propagate_inbounds function set_node!(
space,
field::Fields.Field,
ij::CartesianIndex{2},
slabidx,
val,
)
i, j = Tuple(ij)
if space isa Spaces.FaceExtrudedFiniteDifferenceSpace
v = slabidx.v + half
else
v = slabidx.v
end
h = slabidx.h
fv = Fields.field_values(field)
fv[i, j, nothing, v, h] = val
end
Base.Broadcast.BroadcastStyle(
::Type{<:SpectralBroadcasted{Style}},
) where {Style} = Style()
Base.Broadcast.BroadcastStyle(
style::AbstractSpectralStyle,
::Fields.AbstractFieldStyle,
) = style
"""
div = Divergence()
div.(u)
Computes the per-element spectral (strong) divergence of a vector field ``u``.
The divergence of a vector field ``u`` is defined as
```math
\\nabla \\cdot u = \\sum_i \\frac{1}{J} \\frac{\\partial (J u^i)}{\\partial \\xi^i}
```
where ``J`` is the Jacobian determinant, ``u^i`` is the ``i``th contravariant
component of ``u``.
This is discretized by
```math
\\sum_i I \\left\\{\\frac{1}{J} \\frac{\\partial (I\\{J u^i\\})}{\\partial \\xi^i} \\right\\}
```
where ``I\\{x\\}`` is the interpolation operator that projects to the
unique polynomial interpolating ``x`` at the quadrature points. In matrix
form, this can be written as
```math
J^{-1} \\sum_i D_i J u^i
```
where ``D_i`` is the derivative matrix along the ``i``th dimension
## References
- [Taylor2010](@cite), equation 15
"""
struct Divergence{I} <: SpectralElementOperator end
Divergence() = Divergence{()}()
Divergence{()}(space) = Divergence{operator_axes(space)}()
operator_return_eltype(op::Divergence{I}, ::Type{S}) where {I, S} =
RecursiveApply.rmaptype(Geometry.divergence_result_type, S)
function apply_operator(op::Divergence{(1,)}, space, slabidx, arg)
FT = Spaces.undertype(space)
QS = Spaces.quadrature_style(space)
Nq = Quadratures.degrees_of_freedom(QS)
D = Quadratures.differentiation_matrix(FT, QS)
# allocate temp output
RT = operator_return_eltype(op, eltype(arg))
out = IF{RT, Nq}(MArray, FT)
fill!(parent(out), zero(FT))
@inbounds for i in 1:Nq
ij = CartesianIndex((i,))
local_geometry = get_local_geometry(space, ij, slabidx)
v = get_node(space, arg, ij, slabidx)
Jv¹ =
local_geometry.J ⊠ RecursiveApply.rmap(
v -> Geometry.contravariant1(v, local_geometry),
v,
)
for ii in 1:Nq
out[ii] = out[ii] ⊞ (D[ii, i] ⊠ Jv¹)
end
end
@inbounds for i in 1:Nq
ij = CartesianIndex((i,))
local_geometry = get_local_geometry(space, ij, slabidx)
out[i] = RecursiveApply.rmul(out[i], local_geometry.invJ)
end
return Field(SArray(out), space)
end
Base.@propagate_inbounds function apply_operator(
op::Divergence{(1, 2)},
space,
slabidx,
arg,
)
FT = Spaces.undertype(space)
QS = Spaces.quadrature_style(space)
Nq = Quadratures.degrees_of_freedom(QS)
D = Quadratures.differentiation_matrix(FT, QS)
# allocate temp output
RT = operator_return_eltype(op, eltype(arg))
out = DataLayouts.IJF{RT, Nq}(MArray, FT)
fill!(parent(out), zero(FT))
@inbounds for j in 1:Nq, i in 1:Nq
ij = CartesianIndex((i, j))
local_geometry = get_local_geometry(space, ij, slabidx)
v = get_node(space, arg, ij, slabidx)
Jv¹ =
local_geometry.J ⊠ RecursiveApply.rmap(
v -> Geometry.contravariant1(v, local_geometry),
v,
)
for ii in 1:Nq
out[ii, j] = out[ii, j] ⊞ (D[ii, i] ⊠ Jv¹)
end
Jv² =
local_geometry.J ⊠ RecursiveApply.rmap(
v -> Geometry.contravariant2(v, local_geometry),
v,
)
for jj in 1:Nq
out[i, jj] = out[i, jj] ⊞ (D[jj, j] ⊠ Jv²)
end
end
@inbounds for j in 1:Nq, i in 1:Nq
ij = CartesianIndex((i, j))
local_geometry = get_local_geometry(space, ij, slabidx)
out[i, j] = RecursiveApply.rmul(out[i, j], local_geometry.invJ)
end
return Field(SArray(out), space)
end
Base.@propagate_inbounds function operator_shmem(
space,
::Val{Nvt},
op::Divergence{(1, 2)},
arg,
) where {Nvt}
FT = Spaces.undertype(space)
QS = Spaces.quadrature_style(space)
Nq = Quadratures.degrees_of_freedom(QS)
# allocate temp output
RT = operator_return_eltype(op, eltype(arg))
Jv¹ = CUDA.CuStaticSharedArray(RT, (Nq, Nq, Nvt))
Jv² = CUDA.CuStaticSharedArray(RT, (Nq, Nq, Nvt))
return (Jv¹, Jv²)
end
Base.@propagate_inbounds function operator_fill_shmem!(
op::Divergence{(1, 2)},
(Jv¹, Jv²),
space,
ij,
slabidx,
arg,
)
vt = threadIdx().z
local_geometry = get_local_geometry(space, ij, slabidx)
i, j = ij.I
Jv¹[i, j, vt] =
local_geometry.J ⊠ RecursiveApply.rmap(
v -> Geometry.contravariant1(v, local_geometry),
arg,
)
Jv²[i, j, vt] =
local_geometry.J ⊠ RecursiveApply.rmap(
v -> Geometry.contravariant2(v, local_geometry),
arg,
)
end
Base.@propagate_inbounds function operator_evaluate(
op::Divergence{(1, 2)},
(Jv¹, Jv²),
space,
ij,
slabidx,
)
vt = threadIdx().z
i, j = ij.I
FT = Spaces.undertype(space)
QS = Spaces.quadrature_style(space)
Nq = Quadratures.degrees_of_freedom(QS)
D = Quadratures.differentiation_matrix(FT, QS)
local_geometry = get_local_geometry(space, ij, slabidx)
DJv = D[i, 1] ⊠ Jv¹[1, j, vt]
for k in 2:Nq
DJv = DJv ⊞ D[i, k] ⊠ Jv¹[k, j, vt]
end
for k in 1:Nq
DJv = DJv ⊞ D[j, k] ⊠ Jv²[i, k, vt]
end
return RecursiveApply.rmul(DJv, local_geometry.invJ)
end
"""
wdiv = WeakDivergence()
wdiv.(u)
Computes the "weak divergence" of a vector field `u`.
This is defined as the scalar field ``\\theta \\in \\mathcal{V}_0`` such that
for all ``\\phi\\in \\mathcal{V}_0``
```math
\\int_\\Omega \\phi \\theta \\, d \\Omega
=
- \\int_\\Omega (\\nabla \\phi) \\cdot u \\,d \\Omega
```
where ``\\mathcal{V}_0`` is the space of ``u``.
This arises as the contribution of the volume integral after by applying
integration by parts to the weak form expression of the divergence
```math
\\int_\\Omega \\phi (\\nabla \\cdot u) \\, d \\Omega
=
- \\int_\\Omega (\\nabla \\phi) \\cdot u \\,d \\Omega
+ \\oint_{\\partial \\Omega} \\phi (u \\cdot n) \\,d \\sigma
```
It can be written in matrix form as
```math
ϕ^\\top WJ θ = - \\sum_i (D_i ϕ)^\\top WJ u^i
```
which reduces to
```math
θ = -(WJ)^{-1} \\sum_i D_i^\\top WJ u^i
```
where
- ``J`` is the diagonal Jacobian matrix
- ``W`` is the diagonal matrix of quadrature weights
- ``D_i`` is the derivative matrix along the ``i``th dimension
"""
struct WeakDivergence{I} <: SpectralElementOperator end
WeakDivergence() = WeakDivergence{()}()
WeakDivergence{()}(space) = WeakDivergence{operator_axes(space)}()
operator_return_eltype(::WeakDivergence{I}, ::Type{S}) where {I, S} =
RecursiveApply.rmaptype(Geometry.divergence_result_type, S)
function apply_operator(op::WeakDivergence{(1,)}, space, slabidx, arg)
FT = Spaces.undertype(space)
QS = Spaces.quadrature_style(space)
Nq = Quadratures.degrees_of_freedom(QS)
D = Quadratures.differentiation_matrix(FT, QS)
# allocate temp output
RT = operator_return_eltype(op, eltype(arg))
out = DataLayouts.IF{RT, Nq}(MArray, FT)
fill!(parent(out), zero(FT))
@inbounds for i in 1:Nq
ij = CartesianIndex((i,))
local_geometry = get_local_geometry(space, ij, slabidx)
v = get_node(space, arg, ij, slabidx)
WJv¹ =
local_geometry.WJ ⊠ RecursiveApply.rmap(
v -> Geometry.contravariant1(v, local_geometry),
v,
)
for ii in 1:Nq
out[ii] = out[ii] ⊞ (D[i, ii] ⊠ WJv¹)
end
end
@inbounds for i in 1:Nq
ij = CartesianIndex((i,))
local_geometry = get_local_geometry(space, ij, slabidx)
out[i] = RecursiveApply.rdiv(out[i], ⊟(local_geometry.WJ))
end
return Field(SArray(out), space)
end
function apply_operator(op::WeakDivergence{(1, 2)}, space, slabidx, arg)
FT = Spaces.undertype(space)
QS = Spaces.quadrature_style(space)
Nq = Quadratures.degrees_of_freedom(QS)
D = Quadratures.differentiation_matrix(FT, QS)
RT = operator_return_eltype(op, eltype(arg))
out = DataLayouts.IJF{RT, Nq}(MArray, FT)
fill!(parent(out), zero(FT))
@inbounds for j in 1:Nq, i in 1:Nq
ij = CartesianIndex((i, j))
local_geometry = get_local_geometry(space, ij, slabidx)
v = get_node(space, arg, ij, slabidx)
WJv¹ =
local_geometry.WJ ⊠ RecursiveApply.rmap(
v -> Geometry.contravariant1(v, local_geometry),
v,
)
for ii in 1:Nq
out[ii, j] = out[ii, j] ⊞ (D[i, ii] ⊠ WJv¹)
end
WJv² =
local_geometry.WJ ⊠ RecursiveApply.rmap(
v -> Geometry.contravariant2(v, local_geometry),
v,
)
for jj in 1:Nq
out[i, jj] = out[i, jj] ⊞ (D[j, jj] ⊠ WJv²)
end
end
@inbounds for j in 1:Nq, i in 1:Nq
ij = CartesianIndex((i, j))
local_geometry = get_local_geometry(space, ij, slabidx)
out[i, j] = RecursiveApply.rdiv(out[i, j], ⊟(local_geometry.WJ))
end
return Field(SArray(out), space)
end
Base.@propagate_inbounds function operator_shmem(
space,
::Val{Nvt},
op::WeakDivergence{(1, 2)},
arg,
) where {Nvt}
FT = Spaces.undertype(space)
QS = Spaces.quadrature_style(space)
Nq = Quadratures.degrees_of_freedom(QS)
# allocate temp output
RT = operator_return_eltype(op, eltype(arg))
Nf = DataLayouts.typesize(FT, RT)
WJv¹ = CUDA.CuStaticSharedArray(RT, (Nq, Nq, Nvt))
WJv² = CUDA.CuStaticSharedArray(RT, (Nq, Nq, Nvt))
return (WJv¹, WJv²)
end
Base.@propagate_inbounds function operator_fill_shmem!(
op::WeakDivergence{(1, 2)},
(WJv¹, WJv²),
space,
ij,
slabidx,
arg,
)
vt = threadIdx().z
local_geometry = get_local_geometry(space, ij, slabidx)
i, j = ij.I
WJv¹[i, j, vt] =
local_geometry.WJ ⊠ RecursiveApply.rmap(
v -> Geometry.contravariant1(v, local_geometry),
arg,
)
WJv²[i, j, vt] =
local_geometry.WJ ⊠ RecursiveApply.rmap(
v -> Geometry.contravariant2(v, local_geometry),