Grids

A grid answers: how is the data stored? It pairs a geometry with coordinates, a per-cell measure and an activity mask.

typecoordinatesuse
StructuredGridone 1-D axis per directionrectilinear: lat–lon, any tensor-product sampling
CurvilinearGridone N-D array per directionlogically rectangular, geometrically warped
HEALPixGridnone — (nside, pixel) arithmeticthe HEALPix pixelization
CubedSphereGridnone — (n, cell) arithmeticthe gnomonic cubed sphere
YinYangGridnone — (nlon, nlat, cell) arithmeticKageyama–Sato overset panels
IcosahedralGridnone — (ν, id) arithmeticgeodesic, 12 pentagons
RingGridone value per ringreduced Gaussian, octahedral
UnstructuredGridone value per node, plus CSR neighboursscattered points, an arbitrary mesh
RotatedGridits base grid's, reported in another framea rotated-pole lat–lon mesh

The first two store coordinates because a warped mesh's are arbitrary, and UnstructuredGrid stores them because a node set's are the caller's. The five in between store none, or one value per ring: a cell's position and its neighbours are closed-form in the layout's parameters, so they hold those parameters instead. A RotatedGrid holds a rectilinear grid and a pole rotation, and evaluates each cell's position in the other frame; see Geometry. HEALPixGrid is the same size at nside = 1024 — 12.6 million pixels — as at nside = 1. Ask for the dense cloud with Grids.materialize when something outside needs it.

Building one

using FlowGeometries: FlowGeometries as FG

# From a sampling — the usual route
grid = FG.Connectivity.structured_grid(FG.SphericalSampling.Fejer1Sampling(), 64)
g    = FG.Grids.HEALPixGrid(16)
rg   = FG.Grids.RingGrid(FG.SphericalSampling.OctahedralGaussianSampling(64))
cs   = FG.Grids.CubedSphereGrid(24)
yy   = FG.Grids.YinYangGrid(32, 22)
ico  = FG.Grids.IcosahedralGrid(16)

# Or directly, in any number of dimensions; the mask is optional
sg  = FG.Grids.StructuredGrid(FG.Geometry.SphericalGeometry(), λaxis, φaxis, mask; topology = FG.Grids.Periodic())
cg  = FG.Grids.CurvilinearGrid(FG.Geometry.SphericalGeometry(), λ2d, φ2d, mask; topology = FG.Grids.Periodic())
g4  = FG.Grids.StructuredGrid(FG.Geometry.CartesianGeometry(),
                              0:1.0:3, 0:1.0:3, 0:1.0:3, 0:1.0:3)  # 4-D, all active
size(g4)
(4, 4, 4, 4)

A direction wraps when its axis type says so — a PeriodicAxis, n equal cells around one period, as a sampling's longitude is — or when topology names it Periodic. Every other direction is bounded; nothing is read off the coordinate values. On a periodic axis the seam is one step wide by construction, so its widths, faces and stencil rows keep their uniform forms; a plain axis declared periodic takes the per-cell forms, which hold whatever seam its period leaves.

wrapped = FG.Grids.StructuredGrid(FG.Geometry.SphericalGeometry(), FG.Axes.PeriodicAxis(0.0, 2π, 64), φaxis)
FG.Grids.isperiodic(wrapped, 1), FG.Grids.isperiodic(FG.Grids.StructuredGrid(geo, λaxis, φaxis), 1)
(true, false)

A formula layout answers every per-cell question by arithmetic. HEALPix's storage does not depend on its resolution; the cubed sphere and the icosahedral geodesic hold their cell areas besides, a quadrant of one panel and one area per symmetry class of vertex, and Yin–Yang one panel's cell measures, which both panels share.

FG.Grids.coords(g, 100), FG.Grids.measure(g, 100), collect(FG.Grids.neighbors(g, 100))
((λ = 3.478191866474414, φ = 1.2116520114493075), 1.6603661194980087e11, [51, 73, 74, 99, 101, 130, 131, 165])
λ, φ = FG.Grids.materialize(g)                     # the dense cloud, asked for explicitly
length(λ)
3072

The common interface

Everything below works on every architecture:

