% RELEVANTPLOTS.M %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Make relevant plots after simulation of Kaibel column

t = 1:size(x_prod,1);

% PLOT PRODUCT COMPOSITIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
figure();

subplot(2,2,1);
plot(t,x_prod(:,1));
xlabel('Time [min]'); ylabel('Mole fraction');
title('Top product, D');
ylim([0 1]); xlim([1 length(t) - 1]);

subplot(2,2,2);
plot(t,x_prod(:,3));
xlabel('Time [min]'); ylabel('Mole fraction');
title('Side product 1, S1');
ylim([0 1]); xlim([1 length(t) - 1]);

subplot(2,2,3);
plot(t,x_prod(:,4));
xlabel('Time [min]'); ylabel('Mole fraction');
title('Side product 2, S2');
ylim([0 1]); xlim([1 length(t) - 1]);

subplot(2,2,4);
plot(t,x_prod(:,2));
xlabel('Time [min]'); ylabel('Mole fraction');
title('Bottom product, B');
ylim([0 1]); xlim([1 length(t) - 1]);

% PLOT CONTROLLED TEMPERATURES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
figure();

subplot(2,2,1);
plot(t,temp(:,30)); hold on; plot(t,dyndata.y_nom(36),':');
xlabel('Time [min]'); ylabel('Temperature [K]');
title('T30 Control variable for reflux (L)');
ylim([330 390]); xlim([1 length(t) - 1]);

subplot(2,2,2);
plot(t,temp(:,17)); hold on; plot(t,dyndata.y_nom(23),':');
xlabel('Time [min]'); ylabel('Temperature [K]');
title('T17 Control variable for liquid split (Rl)');
ylim([330 390]); xlim([1 length(t) - 1]);

subplot(2,2,3);
plot(t,temp(:,59)); hold on; plot(t,dyndata.y_nom(65),':');
xlabel('Time [min]'); ylabel('Temperature [K]');
title('T59 Control variable for side stream 1 (S1)');
ylim([330 390]); xlim([1 length(t) - 1]);

subplot(2,2,4);
plot(t,temp(:,49)); hold on; plot(t,dyndata.y_nom(55),':');
xlabel('Time [min]'); ylabel('Temperature [K]');
title('T49 Control variable for side stream 2 (S2)');
ylim([330 390]); xlim([1 length(t) - 1]);

% PLOT COMPOSITION INPUTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
figure();

subplot(2,2,1);
plot(t,input(:,1));
xlabel('Time [min]'); ylabel('Flow');
title('Reflux, L');
xlim([1 length(t) - 1]); ylim([0 4]);

subplot(2,2,2);
plot(t,input(:,5));
xlabel('Time [min]'); ylabel('Flow');
title('Liquid split ratio, Rl');
xlim([1 length(t) - 1]); ylim([0 1]);

subplot(2,2,3);
plot(t,input(:,3));
xlabel('Time [min]'); ylabel('Flow');
title('Side stream 1, S1');
xlim([1 length(t) - 1]); ylim([0 1]);

subplot(2,2,4);
plot(t,input(:,4));
xlabel('Time [min]'); ylabel('Flow');
title('Side stream 2, S2');
xlim([1 length(t) - 1]); ylim([0 1]);

% PLOT LEVELS AND THEIR INPUTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
figure();

subplot(2,2,1);
plot(t,level(:,1)); hold on; plot(t,dyndata.y_nom(5),':');
xlabel('Time [min]'); ylabel('Mass [kg]');
title('Reflux tank holdup');
xlim([1 length(t) - 1]);

subplot(2,2,2);
plot(t,input(:,12));
xlabel('Time [min]'); ylabel('Flow');
title('Top product stream, D');
xlim([1 length(t) - 1]); ylim([0 1]);

subplot(2,2,3);
plot(t,level(:,2)); hold on; plot(t,dyndata.y_nom(6),':');
xlabel('Time [min]'); ylabel('Mass [kg]');
title('Reboiler tank holdup');
xlim([1 length(t) - 1]);

subplot(2,2,4);
plot(t,input(:,13));
xlabel('Time [min]'); ylabel('Flow');
title('Bottom product stream, B');
xlim([1 length(t) - 1]); ylim([0 1]);

% PLOT TOTAL IMPURITY FLOW %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
figure();

totimpfl = sum(input(:,12) .* (1 - x_prod(:,1)) + input(:,3) .* (1 - x_prod(:,2)) + input(:,4) .* (1 - x_prod(:,3)) + input(:,13) .* (1 - x_prod(:,4)));

plot(input(:,12) .* (1 - x_prod(:,1)) + input(:,3) .* (1 - x_prod(:,2)) + input(:,4) .* (1 - x_prod(:,3)) + input(:,13) .* (1 - x_prod(:,4)));
xlabel('Time [min]'); ylabel('Flow [mol/min]');
title(strcat('Total impurity flow, total integrated flow: ',num2str(totimpfl),' [mol]'));
xlim([1 length(t) - 1]); ylim([0.03 0.09]);

% TEMPERATURE ERROR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
figure();

plot(abs(368.2944 - temp(:,17)),'b-');
hold on;
plot(abs(341.9690 - temp(:,30)),'b:');
plot(abs(355.8309 - temp(:,59)),'b--');
plot(abs(379.6372 - temp(:,49)),'b.-');
legend('T17','T30','T59','T49');
title(strcat('Absolute error. Integrated temperature errors; T17:',num2str(sum(abs(368.2944 - temp(:,17)))),', T30:',num2str(sum(abs(341.9690 - temp(:,30)))),', T59:',num2str(sum(abs(355.8309 - temp(:,59)))),', T49:',num2str(sum(abs(379.6372 - temp(:,49)))),', Total:',num2str(sum(abs(368.2944 - temp(:,17))) + sum(abs(341.9690 - temp(:,30))) + sum(abs(379.6372 - temp(:,49))) + sum(abs(355.8309 - temp(:,59))))));