
% Clearing of the workspace and closing of all windows
clc; clear; close all;


% Definition of the current path folder
FileName = mfilename('fullpath');
[directory,~,~] = fileparts(FileName);
[dirmain,~,~] = fileparts(directory);

% Load of the loss calculations for the variant without loop in the SOC
% definition and the variant with loop in the SOC definition
% load([dirmain '\Data\SS_opt_SOC_dist1234_LoopL.mat'])
% load([dirmain '\Data\SS_opt_SOC_dist1234_LoopL_newy.mat'])
% load('LocalSave\SS_opt_SOC_dist1234_LoopL.mat')
% load('LocalSave\SS_opt_SOC_dist1234_LoopL_newy.mat')
% load('LocalSave\SS_opt_SOC_dist1234_LoopL_newW.mat')
%%
SS_SOC_wo = SS_SOC;
H_wo = H;
% ind_wo = ind;
% load([dirmain '\Data\SS_opt_SOC_dist1234_Loop.mat'])
load('LocalSave\SS_opt_SOC_dist1234_Loop.mat')
SS_SOC_w = SS_SOC;
H_w = H;
% ind_w = ind;

dist_var = 1:4;
H_var = 4;
n.H = numel(H_var);

% Definition of integer values
itg.fig = 1;
itg.lab = 1;
itg.arr = 1;
itg.sub = 1;
itg.axe = 1;
itg.fda = 1;

% Definition of figure and values
fig.xlab = {'Ratio H$_2$/N$_2$ [-]','$\dot{m}_{Feed}$ [kg/s]','$p_{Feed}$ [bar]','$T_{Feed}$ [$^\circ$C]',...
            '$w_{\mathrm{NH}_3,Feed}$ [wt.\%]','Catalyst activity [-]','Recovery NH$_3$ [-]'};
fig.xlim = [];
fig.pos  = [1 21 21 21 1 41 31]; 
fig.d     = [0.1 2 10 10 0.5 0.05];
for k = dist_var
    if k == 4
        fig.xlim = [fig.xlim;
                    floor((SS_SOC_w{k,H_var}.Var(1)*100/fig.d(k+1)))*fig.d(k+1) ...
                    fig.d(k+1) ...
                    ceil((SS_SOC_w{k,H_var}.Var(end)*100/fig.d(k+1)))*fig.d(k+1)];
    else
        fig.xlim = [fig.xlim;
                    floor((SS_SOC_w{k,H_var}.Var(1)/fig.d(k+1)))*fig.d(k+1) ...
                    fig.d(k+1) ...
                    ceil((SS_SOC_w{k,H_var}.Var(end)/fig.d(k+1)))*fig.d(k+1)];
    end
end
fig.dimw  = 8.8; 12.5;
fig.dimh  = 8.0; 10.0; 
fig.Font  = [10 8];
fig.label = [];
fig.sub   = [];
fig.dat   = [];

% Definition of the colours
rk_w = {'k','k','k','k'};
rk_wo = {'r','r','r','r'};
H_cell = {'T','NSM','ELM$_1$','ELM$_2$'};

% Initialization of the figure
fig.in(itg.fig) = figure(itg.fig);
set(fig.in(itg.fig),'units','centimeters');
set(gcf, 'position', [5+itg.fig,5+itg.fig, fig.dimw, fig.dimh]);

% Plotting of the loss with flowrate change
for k = dist_var
    fig.sub(itg.sub) = subplot(2,2,k); itg.sub = itg.sub+1;
    fig.axes(itg.axe) = gca;
    xlim(fig.xlim(k,[1 3]))
    if k == 1
        fig.axes(itg.axe).Position = [.11 .62 .375 .35];
    elseif k == 2
        fig.axes(itg.axe).Position = [.6  .62 .375 .35];
    elseif k == 3
        fig.axes(itg.axe).Position = [.11 .11 .375 .35];
    elseif k == 4
        fig.axes(itg.axe).Position = [.6  .11 .375 .35];
    end
    
    if k == 1 || k == 5
        ylim([0 6])
        fig.axes(itg.axe).YTick = 0:2:6;
    else
        ylim([0 1])
        fig.axes(itg.axe).YTick = 0:0.2:1;
        fig.axes(itg.axe).YTickLabel = {'0.0';'0.2';'0.4';'0.6';'0.8';'1.0'};
    end
    hold on
    for l = H_var
        if k == 4
            fig.dat(itg.fda:itg.fda+n.H-1) = plot(SS_SOC_wo{k,l}.Var*100,SS_SOC_wo{k,l}.loss,rk_wo{l}); itg.fda = itg.fda+n.H;
            fig.dat(itg.fda:itg.fda+n.H-1) = plot(SS_SOC_w{k,l}.Var*100,SS_SOC_w{k,l}.loss,rk_w{l});    itg.fda = itg.fda+n.H;
        else
            fig.dat(itg.fda:itg.fda+n.H-1) = plot(SS_SOC_wo{k,l}.Var,SS_SOC_wo{k,l}.loss,rk_wo{l}); itg.fda = itg.fda+n.H;
            fig.dat(itg.fda:itg.fda+n.H-1) = plot(SS_SOC_w{k,l}.Var,SS_SOC_w{k,l}.loss,rk_w{l});    itg.fda = itg.fda+n.H;
        end
    end
    plot(SS_SOC_w{k,l}.Var(fig.pos(k+1)),0,'kx','MarkerSize',6)
    fig.axes(itg.axe).XTick = fig.xlim(k,1):fig.xlim(k,2):fig.xlim(k,3);
    fig.label(itg.lab) = xlabel(fig.xlab{k+1}); itg.lab = itg.lab+1;
    if k == 1
        [~,fig.leg] = legend('Local (0)','Global');
        set(fig.axes(itg.axe).Legend,'Location','northwest','FontSize',fig.Font(2),'Interpreter','latex','Box','off')
    elseif k == 4
        fig.axes(itg.axe).XTickLabel = {'0.0';'0.5';'1.0';'1.5';'2.0'};
    end
    if mod(k-1,2) == 0
        fig.label(itg.lab) = ylabel('Loss [\%]'); itg.lab = itg.lab+1;
    end
    itg.axe = itg.axe+1;
