clc;clear; close all

set(0,'DefaultTextFontName','Times',...
     'DefaultTextFontSize',18,...
     'DefaultAxesFontName','Times',...
     'DefaultAxesFontSize',18,...
     'DefaultLineLineWidth',4,...
     'DefaultLineMarkerSize',7.75,...
     'DefaultStairLineWidth',2)

%% Colors
pos   = [100 100 500 400]; % figure position on screen

orange  = [1 0.5 0];
green   = [0 0.5 0];
greencg = [83,188,102]/256;
bluecg  = [44,144,195]/256;
movcg   = [198,102,177]/256;
indigo   = [0.2930 0 0.5078];

pistachio = [166 180 1]/256;
gold     = [0.9805, 0.7773,  0.25];
brick    = [108, 1, 2]/256;
cherry   = [222, 58, 58]/256;%[213, 1, 2]/256;
lilac    = [0.7383    0.5898    0.7930];
hotpink  = [0.8945    0.4102    0.5898];
cerulean = [0.1094    0.6641    0.8359];
plum     = [194, 147, 141]/256;

black = [0 0 0];

clr = [bluecg;indigo;movcg;greencg;black];
%% Compare new transformation method and fb lin for unmeasured disturbances
tend = 720;
filep = {'Shower_Dist_org','Shower_Dist_fast','Shower_Dist_slow','Shower_Dist_fblin'};

for j=1:4
     load(filep{j})
     
     figure(2); % plot T
     plot(time, T,'Color',clr(j,:))
     hold on
     xlim([0 tend])
     ylim([25 30.5])
     xlabel('Time [s]')
     ylabel('Temperature (y_2=T) [^oC]')
     set(gcf,'Position',pos)
     
     figure(3) % plot u1=q1
     plot(time,uplant(:,1),'Color',clr(j,:))
     hold on
     xlim([0 tend])
     ylim([0.45 0.55])
     xlabel('Time')
     ylabel('Flowrate (u_1=q_1) [m^3 s^{-1}]')
     set(gcf,'Position',pos)
     
     figure(4) % plot u2=q2
     plot(time,uplant(:,2),'Color',clr(j,:))
     hold on
     xlim([0 tend])
     ylim([0.45 0.55])
     xlabel('Time')
     ylabel('Flowrate (u_2=F_2) [m^3 s^{-1}]')
     set(gcf,'Position',pos)
     
end
figure(1) % plot y1=q
plot(time,q,'Color',greencg)
hold on
plot([0 time(end)],[q0 q0],'Color',clr(end,:),'LineStyle','--') % setpoint
hold on
xlim([0 tend])
ylim([0.9 1.1])
ylabel('Flowrate (y_1=F) [m^3 s^{-1}]')
xlabel('Time [s]')
set(gcf,'Position',pos)
legend('y_1','y_1^s','Location','northwest')
legend boxoff
print('-depsc2','-r600','Shower_dist_y1.eps')

figure(2);plot([0 time(end)], [T0 T0],'Color',clr(end,:),'LineStyle','--') % setpoint
legend('A=-0.01 (original)','A=-0.02 (faster)','A=-0.005 (slower)','A=0 (feedback linearization)','y_2^s','Location','southwest')
legend boxoff
print('-depsc2','-r600','Shower_dist_y2.eps')

figure(3)
legend('A=-0.01 (original)','A=-0.02 (faster)','A=-0.005 (slower)','A=0 (feedback linearization)','Location','southwest')
legend boxoff
print('-depsc2','-r600','Shower_dist_u1.eps')

figure(4)
legend('A=-0.01 (original)','A=-0.02 (faster)','A=-0.005 (slower)','A=0 (feedback linearization)','Location','northwest')
legend boxoff
print('-depsc2','-r600','Shower_dist_u2.eps')


%% Compare calculation block and cascade for input saturation
tend = 300;
filep={'Shower_Input_Sat_trf_pairing1','Shower_Input_Sat_cascade_pairing1'...
     'Shower_Input_Sat_cascade_pairing2'};
