#!/bin/bash

# Submit an AMBER job

#######################################################################

#PBS -q batch
#PBS -l nodes=1:ppn=1
#PBS -N d4m
#PBS -j oe

host=swift
num_cpus=1
pmemd=pmemd.cuda

#######################################################################

cd $PBS_O_WORKDIR

# Set up some info to use
export infilepath="./"
export MININ="min.in"
export HEATIN="heat_gpu.in"
export EQUILIN="equil.in"
export MDIN="prod.in"

# Set $host implied variables
if [[ $host = swift ]]; then
export AMBERHOME=/programs/gpu/amber12
export MPI_HOME=/programs/gpu/mvapich2-1.8a1p1
export CUDA_HOME=/programs/gpu/cuda-4.0
export PATH=$AMBERHOME/bin:$MPI_HOME/bin:$CUDA_HOME/bin:/opt/torque/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
export LD_LIBRARY_PATH=$MPI_HOME/lib:$CUDA_HOME/lib64:$CUDA_HOME/lib:/usr/local/lib64:/usr/local/lib:/lib64:/lib:/usr/lib64:/usr/lib

elif [[ $host = eliot ]]; then
export AMBERHOME=/programs/amber11
export PATH=$PATH:/usr/lib64/qt-3.3/bin:/usr/kerberos/bin:/programs/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/opt/torque/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/programs/lib:/usr/local/lib

elif [[$host = tolkien ]]; then
export AMBERHOME=/programs/gpu/amber12
export MPI_HOME=/programs/gpu/mvapich2-1.8a1p1
export CUDA_HOME=/programs/gpu/cuda-4.0
export TORQUE_HOME=/programs/gpu/torque-3.0.3
export PATH=$AMBERHOME/bin:$MPI_HOME/bin:$CUDA_HOME/bin:$TORQUE_HOME/bin:$TORQUE_HOME/sbin:$PATH
export LD_LIBRARY_PATH=$MPI_HOME/lib:$CUDA_HOME/lib64:$CUDA_HOME/lib:$TORQUE_HOME/lib:/usr/local/lib64:/usr/local/lib:/lib64:/lib:/usr/lib64:/usr/lib

fi

# Set $num_cpus implied variables
if [[ $num_cpus = 1 ]]; then
export DO_PARALLEL=

else
export DO_PARALLEL="mpirun -np $num_cpus"

fi

# Set $pmemd implied variables
if [[ $pmemd = pmemd || $pmemd = pmemd.MPI ]]; then
wallclock="Master Total wall time"

elif [[ $pmemd = pmemd.cuda || $pmemd = pmemd.cuda.MPI ]]; then
wallclock="Total wall time"
sleep 10
export CUDA_VISIBLE_DEVICES=`/programs/gpu/utilities/gpu-free`

elif [[ $pmemd = sander || $pmemd = sander.MPI ]]; then
wallclock="wallclock() was called"

fi 

#######################################################################

#$DO_PARALLEL $AMBERHOME/bin/$pmemd  \
#-i ${infilepath}$MININ \
#-o min.out \
#-p in.parm7 \
#-c in.rst7 \
#-r min.rst7 \
#-x min.nc \
#-inf min.info \
#
#if grep -q "$wallclock" min.out ; then
#
#$DO_PARALLEL $AMBERHOME/bin/$pmemd  \
#-i ${infilepath}$HEATIN \
#-o heat.out \
#-p in.parm7 \
#-c min.rst7 \
#-r heat.rst7 \
#-x heat.nc \
#-inf heat.info \
#
#fi
#
#if grep -q "$wallclock" heat.out ; then
#
#$DO_PARALLEL $AMBERHOME/bin/$pmemd  \
#-i ${infilepath}$EQUILIN \
#-o equil.out \
#-p in.parm7 \
#-c heat.rst7 \
#-r equil.rst7 \
#-x equil.nc \
#-inf equil.info \
#
#fi 

#if grep -q "$wallclock" equil.out ; then

$DO_PARALLEL $AMBERHOME/bin/$pmemd  \
-i ${infilepath}$MDIN \
-o md.out \
-p in.parm7 \
-c equil.rst7 \
-r md.rst7 \
-x md.nc \
-inf md.info \

#fi 
