function [J message] = MCHE_obj_area(x,d,parameters)

% Calculate objective for optimization of MCHE model (MCHE_areaspecs.usc)
global h

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

sol.CanSolve = 0 ;

x = parameters.scale_x*x ;

%Reset optimization variables
f.MaterialStreams.Item('MR10').PressureValue = x(1) ;
f.MaterialStreams.Item('MR17').PressureValue = x(2) ; 
f.MaterialStreams.Item('MR10').MolarFlowValue = x(3) ;
f.MaterialStreams.Item('C2').MolarFlowValue = x(4) ;
f.MaterialStreams.Item('C3').MolarFlowValue = x(5) ;
f.MaterialStreams.Item('N2').MolarFlowValue = x(6) ;


%Reset disturbances
f.MaterialStreams.Item('NG4').TemperatureValue = d(1) ; % degrees C
f.MaterialStreams.Item('NG4').PressureValue = d(2) ; % kilopascal
f.MaterialStreams.Item('NG4').MolarFlowValue = d(3); %mol/s
f.MaterialStreams.Item('MR10').TemperatureValue = d(1);

rcy = parameters.recycles;
switch rcy
    case('matlab')
        f.MaterialStreams.Item('MR16a').TemperatureValue = x(7) ;
        f.MaterialStreams.Item('MR19a').TemperatureValue = x(8) ;
        f.Operations.Item('RCY-2').IsIgnored = 1;
        f.Operations.Item('RCY-3').IsIgnored = 1;
        sol.CanSolve = 1 ;

        if f.MaterialStreams.Item('Subcooled NG').Temperature.Isknown == 1 
            J = f.Operations.Item('K-103').EnergyValue/100 ;
            message = 'solved' ;
        elseif f.MaterialStreams.Item('Subcooled NG').Temperature.Isknown == 0
            J = 1e5 ;
            message = 'convergence failure' ;
        end
        
    case('unisim')
        f.MaterialStreams.Item('MR16a').TemperatureValue = -157 ; %Starting guess for recycle. Should remain near constant.
        f.MaterialStreams.Item('MR19a').TemperatureValue = -95 ; % Use a "too cold" starting point for stream MR19  
        f.Operations.Item('RCY-2').IsIgnored = 0;
        f.Operations.Item('RCY-3').IsIgnored = 0;
        sol.CanSolve = 1 ;
        
        con1 = strcmp('Converged',f.Operations.Item('RCY-2').RecycleConvergence) ;
        con2 = strcmp('Converged',f.Operations.Item('RCY-3').RecycleConvergence) ;
        
        if f.MaterialStreams.Item('Subcooled NG').Temperature.Isknown == 1 && con1==1 && con2==1
            J = f.Operations.Item('K-103').EnergyValue/100 ;
            message = 'solved' ;
        elseif f.MaterialStreams.Item('Subcooled NG').Temperature.Isknown == 1 && con1==1 && con2==0 % Either one of the recycles or the heat exchanger is not converged
            J = 1e5 ;
            message = 'recycle failure' ;
        elseif f.MaterialStreams.Item('Subcooled NG').Temperature.Isknown == 1 && con1==0 && con2==1 % Either one of the recycles or the heat exchanger is not converged
            J = 1e5 ;
            message = 'recycle failure' ;
        elseif f.MaterialStreams.Item('Subcooled NG').Temperature.Isknown == 0 % Either one of the recycles or the heat exchanger is not converged
            J = 1e5 ;
            message = 'heat exchanger failure' ;
        end
end