Skip to content

Commit e5f0245

Browse files
committed
better error messages
1 parent ea431a1 commit e5f0245

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/systems/abstractsystem.jl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,8 @@ The `simplified_sys` has undergone [`structural_simplify`](@ref) and had any occ
991991
992992
See also [`linearize`](@ref) which provides a higher-level interface.
993993
"""
994-
function linearization_function(sys::AbstractSystem, inputs, outputs; simplify = false,
994+
function linearization_function(sys::AbstractSystem, inputs,
995+
outputs; simplify = false,
995996
kwargs...)
996997
sys = expand_connections(sys)
997998
state = TearingState(sys)
@@ -1030,7 +1031,7 @@ function linearization_function(sys::AbstractSystem, inputs, outputs; simplify =
10301031
function (u, p, t)
10311032
if u !== nothing # Handle systems without states
10321033
length(sts) == length(u) ||
1033-
error("Number of state variables does not match the number of input states")
1034+
error("Number of state variables ($(length(sts))) does not match the number of input states ($(length(u)))")
10341035
uf = SciMLBase.UJacobianWrapper(fun, t, p)
10351036
fg_xz = ForwardDiff.jacobian(uf, u)
10361037
h_xz = ForwardDiff.jacobian(xz -> h(xz, p, t), u)
@@ -1039,7 +1040,7 @@ function linearization_function(sys::AbstractSystem, inputs, outputs; simplify =
10391040
fg_u = ForwardDiff.jacobian(pf, p)[:, input_idxs]
10401041
else
10411042
length(sts) == 0 ||
1042-
error("Number of state variables does not match the number of input states")
1043+
error("Number of state variables (0) does not match the number of input states ($(length(u)))")
10431044
fg_xz = zeros(0, 0)
10441045
h_xz = fg_u = zeros(0, length(inputs))
10451046
end
@@ -1060,23 +1061,31 @@ end
10601061

10611062
function markio!(state, inputs, outputs)
10621063
fullvars = state.fullvars
1063-
inputset = Set(inputs)
1064-
outputset = Set(outputs)
1064+
inputset = Dict(inputs .=> false)
1065+
outputset = Dict(outputs .=> false)
10651066
for (i, v) in enumerate(fullvars)
1066-
if v in inputset
1067+
if v in keys(inputset)
10671068
v = setmetadata(v, ModelingToolkit.VariableInput, true)
10681069
v = setmetadata(v, ModelingToolkit.VariableOutput, false)
1070+
inputset[v] = true
10691071
fullvars[i] = v
1070-
elseif v in outputset
1072+
elseif v in keys(outputset)
10711073
v = setmetadata(v, ModelingToolkit.VariableInput, false)
10721074
v = setmetadata(v, ModelingToolkit.VariableOutput, true)
1075+
outputset[v] = true
10731076
fullvars[i] = v
10741077
else
10751078
v = setmetadata(v, ModelingToolkit.VariableInput, false)
10761079
v = setmetadata(v, ModelingToolkit.VariableOutput, false)
10771080
fullvars[i] = v
10781081
end
10791082
end
1083+
all(values(inputset)) ||
1084+
error("Some specified inputs were not found in system. The following Dict indicates the found variables",
1085+
inputset)
1086+
all(values(outputset)) ||
1087+
error("Some specified outputs were not found in system. The following Dict indicates the found variables",
1088+
outputset)
10801089
state
10811090
end
10821091

test/linearize.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ lsys = ModelingToolkit.reorder_states(lsys, states(ssys), reverse(desired_order)
104104
@test lsys.C == [-4000 400]
105105
@test lsys.D == [4400 -4400]
106106

107+
## Test that there is a warning when input is misspecified
108+
@test_throws "Some specified inputs were not found" linearize(pid,
109+
[
110+
pid.reference.u,
111+
pid.measurement.u,
112+
], [ctr_output.u])
113+
@test_throws "Some specified outputs were not found" linearize(pid,
114+
[reference.u, measurement.u],
115+
[pid.ctr_output.u])
116+
107117
## Test operating points
108118

109119
# The saturation has no dynamics

0 commit comments

Comments
 (0)