Skip to content

solver composition corrections - #1112

Merged
Debraheem merged 11 commits into
mainfrom
EbF/bugfix/solver_composition_corrections
Nov 17, 2026
Merged

Debraheem merged 11 commits into
mainfrom
EbF/bugfix/solver_composition_corrections

Conversation

@Debraheem

Copy link
Copy Markdown
Member

With llm machine help, I’ve been delving deep into MESA’s solver for some time and this has led to a somewhat surprising but welcome major bugfix to MESA’s Newton solver. The resulting changes in overall performance and more specifically MESA’s convergence properties are large.

Composition consistency

MESA’s Newton solver had an inconsistency where the solver checks in check1_chem would clip/floor negative abundance trials to 0, but wouldn’t update solver_dx, so the next solver iteration wouldn’t see the clip and would try to produce a negative or positive abundance change from an isotope with no abundance.

This issue is particularly prevalent near moving burning fronts, like the H burning shell climbing up the RGB in a low mass star, since a narrow burning region burns one or a few zones at a time. When an isotope’s abundance in those zones drops to 0, this issue comes out. This is likely one of the reasons why it took so long to get to the He flash in current MESA. The solver needed careful timesteps to resolve the decay to 0 so that sudden clipping of negative trial abundances (he3 in the RGB example) didn’t destabilize the solver.

The fix updates the composition components of solver_dx and xa_sub_xa_start when the composition checks change the abundance. This keeps the stored abundance change consistent with the abundance used to evaluate the equations.

Screenshot 2026-11-09 at 4 11 51 PM

Tests and run times

The comparisons use the dedt energy equation and fully implicit burning. The runs used an Apple M1 Max with MESA SDK 26.6.1 and eight cores, with export OMP_NUM_THREADS=8. Times include initialization and output, and exclude compilation.

The original timing comparisons and figures were produced before the final three followup commits described below. The new limit-50 calculation includes these fixes and the updated helium luminosity defaults.

1M_pre_ms_to_wd: RGB evolution

These calculations cover the pre-main sequence through the onset of the helium flash. I compare hydrogen abundance change limits of dX_div_X_limit(1) = 0.9d0, 5d0, 10d0 and 50d0 during RGB evolution, keeping time_delta_coeff = 1.

dX_div_X_limit(1) Version Accepted steps Newton iterations Total retries Final rel_run_E_err magnitude Time (min)
0.9 main 10,557 62,163 1 1.22e-9 80.19
0.9 Solver correction branch 10,571 62,212 1 1.07e-9 78.81
5 main 2,807 31,516 85 1.71e-7 34.23
5 Solver correction branch 2,699 23,587 27 2.36e-9 28.82
10 main 3,218 50,685 647 2.21e-7 57.67
10 Solver correction branch 1,719 16,703 1 2.61e-10 20.86
50 Solver correction branch 906 9,400 1 6.45e-8 11.22

The table includes model construction and rejected solves. Each calculation has one construction retry on the first model coming out of the pre-ms relaxation. During RGB evolution itself, the limit-10 calculation has 0 retries on the solver correction branch versus 646 on main. The limit-50 calculation uses a hydrogen abundance change limit of 10 before RGB evolution and 50 during RGB evolution.

Compared with main at the original limit of 0.9, the solver correction branch at 10 reaches the helium flash 3.8 times faster, using 6.1 times fewer accepted steps, while retaining better numerical energy conservation. With both versions using a limit of 10, the solver correction branch is 2.8 times faster. See the figure below.

Screenshot 2026-11-09 at 4 13 52 PM

Generally, I find that reducing the timestep makes each solve less nonlinear, and hence the short timestep limit of this change is analogous to main. This is reflected in the similar results with dX_div_X_limit(1) = 0.9d0. What this pr does, is allow MESA to take larger timesteps while still converging to the same level. This does not necessarilly mean the calculation is more accurate with larger timesteps, but rather it just means that MESA can find a solution to the precision required by the tolerances and equations supplied.

I have also changed the defaults for lgL_He_burn_min to -0.5d0 and lgL_He_burn_relative_limit to 5d0, which allows the helium luminosity timestep control to act earlier. With these changes, our most aggressive test with dX_div_X_limit(1) = 50d0 reaches the He flash in 906 timesteps from the pre-main sequence, including 611 RGB timesteps with 0 retries. The full calculation takes 11.22 minutes, with 9,400 Newton iterations and one construction retry. See below.

611_timesteps_rgb_evolution_every3

This PR also allows the normal dX_div_X_limit control to be set above 1 and changes the default hydrogen limit to dX_div_X_limit(1) = 10d0.

He-flash to core-He depletion

I continued both limit-0.9 models through the helium flash and core-helium burning to central helium mass fractions below 1d-4.

Version Accepted steps Newton iterations Retries Final rel_run_E_err magnitude Time (min)
main 3,045 17,574 50 3.71e-8 23.83
Solver correction branch 3,037 17,512 45 8.45e-9 25.08

The cumulative energy errors include the preceding evolution. Restricting the accumulation to this helium evolution gives relative errors of 3.43e-8 on main and 6.02e-9 on the solver correction branch.

Over the helium flash, timesteps are primarily controlled by the helium burning luminosity, so the convergence improvements do not manifest as strongly there. The Newton work remains similar, while the cumulative energy error is smaller on the solver correction branch.

