function gray_scott_movie ( nx, ny, f, k, dx, dy, dt, du, dv, initial_condition )

%*****************************************************************************80
%
%% gray_scott_movie() generates Gray-Scott solutions and creates a movie.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    13 January 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer nx, ny: the number of columns and rows of data.
%
%    real f, k: the flow rate and the kill rate.
%
%    real dx, dy: the x and y spacing.
%
%    real dt: the time step.
%
%    real du, dv: the diffusion rates for u and v.
%
%    character initial_condition: a choice of one of three patterns:
%    'wavefront'
%    'bar'
%    'square'
%

%
%  Set the frame rate for the movie.
%
  FrameRate = 100;
%
%  Initialize the movie.
%
  colormap ( 'jet' );

  moviename = strcat ( 'gray_scott_', num2str(f), '_', num2str(k), '.avi' );
  V = VideoWriter ( moviename, 'Motion JPEG AVI' );
  V.FrameRate = FrameRate;
  open ( V );

  figure ( 1 );
%
%  Compute the sequence of solutions.
%
  nt = 15001;

  for it = 1 : nt
%
%  On first step, initialize the data.
%
    if ( it == 1 )

      u = ones ( nx, ny );
      v = zeros ( nx, ny );

      xm = floor ( nx / 2 );
      ym = floor ( ny / 2 );

      if ( initial_condition == 'wavefront' )
        u(xm-30:xm+30,ym-6:ym-4) = 0.75;
        u(xm-30:xm+30,ym-3:ym-1) = 0.50;
        u(xm-30:xm+30,ym  :ym+2) = 0.25;
        u(xm-30:xm+30,ym+3:ym+5) = 0.00;
        v(xm-30:xm+30,ym+3:ym+5) = 1.00;
      elseif ( initial_condition == 'bar' ) 
        u(xm-10:xm+10,ym-2:ym+2) = 0.0;
        v(xm-10:xm+10,ym-2:ym+2) = 1.0;
      else 
        u(xm-5:xm+5,ym-5:ym+5) = 0.0;
        v(xm-5:xm+5,ym-5:ym+5) = 1.0;
      end
%
%  On subsequent steps, use the Euler method to update the data.
%
    else

      uLaplace = laplacian9_torus ( u, dx, dy );
      vLaplace = laplacian9_torus ( v, dx, dy );

      dudt = du * uLaplace - u .* v.^2 + f * ( 1.0 - u );
      dvdt = dv * vLaplace + u .* v.^2 - ( f + k ) * v;

      u = u + dt * dudt;
      v = v + dt * dvdt;

    end
%
%  Display the data, and create a graphics frame, and add it to the movie.
%
    if ( mod ( it, FrameRate ) == 1 )
      image ( u, 'CDataMapping', 'scaled' );
      label = sprintf ( 'Gray-Scott U, step = %d\n', it );
      title ( label );
      axis ( 'equal' );
      axis ( 'tight' );
      set ( gca, 'xtick', [] );
      set ( gca, 'ytick', [] );
      frame = getframe ( 1 );
      writeVideo ( V, frame );
    end

  end

  close ( V );
  fprintf ( 1, '  Movie saved as "%s"\n', moviename );
%
%  Save the last image as a PNG snapshot.
%
  filename = strcat ( 'gray_scott_', num2str(f), '_', num2str(k), '.png' );
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end

