%Submodel REACTOR, calculating mass balances for a simple CSTR.

%Extract reaction parameters and calculate kinetic constant k

nu = param.nu ; % Stoichometric coefficients
A = param.A ; % Constant coefficient in Arrhenius term
Ea = param.Ea ; % Activation energy for reactions
Rgas = param.R ; % Universal gas constant, 8.314 kJ/(kmol K)
k = A.*exp(-Ea/(Rgas*Tr)) ;

Mr = sum(nr) ;
xr = nr/Mr ; % Mole fractions in reactor

r = k*xr(1) ; %Reaction rates are k*xa.

in = RF*x_rf ; % Inflow to reactor
gen = Mr*nu*r ; % Generated by reaction
out = RP*xr ; % Outflow from reactor

%Mass balance: accumulated = in + formed - out
dndt_r = in + gen - out ;

conv_A = 100*(in(1) - out(1))/in(1) ; %Conversion of A, in %

convers = 100*gen(2:3)./[1 ; 2]/in(1);

s_AB = k(1)/k(2) ; %Relative selectivity (sA/sB)