From 431d270e36edd460c6c6cc33c50f0d3d88783b4f Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 26 Sep 2024 19:26:16 +0530 Subject: [PATCH] fix: require guesses for parameters --- src/systems/nonlinear/initializesystem.jl | 8 ++++---- test/initializationsystem.jl | 17 ++++++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/systems/nonlinear/initializesystem.jl b/src/systems/nonlinear/initializesystem.jl index b79c307876..850ec35085 100644 --- a/src/systems/nonlinear/initializesystem.jl +++ b/src/systems/nonlinear/initializesystem.jl @@ -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 @@ -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 @@ -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) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 13bb7be528..77ad51c4a3 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -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) @@ -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) @@ -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) @@ -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