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

% Topology to be investigated:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%            1     2             %
%        ----0-----0----         %
%   ----|               |----    %
%        -------0-------         %
%               3                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all;
close all;
clc;

% Calling parameters from Data.m
[T0,Th1,Th2,Th3...
            m0,m1,m2,mh1,mh2,mh3...
            rho_0, hc, Cp0,...
            Vwall, rho_wall, Cp_wall,...
            filterk, filtert,...
            P1, P2, P3] = Data;
 

% SIMULINK FILE FOR SIMULATION WITH THE MODIFIED CV        
% sim('dynamic_21_1_1')

% SIMULINK FILE FOR SIMULATION WITH THE ORIGINAL CV
sim('dynamic_21_1')



% % TUNING OF CONTROLLER
% % 10% STEP CHANGE INLET MASS FLOW COLD STREAM
% % TUNING PLOT
% t0 = 800;
% tend = 1800;
% 
% cv1_0 = 1e7;
% cv1_end = 3e7;
% cv1_step = 0.5e7;
% 
% m1_0 = 28;
% m1_end = 34;
% m1_step = 1;
% 
% k = figure;
% [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_21_numJT.eps');


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

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



% PLOTING THE RESULTS

t0 = 800;
tend = 2000;

cv1_0 = -5;
cv1_end = 5;
cv1_step = 5;


% % RESULTS FOR THE CASE WITH COOLING HX (MOD. CV)

% % TEMPERATURE PROFILES W/ COOLING TH2
% h = figure;
% figure(1)
% plot(t,T1,t,T2,t,Th2_d,'LineWidth',2)
% xlabel('Time [sec]','fontsize',12);
% ylabel('Temperature [ \circC]','fontsize',12);
% legend('T_{1,1}','T_{2,1}','Th_{2,1}')
% axis([t0 tend 170 260])
% grid on
% % print(h,'-depsc','T_coolHX2_numJT_Tune1.eps');
% 
% % SPLIT PROFILE W/ COOLING TH2
% j = figure;
% figure(2)
% plot(t,u1,'LineWidth',2)
% xlabel('Time [sec]','fontsize',12);
% ylabel('Split u','fontsize',12);
% % legend('T1','T2','Th2')
% axis([t0 tend 0 1])
% grid on
% % print(j,'-depsc','Split_coolHX2_numJT_Tune1.eps');


% % RESULTS FOR THE ORIGINAL CASE (ORG. CV)

% % CONTROLLED VARIABLE PROFILE WITHOUT FILTER
% k = figure;
% % figure(3)
% plot(t,cv1,'LineWidth',2)
% % h=BreakXAxis(t,cv1,-1e7,-5000,1000);
% % legend('Without AF','With AF')
% % title('CV (J1-J2)')
% xlabel('Time [sec]','fontsize',12);
% ylabel('Mod. control variable, JT [^{\circ}C^4]','fontsize',12)
% axis([t0 tend cv1_0 cv1_end])
% grid on
% print(k,'-depsc','CV_coolHX2_fullplot_Tune2.eps');


% % SPLIT WITHOUT FILTER
% % figure(3)
% i = figure;
% plot(t,u1,'LineWidth',2)
% % legend('Without AF','With AF')
% % title('CV (J1-J2)')
% xlabel('Time [sec]','fontsize',12)
% ylabel('Split u (Upper path)','fontsize',12)
% axis([t0 tend 0.1 0.8])
% grid on
% print(i,'-depsc','Split_21.eps');



% CONTROLLED VARIABLE PROFILE WITH FILTER
l = 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(l,'-depsc','CV_filter_21.eps');

% SPLIT WITH FILTER
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.601])
grid on
% print(i,'-depsc','Split_filter_21.eps');

% TEMPERATURE PROFILES WITH FILTER
j = figure;
plot(t,T1_AF,t,T2_AF,t,T3,t,Tend_AF,'LineWidth',2)
xlabel('Time[sec]','fontsize',12)
ylabel('Temperature [^{\circ}C]','fontsize',12)
axis([t0 tend 160 210])
legend('T_{1,1}','T_{2,1}','T_{1,2}','T_{end}')
grid on
% print(j,'-depsc','T_21.eps');
