function B = B_matrix( Dbin, x )
%--------------------------------------------------------------------------
%                      Author: James C. Sutherland
%--------------------------------------------------------------------------
%
% This routine calculates the B matrix from the binary diffusivities and
% the composition.  This is for a MOLAR AVERAGE velocity reference frame.
%
% INPUT:
%    x     (n)  Column vector of mole fractions
%    Dms   (n x n) Matrix of Maxwell-Stefan diffusion coefficients
%
% OUTPUT:
%    B (n-1 x n-1) The "B" matrix as defined in T&K eqns 2.1.21-2.1.22
%
%--------------------------------------------------------------------------

n = length(x);  % number of species.
B = zeros( n-1, n-1 );

% fill the diagonals (TK Eq. 2.1.21)
for i=1:n-1
  sumterm = 0;
  for k=1:n
    if k~=i
      sumterm = sumterm + x(k)/Dbin(i,k);
    end
  end
  B(i,i) = sumterm + x(i)/Dbin(i,n);
end

% fill the off-diagonals (TK Eq. 2.1.22)
for i=1:n-1
  for j=1:n-1
    if i~=j
      B(i,j) = -x(i) * (1/Dbin(i,j) - 1/Dbin(i,n));
    end
  end
end