This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
/
Topologies.jl
1298 lines (1085 loc) · 36.5 KB
/
Topologies.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 Topologies
import ..BrickMesh
import MPI
using DocStringExtensions
export AbstractTopology,
BrickTopology,
StackedBrickTopology,
CubedShellTopology,
StackedCubedSphereTopology,
isstacked,
cubedshellwarp,
cubedshellunwarp
export grid1d, SingleExponentialStretching, InteriorStretching
"""
AbstractTopology{dim}
Represents the connectivity of individual elements, with local dimension `dim`.
"""
abstract type AbstractTopology{dim} end
"""
BoxElementTopology{dim, T} <: AbstractTopology{dim}
The local topology of a larger MPI-distributed topology, represented by
`dim`-dimensional box elements.
This contains the necessary information for the connectivity elements of the
elements on the local process, along with "ghost" elements from neighbouring
processes.
# Fields
$(DocStringExtensions.FIELDS)
"""
struct BoxElementTopology{dim, T} <: AbstractTopology{dim}
"""
MPI communicator for communicating with neighbouring processes.
"""
mpicomm::MPI.Comm
"""
Range of element indices
"""
elems::UnitRange{Int64}
"""
Range of real (aka nonghost) element indices
"""
realelems::UnitRange{Int64}
"""
Range of ghost element indices
"""
ghostelems::UnitRange{Int64}
"""
Ghost element to face is received; `ghostfaces[f,ge] == true` if face `f` of
ghost element `ge` is received.
"""
ghostfaces::BitArray{2}
"""
Array of send element indices
"""
sendelems::Array{Int64, 1}
"""
Send element to face is sent; `sendfaces[f,se] == true` if face `f` of send
element `se` is sent.
"""
sendfaces::BitArray{2}
"""
Array of real elements that do not have a ghost element as a neighbor.
"""
interiorelems::Array{Int64, 1}
"""
Array of real elements that have at least on ghost element as a neighbor.
Note that this is different from `sendelems` because `sendelems` duplicates
elements that need to be sent to multiple neighboring processes.
"""
exteriorelems::Array{Int64, 1}
"""
Element to vertex coordinates; `elemtocoord[d,i,e]` is the `d`th coordinate
of corner `i` of element `e`
!!! note
currently coordinates always are of size 3 for `(x1, x2, x3)`
"""
elemtocoord::Array{T, 3}
"""
Element to neighboring element; `elemtoelem[f,e]` is the number of the
element neighboring element `e` across face `f`. If there is no neighboring
element then `elemtoelem[f,e] == e`.
"""
elemtoelem::Array{Int64, 2}
"""
Element to neighboring element face; `elemtoface[f,e]` is the face number of
the element neighboring element `e` across face `f`. If there is no
neighboring element then `elemtoface[f,e] == f`."
"""
elemtoface::Array{Int64, 2}
"""
element to neighboring element order; `elemtoordr[f,e]` is the ordering
number of the element neighboring element `e` across face `f`. If there is
no neighboring element then `elemtoordr[f,e] == 1`.
"""
elemtoordr::Array{Int64, 2}
"""
Element to boundary number; `elemtobndy[f,e]` is the boundary number of face
`f` of element `e`. If there is a neighboring element then `elemtobndy[f,e]
== 0`.
"""
elemtobndy::Array{Int64, 2}
"""
List of the MPI ranks for the neighboring processes
"""
nabrtorank::Array{Int64, 1}
"""
Range in ghost elements to receive for each neighbor
"""
nabrtorecv::Array{UnitRange{Int64}, 1}
"""
Range in `sendelems` to send for each neighbor
"""
nabrtosend::Array{UnitRange{Int64}, 1}
"""
original order in partitioning
"""
origsendorder::Array{Int64, 1}
"""
boolean for whether or not this topology has a boundary
"""
hasboundary::Bool
function BoxElementTopology{dim, T}(
mpicomm,
elems,
realelems,
ghostelems,
ghostfaces,
sendelems,
sendfaces,
elemtocoord,
elemtoelem,
elemtoface,
elemtoordr,
elemtobndy,
nabrtorank,
nabrtorecv,
nabrtosend,
origsendorder,
hasboundary,
) where {dim, T}
exteriorelems = sort(unique(sendelems))
interiorelems = sort(setdiff(realelems, exteriorelems))
return new{dim, T}(
mpicomm,
elems,
realelems,
ghostelems,
ghostfaces,
sendelems,
sendfaces,
interiorelems,
exteriorelems,
elemtocoord,
elemtoelem,
elemtoface,
elemtoordr,
elemtobndy,
nabrtorank,
nabrtorecv,
nabrtosend,
origsendorder,
hasboundary,
)
end
end
"""
hasboundary(topology::AbstractTopology)
query function to check whether a topology has a boundary (i.e., not fully
periodic)
"""
hasboundary(topology::AbstractTopology) = topology.hasboundary
if VERSION >= v"1.2-"
isstacked(::T) where {T <: AbstractTopology} = hasfield(T, :stacksize)
else
isstacked(::T) where {T <: AbstractTopology} =
Base.fieldindex(T, :stacksize, false) > 0
end
"""
BrickTopology{dim, T} <: AbstractTopology{dim}
A simple grid-based topology. This is a convenience wrapper around
[`BoxElementTopology`](@ref).
"""
struct BrickTopology{dim, T} <: AbstractTopology{dim}
topology::BoxElementTopology{dim, T}
end
Base.getproperty(a::BrickTopology, p::Symbol) =
getproperty(getfield(a, :topology), p)
"""
CubedShellTopology{T} <: AbstractTopology{2}
A cube-shell topology. This is a convenience wrapper around
[`BoxElementTopology`](@ref).
"""
struct CubedShellTopology{T} <: AbstractTopology{2}
topology::BoxElementTopology{2, T}
end
Base.getproperty(a::CubedShellTopology, p::Symbol) =
getproperty(getfield(a, :topology), p)
"""
StackedBrickTopology{dim, T} <: AbstractTopology{dim}
A simple grid-based topology, where all elements on the trailing dimension are
stacked to be contiguous. This is a convenience wrapper around
[`BoxElementTopology`](@ref).
"""
struct StackedBrickTopology{dim, T} <: AbstractTopology{dim}
topology::BoxElementTopology{dim, T}
stacksize::Int64
end
function Base.getproperty(a::StackedBrickTopology, p::Symbol)
return p == :stacksize ? getfield(a, p) :
getproperty(getfield(a, :topology), p)
end
"""
StackedCubedSphereTopology{3, T} <: AbstractTopology{3}
A cube-sphere topology. All elements on the same "vertical" dimension are
stacked to be contiguous. This is a convenience wrapper around
[`BoxElementTopology`](@ref).
"""
struct StackedCubedSphereTopology{T} <: AbstractTopology{3}
topology::BoxElementTopology{3, T}
stacksize::Int64
end
function Base.getproperty(a::StackedCubedSphereTopology, p::Symbol)
return p == :stacksize ? getfield(a, p) :
getproperty(getfield(a, :topology), p)
end
""" A wrapper for the BrickTopology """
BrickTopology(mpicomm, Nelems::NTuple{N, Integer}; kw...) where {N} =
BrickTopology(mpicomm, map(Ne -> 0:Ne, Nelems); kw...)
"""
BrickTopology{dim, T}(mpicomm, elemrange; boundary, periodicity)
Generate a brick mesh topology with coordinates given by the tuple `elemrange`
and the periodic dimensions given by the `periodicity` tuple.
The elements of the brick are partitioned equally across the MPI ranks based
on a space-filling curve.
By default boundary faces will be marked with a one and other faces with a
zero. Specific boundary numbers can also be passed for each face of the brick
in `boundary`. This will mark the nonperiodic brick faces with the given
boundary number.
# Examples
We can build a 3 by 2 element two-dimensional mesh that is periodic in the
\$x2\$-direction with
```jldoctest brickmesh
using ClimateMachine.Topologies
using MPI
MPI.Init()
topology = BrickTopology(MPI.COMM_SELF, (2:5,4:6);
periodicity=(false,true),
boundary=((1,2),(3,4)))
```
This returns the mesh structure for
x2
^
|
6- +-----+-----+-----+
| | | | |
| | 3 | 4 | 5 |
| | | | |
5- +-----+-----+-----+
| | | | |
| | 1 | 2 | 6 |
| | | | |
4- +-----+-----+-----+
|
+--|-----|-----|-----|--> x1
2 3 4 5
For example, the (dimension by number of corners by number of elements) array
`elemtocoord` gives the coordinates of the corners of each element.
```jldoctest brickmesh
julia> topology.elemtocoord
2×4×6 Array{Int64,3}:
[:, :, 1] =
2 3 2 3
4 4 5 5
[:, :, 2] =
3 4 3 4
4 4 5 5
[:, :, 3] =
2 3 2 3
5 5 6 6
[:, :, 4] =
3 4 3 4
5 5 6 6
[:, :, 5] =
4 5 4 5
5 5 6 6
[:, :, 6] =
4 5 4 5
4 4 5 5
```
Note that the corners are listed in Cartesian order.
The (number of faces by number of elements) array `elemtobndy` gives the
boundary number for each face of each element. A zero will be given for
connected faces.
```jldoctest brickmesh
julia> topology.elemtobndy
4×6 Array{Int64,2}:
1 0 1 0 0 0
0 0 0 0 2 2
0 0 0 0 0 0
0 0 0 0 0 0
```
Note that the faces are listed in Cartesian order.
"""
function BrickTopology(
mpicomm,
elemrange;
boundary = ntuple(j -> (1, 1), length(elemrange)),
periodicity = ntuple(j -> false, length(elemrange)),
connectivity = :face,
ghostsize = 1,
)
if boundary isa Matrix
boundary = tuple(mapslices(x -> tuple(x...), boundary, dims = 1)...)
end
# We cannot handle anything else right now...
@assert connectivity == :face
@assert ghostsize == 1
mpirank = MPI.Comm_rank(mpicomm)
mpisize = MPI.Comm_size(mpicomm)
topology = BrickMesh.brickmesh(
elemrange,
periodicity,
part = mpirank + 1,
numparts = mpisize,
boundary = boundary,
)
topology = BrickMesh.partition(mpicomm, topology...)
origsendorder = topology[5]
topology = BrickMesh.connectmesh(mpicomm, topology[1:4]...)
dim = length(elemrange)
T = eltype(topology.elemtocoord)
return BrickTopology{dim, T}(BoxElementTopology{dim, T}(
mpicomm,
topology.elems,
topology.realelems,
topology.ghostelems,
topology.ghostfaces,
topology.sendelems,
topology.sendfaces,
topology.elemtocoord,
topology.elemtoelem,
topology.elemtoface,
topology.elemtoordr,
topology.elemtobndy,
topology.nabrtorank,
topology.nabrtorecv,
topology.nabrtosend,
origsendorder,
!minimum(periodicity),
))
end
""" A wrapper for the StackedBrickTopology """
StackedBrickTopology(mpicomm, Nelems::NTuple{N, Integer}; kw...) where {N} =
StackedBrickTopology(mpicomm, map(Ne -> 0:Ne, Nelems); kw...)
"""
StackedBrickTopology{dim, T}(mpicomm, elemrange; boundary, periodicity)
Generate a stacked brick mesh topology with coordinates given by the tuple
`elemrange` and the periodic dimensions given by the `periodicity` tuple.
The elements are stacked such that the elements associated with range
`elemrange[dim]` are contiguous in the element ordering.
The elements of the brick are partitioned equally across the MPI ranks based
on a space-filling curve. Further, stacks are not split at MPI boundaries.
By default boundary faces will be marked with a one and other faces with a
zero. Specific boundary numbers can also be passed for each face of the brick
in `boundary`. This will mark the nonperiodic brick faces with the given
boundary number.
# Examples
We can build a 3 by 2 element two-dimensional mesh that is periodic in the
\$x2\$-direction with
```jldoctest brickmesh
using ClimateMachine.Topologies
using MPI
MPI.Init()
topology = StackedBrickTopology(MPI.COMM_SELF, (2:5,4:6);
periodicity=(false,true),
boundary=((1,2),(3,4)))
```
This returns the mesh structure stacked in the \$x2\$-direction for
x2
^
|
6- +-----+-----+-----+
| | | | |
| | 2 | 4 | 6 |
| | | | |
5- +-----+-----+-----+
| | | | |
| | 1 | 3 | 5 |
| | | | |
4- +-----+-----+-----+
|
+--|-----|-----|-----|--> x1
2 3 4 5
For example, the (dimension by number of corners by number of elements) array
`elemtocoord` gives the coordinates of the corners of each element.
```jldoctest brickmesh
julia> topology.elemtocoord
2×4×6 Array{Int64,3}:
[:, :, 1] =
2 3 2 3
4 4 5 5
[:, :, 2] =
2 3 2 3
5 5 6 6
[:, :, 3] =
3 4 3 4
4 4 5 5
[:, :, 4] =
3 4 3 4
5 5 6 6
[:, :, 5] =
4 5 4 5
4 4 5 5
[:, :, 6] =
4 5 4 5
5 5 6 6
```
Note that the corners are listed in Cartesian order.
The (number of faces by number of elements) array `elemtobndy` gives the
boundary number for each face of each element. A zero will be given for
connected faces.
```jldoctest brickmesh
julia> topology.elemtobndy
4×6 Array{Int64,2}:
1 0 1 0 0 0
0 0 0 0 2 2
0 0 0 0 0 0
0 0 0 0 0 0
```
Note that the faces are listed in Cartesian order.
"""
function StackedBrickTopology(
mpicomm,
elemrange;
boundary = ntuple(j -> (1, 1), length(elemrange)),
periodicity = ntuple(j -> false, length(elemrange)),
connectivity = :face,
ghostsize = 1,
)
if boundary isa Matrix
boundary = tuple(mapslices(x -> tuple(x...), boundary, dims = 1)...)
end
dim = length(elemrange)
dim <= 1 && error("Stacked brick topology works for 2D and 3D")
# Build the base topology
basetopo = BrickTopology(
mpicomm,
elemrange[1:(dim - 1)];
boundary = boundary[1:(dim - 1)],
periodicity = periodicity[1:(dim - 1)],
connectivity = connectivity,
ghostsize = ghostsize,
)
# Use the base topology to build the stacked topology
stack = elemrange[dim]
stacksize = length(stack) - 1
nvert = 2^dim
nface = 2dim
nreal = length(basetopo.realelems) * stacksize
nghost = length(basetopo.ghostelems) * stacksize
elems = 1:(nreal + nghost)
realelems = 1:nreal
ghostelems = nreal .+ (1:nghost)
sendelems =
similar(basetopo.sendelems, length(basetopo.sendelems) * stacksize)
for i in 1:length(basetopo.sendelems), j in 1:stacksize
sendelems[stacksize * (i - 1) + j] =
stacksize * (basetopo.sendelems[i] - 1) + j
end
ghostfaces = similar(basetopo.ghostfaces, nface, length(ghostelems))
ghostfaces .= false
for i in 1:length(basetopo.ghostelems), j in 1:stacksize
e = stacksize * (i - 1) + j
for f in 1:(2 * (dim - 1))
ghostfaces[f, e] = basetopo.ghostfaces[f, i]
end
end
sendfaces = similar(basetopo.sendfaces, nface, length(sendelems))
sendfaces .= false
for i in 1:length(basetopo.sendelems), j in 1:stacksize
e = stacksize * (i - 1) + j
for f in 1:(2 * (dim - 1))
sendfaces[f, e] = basetopo.sendfaces[f, i]
end
end
elemtocoord = similar(basetopo.elemtocoord, dim, nvert, length(elems))
for i in 1:length(basetopo.elems), j in 1:stacksize
e = stacksize * (i - 1) + j
for v in 1:(2^(dim - 1))
for d in 1:(dim - 1)
elemtocoord[d, v, e] = basetopo.elemtocoord[d, v, i]
elemtocoord[d, 2^(dim - 1) + v, e] =
basetopo.elemtocoord[d, v, i]
end
elemtocoord[dim, v, e] = stack[j]
elemtocoord[dim, 2^(dim - 1) + v, e] = stack[j + 1]
end
end
elemtoelem = similar(basetopo.elemtoelem, nface, length(elems))
elemtoface = similar(basetopo.elemtoface, nface, length(elems))
elemtoordr = similar(basetopo.elemtoordr, nface, length(elems))
elemtobndy = similar(basetopo.elemtobndy, nface, length(elems))
for e in 1:(length(basetopo.elems) * stacksize), f in 1:nface
elemtoelem[f, e] = e
elemtoface[f, e] = f
elemtoordr[f, e] = 1
elemtobndy[f, e] = 0
end
for i in 1:length(basetopo.realelems), j in 1:stacksize
e1 = stacksize * (i - 1) + j
for f in 1:(2 * (dim - 1))
e2 = stacksize * (basetopo.elemtoelem[f, i] - 1) + j
elemtoelem[f, e1] = e2
elemtoface[f, e1] = basetopo.elemtoface[f, i]
# We assume a simple orientation right now
@assert basetopo.elemtoordr[f, i] == 1
elemtoordr[f, e1] = basetopo.elemtoordr[f, i]
end
et = stacksize * (i - 1) + j + 1
eb = stacksize * (i - 1) + j - 1
ft = 2 * (dim - 1) + 1
fb = 2 * (dim - 1) + 2
ot = 1
ob = 1
if j == stacksize
et = periodicity[dim] ? stacksize * (i - 1) + 1 : e1
ft = periodicity[dim] ? ft : 2 * (dim - 1) + 2
end
if j == 1
eb = periodicity[dim] ? stacksize * (i - 1) + stacksize : e1
fb = periodicity[dim] ? fb : 2 * (dim - 1) + 1
end
elemtoelem[2 * (dim - 1) + 1, e1] = eb
elemtoelem[2 * (dim - 1) + 2, e1] = et
elemtoface[2 * (dim - 1) + 1, e1] = fb
elemtoface[2 * (dim - 1) + 2, e1] = ft
elemtoordr[2 * (dim - 1) + 1, e1] = ob
elemtoordr[2 * (dim - 1) + 2, e1] = ot
end
for i in 1:length(basetopo.elems), j in 1:stacksize
e1 = stacksize * (i - 1) + j
for f in 1:(2 * (dim - 1))
elemtobndy[f, e1] = basetopo.elemtobndy[f, i]
end
bt = bb = 0
if j == stacksize
bt = periodicity[dim] ? bt : boundary[dim][2]
end
if j == 1
bb = periodicity[dim] ? bb : boundary[dim][1]
end
elemtobndy[2 * (dim - 1) + 1, e1] = bb
elemtobndy[2 * (dim - 1) + 2, e1] = bt
end
nabrtorank = basetopo.nabrtorank
nabrtorecv = UnitRange{Int}[
UnitRange(
stacksize * (first(basetopo.nabrtorecv[n]) - 1) + 1,
stacksize * last(basetopo.nabrtorecv[n]),
) for n in 1:length(nabrtorank)
]
nabrtosend = UnitRange{Int}[
UnitRange(
stacksize * (first(basetopo.nabrtosend[n]) - 1) + 1,
stacksize * last(basetopo.nabrtosend[n]),
) for n in 1:length(nabrtorank)
]
T = eltype(basetopo.elemtocoord)
StackedBrickTopology{dim, T}(
BoxElementTopology{dim, T}(
mpicomm,
elems,
realelems,
ghostelems,
ghostfaces,
sendelems,
sendfaces,
elemtocoord,
elemtoelem,
elemtoface,
elemtoordr,
elemtobndy,
nabrtorank,
nabrtorecv,
nabrtosend,
basetopo.origsendorder,
!minimum(periodicity),
),
stacksize,
)
end
"""
CubedShellTopology(mpicomm, Nelem, T) <: AbstractTopology{dim}
Generate a cubed shell mesh with the number of elements along each dimension of
the cubes being `Nelem`. This topology actual creates a cube mesh, and the
warping should be done after the grid is created using the `cubedshellwarp`
function. The coordinates of the points will be of type `T`.
The elements of the shell are partitioned equally across the MPI ranks based
on a space-filling curve.
Note that this topology is logically 2-D but embedded in a 3-D space
# Examples
We can build a cubed shell mesh with 10 elements on each cube, total elements is
`10 * 10 * 6 = 600`, with
```jldoctest brickmesh
using ClimateMachine.Topologies
using MPI
MPI.Init()
topology = CubedShellTopology(MPI.COMM_SELF, 10, Float64)
# Typically the warping would be done after the grid is created, but the cell
# corners could be warped with...
# Shell radius = 1
x1, x2, x3 = ntuple(j->topology.elemtocoord[j, :, :], 3)
for n = 1:length(x1)
x1[n], x2[n], x3[n] = Topologies.cubedshellwarp(x1[n], x2[n], x3[n])
end
# Shell radius = 10
x1, x2, x3 = ntuple(j->topology.elemtocoord[j, :, :], 3)
for n = 1:length(x1)
x1[n], x2[n], x3[n] = Topologies.cubedshellwarp(x1[n], x2[n], x3[n], 10)
end
```
"""
function CubedShellTopology(
mpicomm,
Neside,
T;
connectivity = :face,
ghostsize = 1,
)
# We cannot handle anything else right now...
@assert connectivity == :face
@assert ghostsize == 1
mpirank = MPI.Comm_rank(mpicomm)
mpisize = MPI.Comm_size(mpicomm)
topology = cubedshellmesh(Neside, part = mpirank + 1, numparts = mpisize)
topology = BrickMesh.partition(mpicomm, topology...)
origsendorder = topology[5]
dim, nvert = 3, 4
elemtovert = topology[1]
nelem = size(elemtovert, 2)
elemtocoord = Array{T}(undef, dim, nvert, nelem)
ind2vert = CartesianIndices((Neside + 1, Neside + 1, Neside + 1))
for e in 1:nelem
for n in 1:nvert
v = elemtovert[n, e]
i, j, k = Tuple(ind2vert[v])
elemtocoord[:, n, e] =
(2 * [i - 1, j - 1, k - 1] .- Neside) / Neside
end
end
topology = BrickMesh.connectmesh(
mpicomm,
topology[1],
elemtocoord,
topology[3],
topology[4];
dim = 2,
)
CubedShellTopology{T}(BoxElementTopology{2, T}(
mpicomm,
topology.elems,
topology.realelems,
topology.ghostelems,
topology.ghostfaces,
topology.sendelems,
topology.sendfaces,
topology.elemtocoord,
topology.elemtoelem,
topology.elemtoface,
topology.elemtoordr,
topology.elemtobndy,
topology.nabrtorank,
topology.nabrtorecv,
topology.nabrtosend,
origsendorder,
false,
))
end
"""
cubedshellmesh(T, Ne; part=1, numparts=1)
Generate a cubed mesh with each of the "cubes" has an `Ne X Ne` grid of
elements.
The mesh can optionally be partitioned into `numparts` and this returns
partition `part`. This is a simple Cartesian partition and further partitioning
(e.g, based on a space-filling curve) should be done before the mesh is used for
computation.
This mesh returns the cubed spehere in a flatten fashion for the vertex values,
and a remapping is needed to embed the mesh in a 3-D space.
The mesh structures for the cubes is as follows:
```
x2
^
|
4Ne- +-------+
| | |
| | 6 |
| | |
3Ne- +-------+
| | |
| | 5 |
| | |
2Ne- +-------+
| | |
| | 4 |
| | |
Ne- +-------+-------+-------+
| | | | |
| | 1 | 2 | 3 |
| | | | |
0- +-------+-------+-------+
|
+---|-------|-------|------|-> x1
0 Ne 2Ne 3Ne
```
"""
function cubedshellmesh(Ne; part = 1, numparts = 1)
dim = 2
@assert 1 <= part <= numparts
globalnelems = 6 * Ne^2
# How many vertices and faces per element
nvert = 2^dim # 4
nface = 2dim # 4
# linearly partition to figure out which elements we own
elemlocal = BrickMesh.linearpartition(prod(globalnelems), part, numparts)
# elemen to vertex maps which we own
elemtovert = Array{Int}(undef, nvert, length(elemlocal))
elemtocoord = Array{Int}(undef, dim, nvert, length(elemlocal))
nelemcube = Ne^dim # Ne^2
etoijb = CartesianIndices((Ne, Ne, 6))
bx = [0 Ne 2Ne Ne Ne Ne]
by = [0 0 0 Ne 2Ne 3Ne]
vertmap = LinearIndices((Ne + 1, Ne + 1, Ne + 1))
for (le, e) in enumerate(elemlocal)
i, j, blck = Tuple(etoijb[e])
elemtocoord[1, :, le] = bx[blck] .+ [i - 1 i i - 1 i]
elemtocoord[2, :, le] = by[blck] .+ [j - 1 j - 1 j j]
for n in 1:4
ix = i + mod(n - 1, 2)
jx = j + div(n - 1, 2)
# set the vertices like they are the face vertices of a cube
if blck == 1
elemtovert[n, le] = vertmap[1, Ne + 2 - ix, jx]
elseif blck == 2
elemtovert[n, le] = vertmap[ix, 1, jx]
elseif blck == 3
elemtovert[n, le] = vertmap[Ne + 1, ix, jx]
elseif blck == 4
elemtovert[n, le] = vertmap[ix, jx, Ne + 1]
elseif blck == 5
elemtovert[n, le] = vertmap[ix, Ne + 1, Ne + 2 - jx]
elseif blck == 6
elemtovert[n, le] = vertmap[ix, Ne + 2 - jx, 1]
end
end
end
# no boundaries for a shell
elemtobndy = zeros(Int, nface, length(elemlocal))
# no faceconnections for a shell
faceconnections = Array{Array{Int, 1}}(undef, 0)
(elemtovert, elemtocoord, elemtobndy, faceconnections, collect(elemlocal))
end
"""
cubedshellwarp(a, b, c, R = max(abs(a), abs(b), abs(c)))
Given points `(a, b, c)` on the surface of a cube, warp the points out to a
spherical shell of radius `R` based on the equiangular gnomonic grid proposed by
Ronchi, Iacono, Paolucci (1996) https://linkinghub.elsevier.com/retrieve/pii/S0021999196900479
```
@article{RonchiIaconoPaolucci1996,
title={The ``cubed sphere'': a new method for the solution of partial
differential equations in spherical geometry},
author={Ronchi, C. and Iacono, R. and Paolucci, P. S.},
journal={Journal of Computational Physics},
volume={124},
number={1},
pages={93--114},
year={1996},
doi={10.1006/jcph.1996.0047}
}
```
"""
function cubedshellwarp(a, b, c, R = max(abs(a), abs(b), abs(c)))
function f(sR, ξ, η)
X, Y = tan(π * ξ / 4), tan(π * η / 4)
x1 = sR / sqrt(X^2 + Y^2 + 1)
x2, x3 = X * x1, Y * x1
x1, x2, x3
end
fdim = argmax(abs.((a, b, c)))
if fdim == 1 && a < 0
# (-R, *, *) : Face I from Ronchi, Iacono, Paolucci (1996)
x1, x2, x3 = f(-R, b / a, c / a)
elseif fdim == 2 && b < 0
# ( *,-R, *) : Face II from Ronchi, Iacono, Paolucci (1996)
x2, x1, x3 = f(-R, a / b, c / b)
elseif fdim == 1 && a > 0
# ( R, *, *) : Face III from Ronchi, Iacono, Paolucci (1996)
x1, x2, x3 = f(R, b / a, c / a)
elseif fdim == 2 && b > 0
# ( *, R, *) : Face IV from Ronchi, Iacono, Paolucci (1996)
x2, x1, x3 = f(R, a / b, c / b)
elseif fdim == 3 && c > 0
# ( *, *, R) : Face V from Ronchi, Iacono, Paolucci (1996)
x3, x2, x1 = f(R, b / c, a / c)
elseif fdim == 3 && c < 0
# ( *, *,-R) : Face VI from Ronchi, Iacono, Paolucci (1996)
x3, x2, x1 = f(-R, b / c, a / c)
else
error("invalid case for cubedshellwarp: $a, $b, $c")
end
return x1, x2, x3
end
"""
cubedshellunwarp(x1, x2, x3)
The inverse of [`cubedshellwarp`](@ref).
"""
function cubedshellunwarp(x1, x2, x3)
function g(R, X, Y)
ξ = atan(X) * 4 / pi
η = atan(Y) * 4 / pi
R, R * ξ, R * η
end
R = hypot(x1, x2, x3)
fdim = argmax(abs.((x1, x2, x3)))
if fdim == 1 && x1 < 0
# (-R, *, *) : Face I from Ronchi, Iacono, Paolucci (1996)
a, b, c = g(-R, x2 / x1, x3 / x1)
elseif fdim == 2 && x2 < 0
# ( *,-R, *) : Face II from Ronchi, Iacono, Paolucci (1996)
b, a, c = g(-R, x1 / x2, x3 / x2)
elseif fdim == 1 && x1 > 0
# ( R, *, *) : Face III from Ronchi, Iacono, Paolucci (1996)
a, b, c = g(R, x2 / x1, x3 / x1)
elseif fdim == 2 && x2 > 0
# ( *, R, *) : Face IV from Ronchi, Iacono, Paolucci (1996)
b, a, c = g(R, x1 / x2, x3 / x2)