%% Model Driver Script

% This script provides a model driver script to run
% the sudden expansion test case.

clc;
close all;
clear;

% pause off
% fprintf('Pause turned off. To turn back on, use "pause on".\n\n');

app.Debug_Mode = false;


%% Set up Setup and Plot/Save Script Names and Specify Run Directory

% Initialize setup script and plot scripts.
app.SetupScript = 'SuddenExpansionSetup';
app.PlotScript = 'SuddenExpansionPlots';

% Specify the folder name for this run.
% All the codes and output data of this run will be saved in the directory
% "trunk/Save/app.savemat/"
app.savemat = 'SuddenExpansion_test';


%% Set up Codes and Working Directories (No Need to Modify!)

% Delete all existing user-added paths.

restoredefaultpath;

% Extract the trunk directory
d0 = pwd;
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***'); disp(' ');
    disp([sprintf('%s/Save/%s/',d0,app.savemat),' already exists.']);
    disp(' ');
    error('Terminating run due to pre-existing output files');
end

% Copy subdirectory of Cases to the run folder
tmpDir = pwd;
dirName = tmpDir(strfind(tmpDir,'Cases/')+6:end);

mkdir(sprintf('%s/Save/%s/%s',d0,app.savemat,dirName))
copyfile(sprintf('%s',pwd),...
    sprintf('%s/Save/%s/%s',d0,app.savemat,dirName));
mkdir(sprintf('%s/Save/%s/Src',d0,app.savemat))
copyfile(sprintf('%s/Src',d0),...
    sprintf('%s/Save/%s/Src',d0,app.savemat));

% Move working directory to the run folder
if ~app.Debug_Mode
    d_current = cd(sprintf('%s/Save/%s',d0,app.savemat));
    
    
    % Mex advection subroutines - Note that these options are specified for the mex function
    % to be compatible with gcc 4.4.6
    cd Src/Advect
    % mex CXXFLAGS='-ansi -fexceptions -fPIC -fno-omit-frame-pointer -pthread' UVadvect_TVD_mx.cpp
    % mex CXXFLAGS='-ansi -fexceptions -fPIC -fno-omit-frame-pointer -pthread' UVadvect_DO_mx.cpp
    % mex CXXFLAGS='-ansi -fexceptions -fPIC -fno-omit-frame-pointer -pthread' UVadvect_DO_CDS_mx.cpp
    % mex CXXFLAGS='-ansi -fexceptions -fPIC -fno-omit-frame-pointer -pthread' SCAadvect_TVD_mx.cpp
    % mex CXXFLAGS='-ansi -fexceptions -fPIC -fno-omit-frame-pointer -pthread' SCAadvect_mx.cpp
    % mex CXXFLAGS='-ansi -fexceptions -fPIC -fno-omit-frame-pointer -pthread' SCAadvect_CDS_mx.cpp
    cd ../..
    
    % Add directories of the source codes
    addpath('Src/Advect');
    addpath('Src/MatrixOps');
    addpath('Src/NSSolvers');
    addpath('Src/Util');
    addpath(dirName)
else
    run(sprintf('%s/setupp.m',d0))
end
%% Specify Parameters for the Run

% Build app Structure.
app.Nx = 100;
app.Ny = 50;
app.T = 50;
app.dt = 5/200;
app.nu = 0.001;
app.PlotIntrvl = 40;

% Call Solver.
disp('Fluids Solver for Sudden Expansion \n');
[P, u, v, rho, NodeP, Nodeu, Nodev] = FluidsSolver2(app, app.SetupScript, app.PlotScript);


%% Move back to the Orginal Working Directory

if ~app.Debug_Mode
    cd(d_current);
end
% zip the Src folder, the subdirectory of Cases, and a copy of the log file
zip(sprintf('%s/Save/%s/runfiles.zip',d0, app.savemat),{sprintf('%s/Save/%s/Src',d0,app.savemat),...
    sprintf('%s/Save/%s/%s',d0,app.savemat,dirName),...
    sprintf('%s/Save/%s/%s.log',d0,app.savemat,app.savemat)})
try
    rmdir(sprintf('%s/Save/%s/Src',d0,app.savemat),'s')
catch
    rmdir(sprintf('%s/Save/%s/Src',d0,app.savemat),'s')
end
rmdir(sprintf('%s/Save/%s/%s',d0,app.savemat,dirName),'s')

