-
Notifications
You must be signed in to change notification settings - Fork 191
/
conformal_cubed_sphere_grid.jl
359 lines (279 loc) · 18.4 KB
/
conformal_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
using Rotations
using Suppressor
using Oceananigans.Grids
using Oceananigans.Grids: R_Earth, interior_indices
import Base: show, size, eltype
import Oceananigans.Grids: topology, architecture, halo_size, on_architecture, size_summary
struct CubedSpherePanelConnectivityDetails{P, S}
panel :: P
side :: S
end
short_string(deets::CubedSpherePanelConnectivityDetails) = "panel $(deets.panel) $(deets.side) side"
Base.show(io::IO, deets::CubedSpherePanelConnectivityDetails) =
print(io, "CubedSpherePanelConnectivityDetails: $(short_string(deets))")
struct CubedSpherePanelConnectivity{W, E, S, N}
west :: W
east :: E
south :: S
north :: N
end
CubedSpherePanelConnectivity(; west, east, south, north) =
CubedSpherePanelConnectivity(west, east, south, north)
function Base.show(io::IO, connectivity::CubedSpherePanelConnectivity)
print(io, "CubedSpherePanelConnectivity:\n",
"├── west: $(short_string(connectivity.west))\n",
"├── east: $(short_string(connectivity.east))\n",
"├── south: $(short_string(connectivity.south))\n",
"└── north: $(short_string(connectivity.north))")
end
function default_panel_connectivity()
# Adopted from figure 8.4 of https://mitgcm.readthedocs.io/en/latest/phys_pkgs/exch2.html?highlight=cube%20sphere#fig-6tile
#
# panel P5 panel P6
# +----------+----------+
# | ↑↑ | ↑↑ |
# | 1W | 1S |
# |←3N P5 6W→|←5E P6 2S→|
# | 4N | 4E |
# panel P3 | ↓↓ | ↓↓ |
# +----------+----------+----------+
# | ↑↑ | ↑↑ |
# | 5W | 5S |
# |←1N P3 4W→|←3E P4 6S→|
# | 2N | 2E |
# | ↓↓ | ↓↓ |
# +----------+----------+----------+
# | ↑↑ | ↑↑ | panel P4
# | 3W | 3S |
# |←5N P1 2W→|←1E P2 4S→|
# | 6N | 6E |
# | ↓↓ | ↓↓ |
# +----------+----------+
# panel P1 panel P2
panel1_connectivity = CubedSpherePanelConnectivity(
west = CubedSpherePanelConnectivityDetails(5, :north),
east = CubedSpherePanelConnectivityDetails(2, :west),
south = CubedSpherePanelConnectivityDetails(6, :north),
north = CubedSpherePanelConnectivityDetails(3, :west),
)
panel2_connectivity = CubedSpherePanelConnectivity(
west = CubedSpherePanelConnectivityDetails(1, :east),
east = CubedSpherePanelConnectivityDetails(4, :south),
south = CubedSpherePanelConnectivityDetails(6, :east),
north = CubedSpherePanelConnectivityDetails(3, :south),
)
panel3_connectivity = CubedSpherePanelConnectivity(
west = CubedSpherePanelConnectivityDetails(1, :north),
east = CubedSpherePanelConnectivityDetails(4, :west),
south = CubedSpherePanelConnectivityDetails(2, :north),
north = CubedSpherePanelConnectivityDetails(5, :west),
)
panel4_connectivity = CubedSpherePanelConnectivity(
west = CubedSpherePanelConnectivityDetails(3, :east),
east = CubedSpherePanelConnectivityDetails(6, :south),
south = CubedSpherePanelConnectivityDetails(2, :east),
north = CubedSpherePanelConnectivityDetails(5, :south),
)
panel5_connectivity = CubedSpherePanelConnectivity(
west = CubedSpherePanelConnectivityDetails(3, :north),
east = CubedSpherePanelConnectivityDetails(6, :west),
south = CubedSpherePanelConnectivityDetails(4, :north),
north = CubedSpherePanelConnectivityDetails(1, :west),
)
panel6_connectivity = CubedSpherePanelConnectivity(
west = CubedSpherePanelConnectivityDetails(5, :east),
east = CubedSpherePanelConnectivityDetails(2, :south),
south = CubedSpherePanelConnectivityDetails(4, :east),
north = CubedSpherePanelConnectivityDetails(1, :south),
)
panel_connectivity = (
panel1_connectivity,
panel2_connectivity,
panel3_connectivity,
panel4_connectivity,
panel5_connectivity,
panel6_connectivity
)
return panel_connectivity
end
# Note: I think we want to keep panels and panel_connectivity tuples
# so it's easy to support an arbitrary number of panels.
struct ConformalCubedSphereGrid{FT, F, C, Arch} <: AbstractHorizontallyCurvilinearGrid{FT, FullyConnected, FullyConnected, Bounded, Arch}
architecture :: Arch
panels :: F
panel_connectivity :: C
end
function ConformalCubedSphereGrid(arch = CPU(), FT=Float64;
panel_size, z,
panel_halo = (1, 1, 1),
panel_topology = (FullyConnected, FullyConnected, Bounded),
radius = R_Earth)
@warn "ConformalCubedSphereGrid is experimental: use with caution!"
size, halo, topology = panel_size, panel_halo, panel_topology
panel_kwargs = (; z, size, topology, halo, radius)
# +z panel (panel 1)
z⁺_panel_grid = @suppress OrthogonalSphericalShellGrid(arch, FT; rotation=nothing, panel_kwargs...)
# +x panel (panel 2)
x⁺_panel_grid = @suppress OrthogonalSphericalShellGrid(arch, FT; rotation=RotX(π/2), panel_kwargs...)
# +y panel (panel 3)
y⁺_panel_grid = @suppress OrthogonalSphericalShellGrid(arch, FT; rotation=RotY(π/2), panel_kwargs...)
# -x panel (panel 4)
x⁻_panel_grid = @suppress OrthogonalSphericalShellGrid(arch, FT; rotation=RotX(-π/2), panel_kwargs...)
# -y panel (panel 5)
y⁻_panel_grid = @suppress OrthogonalSphericalShellGrid(arch, FT; rotation=RotY(-π/2), panel_kwargs...)
# -z panel (panel 6)
z⁻_panel_grid = @suppress OrthogonalSphericalShellGrid(arch, FT; rotation=RotX(π), panel_kwargs...)
panels = (
z⁺_panel_grid,
x⁺_panel_grid,
y⁺_panel_grid,
x⁻_panel_grid,
y⁻_panel_grid,
z⁻_panel_grid
)
panel_connectivity = default_panel_connectivity()
return ConformalCubedSphereGrid{FT, typeof(panels), typeof(panel_connectivity), typeof(arch)}(arch, panels, panel_connectivity)
end
function ConformalCubedSphereGrid(filepath::AbstractString, arch = CPU(), FT=Float64; Nz, z, radius = R_Earth, halo = (1, 1, 1))
@warn "ConformalCubedSphereGrid is experimental: use with caution!"
panel_topo = (FullyConnected, FullyConnected, Bounded)
panel_kwargs = (; Nz, z, topology=panel_topo, radius, halo)
panels = Tuple(OrthogonalSphericalShellGrid(filepath, arch, FT; panel=n, panel_kwargs...) for n in 1:6)
panel_connectivity = default_panel_connectivity()
grid = ConformalCubedSphereGrid{FT, typeof(panels), typeof(panel_connectivity), typeof(arch)}(arch, panels, panel_connectivity)
fill_grid_metric_halos!(grid)
fill_grid_metric_halos!(grid)
return grid
end
Base.summary(grid::OrthogonalSphericalShellGrid{FT, FullyConnected, FullyConnected, TZ}) where {FT, TZ} =
string(size_summary(size(grid)),
" OrthogonalSphericalShellGrid with topology (FullyConnected, FullyConnected, $TZ)",
" and with ", size_summary(halo_size(grid)), " halo")
function Base.summary(grid::ConformalCubedSphereGrid)
Nx, Ny, Nz, Nf = size(grid)
FT = eltype(grid)
metric_computation = isnothing(grid.panels[1].Δxᶠᶜᵃ) ? "without precomputed metrics" : "with precomputed metrics"
return string(size_summary(size(grid)), " × $Nf panels",
" ConformalCubedSphereGrid{$FT} on ", summary(architecture(grid)),
" ", metric_computation)
end
function Base.show(io::IO, grid::ConformalCubedSphereGrid, withsummary=true)
if withsummary
print(io, summary(grid), "\n")
end
return print(io, "| Panels: \n",
"├── ", summary(grid.panels[1]), "\n",
"├── ", summary(grid.panels[2]), "\n",
"├── ", summary(grid.panels[3]), "\n",
"├── ", summary(grid.panels[4]), "\n",
"├── ", summary(grid.panels[5]), "\n",
"└── ", summary(grid.panels[6]))
end
#####
##### Nodes for OrthogonalSphericalShellGrid
#####
@inline λnode(LX::Face, LY::Face, LZ, i, j, k, grid::OrthogonalSphericalShellGrid) = @inbounds grid.λᶠᶠᵃ[i, j]
@inline λnode(LX::Face, LY::Center, LZ, i, j, k, grid::OrthogonalSphericalShellGrid) = @inbounds grid.λᶠᶜᵃ[i, j]
@inline λnode(LX::Center, LY::Face, LZ, i, j, k, grid::OrthogonalSphericalShellGrid) = @inbounds grid.λᶜᶠᵃ[i, j]
@inline λnode(LX::Center, LY::Center, LZ, i, j, k, grid::OrthogonalSphericalShellGrid) = @inbounds grid.λᶜᶜᵃ[i, j]
@inline φnode(LX::Face, LY::Face, LZ, i, j, k, grid::OrthogonalSphericalShellGrid) = @inbounds grid.φᶠᶠᵃ[i, j]
@inline φnode(LX::Face, LY::Center, LZ, i, j, k, grid::OrthogonalSphericalShellGrid) = @inbounds grid.φᶠᶜᵃ[i, j]
@inline φnode(LX::Center, LY::Face, LZ, i, j, k, grid::OrthogonalSphericalShellGrid) = @inbounds grid.φᶜᶠᵃ[i, j]
@inline φnode(LX::Center, LY::Center, LZ, i, j, k, grid::OrthogonalSphericalShellGrid) = @inbounds grid.φᶜᶜᵃ[i, j]
@inline znode(LX, LY, LZ::Face, i, j, k, grid::OrthogonalSphericalShellGrid) = @inbounds grid.zᵃᵃᶠ[k]
@inline znode(LX, LY, LZ::Center, i, j, k, grid::OrthogonalSphericalShellGrid) = @inbounds grid.zᵃᵃᶜ[k]
λnodes(LX::Face, LY::Face, LZ, grid::OrthogonalSphericalShellGrid{TX, TY}) where {TX, TY} =
view(grid.λᶠᶠᵃ, interior_indices(LX, TX, grid.Nx), interior_indices(LY, TY, grid.Ny))
λnodes(LX::Face, LY::Center, LZ, grid::OrthogonalSphericalShellGrid{TX, TY}) where {TX, TY} =
view(grid.λᶠᶜᵃ, interior_indices(LX, TX, grid.Nx), interior_indices(LY, TY, grid.Ny))
λnodes(LX::Center, LY::Face, LZ, grid::OrthogonalSphericalShellGrid{TX, TY}) where {TX, TY} =
view(grid.λᶜᶠᵃ, interior_indices(LX, TX, grid.Nx), interior_indices(LY, TY, grid.Ny))
λnodes(LX::Center, LY::Center, LZ, grid::OrthogonalSphericalShellGrid{TX, TY}) where {TX, TY} =
view(grid.λᶜᶜᵃ, interior_indices(LX, TX, grid.Nx), interior_indices(LY, TY, grid.Ny))
φnodes(LX::Face, LY::Face, LZ, grid::OrthogonalSphericalShellGrid{TX, TY}) where {TX, TY} =
view(grid.φᶠᶠᵃ, interior_indices(LX, TX, grid.Nx), interior_indices(LY, TY, grid.Ny))
φnodes(LX::Face, LY::Center, LZ, grid::OrthogonalSphericalShellGrid{TX, TY}) where {TX, TY} =
view(grid.φᶠᶜᵃ, interior_indices(LX, TX, grid.Nx), interior_indices(LY, TY, grid.Ny))
φnodes(LX::Center, LY::Face, LZ, grid::OrthogonalSphericalShellGrid{TX, TY}) where {TX, TY} =
view(grid.φᶜᶠᵃ, interior_indices(LX, TX, grid.Nx), interior_indices(LY, TY, grid.Ny))
φnodes(LX::Center, LY::Center, LZ, grid::OrthogonalSphericalShellGrid{TX, TY}) where {TX, TY} =
view(grid.φᶜᶜᵃ, interior_indices(LX, TX, grid.Nx), interior_indices(LY, TY, grid.Ny))
#####
##### Grid utils
#####
Base.size(grid::ConformalCubedSphereGrid) = (size(grid.panels[1])..., length(grid.panels))
Base.size(loc, grid::ConformalCubedSphereGrid) = size(loc, grid.panels[1])
Base.size(grid::ConformalCubedSphereGrid, i) = size(grid)[i]
halo_size(ccsg::ConformalCubedSphereGrid) = halo_size(first(ccsg.panels)) # hack
Base.eltype(grid::ConformalCubedSphereGrid{FT}) where FT = FT
topology(::ConformalCubedSphereGrid) = (Bounded, Bounded, Bounded)
topology(grid::ConformalCubedSphereGrid, i) = topology(grid)[i]
architecture(grid::ConformalCubedSphereGrid) = grid.architecture
function on_architecture(arch, grid::ConformalCubedSphereGrid)
panels = Tuple(on_architecture(arch, grid.panels[n]) for n in 1:6)
panel_connectivity = grid.panel_connectivity
FT = eltype(grid)
return ConformalCubedSphereGrid{FT, typeof(panels), typeof(panel_connectivity), typeof(arch)}(arch, panels, panel_connectivity)
end
#####
##### filling grid halos
#####
function grid_metric_halo(grid_metric, grid, location, topo, side)
LX, LY = location
TX, TY = topo
side == :west && return underlying_west_halo(grid_metric, grid, LX, TX)
side == :east && return underlying_east_halo(grid_metric, grid, LX, TX)
side == :south && return underlying_south_halo(grid_metric, grid, LY, TY)
side == :north && return underlying_north_halo(grid_metric, grid, LY, TY)
end
function grid_metric_boundary(grid_metric, grid, location, topo, side)
LX, LY = location
TX, TY = topo
side == :west && return underlying_west_boundary(grid_metric, grid, LX, TX)
side == :east && return underlying_east_boundary(grid_metric, grid, LX, TX)
side == :south && return underlying_south_boundary(grid_metric, grid, LY, TY)
side == :north && return underlying_north_boundary(grid_metric, grid, LY, TY)
end
function fill_grid_metric_halos!(grid)
topo_bb = (Bounded, Bounded)
loc_cc = (Center, Center)
loc_cf = (Center, Face )
loc_fc = (Face, Center)
loc_ff = (Face, Face )
for panel_number in 1:6, side in (:west, :east, :south, :north)
connectivity_info = getproperty(grid.panel_connectivity[panel_number], side)
src_panel_number = connectivity_info.panel
src_side = connectivity_info.side
grid_panel = grid.panels[panel_number]
src_grid_panel = grid.panels[src_panel_number]
if sides_in_the_same_dimension(side, src_side)
grid_metric_halo(grid_panel.Δxᶜᶜᵃ, grid_panel, loc_cc, topo_bb, side) .= grid_metric_boundary(grid_panel.Δxᶜᶜᵃ, src_grid_panel, loc_cc, topo_bb, src_side)
grid_metric_halo(grid_panel.Δyᶜᶜᵃ, grid_panel, loc_cc, topo_bb, side) .= grid_metric_boundary(grid_panel.Δyᶜᶜᵃ, src_grid_panel, loc_cc, topo_bb, src_side)
grid_metric_halo(grid_panel.Azᶜᶜᵃ, grid_panel, loc_cc, topo_bb, side) .= grid_metric_boundary(grid_panel.Azᶜᶜᵃ, src_grid_panel, loc_cc, topo_bb, src_side)
grid_metric_halo(grid_panel.Δxᶜᶠᵃ, grid_panel, loc_cf, topo_bb, side) .= grid_metric_boundary(grid_panel.Δxᶜᶠᵃ, src_grid_panel, loc_cf, topo_bb, src_side)
grid_metric_halo(grid_panel.Δyᶜᶠᵃ, grid_panel, loc_cf, topo_bb, side) .= grid_metric_boundary(grid_panel.Δyᶜᶠᵃ, src_grid_panel, loc_cf, topo_bb, src_side)
grid_metric_halo(grid_panel.Azᶜᶠᵃ, grid_panel, loc_cf, topo_bb, side) .= grid_metric_boundary(grid_panel.Azᶜᶠᵃ, src_grid_panel, loc_cf, topo_bb, src_side)
grid_metric_halo(grid_panel.Δxᶠᶜᵃ, grid_panel, loc_fc, topo_bb, side) .= grid_metric_boundary(grid_panel.Δxᶠᶜᵃ, src_grid_panel, loc_fc, topo_bb, src_side)
grid_metric_halo(grid_panel.Δyᶠᶜᵃ, grid_panel, loc_fc, topo_bb, side) .= grid_metric_boundary(grid_panel.Δyᶠᶜᵃ, src_grid_panel, loc_fc, topo_bb, src_side)
grid_metric_halo(grid_panel.Azᶠᶜᵃ, grid_panel, loc_fc, topo_bb, side) .= grid_metric_boundary(grid_panel.Azᶠᶜᵃ, src_grid_panel, loc_fc, topo_bb, src_side)
grid_metric_halo(grid_panel.Δxᶠᶠᵃ, grid_panel, loc_ff, topo_bb, side) .= grid_metric_boundary(grid_panel.Δxᶠᶠᵃ, src_grid_panel, loc_ff, topo_bb, src_side)
grid_metric_halo(grid_panel.Δyᶠᶠᵃ, grid_panel, loc_ff, topo_bb, side) .= grid_metric_boundary(grid_panel.Δyᶠᶠᵃ, src_grid_panel, loc_ff, topo_bb, src_side)
grid_metric_halo(grid_panel.Azᶠᶠᵃ, grid_panel, loc_ff, topo_bb, side) .= grid_metric_boundary(grid_panel.Azᶠᶠᵃ, src_grid_panel, loc_ff, topo_bb, src_side)
else
reverse_dim = src_side in (:west, :east) ? 1 : 2
grid_metric_halo(grid_panel.Δxᶜᶜᵃ, grid_panel, loc_cc, topo_bb, side) .= reverse(permutedims(grid_metric_boundary(grid_panel.Δyᶜᶜᵃ, src_grid_panel, loc_cc, topo_bb, src_side), (2, 1, 3)), dims=reverse_dim)
grid_metric_halo(grid_panel.Δyᶜᶜᵃ, grid_panel, loc_cc, topo_bb, side) .= reverse(permutedims(grid_metric_boundary(grid_panel.Δxᶜᶜᵃ, src_grid_panel, loc_cc, topo_bb, src_side), (2, 1, 3)), dims=reverse_dim)
grid_metric_halo(grid_panel.Azᶜᶜᵃ, grid_panel, loc_cc, topo_bb, side) .= reverse(permutedims(grid_metric_boundary(grid_panel.Azᶜᶜᵃ, src_grid_panel, loc_cc, topo_bb, src_side), (2, 1, 3)), dims=reverse_dim)
grid_metric_halo(grid_panel.Δxᶜᶠᵃ, grid_panel, loc_cf, topo_bb, side) .= reverse(permutedims(grid_metric_boundary(grid_panel.Δyᶠᶜᵃ, src_grid_panel, loc_fc, topo_bb, src_side), (2, 1, 3)), dims=reverse_dim)
grid_metric_halo(grid_panel.Δyᶜᶠᵃ, grid_panel, loc_cf, topo_bb, side) .= reverse(permutedims(grid_metric_boundary(grid_panel.Δxᶠᶜᵃ, src_grid_panel, loc_fc, topo_bb, src_side), (2, 1, 3)), dims=reverse_dim)
grid_metric_halo(grid_panel.Azᶜᶠᵃ, grid_panel, loc_cf, topo_bb, side) .= reverse(permutedims(grid_metric_boundary(grid_panel.Azᶠᶜᵃ, src_grid_panel, loc_fc, topo_bb, src_side), (2, 1, 3)), dims=reverse_dim)
grid_metric_halo(grid_panel.Δxᶠᶜᵃ, grid_panel, loc_fc, topo_bb, side) .= reverse(permutedims(grid_metric_boundary(grid_panel.Δyᶜᶠᵃ, src_grid_panel, loc_cf, topo_bb, src_side), (2, 1, 3)), dims=reverse_dim)
grid_metric_halo(grid_panel.Δyᶠᶜᵃ, grid_panel, loc_fc, topo_bb, side) .= reverse(permutedims(grid_metric_boundary(grid_panel.Δxᶜᶠᵃ, src_grid_panel, loc_cf, topo_bb, src_side), (2, 1, 3)), dims=reverse_dim)
grid_metric_halo(grid_panel.Azᶠᶜᵃ, grid_panel, loc_fc, topo_bb, side) .= reverse(permutedims(grid_metric_boundary(grid_panel.Azᶜᶠᵃ, src_grid_panel, loc_cf, topo_bb, src_side), (2, 1, 3)), dims=reverse_dim)
grid_metric_halo(grid_panel.Δxᶠᶠᵃ, grid_panel, loc_ff, topo_bb, side) .= reverse(permutedims(grid_metric_boundary(grid_panel.Δyᶠᶠᵃ, src_grid_panel, loc_ff, topo_bb, src_side), (2, 1, 3)), dims=reverse_dim)
grid_metric_halo(grid_panel.Δyᶠᶠᵃ, grid_panel, loc_ff, topo_bb, side) .= reverse(permutedims(grid_metric_boundary(grid_panel.Δxᶠᶠᵃ, src_grid_panel, loc_ff, topo_bb, src_side), (2, 1, 3)), dims=reverse_dim)
grid_metric_halo(grid_panel.Azᶠᶠᵃ, grid_panel, loc_ff, topo_bb, side) .= reverse(permutedims(grid_metric_boundary(grid_panel.Azᶠᶠᵃ, src_grid_panel, loc_ff, topo_bb, src_side), (2, 1, 3)), dims=reverse_dim)
end
end
return nothing
end