Figure 7.23 (page 395):

Concentration profiles of reactants (log scale); fluid concentration of O_2 (x), CO (+), C_3H_6 (*).

Code for Figure 7.23

Text of the GNU GPL.

main.m

%% Copyright (C) 2001, James B. Rawlings and John G. Ekerdt
%%
%% This program is free software; you can redistribute it and/or
%% modify it under the terms of the GNU General Public License as
%% published by the Free Software Foundation; either version 2, or (at
%% your option) any later version.
%%
%% This program is distributed in the hope that it will be useful, but
%% WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%% General Public License for more details.
%%
%% You should have received a copy of the GNU General Public License
%% along with this program; see the file COPYING.  If not, write to
%% the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
%% MA 02111-1307, USA.

global  npts A Aint Bint Rint  caf cbf ccf ...
        k10 E1 k20 E2 Ka0 Ea Kc0 Ec Da Db Dc T ...
        Ra Rb Rc Da Db Dc kma kmb kmc dcadr dcbdr dccdr
%%
%% Packed bed reactor simulation (steady state)
%%
%% Use a marching method for the packed bed.
%% concentration, c_j, temperature, T, and pressure, P
%% volumetric flowrate, Q, from equation of state
%%
%% Compute the pellet concentration profiles.
%%
%% Method can address:
%%
%%           multi-component, multi-reaction, arbitrary kinetics
%%           reaction-diffusion problem in pellet, accurate profiles
%%           flux and/or value boundary conditions at pellet exterior
%%           energy balance in pellet and fluid
%%           pressure drop and equation of state for fluid
%%           sphere, semi-infinite cylinder or slab geometries for pellet
%%           axial dispersion in the bed
%%           transient problem
%%           
%% Method does not address:
%%
%%           radial profiles in bed
%%           non-uniform pellet exterior
%%
%% 5/07/02
%%
%% jbr
%%
%%
%% units: mol, cm, sec, K
%%
%% Simulate pellet with parameters from Co, Cavendish, Hegedus.
%%
%% C0     + 1/2 O_2 -->   CO_2
%%
%% C_3H_6 + 9/2 O_2 --> 3 CO_2 + 3 H_2O
%%
%% A=CO  B=O_2  C=C_3H_6  D=CO_2  E=H_2O
%%
%% r_1 = k_1 c_A c_B / (1+K_A c_A + K_C c_C)^2
%% r_2 = k_2 c_C c_B / (1+K_A c_A + K_C c_C)^2

more off;

Rg  = 8.314;  % J/K mol
P   = 1.013e5;  % N/m^2
T   = 550; % K
cf  = P/(Rg*T)*1e-6; % mol/cm^3
Rp  = 0.175; % cm radius of catalyst particle

caf = cf*0.02;
cbf = cf*0.03;
ccf = cf*5e-4;

k10  = 6.802e16*2.6e6*80/100*0.05/100;  %mol/cm^3 s
k20  = 1.416e18*2.6e6*80/100*0.05/100;  %mol/cm^3 s
E1   = 13108; %K
E2   = 15109; %K
Ka0  = 8.099e6; % cm^3/mol
Kc0  = 2.579e8; % cm^3/mol
Ea   = -409; %K
Ec   = 191; %K
Da   = 0.0487; % cm^2/s
Db   = 0.0469; % cm^2/s
Dc   = 0.0487; % cm^2/s
kma  = 0.4*9.76;   % cm/s
kmb  = 0.4*10.18;  % cm/s
kmc  = 0.4*9.76;   % cm/s
%%
%% global collocation 
%%
npts = 200;
[R A B Q] = colloc(npts-2, 'left', 'right');
R = R*Rp;
A = A/Rp;
B = B/(Rp*Rp);
Q = Q*Rp;
Aint = A(2:npts-1,:);
Bint = B(2:npts-1,:);
Rint = R(2:npts-1);
%%
%%
%%
%% find the pellet profile at the fluid conditions
%%

ca0 = logspace(log10(caf)-10,log10(caf),npts)';
cb0 = logspace(log10(cbf)-1,log10(cbf),npts)';
cc0 = logspace(log10(ccf)-6,log10(ccf),npts)';

c0=[ca0;cb0;cc0];
tol = 1e-12;
opts = optimset ('MaxFunEvals', 2000*numel (c0), ...
                 'MaxIter', 500*numel (c0), ...
		 'TolX', tol);
[c,fval,info] = fsolve('pellet',c0,opts);

info

ca = c(1:npts);
cb = c(npts+1:2*npts);
cc = c(2*npts+1:3*npts);
dcadr = A*ca;
dcbdr = A*cb;
dccdr = A*cc;

