Skip to content

MUG: Bring Lagrange xMHD (xmhd_lag) to parity with the H(Curl) implementation - #16

Closed
hansec wants to merge 7 commits into
mainfrom
claude/nedelec-lagrange-xmhd-parity-rj8661
Closed

hansec wants to merge 7 commits into
mainfrom
claude/nedelec-lagrange-xmhd-parity-rj8661

Conversation

@hansec

@hansec hansec commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Primary changes

src/physics/xmhd_lag.F90 (all fields, including B, on Lagrange elements) now supports the same system of equations, time stepping, boundary conditions, and options as src/physics/xmhd.F90. It keeps its vector-Lagrange magnetic field with the k_db ∇∇·B divergence-cleaning term, so the periodic divergence cleaning used by xmhd is not needed (nclean is ignored).

The module was rebuilt from xmhd.F90, keeping only the magnetic-field-specific parts from the old Lagrange code. The field ordering (B, V, n, T), and therefore the restart layout, is unchanged. Newly supported in xmhd_lag:

  • Viscosity: nu_visc fluid viscosity (kin/iso), Braginskii parallel viscosity (nu_par), and gyroviscosity (nu_gyro).
  • Thermal conduction: separate ion/electron coefficients (kappa_par(2), kappa_perp(2)), chi_perp_e_min, and the res/diff/visc/cond_shaping functions.
  • Boundary conditions: bbc='bc'/'ic' (applied to the nodal B as fixed normal/tangential components), vbc='norm'/'tang'/'xml', and per-surface nbc/tbc='x'.
  • Rotationally periodic (revolved) domains for both B and V, including the multigrid interpolation operators.
  • Linear runs: xmhd_lin_bdf2, the backward-Euler start, and restart from one level down.
  • Solver and run options: dt_max, gmres_maxits/gmres_ninner, nl_update, per-level xmhd_nparts, profile_only, and the same row/column equation scaling as xmhd.
  • Bugs fixed in the old Lagrange code:
    • Solid-region velocity was zeroed on the input fields instead of the solution, so it had no effect.
    • The hyper-diffusion term was missing a factor of 2.
    • A hard-coded floor of 10 was applied to the electron perpendicular diffusivity.
    • The coarse-level multigrid averaging array had the wrong size.
    • Geometric multigrid levels did not work: the module mesh pointer stayed on the finest mesh, and the solid flags were per fine-mesh cell rather than per region.

Hyper-resistivity (eta_hyper>0) is carried over from the old Lagrange implementation unchanged. It is not verified, and is deferred to future work.

Tests and examples:

  • New Lagrange tests:
    • test_MUG_visc_lag, test_MUG_gyro_lag, test_MUG_hall_lag: compared with linear theory. They share their drivers and theory with the H(Curl) tests; the viscosity deviations match xmhd to about 6 digits.
    • test_MUG_revolved_lag: a Z-pinch equilibrium on a revolved wedge, with several boundary-condition combinations, multigrid, and MPI. It holds equilibrium to about 1e-8, and a negative control with the periodic rotation disabled makes it fail.
    • Backward-Euler linear Alfvén cases and geometric-multigrid Alfvén cases.
  • Updated test settings: the Lagrange Alfvén and sound settings now use nu_visc (same physics as before).
  • New example MUG_gyro_grav_lag: a Lagrange variant of the gyroviscous gravitational-instability example, with its own doc page.
  • Docs: doc_mug_main.md and doc_mug_xmhd_eqns.md describe the remaining differences between the two implementations (a new doc_mug_xmhd_eqns_lag section).

Secondary changes

  • Native linear algebra round-off (from a separate local session):
    • Native global dot products and sums now accumulate only owned entries, instead of summing everything and subtracting shared/periodic duplicates (base/oft_stitching.F90).
    • Square roots of self dot products in the native solvers are clamped at zero.
    • This fixes NaNs in serial multigrid runs with an exact LU smoother. It affects every tool that uses native linear algebra.
  • xmhd.F90 geometric multigrid fix: the same fix as in xmhd_lag: set the level mesh in xmhd_set_level and use per-region solid flags. Geometric MG levels previously segfaulted in xmhd. Matching geometric-MG tests were added to test_alfven.py.
  • Shared test helpers: test_MUG_visc.py and test_MUG_gyro.py take an exe argument so the Lagrange drivers can reuse them.

API changes

Input-file changes for xmhd_lag (now the same as xmhd):

  • nu_visc must now be set (use 0 for none). nu_perp is still accepted as a deprecated alias.
  • nu_par now means Braginskii parallel viscosity, added to nu_visc. It previously meant the isotropic fluid viscosity.
  • visc_type='ani' was removed.
  • kappa_par/kappa_perp are now 2-element (ion, electron) arrays.
  • New options: k_db (kept), bbc, dt_max, gmres_maxits, gmres_ninner, nl_update, xmhd_lin_bdf2. xmhd_nparts is now a per-level total.
  • xmhd_minlev now defaults to the finest level, as in xmhd (it was 3).

