0;


n_rows      = 1;
n_columns   = 2;


alphabet = ["a","b","c","d","e","f","g","h"];


set(0,'DefaultAxesFontName','Times')
set(0,'DefaultAxesFontSize',10)
widthCM=13.5; % width of plots in cm.  JFM width is 135mm.
fig = figure('units','centimeters','position',...
    [5 -5 widthCM 0.8*(n_rows/n_columns)*widthCM]);


files = [...
    "planarbend3D_alpha030_a001_omega4.8000_linear_s_d01.9635_ptot.mat",...
    "planarbend3D_alpha030_a001_omega4.8000_linear_s_d01.9635_ptot.mat"];

tiles(n_rows*n_columns) ...
    = struct("pp",[],"plotconst",[],"plotparams",[],"maxP",[]);
grids(n_rows*n_columns) = struct("xx",[],"yy",[],"zz",[],"P_a",[]);

tiles(1).plotconst = 2;
tiles(2).plotconst = 0;

tiles(1).plotparams.n_contours          = 8; %half
tiles(2).plotparams.n_contours          = 13; %half



load(files(1),'numparams','geomname','geomparams')
    
omega   = numparams.omega;
a_mx    = numparams.a_mx;

maxes = zeros(n_rows*n_columns,1);
%real part or absolute value: false is re, true is abs
absol        = false;
contours     = true;
n_contours   = 16; %half
%framerate, cycles per second, tildes indicate dimensionality
%(t_end_tilde*fps must be an integer)
fps         = 30;
cps         = 1;
omega_tilde = 2*pi*cps;
t_end_tilde = 4;
t_end       = omega_tilde*t_end_tilde/omega;
%minimum time necessary for plotting; minimum number of frames required
t_min_tilde = min(t_end_tilde,2*pi/omega_tilde);
n_frames    = t_min_tilde*fps;
%point through first cycle at which frames begin, in [-0.5,0.5)
startfrac   = 0;
t_grid      = 0 + startfrac*2*pi/omega:omega_tilde/(fps*omega)...
    :t_end -omega_tilde/(fps*omega) + startfrac*2*pi/omega;
%a-vector with all temporal mode information
exp_a_grid  = zeros(2*a_mx+1,length(t_grid));
exp_a_gridq = zeros(2*a_mx+1,length(t_grid));
for I_t = 1:length(t_grid)
    exp_a_grid(:,I_t) ...
        = exp(-1i*linspace(-a_mx,a_mx,2*a_mx+1)*omega*t_grid(I_t));
    exp_a_gridq(:,I_t) ...
        = exp(-1i*linspace(-a_mx,a_mx,2*a_mx+1)*omega*(t_grid(I_t) ...
        + 0.5*pi/omega));
end


