@@ -15,7 +15,7 @@ Output the product of a gain value with the input signal.
15
15
@mtkmodel Gain begin
16
16
@extend u, y = siso = SISO ()
17
17
@parameters begin
18
- k, [description = " Gain function " ]
18
+ k, [description = " Gain" ]
19
19
end
20
20
@equations begin
21
21
y ~ k * u
@@ -37,13 +37,23 @@ Output the product of a gain matrix with the input signal vector.
37
37
- `input`
38
38
- `output`
39
39
"""
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
46
55
end
56
+ MatrixGain. f (K; name) = MatrixGain. f (; name, K)
47
57
48
58
"""
49
59
Sum(n::Int; name)
@@ -59,14 +69,19 @@ Output the sum of the elements of the input port vector.
59
69
- `input`
60
70
- `output`
61
71
"""
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
69
83
end
84
+ Sum. f (n; name) = Sum. f (; n, name)
70
85
71
86
"""
72
87
Feedback(;name)
@@ -91,7 +106,7 @@ Output difference between reference input (input1) and feedback input (input2).
91
106
end
92
107
93
108
"""
94
- Add(;name, k1= 1, k2= 1)
109
+ Add(; name, k1 = 1, k2 = 1)
95
110
96
111
Output the sum of the two scalar inputs.
97
112
@@ -106,20 +121,23 @@ Output the sum of the two scalar inputs.
106
121
- `input2`
107
122
- `output`
108
123
"""
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
119
137
end
120
138
121
139
"""
122
- Add(;name, k1= 1, k2= 1,k3= 1)
140
+ Add(; name, k1 = 1, k2 = 1,k3 = 1)
123
141
124
142
Output the sum of the three scalar inputs.
125
143
@@ -136,22 +154,25 @@ Output the sum of the three scalar inputs.
136
154
- `input3`
137
155
- `output`
138
156
"""
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
151
172
end
152
173
153
174
"""
154
- Product(;name)
175
+ Product(; name)
155
176
156
177
Output product of the two inputs.
157
178
@@ -161,18 +182,19 @@ Output product of the two inputs.
161
182
- `input2`
162
183
- `output`
163
184
"""
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
172
194
end
173
195
174
196
"""
175
- Division(;name)
197
+ Division(; name)
176
198
177
199
Output first input divided by second input.
178
200
@@ -182,14 +204,15 @@ Output first input divided by second input.
182
204
- `input2`
183
205
- `output`
184
206
"""
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
193
216
end
194
217
195
218
"""
@@ -204,12 +227,16 @@ If the given function is not composed of simple core methods (e.g. sin, abs, ...
204
227
- `input`
205
228
- `output`
206
229
"""
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
212
238
end
239
+ StaticNonLinearity. f (func ; name) = StaticNonLinearity. f (; func = [func], name)
213
240
214
241
"""
215
242
Abs(;name)
@@ -321,14 +348,15 @@ Output the arc tangent of the input.
321
348
- `input2`
322
349
- `output`
323
350
"""
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
332
360
end
333
361
334
362
"""
0 commit comments