def compare(u, U_adv, trained_model, roe=roe): f = lambda x : U_adv*x fprime = lambda x : U_adv u_for_ke = np.copy(u[1:-1]) u_for_roe = np.copy(u) u_pred_next = np.zeros_like(u[1:-1]) u_next = np.zeros_like(u) for i in range(0, 200, 5) : for j in range(1, 249) : u_next[j] = solvers.timestep_roe(u_for_roe, j, 0.4, f, fprime) u_next[0] = u_next[-2] u_next[-1] = u_next[2] u_pred_next = trained_model.predict(u_for_ke.reshape(1, 248, 1)) plt.figure("Evolution comparaison") plt.clf() # plt.plot(roe.line_x, u_next, label="true next state it = %d" %(i+1)) plt.plot(roe.line_x[1:-1], u_pred_next.ravel(), label="Prediction", fillstyle='none', linestyle='-.', marker='o' ) plt.legend() plt.pause(0.1) u_for_ke = u_pred_next.ravel() u_for_roe = u_next
def true_solve(self, u): u_next = np.zeros_like(u) for j in range(1, len(self.line_x) - 1): u_next[j] = solvers.timestep_roe(u, j, self.CFL, self.f, self.fprime) u_next[0] = u_next[-2] u_next[-1] = u_next[2] return u_next
def action_with_burger(state): """ On utilise Burger comme prediction """ next_state = np.zeros_like(state) for j in range(1, len(state) - 1): next_state[j] = solvers.timestep_roe(state, j, r, f, fprime) next_state[0] = next_state[-2] next_state[-1] = next_state[1] return next_state
def action_with_burger(state, r, f, fprime): """ With one iniitial state, we predict next_state """ next_state = np.zeros_like(state) for j in range(1, len(state) - 1): next_state[j] = solvers.timestep_roe(state, j, r, f, fprime) next_state[0] = next_state[-3] next_state[-1] = next_state[2] return next_state
def create_dataset(conditions, base_path=base_path, str_case=str_case, overwrite=False): pathtocheckX = osp.join(base_path, str_case + "X.npy") pathtochecky = osp.join(base_path, str_case + "y.npy") dt = conditions['tf'] / conditions['Nt'] dx = float(conditions['L']) / (conditions['Nx'] - 1) if osp.exists(pathtocheckX) and osp.exists( pathtochecky) and overwrite == False: print("%s and %s exist" % (pathtocheckX, pathtochecky)) X = np.load(pathtocheckX) y = np.load(pathtochecky) else: if not osp.exists(base_path): os.mkdir(base_path) X, y = np.zeros((1, 4)), np.zeros((1)) add_block = lambda u, j: [ u[j - 1], u[j], u[j + 1], (u[j + 1] - u[j - 1]) / (2 * dx) ] # plt.figure("Evolution") for n in range(n_cases): amp = np.random.choice(np.linspace(0.4, 0.55, 10)) for a in range(n_phase): p = np.random.choice(np.linspace(-np.pi, np.pi, 200)) u = amp * np.sin(2 * np.pi * (line_x) / (conditions['L']) + p) u_next = amp * np.sin(2 * np.pi * (line_x) / (conditions['L']) + p) # plt.clf() for t in range(conditions['Nt']): if t != 0: u = u_next u_next = np.zeros_like(u) for j in range(1, len(line_x) - 1): u_next[j] = solvers.timestep_roe( u, j, dt / dx, conditions['f'], conditions['fprime']) u_next[0] = u_next[-2] u_next[-1] = u_next[2] # plt.plot(line_x[1:-1], u_next[1:-1]) # plt.pause(0.4) # if t % 20 == 0 : # plt.clf() for j in range(1, len(line_x) - 1): X = np.block([[X], add_block(u, j)]) y = np.block([[y], [u_next[j]]]) X = np.delete(X, 0, axis=0) y = np.delete(y, 0, axis=0) x = [] for j in range(len(X) // szline): x.append([X[j * szline:(j + 1) * szline]]) x = np.array(x) X = x.reshape(-1, szline, X.shape[1]) np.save(pathtocheckX, X) np.save(pathtochecky, y) return X, y