#!/bin/ksh
# palm_simple_run - a simple method for running the palm code without
#                   using the mrun script
# $Id: palm_simple_run 515 2010-03-18 02:30:38Z schwenkel $

     # This script runs the palm code in a unique subdirectory (OUTPUT...,
     # current time/date and number of processors are part of the subdirectory
     # name).
     # It requires that palm has been installed with command
     # palm_simple_install and that the executable palm has been created
     # with make in directory  ...../MAKE_DEPOSITORY_simple

     # Last changes:
     # 25/01/10 - Siggi - Generating the first version
     # 18/03/10 - Siggi - Some comments changed


    # Variable declarations + default values
 case=example_cbl
 cpumax=999999
 localhost=unknown
 localhost_realname=$(hostname)
 mpi_procs=1
 mpi_procs_per_node=1
 openmp_threads=1

 typeset -i  ii


    # Read shellscript options
 while  getopts  :c:l:n:p:t:  option
 do
   case  $option  in
       (c)   case=$OPTARG;;
       (l)   localhost=$OPTARG;;
       (n)   mpi_procs_per_node=$OPTARG;;
       (p)   mpi_procs=$OPTARG;;
       (t)   openmp_threads=$OPTARG;;
       (\?)  printf "\n  +++ unknown option $OPTARG \n"
             printf "\n      allowed option are -d, -f, -l, -s \n"
             exit;;
   esac
 done


    # Find out the global svn revision number
 global_revision=`svnversion ${palm_dir}trunk  2>/dev/null`
 global_revision="Rev: $global_revision"


    # Generate unique directory/files for this run
 timedate="`date +%d.%b_%H:%M:%S`"
 suffix=$case+$mpi_procs+$timedate
 RUNDIR=OUTPUT.$suffix/

 if [[ ! -d $RUNDIR ]]
 then
    mkdir $RUNDIR
    echo "*** running in directory $RUNDIR"
 else
    echo "+++ ERROR: $RUNDIR exists\!   Must be unique.  Exiting."
    exit
 fi


    # Check if palm has been installed and copy executable into the run
    # directory
 if [[ ! -f ${palm_dir}MAKE_DEPOSITORY_simple/palm ]]
 then
    echo "+++ ERROR: palm executable does not exist."
    echo "           Please run \"palm_simple_install\"."
    exit
 else
    cp  ${palm_dir}MAKE_DEPOSITORY_simple/palm  $RUNDIR/palm
 fi


    # Check, if parameter file exists and copy into the run directory
 if [[ ! -f ${palm_dir}JOBS/${case}/INPUT/${case}_p3d ]]
 then
    echo "+++ ERROR: parameter file"
    echo "           \"${palm_dir}JOBS/${case}/INPUT/${case}_p3d\""
    echo "           does not exist."
    exit
 else
    cp  ${palm_dir}JOBS/${case}/INPUT/${case}_p3d  $RUNDIR/PARIN
 fi


    # Switch to run directory
 cd  $RUNDIR



    # Create NAMELIST file containing environment values needed by palm
 cat  >  ENVPAR  <<  %%END%%
 &envpar  run_identifier = '$case', host = '$localhost',
          write_binary = false, tasks_per_node = $mpi_procs_per_node,
          maximum_cpu_time_allowed = ${cpumax}.,
          revision = '$global_revision',
          local_dvrserver_running = .FALSE. /

%%END%%


    # Coupled runs cannot be carried out with this simple run script
 echo "no_coupling"  >  runfile_atmos


    # Generate hostfile (if neccessary)
 (( ii = 1 ))
 while (( ii <= $mpi_procs ))
 do
    echo  $localhost_realname  >>  hostfile
    (( ii = ii + 1 ))
 done


    # Set number of OpenMP threads
 export OMP_NUM_THREADS=$openmp_threads



    # Start palm run
 echo "*** palm will be run:  MPI tasks: $mpi_procs   OpenMP thread: $OMP_NUM_THREADS"

# IMUK:
 mpiexec  -machinefile hostfile  -n $mpi_procs  ./palm  < runfile_atmos

# SGI-MPT HLRN
# mpiexec_mpt  -np $mpi_procs  ./palm  < runfile_atmos


 echo "*** palm finished"
 echo "*** see"
 echo "    \"$RUNDIR\""
 echo "    for results"
