Skip to content

Commit 47c5a75

Browse files
committed
Add support for pure D systems in lsim function and corresponding tests
1 parent 3e71154 commit 47c5a75

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/ControlSystemsBase/src/timeresp.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ function lsim(sys::AbstractStateSpace, u::AbstractVecOrMat, t::AbstractVector;
196196
error("Time vector t must be uniformly spaced")
197197
end
198198

199+
# Handle pure D system (no states)
200+
if nx == 0
201+
x = Matrix{eltype(u)}(undef, 0, length(t)) # no states
202+
y = sys.D * u
203+
dsys = sys
204+
return SimResult(y, t, x, u, dsys)
205+
end
206+
199207
if iscontinuous(sys)
200208
if method === :zoh
201209
dsys = c2d(sys, dt, :zoh)
@@ -280,6 +288,18 @@ function lsim(sys::AbstractStateSpace, u::Function, t::AbstractVector;
280288

281289
dt = t[2] - t[1]
282290

291+
# Handle pure D system (no states)
292+
if nx == 0
293+
uout = Matrix{T}(undef, nu, length(t))
294+
for i = 1:length(t)
295+
uout[:, i] = u(T[], t[i])
296+
end
297+
x = Matrix{T}(undef, 0, length(t)) # no states
298+
y = sys.D * uout
299+
simsys = sys
300+
return SimResult(y, t, x, uout, simsys)
301+
end
302+
283303
if !iscontinuous(sys) || method (:zoh, :tustin, :foh, :fwdeuler)
284304
if iscontinuous(sys)
285305
simsys = c2d(sys, dt, method)

test/test_timeresp.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Test lsim for pure D system (no states) with matrix input
2+
sysDm = ss([], [], [], [2.0])
3+
tDm = 0:0.1:1
4+
uDm = fill(3.0, 1, length(tDm))
5+
resDm = lsim(sysDm, uDm, tDm)
6+
@test resDm.y[:] == fill(2.0 * 3.0, length(tDm))
17
import OrdinaryDiffEq
28
using OrdinaryDiffEq: solve
39
using ControlSystems
@@ -35,6 +41,14 @@ th = 1e-6
3541
# Error for nonuniformly spaced vector
3642
@test_throws ErrorException lsim(sys, [1 2 3 4], [0, 1, 1, 2])
3743

44+
45+
# Test lsim for pure D system (no states)
46+
sysD = ss([], [], [], [2.0])
47+
tD = 0:0.1:1
48+
uD(x, t) = [3.0]
49+
resD = lsim(sysD, uD, tD)
50+
@test resD.y[:] == fill(2.0 * 3.0, length(tD))
51+
3852
# Test for problem with broadcast dimensions
3953
@test lsim(sys, zeros(1, 5), 0:0.2:0.8)[1][:] == zeros(5)
4054

0 commit comments

Comments
 (0)