function fem_basis_t6_display ( prefix, node_index )

%*****************************************************************************80
%
%% fem_basis_t6_display() displays a finite element T6 basis.
%
%  Discussion:
%
%    This program reads a data file defining a set of nodes, and a
%    data file defining the triangulation of those nodes using 6 node triangles.
%
%    It is assumed that the 6 node triangles have straight sides, that is,
%    that each midside node lies on the line segment connecting the
%    corresponding vertices.
%
%    The display is initially "flat", but by using the manipulator
%    on the graphics menu, the user can easily get some dramatic images
%    of the basis function.
%
%  Usage:
%
%    fem_basis_t6_display ( 'prefix', node_index )
%
%    where 'prefix' is the common prefix for the FEM files:
%
%    * 'prefix'_nodes.txt,    the node coordinates.
%    * 'prefix'_elements.txt, the nodes that make up each element;
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    18 July 2023
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    character PREFIX, the common filename prefix for the data files.
%
  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'fem_basis_t6_display():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Display basis functions associated with \n' );
  fprintf ( 1, '  a finite element grid of quadratic triangles ("T6").\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  This program reads two files:\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  * node_file,     the node file,\n' );
  fprintf ( 1, '  * element_file,  the element file,\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  The user specifies a basis function by node index,\n' );
  fprintf ( 1, '  and the program displays a surface plot of that basis function.\n' );
  fprintf ( 1, '  (Use the 3D ROTATE option to see the full picture!\n' );
%
%  Get the prefix if missing.
%
  if ( nargin < 1 )
    prefix = input ( '  Enter the common file prefix:  ' );
  end
%
%  Get the node index if missing.
%
  if ( nargin < 2 )
    node_index = input ( '  Enter the node index:  ' );
  end
%
%  Construct the file names.
%
  node_filename = strcat ( prefix, '_nodes.txt' );
  element_filename = strcat ( prefix, '_elements.txt' );
%
%  Read the nodes.
%
  node_xy = load ( node_filename );
  node_xy = node_xy';
  [ dim_num, node_num ] = size ( node_xy );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Spatial dimension = %d\n', dim_num );
  fprintf ( 1, '  Number of nodes = %d\n', node_num );

  if ( dim_num ~= 2 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'FEM_BASIS_T6_DISPLAY - Fatal error!\n' );
    fprintf ( 1, '  The spatial dimension is not 2.\n' );
    error ( 'FEM_BASIS_T6_DISPLAY - Fatal error!' );
  end
%
%  Read the elements.
%
  element_node = load ( element_filename );
  element_node = element_node';
  [ element_order, element_num ] = size ( element_node );

  if ( element_order ~= 6 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'FEM_BASIS_T6_DISPLAY\n' );
    fprintf ( 1, '  This program requires that the elements have order 6.\n' );
    fprintf ( 1, '  Your elements have order %d\n', element_order );
    error ( 'FEM_BASIS_T6_DISPLAY - Fatal error!' );
  end
%
%  Set up the graph.
%
  x_min = min ( node_xy(1,:) );
  x_max = max ( node_xy(1,:) );
  y_min = min ( node_xy(2,:) );
  y_max = max ( node_xy(2,:) );

  node_min = min ( min ( element_node ) );
  node_max = max ( max ( element_node ) );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Every basis function is associated with a node.\n' );
  fprintf ( 1, '  To chooose a basis function, you specify a node.\n' );
  fprintf ( 1, '  Nodes range in value from %d to %d.\n', node_min, node_max );
%
%  Clear the figure.
%
  clf ( );
  
  fprintf ( 1, '\n' );
  
  z_min = 0.0;
  z_max = 0.0;

  for element = 1 : element_num

    pt(1,1:6) = node_xy(1,element_node(1:6,element)); 
    pt(2,1:6) = node_xy(2,element_node(1:6,element));

    local = 0;
    for j = 1 : element_order
      if ( element_node(j,element) == node_index )
        local = j;
        fprintf ( 1, '  Node %d occurs as local node %d in element %d.\n', ...
          node_index, j, element );
      end 
    end
%
%  If the node is not part of this element, we can draw the zero patch easily.
%
    if ( local == 0 )

      x = zeros(2,2);
      y = zeros(2,2);
      z = zeros(2,2);
      x(1,1) = pt(1,1);
      y(1,1) = pt(2,1);
      x(2,1) = pt(1,1);
      y(2,1) = pt(2,1);
      x(1,2) = pt(1,2);
      y(1,2) = pt(2,2);
      x(2,2) = pt(1,3);
      y(2,2) = pt(2,3);
%
%  If the node is part of this element, then we want to use an 11x11 grid of points
%  to sample the (reference) element and generate data for a smooth surface.
%
    else

      x = zeros(11,11);
      y = zeros(11,11);
      z = zeros(11,11);

      for i = 0 : 10
