% Simulate the simulink model MulticompModel_SL 
% plots and save results
clear all
close all
tic

% runs the simulink model with ration control
% [t,x,y] = sim('MulticompModel');
% runs the simulink model with Single loop
[t,x,y] = sim('MulticompModel_SL');
% Array of handles to active figures
figure_handler = [];
%% Component profiles, using the last calculated value:
%% Deethanizer

NT=32;
figure_handler(1)=figure(1);
C5=ones(1,NT)-CompE(end,1:NT)-CompE(end,NT+1:2*NT)-CompE(end,2*NT+1:3*NT)-CompE(end,3*NT+1:4*NT);
plot(CompE(end,1:NT),'g');
hold on
plot(CompE(end,NT+1:2*NT),'b');
plot(CompE(end,2*NT+1:3*NT),'r');
plot(CompE(end,3*NT+1:4*NT),'K:');
plot((C5),'R:');
legend ('C2','C3','iC4','nC4','C5');title('compositionprofiledeethanizer');xlabel('Column_Trays');ylabel('Mol fraction')
hold off
%% Depropanizer
NT=48;
figure_handler(2)=figure(2);
C5=ones(1,NT)-CompP(end,1:NT)-CompP(end,NT+1:2*NT)-CompP(end,2*NT+1:3*NT)-CompP(end,3*NT+1:4*NT);
plot(CompP(end,1:NT),'g');
hold on
plot(CompP(end,NT+1:2*NT),'b');
plot(CompP(end,2*NT+1:3*NT),'r');
plot(CompP(end,3*NT+1:4*NT),'K:');
plot((C5),'R:');
legend ('C2','C3','iC4','nC4','C5');title('compositionprofiledepropanizer');xlabel('Column_Trays');ylabel('Mol fraction')
hold off


%% Debutannizer
NT=40;
figure_handler(3)=figure(3);
C4tot=CompB(end,2*NT+1:3*NT)+CompB(end,3*NT+1:4*NT);
C5=ones(1,NT)-CompB(end,1:NT)-CompB(end,NT+1:2*NT)-CompB(end,2*NT+1:3*NT)-CompB(end,3*NT+1:4*NT);
plot(CompB(end,1:NT),'g');
hold on
plot(CompB(end,NT+1:2*NT),'b');
% plot(CompB(end,2*NT+1:3*NT),'r');
% plot(CompB(end,3*NT+1:4*NT),'K:');
plot((C4tot),'k');
plot((C5),'R:');
legend ('C2','C3','C4','5');title('compositionprofiledebutanizer');xlabel('Column_Trays');ylabel('Mol fraction')
hold off
%% Butansplitter
NT=92;
figure_handler(4)=figure(4);
C5=ones(1,NT)-CompS(end,1:NT)-CompS(end,NT+1:2*NT)-CompS(end,2*NT+1:3*NT)-CompS(end,3*NT+1:4*NT);
plot(CompS(end,1:NT),'g');
hold on
plot(CompS(end,NT+1:2*NT),'b');
plot(CompS(end,2*NT+1:3*NT),'r');
plot(CompS(end,3*NT+1:4*NT),'K:');
plot((C5),'R:');
legend ('C2','C3','iC4','nC4','C5');title('compositionprofilebutansplitter');xlabel('Column_Trays');ylabel('Mol fraction')
hold off
%% Temperature profile, using the last calculated value:
%% Profile in the Depropanizer,Depropanizer,Debutannizer, butansplitter
figure_handler(5)=figure(5);
plot(TempE(end,1:32));
title('TemperatureDeethanizer');xlabel('Column_Trays');ylabel('Temperature [K]')
figure_handler(6)=figure(6);
plot(TempP(end,1:48));
title('TemperatureDepropanizer');xlabel('Column_Trays');ylabel('Temperature [K]')
figure_handler(7)=figure(7);
plot(TempB(end,1:40));
title('TemperatureDebutanizer');xlabel('Column_Trays');ylabel('Temperature [K]')
figure_handler(8)=figure(8);
plot(TempS(end,1:92));
title('TemperatureButansplitter');xlabel('Column_Trays');ylabel('Temperature [K]')

