Skip to content

Fix reltol type instability in Newton solver #2823

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

Conversation

ChrisRackauckas-Claude
Copy link

Summary

Problem

The Newton solver had a type instability where reltol could be either:

  • A scalar (eps(eltype(dz))) when adaptive=false
  • A scalar or vector (integrator.opts.reltol) when adaptive=true

Since adaptive is checked at runtime, Julia couldn't determine the type at compile time, causing performance degradation visible in profiling.

Solution

Changed the condition from:

if integrator.opts.adaptive
    reltol = integrator.opts.reltol
else
    reltol = eps(eltype(dz))
end

To:

if \!integrator.opts.adaptive && \!(integrator.opts.reltol isa AbstractArray)
    reltol = eps(eltype(dz))
else
    reltol = integrator.opts.reltol
end

This ensures reltol is only set to eps(eltype(dz)) when it would be type-compatible (i.e., when the original reltol is not an array), avoiding the type instability.

Test plan

Fixes part of #2819

🤖 Generated with Claude Code

The issue was that reltol could be either a scalar (when adaptive=false)
or the value from integrator.opts.reltol (which could be scalar or vector).
This runtime branch caused type instability.

The fix checks both conditions at once to ensure reltol is only set to
eps(eltype(dz)) when it would actually be a scalar, avoiding the type
instability when reltol is a vector.

Fixes part of issue SciML#2819
@ChrisRackauckas ChrisRackauckas force-pushed the fix-reltol-type-instability branch from ac54fc1 to e619444 Compare August 9, 2025 20:40
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