Home > tt2 > tests > test_full_KN2.m

test_full_KN2

PURPOSE ^

The test script for the Crank-Nicolson scheme with global time stepping

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

The test script for the Crank-Nicolson scheme with global time stepping

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %The test script for the Crank-Nicolson scheme with global time stepping
0002 times_glob = zeros(9,6);
0003 appr_glob = zeros(9,6);
0004 
0005 % for d0t=6:14
0006 % for d0x=6:11
0007 d0t = 12; % quantics dims for t
0008 d0x = 10; % quantics dims for x
0009 dpx = 2; % phys. dims for x
0010 
0011 a = 0;
0012 b = 1;
0013 h = (b-a)/(2^d0x+1);
0014 
0015 
0016 tol = 1e-6;
0017 eps = 1e-8;
0018 
0019 % tranges = [0, 0.01, 0.05, 0.1, 0.2, 0.5, 1, 2, 4];
0020 tranges = [0,0.5];
0021 
0022 
0023 Ax = tt_matrix(tt_qlaplace_dd(d0x*ones(1,dpx)));
0024 Ax = Ax/(h^2);
0025 
0026 Ix = tt_matrix(tt_eye(2, dpx*d0x));
0027 
0028 x = (a+h:h:b-h)';
0029 
0030 z = (0:h:(b-a-2*h))';
0031 z = exp(-z.^2/(4*tranges(end)));
0032 z = tt_reshape(tt_tensor(z), 2*ones(1,d0x), eps);
0033 
0034 C1l = tt_matrix(tt_qltrtoepl(core(z)));
0035 C1d = diag(diag(C1l));
0036 C1 = C1l+ C1l' - C1d;
0037 C1 = round(C1, eps)*h;
0038 % C1 = toeplitz(z)*h;
0039 % C1 = tt_matrix(full_to_nd(C1, eps));
0040 
0041 C = [];
0042 for i=1:dpx
0043     C = kron(C,C1);
0044 end;
0045 
0046 u00 = tt_reshape(tt_tensor(sin(pi*x)), 2*ones(1,d0x), eps);
0047 % u00 = tt_reshape(tt_tensor(exp(-abs(x)*sqrt(0.5)/0.5)), 2*ones(1,d0x), eps);
0048 % u00 = kron(u00,u00);
0049 % u_ex2 = kron(exp(-x.^2*0.5/(0.25+2*tranges(end))), exp(-x.^2*0.5/(0.25+2*tranges(end))))*((4*pi*tranges(end))^(-dpx/2));
0050 
0051 x = tt_tensor(x);
0052 x = tt_reshape(x, 2*ones(d0x, 1), eps);
0053 e1 = tt_tensor(tt_ones(2, d0x));
0054 % u0 = zeros(2^d0x,2^d0x);
0055 % u0(2:2^d0x-1, 2:2^d0x-1)=1;
0056 % u00 = tt_reshape(tt_tensor(u00), 2*ones(1,dpx*d0x), eps);
0057 % u_ex2 = tt_reshape(tt_tensor(u_ex2), 2*ones(1,dpx*d0x), eps);
0058 % u0 = tt_tensor(tt_ones(d0x*dpx,2));
0059 % u00 = kron((x-a*e1).*(b*e1-x), e1).*kron(e1, (x-a*e1).*(b*e1-x));
0060 
0061 % u00 = ((x-a*e1).*(b*e1-x)/(a*b)).*u00;
0062 % for i=2:gamma
0063 %     u00 = u00.*(x-a*e1).*(b*e1-x)/(a*b);
0064     u00 = round(u00, eps);
0065 % end;
0066 
0067 u00 = kron(u00,u00);
0068 
0069 % u_ex = round(C*u00, eps)*((4*pi*tranges(end))^(-dpx/2));
0070 u_ex = u00*exp(-2*pi^2*tranges(end));
0071 % u0 = round(u0, eps);
0072 % keyboard;
0073 
0074 spacial_rhs = tt_zeros(2,d0x*dpx);
0075 
0076 u0 = u00;
0077 % Now letz init the combined timestep procedure
0078 ttimes = zeros(1, max(size(tranges))-1);
0079 resids = zeros(1, max(size(tranges))-1);
0080 for out_t=1:max(size(tranges))-1
0081     tau = (tranges(out_t+1)-tranges(out_t))/(2^d0t);
0082     
0083     St = tt_shf(d0t); St = tt_matrix(St); St=St'; % lower shift matrix for gradient
0084     It = tt_matrix(tt_eye(2,d0t));
0085     Grad_t = (It-St)/tau; % for gradient
0086     Grad_t = round(Grad_t, eps);
0087     
0088     KN_term = (It+St)*0.5; % Krank-Nikolson term
0089     KN_term = round(KN_term, eps);
0090     
0091     e1t = cell(d0t,1);
0092     for i=1:d0t
0093         e1t{i}=[1;0];
0094     end;
0095     e1t = tt_tensor(e1t); % first identity vector for t - we need to put u0 into rhs
0096     eet = tt_tensor(tt_ones(2,d0t));
0097 %     e2t = round(e2t, 1e-12);
0098     
0099     
0100     % global matrix
0101     M = kron(Ix, Grad_t) + kron(Ax, KN_term);
0102     M = round(M, eps);
0103     
0104     % f1 = sin(pi*(1:1:2^d0x)'/(1+2^d0x));
0105     % u0 = full_to_qtt(f1, 1e-12);
0106     % u0 = tt_tensor(u0);
0107     % u0 = kron(u0,u0);
0108     
0109     % u0 = tt_tensor(tt_ones(d0x*dpx, 2));
0110     
0111     U = kron(u0, eet);
0112     
0113     tic;
0114     u0_rhs = u0/tau - (Ax*u0)*0.5; % stuff u0 to rhs of KN scheme
0115     u0_rhs = round(u0_rhs, eps);
0116     rhs = kron(u0_rhs, e1t) + kron(spacial_rhs, eet);
0117     rhs = round(rhs, eps);
0118     
0119     % norm_rhs = mvk(M',rhs,tol,20,tt_tensor(tt_random(2,rhs.d,2)),1000);
0120 %     [U,swps] = dmrg_solve2(M, rhs, U, tol, [], [], 100, []);
0121     [U,swps] = dmrg_solve2(M, rhs, tol, 'x0', U, 'nswp', 20);
0122     ttimes(out_t) = toc;
0123     
0124 %     Mx = mvk(M,U,tol,20,tt_tensor(tt_random(2,rhs.d,2)),1000);
0125     Mx = mvk3(M,U,tol);
0126     %     Mx = M*x;
0127     resids(out_t) = norm(Mx-rhs)/norm(rhs);
0128         
0129 
0130     % prepare new start
0131     ind = num2cell([1;2]*ones(1,U.d), 1);    
0132 %     last_indices = cell(1, d0t);
0133     for i=1:d0t
0134         ind{i+dpx*d0x}=2;
0135     end;
0136     u0 = U(ind);
0137     u0 = tt_reshape(u0, 2*ones(1,dpx*d0x));
0138 %     u0 = core(U);
0139 %     u0 = tt_elem3(u0, last_indices);
0140 %     u0 = tt_squeeze(u0);
0141 %     u0 = tt_tensor(u0);
0142     
0143 %     appr = norm(u0-u00*exp(-2*pi^2*tranges(out_t+1)))/norm(u00*exp(-2*pi^2*tranges(out_t+1)))
0144     appr = norm(u0-u_ex)/norm(u_ex)
0145     
0146     fprintf('Time range: [%g; %g], solve_time = %g, resid = %3.3e, swps: %d\n', tranges(out_t), tranges(out_t+1), ttimes(out_t), resids(out_t), swps);
0147 %     keyboard;
0148 end;
0149 
0150 times_glob(d0t-5, d0x-5)=sum(ttimes);
0151 appr_glob(d0t-5, d0x-5)=appr;
0152 
0153 % end;
0154 % end;

Generated on Wed 08-Feb-2012 18:20:24 by m2html © 2005