Skip to content

Commit 05f718d

Browse files
Merge pull request #291 from AayushSabharwal/as/fix-realio-guesses
fix: fix guesses for `Real*Input` and `Real*Output`
2 parents 37faf7b + 7b38a53 commit 05f718d

File tree

5 files changed

+73
-27
lines changed

5 files changed

+73
-27
lines changed

src/Blocks/math.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ Output first input divided by second input.
200200
@mtkmodel Division begin
201201
@components begin
202202
input1 = RealInput()
203-
input2 = RealInput(u_start = 1.0) # denominator can not be zero
203+
input2 = RealInput(guess = 1.0) # denominator can not be zero
204204
output = RealOutput()
205205
end
206206
@equations begin

src/Blocks/sources.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,15 @@ Generate step signal.
320320
321321
- `output`
322322
"""
323-
@component function Step(; name, height = 1.0, offset = 0.0, start_time = 0.0, duration = Inf,
323+
@component function Step(;
324+
name, height = 1.0, offset = 0.0, start_time = 0.0, duration = Inf,
324325
smooth = 1e-5)
325326
@named output = RealOutput()
326327
duration_numeric = duration
327328
pars = @parameters offset=offset start_time=start_time height=height duration=duration
328329
equation = if smooth == false # use comparison in case smooth is a float
329-
offset + ifelse((start_time < t) & (t < start_time + duration), height, zero(height))
330+
offset +
331+
ifelse((start_time < t) & (t < start_time + duration), height, zero(height))
330332
else
331333
smooth === true && (smooth = 1e-5)
332334
if duration_numeric == Inf

src/Blocks/utils.jl

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
@connector function RealInput(; name, nin = 1, u_start = nin > 1 ? zeros(nin) : 0.0)
1+
@connector function RealInput(;
2+
name, nin = 1, u_start = nothing, guess = nin > 1 ? zeros(nin) : 0.0)
23
nin > 1 && @warn "For inputs greater than one, use `RealInputArray`."
4+
if u_start !== nothing
5+
Base.depwarn(
6+
"The keyword argument `u_start` is deprecated. Use `guess` instead.", :u_start)
7+
guess = u_start
8+
end
39
if nin == 1
410
@variables u(t) [
511
input = true,
@@ -10,45 +16,54 @@
1016
input = true,
1117
description = "Inner variable in RealInput $name"
1218
]
13-
u = collect(u)
1419
end
15-
ODESystem(Equation[], t, [u...], []; name = name, guesses = [u => u_start])
20+
ODESystem(Equation[], t, [u], []; name = name, guesses = [u => guess])
1621
end
1722
@doc """
18-
RealInput(;name, u_start)
23+
RealInput(;name, guess)
1924
2025
Connector with one input signal of type Real.
2126
2227
# Parameters:
23-
- `u_start=0`: Guess value for `u`.
28+
- `guess=0`: Guess value for `u`.
2429
2530
# States:
2631
- `u`: Value of the connector which is a scalar.
2732
""" RealInput
2833

29-
@connector function RealInputArray(; name, nin, u_start = zeros(nin))
34+
@connector function RealInputArray(; name, nin, u_start = nothing, guess = zeros(nin))
35+
if u_start !== nothing
36+
Base.depwarn(
37+
"The keyword argument `u_start` is deprecated. Use `guess` instead.", :u_start)
38+
guess = u_start
39+
end
3040
@variables u(t)[1:nin] [
3141
input = true,
3242
description = "Inner variable in RealInputArray $name"
3343
]
34-
u = collect(u)
35-
ODESystem(Equation[], t, [u...], []; name = name, guesses = [u => u_start])
44+
ODESystem(Equation[], t, [u], []; name = name, guesses = [u => guess])
3645
end
3746
@doc """
38-
RealInputArray(;name, nin, u_start)
47+
RealInputArray(;name, nin, guess)
3948
4049
Connector with an array of input signals of type Real.
4150
4251
# Parameters:
4352
- `nin`: Number of inputs.
44-
- `u_start=zeros(nin)`: Guess value for `u`.
53+
- `guess=zeros(nin)`: Guess value for `u`.
4554
4655
# States:
4756
- `u`: Value of the connector which is an array.
4857
""" RealInputArray
4958

50-
@connector function RealOutput(; name, nout = 1, u_start = nout > 1 ? zeros(nout) : 0.0)
59+
@connector function RealOutput(;
60+
name, nout = 1, u_start = nothing, guess = nout > 1 ? zeros(nout) : 0.0)
5161
nout > 1 && @warn "For outputs greater than one, use `RealOutputArray`."
62+
if u_start !== nothing
63+
Base.depwarn(
64+
"The keyword argument `u_start` is deprecated. Use `guess` instead.", :u_start)
65+
guess = u_start
66+
end
5267
if nout == 1
5368
@variables u(t) [
5469
output = true,
@@ -59,38 +74,41 @@ Connector with an array of input signals of type Real.
5974
output = true,
6075
description = "Inner variable in RealOutput $name"
6176
]
62-
u = collect(u)
6377
end
64-
ODESystem(Equation[], t, [u...], []; name = name, guesses = [u => u_start])
78+
ODESystem(Equation[], t, [u], []; name = name, guesses = [u => guess])
6579
end
6680
@doc """
67-
RealOutput(;name, u_start)
81+
RealOutput(;name, guess)
6882
6983
Connector with one output signal of type Real.
7084
7185
# Parameters:
72-
- `u_start=0`: Guess value for `u`.
86+
- `guess=0`: Guess value for `u`.
7387
7488
# States:
7589
- `u`: Value of the connector which is a scalar.
7690
""" RealOutput
7791

78-
@connector function RealOutputArray(; name, nout, u_start = zeros(nout))
92+
@connector function RealOutputArray(; name, nout, u_start = nothing, guess = zeros(nout))
93+
if u_start !== nothing
94+
Base.depwarn(
95+
"The keyword argument `u_start` is deprecated. Use `guess` instead.", :u_start)
96+
guess = u_start
97+
end
7998
@variables u(t)[1:nout] [
8099
output = true,
81100
description = "Inner variable in RealOutputArray $name"
82101
]
83-
u = collect(u)
84-
ODESystem(Equation[], t, [u...], []; name = name, guesses = [u => u_start])
102+
ODESystem(Equation[], t, [u], []; name = name, guesses = [u => guess])
85103
end
86104
@doc """
87-
RealOutputArray(;name, nout, u_start)
105+
RealOutputArray(;name, nout, guess)
88106
89107
Connector with an array of output signals of type Real.
90108
91109
# Parameters:
92110
- `nout`: Number of outputs.
93-
- `u_start=zeros(nout)`: Guess value for `u`.
111+
- `guess=zeros(nout)`: Guess value for `u`.
94112
95113
# States:
96114
- `u`: Value of the connector which is an array.
@@ -116,8 +134,8 @@ Single input single output (SISO) continuous system block.
116134
y(t) = y_start, [description = "Output of SISO system"]
117135
end
118136
@components begin
119-
input = RealInput(u_start = u_start)
120-
output = RealOutput(u_start = y_start)
137+
input = RealInput(guess = u_start)
138+
output = RealOutput(guess = y_start)
121139
end
122140
@equations begin
123141
u ~ input.u
@@ -139,8 +157,8 @@ Base class for a multiple input multiple output (MIMO) continuous system block.
139157
"""
140158
@component function MIMO(; name, nin = 1, nout = 1, u_start = zeros(nin),
141159
y_start = zeros(nout))
142-
@named input = RealInput(nin = nin, u_start = u_start)
143-
@named output = RealOutput(nout = nout, u_start = y_start)
160+
@named input = RealInput(nin = nin, guess = u_start)
161+
@named output = RealOutput(nout = nout, guess = y_start)
144162
@variables(u(t)[1:nin]=u_start, [description = "Input of MIMO system $name"],
145163
y(t)[1:nout]=y_start, [description = "Output of MIMO system $name"],)
146164
eqs = [

test/Blocks/utils.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using ModelingToolkitStandardLibrary.Blocks
2+
using ModelingToolkit
3+
4+
@testset "Guesses" begin
5+
for (block, guess) in [
6+
(RealInput(; name = :a), 0.0),
7+
(RealInput(; nin = 3, name = :a), zeros(3)),
8+
(RealOutput(; name = :a), 0.0),
9+
(RealOutput(; nout = 3, name = :a), zeros(3)),
10+
(RealInputArray(; nin = 3, name = :a), zeros(3)),
11+
(RealOutputArray(; nout = 3, name = :a), zeros(3))
12+
]
13+
guesses = ModelingToolkit.guesses(block)
14+
@test guesses[@nonamespace block.u] == guess
15+
end
16+
end
17+
18+
@test_deprecated RealInput(; name = :a, u_start = 1.0)
19+
@test_deprecated RealInput(; name = :a, nin = 2, u_start = ones(2))
20+
@test_deprecated RealOutput(; name = :a, u_start = 1.0)
21+
@test_deprecated RealOutput(; name = :a, nout = 2, u_start = ones(2))
22+
@test_deprecated RealInputArray(; name = :a, nin = 2, u_start = ones(2))
23+
@test_deprecated RealOutputArray(; name = :a, nout = 2, u_start = ones(2))

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ using SafeTestsets
55
end
66

77
# Blocks
8+
@safetestset "Blocks: utils" begin
9+
include("Blocks/utils.jl")
10+
end
811
@safetestset "Blocks: math" begin
912
include("Blocks/math.jl")
1013
end

0 commit comments

Comments
 (0)