|
8 | 8 |
|
9 | 9 | """ |
10 | 10 |
|
| 11 | +import re |
11 | 12 | import warnings |
12 | 13 |
|
13 | | -import pytest |
14 | | -import numpy as np |
15 | 14 | import matplotlib.pyplot as plt |
| 15 | +import numpy as np |
| 16 | +import pytest |
| 17 | + |
16 | 18 | import control as ct |
17 | 19 |
|
18 | 20 | pytestmark = pytest.mark.usefixtures("mplcleanup") |
@@ -66,9 +68,12 @@ def test_nyquist_basic(): |
66 | 68 | assert _Z(sys) == N_sys + _P(sys) |
67 | 69 |
|
68 | 70 | # With a larger indent_radius, we get a warning message + wrong answer |
69 | | - with pytest.warns(UserWarning, match="contour may miss closed loop pole"): |
| 71 | + with pytest.warns() as rec: |
70 | 72 | N_sys = ct.nyquist_response(sys, indent_radius=0.2) |
71 | 73 | assert _Z(sys) != N_sys + _P(sys) |
| 74 | + assert len(rec) == 2 |
| 75 | + assert re.search("contour may miss closed loop pole", str(rec[0].message)) |
| 76 | + assert re.search("encirclements does not match", str(rec[1].message)) |
72 | 77 |
|
73 | 78 | # Unstable system |
74 | 79 | sys = ct.tf([10], [1, 2, 2, 1]) |
@@ -104,11 +109,15 @@ def test_nyquist_basic(): |
104 | 109 | sys, np.linspace(1e-4, 1e2, 100), indent_radius=1e-2, |
105 | 110 | return_contour=True) |
106 | 111 | assert not all(contour_indented.real == 0) |
107 | | - with pytest.warns(UserWarning, match="encirclements does not match"): |
| 112 | + |
| 113 | + with pytest.warns() as record: |
108 | 114 | count, contour = ct.nyquist_response( |
109 | 115 | sys, np.linspace(1e-4, 1e2, 100), indent_radius=1e-2, |
110 | 116 | return_contour=True, indent_direction='none') |
111 | 117 | np.testing.assert_almost_equal(contour, 1j*np.linspace(1e-4, 1e2, 100)) |
| 118 | + assert len(record) == 2 |
| 119 | + assert re.search("encirclements .* non-integer", str(record[0].message)) |
| 120 | + assert re.search("encirclements does not match", str(record[1].message)) |
112 | 121 |
|
113 | 122 | # Nyquist plot with poles at the origin, omega unspecified |
114 | 123 | sys = ct.tf([1], [1, 3, 2]) * ct.tf([1], [1, 0]) |
@@ -264,14 +273,19 @@ def test_nyquist_indent_default(indentsys): |
264 | 273 | def test_nyquist_indent_dont(indentsys): |
265 | 274 | # first value of default omega vector was 0.1, replaced by 0. for contour |
266 | 275 | # indent_radius is larger than 0.1 -> no extra quater circle around origin |
267 | | - with pytest.warns(UserWarning, match="encirclements does not match"): |
| 276 | + with pytest.warns() as record: |
268 | 277 | count, contour = ct.nyquist_response( |
269 | 278 | indentsys, omega=[0, 0.2, 0.3, 0.4], indent_radius=.1007, |
270 | 279 | plot=False, return_contour=True) |
271 | 280 | np.testing.assert_allclose(contour[0], .1007+0.j) |
272 | 281 | # second value of omega_vector is larger than indent_radius: not indented |
273 | 282 | assert np.all(contour.real[2:] == 0.) |
274 | 283 |
|
| 284 | + # Make sure warnings are as expected |
| 285 | + assert len(record) == 2 |
| 286 | + assert re.search("encirclements .* non-integer", str(record[0].message)) |
| 287 | + assert re.search("encirclements does not match", str(record[1].message)) |
| 288 | + |
275 | 289 |
|
276 | 290 | def test_nyquist_indent_do(indentsys): |
277 | 291 | plt.figure(); |
@@ -352,9 +366,8 @@ def test_nyquist_exceptions(): |
352 | 366 | ct.nyquist_plot(sys, indent_direction='up') |
353 | 367 |
|
354 | 368 | # Discrete time system sampled above Nyquist frequency |
355 | | - sys = ct.drss(2, 1, 1) |
356 | | - sys.dt = 0.01 |
357 | | - with pytest.warns(UserWarning, match="above Nyquist"): |
| 369 | + sys = ct.ss([[-0.5, 0], [1, 0.5]], [[0], [1]], [[1, 0]], 0, 0.1) |
| 370 | + with pytest.warns(UserWarning, match="evaluation above Nyquist"): |
358 | 371 | ct.nyquist_plot(sys, np.logspace(-2, 3)) |
359 | 372 |
|
360 | 373 |
|
|
0 commit comments