% Lab controller options
% Gisle Otto Eikrem
% 22.08.2003


if controller_type == 'openloop',
     
 if i < C1,
     u_PC_simple(i) = 0.8; %0.8
 elseif (i >= C1)&(i < C2),
     u_PC_simple(i) = 0.8; %0.8
 else
     u_PC_simple(i) = 0.8; %0.8
 end
 
 which_controller = 'openloop'; % variable to check which controller has been applied.    
end % controller_type == 'openloop',


if controller_type == 'dhpcontr',
    
% Run two controllers in parallell
    % * One to control the simplified gas-lift model
    % * One to control the OLGA 2000 model    
    
% Control of the simplified model

dhp_set_simple(i) = 2; %0.85; % barg - DHP pressure setpoint    
e_simple(i) = dhp_set_simple(i) - p_bottom_riser_bara(max(1,i-1)); % bar - error 

Kp_simple = -2; %-1; %-1; %-0.2; %-0.2;
Ti_simple = 400; %200 %100;
    
% if i > C4,
%     W(3,3) = 1e9; % DHP fallout
% end
 
 if i < C0,
     u_PC_simple(i) = 0.4;
 elseif i < C1,
     u_PC_simple(i) = 0.8;
 elseif (i >= C1)&(i < C2),
     u_PC_simple(i) = u_PC_simple(i-Num) + Kp_simple*((e_simple(i)-e_simple(i-Num))+Ts*e_simple(i)/Ti_simple);
 else
     u_PC_simple(i) = u_PC_simple(i-Num);
 end
 
%  % Control of the olga model
% 
% dhp_set_olga(i) = 1.2; % barg - DHP pressure setpoint    
% e_olga(i) = dhp_set_olga(i) - DHP_olga_barg(i-1); % bar - error 
% e_olga(i:i+Num-1) = e_olga(i);
% dhp_set_olga(i:i+Num-1) = dhp_set_olga(i);
% 
% Kp_olga = -0.2;
% Ti_olga = 100;
%     
% % if i > C4,
% %     W(3,3) = 1e9; % DHP fallout
% % end
%  
%  if i < C1,
%      u_PC_olga(i) = 0.5;
%  elseif (i >= C1)&(i < C2),
%      u_PC_olga(i) = u_PC_olga(i-Num) + Kp_olga*((e_olga(i)-e_olga(i-Num))+Ts_olga*e_olga(i)/Ti);
%  else
%      u_PC_olga(i) = u_PC_olga(i-Num);
%  end
 
 which_controller = 'dhpcontr'; % variable to check which controller has been applied.    
end % controller_type == 'openloop',

% Give the same valve opening to the next n iteration steps

   %Preventing integrator wind-up
   if u_PC_simple(i) > 1.0
     	u_PC_simple(i) = 1.0;
   elseif u_PC_simple(i) < 0.0
       u_PC_simple(i) = 0.0;
   end
   
u_PC_simple(i+1:i+Num-1) = u_PC_simple(i);

