Skip to content

Commit 431d270

Browse files
fix: require guesses for parameters
1 parent e715e28 commit 431d270

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/systems/nonlinear/initializesystem.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function generate_initializesystem(sys::ODESystem;
119119
push!(u0, varp => _val1)
120120
elseif _val3 !== nothing
121121
# assuming an equation exists (either via algebraic equations or initialization_eqs)
122-
push!(u0, varp => _val1)
122+
push!(u0, varp => _val3)
123123
elseif check_defguess
124124
error("Invalid setup: parameter $(p) has no default value, initial value, or guess")
125125
end
@@ -129,7 +129,7 @@ function generate_initializesystem(sys::ODESystem;
129129
push!(eqs_ics, varp ~ _val2)
130130
push!(u0, varp => _val2)
131131
elseif _val3 !== nothing
132-
push!(u0, varp => _val1)
132+
push!(u0, varp => _val3)
133133
elseif check_defguess
134134
error("Invalid setup: parameter $(p) has no default value, initial value, or guess")
135135
end
@@ -186,8 +186,8 @@ function is_parameter_solvable(p, pmap, defs, guesses)
186186
_val3 = get(guesses, p, nothing)
187187
# either (missing is a default or was passed to the ODEProblem) or (nothing was passed to
188188
# the ODEProblem and it has a default and a guess)
189-
return (_val1 === missing || _val2 === missing) ||
190-
(_val1 === nothing && _val2 !== nothing && _val3 !== nothing)
189+
return ((_val1 === missing || _val2 === missing) ||
190+
(_val1 === nothing && _val2 !== nothing)) && _val3 !== nothing
191191
end
192192

193193
function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p)

test/initializationsystem.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success
587587
pmap = Dict()
588588
pmap[q] = 1.0
589589
# `missing` default, equation from ODEProblem
590-
@mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => missing])
590+
@mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => missing], guesses = [p => 1.0])
591591
pmap[p] = 2q
592592
prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap)
593593
test_parameter(prob, p, 2.0)
@@ -605,7 +605,7 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success
605605
test_parameter(prob2, p, 2.0)
606606

607607
# `missing` to ODEProblem, equation from default
608-
@mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => 2q])
608+
@mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => 2q], guesses = [p => 1.0])
609609
pmap[p] = missing
610610
prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap)
611611
test_parameter(prob, p, 2.0)
@@ -652,9 +652,6 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success
652652
@mtkbuild sys = ODESystem([D(x) ~ x, p ~ x + y], t; guesses = [p => 0.0])
653653
@test_throws ModelingToolkit.MissingParametersError ODEProblem(
654654
sys, [x => 1.0, y => 1.0], (0.0, 1.0))
655-
@mtkbuild sys = ODESystem([D(x) ~ x, p ~ x + y], t)
656-
@test_throws ["Invalid setup", "parameter p", "guess"] ModelingToolkit.generate_initializesystem(
657-
sys; u0map = Dict(x => 1.0, y => 1.0), pmap = Dict(p => missing), check_defguess = true)
658655

659656
@testset "Null system" begin
660657
@variables x(t) y(t) s(t)
@@ -726,3 +723,13 @@ end
726723
@test init(prob2, Tsit5())[x] 0.5
727724
@test_nowarn solve(prob2, Tsit5())
728725
end
726+
727+
@testset "Equations for dependent parameters" begin
728+
@variables x(t)
729+
@parameters p q = 5 r
730+
@mtkbuild sys = ODESystem(D(x) ~ 2x + r, t; parameter_dependencies = [r ~ p + 2q, q ~ p + 3], guesses = [p => 1.0])
731+
prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => missing])
732+
@test length(equations(ModelingToolkit.get_parent(prob.f.initializeprob.f.sys))) == 4
733+
integ = init(prob, Tsit5())
734+
@test integ.ps[p] 2
735+
end

0 commit comments

Comments
 (0)