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

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

smi="Cc1cc(-c2c(F)cccc2-c2ccc3c(ccn3C(=O)OC(C)(C)C)c2)cnc1-c1c(F)cccc1-c1ccc2c(ccn2C(=O)OC(C)(C)C)c1"
name="A032B048C089"

# Define variables
dir=$SLURM_SUBMIT_DIR
scriptdir=/home/$USER/bin/projects/spectra
marvindir=/home/$USER/bin/marvin/bin
xtbdir=/home/$USER/bin/xtb/bin

echo "job start"
echo $(date +%T)
# Create folders and files
#mkdir $dir
cp -rf /home/$USER/madness/jobtemplate/* $dir
workdir=$SLURM_TMPDIR/$name
mkdir $workdir
cp -rf $dir/* $workdir

# Get initial xyz
cd $workdir/mol
echo $smi > ./mol.smi

# Setup molconvert environment
source $HOME/bin/load_mconv.sh

# Convert smi to xyz
$marvindir/molconvert xyz:+H ./mol.smi -o ./mol.xyz -3:S{fast}

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

# Run crest
cp -R ./mol.xyz ../crest
cd ../crest

# Setup crest environment
export PATH=$PATH:/home/$USER/bin/xtb/bin
export XTBPATH=/home/$USER/bin/xtb/share/xtb
export KMP_NUM_THREADS=$nt
export MKL_NUM_THREADS=$nt
export KMP_STACKSIZE="1000M"
ulimit -s unlimited

# Reopt with gfn0 to prevent unrecognized bonds in crest
$xtbdir/xtb ./mol.xyz --opt loose --chrg $charge --uhf $((spin-1)) --gfn 1 -P $nt > ./xtbopt.out

# Perform crest
$xtbdir/crest ./xtbopt.xyz --chrg $charge --uhf $((spin-1)) --opt normal -gfnff -T $nt > ./crest.out

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

# Perform opt job
cp ./crest_conformers.xyz ../opt
cd ../opt
head -n $nxyz ./crest_conformers.xyz > ./crest_best.xyz
$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 
source $HOME/madness/load_xyz2mol.sh
python reorientxyz.py 

# Perform hess job, generate hess.npy
cd ../hess
#cp ../opt
$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

deactivate

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

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

# Setup orca environment
source $HOME/madness/load_orca.sh

# Run ORCA spectra calculation
#${EBROOTORCA}/orca S0_opt.inp > S0_opt.out
${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

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

#load Multiwfn and extract excited state information
source $HOME/load_env/load_Multiwfn.sh
#cp $HOME/bin/Multiwfn_3.8/settings.ini ./
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

grep "CARTESIAN GRADIENT" ./S0_tdsp.out -A$((natoms+2)) >> grads.out

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

python engrad2np.py $natoms
python mu2np.py
#rm *.tmp

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?
source $HOME/madness/load_spectra.sh 
python $HOME/bin/spectra/spectra_vg_corr.py -grid all
#cd ../spec_eckart
#python $HOME/bin/spectra/spectra_vg_eckart.py -grid all

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

# Copy back all files
cp $dir/sl*.out $workdir/
cp -rf $workdir/* $dir

