From 527a96b4de12002a369dc6a262fe40ee97ba18ac Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Mon, 16 Oct 2023 07:52:28 +0200 Subject: [PATCH 1/2] allow scalar IO in `linearize` --- src/systems/abstractsystem.jl | 16 +++++++++------- test/linearize.jl | 7 +++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index c34fd24885..4e60c409fb 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1285,12 +1285,14 @@ The `simplified_sys` has undergone [`structural_simplify`](@ref) and had any occ See also [`linearize`](@ref) which provides a higher-level interface. """ function linearization_function(sys::AbstractSystem, inputs, - outputs; simplify = false, - initialize = true, - op = Dict(), - p = DiffEqBase.NullParameters(), - zero_dummy_der = false, - kwargs...) + outputs; simplify = false, + initialize = true, + op = Dict(), + p = DiffEqBase.NullParameters(), + zero_dummy_der = false, + kwargs...) + inputs isa AbstractVector || (inputs = [inputs]) + outputs isa AbstractVector || (outputs = [outputs]) ssys, diff_idxs, alge_idxs, input_idxs = io_preprocessing(sys, inputs, outputs; simplify, kwargs...) @@ -1584,7 +1586,7 @@ lsys_sym, _ = ModelingToolkit.linearize_symbolic(cl, [f.u], [p.x]) ``` """ function linearize(sys, lin_fun; t = 0.0, op = Dict(), allow_input_derivatives = false, - p = DiffEqBase.NullParameters()) + p = DiffEqBase.NullParameters(), kwargs...) x0 = merge(defaults(sys), op) u0, p2, _ = get_u0_p(sys, x0, p; use_union = false, tofloat = true) diff --git a/test/linearize.jl b/test/linearize.jl index 8561d26819..d77793275c 100644 --- a/test/linearize.jl +++ b/test/linearize.jl @@ -26,6 +26,13 @@ lsys, ssys = linearize(sys, [r], [r]) @test lsys.C[] == 0 @test lsys.D[] == 1 +lsys, ssys = linearize(sys, r, r) # Test allow scalars + +@test lsys.A[] == -2 +@test lsys.B[] == 1 +@test lsys.C[] == 0 +@test lsys.D[] == 1 + ## ``` From dd23d21d25a379b4db8cdb729cd80fd4747c755c Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Fri, 3 Nov 2023 08:00:06 +0100 Subject: [PATCH 2/2] improve linearize docstring rm kwargs... --- src/systems/abstractsystem.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 4e60c409fb..fb65274828 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -1285,12 +1285,12 @@ The `simplified_sys` has undergone [`structural_simplify`](@ref) and had any occ See also [`linearize`](@ref) which provides a higher-level interface. """ function linearization_function(sys::AbstractSystem, inputs, - outputs; simplify = false, - initialize = true, - op = Dict(), - p = DiffEqBase.NullParameters(), - zero_dummy_der = false, - kwargs...) + outputs; simplify = false, + initialize = true, + op = Dict(), + p = DiffEqBase.NullParameters(), + zero_dummy_der = false, + kwargs...) inputs isa AbstractVector || (inputs = [inputs]) outputs isa AbstractVector || (outputs = [outputs]) ssys, diff_idxs, alge_idxs, input_idxs = io_preprocessing(sys, inputs, outputs; @@ -1488,7 +1488,7 @@ end (; A, B, C, D), simplified_sys = linearize(sys, inputs, outputs; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false, kwargs...) (; A, B, C, D) = linearize(simplified_sys, lin_fun; t=0.0, op = Dict(), allow_input_derivatives = false, zero_dummy_der=false) -Return a NamedTuple with the matrices of a linear statespace representation +Linearize `sys` between `inputs` and `outputs`, both vectors of variables. Return a NamedTuple with the matrices of a linear statespace representation on the form ```math @@ -1586,7 +1586,7 @@ lsys_sym, _ = ModelingToolkit.linearize_symbolic(cl, [f.u], [p.x]) ``` """ function linearize(sys, lin_fun; t = 0.0, op = Dict(), allow_input_derivatives = false, - p = DiffEqBase.NullParameters(), kwargs...) + p = DiffEqBase.NullParameters()) x0 = merge(defaults(sys), op) u0, p2, _ = get_u0_p(sys, x0, p; use_union = false, tofloat = true)