Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore error when unknowns in the initialization system don't have guesses #2877

Merged
merged 5 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading