Skip to content

Commit

Permalink
Merge pull request #2877 from hersle/error_missing_guesses_v2
Browse files Browse the repository at this point in the history
Restore error when unknowns in the initialization system don't have guesses
  • Loading branch information
ChrisRackauckas authored Jul 20, 2024
2 parents f5587e1 + 1b22927 commit c6a9ee9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
4 changes: 2 additions & 2 deletions docs/src/tutorials/initialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ long enough you will see that `λ = 0` is required for this equation, but since
`λ = 1` we end up with a set of equations that are impossible to satisfy.

!!! note

If you would prefer to have an error instead of a warning in the context of non-fully
determined systems, pass the keyword argument `fully_determined = true` into the
problem constructor. Additionally, any warning about not being fully determined can
Expand Down Expand Up @@ -278,7 +278,7 @@ sol = solve(iprob)
```

!!! note

For more information on solving NonlinearProblems and NonlinearLeastSquaresProblems,
check out the [NonlinearSolve.jl tutorials!](https://docs.sciml.ai/NonlinearSolve/stable/tutorials/getting_started/).

Expand Down
63 changes: 32 additions & 31 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -663,42 +663,43 @@ function promote_to_concrete(vs; tofloat = true, use_union = true)
vs = Any[vs...]
end
T = eltype(vs)
if Base.isconcretetype(T) && (!tofloat || T === float(T)) # nothing to do
return vs
else
sym_vs = filter(x -> SymbolicUtils.issym(x) || SymbolicUtils.iscall(x), vs)
isempty(sym_vs) || throw_missingvars_in_sys(sym_vs)

C = nothing
for v in vs
E = typeof(v)
if E <: Number
if tofloat
E = float(E)
end
end
if C === nothing
C = E
end
if use_union
C = Union{C, E}
else
@assert C==E "`promote_to_concrete` can't make type $E uniform with $C"
C = E
end
end

y = similar(vs, C)
for i in eachindex(vs)
if (vs[i] isa Number) & tofloat
y[i] = float(vs[i]) #needed because copyto! can't convert Int to Float automatically
else
y[i] = vs[i]
# return early if there is nothing to do
#Base.isconcretetype(T) && (!tofloat || T === float(T)) && return vs # TODO: disabled float(T) to restore missing errors in https://github.com/SciML/ModelingToolkit.jl/issues/2873
Base.isconcretetype(T) && !tofloat && return vs

sym_vs = filter(x -> SymbolicUtils.issym(x) || SymbolicUtils.iscall(x), vs)
isempty(sym_vs) || throw_missingvars_in_sys(sym_vs)

C = nothing
for v in vs
E = typeof(v)
if E <: Number
if tofloat
E = float(E)
end
end
if C === nothing
C = E
end
if use_union
C = Union{C, E}
else
@assert C==E "`promote_to_concrete` can't make type $E uniform with $C"
C = E
end
end

return y
y = similar(vs, C)
for i in eachindex(vs)
if (vs[i] isa Number) & tofloat
y[i] = float(vs[i]) #needed because copyto! can't convert Int to Float automatically
else
y[i] = vs[i]
end
end

return y
end

struct BitDict <: AbstractDict{Int, Int}
Expand Down
8 changes: 8 additions & 0 deletions test/initializationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,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

0 comments on commit c6a9ee9

Please sign in to comment.