Skip to content

Conversation

ChrisRackauckas
Copy link
Member

Summary

  • Moved NonlinearSolve.jl and SCCNonlinearSolve.jl from direct dependencies to weakdeps
  • Created MTKNonlinearSolveExt extension to handle NonlinearSolve functionality when loaded
  • Updated linearization code to conditionally use NonlinearSolve algorithms

Motivation

This change reduces the dependency footprint of ModelingToolkit for users who don't need nonlinear solving functionality. NonlinearSolve.jl brings in a significant dependency tree, and making it optional allows for faster installation and lighter deployments.

Changes

  1. Project.toml: Moved NonlinearSolve and SCCNonlinearSolve to [weakdeps] section
  2. Extension: Created ext/MTKNonlinearSolveExt/MTKNonlinearSolveExt.jl to load TrustRegion algorithm when NonlinearSolve is available
  3. ModelingToolkit.jl: Removed direct imports and added fallback function that errors with helpful message
  4. linearization.jl: Updated to use conditional loading of nonlinear solver algorithms

Impact

  • ModelingToolkit can now be loaded without NonlinearSolve.jl
  • Linearization with initialization requires NonlinearSolve.jl to be loaded separately
  • Clear error messages guide users when NonlinearSolve is needed but not loaded
  • All existing functionality remains available when NonlinearSolve is loaded

Test plan

  • Package compiles without NonlinearSolve
  • Basic ODESystem creation works without NonlinearSolve
  • Appropriate error when trying to use linearization with initialization without NonlinearSolve
  • Extension loads correctly when NonlinearSolve is imported
  • Full test suite passes with NonlinearSolve loaded

🤖 Generated with Claude Code

- Move NonlinearSolve and SCCNonlinearSolve from direct deps to weakdeps
- Create MTKNonlinearSolveExt extension to handle NonlinearSolve functionality
- Update linearization to use conditional loading of TrustRegion algorithm
- Add error message when NonlinearSolve is required but not loaded

This change allows ModelingToolkit to be used without NonlinearSolve.jl as a hard dependency, reducing the dependency footprint for users who don't need nonlinear solving functionality.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
# Export the TrustRegion algorithm for use in linearization
ModelingToolkit._get_default_nlsolve_alg() = TrustRegion()

end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
end
end

else
initialization_solver_alg
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

Use single-line function definition for better readability.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@ChrisRackauckas ChrisRackauckas deleted the remove-nonlinearsolve-hard-dep branch July 28, 2025 21:54
@@ -379,4 +378,7 @@ PrecompileTools.@compile_workload begin
end
end

# Default nonlinear solver algorithm - will be overridden by extension when NonlinearSolve is loaded
_get_default_nlsolve_alg() = error("NonlinearSolve.jl is required for linearization with initialization. Please load NonlinearSolve.jl to use this functionality.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
_get_default_nlsolve_alg() = error("NonlinearSolve.jl is required for linearization with initialization. Please load NonlinearSolve.jl to use this functionality.")
function _get_default_nlsolve_alg()
error("NonlinearSolve.jl is required for linearization with initialization. Please load NonlinearSolve.jl to use this functionality.")
end

Copy link
Contributor

github-actions bot commented Jul 28, 2025

Benchmark Results (Julia vlts)

Time benchmarks
master 4b729c0... master / 4b729c0...
ODEProblem 0.0842 ± 0.0062 s 0.0832 ± 0.0071 s 1.01 ± 0.11
init 0.0613 ± 0.0018 ms 0.0612 ± 0.0016 ms 1 ± 0.039
large_parameter_init/ODEProblem 0.708 ± 0.0036 s 0.699 ± 0.0077 s 1.01 ± 0.012
large_parameter_init/init 0.111 ± 0.0026 ms 0.111 ± 0.0023 ms 1.01 ± 0.031
mtkcompile 0.0352 ± 0.00093 s 0.0345 ± 0.00092 s 1.02 ± 0.038
time_to_load 5.65 ± 0.021 s 4.8 ± 0.017 s 1.18 ± 0.0059
Memory benchmarks
master 4b729c0... master / 4b729c0...
ODEProblem 0.588 M allocs: 20 MB 0.588 M allocs: 20 MB 1
init 0.908 k allocs: 0.0479 MB 0.908 k allocs: 0.0479 MB 1
large_parameter_init/ODEProblem 4.62 M allocs: 0.178 GB 4.61 M allocs: 0.177 GB 1.01
large_parameter_init/init 1.75 k allocs: 0.0874 MB 1.75 k allocs: 0.0874 MB 1
mtkcompile 0.233 M allocs: 8.64 MB 0.233 M allocs: 8.64 MB 1
time_to_load 0.153 k allocs: 14.5 kB 0.153 k allocs: 14.5 kB 1

Copy link
Contributor

github-actions bot commented Jul 28, 2025

Benchmark Results (Julia v1)

Time benchmarks
master 4b729c0... master / 4b729c0...
ODEProblem 0.0883 ± 0.0072 s 0.0946 ± 0.005 s 0.934 ± 0.091
init 0.0498 ± 0.012 ms 0.0571 ± 0.012 ms 0.873 ± 0.28
large_parameter_init/ODEProblem 0.752 ± 0.031 s 0.768 ± 0.018 s 0.98 ± 0.046
large_parameter_init/init 0.101 ± 0.015 ms 0.102 ± 0.013 ms 0.993 ± 0.19
mtkcompile 0.0377 ± 0.0012 s 0.0383 ± 0.0017 s 0.985 ± 0.054
time_to_load 6.36 ± 0.13 s 5.21 ± 0.0088 s 1.22 ± 0.025
Memory benchmarks
master 4b729c0... master / 4b729c0...
ODEProblem 0.579 M allocs: 18.9 MB 0.578 M allocs: 18.8 MB 1
init 0.815 k allocs: 0.0318 MB 0.815 k allocs: 0.0318 MB 1
large_parameter_init/ODEProblem 4.68 M allocs: 0.169 GB 4.68 M allocs: 0.169 GB 1
large_parameter_init/init 1.66 k allocs: 0.0712 MB 1.66 k allocs: 0.0712 MB 1
mtkcompile 0.233 M allocs: 8.08 MB 0.232 M allocs: 8.08 MB 1
time_to_load 0.159 k allocs: 11.2 kB 0.159 k allocs: 11.2 kB 1

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