示例#1
0
    def setUp(self):
        """..."""

        self.tol = 1e-10
        self.d = 10
        self.m = 5
        self.n = 20
        self.data = np.random.rand(self.d, self.m)
        self.data_2 = np.random.rand(self.d, self.n)
        self.phi_1 = [[tdt.constant_function(), tdt.identity(i), tdt.monomial(i,2)] for i in range(self.d)]
        self.psi_1 = [lambda t: 1, lambda t: t, lambda t: t**2]
        self.phi_2 = [[tdt.constant_function()] + [tdt.sin(i,1) for i in range(self.d)], [tdt.constant_function()] + [tdt.cos(i,1) for i in range(self.d)] ]
        self.psi_2 = [lambda t: np.sin(t), lambda t: np.cos(t)]
示例#2
0
def plot_basis_functions(dimension, downsampling_rate, directory):
    """Plot basis functions

    Parameters
    ----------
    dimension: int
        number of contact indices
    downsampling_rate: int
        downsampling rate for trajectory data
    directory: string
        directory data to load
    """

    # load contact indices (sorted by relevance)
    contact_indices = np.load(
        directory + 'ntl9_contact_indices.npz')['indices'][:dimension]

    # load trajectories
    data, trajectory_lengths = load_data(directory, 1, contact_indices)

    # plot basis functions
    plt.figure(dpi=300)
    plt.hist(data[:10].flatten(), 10000, histtype='bar')
    x_values = np.arange(0, 1, 0.001)[:, None]
    y_values = tdt.gauss_function(0, 0.285, 0.001)(x_values.T)
    plt.plot(x_values, 170000 * y_values, linewidth=2)
    y_values = tdt.gauss_function(0, 0.62, 0.01)(x_values.T)
    plt.plot(x_values, 2500 * y_values, linewidth=2)
    plt.xlim([0, 1])
    plt.xlabel(r"$\Delta [\mathrm{nm}]$")
    plt.savefig(directory + 'ntl9_basis_functions_1.pdf')
    plt.show()
    plt.figure(dpi=300)
    x = np.arange(0, 1, 0.001)[:, None]
    plt.plot(x, tdt.constant_function(0)(x.T), linewidth=2)
    plt.plot(x, tdt.gauss_function(0, 0.285, 0.001)(x.T), linewidth=2)
    plt.plot(x, tdt.gauss_function(0, 0.62, 0.01)(x.T), linewidth=2)
    plt.xlim([0, 1])
    plt.ylim([0, 1.2])
    plt.xlabel(r"$\Delta [\mathrm{nm}]$")
    plt.savefig(directory + 'ntl9_basis_functions_2.pdf')
    plt.show()
示例#3
0
    def test_basis_functions(self):
        """test basis functions"""

        constant = tdt.constant_function()
        indicator = tdt.indicator_function(0,0,0.5)
        identity = tdt.identity(0)
        monomial = tdt.monomial(0,2)
        sin = tdt.sin(0,1)
        cos = tdt.cos(0,1)
        gauss = tdt.gauss_function(0,1,1)
        periodic_gauss = tdt.periodic_gauss_function(0,1,1)
        
        self.assertEqual(np.sum(np.abs(constant(self.data)-np.ones(self.m))), 0)
        self.assertEqual(np.sum(np.abs(indicator(self.data)-np.logical_and(self.data[0, :]>=0, self.data[0, :]<0.5))), 0)
        self.assertEqual(np.sum(np.abs(identity(self.data)-self.data[0, :])), 0)
        self.assertEqual(np.sum(np.abs(monomial(self.data)-self.data[0, :]**2)), 0)
        self.assertEqual(np.sum(np.abs(sin(self.data)-np.sin(self.data[0,:]))), 0)
        self.assertEqual(np.sum(np.abs(cos(self.data)-np.cos(self.data[0,:]))), 0)
        self.assertEqual(np.sum(np.abs(gauss(self.data)-np.exp(-0.5 * (self.data[0,:] - 1) ** 2))), 0)
        self.assertEqual(np.sum(np.abs(periodic_gauss(self.data)-np.exp(-0.5 * np.sin(0.5 * (self.data[0,:] - 1)) ** 2))), 0)
