Skip to content

Commit ec5a5f2

Browse files
authored
Merge pull request #1427 from SciML/myb/fix
Handle the trivial case of dependency chasing
2 parents 6e98586 + 72e7d6e commit ec5a5f2

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/structural_transformation/codegen.jl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,21 @@ function gen_nlsolve!(is_not_prepended_assignment, eqs, vars, u0map::AbstractDic
104104

105105
# Compute necessary assignments for the nlsolve expr
106106
init_assignments = [var2assignment[p] for p in paramset if haskey(var2assignment, p)]
107-
tmp = [init_assignments]
108-
# `deps[init_assignments]` gives the dependency of `init_assignments`
109-
while true
110-
next_assignments = reduce(vcat, deps[init_assignments])
111-
isempty(next_assignments) && break
112-
init_assignments = next_assignments
113-
push!(tmp, init_assignments)
107+
if isempty(init_assignments)
108+
needed_assignments_idxs = Int[]
109+
needed_assignments = similar(assignments, 0)
110+
else
111+
tmp = [init_assignments]
112+
# `deps[init_assignments]` gives the dependency of `init_assignments`
113+
while true
114+
next_assignments = reduce(vcat, deps[init_assignments])
115+
isempty(next_assignments) && break
116+
init_assignments = next_assignments
117+
push!(tmp, init_assignments)
118+
end
119+
needed_assignments_idxs = reduce(vcat, unique(reverse(tmp)))
120+
needed_assignments = assignments[needed_assignments_idxs]
114121
end
115-
needed_assignments_idxs = reduce(vcat, unique(reverse(tmp)))
116-
needed_assignments = assignments[needed_assignments_idxs]
117122

118123
# Compute `params`. They are like enclosed variables
119124
rhsvars = [ModelingToolkit.vars(r.rhs) for r in needed_assignments]

test/structural_transformation/tearing.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,36 @@ sol2 = solve(ODEProblem{false}(
192192

193193
@test sol1[y, :] == sol1[x, :]
194194
@test (@. sin(sol1[z, :]) + sol1[y, :]) pr * sol1.t atol=1e-5
195+
196+
# 1426
197+
function Translational_Mass(;name, m = 1.0)
198+
sts = @variables s(t) v(t) a(t)
199+
ps = @parameters m=m
200+
D = Differential(t)
201+
eqs = [
202+
D(s) ~ v
203+
D(v) ~ a
204+
m*a ~ 0.0
205+
]
206+
ODESystem(eqs, t, sts, ps; name=name)
207+
end
208+
209+
m = 1.0
210+
@named mass = Translational_Mass(m=m)
211+
212+
ms_eqs = []
213+
214+
@named _ms_model = ODESystem(ms_eqs, t)
215+
@named ms_model = compose(_ms_model,
216+
[mass])
217+
218+
# Mass starts with velocity = 1
219+
u0 = [
220+
mass.s => 0.0
221+
mass.v => 1.0
222+
]
223+
224+
sys = structural_simplify(ms_model)
225+
prob_complex = ODAEProblem(sys, u0, (0, 1.0))
226+
sol = solve(prob_complex, Tsit5())
227+
@test all(sol[mass.v] .== 1)

0 commit comments

Comments
 (0)