#!/bin/bash
#SBATCH --account=def-aspuru
#SBATCH --mem-per-cpu=2G
#SBATCH --time=00-2:00
#SBATCH --signal=B:15@120
#SBATCH --ntasks=16
#SBATCH --nodes=1
##SBATCH --mail-type=END
#SBATCH --mail-type=FAIL
#SBATCH --mail-user=clascclon@gmail.com
#SBATCH --job-name=A033B056C094

np=16
nt=$(( np * 2 ))
charge=0
spin=1

smi="c1ccc(-c2ccc(N(c3ccccc3)c3ccc(-c4ccc(-c5ccc(-c6ccc(-c7ccc(N(c8ccccc8)c8ccc(-c9ccccc9)cc8)cc7)s6)o5)s4)cc3)cc2)cc1"
name="A033B056C094"
input="7016.inp"
# Define variables
dir=$SLURM_SUBMIT_DIR
scriptdir=/home/$USER/bin/projects/spectra
marvindir=/home/$USER/bin/marvin/bin
xtbdir=/home/$USER/bin/xtb/bin

function cleanup_submitnext() {
  deactivate
  cp $dir/sl*.out $workdir/
  cp -rf $workdir/* $dir
  rm -rf $workdir

  cd $dir
  cd ..
  bash submit.sh $input

  exit 0
}
trap cleanup_submitnext SIGTERM EXIT

echo "job start"
echo $(date +%T)
#source $HOME/madness/load_madness_env.sh

# Create folders and files
cp -rf /home/$USER/madness/jobtemplate/* $dir
workdir=$SLURM_TMPDIR/$name
mkdir $workdir
cp -rf $dir/* $workdir

# Use cheapocrest to get lowest conformer
cd $workdir/crest
source $HOME/madness/load_madness_obabel.sh $nt
echo $smi > mol.smi
python cheapocrest-python.py --nconfs 50 --ff uff --chrg $charge --theory gfnff --solvent acetonitrile $smi >cheapocrest.out
cp ./crest_best.xyz ../opt

# Get number of atoms in molecule
natoms=$(cat ./crest_best.xyz | head -n1)
nxyz=$(( natoms + 2 ))
#head -n $nxyz ./crest_conformers.xyz > ./crest_best.xyz

echo "crest done"
echo $(date +%T)

# Perform opt job
source $HOME/madness/load_madness_orca.sh $nt
#cp ./crest_best.xyz ../opt
cd ../opt
$xtbdir/xtb crest_best.xyz --chrg $charge --uhf $((spin-1)) --opt verytight --gfn 2 -P $nt > ./opt.out

# ReOrient xyz file and send to ../hess folder 
python reorientxyz.py 

# Perform hess job, generate hess.npy
cd ../hess
$xtbdir/xtb reorient.xyz --chrg $charge --uhf $((spin-1)) --hess --gfn 2 -P $nt > ./hess.out
python xtbhess2np.py $natoms
cp ./reorient.xyz ../orcajobs/S0_opt.xyz

echo "xtb done"
echo $(date +%T)

# Perform UV-VIS absorption spectrum prediction
cd ../orcajobs/

# Run ORCA spectra calculation
${EBROOTORCA}/orca S0_tdsp.inp  | tee -a S0_tdsp.out $dir/orcajobs/S0_tdsp.out
#transform the standard orca .gbw wavefunction output to molden format for Multiwfn to read
${EBROOTORCA}/orca_2mkl S0_tdsp -molden
mv S0_tdsp.molden.input S0_tdsp.molden

nele=$(awk '/Number of Electrons/{print $NF }' S0_tdsp.out)
echo $nele >> ../spec/orca_mo.out
grep "ORBITAL ENERGIES" S0_tdsp.out -A$((nele + 3)) >> ../spec/orca_mo.out
grep "CARTESIAN GRADIENT" ./S0_tdsp.out -A$((natoms+2)) >> grads.out

echo "orca done"
echo $(date +%T)

#Multiwfn extract excited state information
Multiwfn ./S0_tdsp.molden -nt $np < ./ES_info.txt | tee ./ES_info.out

grep "Sr index" ./ES_info.out |nl >> ES_results.out; echo >> ES_results.out
grep "D index" ./ES_info.out |nl >> ES_results.out; echo >> ES_results.out
grep "RMSD of hole in" ./ES_info.out |nl >> ES_results.out; echo >> ES_results.out
grep "RMSD of electron in" ./ES_info.out |nl >> ES_results.out; echo >> ES_results.out
grep "H index" ./ES_info.out |nl >> ES_results.out; echo >> ES_results.out
grep " t index" ./ES_info.out |nl >> ES_results.out; echo >> ES_results.out
grep "Delta_r =" ./ES_info.out |nl >> ES_results.out; echo >> ES_results.out
grep "lambda =" ./ES_info.out |nl >> ES_results.out; echo >> ES_results.out
grep "Transition electric dipole moment between ground state" ./ES_info.out -A7 >> ES_results.out
grep "Transition electric dipole moment between excited states" ./ES_info.out -A17 >> ES_results.out

echo "Multiwfn done"
echo $(date +%T)

python engrad2np.py $natoms
python mu2np.py

cd ../spec
cp ../hess/hessian.npy .
cp ../orcajobs/dipmoments.npy .
cp ../orcajobs/grads.npy .
cp ../orcajobs/excenergies.npy .
cp ../orcajobs/S0_opt.xyz .

# Caldulate spectra, seems there is bug?
python $HOME/bin/spectra/spectra_vg_corr.py -grid all

echo "all done"
echo $(date +%T)

wait

