This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
DGModel_kernels.jl
3232 lines (2841 loc) · 110 KB
/
DGModel_kernels.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
using .NumericalFluxes:
numerical_flux_gradient!,
numerical_flux_first_order!,
numerical_flux_second_order!,
numerical_flux_divergence!,
numerical_flux_higher_order!,
numerical_boundary_flux_gradient!,
numerical_boundary_flux_first_order!,
numerical_boundary_flux_second_order!,
numerical_boundary_flux_divergence!,
numerical_boundary_flux_higher_order!,
CentralNumericalFluxGradient
using ..Mesh.Geometry
# {{{ FIXME: remove this after we've figure out how to pass through to kernel
const _ξ1x1, _ξ2x1, _ξ3x1 = Grids._ξ1x1, Grids._ξ2x1, Grids._ξ3x1
const _ξ1x2, _ξ2x2, _ξ3x2 = Grids._ξ1x2, Grids._ξ2x2, Grids._ξ3x2
const _ξ1x3, _ξ2x3, _ξ3x3 = Grids._ξ1x3, Grids._ξ2x3, Grids._ξ3x3
const _M, _MI = Grids._M, Grids._MI
const _x1, _x2, _x3 = Grids._x1, Grids._x2, Grids._x3
const _JcV = Grids._JcV
const _n1, _n2, _n3 = Grids._n1, Grids._n2, Grids._n3
const _sM, _vMI = Grids._sM, Grids._vMI
# }}}
"""
function volume_tendency!(
balance_law::BalanceLaw,
::Val{dim},
::Val{polyorder},
model_direction,
direction,
tendency,
state_prognostic,
state_gradient_flux,
Qhypervisc_grad,
state_auxiliary,
vgeo,
t,
ω,
D,
elems,
α,
β,
add_source = false,
)
Compute kernel for evaluating the volume tendencies for the
DG form:
∫ₑ ψ⋅ ∂q/∂t dx - ∫ₑ ∇ψ⋅(Fⁱⁿᵛ + Fᵛⁱˢᶜ) dx + ∮ₑ n̂ ψ⋅(Fⁱⁿᵛ* + Fᵛⁱˢᶜ*) dS,
or equivalently in matrix form:
dQ/dt = M⁻¹(MS + DᵀM(Fⁱⁿᵛ + Fᵛⁱˢᶜ) + ∑ᶠ LᵀMf(Fⁱⁿᵛ* + Fᵛⁱˢᶜ*)).
This kernel computes the volume terms: M⁻¹(MS + DᵀM(Fⁱⁿᵛ + Fᵛⁱˢᶜ)),
where M is the mass matrix and D is the differentiation matrix,
S is the source, and Fⁱⁿᵛ, Fᵛⁱˢᶜ are the inviscid and viscous
fluxes, respectively.
"""
@kernel function volume_tendency!(
balance_law::BalanceLaw,
::Val{info},
model_direction,
direction,
tendency,
state_prognostic,
state_gradient_flux,
Qhypervisc_grad,
state_auxiliary,
vgeo,
t,
ω,
D,
elems,
α,
β,
add_source = false,
) where {info}
@uniform begin
dim = info.dim
FT = eltype(state_prognostic)
num_state_prognostic = number_states(balance_law, Prognostic())
num_state_gradient_flux = number_states(balance_law, GradientFlux())
num_state_auxiliary = number_states(balance_law, Auxiliary())
ngradlapstate = number_states(balance_law, GradientLaplacian())
nhyperviscstate = number_states(balance_law, Hyperdiffusive())
@inbounds Nq1 = info.Nq[1]
@inbounds Nq2 = info.Nq[2]
Nq3 = info.Nqk
local_source = MArray{Tuple{num_state_prognostic}, FT}(undef)
local_state_prognostic = MArray{Tuple{num_state_prognostic}, FT}(undef)
local_state_gradient_flux =
MArray{Tuple{num_state_gradient_flux}, FT}(undef)
local_state_hyperdiffusion = MArray{Tuple{nhyperviscstate}, FT}(undef)
local_state_auxiliary = MArray{Tuple{num_state_auxiliary}, FT}(undef)
local_flux = MArray{Tuple{3, num_state_prognostic}, FT}(undef)
local_flux_3 = MArray{Tuple{num_state_prognostic}, FT}(undef)
end
# Arrays for F, and the differentiation matrix D
shared_flux = @localmem FT (2, Nq1, Nq2, num_state_prognostic)
# Storage for tendency and mass inverse M⁻¹
local_tendency = @private FT (Nq3, num_state_prognostic)
local_MI = @private FT (Nq3,)
# Grab the index associated with the current element `e` and the
# horizontal quadrature indices `i` (in the ξ1-direction),
# `j` (in the ξ2-direction) [directions on the reference element].
# Parallelize over elements, then over columns
e = @index(Group, Linear)
i, j = @index(Local, NTuple)
@inbounds begin
@unroll for k in 1:Nq3
ijk = i + Nq1 * ((j - 1) + Nq2 * (k - 1))
# initialize local tendency
@unroll for s in 1:num_state_prognostic
local_tendency[k, s] = zero(FT)
end
# read in mass matrix inverse for element `e`
local_MI[k] = vgeo[ijk, _MI, e]
end
@unroll for k in 1:Nq3
@synchronize
ijk = i + Nq1 * ((j - 1) + Nq2 * (k - 1))
M = vgeo[ijk, _M, e]
# Extract Jacobian terms ∂ξᵢ/∂xⱼ
ξ1x1 = vgeo[ijk, _ξ1x1, e]
ξ1x2 = vgeo[ijk, _ξ1x2, e]
ξ1x3 = vgeo[ijk, _ξ1x3, e]
if dim == 3 || (dim == 2 && direction isa EveryDirection)
ξ2x1 = vgeo[ijk, _ξ2x1, e]
ξ2x2 = vgeo[ijk, _ξ2x2, e]
ξ2x3 = vgeo[ijk, _ξ2x3, e]
end
if dim == 3 && direction isa EveryDirection
ξ3x1 = vgeo[ijk, _ξ3x1, e]
ξ3x2 = vgeo[ijk, _ξ3x2, e]
ξ3x3 = vgeo[ijk, _ξ3x3, e]
end
# Read fields into registers (hopefully)
@unroll for s in 1:num_state_prognostic
local_state_prognostic[s] = state_prognostic[ijk, s, e]
end
@unroll for s in 1:num_state_auxiliary
local_state_auxiliary[s] = state_auxiliary[ijk, s, e]
end
@unroll for s in 1:num_state_gradient_flux
local_state_gradient_flux[s] = state_gradient_flux[ijk, s, e]
end
@unroll for s in 1:nhyperviscstate
local_state_hyperdiffusion[s] = Qhypervisc_grad[ijk, s, e]
end
# Computes the local inviscid fluxes Fⁱⁿᵛ
fill!(local_flux, -zero(eltype(local_flux)))
flux_first_order_arr!(
balance_law,
local_flux,
local_state_prognostic,
local_state_auxiliary,
t,
(model_direction,),
)
@unroll for s in 1:num_state_prognostic
shared_flux[1, i, j, s] = local_flux[1, s]
shared_flux[2, i, j, s] = local_flux[2, s]
local_flux_3[s] = local_flux[3, s]
end
# Computes the local viscous fluxes Fᵛⁱˢᶜ
fill!(local_flux, -zero(eltype(local_flux)))
flux_second_order_arr!(
balance_law,
local_flux,
local_state_prognostic,
local_state_gradient_flux,
local_state_hyperdiffusion,
local_state_auxiliary,
t,
)
@unroll for s in 1:num_state_prognostic
shared_flux[1, i, j, s] += local_flux[1, s]
shared_flux[2, i, j, s] += local_flux[2, s]
local_flux_3[s] += local_flux[3, s]
end
# Build "inside metrics" flux
@unroll for s in 1:num_state_prognostic
F1, F2, F3 = shared_flux[1, i, j, s],
shared_flux[2, i, j, s],
local_flux_3[s]
shared_flux[1, i, j, s] =
M * (ξ1x1 * F1 + ξ1x2 * F2 + ξ1x3 * F3)
if dim == 3 || (dim == 2 && direction isa EveryDirection)
shared_flux[2, i, j, s] =
M * (ξ2x1 * F1 + ξ2x2 * F2 + ξ2x3 * F3)
end
if dim == 3 && direction isa EveryDirection
local_flux_3[s] = M * (ξ3x1 * F1 + ξ3x2 * F2 + ξ3x3 * F3)
end
end
# In the case of the remainder model we may need to loop through the
# models to add in restricted direction components
if model_direction isa EveryDirection && balance_law isa RemBL
if rembl_has_subs_direction(HorizontalDirection(), balance_law)
fill!(local_flux, -zero(eltype(local_flux)))
flux_first_order_arr!(
balance_law,
local_flux,
local_state_prognostic,
local_state_auxiliary,
t,
(HorizontalDirection(),),
)
# Precomputing J ∇ξⁱ⋅ F
@unroll for s in 1:num_state_prognostic
F1, F2, F3 =
local_flux[1, s], local_flux[2, s], local_flux[3, s]
shared_flux[1, i, j, s] +=
M * (ξ1x1 * F1 + ξ1x2 * F2 + ξ1x3 * F3)
if dim == 3
shared_flux[2, i, j, s] +=
M * (ξ2x1 * F1 + ξ2x2 * F2 + ξ2x3 * F3)
end
end
end
end
if dim == 3 && direction isa EveryDirection
@unroll for n in 1:Nq3
MI = local_MI[n]
@unroll for s in 1:num_state_prognostic
local_tendency[n, s] += MI * D[k, n] * local_flux_3[s]
end
end
end
# Computes the contribution due to the source term S
if add_source
fill!(local_source, -zero(eltype(local_source)))
source_arr!(
balance_law,
local_source,
local_state_prognostic,
local_state_gradient_flux,
local_state_auxiliary,
t,
(model_direction,),
)
@unroll for s in 1:num_state_prognostic
local_tendency[k, s] += local_source[s]
end
end
@synchronize
# Weak "inside metrics" derivative.
# Computes the rest of the volume term: M⁻¹DᵀF
MI = local_MI[k]
@unroll for s in 1:num_state_prognostic
@unroll for n in 1:Nq1
# ξ1-grid lines
local_tendency[k, s] +=
MI * D[n, i] * shared_flux[1, n, j, s]
# ξ2-grid lines
if dim == 3 || (dim == 2 && direction isa EveryDirection)
local_tendency[k, s] +=
MI * D[n, j] * shared_flux[2, i, n, s]
end
end
end
end
@unroll for k in 1:Nq3
ijk = i + Nq1 * ((j - 1) + Nq2 * (k - 1))
@unroll for s in 1:num_state_prognostic
if β != 0
T = α * local_tendency[k, s] + β * tendency[ijk, s, e]
else
T = α * local_tendency[k, s]
end
tendency[ijk, s, e] = T
end
end
end
end
@kernel function volume_tendency!(
balance_law::BalanceLaw,
::Val{info},
model_direction,
::VerticalDirection,
tendency,
state_prognostic,
state_gradient_flux,
Qhypervisc_grad,
state_auxiliary,
vgeo,
t,
ω,
D,
elems,
α,
β,
add_source = false,
) where {info}
@uniform begin
dim = info.dim
FT = eltype(state_prognostic)
num_state_prognostic = number_states(balance_law, Prognostic())
num_state_gradient_flux = number_states(balance_law, GradientFlux())
num_state_auxiliary = number_states(balance_law, Auxiliary())
ngradlapstate = number_states(balance_law, GradientLaplacian())
nhyperviscstate = number_states(balance_law, Hyperdiffusive())
@inbounds Nq1 = info.Nq[1]
@inbounds Nq2 = info.Nq[2]
Nq3 = info.Nqk
local_source = MArray{Tuple{num_state_prognostic}, FT}(undef)
local_state_prognostic = MArray{Tuple{num_state_prognostic}, FT}(undef)
local_state_gradient_flux =
MArray{Tuple{num_state_gradient_flux}, FT}(undef)
local_state_hyperdiffusion = MArray{Tuple{nhyperviscstate}, FT}(undef)
local_state_auxiliary = MArray{Tuple{num_state_auxiliary}, FT}(undef)
local_flux = MArray{Tuple{3, num_state_prognostic}, FT}(undef)
local_flux_total = MArray{Tuple{3, num_state_prognostic}, FT}(undef)
_ζx1 = dim == 2 ? _ξ2x1 : _ξ3x1
_ζx2 = dim == 2 ? _ξ2x2 : _ξ3x2
_ζx3 = dim == 2 ? _ξ2x3 : _ξ3x3
@inbounds Nqv = dim == 2 ? Nq2 : info.Nq[dim]
shared_flux_size =
dim == 2 ? (Nq1, Nqv, num_state_prognostic) : (0, 0, 0)
end
# Arrays for F, and the differentiation matrix D
shared_flux = @localmem FT shared_flux_size
# Storage for tendency and mass inverse M⁻¹
local_tendency = @private FT (Nq3, num_state_prognostic)
local_MI = @private FT (Nq3,)
# Grab the index associated with the current element `e` and the
# horizontal quadrature indices `i` (in the ξ1-direction),
# `j` (in the ξ2-direction) [directions on the reference element].
# Parallelize over elements, then over columns
e = @index(Group, Linear)
i, j = @index(Local, NTuple)
@inbounds begin
@unroll for k in 1:Nq3
ijk = i + Nq1 * ((j - 1) + Nq2 * (k - 1))
# initialize local tendency
@unroll for s in 1:num_state_prognostic
local_tendency[k, s] = zero(FT)
end
# read in mass matrix inverse for element `e`
local_MI[k] = vgeo[ijk, _MI, e]
end
# ensure D is loaded
@synchronize(dim == 3)
@unroll for k in 1:Nq3
ijk = i + Nq1 * ((j - 1) + Nq2 * (k - 1))
M = vgeo[ijk, _M, e]
# Extract vertical Jacobian terms ∂ζ/∂xⱼ
ζx1 = vgeo[ijk, _ζx1, e]
ζx2 = vgeo[ijk, _ζx2, e]
ζx3 = vgeo[ijk, _ζx3, e]
# Read fields into registers (hopefully)
@unroll for s in 1:num_state_prognostic
local_state_prognostic[s] = state_prognostic[ijk, s, e]
end
@unroll for s in 1:num_state_auxiliary
local_state_auxiliary[s] = state_auxiliary[ijk, s, e]
end
@unroll for s in 1:num_state_gradient_flux
local_state_gradient_flux[s] = state_gradient_flux[ijk, s, e]
end
@unroll for s in 1:nhyperviscstate
local_state_hyperdiffusion[s] = Qhypervisc_grad[ijk, s, e]
end
# Computes the local inviscid fluxes Fⁱⁿᵛ
fill!(local_flux, -zero(eltype(local_flux)))
flux_first_order_arr!(
balance_law,
local_flux,
local_state_prognostic,
local_state_auxiliary,
t,
(model_direction,),
)
@unroll for s in 1:num_state_prognostic
local_flux_total[1, s] = local_flux[1, s]
local_flux_total[2, s] = local_flux[2, s]
local_flux_total[3, s] = local_flux[3, s]
end
# Computes the local viscous fluxes Fᵛⁱˢᶜ
fill!(local_flux, -zero(eltype(local_flux)))
flux_second_order_arr!(
balance_law,
local_flux,
local_state_prognostic,
local_state_gradient_flux,
local_state_hyperdiffusion,
local_state_auxiliary,
t,
)
@unroll for s in 1:num_state_prognostic
local_flux_total[1, s] += local_flux[1, s]
local_flux_total[2, s] += local_flux[2, s]
local_flux_total[3, s] += local_flux[3, s]
end
# Build "inside metrics" flux
@unroll for s in 1:num_state_prognostic
F1, F2, F3 = local_flux_total[1, s],
local_flux_total[2, s],
local_flux_total[3, s]
Fv = M * (ζx1 * F1 + ζx2 * F2 + ζx3 * F3)
if dim == 2
shared_flux[i, j, s] = Fv
else
local_flux_total[1, s] = Fv
end
end
# In the case of the remainder model we may need to loop through the
# models to add in restricted direction components
if model_direction isa EveryDirection && balance_law isa RemBL
if rembl_has_subs_direction(VerticalDirection(), balance_law)
fill!(local_flux, -zero(eltype(local_flux)))
flux_first_order_arr!(
balance_law,
local_flux,
local_state_prognostic,
local_state_auxiliary,
t,
(VerticalDirection(),),
)
# Precomputing J ∇ζ⋅ F
@unroll for s in 1:num_state_prognostic
F1, F2, F3 =
local_flux[1, s], local_flux[2, s], local_flux[3, s]
Fv = M * (ζx1 * F1 + ζx2 * F2 + ζx3 * F3)
if dim == 2
shared_flux[i, j, s] += Fv
else
local_flux_total[1, s] += Fv
end
end
end
end
if dim == 3
@unroll for n in 1:Nq3
MI = local_MI[n]
@unroll for s in 1:num_state_prognostic
local_tendency[n, s] +=
MI * D[k, n] * local_flux_total[1, s]
end
end
end
# Computes the contribution due to the source term S
if add_source
fill!(local_source, -zero(eltype(local_source)))
source_arr!(
balance_law,
local_source,
local_state_prognostic,
local_state_gradient_flux,
local_state_auxiliary,
t,
(model_direction,),
)
@unroll for s in 1:num_state_prognostic
local_tendency[k, s] += local_source[s]
end
end
@synchronize(dim == 2)
# Weak "inside metrics" derivative.
# Computes the rest of the volume term: M⁻¹DᵀMF
if dim == 2
MI = local_MI[k]
@unroll for n in 1:Nqv
@unroll for s in 1:num_state_prognostic
local_tendency[k, s] +=
MI * D[n, j] * shared_flux[i, n, s]
end
end
end
end
@unroll for k in 1:Nq3
ijk = i + Nq1 * ((j - 1) + Nq2 * (k - 1))
@unroll for s in 1:num_state_prognostic
if β != 0
T = α * local_tendency[k, s] + β * tendency[ijk, s, e]
else
T = α * local_tendency[k, s]
end
tendency[ijk, s, e] = T
end
end
end
end
@doc """
function dgsem_interface_tendency!(
balance_law::BalanceLaw,
::Val{dim},
::Val{polyorder},
direction,
numerical_flux_first_order,
numerical_flux_second_order,
tendency,
state_prognostic,
state_gradient_flux,
Qhypervisc_grad,
state_auxiliary,
vgeo,
sgeo,
t,
vmap⁻,
vmap⁺,
elemtobndy,
elems,
α,
)
Compute kernel for evaluating the interface tendencies for the
DG form:
∫ₑ ψ⋅ ∂q/∂t dx - ∫ₑ ∇ψ⋅(Fⁱⁿᵛ + Fᵛⁱˢᶜ) dx + ∮ₑ n̂ ψ⋅(Fⁱⁿᵛ⋆ + Fᵛⁱˢᶜ⋆) dS,
or equivalently in matrix form:
dQ/dt = M⁻¹(MS + DᵀM(Fⁱⁿᵛ + Fᵛⁱˢᶜ) + ∑ᶠ LᵀMf(Fⁱⁿᵛ⋆ + Fᵛⁱˢᶜ⋆)).
This kernel computes the surface terms: M⁻¹ ∑ᶠ LᵀMf(Fⁱⁿᵛ⋆ + Fᵛⁱˢᶜ⋆)),
where M is the mass matrix, Mf is the face mass matrix, L is an interpolator
from volume to face, and Fⁱⁿᵛ⋆, Fᵛⁱˢᶜ⋆
are the numerical fluxes for the inviscid and viscous
fluxes, respectively.
""" dgsem_interface_tendency!
@kernel function dgsem_interface_tendency!(
balance_law::BalanceLaw,
::Val{info},
direction,
numerical_flux_first_order,
numerical_flux_second_order,
tendency,
state_prognostic,
state_gradient_flux,
Qhypervisc_grad,
state_auxiliary,
vgeo,
sgeo,
t,
vmap⁻,
vmap⁺,
elemtobndy,
elems,
α,
) where {info}
@uniform begin
dim = info.dim
FT = eltype(state_prognostic)
num_state_prognostic = number_states(balance_law, Prognostic())
num_state_gradient_flux = number_states(balance_law, GradientFlux())
nhyperviscstate = number_states(balance_law, Hyperdiffusive())
num_state_auxiliary = number_states(balance_law, Auxiliary())
ngradlapstate = number_states(balance_law, GradientLaplacian())
nface = info.nface
Np = info.Np
Nqk = info.Nqk
faces = 1:nface
if direction isa VerticalDirection
faces = (nface - 1):nface
elseif direction isa HorizontalDirection
faces = 1:(nface - 2)
end
local_state_prognostic⁻ = MArray{Tuple{num_state_prognostic}, FT}(undef)
local_state_gradient_flux⁻ =
MArray{Tuple{num_state_gradient_flux}, FT}(undef)
local_state_hyperdiffusion⁻ = MArray{Tuple{nhyperviscstate}, FT}(undef)
local_state_auxiliary⁻ = MArray{Tuple{num_state_auxiliary}, FT}(undef)
# Need two copies since numerical_flux_first_order! can modify state_prognostic⁺
local_state_prognostic⁺nondiff =
MArray{Tuple{num_state_prognostic}, FT}(undef)
local_state_prognostic⁺diff =
MArray{Tuple{num_state_prognostic}, FT}(undef)
# Need two copies since numerical_flux_first_order! can modify state_auxiliary⁺
local_state_auxiliary⁺nondiff =
MArray{Tuple{num_state_auxiliary}, FT}(undef)
local_state_auxiliary⁺diff =
MArray{Tuple{num_state_auxiliary}, FT}(undef)
local_state_gradient_flux⁺ =
MArray{Tuple{num_state_gradient_flux}, FT}(undef)
local_state_hyperdiffusion⁺ = MArray{Tuple{nhyperviscstate}, FT}(undef)
local_state_prognostic_bottom1 =
MArray{Tuple{num_state_prognostic}, FT}(undef)
local_state_gradient_flux_bottom1 =
MArray{Tuple{num_state_gradient_flux}, FT}(undef)
local_state_auxiliary_bottom1 =
MArray{Tuple{num_state_auxiliary}, FT}(undef)
local_flux = MArray{Tuple{num_state_prognostic}, FT}(undef)
end
eI = @index(Group, Linear)
n = @index(Local, Linear)
e = @private Int (1,)
@inbounds e[1] = elems[eI]
@inbounds for f in faces
# The remainder model needs to know which direction of face the model is
# being evaluated for. So faces 1:(nface - 2) are flagged as
# `HorizontalDirection()` faces and the remaining two faces are
# `VerticalDirection()` faces
face_direction =
f in 1:(nface - 2) ? (EveryDirection(), HorizontalDirection()) :
(EveryDirection(), VerticalDirection())
e⁻ = e[1]
normal_vector = SVector(
sgeo[_n1, n, f, e⁻],
sgeo[_n2, n, f, e⁻],
sgeo[_n3, n, f, e⁻],
)
bctag = elemtobndy[f, e⁻]
# Get surface mass, volume mass inverse
sM, vMI = sgeo[_sM, n, f, e⁻], sgeo[_vMI, n, f, e⁻]
id⁻, id⁺ = vmap⁻[n, f, e⁻], vmap⁺[n, f, e⁻]
e⁺ = ((id⁺ - 1) ÷ Np) + 1
vid⁻, vid⁺ = ((id⁻ - 1) % Np) + 1, ((id⁺ - 1) % Np) + 1
if bctag != 0
# TODO: we will use vmap⁺ to store the boundary element info
#be = e⁺
#bcid = vid⁺
e⁺ = e⁻
vid⁺ = vid⁻
end
# Load minus side data
@unroll for s in 1:num_state_prognostic
local_state_prognostic⁻[s] = state_prognostic[vid⁻, s, e⁻]
end
@unroll for s in 1:num_state_gradient_flux
local_state_gradient_flux⁻[s] = state_gradient_flux[vid⁻, s, e⁻]
end
@unroll for s in 1:nhyperviscstate
local_state_hyperdiffusion⁻[s] = Qhypervisc_grad[vid⁻, s, e⁻]
end
@unroll for s in 1:num_state_auxiliary
local_state_auxiliary⁻[s] = state_auxiliary[vid⁻, s, e⁻]
end
# Load plus side data
@unroll for s in 1:num_state_prognostic
local_state_prognostic⁺diff[s] =
local_state_prognostic⁺nondiff[s] =
state_prognostic[vid⁺, s, e⁺]
end
@unroll for s in 1:num_state_gradient_flux
local_state_gradient_flux⁺[s] = state_gradient_flux[vid⁺, s, e⁺]
end
@unroll for s in 1:nhyperviscstate
local_state_hyperdiffusion⁺[s] = Qhypervisc_grad[vid⁺, s, e⁺]
end
@unroll for s in 1:num_state_auxiliary
local_state_auxiliary⁺diff[s] =
local_state_auxiliary⁺nondiff[s] = state_auxiliary[vid⁺, s, e⁺]
end
# Oh dang, it's boundary conditions
fill!(local_flux, -zero(eltype(local_flux)))
if bctag == 0
numerical_flux_first_order!(
numerical_flux_first_order,
balance_law,
Vars{vars_state(balance_law, Prognostic(), FT)}(local_flux),
SVector(normal_vector),
Vars{vars_state(balance_law, Prognostic(), FT)}(
local_state_prognostic⁻,
),
Vars{vars_state(balance_law, Auxiliary(), FT)}(
local_state_auxiliary⁻,
),
Vars{vars_state(balance_law, Prognostic(), FT)}(
local_state_prognostic⁺nondiff,
),
Vars{vars_state(balance_law, Auxiliary(), FT)}(
local_state_auxiliary⁺nondiff,
),
t,
face_direction,
)
numerical_flux_second_order!(
numerical_flux_second_order,
balance_law,
Vars{vars_state(balance_law, Prognostic(), FT)}(local_flux),
normal_vector,
Vars{vars_state(balance_law, Prognostic(), FT)}(
local_state_prognostic⁻,
),
Vars{vars_state(balance_law, GradientFlux(), FT)}(
local_state_gradient_flux⁻,
),
Vars{vars_state(balance_law, Hyperdiffusive(), FT)}(
local_state_hyperdiffusion⁻,
),
Vars{vars_state(balance_law, Auxiliary(), FT)}(
local_state_auxiliary⁻,
),
Vars{vars_state(balance_law, Prognostic(), FT)}(
local_state_prognostic⁺diff,
),
Vars{vars_state(balance_law, GradientFlux(), FT)}(
local_state_gradient_flux⁺,
),
Vars{vars_state(balance_law, Hyperdiffusive(), FT)}(
local_state_hyperdiffusion⁺,
),
Vars{vars_state(balance_law, Auxiliary(), FT)}(
local_state_auxiliary⁺diff,
),
t,
)
else
if (dim == 2 && f == 3) || (dim == 3 && f == 5)
if info.N[end] == 0
# Loop up to next element for all horizontal elements
@unroll for s in 1:num_state_prognostic
local_state_prognostic_bottom1[s] =
state_prognostic[n, s, e⁻ + 1]
end
@unroll for s in 1:num_state_gradient_flux
local_state_gradient_flux_bottom1[s] =
state_gradient_flux[n, s, e⁻ + 1]
end
@unroll for s in 1:num_state_auxiliary
local_state_auxiliary_bottom1[s] =
state_auxiliary[n, s, e⁻ + 1]
end
else
# Loop up the first element along all horizontal elements
@unroll for s in 1:num_state_prognostic
local_state_prognostic_bottom1[s] =
state_prognostic[n + Nqk^2, s, e⁻]
end
@unroll for s in 1:num_state_gradient_flux
local_state_gradient_flux_bottom1[s] =
state_gradient_flux[n + Nqk^2, s, e⁻]
end
@unroll for s in 1:num_state_auxiliary
local_state_auxiliary_bottom1[s] =
state_auxiliary[n + Nqk^2, s, e⁻]
end
end
end
bcs = boundary_conditions(balance_law)
# TODO: there is probably a better way to unroll this loop
Base.Cartesian.@nif 7 d -> bctag == d <= length(bcs) d -> begin
bc = bcs[d]
numerical_boundary_flux_first_order!(
numerical_flux_first_order,
bc,
balance_law,
Vars{vars_state(balance_law, Prognostic(), FT)}(local_flux),
SVector(normal_vector),
Vars{vars_state(balance_law, Prognostic(), FT)}(
local_state_prognostic⁻,
),
Vars{vars_state(balance_law, Auxiliary(), FT)}(
local_state_auxiliary⁻,
),
Vars{vars_state(balance_law, Prognostic(), FT)}(
local_state_prognostic⁺nondiff,
),
Vars{vars_state(balance_law, Auxiliary(), FT)}(
local_state_auxiliary⁺nondiff,
),
t,
face_direction,
Vars{vars_state(balance_law, Prognostic(), FT)}(
local_state_prognostic_bottom1,
),
Vars{vars_state(balance_law, Auxiliary(), FT)}(
local_state_auxiliary_bottom1,
),
)
numerical_boundary_flux_second_order!(
numerical_flux_second_order,
bc,
balance_law,
Vars{vars_state(balance_law, Prognostic(), FT)}(local_flux),
normal_vector,
Vars{vars_state(balance_law, Prognostic(), FT)}(
local_state_prognostic⁻,
),
Vars{vars_state(balance_law, GradientFlux(), FT)}(
local_state_gradient_flux⁻,
),
Vars{vars_state(balance_law, Hyperdiffusive(), FT)}(
local_state_hyperdiffusion⁻,
),
Vars{vars_state(balance_law, Auxiliary(), FT)}(
local_state_auxiliary⁻,
),
Vars{vars_state(balance_law, Prognostic(), FT)}(
local_state_prognostic⁺diff,
),
Vars{vars_state(balance_law, GradientFlux(), FT)}(
local_state_gradient_flux⁺,
),
Vars{vars_state(balance_law, Hyperdiffusive(), FT)}(
local_state_hyperdiffusion⁺,
),
Vars{vars_state(balance_law, Auxiliary(), FT)}(
local_state_auxiliary⁺diff,
),
t,
Vars{vars_state(balance_law, Prognostic(), FT)}(
local_state_prognostic_bottom1,
),
Vars{vars_state(balance_law, GradientFlux(), FT)}(
local_state_gradient_flux_bottom1,
),
Vars{vars_state(balance_law, Auxiliary(), FT)}(
local_state_auxiliary_bottom1,
),
)
end d -> throw(BoundsError(bcs, bctag))
end
# Update RHS (in outer face loop): M⁻¹ Mfᵀ(Fⁱⁿᵛ⋆ + Fᵛⁱˢᶜ⋆))
@unroll for s in 1:num_state_prognostic
# FIXME: Should we pretch these?
tendency[vid⁻, s, e⁻] -= α * vMI * sM * local_flux[s]
end
# Need to wait after even faces to avoid race conditions
@synchronize(f % 2 == 0)
end
end
"""
function volume_gradients!(
balance_law::BalanceLaw,
::Val{dim},
::Val{polyorder},
direction,
state_prognostic,
state_gradient_flux,
Qhypervisc_grad,
state_auxiliary,
vgeo,
t,
D,
::Val{hypervisc_indexmap},
elems,
increment = false,
)
Computes the volume integral for the auxiliary equation
(in DG strong form):
∫ₑ ψI⋅Σ dx = ∫ₑ ψI⋅∇G dx + ∮ₑ nψI⋅(G* - G) dS,
or equivalently in matrix notation:
Σ = M⁻¹ LᵀMf(G* - G) + D G
This kernel computes the volume gradient: D * G, where
D is the differentiation matrix and G is the auxiliary
gradient flux.
"""
@kernel function volume_gradients!(
balance_law::BalanceLaw,
::Val{info},
direction,
state_prognostic,
state_gradient_flux,
Qhypervisc_grad,
state_auxiliary,
vgeo,
t,
D,
::Val{hypervisc_indexmap},
elems,
increment = false,
) where {info, hypervisc_indexmap}
@uniform begin
dim = info.dim
FT = eltype(state_prognostic)
num_state_prognostic = number_states(balance_law, Prognostic())
ngradstate = number_states(balance_law, Gradient())
ngradlapstate = number_states(balance_law, GradientLaplacian())
num_state_gradient_flux = number_states(balance_law, GradientFlux())
num_state_auxiliary = number_states(balance_law, Auxiliary())
# Kernel assumes same polynomial order in both
# horizontal directions (x, y)
@inbounds Nq1 = info.Nq[1]
@inbounds Nq2 = info.Nq[2]
Nq3 = info.Nqk
ngradtransformstate = num_state_prognostic
local_transform = MArray{Tuple{ngradstate}, FT}(undef)
local_state_gradient_flux =
MArray{Tuple{num_state_gradient_flux}, FT}(undef)
end
# Transformation from conservative variables to
# primitive variables (i.e. ρu → u)
shared_transform = @localmem FT (Nq1, Nq2, ngradstate)
local_state_prognostic = @private FT (ngradtransformstate, Nq3)
local_state_auxiliary = @private FT (num_state_auxiliary, Nq3)
local_transform_gradient = @private FT (3, ngradstate, Nq3)
Gξ3 = @private FT (ngradstate, Nq3)
# Grab the index associated with the current element `e` and the
# horizontal quadrature indices `i` (in the ξ1-direction),
# `j` (in the ξ2-direction) [directions on the reference element].
# Parallelize over elements, then over columns
e = @index(Group, Linear)
i, j = @index(Local, NTuple)
@inbounds @views begin
@unroll for k in 1:Nq3
# Initialize local gradient variables
@unroll for s in 1:ngradstate
local_transform_gradient[1, s, k] = -zero(FT)
local_transform_gradient[2, s, k] = -zero(FT)
local_transform_gradient[3, s, k] = -zero(FT)
Gξ3[s, k] = -zero(FT)
end
# Load prognostic and auxiliary variables
ijk = i + Nq1 * ((j - 1) + Nq2 * (k - 1))
@unroll for s in 1:ngradtransformstate
local_state_prognostic[s, k] = state_prognostic[ijk, s, e]