GVEC Tokamak Interface Test
Note
For general information about finite element discretization, basis functions, mesh parameters, polynomial degrees, boundary conditions, and matrix/operator dimensions, see Overview.
This script tests the interface with GVEC data for tokamak configurations.
The script is located at scripts/interactive/test_gvec_tokamak.py.
Problem Statement
This script tests the interface between MRX and GVEC (Generalized Variational Equilibrium Code) data for tokamak geometries. The script does not solve a PDE directly, but rather:
Loads GVEC equilibrium data from HDF5 files
Interpolates the GVEC coordinate mapping into MRX spline space
Tests projection accuracy for functions defined on the GVEC geometry
The test function is:
which is projected onto the 0-form finite element space and the projection error is computed.
GVEC Data Structure
GVEC provides equilibrium data in the form: - \(\rho \in [0,1]\): Normalized radial coordinate - \(\theta \in [0,2\pi]\): Poloidal angle - \(\theta^*(\rho,\theta)\): Modified poloidal angle mapping - \(X_1(\rho,\theta^*), X_2(\rho,\theta^*)\): Physical coordinates \((R, Z)\) in cylindrical coordinates
The mapping from logical coordinates \((\rho, \theta^*, \zeta)\) to physical coordinates is:
Least-Squares Interpolation
The functions \(X_1(\rho,\theta^*)\) and \(X_2(\rho,\theta^*)\) are interpolated into MRX spline space using least-squares fitting:
This leads to the linear system:
where \(M \in \mathbb{R}^{(m_\rho m_\theta) \times N_0}\) is the design matrix and \(\mathbf{c} \in \mathbb{R}^{N_0 \times 2}\) contains coefficients for both \(X_1\) and \(X_2\).
Projection Error Test
The projection error is computed as:
where \(f_h = \sum_{i=1}^{N_0} \hat{f}_i \Lambda_0^i\) is the projected function and \(\hat{f} = P_0(f)\) is the projection of \(f\) onto the 0-form space.
Usage:
python scripts/interactive/test_gvec_tokamak.py
The script generates plots showing the tokamak configuration and projection errors.
Code Walkthrough
Block 1: Imports and Data Loading (lines 1-27)
Imports xarray for reading HDF5/NetCDF data and MRX modules. Loads GVEC tokamak equilibrium data from ``data/gvec_tokamak.h5`:
\(\theta^*\): Modified poloidal angle mapping \(\theta^*(\rho,\theta)\)
\(\rho, \theta\): Radial and poloidal coordinate grids
\(X_1, X_2\): Physical coordinates \((R, Z)\) as functions of \((\rho, \theta^*)\)
Block 2: Mapping Interpolation (lines 29-59)
Interpolates GVEC coordinate mapping into MRX spline space:
Creates DeRham sequence
mapSeqfor approximating \(X_1(\rho,\theta^*)\) and \(X_2(\rho,\theta^*)\)Builds design matrix \(M\) by evaluating 0-form basis functions at GVEC grid points
Solves least-squares problem:
to find spline coefficients \(\mathbf{c}\).
- Constructs gvec_stellarator_map from interpolated coordinates (note: uses
stellarator map function but with
nfp=1for axisymmetric tokamak)
Block 3: Projection Accuracy Test (lines 61-140)
Tests projection accuracy for various mesh sizes:
Defines test function:
Creates DeRham sequence using GVEC mapping
Projects function into finite element space: \(\hat{f} = P_0(f)\)
Reconstructs function: \(f_h = \text{DiscreteFunction}(\hat{f}, \Lambda_0, E_0)\)
Computes L2 projection error using
jax.lax.scanto avoid memory issuesTests convergence as mesh is refined (\(n \in \{4,6,8,\ldots,18\}\))
Block 4: Visualization (lines 142-241)
Generates plots:
GVEC coordinate mapping visualization
Projection error convergence plot
Comparison of exact vs. projected function
This script validates that MRX can accurately represent geometries from external equilibrium codes like GVEC, enabling the use of MRX solvers on experimentally relevant configurations.