%
%  To use SURFACE, we need to define a logical rectangular grid of points.
%  But our region is a triangle, so the easiest thing to do is to identify
%  two corners of the grid.  
%  We try to do this in such a way that the same thing happens in each
%  element, and the resulting grid looks less haphazard.
%
        if ( local == 1 || local == 5 )
          xlo = ( ( 10 - i ) * pt(1,1) + i * pt(1,2) ) / 10;
          ylo = ( ( 10 - i ) * pt(2,1) + i * pt(2,2) ) / 10;
          xhi = ( ( 10 - i ) * pt(1,1) + i * pt(1,3) ) / 10;
          yhi = ( ( 10 - i ) * pt(2,1) + i * pt(2,3) ) / 10;
        elseif ( local == 2 || local == 6 )
          xlo = ( ( 10 - i ) * pt(1,2) + i * pt(1,1) ) / 10;
          ylo = ( ( 10 - i ) * pt(2,2) + i * pt(2,1) ) / 10;
          xhi = ( ( 10 - i ) * pt(1,2) + i * pt(1,3) ) / 10;
          yhi = ( ( 10 - i ) * pt(2,2) + i * pt(2,3) ) / 10;
        elseif ( local == 3 || local == 4 )
          xlo = ( ( 10 - i ) * pt(1,3) + i * pt(1,2) ) / 10;
          ylo = ( ( 10 - i ) * pt(2,3) + i * pt(2,2) ) / 10;
          xhi = ( ( 10 - i ) * pt(1,3) + i * pt(1,1) ) / 10;
          yhi = ( ( 10 - i ) * pt(2,3) + i * pt(2,1) ) / 10;
        end 

        for j = 0 : 10

          pp(1) = ( ( 10 - j ) * xlo + j * xhi ) / 10;
          pp(2) = ( ( 10 - j ) * ylo + j * yhi ) / 10;
          
          zz = basis_11_t6 ( pt, local, pp );

          z_min = min ( z_min, zz );
          z_max = max ( z_max, zz );

          x(i+1,j+1) = pp(1);
          y(i+1,j+1) = pp(2);
          z(i+1,j+1) = zz;

        end
      end

    end

    caxis ( 'auto' );
     
    surface ( x, y, z, 'FaceColor', 'interp' );

  end
%
%  Using SHADING FACETED draws nice little boundary lines between each patch.
%  However, the way we draw the basis function means that that surface is simply
%  covered in black lines, losing all the color information.
%
%  SHADING INTERP loses the little boundary lines, but allows the color information
%  to be seen.
%
%  You have your clumsy choice of ONE or THE OTHER.
%
  shading ( 'faceted' );
% shading ( 'interp' );

  axis ( [ x_min, x_max, y_min, y_max, z_min, z_max ] );
  axis ( 'equal' );

  xlabel ( '--X axis--' );
  ylabel ( '--Y axis--' );
  zlabel ( '--Z axis--' );

  title_string = sprintf ( 'T6 basis function for node %d', node_index );
  title ( title_string );
  view ( 0.0, 30.0 );

  filename = sprintf ( '%s_%d.png', prefix, node_index );
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'fem_basis_t6_display():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  return
end
function [ bi, dbidx, dbidy ] = basis_11_t6 ( t, i, p )

%*****************************************************************************80
%
%% basis_11_t6(): one basis at one point for the T6 element.
%
%  Discussion:
%
%    The routine is given the coordinates of the nodes of a triangle. 
%        
%           3
%          / \
%         6   5
%        /     \
%       1---4---2
%
%    It evaluates the quadratic basis function B(I)(X,Y) associated with
%    node I, which has the property that it is a quadratic function
%    which is 1 at node I and zero at the other five nodes.
%
%    This routine assumes that the sides of the triangle are straight,
%    so that the midside nodes fall on the line between two vertices.
%
%    This routine relies on the fact that each basis function can be
%    written as the product of two linear factors, which are easily
%    computed and normalized.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    08 February 2006
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real T(2,6), the coordinates of the nodes.
%
%    integer I, the index of the desired basis function.
%    I should be between 1 and 6.
%
%    real P(2), the coordinates of a point at which the basis
%    function is to be evaluated.
%
%  Output:
%
%    real BI, DBIDX, DBIDY, the values of the basis function
%    and its X and Y derivatives.
%
  if ( i < 1 || 6 < i )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'BASIS_11_T6 - Fatal error!\n' );
    fprintf ( 1, '  Basis index I is not between 1 and 6.\n' );
    fprintf ( 1, '  I = ', i );
    error ( 'BASIS_11_T6 - Fatal error!' );
  end