20M_pre_ms_to_core_collapse

Both versions start from the same model after central carbon depletion and use the same physical, mesh and timestep controls, with fully implicit burning.

inlist_to_lgTmax

Evolution through a maximum temperature of $10^{9.6}$ K:

Version History models Newton iterations Retries Time (min)
main 670 5,402 44 12.30
Solver correction branch 620 5,077 32 11.67

inlist_to_cc

Evolution from that point to an iron core infall speed of 100 km/s:

Version History models Newton iterations Retries Time (min)
main 912 12,903 138 37.17
Solver correction branch 504 5,913 69 17.57

Both inlists combined

Version History models Newton iterations Retries Time (min)
main 1,582 18,305 182 49.51
Solver correction branch 1,124 10,990 101 29.27

The core collapse inlist runs 2.1 times faster, with 54% fewer Newton iterations and 50% fewer retries. Over both inlists, the calculation runs 1.7 times faster, with 40% fewer Newton iterations and 45% fewer retries.

The final rel_run_E_err magnitude decreases from 8.57e-4 to 3.57e-4, although the maximum per-step rel_E_err magnitude is higher on the solver correction branch: 3.56e-5 versus 1.41e-5.

The central density and temperature evolution also differs, which could have implications elsewhere.

Other changes on the branch

  • Added consistency checks for other linear solver variables that can also be clipped, after finding the composition issue.
  • Fixed the line search since it mistakenly assumed a full initial step in the first quadratic backtracking formula, regardless of the value of alam. This did not substantially change my results, but it is certainly a bug to fix.
  • Saved the Newton state in dxsave and the correction workspace in ddxsave before either evaluation path. Previously, a failed trial could restore stale or uninitialized solver values when the initial merit evaluation was skipped, such as during the first iterations of a timestep. The correction workspace is also initialized before saving it.

Final three followup commits

The original comparison runs predate these changes. Their effects have been small in my subsequent testing, and I can move them into a separate PR if preferred:

  • Removed duplicate counting of composition residuals from the average residual calculation.
  • Calculated composition correction averages independently in each cell, removing the artificial dependence of the correction norm on cell order.
  • Added the temperature and density derivatives of the ppII and ppIII branching fractions, after noticing departures between finite difference and analytic partials while investigating RGB evolution and the helium flash.

Further testing

My guess is that runtime in the test_suite can be substantially reduced. In cases such as 1M_thermohaline and c13_pocket, I suspect substantial improvements, potentially factors of 2–4, but we’ll have to test and see.

The broader implications are still unclear, but this is certainly a bug we need to fix. I checked the history and it appears to have been present for a very long time, potentially since MESA’s inception, although the solver worked slightly differently before.

I have not attempted relaxing dX_div_X_limit(2) = 0.9d0 for helium yet, but it would be the next thing to try relaxing.

Synchronize solver_dx when the native checks correct RSP2 w, clip
alpha_RTI, or bound w_div_w_crit_roche. Update dxh_alpha_RTI as well
so the RTI time term uses the evaluated alpha_RTI.

Replace the unreachable negative alpha_RTI check after max(0, x)
with a comparison against the raw trial value. Retain stored changes
when the physical variable is unchanged.
Set dX_div_X_limit(1) to 10 by default and keep the relative abundance
checks active above 1. Remove redundant 1M controls and the explicit
relative limits in the thermohaline and C13 pocket cases.

Keep the full 1M runner enabled and preserve PGSTAR. Use fully
implicit burning in the 20M case, retain its comparison controls,
and restore normal profile intervals with solver progress output off.
Remove the run_all restart recommendation from the massive star READMEs.
@Debraheem Debraheem added bug Something isn't working enhancement New feature or request numerics issues with math/num/mtx modules solver labels Nov 9, 2026
Include the temperature and density response of the Be7 capture branching fractions in the combined pp rates. Retain the rate values and constant half-branch behavior for negligible capture rates.

Validation: the same-state heating derivative discrepancy falls from about 9e-7 to 1e-12 in the scaled energy equation. The rate-value evaluations are unchanged.
The full equation loop already includes composition. Remove the obsolete
composition pass so each included residual contributes once to the mean.
Maximum residuals, equation weights and the Newton system are unchanged.

The older structural loop and separate composition loop were disjoint in
r1396. Duplicate counting is present by r3731 and predates public Git
history, so no introducing PR has been verified.

The extracted sizequ passed 57 numerical checks covering weighted means,
ignored equations, maximum residuals and nonfinite inputs.
@Debraheem
Debraheem force-pushed the EbF/bugfix/solver_composition_corrections branch from 29d3119 to 7f47e7f Compare November 9, 2026 21:47
Debraheem added a commit that referenced this pull request Nov 9, 2026
Merge PRs #1111 and #1112 into the split/merge AMR branch.
…d partials in 20M, update standard mod files.
…hell merger post-stripping from undoing gamma1 instability and stalling the marger + speed ups
Debraheem added a commit that referenced this pull request Nov 17, 2026
Integrate the published solver branch from PR #1112 for combined testing. Preserve the diffusion controls and use the solver branch active Newton settings in the 20M case.
@Debraheem
Debraheem merged commit 8baceed into main Nov 17, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request numerics issues with math/num/mtx modules solver

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant