-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtest_analysis_points.jl
400 lines (324 loc) · 12.9 KB
/
test_analysis_points.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
using Test, LinearAlgebra
using ModelingToolkit
using ModelingToolkitStandardLibrary.Blocks
using OrdinaryDiffEq
using ModelingToolkit: get_eqs, vars, @set!, t_nounits as t
using ControlSystemsBase
@named P = FirstOrder(k = 1, T = 1)
@named C = Gain(; k = -1)
@test_logs (:warn,) (:warn,) connect(P.input, :bad_connection, C.output)
# Test with explicitly created AnalysisPoint
ap = AnalysisPoint(:plant_input)
eqs = [connect(P.output, C.input)
connect(C.output, ap, P.input)]
sys = ODESystem(eqs, t, systems = [P, C], name = :hej)
ssys = structural_simplify(sys)
prob = ODEProblem(ssys, [P.x => 1], (0, 10))
sol = solve(prob, Rodas5())
@test norm(sol.u[1]) >= 1
@test norm(sol.u[end]) < 1e-6 # This fails without the feedback through C
# plot(sol)
matrices, _ = get_sensitivity(sys, ap)
@test matrices.A[] == -2
@test matrices.B[] * matrices.C[] == -1 # either one negative
@test matrices.D[] == 1
matrices, _ = get_comp_sensitivity(sys, ap)
@test matrices.A[] == -2
@test matrices.B[] * matrices.C[] == 1 # both positive or negative
@test matrices.D[] == 0
#=
# Equivalent code using ControlSystems. This can be used to verify the expected results tested for above.
using ControlSystemsBase
P = tf(1.0, [1, 1])
C = 1 # Negative feedback assumed in ControlSystems
S = sensitivity(P, C) # or feedback(1, P*C)
T = comp_sensitivity(P, C) # or feedback(P*C)
=#
# Test with automatically created analysis point
eqs = [connect(P.output, C.input)
connect(C.output, :plant_input, P.input)]
sys = ODESystem(eqs, t, systems = [P, C], name = :hej)
matrices, _ = get_sensitivity(sys, :plant_input)
@test matrices.A[] == -2
@test matrices.B[] * matrices.C[] == -1 # either one negative
@test matrices.D[] == 1
matrices, _ = get_comp_sensitivity(sys, :plant_input)
@test matrices.A[] == -2
@test matrices.B[] * matrices.C[] == 1 # both positive
@test matrices.D[] == 0
## get_looptransfer
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
#=
# Equivalent code using ControlSystems. This can be used to verify the expected results tested for above.
using ControlSystemsBase
P = tf(1.0, [1, 1])
C = -1
L = P*C
=#
# Open loop
open_sys = Blocks.open_loop(sys, :plant_input)
@unpack u, y = open_sys
# Linearizing the open-loop system should yield the same system as get_looptransfer
matrices, _ = linearize(open_sys, [u], [y])
@test matrices.A[] == -1
@test matrices.B[] * matrices.C[] == -1 # either one negative
@test matrices.D[] == 0
# Test with more than one AnalysisPoint
eqs = [connect(P.output, :plant_output, C.input)
connect(C.output, :plant_input, P.input)]
sys = ODESystem(eqs, t, systems = [P, C], name = :hej)
matrices, _ = get_sensitivity(sys, :plant_input)
@test matrices.A[] == -2
@test matrices.B[] * matrices.C[] == -1 # either one negative
@test matrices.D[] == 1
## Test linearize between analysis points
matrices, _ = linearize(sys, :plant_input, :plant_output)
# Result should be the same as feedpack(P, 1), i.e., the closed-loop transfer function from plant input to plant output
@test matrices.A[] == -2
@test matrices.B[] * matrices.C[] == 1 # both positive
@test matrices.D[] == 0
# Test with output given by symbolic variable instead of analysis point
matrices2, _ = linearize(sys, :plant_input, [P.output.u])
@test matrices2 == matrices
## Test with subsystems
@named P = FirstOrder(k = 1, T = 1)
@named C = Gain(; k = 1)
@named add = Blocks.Add(k2 = -1)
eqs = [connect(P.output, :plant_output, add.input2)
connect(add.output, C.input)
connect(C.output, :plant_input, P.input)]
# eqs = [connect(P.output, add.input2)
# connect(add.output, C.input)
# connect(C.output, P.input)]
sys_inner = ODESystem(eqs, t, systems = [P, C, add], name = :inner)
@named r = Constant(k = 1)
@named F = FirstOrder(k = 1, T = 3)
eqs = [connect(r.output, F.input)
connect(F.output, sys_inner.add.input1)]
sys_outer = ODESystem(eqs, t, systems = [F, sys_inner, r], name = :outer)
# test first that the structural_simplify works correctly
ssys = structural_simplify(sys_outer)
prob = ODEProblem(ssys, Pair[], (0, 10))
# sol = solve(prob, Rodas5())
# plot(sol)
matrices, _ = get_sensitivity(sys_outer, :inner_plant_input)
using ControlSystemsBase # This is required to simplify the results to test against known solution
lsys = sminreal(ss(matrices...))
@test lsys.A[] == -2
@test lsys.B[] * lsys.C[] == -1 # either one negative
@test lsys.D[] == 1
matrices_So, _ = get_sensitivity(sys_outer, :inner_plant_output)
lsyso = sminreal(ss(matrices_So...))
@test lsys == lsyso || lsys == -1 * lsyso * (-1) # Output and input sensitivities are equal for SISO systems
## A more complicated test case
using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra
using ModelingToolkitStandardLibrary.Mechanical.Rotational
using ModelingToolkitStandardLibrary.Blocks: Sine, PID, SecondOrder, Step, RealOutput
using ModelingToolkit: connect
# Parameters
m1 = 1
m2 = 1
k = 1000 # Spring stiffness
c = 10 # Damping coefficient
@named inertia1 = Inertia(; J = m1)
@named inertia2 = Inertia(; J = m2)
@named spring = Spring(; c = k)
@named damper = Damper(; d = c)
@named torque = Torque()
function SystemModel(u = nothing; name = :model)
eqs = [connect(torque.flange, inertia1.flange_a)
connect(inertia1.flange_b, spring.flange_a, damper.flange_a)
connect(inertia2.flange_a, spring.flange_b, damper.flange_b)]
if u !== nothing
push!(eqs, connect(torque.tau, u.output))
return ODESystem(eqs, t;
systems = [
torque,
inertia1,
inertia2,
spring,
damper,
u
],
name)
end
ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper], name)
end
@named r = Step(start_time = 0)
model = SystemModel()
@named pid = PID(k = 100, Ti = 0.5, Td = 1)
@named filt = SecondOrder(d = 0.9, w = 10)
@named sensor = AngleSensor()
@named er = Add(k2 = -1)
connections = [connect(r.output, :r, filt.input)
connect(filt.output, er.input1)
connect(pid.ctr_output, :u, model.torque.tau)
connect(model.inertia2.flange_b, sensor.flange)
connect(sensor.phi, :y, er.input2)
connect(er.output, :e, pid.err_input)]
closed_loop = ODESystem(connections, t, systems = [model, pid, filt, sensor, r, er],
name = :closed_loop, defaults = [
model.inertia1.phi => 0.0,
model.inertia2.phi => 0.0,
model.inertia1.w => 0.0,
model.inertia2.w => 0.0,
filt.x => 0.0,
filt.xd => 0.0
])
sys = structural_simplify(closed_loop)
prob = ODEProblem(sys, unknowns(sys) .=> 0.0, (0.0, 4.0))
sol = solve(prob, Rodas5P(), reltol = 1e-6, abstol = 1e-9)
# plot(
# plot(sol, vars = [filt.y, model.inertia1.phi, model.inertia2.phi]),
# plot(sol, vars = [pid.ctr_output.u], title = "Control signal"),
# legend = :bottomright,
# )
matrices, ssys = linearize(closed_loop, :r, :y)
lsys = ss(matrices...) |> sminreal
@test lsys.nx == 8
stepres = ControlSystemsBase.step(c2d(lsys, 0.001), 4)
@test Array(stepres.y[:])≈Array(sol(0:0.001:4, idxs = model.inertia2.phi)) rtol=1e-4
# plot(stepres, plotx=true, ploty=true, size=(800, 1200), leftmargin=5Plots.mm)
# plot!(sol, vars = [model.inertia2.phi], sp=1, l=:dash)
matrices, ssys = get_sensitivity(closed_loop, :y)
So = ss(matrices...)
matrices, ssys = get_sensitivity(closed_loop, :u)
Si = ss(matrices...)
@test tf(So) ≈ tf(Si)
## A simple multi-level system with loop openings
@named P_inner = FirstOrder(k = 1, T = 1)
@named feedback = Feedback()
@named ref = Step()
@named sys_inner = ODESystem(
[connect(P_inner.output, :y, feedback.input2)
connect(feedback.output, :u, P_inner.input)
connect(ref.output, :r, feedback.input1)],
t,
systems = [P_inner, feedback, ref])
Sinner = sminreal(ss(get_sensitivity(sys_inner, :u)[1]...))
@named sys_inner = ODESystem(
[connect(P_inner.output, :y, feedback.input2)
connect(feedback.output, :u, P_inner.input)],
t,
systems = [P_inner, feedback])
@named P_outer = FirstOrder(k = rand(), T = rand())
@named sys_outer = ODESystem(
[connect(sys_inner.P_inner.output, :y2, P_outer.input)
connect(P_outer.output, :u2, sys_inner.feedback.input1)],
t,
systems = [P_outer, sys_inner])
Souter = sminreal(ss(get_sensitivity(sys_outer, :sys_inner_u)[1]...))
Sinner2 = sminreal(ss(get_sensitivity(sys_outer, :sys_inner_u, loop_openings = [:y2])[1]...))
@test Sinner.nx == 1
@test Sinner == Sinner2
@test Souter.nx == 2
## Sensitivities in multivariate signals
import ControlSystemsBase as CS
import ModelingToolkitStandardLibrary.Blocks
A = [-0.994 -0.0794; -0.006242 -0.0134]
B = [-0.181 -0.389; 1.1 1.12]
C = [1.74 0.72; -0.33 0.33]
D = [0.0 0.0; 0.0 0.0]
@named P = Blocks.StateSpace(A, B, C, D)
Pss = CS.ss(A, B, C, D)
A = [-0.097;;]
B = [-0.138 -1.02]
C = [-0.076; 0.09;;]
D = [0.0 0.0; 0.0 0.0]
@named K = Blocks.StateSpace(A, B, C, D)
Kss = CS.ss(A, B, C, D)
eqs = [connect(P.output, :plant_output, K.input)
connect(K.output, :plant_input, P.input)]
sys = ODESystem(eqs, t, systems = [P, K], name = :hej)
matrices, _ = Blocks.get_sensitivity(sys, :plant_input)
S = CS.feedback(I(2), Kss * Pss, pos_feedback = true)
# bodeplot([ss(matrices...), S])
@test CS.tf(CS.ss(matrices...)) ≈ CS.tf(S)
matrices, _ = Blocks.get_comp_sensitivity(sys, :plant_input)
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; 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)
matrices, _ = linearize(sys, :plant_input, :plant_output)
G = CS.feedback(Pss, Kss, pos_feedback = true)
@test CS.tf(CS.ss(matrices...)) ≈ CS.tf(G)
## Multiple analysis points ====================================================
@named P = FirstOrder(k = 1, T = 1)
@named C = Gain(; k = 1)
@named add = Blocks.Add(k2 = -1)
eqs = [connect(P.output, :plant_output, add.input2)
connect(add.output, C.input)
connect(C.output, :plant_input, P.input)]
sys_inner = ODESystem(eqs, t, systems = [P, C, add], name = :inner)
@named r = Constant(k = 1)
@named F = FirstOrder(k = 1, T = 3)
eqs = [connect(r.output, F.input)
connect(F.output, sys_inner.add.input1)]
sys_outer = ODESystem(eqs, t, systems = [F, sys_inner, r], name = :outer)
matrices, _ = get_sensitivity(sys_outer, [:inner_plant_input, :inner_plant_output])
Ps = tf(1, [1, 1]) |> ss
Cs = tf(1) |> ss
G = CS.ss(matrices...) |> sminreal
Si = CS.feedback(1, Cs * Ps)
@test tf(G[1, 1]) ≈ tf(Si)
So = CS.feedback(1, Ps * Cs)
@test tf(G[2, 2]) ≈ tf(So)
@test tf(G[1, 2]) ≈ tf(-CS.feedback(Cs, Ps))
@test tf(G[2, 1]) ≈ tf(CS.feedback(Ps, Cs))
matrices, _ = get_comp_sensitivity(sys_outer, [:inner_plant_input, :inner_plant_output])
G = CS.ss(matrices...) |> sminreal
Ti = CS.feedback(Cs * Ps)
@test tf(G[1, 1]) ≈ tf(Ti)
To = CS.feedback(Ps * Cs)
@test tf(G[2, 2]) ≈ tf(To)
@test tf(G[1, 2]) ≈ tf(CS.feedback(Cs, Ps)) # The negative sign appears in a confusing place due to negative feedback not happening through Ps
@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; 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; 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];
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)
@test sminreal(L[1, 2]) ≈ ss(-1)
@test tf(L[2, 1]) ≈ tf(Ps)
matrices, _ = linearize(sys_outer, [:inner_plant_input], [:inner_plant_output])
G = CS.ss(matrices...) |> sminreal
@test tf(G) ≈ tf(CS.feedback(Ps, Cs))
## unit test
@mtkmodel SingleIntegrator begin
@parameters begin
to_mps = 1, [unit = u"m/s"]
end
@variables begin
(x(t) = 0), [unit = u"m", description = "Position"]
control(t), [unit = u"m/s", description = "Control Velocity"]
end
@components begin
uIn = RealInput()
c1 = Blocks.Constant(k = 1)
end
@equations begin
control ~ (uIn.u) * to_mps
D(x) ~ control
connect(c1.output, :test_point, uIn)
end
end
@mtkbuild single_integrator = SingleIntegrator()
prob = ODEProblem(single_integrator, [], (0, 10))
sol = solve(prob, Tsit5())
@test sol(10)[] ≈ 10