#!/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=10:00:00

# A few parameters that might need to be changed 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"
########################################################################


ulimit -s unlimited

# Running the PCM
echo "Run PCM $iPCM is starting."
read i_myear n_myear convert_years iPCM iPEM nPCM nPCM_ini < info_PEM.txt
cp run_PCM.def run.def
srun --cpu-bind=threads --label -c${OMP_NUM_THREADS:=1} $exePCM > out_runPCM${iPCM} 2>&1
if [ ! -f "restartfi.nc" ] || ! (tail -n 1 out_runPCM${iPCM} | grep -iq "everything is cool!"); then # Check if it ended abnormally
    echo "Error: the run PCM $iPCM crashed!"
    echo "Be careful: there may be dependent jobs remaining in the SLURM queue with status 'DependencyNeverSatisfied'! You can cancel them by executing the script \"kill_launchPEM.sh\"."
    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 < 1" | bc -l) -eq 1 ]; then # Only the last 2 years are taken for the PEM
    mv Xdiurnalave.nc diags/data2reshape${iPCM}.nc
else
    cp Xdiurnalave.nc diags/data2reshape${iPCM}.nc
    mv Xdiurnalave.nc data2reshape_Y${k}.nc
fi
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++))
((i_myear++))
sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
