% Script use to plot the results
% The c
%% Open loop response for step in cold valve
clc
clear
close all
load('outputOL-cold')

Thout = Th(:,end)-273.15;
Fcmax = 10; Fhmax = 3;
Fc = u1*Fcmax; % cold valve
Fh = u2*Fhmax; % hot valve

figure('Name','OL')
a(1) = subplot(2,2,1);
plot(t,Fc,'b','Linewidth',1)
hold on
plot(t,Fh,'r','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('Mass flow (kg/s)','Interpreter','latex');
xlim([0 600])
ylim([2 6])
legend('$F_C$','$F_H$','Location','best','Orientation','horizontal')
legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
ax.Legend.Interpreter = 'latex';


%figure('Name','OL-cold')
% plot(t,Tc,'k--','Linewidth',1)
% hold on
subplot(2,2,3);
plot(t,Thout,'r','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('Temperature, $T_H$ ($^\circ$C)','Interpreter','latex');
xlim([0 600])
ylim([298 300]-273.15)
% legend('cold','hot')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
%ax.Legend.Interpreter = 'latex';
strTC = {'$K_c=-0.055$','$\tau=74$','$\theta=44$'};
annTC = annotation('textbox',[0.2815    0.2792    0.1955    0.1783],'String',strTC,'FitBoxToText','on','FontSize',12,'Color','k');
annTC.LineStyle = 'none';
annTC.Interpreter = 'latex';

%% Open loop response for step in hot valve
clc
clear
%close all
load('outputOL-hot')

Thout = Th(:,end)-273.15;
Fcmax = 10; Fhmax = 3;
Fc = u1*Fcmax; % cold valve
Fh = u2*Fhmax; % hot valve

%figure('Name','OL-hot')
subplot(2,2,2);
plot(t,Fc,'b','Linewidth',1)
hold on
plot(t,Fh,'r','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('Mass flow (kg/s)','Interpreter','latex');
xlim([0 600])
ylim([2 6])
legend('$F_C$','$F_H$','Location','best','Orientation','horizontal')
legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
ax.Legend.Interpreter = 'latex';

%figure('Name','OL-hot')
% plot(t,Tc,'k--','Linewidth',1)
% hold on
subplot(2,2,4);
plot(t,Thout,'r','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('Temperature, $T_H$ ($^\circ$C)','Interpreter','latex');
xlim([0 600])
ylim([298 300]-273.15)
%legend('cold ','hot ')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
%ax.Legend.Interpreter = 'latex';
strTC2 = {'$K_c=0.0803$','$\tau=86$','$\theta=7$'};
annTC2 = annotation('textbox',[0.7408 0.2792 0.1878 0.1783],'String',strTC2,'FitBoxToText','on','FontSize',12,'Color','k');
annTC2.LineStyle = 'none';
annTC2.Interpreter = 'latex';

%% Open loop response for step in hot valve with cold valve in close loop.
clc
clear
%close all
load('outputOL-hot-cold')


Fcmax = 10; Fhmax = 3;
Fc = u1*Fcmax; % cold valve
Fh = u2*Fhmax; % hot valve

figure('Name','OL-hot')

plot(t,Fc,'b','Linewidth',1)
hold on
plot(t,Fh,'r','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('Mass flow (kg/s)','Interpreter','latex');
xlim([0 600])
ylim([2 6])
legend('$F_C$','$F_H$','Location','best','Orientation','horizontal')
legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
ax.Legend.Interpreter = 'latex';
strVPC = {'$K_c=3$','$\tau=77$','$\theta=12$'};
annVPC = annotation('textbox',[0.6403    0.6107    0.1878    0.1783],'String',strVPC,'FitBoxToText','on','FontSize',12,'Color','k');
annVPC.LineStyle = 'none';
annVPC.Interpreter = 'latex';

%% Split range control
clc
%close all
clear 

load('outputSRC.mat')

Thout = Th(:,end)-273.15;
Fcmax = 10; Fhmax = 3;
Fc = u1*Fcmax; % cold valve
Fh = u2*Fhmax; % hot valve

figure('Name','SRC')
subplot(2,1,1)
plot(t,Fc,'b','Linewidth',1)
hold on
plot(t,Fh,'r','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('Mass flow (kg/s)','Interpreter','latex');
%xlim([0 600])
%ylim([0.3 1.1])
legend('$F_C$','$F_H$','Location','best','Orientation','horizontal')
legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
ax.Legend.Interpreter = 'latex';

%figure('Name','SRC')
% plot(t,Tc,'k--','Linewidth',1)
% hold on
subplot(2,1,2)
plot(t,Thout,'r','Linewidth',1)
hold on
plot(t,ones(length(t),1)*ThSP-273.15,'r--','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('Temperature ($^\circ$C)','Interpreter','latex');
%xlim([0 600])
ylim([298 303]-273.15)
legend('$T_H$','$T_{H,sp}$','Location','best','Orientation','horizontal')
legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
ax.Legend.Interpreter = 'latex';

%% Input resetting

clc
%close all
clear 

load('outputIR.mat')

Thout = Th(:,end)-273.15;
Fcmax = 10; Fhmax = 3;
Fc = u1*Fcmax; % cold valve
Fh = u2*Fhmax; % hot valve


figure('Name','IR')
subplot(2,1,1)
plot(t,Fc,'b','Linewidth',1)
hold on
plot(t,Fh,'r','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('Mass flow (kg/s)','Interpreter','latex');
%xlim([0 600])
%ylim([0.3 1.1])
legend('$F_C$','$F_H$','Location','best','Orientation','horizontal')
legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
ax.Legend.Interpreter = 'latex';

%figure('Name','IR')
% plot(t,Tc,'k--','Linewidth',1)
% hold on
subplot(2,1,2)
plot(t,Thout,'r','Linewidth',1)
hold on
plot(t,ones(length(t),1)*ThSP-273.15,'r--','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('Temperature ($^\circ$C)','Interpreter','latex');
%xlim([0 600])
ylim([298 303]-273.15)
legend('$T_H$','$T_{H,sp}$','Location','best','Orientation','horizontal')
legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
ax.Legend.Interpreter = 'latex';

%% 2 controllers with different set points
clc
%close all
clear 

load('output2C.mat')

Thout = Th(:,end)-273.15;
Fcmax = 10; Fhmax = 3;
Fc = u1*Fcmax; % cold valve
Fh = u2*Fhmax; % hot valve

figure('Name','2C')
subplot(2,1,1)
plot(t,Fc,'b','Linewidth',1)
hold on
plot(t,Fh,'r','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('Mass flow (kg/s)','Interpreter','latex');
%xlim([0 600])
%ylim([0.3 1.1])
legend('$F_C$','$F_H$','Location','best','Orientation','horizontal')
legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
ax.Legend.Interpreter = 'latex';

%figure('Name','2C')
% plot(t,Tc,'k--','Linewidth',1)
% hold on
subplot(2,1,2)
plot(t,Thout,'r','Linewidth',1)
hold on
plot(t,ones(length(t),1)*ThSP-273.15,'r--','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('Temperature ($^\circ$C)','Interpreter','latex');
%xlim([0 600])
ylim([298 303]-273.15)
legend('$T_H$','$T_{H,sp}$','Location','best','Orientation','horizontal')
legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
ax.Legend.Interpreter = 'latex';

%% Three ACS

clc
%close all
clear 

load('outputSRC.mat')
width=2;
Thout = Th(:,end)-273.15;
Fcmax = 10; Fhmax = 3;
Fc = u1*Fcmax; % cold valve
Fh = u2*Fhmax; % hot valve

figure('Name','ACS')
subplot(3,1,1)%FC
plot(t,Fc,'r','Linewidth',width)
hold on
xlabel('Time (s)', 'Interpreter','latex');
ylabel('$F_C$ (kg/s)','Interpreter','latex');
%xlim([0 600])
%ylim([0.3 1.1])
%legend('$SRC$','Location','best','Orientation','horizontal')
%legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
%ax.Legend.Interpreter = 'latex';


subplot(3,1,2) % FH
plot(t,Fh,'r','Linewidth',width)
hold on
xlabel('Time (s)', 'Interpreter','latex');
ylabel('$F_H$ (kg/s)','Interpreter','latex');
%xlim([0 600])
%ylim([0.3 1.1])
%('$SRC$','Location','best','Orientation','horizontal')
%legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
%ax.Legend.Interpreter = 'latex';


%figure('Name','SRC')
% plot(t,Tc,'k--','Linewidth',1)
% hold on
subplot(3,1,3) %FH
plot(t,Thout,'r','Linewidth',width)
hold on
%plot(t,ones(length(t),1)*ThSP-273.15,'r--','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('$T_H$ ($^\circ$C)','Interpreter','latex');
%xlim([0 600])
ylim([298 303]-273.15)
%legend('$SRC$','$T_H^{sp}$','Location','best','Orientation','horizontal')
%legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
%ax.Legend.Interpreter = 'latex';

%Two controllers
clear 
load('output2C.mat')
width=2;
Thout = Th(:,end)-273.15;
Fcmax = 10; Fhmax = 3;
Fc = u1*Fcmax; % cold valve
Fh = u2*Fhmax; % hot valve

%figure('Name','ACS')
subplot(3,1,1)%FC
plot(t,Fc,'k--','Linewidth',width)
hold on
xlabel('Time (s)', 'Interpreter','latex');
ylabel('$F_C$ (kg/s)','Interpreter','latex');
%xlim([0 600])
%ylim([0.3 1.1])
%legend('$2 Controllers$','Location','best','Orientation','horizontal')
%legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
%ax.Legend.Interpreter = 'latex';


subplot(3,1,2) % FH
plot(t,Fh,'k','Linewidth',width)
hold on
xlabel('Time (s)', 'Interpreter','latex');
ylabel('$F_H$ (kg/s)','Interpreter','latex');
%xlim([0 600])
%ylim([0.3 1.1])
%legend('$SRC$','Location','best','Orientation','horizontal')
%legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
%ax.Legend.Interpreter = 'latex';


%figure('Name','SRC')
% plot(t,Tc,'k--','Linewidth',1)
% hold on
subplot(3,1,3) %FH
plot(t,Thout,'k','Linewidth',width)
hold on
plot(t,ones(length(t),1)*ThSP-273.15,'k--','Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('$T_H$ ($^\circ$C)','Interpreter','latex');
%xlim([0 600])
ylim([298 303]-273.15)
%legend('$SRC$','$T_H^{sp}$','Location','best','Orientation','horizontal')
%legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
%ax.Legend.Interpreter = 'latex';

%Input Resetting
clear 
load('outputIR.mat')
width=2;
Thout = Th(:,end)-273.15;
Fcmax = 10; Fhmax = 3;
Fc = u1*Fcmax; % cold valve
Fh = u2*Fhmax; % hot valve

%figure('Name','ACS')
subplot(3,1,1)%FC
plot(t,Fc,'Color', [50/255 205/255 50/255],'Linewidth',width)
hold on
xlabel('Time (s)', 'Interpreter','latex');
ylabel('$F_C$ (kg/s)','Interpreter','latex');
%xlim([0 600])
%ylim([0.3 1.1])
% legend('$SRC$', '$2C$', '$VPC$','Location','best','Orientation','horizontal')
% legend('boxoff')
% ax = gca;
% ax.FontSize = 14;
% ax.TickLabelInterpreter = 'latex';
% ax.Legend.Interpreter = 'latex';


subplot(3,1,2) % FH
plot(t,Fh,'Color', [50/255 205/255 50/255],'Linewidth',width)
hold on
plot(t,ones(length(t),1)*3,'--', 'Color', [138/255 43/255 226/255],'Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('$F_H$ (kg/s)','Interpreter','latex');
%xlim([0 600])
%ylim([0.3 1.1])
%legend('$SRC$', '$2C$', '$VPC$','Location','best','Orientation','horizontal')
legend('$SRC$', '$2~Controllers$', '$VPC$','Location','best')
legend('boxoff')
ax = gca;
ax.FontSize = 14;
ax.TickLabelInterpreter = 'latex';
ax.Legend.Interpreter = 'latex';


%figure('Name','SRC')
% plot(t,Tc,'k--','Linewidth',1)
% hold on
subplot(3,1,3) %FH
plot(t,Thout,'Color', [50/255 205/255 50/255],'Linewidth',width)
hold on 
plot(t,ones(length(t),1)*ThSP-273.15,'--', 'Color', [138/255 43/255 226/255],'Linewidth',1)
xlabel('Time (s)', 'Interpreter','latex');
ylabel('$T_H$ ($^\circ$C)','Interpreter','latex');
%xlim([0 600])
ylim([298 303]-273.15)
% legend('$SRC$', '$2C$', '$VPC$','Location','best','Orientation','horizontal')
% legend('boxoff')
% ax = gca;
% ax.FontSize = 14;
% ax.TickLabelInterpreter = 'latex';
% ax.Legend.Interpreter = 'latex';
% 
