function wvburg(action)
%WVBURG   Plot characteristic curves for the inviscid Burgers equation
%
%         <DESCRIPTION>
%         wvburg is a function which provides a graphical interface for
%         plotting and exploring characteristic curves in the xt-plane 
%         for an initial value problem for the inviscid Burgers' equation
%
%                    u_t + uu_x = 0,  u(x,0) = f(x).
%
%         <TO RUN>
%         Enter wvburg at the MATLAB prompt with no options.
%
%         <USE>
%         Once started, editable fields in the interface are 
%
%           Time Interval   - Plot characteristics for t0 < t < t1
%           X Interval      - Plot characteristics over the range x0 < x < x1
%           Characteristics - Number of characteristic curves to plot in the
%                             plot region t0 < t < t1, x0 < x < x1.
%           u(x,0)          - Function of x defining the initial condition.   
%                            
%         Button options include 
%
%           REDRAW  -  Draw the characteristic curves after setting the
%                      time interval, x interval, characteristics, and
%                      initial condition.
%           ZOOM    -  Toggle the zoom feature on/off.  When on, click on
%                      the plot display to zoom in or out.
%           CLOSE   -  To exit the interface.

%Created:  6/95 by R. Knobel
%Last Modified: 4/99 by R. Knobel

if nargin < 1
   action = 'start';
end

% If this is not the initial startup of wvburg, collect graphics
% handles from the wvburg figure:
if ~strcmp(action,'start'),
   handles=get(gcf,'UserData');
   hMinT = handles(1);
   hMaxT = handles(2);
   hMinX = handles(3);
   hMaxX = handles(4);
   hChar = handles(5);
   hFunction =handles(6);
   hAxes = handles(7);
   hZoom = handles(8);
end;

% If this is the initial startup of wvburg, open a figure window, 
% create the editable fields and buttons, and initialize the plot:

if strcmp(action,'start'),
   
   figure;
   set(gcf, ...
      'NumberTitle','off', ...
      'Name','Characteristics of Burgers Equation', ...
      'backingstore','off',...
      'Units','normalized');
   
   top=0.95;
   bottom=0.05;
   left=0.82;
   yInitLabelPos=0.90;
   btnWid = 0.13;
   btnHt=0.06;
   btnOffset=0.02;
   spacing=0.02;
   
   % The console frame
   
   frmBorder=0.02;
   yPos=0.02;
   frmPos=[.79 .15 btnWid+3*frmBorder .82];
   h=uicontrol( ...
      'Style','frame', ...
      'Units','normalized', ...
      'Position', frmPos, ...
      'ForegroundColor','white',...
      'BackgroundColor','black');
   
   % The time interval
   
   uicontrol( ...
      'Style','text',...
      'Units','normalized',...
      'Position', [.81 .905 .15 .04],...
      'BackgroundColor','black',...
      'ForegroundColor','red',...
      'String','Time t');
   
   uicontrol( 'Style','text',...
      'Units','normalized',...
      'Position', [.81 .86 .08 .04],...
      'BackgroundColor','black',...
      'ForegroundColor','white',...
      'String','Initial');
   MinT=0;
   hMinT=uicontrol( ...
      'Style','edit',...
      'Units','normalized',...
      'Position', [.89 .86 .07 .04],...
      'BackgroundColor','white',...
      'String',num2str(MinT));
   
   uicontrol( 'Style','text',...
      'Units','normalized',...
      'Position', [.81 .81 .08 .04],...
      'BackgroundColor','black',...
      'ForegroundColor','white',...
      'String','Final');
   MaxT = 5;
   hMaxT=uicontrol( ...
      'Style','edit',...
      'Units','normalized',...
      'Position', [.89 .81 .07 .04],...
      'BackgroundColor',[1 1 1],...
      'String',num2str(MaxT));
   
   % The x interval
   
   uicontrol( ...
      'Style','text',...
      'Units','normalized',...
      'Position', [.81 .73 .15 .04],...
      'BackgroundColor','black',...
      'ForegroundColor','red',...
      'String','Range x');
   
   uicontrol( 'Style','text',...
      'Units','normalized',...
      'Position', [.81 .68 .06 .04],...
      'BackgroundColor','black',...
      'ForegroundColor','white',...
      'String','Min');
   MinX = -10;
   hMinX=uicontrol( ...
      'Style','edit',...
      'Units','normalized',...
      'Position', [.87 .68 .09 .04],...
      'BackgroundColor',[1 1 1],...
      'String',num2str(MinX));
   
   uicontrol( 'Style','text',...
      'Units','normalized',...
      'Position', [.81 .63 .06 .04],...
      'BackgroundColor','black',...
      'ForegroundColor','white',...
      'String','Max');
   MaxX = 10;
   hMaxX=uicontrol( ...
      'Style','edit',...
      'Units','normalized',...
      'Position', [.87 .63 .09 .04],...
      'BackgroundColor',[1 1 1],...
      'String',num2str(MaxX));
   
   % Number of characteristics to plot
   
   uicontrol(...
      'Style','text',...
      'Units','normalized',...
      'Position',[.81 .55 .15 .04],...
      'BackgroundColor','black',...
      'ForegroundColor','red',...
      'String','Number of');
   uicontrol(...
      'Style','text',...
      'Units','normalized',...
      'Position',[.81 .51 .15 .04],...
      'BackgroundColor','black',...
      'ForegroundColor','red',...
      'String','Characteristics');
   
   num_char = 40;
   hChar=uicontrol( ...
      'Style','edit',...
      'Units','normalized',...
      'Position',[.85 .47 .07 .04],...
      'BackgroundColor','white',...
      'String',num2str(num_char));  
   
   % The Zoom button
   
   hZoom = uicontrol( ...
      'Style','push', ...
      'Units','normalized', ...
      'Position',[left .35  btnWid btnHt], ...
      'String','Zoom Off', ...
      'Callback','wvburg(''zoom'')');
   
   % The Redraw button
   
   uicontrol( ...
      'Style','push', ...
      'Units','normalized', ...
      'Position',[left .27  btnWid btnHt], ...
      'String','Redraw', ...
      'Callback','wvburg(''restart'')');
   
   % The CLOSE button
   
   uicontrol('Style','Pushbutton', ...
      'Units','normalize',...
      'Position',[left .19 btnWid btnHt], ...
      'Callback','wvburg(''close'')',...
      'String','Close');
      
   % Function field for the initial condition u(x,0) = f(x)
   
   bgcolor = get(gcf,'Color');
   textcolor = [0 0 0];
   if max(bgcolor)<.5 
      textcolor = [1 1 1];
   end;
   
   f = 'exp(-x^2)'; 
   uicontrol('Style','text',...
      'Units','normalized',...
      'Position',[.23 .07 .10 .045],...
      'BackgroundColor',bgcolor,...
      'ForegroundColor',textcolor,...
      'String','u(x,0) = ');
   
   hFunction=uicontrol('Style','edit',...
      'Units','normalized',...
      'Position',[.33 .07 .3 .045],...
      'BackgroundColor','white',...
      'ForegroundColor','black',...
      'String',f);
   

   % Initialize the plot area and store the graphic handles
   
   hAxes = axes(...
      'Position',[.12 .28 .6 .6],...
      'XLim',[MinX MaxX],...
      'YLim',[MinT MaxT]);
   hold on;
   xlabel('x');
   ylabel('t');
   title('Characteristics for Inviscid Burgers Equation');
   
   handles=[hMinT hMaxT hMinX hMaxX hChar hFunction hAxes hZoom]; 
   set(gcf,'UserData',handles);
      
   wvburg('restart')
   
