% Script for calculating Jud
clear all; close all; clc;
global coldata dyndata hydraulic termodata UU alfa tend NI i
%% COLUMN DATA %%

% Number of stages in 6 sections of Petlyuk 
coldata.Nt = [12 12 12 12 12 12];
% Initial holdup stages
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];
termodata.P = 1.0;
hydraulic.k1 = 1/0.063;
%% THERMODYNAMIC DATA %%
alfa = [4.215374389	2.102194688	1]; %ethanol propanol butanol

%% load nominal x u
load Nominal_final.mat      % The nominal value obtained from optimization for ...
%minimizing the sum of impurities in the product streams (D, S and R)
UU=[];                       
i=0;                       %to initiate Petlyuk_openloop make change in CS by i+1
indmat_Yes_S1
%Change in disturbances can be mase here to obtain Jud and also the input
%change has to be done in Simulation_Petlyuk.m
dyndata.x_nom = x_nom;        %initial composition and holdup states
% disturbance in feed F, h=1e-5
h = 1e-5;
%dd1= h;
%u_nom(6)= u_nom(6)+dd1;
%disturbance in composition A in feed
%dd2=h;
%u_nom(7)=u_nom(7)+dd2;
%disturbance in composition B in feed
%dd3=h;
%u_nom(8)=u_nom(8)+dd3;

%disturbance in boilup
%dd4=h;
%u_nom(2)=u_nom(2)+dd4;

%disturbance in vapor split RV
%dd5=h;
%u_nom(4)=u_nom(4)+dd5;


dyndata.u_nom=u_nom;                  %we have made initial input global
dyndata.temp_nom=temp_nom;
%% Run this file to get initial values with LV Configuartion, uncomment
% line number 
NI=5; % Number of integerators
options = odeset('MaxStep',10);
tend=20000;
[t,x]=ode15s(@Simulation_Petlyuk,[0 tend],[dyndata.x_nom;zeros(NI,1)],options);
%% xf states to plot profiles.
xf=x(end,1:225)';
u_nom=UU(end,2:12)';

%Script file to calculate cost after simulation
petlyuk_cost

%load Jud