From b03e284a4272bc83bfffde214be9203324beb08e Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 26 Jun 2024 13:29:43 +0530 Subject: [PATCH] fix: fix MTKParameters creation using defaults of parameters not in the system --- src/systems/parameter_buffer.jl | 4 +--- test/initial_values.jl | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/systems/parameter_buffer.jl b/src/systems/parameter_buffer.jl index 8a0792fdde..e491344fcd 100644 --- a/src/systems/parameter_buffer.jl +++ b/src/systems/parameter_buffer.jl @@ -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) diff --git a/test/initial_values.jl b/test/initial_values.jl index 8f9607089b..3a7b48901d 100644 --- a/test/initial_values.jl +++ b/test/initial_values.jl @@ -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