def test_constructors(): """ Check that the constructor works """ dmdtools.DMD() dmdtools.DMD(10) dmdtools.DMD(10, True) dmdtools.DMD(10, True, True) # Check the arguments are copied properly args = {"n_rank": 131, "exact": True, "total": False} tmp = dmdtools.DMD(**args) assert tmp.n_rank == args["n_rank"] assert tmp.total == args["total"] assert tmp.exact == args["exact"]
def check_dmd_computation_simple_timeseries(exact, total): """ Check DMD computations on a problem where the true solution is known. All variants of DMD should give identical outputs. """ num_snapshots = 10 A = np.array([[0.9, 0.1], [0.0, 0.8]]) # Create a time series of data data = np.zeros((2, num_snapshots + 1)) data[:, 0] = np.random.randn(2) for ii in range(num_snapshots): data[:, ii + 1] = A.dot(data[:, ii]) X = data[:, :-1] Y = data[:, 1:] ADMD = Y.dot(np.linalg.pinv(X)) vals, vecs = np.linalg.eig(ADMD) inds = np.argsort(np.abs(vals))[::-1] vals = vals[inds] vecs = vecs[:, inds] # DMD class with rank 2 DMD = dmdtools.DMD(2, exact, total) DMD.fit(data) dmd_vals, dmd_modes = dmdtools.sort_modes_evals(DMD) for ii in range(len(vals)): assert np.abs(dmd_vals[ii] - vals[ii]) < 1e-10 check_eigenvectors(vecs[:, ii], dmd_modes[:, ii])
def standard_dmd(): X = np.zeros((n_states, n_snaps - 1)) Y = np.zeros((n_states, n_snaps - 1)) snaps = snapshots(n_states, n_snaps, noise_cov) x = snaps.next() for k, y in enumerate(snaps): X[:, k] = x Y[:, k] = y x = y DMD = dmdtools.DMD() DMD.fit(X, Y) return DMD.modes, DMD.evals
Q = np.linalg.qr(np.random.randn(n, 2))[0] data = Q.dot(data) data = np.r_[data.real, data.imag] # Split and stack real and image parts # Add noise to the data noisy_data = data + std * np.random.randn(data.shape[0], data.shape[1]) # Create a new figure for output fig = plt.figure(1) th = np.linspace(0, 2 * np.pi, 101) plt.plot(np.cos(th), np.sin(th), '-', color='0.75', lw=4) plt.plot(np.diag(Alow).real, np.diag(Alow).imag, 'ko', ms=14) # Note: n_rank is doubled because we only deal with real numbers dmd = dmdtools.DMD(n_rank * 2, False, False) # "standard" DMD dmd = dmd.fit(noisy_data) dmd_vals, dmd_modes = dmd.get_mode_pairs(sortby="LM") # Plot the DMD eigenvalues plt.plot(dmd_vals.real, dmd_vals.imag, 'rv', ms=14) # With TLS DMD tlsdmd = dmdtools.DMD(n_rank * 2, False, True) # "standard" DMD tlsdmd = tlsdmd.fit(noisy_data) tlsdmd_vals, tlsdmd_modes = tlsdmd.get_mode_pairs(sortby="LM") # Plot the DMD eigenvalues plt.plot(tlsdmd_vals.real, tlsdmd_vals.imag, 'b^', ms=14) plt.xlabel("$\Re(\mu)$") plt.ylabel("$\Im(\mu)$")