#!/bin/bash
# $1 - number of processors for parallel run
# $2 - input file without extension
	if [ $# -gt 0 ]; then
		echo "Command line contains $# arguments"
	else 
		echo "Command line contains no arguments"
	fi
#
echo "Number of CPUs = $1"
echo "Input file = $2"
hostname=$(hostname -s)
echo "Hostname = $hostname"
export PROJNAME=$2
export CP2KINPUT=""
export CP2KOUTPUT=$PROJNAME.out
export WORKDIR=$PWD
#export d=`date +%Y-%M-%D`
	if [ -f $PROJNAME.inp ]; then
		export CP2KINPUT=$PROJNAME.inp
	elif [ -f $PROJNAME.INP ]; then
		export CP2KINPUT=$PROJNAME.INP
	else
		echo "Can't find input file $PROJNAME.inp or $PROJNAME.INP in $PWD"
	exit
	fi
#CP2K with OpenMPI 2.1.5 (64-bit)
# Check for number of processors on workstation
MAXCPUS=`cat /proc/cpuinfo | grep processor | wc -l`
echo "Maxcpu's = $MAXCPUS"
export NCPUS=$1
	if [ $NCPUS -gt $MAXCPUS ]; then
		echo "You requested CPU's more then you have installed"
		echo "Number of CPU's will be $MAXCPUS"
		NCPUS=$MAXCPUS
	fi
# CP2K settings . You can change them according to your ystem
export CP2K_HOME=/home/$USER/CP2K_61/cp2k-6.1/exe/local
export CP2K_TEMP=/home/$USER/cp2k_tmp
	if [ ! -d $CP2K_TEMP ]; then
		mkdir -p $CP2K_TEMP; if [ $? -ne 0 ]; then echo "Can't create $CP2K_TEMP directory" ; fi; exit
	fi
# OpenMPI settings . See OpenMPI installation section..
export OMPI_HOME=/home/root/.openmpi215
export PATH=$OMPI_HOME:$PATH
export OMPI_BIN=$OMPI_HOME/bin
#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OMPI_HOME/lib
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
export RSH_COMMAND="/usr/bin/ssh -x" #Has to be set for NumFreq calulation
#
# 
# We are ready to go. Make sure name of executable file is correct.
#Copy Input to Scratch
cp $WORKDIR/$CP2KINPUT $CP2K_TEMP
cp $WORKDIR/3_Thiobenzamid_ERWEITERT-pos-1 $CP2K_TEMP
#cp $WORKDIR/3_Thiobenzamid_BASIS_UC_ERWEITERT_230321-RESTART.wfn $CP2K_TEMP
#cp $WORKDIR/3_Thiobenzamid_BASIS_UC_ERWEITERT_230321-1.restart $CP2K_TEMP
cd $CP2K_TEMP
# MPIRUN
mpirun -v --report-bindings  $CP2K_HOME/cp2k.popt $CP2K_TEMP/$CP2KINPUT >$WORKDIR/$CP2KOUTPUT
# Cleaning scratch folder
cp  $CP2K_TEMP/*.xyz $WORKDIR
cp  $CP2K_TEMP/*.wfn $WORKDIR
cp  $CP2K_TEMP/*.restart $WORKDIR
cp  $CP2K_TEMP/*.restart $WORKDIR
cp  $CP2K_TEMP/*.trj $WORKDIR
cp  $CP2K_TEMP/*.ener $WORKDIR
#rm -rf $CP2K_TEMP
exit