#!/bin/csh
#
# $Id: nqenw 19708 2010-10-29 18:04:21Z d3y133 $
#

onintr interrupted

############################################################################
# Determine if at NERSC
# other sites that require accounting will need this kind of setup as well
############################################################################
set my_hostname = `hostname`
if ((${my_hostname} == 'mcurie')||(${my_hostname} == 'pierre')) then
   @ NERSC = 1
else
   @ NERSC = 0
endif

############################################################################
# Determine the default NWChem executable.  
# .  if NWCHEM_EXECUTABLE is defined use that if it exists
# .  else if NWCHEM_TOP is defined use the version in that tree if it exists
# .  else use the build by Rick
############################################################################


if ($?NQENW_NWCHEM_EXE) then
#  echo "resolved from environment variable NQENW_NWCHEM_EXE"
  set NWCHEM = $NQENW_NWCHEM_EXE
  if (-e $NWCHEM) goto got_default_executable
endif
if ($?NWCHEM_EXECUTABLE) then
#  echo "resolved from environment variable NWCHEM_EXECUTABLE"
  set NWCHEM = $NWCHEM_EXECUTABLE
  if (-e $NWCHEM) goto got_default_executable
endif
if ($?NWCHEM_TOP) then
#  echo "resolved from environment variable NWCHEM_TOP"
  set NWCHEM = $NWCHEM_TOP/bin/CRAY-T3E/nwchem
  if (-e $NWCHEM) goto got_default_executable
endif
if ($NERSC) then
  module load nwchem   
  set NWCHEM = `which nwchem`
  if (-e $NWCHEM) goto got_default_executable
endif
set NWCHEM = ~u5097/bin/nwchem
if (! -e $NWCHEM) set NWCHEM = "no-default-available"
got_default_executable:

############################################################################
# Determine the full path to the current directory.  This must
# constructed to be machine independent so refer it to the login directory
# using tilde for brevity of output (hence the control of globbing and
# avoiding double quotes when testing for existence.
############################################################################

pushd $HOME >& /dev/null
set PWDHOME = `pwd`
popd >& /dev/null
set noglob
set FULLPATH = `pwd | sed -e "s,$PWDHOME,~,"`
unset noglob

############################################################################
# Other miscellaneous defaults
############################################################################

set TIME = "1:00:00"
set NPROC = 1
set QUEUE = batch
if ($?ACCOUNT_NO) then
  set ACCOUNT = $ACCOUNT_NO
else if ($NERSC) then
  set ACCOUNT = `newacct -l | sed -e "s/Current account name: //"`
else
  set ACCOUNT = " "
endif

############################################################################
# If there are no arguments then prompt for data, otherwise
# process the input line arguments
############################################################################

