% close all
% clear all
%first iteration
%p7=112;h5out=71;h9out=100;cp7=2.5;t7=120;pe=7;t9out=170;





% M=12.5;

%skola
addpath('D:\OneDrive - České vysoké učení technické v Praze\Dokumenty\MATLAB\');
%doma
% addpath('c:\Ondra\Skola\Vyuka\Diplomky\bakalarka_Mahrougi\XSteam_Matlab_v2.6\')


%imput
% tlak [kPa]
% teplota [C]

ta=500;
pa=1700;


etatd=0.9;
etap=0.75;
etag=0.99;
etam=0.99;

% Equip. 1 (boiler)
    %evaporator
w1in=0.40;
w1out=0.45;
t1in=229.3;
t1out=238.5;

%plin and plout has ti be same (T is changing)
[p1in,cp1in,h1in,rho1in,s1in] = LiBr_properties_mass_fce(t1in, w1in);
[p1out,cp1out,h1out,rho1out,s1out] = LiBr_properties_mass_fce(t1out, w1out);

Mx1in=w1out/(w1out-w1in);
Mx1out=Mx1in*(w1in)/w1out;

    %superheater
    
h1sup=XSteam('h_pt',pa*0.01,t7);



% Equip. 2 (Turbine)

h2in=XSteam('h_pt',pa*0.01,ta);
s2in=XSteam('s_pt',pa*0.01,ta);

h2outad=XSteam('h_ps',pe*0.01,s2in);
h2out=h2in-(h2in-h2outad)*etatd;
t2out=XSteam('T_ph',pe*0.01,h2outad);
%Extraction
p2extr=p7;
h2extrad=XSteam('h_ps',p2extr*0.01,s2in);
h2extr=h2in-(h2in-h2extrad)*etatd;

mi=(h2extr-h2out)/(h2in-h2out);




% Equip. 3 (Absorber)
w3=0.401;
t3=55;

    [p3,cp3,h3,rho3,s3] = LiBr_properties_mass_fce(t3, w3);
    pe=p3;

%flow ratio

    Mx3=((h3-h2out))/(h5out-h3);

% Equip. 4 (Condensate pump)

        h4in=h3;
        h4out=((1/rho3)*(p7-p3)/etap)+h4in;
% Equip. 5 (Cooler)
    t5in=t9out;
    t5out=35;
    h5in=h9out;
    h5out=h9out-(cp3+cp7)*0.5*(t5in-t5out);
    

% Equip. 6 (Regenerator)

dt6=t7-t3-5;

h6inlow=h4out;
h6outlow=h6inlow+dt6*2.5; %average Cp
dh6l=(h6outlow-h6inlow);

h6inr=h1out;
h6outr=(Mx1out*h6inr-dh6l*Mx1in)/Mx1out;
dt62=Mx1in*dh6l/(2.5*Mx1out);


% Equip. 7 (Deareator)
        
        %presure 
        t7=120;
        w7=0.4;
        [p7,cp7,h7,rho7,s7] = LiBr_properties_mass_fce(t7, w7);
        
        alfa=(Mx1in*h7-Mx1in*h6outlow)/(h2extr-h6outlow);


% Equip. 8 (Booster pump)
        h8in=h7;
        h8out=((1/rho7)*(pa-p7)/etap)+h8in;


% Equip. 9 (Mixing point)

h9out=(Mx1out*h6outr+(Mx3-Mx1out)*h4out)/(Mx3);
t9in=t1out-dt62;
% t9out=t3+((h9out-h4in)/(Mx1out+Mx3));

t9out=(t3*(Mx3-Mx1out)+t9in*Mx1out)/(Mx3);


% ((Mx3-Mx1out)/Mx3)*h4out+((Mx1out)/Mx3)*h6outr;

% [p9y,cp9y,h9y,rho9y,s9y] = LiBr_properties_mass_fce(65,0.43);

%efficiency

M=12500/((h2in-h2out)*etag*etam*(1-alfa*mi));

Pkon=(h4out-h4in)*M*(Mx3+1-alfa);
Pboos=(h8out-h8in)*M*Mx1in;

Qsup=((h1out-h8out)*M*Mx1in)+(h2in-h1sup)*M;
Qrel=(h5in-h5out)*M*Mx3;

ef=(12500-Pkon-Pboos)/Qsup
ef1=(12500)/Qsup
ef2=(Qsup-Qrel)/Qsup







