diff --git a/src/systems/diffeqs/abstractodesystem.jl b/src/systems/diffeqs/abstractodesystem.jl index ab0565eb31..343d2856ca 100644 --- a/src/systems/diffeqs/abstractodesystem.jl +++ b/src/systems/diffeqs/abstractodesystem.jl @@ -1562,14 +1562,17 @@ 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) + defs = defaults(isys) + + # Check that all unknowns have guesses + 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, ", ")).")) - u0map = merge(todict(guesses), todict(u0map)) + u0map = merge(guesses, u0map) if neqs == nunknown NonlinearProblem(isys, u0map, parammap; kwargs...) else diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index f4097ecd97..3cd30dd7e6 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 ODEProblem(ssys, [x => 3], (0, 1), []) # y should have a guess +end