Skip to content

Commit 1f649de

Browse files
committed
reactivate the the fast forced_response(U=0) algorithm and test it
1 parent 36a4f6a commit 1f649de

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

control/tests/timeresp_test.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def tsystem(self, request):
4343
siso_ss1.ystep = np.array([9., 17.6457, 24.7072, 30.4855, 35.2234,
4444
39.1165, 42.3227, 44.9694, 47.1599,
4545
48.9776])
46+
# X0 = [0.5, 1]
4647
siso_ss1.yinitial = np.array([11., 8.1494, 5.9361, 4.2258, 2.9118,
4748
1.9092, 1.1508, 0.5833, 0.1645, -0.1391])
4849
ss1 = siso_ss1.sys
@@ -127,6 +128,7 @@ def tsystem(self, request):
127128

128129
siso_dss1 = copy(siso_dtf1)
129130
siso_dss1.sys = tf2ss(siso_dtf1.sys)
131+
siso_dss1.yinitial = np.array([-1., -0.5, 0.75, -0.625, 0.4375])
130132

131133
siso_dss2 = copy(siso_dtf2)
132134
siso_dss2.sys = tf2ss(siso_dtf2.sys)
@@ -642,19 +644,23 @@ def test_forced_response_legacy(self):
642644
[pytest.param("siso_ss1",
643645
{'X0': [0.5, 1], 'T': np.linspace(0, 1, 10)},
644646
'yinitial',
645-
id="ctime no T"),
647+
id="ctime no U"),
648+
pytest.param("siso_dss1",
649+
{'T': np.arange(0, 5, 1,),
650+
'X0': [0.5, 1]}, 'yinitial',
651+
id="dt=True, no U"),
646652
pytest.param("siso_dtf1",
647653
{'U': np.ones(5,)}, 'ystep',
648-
id="dt=True, no U"),
654+
id="dt=True, no T"),
649655
pytest.param("siso_dtf2",
650656
{'U': np.ones(25,)}, 'ystep',
651-
id="dt=0.2, no U"),
657+
id="dt=0.2, no T"),
652658
pytest.param("siso_ss2_dtnone",
653659
{'U': np.ones(10,)}, 'ystep',
654-
id="dt=None, no U"),
660+
id="dt=None, no T"),
655661
pytest.param("siso_dtf3",
656662
{'U': np.ones(10,)}, 'ystep',
657-
id="dt with rounding error"),
663+
id="dt with rounding error, no T"),
658664
],
659665
indirect=["tsystem"])
660666
def test_forced_response_T_U(self, tsystem, fr_kwargs, refattr):
@@ -669,13 +675,13 @@ def test_forced_response_invalid_c(self, tsystem):
669675
with pytest.raises(TypeError,
670676
match="StateSpace.*or.*TransferFunction"):
671677
forced_response("not a system")
672-
673-
# ctime
674678
with pytest.raises(ValueError, match="T.*is mandatory for continuous"):
675679
forced_response(tsystem.sys)
676680
with pytest.raises(ValueError, match="time values must be equally "
677681
"spaced"):
678682
forced_response(tsystem.sys, [0, 0.1, 0.12, 0.4])
683+
with pytest.raises(ValueError, match="must start with 0"):
684+
forced_response(tsystem.sys, [1, 1.1, 1.2, 1.3])
679685

680686
@pytest.mark.parametrize("tsystem", ["siso_dss2"], indirect=True)
681687
def test_forced_response_invalid_d(self, tsystem):

control/timeresp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ def forced_response(sys, T=None, U=0., X0=0., transpose=False,
379379
dot = np.dot # Faster and shorter code
380380

381381
# Faster algorithm if U is zero
382-
if U is None or (isinstance(U, (int, float)) and U == 0):
382+
# (if not None, it was converted to array above)
383+
if U is None or np.all(U == 0):
383384
# Solve using matrix exponential
384385
expAdt = sp.linalg.expm(A * dt)
385386
for i in range(1, n_steps):

0 commit comments

Comments
 (0)