%The contraints in mymodel.usc are established. There are constrains in all
%the heat exchangers, superheating, temperatures after air cooling,
%precooled NG and MR temperatures

function [c,ceq] = mycon(u)

% Establishing access to the UniSim Design Server and mymodel.usc
h = actxserver('UnisimDesign.Application');
hyCase = h.Activedocument ;
solv = hyCase.Solver ;
f = hyCase.Flowsheet ;
m = f.MaterialStreams ;
global scale1;
x = u.*scale1;
 
solv.CanSolve = 0 ;

m.Item('MR_1').PressureValue = x(1); %kPa
m.Item('MR_2').PressureValue = x(2);
m.Item('MR').MolarFlowValue = x(3); %kmole/s
m.Item('MR_2*').MolarFlowValue = x(4);
m.Item('MR_3*').MolarFlowValue = x(5);
m.Item('NG1').TemperatureValue = x(6);
m.Item('MR1').TemperatureValue = x(7);
m.Item('NG2').TemperatureValue = x(8);
m.Item('MR2').TemperatureValue = x(9);
m.Item('NG3').TemperatureValue = x(10);
m.Item('MR3').TemperatureValue = x(11);

solv.CanSolve = 1;  

%Equality constraints (UA-constraints) for Operation Case
ceq = 1e-13*[
            
    f.Operations.Item('LNG').Cell('D5').CellValue  - 8497454  %LNG-100 MR 
    f.Operations.Item('LNG').Cell('D6').CellValue  - 17550335  %LNG-200 MR
    f.Operations.Item('LNG').Cell('D7').CellValue  - 18873920  %LNG-300 MR 
    f.Operations.Item('LNG').Cell('E5').CellValue  -  1293992  %LNG-100 NG 
    f.Operations.Item('LNG').Cell('E6').CellValue  -  2623097  %LNG-200 NG 
    f.Operations.Item('LNG').Cell('E7').CellValue  -  3126706  %LNG-300 NG
     ];

a=1e-3;

%Inequality constraints
c = [
    a*f.Operations.Item('SUPCON').Cell('B4').CellValue   %K-100
    a*f.Operations.Item('SUPCON').Cell('C4').CellValue   %K-200
    a*f.Operations.Item('SUPCON').Cell('D4').CellValue   %K-300
    
    -f.Operations.Item('LNG-100').MinApproachValue
    -f.Operations.Item('LNG-200').MinApproachValue
    -f.Operations.Item('LNG-300').MinApproachValue  
    ];
    
    
    
