import os import numpy as np import modred as mr # Create directory for output files out_dir = 'rom_ex1_out' if not os.path.isdir(out_dir): os.makedirs(out_dir) # Create random system and modes nx = 100 num_inputs = 2 num_outputs = 3 num_basis_vecs = 10 A = np.mat(np.random.random((nx, nx))) B = np.mat(np.random.random((nx, num_inputs))) C = np.mat(np.random.random((num_outputs, nx))) basis_vecs = np.mat(np.random.random((nx, num_basis_vecs))) LTI_proj = mr.LTIGalerkinProjectionMatrices(basis_vecs) A_reduced, B_reduced, C_reduced = LTI_proj.compute_model( A * basis_vecs, B, C * basis_vecs) LTI_proj.put_model( '%s/A_reduced.txt' % out_dir, '%s/B_reduced.txt' % out_dir, '%s/C_reduced.txt' % out_dir)
# Plot the first 3 modes if plots: for i in range(3): PLT.figure() PLT.hold(True) PLT.plot(x, direct_modes[:, i].real, '-o') PLT.plot(x, adjoint_modes[:, i].real, '-x') PLT.xlabel('Space') PLT.ylabel('Real(q)') PLT.legend(['direct', 'adjoint']) PLT.title('Direct and adjoint mode %d' % (i + 1)) # Project the linear dynamics onto the modes projection = MR.LTIGalerkinProjectionMatrices(direct_modes, adjoint_basis_vecs=adjoint_modes, inner_product_weights=weights, is_basis_orthonormal=True) A_direct_modes = A * direct_modes Ar, Br, Cr = projection.compute_model(A_direct_modes, B, C.dot(direct_modes)) # Verify that the model accurately reproduces the impulse response qr = N.mat(N.zeros((r, nt), dtype=complex)) qr[:, 0] = Br LHSr = N.identity(r) - dt / 2. * Ar RHSr = N.identity(r) + dt / 2. * Ar for ti in range(nt - 1): qr[:, ti + 1] = N.linalg.solve(LHSr, RHSr * qr[:, ti]) y = C * q yr = Cr * qr print 'Max error in reduced system impulse response output y is', N.amax(