figure_handler(9)=figure(9);
%% plots the temperature on the controlled tray

subplot(2,2,1),plot(TempE(:,5),'g');title ('T_E5')
hold on
subplot(2,2,2),plot(TempP(:,5),'b');title ('T_P5')
subplot(2,2,3),plot(TempB(:,5),'r');title ('T_B5')
subplot(2,2,4),plot(TempS(:,5),'K:');title ('T_S5')
% legend ('T_E5','T_P5','T_B5','T_S5');title('Temperature on TC_tray');xlabel('Time [min]');ylabel('Temperature [K]')

hold off

figure_handler(10)=figure(10);
%% plots the level in deethanizer and depropanizer tower

subplot(2,2,1),plot(LD_E,'g');title ('Level-MD deethanizer')
hold on
subplot(2,2,2),plot(LB_E,'g:');title ('Level-MB deethanizer')
subplot(2,2,3),plot(LD_P,'b');title ('Level-MD depropanizer')
subplot(2,2,4),plot(LB_P,'b:');title ('Level-MB depropanizer')
figure_handler(11)=figure(11);
%% plots the level in the debutanizer and butansplitter tower

subplot(2,2,1),plot(LD_B,'r');title ('Level-MD debutanizer')
hold on
subplot(2,2,2),plot(LB_B,'r:');title ('Level-MB debutanizer')
subplot(2,2,3),plot(LD_S,'k');title ('Level-MD butansplitter')
subplot(2,2,4),plot(LB_S,'k:');title ('Level-MB butansplitter')

figure_handler(12)=figure(12);
%% plots flows into towers

subplot(2,2,1),plot(FE,'r');title ('Feed deethanizer')
hold on
subplot(2,2,2),plot(FP,'r:');title ('Feed depropanizer')
subplot(2,2,3),plot(FB,'k');title ('Feed debutanizer')
subplot(2,2,4),plot(FS,'k:');title ('Feed butansplitter')


figure_handler(13)=figure(13);
%% plots the light component in deethanizer and depropanizer tower

subplot(2,2,1),plot(y1E,'b');title ('Ethane deethanizer(top)')
hold on
subplot(2,2,2),plot(y2E,'r:');title ('Ethane deethanizer(bottom)')
subplot(2,2,3),plot(y1P,'b');title ('Propane depropanizer(top)')
subplot(2,2,4),plot(y2P,'r:');title ('Propane depropanizer(bottom)')
hold off

figure_handler(14)=figure(14);
%% plots the light component in the debutanizer and butansplitter tower

subplot(2,2,1),plot(y1B,'b');title ('iC4+nC4 debutanizer(top)')
hold on
subplot(2,2,2),plot(y2B,'r:');title ('iC4+nC4 debutanizer(bottom)')
subplot(2,2,3),plot(y1S,'b');title ('iC4 Butansplitter(top)')
subplot(2,2,4),plot(y2S,'r:');title ('iC4 Butansplitter(bottom')
hold off
figure_handler(15)=figure(15);
%% plots the reboiler flow

subplot(2,2,1),plot(VE,'b');title ('VE')
hold on
subplot(2,2,2),plot(VP,'r');title ('VP')
subplot(2,2,3),plot(VB,'b');title ('VB')
subplot(2,2,4),plot(VS,'r:');title ('VS')

figure_handler(16)=figure(16);
%% plots the products in the last two distillation column
subplot(2,2,1),plot(CompB(:,120),'b');title ('iC4 in debutanizer(Top)')
hold on
subplot(2,2,2),plot(CompB(:,160),'r:');title ('nC4 in debutanizer(Top) ')
subplot(2,2,3),plot(CompS(:,276),'b');title ('iC4 in butansplitter(Top) ')
subplot(2,2,4),plot(CompS(:,277),'k');title ('nC4 in butansplitter(Bottom) ')

%% Save: mat-file
%SAVE results
%Ration control
% save('multicomp_sep_Bias_qFe.mat');
% single loop
save('multicomp_sep_Single_sFS.mat');


toc


















