#!/bin/sh
#################################################################################
##                                                                             ##
##                 l  p  j  s  u  b  m  i  t  _  a  i  x                       ##
##                                                                             ##
##    sh script to generate and submit LoadLeveler job.                        ##
##                                                                             ##
##    Usage: lpjsubmit [-class c] ntask [LPJ args...]                          ##
##                                                                             ##
## (C) Potsdam Institute for Climate Impact Research (PIK), see COPYRIGHT file ##
## authors, and contributors see AUTHORS file                                  ##
## This file is part of LPJmL and licensed under GNU AGPL Version 3            ##
## or later. See LICENSE file or go to http://www.gnu.org/licenses/            ##
## Contact: https://github.com/PIK-LPJmL/LPJmL                                 ##
##                                                                             ##
#################################################################################

if [ $# -lt 1 ]
then
  echo >&2 Error: Number of tasks missing
  echo >&2 Usage: $0 [-class c] ntasks [args]
  exit 1
fi

if [ "$LPJROOT" = "" ]
then
  echo >&2 Error: environment variable LPJROOT is not defined
  echo >&2 "Define by export LPJROOT=<path to lpjml directory>"
  exit 1
else
  echo Environment variables
  echo "LPJROOT:    " $LPJROOT
fi
if [ $1 = "-class" ]
then
  if [ $# -lt 2 ]
  then
    echo >&2 Error: class missing
    echo >&2 Usage: $0 [-class c] ntasks [args]
    exit 1
  fi
  shift 1
  class=$1
  shift 1
  if [ $# -lt 1 ]
  then
    echo >&2 Error: Number of tasks missing
    echo >&2 Usage: $0 [-class c] ntasks [args]
    exit 1
  fi
else
  class=short
fi

ntask=$1 # number of tasks
shift 1
args=$*  # runtime arguments for lpjml

# check, whether LPJ configuration is valid

if $LPJROOT/bin/lpjcheck $args ;
then
# yes, submit LoadL job control file
  llsubmit -  <<EOF 
#!/bin/ksh 
# @ job_type = parallel
# @ total_tasks = $ntask
# @ class = $class
# @ comment = LPJmL Version $(cat $LPJROOT/VERSION) args: $args
# @ environment = COPY_ALL
# @ blocking = unlimited
# @ output = lpj.\$(cluster).out
# @ error = lpj.\$(cluster).err
# @ network.MPI = css0,shared,US
# @ executable = /usr/bin/poe
# @ arguments = \$LPJROOT/bin/lpjml $args -shared_memory yes -wait_mode poll -euilib "us"
# @ queue
EOF
  llq -u $(whoami)
else
  echo >&2 "Error in LPJ configuration, job not submitted"
  exit 1
fi
