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


echo "============================================================================================="
echo "=                                                                                           ="
echo "=                        Path with Serial Vasp  with unique machine                         ="
echo "=                                                                                           ="
echo "=                           `date`                                          ="
echo "=                                                                                           ="
echo "============================================================================================="

HOMEDIR=$SGE_O_WORKDIR
SCRATCHDIR=`echo $HOMEDIR | awk -F "/" '{$2="scratch"; for(j=2;j<=NF;j++) {printf("/%s",$j);} print ""} ' `

#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_Serial
#
#
# 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
cp $HOSTFILE $HOMEDIR/local_machines


#################################
#
# We go to the Work Directory
cd $HOMEDIR


#################################
#
#
# We check that the INCAR file is not a NEB
#
NbIm=`grep -i IMAGES INCAR  `
if [ "$NbIm" != "" ]; then
  mv INCAR INCAR.ori
  grep -v IMAGES INCAR.ori > INCAR

 echo "WARNING WARNING WARNING WARNING WARNING WARNING"
 echo "IMAGES was in INCAR "
 echo "I copied INCAR to INCAR.ori and deleted this line"
 echo "Check it !!!"
 echo "WARNING WARNING WARNING WARNING WARNING WARNING"
fi

##############################
#
# As we work in the scratchdir
#
# We will create directories there
Master="`head -1 $HOSTFILE`"
echo $Master > $HOMEDIR/Master

FUniq=`which uniq`
Uniq=${FUniq:-"sort -u"}
$Uniq $HOSTFILE > $HOMEDIR/machines_unique
grep -v $Master $HOMEDIR/machines_unique > $HOMEDIR/machines_other


#### We create the directories and copy files to other machines
for server in `cat $HOMEDIR/machines_other`
do
  ssh $server mkdir -p ${SCRATCHDIR}
#### we copy the Vasp executable to all nodes
  scp ${RUNNINGMACHINE}:${VASPDIR}/${VASPEXEC} $server:${SCRATCHDIR}/${VASPEXEC}

#### we copy the files to all nodes
  for FILE in INCAR KPOINTS POTCAR 
  do
    scp $RUNNINGMACHINE:$HOMEDIR/$FILE $server:${SCRATCHDIR}/.
  done
done

#### We create the directories and copy files on Master
mkdir -p ${SCRATCHDIR}

#### we copy the Vasp executable to Scratch
cp ${VASPDIR}/${VASPEXEC} ${SCRATCHDIR}/${VASPEXEC}

#### we copy the files to SCRATCHDIR
for FILE in INCAR KPOINTS POTCAR
do
  cp $HOMEDIR/$FILE ${SCRATCHDIR}/.
done


#############################
#
#  We create the 'VaspExe' file that Path will use to run the
# actual VASP calculations of E and forces
#
#############################
NOther=`wc -l  $HOMEDIR/machines_other | awk '{print $1}'`
if [ $NOther -ge 1 ]; then
cat << EOF > ./RunVasp
#!/bin/bash

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


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

#### We copy the POSCAR file to other machines
for server in \`cat \$HOMEDIR/machines_other\`
do
  scp \$HOMEDIR/POSCAR \$server:\${SCRATCHDIR}/.
done

#### We copy files on Master
cp \$HOMEDIR/POSCAR \${SCRATCHDIR}/.


cd \$SCRATCHDIR

$MPIRUN -v -prefix $MPIDIR  -mca btl openib,self -np $NSLOTS \${SCRATCHDIR}/${VASPEXEC} > \${HOMEDIR}/out_\${RUNNEW}

for FILE in  OUTCAR POSCAR OSZICAR XDATCAR CONTCAR
do
 cp \${SCRATCHDIR}/\$FILE \$HOMEDIR/\${FILE}_\${RUNNEW}
 cp \$HOMEDIR/\${FILE}_\${RUNNEW}  \$HOMEDIR/\${FILE}
done

cp \${SCRATCHDIR}/out  \$HOMEDIR/out_\${RUNNEW}

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

cd \$HOMEDIR

EOF
else
cat << EOF > ./RunVasp
#!/bin/bash

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

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

#### We copy the POSCAR file
cp \$HOMEDIR/POSCAR \${SCRATCHDIR}/.

cd \$SCRATCHDIR

$MPIRUN -v -prefix $MPIDIR  -mca btl openib,self -np $NSLOTS \${SCRATCHDIR}/${VASPEXEC} > \${HOMEDIR}/out_\${RUNNEW}


for FILE in  OUTCAR POSCAR OSZICAR XDATCAR CONTCAR
do
 cp \${SCRATCHDIR}/\$FILE \$HOMEDIR/\${FILE}_\${RUNNEW}
 cp \$HOMEDIR/\${FILE}_\${RUNNEW}  \$HOMEDIR/\${FILE}
done

cp \${SCRATCHDIR}/out  \$HOMEDIR/out_\${RUNNEW}

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

cd \$HOMEDIR

EOF
fi

chmod u+x ./RunVasp

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

./Path.exe ${Job}.path > ${Job}.out 2> ${Job}.err
 
#######################""
#
# We delete the SCRATCHDIRs
for server in \`cat \$HOMEDIR/machines_other\`
do
  ssh $server rm -rf $SCRATCHDIR
done

rm -rf $SCRATCHDIR
