% Plot_PPT

% Calculation of the loss for each of the cases
if ~isfield(SS_SOC{1,1},'loss')
for j = 1:n.d
    for l = H_var
        if numel(SS_opt{j}.Var) == numel(SS_SOC{j,l}.Var)
            if sum(abs(diff([SS_opt{j}.Var;SS_SOC{j,l}.Var]))) < 1e-6
                SS_SOC{j,l}.Var = SS_opt{j}.Var;
            end
        end
        SS_SOC{j,l}.loss = (SS_opt{j}.f(end,ismember(SS_opt{j}.Var,SS_SOC{j,l}.Var))-SS_SOC{j,l}.f(end,:))'...
                            ./SS_opt{j}.f(end,ismember(SS_opt{j}.Var,SS_SOC{j,l}.Var))'*100;
    end
end
end

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

% Definition of figure and values
fig.xlab = {'Flowrate [kg/s]','Pressure [bar]','Temperature [C]',...
            'Mass fraction [-]','Ratio H_2/N_2 [-]','Catalyst activity [-]'};
fig.xlim = [];
fig.d     = [5 25 25 0.02 0.1 0.2];
for k = 1:n.d
    fig.xlim = [fig.xlim;
                floor(round(SS_opt{k}.Var(1)/fig.d(k)))*fig.d(k) ...
                fig.d(k) ...
                ceil(round(SS_opt{k}.Var(end)/fig.d(k)))*fig.d(k)];
end
fig.dimw  = 24;
fig.dimh  = 12;
fig.label = [];
fig.Font  = [16 14];
fig.ann   = [];
fig.sub   = [];

% Definition of the colours
rk = {'k','k--','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 = 1:n.d
    fig.sub(k) = subplot(2,ceil(n.d/2),k);
    fig.axes(k) = gca;
    xlim(fig.xlim(k,[1 3]))
%         ylim([0 3])
    hold on
    for l = H_var
        plot(SS_SOC{k,l}.Var,SS_SOC{k,l}.loss,rk{l})
    end
    fig.axes(k).XTick = fig.xlim(k,1):fig.xlim(k,2):fig.xlim(k,3);
%         fig.axes(k).YTick = 0:1:3;
    fig.label(itg.lab) = xlabel(fig.xlab{k}); itg.lab = itg.lab+1;
    fig.axes(k).LineWidth = 1;
    if mod(k-1,n.d/2) == 0
        fig.label(itg.lab) = ylabel('Loss [%]'); itg.lab = itg.lab+1;
    end
    switch k
        case 1
            fig.label(itg.lab) = legend(H_cell{H_var},'Location','best'); itg.lab = itg.lab+1;
            legend BOXOFF 
        case 4
            fig.axes(k).XTick = [0.04 0.06 0.08 0.10 0.12];
            fig.axes(k).XTickLabel = {'0.04', '0.06', '0.08', '0.10', '0.12'};
        case 5
%                 ylim([0 0.02])
%                 fig.axes(k).YTick = 0:0.005:0.02;
%                 fig.axes(k).YTickLabel = {'0.000','0.005','0.010','0.015','0.020'};
    end
end

% Modificiation of the plotting properties
set(fig.ann, 'LineStyle', 'none');
set(fig.axes,'XMinorTick', 'on');
set(fig.ann, 'Fontsize', fig.Font(1));
set(fig.label, 'Fontsize', fig.Font(1));
set(fig.label(3), 'Fontsize', fig.Font(2));
set(fig.axes,'Fontsize', fig.Font(2));
