def test_root_locus_zoom(self): """Check the zooming functionality of the Root locus plot""" system = TransferFunction([1000], [1, 25, 100, 0]) root_locus(system) fig = plt.gcf() ax_rlocus = fig.axes[0] event = type('test', (object,), {'xdata': 14.7607954359, 'ydata': -35.6171379864, 'inaxes': ax_rlocus.axes})() ax_rlocus.set_xlim((-10.813628105112421, 14.760795435937652)) ax_rlocus.set_ylim((-35.61713798641108, 33.879716621220311)) plt.get_current_fig_manager().toolbar.mode = 'zoom rect' _RLClickDispatcher(event, system, fig, ax_rlocus, '-') zoom_x = ax_rlocus.lines[-2].get_data()[0][0:5] zoom_y = ax_rlocus.lines[-2].get_data()[1][0:5] zoom_y = [abs(y) for y in zoom_y] zoom_x_valid = [-5. ,- 4.61281263, - 4.16689986, - 4.04122642, - 3.90736502] zoom_y_valid = [0. ,0., 0., 0., 0.] assert_array_almost_equal(zoom_x,zoom_x_valid) assert_array_almost_equal(zoom_y,zoom_y_valid)
def test_bode_margin(self): num = [1000] den = [1, 25, 100, 0] sys = ctrl.tf(num, den) ctrl.bode_plot(sys, margins=True,dB=False,deg = True) fig = plt.gcf() allaxes = fig.get_axes() mag_to_infinity = (np.array([6.07828691, 6.07828691]), np.array([1.00000000e+00, 1.00000000e-08])) assert_array_almost_equal(mag_to_infinity, allaxes[0].lines[2].get_data()) gm_to_infinty = (np.array([10., 10.]), np.array([4.00000000e-01, 1.00000000e-08])) assert_array_almost_equal(gm_to_infinty, allaxes[0].lines[3].get_data()) one_to_gm = (np.array([10., 10.]), np.array([1., 0.4])) assert_array_almost_equal(one_to_gm, allaxes[0].lines[4].get_data()) pm_to_infinity = (np.array([6.07828691, 6.07828691]), np.array([100000., -157.46405841])) assert_array_almost_equal(pm_to_infinity, allaxes[1].lines[2].get_data()) pm_to_phase = (np.array([6.07828691, 6.07828691]), np.array([-157.46405841, -180.])) assert_array_almost_equal(pm_to_phase, allaxes[1].lines[3].get_data()) phase_to_infinity = (np.array([10., 10.]), np.array([1.00000000e-08, -1.80000000e+02])) assert_array_almost_equal(phase_to_infinity, allaxes[1].lines[4].get_data())
def test_sisotool(self): sisotool(self.system,Hz=False) fig = plt.gcf() ax_mag,ax_rlocus,ax_phase,ax_step = fig.axes[0],fig.axes[1],fig.axes[2],fig.axes[3] # Check the initial root locus plot points initial_point_0 = (np.array([-22.53155977]),np.array([0.])) initial_point_1 = (np.array([-1.23422011]), np.array([-6.54667031])) initial_point_2 = (np.array([-1.23422011]), np.array([06.54667031])) assert_array_almost_equal(ax_rlocus.lines[0].get_data(),initial_point_0) assert_array_almost_equal(ax_rlocus.lines[1].get_data(),initial_point_1) assert_array_almost_equal(ax_rlocus.lines[2].get_data(),initial_point_2) # Check the step response before moving the point step_response_original = np.array([ 0., 0.02233651, 0.13118374, 0.33078542, 0.5907113, 0.87041549, 1.13038536, 1.33851053, 1.47374666, 1.52757114]) assert_array_almost_equal(ax_step.lines[0].get_data()[1][:10],step_response_original) bode_plot_params = { 'omega': None, 'dB': False, 'Hz': False, 'deg': True, 'omega_limits': None, 'omega_num': None, 'sisotool': True, 'fig': fig, 'margins': True } # Move the rootlocus to another point event = type('test', (object,), {'xdata': 2.31206868287,'ydata':15.5983051046, 'inaxes':ax_rlocus.axes})() _RLClickDispatcher(event=event, sys=self.system, fig=fig,ax_rlocus=ax_rlocus,sisotool=True, plotstr='-' ,bode_plot_params=bode_plot_params, tvect=None) # Check the moved root locus plot points moved_point_0 = (np.array([-29.91742755]), np.array([0.])) moved_point_1 = (np.array([2.45871378]), np.array([-15.52647768])) moved_point_2 = (np.array([2.45871378]), np.array([15.52647768])) assert_array_almost_equal(ax_rlocus.lines[-3].get_data(),moved_point_0) assert_array_almost_equal(ax_rlocus.lines[-2].get_data(),moved_point_1) assert_array_almost_equal(ax_rlocus.lines[-1].get_data(),moved_point_2) # Check if the bode_mag line has moved bode_mag_moved = np.array([ 111.83321224, 92.29238035, 76.02822315, 62.46884113, 51.14108703, 41.6554004, 33.69409534, 27.00237344, 21.38086717, 16.67791585]) assert_array_almost_equal(ax_mag.lines[0].get_data()[1][10:20],bode_mag_moved) # Check if the step response has changed step_response_moved = np.array([[ 0., 0.02458187, 0.16529784 , 0.46602716 , 0.91012035 , 1.43364313, 1.93996334 , 2.3190105 , 2.47041552 , 2.32724853] ]) assert_array_almost_equal(ax_step.lines[0].get_data()[1][:10],step_response_moved)
def test_bode_margin(self): num = [1000] den = [1, 25, 100, 0] sys = ctrl.tf(num, den) ctrl.bode_plot(sys, margins=True, dB=False, deg=True) fig = plt.gcf() allaxes = fig.get_axes() mag_to_infinity = (np.array([6.07828691, 6.07828691]), np.array([1.00000000e+00, 1.00000000e-08])) assert_array_almost_equal(mag_to_infinity, allaxes[0].lines[2].get_data()) gm_to_infinty = (np.array([10., 10.]), np.array([4.00000000e-01, 1.00000000e-08])) assert_array_almost_equal(gm_to_infinty, allaxes[0].lines[3].get_data()) one_to_gm = (np.array([10., 10.]), np.array([1., 0.4])) assert_array_almost_equal(one_to_gm, allaxes[0].lines[4].get_data()) pm_to_infinity = (np.array([6.07828691, 6.07828691]), np.array([100000., -157.46405841])) assert_array_almost_equal(pm_to_infinity, allaxes[1].lines[2].get_data()) pm_to_phase = (np.array([6.07828691, 6.07828691]), np.array([-157.46405841, -180.])) assert_array_almost_equal(pm_to_phase, allaxes[1].lines[3].get_data()) phase_to_infinity = (np.array([10., 10.]), np.array([1.00000000e-08, -1.80000000e+02])) assert_array_almost_equal(phase_to_infinity, allaxes[1].lines[4].get_data())
def test_sisotool(self): sisotool(self.system, Hz=False) fig = plt.gcf() ax_mag, ax_rlocus, ax_phase, ax_step = fig.axes[0], fig.axes[ 1], fig.axes[2], fig.axes[3] # Check the initial root locus plot points initial_point_0 = (np.array([-22.53155977]), np.array([0.])) initial_point_1 = (np.array([-1.23422011]), np.array([-6.54667031])) initial_point_2 = (np.array([-1.23422011]), np.array([06.54667031])) assert_array_almost_equal(ax_rlocus.lines[0].get_data(), initial_point_0) assert_array_almost_equal(ax_rlocus.lines[1].get_data(), initial_point_1) assert_array_almost_equal(ax_rlocus.lines[2].get_data(), initial_point_2) # Check the step response before moving the point step_response_original = np.array([ 0., 0.02233651, 0.13118374, 0.33078542, 0.5907113, 0.87041549, 1.13038536, 1.33851053, 1.47374666, 1.52757114 ]) assert_array_almost_equal(ax_step.lines[0].get_data()[1][:10], step_response_original) bode_plot_params = { 'omega': None, 'dB': False, 'Hz': False, 'deg': True, 'omega_limits': None, 'omega_num': None, 'sisotool': True, 'fig': fig, 'margins': True } # Move the rootlocus to another point event = type( 'test', (object, ), { 'xdata': 2.31206868287, 'ydata': 15.5983051046, 'inaxes': ax_rlocus.axes })() _RLClickDispatcher(event=event, sys=self.system, fig=fig, ax_rlocus=ax_rlocus, sisotool=True, plotstr='-', bode_plot_params=bode_plot_params, tvect=None) # Check the moved root locus plot points moved_point_0 = (np.array([-29.91742755]), np.array([0.])) moved_point_1 = (np.array([2.45871378]), np.array([-15.52647768])) moved_point_2 = (np.array([2.45871378]), np.array([15.52647768])) assert_array_almost_equal(ax_rlocus.lines[-3].get_data(), moved_point_0) assert_array_almost_equal(ax_rlocus.lines[-2].get_data(), moved_point_1) assert_array_almost_equal(ax_rlocus.lines[-1].get_data(), moved_point_2) # Check if the bode_mag line has moved bode_mag_moved = np.array([ 111.83321224, 92.29238035, 76.02822315, 62.46884113, 51.14108703, 41.6554004, 33.69409534, 27.00237344, 21.38086717, 16.67791585 ]) assert_array_almost_equal(ax_mag.lines[0].get_data()[1][10:20], bode_mag_moved) # Check if the step response has changed step_response_moved = np.array([[ 0., 0.02458187, 0.16529784, 0.46602716, 0.91012035, 1.43364313, 1.93996334, 2.3190105, 2.47041552, 2.32724853 ]]) assert_array_almost_equal(ax_step.lines[0].get_data()[1][:10], step_response_moved)