#!/bin/csh

echo "NWChem ANL SP Script"
echo "--------------------"
echo "                    "
echo " Contact nwchem-support@emsl.pnl.gov with problems "
echo "                    "

#
# argument 1 = work directory (made if ncessary)
# argument 2 = full pathname of input file
# argument 3 = full pathname of output file which defaults
#              to name of input file with .nw replaced by .out
# e.g.
#     runnwchem /tmp /sphome/harrison/test.nw 
#     or
#     runnwchem /tmp /sphome/harrison/test.nw /sphome/harrison/test.out
#
set STATUS = 0
#
# Extract arguments
#
setenv DIR $argv[1]
setenv INPUT $argv[2]
if ($#argv >= 3) then
  setenv OUTPUT $argv[3]
else
  setenv OUTPUT `dirname $INPUT`/`basename $INPUT .nw`.out
endif
#
# Path to executable
#
setenv NWCHEM /sphome/harrison/hpcci/nwchem
#
# Make sure that the work directory exists
#
if (! -e $DIR) then
  mkdir -p $DIR
  if ($status != 0) then
    echo Failed to make work directory $DIR
    set STATUS = 1
    goto release
  endif
endif
cd $DIR
if ($status != 0) then
  echo Failed to change to work directory $DIR
  set STATUS = 1
  goto release
endif
#
# Ensure the input file exists
#
if (! -e $INPUT) then
  echo Input file $INPUT is not accessible
  set STATUS = 1
  goto release
endif
#
# Partition info
#
set ME = `whoami`
set JID = `/usr/local/bin/getjid`
set SPNODES = `cat /sphome/$ME/SPnodes.$JID`
setenv MP_HOSTFILE /sphome/$ME/SPnodes.$JID
setenv MP_INFOLEVEL 1
setenv MP_EUILIB us
setenv MP_PULSE 0
setenv MP_CSS_INTERRUPT yes
set NUMNODES = `wc -l $MP_HOSTFILE`
setenv MP_PROCS $NUMNODES[1]
#
# Echo info
#
echo "NWChem   =" $NWCHEM
echo "Work dir =" $DIR
echo "Input    =" $INPUT
echo "Output   =" $OUTPUT
echo "Job id   =" $JID
echo "User     =" $ME
echo "Nproc    =" $MP_PROCS
echo "Nodes    =" $SPNODES
echo "Hostfile =" $MP_HOSTFILE
echo "Date     =" `date`
echo " "
#
# start job run
#
echo poe $NWCHEM $INPUT \>\& $OUTPUT
     poe $NWCHEM $INPUT >& $OUTPUT
#
release:
echo "Removing hostfile and Releasing Partition"
rm -f $MP_HOSTFILE
/usr/local/bin/sprelease $JID
exit $STATUS
#
