Skip to content

Commit

Permalink
fix: require guesses for parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Sep 26, 2024
1 parent e715e28 commit 431d270
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/systems/nonlinear/initializesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function generate_initializesystem(sys::ODESystem;
push!(u0, varp => _val1)
elseif _val3 !== nothing
# assuming an equation exists (either via algebraic equations or initialization_eqs)
push!(u0, varp => _val1)
push!(u0, varp => _val3)
elseif check_defguess
error("Invalid setup: parameter $(p) has no default value, initial value, or guess")
end
Expand All @@ -129,7 +129,7 @@ function generate_initializesystem(sys::ODESystem;
push!(eqs_ics, varp ~ _val2)
push!(u0, varp => _val2)
elseif _val3 !== nothing
push!(u0, varp => _val1)
push!(u0, varp => _val3)
elseif check_defguess
error("Invalid setup: parameter $(p) has no default value, initial value, or guess")
end
Expand Down Expand Up @@ -186,8 +186,8 @@ function is_parameter_solvable(p, pmap, defs, guesses)
_val3 = get(guesses, p, nothing)
# either (missing is a default or was passed to the ODEProblem) or (nothing was passed to
# the ODEProblem and it has a default and a guess)
return (_val1 === missing || _val2 === missing) ||
(_val1 === nothing && _val2 !== nothing && _val3 !== nothing)
return ((_val1 === missing || _val2 === missing) ||
(_val1 === nothing && _val2 !== nothing)) && _val3 !== nothing
end

function SciMLBase.remake_initializeprob(sys::ODESystem, odefn, u0, t0, p)
Expand Down
17 changes: 12 additions & 5 deletions test/initializationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success
pmap = Dict()
pmap[q] = 1.0
# `missing` default, equation from ODEProblem
@mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => missing])
@mtkbuild sys = ODESystem([D(x) ~ x * q, D(y) ~ y * p], t; defaults = [p => missing], guesses = [p => 1.0])
pmap[p] = 2q
prob = ODEProblem(sys, u0map, (0.0, 1.0), pmap)
test_parameter(prob, p, 2.0)
Expand All @@ -605,7 +605,7 @@ sol = solve(oprob_2nd_order_2, Rosenbrock23()) # retcode: Success
test_parameter(prob2, p, 2.0)

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

@testset "Null system" begin
@variables x(t) y(t) s(t)
Expand Down Expand Up @@ -726,3 +723,13 @@ end
@test init(prob2, Tsit5())[x] 0.5
@test_nowarn solve(prob2, Tsit5())
end

@testset "Equations for dependent parameters" begin
@variables x(t)
@parameters p q = 5 r
@mtkbuild sys = ODESystem(D(x) ~ 2x + r, t; parameter_dependencies = [r ~ p + 2q, q ~ p + 3], guesses = [p => 1.0])
prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p => missing])
@test length(equations(ModelingToolkit.get_parent(prob.f.initializeprob.f.sys))) == 4
integ = init(prob, Tsit5())
@test integ.ps[p] 2
end

0 comments on commit 431d270

Please sign in to comment.