Skip to content

Commit

Permalink
handle parameters that only appear as defaults
Browse files Browse the repository at this point in the history
closes #2322
  • Loading branch information
baggepinnen committed Nov 3, 2023
1 parent a0a95b5 commit d715ca3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ function collect_var!(states, parameters, var, iv)
elseif !isconstant(var)
push!(states, var)
end
# Add also any parameters that appear only as defaults in the var
if hasdefault(var)
collect_vars!(states, parameters, getdefault(var), iv)

Check warning on line 497 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L496-L497

Added lines #L496 - L497 were not covered by tests
end
return nothing
end

Expand Down
23 changes: 23 additions & 0 deletions test/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1044,3 +1044,26 @@ new_sys2 = complete(substitute(sys2, Dict(:sub => sub)))
Set(states(new_sys2)) == Set([new_sys2.x1, new_sys2.sys1.x1,
new_sys2.sys1.sub.s1, new_sys2.sys1.sub.s2,
new_sys2.sub.s1, new_sys2.sub.s2])

let # Issue https://github.com/SciML/ModelingToolkit.jl/issues/2322
@parameters a=10 b=a / 10 c=a / 20

@variables t
Dt = Differential(t)

@variables x(t)=1 z(t)

eqs = [Dt(x) ~ -b * (x - z),
0 ~ z - c * x]

sys = ODESystem(eqs, t; name = :kjshdf)

sys_simp = structural_simplify(sys)

@test a keys(ModelingToolkit.defaults(sys_simp))

tspan = (0.0, 1)
prob = ODEProblem(sys_simp, [], tspan)
sol = solve(prob, Rodas4())
@test sol(1)[]0.6065307685451087 rtol=1e-4
end

0 comments on commit d715ca3

Please sign in to comment.