Skip to content

Commit

Permalink
fix: fix MTKParameters creation using defaults of parameters not in t…
Browse files Browse the repository at this point in the history
…he system
  • Loading branch information
AayushSabharwal committed Jul 1, 2024
1 parent a203b86 commit b03e284
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/systems/parameter_buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ function MTKParameters(
p = Dict()
end
p = todict(p)
defs = Dict(default_toterm(unwrap(k)) => v
for (k, v) in defaults(sys)
if unwrap(k) in all_ps || default_toterm(unwrap(k)) in all_ps)
defs = Dict(default_toterm(unwrap(k)) => v for (k, v) in defaults(sys))
if eltype(u0) <: Pair
u0 = todict(u0)
elseif u0 isa AbstractArray && !isempty(u0)
Expand Down
17 changes: 17 additions & 0 deletions test/initial_values.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,20 @@ target_varmap = Dict(p => ones(3), q => 2ones(3), q[1] => 2.0, q[2] => 2.0, q[3]
eqs = [D(D(z)) ~ ones(2, 2)]
@mtkbuild sys = ODESystem(eqs, t)
@test_nowarn ODEProblem(sys, [z => zeros(2, 2), D(z) => ones(2, 2)], (0.0, 10.0))

# Initialization with defaults involving parameters that are not part of the system
# Issue#2817
@parameters A1 A2 B1 B2
@variables x1(t) x2(t)
@mtkbuild sys = ODESystem(
[
x1 ~ B1,
x2 ~ B2
], t; defaults = [
A2 => 1 - A1,
B1 => A1,
B2 => A2
])
prob = ODEProblem(sys, [], (0.0, 1.0), [A1 => 0.3])
@test prob.ps[B1] == 0.3
@test prob.ps[B2] == 0.7

0 comments on commit b03e284

Please sign in to comment.