function [r] = Rrate_NewDist_MESC(T,wNH3,in,kval,d)

% This function calculates the reation rate of ammonia in kg NH3/kg cat,s.
%
% The required input is given by
%   T       Temperature of the stage
%   c       Mass fraction of ammonia of the stage
%   inl     Array containing the inlet conditions for the pressure
%   kval    Structure containing parameters for the reaction rate constant
%   d       Catalyst activity

% Definition of the mass fractions and molar fractions
mNH3 = wNH3;
mH2  = (1-wNH3)*2*in(5)/(28+2*in(5)); 
mN2  = (1-wNH3)*28/(28+2*in(5)); 
nNH3 = mNH3/17; 
nH2  = mH2/2; 
nN2  = mN2/28; 
x    = nNH3/(nNH3+nH2+nN2)*1;

% Calculation of the reaction rate constants
k1  = kval.Afor*exp(-kval.Eafor /(kval.R*(T(1)+273)));  % Forward reaction rate constant
k2  = kval.Abac*exp(-kval.Eabac /(kval.R*(T(1)+273)));  % Backward reaction rate constant

% Calculation of the partial pressure [bar]
pNH3 = x*in(2) ;
pH2  = (1-x)*in(5)/(1+in(5))*in(2);
pN2  = (1-x)*1/(1+in(5))*in(2); 

% Calculation of the reaction rate and transformation of it
r = k1*pN2*pH2^1.5/pNH3 - k2*pNH3/pH2^1.5;	% [mol N2/ m3 cat, h]
r = r*2*17/2200/3600;                   % [kg NH3/ kg cat, s]
r = (1+d)*4.75*r;                       % Multiply by 4.75 to match industrial instability