33margin_test.py - test suite for stability margin commands
44
55RMM, 15 Jul 2011
6- BG, 30 June 2020 -- convert to pytest, gh-425
6+ BG, 30 Jun 2020 -- convert to pytest, gh-425
7+ BG, 16 Nov 2020 -- pick from gh-438 and add discrete test
78"""
89from __future__ import print_function
910
@@ -113,19 +114,24 @@ def test_margin_3input(tsys):
113114 assert_allclose (out , np .array (refout )[[0 , 1 , 3 , 4 ]], atol = 1.5e-3 )
114115
115116
116- def test_phase_crossover_frequencies ():
117+ @pytest .mark .parametrize (
118+ 'tfargs, omega_ref, gain_ref' ,
119+ [(([1 ], [1 , 2 , 3 , 4 ]), [1.7325 , 0. ], [- 0.5 , 0.25 ]),
120+ (([1 ], [1 , 1 ]), [0. ], [1. ]),
121+ (([2 ], [1 , 3 , 3 , 1 ]), [1.732 , 0. ], [- 0.25 , 2. ]),
122+ ((np .array ([3 , 11 , 3 ]) * 1e-4 , [1. , - 2.7145 , 2.4562 , - 0.7408 ], .1 ),
123+ [1.6235 , 0. ], [- 0.28598 , 1.88889 ]),
124+ ])
125+ def test_phase_crossover_frequencies (tfargs , omega_ref , gain_ref ):
117126 """Test phase_crossover_frequencies() function"""
118- sys = TransferFunction ([ 1 ], [ 1 , 2 , 3 , 4 ] )
127+ sys = TransferFunction (* tfargs )
119128 omega , gain = phase_crossover_frequencies (sys )
120- assert_allclose (omega , [ 1.73205 , 0. ] , atol = 1.5e-3 )
121- assert_allclose (gain , [ - 0.5 , 0.25 ] , atol = 1.5e-3 )
129+ assert_allclose (omega , omega_ref , atol = 1.5e-3 )
130+ assert_allclose (gain , gain_ref , atol = 1.5e-3 )
122131
123- tf = TransferFunction ([1 ], [1 , 1 ])
124- omega , gain = phase_crossover_frequencies (tf )
125- assert_allclose (omega , [0. ], atol = 1.5e-3 )
126- assert_allclose (gain , [1. ], atol = 1.5e-3 )
127132
128- # MIMO
133+ def test_phase_crossover_frequencies_mimo ():
134+ """Test MIMO exception"""
129135 tf = TransferFunction ([[[1 ], [2 ]],
130136 [[3 ], [4 ]]],
131137 [[[1 , 2 , 3 , 4 ], [1 , 1 ]],
@@ -134,7 +140,6 @@ def test_phase_crossover_frequencies():
134140 omega , gain = phase_crossover_frequencies (tf )
135141
136142
137-
138143def test_mag_phase_omega ():
139144 """Test for bug reported in gh-58"""
140145 sys = TransferFunction (15 , [1 , 6 , 11 , 6 ])
@@ -327,3 +332,21 @@ def test_zmore_stability_margins(tsys_zmore):
327332 tsys_zmore ['result' ],
328333 atol = tsys_zmore ['atol' ],
329334 rtol = tsys_zmore ['rtol' ])
335+
336+
337+ @pytest .mark .parametrize (
338+ 'cnum, cden, dt,'
339+ 'ref,'
340+ 'rtol' ,
341+ [([2 ], [1 , 3 , 2 , 0 ], 1e-2 , # gh-465
342+ (2.9558 , 32.8170 , 0.43584 , 1.4037 , 0.74953 , 0.97079 ),
343+ 0.1 # very crude tolerance, because the gradients are not great
344+ ),
345+ ([2 ], [1 , 3 , 3 , 1 ], .1 , # 2/(s+1)**3
346+ [3.4927 , 69.9996 , 0.5763 , 1.6283 , 0.7631 , 1.2019 ],
347+ 1e-3 )])
348+ def test_stability_margins_discrete (cnum , cden , dt , ref , rtol ):
349+ """Test stability_margins with discrete TF input"""
350+ tf = TransferFunction (cnum , cden ).sample (dt )
351+ out = stability_margins (tf )
352+ assert_allclose (out , ref , rtol = rtol )
0 commit comments