#!/bin/bash -l
#$ -P Gold
#$ -A KCL_Kantorovitch
#$ -l h_rt=48:00:00
#$ -N cp2k_0-1
#$ -pe mpi 120
#$ -cwd

module purge
module load gerun
module load gcc-libs
module load compilers/gnu/4.9.2
module load mpi/openmpi/3.1.4/gnu-4.9.2
module load openblas/0.3.7-openmp/gnu-4.9.2
module load cp2k/7.1/ompi/gnu-4.9.2

export OMP_NUM_THREADS=1

#......... file names that depend on the current job

KS_file=Gold-KS_MAT-1_l.Log
XYZ_file=Gold-pos-1.xyz
WFN_file=Gold-RESTART.wfn

#......... workflow

#......... to restart the script, create the file 'started' in the directory where the job was stopped
#......... workflow

dir0=0.0.3

for d1 in   0 1 2 3 4 5 ; do
  for d2 in 0 1 2 3 4 5  ; do

    for d3 in  3 2 1 0  ; do

        dir=$d1.$d2.$d3
        cd $dir
        echo '.... working in directory ' $dir ' ....'
        echo '.... working in directory ' `pwd`

        if [[ -s safe ]] ; then
	    echo 'skipping directory ' $dir ' as it is NOT SAFE'
	else
	
            if [ $d3 == 3 ] ; then
		
		if [ $d1 == 0 &&  $d2 == 0 ]; then
		    
		    if [[ -s done ]] ; then
			echo SKIP $dir
		    else
			echo 'no need for the previous directory'
			echo '(a)...... run CP2K  .......'
			gerun cp2k.popt -inp input_SCT.inp > output_SCT.out
			echo 'check if finished OK'
			grep 'GEOMETRY OPTIMIZATION COMPLETED' output_SCT.out > success
			if [[ -s success ]] ; then
			    echo "OK, finished relaxation"
			    echo  OK > done
			    echo '(b)...... extract KS & OV .......'
			    ~/bin/extract  $KS_file
			    gzip KS.dat OV.dat
			    echo '(c)...... save geometry .......'
			    tetr < ../inp.2K > tetr.out
			    cp coordinates.inc coordinates_rlx.inc
			else
			    echo started > started
			    echo " relaxation NOT finished."
			fi
		    fi
		    
		else
		    
		    if [[ -s done ]] ; then
			echo SKIP $dir
		    else
			if [[ -e started ]] ; then
			    echo SKIP copying, use current information
			    if [[ -s $XYZ_file ]]; then
				echo '(o)...... update geomnetry if run CP2K before .......'
				tetr < ../inp.2K > tetr.out1
			    fi
			else
			    echo 'INFO: previous directory = ../'$dir0
			    cp ../$dir0/$WFN_file .
			    echo started > started
			fi
			echo '(a)...... run CP2K .......'
			gerun cp2k.popt -inp input_SCT.inp > output_SCT.out
			echo 'check if finished OK'
			grep 'GEOMETRY OPTIMIZATION COMPLETED' output_SCT.out > success
			if [[ -s success ]] ; then
			    echo "OK, finished relaxation"
			    echo  OK > done
			    echo '(b)...... extract KS & OV .......'
			    ~/bin/extract  $KS_file
			    gzip KS.dat OV.dat
			    echo '(c)...... save geometry .......'
			    tetr < ../inp.2K > tetr.out
			    cp coordinates.inc coordinates_rlx.inc
			else
			    echo " relaxation NOT finished."
			fi
		    fi
		fi
		
            else
		
		if [[ -s done ]] ; then
		    echo SKIP $dir
		else
		    if [[ -s started ]] ; then
			echo SKIP copying, use current information
			if [[ -s $XYZ_file ]]; then
			    echo '(o)...... update geomnetry if run CP2K before .......'
			    tetr < ../inp.2K > tetr.out1
			fi
		    else
			echo 'INFO: previous directory = ../'$prev
			cp ../$prev/$WFN_file  .
			echo started > started
		    fi
		    echo '(a)...... run CP2K .......'
		    gerun cp2k.popt -inp input_SCT.inp > output_SCT.out
		    echo 'check if finished OK'
		    grep 'GEOMETRY OPTIMIZATION COMPLETED' output_SCT.out > success
		    if [[ -s success ]] ; then
			echo "OK, finished relaxation"
			echo  OK > done
			echo '(b)...... extract KS & OV .......'
			~/bin/extract  $KS_file
			gzip KS.dat OV.dat
			echo '(c)...... save geometry .......'
			tetr < ../inp.2K > tetr.out
			cp coordinates.inc coordinates_rlx.inc
		    else
			echo " relaxation NOT finished."
		    fi
		fi
	    fi
            prev=$dir
	
            echo '(d).....  remove unnnecessaery files ....'
            rm *bak* *-KS* *BFGS* *restart

	fi
        cd ..

    done
    echo

  done
done
