function mesh_bandwidth ( element_filename )

%*****************************************************************************80
%
%% mesh_bandwidth() determines the geometric bandwidth of a mesh of elements.
%
%  Discussion:
%
%    The user supplies an element file, listing the indices of the nodes that
%    make up each element.
%
%    The program computes the geometric bandwidth associated with the mesh.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    22 October 2022
%
%  Author:
%
%    John Burkardt
%
%  Usage:
%
%    mesh_bandwidth ( 'element_filename' )
%
%  Input:
%
%    character element_filename, the file containing the element definitions.
%
  timestamp ( );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'mesh_bandwidth():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Read a file which defines a mesh in 2D, 3D, or ND,\n' );
  fprintf ( 1, '  made up of elements of uniform order.\n' );
  fprintf ( 1, '  Determine the geometric mesh bandwidth.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '    M = ML + 1 + MU.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  which is the bandwidth of the vertex connectivity\n' );
  fprintf ( 1, '  matrix.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Note that a matrix associated with variables defined\n' );
  fprintf ( 1, '  at the  nodes could have a greater bandwidth than M,\n' );
  fprintf ( 1, '  since you might have multiple variables at a vertex,\n' );
  fprintf ( 1, '  or the variable might be a vector quantity,\n' );
  fprintf ( 1, '  or physical effects might link two variables that are\n' );
  fprintf ( 1, '  not associated with vertices that are connected.\n' );
%
%  Read the triangulation data.
%
  element_node = load ( element_filename );
  [ element_num, element_order ] = size ( element_node );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Read element node data from "%s".\n', element_filename );
  fprintf ( 1, '  Element number ELEMENT_NUM  = %d\n', element_num );
  fprintf ( 1, '  Element order ELEMENT_ORDER = %d\n', element_order );
%
%  Compute the bandwidth.
%
  [ ml, mu, m ] = bandwidth_mesh ( element_num, element_order, element_node );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Lower bandwidth ML = %d\n', ml );
  fprintf ( 1, '  Upper bandwidth MU = %d\n', mu );
  fprintf ( 1, '  Total bandwidth M  = %d\n', m );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'mesh_bandwidth():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  return
end
function [ ml, mu, m ] = bandwidth_mesh ( element_num, element_order, ...
  element_node )

%*****************************************************************************80
%
%% bandwidth_mesh() determines the bandwidth of the coefficient matrix.
%
%  Discussion:
%
%    The quantity computed here is the "geometric" bandwidth determined
%    by the finite element mesh alone.
%
%    If a single finite element variable is associated with each node
%    of the mesh, and if the nodes and variables are numbered in the
%    same way, then the geometric bandwidth is the same as the bandwidth
%    of a typical finite element matrix.
%
%    The bandwidth M is defined in terms of the lower and upper bandwidths:
%
%      M = ML + 1 + MU
%
%    where 
%
%      ML = maximum distance from any diagonal entry to a nonzero
%      entry in the same row, but earlier column,
%
%      MU = maximum distance from any diagonal entry to a nonzero
%      entry in the same row, but later column.
%
%    Because the finite element node adjacency relationship is symmetric,
%    we are guaranteed that ML = MU.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    22 October 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer ELEMENT_NUM, the number of elements.
%
%    integer ELEMENT_ORDER, the order of the elements.
%
%    integer ELEMENT_NODE(ELEMENT_NUM,ELEMENT_ORDER);
%    ELEMENT_NODE(I,J) is the global index of local node J in element I.
%
%  Output:
%
%    integer ML, MU, the lower and upper bandwidths of the matrix.
%
%    integer M, the bandwidth of the matrix.
%
  ml = 0;
  mu = 0;

  for element = 1 : element_num

    for local_i = 1 : element_order
      global_i = element_node(element,local_i);

      for local_j = 1 : element_order
        global_j = element_node(element,local_j);

        mu = max ( mu, global_j - global_i );
        ml = max ( ml, global_i - global_j );

      end
    end
  end

  m = ml + 1 + mu;

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% timestamp() prints the current YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  t = now;
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end
 
