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 (n_rows/n_columns)*widthCM]);
tiledlayout(n_rows,n_columns,'TileSpacing','compact','Padding','compact');


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(3).plotconst = 2;
% tiles(4).plotconst = 2;

tiles(1).plotparams.n_contours          = 8; %half
tiles(2).plotparams.n_contours          = 13; %half
% tiles(3).plotparams.n_contours          = 9; %half
% tiles(4).plotparams.n_contours          = 20; %half

maxes = zeros(n_rows*n_columns,1);


for I_file = 1:n_rows*n_columns
    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;
    t       = startfrac*2*pi/omega;
    %a-vector with all temporal mode information
    exp_a_grid = exp(-1i*linspace(-a_mx,a_mx,2*a_mx+1)*omega*t);
    exp_a_gridq ...
        = exp(-1i*linspace(-a_mx,a_mx,2*a_mx+1)*omega*(t + 0.5*pi/omega));
    if tiles(I_file).plotconst == 0
        n_r     = 100;
        n_theta = 100;
        
        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);
        for I_r = 1:n_r+1
            for I_theta = 1:n_theta+1
                pp(I_theta,I_r) = P_a((I_r-1)*(n_theta+1)+I_theta,:)...
                    *exp_a_grid'/mach ...
                    + 1i*real(P_a((I_r-1)*(n_theta+1)+I_theta,:)...
                    *exp_a_gridq')/mach;
            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);
        pp_pl02 = zeros(n_r+1,n_theta+1);
        pp_pl1  = zeros(n_s+1,n_theta+1);
        for I_r = 1:n_r+1
            for I_theta = 1:n_theta+1
                pp_pl01(I_r,I_theta)...
                    = real(P_a_pl01((I_r-1)*(n_theta+1)+I_theta,:)...
                    *exp_a_grid')/mach ...
                    + 1i*real(P_a_pl01((I_r-1)*(n_theta+1)+I_theta,:)...
                    *exp_a_gridq')/mach;
                pp_pl02(I_r,I_theta)...
                    = real(P_a_pl02((I_r-1)*(n_theta+1)+I_theta,:)...
                    *exp_a_grid')/mach ...
                    + 1i*real(P_a_pl02((I_r-1)*(n_theta+1)+I_theta,:)...
                    *exp_a_gridq')/mach;
            end
        end
        for I_s = 1:n_s+1
            for I_theta = 1:n_theta+1
                pp_pl1(I_s,I_theta)...
                    = real(P_a_pl1((I_s-1)*(n_theta+1)+I_theta,:)...
                    *exp_a_grid')/mach ...
                    + 1i*real(P_a_pl1((I_s-1)*(n_theta+1)+I_theta,:)...
                    *exp_a_gridq')/mach;
            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.r_0      = 1;
        plotparams.n_s  = n_s;
        plotparams.n_r  = n_r;

        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_s = 1:n_s+1
            for I_r = 1:2*n_r+1
                pp(I_r,I_s) = real(P_a((I_s-1)*(2*n_r+1)+I_r,:)...
                    *exp_a_grid')/mach ...
                    + 1i*real(P_a((I_s-1)*(2*n_r+1) + I_r,:)...
                    *exp_a_gridq')/mach;
            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_s = 1:n_s+1
            for I_x = 1:n_x+1
                pp(I_x,I_s) ...
                    = real(P_a((I_s-1)*(n_x+1)+I_x,:)...
                    *exp_a_grid')/mach ...
                    + 1i*real(P_a((I_s-1)*(n_x+1)+I_x,:)...
                    *exp_a_gridq')/mach;
            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

for I_tile = 1:n_rows*n_columns
    nexttile
    %% DRAW
    disp('plotting...')
    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.pp_pl01);
            plotquantity02  = real(pp.pp_pl02);
            plotquantity1   = real(pp.pp_pl1);
        else
            plotquantity01  = abs(pp.pp_pl01);
            plotquantity02  = abs(pp.pp_pl02);
            plotquantity1   = abs(pp.pp_pl1);
        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);
        else
            plotquantity = abs(pp);
        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';

exportgraphics(gcf,'stillplaceholderp2.pdf','ContentType','vector')
