Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Derivatives for Cubic Interpolation in Composition for Opacity #593

Open
evbauer opened this issue Oct 19, 2023 · 5 comments
Open
Labels
enhancement New feature or request hackathon Potential issue to be tackled on a hackathon kap Opacity module

Comments

@evbauer
Copy link
Member

evbauer commented Oct 19, 2023

Right now, our default is to do linear interpolation in composition (X,Z) for opacity tables. Even though cubic interpolation returns more realistic opacities, it returns much worse derivatives. Turning on cubic interpolation by default results in a number of test suite failures: https://testhub.mesastar.org/test_kap_cubic_splines/commits

It seems a big part of the issue is that we interpolate the derivatives from the tables, rather than taking derivatives of the interpolants. This may require a fairly significant amount of effort to refactor and overhaul how the code approaches composition interpolation.

call interp1(logKs, logK, ierr)
if (ierr /= 0) then
call mesa_error(__FILE__,__LINE__,'failed in interp1 for logK')
return
end if
call interp1(dlogKs_dlogRho, dlogK_dlogRho, ierr)
if (ierr /= 0) then
call mesa_error(__FILE__,__LINE__,'failed in interp1 for dlogK_dlogRho')
return
end if
call interp1(dlogKs_dlogT, dlogK_dlogT, ierr)
if (ierr /= 0) then
call mesa_error(__FILE__,__LINE__,'failed in interp1 for dlogK_dlogT')
return
end if

To get a sense of how bad the derivatives are, here are some dfridr plots from @Debraheem at X = 0.625, Z = 0 when using cubic interpolation.

image
image
image
image

@evbauer evbauer added enhancement New feature or request kap Opacity module hackathon Potential issue to be tackled on a hackathon labels Oct 19, 2023
@evbauer
Copy link
Member Author

evbauer commented Jan 3, 2024

Incorporating autodiff for the composition interpolation helps substantially (see f373537).

The dfridr plot at X=0.625, Z=0 now looks like this:
iTerm2 gskssA kap_plotter

If we add in some metallicity that is between tabulation points, the plot for X=0.625, Z=5e-4 looks somewhat worse:
iTerm2 yv3rvE cubic

So we still want to investigate where these residual bad stripes are coming from, but even this is substantially better than what things looked like previously.

@evbauer
Copy link
Member Author

evbauer commented Jan 4, 2024

Zooming in on the area with the stripes shows that these bad derivatives occur right along lines of constant logT and logR corresponding to grid points in the underlying opacity tabulations.
iTerm2 MPzYyv kap_plotter

I think the dfridr badness is actually mostly an artifact of the fact that the underlying derivatives have discontinuities across grid points, which perhaps shouldn't be shocking. Here's a zoom in on the structure of the logT derivative when doing cubic composition interpolation (with autodiff):
iTerm2 ewoC2l dlogT_cubic

And for comparison here's the derivative when doing linear composition interpolation:
iTerm2 e8vWIS dlogT_linear

@evbauer
Copy link
Member Author

evbauer commented Jan 4, 2024

This might improve if we allowed the composition interpolation to use something other than the "piecewise monotonic" cubic interpolation routines. Could we relax that so that we maybe get less discontinuities in the derivatives of the interpolants?

Or should we declare victory and say that the quality of these derivatives is good enough?

@fxt44 do you have any thoughts?

@fxt44
Copy link
Member

fxt44 commented Jan 4, 2024

as long as this effort is in experimental mode, maybe do the experiment of trying a different cubic. even if it ends up doing little in this opacity case, it might be informative for other cubic interpolation use cases.

@Debraheem
Copy link
Member

Debraheem commented Jan 4, 2024

Using the monotonicity preserving interpolation routines e.g. interp_mp -> interp_m3a, interp_m3b, interp_m3q in place of the peace-wise monotonic spline used to generate Evan's plots (interp_pm) from $MESA_DIR/interp_1d/public/interp_lib.f90

where a, q, b stand for the (average, quartic, and superbee limiters) MESA uses citing:
Huynh, H.T., "Accurate Monotone Cubic Interpolation", SIAM J Numer. Anal. (30) 1993, 57-100.
Suresh, A, and H.T. Huynh, "Accurate Monotonicity-Preserving Schemes with Runge-Kutta Time Stepping", JCP (136) 1997, 83-99.

I remade the same two (dfridr and dlogkapdlogT) plots for each.

m3a
dfridr_m3a

derivative_m3a
dfridr_m3a

m3b
dfridr_m3b
derivative_m3b

m3q

dfridr_m3q

derivative_m3q

These monotinicity presrvering routines don't seem to do much better in terms reducing discontinuities in the derivative of the interpolant.

@pmocz pmocz added this to Opacities May 31, 2024
@pmocz pmocz moved this to In progress in Opacities May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hackathon Potential issue to be tackled on a hackathon kap Opacity module
Projects
Status: In progress
Development

No branches or pull requests

4 participants