def f(t, y): dx = 1 y_x = diff(y, dx, endpoints=True) y_xx = diff2(y, dx, endpoints=True) y2_x = diff(y**2, dx, endpoints=True) return np.array([y, y**2, y**3, y_x, y_xx, y_x * y, y_x * y_x, y_x * y_xx ]).reshape(-1, len(y)).T @ xi
def lib_at_c(I, c): dx = 1 dy = 1 I_x = diff(I, dx, axis=0, endpoints=True) I_y = diff(I, dy, axis=1, endpoints=True) I_xx = diff2(I, dx, axis=0, endpoints=True) I_xy = pdiff(I, [dx, dy], axis=[0, 1], endpoints=True) I_yy = diff2(I, dy, axis=1, endpoints=True) return (np.array([I, I_x, I_y, I_xx, I_xy, I_yy]).reshape(-1, M * N).T @ c)
def f(t, y): dx = 1 dy = 1 I = np.reshape(y, (M, N)) I_x = diff(I, dx, axis=0, endpoints=True) I_y = diff(I, dx, axis=1, endpoints=True) I_xx = diff2(I, dx, axis=0, endpoints=True) I_xy = pdiff(I, [dx, dx], axis=[0, 1], endpoints=True) I_yy = diff2(I, dx, axis=1, endpoints=True) return np.array([I, I_x, I_y, I_xx, I_xy, I_yy]).reshape(-1, M * N).T @ c
def diff_model(t_fine, P_, max_deg, num_refinements): # compute time derivative of data dt = t_fine[1] - t_fine[0] dP = diff(P_, dt, endpoints=True, axis=1) # make dictionary of polynomials in both population1 A = bin_exp(P_, max_deg) weights = np.sum(A, axis=0) # compute coefficeints c = np.linalg.lstsq(A, dP.T)[0] # c[:,0] = lasso(A,dP[0].T,alpha=100) # c[:,1] = lasso(A,dP[1].T,alpha=100) params = np.ones(np.shape(c)) # iteritively remove some functions from library for i in range(2): for j in range(num_refinements[i]): # find nonzero entries nz = np.nonzero(params[:, i])[0] # find index of lowest weight of these entires x = np.argmin(weights[nz] * c[nz, i]) # zero this index params[nz[x], i] = 0 # compute new coefficeints c[:, i] = np.linalg.lstsq(A * params[:, i], dP[i].T)[0] # solve solution from given coefficeints def rhs(t, xy): return bin_exp(xy, max_deg) @ c ivp = integrate.solve_ivp(rhs, [0, 60], P_[:, 0], dense_output=True) return { "lib": lambda xy: bin_exp(xy, max_deg) @ c, "model": ivp.sol, "num_params": np.sum(params, axis=0), "num_data": len(t_fine) }
from data_driven_modeling import diff, diff2, lasso from scipy import io burgers_mat = io.loadmat('burgers.mat') #%% x = burgers_mat['x'][0] t = burgers_mat['t'][:, 0] usol = burgers_mat['usol'] u = np.real(usol) [m, n] = np.shape(u) dt = t[1] - t[0] u_t = diff(u, dt, axis=1, endpoints=True) # %% ''' D = np.diag(np.ones(m-1),1)-np.diag(np.ones(m-1),-1) D[0,m-1] = -1 D[m-1,0] = 1 D /= 2*dx D2 = -2*np.diag(np.ones(m))+np.diag(np.ones(m-1),1)+np.diag(np.ones(m-1),-1) D2 /= (dx)**2 D2[m-1,0]=1; D2[0,m-1]=1; ux = D@X[:,1:-1]
plt.axis('image') plt.savefig('img/BZ/slice_' + str(t) + '.pdf') #%% TRY 1D SLICES u = I[np.arange(130), np.arange(130)] #u = BZ_tensor[np.arange(320,220,-1),np.arange(340,440),400:] [M, T] = np.shape(u) plt.figure() plt.scatter(np.arange(M), u[:, 200]) plt.show() dt = 1 u_t = diff(u, dt, axis=1, endpoints=True) u_tt = diff2(u, dt, axis=1, endpoints=True) dx = 1 u_x = diff(u, dx, endpoints=True) u_xx = diff2(u, dx, endpoints=True) u2_x = diff(u**2, dx, endpoints=True) A = np.array([u, u**2, u**3, u_x, u_xx, u_x * u, u_x * u_x, u_x * u_xx]).reshape(-1, M * T).T #%% regress xi = np.linalg.lstsq(A, u_t.reshape(-1))[0] #%%