%%
%% compute other products concentrations
%%
kmd = kma;
kme = kmb;
Dd  = Da;
De  = Db;
cdf = 0;
cef = 0;
dcddr = (-Da*dcadr(npts)-3*Dc*dccdr(npts)) /Dd ;
dcedr = (-3*Dc*dccdr(npts)) /De ;
cdR   = cdf - Dd*dcddr/kmd;
ceR   = cef - De*dcedr/kme;
concd = ( Da*(ca(npts)-ca) + 3*Dc*(cc(npts)-cc) )/Dd + cdR;
ce    = ( 3*Dc*(cc(npts)-cc) )/De + ceR;

table = [R ca cb cc concd ce dcadr dcbdr dccdr];

table_2 = [Rp caf cbf ccf cdf cef];

subplot (3, 1, 1);
plot (table(:,1), table(:,2:4), table_2(:,1), table_2(:,2:4));
title ('Figure 7.23')

subplot (3, 1, 2);
semilogy (table(:,1), table(:,2:4), table_2(:,1), table_2(:,2:4));
title ('Figure 7.23')

subplot (3, 1, 3);
plot (table(:,1), table(:,5:6), table_2(:,1), table_2(:,5:6));
title ('Figure 7.23')

pellet.m

function retval = pellet(x)
  global  npts A Aint Bint Rint  caf cbf ccf ...
          k10 E1 k20 E2 Ka0 Ea Kc0 Ec Da Db Dc T ...
          Ra Rb Rc Da Db Dc kma kmb kmc dcadr dcbdr dccdr
  %%
  %% component A
  %%
  ca = x(1:npts);
  cb = x(npts+1:2*npts);
  cc = x(2*npts+1:3*npts);
  k1      = k10*exp(-E1/T);
  k2      = k20*exp(-E2/T);
  Ka      = Ka0*exp(-Ea/T);
  Kc      = Kc0*exp(-Ec/T);
  den     = (1+Ka*ca+Kc*cc).^2;
  r1      = k1.*ca.*cb./den;
  r2      = k2*cc.*cb./den;
  Ra      = - r1;
  Rb      = - 1/2*r1  - 9/2*r2;
  Rc      = - r2;
  ip = 1;
  retval(ip)    = A(1,:)*ca;
  caint = ca(2:npts-1);
  retval(ip+1:ip+npts-2) = Bint*ca + 2*Aint*ca./Rint + ...
      Ra(2:npts-1)/Da;
  dcadr = A(npts,:)*ca;
  retval(ip+npts-1) = Da*dcadr - kma*(caf - ca(npts));
  %%
  %% component B
  %%
  ip = npts+1;
  cbint = cb(2:npts-1);
  retval(ip)    = A(1,:)*cb;
  retval(ip+1:ip+npts-2) = Bint*cb + 2*Aint*cb./Rint + ...
      Rb(2:npts-1)/Db;
  dcbdr = A(npts,:)*cb;
  retval(ip+npts-1) =  Db*dcbdr - kmb*(cbf - cb(npts));
  %%
  %% component C
  %%
  ip = 2*npts+1;
  ccint = cc(2:npts-1);
  retval(ip)    = A(1,:)*cc;
  retval(ip+1:ip+npts-2) = Bint*cc + 2*Aint*cc./Rint + ...
      Rc(2:npts-1)/Dc;
  dccdr = A(npts,:)*cc;
  retval(ip+npts-1) = Dc*dccdr - kmc*(ccf - cc(npts));

../../util/matlab/colloc.m

function [r, a, b, q] = colloc (n, varargin)

  nargs = nargin;

  if (nargs < 1 || nargs > 3)
    error ('usage: [r, a, b, q] = colloc (n, ''left'', ''right'')');
  end

  if (~ (isnumeric (n) && numel (n) == 1 && round (n) == n))
    error ('colloc: first argument must be an integer scalar');
  end

  if (isnan (n) || isinf (n))
    error ('colloc: NaN is invalid as N');
  end

  if (n < 0)
    error ('colloc: first argument must be non-negative');
  end

  n0 = 0;
  n1 = 0;

  for i = 2:nargs
    s = varargin{i-1};
    if (~ ischar (s))
      error ('colloc: expecting character string argument');
    end

    s = lower (s);
    if (strcmp (s, 'r') || strcmp (s, 'right'))
      n1 = 1;
    elseif (strcmp (s, 'l') || strcmp (s, 'left'))
      n0 = 1;
    else
      error ('colloc: unrecognized argument');
    end
  end

  nt = n + n0 + n1;

  if (nt < 1)
    error ('colloc: the total number of roots must be positive');
  end

  alpha = 0;
  beta = 0;

  %% Compute roots.

  [dif1, dif2, dif3, r] = jcobi (n, n0, n1, alpha, beta);

  %% First derivative weights.

  a = zeros (nt, nt);
  for i = 1:nt
    a(i,:) = dfopr (n, n0, n1, i, 1, dif1, dif2, dif3, r)';
  end

  %% Second derivative weights.

  b = zeros (nt, nt);
  for i = 1:nt
    b(i,:) = dfopr (n, n0, n1, i, 2, dif1, dif2, dif3, r)';
  end

  %% Gaussian quadrature weights.

  id = 3;
  q = dfopr (n, n0, n1, 0, id, dif1, dif2, dif3, r);

