Skip to content

Commit 12e3280

Browse files
committed
Overhaul find_linear_equations
1 parent 5130987 commit 12e3280

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/systems/systemstructure.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ struct SystemStructure
4949
varassoc::Vector{Int}
5050
algeqs::BitVector
5151
graph::BipartiteGraph{Int,Nothing}
52-
solvable_graph::BipartiteGraph{Int,Vector{Vector{Int}}}
53-
is_linear_equations::BitVector
52+
solvable_graph::BipartiteGraph{Int,Nothing}
5453
assign::Vector{Int}
5554
inv_assign::Vector{Int}
5655
scc::Vector{Vector{Int}}
@@ -83,7 +82,7 @@ eqtype(s::SystemStructure, eq::Integer)::EquationType = isalgeq(s, eq) ? ALGEBRA
8382
function initialize_system_structure(sys)
8483
sys, dxvar_offset, fullvars, varassoc, algeqs, graph = init_graph(flatten(sys))
8584

86-
solvable_graph = BipartiteGraph(neqs, nvars, metadata=map(_->Int[], 1:nsrcs(graph)))
85+
solvable_graph = BipartiteGraph(neqs, nvars)
8786

8887
@set sys.structure = SystemStructure(
8988
dxvar_offset,
@@ -92,7 +91,6 @@ function initialize_system_structure(sys)
9291
algeqs,
9392
graph,
9493
solvable_graph,
95-
falses(ndsts(graph)),
9694
Int[],
9795
Int[],
9896
Vector{Int}[],
@@ -173,15 +171,20 @@ function init_graph(sys)
173171
sys, dxvar_offset, fullvars, varassoc, algeqs, graph
174172
end
175173

176-
function find_solvables!(sys)
174+
function find_linear_equations(sys)
177175
s = structure(sys)
178-
@unpack fullvars, graph, solvable_graph, is_linear_equations = s
176+
@unpack fullvars, graph = s
177+
is_linear_equations = falses(ndsts(graph))
179178
eqs = equations(sys)
180-
empty!(solvable_graph)
179+
eadj = Vector{Int}[]
180+
cadj = Vector{Int}[]
181+
coeffs = Int[]
181182
for (i, eq) in enumerate(eqs); isdiffeq(eq) && continue
182-
term = value(eq.rhs - eq.lhs)
183+
empty!(coeffs)
183184
linear_term = 0
184185
all_int_algvars = true
186+
187+
term = value(eq.rhs - eq.lhs)
185188
for j in 𝑠neighbors(graph, i)
186189
if !isalgvar(s, j)
187190
all_int_algvars = false
@@ -190,14 +193,14 @@ function find_solvables!(sys)
190193
var = fullvars[j]
191194
c = expand_derivatives(Differential(var)(term), false)
192195
# test if `var` is linear in `eq`.
193-
if !(c isa Symbolic) && c isa Number && c != 0
196+
if !(c isa Symbolic) && c isa Number
194197
if isinteger(c)
195198
c = convert(Integer, c)
196199
else
197200
all_int_algvars = false
198201
end
199202
linear_term += c * var
200-
add_edge!(solvable_graph, i, j, c)
203+
push!(coeffs, c)
201204
end
202205
end
203206

@@ -209,16 +212,13 @@ function find_solvables!(sys)
209212
# where ``c_i`` ∈ ℤ and ``a_i`` denotes algebraic variables.
210213
if all_int_algvars && isequal(linear_term, term)
211214
is_linear_equations[i] = true
215+
push!(eadj, i)
216+
push!(cadj, copy(coeff))
212217
else
213-
# We use 0 as a sentinel value for a nonlinear or non-integer term.
214-
215-
# Don't move the reference, because it might lead to pointer
216-
# invalidations.
217218
is_linear_equations[i] = false
218-
fill!(solvable_graph.metadata[i], 0)
219219
end
220220
end
221-
s
221+
return is_linear_equations, eadj, cadj
222222
end
223223

224224
end # module

0 commit comments

Comments
 (0)