% RUN FILE FOR DYNAMIC SIMULATION OF THE 3:2 HEN

% Topology to be investigated:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%            1     2     3           %
%        ----0-----0-----0----       %
%   ----|                     |----  %
%        -------0------0------       %
%               4      5             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all;
close all;
clc;

% Calling parameters from Data.m file
[T0,Th1,Th2,Th3,Th4,Th5,...
            m0,m1,m2,mh1,mh2,mh3,mh4,mh5,...
            rho_0,hc,Cp0,...
            Vwall,rho_wall,Cp_wall,...
            P1,P2,P3,P4,P5] = Data;
 

        
sim('dynamic_32')


% % TUNING OF CONTROLLER
% % 10% STEP CHANGE INLET MASS FLOW COLD STREAM
% % TUNING PLOT
% t0 = 800;
% tend = 2000;
% 
% cv1_0 = -5;
% cv1_end = 10;
% cv1_step = 3;
% 
% m1_0 = 16;
% m1_end = 20;
% m1_step = 1;
% 
% [AX,H1,H2] = plotyy(t,cv1,t,m1);
% set(get(AX(1),'Ylabel'),'String','Controlled variable, JT [^{\circ}C]','fontsize',12)
% set(get(AX(2),'Ylabel'),'String','Mass flow m_1 to upper path [kg/sec]','fontsize',12)
% axis(AX(1),[t0 tend cv1_0 cv1_end]);
% axis(AX(2),[t0 tend m1_0 m1_end]);
% set(AX(1),'YLim',[cv1_0 cv1_end])
% set(AX(1),'YTick',cv1_0:cv1_step:cv1_end)
% set(AX(2),'YLim',[m1_0 m1_end])
% set(AX(2),'YTick',m1_0:m1_step:m1_end)
% xlabel('Time [sec]','fontsize',12)
% set(H1,'linewidth',2)
% set(H2,'linewidth',2)
% grid on
% print(k,'-depsc','tune_32.eps');


% IMPLEMENTING FILTERS - SIMULATING BEHAVIOR WITH AND WITHOUT FILTE
% % Without Filter
% cv1_noAF = cv1;
% u1_noAF = u1;
% T1_noAF = T1;
% T2_noAF = T2;
% Tend_noAF = Tend;
% 
% save no_filter

% % With Filter
% cv1_AF = cv1;
% u1_AF = u1;
% T1_AF = T1;
% T2_AF = T2;
% T3_AF = T3;
% T4_AF = T4;
% T5_AF = T5;
% Tend_AF = Tend;
% 
% save filter


% PLOTING THE RESULTS

t0 = 800;
tend = 3000;

cv1_0 = -0.5;
cv1_end = 3;
cv1_step = 0.1;


% CONTROLLED VARIABLE PROFILE
k = figure;
plot(t,cv1_noAF,'b',t,cv1_AF,'r','LineWidth',2)
legend('Without filter','With filter')
xlabel('Time [sec]','fontsize',12);
ylabel('Controlled variable, JT [^{\circ}C]','fontsize',12)
axis([t0 tend cv1_0 cv1_end])
grid on
% print(k,'-depsc','CV_32.eps');

% SPLIT
i = figure;
plot(t,u1_noAF,'b',t,u1_AF,'r','LineWidth',2)
legend('Without filter','With filter')
xlabel('Time [sec]','fontsize',12)
ylabel('Split u (Upper path)','fontsize',12)
axis([t0 tend 0.3 0.38])
grid on
% print(i,'-depsc','Split_32.eps');

% TEMPERATURE PROFILES
j = figure;
plot(t,T1_AF,t,T2_AF,t,T3_AF,t,T4_AF,t,T5_AF,t,...
    Tend_AF,'LineWidth',2)
xlabel('Time[sec]','fontsize',12)
ylabel('Temperature [^{\circ}C]','fontsize',12)
axis([t0 tend 145 195])
legend('T_{1,1}','T_{2,1}','T_{3,1}','T_{1,2}','T_{2,2}','T_{end}')
grid on
% print(j,'-depsc','T_32.eps');
