
%Load simulation results
load('Room3Ctrl_')    %One controller for each MV
load('RoomSRC_')  %Std SRC

set(0,'DefaultTextFontName','Times',...
'DefaultTextFontSize',24,...
'DefaultAxesFontName','Times',...
'DefaultAxesFontSize',24,...
'DefaultLineLineWidth',2.5,...
'DefaultLineMarkerSize',12)

% In paper: Figs 5-8
tplot = Room3Ctrl_t/3600;
tplot2 = RoomSRC_t/3600;
timescale='h';

figure
subplot(3,1,1)

plot(tplot,Room3Ctrl_Tdes_sp,'--','Linewidth', 1,'Color', [0.7500         0    0.7500] )
hold on
plot(tplot,Room3Ctrl_ACsp,'--', 'Linewidth', 1, 'Color',[    0    0.4470    0.7410])
plot(tplot,Room3Ctrl_EHsp,'--', 'Linewidth', 1, 'Color', [1.0000         0         0])
plot(tplot,Room3Ctrl_HWsp,'--', 'Linewidth', 1,'Color', [ 0.9290    0.6940    0.1250] )
plot(tplot,Room3Ctrl_T,'Color', [ 0.4940    0.1840    0.5560])
ylabel('T [°C]')
ylim([15 24])
title ('$y = T$','Interpreter','Latex');
%legend('$T^{des}$','$T$','Interpreter','Latex', 'Orientation','horizontal');
legend('$T^{des}$','$T_{ac}^{sp}$', '$T_{el}^{sp}$','$T_{hw}^{sp}$', '$T$','Interpreter','Latex', 'Orientation','horizontal');
hold off
legend boxoff 

subplot(3,1,2)
plot(tplot,Room3Ctrl_Tdes_sp,'--','Linewidth', 1,'Color', [ 0.7500         0    0.7500] )
hold on
plot(tplot,Room3Ctrl_Tin, 'Color', [ 0.4660    0.6740    0.1880])
ylabel('T, [°C]')
ylim([0 35])
title ('$d = T^{amb}$','Interpreter','Latex');
legend('$T^{des}$','$T_{in}$','Interpreter','Latex','Orientation','horizontal');
legend boxoff 

subplot(3,1,3)
plot(tplot,Room3Ctrl_T, 'Color', [ 0.4940    0.1840    0.5560])
hold on
plot(tplot,Room3Ctrl_Tfl, '-.','Color', [   0    0.7500    0.7500])
plot(tplot,Room3Ctrl_Tdes_sp,'--','Linewidth', 1,'Color', [ 0.7500         0    0.7500] )
title ('$x = [T, T_{fl}]$','Interpreter','Latex');
ylabel('T, [°C]')
legend('$T$', '$T_{fl}$','$T^{des}$','Interpreter','Latex','Orientation','horizontal');
hold off
legend boxoff 
ylim([15 30])
xlabel(strcat('time [',timescale,']'))

%% MVs

