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


files = [...
    "exphorn2D_alpha050_a001_omega0.1865_linear_s_d10.0000_ptot.mat",...
    "exphorn2D_alpha050_a001_omega0.1865_linear_s_d10.0000_ptot.mat"];

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

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

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

maxes = zeros(n_rows*n_columns,1);

tiles(1).plotparams.startfrac   = 0.25;
tiles(2).plotparams.startfrac   = 0.5;


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;
    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

    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));
    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');
    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
    % if I_tile ==2
    %     nexttile(2,[2 1])
    % elseif I_tile == 3
    %     nexttile(3)
    % elseif I_tile == 1
    %     nexttile(1)
    % end
    %% 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 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
    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')
