Index: palm/trunk/SCRIPTS/palm_simple_run
===================================================================
--- palm/trunk/SCRIPTS/palm_simple_run	(revision 2718)
+++ palm/trunk/SCRIPTS/palm_simple_run	(revision 2850)
@@ -1,3 +1,3 @@
-#!/bin/bash
+#!/usr/bin/env bash
 #--------------------------------------------------------------------------------#
 # This file is part of the PALM model system.
@@ -24,4 +24,7 @@
 # -----------------
 # $Id$
+# Refactoring of the palm_simple_ build and run scripts
+# 
+# 2718 2018-01-02 08:49:38Z maronga
 # Corrected "Former revisions" section
 #
@@ -63,129 +66,190 @@
 # with make in directory  ...../MAKE_DEPOSITORY_simple
 #--------------------------------------------------------------------------------#
-
-
-    # Variable declarations + default values
- case=example_cbl
- cpumax=999999
- execute_for=unknown
- 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:e:l:n:p:t:  option
- do
-   case  $option  in
-       (c)   case=$OPTARG;;
-       (e)   execute_for=$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 -c, -e, -l, -n, -p, -t \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"
-
- case $execute_for in
-
-    (imuk)         mpiexec  -machinefile hostfile  -n $mpi_procs  ./palm  < runfile_atmos;;
-    (sgi-mpt)      mpiexec_mpt  -np $mpi_procs  ./palm  < runfile_atmos;;
-    (hpc-flow)     mpiexec  -machinefile $TMPDIR/machines -n $mpi_procs  -env I_MPI_FABRICS shm:ofa ./palm  < runfile_atmos;;
-    (pgi-openacc)  export PGI_ACC_SYNCHRONOUS=1; ./palm;;
-    (*)      echo "+++ -e option to define execution command is missing";;
-
- esac
-
- echo "*** palm finished"
- echo "*** see"
- echo "    \"$RUNDIR\""
- echo "    for results"
+SOURCE="${BASH_SOURCE[0]}"
+while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
+  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
+  SOURCE="$(readlink "$SOURCE")"
+  # if $SOURCE was a relative symlink, we need to resolve it
+  # relative to the path where the symlink file was located
+  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
+done
+SCRIPT_LOCATION="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
+
+working_dir=$(readlink -f "${SCRIPT_LOCATION}/../../")
+trunk_dir=$(readlink -f "${SCRIPT_LOCATION}/../")
+
+# Variable declarations + default values
+build_config=unknown
+run_config=unknown
+test_case=unknown
+mpi_ranks=1
+mpi_ranks_per_node=1
+openmp_threads=1
+localhost_realname=$(hostname)
+cpumax=999999
+
+print_help() {
+   echo "Usage: palm_simple_run -b <build-config> -c <run-config> -s <test-case>"
+   echo "                       [-n <mpi-ranks-per-node>] [-p <mpi-ranks>]"
+   echo "                       [-t <openmp-threads>]"
+   echo ""
+   echo "List of allowed option:"
+   echo ""
+   echo "   -b <build-config>        Suffix of any MAKE.inc.<build-config> file in the trunk/INSTALL dir."
+   echo "   -c <run-config>          Suffix of any RUN.cmd.<run-config> file in the trunk/INSTALL dir."
+   echo "   -n <mpi-ranks-per-node>  Number of MPI ranks per compute node."
+   echo "   -p <mpi-ranks>           Number of MPI ranks in total."
+   echo "   -s <test-case>           Prefix of any <test-case>_p3d file in the trunk/INSTALL dir."
+   echo "   -t <openmp-threads>      Number of OpenMP threads"
+   echo ""
+   echo ""
+}
+
+
+   # Read shellscript options
+while  getopts  :b:c:n:p:s:t:h  option
+do
+  case  $option  in
+      (b)   build_config=$OPTARG;;
+      (c)   run_config=$OPTARG;;
+      (n)   mpi_ranks_per_node=$OPTARG;;
+      (p)   mpi_ranks=$OPTARG;;
+      (s)   test_case=$OPTARG;;
+      (t)   openmp_threads=$OPTARG;;
+      (h)   print_help
+            exit 0;;
+      (\?)  echo "Unknown option -$OPTARG"
+            print_help
+            exit 1;;
+  esac
+done
+
+if [[ ${build_config} == "unknown" ]]
+then
+   echo "Missing option -b <build-config>"
+   print_help
+   exit 1
+fi
+
+if [[ ${run_config} == "unknown" ]]
+then
+   echo "Missing option -c <run-config>"
+   print_help
+   exit 1
+fi
+
+if [[ ${test_case} == "unknown" ]]
+then
+   echo "Missing option -s <test-case>"
+   print_help
+   exit 1
+fi
+
+build_dir=${working_dir}/BUILD_${build_config}
+
+if [[ ! -d ${build_dir} ]]
+then
+   echo "+++ ERROR: No build for configuration ${build_config} exists! "
+   echo "           Please run \"palm_simple_install -b ${build_config}\""
+   echo "           or specify a different build that this run should be based on"
+   exit 1
+fi
+
+   # Check, if include file exists
+run_config_file=${trunk_dir}/INSTALL/RUN.cmd.${run_config}
+if [[ ! -f ${run_config_file} ]]
+then
+   echo "+++ ERROR: no such run command file:"
+   echo "    \"${run_config_file}\""
+   exit 1
+fi
+
+   # Find out the global svn revision number
+global_revision=$(svnversion ${trunk_dir}  2>/dev/null)
+global_revision="Rev: $global_revision"
+
+
+   # Generate unique directory/files for this run
+#timedate="$(date +%F_%H:%M:%S)"
+suffix=${build_config}_${run_config}_${test_case}_${mpi_ranks}_${mpi_ranks_per_node}_${openmp_threads}
+execution_dir=${working_dir}/RUN_${suffix}
+
+if [[ ! -d ${execution_dir} ]]
+then
+   mkdir -p ${execution_dir}
+   echo "*** PALM running in directory ${execution_dir}"
+else
+   echo "+++ ERROR: ${execution_dir} exists\!   Must be unique.  Exiting."
+   exit 1
+fi
+
+
+# Check if palm has been installed and copy executable into the run
+# directory
+if [[ ! -f ${build_dir}/palm ]]
+then
+   echo "+++ ERROR: palm executable does not exist."
+   echo "           Please run \"palm_simple_install\"."
+   exit 1
+else
+   cp  ${build_dir}/palm  ${execution_dir}/palm
+fi
+
+
+# Check, if parameter file exists and copy into the run directory
+if [[ ! -f ${trunk_dir}/INSTALL/${test_case}_p3d ]]
+then
+   echo "+++ ERROR: parameter file"
+   echo "           \"${trunk_dir}/INSTALL/${test_case}_p3d\""
+   echo "           does not exist."
+   exit 1
+else
+   cp  ${trunk_dir}/INSTALL/${test_case}_p3d  ${execution_dir}/PARIN
+fi
+
+
+
+
+# Create NAMELIST file containing environment values needed by palm
+cat  >  ${execution_dir}/ENVPAR  <<  EOF
+&envpar  run_identifier = '${test_case}',
+         host = 'localhost',
+         revision = '${global_revision}',
+         tasks_per_node = ${mpi_ranks_per_node},
+         maximum_parallel_io_streams = ${mpi_ranks_per_node},
+         batch_job = .TRUE.,
+         write_binary = .FALSE.,
+         maximum_cpu_time_allowed = ${cpumax}.,
+         local_dvrserver_running = .FALSE.,
+/
+EOF
+
+
+# Coupled runs cannot be carried out with this simple run script
+echo "no_coupling"  >  ${execution_dir}/runfile_atmos
+
+
+# Generate hostfile (if neccessary)
+(( ii = 1 ))
+while (( ii <= $mpi_ranks ))
+do
+   echo  $localhost_realname  >>  ${execution_dir}/hostfile
+   (( ii = ii + 1 ))
+done
+
+
+# Switch to run directory
+cd  ${execution_dir}
+
+# Start palm run
+echo "*** PALM will be executed with configuration: ${run_config}"
+echo "       MPI ranks in total: $mpi_ranks"
+echo "       MPI ranks per node: $mpi_ranks_per_node"
+echo "       OpenMP threads:     $openmp_threads"
+
+# execute according to the run command file
+source ${run_config_file}
+
+echo "*** PALM execution finished. Results can be found in:"
+echo "    \"${execution_dir}\""
+exit 0
