Skip to content

Commit 8c4f770

Browse files
chore: improve printing of uninitialized variables
1 parent 646bace commit 8c4f770

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,3 +1079,23 @@ function Base.showerror(io::IO, ::NotPossibleError)
10791079
This should not be possible. Please open an issue in ModelingToolkit.jl with an MWE.
10801080
""")
10811081
end
1082+
1083+
"""
1084+
$(TYPEDSIGNATURES)
1085+
1086+
Given a vector of variables in the system, return the corresponding `Differential` form of variable if possible.
1087+
Else returns the variable as-is.
1088+
"""
1089+
function underscore_to_D(v::AbstractVector, sys)
1090+
maps = get_schedule(sys).dummy_sub
1091+
inv_maps = Dict{valtype(maps), Vector{Base.keytype(maps)}}()
1092+
1093+
for (k, v) in maps
1094+
push!(get!(() -> valtype(inv_maps)[], inv_maps, v), k)
1095+
end
1096+
map(Base.Fix2(underscore_to_D, inv_maps), v)
1097+
end
1098+
1099+
function underscore_to_D(v, inv_map)
1100+
only(get(inv_map, v, [v]))
1101+
end

0 commit comments

Comments
 (0)