#!/bin/bash
# copy in the same dir as bench.sh
#
# If running on belenos : 
# - either run in a job (with run_in_job=1)
# - either connect through a machine with salloc
#   and run interactivly with run_in_job=0
# - don't launch interactivly in a login node
#   even with nproc=1 because of uncontrolled 
#   multithreading in emulation 
#
# June 2026, Maëlle Coulon--Decorzens 
#### A first test of bench, to be done ####
# - seed ne marche pas sur belenos en job
# - je pense que pour l'instant on peut pas lancer
#   deux experiences en même temps en job
#   ça va se marcher dessus ???? OU PAS ????
###########################################

set -eu

listexp="ArtII1"

# If set to 1, it will launch the experiment in a job
# (one job per experiment)
run_in_job=1

# number of 1D simulation to be run in parallel
# 0 to use the number of available threads
# (don't use 0 in shared partition in belenos !!!)
npara=20

# To be implemented : 
## read metric file if exists?
#use_metric_file=0

exe="bash bench.sh"
## Example with seed for reproductibility
## doesn't work on belenos
#exe="./bench.sh -seed 123 -cutoff_val 3 -cutoff_wav 1" # -noplot 

mkdir -p log

####### Some Tool function ######

function prepare_jobfile {
  #this function write the file to be submitted 
  #in a job on belenos
  wdir=$1
  to_exe=$2
  here=$(pwd)
cat > slurm_belenos_$wdir <<eod 
#!/bin/bash
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=$HTUNE_SERIE_NPROCS
#SBATCH --mem=240G
#SBATCH --time=00:03:00
#SBATCH --partition=shared
#SBATCH --job-name=$wdir
#SBATCH --output=slurm_${wdir}.out
#SBATCH --error=slurm_${wdir}.err

cd $here
echo \$(date)
$exe $to_exe
echo \$(date)
exit 0
eod
}


function execute {
  wdir=$1
  to_exe=$2
  
  if [ $npara -gt 0 ] ; then
    if [ $run_in_job -eq 1 ] ; then
     # start by checking wether $dir exist or not
      if [ -d `pwd`/WORK/$wdir ] ; then
        echo "$wdir already exist, do you want to continue ? (y/n)"
        local answ
        read -r answ
        if [[ $answ = "n" ]] ; then
          exit 0
        else
          exe="yes \"y\" | $exe"
          echo $exe
        fi
      fi
      export HTUNE_SERIE_NPROCS=$npara
      echo submiting slurm_belenos_$wdir job with $npara tasks
      prepare_jobfile $wdir "$to_exe"  
      #sbatch slurm_belenos_$wdir
    else
      eval $exe "$to_exe"
    fi
  else
    echo please set npara larger than 0 on belenos
    exit 0
  fi
}

###############################
# Loop on experiments to launch
###############################

for exp in $listexp ; do
case $exp  in
install)
# the only experiment that can be launch in a login node
to_exe="-model ARPCLIMAT -waves 1 -metrics COUCOU_COUCOU -wdir $exp -param param_test -sample_size_next_design 3 -GCM pre -noplot"
execute "$wdir" "$to_exe"
;;

tke)
### A TESTER quand GABLS est réparé
waves="`seq 1 10`"
metrics="GABLS1_REF_zav-130-190-u_8_9,GABLS1_REF_zav-60-100-tke_8_9,GABLS1_REF_zav-20-60-tke_8_9,GABLS1_REF_zav-130-160-theta_8_9,GABLS1_REF_zav-30-60-theta_8_9"
nb_simu=40
param=param_tke
to_exe="-model ARPCLIMAT -waves \"$waves\" -metrics $metrics -wdir $exp -param $param -sample_size_next_design $nb_simu -noplot"
execute "$wdir" "$to_exe"
;;

first_test*) # exp pour les dvp en cours
waves=""
opt_model="-nlev 91"
metrics="ARMCU_REF_zav-400-600-qv_7_9,ARMCU_REF_zav-400-600-theta_7_9"
param=param_test  
nb_simu=5
to_exe="-model ARPCLIMAT -opt_model "$opt_model" -waves "$waves" -metrics $metrics -wdir $exp -param $param -sample_size_next_design $nb_simu -noplot" 
execute "$wdir" "$to_exe"
;;

ArtII1)
metrics="ARMCU_REF_zav-400-600-theta_7_9,ARMCU_REF_zav-400-600-qv_7_9,ARMCU_REF_nebmax_7_9,RICO_SHORT_nebmax_19_25,SANDU_REF_neb4zave_50_60"
wdir=${exp}_3
to_exe="-model ARPCLIMAT -opt_model \"-nlev 91\" -waves \"1 2\" -metrics $metrics -wdir $wdir -param param_test -sample_size_next_design 50 -noplot"
execute "$wdir" "$to_exe"
 ;;
 
esac
done


exit

