- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 233
Make NonlinearSolve.jl an optional dependency #3850
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
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| module MTKNonlinearSolveExt | ||
|  | ||
| using ModelingToolkit | ||
| using NonlinearSolve | ||
| using SCCNonlinearSolve | ||
|  | ||
| # Export the TrustRegion algorithm for use in linearization | ||
| ModelingToolkit._get_default_nlsolve_alg() = TrustRegion() | ||
|  | ||
| end | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|  | @@ -52,8 +52,7 @@ import JuliaFormatter | |||||||||
| using MLStyle | ||||||||||
| import Moshi | ||||||||||
| using Moshi.Data: @data | ||||||||||
| using NonlinearSolve | ||||||||||
| import SCCNonlinearSolve | ||||||||||
| # NonlinearSolve and SCCNonlinearSolve are now loaded via extension | ||||||||||
| using ImplicitDiscreteSolve | ||||||||||
| using Reexport | ||||||||||
| using RecursiveArrayTools | ||||||||||
|  | @@ -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.") | ||||||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶 
        Suggested change
       
 | ||||||||||
|  | ||||||||||
| end # module | ||||||||||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||
| """ | ||||
| lin_fun, simplified_sys = linearization_function(sys::AbstractSystem, inputs, outputs; simplify = false, initialize = true, initialization_solver_alg = TrustRegion(), kwargs...) | ||||
| lin_fun, simplified_sys = linearization_function(sys::AbstractSystem, inputs, outputs; simplify = false, initialize = true, initialization_solver_alg = nothing, kwargs...) | ||||
|  | ||||
| Return a function that linearizes the system `sys`. The function [`linearize`](@ref) provides a higher-level and easier to use interface. | ||||
|  | ||||
|  | @@ -24,7 +24,7 @@ The `simplified_sys` has undergone [`mtkcompile`](@ref) and had any occurring in | |||
| - `outputs`: A vector of variables that indicate the outputs of the linearized input-output model. | ||||
| - `simplify`: Apply simplification in tearing. | ||||
| - `initialize`: If true, a check is performed to ensure that the operating point is consistent (satisfies algebraic equations). If the op is not consistent, initialization is performed. | ||||
| - `initialization_solver_alg`: A NonlinearSolve algorithm to use for solving for a feasible set of state and algebraic variables that satisfies the specified operating point. | ||||
| - `initialization_solver_alg`: A NonlinearSolve algorithm to use for solving for a feasible set of state and algebraic variables that satisfies the specified operating point. If `nothing` (default), a default algorithm will be used when NonlinearSolve.jl is loaded. | ||||
| - `autodiff`: An `ADType` supported by DifferentiationInterface.jl to use for calculating the necessary jacobians. Defaults to using `AutoForwardDiff()` | ||||
| - `kwargs`: Are passed on to `find_solvables!` | ||||
|  | ||||
|  | @@ -39,7 +39,7 @@ function linearization_function(sys::AbstractSystem, inputs, | |||
| op = Dict(), | ||||
| p = DiffEqBase.NullParameters(), | ||||
| zero_dummy_der = false, | ||||
| initialization_solver_alg = TrustRegion(), | ||||
| initialization_solver_alg = nothing, | ||||
| autodiff = AutoForwardDiff(), | ||||
| eval_expression = false, eval_module = @__MODULE__, | ||||
| warn_initialize_determined = true, | ||||
|  | @@ -81,9 +81,16 @@ function linearization_function(sys::AbstractSystem, inputs, | |||
| ps = parameters(sys) | ||||
| h = build_explicit_observed_function(sys, outputs; eval_expression, eval_module) | ||||
|  | ||||
| # Use default algorithm if none provided and initialization is enabled | ||||
| actual_solver_alg = if initialization_solver_alg === nothing && initialize | ||||
| _get_default_nlsolve_alg() | ||||
| else | ||||
| initialization_solver_alg | ||||
| end | ||||
|  | ||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [JuliaFormatter] reported by reviewdog 🐶 
        Suggested change
       | ||||
| initialization_kwargs = (; | ||||
| abstol = initialization_abstol, reltol = initialization_reltol, | ||||
| nlsolve_alg = initialization_solver_alg) | ||||
| nlsolve_alg = actual_solver_alg) | ||||
|  | ||||
| p = parameter_values(prob) | ||||
| t0 = current_time(prob) | ||||
|  | ||||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JuliaFormatter] reported by reviewdog 🐶