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:

\[-\Delta u = f \quad \text{in } \Omega\]

with homogeneous Dirichlet boundary conditions:

\[u|_{\partial\Omega} = 0\]

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:

\[\begin{split}F(r, \chi, \zeta) = \begin{bmatrix} (R_0 + \epsilon r \cos(2\pi\chi)) \cos(2\pi\zeta) \\ (R_0 + \epsilon r \cos(2\pi\chi)) \sin(2\pi\zeta) \\ \epsilon r \sin(2\pi\chi) \end{bmatrix}\end{split}\]

Exact Solution

The exact solution is:

\[u(r,\chi,\zeta) = \frac{1}{4}(r^2 - r^4) \cos(2\pi\zeta)\]

which is independent of the poloidal angle \(\chi\).

Source Term

The corresponding source term is:

\[f(r,\chi,\zeta) = \cos(2\pi\zeta) \left[ -\frac{1}{a^2}(1-4r^2) - \frac{1}{aR}\left(\frac{r}{2}-r^3\right)\cos(2\pi\chi) + \frac{1}{4}\frac{r^2-r^4}{R^2} \right]\]

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:

\[M_0 \Delta_0 \hat{u} = P_0(f)\]

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:

\[u(r,\chi,z) = \frac{1}{4}(r^2 - r^4) \cos(2\pi z)\]

Source term:

\[f(r,\chi,z) = \cos(2\pi z) \left[ -\frac{1}{a^2}(1-4r^2) - \frac{1}{aR}\left(\frac{r}{2}-r^3\right)\cos(2\pi\chi) + \frac{1}{4}\frac{r^2-r^4}{R^2} \right]\]

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:

\[M_0 \Delta_0 \hat{u} = P_0(f)\]

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.