From d8dec365d56cd0372c7a00886795850ac5841655 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 18 Jul 2024 12:08:43 +0200 Subject: [PATCH 01/10] Minor cleanup to simplify error implementation --- src/systems/diffeqs/abstractodesystem.jl | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index ab0565eb31..34fff36c17 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1562,14 +1562,10 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, parammap = parammap isa DiffEqBase.NullParameters || isempty(parammap) ? [get_iv(sys) => t] : merge(todict(parammap), Dict(get_iv(sys) => t)) - if isempty(u0map) - u0map = Dict() - end - if isempty(guesses) - guesses = Dict() - end + u0map = isempty(u0map) ? Dict() : todict(u0map) + guesses = isempty(guesses) ? Dict() : todict(guesses) - u0map = merge(todict(guesses), todict(u0map)) + u0map = merge(guesses, u0map) if neqs == nunknown NonlinearProblem(isys, u0map, parammap; kwargs...) else From 53e2aad2035bc8ce838ef29e4e9319d6d0aae22e Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 18 Jul 2024 12:30:33 +0200 Subject: [PATCH 02/10] Check that all unknowns have a guess --- src/systems/diffeqs/abstractodesystem.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 34fff36c17..c7cc7398a8 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1565,6 +1565,12 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, u0map = isempty(u0map) ? Dict() : todict(u0map) guesses = isempty(guesses) ? Dict() : todict(guesses) + # Check that all unknowns have guesses + # TODO: which, if any, unknowns should be excluded from this guess? + for x in unknowns(isys) + x in keys(guesses) || throw(ArgumentError("Missing guess for $x.")) + end + u0map = merge(guesses, u0map) if neqs == nunknown NonlinearProblem(isys, u0map, parammap; kwargs...) From ff443e48dbc9120f015116663aed92f2042f730a Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 18 Jul 2024 12:43:43 +0200 Subject: [PATCH 03/10] Test a system that really should have a guess --- test/initializationsystem.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index f4097ecd97..13d797d189 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -456,3 +456,11 @@ sys = structural_simplify(unsimp; fully_determined = false) sys = extend(sysx, sysy) @test length(equations(generate_initializesystem(sys))) == 2 @test length(ModelingToolkit.guesses(sys)) == 1 + +# https://github.com/SciML/ModelingToolkit.jl/issues/2873 +@testset "Error on missing defaults" begin + @variables x(t) y(t) + @named sys = ODESystem([x^2 + y^2 ~ 25, D(x) ~ 1], t) + ssys = structural_simplify(sys) + @test_throws ArgumentError prob = ODEProblem(ssys, [x => 3], (0, 1), [] #=; guesses = [y => 5]=#) +end From c4463b3ecfa38061da71400620acbca9f3bce9c8 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 18 Jul 2024 12:46:52 +0200 Subject: [PATCH 04/10] Format --- test/initializationsystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index 13d797d189..3cd30dd7e6 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -462,5 +462,5 @@ sys = extend(sysx, sysy) @variables x(t) y(t) @named sys = ODESystem([x^2 + y^2 ~ 25, D(x) ~ 1], t) ssys = structural_simplify(sys) - @test_throws ArgumentError prob = ODEProblem(ssys, [x => 3], (0, 1), [] #=; guesses = [y => 5]=#) + @test_throws ArgumentError ODEProblem(ssys, [x => 3], (0, 1), []) # y should have a guess end From c193782289bc58dcfa6e95b2b8d5ed891f7d27a2 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Thu, 18 Jul 2024 07:22:25 -0400 Subject: [PATCH 05/10] Update src/systems/diffeqs/abstractodesystem.jl --- src/systems/diffeqs/abstractodesystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index c7cc7398a8..0aa1820ddb 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1564,11 +1564,12 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, merge(todict(parammap), Dict(get_iv(sys) => t)) u0map = isempty(u0map) ? Dict() : todict(u0map) guesses = isempty(guesses) ? Dict() : todict(guesses) + defaults = defaults(isys) # Check that all unknowns have guesses # TODO: which, if any, unknowns should be excluded from this guess? for x in unknowns(isys) - x in keys(guesses) || throw(ArgumentError("Missing guess for $x.")) + x in keys(guesses) || x in keys(defaults) || throw(ArgumentError("Missing guess for $x.")) end u0map = merge(guesses, u0map) From 48a24525457c9a40a7acb6f1d376dd7b294171b5 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 18 Jul 2024 14:15:32 +0200 Subject: [PATCH 06/10] Format --- src/systems/diffeqs/abstractodesystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 0aa1820ddb..b1c6479040 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1569,7 +1569,8 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, # Check that all unknowns have guesses # TODO: which, if any, unknowns should be excluded from this guess? for x in unknowns(isys) - x in keys(guesses) || x in keys(defaults) || throw(ArgumentError("Missing guess for $x.")) + x in keys(guesses) || x in keys(defaults) || + throw(ArgumentError("Missing guess for $x.")) end u0map = merge(guesses, u0map) From 6f1cffbb28c833fd3ca5d89f234ce561cc32ffd4 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 18 Jul 2024 14:20:14 +0200 Subject: [PATCH 07/10] Fix name clash for defaults --- src/systems/diffeqs/abstractodesystem.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index b1c6479040..001c7b82a7 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1564,12 +1564,12 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, merge(todict(parammap), Dict(get_iv(sys) => t)) u0map = isempty(u0map) ? Dict() : todict(u0map) guesses = isempty(guesses) ? Dict() : todict(guesses) - defaults = defaults(isys) + defs = defaults(isys) # Check that all unknowns have guesses # TODO: which, if any, unknowns should be excluded from this guess? for x in unknowns(isys) - x in keys(guesses) || x in keys(defaults) || + x in keys(guesses) || x in keys(defs) || throw(ArgumentError("Missing guess for $x.")) end From 536aff61ff6996149f7c3a9163717d6b1fe05122 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Thu, 18 Jul 2024 14:26:49 +0200 Subject: [PATCH 08/10] Show all unknowns missing guesses; not only the first --- src/systems/diffeqs/abstractodesystem.jl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 001c7b82a7..8d6817a3d4 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1567,11 +1567,9 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, defs = defaults(isys) # Check that all unknowns have guesses - # TODO: which, if any, unknowns should be excluded from this guess? - for x in unknowns(isys) - x in keys(guesses) || x in keys(defs) || - throw(ArgumentError("Missing guess for $x.")) - end + unknowns_missing = setdiff(unknowns(isys), [keys(guesses), keys(defs)]) + !isempty(unknowns_missing) && + throw(ArgumentError("Missing guesses for unknowns $(join(unknowns_missing, ", ")).")) u0map = merge(guesses, u0map) if neqs == nunknown From 4d62d114fc93bdd5c1c4ac13251d4fc7e7e87f58 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 19 Jul 2024 10:09:30 +0200 Subject: [PATCH 09/10] Also include u0map in known unknowns; merge keys in a way that actually works --- src/systems/diffeqs/abstractodesystem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index 8d6817a3d4..cee046517c 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1567,7 +1567,7 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, defs = defaults(isys) # Check that all unknowns have guesses - unknowns_missing = setdiff(unknowns(isys), [keys(guesses), keys(defs)]) + unknowns_missing = setdiff(unknowns(isys), union(keys(guesses), keys(defs), keys(u0map))) !isempty(unknowns_missing) && throw(ArgumentError("Missing guesses for unknowns $(join(unknowns_missing, ", ")).")) From 699a98deb3b0f7b928bf3527a039fdd7b757afd8 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Fri, 19 Jul 2024 10:16:15 +0200 Subject: [PATCH 10/10] Format --- src/systems/diffeqs/abstractodesystem.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index cee046517c..343d2856ca 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1567,7 +1567,8 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, defs = defaults(isys) # Check that all unknowns have guesses - unknowns_missing = setdiff(unknowns(isys), union(keys(guesses), keys(defs), keys(u0map))) + unknowns_given = union(keys(guesses), keys(defs), keys(u0map)) + unknowns_missing = setdiff(unknowns(isys), unknowns_given) !isempty(unknowns_missing) && throw(ArgumentError("Missing guesses for unknowns $(join(unknowns_missing, ", "))."))