@@ -14,6 +14,29 @@ is known on closed form. For algebraic systems (without differential variables),
14
14
an integrator with a constant input is often used together with the system under test.
15
15
=#
16
16
17
+ @testset " only discrete" begin
18
+ @info " Testing only discrete"
19
+ dt = 0.5
20
+ c = Clock (dt)
21
+ k = ShiftIndex (c)
22
+
23
+ @mtkmodel TestDiscreteOnly begin
24
+ @variables begin
25
+ x (t) = 1
26
+ end
27
+ @equations begin
28
+ x (k) ~ SampleTime ()* x (k- 1 )
29
+ end
30
+ end
31
+
32
+ @named model = TestDiscreteOnly ()
33
+ model = complete (model)
34
+ ssys = structural_simplify (IRSystem (model))
35
+ prob = ODEProblem (ssys, [model. x (k- 1 ) => 1.0 ], (0.0 , 10.0 ))
36
+ sol = solve (prob, Tsit5 ())
37
+ @test sol[model. x] == dt .^ (1 : 21 )
38
+ end
39
+
17
40
@testset " Integrator" begin
18
41
clock = Clock (0.1 )
19
42
k = ShiftIndex (clock)
@@ -89,7 +112,7 @@ k = ShiftIndex()
89
112
sampler = Sampler (; dt)
90
113
zoh = ZeroOrderHold ()
91
114
controller = DiscretePIDParallel (
92
- kp = 2 , ki = 2 , Imethod = :forward , with_D = false )
115
+ kp = 2 , ki = 2 , Imethod = :backward , with_D = false ) # NOTE: not sure why tests pass with backward euler here but fwdeuler in ControlSystemsBase
93
116
ref = Constant (k = 0.5 )
94
117
end
95
118
@equations begin
@@ -114,7 +137,8 @@ import ControlSystemsBase as CS
114
137
let (; c2d, tf, feedback, lsim) = CS
115
138
116
139
P = CS. c2d (CS. ss ([- 1 ], [1 ], [1 ], 0 ), dt)
117
- C = CS. c2d (CS. ss ([0 ], [1 ], [2 ], [2 ]), dt, :fwdeuler )
140
+ # C = CS.c2d(CS.ss([0], [1], [2], [2]), dt, :fwdeuler)
141
+ C = CS. ss ([1 ], [1 ], [2 * dt], [2 ], dt)
118
142
119
143
# Test the output of the continuous partition
120
144
G = feedback (P * C)
@@ -134,15 +158,16 @@ let (; c2d, tf, feedback, lsim) = CS
134
158
# plot(timevec, [y sol(timevec, idxs = model.plant.output.u)[:]], m = :o, lab = ["CS" "MTK"])
135
159
# display(current())
136
160
137
- @test_broken sol (timevec, idxs = model. plant. output. u)[:]≈ y rtol= 1e-5
161
+ @test sol (timevec, idxs = model. plant. output. u)[:]≈ y rtol= 1e-5
138
162
163
+ # Test the output of the discrete partition
164
+ G = feedback (C, P)
165
+ res = lsim (G, (x, t) -> [0.5 ], timevec)
166
+ y = res. y[:]
139
167
@test_skip begin
140
- # Test the output of the discrete partition
141
- G = feedback (C, P)
142
- res = lsim (G, (x, t) -> [0.5 ], timevec)
143
- y = res. y[:]
144
- @test_broken sol (timevec .+ 1e-10 , idxs = model. controller. output. u)≈ y rtol= 1e-8 # Broken due to discrete observed
145
- # plot([y sol(timevec .+ 1e-12, idxs=model.controller.output.u)], lab=["CS" "MTK"])
168
+ @test_broken sol (timevec .+ 1e-10 , idxs = model. plant. input. u)≈ y rtol= 1e-8 # Broken due to discrete observed
169
+ # plot([y sol(timevec .+ 1e-12, idxs=model.plant.input.u)], lab=["CS" "MTK"])
170
+ plot (timevec, [y sol (timevec .+ 1e-12 , idxs = model. controller. u)[:]], m = :o , lab = [" CS" " MTK" ])
146
171
end
147
172
end
148
173
# ==============================================================================
0 commit comments