Skip to content

Commit 691747a

Browse files
feat: use SCCNonlinearProblem for initialization
1 parent 27d7f70 commit 691747a

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
4444
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
4545
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
4646
RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
47+
SCCNonlinearSolve = "9dfe8606-65a1-4bb3-9748-cb89d1561431"
4748
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
4849
SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226"
4950
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
@@ -126,7 +127,8 @@ REPL = "1"
126127
RecursiveArrayTools = "3.26"
127128
Reexport = "0.2, 1"
128129
RuntimeGeneratedFunctions = "0.5.9"
129-
SciMLBase = "2.64"
130+
SCCNonlinearSolve = "1.0.0"
131+
SciMLBase = "2.65"
130132
SciMLStructures = "1.0"
131133
Serialization = "1"
132134
Setfield = "0.7, 0.8, 1"

src/ModelingToolkit.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ using Distributed
5050
import JuliaFormatter
5151
using MLStyle
5252
using NonlinearSolve
53+
import SCCNonlinearSolve
5354
using Reexport
5455
using RecursiveArrayTools
5556
import Graphs: SimpleDiGraph, add_edge!, incidence_matrix

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem,
13011301
initialization_eqs = [],
13021302
fully_determined = nothing,
13031303
check_units = true,
1304+
use_scc = true,
13041305
kwargs...) where {iip, specialize}
13051306
if !iscomplete(sys)
13061307
error("A completed system is required. Call `complete` or `structural_simplify` on the system before creating an `ODEProblem`")
@@ -1318,8 +1319,8 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem,
13181319
end
13191320

13201321
ts = get_tearing_state(isys)
1321-
if warn_initialize_determined &&
1322-
(unassigned_vars = StructuralTransformations.singular_check(ts); !isempty(unassigned_vars))
1322+
unassigned_vars = StructuralTransformations.singular_check(ts)
1323+
if warn_initialize_determined && !isempty(unassigned_vars)
13231324
errmsg = """
13241325
The initialization system is structurally singular. Guess values may \
13251326
significantly affect the initial values of the ODE. The problematic variables \
@@ -1381,9 +1382,11 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem,
13811382
end
13821383
for (k, v) in u0map)
13831384
end
1384-
if neqs == nunknown
1385-
NonlinearProblem(isys, u0map, parammap; kwargs...)
1385+
1386+
TProb = if neqs == nunknown && isempty(unassigned_vars)
1387+
use_scc && neqs > 0 && is_split(isys) ? SCCNonlinearProblem : NonlinearProblem
13861388
else
1387-
NonlinearLeastSquaresProblem(isys, u0map, parammap; kwargs...)
1389+
NonlinearLeastSquaresProblem
13881390
end
1391+
TProb(isys, u0map, parammap; kwargs...)
13891392
end

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ function SciMLBase.SCCNonlinearProblem{iip}(sys::NonlinearSystem, u0map,
692692
push!(subprobs, prob)
693693
end
694694

695-
return SCCNonlinearProblem(subprobs, explicitfuns)
695+
return SCCNonlinearProblem(subprobs, explicitfuns, sys, p)
696696
end
697697

698698
"""

src/systems/problem_utils.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ Keyword arguments:
541541
Only applicable if `warn_cyclic_dependency == true`.
542542
- `substitution_limit`: The number times to substitute initial conditions into each
543543
other to attempt to arrive at a numeric value.
544+
- `use_scc`: Whether to use `SCCNonlinearProblem` for initialization if the system is fully
545+
determined.
544546
545547
All other keyword arguments are passed as-is to `constructor`.
546548
"""
@@ -554,7 +556,7 @@ function process_SciMLProblem(
554556
symbolic_u0 = false, warn_cyclic_dependency = false,
555557
circular_dependency_max_cycle_length = length(all_symbols(sys)),
556558
circular_dependency_max_cycles = 10,
557-
substitution_limit = 100, kwargs...)
559+
substitution_limit = 100, use_scc = true, kwargs...)
558560
dvs = unknowns(sys)
559561
ps = parameters(sys)
560562
iv = has_iv(sys) ? get_iv(sys) : nothing
@@ -607,7 +609,7 @@ function process_SciMLProblem(
607609
sys, t, u0map, pmap; guesses, warn_initialize_determined,
608610
initialization_eqs, eval_expression, eval_module, fully_determined,
609611
warn_cyclic_dependency, check_units = check_initialization_units,
610-
circular_dependency_max_cycle_length, circular_dependency_max_cycles)
612+
circular_dependency_max_cycle_length, circular_dependency_max_cycles, use_scc)
611613
initializeprobmap = getu(initializeprob, unknowns(sys))
612614

613615
punknowns = [p

0 commit comments

Comments
 (0)