end
itg.fig = itg.fig+1;

% Plotting of the difference in loss inbetween the methods
for k = dist_var
    for l = H_var
        Diffloss{k,l} = (SS_SOC_wo{k,l}.loss(ismember(SS_SOC_wo{k,l}.Var,SS_SOC_w{k,l}.Var))-SS_SOC_w{k,l}.loss(ismember(SS_SOC_w{k,l}.Var,SS_SOC_wo{k,l}.Var)))'...
                         ;% ./SS_SOC_w{j,l}.loss(ismember(SS_SOC_w{j,l}.Var,SS_SOC_wo{j,l}.Var))'*100;
    end
end

% Initialization of the figure
fig.in(itg.fig) = figure(itg.fig);
set(fig.in(itg.fig),'units','centimeters');
set(gcf, 'position', [5+itg.fig,5+itg.fig, fig.dimw, fig.dimh]);

% Plotting of the loss with flowrate change
for k = dist_var
    fig.sub(itg.sub) = subplot(2,2,k); itg.sub = itg.sub+1;
    fig.axes(itg.axe) = gca;
    xlim(fig.xlim(k,[1 3]))
    if k == 1
        fig.axes(itg.axe).Position = [.11 .62 .375 .35];
    elseif k == 2
        fig.axes(itg.axe).Position = [.6  .62 .375 .35];
    elseif k == 3
        fig.axes(itg.axe).Position = [.11 .11 .375 .35];
    elseif k == 4
        fig.axes(itg.axe).Position = [.6  .11 .375 .35];
    end
    hold on
    for l = H_var
        if k == 4
            fig.dat(itg.fda:itg.fda+n.H-1) = plot(SS_SOC_w{k,l}.Var*100,Diffloss{k,l},rk_w{l}); itg.fda = itg.fda+n.H;
        else
            fig.dat(itg.fda:itg.fda+n.H-1) = plot(SS_SOC_w{k,l}.Var,Diffloss{k,l},rk_w{l});     itg.fda = itg.fda+n.H;
        end
    end
%     if k ~= 1
        plot(fig.xlim(k,[1 3]),[0 0],'k')
%     end
    plot(SS_SOC_w{k,l}.Var(fig.pos(k+1)),0,'kx','MarkerSize',6)
    fig.axes(itg.axe).XTick = fig.xlim(k,1):fig.xlim(k,2):fig.xlim(k,3);
    fig.label(itg.lab) = xlabel(fig.xlab{k+1}); itg.lab = itg.lab+1;
%     fig.axes(itg.axe).LineWidth = 1;
    if mod(k-1,2) == 0
        fig.label(itg.lab) = ylabel('$\Delta$ Loss [\%]'); itg.lab = itg.lab+1;
    end
    itg.axe = itg.axe+1;
end

for k = 3:6
    fig.leg(k).XData = [fig.leg(k).XData(1) 0.2];
end
for k = 1:2
    fig.leg(k).Position = [.25 fig.leg(k).Position(2:3)];
end
fig.axes(1).Legend.Position = [0.26 0.86 0.3 0.1];

% Modificiation of the plotting properties
set(fig.label, 'Interpreter', 'Latex');
set(fig.axes,'TickLabelInterpreter', 'Latex');
set(fig.axes,'XMinorTick', 'on');
set(fig.axes,'YMinorTick', 'on');
set(fig.label, 'Fontsize', fig.Font(1));
set(fig.axes,'Fontsize', fig.Font(2));
set(fig.axes,'Box','on');
set(fig.axes,'LineWidth',.75);
set(fig.dat,'LineWidth',.75)

%%
for k = 1:2
    fig.leg(k).Interpreter = 'latex';
    fig.leg(k).FontSize = fig.Font(2);
end

