From c835625c7748a5dc4e9c9dde9ef4f8a906303713 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 18 Jun 2024 15:33:50 +0530 Subject: [PATCH] fix: check for non-parameter values in operating point --- src/systems/abstractsystem.jl | 4 ++- test/downstream/linearize.jl | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 2ac84b2a8a..040977fdb8 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -2000,7 +2000,9 @@ function linearization_function(sys::AbstractSystem, inputs, p = todict(p) newps = deepcopy(sys_ps) for (k, v) in p - setp(sys, k)(newps, v) + if is_parameter(sys, k) + setp(sys, k)(newps, v) + end end p = newps end diff --git a/test/downstream/linearize.jl b/test/downstream/linearize.jl index 6e9a4a070d..2561d5d936 100644 --- a/test/downstream/linearize.jl +++ b/test/downstream/linearize.jl @@ -258,3 +258,50 @@ closed_loop = ODESystem(connections, t, systems = [model, pid, filt, sensor, r, ]) @test_nowarn linearize(closed_loop, :r, :y) + +# https://discourse.julialang.org/t/mtk-change-in-linearize/115760/3 +@mtkmodel Tank_noi begin + # Model parameters + @parameters begin + ρ = 1, [description = "Liquid density"] + A = 5, [description = "Cross sectional tank area"] + K = 5, [description = "Effluent valve constant"] + h_ς = 3, [description = "Scaling level in valve model"] + end + # Model variables, with initial values needed + @variables begin + m(t) = 1.5 * ρ * A, [description = "Liquid mass"] + md_i(t), [description = "Influent mass flow rate"] + md_e(t), [description = "Effluent mass flow rate"] + V(t), [description = "Liquid volume"] + h(t), [description = "level"] + end + # Providing model equations + @equations begin + D(m) ~ md_i - md_e + m ~ ρ * V + V ~ A * h + md_e ~ K * sqrt(h / h_ς) + end +end + +@named tank_noi = Tank_noi() +@unpack md_i, h, m = tank_noi +m_ss = 2.4000000003229878 +@test_nowarn linearize(tank_noi, [md_i], [h]; op = Dict(m => m_ss, md_i => 2)) + +# Test initialization +@variables x(t) y(t) u(t) = 1.0 +@parameters p = 1.0 +eqs = [D(x) ~ p * u, x ~ y] +@named sys = ODESystem(eqs, t) + +matrices1, _ = linearize(sys, [u], []; op = Dict(x => 2.0)) +matrices2, _ = linearize(sys, [u], []; op = Dict(y => 2.0)) +@test matrices1 == matrices2 + +# Ensure parameter values passed as `Dict` are respected +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 matrice.f_u[] == 3.0