-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.jl
More file actions
364 lines (326 loc) · 12.8 KB
/
utils.jl
File metadata and controls
364 lines (326 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
###
### Bipartite graph utilities
###
using BipartiteGraphs: 𝑠vertices, 𝑠neighbors
n_concrete_eqs(state::TransformationState) = n_concrete_eqs(state.structure)
n_concrete_eqs(structure::SystemStructure) = n_concrete_eqs(structure.graph)
function n_concrete_eqs(graph::BipartiteGraph)
count(e -> !isempty(𝑠neighbors(graph, e)), 𝑠vertices(graph))
end
struct InvalidSystemException <: Exception
msg::String
end
function Base.showerror(io::IO, e::InvalidSystemException)
print(io, "InvalidSystemException: ", e.msg)
end
struct ExtraVariablesSystemException <: Exception
msg::String
end
function Base.showerror(io::IO, e::ExtraVariablesSystemException)
println(io, "ExtraVariablesSystemException: ", e.msg)
print(io,
"Note that the process of determining extra variables is a best-effort heuristic. " *
"The true extra variables are dependent on the model and may not be in this list.")
end
struct ExtraEquationsSystemException <: Exception
msg::String
end
function Base.showerror(io::IO, e::ExtraEquationsSystemException)
println(io, "ExtraEquationsSystemException: ", e.msg)
print(io,
"Note that the process of determining extra equations is a best-effort heuristic. " *
"The true extra equations are dependent on the model and may not be in this list.")
end
function error_reporting(state, bad_idxs, n_highest_vars, iseqs, orig_inputs)
io = IOBuffer()
neqs = n_concrete_eqs(state)
if iseqs
error_title = "More equations than variables, here are the potential extra equation(s):\n"
out_arr = has_equations(state) ? equations(state)[bad_idxs] : bad_idxs
else
error_title = "More variables than equations, here are the potential extra variable(s):\n"
out_arr = get_fullvars(state)[bad_idxs]
unset_inputs = intersect(out_arr, orig_inputs)
n_missing_eqs = n_highest_vars - neqs
n_unset_inputs = length(unset_inputs)
if n_unset_inputs > 0
println(io, "In particular, the unset input(s) are:")
Base.print_array(io, unset_inputs)
println(io)
println(io, "The rest of potentially unset variable(s) are:")
end
end
Base.print_array(io, out_arr)
msg = String(take!(io))
if iseqs
throw(ExtraEquationsSystemException("The system is unbalanced. There are " *
"$n_highest_vars highest order derivative variables "
* "and $neqs equations.\n"
* error_title
* msg))
else
throw(ExtraVariablesSystemException("The system is unbalanced. There are " *
"$n_highest_vars highest order derivative variables "
* "and $neqs equations.\n"
* error_title
* msg))
end
end
###
### Structural check
###
"""
$(TYPEDSIGNATURES)
Check if the `state` represents a singular system, and return the unmatched variables.
"""
function singular_check(state::TransformationState)
(; graph, var_to_diff) = state.structure
fullvars = get_fullvars(state)
# This is defined to check if Pantelides algorithm terminates. For more
# details, check the equation (15) of the original paper.
extended_graph = (@set graph.fadjlist = Vector{Int}[graph.fadjlist;
map(collect, edges(var_to_diff))])
extended_var_eq_matching = maximal_matching(extended_graph)
nvars = ndsts(graph)
unassigned_var = eltype(get_fullvars(state))[]
for (vj, eq) in enumerate(extended_var_eq_matching)
vj > nvars && break
if eq === unassigned && !isempty(𝑑neighbors(graph, vj))
push!(unassigned_var, fullvars[vj])
end
end
return unassigned_var
end
"""
$(TYPEDSIGNATURES)
Check the consistency of `state`, given the inputs `orig_inputs`. If `nothrow == false`,
throws an error if the system is under-/over-determined or singular. In this case, if the
function returns it will return `true`. If `nothrow == true`, it will return `false`
instead of throwing an error. The singular case will print a warning.
"""
function check_consistency(state::TransformationState, orig_inputs; nothrow = false)
neqs = n_concrete_eqs(state)
(; graph, var_to_diff) = state.structure
highest_vars = computed_highest_diff_variables(complete!(state.structure))
n_highest_vars = 0
for (v, h) in enumerate(highest_vars)
h || continue
isempty(𝑑neighbors(graph, v)) && continue
n_highest_vars += 1
end
is_balanced = n_highest_vars == neqs
if neqs > 0 && !is_balanced
nothrow && return false
varwhitelist = var_to_diff .== nothing
var_eq_matching = maximal_matching(graph,
dstfilter = v -> varwhitelist[v]) # not assigned
# Just use `error_reporting` to do conditional
iseqs = n_highest_vars < neqs
if iseqs
eq_var_matching = invview(complete(var_eq_matching, nsrcs(graph))) # extra equations
bad_idxs = findall(isequal(unassigned), @view eq_var_matching[1:nsrcs(graph)])
else
bad_idxs = findall(isequal(unassigned), var_eq_matching)
end
error_reporting(state, bad_idxs, n_highest_vars, iseqs, orig_inputs)
end
unassigned_var = singular_check(state)
if !isempty(unassigned_var) || !is_balanced
nothrow && return false
io = IOBuffer()
Base.print_array(io, unassigned_var)
unassigned_var_str = String(take!(io))
errmsg = "The system is structurally singular! " *
"Here are the problematic variables: \n" *
unassigned_var_str
throw(InvalidSystemException(errmsg))
end
return true
end
###
### BLT ordering
###
"""
$TYPEDSIGNATURES
Find strongly connected components of the variables defined by `g`. `assign`
gives the undirected bipartite graph a direction. When `assign === nothing`, we
assume that the ``i``-th variable is assigned to the ``i``-th equation.
"""
function find_var_sccs(g::BipartiteGraph, assign = nothing)
cmog = DiCMOBiGraph{true}(g,
Matching(assign === nothing ? Base.OneTo(nsrcs(g)) : assign))
sccs = Graphs.strongly_connected_components(cmog)
cgraph = MatchedCondensationGraph(cmog, sccs)
toporder = topological_sort(cgraph)
sccs = sccs[toporder]
foreach(sort!, sccs)
return sccs
end
"""
$TYPEDSIGNATURES
Find strongly connected components of algebraic variables in a system.
"""
function algebraic_variables_scc(structure::SystemStructure)
graph = structure.graph
# skip over differential equations
algvars = BitSet(findall(v -> isalgvar(structure, v), 1:ndsts(graph)))
algeqs = BitSet(findall(map(1:nsrcs(graph)) do eq
all(v -> !isdervar(structure, v),
𝑠neighbors(graph, eq))
end))
var_eq_matching = complete(
maximal_matching(graph, e -> e in algeqs, v -> v in algvars), ndsts(graph))
var_sccs = find_var_sccs(complete(graph), var_eq_matching)
return var_eq_matching, var_sccs
end
"""
$(TYPEDSIGNATURES)
Obtain the incidence matrix of the system sorted by the algebraic SCCs.
"""
function sorted_incidence_matrix(ts::TransformationState, val = true; only_algeqs = false,
only_algvars = false)
var_eq_matching, var_scc = algebraic_variables_scc(ts)
s = ts.structure
graph = ts.structure.graph
varsmap = zeros(Int, ndsts(graph))
eqsmap = zeros(Int, nsrcs(graph))
varidx = 0
eqidx = 0
for vs in var_scc, v in vs
eq = var_eq_matching[v]
if eq !== unassigned
eqsmap[eq] = (eqidx += 1)
varsmap[v] = (varidx += 1)
end
end
for i in diffvars_range(s)
varsmap[i] = (varidx += 1)
end
for i in dervars_range(s)
varsmap[i] = (varidx += 1)
end
for i in 1:nsrcs(graph)
if eqsmap[i] == 0
eqsmap[i] = (eqidx += 1)
end
end
I = Int[]
J = Int[]
algeqs_set = algeqs(s)
for eq in 𝑠vertices(graph)
only_algeqs && (eq in algeqs_set || continue)
for var in 𝑠neighbors(graph, eq)
only_algvars && (isalgvar(s, var) || continue)
i = eqsmap[eq]
j = varsmap[var]
(iszero(i) || iszero(j)) && continue
push!(I, i)
push!(J, j)
end
end
SparseArrays.sparse(I, J, val, nsrcs(graph), ndsts(graph))
end
function add_row_coeffs!(
row_col_i::Vector{Int}, row_val_i::Vector{T}, old_to_new_var::Vector{Int},
aliases::Dict{Int, Int}, old_var::Int, coeff::T
) where {T <: Integer}
alias = get(aliases, old_var, 0)
iszero(alias) && return
push!(row_col_i, old_to_new_var[alias])
push!(row_val_i, coeff)
return nothing
end
function add_row_coeffs!(
row_col_i::Vector{Int}, row_val_i::Vector{T}, old_to_new_var::Vector{Int},
aliases::Dict{Int, SparseArrays.SparseVector{T, Int}}, old_var::Int, coeff::T
) where {T <: Integer}
alias = get(aliases, old_var, nothing)
if alias isa SparseArrays.SparseVector{T, Int}
I, V = SparseArrays.findnz(alias)
for (i, v) in zip(I, V)
iszero(old_to_new_var[i])
end
append!(row_col_i, Iterators.map(Base.Fix1(getindex, old_to_new_var), I))
append!(row_val_i, Iterators.map(Base.Fix1(*, coeff), V))
end
return nothing
end
"""
$TYPEDSIGNATURES
Construct the new coefficient matrix with:
- Some equations removed, as indicated by `old_to_new_eq` which is obtained from
`get_old_to_new_idxs`.
- Some variables removed and aliased to other variables, as indicated by `old_to_new_var`
and `aliases`. `aliases` can be a `Dict{Int, Int}` indicating variables exactly aliased
to others, or `Dict{Int, SparseVector{eltype(mm)}}` indicating variables aliased to linear
combinations of others. Note that this is not recursive - if one variable
depends on another aliased variable, it will lead to incorrect results.
"""
function get_new_mm(
aliases::Dict{Int}, old_to_new_eq::Vector{Int}, old_to_new_var::Vector{Int},
mm::CLIL.SparseMatrixCLIL
)
new_nparentrows = mm.nparentrows
new_row_cols = eltype(mm.row_cols)[]
new_row_vals = eltype(mm.row_vals)[]
new_nzrows = Int[]
indices = Int[]
for (i, eq) in enumerate(mm.nzrows)
old_to_new_eq[eq] > 0 || continue
new_row_col_i = eltype(eltype(new_row_cols))[]
new_row_val_i = eltype(eltype(new_row_vals))[]
sizehint!(new_row_col_i, length(mm.row_cols[i]))
sizehint!(new_row_val_i, length(mm.row_vals[i]))
still_valid_eq = true
for (var, coeff) in zip(mm.row_cols[i], mm.row_vals[i])
if old_to_new_var[var] > 0
push!(new_row_col_i, old_to_new_var[var])
push!(new_row_val_i, coeff)
continue
end
# This variable is removed, but not aliased to an integer coefficient linear
# combination. As a result, this equation cannot be retained in `mm`.
if !haskey(aliases, var)
still_valid_eq = false
break
end
add_row_coeffs!(new_row_col_i, new_row_val_i, old_to_new_var, aliases, var, coeff)
end
bad_idx = findfirst(iszero, new_row_col_i)
if bad_idx isa Int
throw(BadMMAliasError(bad_idx))
end
still_valid_eq || continue
empty!(indices)
append!(indices, LinearIndices(new_row_col_i))
sortperm!(indices, new_row_col_i)
final_row_cols = empty(new_row_col_i)
final_row_vals = empty(new_row_val_i)
push!(final_row_cols, new_row_col_i[indices[1]])
push!(final_row_vals, new_row_val_i[indices[1]])
for i in Iterators.drop(eachindex(indices), 1)
if new_row_col_i[indices[i]] == new_row_col_i[indices[i - 1]]
final_row_vals[end] += new_row_val_i[indices[i]]
else
push!(final_row_cols, new_row_col_i[indices[i]])
push!(final_row_vals, new_row_val_i[indices[i]])
end
end
push!(new_row_cols, final_row_cols)
push!(new_row_vals, final_row_vals)
push!(new_nzrows, old_to_new_eq[eq])
end
return typeof(mm)(new_nparentrows, count(!iszero, old_to_new_var), new_nzrows, new_row_cols, new_row_vals)
end
struct BadMMAliasError <: Exception
eq::Int
end
function Base.showerror(io::IO, err::BadMMAliasError)
return print(
io, """
When processing equation $(err.eq), the list of aliases resulted in a linear
combination of a removed variable. No variable should be aliased to a removed
variable.
"""
)
end