% Refresh the characteristic plot:
   
elseif strcmp(action,'restart'),
   
   f       = get(hFunction,'String');
   MinT    = str2num(get(hMinT,'String'));
   MaxT    = str2num(get(hMaxT,'String'));
   MinX    = str2num(get(hMinX,'String'));
   MaxX    = str2num(get(hMaxX,'String'));
   NumChar = str2num(get(hChar,'String'));
   
   % vectorize the initial condition function if not already
   % vectorized. 
   l = length(f);
   for k = fliplr(find((f=='^') | (f=='*') | (f=='/')))
      if ~strcmp(f(k-1),'.')
         f = [f(1:k-1) '.' f(k:l)];
         l = l+1;
      end
   end
   vector_f = f;
   
   % calculate start and end points of the characteristic lines
   x_0 = linspace(MinX,MaxX,NumChar);
   t_0 = MinT*ones(size(x_0));
   
   x   = x_0;
   u_0 = eval(vector_f);
   
   t_1 = MaxT*ones(size(x_0));
   x_1 = u_0 .* t_1 + x_0;
   
   % reset the plot area and plot the new characteristic lines
   cla;
   set(hAxes, 'XLim',[MinX MaxX], 'YLim',[MinT MaxT]);
   
   for i=1:NumChar
      plot( [x_0(i) x_1(i)], [t_0(i) t_1(i)] )
   end;
   
% Toggle the zoom feature:
elseif strcmp(action,'zoom'),
   
   if strcmp(get(hZoom,'String'),'Zoom On'),
      zoom off;
      set(hZoom,'String','Zoom Off')
      set(gcf,'Pointer','arrow');
   else
      zoom on;
      set(hZoom,'String','Zoom On')
      set(gcf,'Pointer','crosshair')
   end
   
% Exit:
elseif strcmp(action,'close'),
   
   close
   
end



  