#!/bin/bash
#SBATCH --job-name=jobPCM0
#SBATCH --account=cin0391
### GENOA nodes accommodate 96 cores
#SBATCH --constraint=GENOA
### Number of Nodes to use
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=24
#SBATCH --cpus-per-task=4
#SBATCH --threads-per-core=1 # --hint=nomultithread
###SBATCH --exclusive
#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:
exePCM="gcm_64x48x32_phymars_para.e"

# Execution command:
exe_cmd="srun --cpu-bind=threads --label -c${OMP_NUM_THREADS:=1} $exePCM"
########################################################################


ulimit -s unlimited

# Running the PCM
read i_myear n_myear convert_years iPCM iPEM nPCM nPCM_ini < info_PEM.txt
echo "Run \"PCM $iPCM\" is starting."
cp run_PCM.def run.def
eval "$exe_cmd > out_runPCM${iPCM} 2>&1"
if [ ! -f "restartfi.nc" ] || ! (tail -n 100 out_runPCM${iPCM} | grep -iq "everything is cool!"); then # Check if it ended abnormally
    echo "Error: the run \"PCM $iPCM\" crashed!"
    if [ $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_launchPEM.sh\"."
    fi
    exit 1
fi

# Copy data files and prepare the next run
mv out_runPCM${iPCM} out_PCM/run${iPCM}
if [ -f "diagfi.nc" ]; then
    mv diagfi.nc diags/diagfi${iPCM}.nc
fi
if [ -f "diagsoil.nc" ]; then
    mv diagsoil.nc diags/diagsoil${iPCM}.nc
fi
if [ -f "stats.nc" ]; then
    mv stats.nc diags/stats${iPCM}.nc
fi
k=0
if [ $(echo "$k > 0" | bc) -eq 1 ]; then # Only the last 2 years are taken for the PEM
    cp Xdiurnalave.nc data2reshape_Y${k}.nc
fi
mv Xdiurnalave.nc diags/data2reshape${iPCM}.nc
cp restartfi.nc starts/restartfi${iPCM}.nc
mv restartfi.nc startfi.nc
if [ -f "restart.nc" ]; then
    cp restart.nc starts/restart${iPCM}.nc
    mv restart.nc start.nc
elif [ -f "restart1D.txt" ]; then
    cp restart1D.txt starts/restart1D${iPCM}.txt
    mv restart1D.txt start1D.txt
fi
((iPCM++))
if [ $counting -ne 0 ]; then
    i_myear=$(echo "$i_myear + 1." | bc -l)
fi
sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
