-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathalias_elimination.jl
466 lines (414 loc) · 14.4 KB
/
alias_elimination.jl
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
using SymbolicUtils: Rewriters
using Graphs.Experimental.Traversals
function alias_eliminate_graph!(state::TransformationState; kwargs...)
mm = linear_subsys_adjmat!(state; kwargs...)
if size(mm, 1) == 0
return mm # No linear subsystems
end
@unpack graph, var_to_diff, solvable_graph = state.structure
mm = alias_eliminate_graph!(state, mm)
s = state.structure
for g in (s.graph, s.solvable_graph)
g === nothing && continue
for (ei, e) in enumerate(mm.nzrows)
set_neighbors!(g, e, mm.row_cols[ei])
end
end
return mm
end
# For debug purposes
function aag_bareiss(sys::AbstractSystem)
state = TearingState(sys)
complete!(state.structure)
mm = linear_subsys_adjmat!(state)
return aag_bareiss!(state.structure.graph, state.structure.var_to_diff, mm)
end
function extreme_var(var_to_diff, v, level = nothing, ::Val{descend} = Val(true);
callback = _ -> nothing) where {descend}
g = descend ? invview(var_to_diff) : var_to_diff
callback(v)
while (v′ = g[v]) !== nothing
v::Int = v′
callback(v)
if level !== nothing
descend ? (level -= 1) : (level += 1)
end
end
level === nothing ? v : (v => level)
end
alias_elimination(sys) = alias_elimination!(TearingState(sys))[1]
function alias_elimination!(state::TearingState; kwargs...)
sys = state.sys
complete!(state.structure)
graph_orig = copy(state.structure.graph)
mm = alias_eliminate_graph!(state; kwargs...)
fullvars = state.fullvars
@unpack var_to_diff, graph, solvable_graph = state.structure
subs = Dict()
obs = Equation[]
# If we encounter y = -D(x), then we need to expand the derivative when
# D(y) appears in the equation, so that D(-D(x)) becomes -D(D(x)).
to_expand = Int[]
diff_to_var = invview(var_to_diff)
dels = Int[]
eqs = collect(equations(state))
resize!(eqs, nsrcs(graph))
for (ei, e) in enumerate(mm.nzrows)
vs = 𝑠neighbors(graph, e)
if isempty(vs)
# remove empty equations
push!(dels, e)
else
rhs = mapfoldl(+, pairs(nonzerosmap(@view mm[ei, :]))) do (var, coeff)
iszero(coeff) && return 0
return coeff * fullvars[var]
end
eqs[e] = 0 ~ rhs
end
end
deleteat!(eqs, sort!(dels))
old_to_new_eq = Vector{Int}(undef, nsrcs(graph))
idx = 0
cursor = 1
ndels = length(dels)
for i in eachindex(old_to_new_eq)
if cursor <= ndels && i == dels[cursor]
cursor += 1
old_to_new_eq[i] = -1
continue
end
idx += 1
old_to_new_eq[i] = idx
end
n_new_eqs = idx
lineqs = BitSet(mm.nzrows)
eqs_to_update = BitSet()
nvs_orig = ndsts(graph_orig)
for ieq in eqs_to_update
eq = eqs[ieq]
eqs[ieq] = fast_substitute(eq, subs)
end
@set! mm.nparentrows = nsrcs(graph)
@set! mm.row_cols = eltype(mm.row_cols)[mm.row_cols[i]
for (i, eq) in enumerate(mm.nzrows)
if old_to_new_eq[eq] > 0]
@set! mm.row_vals = eltype(mm.row_vals)[mm.row_vals[i]
for (i, eq) in enumerate(mm.nzrows)
if old_to_new_eq[eq] > 0]
@set! mm.nzrows = Int[old_to_new_eq[eq] for eq in mm.nzrows if old_to_new_eq[eq] > 0]
for old_ieq in to_expand
ieq = old_to_new_eq[old_ieq]
eqs[ieq] = expand_derivatives(eqs[ieq])
end
diff_to_var = invview(var_to_diff)
new_graph = BipartiteGraph(n_new_eqs, ndsts(graph))
new_solvable_graph = BipartiteGraph(n_new_eqs, ndsts(graph))
new_eq_to_diff = DiffGraph(n_new_eqs)
eq_to_diff = state.structure.eq_to_diff
for (i, ieq) in enumerate(old_to_new_eq)
ieq > 0 || continue
set_neighbors!(new_graph, ieq, 𝑠neighbors(graph, i))
set_neighbors!(new_solvable_graph, ieq, 𝑠neighbors(solvable_graph, i))
new_eq_to_diff[ieq] = eq_to_diff[i]
end
# update DiffGraph
new_var_to_diff = DiffGraph(length(var_to_diff))
for v in 1:length(var_to_diff)
new_var_to_diff[v] = var_to_diff[v]
end
state.structure.graph = new_graph
state.structure.solvable_graph = new_solvable_graph
state.structure.eq_to_diff = new_eq_to_diff
state.structure.var_to_diff = new_var_to_diff
sys = state.sys
@set! sys.eqs = eqs
state.sys = sys
return invalidate_cache!(sys), mm
end
"""
$(SIGNATURES)
Find the first linear variable such that `𝑠neighbors(adj, i)[j]` is true given
the `constraint`.
"""
@inline function find_first_linear_variable(M::SparseMatrixCLIL,
range,
mask,
constraint)
eadj = M.row_cols
@inbounds for i in range
vertices = eadj[i]
if constraint(length(vertices))
for (j, v) in enumerate(vertices)
if (mask === nothing || mask[v])
return (CartesianIndex(i, v), M.row_vals[i][j])
end
end
end
end
return nothing
end
@inline function find_first_linear_variable(M::AbstractMatrix,
range,
mask,
constraint)
@inbounds for i in range
row = @view M[i, :]
if constraint(count(!iszero, row))
for (v, val) in enumerate(row)
if mask === nothing || mask[v]
return CartesianIndex(i, v), val
end
end
end
end
return nothing
end
function find_masked_pivot(variables, M, k)
r = find_first_linear_variable(M, k:size(M, 1), variables, isequal(1))
r !== nothing && return r
r = find_first_linear_variable(M, k:size(M, 1), variables, isequal(2))
r !== nothing && return r
r = find_first_linear_variable(M, k:size(M, 1), variables, _ -> true)
return r
end
count_nonzeros(a::AbstractArray) = count(!iszero, a)
# N.B.: Ordinarily sparse vectors allow zero stored elements.
# Here we have a guarantee that they won't, so we can make this identification
count_nonzeros(a::CLILVector) = nnz(a)
# Linear variables are highest order differentiated variables that only appear
# in linear equations with only linear variables. Also, if a variable's any
# derivatives is nonlinear, then all of them are not linear variables.
function find_linear_variables(graph, linear_equations, var_to_diff, irreducibles)
stack = Int[]
linear_variables = falses(length(var_to_diff))
var_to_lineq = Dict{Int, BitSet}()
mark_not_linear! = let linear_variables = linear_variables, stack = stack,
var_to_lineq = var_to_lineq
v -> begin
linear_variables[v] = false
push!(stack, v)
while !isempty(stack)
v = pop!(stack)
eqs = get(var_to_lineq, v, nothing)
eqs === nothing && continue
for eq in eqs, v′ in 𝑠neighbors(graph, eq)
if linear_variables[v′]
linear_variables[v′] = false
push!(stack, v′)
end
end
end
end
end
for eq in linear_equations, v in 𝑠neighbors(graph, eq)
linear_variables[v] = true
vlineqs = get!(() -> BitSet(), var_to_lineq, v)
push!(vlineqs, eq)
end
for v in irreducibles
lv = extreme_var(var_to_diff, v)
while true
mark_not_linear!(lv)
lv = var_to_diff[lv]
lv === nothing && break
end
end
linear_equations_set = BitSet(linear_equations)
for (v, islinear) in enumerate(linear_variables)
islinear || continue
lv = extreme_var(var_to_diff, v)
oldlv = lv
remove = invview(var_to_diff)[v] !== nothing
while !remove
for eq in 𝑑neighbors(graph, lv)
if !(eq in linear_equations_set)
remove = true
end
end
lv = var_to_diff[lv]
lv === nothing && break
end
lv = oldlv
remove && while true
mark_not_linear!(lv)
lv = var_to_diff[lv]
lv === nothing && break
end
end
return linear_variables
end
function aag_bareiss!(structure, mm_orig::SparseMatrixCLIL{T, Ti}) where {T, Ti}
@unpack graph, var_to_diff = structure
mm = copy(mm_orig)
linear_equations_set = BitSet(mm_orig.nzrows)
# All unassigned (not a pivot) algebraic variables that only appears in
# linear algebraic equations can be set to 0.
#
# For all the other variables, we can update the original system with
# Bareiss'ed coefficients as Gaussian elimination is nullspace preserving
# and we are only working on linear homogeneous subsystem.
is_algebraic = let var_to_diff = var_to_diff
v -> var_to_diff[v] === nothing === invview(var_to_diff)[v]
end
is_linear_variables = is_algebraic.(1:length(var_to_diff))
is_highest_diff = computed_highest_diff_variables(structure)
for i in 𝑠vertices(graph)
# only consider linear algebraic equations
(i in linear_equations_set && all(is_algebraic, 𝑠neighbors(graph, i))) &&
continue
for j in 𝑠neighbors(graph, i)
is_linear_variables[j] = false
end
end
solvable_variables = findall(is_linear_variables)
local bar
try
bar = do_bareiss!(mm, mm_orig, is_linear_variables, is_highest_diff)
catch e
e isa OverflowError || rethrow(e)
mm = convert(SparseMatrixCLIL{BigInt, Ti}, mm_orig)
bar = do_bareiss!(mm, mm_orig, is_linear_variables, is_highest_diff)
end
return mm, solvable_variables, bar
end
function do_bareiss!(M, Mold, is_linear_variables, is_highest_diff)
rank1r = Ref{Union{Nothing, Int}}(nothing)
rank2r = Ref{Union{Nothing, Int}}(nothing)
find_pivot = let rank1r = rank1r
(M, k) -> begin
if rank1r[] === nothing
r = find_masked_pivot(is_linear_variables, M, k)
r !== nothing && return r
rank1r[] = k - 1
end
if rank2r[] === nothing
r = find_masked_pivot(is_highest_diff, M, k)
r !== nothing && return r
rank2r[] = k - 1
end
# TODO: It would be better to sort the variables by
# derivative order here to enable more elimination
# opportunities.
return find_masked_pivot(nothing, M, k)
end
end
pivots = Int[]
find_and_record_pivot = let pivots = pivots
(M, k) -> begin
r = find_pivot(M, k)
r === nothing && return nothing
push!(pivots, r[1][2])
return r
end
end
myswaprows! = let Mold = Mold
(M, i, j) -> begin
Mold !== nothing && swaprows!(Mold, i, j)
swaprows!(M, i, j)
end
end
bareiss_ops = ((M, i, j) -> nothing, myswaprows!,
bareiss_update_virtual_colswap_mtk!, bareiss_zero!)
rank3, = bareiss!(M, bareiss_ops; find_pivot = find_and_record_pivot)
rank2 = something(rank2r[], rank3)
rank1 = something(rank1r[], rank2)
(rank1, rank2, rank3, pivots)
end
function alias_eliminate_graph!(state::TransformationState, ils::SparseMatrixCLIL)
@unpack structure = state
@unpack graph, solvable_graph, var_to_diff, eq_to_diff = state.structure
# Step 1: Perform Bareiss factorization on the adjacency matrix of the linear
# subsystem of the system we're interested in.
#
ils, solvable_variables, (rank1, rank2, rank3, pivots) = aag_bareiss!(structure, ils)
## Step 2: Simplify the system using the Bareiss factorization
rk1vars = BitSet(@view pivots[1:rank1])
for v in solvable_variables
v in rk1vars && continue
@set! ils.nparentrows += 1
push!(ils.nzrows, ils.nparentrows)
push!(ils.row_cols, [v])
push!(ils.row_vals, [convert(eltype(ils), 1)])
add_vertex!(graph, SRC)
add_vertex!(solvable_graph, SRC)
add_edge!(graph, ils.nparentrows, v)
add_edge!(solvable_graph, ils.nparentrows, v)
add_vertex!(eq_to_diff)
end
return ils
end
function exactdiv(a::Integer, b)
d, r = divrem(a, b)
@assert r == 0
return d
end
swap!(v, i, j) = v[i], v[j] = v[j], v[i]
"""
$(SIGNATURES)
Use Kahn's algorithm to topologically sort observed equations.
Example:
```julia
julia> t = ModelingToolkit.t_nounits
julia> @variables x(t) y(t) z(t) k(t)
(x(t), y(t), z(t), k(t))
julia> eqs = [
x ~ y + z
z ~ 2
y ~ 2z + k
];
julia> ModelingToolkit.topsort_equations(eqs, [x, y, z, k])
3-element Vector{Equation}:
Equation(z(t), 2)
Equation(y(t), k(t) + 2z(t))
Equation(x(t), y(t) + z(t))
```
"""
function topsort_equations(eqs, unknowns; check = true)
graph, assigns = observed2graph(eqs, unknowns)
neqs = length(eqs)
degrees = zeros(Int, neqs)
for 𝑠eq in 1:length(eqs)
var = assigns[𝑠eq]
for 𝑑eq in 𝑑neighbors(graph, var)
# 𝑠eq => 𝑑eq
degrees[𝑑eq] += 1
end
end
q = Queue{Int}(neqs)
for (i, d) in enumerate(degrees)
d == 0 && enqueue!(q, i)
end
idx = 0
ordered_eqs = similar(eqs, 0)
sizehint!(ordered_eqs, neqs)
while !isempty(q)
𝑠eq = dequeue!(q)
idx += 1
push!(ordered_eqs, eqs[𝑠eq])
var = assigns[𝑠eq]
for 𝑑eq in 𝑑neighbors(graph, var)
degree = degrees[𝑑eq] = degrees[𝑑eq] - 1
degree == 0 && enqueue!(q, 𝑑eq)
end
end
(check && idx != neqs) && throw(ArgumentError("The equations have at least one cycle."))
return ordered_eqs
end
function observed2graph(eqs, unknowns)
graph = BipartiteGraph(length(eqs), length(unknowns))
v2j = Dict(unknowns .=> 1:length(unknowns))
# `assigns: eq -> var`, `eq` defines `var`
assigns = similar(eqs, Int)
for (i, eq) in enumerate(eqs)
lhs_j = get(v2j, eq.lhs, nothing)
lhs_j === nothing &&
throw(ArgumentError("The lhs $(eq.lhs) of $eq, doesn't appear in unknowns."))
assigns[i] = lhs_j
vs = vars(eq.rhs; op = Symbolics.Operator)
for v in vs
j = get(v2j, v, nothing)
j !== nothing && add_edge!(graph, i, j)
end
end
return graph, assigns
end