#PBS -S /bin/sh
#PBS -N VaspPara
#PBS -l nodes=8:Other
#PBS -q Para8
#PBS -o Test_Vasp_Para.batch-log
#PBS -j oe
#PBS -M Paul.Fleurat-Lessard@ens-lyon.fr
#PBS -m aeb

#!/bin/bash

home_dir=$PBS_O_WORKDIR
work_dir=$PBS_O_WORKDIR
numb=3x3_run1

######################################
#
# you might have to change the following variables
#
######################################
#
# Name of the Path Input file
#
Job=Test_Vasp_Para
#
#
# Where is Path.exe ??
#
PathExe=~/Programs/Carte/src/Path.exe
#
# Where is VASP ?
#
export VASP=/home/dloffred/VASP_SOURCES/Intel_P4/VASP46_CINEB
#
# Whre is mpirun ?
#
export MPIRUN=/usr/local/mpich-intel-090404/bin/mpirun
#
# Some libraries... 
#
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/intel/fc/9.1.037/lib/:/opt/intel/cc/9.1.045/lib:/usr/lib:/opt/intel/mkl/8.1/lib/32
export PATH=/opt/intel/fc/9.1.037/bin/:/opt/intel/cc/9.1.045/bin/:$PATH:$HOME/bin:/usr/local/mpich-intel9/bin
export LD_RUN_PATH=$LD_RUN_PATH:/opt/intel/fc/9.1.037/lib/:/opt/intel/cc/9.1.045/lib
export MANPATH=$MANPATH:/opt/intel/fc/9.1.037/man/:/opt/intel/cc/9.1.045/man
export LANG=C
#
#Usually nothing has to be changed after this line
#
#################################################




#################################
#
# We create the machinefile
#
# even if it is not really needed for SERIAL calculations
#
#################################
nodes=`cat $PBS_NODEFILE`
nnodes=`wc $PBS_NODEFILE | awk '{print $1}'` 

confile=$home_dir/ip.$PBS_JOBID.conf
touch $confile
j="init"
for i in $nodes
do
if [ $j != $i ]
then echo "$i:1" >> $confile
else
	echo "vous calculez sur des noeuds smp"
fi
j=$i
done

###############################
#
# We go to the Work Directory
#
###############################

cd $work_dir

#! 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}' `
n=0
while [ $n -le $NbIm ]
do
ndir=`echo $n | awk '{k="00" $1; l=length(k); l=l-1; print substr(k,l,2)}'`
mkdir -p $ndir
let n=n+1
done

# 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

#############################
#
#  We create the 'VaspExe' file that Path will use to run the
# actual VASP calculations of E and forces
#
#############################
echo "$MPIRUN -nolocal -machinefile $confile -np $nnodes $VASP > out_$numb" >  ./RunVasp
chmod u+x ./RunVasp

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

#############################
#
#  We create the 'VaspExe' file that Path will use to run the
# actual VASP calculations of E and forces
#
#############################

echo "$MPIRUN -nolocal -machinefile $confile -np $nnodes $VASP > out_$numb" >  ./RunVasp
chmod u+x ./RunVasp

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

./Path.exe ${Job}.path &> ${Job}.out
 