%
%  Determine the pairs of nodes.
%
  if ( i <= 3 )
    j1 = i4_wrap ( i + 1, 1, 3 );
    j2 = i4_wrap ( i + 2, 1, 3 );
    k1 = i + 3;
    k2 = i4_wrap ( i + 5, 4, 6 );
  else
    j1 = i - 3;
    j2 = i4_wrap ( i - 3 + 2, 1, 3 );
    k1 = i4_wrap ( i - 3 + 1, 1, 3 );
    k2 = i4_wrap ( i - 3 + 2, 1, 3 );
  end
%
%  Evaluate the two linear factors GF and HF, 
%  and their normalizers GN and HN.
%
  gf = ( p(1)    - t(1,j1) ) * ( t(2,j2) - t(2,j1) ) ...
     - ( t(1,j2) - t(1,j1) ) * ( p(2)    - t(2,j1) );

  gn = ( t(1,i)  - t(1,j1) ) * ( t(2,j2) - t(2,j1) ) ...
     - ( t(1,j2) - t(1,j1) ) * ( t(2,i)  - t(2,j1) );  

  hf = ( p(1)    - t(1,k1) ) * ( t(2,k2) - t(2,k1) ) ...
     - ( t(1,k2) - t(1,k1) ) * ( p(2)    - t(2,k1) );

  hn = ( t(1,i)  - t(1,k1) ) * ( t(2,k2) - t(2,k1) ) ...
     - ( t(1,k2) - t(1,k1) ) * ( t(2,i)  - t(2,k1) );
%
%  Construct the basis function and its derivatives.
%
  bi =        ( gf                  / gn ) * (   hf                  / hn );

  dbidx =   ( ( t(2,j2) - t(2,j1) ) / gn ) * (   hf                  / hn ) ...
          + (   gf                  / gn ) * ( ( t(2,k2) - t(2,k1) ) / hn );

  dbidy = - ( ( t(1,j2) - t(1,j1) ) / gn ) * (   hf                  / hn ) ...
          - (   gf                  / gn ) * ( ( t(1,k2) - t(1,k1) ) / hn );

  return
end
function value = i4_modp ( i, j )

%*****************************************************************************80
%
%% i4_modp() returns the nonnegative remainder of I4 division.
%
%  Discussion:
%
%    If
%      NREM = I4_MODP ( I, J )
%      NMULT = ( I - NREM ) / J
%    then
%      I = J * NMULT + NREM
%    where NREM is always nonnegative.
%
%    The MOD function computes a result with the same sign as the
%    quantity being divided.  Thus, suppose you had an angle A,
%    and you wanted to ensure that it was between 0 and 360.
%    Then mod(A,360) would do, if A was positive, but if A
%    was negative, your result would be between -360 and 0.
%
%    On the other hand, I4_MODP(A,360) is between 0 and 360, always.
%
%  Example:
%
%        I     J     MOD  I4_MODP    Factorization
%
%      107    50       7       7    107 =  2 *  50 + 7
%      107   -50       7       7    107 = -2 * -50 + 7
%     -107    50      -7      43   -107 = -3 *  50 + 43
%     -107   -50      -7      43   -107 =  3 * -50 + 43
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    02 March 1999
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer I, the number to be divided.
%
%    integer J, the number that divides I.
%
%  Output:
%
%    integer VALUE, the nonnegative remainder when I is
%    divided by J.
%
  if ( j == 0 )
    fprintf ( 1, '\n' );
    fprintf ( 1, 'I4_MODP - Fatal error!\n' );
    fprintf ( 1, '  Illegal divisor J = %d\n', j );
    error ( 'I4_MODP - Fatal error!' );
  end

  value = mod ( i, j );

  if ( value < 0 )
    value = value + abs ( j );
  end

  return
end
function value = i4_wrap ( ival, ilo, ihi )

%*****************************************************************************80
%
%% i4_wrap() forces an integer to lie between given limits by wrapping.
%
%  Example:
%
%    ILO = 4, IHI = 8
%
%    I   Value
%
%    -2     8
%    -1     4
%     0     5
%     1     6
%     2     7
%     3     8
%     4     4
%     5     5
%     6     6
%     7     7
%     8     8
%     9     4
%    10     5
%    11     6
%    12     7
%    13     8
%    14     4
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    02 October 2006
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer IVAL, an integer value.
%
%    integer ILO, IHI, the desired bounds for the integer value.
%
%  Output:
%
%    integer VALUE, a "wrapped" version of IVAL.
%
  jlo = min ( ilo, ihi );
  jhi = max ( ilo, ihi );

  wide = jhi - jlo + 1;

  if ( wide == 1 )
    value = jlo;
  else
    value = jlo + i4_modp ( ival - jlo, wide );
  end

  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
 
