Skip to content

Commit 6bf4b5d

Browse files
author
Sathvik Bhagavan
committed
refactor: add RealInputArray and RealOutputArray
1 parent 633823c commit 6bf4b5d

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

src/Blocks/Blocks.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import IfElse: ifelse
77
import ..@symcheck
88
using ModelingToolkit: getdefault, t_nounits as t, D_nounits as D
99

10-
export RealInput, RealOutput, SISO
10+
export RealInput, RealInputArray, RealOutput, RealOutputArray, SISO
1111
include("utils.jl")
1212

1313
export Gain, Sum, MatrixGain, Feedback, Add, Add3, Product, Division

src/Blocks/utils.jl

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
@connector function RealInput(;
2-
name, nin = 1, isarray = false, u_start = isarray ? zeros(nin) : 0.0)
3-
if nin == 1 && !isarray
1+
@connector function RealInput(; name, nin = 1, u_start = nin > 1 ? zeros(nin) : 0.0)
2+
nin > 1 && @warn "For inputs greater than one, use `RealInputArray`."
3+
if nin == 1
44
@variables u(t)=u_start [
55
input = true,
66
description = "Inner variable in RealInput $name"
@@ -15,22 +15,41 @@
1515
ODESystem(Equation[], t, [u...], []; name = name)
1616
end
1717
@doc """
18-
RealInput(;name, nin, u_start)
18+
RealInput(;name, u_start)
1919
2020
Connector with one input signal of type Real.
2121
2222
# Parameters:
23-
- `nin=1`: Number of inputs
24-
- `u_start=0`: Initial value for `u`
25-
- `isarray=false`: This is only applicable for `nin=1`. Boolean flag to use a scalar or a one element symbolic vector
23+
- `u_start=0`: Initial value for `u`.
2624
2725
# States:
28-
- `u`: Value of the connector; if nin=1 this is a scalar
26+
- `u`: Value of the connector which is a scalar.
2927
""" RealInput
3028

31-
@connector function RealOutput(;
32-
name, nout = 1, isarray = false, u_start = isarray ? zeros(nout) : 0.0)
33-
if nout == 1 && !isarray
29+
@connector function RealInputArray(; name, nin = 2, u_start = zeros(nin))
30+
@variables u(t)[1:nin]=u_start [
31+
input = true,
32+
description = "Inner variable in RealInputArray $name"
33+
]
34+
u = collect(u)
35+
ODESystem(Equation[], t, [u...], []; name = name)
36+
end
37+
@doc """
38+
RealInputArray(;name, nin, u_start)
39+
40+
Connector with an array of input signals of type Real.
41+
42+
# Parameters:
43+
- `nin=2`: Number of inputs.
44+
- `u_start=zeros(nin)`: Initial value for `u`.
45+
46+
# States:
47+
- `u`: Value of the connector which is an array.
48+
""" RealInputArray
49+
50+
@connector function RealOutput(; name, nout = 1, u_start = nout > 1 ? zeros(nout) : 0.0)
51+
nout > 1 && @warn "For outputs greater than one, use `RealOutputArray`."
52+
if nout == 1
3453
@variables u(t)=u_start [
3554
output = true,
3655
description = "Inner variable in RealOutput $name"
@@ -45,19 +64,38 @@ Connector with one input signal of type Real.
4564
ODESystem(Equation[], t, [u...], []; name = name)
4665
end
4766
@doc """
48-
RealOutput(;name, nout, u_start)
67+
RealOutput(;name, u_start)
4968
5069
Connector with one output signal of type Real.
5170
5271
# Parameters:
53-
- `nout=1`: Number of outputs
54-
- `u_start=0`: Initial value for `u`
55-
- `isarray=false`: This is only applicable for `nout=1`. Boolean flag to use a scalar or a one element symbolic vector
72+
- `u_start=0`: Initial value for `u`.
5673
5774
# States:
58-
- `u`: Value of the connector; if nout=1 this is a scalar
75+
- `u`: Value of the connector which is a scalar.
5976
""" RealOutput
6077

78+
@connector function RealOutputArray(; name, nout = 2, u_start = zeros(nout))
79+
@variables u(t)[1:nout]=u_start [
80+
output = true,
81+
description = "Inner variable in RealOutput $name"
82+
]
83+
u = collect(u)
84+
ODESystem(Equation[], t, [u...], []; name = name)
85+
end
86+
@doc """
87+
RealOutputArray(;name, nout, u_start)
88+
89+
Connector with an array of output signals of type Real.
90+
91+
# Parameters:
92+
- `nout=2`: Number of outputs.
93+
- `u_start=zeros(nout)`: Initial value for `u`.
94+
95+
# States:
96+
- `u`: Value of the connector which is an array.
97+
""" RealOutputArray
98+
6199
"""
62100
SISO(;name, u_start = 0.0, y_start = 0.0)
63101

0 commit comments

Comments
 (0)