% Task  : Transformed MVs. Mixing example
% Date  : 22 June 2021
% Author: CZ
% TODO  : -
% Var   : MVs=[q1;q2]; CVs=[q;T]; DVs=[T1;T2].

SetUpSimulations % initialize values
SetUpPlots       % set up plotting
%% Flow controller tuning
taucq  = 2;                                % [s]. closed loop time constant
KIq    = 1/(taucq);                                         % integral gain
vqbias = q0;                                        %[kg/s] controller bias


%% Simulate with static and dynamic transformations
for i = [0,1]
     transf = i; % 0 static; 1-dynamic
     
     switch transf
          case 0 % static
               kT     = q0/V;
               tauT   = V/q0;
               vTbias = T0;
               n = 1; % how much faster than the residence time
          case 1 % dynamic
               kT     = -B/A;  % gain of transformed system for T
               tauT   = -1/A;     % time constant of the transformed system
               vTbias = -A/B*T0;                   % controller bias for TC
               n = 4; % how much faster than the residence time
          otherwise
               disp('Incorect transformation')
     end
     
     taucT  = V/q0/n;    % [s]. closed loop time constant for  TC
     KcT    = 1/kT*tauT/(taucT);       % proportional gain for TC
     tauIT  = min(-1/A, 4*(taucT));        % integral time for TC
     
     p   = [V;A;B; transf];                               % vector of parameters
     
     if OL == 1 % simulation without outter controller
          vqbias_step =  qstep;
          vTbias_step =  Tstep;
     else % simulation with outer controller
          vqbias_step = vqbias;
          vTbias_step = vTbias;
     end
     
     sim('simTransformedMVs')
     
     %% Plot results
     figure(1)
     plot(time,d(:,1),'Color','blue')
     
     
     figure(2)
     plot(time,d(:,2),'Color','red')     
     
     
     figure(3)
     plot(time, q,'Color',clr(i+1,:),'LineStyle',lin{i+1})
     hold on
     
     
     figure(4)
     plot(time, T,'Color',clr(i+1,:),'LineStyle',lin{i+1})
     hold on
     
     
     figure(5)
     plot(time,v(:,1),'Color',cherry)
     
     
     
     figure(6)
     plot(time,v(:,2),'Color',cherry)
     
     
     
     figure(7)
     plot(time,uplant(:,1),'Color',clr(i+1,:),'LineStyle',lin{i+1})
     hold on
     ylim([1.9 10.1])
     
     
     figure(8)
     plot(time,uplant(:,2),'Color',clr(i+1,:),'LineStyle',lin{i+1})
     hold on
     
     
end
var = {'d1','d2','y1','y2','v1','v2','u1','u2'};

figure(1);
ylim([19 31])
legend('d_1=T_1', 'Location','best')
ylabel('Temperature T_1 [^oC]')

figure(2)
ylim([49 61])
legend('d_2=T_2', 'Location','best')
ylabel('Temperature T_2 [^oC]')

figure(3)
ylim([9 15])
plot(time,qsp,'--','Color',cherry)
legend('y_1 = F (static v_0)','y_1 = F (dynamic v_A)','y_1^s = q^s', 'Location','SouthEast')
ylabel('Flowratw q [kg s^{-1}]')

figure(4)
plot(time, Tsp,':','Color',cherry)
legend('y_2=T (static v_0)','y_2=T (dynamic v_A)' ,'y_2^s = T^s', 'Location','best')
ylim([34 46])
ylabel('Temperature T [^oC]')

figure(5)
ylim([9 15])
legend('v_{01}^s','Location','northeast')
ylabel('Tranformed input')

figure(6)
ylim([34 54])
legend('v_{2}^s','Location','northeast')
ylabel('Tranformed input ')

figure(7)
ylim([1 12])
legend('u_1 = F_1 (static v_0)','u_1 = F_1 (dynamic v_A)','Location','northeast')
ylabel('Flowrate F_1 [kg s^{-1}]')

figure(8)
ylim([1 12])
legend('u_2 = F_2 (static v_0)','u_2 = F_2 (dynamic v_A)','Location','southeast')
xlabel('Time')
ylabel('Flowrate F_2 [kg s^{-1}]')

for i = 1:8
     figure(i)
     xlabel('Time [s]')
     filename = strcat('mixTe_',var{i},'.eps');
     legend boxoff
     set(gcf,'Position',pos)
     print('-depsc2','-r600',filename)
end