arXiv2026
The existence of an ideal MagnetoHydroDynamic (MHD) equilibrium does not guarantee its stability. Finite toroidal mode number (n) instabilities degrade performance in both tokamaks and stellarators and differentiable stability optimization tools to date have operated only in the infinite-n limit. We present AGNI (Analysis of Global Normal modes in Ideal MHD), a GPU-accelerated, automatically differentiable finite-n ideal MHD stability solver and optimizer. AGNI discretizes the ideal MHD energy principle pseudospectrally in real space using differentiation matrices and geometric coefficients from a DESC equilibrium, giving a variational eigenvalue problem for the plasma displacement, and efficiently finds the most unstable modes. Built on jax, AGNI yields reverse-mode gradients of the growth rate with respect to boundary-shape and profile parameters without re-solving the equilibrium. We benchmark AGNI against the initial-value code NIMSTELL for a modified Landreman-Bulle-Drevlak quasi-helically symmetric stellarator and a DSHAPE tokamak,recovering the dominant mode with agreement in terms of growth rate and eigenfunction structure, and verify the automatic differentiation gradients against central finite differences. We quantify CPU and GPU cost for eigenvalue and gradient evaluation, establish the finite-precision limit on resolving near-marginal eigenvalues, and present a numerical scheme to impose incompressibility, compatible with gradient-based optimization. AGNI will allow us to optimize tokamaks, stellarators, and mirrors against ideal MHD instabilities.