def test_default_timevector_long(self): """Test long time vector""" # TF with fast oscillations simulates only 5000 time steps # even with long tfinal wn = 100 tfsys = TransferFunction(1, [1, 0, wn**2]) tout = _default_time_vector(tfsys, tfinal=100) assert len(tout) == 5000
def test_auto_generated_time_vector(self): # confirm a TF with a pole at p simulates for ratio/p seconds p = 0.5 ratio = 9.21034*p # taken from code ratio2 = 25*p np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction(1, [1, .5]))[0], (ratio/p)) np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction(1, [1, .5]).sample(.1))[0], (ratio2/p)) # confirm a TF with poles at 0 and p simulates for ratio/p seconds np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction(1, [1, .5, 0]))[0], (ratio2/p)) # confirm a TF with a natural frequency of wn rad/s gets a # dt of 1/(ratio*wn) wn = 10 ratio_dt = 1/(0.025133 * ratio * wn) np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction(1, [1, 0, wn**2]))[1], 1/(ratio_dt*ratio*wn)) wn = 100 np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction(1, [1, 0, wn**2]))[1], 1/(ratio_dt*ratio*wn)) zeta = .1 np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction(1, [1, 2*zeta*wn, wn**2]))[1], 1/(ratio_dt*ratio*wn)) # but a smapled one keeps its dt np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction(1, [1, 2*zeta*wn, wn**2]).sample(.1))[1], .1) np.testing.assert_array_almost_equal( np.diff(initial_response(TransferFunction(1, [1, 2*zeta*wn, wn**2]).sample(.1))[0][0:2]), .1) np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction(1, [1, 2*zeta*wn, wn**2]))[1], 1/(ratio_dt*ratio*wn)) # TF with fast oscillations simulates only 5000 time steps even with long tfinal self.assertEqual(5000, len(_default_time_vector(TransferFunction(1, [1, 0, wn**2]),tfinal=100))) sys = TransferFunction(1, [1, .5, 0]) sysdt = TransferFunction(1, [1, .5, 0], .1) # test impose number of time steps self.assertEqual(10, len(step_response(sys, T_num=10)[0])) # test that discrete ignores T_num self.assertNotEqual(15, len(step_response(sysdt, T_num=15)[0])) # test impose final time np.testing.assert_array_almost_equal( 100, np.ceil(step_response(sys, 100)[0][-1])) np.testing.assert_array_almost_equal( 100, np.ceil(step_response(sysdt, 100)[0][-1])) np.testing.assert_array_almost_equal( 100, np.ceil(impulse_response(sys, 100)[0][-1])) np.testing.assert_array_almost_equal( 100, np.ceil(initial_response(sys, 100)[0][-1]))
def test_auto_generated_time_vector_tfinal(self, tfsys, tfinal): """Confirm a TF with a pole at p simulates for tfinal seconds""" ideal_tfinal, ideal_dt = _ideal_tfinal_and_dt(tfsys) np.testing.assert_allclose(ideal_tfinal, tfinal, rtol=1e-4) T = _default_time_vector(tfsys) np.testing.assert_allclose(T[-1], tfinal, atol=0.5*ideal_dt)
def test_auto_generated_time_vector(self): # confirm a TF with a pole at p simulates for 7.0/p seconds p = 0.5 np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction(1, [1, .5]))[0], (7 / p)) np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction(1, [1, .5]).sample(.1))[0], (7 / p)) # confirm a TF with poles at 0 and p simulates for 7.0/p seconds np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction(1, [1, .5, 0]))[0], (7 / p)) # confirm a TF with a natural frequency of wn rad/s gets a # dt of 1/(7.0*wn) wn = 10 np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction(1, [1, 0, wn**2]))[1], 1 / (7.0 * wn)) zeta = .1 np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction( 1, [1, 2 * zeta * wn, wn**2]))[1], 1 / (7.0 * wn)) # but a smapled one keeps its dt np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt( TransferFunction(1, [1, 2 * zeta * wn, wn**2]).sample(.1))[1], .1) np.testing.assert_array_almost_equal( np.diff( initial_response( TransferFunction( 1, [1, 2 * zeta * wn, wn**2]).sample(.1))[0][0:2]), .1) np.testing.assert_array_almost_equal( _ideal_tfinal_and_dt(TransferFunction( 1, [1, 2 * zeta * wn, wn**2]))[1], 1 / (7.0 * wn)) # TF with fast oscillations simulates only 5000 time steps even with long tfinal self.assertEqual( 5000, len( _default_time_vector(TransferFunction(1, [1, 0, wn**2]), tfinal=100))) # and simulates for 7.0/dt time steps self.assertEqual( len(_default_time_vector(TransferFunction(1, [1, 0, wn**2]))), int(7.0 / (1 / (7.0 * wn)))) sys = TransferFunction(1, [1, .5, 0]) sysdt = TransferFunction(1, [1, .5, 0], .1) # test impose number of time steps self.assertEqual(10, len(step_response(sys, T_num=10)[0])) self.assertEqual(10, len(step_response(sysdt, T_num=10)[0])) # test impose final time np.testing.assert_array_almost_equal(100, step_response(sys, 100)[0][-1], decimal=.5) np.testing.assert_array_almost_equal(100, step_response(sysdt, 100)[0][-1], decimal=.5) np.testing.assert_array_almost_equal(100, impulse_response(sys, 100)[0][-1], decimal=.5) np.testing.assert_array_almost_equal(100, initial_response(sys, 100)[0][-1], decimal=.5)