Skip to content

Commit

Permalink
Keep non-array connector variable collected (#296)
Browse files Browse the repository at this point in the history
* Keep non-array connector variable collected

* format

* update guess tests

* update tests
  • Loading branch information
baggepinnen authored Jun 10, 2024
1 parent 3ae7030 commit 6ed304b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/Blocks/utils.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@connector function RealInput(;
name, nin = 1, u_start = nothing, guess = nin > 1 ? zeros(nin) : 0.0)
nin > 1 && @warn "For inputs greater than one, use `RealInputArray`."
if u_start !== nothing
Base.depwarn(
"The keyword argument `u_start` is deprecated. Use `guess` instead.", :u_start)
Expand All @@ -16,8 +15,9 @@
input = true,
description = "Inner variable in RealInput $name"
]
u = collect(u)
end
ODESystem(Equation[], t, [u], []; name = name, guesses = [u => guess])
ODESystem(Equation[], t, [u;], []; name = name, guesses = [(u .=> guess);])
end
@doc """
RealInput(;name, guess)
Expand Down Expand Up @@ -58,7 +58,6 @@ Connector with an array of input signals of type Real.

@connector function RealOutput(;
name, nout = 1, u_start = nothing, guess = nout > 1 ? zeros(nout) : 0.0)
nout > 1 && @warn "For outputs greater than one, use `RealOutputArray`."
if u_start !== nothing
Base.depwarn(
"The keyword argument `u_start` is deprecated. Use `guess` instead.", :u_start)
Expand All @@ -74,8 +73,9 @@ Connector with an array of input signals of type Real.
output = true,
description = "Inner variable in RealOutput $name"
]
u = collect(u)
end
ODESystem(Equation[], t, [u], []; name = name, guesses = [u => guess])
ODESystem(Equation[], t, [u;], []; name = name, guesses = [(u .=> guess);])
end
@doc """
RealOutput(;name, guess)
Expand Down
11 changes: 5 additions & 6 deletions test/Blocks/test_analysis_points.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ matrices, _ = get_comp_sensitivity(sys, :plant_input)

## get_looptransfer

matrices, _ = Blocks.get_looptransfer(sys, :plant_input; p = Dict(P.input.u => P.u_start))
matrices, _ = Blocks.get_looptransfer(sys, :plant_input)
@test matrices.A[] == -1
@test matrices.B[] * matrices.C[] == -1 # either one negative
@test matrices.D[] == 0
Expand Down Expand Up @@ -308,7 +308,7 @@ T = -CS.feedback(Kss * Pss, I(2), pos_feedback = true)
@test CS.tf(CS.ss(matrices...)) CS.tf(T)

matrices, _ = Blocks.get_looptransfer(
sys, :plant_input; p = Dict(P.input.u[1] => 0.0, P.input.u[2] => 0.0))
sys, :plant_input)
L = Kss * Pss
@test CS.tf(CS.ss(matrices...)) CS.tf(L)

Expand Down Expand Up @@ -361,18 +361,17 @@ To = CS.feedback(Ps * Cs)

# matrices, _ = get_looptransfer(sys_outer, [:inner_plant_input, :inner_plant_output])
matrices, _ = get_looptransfer(
sys_outer, :inner_plant_input; p = Dict(sys_inner.P.input.u => sys_inner.P.u_start))
sys_outer, :inner_plant_input)
L = CS.ss(matrices...) |> sminreal
@test tf(L) -tf(Cs * Ps)

matrices, _ = get_looptransfer(
sys_outer, :inner_plant_output; p = Dict(sys_inner.add.input2.u => 0.0))
sys_outer, :inner_plant_output)
L = CS.ss(matrices...) |> sminreal
@test tf(L[1, 1]) -tf(Ps * Cs)

# Calling looptransfer like below is not the intended way, but we can work out what it should return if we did so it remains a valid test
matrices, _ = get_looptransfer(sys_outer, [:inner_plant_input, :inner_plant_output];
p = Dict(sys_inner.P.input.u => sys_inner.P.u_start, sys_inner.add.input2.u => 0.0))
matrices, _ = get_looptransfer(sys_outer, [:inner_plant_input, :inner_plant_output])
L = CS.ss(matrices...) |> sminreal
@test tf(L[1, 1]) tf(0)
@test tf(L[2, 2]) tf(0)
Expand Down
18 changes: 13 additions & 5 deletions test/Blocks/utils.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using ModelingToolkitStandardLibrary.Blocks
using ModelingToolkit

@testset "Guesses" begin
@testset "Array Guesses" begin
for (block, guess) in [
(RealInput(; name = :a), 0.0),
(RealInput(; nin = 3, name = :a), zeros(3)),
(RealOutput(; name = :a), 0.0),
(RealOutput(; nout = 3, name = :a), zeros(3)),
(RealInputArray(; nin = 3, name = :a), zeros(3)),
(RealOutputArray(; nout = 3, name = :a), zeros(3))
]
Expand All @@ -15,6 +11,18 @@ using ModelingToolkit
end
end

@testset "Scalarized Guesses" begin
for (block, guess) in [
(RealInput(; name = :a), 0.0),
(RealInput(; nin = 3, name = :a), zeros(3)),
(RealOutput(; name = :a), 0.0),
(RealOutput(; nout = 3, name = :a), zeros(3))
]
guesses = ModelingToolkit.guesses(block)
@test guesses[@nonamespace block.u[1]] == guess[1]
end
end

@test_deprecated RealInput(; name = :a, u_start = 1.0)
@test_deprecated RealInput(; name = :a, nin = 2, u_start = ones(2))
@test_deprecated RealOutput(; name = :a, u_start = 1.0)
Expand Down

0 comments on commit 6ed304b

Please sign in to comment.