示例#4
0
                             ".npy")[::downsampling_rate, 2:12]
        trajectory_lengths.append(trajectory.shape[0])
        data.append(trajectory.T)
    data = np.hstack(data)

    return data, trajectory_lengths


# title
utl.header(title='Deca-alanine')

# define basis functions
basis_list = []
for i in range(5):
    basis_list.append([
        tdt.constant_function(),
        tdt.periodic_gauss_function(2 * i, -2, 0.8),
        tdt.periodic_gauss_function(2 * i, 1, 0.5)
    ])
    basis_list.append([
        tdt.constant_function(),
        tdt.periodic_gauss_function(2 * i + 1, -0.5, 0.8),
        tdt.periodic_gauss_function(2 * i + 1, 0, 4),
        tdt.periodic_gauss_function(2 * i + 1, 2, 0.8)
    ])

# parameters
downsampling_rate = 500
lag_times_int = np.array([1, 2, 4, 8, 10, 12])
lag_times_phy = 1e-3 * downsampling_rate * lag_times_int
lag_times_msm = 1e-3 * np.array([100, 200, 500, 1000, 2000, 4000, 6000])
示例#5
0
    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])
示例#6
0
def tedmd_hocur(dimensions, downsampling_rate, integer_lag_times, max_rank,
                directory):
    """tEDMD using AMUSEt with HOSVD

    Parameters
    ----------
    dimensions: list of ints
        numbers of contact indices
    downsampling_rate: int
        downsampling rate for trajectory data
    integer_lag_times: list of ints
        integer lag times for application of tEDMD
    max_rank: int
        maximum rank for HOCUR
    directory: string
        directory data to load
    """

    # progress
    start_time = utl.progress('Apply AMUSEt (HOCUR)', 0)

    for i in range(len(dimensions)):

        # parameters
        time_step = 2e-3
        lag_times = time_step * downsampling_rate * integer_lag_times

        # define basis list
        basis_list = [[
            tdt.constant_function(),
            tdt.gauss_function(i, 0.285, 0.001),
            tdt.gauss_function(i, 0.62, 0.01)
        ] for i in range(dimensions[i])]

        # progress
        utl.progress('Apply AMUSEt (HOCUR, p=' + str(dimensions[i]) + ')',
                     100 * i / len(dimensions),
                     cpu_time=_time.time() - start_time)

        # load contact indices (sorted by relevance)
        contact_indices = np.load(
            directory + 'ntl9_contact_indices.npz')['indices'][:dimensions[i]]

        # load data
        data, trajectory_lengths = load_data(directory,
                                             downsampling_rate,
                                             contact_indices,
                                             progress=False)

        # select snapshot indices for x and y data
        x_indices = []
        y_indices = []
        for j in range(len(integer_lag_times)):
            x_ind, y_ind = xy_indices(trajectory_lengths, integer_lag_times[j])
            x_indices.append(x_ind)
            y_indices.append(y_ind)

        # apply AMUSEt
        with utl.timer() as timer:
            eigenvalues, _ = tedmd.amuset_hocur(data,
                                                x_indices,
                                                y_indices,
                                                basis_list,
                                                max_rank=max_rank)
        cpu_time = timer.elapsed

        for j in range(len(integer_lag_times)):
            eigenvalues[j] = [eigenvalues[j][1]]

        # Save results to file:
        dic = {}
        dic["lag_times"] = lag_times
        dic["eigenvalues"] = eigenvalues
        dic["cpu_time"] = cpu_time
        np.savez_compressed(
            directory + "Results_NTL9_HOCUR_d" + str(dimensions[i]) + ".npz",
            **dic)

        utl.progress('Apply AMUSEt (HOCUR)',
                     100 * (i + 1) / len(dimensions),
                     cpu_time=_time.time() - start_time)