#!/bin/ksh
#set -x
######################################################################
#
############### User's section #######################################
##
## - Define architecture and coupler (oasis3 or oasis4)
arch=Linux      
coupler=oasis3
host=`uname -n`
user=`whoami`

CHAN=MPI1 # MPI1 or MPI2; 
          # MPI2 not possible in paralle mode
MPILIB=mpich #lam or mpich (make sure to compile oasis with the good library)
#
run_oasis3_ipslpara=0 
   # 0 or 1 for OASIS3 running on 1 or 2 pes with IPSL parallelisation
   # if 0, OASIS3 must have been compiled without CPP key -Duse_oasis_para 
   # if 1, OASIS3 must have been compiled with CPP key -Duse_oasis_para
#
run_oasis3_cmccpara=0 
   # 0 or 1 for OASIS3 running on 1 or 2 pes with CMCC parallelisation
   # if 1, OASIS3 must have been compiled with option -Duse_oasis_cmcc_para
#
## - Define paths
srcdir=`pwd`
datadir=$srcdir/data_oasis3

toyname=`basename $srcdir`

if [ $MPILIB == lam ] ; then
## - Define LAMRUN, only used for arch=Linux
LAMRUN=/usr/local/lam7.1.4/bin/
elif [ $MPILIB == mpich ] ; then
## - Define MPIRUN, only used for arch=Linux
MPIRUN=/usr/local/pgi/linux86/9.0/mpi/mpich/bin/mpirun
fi


## - Define name of the executable for the coupler
#
coupler_exe=oasis3.$CHAN.x
#

# - Toy models: Define number of processes to run each executable
#               and name of executables
rundir=/home/$user/prism/src/mod/oasis3/examples/toysimple/work
#name of the executables
    exe1=toyocn
    exe2=toyatm
# number of processes for each executable
    nproc_exe1=1
    nproc_exe2=1
# number of processes involved in the coupling for the namcouple
    ncpl_exe1=$nproc_exe1
    ncpl_exe2=$nproc_exe2

############### End of user's section ################################
#
if [ $run_oasis3_ipslpara == 1 ] && [ $run_oasis3_cmccpara == 1 ] ; then
   echo 'You have to choose between ipsl parallel and cmcc parallel mode'
   exit
fi
# number of processes for OASIS3 executable
    nproc_oa3=1
if [ $run_oasis3_ipslpara == 1 ] || [ $run_oasis3_cmccpara == 1 ] ; then
    nproc_oa3=2
fi

echo ''
echo '*****************************************************************'
echo '*** '$toyname' : '$run
echo ''
echo 'Rundir       :' $rundir
echo 'Architecture :' $arch
echo 'Host         : '$host
echo 'User         : '$user
echo 'Coupler      :' $coupler
echo ''
if [ $run_oasis3_ipslpara == 1 ] ; then
echo 'oasis3 runs on ' $nproc_oa3 'processes with IPSL parallelisation mode'
elif [ $run_oasis3_cmccpara == 1 ] ; then
echo 'oasis3 runs on ' $nproc_oa3 'processes with CMCC parallelisation mode'
else
echo 'oasis3 runs not parallel on 1 process'
fi
echo $exe1' runs on '$nproc_exe1 'process(es)'
echo $exe2' runs on '$nproc_exe2 'process(es)'
echo ''
echo ''
######################################################################
###
### 1. Copy source example directory containing everything needed
###    into rundir

\rm -fr $rundir
mkdir -p $rundir

cp -f $srcdir/*nc  $rundir/.

cp -f $datadir/*txt $rundir/.
cp -f $datadir/*.jnl $rundir/.

cp -f $srcdir/$exe1 $rundir/.
cp -f $srcdir/$exe2 $rundir/.

cp -f $srcdir/$coupler_exe $rundir/.

if [ $run_oasis3_ipslpara == 1 ] ; then
cp -f $datadir/namcouple_0 $rundir/namcouple
cp -f $datadir/namcouple_1 $rundir/namcouple_1
cp -f $datadir/fdocn.nc $rundir/.
cp -f $datadir/fdatm.nc $rundir/.
else
cp -f $datadir/namcouple $rundir/.
cp -f $datadir/fdatm.nc $rundir/.
cp -f $datadir/fdocn.nc $rundir/.
fi
#
if [ $run_oasis3_ipslpara == 1 ] ||[ $run_oasis3_cmccpara == 1 ] ; then
cp -f $datadir/grids.nc $rundir/.
cp -f $datadir/masks.nc $rundir/.
fi

ed $rundir/namcouple <<EOF
g/MODE_MPI/s/MODE_MPI/$CHAN
g/nproc_exe1/s/nproc_exe1/$nproc_exe1/
g/ncpl_exe1/s/ncpl_exe1/$ncpl_exe1/
g/nproc_exe2/s/nproc_exe2/$nproc_exe2/
g/ncpl_exe2/s/ncpl_exe2/$ncpl_exe2/
w
q
EOF

if [ $run_oasis3_ipslpara == 1 ]; then
mv $rundir/namcouple $rundir/namcouple_0
ed $rundir/namcouple_1 <<EOF
g/nproc_exe1/s/nproc_exe1/$nproc_exe1/
g/ncpl_exe1/s/ncpl_exe1/$ncpl_exe1/
g/nproc_exe2/s/nproc_exe2/$nproc_exe2/
g/ncpl_exe2/s/ncpl_exe2/$ncpl_exe2/
w
q
EOF
fi
#

cd $rundir

######################################################################
###
### 3. Creation of configuration scripts

###---------------------------------------------------------------------
### Linux
###---------------------------------------------------------------------

if [ $MPILIB == mpich ] ; then
if [ $arch == Linux ] ; then

  cat <<EOF>> $rundir/appl-linux.conf
$host 0 $rundir/$coupler_exe
EOF
if [ $run_oasis3_ipslpara == 1 ] || [ $run_oasis3_cmccpara == 1 ]; then
  cat <<EOF>> $rundir/appl-linux.conf
$host 1 $rundir/$coupler_exe
EOF
fi

  count=0
  while [[ $count -lt $nproc_exe1 ]];do
   cat <<EOF >> $rundir/appl-linux.conf
$host 1 $rundir/$exe1
EOF
   (( count += 1 ))
  done

  count=0
  while [[ $count -lt $nproc_exe2 ]];do
   cat <<EOF >> $rundir/appl-linux.conf
$host 1 $rundir/$exe2
EOF
   (( count += 1 ))
  done

else 
  echo 'For architecture='$arch ' you have to add your execution'
  exit
fi
fi

######################################################################
###
### 4. Execute the model
echo $toyname 'is executed or submitted to queue.'
echo 'Results are found in rundir : '$rundir

if [ $MPILIB == mpich ] ; then

if [ $arch == Linux ] ; then

  echo 'Executing the model using '$MPIRUN 
  $MPIRUN -p4pg appl-linux.conf ./$coupler_exe > runjob.err
    
fi

elif [ $MPILIB == lam ] ; then

if [ $arch == Linux ] && [ $CHAN == MPI1 ]; then

     echo 'Executing the model using LAM in mode MPI1'
     $LAMRUN/mpiexec -boot -np $nproc_oa3 $coupler_exe : -np $nproc_exe1 $exe1 : -np $nproc_exe2 $exe2 > runjob.err

elif [ $arch == Linux ] && [ $CHAN == MPI2 ]; then

     echo 'Executing the model using LAM in mode MPI2' 
     $LAMRUN/mpiexec -boot -np 1 $coupler_exe > runjob.err

fi

fi
 

######################################################################



