def setUp(self): """Consider the Fermi-Pasta-Ulam problem and Kuramoto model for testing the routines in sle.py""" # set tolerance self.tol = 1e-5 # number of oscillators self.fpu_d = 3 self.kuramoto_d = 10 # parameters for the Fermi-Pasta_ulam problem self.fpu_m = 2000 self.fpu_psi = [lambda t: 1, lambda t: t, lambda t: t ** 2, lambda t: t ** 3] # parameters for the Kuramoto model self.kuramoto_x_0 = 2 * np.pi * np.random.rand(self.kuramoto_d) - np.pi self.kuramoto_w = np.linspace(-5, 5, self.kuramoto_d) self.kuramoto_t = 100 self.kuramoto_m = 1000 self.kuramoto_psi = [lambda t: np.sin(t), lambda t: np.cos(t)] # exact coefficient tensors self.fpu_xi_exact = mdl.fpu_coefficients(self.fpu_d) self.kuramoto_xi_exact = mdl.kuramoto_coefficients(self.kuramoto_d, self.kuramoto_w) # generate test data [self.fpu_x, self.fpu_y] = mdl.fermi_pasta_ulam(self.fpu_d, self.fpu_m) [self.kuramoto_x, self.kuramoto_y] = mdl.kuramoto(self.kuramoto_x_0, self.kuramoto_w, self.kuramoto_t, self.kuramoto_m)
def test_fpu_kuramoto(self): """tests for Fermi-Pasta-Ulam and Kuramoto model""" mdl.fpu_coefficients(self.order) mdl.kuramoto_coefficients(self.order, self.frequencies)
# define arrays for CPU times and relative errors rel_errors = np.zeros([ int((snapshots_max - snapshots_min) / snapshots_step) + 1, int(d_max - d_min) + 1 ]) # compare CPU times of tensor-based and matrix-based approaches for i in range(d_min, d_max + 1): print('Number of osciallators: ' + str(i)) print('-' * (24 + len(str(i))) + '\n') # construct exact solution in TT and matrix format start_time = utl.progress('Construct exact solution in TT format', 0) xi_exact = mdl.fpu_coefficients(i) utl.progress('Construct exact solution in TT format', 100, cpu_time=_time.time() - start_time) # generate data start_time = utl.progress('Generate test data', 0) [x, y] = fermi_pasta_ulam(i, snapshots_max) utl.progress('Generate test data', 100, cpu_time=_time.time() - start_time) start_time = utl.progress('Running MANDy', 0) for j in range(snapshots_min, snapshots_max + snapshots_step, snapshots_step): # storing indices ind_1 = rel_errors.shape[0] - 1 - int( (j - snapshots_min) / snapshots_step)
def setUp(self): """Consider the Fermi-Pasta-Ulam problem and Kuramoto model for testing the routines in sle.py""" # set tolerance self.tol = 1e-5 # number of oscillators self.fpu_d = 4 self.kuramoto_d = 10 # parameters for the Fermi-Pasta_ulam problem self.fpu_m = 2000 self.fpu_psi = [ lambda t: 1, lambda t: t, lambda t: t**2, lambda t: t**3 ] # parameters for the Kuramoto model self.kuramoto_x_0 = 2 * np.pi * np.random.rand(self.kuramoto_d) - np.pi self.kuramoto_w = np.linspace(-5, 5, self.kuramoto_d) self.kuramoto_t = 100 self.kuramoto_m = 1000 self.kuramoto_psi = [lambda t: np.sin(t), lambda t: np.cos(t)] self.kuramoto_basis = [[tdt.constant_function()] + [tdt.sin(i, 1) for i in range(self.kuramoto_d)], [tdt.constant_function()] + [tdt.cos(i, 1) for i in range(self.kuramoto_d)]] self.kuramoto_initial = tt.ones([11, 11], [1, 1], 11) # exact coefficient tensors self.fpu_xi_exact = mdl.fpu_coefficients(self.fpu_d) self.kuramoto_xi_exact = mdl.kuramoto_coefficients( self.kuramoto_d, self.kuramoto_w) # generate test data for FPU self.fpu_x = 0.2 * np.random.rand(self.fpu_d, self.fpu_m) - 0.1 self.fpu_y = np.zeros((self.fpu_d, self.fpu_m)) for j in range(self.fpu_m): self.fpu_y[0, j] = self.fpu_x[1, j] - 2 * self.fpu_x[0, j] + 0.7 * ( (self.fpu_x[1, j] - self.fpu_x[0, j])**3 - self.fpu_x[0, j]**3) for i in range(1, self.fpu_d - 1): self.fpu_y[i, j] = self.fpu_x[ i + 1, j] - 2 * self.fpu_x[i, j] + self.fpu_x[i - 1, j] + 0.7 * ( (self.fpu_x[i + 1, j] - self.fpu_x[i, j])**3 - (self.fpu_x[i, j] - self.fpu_x[i - 1, j])**3) self.fpu_y[-1, j] = -2 * self.fpu_x[-1, j] + self.fpu_x[ -2, j] + 0.7 * (-self.fpu_x[-1, j]**3 - (self.fpu_x[-1, j] - self.fpu_x[-2, j])**3) # generate test data for Kuramoto number_of_oscillators = len(self.kuramoto_x_0) def kuramoto_ode(_, theta): [theta_i, theta_j] = np.meshgrid(theta, theta) return self.kuramoto_w + 2 / number_of_oscillators * np.sin( theta_j - theta_i).sum(0) + 0.2 * np.sin(theta) sol = spint.solve_ivp(kuramoto_ode, [0, self.kuramoto_t], self.kuramoto_x_0, method='BDF', t_eval=np.linspace(0, self.kuramoto_t, self.kuramoto_m)) self.kuramoto_x = sol.y self.kuramoto_y = np.zeros([number_of_oscillators, self.kuramoto_m]) for i in range(self.kuramoto_m): self.kuramoto_y[:, i] = kuramoto_ode(0, self.kuramoto_x[:, i])
import scipy.linalg as splin import scikit_tt.data_driven.mandy as mandy import scikit_tt.models as mdl import scikit_tt.utils as utl import matplotlib.pyplot as plt utl.header(title='MANDy - Fermi-Pasta-Ulam problem', subtitle='Example 1') # model parameters number_of_oscillators = 10 psi = [lambda t: 1, lambda t: t, lambda t: t**2, lambda t: t**3] p = len(psi) # construct exact solution in TT and matrix format utl.progress('Construct exact solution in TT format', 0, dots=7) xi_exact = mdl.fpu_coefficients(number_of_oscillators) utl.progress('Construct exact solution in TT format', 100, dots=7) utl.progress('Construct exact solution in matrix format', 0) xi_exact_mat = xi_exact.full().reshape( [p**number_of_oscillators, number_of_oscillators]) utl.progress('Construct exact solution in matrix format', 100) # snapshot parameters snapshots_min = 1000 snapshots_max = 6000 snapshots_step = 500 # maximum number of snapshots for matrix approach snapshots_mat = 5000 # define arrays for CPU times and relative errors