% Main Optimization file
clear all; close all; clc;
% This is a script file for optimization
% The related script files with this file are Objective.m, Petlyuk_nonlcon
% (inequality constraints),Petlyuk_NLmodel(non-linear model)
% Other script files, .................are based on this main optimization
% file
% Global datas used are stages in the column, initial holdups in reboiler
% and condensers, boiling points of comp A, B and C, relative volatilities,
% thermal data and they are stored in structures coldata, dyndata, and
% hydraulic and termodata
%Nominal_final.mat has nominal values of composition, inputs and
%temperature
% Also the nominal states and input values (Nominal_final.mat) are made global
% by using dyndata as they are used as reference point for steady state
% simulation and also as setpoints for dynamic simulations

global coldata dyndata hydraulic alfa

% COLUMN DATA %%
% Number of stages in each section (C11,C12,C21,C22,C23,C24)
coldata.Nt = [12 12 12 12 12 12];
% Initial holdup in reboiler and condenser
coldata.M0 = [1 1 1 1 1 1 1 1 1 1 1 1];
%Boiling points of ethanol, propanol and n-butanol
coldata.Tboil = [78.37 97 117.4];
%Hydraulics
hydraulic.k1 = 1/0.063;

%Relative volatilit of Ethanol, Propanol and n-Butanol with respect to the
%heaviest n-Butanol
alfa=[4.215374389 2.102194688 1];

% Initial values for optimization made global
%load opt_init.mat  
load Nominal_final

dyndata.x_nom = x_nom;
dyndata.u_nom = u_nom;
%dyndata.temp_nom = temp_nom;
options = optimset('fmincon');
options = optimset(options,'TolFun',1e-10,'TolCon',1e-13,'MaxFunEvals',50000,'Display','none',...
'Algorithm','active-set');%,'Diagnostic','off');%, 'GradObj','on','Hessian','on');%,...

% All the equality and inequality linear constraints are given in the file
% specs
Specs



% Optimization command 
[x_opt,fval,exitflag,output,lambda,Grad,Hessian] = fmincon(@(x) Objective(x),[x_nom;u_nom],Ain,bin,Aeq,beq,[],[],@(x) Petlyuk_nonlcon(x),options);

x_nom=x_opt(1:225);u_nom=x_opt(226:end);

dxdt = norm(Petlyuk_NLmodel(0,x_nom, u_nom));


xf=x_nom;
% script file for plotting composition profiles at the end of each
% optimization
Plot_Profiles
% script file for plotting temperature at the end of each optimization
%plot_temp_profile 
% print selected variables or outputs, temperature or composition
%fval
% get temperatures
%[sys  Temp]=Petlyuk_NLmodel([],xf,u_nom);
%Temp = sys(end-71:end);
%Y0= Temp;
% print the composition at the end of each optimization
[x_nom(im_xt(1)),x_nom(im_xt(2)),1-x_nom(im_xt(1))-x_nom(im_xt(2));...
    x_nom(im_xS1(1)),x_nom(im_xS1(2)),1-x_nom(im_xS1(1))-x_nom(im_xS1(2));...
    x_nom(im_xr(1)),x_nom(im_xr(2)),1-x_nom(im_xr(1))-x_nom(im_xr(2));...
        x_nom(im_x1(1)),x_nom(im_x1(coldata.Nt(1)+1)),1-x_nom(im_x1(1))-x_nom(im_x1(1+coldata.Nt(2)));...
  x_nom(im_x2(coldata.Nt(2))),x_nom(im_x2(2*coldata.Nt(2))),1-x_nom(im_x2...
   (coldata.Nt(2)))-x_nom(im_x2(2*coldata.Nt(2)))]

% The results from optimization are saved in Nominal_final.mat

petlyuk_cost