Skip to content

Variational MHD: relative Newton stopping criteria, en_thermo fix, low-mode TAE examples for the linear models - #824

Merged
spossann merged 1 commit into
develfrom
feature/variational-relative-newton
Oct 23, 2026
Merged

spossann merged 1 commit into
develfrom
feature/variational-relative-newton

Conversation

@nicolominichelli

Copy link
Copy Markdown
Contributor

Summary

Follow-up of #818, motivated by comparing the linear variational MHD models with LinearMHD on the low-mode TAE.

Relative stopping criteria in the variational propagators

The Newton/Picard iterations stopped at an absolute residual (err < tol**2). For the small states of the linear and perturbative models (perturbation amplitude 1e-3) this was effectively a relative tolerance of ~1e-4: in the low-mode TAE the energy drifted by ~1e-5 per step, and the iterations stalled at the accuracy of the (also absolute) linear solves, running to maxiter.

  • VariationalPBEvolve, VariationalQBEvolve, VariationalDensityEvolve, VariationalMagFieldEvolve, VariationalEntropyEvolve, VariationalViscosity, VariationalResistivity: stop when err < tol**2 * min(|state at t^n|**2, 1). This is relative for small states and the previous absolute criterion for full models (norm >= 1), so the full models behave as before.
  • The Newton (Schur) solves, the implicit u/B solves of viscosity/resistivity and the inverse-mass error norms are done for normalized vectors, so their (absolute) solver tolerances act as relative ones.
  • Stagnation check: stop with a warning after 3 iterations without decrease.
  • Shared helpers NewtonConvergence, dot_normalized, inv_norm2 in feec/variational_utilities.py; option docs updated.
  • VariationalMomentumAdvection is unchanged: its Newton (approximate Jacobian) is not monotone and diverged with the relative criterion; it is not used by the linear models.

Preconditioner of the PB/QB Schur solves

VariationalPBEvolve and VariationalQBEvolve precondition their Schur solves with the fixed MassMatrixPreconditioner of Mrho instead of an inner PCG solve (not a linear operator, and expensive): ~2x faster per step for the p model, same iteration count. A better preconditioner (the ~170 Schur iterations per step) is left for a separate PR.

Thermal energy diagnostic

en_thermo / en_thermo_l1 of VariationalPerturbativeMHD and en_thermo of VariationalMHD_with_p computed int p^3 / sqrt(g) d eta instead of int p dx / (gamma - 1). This is wrong in curved geometry, and inconsistent with the energy conserved by VariationalPBEvolve.

Divergence-free initial velocity

project_div_free_velocity: Mv-orthogonal projection of an H1vec velocity onto the kernel of the discrete divergence div Uv of the variational schemes. Its CG has no preconditioner and stops at its iteration limit on 16x16x8 (|div u| 5.7e-5 -> 2.6e-9); this could use the same kind of preconditioner as above.

Examples

examples/VariationalPerturbativeLinearMHD/with_p|with_q/lowmode_tae: the low-mode TAE of examples/LinearMHD/lowmode_tae for VariationalPerturbativeMHD(_with_q) with linear=True. They use first-order time stepping like LinearMHD (implicit_weights=False for q), a divergence-free initial velocity and Dirichlet walls (u.n = 0, b.n = 0). README table updated (also the missing LinearMHD/lowmode_tae).

Test change

test_same_as_full_f (perturbative vs full-f q model): threshold 1e-10 -> 1e-6 relative. The two runs agreed to round-off only because the absolute tolerance made both go through the same arithmetic; with relative criteria they agree to the solver tolerance (measured 4e-8). Tighter solver tolerances are not an option: below ~1e-11 the Schur solves break down with NaN (pre-existing, also on devel; BiCGStab with an inner-iterative preconditioner and no breakdown guard). To be addressed separately.

Tests

  • Energy conservation, low-mode TAE test grid, 8 steps: total drift -4.2e-10 (p), -4.4e-10 (q) (was 6.3e-4 and 4.9e-5).
  • Default model tests (2 ranks): VariationalMHD, VariationalMHD_with_p, VariationalMHD_with_q, VariationalPerturbativeMHD, VariationalPerturbativeMHD_with_q.
  • test_verif_VariationalPerturbativeMHD_with_q.py (2 ranks): 13 passed.
  • Variational propagator tests and test_variational_dissipation.py (serial; they are skipped under MPI / not MPI-safe): passed.
  • test_examples.py for the two new examples: passed.
  • Not run: the non-MHD fluid models (VariationalBarotropicFluid, VariationalCompressibleFluid, VariationalPressurelessFluid, ViscousFluid), which also use these propagators.

🤖 Generated with Claude Code

- Newton/Picard stopping criteria of the variational propagators (PB, QB, density,
  magnetic field, entropy, viscosity, resistivity) relative to the state at t^n
  (err < tol^2 min(|state|^2, 1)): the absolute tolerance was far too loose for the
  small states of the linear and perturbative models (energy drift ~1e-5 per step in
  the low-mode TAE). Linear solves and inverse-mass error norms are done for normalized
  vectors, so their tolerances are relative too. New stagnation check (3 iterations
  without decrease). Shared helpers NewtonConvergence, dot_normalized and inv_norm2 in
  feec/variational_utilities.py. VariationalMomentumAdvection is unchanged.
- Schur solves of VariationalPBEvolve and VariationalQBEvolve preconditioned with the
  fixed MassMatrixPreconditioner instead of an inner PCG solve (2x faster for the p model).
- en_thermo (and en_thermo_l1) of VariationalPerturbativeMHD and VariationalMHD_with_p:
  int p dx / (gamma - 1) (was int p^3 / sqrt(g) d eta, wrong in curved geometry).
- project_div_free_velocity: Mv-orthogonal projection of an H1vec velocity onto the
  kernel of the discrete divergence of the variational schemes.
- Examples examples/VariationalPerturbativeLinearMHD/with_p|with_q/lowmode_tae: the
  low-mode TAE of LinearMHD for the linear variational models (first order in time,
  divergence-free initial velocity).
- test_same_as_full_f: threshold 1e-6 (the runs agree up to the solver tolerance).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@spossann

Copy link
Copy Markdown
Member

@nicolominichelli nice! if it is ready for review, please request a review from me, otherwise I will wait. Do you have time comparisons for the 5 linear MHD models already (lowmode_tae)?

@nicolominichelli

Copy link
Copy Markdown
Contributor Author

I am currently trying to improve preconditioning in the variational models since they are currently 20x slower. There seems to be room for improvement without having to move to curlcurl preconditioner yet

@spossann

Copy link
Copy Markdown
Member

I am currently trying to improve preconditioning in the variational models since they are currently 20x slower. There seems to be room for improvement without having to move to curlcurl preconditioner yet

If you want to profile a run I suggest to use the ProfileManager: https://struphy-hub.github.io/docs-preview/pr-823/sections/userguide.html#profilemanager

@spossann
spossann merged commit 08b863c into devel Oct 23, 2026
28 checks passed
@spossann
spossann deleted the feature/variational-relative-newton branch October 23, 2026 13:43
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