for I_file = 1:n_rows*n_columns
    fprintf(1,'file %2.0f\r', I_file);
    tiles(I_file).plotparams.absol              = false;
    tiles(I_file).plotparams.contours           = true;
    tiles(I_file).plotparams.pressuredirection  = 0;
    tiles(I_file).plotparams.startfrac          = 0.25;
    absol               = tiles(I_file).plotparams.absol;
    contours            = tiles(I_file).plotparams.contours;
    pressuredirection   = tiles(I_file).plotparams.pressuredirection;
    startfrac           = tiles(I_file).plotparams.startfrac;
    load(files(I_file),'numparams','geomname','geomparams')
    geom = eval(strcat(geomname,'(','geomparams',')'));
    geom.inner = false;

    if pressuredirection == -1
        load(files(I_file),'Pmnint')
        Pchoice = Pmnint;
    elseif pressuredirection == 0
        load(files(I_file),'Pint')
        Pchoice = Pint;
    elseif pressuredirection == 1
        load(files(I_file),'Pplint')
        Pchoice = Pplint;
    else
        disp('bad pressure direction')
    end
    %point through first cycle at which frames begin, in [0.5,0.5)
    omega   = numparams.omega;
    mach    = numparams.mach;
    a_mx    = numparams.a_mx;
    if tiles(I_file).plotconst == 0
        n_r     = 100;
        n_theta = 101;

        plotparams.s_0      = (90/90)*geom.L;
        plotparams.n_r      = n_r;
        plotparams.n_theta  = n_theta;

        grids(I_file) = rthetaframe(geom,numparams,plotparams,Pchoice);
        P_a = grids(I_file).P_a;

        pp = zeros(n_theta+1,n_r+1,n_frames);
        for I_t = 1:n_frames
            fprintf(1,'frame %2.0f\r', I_t);
            for I_r = 1:n_r+1
                for I_theta = 1:n_theta+1
                    pp(I_theta,I_r,I_t) ...
                        = P_a((I_r-1)*(n_theta+1)+I_theta,:)...
                        *exp_a_grid(:,I_t)/mach ...
                        + 1i*real(P_a((I_r-1)*(n_theta+1)+I_theta,:)...
                        *exp_a_gridq(:,I_t))/mach;
                end
            end
        end
        tiles(I_file).maxP = max(abs(pp),[],'all');
    elseif tiles(I_file).plotconst == 1
        n_r     = 250;
        n_theta = 150;
        n_s     = 400;

        plotparams.s1       = 0;
        plotparams.s2       = numparams.s_d;
        plotparams.r_0      = 1;
        plotparams.n_r      = n_r;
        plotparams.n_s      = n_s;
        plotparams.n_theta  = n_theta;

        grids(I_file) = sthetaframe(geom,numparams,plotparams,Pchoice);
        P_a         = grids(I_file).P_a;
        P_a_pl01    = P_a.P_a_pl01;
        P_a_pl02    = P_a.P_a_pl02;
        P_a_pl1     = P_a.P_a_pl1;

        pp_pl01 = zeros(n_r+1,n_theta+1,n_frames);
        pp_pl02 = zeros(n_r+1,n_theta+1,n_frames);
        pp_pl1  = zeros(n_s+1,n_theta+1,n_frames);
        for I_t = 1:n_frames
            fprintf(1,'frame %2.0f\r', I_t);
            for I_r = 1:n_r+1
                for I_theta = 1:n_theta+1
                    pp_pl01(I_r,I_theta,I_t)...
                        = real(P_a_pl01((I_r-1)*(n_theta+1)+I_theta,:)...
                        *exp_a_grid(:,I_t))/mach ...
                        +1i*real(P_a_pl01((I_r-1)*(n_theta+1)+I_theta,:)...
                        *exp_a_gridq(:,I_t))/mach;
                    pp_pl02(I_r,I_theta,I_t)...
                        = real(P_a_pl02((I_r-1)*(n_theta+1)+I_theta,:)...
                        *exp_a_grid(:,I_t))/mach ...
                        +1i*real(P_a_pl02((I_r-1)*(n_theta+1)+I_theta,:)...
                        *exp_a_gridq(:,I_t))/mach;
                end
            end
            for I_s = 1:n_s+1
                for I_theta = 1:n_theta+1
                    pp_pl1(I_s,I_theta,I_t)...
                        = real(P_a_pl1((I_s-1)*(n_theta+1)+I_theta,:)...
                        *exp_a_grid(:,I_t))/mach ...
                        + 1i*real(P_a_pl1((I_s-1)*(n_theta+1)+I_theta,:)...
                        *exp_a_gridq(:,I_t))/mach;
                end
            end
        end
        pp = struct("pp_pl1",[],"pp_pl01",[],"pp_pl02",[]);
        pp.pp_pl1   = pp_pl1;
        pp.pp_pl01  = pp_pl01;
        pp.pp_pl02  = pp_pl02;
        tiles(I_file).maxP = max([max(abs(pp_pl01),[],'all'),...
            max(abs(pp_pl02),[],'all'),max(abs(pp_pl1),[],'all')]);
    elseif tiles(I_file).plotconst == 2
        n_s = 250;
        n_r = 100;

        plotparams.s1   = 0;
        plotparams.s2   = numparams.s_d;
        plotparams.n_s  = n_s;
        plotparams.n_r  = n_r;
        plotparams.r_0  = 1;

        grids(I_file) = rsframe(geom,numparams,plotparams,Pchoice);
        P_a = grids(I_file).P_a;

        pp = zeros(2*n_r+1,n_s+1);
        for I_t = 1:n_frames
            fprintf(1,'frame %2.0f\r', I_t);
            for I_s = 1:n_s+1
                for I_r = 1:2*n_r+1
                    pp(I_r,I_s,I_t) ...
                        = real(P_a((I_s-1)*(2*n_r+1)+I_r,:)...
                        *exp_a_grid(:,I_t))/mach ...
                        + 1i*real(P_a((I_s-1)*(2*n_r+1) + I_r,:)...
                        *exp_a_gridq(:,I_t))/mach;
                end
            end
        end
        tiles(I_file).maxP = max(abs(pp),[],'all');
    elseif tiles(I_file).plotconst == -1
        n_s = 200;
        n_x = 200;
        plotparams.s1 = 0;
        plotparams.s2 = numparams.s_d;
        plotparams.n_s = n_s;
        plotparams.n_x = n_x;
        grids(I_file) = xsframe(geom,numparams,plotparams,Pchoice);
        P_a = grids(I_file).P_a;

        pp          = zeros(n_x+1,n_s+1);
        for I_t = 1:n_frames
            fprintf(1,'frame %2.0f\r', I_t);
            for I_s = 1:n_s+1
                for I_x = 1:n_x+1
                    pp(I_x,I_s,I_t) ...
                        = real(P_a((I_s-1)*(n_x+1)+I_x,:)...
                        *exp_a_grid(:,I_t))/mach ...
                        + 1i*real(P_a((I_s-1)*(n_x+1)+I_x,:)...
                        *exp_a_gridq(:,I_t))/mach;
                end
            end
        end
        tiles(I_file).maxP = max(abs(pp),[],'all');
    end
    tiles(I_file).pp = pp;
    maxes(I_file) = tiles(I_file).maxP;
