%
% Example 14.1: Activated Sludge Anaerobic Zone
%
% Batch formulation
%
% Simplified IAWQ No.2 Model assuming:
%  - anaerobic hydrolysis, fermentation, P release
%  - biomass concentrations constant
%
% Bob Newell, March 1996
%
%-----------------------------------------------------------
%
function dxdt = Anaerobic_Zone( t, x )
global p
%
% x = [ 1=Xs 2=Sf 3=Sa 4=Xpp 5=Spo4 6=Xpha
%       7=So2 8=Sno3 ];
% p = [ 1=Khnfe 2=Kx 3=qfe 4=Kfe 
%       5=qpha 6=Ka 7=Kpp 8=Ypo4
%       9=Xh 10=Xpao
%       11=muaerobic 12=Ko2 13=Kfa
%       14=muanoxic 15=Kno3 16=Yh ];
%-----------------------------------------------------------
  hydrate = p(1) * x(1) / ( p(2) + x(1) / ( p(9) + p(10) ) ); 
  fermrate = p(3) * x(2) * p(9) / ( p(4) + x(2) );
  relrate = p(5) * x(3) * x(4) / ( p(6) + x(3) ) ...
               / ( p(7) + x(4) / p(10) );
% optional competition from oxygen and nitrate
  aerobic = p(11) * x(7) * ( x(2) + x(3) ) * p(9) ...
                  / ( p(12) + x(7) ) ...
				  / ( p(13) + x(2) + x(3) );
  anoxic = p(14) * p(12) * ( x(2) + x(3) ) * x(8) * p(9) ...
                  / ( p(12) + x(7) ) / ( p(15) + x(8) ) ...
				  / ( p(13) + x(2) + x(3) );
  propf = x(2) / ( x(2) + x(3) );
%
  dxdt = zeros( 8, 1 );
  dxdt(1) = - hydrate;
  dxdt(2) = hydrate - fermrate ...
               - propf * ( aerobic + anoxic );
  dxdt(3) = fermrate - relrate ...
               - ( 1 - propf ) * ( aerobic + anoxic );
  dxdt(4) = - p(8) * relrate;
  dxdt(5) = p(8) * relrate;
  dxdt(6) = relrate;
% optional competition from oxygen and nitrate
  dxdt(7) = aerobic * ( p(16) - 1 ) / p(16);
  dxdt(8) = - anoxic * ( 1 - p(16) ) / p(16) / 2.86;
%
%-----------------------------------------------------------
% the end!   
