From 629c26b2538547e310d8f4463fd6c9697926d17a Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 10 Jun 2025 20:37:11 +0530 Subject: [PATCH 1/2] fix: do not unwrap initials in `initializeprobpmap` --- src/systems/problem_utils.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/systems/problem_utils.jl b/src/systems/problem_utils.jl index 2016b1efd8..3954d8f1e0 100644 --- a/src/systems/problem_utils.jl +++ b/src/systems/problem_utils.jl @@ -718,6 +718,8 @@ takes a value provider of `srcsys` and a value provider of `dstsys` and returns # Keyword Arguments - `initials`: Whether to include the `Initial` parameters of `dstsys` among the values to be transferred. +- `unwrap_initials`: Whether initials in `dstsys` corresponding to unknowns in `srcsys` are + unwrapped. - `p_constructor`: The `p_constructor` argument to `process_SciMLProblem`. """ function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::AbstractSystem; @@ -740,7 +742,7 @@ function get_mtkparameters_reconstructor(srcsys::AbstractSystem, dstsys::Abstrac end initials_getter = if initials && !isempty(syms[2]) initsyms = Vector{Any}(syms[2]) - allsyms = Set(all_symbols(srcsys)) + allsyms = Set(variable_symbols(srcsys)) if unwrap_initials for i in eachindex(initsyms) sym = initsyms[i] From 3518b395182b7cbb6997f818fa0d387e93cf8f74 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 10 Jun 2025 20:37:22 +0530 Subject: [PATCH 2/2] test: test that redundant observed expressions are not evaluated --- test/code_generation.jl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/code_generation.jl b/test/code_generation.jl index 15de194fd8..158ee64fbe 100644 --- a/test/code_generation.jl +++ b/test/code_generation.jl @@ -110,3 +110,33 @@ end @test val[] == 2 end end + +@testset "Do not codegen redundant expressions" begin + @variables v1(t) = 1 + @variables v2(t) [guess = 0] + + mutable struct Data + count::Int + end + function update!(d::Data, t) + d.count += 1 # Count the number of times the data gets updated. + end + function (d::Data)(t) + update!(d, t) + rand(1:10) + end + + @parameters (d1::Data)(..) = Data(0) + @parameters (d2::Data)(..) = Data(0) + + eqs = [ + D(v1) ~ d1(t), + v2 ~ d2(t) # Some of the data parameters are not actually needed to solve the system. + ] + + @mtkbuild sys = System(eqs, t) + prob = ODEProblem(sys, [], (0.0, 1.0)) + sol = solve(prob, Tsit5()) + + @test sol.ps[d2].count == 0 +end