Skip to content

Commit 196a967

Browse files
refactor: improve error message for missing inputs/disturbance inputs/outputs
1 parent eb3af92 commit 196a967

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

src/linearization.jl

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,51 @@ function linearize_symbolic(sys::AbstractSystem, inputs,
565565
(; A, B, C, D, f_x, f_z, g_x, g_z, f_u, g_u, h_x, h_z, h_u), sys
566566
end
567567

568+
struct IONotFoundError <: Exception
569+
variant::String
570+
sysname::Symbol
571+
not_found::Any
572+
end
573+
574+
function Base.showerror(io::IO, err::IONotFoundError)
575+
println(io,
576+
"The following $(err.variant) provided to `mtkcompile` were not found in the system:")
577+
maybe_namespace_issue = false
578+
for var in err.not_found
579+
println(io, " ", var)
580+
if hasname(var) && startswith(string(getname(var)), string(err.sysname))
581+
maybe_namespace_issue = true
582+
end
583+
end
584+
if maybe_namespace_issue
585+
println(io, """
586+
Some of the missing variables are namespaced with the name of the system \
587+
`$(err.sysname)` passed to `mtkcompile`. This may be indicative of a namespacing \
588+
issue. `mtkcompile` requires that the $(err.variant) provided are not namespaced \
589+
with the name of the root system. This issue can occur when using `getproperty` \
590+
to access the variables passed as $(err.variant). For example:
591+
592+
```julia
593+
@named sys = MyModel()
594+
inputs = [sys.input_var]
595+
mtkcompile(sys; inputs)
596+
```
597+
598+
Here, `mtkcompile` expects the input to be named `input_var`, but since `sys`
599+
performs namespacing, it will be named `sys$(NAMESPACE_SEPARATOR)input_var`. To \
600+
fix this issue, namespacing can be temporarily disabled:
601+
602+
```julia
603+
@named sys = MyModel()
604+
sys = toggle_namespacing(sys, false)
605+
inputs = [sys.input_var]
606+
sys = toggle_namespacing(sys, true)
607+
mtkcompile(sys; inputs)
608+
```
609+
""")
610+
end
611+
end
612+
568613
"""
569614
Modify the variable metadata of system variables to indicate which ones are inputs, outputs, and disturbances. Needed for `inputs`, `outputs`, `disturbances`, `unbound_inputs`, `unbound_outputs` to return the proper subsets.
570615
"""
@@ -605,19 +650,16 @@ function markio!(state, orig_inputs, inputs, outputs, disturbances; check = true
605650
if check
606651
ikeys = keys(filter(!last, inputset))
607652
if !isempty(ikeys)
608-
error(
609-
"Some specified inputs were not found in system. The following variables were not found ",
610-
ikeys)
653+
throw(IONotFoundError("inputs", nameof(state.sys), ikeys))
611654
end
612655
dkeys = keys(filter(!last, disturbanceset))
613656
if !isempty(dkeys)
614-
error(
615-
"Specified disturbance inputs were not found in system. The following variables were not found ",
616-
ikeys)
657+
throw(IONotFoundError("disturbance inputs", nameof(state.sys), ikeys))
658+
end
659+
okeys = keys(filter(!last, outputset))
660+
if !isempty(okeys)
661+
throw(IONotFoundError("outputs", nameof(state.sys), okeys))
617662
end
618-
(all(values(outputset)) || error(
619-
"Some specified outputs were not found in system. The following Dict indicates the found variables ",
620-
outputset))
621663
end
622664
state, orig_inputs
623665
end

test/linearize.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ lsys = ModelingToolkit.reorder_unknowns(lsys, desired_order, reverse(desired_ord
151151
@test lsys.D == [4400 -4400]
152152

153153
## Test that there is a warning when input is misspecified
154-
@test_throws "Some specified inputs were not found" linearize(pid,
154+
@test_throws ["inputs provided to `mtkcompile`", "not found"] linearize(pid,
155155
[
156156
pid.reference.u,
157157
pid.measurement.u
158158
], [ctr_output.u])
159-
@test_throws "Some specified outputs were not found" linearize(pid,
159+
@test_throws ["outputs provided to `mtkcompile`", "not found"] linearize(pid,
160160
[
161161
reference.u,
162162
measurement.u

0 commit comments

Comments
 (0)