Coupling & GPU Usage
Two-Way Magnetosphere–Ionosphere Coupling
In a coupled 3D magnetospheric simulation with an inner boundary at
Extract FACs on the ionosphere grid: For each active
(i, j)node onsol.thetaandsol.phi, calldipole_fac_sample_pointto trace along the dipole field line fromto , sample from the 3D magnetospheric state atx_fac, and project to the ionospheric shell using flux conservation:Solve the 2D potential equation: Call
solve_ionosphere!with optional Laplacian FAC smoothing (n_smooth_fac), hemispheric neutralization (neutralize_fac), and under-relaxation (relax_alpha).Evaluate mapped fields at
: Construct a lightweightIonosphereFieldViewviaionosphere_field_viewand queryeval_ionosphere_Eandeval_ionosphere_u_exbinside inner-boundary or guard-cell kernels.
using IonosphereElectrodynamics
frame = build_dipole_frame((0.0, 0.0, -3.0e-5); r_iono = 1.017)
r_fac = 2.5
for th_deg in (15.0, 25.0, 65.0)
valid, x_fac, _, _, b_iono, br_over_b = dipole_fac_sample_point(
frame, deg2rad(th_deg), deg2rad(90.0), r_fac,
)
if valid
println(
"θ = ", th_deg, "° -> x_fac = ", round.(x_fac; sigdigits = 4),
", |B0_iono| = ", round(b_iono; sigdigits = 4),
", Br/B = ", round(br_over_b; sigdigits = 4),
)
else
println("θ = ", th_deg, "° -> closed below r_fac = ", r_fac)
end
endθ = 15.0° -> x_fac = [6.212e-17, 1.014, 2.285], |B0_iono| = 5.559e-5, Br/B = -0.9911
θ = 25.0° -> x_fac = [1.014e-16, 1.657, 1.872], |B0_iono| = 5.308e-5, Br/B = -0.9739
θ = 65.0° -> closed below r_fac = 2.5GPU / Adapt.jl & Float32 Support
IonosphereFieldView implements Adapt.adapt_structure, allowing a host solver's gradient arrays to be transferred cleanly to GPU device memory (e.g., CUDA/AMDGPU/Metal via Adapt.adapt(backend, ionosphere_field_view(sol))) while keeping DipoleFrame and scalar metadata isbits. Both eval_ionosphere_E and eval_ionosphere_u_exb are @inline, type-stable, and allocation-free for Float64 and Float32:
using IonosphereElectrodynamics
using StaticArrays
cfg32 = IonosphereConfig(;
n_theta_hem = 17,
n_phi = 32,
conductance = AuroralConductance(SolarEUVConductance(; FT = Float32); FT = Float32),
FT = Float32,
)
sol32 = IonosphereSolver(cfg32; r_iono = 1.0f0, dipole_moment = (0.0f0, 0.0f0, -3.0f-5))
jr32 = [1.0f-6 * sin(sol32.theta[i])^2 * sin(sol32.phi[j]) for i in 1:sol32.n_theta, j in 1:sol32.n_phi]
ok = solve_ionosphere!(sol32, jr32)
cpcp32 = cross_polar_cap_potential(sol32)
view32 = ionosphere_field_view(sol32)
x32 = SVector(0.5f0, 0.4f0, 1.5f0)
b32 = dipole_B0(sol32.frame, x32)
e32 = eval_ionosphere_E(view32, x32; B0 = b32)
u32 = eval_ionosphere_u_exb(view32, x32, b32)
println("ok = ", ok)
println("cpcp = (north = ", round(cpcp32.north; sigdigits = 4), ", south = ", round(cpcp32.south; sigdigits = 4), ")")
println("e32 = ", round.(e32; sigdigits = 4), " (", typeof(e32), ")")
println("u32 = ", round.(u32; sigdigits = 4), " (", typeof(u32), ")")ok = true
cpcp = (north = 2.838e-8, south = 2.838e-8)
e32 = Float32[-6.9690004f-10, 2.069f-9, -5.273f-10] (SVector{3, Float32})
u32 = Float32[-0.0001447, -2.557f-5, 9.083f-5] (SVector{3, Float32})Generic Types & Automatic Differentiation
All structs and operations in IonosphereElectrodynamics.jl are parameterized over generic Real scalar types (Float64, Float32, Float16, BigFloat, ForwardDiff.Dual, nested Dual, etc.), infer element types automatically from inputs (defaulting to Float64 when unspecified), and accept SVector{3}, AbstractVector, or NTuple{3} for 3D vectors.
Forward-mode AD (
ForwardDiff.jl): Pointwise conductance, dipole mapping, and 3D field evaluations differentiate natively with respect to coordinates, magnetic fields, and model parameters. LoadingForwardDiffactivates a package extension that differentiates the 2D spherical PDE solve implicitly ( ), reusing a single SIMD primalILU(0)factorization per hemisphere and supporting warm-starts, , and nestedDualHessians.Reverse-mode AD (
ChainRulesCore.jl): LoadingChainRulesCore(directly or viaZygote.jl,Mooncake.jl, orReverseDiff.jl) activates exactimplicit adjoint rules ( rruleandfrule) for the statelesssolve_ionosphereandionosphere_field_view(solver, phi_pot)pipeline using the exact 5-point transpose operator and transposeILU(0)preconditioner in0 Bsteady-state allocations.
using ForwardDiff
using IonosphereElectrodynamics
using StaticArrays
# Differentiate CPCP or mapped 3D E-field w.r.t. conductance parameters or FACs
cfg = IonosphereConfig(; n_theta_hem = 17, n_phi = 32)
sol = IonosphereSolver(cfg; r_iono = 1.017, dipole_moment = (0.0, 0.0, -5.0))
jr0 = [1.0e-6 * sin(sol.theta[i])^2 * sin(sol.phi[j]) for i in 1:sol.n_theta, j in 1:sol.n_phi]
x = SVector(0.5, 0.4, 2.0)
b0 = dipole_B0(sol.frame, x)
# Spatial Jacobian ∂E/∂x
J_x = ForwardDiff.jacobian(pos -> collect(eval_ionosphere_E(sol, pos; B0 = b0)), collect(x))
# Parameter sensitivity through the 2D spherical PDE solve
dE_ds = ForwardDiff.derivative(1.0) do scale
phi = solve_ionosphere(sol, scale .* jr0)
eval_ionosphere_E(ionosphere_field_view(sol, phi), x; B0 = b0)
endAnalytic R1 + R2 Polar Benchmark
The standalone script examples/fleks_analytic_polar.jl reproduces the FLEKS analytic Region 1 + Region 2 Birkeland current benchmark on a 91 × 180 spherical grid (lat_boundary_deg = 20°) with Moen–Brekke solar EUV (+x) and :tanh_sqrt auroral precipitation conductance, yielding a cross-polar-cap potential of

To run the example locally and save the figure:
julia --project=/path/to/env-with-CairoMakie examples/fleks_analytic_polar.jlPerformance Benchmarks
The benchmark/benchmarks.jl suite tracks conductance evaluation, dipole mapping, 3D field/velocity queries, and warm/cold 2D ILU(0)-BiCGSTAB solves using BenchmarkTools.jl and AirspeedVelocity.jl:
cd IonosphereElectrodynamics.jl && benchpkg IonosphereElectrodynamics --rev=main,dirty