if ($#argv == 0) then

# Get INPUT, OUTPUT, NPROC, TIME, QUEUE

  set INTERACTIVE = 1
  echo " "
  echo "NWChem interactive NQE submission"
  echo "-------------------------------------------"
  echo " "

  again2:
  set DEFAULTINPUT = "$FULLPATH/nwchem.nw"
  echo -n "Input file name ($DEFAULTINPUT): "
  set INPUT = "$<"
  if ("$INPUT" == "") set INPUT = "$DEFAULTINPUT"
# See if we need to prepend the full path
  set TEST = "$FULLPATH/$INPUT"
  if (-f $TEST) then
     set INPUT = "$TEST"
  else if (-f $TEST.nw) then
     set INPUT = "$TEST.nw"
  endif
  if (! -f $INPUT) then
    echo "  unable to locate input file $INPUT"
    goto again2
  endif

# Construct an output filename that does not already exist

  set noglob
  set OUTPUT = `dirname $INPUT`/`basename $INPUT .nw`.out
  unset noglob
  @ i = 1
  while (-e $OUTPUT) 
    echo " $OUTPUT already exists ... trying again"
    set noglob
    set OUTPUT = `dirname $INPUT`/`basename $INPUT .nw`.$i.out
    unset noglob
    @ i ++
  end

  echo -n "Full path for output ($OUTPUT): "
  set TEST = "$<"
  if ("$TEST" != "") set OUTPUT = "$TEST"

  echo -n "Number of processes (1): "
  set NPROC = "$<"
  if ("$NPROC" == "") set NPROC = 1

  echo -n "Time limit (1:00:00): "
  set TIME = "$<"
  if ("$TIME" == "") set TIME = "1:00:00"

  echo -n "Account to charge ($ACCOUNT): "
  set TEST = "$<"
  if ("$TEST" != "") set ACCOUNT = "$TEST"

  if ($NERSC) then
	echo "     Note: Queue can only be batch, debug, or long at NERSC"
  endif
  echo -n "NQE queue ($QUEUE): "
  set TEST = "$<"
  if ("$TEST" != "") set QUEUE = "$TEST"

  again1:
  echo -n "NWChem executable ($NWCHEM): "
  set TEST = "$<"
  if ("$TEST" == "") then
    # default
  else if (-f $FULLPATH/$TEST) then
    set NWCHEM = "$FULLPATH/$TEST"
  else if (-f $TEST) then
    set NWCHEM = "$TEST"
  else
    echo "  cannot locate $TEST"
    goto again1
  endif
  if (! -f $NWCHEM) then
    echo "  cannot locate $NWCHEM"
    goto again1
  endif

else

##################################################################################################
# Non-interactive ... nqenw input [-accnt accnt_no] [-procs nproc] [-queue queue] [-time time]
##################################################################################################

  if ("$1" == "-help" || "$1" == "help") then
    goto usage
  endif

  if (! -e $NWCHEM) then
    echo " NWChem executable '$NWCHEM' not found.  Try setting "
    echo " the default executable with (for csh/tcsh)"
    echo "   setenv NWCHEM_EXECUTABLE full-path-for-nwchem"
    exit 1
  endif

  set INPUT = "$1"
  if ("$INPUT" == "") set INPUT = "nwchem.nw"
  set TEST = "$FULLPATH/$INPUT"
  if (-f $TEST) then
     set INPUT = "$TEST"
  else if (-f $TEST.nw) then
     set INPUT = "$TEST.nw"
  endif
  if (! -f $INPUT) then
    echo "  Unable to locate input file $INPUT"
    exit 1
  endif

  set noglob
  set OUTPUT = `dirname $INPUT`/`basename $INPUT .nw`.out
  unset noglob
  @ i = 1
  while (-e $OUTPUT) 
    echo " $OUTPUT already exists ... trying again"
    set noglob
    set OUTPUT = `dirname $INPUT`/`basename $INPUT .nw`.$i.out
    unset noglob
    @ i ++
  end

  shift

# Process remaining optional arguments

  while ($#argv) 
    set key = "$1"
    shift
    if (! $#argv) then
      goto usage
    endif
    set value = "$1"
    shift

    if ("$key" == "-time") then
      set TIME = "$value"
    else if ("$key" == "-queue") then
      set QUEUE = "$value"
#      echo "! With the Maui scheduler only the queue named BATCH can be used"
    else if ("$key" == "-procs") then
      set NPROC = "$value"
    else if ("$key" == "-accnt") then
      set ACCOUNT = "$value"
    else
      echo " unknown option: $key"
      exit 1
    endif
  end
endif

############################################################################
# Finally know everything.  Determine name of file for the job and error
# output, print info out and confirm before submission if interactive
############################################################################

set noglob
set NQE_ERR = `dirname $OUTPUT`/`basename $OUTPUT .out`.err
set NQE_JOB = `dirname $OUTPUT`/`basename $OUTPUT .out`.job
unset noglob

echo " "
echo " NPROC   = $NPROC"
echo " TIME    = $TIME"
echo " QUEUE   = $QUEUE"
echo " INPUT   = $INPUT"
echo " OUTPUT  = $OUTPUT"
echo " ERROR   = $NQE_ERR"
echo " JOB     = $NQE_JOB"
echo " NWCHEM  = $NWCHEM"
echo " ACCOUNT = $ACCOUNT"
echo " "

if ($?INTERACTIVE) then
  echo -n "OK? (yes): "
  set TEST = "$<"
  if ("$TEST" == "" || "$TEST" == "y" || "$TEST" == "yes") then
    # OK
  else
    echo "  job submission aborted"
    exit 1
  endif
endif
############################################################################
# get request name
############################################################################
set REQUEST_NAME = `basename $NQE_JOB .job`


# Remove ~ since poe will not expand it

# Mmmm ... I suspect that simply echoing is enuf for the shell
# to expand the tilde ... sed is not needed ? (rjh)

set THEINPUT  = `echo $INPUT | sed -e "s,~,$PWDHOME,"`
set THEOUTPUT = `echo $OUTPUT | sed -e "s,~,$PWDHOME,"`
set THEERR    = `echo $NQE_ERR | sed -e "s,~,$PWDHOME,"`

# Remove the .err off of the error file so the jobid can be appended
# set NQE_ERR = $NQE_ERR:r

############################################################################
# Put the job into the file $NQE_JOB so the user can refer to it
############################################################################

cat <<EOF > $NQE_JOB
#!/bin/csh -x
#QSUB -mb 
#QSUB -me 
#QSUB -s /bin/csh 
#QSUB -nr 
#QSUB -lM 28Mw 
#QSUB -lm 28Mw 
#QSUB -J m 
#QSUB -lT 7200   # per request limit 
#QSUB -lt 3600   # per process limit 
#QSUB -q $QUEUE
#QSUB -o $THEOUTPUT
#QSUB -e $THEERR
#QSUB -l p_mpp_t=$TIME
#QSUB -l mpp_t=$TIME
#QSUB -l mpp_p=$NPROC
#QSUB -r $REQUEST_NAME
#QSUB -A $ACCOUNT

############################################################################
# Work in the system scratch directory --- faster and more reliable
############################################################################

cd \$BIG

############################################################################
# Set up environment variables for the POE collective commands
############################################################################

cp $NWCHEM \$BIG/nwchem
chmod +x \$BIG/nwchem

############################################################################
# Actually run the job with ja CRI job accounting on
############################################################################

ja 
echo "Start date:\`date\`"
time mpprun -n $NPROC ./nwchem $THEINPUT
echo " End  date:\`date\`"
ja -cst

############################################################################
# Add debugview 
############################################################################
if (-e core) then
   debugview -c core ./nwchem
endif

############################################################################
# End of the job script
############################################################################
EOF

############################################################################
# Submit the job to NQE and display brief queue information
############################################################################


qsub $NQE_JOB

if ($status != 0) then
  echo "  qsub failed"
  exit 1
endif

#qstat -a

############################################################################
# Touch the output file so that subsequent job submissions
# will not attempt to use the same name
############################################################################

touch $THEOUTPUT

############################################################################
# Normal shell script exit here
############################################################################

exit 0


############################################################################
# If interrupted jump here
############################################################################

interrupted:
	echo " interrupted"
	exit 1

############################################################################
# Print usage information
############################################################################

usage:
    echo " nqenw usage: "
    echo "   1) use with no arguments for interactive prompts"
    echo "   2) nqenw filename [-accnt accnt_no] [-procs nproc] [-queue queue] [-time time]"
    echo "   3) nqenw -help ... prints this information"  
    echo " "
    echo "   Input filenames may be specified without any trailing .nw and"
    echo "   either relative to the current directory or with an absolute path."
    echo " "
    exit 1
