#!/bin/bash
#SBATCH --nodes=6                          #A
#SBATCH --ntasks-per-node=4                #B
#SBATCH --cpus-per-task=5                  #C
#SBATCH -J clim
#SBATCH --time=13:00:00
#SBATCH --output run_month1.%j.out
##SBATCH --mem=8GB
#SBATCH --constraint=[BDW28]
#SBATCH --exclusive

## Script to run chained simulations on Occigen
## (uses script "run0" and reference file "run.def.ref")
## Set values of "num_now" and "num_end" in the script below
## to set initial month # and final month # of the simulation

# source the environment (you might need to adapt this path to your case)
source ../trunk/LMDZ.COMMON/arch/arch-X64_OCCIGEN.env

set -xv
ls -al

## set starting month and ending month below:
num_now=1
num_end=12
(( num_previous = $num_now - 1 ))

echo "$num_previous" > num_run
# next month number
(( num_next = $num_now + 1 ))
# true (i.e. modulo 12) month number
(( true_num = $num_now % 12 ))

\rm -f  error; touch error

# environment setup for OpenMP:
export I_MPI_DOMAIN=auto
export I_MPI_PIN_RESPECT_CPUSET=0
#Make sure that OMP_NUM_THREADS = cpus-per-task * KMP_HW_SUBSET
export KMP_HW_SUBSET=1T
export OMP_NUM_THREADS=5                    #C
export OMP_STACKSIZE=400M
export KMP_AFFINITY=granularity=fine,compact,1,0,verbose

case $true_num in
  1 ) sed s/9999/61/ run.def.ref > run.def ; run0 >> error ;;    #1
  2 ) sed s/9999/66/ run.def.ref > run.def ; run0 >> error ;;    #2
  3 ) sed s/9999/66/ run.def.ref > run.def ; run0 >> error ;;    #3
  4 ) sed s/9999/65/ run.def.ref > run.def ; run0 >> error ;;    #4
  5 ) sed s/9999/60/ run.def.ref > run.def ; run0 >> error ;;    #5
  6 ) sed s/9999/54/ run.def.ref > run.def ; run0 >> error ;;    #6
  7 ) sed s/9999/50/ run.def.ref > run.def ; run0 >> error ;;    #7
  8 ) sed s/9999/46/ run.def.ref > run.def ; run0 >> error ;;    #8
  9 ) sed s/9999/47/ run.def.ref > run.def ; run0 >> error ;;    #9
 10 ) sed s/9999/47/ run.def.ref > run.def ; run0 >> error ;;    #10
 11 ) sed s/9999/51/ run.def.ref > run.def ; run0 >> error ;;    #11 
  0 ) sed s/9999/56/ run.def.ref > run.def ; run0 >> error ;;    #12
  * ) echo "error" ;;
esac

# launch job for next month
if (( $num_next <= $num_end )) ; then
  cp -f run_month$num_now tmp
  sed -e "s@run_month${num_now}@run_month${num_next}@" \
      -e "s@num_now=${num_now}@num_now=${num_next}@" tmp > run_month$num_next
  rm tmp
  
  sbatch run_month$num_next
fi

