% Setting up constraints
% Qpg<=Qsg(max)
% Qpw<=Qsw(max)
% Pw>=Pm

function [Cinq C] = nonlncon2(x)
%

Ps=20;
Qsg_max=3300;
Qsw_max= 1600;
%Qsg_max=3800;
%Qsw_max= 1800;

load welldata1

% VARIABLES FOR SELF OPTIMIZING
load optimizing_par1

y=([x(1) x(2) x(3) x(4) x(21) x(17) x(18) x(19)]'-[109.0000   85.5045   77.5209   63.9579  54.2464  3300    1055.5    1600]');
%y=([x(1) x(2) x(3) x(4) x(21) x(17) x(18) x(19)]'-[109.0000   81.9553   63.0727   81.9553  54.2464  3300    1055.5    1600]'); % Vector of variables

%well1=[well1(:,1) well1(:,2) 0.93*well1(:,3) well1(:,4)];         % Well1 - implement -7% disturbance in oil flow.
%well2=[well2(:,1) well2(:,2) 0.93*well2(:,3) well2(:,4)];         % Well2 - implement -7% disturbance in oil flow.
%well3=[well3(:,1) well3(:,2) 0.93*well3(:,3) well3(:,4)];         % Well3 - implement -7% disturbance in oil flow.
%well4=[well4(:,1) well4(:,2) 0.93*well4(:,3) well4(:,4)];         % Well4 - implement -7% disturbance in oil flow.

%Qp=(wpcn1(x(1),well1) + wpcn2(x(2),well2)+ wpcn3(x(3),well3)+ wpcn4(x(4),well4)); %vector of composition in pipe [g o w]'
% Qpg=Qp(1);
% Qpw=Qp(3);

% dP=Ploss(Qp);          % Pressure loss model

% Pm= Ps - dP;  


Cinq(1)=x(21)-x(1);                           % Pressure constraints: Pw>=Pm
Cinq(2)=x(21)-x(2);
Cinq(3)=x(21)-x(3);
Cinq(4)=x(21)-x(4);
Cinq(5)=x(17)-Qsg_max;                       % Gas capacity constraint in separator
Cinq(6)=x(19)-Qsw_max;                       % Water Capacity constraint in separator



 
C(1:3)=x(5:7)'-wpcn1(x(1),well1);                            % Qw1
C(4:6)=x(8:10)'-wpcn2(x(2),well2);                           % Qw2
C(7:9)=x(11:13)'-wpcn3(x(3),well3);                          % Qw3
C(10:12)=x(14:16)'-wpcn4(x(4),well4);                        % Qw4
C(13:15)=x(17:19)'-(x(5:7)'+x(8:10)'+x(11:13)'+x(14:16)');   % Qp
C(16)=x(20)-Pdrop2(x(17:19)');                               % dP
C(17)=x(21)-(Ps+x(20));                                      % Pm
%C(18)=x(17)-Qsg_max;                                        % Gas capacity constraint in separator
%C(19)=x(19)-Qsw_max;                                        % Water Capacity constraint in separator

% Self optimizing
  %C(20)= c1*y-0;
  %C(21)= c2*y-0;

% Saturated well pressures
% C(22)=109-x(1);                           % Pressure constraints: Pw>=Pm
% C(23)=81.9553-x(2);
% C(24)=63.0727-x(3);
% C(25)=81.9552-x(4);

 
Qgow=[ x(17) x(18) x(19)]
disp(x(21))

