Skip to content

Commit ce47f53

Browse files
committed
Show variables/equations instead of indices
1 parent c14002f commit ce47f53

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

src/structural_transformation/utils.jl

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,31 @@ function matching(g::BipartiteGraph, varwhitelist=nothing, eqwhitelist=nothing)
4747
return assign
4848
end
4949

50-
throw_unbalanced(n_highest_vars, neqs, msg, values) = throw(InvalidSystemException(
51-
"The system is unbalanced. "
52-
* "There are $n_highest_vars highest order derivative variables "
53-
* "and $neqs equations.\n"
54-
* msg
55-
* values
56-
))
57-
58-
function fill_unassigned!(unassigned_list, vars, fullvars)
59-
for (vj, eq) in enumerate(vars)
60-
if eq === UNASSIGNED
61-
push!(unassigned_list, fullvars[vj])
62-
end
50+
function error_reporting(sys, bad_idxs, n_highest_vars, iseqs)
51+
io = IOBuffer()
52+
if iseqs
53+
error_title = "More equations than variables:\n"
54+
out_arr = equations(sys)[bad_idxs]
55+
else
56+
error_title = "More variables than equations:\n"
57+
out_arr = structure(sys).fullvars[bad_idxs]
6358
end
59+
60+
msg = String(take!(Base.print_array(io, out_arr)))
61+
neqs = length(equations(sys))
62+
throw(InvalidSystemException(
63+
"The system is unbalanced. "
64+
* "There are $n_highest_vars highest order derivative variables "
65+
* "and $neqs equations.\n"
66+
* msg
67+
))
6468
end
69+
6570
###
6671
### Structural check
6772
###
68-
function check_consistency(s::SystemStructure)
73+
function check_consistency(sys::AbstractSystem)
74+
s = structure(sys)
6975
@unpack varmask, graph, varassoc, fullvars = s
7076
n_highest_vars = count(varmask)
7177
neqs = nsrcs(graph)
@@ -74,21 +80,16 @@ function check_consistency(s::SystemStructure)
7480
if neqs > 0 && !is_balanced
7581
varwhitelist = varassoc .== 0
7682
assign = matching(graph, varwhitelist) # not assigned
77-
unassigned_var = []
78-
if n_highest_vars > neqs
79-
fill_unassigned!(unassigned_var, assign, fullvars)
80-
io = IOBuffer()
81-
Base.print_array(io, unassigned_var)
82-
unassigned_var_str = String(take!(io))
83-
throw_unbalanced(n_highest_vars, neqs, "More variables than equations:\n", unassigned_var_str)
83+
# Just use `error_reporting` to do conditional
84+
iseqs = n_highest_vars > neqs
85+
86+
if iseqs
87+
bad_idxs = findall(isequal(UNASSIGNED), assign)
8488
else
8589
inv_assign = inverse_mapping(assign) # extra equations
86-
fill_unassigned!(unassigned_var, inv_assign, fullvars)
87-
io = IOBuffer()
88-
Base.print_array(io, unassigned_var)
89-
unassigned_var_str = String(take!(io))
90-
throw_unbalanced(n_highest_vars, neqs, "More equations than variables:\n", unassigned_var_str)
90+
bad_idxs = findall(iszero, inv_assign)
9191
end
92+
error_reporting(sys, bad_idxs, n_highest_vars, iseqs)
9293
end
9394

9495
# This is defined to check if Pantelides algorithm terminates. For more

src/systems/abstractsystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ topological sort of the observed equations.
644644
"""
645645
function structural_simplify(sys::AbstractSystem)
646646
sys = initialize_system_structure(alias_elimination(sys))
647-
check_consistency(structure(sys))
647+
check_consistency(sys)
648648
if sys isa ODESystem
649649
sys = dae_index_lowering(sys)
650650
end

0 commit comments

Comments
 (0)