clc;

Globals2D;

N_faces = 3;
NODETOL = 1e-12;

N_vect = 1:8;
n = length(N_vect);

format_spec = '%0.15f';
for i = 1:n
    N = N_vect(i);
    N_p = ((N + 2) * (N + 1)) / 2;
    N_fp = N + 1;
    
    % zatim jsou r, s radkove vektory, proto ukladam transpozici
    [x,y] = OptimalNodes2D(N); [r,s] = xytors(x,y);
    filename = sprintf('r_2D_N=%d.txt',N);
    MatrixToTextFile(r',filename,format_spec,' ');
    filename = sprintf('s_2D_N=%d.txt',N);
    MatrixToTextFile(s',filename,format_spec,' ');
    
    V = VandermondeMatrix2D(N,r,s); invV = inv(V);
    filename = sprintf('V_2D_N=%d.txt',N);
    MatrixToTextFile(V,filename,format_spec,' ');
    filename = sprintf('invV_2D_N=%d.txt',N);
    MatrixToTextFile(invV,filename,format_spec,' ');
    
    M = invV' * invV; invM = V * V';
    filename = sprintf('M_2D_N=%d.txt',N);
    MatrixToTextFile(M,filename,format_spec,' ');
    filename = sprintf('invM_2D_N=%d.txt',N);
    MatrixToTextFile(invM,filename,format_spec,' ');
    
    [V_r,V_s] = GradVandermondeMatrices2D(N,r,s);
    filename = sprintf('V_r_2D_N=%d.txt',N);
    MatrixToTextFile(V_r,filename,format_spec,' ');
    filename = sprintf('V_s_2D_N=%d.txt',N);
    MatrixToTextFile(V_s,filename,format_spec,' ');
    
    [D_r,D_s] = DiffMatrices2D(N,r,s,V,invV);
    filename = sprintf('D_r_2D_N=%d.txt',N);
    MatrixToTextFile(D_r,filename,format_spec,' ');
    filename = sprintf('D_s_2D_N=%d.txt',N);
    MatrixToTextFile(D_s,filename,format_spec,' ');
    
    D_r_weak = (V * V_r') * M; D_s_weak = (V * V_s') * M;
    filename = sprintf('D_r_weak_2D_N=%d.txt',N);
    MatrixToTextFile(D_r_weak,filename,format_spec,' ');
    filename = sprintf('D_s_weak_2D_N=%d.txt',N);
    MatrixToTextFile(D_s_weak,filename,format_spec,' ');
    
    fmask1 = find(abs(s + 1) < NODETOL);
    fmask2 = find(abs(r + s) < NODETOL);
    fmask3 = find(abs(r + 1) < NODETOL);
    Fmask = [fmask1; fmask2; fmask3]';
    filename = sprintf('Fmask_2D_N=%d.txt',N);
    MatrixToTextFile(Fmask,filename,'%d',' ');

    LIFT = Lift2D();
    filename = sprintf('LIFT_2D_N=%d.txt',N);
    MatrixToTextFile(LIFT,filename,format_spec,' ');
end