diff --git a/src/problems/initializationproblem.jl b/src/problems/initializationproblem.jl index b379215e39..6960811bbd 100644 --- a/src/problems/initializationproblem.jl +++ b/src/problems/initializationproblem.jl @@ -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`. @@ -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 diff --git a/src/utils.jl b/src/utils.jl index 11c6436bf3..0da7e4860b 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -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 \ No newline at end of file diff --git a/test/initializationsystem.jl b/test/initializationsystem.jl index bff2c720cf..3288fc22a2 100644 --- a/test/initializationsystem.jl +++ b/test/initializationsystem.jl @@ -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