Skip to content

Fixes for finite difference stencils on nonuniform meshes (rotation) - #1144

Open
AstroBarker wants to merge 2 commits into
mainfrom
blb/rot-fd
Open

AstroBarker wants to merge 2 commits into
mainfrom
blb/rot-fd

Conversation

@AstroBarker

@AstroBarker AstroBarker commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This grew out of conversations with @Debraheem. There are a number of finite differences in star/private/rotation_mix_info.f90 for radial $$\partial_r$$ derivatives for mixing coefficients. These used stencils that were inconsistent on a non-uniform mesh.

TL;DR

The centered finite difference

$$ f'(x) \approx \frac{f(x+h) - f(x-h)}{2h}$$

and friends, which are used in the rotation code for estimating mixing coefficients, are only correct on a uniform mesh. For MESA we can basically guarantee that we do not have such a mesh.

Details

  • New pure module procedures ddr_nonuniform and ddr_one_sided.
  • setup: dRho_dr, dP_dr (which feeds dRho_dr_ad), dj_dr (which feeds dr2omega and Hj) and domega_dlnR now use ddr_nonuniform.
  • omega_shear and set_ST dlnomega_dlnr now use ddr_nonuniform.
  • New set_zone_scale_height(v, r, ktop, kbot, H) computes H = v/|dv/dr| over a contiguous unstable zone: the centered stencil inside, the one-sided stencil at both ends, and a two-point fallback for 2-face zones. SSI, ES and GSF all call it now in place of three copies of the same code.
  • Cleanup:
    • Removed the work arrays dRho, dPressure, domega, d_mu (unused) and d_j_rot.
    • In GSF, removed the unreachable kbot == 1 / ktop == nz branches and the k == -1 debug writes.
  • dr(:) now only feeds the dP/dr sign guard and debug output.

This PR updates these finite differences to use a form appropriate for non-uniform meshes.

Stencils

Define the signed spacings for each side of a face

$$ h_- = r(k-1) - r(k), h_+ = r(k) - r(k+1) $$

The generic second order finite difference is

$$ f'(k) \approx \frac{ h_+^2 (f(k-1) - f(k)) + h_-^2 (f(k) - f(k+1)) }{h_- h_+ (h_- + h_+) }$$

On a uniform mesh it reduces to the usual form. A one sided form is used at mixing boundaries.

These are implemented in elemental functions in num/public/num_lib.f90 and tested in num/test.

Impact

The 20M_z2m2_high_rotation test case seems to proceed with fewer retries compared to main. There is a large impact seen in the GSF diffusion coefficients, seen below (excuse the poor plot)
gsf
where the panels show the log of the GSF diffusion coefficient with the fixed stencils (blue) and without (orange). The old mixing profile is noisy, with jumps corresponding to changes in mesh spacing (shown hastily in the lower panels).

@zingale

zingale commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

not as critical, but I think that dlnX_dr, dlnY_dr, and dlnRho_dr in profile_getval.f90 also use the uniform-grid centered diff.

@AstroBarker

Copy link
Copy Markdown
Contributor Author

not as critical, but I think that dlnX_dr, dlnY_dr, and dlnRho_dr in profile_getval.f90 also use the uniform-grid centered diff.

Noted! I haven't poked around yet for where else this might be relevant.

@Debraheem Debraheem added enhancement New feature or request rotation Capabilities for modeling rotating stars labels Dec 1, 2026
@Debraheem

Copy link
Copy Markdown
Member

Very nice! I will take a deeper look in 24hr when i'm not traveling.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request rotation Capabilities for modeling rotating stars

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants