Skip to content

Commit 07111c2

Browse files
committed
refactor: Blocks/math.jl
1 parent 7a5c20a commit 07111c2

File tree

2 files changed

+99
-72
lines changed

2 files changed

+99
-72
lines changed

src/Blocks/math.jl

Lines changed: 97 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Output the product of a gain value with the input signal.
1515
@mtkmodel Gain begin
1616
@extend u, y = siso = SISO()
1717
@parameters begin
18-
k, [description = "Gain function"]
18+
k, [description = "Gain"]
1919
end
2020
@equations begin
2121
y ~ k * u
@@ -37,13 +37,23 @@ Output the product of a gain matrix with the input signal vector.
3737
- `input`
3838
- `output`
3939
"""
40-
@component function MatrixGain(K::AbstractArray; name)
41-
nout, nin = size(K, 1), size(K, 2)
42-
@named input = RealInput(; nin = nin)
43-
@named output = RealOutput(; nout = nout)
44-
eqs = [output.u[i] ~ sum(K[i, j] * input.u[j] for j in 1:nin) for i in 1:nout]
45-
compose(ODESystem(eqs, t, [], []; name = name), [input, output])
40+
@mtkmodel MatrixGain begin
41+
@parameters begin
42+
K
43+
end
44+
begin
45+
nout = size(getdefault(K), 1)
46+
nin = size(getdefault(K), 2)
47+
end
48+
@components begin
49+
input = RealInput(; nin = size(K, 2))
50+
output = RealOutput(; nout = size(K, 1))
51+
end
52+
@equations begin
53+
[(@info i, j; output.u[i] ~ sum(getdefault(K)[i, j] * input.u[j])) for j in 1:nin for i in 1:nout]...
54+
end
4655
end
56+
MatrixGain.f(K; name) = MatrixGain.f(; name, K)
4757

4858
"""
4959
Sum(n::Int; name)
@@ -59,14 +69,19 @@ Output the sum of the elements of the input port vector.
5969
- `input`
6070
- `output`
6171
"""
62-
@component function Sum(n::Int; name)
63-
@named input = RealInput(; nin = n)
64-
@named output = RealOutput()
65-
eqs = [
66-
output.u ~ sum(input.u),
67-
]
68-
compose(ODESystem(eqs, t, [], []; name = name), [input, output])
72+
@mtkmodel Sum begin
73+
@parameters begin
74+
n
75+
end
76+
@components begin
77+
input = RealInput(; nin = n)
78+
output = RealOutput()
79+
end
80+
@equations begin
81+
output.u ~ sum(input.u)
82+
end
6983
end
84+
Sum.f(n; name) = Sum.f(; n, name)
7085

7186
"""
7287
Feedback(;name)
@@ -91,7 +106,7 @@ Output difference between reference input (input1) and feedback input (input2).
91106
end
92107

93108
"""
94-
Add(;name, k1=1, k2=1)
109+
Add(; name, k1 = 1, k2 = 1)
95110
96111
Output the sum of the two scalar inputs.
97112
@@ -106,20 +121,23 @@ Output the sum of the two scalar inputs.
106121
- `input2`
107122
- `output`
108123
"""
109-
@component function Add(; name, k1 = 1, k2 = 1)
110-
@named input1 = RealInput()
111-
@named input2 = RealInput()
112-
@named output = RealOutput()
113-
pars = @parameters(k1=k1, [description = "Gain of Add $name input1"],
114-
k2=k2, [description = "Gain of Add $name input2"],)
115-
eqs = [
116-
output.u ~ k1 * input1.u + k2 * input2.u,
117-
]
118-
return compose(ODESystem(eqs, t, [], pars; name = name), input1, input2, output)
124+
@mtkmodel Add begin
125+
@components begin
126+
input1 = RealInput()
127+
input2 = RealInput()
128+
output = RealOutput()
129+
end
130+
@parameters begin
131+
k1 = 1, [description = "Gain of Add input1"]
132+
k2 = 1, [description = "Gain of Add input2"]
133+
end
134+
@equations begin
135+
output.u ~ k1 * input1.u + k2 * input2.u
136+
end
119137
end
120138

121139
"""
122-
Add(;name, k1=1, k2=1,k3=1)
140+
Add(; name, k1 = 1, k2 = 1,k3 = 1)
123141
124142
Output the sum of the three scalar inputs.
125143
@@ -136,22 +154,25 @@ Output the sum of the three scalar inputs.
136154
- `input3`
137155
- `output`
138156
"""
139-
@component function Add3(; name, k1 = 1, k2 = 1, k3 = 1)
140-
@named input1 = RealInput()
141-
@named input2 = RealInput()
142-
@named input3 = RealInput()
143-
@named output = RealOutput()
144-
pars = @parameters(k1=k1, [description = "Gain of Add $name input1"],
145-
k2=k2, [description = "Gain of Add $name input2"],
146-
k3=k3, [description = "Gain of Add $name input3"],)
147-
eqs = [
148-
output.u ~ k1 * input1.u + k2 * input2.u + k3 * input3.u,
149-
]
150-
return compose(ODESystem(eqs, t, [], pars; name = name), input1, input2, input3, output)
157+
@mtkmodel Add3 begin
158+
@components begin
159+
input1 = RealInput()
160+
input2 = RealInput()
161+
input3 = RealInput()
162+
output = RealOutput()
163+
end
164+
@parameters begin
165+
k1 = 1, [description = "Gain of Add input1"]
166+
k2 = 1, [description = "Gain of Add input2"]
167+
k3 = 1, [description = "Gain of Add input3"]
168+
end
169+
@equations begin
170+
output.u ~ k1 * input1.u + k2 * input2.u + k3 * input3.u
171+
end
151172
end
152173

153174
"""
154-
Product(;name)
175+
Product(; name)
155176
156177
Output product of the two inputs.
157178
@@ -161,18 +182,19 @@ Output product of the two inputs.
161182
- `input2`
162183
- `output`
163184
"""
164-
@component function Product(; name)
165-
@named input1 = RealInput()
166-
@named input2 = RealInput()
167-
@named output = RealOutput()
168-
eqs = [
169-
output.u ~ input1.u * input2.u,
170-
]
171-
return compose(ODESystem(eqs, t, [], []; name = name), input1, input2, output)
185+
@mtkmodel Product begin
186+
@components begin
187+
input1 = RealInput()
188+
input2 = RealInput()
189+
output = RealOutput()
190+
end
191+
@equations begin
192+
output.u ~ input1.u * input2.u
193+
end
172194
end
173195

174196
"""
175-
Division(;name)
197+
Division(; name)
176198
177199
Output first input divided by second input.
178200
@@ -182,14 +204,15 @@ Output first input divided by second input.
182204
- `input2`
183205
- `output`
184206
"""
185-
@component function Division(; name)
186-
@named input1 = RealInput()
187-
@named input2 = RealInput(u_start = 1.0) # denominator can not be zero
188-
@named output = RealOutput()
189-
eqs = [
190-
output.u ~ input1.u / input2.u,
191-
]
192-
return compose(ODESystem(eqs, t, [], []; name = name), input1, input2, output)
207+
@mtkmodel Division begin
208+
@components begin
209+
input1 = RealInput()
210+
input2 = RealInput(u_start = 1.0) # denominator can not be zero
211+
output = RealOutput()
212+
end
213+
@equations begin
214+
output.u ~ input1.u / input2.u
215+
end
193216
end
194217

195218
"""
@@ -204,12 +227,16 @@ If the given function is not composed of simple core methods (e.g. sin, abs, ...
204227
- `input`
205228
- `output`
206229
"""
207-
@component function StaticNonLinearity(func; name)
208-
@named siso = SISO()
209-
@unpack u, y = siso
210-
eqs = [y ~ func(u)]
211-
extend(ODESystem(eqs, t, [], []; name = name), siso)
230+
@mtkmodel StaticNonLinearity begin
231+
@parameters begin
232+
func
233+
end
234+
@extend u, y = siso = SISO()
235+
@equations begin
236+
y ~ first(getdefault(func))(u)
237+
end
212238
end
239+
StaticNonLinearity.f(func ; name) = StaticNonLinearity.f(; func = [func], name)
213240

214241
"""
215242
Abs(;name)
@@ -321,14 +348,15 @@ Output the arc tangent of the input.
321348
- `input2`
322349
- `output`
323350
"""
324-
@component function Atan2(; name)
325-
@named input1 = RealInput()
326-
@named input2 = RealInput()
327-
@named output = RealOutput()
328-
eqs = [
329-
output.u ~ atan(input1.u, input2.u),
330-
]
331-
compose(ODESystem(eqs, t, [], []; name = name), [input1, input2, output])
351+
@mtkmodel Atan2 begin
352+
@components begin
353+
input1 = RealInput()
354+
input2 = RealInput()
355+
output = RealOutput()
356+
end
357+
@equations begin
358+
output.u ~ atan(input1.u, input2.u)
359+
end
332360
end
333361

334362
"""

test/Blocks/math.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ using OrdinaryDiffEq: ReturnCode.Success
2424
@test all(sol[c.output.u] .≈ 1)
2525
@test sol[int.output.u][end] 2 # expected solution after 1s
2626
end
27-
#=
27+
2828
@testset "Feedback loop" begin
2929
@named c = Constant(; k = 2)
3030
@named gain = Gain(; k = 1)
@@ -191,7 +191,7 @@ end
191191

192192
@testset "MatrixGain" begin
193193
K = [1 2; 3 4]
194-
@named gain = MatrixGain(K)
194+
@named gain = MatrixGain(; K)
195195
K = [1, 2]
196196
@named gain = MatrixGain(K)
197197
# TODO:
@@ -275,4 +275,3 @@ end
275275
@test sol.retcode == Success
276276
@test sol[int.input.u] atan.(sol[c1.output.u], sol[c2.output.u])
277277
end
278-
=#

0 commit comments

Comments
 (0)