Skip to content
Open
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
5 changes: 3 additions & 2 deletions src/problems/initializationproblem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ All other keyword arguments are forwarded to the wrapped nonlinear problem const
# TODO: throw on uninitialized arrays
filter!(x -> !(x isa Symbolics.Arr), uninit)
if time_dependent_init && !isempty(uninit)
allow_incomplete || throw(IncompleteInitializationError(uninit))
allow_incomplete || throw(IncompleteInitializationError(uninit, sys))
# for incomplete initialization, we will add the missing variables as parameters.
# they will be updated by `update_initializeprob!` and `initializeprobmap` will
# use them to construct the new `u0`.
Expand Down Expand Up @@ -146,9 +146,10 @@ const INCOMPLETE_INITIALIZATION_MESSAGE = """

struct IncompleteInitializationError <: Exception
uninit::Any
sys::Any
end

function Base.showerror(io::IO, e::IncompleteInitializationError)
println(io, INCOMPLETE_INITIALIZATION_MESSAGE)
println(io, e.uninit)
println(io, underscore_to_D(e.uninit, e.sys))
end
42 changes: 42 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1143,3 +1143,45 @@ function Base.showerror(io::IO, ::NotPossibleError)
This should not be possible. Please open an issue in ModelingToolkit.jl with an MWE.
""")
end

"""
$(TYPEDSIGNATURES)

Given a vector of variables in the system, return the corresponding `Differential` form of variable if possible.
Else returns the variable as-is.
"""
function underscore_to_D(v::AbstractVector, sys)
maps = isscheduled(sys) ? get_schedule(sys).dummy_sub : Dict()
inv_maps = Dict{valtype(maps), Vector{Base.keytype(maps)}}()

for (k, v) in maps
push!(get!(() -> valtype(inv_maps)[], inv_maps, v), k)
end
iv = get_iv(sys)
map(x -> underscore_to_D(x, iv, inv_maps), v)
end

function underscore_to_D(v, iv, inv_map)
if haskey(inv_map, v)
only(get(inv_map, v, [v]))
else
v = ModelingToolkit.detime_dvs(v)
s = split(string(getname(v)), 'ˍ')
if length(s) > 1
n, suffix = s
else
n, suffix = first(s), ""
end
repeats = length(suffix) ÷ length(string(iv))
D = Differential(iv)
wrap_with_D(v, D, repeats)
end
end

function wrap_with_D(n, D, repeats)
if repeats <= 0
return n
else
wrap_with_D(D(n), D, repeats - 1)
end
end
5 changes: 5 additions & 0 deletions test/initializationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ tspan = (0.0, 100.0)
@test_throws ModelingToolkit.IncompleteInitializationError prob=ODEProblem(
sys, [u0; p], tspan, jac = true)

u0 = [y => 0.0,
z => 0.0]
@test_throws "Differential(t)(x(t))" prob=ODEProblem(
sys, [u0; p], tspan, jac = true)

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

Expand Down
Loading