Skip to content

Commit 712d094

Browse files
Merge pull request #3920 from DhairyaLGandhi/dg/Dprint
Improve Printing of Uninitialized Variables
2 parents 638b393 + a63fb73 commit 712d094

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/problems/initializationproblem.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ All other keyword arguments are forwarded to the wrapped nonlinear problem const
8484
# TODO: throw on uninitialized arrays
8585
filter!(x -> !(x isa Symbolics.Arr), uninit)
8686
if time_dependent_init && !isempty(uninit)
87-
allow_incomplete || throw(IncompleteInitializationError(uninit))
87+
allow_incomplete || throw(IncompleteInitializationError(uninit, sys))
8888
# for incomplete initialization, we will add the missing variables as parameters.
8989
# they will be updated by `update_initializeprob!` and `initializeprobmap` will
9090
# use them to construct the new `u0`.
@@ -146,9 +146,10 @@ const INCOMPLETE_INITIALIZATION_MESSAGE = """
146146

147147
struct IncompleteInitializationError <: Exception
148148
uninit::Any
149+
sys::Any
149150
end
150151

151152
function Base.showerror(io::IO, e::IncompleteInitializationError)
152153
println(io, INCOMPLETE_INITIALIZATION_MESSAGE)
153-
println(io, e.uninit)
154+
println(io, underscore_to_D(e.uninit, e.sys))
154155
end

src/utils.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,3 +1143,45 @@ function Base.showerror(io::IO, ::NotPossibleError)
11431143
This should not be possible. Please open an issue in ModelingToolkit.jl with an MWE.
11441144
""")
11451145
end
1146+
1147+
"""
1148+
$(TYPEDSIGNATURES)
1149+
1150+
Given a vector of variables in the system, return the corresponding `Differential` form of variable if possible.
1151+
Else returns the variable as-is.
1152+
"""
1153+
function underscore_to_D(v::AbstractVector, sys)
1154+
maps = isscheduled(sys) ? get_schedule(sys).dummy_sub : Dict()
1155+
inv_maps = Dict{valtype(maps), Vector{Base.keytype(maps)}}()
1156+
1157+
for (k, v) in maps
1158+
push!(get!(() -> valtype(inv_maps)[], inv_maps, v), k)
1159+
end
1160+
iv = get_iv(sys)
1161+
map(x -> underscore_to_D(x, iv, inv_maps), v)
1162+
end
1163+
1164+
function underscore_to_D(v, iv, inv_map)
1165+
if haskey(inv_map, v)
1166+
only(get(inv_map, v, [v]))
1167+
else
1168+
v = ModelingToolkit.detime_dvs(v)
1169+
s = split(string(getname(v)), 'ˍ')
1170+
if length(s) > 1
1171+
n, suffix = s
1172+
else
1173+
n, suffix = first(s), ""
1174+
end
1175+
repeats = length(suffix) ÷ length(string(iv))
1176+
D = Differential(iv)
1177+
wrap_with_D(v, D, repeats)
1178+
end
1179+
end
1180+
1181+
function wrap_with_D(n, D, repeats)
1182+
if repeats <= 0
1183+
return n
1184+
else
1185+
wrap_with_D(D(n), D, repeats - 1)
1186+
end
1187+
end

test/initializationsystem.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ tspan = (0.0, 100.0)
363363
@test_throws ModelingToolkit.IncompleteInitializationError prob=ODEProblem(
364364
sys, [u0; p], tspan, jac = true)
365365

366+
u0 = [y => 0.0,
367+
z => 0.0]
368+
@test_throws "Differential(t)(x(t))" prob=ODEProblem(
369+
sys, [u0; p], tspan, jac = true)
370+
366371
# DAE Initialization on ODE with nonlinear system for initial conditions
367372
# https://github.com/SciML/ModelingToolkit.jl/issues/2508
368373

0 commit comments

Comments
 (0)