Skip to content

Commit c14002f

Browse files
committed
Start on better error messages
1 parent 081e7b6 commit c14002f

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/structural_transformation/utils.jl

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ 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
63+
end
64+
end
5065
###
5166
### Structural check
5267
###
@@ -56,11 +71,25 @@ function check_consistency(s::SystemStructure)
5671
neqs = nsrcs(graph)
5772
is_balanced = n_highest_vars == neqs
5873

59-
(neqs > 0 && !is_balanced) && throw(InvalidSystemException(
60-
"The system is unbalanced. "
61-
* "There are $n_highest_vars highest order derivative variables "
62-
* "and $neqs equations."
63-
))
74+
if neqs > 0 && !is_balanced
75+
varwhitelist = varassoc .== 0
76+
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)
84+
else
85+
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)
91+
end
92+
end
6493

6594
# This is defined to check if Pantelides algorithm terminates. For more
6695
# details, check the equation (15) of the original paper.

src/systems/systemstructure.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ function initialize_system_structure(sys)
180180
for algvar in algvars
181181
# it could be that a variable appeared in the states, but never appeared
182182
# in the equations.
183-
algvaridx = var2idx[algvar]
183+
algvaridx = get(var2idx, algvar, 0)
184+
algvaridx == 0 && throw(InvalidSystemException("The system is missing "
185+
* "an equation for $algvar."
186+
))
184187
vartype[algvaridx] = ALGEBRAIC_VARIABLE
185188
end
186189

0 commit comments

Comments
 (0)