out = zeros(2)
FG.Grids.coords(grid, i, j)                        # (λ = …, φ = …) or (x = …, y = …)
FG.Grids.coords!(out, grid, i, j)                  # into your buffer
FG.Grids.coords(NTuple{2,Float64}, grid, i, j)     # a specific storage type
FG.Grids.measure(grid, i, j)                       # cell area / volume / control-volume size
FG.Grids.isactive(grid, i, j)                      # participates in the domain?
collect(FG.Grids.neighbors(grid, i, j))            # lazy iterator over neighbour indices
size(grid), length(grid), FG.Grids.size_tuple(grid)
FG.Grids.isperiodic(grid, 1), FG.Grids.period(grid, 1)
(true, 6.283185307179586)

Coordinates are also reachable by their geometry-correct names — grid.λ, grid.φ on a sphere; grid.x, grid.y on a plane.

Cell measure is stored factored

On a rectilinear grid every measure this package supports is a product of one factor per axis: Cartesian Δx·Δy·Δz, spherical (Δλ)·(R²(sin φ₊ − sin φ₋)), the exact area between a cell's faces with the outer latitude faces clamped to the poles, so the cells of a global grid sum to 4πR². A grid built on a sampling with quadrature weights (Gauss–Legendre, Driscoll–Healy, Fejér first rule) takes R²·wⱼ as its latitude factor, and sum(f .* measure(grid)) is that quadrature. A StructuredGrid stores those ∑ Nᵈ factors, and the ∏ Nᵈ products come out of them.

m = FG.Grids.measure(grid)           # a SeparableMeasure — a real AbstractArray
m[3, 5]                        # indexes exactly like the dense outer product
sum(m)                         # ∏ᵈ ∑ᵢ wᵈᵢ — O(∑ Nᵈ) against the dense O(∏ Nᵈ)
FG.Grids.measure_factors(grid)       # the per-axis factors, or `nothing`
FG.Grids.measure_array(grid)         # materialize densely, if you really need it
127×64 Matrix{Float64}:
 2.11112e9  7.42482e9  1.19518e10  …  1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10  …  1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 ⋮                                 ⋱                         
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10  …  1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10  …  1.19518e10  7.42482e9  2.11112e9
 2.11112e9  7.42482e9  1.19518e10     1.19518e10  7.42482e9  2.11112e9

Indexing, broadcasting and collect behave exactly as for the dense array, and the values are bit-identical. At 4000 × 2000 the measure is 0.046 MiB against 61.0 MiB dense, and faster to read on every access pattern measured: the factors stay in cache while a 61 MiB array is DRAM-bound. See Performance.

Operations that keep the measure a product stay factored, so a unit conversion does not undo the saving: scaling by any real number, a multiplicative map (abs, abs2, sqrt, inv), and a factor-wise product or quotient of two measures.

km² = m ./ 1e6
typeof(km²).name.name
:SeparableMeasure

Anything else materializes, correct and dense. exp is the clean example: it is not multiplicative, so exp(∏wᵈ) ≠ ∏exp(wᵈ) and there is no factored form to keep.

typeof(exp.(m)).name.name, typeof((-1.0) .* m).name.name
(:Array, :SeparableMeasure)

sum being O(∑Nᵈ) is also what stops show from adding eight million numbers to print one line.

Masks

FG.Grids.mask(grid)                  # the mask array
FG.Grids.isactive(grid, i, j)        # false = excluded (land, say)
true

When every cell participates the mask is AllActive, which stores its size and nothing else — getindex folds to a constant and count is length without a scan. Pass a real BitArray or Array{Bool} when you need to exclude cells.

Curvilinear corners

CurvilinearGrid's exact quadrilateral cell area comes from its cell vertices — the spherical excess of the two triangles through the four corner directions on a sphere, the shoelace area on a plane.

cg2 = FG.Grids.CurvilinearGrid(geo, λ2d, φ2d, mask; corners = (λc, φc))
size(FG.Grids.corners(cg2, 1)), FG.Grids.corner_coords(cg2, i, j)
((64, 33), (λ = 0.14959965017094254, φ = 1.1780972450961724))