%MVs
figure(2)
subplot (3,1,1)
p1=plot(tplot,Room3Ctrl_uAC);
hold on
plot(tplot,Room3Ctrl_ACmaxplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
plot(tplot,Room3Ctrl_Qminplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
ylabel('$\dot{Q}_{ac}$ [kW]','Interpreter','Latex');
title('$\dot{Q}_{ac}$','Interpreter','Latex');
ylim([-0.2 Room3Ctrl_ACmax+0.2])
ytickformat('%.1f')
%legend('$\dot{Q}_{ac}$','$\dot{Q}_{ac}^{max}$','Interpreter','Latex');
%legend([p1],'$\dot{Q}_{ac}$','Interpreter','Latex');
hold off
%legend boxoff 

subplot (3,1,2)
plot(tplot,Room3Ctrl_uHW,'Color', [ 0.9290    0.6940    0.1250] )
hold on
plot(tplot,Room3Ctrl_HWmaxplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
plot(tplot,Room3Ctrl_Qminplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
ylabel('$\dot{Q}_{hw}$ [kW]','Interpreter','Latex');
title('$\dot{Q}_{hw}$','Interpreter','Latex');
ylim([-0.2 Room3Ctrl_HWmax+0.2])
ytickformat('%.1f')
%title('Heating water')
%legend('$\dot{Q}_{hw}$','$\dot{Q}_{hw}^{max}$','Interpreter','Latex');
hold off
%legend boxoff 

subplot (3,1,3)
plot(tplot,Room3Ctrl_uEH , 'Color', [1.0000         0         0])
hold on
plot(tplot,Room3Ctrl_EHmaxplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
plot(tplot,Room3Ctrl_Qminplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
ylabel('$\dot{Q}_{eh}$ [kW]','Interpreter','Latex');
title('$\dot{Q}_{eh}$','Interpreter','Latex');
ylim([-0.2 Room3Ctrl_EHmax+0.2])
ytickformat('%.1f')
%title('Electrical heating')
%legend('$\dot{Q}_{el}$','$\dot{Q}_{el}$','Interpreter','Latex');
hold off
%legend boxoff 
xlabel(strcat('time [',timescale,']'))

%% Comparison

%MVs
figure(3)
subplot (3,1,1)
p1=plot(tplot,Room3Ctrl_uAC,'Color',[    0.3010    0.7450    0.9330]);
hold on
p2=plot(tplot2,RoomSRC_uAC,'k--');
plot(tplot,Room3Ctrl_ACmaxplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
plot(tplot,Room3Ctrl_Qminplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
ylabel('$\dot{Q}_{ac}$ [kW]','Interpreter','Latex');
title('$\dot{Q}_{ac}$','Interpreter','Latex');
ylim([-0.2 Room3Ctrl_ACmax+0.2])
ytickformat('%.1f')
%legend('$\dot{Q}_{ac}$','$\dot{Q}_{ac}^{max}$','Interpreter','Latex');
legend([p1 p2],'3 Controllers','SRC','Interpreter','Latex', 'Location','East');
hold off
legend boxoff 

subplot (3,1,2)
p1=plot(tplot,Room3Ctrl_uHW,'Color', [ 0.9290    0.6940    0.1250] );
hold on
p2=plot(tplot2,RoomSRC_uHW,'k--'); %,'Color',[ 0.9290    0.6940    0.1250]);
plot(tplot,Room3Ctrl_HWmaxplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
plot(tplot,Room3Ctrl_Qminplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
ylabel('$\dot{Q}_{hw}$ [kW]','Interpreter','Latex');
title('$\dot{Q}_{hw}$','Interpreter','Latex');
ylim([-0.2 Room3Ctrl_HWmax+0.2])
ytickformat('%.1f')
%title('Heating water')
%legend('$\dot{Q}_{hw}$','$\dot{Q}_{hw}^{max}$','Interpreter','Latex');
hold off
legend([p1 p2],'3 Controllers','SRC','Interpreter','Latex', 'Location','West');
legend boxoff 

subplot (3,1,3)
p1=plot(tplot,Room3Ctrl_uEH ,'r');% 'Color', [1.0000         0         0]);
hold on
p2=plot(tplot2,RoomSRC_uEH,'--k'); %,'Color',[ 0.9290    0.6940    0.1250]);
plot(tplot,Room3Ctrl_EHmaxplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
plot(tplot,Room3Ctrl_Qminplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
ylabel('$\dot{Q}_{eh}$ [kW]','Interpreter','Latex');
title('$\dot{Q}_{eh}$','Interpreter','Latex');
ylim([-0.2 Room3Ctrl_EHmax+0.2])
ytickformat('%.1f')
%title('Electrical heating')
%legend('$\dot{Q}_{el}$','$\dot{Q}_{el}$','Interpreter','Latex');
hold off
legend([p1 p2],'3 Controllers','SRC','Interpreter','Latex', 'Location','West');
legend boxoff 
xlabel(strcat('time [',timescale,']'))

%% Comparison T
figure(4)
subplot(3,1,2)
p3=plot(tplot,Room3Ctrl_Tdes_sp,'k--','Linewidth', 1); %,'Color', [0.7500         0    0.7500] );
hold on
p1=plot(tplot,Room3Ctrl_T,'Color', [ 0.4940    0.1840    0.5560]);
p2=plot(tplot2,RoomSRC_T,'Color', [   0    0.7500    0.7500]);
ylabel('T [°C]')
ylim([15 24])
title ('$y = T$','Interpreter','Latex');
legend([p1 p2 p3],'3 Controllers','SRC','$T^{des}$','Interpreter','Latex', 'Orientation','horizontal');
hold off
legend boxoff 

subplot(3,1,1)
plot(tplot,Room3Ctrl_Tdes_sp,'k--','Linewidth', 1); %,'Color', [ 0.7500         0    0.7500] )
hold on
plot(tplot,Room3Ctrl_Tin, 'Color', [ 0.4660    0.6740    0.1880])
ylabel('T, [°C]')
ylim([0 35])
title ('$d = T^{amb}$','Interpreter','Latex');
legend('$T^{des}$','$T_{in}$','Interpreter','Latex','Orientation','horizontal');
legend boxoff 

subplot(3,1,3)
plot(tplot,Room3Ctrl_Jtot, 'Color', [ 0.4940    0.1840    0.5560])
hold on
plot(tplot2,RoomSRC_Jtot, '-.','Color', [   0    0.7500    0.7500])
%plot(tplot,Room3Ctrl_Tdes_sp,'--','Linewidth', 1,'Color', [ 0.7500         0    0.7500] )
title ('$x = [T, T_{fl}]$','Interpreter','Latex');
ylabel('T, [°C]')
legend('3 Controllers', 'SRC','$T^{des}$','Interpreter','Latex','Orientation','horizontal');
hold off
legend boxoff 
%ylim([15 30])
xlabel(strcat('time [',timescale,']'))

%% Extra plot

mv = RoomSRC_mvmax;
Cp = RoomSRC_Cp;

term = mv*Cp/2; %mvCp is actually alpha in the paper
ratio = linspace(0.1,10,11);  %price/kT
deltaT_hot = -ratio*term;
deltaT_cold = ratio*term;

figure(5)
plot(ratio, deltaT_cold)
hold on
plot(ratio, deltaT_hot,'r')
xlabel('p_i/p_T [°C^2/kW]'); %, 'Interpreter', 'Latex')
ylabel ('\Delta y_i^{sp} = T_i^{sp}-T^{sp} [°C]');%, 'Interpreter', 'Latex')
legend ('$Q_{AC}$', '$Q_{EH},Q_{HW}$', 'Interpreter', 'Latex', 'Location', 'East')
legend boxoff 

%% 3 controllers
figure(6)
subplot(3,1,1)
plot(tplot,Room3Ctrl_Tdes_sp,'k--','Linewidth', 1,'Color', [ 0.7500         0    0.7500] )
hold on
plot(tplot,Room3Ctrl_Tin, 'Color', [ 0.4660    0.6740    0.1880])
ylabel('T [°C]')
ylim([0 35])
title ('$d=T^{amb}$','Interpreter','Latex');
legend('$T^{sp}$','$T^{amb}$','Interpreter','Latex','Orientation','vertical', 'Location','Southwest', 'FontSize', 17);
legend boxoff 

subplot(3,1,2)
plot(tplot,Room3Ctrl_Tdes_sp,'--','Linewidth', 1,'Color', [0.7500         0    0.7500] )
hold on
plot(tplot,Room3Ctrl_T,'Color', [ 0.4940    0.1840    0.5560])
plot(tplot,Room3Ctrl_ACsp,'--', 'Linewidth', 1, 'Color',[    0    0.4470    0.7410])
plot(tplot,Room3Ctrl_HWsp,'--', 'Linewidth', 1,'Color', [ 0.9290    0.6940    0.1250] )
plot(tplot,Room3Ctrl_EHsp,'--', 'Linewidth', 1, 'Color', [1.0000         0         0])

ylabel('T [°C]')
ylim([17 23])
title ('$y=T$','Interpreter','Latex');
%legend('$T^{des}$','$T$','Interpreter','Latex', 'Orientation','horizontal');
legend('$T^{sp}$','$T$','$T_{AC}^{sp}$', '$T_{HW}^{sp}$', '$T_{EH}^{sp}$','Interpreter','Latex', 'Orientation','horizontal', 'Location','Southwest', 'FontSize', 15);
hold off
legend boxoff 


subplot(3,1,3)
p1=plot(tplot,Room3Ctrl_uAC,'Color',[    0.3010    0.7450    0.9330]);
hold on
p2=plot(tplot,Room3Ctrl_uHW,'Color', [ 0.9290    0.6940    0.1250] );
p3=plot(tplot,Room3Ctrl_uEH ,'r');
plot(tplot,Room3Ctrl_ACmaxplot,'--', 'Color',[0.3010    0.7450    0.9330],'Linewidth', 1)
plot(tplot,Room3Ctrl_Qminplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
plot(tplot,Room3Ctrl_HWmaxplot,'--', 'Color',[ 0.9290    0.6940    0.1250],'Linewidth', 1)
plot(tplot,Room3Ctrl_EHmaxplot,'r--','Linewidth', 1)

ylabel('Q_{i} [kW]');%,'Interpreter','Latex');
title('$u_i=Q_{i}$','Interpreter','Latex');
ylim([-0.2 Room3Ctrl_ACmax+0.2])
ytickformat('%.0f')
%legend('$\dot{Q}_{ac}$','$\dot{Q}_{ac}^{max}$','Interpreter','Latex');
legend([p1 p2 p3],'$Q_{AC}$','$Q_{HW}$','$Q_{EH}$','Interpreter','Latex','Location','East', 'Orientation','vertical', 'FontSize', 15);%'Interpreter','Latex', 
hold off
legend boxoff 
xlabel(strcat('time [',timescale,']'))
%text(8.1,4.25,'$\dot{Q}_{AC}^{max}$','Interpreter','Latex','FontSize', 10); %,'Color',[0.3010    0.7450    0.9330])
%text(8.1,2.7,'$\dot{Q}_{HW}^{max}$','Interpreter','Latex','FontSize', 10); %,'Color',[ 0.9290    0.6940    0.1250])
%text(8.1,3.55,'$\dot{Q}_{EH}^{max}$','Interpreter','Latex','FontSize', 10); %,'Color','r')
%% Comparison to SRC
figure(7)
subplot(3,1,1)
plot(tplot,Room3Ctrl_Tdes_sp,'k--','Linewidth', 1,'Color', [ 0.7500         0    0.7500] )
hold on
plot(tplot,Room3Ctrl_Tin, 'Color', [ 0.4660    0.6740    0.1880])
ylabel('T [°C]')
ylim([0 35])
title ('$d=T^{amb}$','Interpreter','Latex');
legend('$T^{sp}$','$T^{amb}$','Interpreter','Latex','Orientation','vertical', 'Location','Southwest', 'FontSize', 17);
legend boxoff 

subplot(3,1,2)
p1=plot(tplot,Room3Ctrl_Tdes_sp,'k--','Linewidth', 1,'Color', [0.7500         0    0.7500] );
hold on
p2=plot(tplot,Room3Ctrl_T,'Color', [ 0.4940    0.1840    0.5560]);
p3=plot(tplot2,RoomSRC_T,'--','Color', [   0    0.7500    0.7500]);
ylabel('T [°C]')
ylim([15 24])
title ('$y=T$','Interpreter','Latex');
legend([p1 p2 p3],'$T^{sp}$','Different setpoints','SRC','Interpreter','Latex', 'Orientation','vertical', 'Location','Southwest','FontSize', 17);
hold off
legend boxoff 

subplot(3,1,3)
p1=plot(tplot,Room3Ctrl_uAC,'Color',[    0.3010    0.7450    0.9330]);
hold on
p2=plot(tplot,Room3Ctrl_uHW,'Color', [ 0.9290    0.6940    0.1250] );
p3=plot(tplot,Room3Ctrl_uEH ,'r');
p4=plot(tplot2,RoomSRC_uEH,'--','Color', [128/250 0 0]);
plot(tplot2,RoomSRC_uHW,'--','Color',[255/255 140/250 0]);
plot(tplot2,RoomSRC_uAC,'--','Color',[25/250 25/250 112/250]);
plot(tplot,Room3Ctrl_ACmaxplot,'--', 'Color',[0.3010    0.7450    0.9330],'Linewidth', 1)
plot(tplot,Room3Ctrl_Qminplot,'--', 'Color',[0.5 0.5 0.5],'Linewidth', 1)
plot(tplot,Room3Ctrl_HWmaxplot,'--', 'Color',[ 0.9290    0.6940    0.1250],'Linewidth', 1)
plot(tplot,Room3Ctrl_EHmaxplot,'r--','Linewidth', 1)

ylabel('$Q_{i}$ [kW]','Interpreter','Latex');
title('$u_i=Q_{i}$','Interpreter','Latex');
ylim([-0.2 Room3Ctrl_ACmax+0.2])
ytickformat('%.0f')
%legend('$\dot{Q}_{ac}$','$\dot{Q}_{ac}^{max}$','Interpreter','Latex');
legend([p1 p2 p3 p4],'$Q_{AC}$','$Q_{HW}$','$Q_{EH}$','SRC','Interpreter','Latex', 'Location','East', 'Orientation','vertical', 'FontSize', 15);
hold off
legend boxoff 
xlabel(strcat('time [',timescale,']'))
%%
% figure(8)
% plot(tplot,Room3Ctrl_Jtot, 'Color', [ 0.4940    0.1840    0.5560])
% hold on
% plot(tplot2,RoomSRC_Jtot, '--','Color', [   0    0.7500    0.7500]); %, 'Linewidth',1.5)
% %plot(tplot,Room3Ctrl_Tdes_sp,'--','Linewidth', 1,'Color', [ 0.7500         0    0.7500] )
% ylabel('Accumulated cost [$]')
% legend('3 Controllers', 'SRC','Interpreter','Latex','Orientation','vertical');
% hold off
% legend boxoff 
% %ylim([15 30])
% xlabel(strcat('time [',timescale,']'))
%%

E3Ctrl_AC= Room3Ctrl_E_AC/3600; %kWh
E3Ctrl_HW= Room3Ctrl_E_HW/3600; %kWh
E3Ctrl_EH= Room3Ctrl_E_EH/3600; %kWh

ERoomSRC_AC= RoomSRC_E_AC/3600; %kWh 
ERoomSRC_HW= RoomSRC_E_HW/3600; %kWh
ERoomSRC_EH= RoomSRC_E_EH/3600; %kWh

%Prices
p_AC=0.4;
p_HW=0.8;
p_EH=1.2;
P3Ctrl_AC= p_AC*E3Ctrl_AC; %kWh
P3Ctrl_HW= p_HW*E3Ctrl_HW; %kWh
P3Ctrl_EH= p_EH*E3Ctrl_EH; %kWh

PRoomSRC_AC= p_AC*ERoomSRC_AC; %kWh 
PRoomSRC_HW= p_HW*ERoomSRC_HW; %kWh
PRoomSRC_EH= p_EH*ERoomSRC_EH; %kWh

PTot3Ctrl=P3Ctrl_AC+P3Ctrl_HW+P3Ctrl_EH;
PTotSRC=PRoomSRC_AC+PRoomSRC_HW+PRoomSRC_EH;

figure(9)
plot(tplot,PTot3Ctrl, 'Color', [ 0.4940    0.1840    0.5560])
hold on
plot(tplot2,PTotSRC, '--','Color', [   0    0.7500    0.7500]); %, 'Linewidth',1.5)
%plot(tplot,Room3Ctrl_Tdes_sp,'--','Linewidth', 1,'Color', [ 0.7500         0    0.7500] )
ylabel('Accumulated $J_{energy}$ [\$]','Interpreter','Latex')
legend('Different setpoints', 'SRC','Interpreter','Latex','Orientation','vertical');
hold off
legend boxoff 
%ylim([15 30])
xlabel(strcat('time [',timescale,']'))