Skip to content

Commit dc53eb9

Browse files
committed
unit tests for is_static_gain system method and dt keyword in system __init__ methods
1 parent 2bd912d commit dc53eb9

File tree

3 files changed

+70
-42
lines changed

3 files changed

+70
-42
lines changed

control/tests/discrete_test.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def setUp(self):
4040
self.mimo_ss2d = StateSpace(A, B, C, D, 0.2)
4141

4242
# Single input, single output continuus and discrete transfer function
43-
self.siso_tf1 = TransferFunction([1, 1], [1, 2, 1], None)
43+
self.siso_tf1 = TransferFunction([1, 1], [1, 2, 1], None)
4444
self.siso_tf1c = TransferFunction([1, 1], [1, 2, 1], 0)
4545
self.siso_tf1d = TransferFunction([1, 1], [1, 2, 1], 0.1)
4646
self.siso_tf2d = TransferFunction([1, 1], [1, 2, 1], 0.2)
@@ -75,6 +75,18 @@ def testSystemInitialization(self):
7575
self.assertEqual(self.siso_tf2d.dt, 0.2)
7676
self.assertEqual(self.siso_tf3d.dt, True)
7777

78+
# keyword argument check
79+
# dynamic systems
80+
self.assertEqual(TransferFunction(1, [1, 1], dt=0.1).dt, 0.1)
81+
self.assertEqual(TransferFunction(1, [1, 1], 0.1).dt, 0.1)
82+
self.assertEqual(StateSpace(1,1,1,1, dt=0.1).dt, 0.1)
83+
self.assertEqual(StateSpace(1,1,1,1, 0.1).dt, 0.1)
84+
# static gain system, dt argument should still override default dt
85+
self.assertEqual(TransferFunction(1, [1,], dt=0.1).dt, 0.1)
86+
self.assertEqual(TransferFunction(1, [1,], 0.1).dt, 0.1)
87+
self.assertEqual(StateSpace(0,0,1,1, dt=0.1).dt, 0.1)
88+
self.assertEqual(StateSpace(0,0,1,1, 0.1).dt, 0.1)
89+
7890
def testCopyConstructor(self):
7991
for sys in (self.siso_ss1, self.siso_ss1c, self.siso_ss1d):
8092
newsys = StateSpace(sys)
@@ -325,11 +337,28 @@ def test_sample_system(self):
325337
sysd = sample_system(sysc, 1, method="matched")
326338
self.assertEqual(sysd.dt, 1)
327339

340+
# bilinear approximation with prewarping test
341+
wwarp = 50
342+
Ts = 0.025
343+
# test state space version
344+
plant = self.siso_ss1c
345+
plant_d_warped = plant.sample(Ts, 'bilinear', prewarp_frequency=wwarp)
346+
np.testing.assert_array_almost_equal(
347+
plant.evalfr(wwarp),
348+
plant_d_warped.evalfr(wwarp))
349+
# test transfer function version
350+
plant = self.siso_tf1c
351+
plant_d_warped = plant.sample(Ts, 'bilinear', prewarp_frequency=wwarp)
352+
np.testing.assert_array_almost_equal(
353+
plant.evalfr(wwarp),
354+
plant_d_warped.evalfr(wwarp))
355+
328356
# Check errors
329357
self.assertRaises(ValueError, sample_system, self.siso_ss1d, 1)
330358
self.assertRaises(ValueError, sample_system, self.siso_tf1d, 1)
331359
self.assertRaises(ValueError, sample_system, self.siso_ss1, 1, 'unknown')
332360

361+
333362
def test_sample_ss(self):
334363
# double integrators, two different ways
335364
sys1 = StateSpace([[0.,1.],[0.,0.]], [[0.],[1.]], [[1.,0.]], 0.)

control/tests/statesp_test.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,26 @@ def test_freq_resp(self):
245245
np.testing.assert_almost_equal(phase, true_phase)
246246
np.testing.assert_equal(omega, true_omega)
247247

248+
def test_is_static_gain(self):
249+
A0 = np.zeros((2,2))
250+
A1 = A0.copy()
251+
A1[0,1] = 1.1
252+
B0 = np.zeros((2,1))
253+
B1 = B0.copy()
254+
B1[0,0] = 1.3
255+
C0 = A0
256+
C1 = np.eye(2)
257+
D0 = 0
258+
D1 = np.ones((2,1))
259+
self.assertTrue(StateSpace(A0, B0, C1, D1).is_static_gain()) # True
260+
# fix this once remove_useless_states is false by default
261+
#print(StateSpace(A1, B0, C1, D1).is_static_gain()) # should be False when remove_useless is false
262+
self.assertFalse(StateSpace(A0, B1, C1, D1).is_static_gain()) # False
263+
self.assertFalse(StateSpace(A1, B1, C1, D1).is_static_gain()) # False
264+
self.assertTrue(StateSpace(A0, B0, C0, D0).is_static_gain()) # True
265+
self.assertTrue(StateSpace(A0, B0, C0, D1).is_static_gain()) # True
266+
self.assertTrue(StateSpace(A0, B0, C1, D0).is_static_gain()) # True
267+
248268
@unittest.skipIf(not slycot_check(), "slycot not installed")
249269
def test_minreal(self):
250270
"""Test a minreal model reduction."""
@@ -653,25 +673,5 @@ def test_copy_constructor(self):
653673
linsys.A[0, 0] = -3
654674
np.testing.assert_array_equal(cpysys.A, [[-1]]) # original value
655675