Supply corners when your source model ships its own cell-vertex grid; otherwise they are reconstructed from the centres, which needs at least a 2×2 grid.

Vertices you supply are kept on the grid. Reconstructed ones are input to the area kernel, and are kept only if you ask — one array per direction, one larger in every direction:

lean = FG.Grids.CurvilinearGrid(geo, λ2d, φ2d, mask)
full = FG.Grids.CurvilinearGrid(geo, λ2d, φ2d, mask; keep_corners = true)
FG.Grids.has_corners(lean), FG.Grids.has_corners(full)
(false, true)

A curvilinear grid takes any number of directions — one N-D array each, mask last. Beyond 2-D the cell measure is yours to pass: the corner-area kernel is an exact-quadrilateral algorithm with no N-D generalization here, so asking it for a 3-D measure raises.

cart = FG.Geometry.CartesianGeometry()
X = [x for x in 0.0:1.0:3.0, _ in 1:3, _ in 1:2]
Y = [y for _ in 1:4, y in 0.0:2.0:4.0, _ in 1:2]
Z = [z for _ in 1:4, _ in 1:3, z in 0.0:0.5:0.5]
cg3 = FG.Grids.CurvilinearGrid(cart, X, Y, Z, fill(1.0, 4, 3, 2), trues(4, 3, 2))
size(cg3), FG.Grids.coordinate_names(cg3), FG.Grids.coords(cg3, 2, 3, 2)
((4, 3, 2), (:x, :y, :z), (x = 1.0, y = 4.0, z = 0.5))

The measure goes either before the mask, as above, or as the measure keyword:

FG.Grids.CurvilinearGrid(cart, X, Y, Z, trues(4, 3, 2); measure = fill(1.0, 4, 3, 2));
CurvilinearGrid{Float64} 4×3×2 (24/24 active)
  geometry:  FlowGeometries.Geometry.CartesianGeometry{Float64}()
  x         0.0 … 3.0  (4×3×2)
  y         0.0 … 4.0  (4×3×2)
  z         0.0 … 0.5  (4×3×2)
  measure:   24.0 total

Node grids generalize the same way, with the coordinates as a tuple — a run of vectors cannot say how many of them are coordinates, where a curvilinear grid counts them from ndims(mask):

nodes = FG.Grids.UnstructuredGrid(cart, (rand(6), rand(6), rand(6)), ones(6), trues(6))
FG.Grids.coordinate_names(nodes), FG.Grids.coords(nodes, 4)
((:x, :y, :z), (x = 0.3613108392209502, y = 0.2902734862869807, z = 0.9123747451522886))

Cell areas on the spherical node sets

Cell areas are an exact closed form, dispatched on the sampling — never a uniform 4πR²/N fallback, which is right only for equal-area samplings and silently wrong elsewhere (icosahedral dual cells span a min/max ratio of 0.52).

layout / samplingcell areas
HEALPixGriduniform 4πR²/N — exact by construction, and stored as one number
CubedSphereGridexact solid angle of the cell's own gnomonic rectangle
YinYangGridlat–lon patch area R²Δλ·2sin(Δφ/2)·cosφ, less half its overlap with the other panel
RingGridR²·(2π/nlonᵣ)·wᵣ from the ring's Gaussian weight
IcosahedralGridthe spherical Voronoi dual, from the vertex's own incident triangles
arbitrary pointsVoronoi areas, via the Quickhull or DelaunayTriangulation extension

Cell area relative to the mean

Only HEALPix is one flat colour. The dark spots on the icosahedral panel are its twelve pentagons — the smallest cells on the mesh, and the reason a uniform default is off by nearly a factor of two between the largest and smallest cell there.

Every layout's measures sum to 4πR² to machine precision and need no optional dependency. On the node sets, pass areas = … to supply your own.

Yin–Yang overlap

Yin–Yang panels overlap

The two panels overlap by construction, so their cell areas sum to 3√2πR², 6.07% more than the sphere at every resolution. A cell's measure is its area less half the part of it the other panel also covers, that overlap integrated in closed form, so sum(f .* measure(grid)) counts every point of the sphere once.