% twave Driver script for wave [PHYS210-fda]
%
% Wave solves the 1+1 wave equation using the usual 3-time level,
% O(h^2) FDA.
%
% Initial data is a linear combination of left- and right-moving 
% Gaussian profiles:
%
%  u_l(x) = al * exp[ -((x - x0l) / deltal)^2 ]
%  u_r(x) = ar * exp[ -((x - x0r) / deltar)^2 ]
%
% Boundary conditions are homogeneous Dirichlet (fixed endpoints)

% Script name for tracing output.
S = 'twave';

% Solution time.
tmax = 2.0;
% deltat / deltax (Courant number)
lambda = 0.5;

% Initial data parameters.
al = 1.0;
x0l = 0.45;
deltal = 0.05;
ar = 0.5;
x0r = 0.55;
deltar = 0.1;

% Enables tracing output in wave.
trace = 50;

% Define minimum and maximum levels of discretization.
% nx_level = 2^level + 1.
level_min = 6;
level_max = 9; 

% downsample ~= 0 will downsample in the time dimension
% to facilitate convergence testing with xvs 
% (applyn xy -> <convergence test fcns>.
downsample = 1;

for level = level_min : level_max
   fprintf('\n%s: Running wave at level %d\n\n', S, level);
   tic
   [x, t, u] = wave(tmax, level, lambda, al, x0l, deltal, ar, x0r, deltar, trace);
   if downsample
      inc = 2^(level - level_min);
      t = t(1:inc:end);
      u = u(:, 1:inc:end);
      fprintf('\n');
      fprintf('%s: Output is %d x %d\n', S, length(x), length(t));
   end
   fprintf('\n');
   toc
   fprintf('\n');

   fname=sprintf('twave_%d.dat', level);
   fprintf('%s: Calling xvsout to write to %s\n', S, fname);
   xvsout(fname , t, x, u);
   fprintf('%s: Checking for %s\n', S, fname);
   system(sprintf('ls -lh %s', fname));
end
