#!/bin/sh

#-------------------------------------------------------
# sub-procedures
execute_single_job(){
  # $1  source/result directory
  # $2  nfinp_????
  # $3  pp atom 1
  # $4  pp atom 2
  # $5  pp atom 3
  # $6  pp atom 4
  # $7  pp atom 5
  # $8  pp atom 6
  # $9  pp atom 7
  echo '-----------------------------'
  echo Job dir: data/$1
  echo Out dir: $OUTPUT/$1
  echo Input  : nfinp_$2
  echo '-----------------------------'
  
  if [ `ls $OUTPUT/$1 >& /dev/null `] ; then rm -fr $OUTPUT/$1; fi
  cp data/$1 $OUTPUT/$1 -prd
  pushd $OUTPUT/$1
   if [ $3''  != '' ] ; then ln -fs $PPDIR/$3  fort.37 ; fi
   if [ $4''  != '' ] ; then ln -fs $PPDIR/$4  fort.38 ; fi
   if [ $5''  != '' ] ; then ln -fs $PPDIR/$5  fort.39 ; fi
   if [ $6''  != '' ] ; then ln -fs $PPDIR/$6  fort.40 ; fi
   if [ $7''  != '' ] ; then ln -fs $PPDIR/$7  fort.41 ; fi
   if [ $8''  != '' ] ; then ln -fs $PPDIR/$8  fort.42 ; fi
   if [ $9''  != '' ] ; then ln -fs $PPDIR/$9  fort.43 ; fi
   $MPIRUN -n $NPROCS ../STATE_A < nfinp_$2 > nfout_$2
  popd
}

#-------------------------------------------------------
execute_multi_job(){
  # $1  source/result directory
  # $2  pp atom 1
  # $3  pp atom 2
  # $4  pp atom 3
  # $5  pp atom 4
  # $6  pp atom 5
  # $7  pp atom 6
  # $8  pp atom 7
  echo '-----------------------------'
  echo Job dir: data/$1
  echo Out dir: $OUTPUT/$1
  echo '-----------------------------'
  
  if [ `ls $OUTPUT/$1 >& /dev/null `] ; then rm -fr $OUTPUT/$1; fi
  cp data/$1 $OUTPUT/$1 -prd
  pushd $OUTPUT/$1
   for d in i* ;do
    pushd $d
     if [ $2''  != '' ] ; then ln -fs $PPDIR/$2  fort.37 ; fi
     if [ $3''  != '' ] ; then ln -fs $PPDIR/$3  fort.38 ; fi
     if [ $4''  != '' ] ; then ln -fs $PPDIR/$4  fort.39 ; fi
     if [ $5''  != '' ] ; then ln -fs $PPDIR/$5  fort.40 ; fi
     if [ $6''  != '' ] ; then ln -fs $PPDIR/$6  fort.41 ; fi
     if [ $7''  != '' ] ; then ln -fs $PPDIR/$7  fort.42 ; fi
     if [ $8''  != '' ] ; then ln -fs $PPDIR/$8  fort.43 ; fi
    popd
   done
   $MPIRUN -n $NPROCS ../STATE_A  > nfout_summary
  popd
}

# end of sub-procedures
#-------------------------------------------------------



#-----------------------------------------------
# This script is for running automatic tests
#-----------------------------------------------

#### Source directory/version of STATE----------------
###export EXE_STATE=~/STATE/src/STATE_5.5.1_m2a/STATE


# Number of CPUs-----------------------------------
export NPROCS=6
export OMP_NUM_THREADS=1

# Data directory for PP----------------------------
export PPDIR=~/STATE/gncpp

# MPI run command----------------------------------
export MPIRUN='mpirun '

# Output directory --------------------------------
export OUTPUT=results
mkdir $OUTPUT

# Other setting------------------------------------
rundir=`pwd`
cd $rundir
# generate a machinefile---------------------------
CPUINFO=/proc/cpuinfo
HOST=`hostname`
export MACHINEFILE=machinefile
NCPU=`grep processor ${CPUINFO} | wc -l`
#echo 'Number of CPUs: '${NCPU}
if [ -e ${MACHINEFILE} ];then rm ${MACHINEFILE}; fi ; touch ${MACHINEFILE}
N=0
while [ $N -lt ${NCPU} ]
do
  echo ${HOST} >> ${MACHINEFILE}
  N=`expr $N + 1`
done


######################################
###                                ###
###   AUTOMATIC TEST STARTS HERE   ###
###                                ###
######################################


# Test job list (default setting)------------------
JOBS='O2 CO H2O C2H4 Si_dia Na Mg Pt_fcc Ni_fcc Mo_bcc Ru_hcp TiO2 SiC_sf GaAs Al111 ClonAl100 Alq3 ClonAl100_NEB'