Fortran API: xmhd_run gains an optional profile_only argument. Public shaping procedure pointers and xmhd_B_eps were added. There are no API changes to xmhd.

Testing

  • Cloud sandbox (Ubuntu system libraries, OpenMPI, no PETSc):
    • The MUG suite (both modules), plus base and lin_alg, on the default (non-slow) selection: all pass except the two known environment-dependent test_native_bjacobi ILU iteration-count cases (77/76 vs 73/70).
    • The previously failing test_nl_r1_p3 and all new geometric-MG tests pass with the merged round-off fixes.
  • Local CI-like build (bundled MPICH, PETSc with SuperLU/SuperLU-DIST/MUMPS/UMFPACK): before the round-off fixes were merged, all Lagrange MUG tests passed, slow and PETSc included. Lint and the docs build were clean.

Follow-up before merging (in progress)

  • Switch test_pinch_mg to the faster block-Jacobi/LU multigrid input now that the NaN fix is in.
  • Gravitational-instability example: the Lagrange discrete equilibrium starts about 4000× further from force balance, so the default seed (eps=1e-8) hides the growth phase at β ≥ 1e-3. The example's seed/docs and run_scan.py's growth-rate fit need fixing.
  • Not on this branch: the CMake/FindOFT_PETSc changes needed to configure PETSc builds with UMFPACK.

Generated by Claude Code

claude and others added 7 commits November 19, 2026 00:23
…entation

Port the system of equations, time stepping, and options of xmhd to
xmhd_lag, keeping the vector Lagrange magnetic field with the k_db
divergence cleaning term (periodic divergence cleaning is not needed).

- nu_visc fluid viscosity (kin/iso), Braginskii parallel viscosity
  (nu_par), and gyroviscosity (nu_gyro); visc_type='ani' removed and
  nu_perp accepted as a deprecated alias for nu_visc
- Separate ion/electron conduction coefficients (kappa_par(2),
  kappa_perp(2)), chi_perp_e_min, shaping functions
- bbc ('bc','ic'), vbc 'norm'/'tang'/'xml', nbc/tbc 'x' per surface BCs
- Rotationally periodic (revolved) domains for B and V
- Linear runs: xmhd_lin_bdf2, Euler start, level-down restart
- dt_max, gmres_maxits/gmres_ninner, nl_update, per-level xmhd_nparts,
  profile_only, row/column equation scaling as in xmhd
- Fix geometric multigrid levels (active level mesh, per-region solid
  flags) and several bugs in the previous Lagrange implementation
- Hyper-resistivity (eta_hyper) kept but not verified

Tests and examples:
- New Lagrange viscosity, gyroviscosity, Hall, and revolved-domain tests
- Backward Euler linear Alfven tests for the Lagrange module
- Lagrange variant of the gyroviscous gravitational instability example
- Docs describe the differences between the two implementations

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NKQvs6SxkkHtwxEMXESsSK
Native dot products subtract shared/periodic duplicate entries from the
full local sum. When the Krylov residual nearly vanishes (eg. an exact LU
preconditioner inside an MG smoother) the next Arnoldi vector amplifies
round-off mismatches between duplicate entries, and w.w can evaluate to a
small negative number, giving NaN from SQRT. Clamp w.w and r.r to zero so
this is treated as a (lucky) breakdown. Fixes serial MUG MG failures when
SuperLU is the LU package.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Apply the same non-negative clamp used for the GMRES Arnoldi step to the
other places that take SQRT of a vector self-dot product, which can be
slightly negative due to shared/periodic duplicate entries: CG residual
norms, GMRES initial/re-orthogonalization/inner-convergence norms (real
and complex), Newton residual norms, ML residual reporting, native
vector 2-norms, and the finite-difference Jacobian step size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Native dot products and sums previously added all local entries and then
subtracted non-owned (shared/periodic) duplicates. When duplicates dominate
the sum this suffers catastrophic cancellation, and self dot-products could
evaluate to small negative values (eg. GMRES Arnoldi step after an exact
preconditioner), producing NaN norms.

Instead, build a per-entry 0/1 ownership weight on first use and accumulate
only owned entries in a single streaming loop. An integer weight multiply is
used rather than a logical mask, as GCC does not vectorize the masked
selection; with the weight the real-valued loops vectorize and are 4-8x
faster than the previous two-loop form in a standalone benchmark (gfortran
16, Apple M-series). With flang it ranges from ~10% slower (few boundary
entries) to ~4x faster (many boundary entries). Seams without boundary
entries keep the plain unmasked loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
xmhd_set_level did not update the module mesh pointer, so coarse-level
Jacobians and BC flags were built using the finest mesh. Set the mesh
from the level's FE representation, and replace the fine-mesh per-cell
solid flag with a per-region flag that is valid on all levels.

On this branch only xmhd.F90 changes: the Lagrange parity rewrite of
xmhd_lag.F90 already contains the equivalent fix (reg_solid and the
level mesh pointer in xmhd_set_level).

Add geometric MG regression tests (serial and MPI, nonlinear and linear)
to test_alfven.py and test_alfven_lag.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hansec hansec closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants