This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
/
Interpolation.jl
1856 lines (1615 loc) · 58.6 KB
/
Interpolation.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
module Interpolation
using CUDA
using DocStringExtensions
using LinearAlgebra
using MPI
using OrderedCollections
using StaticArrays
import GaussQuadrature
import KernelAbstractions: CPU, CUDADevice
using ClimateMachine
using ClimateMachine.Mesh.Topologies
using ClimateMachine.Mesh.Grids
using ClimateMachine.Mesh.Geometry
using ClimateMachine.Mesh.Elements
import ClimateMachine.MPIStateArrays: array_device
export dimensions,
accumulate_interpolated_data,
accumulate_interpolated_data!,
InterpolationBrick,
InterpolationCubedSphere,
interpolate_local!,
project_cubed_sphere!,
InterpolationTopology
abstract type InterpolationTopology end
"""
InterpolationBrick{
FT <: AbstractFloat,CuArrays
UI8AD <: AbstractArray{UInt8, 2},
UI16VD <: AbstractVector{UInt16},
I32V <: AbstractVector{Int32},
} <: InterpolationTopology
This interpolation data structure and the corresponding functions works for a
brick, where stretching/compression happens only along the x1, x2 & x3 axis.
Here x1 = X1(ξ1), x2 = X2(ξ2) and x3 = X3(ξ3).
# Fields
$(DocStringExtensions.FIELDS)
# Usage
InterpolationBrick(
grid::DiscontinuousSpectralElementGrid{FT},
xbnd::Array{FT,2},
xres,
) where FT <: AbstractFloat
This interpolation structure and the corresponding functions works for a brick,
where stretching/compression happens only along the x1, x2 & x3 axis. Here x1
= X1(ξ1), x2 = X2(ξ2) and x3 = X3(ξ3).
# Arguments for the inner constructor
- `grid`: DiscontinousSpectralElementGrid
- `xbnd`: Domain boundaries in x1, x2 and x3 directions
- `xres`: Resolution of the interpolation grid in x1, x2 and x3 directions
"""
struct InterpolationBrick{
FT <: AbstractFloat,
T <: Int,
FTV <: AbstractVector{FT},
FTVD <: AbstractVector{FT},
TVD <: AbstractVector{T},
FTA2 <: Array{FT, 2},
UI8AD <: AbstractArray{UInt8, 2},
UI16VD <: AbstractVector{UInt16},
I32V <: AbstractVector{Int32},
} <: InterpolationTopology
"Number of elements"
Nel::T
"Total number of interpolation points"
Np::T
"Total number of interpolation points on local process"
Npl::T
"Polynomial order of spectral element approximation"
poly_order::T
"Domain bounds in x1, x2 and x3 directions"
xbnd::FTA2
"Interpolation grid in x1 direction"
x1g::FTV
"Interpolation grid in x2 direction"
x2g::FTV
"Interpolation grid in x3 direction"
x3g::FTV
"Unique ξ1 coordinates of interpolation points within each spectral element"
ξ1::FTVD
"Unique ξ2 coordinates of interpolation points within each spectral element"
ξ2::FTVD
"Unique ξ3 coordinates of interpolation points within each spectral element"
ξ3::FTVD
"Flags when ξ1/ξ2/ξ3 interpolation point matches with a GLL point"
flg::UI8AD
"Normalization factor"
fac::FTVD
"x1 interpolation grid index of interpolation points within each element on the local process"
x1i::UI16VD
"x2 interpolation grid index of interpolation points within each element on the local process"
x2i::UI16VD
"x3 interpolation grid index of interpolation points within each element on the local process"
x3i::UI16VD
"Offsets for each element"
offset::TVD # offsets for each element for v
"GLL points"
m1_r::FTVD
"GLL weights"
m1_w::FTVD
"Barycentric weights"
wb::FTVD
# MPI setup for gathering interpolated variable on proc # 0
"Number of interpolation points on each of the processes"
Np_all::I32V
"x1 interpolation grid index of interpolation points within each element on all processes stored only on proc 0"
x1i_all::UI16VD
"x2 interpolation grid index of interpolation points within each element on all processes stored only on proc 0"
x2i_all::UI16VD
"x3 interpolation grid index of interpolation points within each element on all processes stored only on proc 0"
x3i_all::UI16VD
function InterpolationBrick(
grid::DiscontinuousSpectralElementGrid{FT},
xbnd::Array{FT, 2},
x1g::AbstractArray{FT, 1},
x2g::AbstractArray{FT, 1},
x3g::AbstractArray{FT, 1},
) where {FT <: AbstractFloat}
mpicomm = grid.topology.mpicomm
pid = MPI.Comm_rank(mpicomm)
npr = MPI.Comm_size(mpicomm)
DA = arraytype(grid) # device array
device = arraytype(grid) <: Array ? CPU() : CUDADevice()
poly_order = polynomialorder(grid)
qm1 = poly_order + 1
ndim = 3
toler = 4 * eps(FT) # tolerance
n1g = length(x1g)
n2g = length(x2g)
n3g = length(x3g)
Np = n1g * n2g * n3g
marker = BitArray{3}(undef, n1g, n2g, n3g)
fill!(marker, true)
Nel = length(grid.topology.realelems) # # of elements on local process
offset = Vector{Int}(undef, Nel + 1) # offsets for the interpolated variable
n123 = zeros(Int, ndim) # # of unique ξ1, ξ2, ξ3 points in each cell
xsten = zeros(Int, 2, ndim) # x1, x2, x3 start and end for each brick element
xbndl = zeros(FT, 2, ndim) # x1,x2,x3 limits (min,max) for each brick element
ξ1 = map(i -> zeros(FT, i), zeros(Int, Nel))
ξ2 = map(i -> zeros(FT, i), zeros(Int, Nel))
ξ3 = map(i -> zeros(FT, i), zeros(Int, Nel))
x1i = map(i -> zeros(UInt16, i), zeros(UInt16, Nel))
x2i = map(i -> zeros(UInt16, i), zeros(UInt16, Nel))
x3i = map(i -> zeros(UInt16, i), zeros(UInt16, Nel))
x = map(i -> zeros(FT, ndim, i), zeros(Int, Nel)) # interpolation grid points embedded in each cell
offset[1] = 0
for el in 1:Nel
for (xg, dim) in zip((x1g, x2g, x3g), 1:ndim)
xbndl[1, dim], xbndl[2, dim] =
extrema(grid.topology.elemtocoord[dim, :, el])
st = findfirst(xg .≥ xbndl[1, dim] .- toler)
if st ≠ nothing
if xg[st] > (xbndl[2, dim] + toler)
st = nothing
end
end
if st ≠ nothing
xsten[1, dim] = st
xsten[2, dim] =
findlast(temp -> temp .≤ xbndl[2, dim] .+ toler, xg)
n123[dim] = xsten[2, dim] - xsten[1, dim] + 1
else
n123[dim] = 0
end
end
if prod(n123) > 0
for k in xsten[1, 3]:xsten[2, 3],
j in xsten[1, 2]:xsten[2, 2],
i in xsten[1, 1]:xsten[2, 1]
if marker[i, j, k]
push!(
ξ1[el],
2 * (x1g[i] - xbndl[1, 1]) /
(xbndl[2, 1] - xbndl[1, 1]) - 1,
)
push!(
ξ2[el],
2 * (x2g[j] - xbndl[1, 2]) /
(xbndl[2, 2] - xbndl[1, 2]) - 1,
)
push!(
ξ3[el],
2 * (x3g[k] - xbndl[1, 3]) /
(xbndl[2, 3] - xbndl[1, 3]) - 1,
)
push!(x1i[el], UInt16(i))
push!(x2i[el], UInt16(j))
push!(x3i[el], UInt16(k))
marker[i, j, k] = false
end
end
offset[el + 1] = offset[el] + length(ξ1[el])
end
end # el loop
m1_r, m1_w = GaussQuadrature.legendre(FT, qm1, GaussQuadrature.both)
wb = Elements.baryweights(m1_r)
Npl = offset[end]
ξ1_d = Array{FT}(undef, Npl)
ξ2_d = Array{FT}(undef, Npl)
ξ3_d = Array{FT}(undef, Npl)
x1i_d = Array{UInt16}(undef, Npl)
x2i_d = Array{UInt16}(undef, Npl)
x3i_d = Array{UInt16}(undef, Npl)
fac_d = zeros(FT, Npl)
flg_d = zeros(UInt8, 3, Npl)
for i in 1:Nel
ctr = 1
for j in (offset[i] + 1):offset[i + 1]
ξ1_d[j] = ξ1[i][ctr]
ξ2_d[j] = ξ2[i][ctr]
ξ3_d[j] = ξ3[i][ctr]
x1i_d[j] = x1i[i][ctr]
x2i_d[j] = x2i[i][ctr]
x3i_d[j] = x3i[i][ctr]
# set up interpolation
fac1 = FT(0)
fac2 = FT(0)
fac3 = FT(0)
for ib in 1:qm1
if abs(m1_r[ib] - ξ1_d[j]) < toler
@inbounds flg_d[1, j] = UInt8(ib)
else
@inbounds fac1 += wb[ib] / (ξ1_d[j] - m1_r[ib])
end
if abs(m1_r[ib] - ξ2_d[j]) < toler
@inbounds flg_d[2, j] = UInt8(ib)
else
@inbounds fac2 += wb[ib] / (ξ2_d[j] - m1_r[ib])
end
if abs(m1_r[ib] - ξ3_d[j]) < toler
@inbounds flg_d[3, j] = UInt8(ib)
else
@inbounds fac3 += wb[ib] / (ξ3_d[j] - m1_r[ib])
end
end
flg_d[1, j] ≠ UInt8(0) && (fac1 = FT(1))
flg_d[2, j] ≠ UInt8(0) && (fac2 = FT(1))
flg_d[3, j] ≠ UInt8(0) && (fac3 = FT(1))
fac_d[j] = FT(1) / (fac1 * fac2 * fac3)
ctr += 1
end
end
# MPI setup for gathering data on proc 0
root = 0
Np_all = zeros(Int32, npr)
Np_all[pid + 1] = Npl
MPI.Allreduce!(Np_all, +, mpicomm)
if pid ≠ root
x1i_all = zeros(UInt16, 0)
x2i_all = zeros(UInt16, 0)
x3i_all = zeros(UInt16, 0)
else
x1i_all = Array{UInt16}(undef, sum(Np_all))
x2i_all = Array{UInt16}(undef, sum(Np_all))
x3i_all = Array{UInt16}(undef, sum(Np_all))
end
MPI.Gatherv!(x1i_d, x1i_all, Np_all, root, mpicomm)
MPI.Gatherv!(x2i_d, x2i_all, Np_all, root, mpicomm)
MPI.Gatherv!(x3i_d, x3i_all, Np_all, root, mpicomm)
if device isa CUDADevice
ξ1_d = DA(ξ1_d)
ξ2_d = DA(ξ2_d)
ξ3_d = DA(ξ3_d)
x1i_d = DA(x1i_d)
x2i_d = DA(x2i_d)
x3i_d = DA(x3i_d)
flg_d = DA(flg_d)
fac_d = DA(fac_d)
offset = DA(offset)
m1_r = DA(m1_r)
m1_w = DA(m1_w)
wb = DA(wb)
x1i_all = DA(x1i_all)
x2i_all = DA(x2i_all)
x3i_all = DA(x3i_all)
end
return new{
FT,
Int,
typeof(x1g),
typeof(ξ1_d),
typeof(offset),
typeof(xbnd),
typeof(flg_d),
typeof(x1i_d),
typeof(Np_all),
}(
Nel,
Np,
Npl,
poly_order,
xbnd,
x1g,
x2g,
x3g,
ξ1_d,
ξ2_d,
ξ3_d,
flg_d,
fac_d,
x1i_d,
x2i_d,
x3i_d,
offset,
m1_r,
m1_w,
wb,
Np_all,
x1i_all,
x2i_all,
x3i_all,
)
end
end # struct InterpolationBrick
"""
interpolate_local!(
intrp_brck::InterpolationBrick{FT},
sv::AbstractArray{FT},
v::AbstractArray{FT},
) where {FT <: AbstractFloat}
This interpolation function works for a brick, where stretching/compression
happens only along the x1, x2 & x3 axis. Here x1 = X1(ξ1), x2 = X2(ξ2) and x3
= X3(ξ3)
# Arguments
- `intrp_brck`: Initialized InterpolationBrick structure
- `sv`: State Array consisting of various variables on the discontinuous
Galerkin grid
- `v`: Interpolated variables
"""
function interpolate_local!(
intrp_brck::InterpolationBrick{FT},
sv::AbstractArray{FT},
v::AbstractArray{FT},
) where {FT <: AbstractFloat}
offset = intrp_brck.offset
m1_r = intrp_brck.m1_r
wb = intrp_brck.wb
ξ1 = intrp_brck.ξ1
ξ2 = intrp_brck.ξ2
ξ3 = intrp_brck.ξ3
flg = intrp_brck.flg
fac = intrp_brck.fac
qm1 = length(m1_r)
Nel = length(offset) - 1
nvars = size(sv, 2)
device = array_device(sv)
if device isa CPU
Nel = length(offset) - 1
vout = zeros(FT, nvars)
vout_ii = zeros(FT, nvars)
vout_ij = zeros(FT, nvars)
for el in 1:Nel # for each element elno
np = offset[el + 1] - offset[el]
off = offset[el]
for i in 1:np # interpolate point-by-point
ξ1l = ξ1[off + i]
ξ2l = ξ2[off + i]
ξ3l = ξ3[off + i]
f1 = flg[1, off + i]
f2 = flg[2, off + i]
f3 = flg[3, off + i]
fc = fac[off + i]
vout .= 0.0
f3 == 0 ? (ikloop = 1:qm1) : (ikloop = f3:f3)
for ik in ikloop
vout_ij .= 0.0
f2 == 0 ? (ijloop = 1:qm1) : (ijloop = f2:f2)
for ij in ijloop
vout_ii .= 0.0
if f1 == 0
for ii in 1:qm1
for vari in 1:nvars
@inbounds vout_ii[vari] +=
sv[
ii + (ij - 1) * qm1 + (ik - 1) *
qm1 *
qm1,
vari,
el,
] * wb[ii] / (ξ1l - m1_r[ii])#phir[ii]
end
end
else
for vari in 1:nvars
@inbounds vout_ii[vari] = sv[
f1 + (ij - 1) * qm1 + (ik - 1) * qm1 * qm1,
vari,
el,
]
end
end
if f2 == 0
for vari in 1:nvars
@inbounds vout_ij[vari] +=
vout_ii[vari] * wb[ij] / (ξ2l - m1_r[ij])#phis[ij]
end
else
for vari in 1:nvars
@inbounds vout_ij[vari] = vout_ii[vari]
end
end
end
if f3 == 0
for vari in 1:nvars
@inbounds vout[vari] +=
vout_ij[vari] * wb[ik] / (ξ3l - m1_r[ik])#phit[ik]
end
else
for vari in 1:nvars
@inbounds vout[vari] = vout_ij[vari]
end
end
end
for vari in 1:nvars
@inbounds v[off + i, vari] = vout[vari] * fc
end
end
end
else
@cuda threads = (qm1, qm1) blocks = (Nel, nvars) shmem =
qm1 * (qm1 + 2) * sizeof(FT) interpolate_brick_CUDA!(
offset,
m1_r,
wb,
ξ1,
ξ2,
ξ3,
flg,
fac,
sv,
v,
)
end
end
function interpolate_brick_CUDA!(
offset::AbstractArray{T, 1},
m1_r::AbstractArray{FT, 1},
wb::AbstractArray{FT, 1},
ξ1::AbstractArray{FT, 1},
ξ2::AbstractArray{FT, 1},
ξ3::AbstractArray{FT, 1},
flg::AbstractArray{UInt8, 2},
fac::AbstractArray{FT, 1},
sv::AbstractArray{FT},
v::AbstractArray{FT},
) where {T <: Int, FT <: AbstractFloat}
tj = threadIdx().x
tk = threadIdx().y # thread ids
el = blockIdx().x # assigning one element per block
st_idx = blockIdx().y
qm1 = length(m1_r)
# create views for shared memory
shm_FT = @cuDynamicSharedMem(FT, (qm1, qm1 + 2))
vout_jk = view(shm_FT, :, 1:qm1)
wb_sh = view(shm_FT, :, qm1 + 1)
m1_r_sh = view(shm_FT, :, qm1 + 2)
# load shared memory
if tk == 1
wb_sh[tj] = wb[tj]
m1_r_sh[tj] = m1_r[tj]
end
sync_threads()
np = offset[el + 1] - offset[el]
off = offset[el]
for i in 1:np # interpolate point-by-point
ξ1l = ξ1[off + i]
ξ2l = ξ2[off + i]
ξ3l = ξ3[off + i]
f1 = flg[1, off + i]
f2 = flg[2, off + i]
f3 = flg[3, off + i]
fc = fac[off + i]
if f1 == 0 # apply phir
@inbounds vout_jk[tj, tk] =
sv[1 + (tj - 1) * qm1 + (tk - 1) * qm1 * qm1, st_idx, el] *
wb_sh[1] / (ξ1l - m1_r_sh[1])
for ii in 2:qm1
@inbounds vout_jk[tj, tk] +=
sv[ii + (tj - 1) * qm1 + (tk - 1) * qm1 * qm1, st_idx, el] *
wb_sh[ii] / (ξ1l - m1_r_sh[ii])
end
else
@inbounds vout_jk[tj, tk] =
sv[f1 + (tj - 1) * qm1 + (tk - 1) * qm1 * qm1, st_idx, el]
end
if f2 == 0 # apply phis
@inbounds vout_jk[tj, tk] *= (wb_sh[tj] / (ξ2l - m1_r_sh[tj]))
end
sync_threads()
if tj == 1 # reduction
if f2 == 0
for ij in 2:qm1
@inbounds vout_jk[1, tk] += vout_jk[ij, tk]
end
else
if f2 ≠ 1
@inbounds vout_jk[1, tk] = vout_jk[f2, tk]
end
end
if f3 == 0 # apply phit
@inbounds vout_jk[1, tk] *= (wb_sh[tk] / (ξ3l - m1_r_sh[tk]))
end
end
sync_threads()
if tj == 1 && tk == 1 # reduction
if f3 == 0
for ik in 2:qm1
@inbounds vout_jk[1, 1] += vout_jk[1, ik]
end
else
if f3 ≠ 1
@inbounds vout_jk[1, 1] = vout_jk[1, f3]
end
end
@inbounds v[off + i, st_idx] = vout_jk[1, 1] * fc
end
end
return nothing
end
function dimensions(interpol::InterpolationBrick)
if Array ∈ typeof(interpol.x1g).parameters
h_x1g = interpol.x1g
h_x2g = interpol.x2g
h_x3g = interpol.x3g
else
h_x1g = Array(interpol.x1g)
h_x2g = Array(interpol.x2g)
h_x3g = Array(interpol.x3g)
end
return OrderedDict(
"x" => (h_x1g, OrderedDict()),
"y" => (h_x2g, OrderedDict()),
"z" => (h_x3g, OrderedDict()),
)
end
"""
InterpolationCubedSphere{
FT <: AbstractFloat,
T <: Int,
FTV <: AbstractVector{FT},
FTVD <: AbstractVector{FT},
TVD <: AbstractVector{T},
UI8AD <: AbstractArray{UInt8, 2},
UI16VD <: AbstractVector{UInt16},
I32V <: AbstractVector{Int32},
} <: InterpolationTopology
This interpolation structure and the corresponding functions works for a cubed sphere topology. The data is interpolated along a lat/long/rad grid.
-90⁰ ≤ lat ≤ 90⁰
-180⁰ ≤ long ≤ 180⁰
Rᵢ ≤ r ≤ Rₒ
# Fields
$(DocStringExtensions.FIELDS)
# Usage
InterpolationCubedSphere(grid::DiscontinuousSpectralElementGrid, vert_range::AbstractArray{FT}, nhor::Int, lat_res::FT, long_res::FT, rad_res::FT) where {FT <: AbstractFloat}
This interpolation structure and the corresponding functions works for a cubed sphere topology. The data is interpolated along a lat/long/rad grid.
-90⁰ ≤ lat ≤ 90⁰
-180⁰ ≤ long ≤ 180⁰
Rᵢ ≤ r ≤ Rₒ
# Arguments for the inner constructor
- `grid`: DiscontinousSpectralElementGrid
- `vert_range`: Vertex range along the radial coordinate
- `lat_res`: Resolution of the interpolation grid along the latitude coordinate in radians
- `long_res`: Resolution of the interpolation grid along the longitude coordinate in radians
- `rad_res`: Resolution of the interpolation grid along the radial coordinate
"""
struct InterpolationCubedSphere{
FT <: AbstractFloat,
T <: Int,
FTV <: AbstractVector{FT},
FTVD <: AbstractVector{FT},
TVD <: AbstractVector{T},
UI8AD <: AbstractArray{UInt8, 2},
UI16VD <: AbstractVector{UInt16},
I32V <: AbstractVector{Int32},
} <: InterpolationTopology
"Number of elements"
Nel::T
"Number of interpolation points"
Np::T
"Number of interpolation points on local process"
Npl::T # # of interpolation points on the local process
"Polynomial order of spectral element approximation"
poly_order::T
"Number of interpolation points in radial direction"
n_rad::T
"Number of interpolation points in lat direction"
n_lat::T
"Number of interpolation points in long direction"
n_long::T
"Interpolation grid in radial direction"
rad_grd::FTV
"Interpolation grid in lat direction"
lat_grd::FTV
"Interpolation grid in long direction"
long_grd::FTV # rad, lat & long locations of interpolation grid
"Device array containing ξ1 coordinates of interpolation points within each element"
ξ1::FTVD
"Device array containing ξ2 coordinates of interpolation points within each element"
ξ2::FTVD
"Device array containing ξ3 coordinates of interpolation points within each element"
ξ3::FTVD
"flags when ξ1/ξ2/ξ3 interpolation point matches with a GLL point"
flg::UI8AD
"Normalization factor"
fac::FTVD
"Radial coordinates of interpolation points withing each element"
radi::UI16VD
"Latitude coordinates of interpolation points withing each element"
lati::UI16VD
"Longitude coordinates of interpolation points withing each element"
longi::UI16VD
"Offsets for each element"
offset::TVD
"GLL points"
m1_r::FTVD
"GLL weights"
m1_w::FTVD
"Barycentric weights"
wb::FTVD
# MPI setup for gathering interpolated variable on proc 0
"Number of interpolation points on each of the processes"
Np_all::I32V
"Radial interpolation grid index of interpolation points within each element on all processes stored only on proc 0"
radi_all::UI16VD
"Latitude interpolation grid index of interpolation points within each element on all processes stored only on proc 0"
lati_all::UI16VD
"Longitude interpolation grid index of interpolation points within each element on all processes stored only on proc 0"
longi_all::UI16VD
function InterpolationCubedSphere(
grid::DiscontinuousSpectralElementGrid,
vert_range::AbstractArray{FT},
nhor::Int,
lat_grd::AbstractArray{FT, 1},
long_grd::AbstractArray{FT, 1},
rad_grd::AbstractArray{FT},
) where {FT <: AbstractFloat}
mpicomm = MPI.COMM_WORLD
pid = MPI.Comm_rank(mpicomm)
npr = MPI.Comm_size(mpicomm)
DA = arraytype(grid) # device array
device = arraytype(grid) <: Array ? CPU() : CUDADevice()
poly_order = polynomialorder(grid)
qm1 = poly_order + 1
toler1 = FT(eps(FT) * vert_range[1] * 2.0) # tolerance for unwarp function
toler2 = FT(eps(FT) * 4.0) # tolerance
toler3 = FT(eps(FT) * vert_range[1] * 10.0) # tolerance for Newton-Raphson
Nel = length(grid.topology.realelems) # # of local elements on the local process
nvert_range = length(vert_range)
nvert = nvert_range - 1 # # of elements in vertical direction
Nel_glob = nvert * nhor * nhor * 6
nblck = nhor * nhor * nvert
Δh = 2 / nhor # horizontal grid spacing in unwarped grid
n_lat, n_long, n_rad =
Int(length(lat_grd)), Int(length(long_grd)), Int(length(rad_grd))
Np = n_lat * n_long * n_rad
uw_grd = zeros(FT, 3)
diffv = zeros(FT, 3)
ξ = zeros(FT, 3)
glob_ord = grid.topology.origsendorder # to account for reordering of elements after the partitioning process
glob_elem_no = zeros(Int, nvert * length(glob_ord))
for i in 1:length(glob_ord), j in 1:nvert
glob_elem_no[j + (i - 1) * nvert] = (glob_ord[i] - 1) * nvert + j
end
glob_to_loc = Dict(glob_elem_no[i] => Int(i) for i in 1:Nel) # using dictionary for speedup
ξ1, ξ2, ξ3 = map(i -> zeros(FT, i), zeros(Int, Nel)),
map(i -> zeros(FT, i), zeros(Int, Nel)),
map(i -> zeros(FT, i), zeros(Int, Nel))
radi, lati, longi = map(i -> zeros(UInt16, i), zeros(UInt16, Nel)),
map(i -> zeros(UInt16, i), zeros(UInt16, Nel)),
map(i -> zeros(UInt16, i), zeros(UInt16, Nel))
offset_d = zeros(Int, Nel + 1)
for i in 1:n_rad
rad = rad_grd[i]
if rad ≤ vert_range[1] # accounting for minor rounding errors from unwarp function at boundaries
vert_range[1] - rad < toler1 ? l_nrm = 1 :
error(
"fatal error, rad lower than inner radius: ",
vert_range[1] - rad,
" $rad_grd /// $lat_grd //// $long_grd",
)
elseif rad ≥ vert_range[end] # accounting for minor rounding errors from unwarp function at boundaries
rad - vert_range[end] < toler1 ? l_nrm = nvert :
error("fatal error, rad greater than outer radius")
else # normal scenario
for l in 2:nvert_range
if vert_range[l] - rad > FT(0)
l_nrm = l - 1
break
end
end
end
for j in 1:n_lat
@inbounds x3_grd = rad * sind(lat_grd[j])
for k in 1:n_long
@inbounds x1_grd =
rad * cosd(lat_grd[j]) * cosd(long_grd[k]) # inclination -> latitude; azimuthal -> longitude.
@inbounds x2_grd =
rad * cosd(lat_grd[j]) * sind(long_grd[k]) # inclination -> latitude; azimuthal -> longitude.
uw_grd[1], uw_grd[2], uw_grd[3] =
Topologies.cubedshellunwarp(x1_grd, x2_grd, x3_grd) # unwarping from sphere to cubed shell
x1_uw2_grd = uw_grd[1] / rad # unwrapping cubed shell on to a 2D grid (in 3D space, -1 to 1 cube)
x2_uw2_grd = uw_grd[2] / rad
x3_uw2_grd = uw_grd[3] / rad
if abs(x1_uw2_grd + 1) < toler2 # face 1 (x1 == -1 plane)
l2 = min(div(x2_uw2_grd + 1, Δh) + 1, nhor)
l3 = min(div(x3_uw2_grd + 1, Δh) + 1, nhor)
el_glob = Int(
l_nrm +
(nhor - l2) * nvert +
(l3 - 1) * nvert * nhor,
)
elseif abs(x2_uw2_grd + 1) < toler2 # face 2 (x2 == -1 plane)
l1 = min(div(x1_uw2_grd + 1, Δh) + 1, nhor)
l3 = min(div(x3_uw2_grd + 1, Δh) + 1, nhor)
el_glob = Int(
l_nrm +
(l1 - 1) * nvert +
(l3 - 1) * nvert * nhor +
nblck * 1,
)
elseif abs(x1_uw2_grd - 1) < toler2 # face 3 (x1 == +1 plane)
l2 = min(div(x2_uw2_grd + 1, Δh) + 1, nhor)
l3 = min(div(x3_uw2_grd + 1, Δh) + 1, nhor)
el_glob = Int(
l_nrm +
(l2 - 1) * nvert +
(l3 - 1) * nvert * nhor +
nblck * 2,
)
elseif abs(x3_uw2_grd - 1) < toler2 # face 4 (x3 == +1 plane)
l1 = min(div(x1_uw2_grd + 1, Δh) + 1, nhor)
l2 = min(div(x2_uw2_grd + 1, Δh) + 1, nhor)
el_glob = Int(
l_nrm +
(l1 - 1) * nvert +
(l2 - 1) * nvert * nhor +
nblck * 3,
)
elseif abs(x2_uw2_grd - 1) < toler2 # face 5 (x2 == +1 plane)
l1 = min(div(x1_uw2_grd + 1, Δh) + 1, nhor)
l3 = min(div(x3_uw2_grd + 1, Δh) + 1, nhor)
el_glob = Int(
l_nrm +
(l1 - 1) * nvert +
(nhor - l3) * nvert * nhor +
nblck * 4,
)
elseif abs(x3_uw2_grd + 1) < toler2 # face 6 (x3 == -1 plane)
l1 = min(div(x1_uw2_grd + 1, Δh) + 1, nhor)
l2 = min(div(x2_uw2_grd + 1, Δh) + 1, nhor)
el_glob = Int(
l_nrm +
(l1 - 1) * nvert +
(nhor - l2) * nvert * nhor +
nblck * 5,
)
else
error("error: unwrapped grid does not lie on any of the 6 faces")
end
el_loc = get(glob_to_loc, el_glob, nothing)
if el_loc ≠ nothing # computing inner coordinates for local elements
invert_trilear_mapping_hex!(
view(grid.topology.elemtocoord, 1, :, el_loc),
view(grid.topology.elemtocoord, 2, :, el_loc),
view(grid.topology.elemtocoord, 3, :, el_loc),
uw_grd,
diffv,
toler3,
ξ,
)
push!(ξ1[el_loc], ξ[1])
push!(ξ2[el_loc], ξ[2])
push!(ξ3[el_loc], ξ[3])
push!(radi[el_loc], UInt16(i))
push!(lati[el_loc], UInt16(j))
push!(longi[el_loc], UInt16(k))
offset_d[el_loc + 1] += 1
end
end
end
end
for i in 2:(Nel + 1)
@inbounds offset_d[i] += offset_d[i - 1]
end
Npl = offset_d[Nel + 1]
v = Vector{FT}(undef, offset_d[Nel + 1]) # Allocating storage for interpolation variable
ξ1_d = Vector{FT}(undef, Npl)
ξ2_d = Vector{FT}(undef, Npl)
ξ3_d = Vector{FT}(undef, Npl)
flg_d = zeros(UInt8, 3, Npl)
fac_d = ones(FT, Npl)
rad_d = Vector{UInt16}(undef, Npl)
lat_d = Vector{UInt16}(undef, Npl)
long_d = Vector{UInt16}(undef, Npl)
m1_r, m1_w = GaussQuadrature.legendre(FT, qm1, GaussQuadrature.both)
wb = Elements.baryweights(m1_r)
for i in 1:Nel
ctr = 1
for j in (offset_d[i] + 1):offset_d[i + 1]
@inbounds ξ1_d[j] = ξ1[i][ctr]
@inbounds ξ2_d[j] = ξ2[i][ctr]
@inbounds ξ3_d[j] = ξ3[i][ctr]
@inbounds rad_d[j] = radi[i][ctr]
@inbounds lat_d[j] = lati[i][ctr]
@inbounds long_d[j] = longi[i][ctr]
# set up interpolation
fac1 = FT(0)
fac2 = FT(0)
fac3 = FT(0)
for ib in 1:qm1
if abs(m1_r[ib] - ξ1_d[j]) < toler2
@inbounds flg_d[1, j] = UInt8(ib)
else
@inbounds fac1 += wb[ib] / (ξ1_d[j] - m1_r[ib])
end
if abs(m1_r[ib] - ξ2_d[j]) < toler2
@inbounds flg_d[2, j] = UInt8(ib)
else
@inbounds fac2 += wb[ib] / (ξ2_d[j] - m1_r[ib])
end
if abs(m1_r[ib] - ξ3_d[j]) < toler2
@inbounds flg_d[3, j] = UInt8(ib)
else
@inbounds fac3 += wb[ib] / (ξ3_d[j] - m1_r[ib])
end
end
flg_d[1, j] ≠ 0 && (fac1 = FT(1))
flg_d[2, j] ≠ 0 && (fac2 = FT(1))
flg_d[3, j] ≠ 0 && (fac3 = FT(1))
fac_d[j] = FT(1) / (fac1 * fac2 * fac3)
ctr += 1
end
end
# MPI setup for gathering data on proc 0
root = 0
Np_all = zeros(Int32, npr)
Np_all[pid + 1] = Int32(Npl)
MPI.Allreduce!(Np_all, +, mpicomm)
if pid ≠ root
radi_all = zeros(UInt16, 0)
lati_all = zeros(UInt16, 0)
longi_all = zeros(UInt16, 0)
else
radi_all = Array{UInt16}(undef, sum(Np_all))
lati_all = Array{UInt16}(undef, sum(Np_all))
longi_all = Array{UInt16}(undef, sum(Np_all))
end
MPI.Gatherv!(rad_d, radi_all, Np_all, root, mpicomm)
MPI.Gatherv!(lat_d, lati_all, Np_all, root, mpicomm)
MPI.Gatherv!(long_d, longi_all, Np_all, root, mpicomm)
if device isa CUDADevice
ξ1_d = DA(ξ1_d)
ξ2_d = DA(ξ2_d)
ξ3_d = DA(ξ3_d)
flg_d = DA(flg_d)
fac_d = DA(fac_d)
rad_d = DA(rad_d)
lat_d = DA(lat_d)
long_d = DA(long_d)
m1_r = DA(m1_r)
m1_w = DA(m1_w)
wb = DA(wb)
offset_d = DA(offset_d)
rad_grd = DA(rad_grd)
lat_grd = DA(lat_grd)
long_grd = DA(long_grd)