3
3
margin_test.py - test suite for stability margin commands
4
4
5
5
RMM, 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
7
8
"""
8
9
from __future__import print_function
9
10
@@ -113,19 +114,24 @@ def test_margin_3input(tsys):
113
114
assert_allclose (out ,np .array (refout )[[0 ,1 ,3 ,4 ]],atol = 1.5e-3 )
114
115
115
116
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 ):
117
126
"""Test phase_crossover_frequencies() function"""
118
- sys = TransferFunction ([ 1 ], [ 1 , 2 , 3 , 4 ] )
127
+ sys = TransferFunction (* tfargs )
119
128
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 )
122
131
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 )
127
132
128
- # MIMO
133
+ def test_phase_crossover_frequencies_mimo ():
134
+ """Test MIMO exception"""
129
135
tf = TransferFunction ([[[1 ], [2 ]],
130
136
[[3 ], [4 ]]],
131
137
[[[1 ,2 ,3 ,4 ], [1 ,1 ]],
@@ -134,7 +140,6 @@ def test_phase_crossover_frequencies():
134
140
omega ,gain = phase_crossover_frequencies (tf )
135
141
136
142
137
-
138
143
def test_mag_phase_omega ():
139
144
"""Test for bug reported in gh-58"""
140
145
sys = TransferFunction (15 , [1 ,6 ,11 ,6 ])
@@ -327,3 +332,21 @@ def test_zmore_stability_margins(tsys_zmore):
327
332
tsys_zmore ['result' ],
328
333
atol = tsys_zmore ['atol' ],
329
334
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 )