Skip to content

Commit 11245c7

Browse files
committed
add some tests
1 parent 64ee27f commit 11245c7

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

test/test_discrete_blocks.jl

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ is known on closed form. For algebraic systems (without differential variables),
1414
an integrator with a constant input is often used together with the system under test.
1515
=#
1616

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+
1740
@testset "Integrator" begin
1841
clock = Clock(0.1)
1942
k = ShiftIndex(clock)
@@ -89,7 +112,7 @@ k = ShiftIndex()
89112
sampler = Sampler(; dt)
90113
zoh = ZeroOrderHold()
91114
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
93116
ref = Constant(k = 0.5)
94117
end
95118
@equations begin
@@ -114,7 +137,8 @@ import ControlSystemsBase as CS
114137
let (; c2d, tf, feedback, lsim) = CS
115138

116139
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)
118142

119143
# Test the output of the continuous partition
120144
G = feedback(P * C)
@@ -134,15 +158,16 @@ let (; c2d, tf, feedback, lsim) = CS
134158
# plot(timevec, [y sol(timevec, idxs = model.plant.output.u)[:]], m = :o, lab = ["CS" "MTK"])
135159
# display(current())
136160

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
138162

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[:]
139167
@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"])
146171
end
147172
end
148173
# ==============================================================================

0 commit comments

Comments
 (0)