% This is a generic driver file for the 2.29 code

function [P u v rho NodeP Nodeu Nodev] = GenericDriver

%% 01 set up paths 

restoredefaultpath
setuppaths;

%% 02 specify run files and save location

app.SetupScript = 'SetUpScript' ;                   % [string] indicates the setup script associated with the run; see trunk/Examples for examples of setup scripts from which the user can begin to compose their own
app.PlotScript  = 'GenericPlotScript';              % [string] indicates the plot script associated with the run; NOTE GenericPlotScript has yet to be created.
app.SaveScript  = 'GenericSaveScript2';             % [string] indicates the save script associated with the run; app.SaveScript is called every app.SaveIntrvl timesteps

app.savemat     = 'run0';                           % [string] name of folder within Save containing .mat results files, log file, and zipped run files
app.saveimg     = 'run0';                           % [string] name of folder within Save containing image files
app.SaveIntrvl     = 100;                           % [integer] results from the solver (.mat files) will be saved every app.SaveIntrvl timesteps
app.SaveIntrvl_log = 10;                            % [integer] specific values (kinetic energy, mean pressure, etc.) are recorded in the log file every app.SaveIntrvl_log timesteps
app.PlotIntrvl     = 100;                           % [integer] results from the solver (image files) will be saved every app.PlotIntrvl timesteps
app.solver         = 'FluidsSolver2';               % [string] solver called in this driver script, this line does NOT call the solver


%% 03 Save copies of defining scripts.

d0=fileparts([pwd,filesep]);
d0=d0(1:strfind(d0,'trunk')+4);

if ~exist(sprintf('%s/Save/%s/',d0,app.savemat),'dir')
   mkdir(sprintf('%s/Save/%s/',d0,app.savemat))
else
   disp (['***Error output directory ',sprintf('%s/Save/%s/',d0,app.savemat),' already exists.']);
   error ('Terminating run due to pre-existing output files');
end

%  Preserve dates.  Only in Linux.
eval(['!/bin/cp -f --preserve=all ',sprintf('%s.m',mfilename('fullpath')),' ',sprintf('%s/Save/%s/',d0,app.savemat)])
eval(['!/bin/cp -f --preserve=all ',sprintf('%s/%s.m',pwd,app.SetupScript),' ',sprintf('%s/Save/%s/',d0,app.savemat)])
eval(['!/bin/cp -f --preserve=all ',sprintf('%s/%s.m',pwd,app.PlotScript),' ',sprintf('%s/Save/%s/',d0,app.savemat)])
eval(['!/bin/cp -f --preserve=all ',sprintf('%s/Src/NSSolvers/%s.m',d0,app.solver),' ',sprintf('%s/Save/%s/',d0,app.savemat)])

zip(sprintf('%s/Save/%s/runfiles.zip',d0, app.savemat),{sprintf('%s/Save/%s/%s.m',d0,app.savemat,mfilename), ...
    sprintf('%s/Save/%s/%s.m',d0,app.savemat,app.SetupScript), ...
    sprintf('%s/Save/%s/%s.m',d0,app.savemat,app.PlotScript), ...
    sprintf('%s/Save/%s/%s.m',d0,app.savemat,app.solver)});

delete(sprintf('%s/Save/%s/%s.m',d0,app.savemat,mfilename), ...
       sprintf('%s/Save/%s/%s.m',d0,app.savemat,app.SetupScript), ...
       sprintf('%s/Save/%s/%s.m',d0,app.savemat,app.PlotScript), ...
       sprintf('%s/Save/%s/%s.m',d0,app.savemat,app.solver))

%% 04 Call Solver
tic;
[P, u, v, rho, NodeP, Nodeu, Nodev] = FluidsSolver2(app,app.SetupScript,app.PlotScript);
etime = tos;
fid = fopen(sprintf('%s/Save/%s/%s.log',d0,app.savemat,app.savemat),'a');
fprintf(fid,'\n\nelapsed time = %f\n\n',etime);
fclose(fid);


