qmax = 1; qmin=0; ltyp2 = {'-','-','--'};
for j=1:3
     load(filep{j})
     
     figure(11) % plot y1=q
     plot(time,q,'Color',clr(j,:))
     hold on
     xlim([0 tend])
     ylim([0.5 1.75])
     ylabel('Flowrate (y_1=F) [m^3 s^{-1}]')
     xlabel('Time [s]')
     set(gcf,'Position',pos)
     
     figure(12); % plot T
     plot(time, T,'Color',clr(j,:))
     hold on
     xlim([0 tend])
     ylim([28 33])
     xlabel('Time [s]')
     ylabel('Temperature (y_2=T) [^oC]')
     set(gcf,'Position',pos)
     
     figure(13) % plot u1=q1
     plot(time,uplant(:,1),'Color',clr(j,:))
     hold on
     xlim([0 tend])
     ylim([0 1.6])
     xlabel('Time')
     ylabel('Flowrate (u_1=F_1) [m^3 s^{-1}]')
     set(gcf,'Position',pos)
     
     figure(14) % plot u2=q2
     plot(time,uplant(:,2),'Color',clr(j,:),'LineStyle',ltyp2{j})
     hold on
     xlim([0 tend])
     ylim([0 1.6])
     xlabel('Time')
     ylabel('Flowrate (u_2=F_2) [m^3 s^{-1}]')
     set(gcf,'Position',pos)
     
end
figure(11); plot(time,v1s,'Color',clr(4,:),'LineStyle',ltyp2{3}) % setpoint
legend('Algebraic block','Cascade: u_1-v_1; u_2-v_2','Cascade: u_1-v_2; u_2-v_1','y_1^s','Location','best')
legend boxoff
print('-depsc2','-r600','Shower_sat_y1.eps')

figure(12);plot(time, v2s,'Color',clr(4,:),'LineStyle',ltyp2{3}) % setpoint
legend('Algebraic block','Cascade: u_1-v_1; u_2-v_2','Cascade: u_1-v_2; u_2-v_1','y_2^s','Location','best')
legend boxoff
print('-depsc2','-r600','Shower_sat_y2.eps')

figure(13)
plot([0 tend],[qmax qmax],'k--')
plot([0 tend],[qmin qmin],'k--')
legend('Algebraic block','Cascade: u_1-v_1; u_2-v_2','Cascade: u_1-v_2; u_2-v_1','Location','north')
legend boxoff
print('-depsc2','-r600','Shower_sat_u1.eps')

figure(14)
plot([0 tend],[qmax qmax],'k--')
plot([0 tend],[qmin qmin],'k--')
legend('Algebraic block','Cascade: u_1-v_1; u_2-v_2','Cascade: u_1-v_2; u_2-v_1','Location','north')
legend boxoff
print('-depsc2','-r600','Shower_sat_u2.eps')

%% plot v for cascade

for k = 2:3
     load(filep{k})
     figure(15)
     plot(time,v(:,1),'Color',clr(k,:))
     hold on
     xlabel('Time [s]')
     ylabel('Transformed input (v_1)')
     xlim([0 tend]);ylim([0 1.7])
     set(gcf,'Position',pos)
     
     figure(16)
     plot(time,v(:,2),'Color',clr(k,:))
     hold on
     xlabel('Time [s]')
     ylabel('Transformed input (v_2)')
     xlim([0 tend]);ylim([19 34])
     set(gcf,'Position',pos)
end
figure(15); plot(time,v1s,'Color',greencg,'LineStyle','-')
legend('Cascade: u_1-v_1; u_2-v_2','Cascade: u_1-v_2; u_2-v_1','v_1^s','Location','best')
legend boxoff
print('-depsc2','-r600','Shower_sat_v1.eps')

figure(16); plot(time,v2s,'Color',greencg,'LineStyle','-')
legend('u_1-v_1; u_2-v_2','u_1-v_2; u_2-v_1','v_2^s','Location','southwest')
legend boxoff
print('-depsc2','-r600','Shower_sat_v2.eps')
