#!/bin/bash
#SBATCH -J c17_scf
#SBATCH --nodes=1 --ntasks-per-node=40
#SBATCH -t 288:00:00
#SBATCH --mail-user=euanbassey@ucsb.edu
#SBATCH --mail-type=start,end

export JOB=c17_scf
cd $SLURM_SUBMIT_DIR

#   unlimit the stack size
ulimit -s unlimited

#   What node are we running on?
echo $SLURM_JOB_NODELIST

#   Directory stuff- may be useful if we have to use /scratch
export DIR=$SLURM_SUBMIT_DIR
echo "submit directory: "
echo $SLURM_SUBMIT_DIR

#   Load the relevant modules
module purge
module load intel/18
module load openmpi/2.1.0
module load crystal17

echo "{\"status\": \"started spin-lock\"}" > status.json

#   Run the spin-locked job
mpirun -np $SLURM_NTASKS Pcrystal < INPUT >& out

#   Remove the unwanted files
rm fort.*.pe* dffit* diis* fort.79

# Copy useful files
cp fort.9 fort.20
cp INPUT spinlk_hyperfine.input
cp out spinlk_hyperfine.out

echo "{\"status\": \"completed spin-lock\"}" > status.json

# Edit the INPUT file
sed -i ':a;N;$!ba;s/\nSPINLOCK.*SMEAR\n0.005//g' INPUT
sed -i ':a;N;$!ba;s/\nSAVEWF/\nSAVEWF\nGUESSP/g' INPUT
sed -i ':a;N;$!ba;s/\nFMIXING\n70/\nFMIXING\n30/g' INPUT

echo "{\"status\": \"started spin-relax\"}" > status.json

# Run the spin-relaxed job
mpirun -np $SLURM_NTASKS Pcrystal < INPUT >& out

#   Remove the unwanted files
rm fort.*.pe* dffit* diis* fort.79

# Copy useful files
cp fort.9 fort.20
cp INPUT spinrlx_hyperfine.input
cp out spinrlx_hyperfine.out

echo "{\"status\": \"completed spin-lock\"}" > status.json

# Transfer the properties.input script to INPUT
cp properties.input INPUT

echo "{\"status\": \"started properties\"}" > status.json

# Run the properties extraction job
mpirun -np $SLURM_NTASKS Pproperties < INPUT >& out

# Copy useful files
cp out properties.out

echo "{\"status\": \"completed properties\"}" > status.json
