Skip to content

Commit af4fa3b

Browse files
Move extract_elements to equations.jl
1 parent 51c8dac commit af4fa3b

File tree

2 files changed

+39
-38
lines changed

2 files changed

+39
-38
lines changed

src/equations.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,42 @@ Base.broadcastable(eq::Equation) = Ref(eq)
1010
Base.:~(lhs::Expression, rhs::Expression) = Equation(lhs, rhs)
1111
Base.:~(lhs::Expression, rhs::Number ) = Equation(lhs, rhs)
1212
Base.:~(lhs::Number , rhs::Expression) = Equation(lhs, rhs)
13+
14+
15+
function extract_elements(eqs, targetmap, default = nothing)
16+
elems = Dict{Symbol, Vector{Variable}}()
17+
names = Dict{Symbol, Set{Symbol}}()
18+
if default == nothing
19+
targets = unique(collect(values(targetmap)))
20+
else
21+
targets = [unique(collect(values(targetmap))), default]
22+
end
23+
for target in targets
24+
elems[target] = Vector{Variable}()
25+
names[target] = Set{Symbol}()
26+
end
27+
for eq in eqs
28+
extract_elements!(eq, elems, names, targetmap, default)
29+
end
30+
Tuple(elems[target] for target in targets)
31+
end
32+
# Walk the tree recursively and push variables into the right set
33+
function extract_elements!(op, elems, names, targetmap, default)
34+
args = isa(op, Equation) ? Expression[op.lhs, op.rhs] : op.args
35+
36+
for arg in args
37+
if arg isa Operation
38+
extract_elements!(arg, elems, names, targetmap, default)
39+
elseif arg isa Variable
40+
if default == nothing
41+
target = haskey(targetmap, arg.subtype) ? targetmap[arg.subtype] : continue
42+
else
43+
target = haskey(targetmap, arg.subtype) ? targetmap[arg.subtype] : default
44+
end
45+
if !in(arg.name, names[target])
46+
push!(names[target], arg.name)
47+
push!(elems[target], arg)
48+
end
49+
end
50+
end
51+
end

src/systems/diffeqs/diffeqsystem.jl

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -137,44 +137,6 @@ function DiffEqBase.ODEFunction(sys::DiffEqSystem;version = ArrayFunction,kwargs
137137
end
138138
end
139139

140-
function extract_elements(eqs, targetmap, default = nothing)
141-
elems = Dict{Symbol, Vector{Variable}}()
142-
names = Dict{Symbol, Set{Symbol}}()
143-
if default == nothing
144-
targets = unique(collect(values(targetmap)))
145-
else
146-
targets = [unique(collect(values(targetmap))), default]
147-
end
148-
for target in targets
149-
elems[target] = Vector{Variable}()
150-
names[target] = Set{Symbol}()
151-
end
152-
for eq in eqs
153-
extract_elements!(eq, elems, names, targetmap, default)
154-
end
155-
Tuple(elems[target] for target in targets)
156-
end
157-
# Walk the tree recursively and push variables into the right set
158-
function extract_elements!(op, elems, names, targetmap, default)
159-
args = isa(op, Equation) ? Expression[op.lhs, op.rhs] : op.args
160-
161-
for arg in args
162-
if arg isa Operation
163-
extract_elements!(arg, elems, names, targetmap, default)
164-
elseif arg isa Variable
165-
if default == nothing
166-
target = haskey(targetmap, arg.subtype) ? targetmap[arg.subtype] : continue
167-
else
168-
target = haskey(targetmap, arg.subtype) ? targetmap[arg.subtype] : default
169-
end
170-
if !in(arg.name, names[target])
171-
push!(names[target], arg.name)
172-
push!(elems[target], arg)
173-
end
174-
end
175-
end
176-
end
177-
178140

179141
export DiffEqSystem, ODEFunction
180142
export generate_ode_function

0 commit comments

Comments
 (0)