%% Optimaliser.m
% Summary:      
% author:       Tor Anders Marvik 
% organization: Department of Chemical Engineering, NTNU, Norway
% contact:      torandma@stud.ntnu.no
% requires:     matlab
% version:      1.0
% todo 1.0:     Debugging "There is allways a second bug"            
% started       Oktober 2012
%%

clc
clear all

serv = actxserver('UniSimDesign.Application');                
hyCase = serv.ActiveDocument;                                 %unisim model


% %=========ATTEMTS ON AUTOMIZED OPTIMIZATION===================
%run('C:\Program Files\MATLAB\R2012a\toolbox\optim\optim\opti_Install')
        %needs to be ran every time matlab is reopend as the installation
        %do not save. Why????
        
% %  TC_feed  PC_feed     PDC     RC_to_col   XC      TC_col   Pressure afte expander
% lb=[-19,    7500,       240,    0.67,        2.5e5,  54,      3400];
% ub=[-14,    8000,       500,    1,           4e5,    60,      3430];
% x0=[-18,    7500,       240,    0.67,        2.5e5,  54,      3430];
% 
% %constraints= [p_top, T_stg19, c1_btm min stream constraints] 
% d=[-3.5000 10  1,-3e4,-1e4,-1e4]';    %c<=d
%                   % 
% 
% prob=optiprob('fun',@(x)cost_function(x,hyCase),'nlcon',@(x)constraints(x,hyCase),...
%               'nlrhs',d,'bounds',lb,ub,'x0',x0);
% opts1 = optiset('solver','nomad','display','iter');
% Opt1 = opti(prob,opts1);
% 
% [x,fval,exitmerke,info] = solve(   Opt1,x0)
%=============================================================


%========SYSTEMATIC SEARCH IN UNISIM==========================



init=struct('init',-1);
data_all=struct('feed_sec2',init,'s1',init,'s2',init,'s1b',init,'s3',init,'s4',init,'s5',init,...
                's6',init,'s7',init,'s8',init,'s9',init,'s10',init,'s11',init,...
                's12',init,'s13',init,'s14',init,'s15',init,'s16',init,'s17',init,...
                's18',init,'s19',init,'to_sec7',init,'D',init,'B',init,'side_btm_out',init,...
                'side_btm_in',init,'side_top_out',init,'side_top_in',init,...
                'constraints',init,'cost_parameters',init);
 x=[-17.7,    7500,       240,    0.59,        2.6e5,  51.4,     3660];
data=[zeros(1,5),zeros(size(x)),zeros(1,8),zeros(1,39),0]; %initilize lenght of data

%  TC_feed  PC_feed     PDC     RC_to_col   XC      TC_col   Pressure afte expander
% x=[-14.5,    7465,       260,    0.5,        2.5e5,  54,     3430];
% TC_feed_change=linspace(-24,-14,10);
% for i=1:length(TC_feed_change)
%     x(1,1)=TC_feed_change(i);
%     [cost_parameters,merke,c,Temp_profile,data_all]=cost_function(x,hyCase,data_all,i);
%     data=[data; cost_parameters,x,c, Temp_profile, merke]; 
% end  
%                     
                    
% x=[-14.5,    7465,       260,    0.5,        2.5e5,  999,     3430];
% pc_feed=linspace(6000,7800,10);  %expansion pressure max 78 bar
% for i=1:length(pc_feed)
%     x(1,2)=pc_feed(i);
%     [cost_parameters,merke,c,Temp_profile,data_all]=cost_function(x,hyCase,data_all,i);
%     data=[data; cost_parameters,x,c, Temp_profile, merke]; 
%     
% end


% 
% x=[-14.5,    7465,       260,    0.5,        2.5e5,  54,     3430];
% PDC=linspace(50,800,10);
% for i=1:length(PDC)
%      x(1,3)=PDC(i);
%     [cost_parameters,merke,c,Temp_profile,data_all]=cost_function(x,hyCase,data_all,i);
%     data=[data; cost_parameters,x,c, Temp_profile, merke]; 
%  end
% 
% x=[-14.5,    7465,       260,    0.5,        2.5e5,  54,     3430];
% RC_to_col=linspace(0.2,0.8,10);
% for i=1:length(RC_to_col)
%     x(1,4)=RC_to_col(i);
%      [cost_parameters,merke,c,Temp_profile,data_all]=cost_function(x,hyCase,data_all,i);
%      data=[data; cost_parameters,x,c, Temp_profile, merke]; 
% end
% 
% x=[-14.5,    7465,       260,    0.5,        2.5e5,  54,     3430];
% XC=linspace(0,4000,10);
% for i=1:length(XC)
%     x(1,5)=XC(i);
%      [cost_parameters,merke,c,Temp_profile,data_all]=cost_function(x,hyCase,data_all,i);
%      data=[data; cost_parameters,x,c, Temp_profile, merke]; 
% end
% 
% 
x=[-14.5,    7465,       260,    0.5,        2.5e5,  54,     3430];
TC_col=linspace(45,60,10);
for i=1:length(TC_col)
    x(1,6)=TC_col(i);
     [cost_parameters,merke,c,Temp_profile,data_all]=cost_function(x,hyCase,data_all,i);
     data=[data; cost_parameters,x,c, Temp_profile, merke]; 
end
 
%
%To se the effect of chaning the pressure after the exapnder, comlumn
%bottom pressure has to be made a fuctinon of the pressure after the
%exapander. Not done
% x=[-14.5,    7465,       260,    0.5,        2.5e5,  54,     3430];
% p_exp=linspace(3000,4000,10);
% for i=1:length(p_exp)
%     x(1,7)=p_exp(i);
%     [cost_parameters,merke,c,Temp_profile,data_all]=cost_function(x,hyCase,data_all,i);
%     data=[data; cost_parameters,x,c, Temp_profile, merke]; 
% end



%==========PLOTTING=======================================
[TC_col,RC]=meshgrid(linspace(45,50,5),linspace(0.3,0.7,5));
cost=zeros(5:5);

switch i
case 1
    for i=1:5
        for j=1:5
            x(6)=TC_col(i,j);
            cost(i,j)=cost_function(x,hyCase)
        end
        x(4)=RC(i,j);
    end


    figure(1)
    surf(TC_col,RC,cost)
    xlabel('Bottom temp column')
    ylabel('Split liquid cold separator')
    zlabel('Cost')
    contour(TC_col,RC,cost)

case 2
    x=[-14.5,    7465,       260,    0.5,        2.5e5,  54,      4150];
    [TC_feed,RC]=meshgrid(linspace(-18,-10,5),linspace(0.3,0.7,5));
    cost=zeros(1,12);
    for i=1:5
        for j=1:5
            x(1)=TC_feed(i,j);
            cost=[cost;x,cost_function(x,hyCase)];
        end
        x(4)=RC(i,j);
    end
    
%    figure(2)
%    surf(TC_feed,RC,cost)   
%    xlabel('Temperature feed sep')
%    ylabel('Split liquid cold separator')
%    zlabel('Cost')


end