# Test job input check-----------------------------
if [ $# -eq 0 ];
then
 echo
 echo '      ###   FOLLOWING TESTS WILL BE PERFORMED   ###'
 echo $JOBS
else
 JOBS=$@
 echo
 echo '      ###   FOLLOWING TESTS WILL BE PERFORMED   ###'
 echo $JOBS
fi

# JOB loop starts  ==================================================================
for i in $JOBS ; do 

if [ $i == 'CO' ]; then
#                    source  workdir nfinp/nfout  potentials
 execute_single_job  CO      CO_dav       pot.C_pbe1  pot.O_pbe1
 execute_single_job  CO      CO_rmm1      pot.C_pbe1  pot.O_pbe1
 execute_single_job  CO      CO_rmm2      pot.C_pbe1  pot.O_pbe1
fi

if [ $i == 'H2' ]; then
 execute_single_job  H2      H2           pot.H_lda3
 execute_single_job  H2      H2_gdiis     pot.H_lda3
fi

if [ $i == 'H2O' ]; then
 execute_single_job  H2O     H2O          pot.H_lda3  pot.O_pbe3
 execute_single_job  H2O     H2O_DPL      pot.H_lda3  pot.O_pbe3
 execute_single_job  H2O     H2O_DPL2     pot.H_lda3  pot.O_pbe3
 execute_single_job  H2O     H2O_DPFPLUS  pot.H_lda3  pot.O_pbe3
 execute_single_job  H2O     H2O_BARE     pot.H_lda3  pot.O_pbe3
 execute_single_job  H2O     H2O_PE1      pot.H_lda3  pot.O_pbe3
 execute_single_job  H2O     H2O_PE2      pot.H_lda3  pot.O_pbe3
 execute_single_job  H2O     H2O_BARE_rmm pot.H_lda3  pot.O_pbe3
 execute_single_job  H2O     H2O_PE1_rmm  pot.H_lda3  pot.O_pbe3
 execute_single_job  H2O     H2O_PE2_rmm  pot.H_lda3  pot.O_pbe3
fi

if [ $i == 'C2H4' ]; then
 execute_single_job  C2H4    C2H4_opt     pot.C_pbe1  pot.H_lda3
 execute_single_job  C2H4    C2H4_md      pot.C_pbe1  pot.H_lda3
 execute_single_job  C2H4    C2H4_distrib pot.C_pbe1  pot.H_lda3
fi
if [ $i == 'C2H4a' ]; then
 execute_single_job  C2H4a    C2H4_md      pot.C_pbe1  pot.H_lda3
fi

if [ $i == 'O2' ]; then
 execute_single_job  O2      O2           pot.O_pbe1
fi

if [ $i == 'Al111' ]; then
 execute_single_job  Al      Al111        pot.Al_pbe1
fi

if [ $i == 'ClonAl100' ]; then
 mkdir $OUTPUT/ClonAl100
 execute_single_job  ClonAl100         ClonAl100    pot.Al_pbe1 pot.Cl_pbe1
 execute_single_job  ClonAl100/top     top_gdiis    pot.Al_pbe1 pot.Cl_pbe1
 execute_single_job  ClonAl100/hollow  hollow_gdiis pot.Al_pbe1 pot.Cl_pbe1
 execute_single_job  ClonAl100/bridge  bridge_opt   pot.Al_pbe1 pot.Cl_pbe1
 execute_single_job  ClonAl100/bridge  bridge_gdiis pot.Al_pbe1 pot.Cl_pbe1
 execute_single_job  ClonAl100/bridge  vib          pot.Al_pbe1 pot.Cl_pbe1
fi
if [ $i == 'ClonAl100_NEB' ]; then
 mkdir $OUTPUT/ClonAl100
 execute_multi_job   ClonAl100/path2   pot.Al_pbe1 pot.Cl_pbe1
fi

if [ $i == 'Na' ]; then
 execute_single_job  Na      Na           pot.Na_pbe1
fi

if [ $i == 'Mg' ]; then
 execute_single_job  Mg      Mg           pot.Mg_pbe2
fi

if [ $i == 'Pt_fcc' ]; then
 execute_single_job  Pt      Pt_fcc       pot.Pt_pbe1s
 execute_single_job  Pt      Pt_fcc_rmm   pot.Pt_pbe1s
fi

if [ $i == 'Ni_fcc' ]; then
 execute_single_job  Ni      Ni_fcc_Dav   pot.Ni_pbe4
 execute_single_job  Ni      Ni_fcc_PDOS  pot.Ni_pbe4
 execute_single_job  Ni      Ni_fcc_rmm   pot.Ni_pbe4
fi

if [ $i == 'Ru_hcp' ]; then
 execute_single_job  Ru      Ru_hcp       pot.Ru_pbe6
 execute_single_job  Ru      Ru_hcp_rmm   pot.Ru_pbe6
fi

if [ $i == 'Mo_bcc' ]; then
 execute_single_job  Mo      Mo_bcc       pot.Mo_pbe3
fi

if [ $i == 'Si_dia' ]; then
 execute_single_job  Si      Si_dia       pot.Si_pbe1
fi

if [ $i == 'Alq3' ]; then
 execute_single_job  Alq3    Alq3         pot.Al_pbe1 pot.H_lda3  pot.C_pbe3  pot.N_pbe1  pot.O_pbe3
fi

if [ $i == 'TiO2' ]; then
 execute_single_job  TiO2    TiO2         pot.Ti_pbe3 pot.O_pbe3  pot.H_lda3
fi

if [ $i == 'TiO2_sf' ]; then
 execute_single_job  TiO2    TiO2_sf      pot.Ti_pbe3 pot.O_pbe3  pot.H_lda3
fi

if [ $i == 'TiO2_LDA+U' ]; then
 execute_single_job  TiO2    TiO2_LDA+U   pot.Ti_pbe3 pot.O_pbe3  pot.H_lda3
fi

if [ $i == 'GaAs' ]; then
 execute_single_job  GaAs    GaAs         pot.Ga_pbe1 pot.As_pbe1
fi

if [ $i == 'SiC_sf' ]; then
 execute_single_job  SiC     SiC_sf       pot.Si_pbe1 pot.C_pbe3  pot.H_lda3
fi

done
# JOB loop ends    ==================================================================


mv $MACHINEFILE  $OUTPUT

echo
echo '           ##########################'
echo '           ###   AUTOMATIC TEST   ###'
echo '           ### SUCCESSFULLY ENDED ###'
echo '           ##########################'
echo

