|
| 1 | +# response_test.py - test response/plot design pattern |
| 2 | +# RMM, 13 Jan 2025 |
| 3 | +# |
| 4 | +# The standard pattern for control plots is to call a _response() or _map() |
| 5 | +# function and then use the plot() method. However, it is also allowed to |
| 6 | +# call the _plot() function directly, in which case the _response()/_map() |
| 7 | +# function is called internally. |
| 8 | +# |
| 9 | +# If there are arguments that are allowed in _plot() that need to be |
| 10 | +# processed by _response(), then we need to make sure that arguments are |
| 11 | +# properly passed from _plot() to _response(). The unit tests in this file |
| 12 | +# make sure that this functionality is implemented properly across all |
| 13 | +# *relevant* _response/_map/plot pairs. |
| 14 | +# |
| 15 | +# Response/map function Plotting function Comments |
| 16 | +# --------------------- ----------------- -------- |
| 17 | +# describing_function_response describing_function_plot no passthru args |
| 18 | +# forced_response time_response_plot no passthru args |
| 19 | +# frequency_response bode_plot included below |
| 20 | +# frequency_response nichols_plot included below |
| 21 | +# gangof4_response gangof4_plot included below |
| 22 | +# impulse_response time_response_plot no passthru args |
| 23 | +# initial_response time_response_plot no passthru args |
| 24 | +# input_output_response time_response_plot no passthru args |
| 25 | +# nyquist_response nyquist_plot included below |
| 26 | +# pole_zero_map pole_zero_plot no passthru args |
| 27 | +# root_locus_map root_locus_plot included below |
| 28 | +# singular_values_response singular_values_plot included below |
| 29 | +# step_response time_response_plot no passthru args |
| 30 | + |
| 31 | +import matplotlib.pyplot as plt |
| 32 | +import numpy as np |
| 33 | +import pytest |
| 34 | + |
| 35 | +import control as ct |
| 36 | + |
| 37 | + |
| 38 | +# List of parameters that should be processed by response function |
| 39 | +@pytest.mark.parametrize("respfcn, plotfcn, respargs", [ |
| 40 | + (ct.frequency_response, ct.bode_plot, |
| 41 | + {'omega_limits': [1e-2, 1e2], 'omega_num': 50, 'Hz': True}), |
| 42 | + (ct.frequency_response, ct.bode_plot, {'omega': np.logspace(2, 2)}), |
| 43 | + (ct.frequency_response, ct.nichols_plot, {'omega': np.logspace(2, 2)}), |
| 44 | + (ct.gangof4_response, ct.gangof4_plot, {'omega': np.logspace(2, 2)}), |
| 45 | + (ct.gangof4_response, ct.gangof4_plot, |
| 46 | + {'omega_limits': [1e-2, 1e2], 'omega_num': 50, 'Hz': True}), |
| 47 | + (ct.nyquist_response, ct.nyquist_plot, |
| 48 | + {'indent_direction': 'right', 'indent_radius': 0.1, 'indent_points': 100, |
| 49 | + 'omega_num': 50, 'warn_nyquist': False}), |
| 50 | + (ct.root_locus_map, ct.root_locus_plot, {'gains': np.linspace(1, 10, 5)}), |
| 51 | + (ct.singular_values_response, ct.singular_values_plot, |
| 52 | + {'omega_limits': [1e-2, 1e2], 'omega_num': 50, 'Hz': True}), |
| 53 | + (ct.singular_values_response, ct.singular_values_plot, |
| 54 | + {'omega': np.logspace(2, 2)}), |
| 55 | +]) |
| 56 | +@pytest.mark.usefixtures('mplcleanup') |
| 57 | +def test_response_plot(respfcn, plotfcn, respargs): |
| 58 | + if respfcn is ct.gangof4_response: |
| 59 | + # Two arguments required |
| 60 | + args = (ct.rss(4, 1, 1, strictly_proper=True), ct.rss(1, 1, 1)) |
| 61 | + else: |
| 62 | + # Single argument is enough |
| 63 | + args = (ct.rss(4, 1, 1, strictly_proper=True), ) |
| 64 | + |
| 65 | + # Standard calling pattern - generate response, then plot |
| 66 | + plt.figure() |
| 67 | + resp = respfcn(*args, **respargs) |
| 68 | + if plotfcn is ct.nichols_plot: |
| 69 | + cplt_resp = resp.plot(plot_type='nichols') |
| 70 | + else: |
| 71 | + cplt_resp = resp.plot() |
| 72 | + |
| 73 | + # Alternative calling pattern - call plotting function directly |
| 74 | + plt.figure() |
| 75 | + cplt_plot = plotfcn(*args, **respargs) |
| 76 | + |
| 77 | + # Make sure the plots have the same elements |
| 78 | + assert cplt_resp.lines.shape == cplt_plot.lines.shape |
| 79 | + assert cplt_resp.axes.shape == cplt_plot.axes.shape |
0 commit comments