end



clim manual


outerclim = max(maxes);
if absol == false
    clim([-outerclim,outerclim]);
else
    clim([0,outerclim])
end






set_custom_colormap(1);

disp('plotting...')

for I_t = 1:length(t_grid)
    I_tmod ...
        = round(rem(t_grid(I_t),2*pi/omega)*fps*omega/(omega_tilde)+1);
    I_tmod = rem(I_tmod-1,n_frames)+1;

    combined = tiledlayout(n_rows,n_columns,'TileSpacing',...
        'compact','Padding','compact');
    title(combined,strcat('$$t=$$',...
        sprintf(' %5.3f',omega*t_grid(I_t)/omega_tilde)),...
        'Interpreter','latex', ...
        'FontName','Times')
    filename ...
        = strcat(pwd,'\DuctAcoustics\plotting\plots\tiledgifs.gif');



    for I_tile = 1:n_rows*n_columns
        nexttile
        %% DRAW
        axis tight
        clim manual


        plotparams = tiles(I_tile).plotparams;

        n_contours = plotparams.n_contours;
        if absol == false
            contourlevels = -outerclim:2*outerclim/(2*n_contours+1):outerclim;
            set_custom_colormap(1);
        else
            contourlevels = 0:outerclim/(2*n_contours+1):outerclim;
            colormap("turbo");
        end

        xx = grids(I_tile).xx;
        yy = grids(I_tile).yy;
        zz = grids(I_tile).zz;
        pp = tiles(I_tile).pp;

        plotconst = tiles(I_tile).plotconst;
        if plotconst == 1
            if absol == false
                plotquantity01  = real(pp_pl01(:,:,I_tmod));
                plotquantity02  = real(pp_pl02(:,:,I_tmod));
                plotquantity1   = real(reshape(pp_pl1(:,:,I_tmod),n_s+1,...
                    n_theta+1));
            else
                plotquantity01  = abs(pp_pl01(:,:,I_tmod));
                plotquantity02  = abs(pp_pl02(:,:,I_tmod));
                plotquantity1   = abs(reshape(pp_pl1(:,:,I_tmod),n_s+1,...
                    n_theta+1));
            end
            xx_pl1  = xx.xx_pl1;
            xx_pl01 = xx.xx_pl01;
            xx_pl02 = xx.xx_pl02;
            yy_pl1  = yy.yy_pl1;
            yy_pl01 = yy.yy_pl01;
            yy_pl02 = yy.yy_pl02;
            zz_pl1  = zz.zz_pl1;
            zz_pl01 = zz.zz_pl01;
            zz_pl02 = zz.zz_pl02;
            plot_pl01 = surf(xx_pl01,yy_pl01,zz_pl01,plotquantity01);
            set(plot_pl01, 'EdgeColor', 'none');
            hold on
            plot_pl02 = surf(xx_pl02,yy_pl02,zz_pl02,plotquantity02);
            set(plot_pl02, 'EdgeColor', 'none');
            plot_pl1 = surf(xx_pl1,yy_pl1,zz_pl1,plotquantity1);
            set(plot_pl1, 'EdgeColor', 'none');
            plot3(xx_pl01(length(xx_pl01(:,1)),:),...
                yy_pl01(length(yy_pl01(:,1)),:),...
                zz_pl01(length(zz_pl01(:,1)),:),'Color','k');
            plot3(xx_pl02(length(xx_pl02(:,1)),:),...
                yy_pl02(length(yy_pl02(:,1)),:),...
                zz_pl02(length(zz_pl02(:,1)),:),'Color','k');
            view(-37.5+180,25)
        else
            if absol == false
                plotquantity = real(pp(:,:,I_tmod));
                contourlevels ...
                    = -outerclim:2*outerclim/(2*n_contours+1):outerclim;
            else
                plotquantity = abs(pp(:,:,I_tmod));
                contourlevels = 0:upperclim/(2*n_contours+1):upperclim;
            end
            if contours == false
                plot = pcolor(xx,yy,plotquantity);
                set(plot, 'EdgeColor', 'none');
            else
                [~,c] = contourf(xx,yy,plotquantity);
                c.LevelList = contourlevels;
            end
            hold on
            if plotconst == 0
                plot(xx(:,length(xx(1,:))),yy(:,length(yy(1,:))),...
                    'LineWidth',2,'Color','k')
                view(90,90)
            elseif plotconst == 2
                plot(xx(1,:),yy(1,:),'LineWidth',2,'Color','k');
                plot(xx(length(xx(:,1)),:),yy(length(yy(:,1)),:),...
                    'LineWidth',2,'Color','k')
            elseif plotconst == -1
                plot(xx(1,:),yy(1,:),'LineWidth',2,'Color','k');
                plot(xx(n_x+1,:),yy(n_x+1,:),'LineWidth',2,'Color','k')
            end
        end
        clim([contourlevels(1),contourlevels(length(contourlevels))]);
        % daspect([10/57 16/29 1])
        daspect([1 1 1])
        % view(90,-90)
        subtitle(strcat("(",alphabet(I_tile),")"))
    end

    C = colorbar;
    C.Label.Interpreter = 'latex';
    % C.Label.String = pressurelabel;
    C.Label.FontSize = 12;
    C.Layout.Tile = 'east';

    drawnow
    [imind,cm] = rgb2ind(frame2im(getframe(fig)),256);
    if I_t == 1
        imwrite(imind,cm,filename,'gif','Loopcount',inf,'DelayTime',...
            1/fps);
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append',...
            'DelayTime',1/fps);
    end
end














