%%
%Load initial values, 23-10-2011 18h10 36''+reconciliation

%CRUDE OIL FEED
T0=125; %°C
m0A=76.582/3.6; %t/h -> kg/s
m0B=159.785/3.6;
m0C=120.414/3.6;
m0D=178.686/3.6;
m0E=126.351/3.6;
m0F=253.604/3.6;
m0=m0A+m0B+m0C+m0D+m0E+m0F;

%Heat capacities
capacities;

%Branch A
uAin=m0A/m0; %split fraction []
ThinA=295.4453; %Hot stream inlet temperature [°C]
cp_hA=k1A*ThinA+k2A; %Hot stream inlet heat capacity [J/kgK]
mhA=57.270/3.6;  %Hot stream flowrate [kg/s]

%Branch B
uBin=m0B/m0;
ThinB=267.82;
cp_hB=k1B*ThinB+k2B; 
mhB=162.6105/3.6; 
LGO22=39.92969/3.6;

%Branch C
uCin=m0C/m0;
ThinC=248.7734;
cp_hC=k1C*ThinC+k2C;
mhC=97.075/3.6;

% Branch D
uDin=m0D/m0;
ThinD1=222.078;
cp_hD1 =k1D1*ThinD1+k2D1;
mhD1=212.225/3.6;

ThinD2=267.632;
cp_hD2=k1D2*ThinD2+k2D2;
mhD2=172.942/3.6;

%Branch E
uEin=m0E/m0;
ThinE=241.587;
cp_hE=k1E*ThinE+k2E;
mhE=142.1482/3.6;

% Branch F
ThinF1=193.843;
cp_hF1=k1F1*ThinF1+k2F1;
mhF1=72.125/3.6;
ThinF2=244.379;
cp_hF2=k1F2*ThinF2+k2F2;
mhF2=(113.081+110.689)/3.6;

% Branch F - Sub-branches F2-F3
splith=110.689/(113.081+110.689); %Hot split fraction
splitc=119.91/253.604; %Cold split fraction

%%
%Initial valve positions computations

z0=[0.8 0.8 0.8 0.8 0.8];
uin=[uAin uBin uCin uDin uEin]; %We know the split fractions

zFin=1; %Olavalve - the valve position on the main branch (F) is fixed to 1

options = optimset('TolFun',1e-15);
z=fsolve(@(z) vav(z,uin,zFin),z0,options); %solver

zin=[z zFin]; %intial valve positions (result)

zF3in=1;
zF2in=(splitc/(1-splitc))*zF3in;

%%
%Initial values given to the filters

zeros = [0 0 0 0 0];
in3A=[125 295.4453 226.4550761762809];
in3B=[125 267.82 208.2976010282482];
in3C=[125 248.7734 213.6725976653782];
in5D=[125 222.078 267.632 181.0689995365419 207.6791409932723];
in3E=[125 241.587 200.2790677090032]; 
in5F=[125 193.843 244.379 137.5308748959151 202.2179313771867]; 
in3F2=[137.5308748959151 244.379 205.7564625690082];
in3F3=[137.5308748959151 244.379 199.0270239621444]; 

