def test_impulse(self): """Test impulse response of discrete system""" rtol = 1e-10 atol = 1e-12 for num_states in [1, 10]: for num_inputs in [1, 3]: for num_outputs in [1, 2, 3, 5]: # Generate state-space system arrays A, B, C = util.drss(num_states, num_inputs, num_outputs) # Check that can give time_steps as argument outputs = util.impulse(A, B, C) num_time_steps = len(outputs) outputs_true = np.zeros( (num_time_steps, num_outputs, num_inputs)) for ti in range(num_time_steps): outputs_true[ti] = C.dot( np.linalg.matrix_power(A, ti).dot(B)) np.testing.assert_allclose( outputs, outputs_true, rtol=rtol, atol=atol) # Check can give num_time_steps as an argument outputs = util.impulse( A, B, C, num_time_steps=num_time_steps) np.testing.assert_allclose( outputs, outputs_true, rtol=rtol, atol=atol)
def test_impulse(self): """Test impulse response of discrete system""" for num_states in [1, 10]: for num_inputs in [1, 3]: for num_outputs in [1, 2, 3, 5]: A, B, C = util.drss(num_states, num_inputs, num_outputs) # Check that can give time_step outputs = util.impulse(A, B, C) num_time_steps = len(outputs) outputs_true = np.zeros( (num_time_steps, num_outputs, num_inputs)) for ti in range(num_time_steps): outputs_true[ti] = C * (A**ti) * B np.testing.assert_allclose(outputs, outputs_true, rtol=1e-7, atol=1e-7) # Check can give num_time_steps as an argument outputs = util.impulse(A, B, C, num_time_steps=num_time_steps) np.testing.assert_allclose(outputs, outputs_true, rtol=1e-7, atol=1e-7)
def test_impulse(self): """Test impulse response of discrete system""" rtol = 1e-10 atol = 1e-12 for num_states in [1, 10]: for num_inputs in [1, 3]: for num_outputs in [1, 2, 3, 5]: # Generate state-space system arrays A, B, C = util.drss(num_states, num_inputs, num_outputs) # Check that can give time_steps as argument outputs = util.impulse(A, B, C) num_time_steps = len(outputs) outputs_true = np.zeros( (num_time_steps, num_outputs, num_inputs)) for ti in range(num_time_steps): outputs_true[ti] = C.dot( np.linalg.matrix_power(A, ti).dot(B)) np.testing.assert_allclose(outputs, outputs_true, rtol=rtol, atol=atol) # Check can give num_time_steps as an argument outputs = util.impulse(A, B, C, num_time_steps=num_time_steps) np.testing.assert_allclose(outputs, outputs_true, rtol=rtol, atol=atol)
def test_assemble_Hankel(self): """ Tests Hankel arrays are symmetric given ``[CB CAB CA**P CA**(P+1)B ...]``.""" rtol = 1e-10 atol = 1e-12 for num_inputs in [1, 3]: for num_outputs in [1, 2, 4]: for sample_interval in [1]: num_time_steps = 50 num_states = 5 A, B, C = util.drss(num_states, num_inputs, num_outputs) time_steps = make_time_steps(num_time_steps, sample_interval) Markovs = util.impulse(A, B, C, time_steps[-1] + 1) Markovs = Markovs[time_steps] if sample_interval == 2: time_steps, Markovs = era.make_sampled_format( time_steps, Markovs) my_ERA = era.ERA(verbosity=0) my_ERA._set_Markovs(Markovs) my_ERA._assemble_Hankel() H = my_ERA.Hankel_array Hp = my_ERA.Hankel_array2 for row in range(my_ERA.mc): for col in range(my_ERA.mo): np.testing.assert_equal( H[row * num_outputs:(row + 1) * num_outputs, col * num_inputs:(col + 1) * num_inputs], H[col * num_outputs:(col + 1) * num_outputs, row * num_inputs:(row + 1) * num_inputs]) np.testing.assert_equal( Hp[row * num_outputs:(row + 1) * num_outputs, col * num_inputs:(col + 1) * num_inputs], Hp[col * num_outputs:(col + 1) * num_outputs, row * num_inputs:(row + 1) * num_inputs]) np.testing.assert_allclose( H[row * num_outputs:(row + 1) * num_outputs, col * num_inputs:(col + 1) * num_inputs], C.dot( np.linalg.matrix_power( A, time_steps[(row + col) * 2]).dot(B)), rtol=rtol, atol=atol) np.testing.assert_allclose( Hp[row * num_outputs:(row + 1) * num_outputs, col * num_inputs:(col + 1) * num_inputs], C.dot( np.linalg.matrix_power( A, time_steps[(row + col) * 2 + 1]).dot(B)), rtol=rtol, atol=atol)
def test_lsim(self): """Test that lsim has right shapes, does not test result""" for num_states in [1, 4, 9]: for num_inputs in [1, 2, 4]: for num_outputs in [1, 2, 3, 5]: A, B, C = util.drss(num_states, num_inputs, num_outputs) nt = 20 inputs = np.random.random((nt, num_inputs)) outputs = util.lsim(A, B, C, inputs) self.assertEqual(outputs.shape, (nt, num_outputs))
def test_drss(self): """Test drss gives correct array dimensions and stable dynamics.""" for num_states in [1, 5, 14]: for num_inputs in [1, 3, 6]: for num_outputs in [1, 2, 3, 7]: A, B, C = util.drss(num_states, num_inputs, num_outputs) self.assertEqual(A.shape, (num_states, num_states)) self.assertEqual(B.shape, (num_states, num_inputs)) self.assertEqual(C.shape, (num_outputs, num_states)) self.assertTrue(np.amax(np.abs(np.linalg.eig(A)[0])) < 1)
def test_drss(self): """Test drss gives correct mat dimensions and stable dynamics.""" for num_states in [1, 5, 14]: for num_inputs in [1, 3, 6]: for num_outputs in [1, 2, 3, 7]: A, B, C = util.drss(num_states, num_inputs, num_outputs) self.assertEqual(A.shape, (num_states,num_states)) self.assertEqual(B.shape, (num_states, num_inputs)) self.assertEqual(C.shape, (num_outputs, num_states)) self.assertTrue(np.amax(np.abs(np.linalg.eig(A)[0])) < 1)
def test_lsim(self): """Test that lsim has right shapes, does not test result""" for num_states in [1, 4, 9]: for num_inputs in [1, 2, 4]: for num_outputs in [1, 2, 3, 5]: A, B, C = util.drss(num_states, num_inputs, num_outputs) nt = 5 inputs = np.random.random((nt, num_inputs)) outputs = util.lsim(A, B, C, inputs) self.assertEqual(outputs.shape, (nt, num_outputs))
def test_balanced_truncation(self): """Test balanced system is close to original.""" for num_inputs in [1, 3]: for num_outputs in [1, 4]: for num_states in [1, 10]: A, B, C = util.drss(num_states, num_inputs, num_outputs) Ar, Br, Cr = util.balanced_truncation(A, B, C) num_time_steps = 10 y = util.impulse(A, B, C, num_time_steps=num_time_steps) yr = util.impulse(Ar, Br, Cr, num_time_steps=num_time_steps) np.testing.assert_allclose(yr, y, rtol=1e-5)
def test_assemble_Hankel(self): """ Tests Hankel arrays are symmetric given ``[CB CAB CA**P CA**(P+1)B ...]``.""" rtol = 1e-10 atol = 1e-12 for num_inputs in [1,3]: for num_outputs in [1, 2, 4]: for sample_interval in [1]: num_time_steps = 50 num_states = 5 A, B, C = util.drss(num_states, num_inputs, num_outputs) time_steps = make_time_steps( num_time_steps, sample_interval) Markovs = util.impulse(A, B, C, time_steps[-1] + 1) Markovs = Markovs[time_steps] if sample_interval == 2: time_steps, Markovs = era.make_sampled_format( time_steps, Markovs) my_ERA = era.ERA(verbosity=0) my_ERA._set_Markovs(Markovs) my_ERA._assemble_Hankel() H = my_ERA.Hankel_array Hp = my_ERA.Hankel_array2 for row in range(my_ERA.mc): for col in range(my_ERA.mo): np.testing.assert_equal( H[row * num_outputs:(row + 1) * num_outputs, col * num_inputs:(col + 1) * num_inputs], H[col * num_outputs:(col + 1) * num_outputs, row * num_inputs:(row + 1) * num_inputs]) np.testing.assert_equal( Hp[row * num_outputs:(row + 1) * num_outputs, col * num_inputs:(col + 1) * num_inputs], Hp[col * num_outputs:(col + 1) * num_outputs, row * num_inputs:(row + 1) * num_inputs]) np.testing.assert_allclose( H[row * num_outputs:(row + 1) * num_outputs, col * num_inputs:(col + 1) * num_inputs], C.dot( np.linalg.matrix_power( A, time_steps[(row + col) * 2]).dot( B)), rtol=rtol, atol=atol) np.testing.assert_allclose( Hp[row * num_outputs:(row + 1) * num_outputs, col * num_inputs:(col + 1) * num_inputs], C.dot( np.linalg.matrix_power( A, time_steps[(row + col) * 2 + 1]).dot( B)), rtol=rtol, atol=atol)
def test_lsim(self): """Test that lsim has right shapes, does not test result""" for num_states in [1, 4, 9]: for num_inputs in [1, 2, 4]: for num_outputs in [1, 2, 3, 5]: #print 'num_states %d, num_inputs %d, num_outputs %d'%(num_states, num_inputs, num_outputs) A, B, C = util.drss(num_states, num_inputs, num_outputs) #print 'Shape of C is',C.shape nt = 5 inputs = np.random.random((nt, num_inputs)) outputs = util.lsim(A, B, C, inputs) self.assertEqual(outputs.shape, (nt, num_outputs))
def test_balanced_truncation(self): """Test balanced system is close to original.""" for num_inputs in [1, 3]: for num_outputs in [1, 4]: for num_states in [1, 10]: A, B, C = util.drss(num_states, num_inputs, num_outputs) Ar, Br, Cr = util.balanced_truncation(A, B, C) num_time_steps = 10 y = util.impulse(A, B, C, num_time_steps=num_time_steps) yr = util.impulse(Ar, Br, Cr, num_time_steps=num_time_steps) np.testing.assert_allclose(yr, y, rtol=1e-3, atol=1e-3)
def test_impulse(self): """Test impulse response of discrete system""" for num_states in [1, 10]: for num_inputs in [1, 3]: for num_outputs in [1, 2, 3, 5]: A, B, C = util.drss(num_states, num_inputs, num_outputs) # Check that can give time_step outputs = util.impulse(A, B, C) num_time_steps = len(outputs) outputs_true = np.zeros((num_time_steps, num_outputs, num_inputs)) for ti in range(num_time_steps): outputs_true[ti] = C * (A**ti) * B np.testing.assert_allclose(outputs, outputs_true) # Check can give num_time_steps as an argument outputs = util.impulse(A, B, C, num_time_steps=num_time_steps) np.testing.assert_allclose(outputs, outputs_true)
def test_assemble_Hankel(self): """ Tests Hankel mats are symmetric given ``[CB CAB CA**P CA**(P+1)B ...]``.""" for num_inputs in [1, 3]: for num_outputs in [1, 2, 4]: for sample_interval in [1]: num_time_steps = 50 num_states = 5 A, B, C = util.drss(num_states, num_inputs, num_outputs) time_steps = make_time_steps(num_time_steps, sample_interval) Markovs = util.impulse(A, B, C, time_steps[-1] + 1) Markovs = Markovs[time_steps] if sample_interval == 2: time_steps, Markovs = era.make_sampled_format( time_steps, Markovs) myERA = era.ERA(verbosity=0) myERA._set_Markovs(Markovs) myERA._assemble_Hankel() H = myERA.Hankel_mat Hp = myERA.Hankel_mat2 for row in range(myERA.mc): for col in range(myERA.mo): np.testing.assert_allclose( H[row * num_outputs:(row + 1) * num_outputs, col * num_inputs:(col + 1) * num_inputs], H[col * num_outputs:(col + 1) * num_outputs, row * num_inputs:(row + 1) * num_inputs]) np.testing.assert_allclose( Hp[row * num_outputs:(row + 1) * num_outputs, col * num_inputs:(col + 1) * num_inputs], Hp[col * num_outputs:(col + 1) * num_outputs, row * num_inputs:(row + 1) * num_inputs]) np.testing.assert_allclose( H[row * num_outputs:(row + 1) * num_outputs, col * num_inputs:(col + 1) * num_inputs], C * (A**time_steps[(row + col) * 2]) * B) np.testing.assert_allclose( Hp[row * num_outputs:(row + 1) * num_outputs, col * num_inputs:(col + 1) * num_inputs], C * (A**time_steps[(row + col) * 2 + 1]) * B)
def test_compute_model(self): """ Test ROM Markov params similar to those given - generates data - assembles Hankel array - computes SVD - forms the ROM discrete arrays A, B, and C (D = 0) - Tests Markov parameters from ROM are approx. equal to full plant's """ num_time_steps = 40 num_states_plant = 12 num_states_model = num_states_plant // 3 for num_inputs in [1, 3]: for num_outputs in [1, 2]: for sample_interval in [1, 2, 4]: time_steps = make_time_steps(num_time_steps, sample_interval) A, B, C = util.drss(num_states_plant, num_inputs, num_outputs) my_ERA = era.ERA(verbosity=0) Markovs = util.impulse(A, B, C, time_steps[-1] + 1) Markovs = Markovs[time_steps] if sample_interval == 2: time_steps, Markovs =\ era.make_sampled_format(time_steps, Markovs) num_time_steps = time_steps.shape[0] A_path_computed = join(self.test_dir, 'A_computed.txt') B_path_computed = join(self.test_dir, 'B_computed.txt') C_path_computed = join(self.test_dir, 'C_computed.txt') A, B, C = my_ERA.compute_model(Markovs, num_states_model) my_ERA.put_model(A_path_computed, B_path_computed, C_path_computed) #sing_vals = my_ERA.sing_vals[:num_states_model] # Flatten vecs into 2D X and Y arrays: # [B AB A**PB A**(P+1)B ...] #direct_vecs_flat = direct_vecs.swapaxes(0,1).reshape( # (num_states_model,-1))) # Exact grammians from Lyapunov eqn solve #gram_cont = util.solve_Lyapunov(A, B*B.H) #gram_obs = util.solve_Lyapunov(A.H, C.H*C) #print(np.sort(np.linalg.eig(gram_cont)[0])[::-1]) #print(sing_vals) #np.testing.assert_allclose(gram_cont.diagonal(), # sing_vals, atol=.1, rtol=.1) #np.testing.assert_allclose(gram_obs.diagonal(), # sing_vals, atol=.1, rtol=.1) #np.testing.assert_allclose(np.sort(np.linalg.eig( # gram_cont)[0])[::-1], sing_vals, # atol=.1, rtol=.1) #np.testing.assert_allclose(np.sort(np.linalg.eig( # gram_obs)[0])[::-1], sing_vals, # atol=.1, rtol=.1) # Check that the diagonals are largest entry on each row #self.assertTrue((np.max(np.abs(gram_cont),axis=1) == # np.abs(gram_cont.diagonal())).all()) #self.assertTrue((np.max(np.abs(gram_obs),axis=1) == # np.abs(gram_obs.diagonal())).all()) # Check the ROM Markov params match the full plant's Markovs_model = np.zeros(Markovs.shape) for ti, tv in enumerate(time_steps): Markovs_model[ti] = C.dot( np.linalg.matrix_power(A, tv).dot(B)) #print( # 'Computing ROM Markov param at time step %d' % tv) """ import matplotlib.pyplot as PLT for input_num in range(num_inputs): PLT.figure() PLT.hold(True) for output_num in range(num_outputs): PLT.plot(time_steps[:50], # Markovs_model[:50, output_num,input_num], 'ko') PLT.plot(time_steps[:50],Markovs[:50, # output_num, input_num],'rx') PLT.plot(time_steps_dense[:50], # Markovs_dense[:50, output_num, input_num],'b--') PLT.title('input %d to outputs'%input_num) PLT.legend(['ROM','Plant','Dense plant']) PLT.show() """ np.testing.assert_allclose(Markovs_model.squeeze(), Markovs.squeeze(), rtol=0.5, atol=0.5) np.testing.assert_equal( util.load_array_text(A_path_computed), A) np.testing.assert_equal( util.load_array_text(B_path_computed), B) np.testing.assert_equal( util.load_array_text(C_path_computed), C)
def test_compute_model(self): """ Test ROM Markov params similar to those given - generates data - assembles Hankel array - computes SVD - forms the ROM discrete arrays A, B, and C (D = 0) - Tests Markov parameters from ROM are approx. equal to full plant's """ num_time_steps = 40 num_states_plant = 12 num_states_model = num_states_plant // 3 for num_inputs in [1, 3]: for num_outputs in [1, 2]: for sample_interval in [1, 2, 4]: time_steps = make_time_steps( num_time_steps, sample_interval) A, B, C = util.drss( num_states_plant, num_inputs, num_outputs) my_ERA = era.ERA(verbosity=0) Markovs = util.impulse(A, B, C, time_steps[-1] + 1) Markovs = Markovs[time_steps] if sample_interval == 2: time_steps, Markovs =\ era.make_sampled_format(time_steps, Markovs) num_time_steps = time_steps.shape[0] A_path_computed = join(self.test_dir, 'A_computed.txt') B_path_computed = join(self.test_dir, 'B_computed.txt') C_path_computed = join(self.test_dir, 'C_computed.txt') A, B, C = my_ERA.compute_model(Markovs, num_states_model) my_ERA.put_model( A_path_computed, B_path_computed, C_path_computed) #sing_vals = my_ERA.sing_vals[:num_states_model] # Flatten vecs into 2D X and Y arrays: # [B AB A**PB A**(P+1)B ...] #direct_vecs_flat = direct_vecs.swapaxes(0,1).reshape( # (num_states_model,-1))) # Exact grammians from Lyapunov eqn solve #gram_cont = util.solve_Lyapunov(A, B*B.H) #gram_obs = util.solve_Lyapunov(A.H, C.H*C) #print(np.sort(np.linalg.eig(gram_cont)[0])[::-1]) #print(sing_vals) #np.testing.assert_allclose(gram_cont.diagonal(), # sing_vals, atol=.1, rtol=.1) #np.testing.assert_allclose(gram_obs.diagonal(), # sing_vals, atol=.1, rtol=.1) #np.testing.assert_allclose(np.sort(np.linalg.eig( # gram_cont)[0])[::-1], sing_vals, # atol=.1, rtol=.1) #np.testing.assert_allclose(np.sort(np.linalg.eig( # gram_obs)[0])[::-1], sing_vals, # atol=.1, rtol=.1) # Check that the diagonals are largest entry on each row #self.assertTrue((np.max(np.abs(gram_cont),axis=1) == # np.abs(gram_cont.diagonal())).all()) #self.assertTrue((np.max(np.abs(gram_obs),axis=1) == # np.abs(gram_obs.diagonal())).all()) # Check the ROM Markov params match the full plant's Markovs_model = np.zeros(Markovs.shape) for ti, tv in enumerate(time_steps): Markovs_model[ti] = C.dot( np.linalg.matrix_power(A, tv).dot( B)) #print( # 'Computing ROM Markov param at time step %d' % tv) """ import matplotlib.pyplot as PLT for input_num in range(num_inputs): PLT.figure() PLT.hold(True) for output_num in range(num_outputs): PLT.plot(time_steps[:50], # Markovs_model[:50, output_num,input_num], 'ko') PLT.plot(time_steps[:50],Markovs[:50, # output_num, input_num],'rx') PLT.plot(time_steps_dense[:50], # Markovs_dense[:50, output_num, input_num],'b--') PLT.title('input %d to outputs'%input_num) PLT.legend(['ROM','Plant','Dense plant']) PLT.show() """ np.testing.assert_allclose( Markovs_model.squeeze(), Markovs.squeeze(), rtol=0.5, atol=0.5) np.testing.assert_equal( util.load_array_text(A_path_computed), A) np.testing.assert_equal( util.load_array_text(B_path_computed), B) np.testing.assert_equal( util.load_array_text(C_path_computed), C)