Skip to content

Commit c6a9ee9

Browse files
authoredJul 20, 2024
Merge pull request #2877 from hersle/error_missing_guesses_v2
Restore error when unknowns in the initialization system don't have guesses
2 parents f5587e1 + 1b22927 commit c6a9ee9

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed
 

‎docs/src/tutorials/initialization.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ long enough you will see that `λ = 0` is required for this equation, but since
195195
`λ = 1` we end up with a set of equations that are impossible to satisfy.
196196

197197
!!! note
198-
198+
199199
If you would prefer to have an error instead of a warning in the context of non-fully
200200
determined systems, pass the keyword argument `fully_determined = true` into the
201201
problem constructor. Additionally, any warning about not being fully determined can
@@ -278,7 +278,7 @@ sol = solve(iprob)
278278
```
279279

280280
!!! note
281-
281+
282282
For more information on solving NonlinearProblems and NonlinearLeastSquaresProblems,
283283
check out the [NonlinearSolve.jl tutorials!](https://docs.sciml.ai/NonlinearSolve/stable/tutorials/getting_started/).
284284

‎src/utils.jl

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -663,42 +663,43 @@ function promote_to_concrete(vs; tofloat = true, use_union = true)
663663
vs = Any[vs...]
664664
end
665665
T = eltype(vs)
666-
if Base.isconcretetype(T) && (!tofloat || T === float(T)) # nothing to do
667-
return vs
668-
else
669-
sym_vs = filter(x -> SymbolicUtils.issym(x) || SymbolicUtils.iscall(x), vs)
670-
isempty(sym_vs) || throw_missingvars_in_sys(sym_vs)
671-
672-
C = nothing
673-
for v in vs
674-
E = typeof(v)
675-
if E <: Number
676-
if tofloat
677-
E = float(E)
678-
end
679-
end
680-
if C === nothing
681-
C = E
682-
end
683-
if use_union
684-
C = Union{C, E}
685-
else
686-
@assert C==E "`promote_to_concrete` can't make type $E uniform with $C"
687-
C = E
688-
end
689-
end
690666

691-
y = similar(vs, C)
692-
for i in eachindex(vs)
693-
if (vs[i] isa Number) & tofloat
694-
y[i] = float(vs[i]) #needed because copyto! can't convert Int to Float automatically
695-
else
696-
y[i] = vs[i]
667+
# return early if there is nothing to do
668+
#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
669+
Base.isconcretetype(T) && !tofloat && return vs
670+
671+
sym_vs = filter(x -> SymbolicUtils.issym(x) || SymbolicUtils.iscall(x), vs)
672+
isempty(sym_vs) || throw_missingvars_in_sys(sym_vs)
673+
674+
C = nothing
675+
for v in vs
676+
E = typeof(v)
677+
if E <: Number
678+
if tofloat
679+
E = float(E)
697680
end
698681
end
682+
if C === nothing
683+
C = E
684+
end
685+
if use_union
686+
C = Union{C, E}
687+
else
688+
@assert C==E "`promote_to_concrete` can't make type $E uniform with $C"
689+
C = E
690+
end
691+
end
699692

700-
return y
693+
y = similar(vs, C)
694+
for i in eachindex(vs)
695+
if (vs[i] isa Number) & tofloat
696+
y[i] = float(vs[i]) #needed because copyto! can't convert Int to Float automatically
697+
else
698+
y[i] = vs[i]
699+
end
701700
end
701+
702+
return y
702703
end
703704

704705
struct BitDict <: AbstractDict{Int, Int}

‎test/initializationsystem.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,11 @@ sys = structural_simplify(unsimp; fully_determined = false)
478478
sys = extend(sysx, sysy)
479479
@test length(equations(generate_initializesystem(sys))) == 2
480480
@test length(ModelingToolkit.guesses(sys)) == 1
481+
482+
# https://github.com/SciML/ModelingToolkit.jl/issues/2873
483+
@testset "Error on missing defaults" begin
484+
@variables x(t) y(t)
485+
@named sys = ODESystem([x^2 + y^2 ~ 25, D(x) ~ 1], t)
486+
ssys = structural_simplify(sys)
487+
@test_throws ArgumentError ODEProblem(ssys, [x => 3], (0, 1), []) # y should have a guess
488+
end

0 commit comments

Comments
 (0)