/
multi_region_cubed_sphere_grid.jl
398 lines (334 loc) · 19.1 KB
/
multi_region_cubed_sphere_grid.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
using Oceananigans.Architectures: architecture
using Oceananigans.Grids: conformal_cubed_sphere_panel,
R_Earth,
halo_size,
size_summary,
total_length,
topology
using CubedSphere
using Distances
import Oceananigans.Grids: grid_name
const ConformalCubedSphereGrid{FT, TX, TY, TZ} = MultiRegionGrid{FT, TX, TY, TZ, <:CubedSpherePartition}
"""
ConformalCubedSphereGrid(arch=CPU(), FT=Float64;
panel_size,
z,
horizontal_direction_halo = 1,
z_halo = horizontal_direction_halo,
horizontal_topology = FullyConnected,
z_topology = Bounded,
radius = R_Earth,
partition = CubedSpherePartition(; R = 1),
devices = nothing)
Return a `ConformalCubedSphereGrid` that comprises of six [`conformal_cubed_sphere_panel`](@ref)
grids; we refer to each of these grids as a "panel". Each panel corresponds to a face of the cube.
The keyword arguments prescribe the properties of each of the panels. Only the topology in
the vertical direction can be prescribed and that's done via the `z_topology` keyword
argumet (default: `Bounded`). Topologies in both horizontal directions for a `ConformalCubedSphereGrid`
are _always_ [`FullyConnected`](@ref).
Halo size in both horizontal dimensions _must_ be equal; this is prescribed via the
`horizontal_halo :: Integer` keyword argument. The number of halo points in the ``z``-direction
is prescribed by the `z_halo :: Integer` keyword argument.
The connectivity between the `ConformalCubedSphereGrid` panels is depicted below.
```
+==========+==========+
∥ ↑ ∥ ↑ ∥
∥ 1W ∥ 1S ∥
∥←3N P5 6W→∥←5E P6 2S→∥
∥ 4N ∥ 4E ∥
∥ ↓ ∥ ↓ ∥
+==========+==========+==========+
∥ ↑ ∥ ↑ ∥
∥ 5W ∥ 5S ∥
∥←1N P3 4W→∥←3E P4 6S→∥
∥ 2N ∥ 2E ∥
∥ ↓ ∥ ↓ ∥
+==========+==========+==========+
∥ ↑ ∥ ↑ ∥
∥ 3W ∥ 3S ∥
∥←5N P1 2W→∥←1E P2 4S→∥
∥ 6N ∥ 6E ∥
∥ ↓ ∥ ↓ ∥
+==========+==========+
```
The North Pole of the sphere lies in the center of panel 3 (P3) and the South Pole
in the center of panel 6 (P6).
The `partition` keyword argument prescribes the partitioning in regions within each
panel; see [`CubedSpherePartition`](@ref). For example, a `CubedSpherePartition(; R=2)`
implies that each of the panels are partitioned into 2 regions in each dimension;
this adds up, e.g., to 24 regions for the whole sphere. In the depiction below,
the intra-panel `x, y` indices are depicted in the center of each region and the overall
region index is shown at the bottom right of each region.
```
+==========+==========+==========+==========+
∥ ↑ | ↑ ∥ ↑ | ↑ ∥
∥ | ∥ | ∥
∥← (1, 2) →|← (2, 2) →∥← (1, 2) →|← (2, 2) →∥
∥ | ∥ | ∥
∥ ↓ 19 | ↓ 20 ∥ ↓ 23 | ↓ 24 ∥
+-------- P 5 --------+-------- P 6 --------+
∥ ↑ | ↑ ∥ ↑ | ↑ ∥
∥ | ∥ | ∥
∥← (1, 1) →|← (2, 1) →∥← (1, 1) →|← (2, 1) →∥
∥ | ∥ | ∥
∥ ↓ 17 | ↓ 18 ∥ ↓ 21 | ↓ 22 ∥
+==========+==========+==========+==========+==========+==========+
∥ ↑ | ↑ ∥ ↑ | ↑ ∥
∥ | ∥ | ∥
∥← (1, 2) →|← (2, 2) →∥← (1, 2) →|← (2, 2) →∥
∥ | ∥ | ∥
∥ ↓ 11 | ↓ 12 ∥ ↓ 15 | ↓ 16 ∥
+-------- P 3 --------+-------- P 4 --------+
∥ ↑ | ↑ ∥ ↑ | ↑ ∥
∥ | ∥ | ∥
∥← (1, 1) →|← (2, 1) →∥← (1, 1) →|← (2, 1) →∥
∥ | ∥ | ∥
∥ ↓ 9 | ↓ 10 ∥ ↓ 13 | ↓ 14 ∥
+==========+==========+==========+==========+==========+==========+
∥ ↑ | ↑ ∥ ↑ | ↑ ∥
∥ | ∥ | ∥
∥← (1, 2) →|← (2, 2) →∥← (1, 2) →|← (2, 2) →∥
∥ | ∥ | ∥
∥ ↓ 3 | ↓ 4 ∥ ↓ 7 | ↓ 8 ∥
+-------- P 1 --------+-------- P 2 --------+
∥ ↑ | ↑ ∥ ↑ | ↑ ∥
∥ | ∥ | ∥
∥← (1, 1) →|← (2, 1) →∥← (1, 1) →|← (2, 1) →∥
∥ | ∥ | ∥
∥ ↓ 1 | ↓ 2 ∥ ↓ 5 | ↓ 6 ∥
+==========+==========+==========+==========+
```
Below, we show in detail panels 1 and 2 and the connectivity
of each panel.
```
+===============+==============+==============+===============+
∥ ↑ | ↑ ∥ ↑ | ↑ ∥
∥ 11W | 9W ∥ 9S | 10S ∥
∥←19N (2, 1) 4W→|←3E (2, 2) 7W→∥←4E (2, 1) 8W→|←7E (2, 2) 13S→∥
∥ 1N | 2N ∥ 5N | 6N ∥
∥ ↓ 3 | ↓ 4 ∥ ↓ 7 | ↓ 8 ∥
+------------- P 1 ------------+------------ P 2 -------------+
∥ ↑ | ↑ ∥ ↑ | ↑ ∥
∥ 3S | 4S ∥ 7S | 8S ∥
∥←20N (1, 1) 2W→|←1E (2, 1) 5W→∥←2E (1, 1) 6W→|←5E (2, 1) 14S→∥
∥ 23N | 24N ∥ 24N | 22N ∥
∥ ↓ 1 | ↓ 2 ∥ ↓ 5 | ↓ 6 ∥
+===============+==============+==============+===============+
```
Example
=======
```jldoctest cubedspheregrid
julia> using Oceananigans
julia> grid = ConformalCubedSphereGrid(panel_size=(12, 12, 1), z=(-1, 0), radius=1)
ConformalCubedSphereGrid{Float64, FullyConnected, FullyConnected, Bounded} partitioned on CPU():
├── grids: 12×12×1 OrthogonalSphericalShellGrid{Float64, FullyConnected, FullyConnected, Bounded} on CPU with 3×3×3 halo and with precomputed metrics
├── partitioning: CubedSpherePartition with (1 region in each panel)
├── connectivity: CubedSphereConnectivity
└── devices: (CPU(), CPU(), CPU(), CPU(), CPU(), CPU())
```
The connectivities of the regions of our grid are stored in `grid.connectivity`.
For example, to find out all connectivites on the South boundary of each region we call
```jldoctest cubedspheregrid
julia> using Oceananigans.MultiRegion: East, North, West, South
julia> for region in 1:length(grid); println(grid.connectivity.connections[region].south); end
CubedSphereRegionalConnectivity
├── from: Oceananigans.MultiRegion.North side, region 6
├── to: Oceananigans.MultiRegion.South side, region 1
└── no rotation
CubedSphereRegionalConnectivity
├── from: Oceananigans.MultiRegion.East side, region 6
├── to: Oceananigans.MultiRegion.South side, region 2
└── counter-clockwise rotation ↺
CubedSphereRegionalConnectivity
├── from: Oceananigans.MultiRegion.North side, region 2
├── to: Oceananigans.MultiRegion.South side, region 3
└── no rotation
CubedSphereRegionalConnectivity
├── from: Oceananigans.MultiRegion.East side, region 2
├── to: Oceananigans.MultiRegion.South side, region 4
└── counter-clockwise rotation ↺
CubedSphereRegionalConnectivity
├── from: Oceananigans.MultiRegion.North side, region 4
├── to: Oceananigans.MultiRegion.South side, region 5
└── no rotation
CubedSphereRegionalConnectivity
├── from: Oceananigans.MultiRegion.East side, region 4
├── to: Oceananigans.MultiRegion.South side, region 6
└── counter-clockwise rotation ↺
```
"""
function ConformalCubedSphereGrid(arch::AbstractArchitecture=CPU(), FT=Float64;
panel_size,
z,
horizontal_direction_halo = 3,
z_halo = horizontal_direction_halo,
horizontal_topology = FullyConnected,
z_topology = Bounded,
radius = R_Earth,
partition = CubedSpherePartition(; R = 1),
devices = nothing)
Nx, Ny, _ = panel_size
region_topology = (horizontal_topology, horizontal_topology, z_topology)
region_halo = (horizontal_direction_halo, horizontal_direction_halo, z_halo)
Nx !== Ny && error("Horizontal sizes for ConformalCubedSphereGrid must be equal; Nx=Ny.")
devices = validate_devices(partition, arch, devices)
devices = assign_devices(partition, devices)
connectivity = CubedSphereConnectivity(devices, partition)
region_size = []
region_η = []
region_ξ = []
region_rotation = []
for r in 1:length(partition)
Lξ_total, Lη_total = 2, 2 # a cube's face has (ξ, η) ∈ [-1, 1] x [-1, 1]
Lξᵢⱼ = Lξ_total / Rx(r, partition)
Lηᵢⱼ = Lη_total / Ry(r, partition)
pᵢ = intra_panel_index_x(r, partition)
pⱼ = intra_panel_index_y(r, partition)
push!(region_size, (panel_size[1] ÷ Rx(r, partition), panel_size[2] ÷ Ry(r, partition), panel_size[3]))
push!(region_ξ, (-1 + Lξᵢⱼ * (pᵢ - 1), -1 + Lξᵢⱼ * pᵢ))
push!(region_η, (-1 + Lηᵢⱼ * (pⱼ - 1), -1 + Lηᵢⱼ * pⱼ))
push!(region_rotation, connectivity.rotations[panel_index(r, partition)])
end
region_size = MultiRegionObject(tuple(region_size...), devices)
region_ξ = Iterate(region_ξ)
region_η = Iterate(region_η)
region_rotation = Iterate(region_rotation)
region_grids = construct_regionally(conformal_cubed_sphere_panel, arch, FT;
size = region_size,
z,
halo = region_halo,
topology = region_topology,
radius,
ξ = region_ξ,
η = region_η,
rotation = region_rotation)
grid = MultiRegionGrid{FT, region_topology...}(arch,
partition,
connectivity,
region_grids,
devices)
fields = (:λᶜᶜᵃ, :φᶜᶜᵃ, :Azᶜᶜᵃ , :λᶠᶠᵃ, :φᶠᶠᵃ, :Azᶠᶠᵃ)
LXs = (:Center, :Center, :Center, :Face, :Face, :Face )
LYs = (:Center, :Center, :Center, :Face, :Face, :Face )
for (field, LX, LY) in zip(fields, LXs, LYs)
expr = quote
$(Symbol(field)) = Field{$(Symbol(LX)), $(Symbol(LY)), Nothing}($(grid))
CUDA.@allowscalar begin
for region in 1:number_of_regions($(grid))
getregion($(Symbol(field)), region).data .= getregion($(grid), region).$(Symbol(field))
end
end
if $(horizontal_topology) == FullyConnected
fill_halo_regions!($(Symbol(field)))
end
CUDA.@allowscalar begin
for region in 1:number_of_regions($(grid))
getregion($(grid), region).$(Symbol(field)) .= getregion($(Symbol(field)), region).data
end
end
end # quote
eval(expr)
end
fields_1 = (:Δxᶜᶜᵃ, :Δxᶠᶜᵃ, :Δyᶠᶜᵃ, :λᶠᶜᵃ, :φᶠᶜᵃ, :Azᶠᶜᵃ , :Δxᶠᶠᵃ)
LXs_1 = (:Center, :Face, :Face, :Face, :Face, :Face , :Face )
LYs_1 = (:Center, :Center, :Center, :Center, :Center, :Center, :Face )
fields_2 = (:Δyᶜᶜᵃ, :Δyᶜᶠᵃ, :Δxᶜᶠᵃ, :λᶜᶠᵃ, :φᶜᶠᵃ, :Azᶜᶠᵃ , :Δyᶠᶠᵃ)
LXs_2 = (:Center, :Center, :Center, :Center, :Center, :Center, :Face )
LYs_2 = (:Center, :Face, :Face, :Face, :Face, :Face , :Face )
for (field_1, LX_1, LY_1, field_2, LX_2, LY_2) in zip(fields_1, LXs_1, LYs_1, fields_2, LXs_2, LYs_2)
expr = quote
$(Symbol(field_1)) = Field{$(Symbol(LX_1)), $(Symbol(LY_1)), Nothing}($(grid))
$(Symbol(field_2)) = Field{$(Symbol(LX_2)), $(Symbol(LY_2)), Nothing}($(grid))
CUDA.@allowscalar begin
for region in 1:number_of_regions($(grid))
getregion($(Symbol(field_1)), region).data .= getregion($(grid), region).$(Symbol(field_1))
getregion($(Symbol(field_2)), region).data .= getregion($(grid), region).$(Symbol(field_2))
end
end
if $(horizontal_topology) == FullyConnected
fill_halo_regions!(($(Symbol(field_1)), $(Symbol(field_2))); signed = false)
end
CUDA.@allowscalar begin
for region in 1:number_of_regions($(grid))
getregion($(grid), region).$(Symbol(field_1)) .= getregion($(Symbol(field_1)), region).data
getregion($(grid), region).$(Symbol(field_2)) .= getregion($(Symbol(field_2)), region).data
end
end
end # quote
eval(expr)
end
CUDA.@allowscalar begin
# hardcoding NW/SE corner values only works for a one-region-per panel partition
number_of_regions(grid) !== 6 && error("requires cubed sphere grids with 1 region per panel")
for region in 1:number_of_regions(grid)
if isodd(region)
# Coordinates of "missing" NW corner points on odd panels can't be read from the interior
# so we compute them via conformal_cubed_sphere_mapping
φc, λc = cartesian_to_lat_lon(conformal_cubed_sphere_mapping(1, -1)...)
getregion(grid, region).φᶠᶠᵃ[1, Ny+1] = φc
getregion(grid, region).λᶠᶠᵃ[1, Ny+1] = λc
elseif iseven(region)
# Coordinates of "missing" SE corner points on even panels can't be read from the interior
# so we compute them via conformal_cubed_sphere_mapping
φc, λc = -1 .* cartesian_to_lat_lon(conformal_cubed_sphere_mapping(-1, -1)...)
getregion(grid, region).φᶠᶠᵃ[Nx+1, 1] = φc
getregion(grid, region).λᶠᶠᵃ[Nx+1, 1] = λc
end
end
for region in 1:number_of_regions(grid)
getregion(grid, region).λᶜᶜᵃ[getregion(grid, region).λᶜᶜᵃ .== -180] .= 180
getregion(grid, region).λᶠᶜᵃ[getregion(grid, region).λᶠᶜᵃ .== -180] .= 180
getregion(grid, region).λᶜᶠᵃ[getregion(grid, region).λᶜᶠᵃ .== -180] .= 180
getregion(grid, region).λᶠᶠᵃ[getregion(grid, region).λᶠᶠᵃ .== -180] .= 180
end
end # CUDA.@allowscalar
return grid
end
"""
ConformalCubedSphereGrid(filepath::AbstractString, arch::AbstractArchitecture=CPU(), FT=Float64;
Nz,
z,
panel_halo = (4, 4, 4),
panel_topology = (FullyConnected, FullyConnected, Bounded),
radius = R_Earth,
devices = nothing)
Load a `ConformalCubedSphereGrid` from `filepath`.
"""
function ConformalCubedSphereGrid(filepath::AbstractString, arch::AbstractArchitecture=CPU(), FT=Float64;
Nz,
z,
panel_halo = (4, 4, 4),
panel_topology = (FullyConnected, FullyConnected, Bounded),
radius = R_Earth,
devices = nothing)
# only 6-panel partition, i.e. R = 1, are allowed when loading a ConformalCubedSphereGrid from file
partition = CubedSpherePartition(R = 1)
devices = validate_devices(partition, arch, devices)
devices = assign_devices(partition, devices)
region_Nz = MultiRegionObject(Tuple(repeat([Nz], length(partition))), devices)
region_panels = Iterate(Array(1:length(partition)))
region_grids = construct_regionally(conformal_cubed_sphere_panel, filepath, arch, FT;
Nz = region_Nz,
z,
panel = region_panels,
topology = panel_topology,
halo = panel_halo,
radius)
connectivity = CubedSphereConnectivity(devices, partition)
return MultiRegionGrid{FT, panel_topology...}(arch, partition, connectivity, region_grids, devices)
end
function with_halo(new_halo, csg::ConformalCubedSphereGrid)
region_rotation = []
for region in 1:length(csg.partition)
push!(region_rotation, csg[region].conformal_mapping.rotation)
end
apply_regionally!(with_halo, new_halo, csg; rotation = Iterate(region_rotation))
return csg
end
function Base.summary(grid::ConformalCubedSphereGrid{FT, TX, TY, TZ}) where {FT, TX, TY, TZ}
return string(size_summary(size(grid)),
" ConformalCubedSphereGrid{$FT, $TX, $TY, $TZ} on ", summary(architecture(grid)),
" with ", size_summary(halo_size(grid)), " halo")
end
radius(mrg::ConformalCubedSphereGrid) = first(mrg).radius
grid_name(mrg::ConformalCubedSphereGrid) = "ConformalCubedSphereGrid"