def test_als_eig(self): """test for ALS with solver='eig'""" # solve eigenvalue problem in TT format (number_ev=1 and operator_gevp is defined as identity tensor) _, eigentensor = evp.als(self.operator_tt, self.initial_tt, operator_gevp=self.operator_gevp, repeats=10) self.assertTrue(isinstance(eigentensor, TT)) # solve eigenvalue problem in TT format (number_ev=self.number_ev) eigenvalues_tt, eigenvectors_tt = evp.als(self.operator_tt, self.initial_tt, number_ev=self.number_ev, repeats=10) # solve eigenvalue problem in matrix format eigenvalues_mat, eigenvectors_mat = splin.eigs(self.operator_mat, k=self.number_ev) eigenvalues_mat = np.real(eigenvalues_mat) eigenvectors_mat = np.real(eigenvectors_mat) idx = eigenvalues_mat.argsort()[::-1] eigenvalues_mat = eigenvalues_mat[idx] eigenvectors_mat = eigenvectors_mat[:, idx] # compute relative error between exact and approximate eigenvalues rel_err_val = [] for i in range(self.number_ev): rel_err_val.append( np.abs(eigenvalues_mat[i] - eigenvalues_tt[i]) / eigenvalues_mat[i]) # compute relative error between exact and approximate eigenvectors rel_err_vec = [] for i in range(self.number_ev): norm_1 = np.linalg.norm(eigenvectors_mat[:, i] + eigenvectors_tt[i].matricize()) norm_2 = np.linalg.norm(eigenvectors_mat[:, i] - eigenvectors_tt[i].matricize()) rel_err_vec.append( np.amin([norm_1, norm_2]) / np.linalg.norm(eigenvectors_mat[:, i])) # check if relative errors are smaller than tolerance for i in range(self.number_ev): self.assertLess(rel_err_val[i], self.tol_eigval) self.assertLess(rel_err_vec[i], self.tol_eigvec)
def test_als_eigh(self): """test for ALS with solver='eigh'""" # make problem symmetric self.operator_tt = 0.5 * (self.operator_tt + self.operator_tt.transpose()) self.operator_mat = 0.5 * (self.operator_mat + self.operator_mat.transpose()) # solve eigenvalue problem in TT format (number_ev=self.number_ev) eigenvalues_tt, eigenvectors_tt = evp.als(self.operator_tt, self.initial_tt, number_ev=self.number_ev, repeats=10, solver='eigh') # solve eigenvalue problem in matrix format eigenvalues_mat, eigenvectors_mat = splin.eigs(self.operator_mat, k=self.number_ev) eigenvalues_mat = np.real(eigenvalues_mat) eigenvectors_mat = np.real(eigenvectors_mat) idx = eigenvalues_mat.argsort()[::-1] eigenvalues_mat = eigenvalues_mat[idx] eigenvectors_mat = eigenvectors_mat[:, idx] # compute relative error between exact and approximate eigenvalues rel_err_val = [] for i in range(self.number_ev): rel_err_val.append( np.abs(eigenvalues_mat[i] - eigenvalues_tt[i]) / eigenvalues_mat[i]) # compute relative error between exact and approximate eigenvectors rel_err_vec = [] for i in range(self.number_ev): norm_1 = np.linalg.norm(eigenvectors_mat[:, i] + eigenvectors_tt[i].matricize()) norm_2 = np.linalg.norm(eigenvectors_mat[:, i] - eigenvectors_tt[i].matricize()) rel_err_vec.append( np.amin([norm_1, norm_2]) / np.linalg.norm(eigenvectors_mat[:, i])) # check if relative errors are smaller than tolerance for i in range(self.number_ev): self.assertLess(rel_err_val[i], self.tol_eigval) self.assertLess(rel_err_vec[i], self.tol_eigvec)
print('----------------------------------------------------------') print('p_CO in atm Method TT ranks Closeness CPU time') print('----------------------------------------------------------') # construct eigenvalue problems to find the stationary distributions # ------------------------------------------------------------------ for i in range(8): # construct and solve eigenvalue problem for current CO pressure operator = mdl.co_oxidation( 20, 10**(8 + p_CO_exp[i])).ortho_left().ortho_right() initial_guess = tt.ones(operator.row_dims, [1] * operator.order, ranks=R[i]).ortho_left().ortho_right() with utl.timer() as time: eigenvalues, solution = evp.als(tt.eye(operator.row_dims) + operator, initial_guess, repeats=10, solver='eigs') solution = (1 / solution.norm(p=1)) * solution # compute turn-over frequency of CO2 desorption tof.append(utl.two_cell_tof(solution, [2, 1], 1.7e5)) # print results string_p_CO = '10^' + (p_CO_exp[i] >= 0) * '+' + str("%.1f" % p_CO_exp[i]) string_method = ' ' * 9 + 'EVP' + ' ' * 9 string_rank = (R[i] < 10) * ' ' + str(R[i]) + ' ' * 8 string_closeness = str("%.2e" % (operator @ solution).norm()) + ' ' * 6 string_time = (time.elapsed < 10) * ' ' + str("%.2f" % time.elapsed) + 's' print(string_p_CO + string_method + string_rank + string_closeness + string_time)
utl.progress('Load data', 100, dots=39) # construct TT operator # --------------------- utl.progress('Construct operator', 0, dots=30) operator = pf.perron_frobenius_2d(transitions, [n_states, n_states], simulations) utl.progress('Construct operator', 100, dots=30) # approximate leading eigenfunctions of the Perron-Frobenius and Koopman operator # ------------------------------------------------------------------------------- initial = tt.ones(operator.row_dims, [1] * operator.order, ranks=11) utl.progress('Approximate eigenfunctions in the TT format', 0, dots=5) eigenvalues_pf, eigenfunctions_pf = evp.als(operator, initial, number_ev, 3) utl.progress('Approximate eigenfunctions in the TT format', 50, dots=5) eigenvalues_km, eigenfunctions_km = evp.als(operator.transpose(), initial, number_ev, 2) utl.progress('Approximate eigenfunctions in the TT format', 100, dots=5) # compute exact eigenvectors # -------------------------- utl.progress('Compute exact eigenfunctions in matrix format', 0) eigenvalues_pf_exact, eigenfunctions_pf_exact = splin.eigs( operator.matricize(), k=number_ev) utl.progress('Compute exact eigenfunctions in matrix format', 100) # convert results to matrices # ---------------------------
transitions = io.loadmat("data/QuadrupleWell3D_25x25x25_100.mat")["indices"] # load data utl.progress('Load data', 100, dots=39) # construct TT operator # --------------------- utl.progress('Construct operator', 0, dots=30) operator = pf.perron_frobenius_3d(transitions, [n_states] * 3, simulations) utl.progress('Construct operator', 100, dots=30) # approximate leading eigenfunctions # ---------------------------------- utl.progress('Approximate eigenfunctions in the TT format', 0, dots=5) initial = tt.uniform(operator.row_dims, ranks=[1, 20, 10, 1]) eigenvalues, eigenfunctions = evp.als(operator, initial, number_ev, 2) utl.progress('Approximate eigenfunctions in the TT format', 100, dots=5) # compute exact eigenvectors # -------------------------- utl.progress('Compute exact eigenfunctions in matrix format', 0) eigenvalues_exact, eigenfunctions_exact = splin.eigs(operator.matricize(), k=number_ev) idx = eigenvalues_exact.argsort()[::-1] eigenvalues_exact = eigenvalues_exact[idx] eigenfunctions_exact = eigenfunctions_exact[:, idx] utl.progress('Compute exact eigenfunctions in matrix format', 100) # convert results to matrices # ----------------------------------
'/data/quadruple_well_transitions.npz')['transitions'] # construct TT operator # --------------------- start_time = utl.progress('Construct operator', 0) operator = ulam.ulam_3d(transitions, [n_states] * 3, simulations) utl.progress('Construct operator', 100, cpu_time=_time.time() - start_time) # approximate leading eigenfunctions # ---------------------------------- start_time = utl.progress('Approximate eigenfunctions in the TT format', 0) initial = tt.uniform(operator.row_dims, ranks=[1, 20, 10, 1]) eigenvalues, eigenfunctions = evp.als(operator, initial, number_ev=number_ev, repeats=2) utl.progress('Approximate eigenfunctions in the TT format', 100, cpu_time=_time.time() - start_time) # compute exact eigenvectors # -------------------------- start_time = utl.progress('Compute exact eigenfunctions in matrix format', 0) eigenvalues_exact, eigenfunctions_exact = splin.eigs(operator.matricize(), k=number_ev) idx = eigenvalues_exact.argsort()[::-1] eigenvalues_exact = eigenvalues_exact[idx] eigenfunctions_exact = eigenfunctions_exact[:, idx] utl.progress('Compute exact eigenfunctions in matrix format',