Toroid Poisson (Interactive)
Note
For general information about finite element discretization, basis functions, mesh parameters, polynomial degrees, boundary conditions, and matrix/operator dimensions, see Overview.
This script solves a Poisson problem on a toroidal domain interactively.
The script is located at scripts/interactive/toroid_poisson.py.
Problem Statement
This script is similar to toroid_poisson.py but focuses on interactive exploration
and additional diagnostics. It solves the Poisson equation:
with homogeneous Dirichlet boundary conditions:
where: - \(u: \Omega \to \mathbb{R}\) is the scalar solution (0-form) - \(f: \Omega \to \mathbb{R}\) is the source term (0-form) - \(\Delta = \nabla \cdot \nabla\) is the scalar Laplacian operator - \(\Omega\) is a toroidal domain - \(\partial\Omega\) denotes the boundary of the toroidal domain
Toroidal Geometry
The toroidal domain is parameterized by: - Minor radius: \(a=1/3\) - Major radius: \(R_0 = 1.0\) - Aspect ratio: \(\epsilon = a/R_0 = 1/3\)
The mapping from logical coordinates \((r, \chi, \zeta)\) to physical coordinates is:
Exact Solution
The exact solution is:
which is independent of the poloidal angle \(\chi\).
Source Term
The corresponding source term is:
where \(R = R_0 + a r \cos(2\pi\chi)\).
The script demonstrates:
Setting up finite element spaces on a toroidal domain
Solving Poisson equations in toroidal geometry
Interactive visualization of results
Computing condition numbers and matrix sparsity for diagnostics
Usage:
python scripts/interactive/toroid_poisson.py <n> <p>
where n is the number of elements and p is the polynomial degree.
Finite Element Discretization
The domain is discretized using a DeRham sequence with: - Mesh parameters: \(n_r = n_\chi = n_\zeta = n\) elements in each direction - Polynomial degrees: \(p_r = p_\chi = p_\zeta = p\) - Quadrature order: \(q = p\) - Boundary conditions: Clamped in radial direction, periodic in poloidal and toroidal directions
Matrix and Operator Dimensions
The 0-form mass matrix \(M_0 \in \mathbb{R}^{N_0 \times N_0}\) and Laplacian \(\Delta_0 \in \mathbb{R}^{N_0 \times N_0}\) are used.
The discrete Poisson equation:
where \(\hat{u} \in \mathbb{R}^{N_0}\) are the solution coefficients.
Diagnostics
The script computes: - Condition number: \(\kappa(A) = \sigma_{\max}(A)/\sigma_{\min}(A)\) where \(A = M_0 \Delta_0\) - Sparsity: Fraction of non-zero entries in the system matrix
Code Walkthrough
Block 1: Imports and Setup (lines 1-14)
Imports modules and enables 64-bit precision. Uses toroid_map for domain geometry.
Block 2: Error and Diagnostics Function (lines 17-105)
The get_err() function computes error and additional diagnostics.
Exact solution:
Source term:
where \(R = R_0 + a r \cos(2\pi\chi)\) and \(\epsilon = a/R_0 = 1/3\) is the aspect ratio.
Sets up DeRham sequence with toroidal mapping, assembles mass matrix \(M_0\) and Laplacian \(\Delta_0\), solves system:
Computes relative L2 error using jax.lax.scan to avoid memory issues with large arrays.
Also computes condition number and sparsity of the system matrix \(M_0 \Delta_0\).
Block 3: Main Function (lines 108-149)
Parses command-line arguments for mesh size \(n\) and polynomial degree \(p\), computes error, condition number, and sparsity, and saves results to a text file.
The interactive version provides more detailed diagnostics than the tutorial version, making it useful for understanding numerical properties of the discretization.