end


%% The following routines (JCOBI, DIF, DFOPR, INTRP, AND RADAU)
%% are the same as found in Villadsen, J. and M.L. Michelsen,
%% Solution of Differential Equation Models by Polynomial
%% Approximation, Prentice-Hall (1978) pages 418-420.
%%
%% Cosmetic changes (elimination of arithmetic IF statements, most
%% GO TO statements, and indentation of program blocks) made by:
%%
%% John W. Eaton
%% Department of Chemical Engineering
%% The University of Texas at Austin
%% Austin, Texas 78712
%%
%% June 6, 1987
%%
%% Some error checking additions also made on June 7, 1987
%%
%% Further cosmetic changes made August 20, 1987
%%
%% Translated from Fortran December 14, 2006

function vect = dfopr (n, n0, n1, i, id, dif1, dif2, dif3, root)

%%   Villadsen and Michelsen, pages 133-134, 419
%%
%%   Input parameters:
%%
%%     N      : The degree of the Jacobi polynomial, (i.e. the number
%%              of interior interpolation points)
%%
%%     N0     : Determines whether x = 0 is included as an
%%              interpolation point
%%
%%                n0 = 0  ==>  x = 0 is not included
%%                n0 = 1  ==>  x = 0 is included
%%
%%     N1     : Determines whether x = 1 is included as an
%%              interpolation point
%%
%%                n1 = 0  ==>  x = 1 is not included
%%                n1 = 1  ==>  x = 1 is included
%%
%%     I      : The index of the node for which the weights are to be
%%              calculated
%%
%%     ID     : Indicator
%%
%%                id = 1  ==>  first derivative weights are computed
%%                id = 2  ==>  second derivative weights are computed
%%                id = 3  ==>  Gaussian weights are computed (in this
%%                             case, I is not used).
%%
%%   Output parameters:
%%
%%     DIF1   : vector containing the first derivative
%%              of the node polynomial at the zeros
%%
%%     DIF2   : vector containing the second derivative
%%              of the node polynomial at the zeros
%%
%%     DIF3   : vector containing the third derivative
%%              of the node polynomial at the zeros
%%
%%     VECT   : vector of computed weights

  if (n0 ~= 0 && n0 ~= 1)
    error ('dfopr: n0 not equal to 0 or 1');
  end

  if (n1 ~= 0 && n1 ~= 1)
    error ('dfopr: n1 not equal to 0 or 1');
  end

  if (n < 0)
    error ('dfopr: n less than 0');
  end

  nt = n + n0 + n1;

  if (id ~= 1 && id ~= 2 && id ~= 3)
    error ('dfopr: id not equal to 1, 2, or 3');
  end

  if (id ~= 3)
    if (i < 1)
      error ('dfopr: index less than zero');
    end

    if (i > nt)
      error ('dfopr: index greater than number of interpolation points');
    end
  end

  if (nt < 1)
    error ('dfopr: number of interpolation points less than 1');
  end

%% Evaluate discretization matrices and Gaussian quadrature
%% weights.  Quadrature weights are normalized to sum to one.

  vect = zeros (nt, 1);

  if (id ~= 3)
    for j = 1:nt
      if (j == i)
	if (id == 1)
	  vect(i) = dif2(i)/dif1(i)/2.0;
	else
	  vect(i) = dif3(i)/dif1(i)/3.0;
	end
      else
	y = root(i) - root(j);
	vect(j) = dif1(i)/dif1(j)/y;
	if (id == 2)
	  vect(j) = vect(j)*(dif2(i)/dif1(i) - 2.0/y);
	end
      end
    end
  else
    y = 0.0;

    for j = 1:nt

      x = root(j);
      ax = x*(1.0 - x);

      if (n0 == 0)
	ax = ax/x/x;
      end

      if (n1 == 0)
	ax = ax/(1.0 - x)/(1.0 - x);
      end

      vect(j) = ax/dif1(j)^2;
      y = y + vect(j);

    end

    vect = vect/y;

  end

end

function [dif1, dif2, dif3, root] = jcobi (n, n0, n1, alpha, beta)

