Skip to content

Commit 83956e8

Browse files
authored
Merge pull request #2068 from SciML/myb/odeprob
Fix a cycle in dependency chasing
2 parents 79ccba6 + 1c6fff7 commit 83956e8

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

src/structural_transformation/codegen.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ function gen_nlsolve!(is_not_prepended_assignment, eqs, vars, u0map::AbstractDic
114114
tmp = [init_assignments]
115115
# `deps[init_assignments]` gives the dependency of `init_assignments`
116116
while true
117-
next_assignments = reduce(vcat, deps[init_assignments])
117+
next_assignments = unique(reduce(vcat, deps[init_assignments]))
118118
isempty(next_assignments) && break
119119
init_assignments = next_assignments
120120
push!(tmp, init_assignments)
121121
end
122-
needed_assignments_idxs = reduce(vcat, unique(reverse(tmp)))
122+
needed_assignments_idxs = unique(reduce(vcat, reverse(tmp)))
123123
needed_assignments = assignments[needed_assignments_idxs]
124124
end
125125

src/structural_transformation/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function sorted_incidence_matrix(ts::TransformationState, val = true; only_algeq
165165
push!(J, j)
166166
end
167167
end
168-
sparse(I, J, val, nsrcs(g), ndsts(g))
168+
sparse(I, J, val, nsrcs(graph), ndsts(graph))
169169
end
170170

171171
###

test/odaeproblem.jl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using ModelingToolkit, ModelingToolkitStandardLibrary, Test
2+
using OrdinaryDiffEq
3+
using ModelingToolkitStandardLibrary.Electrical
4+
using ModelingToolkitStandardLibrary.Blocks
5+
6+
function Segment(; name)
7+
@named R = Resistor(; R = 1)
8+
@named r = Resistor(; R = 1)
9+
@named C = Capacitor(; C = 1)
10+
11+
@named p1 = Pin() # top-left
12+
@named p2 = Pin() # top-right
13+
@named n = Pin() # bottom
14+
15+
eqs = [connect(p1, R.p)
16+
connect(R.n, p2, r.p)
17+
connect(r.n, C.p)
18+
connect(C.n, n)]
19+
20+
return ODESystem(eqs, t, [], [];
21+
name = name,
22+
systems = [r, R, C, n, p1, p2])
23+
end
24+
25+
function Strip(; name)
26+
num_segments = 10
27+
# construct `num_segments` segments
28+
segments = [Segment(; name = Symbol(:St_, seg))
29+
for seg in 1:num_segments]
30+
31+
@named p1 = Pin() # top-left
32+
@named p2 = Pin() # top-right
33+
@named n = Pin() # bottom
34+
35+
eqs = [connect(p1, segments[1].p1)
36+
connect(p2, segments[end].p2)
37+
[connect(n, seg.n) for seg in segments]...
38+
[connect(segments[i].p2, segments[i + 1].p1) for i in 1:(num_segments - 1)]...]
39+
40+
return ODESystem(eqs, t, [], []; name,
41+
systems = [p1, p2, n, segments...])
42+
end
43+
44+
@variables t
45+
@named source = Voltage()
46+
@named c = Constant(k = 0.01)
47+
48+
@named ground = Ground()
49+
@named strip = Strip()
50+
51+
rc_eqs = [connect(c.output, source.V)
52+
connect(source.p, strip.p1, strip.p2)
53+
connect(strip.n, source.n, ground.g)]
54+
55+
@named rc_model = ODESystem(rc_eqs, t, systems = [strip, c, source, ground])
56+
sys = structural_simplify(rc_model)
57+
58+
prob = ODAEProblem(sys, [], (0, 10))
59+
@test_nowarn solve(prob, Tsit5())

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ using SafeTestsets, Test
2323
@safetestset "JumpSystem Test" begin include("jumpsystem.jl") end
2424
@safetestset "Constraints Test" begin include("constraints.jl") end
2525
@safetestset "Reduction Test" begin include("reduction.jl") end
26+
@safetestset "ODAEProblem Test" begin include("odaeproblem.jl") end
2627
@safetestset "Components Test" begin include("components.jl") end
2728
@safetestset "print_tree" begin include("print_tree.jl") end
2829
@safetestset "Error Handling" begin include("error_handling.jl") end

0 commit comments

Comments
 (0)