Skip to content

Frequently Asked Questions (FAQ) & Troubleshooting

This page answers common questions regarding floating-point precision, backend routing (backend="auto" | "rust" | "jax"), block-tridiagonal Henyey solver selection (bcyclic vs. pcr vs. rust_ffi), legacy Fortran MESA file compatibility, and multi-thread/GPU configuration.


1. Precision, Numerics Modes & Backend Dispatch

What is the difference between "strict" and "fast" numerics modes?

All Rust physics crates call flexmesa-math (#165) and support two run-level arithmetic modes built from the same Rust physics source (flexmesa.get_numerics_mode(), flexmesa.set_numerics_mode("fast"), flexmesa.numerics_mode_context("fast"), or --numerics-mode {strict,fast}):

  • "strict" (Default): Calls vendored crlibm 1.0beta4 (CRMATH 1.2) through flexmesa-crlibm-sys without fused multiply-add contraction (a * b + c) and preserves upstream operation order. "strict" is hardware- and thread-count-independent (x86_64 and aarch64) and is the only mode judged against MESA 26.4.1.
  • "fast": Uses platform std f64 intrinsics and hardware mul_add. "fast" is hardware-dependent, is validated exclusively against "strict" (notes/audit/fast_vs_strict.md), and is never compared against MESA.

Why does importing flexmesa enable 64-bit precision in JAX (jax_enable_x64)?

Stellar structure, spline table interpolation, and stiff nuclear networks span 15 to 30 orders of magnitude in density, pressure, and reaction timescale. In 32-bit float32 arithmetic (\(\epsilon_{\mathrm{mach}} \approx 1.19 \times 10^{-7}\)), hydrostatic pressure differences across thin photospheric shells (\(dq_k \sim 10^{-10}\)) and second-order Helmholtz free-energy derivatives suffer severe catastrophic cancellation. Importing flexmesa calls flexmesa.enable_float64(), which sets jax.config.update("jax_enable_x64", True) before any arrays or JIT traces are constructed.

If you import jax.numpy before flexmesa in a custom script, either import flexmesa first or set JAX_ENABLE_X64=1 in your shell environment.

How does backend="auto" decide between Safe Rust and JAX?

Safe Rust is the canonical implementation across FlexMESA, and every dual-backend dispatcher inspects its positional and keyword arguments recursively via flexmesa.resolve_backend(backend, *args) (descending through nested containers, dataclasses, object-dtype NumPy arrays, __jax_array__ objects, and registered JAX PyTrees):

  • If any inspected leaf is a jax.Array or active JAX tracer (jax.core.Tracer inside jax.jit, jax.grad, jax.jacfwd, or jax.vmap), backend="auto" routes to the derived JAX path (either a pure @jax.jit kernel or a @jax.custom_vjp / @jax.custom_jvp host callback wrapping flexmesa._rust; star, binary, and astero JAX helper modules remain frozen under JAX_ENGINE_STATUS).
  • Otherwise (for Python scalars and numpy.ndarray arrays), backend="auto" dispatches zero-copy to the compiled flexmesa._rust (PyO3) Safe Rust extension.

You can override this behavior at any time by passing backend="rust" or backend="jax" explicitly. See the Backends & JAX Contract guide for per-package AD modes and device support.


2. Linear Solvers & Automatic Differentiation

When should I use solver_mode="pcr" versus solver_mode="rust_ffi" in flexmesa.mtx?

When solving block-tridiagonal linear systems \(A x = b\) of shape (nz, m, m) in JAX (mtx.solve_block_tridiagonal_jax):

  • solver_mode="pcr" (Native GPU Parallel Cyclic Reduction): Executes \(\lceil \log_2 N_{\mathrm{zones}} \rceil\) vectorized block reduction steps (12 steps at \(N_{\mathrm{zones}} = 4096\)) entirely inside XLA without leaving the GPU. Use "pcr" whenever you are vectorizing across batches of stars with jax.vmap on a GPU or TPU.
  • solver_mode="rust_ffi" (Safe Rust bcyclic + Analytical @jax.custom_vjp): Dispatches the forward pass to flexmesa._rust.solve_block_tridiag_bcyclic via jax.pure_callback and differentiates the reverse pass analytically via the Implicit Function Theorem adjoint system \(A^\top \lambda = \bar{x}\) and \(\bar{A} = -\lambda x^\top\). Use "rust_ffi" for single-star CPU workflows or hybrid CPU/GPU adjoint sensitivities where host solvers (1.38 ms 1-thread PyO3 bcyclic and 1.29 ms single-system Safe Rust block-Thomas vs. 7.45 ms single-profile GPU PCR at \(N_{\mathrm{zones}} = 4096\), or 0.32 ms 4-system amortized Safe Rust block-Thomas vs. 2.80 ms vmap GPU PCR; see Kernel Performance Scorecards and Stellar Evolution Profiling) are faster than PCR and GPU dispatch overhead.
import jax.numpy as jnp
import flexmesa.mtx as mtx

# Solve a 4-zone, 2x2 block-tridiagonal system with native PCR
nz, m = 4, 2
sub = jnp.zeros((nz, m, m), dtype=jnp.float64)
diag = jnp.broadcast_to(jnp.eye(m, dtype=jnp.float64) * 2.0, (nz, m, m))
sup = jnp.zeros((nz, m, m), dtype=jnp.float64)
rhs = jnp.ones((nz, m), dtype=jnp.float64)

sol = mtx.solve_block_tridiagonal_jax(sub, diag, sup, rhs, solver_mode="pcr")
print("PCR solution norm:", float(jnp.linalg.norm(sol)))

How does FlexMESA differentiate iterative solvers (set_mlt, set_tdc, eos_pt_get) without differentiating through the iteration loop?

Propagating 33-variable or 70-variable dual numbers through every bracket-search and Newton-Raphson iteration is expensive (the measured turb_tdc timings are in the Wave 1 benchmark report). The derived JAX layer instead converges the scalar root \(y^*\) in plain f64 and applies the Implicit Function Theorem once at the converged root \(Q(y^*, \theta) = 0\) (Time-Dependent Convection is the exception: both Safe Rust and Python/JAX follow MESA and carry the partials through the Newton iteration):

\[\frac{\partial y^*}{\partial \theta} = -\left(\frac{\partial Q}{\partial y}\right)^{-1} \frac{\partial Q}{\partial \theta}\]

This yields the exact analytical tangent (matching infinite-precision unrolled AD to \(\le 10^{-15}\)) without differentiating through the iteration loop.


3. Fortran MESA Compatibility & Data Tables

Can I load existing Fortran MESA .mod files and inlist directories directly?

Yes, though &controls and &star_job coverage is currently partial (#147). StarModel.from_mod_file("path/to/model.mod") parses upstream ASCII .mod files (converting 1-based Fortran zone indexing and D exponents into 0-based C-contiguous FlexMESA arrays), and star.parse_inlist_bundle("path/to/inlist") recursively resolves read_extra_star_job_inlist*, read_extra_controls_inlist*, and &pgstar namelist chains (unrecognized or unported keys are stored in extra_fields or ignored by the step driver until wired under #147). See Inlists, Models & Data Caches for worked examples.

Why does zone index 0 correspond to the stellar surface instead of the center?

Upstream Fortran MESA indexes stellar zones from k = 1 at the outer photosphere (q(1) = 1.0) to k = s% nz at the stellar center (q(nz) = dq(nz)). Preserving the surface-to-center ordering (k = 0 at the photosphere to k = nz - 1 at the center) ensures that optical-depth integration (\(\tau\) increasing inward from k = 0), atmospheric boundary conditions at the first block row (k = 0), and GYRE/FGONG reversal conventions map 1-to-1 with upstream MESA without reversing array strides during FFI calls.


4. Threading, Memory & GPU Environment Variables

Which environment variables control CPU thread pools and GPU memory allocation?

For reproducible multi-core benchmarks or concurrent multi-process runs, configure the following environment variables before launching Python:

# Cap Safe Rust rayon and BLAS/OpenMP worker threads
export RAYON_NUM_THREADS=4
export OMP_NUM_THREADS=4
export OPENBLAS_NUM_THREADS=4
export MKL_NUM_THREADS=4

# Prevent XLA from pre-allocating 75% of GPU VRAM when sharing a workstation GPU
export XLA_PYTHON_CLIENT_PREALLOCATE=false
export XLA_PYTHON_CLIENT_MEM_FRACTION=0.65

# Force CPU-only JAX execution if the GPU is occupied by another job
export JAX_PLATFORMS=cpu