#PBS -S /bin/csh
#PBS -V
#PBS -q long
#PBS -N ec19_final_01
#PBS -W group_list=g26150
#PBS -l select=162:ncpus=24:model=has
#PBS -l walltime=84:00:00
#PBS -j oe
#PBS -o job.out
#PBS -m b
#PBS -m e
#PBS -r n

#-------------------------------------------------------------------------------
# Email the user that the job has started.
#-------------------------------------------------------------------------------

ssh bridge2 mail cdowns@predsci.com -s "${PBS_JOBID}_has_begun" < /dev/null

#-------------------------------------------------------------------------------
# Set up the variables that control where the run is executed.
#-------------------------------------------------------------------------------

set runid=${PBS_JOBNAME}
set root_disk=/nobackupp8/cjdowns

set sourcedir=~cjdowns/mas/runs/${runid}
set rundir_root=${root_disk}/mas/runs

#-------------------------------------------------------------------------------
# Create the run directory.
#-------------------------------------------------------------------------------

if (!(-d ${rundir_root})) then
  mkdir ${rundir_root}
endif

# If a directory with the same name already exists, move it to one with
# a name that has the date and time appended.
# This prevents the previous run from being wiped out if PBS decides to
# restart a job!

if (-d ${rundir_root}/${runid}) then
  set dt=`date +"%Y%m%d_%H%M%S"`
  mv ${rundir_root}/${runid} ${rundir_root}/${runid}_${dt}
endif
  
mkdir ${rundir_root}/${runid}

#-------------------------------------------------------------------------------
# Copy the files into the run directory.
#-------------------------------------------------------------------------------

cd ${rundir_root}/${runid}
cp -RHp ${sourcedir}/* .

#-------------------------------------------------------------------------------
# Run the code.
#-------------------------------------------------------------------------------

mpiexec mas -wc_limit 84:00:00 -timer mpi -wc_res 12:00 $runid >& $runid.log

#-------------------------------------------------------------------------------
# Email the user that the job has finished.
#-------------------------------------------------------------------------------

ssh bridge2 mail cdowns@predsci.com -s "${PBS_JOBID}_has_finished" < /dev/null
