Skip to content

refactor(Blocks): add RealInputArray and RealOutputArray #283

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 5 commits into from
Apr 18, 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
2 changes: 1 addition & 1 deletion src/Blocks/Blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import IfElse: ifelse
import ..@symcheck
using ModelingToolkit: getdefault, t_nounits as t, D_nounits as D

export RealInput, RealOutput, SISO
export RealInput, RealInputArray, RealOutput, RealOutputArray, SISO
include("utils.jl")

export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division
Expand Down
70 changes: 56 additions & 14 deletions src/Blocks/utils.jl
Original file line number Diff line number Diff line change
@@ -1,59 +1,101 @@
@connector function RealInput(; name, nin = 1, u_start = nin > 1 ? zeros(nin) : 0.0)
nin > 1 && @warn "For inputs greater than one, use `RealInputArray`."
if nin == 1
@variables u(t)=u_start [
@variables u(t) [
input = true,
description = "Inner variable in RealInput $name"
]
else
@variables u(t)[1:nin]=u_start [
@variables u(t)[1:nin] [
input = true,
description = "Inner variable in RealInput $name"
]
u = collect(u)
end
ODESystem(Equation[], t, [u...], []; name = name)
ODESystem(Equation[], t, [u...], []; name = name, guesses = [u => u_start])
end
@doc """
RealInput(;name, nin, u_start)
RealInput(;name, u_start)

Connector with one input signal of type Real.

# Parameters:
- `nin=1`: Number of inputs
- `u_start=0`: Initial value for `u`
- `u_start=0`: Guess value for `u`.

# States:
- `u`: Value of the connector; if nin=1 this is a scalar
- `u`: Value of the connector which is a scalar.
""" RealInput

@connector function RealInputArray(; name, nin, u_start = zeros(nin))
@variables u(t)[1:nin] [
input = true,
description = "Inner variable in RealInputArray $name"
]
u = collect(u)
ODESystem(Equation[], t, [u...], []; name = name, guesses = [u => u_start])
end
@doc """
RealInputArray(;name, nin, u_start)

Connector with an array of input signals of type Real.

# Parameters:
- `nin`: Number of inputs.
- `u_start=zeros(nin)`: Guess value for `u`.

# States:
- `u`: Value of the connector which is an array.
""" RealInputArray

@connector function RealOutput(; name, nout = 1, u_start = nout > 1 ? zeros(nout) : 0.0)
nout > 1 && @warn "For outputs greater than one, use `RealOutputArray`."
if nout == 1
@variables u(t)=u_start [
@variables u(t) [
output = true,
description = "Inner variable in RealOutput $name"
]
else
@variables u(t)[1:nout]=u_start [
@variables u(t)[1:nout] [
output = true,
description = "Inner variable in RealOutput $name"
]
u = collect(u)
end
ODESystem(Equation[], t, [u...], []; name = name)
ODESystem(Equation[], t, [u...], []; name = name, guesses = [u => u_start])
end
@doc """
RealOutput(;name, nout, u_start)
RealOutput(;name, u_start)

Connector with one output signal of type Real.

# Parameters:
- `nout=1`: Number of outputs
- `u_start=0`: Initial value for `u`
- `u_start=0`: Guess value for `u`.

# States:
- `u`: Value of the connector; if nout=1 this is a scalar
- `u`: Value of the connector which is a scalar.
""" RealOutput

@connector function RealOutputArray(; name, nout, u_start = zeros(nout))
@variables u(t)[1:nout] [
output = true,
description = "Inner variable in RealOutputArray $name"
]
u = collect(u)
ODESystem(Equation[], t, [u...], []; name = name, guesses = [u => u_start])
end
@doc """
RealOutputArray(;name, nout, u_start)

Connector with an array of output signals of type Real.

# Parameters:
- `nout`: Number of outputs.
- `u_start=zeros(nout)`: Guess value for `u`.

# States:
- `u`: Value of the connector which is an array.
""" RealOutputArray

"""
SISO(;name, u_start = 0.0, y_start = 0.0)

Expand Down
14 changes: 9 additions & 5 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)
matrices, _ = Blocks.get_looptransfer(sys, :plant_input; p = Dict(P.input.u => P.u_start))
@test matrices.A[] == -1
@test matrices.B[] * matrices.C[] == -1 # either one negative
@test matrices.D[] == 0
Expand Down Expand Up @@ -299,7 +299,8 @@ T = -CS.feedback(Kss * Pss, I(2), pos_feedback = true)
# bodeplot([ss(matrices...), T])
@test CS.tf(CS.ss(matrices...)) ≈ CS.tf(T)

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

Expand Down Expand Up @@ -351,16 +352,19 @@ To = CS.feedback(Ps * Cs)
@test tf(G[2, 1]) ≈ tf(-CS.feedback(Ps, Cs))

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

matrices, _ = get_looptransfer(sys_outer, :inner_plant_output)
matrices, _ = get_looptransfer(
sys_outer, :inner_plant_output; p = Dict(sys_inner.add.input2.u => 0.0))
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])
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))
L = CS.ss(matrices...) |> sminreal
@test tf(L[1, 1]) ≈ tf(0)
@test tf(L[2, 2]) ≈ tf(0)
Expand Down