#!/bin/bash
### Partition to use
#SBATCH --account=cin0391
#SBATCH --constraint=GENOA
### Number of nodes/cores to use
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=24
#SBATCH --cpus-per-task=8
#SBATCH --threads-per-core=1 # --hint=nomultithread
### Job information
#SBATCH --job-name=jobPCM0
#SBATCH --output=jobPCM_%j.out
#SBATCH --time=4:00:00

########################################################################
# Modify here the parameters depending on your setup
####################################################
# Path to the arch.env to source:
source ../trunk/LMDZ.COMMON/arch.env

# Number of threads to use (must be the same as "#SBATCH --cpus-per-task=" above)
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK
export OMP_STACKSIZE=400M

# Name of executable for the PCM:
pcm_exe="gcm_64x48x32_phymars_para.e"

# Execution command:
exec_cmd="srun --ntasks-per-node=${SLURM_NTASKS_PER_NODE} --cpu-bind=none --mem-bind=none --label -- ./adastra_cpu_binding.sh ./$pcm_exe"
########################################################################


ulimit -s unlimited

# Checking the executable
if [ ! -f $pcm_exe ]; then
    echo "Error: required file \"$pcm_exe\" not found!"
    exit 1
fi

# Running the PCM
read n_yr_sim ntot_yr_sim r_plnt2earth_yr i_pcm_run i_pem_run n_pcm_runs n_pcm_runs_ini < pem_workflow.sts
echo "Run \"PCM $i_pcm_run\" is starting."
cp run_pcm.def run.def
eval "$exec_cmd > run.log 2>&1"
if [ ! -f "restartfi.nc" ] || ! (tail -n 100 run.log | grep -iq "everything is cool!"); then # Check if it ended abnormally
    echo "Error: the run \"PCM $i_pcm_run\" crashed!"
    if [ $exec_mode -ne 0 ]; then
        echo "Be careful: there may be dependent jobs remaining in the queue! You can cancel them by executing the script \"kill_pem_workflow.sh\"."
    fi
    exit 1
fi

# Copy data files and prepare the next run
mv run.log logs/run_pcm${i_pcm_run}.log
if [ -f "diagfi.nc" ]; then
    mv diagfi.nc diags/diagfi${i_pcm_run}.nc
fi
if [ -f "diagsoil.nc" ]; then
    mv diagsoil.nc diags/diagsoil${i_pcm_run}.nc
fi
if [ -f "stats.nc" ]; then
    mv stats.nc diags/stats${i_pcm_run}.nc
fi
k=0
if [ $(echo "$k > 0" | bc) -eq 1 ]; then # Only the last 2 years are taken for the PEM
    cp Xoutdaily4pem.nc Xoutdaily4pem_Y${k}.nc
    cp Xoutyearly4pem.nc Xoutyearly4pem_Y${k}.nc
fi
mv Xoutdaily4pem.nc diags/Xoutdaily4pem${i_pcm_run}.nc
mv Xoutyearly4pem.nc diags/Xoutyearly4pem${i_pcm_run}.nc
cp restartfi.nc starts/restartfi${i_pcm_run}.nc
mv restartfi.nc startfi.nc
if [ -f "restart.nc" ]; then
    cp restart.nc starts/restart${i_pcm_run}.nc
    mv restart.nc start.nc
elif [ -f "restart1D.txt" ]; then
    cp restart1D.txt starts/restart1D${i_pcm_run}.txt
    mv restart1D.txt start1D.txt
fi
((i_pcm_run++))
sed -i "1s/.*/$n_yr_sim $ntot_yr_sim $r_plnt2earth_yr $i_pcm_run $i_pem_run $n_pcm_runs $n_pcm_runs_ini/" pem_workflow.sts
