function [J eta ph] = obj_prico_temp(x,d,par)

% Calculate objective function for optimization of PRICO model where
% temperatures are specified.

global h

hyCase = h.Activedocument;
sol=hyCase.Solver;
f = hyCase.Flowsheet;

d0 = par.d0 ;

sol.CanSolve = 0 ;
f.Operations.Item('RCY-1').IsIgnored = 1 ; % Make sure recycle is ignored

% Disturbances: Feed conditions and cooling temperature
f.MaterialStreams.Item('MR4').TemperatureValue = d(1) ; 
f.MaterialStreams.Item('NG1').TemperatureValue = d(1) ; % Assume MR and NG are cooled to the same temperature
f.MaterialStreams.Item('NG1').PressureValue = d(2) ; 
f.MaterialStreams.Item('NG1').MolarFlowValue = d(3)*d0(3); 



% Inputs

x = par.scalex\x ;

f.MaterialStreams.Item('MR1').TemperatureValue = x(1) ; % Temperature guess for recycle (C)
f.MaterialStreams.Item('MR5').TemperatureValue = x(2) ; % Temperature of hot MR after HX (C)
f.MaterialStreams.Item('MR7').PressureValue = x(3) ; % Pressure after MR turbine (kPa)
f.MaterialStreams.Item('MR9').PressureValue = x(4) ; % Pressure before compressor (kPa)
f.Operations.Item('K-100').CompressorSpeedValue = x(5) ; % Compressor speed (NB! Hz, not rpm)
f.MaterialStreams.Item('MR1').MolarFlowValue = x(6) ;
       

sol.CanSolve = 1 ;


if f.Operations.Item('K-100').Energy.IsKnown == 1 && f.Operations.Item('HX').UA.IsKnown == 1
    W = f.Operations.Item('K-100').EnergyValue ; % Compressor work is our objective function
    J = 1e-5*W ; % Scale the objective function
    eta = f.Operations.Item('K-100').CompAdiabaticEffValue ;
    ph = f.MaterialStreams.Item('MR3').PressureValue ;
else
    J = 1e3 ;
    eta = NaN ;
    ph = NaN ;
end
    
    
