#!/bin/bash
#$ -S /bin/bash
#$ -N Vasp_PHome
#$ -cwd 
#$ -V
#$ -o Vasp_para.batch-log
#$ -j y
#$ -q dl165adf
#$ -pe mpi8 8

#set -x

echo "============================================================================================="
echo "=                                                                                           ="
echo "=                        Vasp Parallel with unique machine                                  ="
echo "=                                                                                           ="
echo "=                           `date`                                          ="
echo "=                                                                                           ="
echo "============================================================================================="

HOMEDIR=$SGE_O_WORKDIR

echo "No SCRATCHDIR"

#Iter is the index for storing files as POSCAR_runXX
Iter=1
echo $Iter > Path.iter


######################################
#
# you might have to change the following variables
#
######################################
#
# Name of the Path Input file
#
Job=Test_Vasp_Para
#
#
# Where is Path.exe ??
#
PathExe=~/Programs/optnpath/src/Path.exe
#
# Where is VASP ?
#
export VASPDIR=~/Programs/VASP
export VASPEXEC=VASP4635_R410_STD
#
# Whre is mpirun ?
#
export MPIDIR=/softs/openmpi-1.4.1-intel/
export MPIRUN=$MPIDIR/bin/mpirun
#
#
# Machine where the files are
#
RUNNINGMACHINE=r410comp1-pub

# Some libraries... 
#
#For intel 11.1.069
source /softs/intel/v11.1.069/bin/intel64/ifortvars_intel64.sh
export LD_LIBRARY_PATH=/softs/fftw/2.1.5/ps-gb/lib:$LD_LIBRARY_PATH:/usr/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/softs/intel/v11.1.069/mkl/lib/em64t/:$MPIDIR/lib/:/softs/intel/v11.1.069/lib/intel64
export LANG=C
#
#Usually nothing has to be changed after this line
#
#################################################

#
# Machinefile
#
HOSTFILE=$TMPDIR/machines


#################################
#
#! we create the 00, 01... and 0x directorires
# First we get the number of images in the INCAR file:
NbIm=`grep -i IMAGES INCAR | awk -F"=" '{n=$2*1+1; print n}' `
#
#
#################################
#
# We go to the Work Directory
cd $HOMEDIR

# A small test to see if everything matches
NbImP=` sed 's/,/^M/g' ${Job}.path | grep -i ngeomf | awk -F"=" '{n=$2*1-1; print n}' `
if [ $NbImP -ne $NbIm ]; then
 echo "WARNING WARNING WARNING WARNING WARNING WARNING"
 echo "IMAGES in INCAR and NGeomF in " ${Job}.path
 echo "Do Not Match... problems might be expected..."
 echo "Check it !!!"
 echo "WARNING WARNING WARNING WARNING WARNING WARNING"
fi

if [ $NbIm -le 9 ]; then
 Pref="0"
else
 Pref=""
fi

#### We create the directories and copy files on Master
for n in `seq -w 0 $NbIm`
do
  ndir=$Pref$n
  mkdir -p ${HOMEDIR}/${ndir}
done


# ###########################
#
#  We create the 'VaspExe' file that Path will use to run the
# actual VASP calculations of E and forces
#
#############################
# We work in HOMEDIR. Nothing to copy to scratch 
cat << EOF > ./RunVasp
#!/bin/bash

#set -x
HOMEDIR=$HOMEDIR
SCRATCHDIR=$SCRATCHDIR
Pref=$Pref

cd $HOMEDIR
$MPIRUN -v -prefix $MPIDIR -mca pls_rsh_agent "ssh" -mca btl openib,self -np $NSLOTS ${VASPDIR}/${VASPEXEC} > \${HOMEDIR}/out

Iter=`cat \$HOMEDIR/Path.iter`
RUNNEW=run\${Iter}

for n in \`seq -w 0 $NbIm\`
do
  ndir=$Pref\$n
  for FILE in  OUTCAR POSCAR OSZICAR XDATCAR CONTCAR
  do
   cp \${HOMEDIR}/\$ndir/\$FILE \$HOMEDIR/\$ndir/\${FILE}_\${RUNNEW}
  done
done
cp \${HOMEDIR}/out  \$HOMEDIR/out_\${RUNNEW}


let Iter=Iter+1
echo \$Iter > \$HOMEDIR/Path.iter


EOF
chmod u+x ./RunVasp

###############################
#
# We launch PATH
#
###############################
ln -fs ${PathExe} .

./Path.exe ${Job}.path > ${Job}.out 2> ${Job}.err
 
