Private types and functions
Documentation for CubedSphere.jl's internal interface.
CubedSphere
CubedSphere.compute_deviation_from_isotropy — Methodcompute_deviation_from_isotropy(X, Y, Z; radius = 1)Compute a scalar measure of the deviation from isotropy for a spherical grid (e.g., a conformal cubed-sphere panel), defined by the Cartesian coordinate arrays X, Y, and Z. Each of X, Y, and Z is a 2D array of size (Nx, Ny) holding the Cartesian coordinates of the grid vertices on the sphere, such that the point at (i, j) corresponds to (X[i, j], Y[i, j], Z[i, j]). The grid therefore contains (Nx−1) × (Ny−1) spherical quadrilateral cells.
For each quadrilateral cell, the function computes the great-circle distances of its four edges on the sphere, evaluates the sum of absolute differences between consecutive edge lengths as a measure of cell anisotropy, and then returns the Euclidean norm of these deviations over the entire grid.
Arguments
X,Y,Z:(Nx, Ny)arrays of Cartesian coordinates of grid vertices on the sphere.
Keyword Arguments
radius: Sphere radius. Default: 1.
Returns
- A non-negative scalar quantifying the overall deviation from isotropy in the grid. Larger values correspond to more anisotropic grids.
Examples
A regular latitude-longitude grid:
using CubedSphere: compute_deviation_from_isotropy
Nx, Ny = 3, 3
lons = range(-π/4, π/4, length = Nx)
lats = range(-π/6, π/6, length = Ny)
X = [cos(φ) * cos(λ) for λ in lons, φ in lats]
Y = [cos(φ) * sin(λ) for λ in lons, φ in lats]
Z = [sin(φ) for λ in lons, φ in lats]
compute_deviation_from_isotropy(X, Y, Z)
# output
1.6552138747243959The vertices of a tetrahedron:
using CubedSphere: compute_deviation_from_isotropy
V = [ 0 0 1 ;
2√2/3 0 -1/3;
-√2/3 √6/3 -1/3;
-√2/3 -√6/3 -1/3]
X = reshape(V[:, 1], 2, 2)
Y = reshape(V[:, 2], 2, 2)
Z = reshape(V[:, 3], 2, 2)
isapprox(compute_deviation_from_isotropy(X, Y, Z), 0, atol=1e-14)
# output
trueCubedSphere.compute_model_diagnostics — Methodcompute_model_diagnostics(X, Y, Z, minimum_reference_cell_area)Compute the two model diagnostics for a given mapped grid:
- Normalized minimum cell width:
sqrt(min(cell_area) / minimum_reference_cell_area), using spherical cell areas fromcompute_cell_areas(X, Y, Z)and a uniform-grid reference area. - Deviation from isotropy: the heuristic anisotropy measure from
compute_deviation_from_isotropy(X, Y, Z).
Arguments
X, Y, Z:(Nx, Ny)Cartesian coordinate arrays on the sphere.minimum_reference_cell_area: The minimum cell area of a reference (typically uniform) grid.
Returns
- A 2-element vector
[normalized_minimum_cell_width, deviation_from_isotropy].
CubedSphere.compute_weighted_model_diagnostics — Methodcompute_weighted_model_diagnostics(model_diagnostics)Apply weights from specify_weights_for_model_diagnostics() to the unweighted diagnostics.
Arguments
model_diagnostics: A 2-element vector[normalized_minimum_cell_width, deviation_from_isotropy].
Returns
- A 2-element vector with weights applied, in the same order as the inputs.
CubedSphere.cube_to_sphere — Methodcube_to_sphere(x, y)Maps the coordinates $(x, y) ∈ [-1, 1] × [-1, 1]$ of the face of a cube to the 3D coordinates $X, Y, Z$ on the sphere via conformal_cubed_sphere_mapping, that is:
X[i, j], Y[i, j], Z[i, j] = conformal_cubed_sphere_mapping(x[i], y[j])CubedSphere.exponential_spacing — Methodexponential_spacing(N, k₀ByN)Construct a symmetric set of N face locations on [-1, 1] with exponentially graded spacing away from the domain center. Let k₀ = k₀ByN * N, and define an exponential map on the right half, x(t) = α·exp(t/k₀) + β, anchored so that it passes through (t₀, 0) and (t₁, 1), then mirror about zero. Endpoints are fixed at -1 and +1.
- Odd
N(M = (N+1)/2): a face lies at0(faces[M] = 0). The right-half faces uset = 1, …, Mwithx(1) = 0,x(M) = 1, and the left half is the negative mirror. - Even
N(M = N/2): no face at0. The two central faces straddle zero, with0midway between them; the right-half faces uset = 2, …, M+1anchored byx(1.5) = 0,x(M+1) = 1, and the left half is mirrored.
Arguments
N: Number of faces (N ≥ 2).k₀ByN: Grading parameter scaled byN(the code usesk₀ = k₀ByN * N). Largerk₀ByNyields spacing closer to uniform; smallerk₀ByNincreases clustering near the center. Requiresk₀ByN > 0.
Returns
faces: A length-Nstrictly increasing vector of face coordinates on[-1, 1]with symmetryfaces[i] = - faces[N+1-i], and end-pointsfaces[1] = -1,faces[N] = 1.
CubedSphere.forward_map — Methodforward_map(Nx, Ny, spacing, θ)Evaluate the (weighted) model diagnostics for a non-uniform conformal cubed-sphere panel defined by parameters θ. The steps are:
- Clamp
θto parameter limits fromspecify_parameter_limits(spacing). - Build a reference uniform grid via
conformal_cubed_sphere_coordinates(Nx, Ny; spacing=UniformSpacing())and compute theminimum_reference_cell_area. - Build the non-uniform grid via
conformal_cubed_sphere_coordinates(Nx, Ny; spacing, ...), passing the parameters inθaccording tospacingtype. - Compute model diagnostics and then apply weights.
Arguments
Nx, Ny: Number of grid vertices along the panel coordinates.spacing:GeometricSpacing()(interpretsθ[1]asratio^(Nx-1)) orExponentialSpacing()(interpretsθ[1]ask₀/Nx).θ: Parameter vector (one element in this implementation).
Returns
- A 2-element vector of weighted model diagnostics.
CubedSphere.geometric_spacing — Methodgeometric_spacing(N, ratio_raised_to_N_minus_one)Construct a symmetric set of N face locations on the interval [-1, 1] with geometrically graded spacing away from the domain center. Let r be the geometric ratio recovered from ratio_raised_to_N_minus_one via r = (ratio_raised_to_N_minus_one)^(1/(N-1)). The gaps between consecutive faces grow by a factor of r as one moves outward from the center, and the layout is mirrored about zero. Endpoints are fixed at -1 and +1.
- Odd
N: A face lies at0. Outward gaps areΔx, Δx*r, Δx*r^2, …, mirrored about0, and chosen so the rightmost face lands at+1. - Even
N: No face at0. Central faces are at±Δx/2; outward gaps areΔx*r, Δx*r^2, …, mirrored, and chosen so the rightmost face lands at+1.
Arguments
N: Number of faces (N ≥ 2).ratio_raised_to_N_minus_one: The valuer^(N-1)for some geometric ratior > 0. Values near1yield nearly uniform spacing. (Exactly1is not supported by the closed-form formulas used here.)
Returns
faces: A length-Nmonotonically increasing vector of face coordinates on[-1, 1]with geometric grading and symmetry:faces[1] = -1,faces[N] = 1, andfaces[i] = -faces[N+1-i].
CubedSphere.optimize! — Methodoptimize!(Nx, Ny, spacing, θ; N_iterations=10, Δt=1)Run an Ensemble Kalman Inversion (EKI) to tune the spacing parameter(s) for a non-uniform conformal cubed sphere panel. An ensemble of parameter vectors θ is iteratively updated so that the weighted model diagnostics produced by forward_map match the ideal targets from specify_ideal_weighted_model_diagnostics().
At each iteration:
- Forward evaluations (parallelized): For each ensemble member
θ[n], compute the predicted diagnosticsG[n] = forward_map(Nx, Ny, spacing, θ[n]). - Ensemble statistics: Form means
θ̄ = mean(θ)andG̅ = mean(G), then compute- Cross-covariance
Cᵘᵖ = cov(θ, G)(shape:nθ × ndata), and - Data covariance
Cᵖᵖ = cov(G, G)(shape:ndata × ndata),
(N_ensemble-1)denominator. - Cross-covariance
- Perturbed observations: For each member, build
y[n] = ideal + Δt * η[n]whereη[n] ~ N(0, I). HereΔtsets the perturbation magnitude of the observations. - Residuals:
r[n] = y[n] - G[n]. - Implicit update (Kalman-like step): Update each parameter vector via
θ[n] ← θ[n] + K * r[n], withK = Cᵘᵖ * (Cᵖᵖ + I/Δt)⁻¹, implemented by solving the linear system with a Cholesky factorization ofCᵖᵖ + I/Δt. The sameΔtalso acts as an implicit damping/step-size control: smallerΔt⇒ stronger regularization and smaller updates; largerΔt⇒ weaker regularization and larger, noisier updates. - Monitoring: Report
error = ‖mean(r)‖and store a snapshot of the ensemble.
This function mutates the input ensemble θ in place (it becomes the final ensemble) and records the full ensemble after each iteration.
Arguments
Nx, Ny: Number of grid vertices along the panel coordinates.spacing:GeometricSpacing()orExponentialSpacing().θ: Initial ensemble — a vector of one-element parameter vectors[θ](lengthN_ensemble).
Keyword Arguments
N_iterations: Number of EKI iterations. Default: 10Δt: Pseudo-time step that sets both the observation perturbation scale and the implicit damping in the linear solve(Cᵖᵖ + I/Δt).
Returns
θ_series: A lengthN_iterations + 1vector; each entry is a snapshot of the full ensemble (index 1 is the initial ensemble; the last is the final ensemble). The inputθis also mutated to the final state.
CubedSphere.specify_ideal_weighted_model_diagnostics — Methodspecify_ideal_weighted_model_diagnostics()Return the target (ideal) values for the weighted model diagnostics used by the inversion. By default, the target normalized minimum cell width is 4 and the target deviation from isotropy is 0, then weights are applied in the same order.
Returns
- A 2-element vector of target weighted diagnostics.
CubedSphere.specify_parameter_limits — Methodspecify_parameter_limits(spacing)Return the lower and upper bounds for the single spacing parameter as a 2-element vector [min, max]. For a consistent interface with possible multi-parameter extensions, this vector is returned wrapped in a one-element array: [[min, max]]. The bounds depend on spacing:
GeometricSpacing()returns[[min, max]]forratio^(N-1).ExponentialSpacing()returns[[min, max]]fork₀/N.
Returns [θ_limits] where θ_limits == [min, max] for the single parameter in this implementation.
CubedSphere.specify_parameters — Methodspecify_parameters(spacing)Return a one-element vector [θ] containing the initial guess for the spacing parameters used to build non-uniform conformal cubed sphere panels for different spacings.
GeometricSpacing(): uses a single parameter interpreted downstream asratio^(N-1)for geometric grading.ExponentialSpacing(): uses a single parameter interpreted downstream ask₀/Nfor exponential grading.
CubedSphere.specify_random_parameters — Methodspecify_random_parameters(N_ensemble, spacing)Draw an ensemble of random parameter vectors within the limits from specify_parameter_limits(spacing). Each ensemble member is sampled uniformly within its parameter bounds.
Arguments
N_ensemble: Number of ensemble members to generate.spacing:GeometricSpacing()orExponentialSpacing().
Returns
- A vector of length
N_ensemble, where each element is a one-element parameter vector[θ](a single parameter in this implementation).
CubedSphere.specify_weights_for_model_diagnostics — Methodspecify_weights_for_model_diagnostics()Return the weights applied to the model diagnostics used by the objective function. The two diagnostics are, in order: (1) normalized minimum cell width, (2) deviation from isotropy.
Returns
- A 2-element vector of weights, e.g.,
[10, 1].