%%   Villadsen and Michelsen, pages 131-132, 418
%%
%%   This subroutine computes the zeros of the Jacobi polynomial
%%
%%      (ALPHA,BETA)
%%     P  (X)
%%      N
%%
%%   Input parameters:
%%
%%     N      : The degree of the Jacobi polynomial, (i.e. the number
%%              of interior interpolation points)
%%
%%     N0     : Determines whether x = 0 is included as an
%%              interpolation point
%%
%%                N0 = 0  ==>  x = 0 is not included
%%                N0 = 1  ==>  x = 0 is included
%%
%%     N1     : Determines whether x = 1 is included as an
%%              interpolation point
%%
%%                N1 = 0  ==>  x = 1 is not included
%%                N1 = 1  ==>  x = 1 is included
%%
%%     ALPHA  : The value of alpha in the description of the jacobi
%%              polynomial
%%
%%     BETA   : The value of beta in the description of the Jacobi
%%              polynomial
%%
%%     For a more complete explanation of alpha and beta, see Villadsen
%%     and Michelsen, pages 57 to 59
%%
%%   Output parameters:
%%
%%     ROOT   : vector containing the n + n0 + n1 zeros of the node
%%              polynomial used in the interpolation routine
%%
%%     DIF1   : vector containing the first derivative
%%              of the node polynomial at the zeros
%%
%%     DIF2   : vector containing the second derivative
%%              of the node polynomial at the zeros
%%
%%     DIF3   : vector containing the third derivative
%%              of the node polynomial at the zeros

  if (n0 ~= 0 && n0 ~= 1)
    error ('jcobi: n0 not equal to 0 or 1');
  end

  if (n1 ~= 0 && n1 ~= 1)
    error ('jcobi: n1 not equal to 0 or 1');
  end

  if (n < 0)
    error ('jcobi: n less than 0');
  end

  nt = n + n0 + n1;

  if (nt < 1)
    error ('jcobi: number of interpolation points less than 1');
  end

  dif1 = zeros (nt, 1);
  dif2 = zeros (nt, 1);
  dif3 = zeros (nt, 1);
  root = zeros (nt, 1);

%% First evaluation of coefficients in recursion formulas.
%% recursion coefficients are stored in dif1 and dif2.

  ab = alpha + beta;
  ad = beta - alpha;
  ap = beta*alpha;
  dif1(1) = (ad/(ab + 2.0) + 1.0)/2.0;
  dif2(1) = 0.0;

  if (n >= 2)
    for i = 2:n

      z1 = i - 1.0;
      z = ab + 2*z1;
      dif1(i) = (ab*ad/z/(z + 2.0) + 1.0)/2.0;

      if (i == 2)
	dif2(i) = (ab + ap + z1)/z/z/(z + 1.0);
      else
	z = z*z;
	y = z1*(ab + z1);
	y = y*(ap + y);
	dif2(i) = y/z/(z - 1.0);
      end

    end
  end

%% Root determination by newton method with suppression of
%% previously determined roots.

  x = 0.0;

  for i = 1:n

    done = false;

    while (~ done)

      xd = 0.0;
      xn = 1.0;
      xd1 = 0.0;
      xn1 = 0.0;

      for j = 1:n
	xp = (dif1(j) - x)*xn  - dif2(j)*xd;
	xp1 = (dif1(j) - x)*xn1 - dif2(j)*xd1 - xn;
	xd = xn;
	xd1 = xn1;
	xn = xp;
	xn1 = xp1;
      end

      zc = 1.0;
      z = xn/xn1;

      if (i ~= 1)
	for j = 2:i
	  zc = zc - z/(x - root(j-1));
	end
      end

      z = z/zc;
      x = x - z;

      if (abs(z) <= 1.0e-09)
	done = true;
      end

    end

    root(i) = x;
    x = x + 0.0001;

  end

%% Add interpolation points at x = 0 and/or x = 1.

  nt = n + n0 + n1;

  if (n0 ~= 0)
    for i = 1:n
      j = n + 1 - i;
      root(j+1) = root(j);
    end
    root(1) = 0.0;
  end

  if (n1 == 1)
    root(nt) = 1.0;
  end


%% Use recursion formulas to evaluate derivatives of node polynomial
%%
%%                   N0     (ALPHA,BETA)           N1
%%     P  (X)  =  (X)   *  P (X)         *  (1 - X)
%%      NT                   N
%%
%% at the interpolation points.

  for i = 1:nt
    x = root(i);
    dif1(i) = 1.0;
    dif2(i) = 0.0;
    dif3(i) = 0.0;
    for j = 1:nt
      if (j ~= i)
	y = x - root(j);
	dif3(i) = y*dif3(i) + 3.0*dif2(i);
	dif2(i) = y*dif2(i) + 2.0*dif1(i);
	dif1(i) = y*dif1(i);
      end
    end
  end

end