function allen_cahn_ode45 ( xi, nu )

%*****************************************************************************80
%
%% allen_cahn_ode45 solves the Allen-Cahn PDE using ode45.
%
%  Discussion:
%
%    This version of the equation has the form:
%
%      du/dt = nu * uxx - u* (u^2-1)/(2*xi)
%
%    uxx is the second derivative of u with respect to x;
%    nu is a positive diffusion coefficient;
%    xi is a positive parameter called the interface thickness.
%
%    The variable u is allowed the range -1 <= u <= +1.  The long-term
%    behavior of the solution is to separate into areas with u=+1 and
%    u=-1, with an intermediate interface whose width is related to xi.
%
%    The problem is solved on a 1D interval.
%
%    The boundary conditions on both ends are du/dn = 0.
%    This is handled by setting u(1)=u(2), and u(nx)=u(nx-1).
%
%    The parameter xi is described as an interface thickness.
%
%    For this problem, if nu=1, three humps appear and then:
%      xi = 0.01;   all humps stay;
%      xi = 0.015;  one hump goes away;
%      xi = 0.0155; two humps slowly disappear at different times;
%      xi = 0.02;   two humps disappear fast.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    30 June 2020
%
%  Author:
%
%    John Burkardt, Catalin Trenchea
%
%  Reference:
%
%    Jian Zhang, Qiang Du,
%    Numerical studies of discrete approximations to the Allen-Cahn
%    equation in the sharp interface limit,
%    SIAM Journal on Scientific Computing,
%    Volume 31, Number 4, pages 3042-3063, 2009.
%
%  Input:
%
%    real XI, the interface thickness.  Default is 0.015.
%
%    real NU, the diffusion coefficient.  Default is 1.0.
%    For stability in this code, NU <= 1 is recommended.
%
  addpath ( '../allen_cahn_pde' );

  if ( nargin < 1 )
    xi = 0.015;
  end
%
%  Set the diffusion coefficient.
%
  if ( nargin < 2 )
    nu = 1.0;
  end

  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'allen_cahn_ode45\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Use ode45() to solve the\n' );
  fprintf ( 1, '  Allen-Cahn 1D reaction-diffusion PDE.\n' );
  fprintf ( 1, '  Interface thickness xi = %g\n', xi );
  fprintf ( 1, '  Diffusion coefficient nu = %g\n', nu );
%
%  Set the X values.
%
  nx = 101;
  xmin = 0.0;
  xmax = 2.0;
  x = linspace ( xmin, xmax, nx );
  dx = ( xmax - xmin ) / ( nx - 1 );
%
%  Set the T values (for display only).
%
  nt = 61;
  tmin = 0.0;
  tmax = 1.0;
  tmax = 0.15;
  t = linspace ( tmin, tmax, nt );
  dt = ( tmax - tmin ) / ( nt - 1 );
  
  fprintf ( 1, '  Use %d nodes, with dx=%g in space interval [%g,%g]\n', ...
    nx, dx, xmin, xmax );
  fprintf ( 1, '  Use %d nodes, with dt=%g in time interval [%g,%g]\n', ...
    nt, dt, tmin, tmax );

  u0 = allen_cahn_initial_condition ( x );
%
%  Call ODE45 to compute the solution.
%
  [ t, u ] = ode45 ( @(t,u)allen_cahn_deriv(t,u,x,xi,nu), [ tmin, tmax ], u0 );

  nt = length ( t );
%
%  Display each solution at time T(J).
%
  jsave = 0;

  for j = 1 : nt

    if ( 0.005 <= t(j) )
      if ( t(j-1) <= 0.005 )
        jsave = j;
      end
    end

    if ( jsave == 0 || mod ( j, 10 ) == 0 )
      clf ( );
      hold ( 'on' );
      plot ( x, u(j,:), 'b-', 'linewidth', 2 );
      plot ( [x(1),x(nx)], [0.0,0.0], 'k-', 'linewidth', 2 ); 
      if ( jsave == 0 )
        legend ( 'u(t)', 'x axis', 'location', 'northwest' );
      else
        plot ( x, u(jsave,:), 'ro' );
        legend ( 'u(t)', 'x axis', 'u(t=0.005)', 'location', 'northwest' );
      end
      hold ( 'off' );
      axis ( [ xmin, xmax, -1.5, 1.5 ] );
      grid ( 'on' );
      label = sprintf ( '\\xi=%g, \\nu=%g, step=%d, t=%.3f', xi, nu, j, t(j) );
      title ( label );

      pause ( 0.02 );

    end

  end
%
%  Save last plot.
%
  filename = 'allen_cahn_ode45.png';
  print ( '-dpng', filename );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'allen_cahn_ode45\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  rmpath ( '../allen_cahn_pde' );

  return
end

