% Supervisory control structure for economics
% Tuning done using sequential controller tuning
% Use of SIMC for tuning the loop
function xprime = dynamic_Petlyuk(t,X) 
%This model is for simulations
%This is a dynamic model with PI controllers, It returns derivative  
% using the basic non-linear model Petlyuk_dxdt and it can be used for both
%steady-state simulation as well as dynamic simulations
%At steady state au=0 (except for condensers and reboiler holdups as they
%don't have steady-state effect)

%4. for dynamic simulation  : script file dynamic_Petlyuk.m
global UU dyndata tend NI i
indmat_Yes_S1
Tboil=coldata.Tboil;
%better approximation for step size with some algorithm write in further
%work or discussion
%Delta1 = 1e-6;           %uncomment and use this perturbation in L and S1 to obtain Juu,
%
%Delta2= 1e-5;            %uncomment and use this perturbation in Rl to obtain Juu
     
     Xp1 =  X(II1);
     Xp2 =  X(II2);
     Xp3 =  X(II3);
     Xp4 =  X(II4);
     Xp5 =  X(II5);
     Xp6 =  X(II6);
     Xp1=[Xp1 1-sum(Xp1,2)];    % compositions A, B and C in section 1
     Xp2=[Xp2 1-sum(Xp2,2)];    % compositions A, B and C in section 2
     Xp3=[Xp3 1-sum(Xp3,2)];    % compositions A, B and C in section 3
     Xp4=[Xp4 1-sum(Xp4,2)];    % compositions A, B and C in section 4
     Xp5=[Xp5 1-sum(Xp5,2)];    % compositions A, B and C in section 5
     Xp6=[Xp6 1-sum(Xp6,2)];    % compositions A, B and C in section 6
     Xp=[Xp1;Xp2;Xp3;Xp4;Xp5;Xp6];% Compositions A, B and C in the column starting
                                  % from section 1  and ending at section6 
     Temp=Xp*Tboil';              % Temperatures on all stages sections (1,2,3,4,5,6)
     

% D and B used to control hold ups in the condenser (X(im_mt)) and reboiler
%  (X(im_mr))respectively

[D,e1] = Controller_PI(X(im_mt),dyndata.x_nom(im_mt),dyndata.u_nom(end-1),X(im_mS1+1),1*0,1001,0,2);
[B,e2] = Controller_PI(X(im_mr),dyndata.x_nom(im_mr),dyndata.u_nom(end),X(im_mS1+2),1*0,1001,0,2);

au=0;
%Gy can be calculated here by giving step change in inputs (L, S and Rl) here.
[L,e3] = Controller_PI(Temp(38),(dyndata.temp_nom(38)),...
            dyndata.u_nom(1)+0.01,X(im_mS1+3),0.48*au/2,40*2,0,4);

% step change in L by adding 1e-6
%[L,e3] = Controller_PI(log(X(im_xt(2))),log(dyndata.x_nom(im_xt(2))),...
           %dyndata.u_nom(1)-Delta1,X(im_mS1+3),0.48*au/2,40*2,0,4);
      
[S1,e4] = Controller_PI(Temp(42),dyndata.temp_nom(42),...
            dyndata.u_nom(5),X(im_mS1+4),88*au/8,40*8,0,4);
% step change in S1, i.e.dyndata.u_nom(5)+1e-6
%[S1,e4] = Controller_PI(log((X(im_xS1(2)))),log((dyndata.x_nom(im_xS1(2)))),...
            %dyndata.u_nom(5)-Delta1,X(im_mS1+4),88*au/8,40*8,0,4);
        
[Rl,e5] = Controller_PI(log(1-sum(X(im_x1([1 1+coldata.Nt(1)])))),...
            log(1-sum(dyndata.x_nom(im_x1([1 1+coldata.Nt(1)])))),...
            dyndata.u_nom(3)+(1e-5),X(im_mS1+5),-0.333*au,40,0,1);
% step change in Rl, i.e.dyndata.u_nom(3)+1e-3
%[Rl,e5] = Controller_PI(Temp(6),...
            %dyndata.temp_nom(6),...
            %dyndata.u_nom(3)+Delta2,X(im_mS1+5),-0.333*au,40,0,1);
 
VBB = dyndata.u_nom(2);              %Boilups if fixed
RV = dyndata.u_nom(4);               % RV is fixed

% loop to obtain the values of i/p and disturbances throughout simulation
% time
if(t>0.01 || rem(i,10)==0) || t==tend
%     t
UU=[UU; t [L;VBB;Rl;RV;S1;dyndata.u_nom(6:9); D; B]'];

end
i=i+1;
%     t

xprime =Petlyuk_dxdt(t,X(1:end),[L;VBB;Rl;RV;S1;dyndata.u_nom(6:9); D; B ]);
xprime=[xprime;e1;e2;e3;e4;e5];