656-
def test_sample_system_prewarping(self):
657-
"""test that prewarping works when converting from cont to discrete time system"""
658-
A = np.array([
659-
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00, 0.00000000e+00],
660-
[-3.81097561e+01, -1.12500000e+00, 0.00000000e+00, 0.00000000e+00],
661-
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00],
662-
[ 0.00000000e+00, 0.00000000e+00, -1.66356135e+04, -1.34748470e+01]])
663-
B = np.array([
664-
[ 0. ], [ 38.1097561 ],[ 0. ],[16635.61352143]])
665-
C = np.array([[0.90909091, 0. , 0.09090909, 0. ],])
666-
wwarp = 50
667-
Ts = 0.025
668-
plant = StateSpace(A,B,C,0)
669-
plant_d_warped = plant.sample(Ts, 'bilinear', prewarp_frequency=wwarp)
670-
np.testing.assert_array_almost_equal(
671-
evalfr(plant, wwarp*1j),
672-
evalfr(plant_d_warped, np.exp(wwarp*1j*Ts)),
673-
decimal=4)
674-
675-
676676
if __name__ == "__main__":
677677
unittest.main()

control/tests/xferfcn_test.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,26 @@ def test_evalfr_siso(self):
409409
sys._evalfr(32.),
410410
np.array([[0.00281959302585077 - 0.030628473607392j]]))
411411

412+
def test_is_static_gain(self):
413+
numstatic = 1.1
414+
denstatic = 1.2
415+
numdynamic = [1,1]
416+
dendynamic = [2,1]
417+
numstaticmimo = [[[1.1,], [1.2,]], [[1.2,], [0.8,]]]
418+
denstaticmimo = [[[1.9,], [1.2,]], [[1.2,], [0.8,]]]
419+
numdynamicmimo = [[[1.1, 0.9], [1.2]], [[1.2], [0.8]]]
420+
dendynamicmimo = [[[1.1, 0.7], [0.2]], [[1.2], [0.8]]]
421+
self.assertTrue(TransferFunction(numstatic,denstatic).is_static_gain()) # True
422+
self.assertTrue(TransferFunction(numstaticmimo,denstaticmimo).is_static_gain()) # True
423+
424+
self.assertFalse(TransferFunction(numstatic,dendynamic).is_static_gain()) # False
425+
self.assertFalse(TransferFunction(numdynamic,dendynamic).is_static_gain()) # False
426+
self.assertFalse(TransferFunction(numdynamic,denstatic).is_static_gain()) # False
427+
self.assertFalse(TransferFunction(numstatic,dendynamic).is_static_gain()) # False
428+
429+
self.assertFalse(TransferFunction(numstaticmimo,dendynamicmimo).is_static_gain()) # False
430+
self.assertFalse(TransferFunction(numdynamicmimo,denstaticmimo).is_static_gain()) # False
431+
412432
# This test only works in Python 3 due to a conflict with the same
413433
# warning type in other test modules (frd_test.py). See
414434
# https://bugs.python.org/issue4180 for more details
@@ -912,26 +932,5 @@ def test_repr(self):
912932
H.den[p][m], H2.den[p][m])
913933
self.assertEqual(H.dt, H2.dt)
914934

915-
def test_sample_system_prewarping(self):
916-
"""test that prewarping works when converting from cont to discrete time system"""
917-
A = np.array([
918-
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00, 0.00000000e+00],
919-
[-3.81097561e+01, -1.12500000e+00, 0.00000000e+00, 0.00000000e+00],
920-
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00],
921-
[ 0.00000000e+00, 0.00000000e+00, -1.66356135e+04, -1.34748470e+01]])
922-
B = np.array([
923-
[ 0. ], [ 38.1097561 ],[ 0. ],[16635.61352143]])
924-
C = np.array([[0.90909091, 0. , 0.09090909, 0. ],])
925-
wwarp = 50
926-
Ts = 0.025
927-
plant = StateSpace(A,B,C,0)
928-
plant = ss2tf(plant)
929-
plant_d_warped = plant.sample(Ts, 'bilinear', prewarp_frequency=wwarp)
930-
np.testing.assert_array_almost_equal(
931-
evalfr(plant, wwarp*1j),
932-
evalfr(plant_d_warped, np.exp(wwarp*1j*Ts)),
933-
decimal=4)
934-
935-
936935
if __name__ == "__main__":
937936
unittest.main()

0 commit comments

Comments
 (0)