Skip to content

fix: fix missing MTKParameters handling in linearize #3006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2285,7 +2285,7 @@ function linearization_function(sys::AbstractSystem, inputs,

function (u, p, t)
p_setter!(oldps, p_getter(u, p..., t))
newu = u_getter(u, p, t)
newu = u_getter(u, p..., t)
return newu, oldps
end
end
Expand All @@ -2303,6 +2303,13 @@ function linearization_function(sys::AbstractSystem, inputs,
initfn = NonlinearFunction(initsys; eval_expression, eval_module)
initprobmap = build_explicit_observed_function(
initsys, unknowns(sys); eval_expression, eval_module)
if has_index_cache(sys) && get_index_cache(sys) !== nothing
initprobmap = let inner = initprobmap
fn(u, p::MTKParameters) = inner(u, p...)
fn(u, p) = inner(u, p)
fn
end
end
ps = parameters(sys)
h = build_explicit_observed_function(sys, outputs; eval_expression, eval_module)
lin_fun = let diff_idxs = diff_idxs,
Expand Down Expand Up @@ -2342,7 +2349,7 @@ function linearization_function(sys::AbstractSystem, inputs,
initu0, initp = get_initprob_u_p(u, p, t)
initprob = NonlinearLeastSquaresProblem(initfn, initu0, initp)
nlsol = solve(initprob, initialization_solver_alg)
u = initprobmap(nlsol)
u = initprobmap(state_values(nlsol), parameter_values(nlsol))
end
end
uf = SciMLBase.UJacobianWrapper(fun, t, p)
Expand Down
10 changes: 10 additions & 0 deletions test/downstream/linearize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,13 @@ linfun, _ = linearization_function(sys, [u], []; op = Dict(x => 2.0))
matrices = linfun([1.0], Dict(p => 3.0), 1.0)
# this would be 1 if the parameter value isn't respected
@test matrices.f_u[] == 3.0

@testset "Issue #2941" begin
@variables x(t) y(t)
@parameters p
eqs = [0 ~ x * log(y) - p]
@named sys = ODESystem(eqs, t; defaults = [p => 1.0])
sys = complete(sys)
@test_nowarn linearize(
sys, [x], []; op = Dict(x => 1.0), allow_input_derivatives = true)
end
Loading