Skip to content

Commit 6542bba

Browse files
Handle dummy derivative u0's and throw custom incomplete init error
1 parent 11d096f commit 6542bba

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,21 @@ function InitializationProblem{false}(sys::AbstractODESystem, args...; kwargs...
15401540
InitializationProblem{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...)
15411541
end
15421542

1543+
const INCOMPLETE_INITIALIZATION_MESSAGE = """
1544+
Initialization incomplete. Not all of the state variables of the
1545+
DAE system can be determined by the initialization. Missing
1546+
variables:
1547+
"""
1548+
1549+
struct IncompleteInitializationError <: Exception
1550+
uninit
1551+
end
1552+
1553+
function Base.showerror(io::IO, e::IncompleteInitializationError)
1554+
println(io, INCOMPLETE_INITIALIZATION_MESSAGE)
1555+
println(io, e.uninit)
1556+
end
1557+
15431558
function InitializationProblem{iip, specialize}(sys::AbstractODESystem,
15441559
t::Number, u0map = [],
15451560
parammap = DiffEqBase.NullParameters();
@@ -1560,6 +1575,11 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem,
15601575
generate_initializesystem(sys; u0map); fully_determined = false)
15611576
end
15621577

1578+
uninit = setdiff(unknowns(sys),[unknowns(isys); getfield.(observed(isys),:lhs)])
1579+
if !isempty(uninit)
1580+
throw(IncompleteInitializationError(uninit))
1581+
end
1582+
15631583
neqs = length(equations(isys))
15641584
nunknown = length(unknowns(isys))
15651585

src/systems/nonlinear/initializesystem.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,23 @@ function generate_initializesystem(sys::ODESystem;
1717
# Start the equations list with algebraic equations
1818
eqs_ics = eqs[idxs_alge]
1919
u0 = Vector{Pair}(undef, 0)
20-
defs = merge(defaults(sys), todict(u0map))
2120

2221
full_states = [sts; getfield.((observed(sys)), :lhs)]
2322
set_full_states = Set(full_states)
2423
guesses = todict(guesses)
2524
schedule = getfield(sys, :schedule)
26-
27-
dd_guess = if schedule !== nothing
25+
26+
if schedule !== nothing
2827
guessmap = [x[2] => get(guesses, x[1], default_dd_value)
2928
for x in schedule.dummy_sub]
30-
Dict(filter(x -> !isnothing(x[1]), guessmap))
29+
dd_guess = Dict(filter(x -> !isnothing(x[1]), guessmap))
30+
filtered_u0 = todict([get(schedule.dummy_sub, x[1], x[1]) => x[2] for x in u0map])
3131
else
32-
Dict()
32+
dd_guess = Dict()
33+
filtered_u0 = u0map
3334
end
3435

36+
defs = merge(defaults(sys), filtered_u0)
3537
guesses = merge(get_guesses(sys), todict(guesses), dd_guess)
3638

3739
for st in full_states

test/initializationsystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ p = [σ => 28.0,
331331
β => 8 / 3]
332332

333333
tspan = (0.0, 100.0)
334-
@test_throws ArgumentError prob=ODEProblem(sys, u0, tspan, p, jac = true)
334+
@test_throws ModelingToolkit.IncompleteInitializationError prob=ODEProblem(sys, u0, tspan, p, jac = true)
335335

336336
# DAE Initialization on ODE with nonlinear system for initial conditions
337337
# https://github.com/SciML/ModelingToolkit.jl/issues/2508

0 commit comments

Comments
 (0)