Skip to content

Commit

Permalink
reactivate the the fast forced_response(U=0) algorithm and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
bnavigator committed Mar 26, 2021
1 parent d87a0d1 commit 77c3b40
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
20 changes: 13 additions & 7 deletions control/tests/timeresp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def tsystem(self, request):
siso_ss1.ystep = np.array([9., 17.6457, 24.7072, 30.4855, 35.2234,
39.1165, 42.3227, 44.9694, 47.1599,
48.9776])
# X0 = [0.5, 1]
siso_ss1.yinitial = np.array([11., 8.1494, 5.9361, 4.2258, 2.9118,
1.9092, 1.1508, 0.5833, 0.1645, -0.1391])
ss1 = siso_ss1.sys
Expand Down Expand Up @@ -135,6 +136,7 @@ def tsystem(self, request):

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

siso_dss2 = copy(siso_dtf2)
siso_dss2.sys = tf2ss(siso_dtf2.sys)
Expand Down Expand Up @@ -634,19 +636,23 @@ def test_forced_response_legacy(self):
[pytest.param("siso_ss1",
{'X0': [0.5, 1], 'T': np.linspace(0, 1, 10)},
'yinitial',
id="ctime no T"),
id="ctime no U"),
pytest.param("siso_dss1",
{'T': np.arange(0, 5, 1,),
'X0': [0.5, 1]}, 'yinitial',
id="dt=True, no U"),
pytest.param("siso_dtf1",
{'U': np.ones(5,)}, 'ystep',
id="dt=True, no U"),
id="dt=True, no T"),
pytest.param("siso_dtf2",
{'U': np.ones(25,)}, 'ystep',
id="dt=0.2, no U"),
id="dt=0.2, no T"),
pytest.param("siso_ss2_dtnone",
{'U': np.ones(10,)}, 'ystep',
id="dt=None, no U"),
id="dt=None, no T"),
pytest.param("siso_dtf3",
{'U': np.ones(10,)}, 'ystep',
id="dt with rounding error"),
id="dt with rounding error, no T"),
],
indirect=["tsystem"])
def test_forced_response_T_U(self, tsystem, fr_kwargs, refattr):
Expand All @@ -661,13 +667,13 @@ def test_forced_response_invalid_c(self, tsystem):
with pytest.raises(TypeError,
match="StateSpace.*or.*TransferFunction"):
forced_response("not a system")

# ctime
with pytest.raises(ValueError, match="T.*is mandatory for continuous"):
forced_response(tsystem.sys)
with pytest.raises(ValueError, match="time values must be equally "
"spaced"):
forced_response(tsystem.sys, [0, 0.1, 0.12, 0.4])
with pytest.raises(ValueError, match="must start with 0"):
forced_response(tsystem.sys, [1, 1.1, 1.2, 1.3])

@pytest.mark.parametrize("tsystem", ["siso_dss2"], indirect=True)
def test_forced_response_invalid_d(self, tsystem):
Expand Down
3 changes: 2 additions & 1 deletion control/timeresp.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ def forced_response(sys, T=None, U=0., X0=0., transpose=False,
dot = np.dot # Faster and shorter code

# Faster algorithm if U is zero
if U is None or (isinstance(U, (int, float)) and U == 0):
# (was converted to arrqy above if not None)
if U is None or np.all(U == 0):
# Solve using matrix exponential
expAdt = sp.linalg.expm(A * dt)
for i in range(1, n_steps):
Expand Down

0 comments on commit 77c3b40

Please sign in to comment.