Commit df95b6b5 authored by Mark Olesen's avatar Mark Olesen

BUG: missing foamExec causes foamJob failure (#1309, #1411)

- foamExec was removed as part of commit f308aa61 but
  overlooked the fact that it is partly still needed by foamJob.
  When running in parallel, it is used to source the etc/bashrc env.

  Reinstated a stripped down form, which has been relocated to
  bin/tools directory (it should not be directly called by the user).

  The previous version switching functionality has nonetheless be
  dropped since it only worked only when a rigid naming convention for
  OpenFOAM installations was followed.

ENH: add foamJob log=, -log-app, -no-log options, improved coding quality
parent 9e3d9b10
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# ========= | # ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox # \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration | # \\ / O peration |
# \\ / A nd | Copyright (C) 2004-2011, 2018 OpenCFD Ltd. # \\ / A nd | Copyright (C) 2004-2011, 2019 OpenCFD Ltd.
# \\/ M anipulation | # \\/ M anipulation |
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# | Copyright (C) 2011-2015 OpenFOAM Foundation # | Copyright (C) 2011-2015 OpenFOAM Foundation
...@@ -32,6 +32,9 @@ ...@@ -32,6 +32,9 @@
# Redirects the output to 'log' in the case directory. # Redirects the output to 'log' in the case directory.
# #
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# If dispatching via foamExec
foamExec="$WM_PROJECT_DIR/bin/tools/foamExec"
usage() { usage() {
exec 1>&2 exec 1>&2
while [ "$#" -ge 1 ]; do echo "$1"; shift; done while [ "$#" -ge 1 ]; do echo "$1"; shift; done
...@@ -40,80 +43,30 @@ usage() { ...@@ -40,80 +43,30 @@ usage() {
Usage: ${0##*/} [OPTION] <application> ... Usage: ${0##*/} [OPTION] <application> ...
options: options:
-case <dir> specify alternative case directory, default is the cwd -case <dir> specify alternative case directory, default is the cwd
-parallel parallel run of processors -parallel run in parallel (with mpirun)
-screen also sends output to screen -screen also send output to screen
-append append to log file instead of overwriting it -append append to existing log file instead of overwriting it
-log=FILE specify the log file
-log-app Use log.{appName} for the log file
-no-check run without fewer checks (eg, processor dirs etc)
-no-log run without log file
-wait wait for execution to complete (when not using -screen) -wait wait for execution to complete (when not using -screen)
-version=VER specify an alternative OpenFOAM version
-help print the usage -help print the usage
* run an OpenFOAM job in background. Run an OpenFOAM job in background, redirecting output to a 'log' file
Redirects the output to 'log' in the case directory in the case directory
USAGE USAGE
exit 1 exit 1
} }
# Echo strings that have single quotes #------------------------------------------------------------------------------
echoArgs() { # Parse options
addSpace=""
for stringItem in "$@"
do
echo -n "${addSpace}"
if [ "${stringItem##* }" = "$stringItem" ]
then
echo -n "$stringItem"
addSpace=" "
else
echo -n "'$stringItem'"
addSpace=" "
fi
done
unset stringItem addSpace
}
# Replacement for possibly buggy 'which'
findExec() {
case "$1" in
*/*)
if [ -x "$1" ]
then
echo "$1"
return 0
fi
;;
esac
oldIFS=$IFS
IFS=':'
for d in $PATH
do
# echo "testing: $d/$1" 1>&2
if [ -x "$d/$1" -a ! -d "$d/$1" ]
then
# echo "Found exec: $d/$1" 1>&2
IFS=$oldIFS
echo "$d/$1"
return 0
fi
done
IFS=$oldIFS
echo ""
return 1
}
# Main script logFile="log"
#~~~~~~~~~~~~ optCheck=true
unset parallelOpt screenOpt waitOpt unset optParallel optScreen optWait logMode
unset projectVersion
# Parse options
while [ "$#" -gt 0 ] while [ "$#" -gt 0 ]
do do
case "$1" in case "$1" in
...@@ -126,23 +79,37 @@ do ...@@ -126,23 +79,37 @@ do
shift shift
;; ;;
-p | -parallel) -p | -parallel)
parallelOpt=true optParallel=true
;; ;;
-s | -screen) -s | -screen)
screenOpt=true optScreen=true
;; ;;
-a | -append) -a | -append)
appendOpt=true logMode=append
;; ;;
-w | -wait) -w | -wait)
waitOpt=true optWait=true
;;
-no-check*)
unset optCheck
;;
-no-log)
logMode=none
logFile=log # Consistency if -append toggles it back on
;;
-log=*)
logFile="${1##*=}"
[ -n "$logFile" ] || logFile="log"
;;
-log-app)
logFile="{LOG_APPNAME}" # Tag for log.appName output
;; ;;
-version=*) -version=*)
projectVersion="${1#*=}" # for foamExec echo "Ignoring version option" 1>&2
;; ;;
-v | -version) -v | -version)
[ "$#" -ge 2 ] || usage "'$1' option requires an argument" [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
projectVersion="$2" # for foamExec echo "Ignoring version option" 1>&2
shift shift
;; ;;
--) --)
...@@ -166,65 +133,74 @@ done ...@@ -166,65 +133,74 @@ done
# The requested application # The requested application
appName="$1" appName="$1"
# Use foamExec for a specified version # Does application even exist?
# Also need foamExec for remote (parallel) runs APPLICATION="$(command -v "$appName")" || \
if [ -n "$projectVersion" -o "$parallelOpt" = true ] usage "Application '$appName' not found"
if [ "$logFile" = "{LOG_APPNAME}" ]
then then
# When possible, determine if application even exists logFile="log.${appName##*/}"
if [ -z "$projectVersion" ] fi
then
findExec "$appName" >/dev/null || usage "Application '$appName' not found"
fi
# Use foamExec for dispatching
APPLICATION=$(findExec foamExec) || usage "'foamExec' not found"
[ -n "$projectVersion" ] && APPLICATION="$APPLICATION -version $projectVersion" # Need foamExec for remote (parallel) runs
if [ "$optParallel" = true ]
then
# Use foamExec for dispatching
[ -x "$foamExec" ] || usage "File not found: $foamExec"
APPLICATION="$foamExec"
else else
APPLICATION=$(findExec "$appName") || usage "Application '$appName' not found" # Drop first argument in favour of fully qualified APPLICATION
echo "Application : $appName"
shift shift
fi fi
if [ "$parallelOpt" = true ] # Stringify args, adding single quotes for args with spaces
then echoArgs()
# parallel {
# ~~~~~~~~ unset stringifiedArgs
for stringItem in "$@"
do
case "$stringItem" in (*' '*) stringItem="'$stringItem'" ;; esac
stringifiedArgs="${stringifiedArgs}${stringifiedArgs:+ }${stringItem}"
done
echo "$stringifiedArgs"
}
if [ "$optParallel" = true ]
then
# #
# Check if the case decomposed # Parallel
# #
if [ -r "processor0" -o -r "processors" ] dict="system/decomposeParDict"
[ -r "$dict" ] || {
echo "No $dict found, which is required for parallel running."
exit 1
}
nProcs="$(foamDictionary -entry numberOfSubdomains -value $dict 2>/dev/null)"
# Check if case is decomposed
if [ "$optCheck" = true ]
then then
nprocs="$(foamDictionary -entry numberOfSubdomains -value system/decomposeParDict 2>/dev/null)" [ -r "processor0" ] || [ -r "processors" ] || {
else
echo "Case is not currently decomposed" echo "Case is not currently decomposed"
if [ -r system/decomposeParDict ] echo "Try decomposing first with \"foamJob decomposePar\""
then
echo "system/decomposeParDict exists"
echo "Try decomposing with \"foamJob decomposePar\""
exit 1
else
echo "Cannot find system/decomposeParDict file required to decompose the case for parallel running."
echo "Please consult the User Guide for details of parallel running"
exit 1 exit 1
fi }
fi fi
# # Locate mpirun
# Find mpirun mpirun=$(command -v mpirun) || usage "'mpirun' not found"
# mpiopts="-n $nProcs"
mpirun=$(findExec mpirun) || usage "'mpirun' not found"
mpiopts="-np $nprocs"
#
# Check if the machine ready to run parallel # Check if the machine ready to run parallel
#
echo "Parallel processing using $WM_MPLIB with $nprocs processors"
case "$WM_MPLIB" in case "$WM_MPLIB" in
*OPENMPI) *OPENMPI*)
# Add hostfile info # Add hostfile info
for hostfile in \ for hostfile in \
hostfile \ hostfile \
...@@ -233,7 +209,7 @@ then ...@@ -233,7 +209,7 @@ then
system/machines \ system/machines \
; ;
do do
if [ -r $hostfile ] if [ -r "$hostfile" ]
then then
mpiopts="$mpiopts -hostfile $hostfile" mpiopts="$mpiopts -hostfile $hostfile"
break break
...@@ -242,59 +218,107 @@ then ...@@ -242,59 +218,107 @@ then
# Send FOAM_SETTINGS to parallel processes, so that the proper # Send FOAM_SETTINGS to parallel processes, so that the proper
# definitions are sent as well. # definitions are sent as well.
if [ -n "$FOAM_SETTINGS" ]
then
mpiopts="$mpiopts -x FOAM_SETTINGS" mpiopts="$mpiopts -x FOAM_SETTINGS"
fi
;; ;;
esac esac
# #
# Run (in parallel) # Run (in parallel)
# #
if [ "$screenOpt" = true ] echo "Application : $appName ($nProcs processes)"
if [ "$logMode" != "none" ]
then then
[ "$appendOpt" = true ] && teeOpts=" -a" echo "Output : $logFile"
echo "Executing: $mpirun $mpiopts $APPLICATION $(echoArgs "$@") -parallel | tee $teeOpts log" fi
$mpirun $mpiopts $APPLICATION "$@" -parallel | tee $teeOpts log echo "Executing : $mpirun $mpiopts $APPLICATION $(echoArgs "$@") -parallel"
else if [ "$optScreen" = true ]
if [ "$appendOpt" = true ]
then then
echo "Executing: $mpirun $mpiopts $APPLICATION $(echoArgs "$@") -parallel >> log 2>&1" case "$logMode" in
$mpirun $mpiopts $APPLICATION "$@" -parallel >> log 2>&1 & none)
"$mpirun" $mpiopts "$APPLICATION" "$@" -parallel
;;
append)
"$mpirun" $mpiopts "$APPLICATION" "$@" -parallel | tee -a "$logFile"
;;
*)
"$mpirun" $mpiopts "$APPLICATION" "$@" -parallel | tee "$logFile"
;;
esac
else else
echo "Executing: $mpirun $mpiopts $APPLICATION $(echoArgs "$@") -parallel > log 2>&1" case "$logMode" in
$mpirun $mpiopts $APPLICATION "$@" -parallel > log 2>&1 & none)
fi "$mpirun" $mpiopts "$APPLICATION" "$@" -parallel > /dev/null 2>&1 &
;;
append)
"$mpirun" $mpiopts "$APPLICATION" "$@" -parallel >> "$logFile" 2>&1 &
;;
*)
"$mpirun" $mpiopts "$APPLICATION" "$@" -parallel > "$logFile" 2>&1 &
;;
esac
pid=$! pid=$!
if [ "$waitOpt" = true ] if [ "$optWait" = true ]
then then
wait $pid echo "Waiting for process $pid to finish"
wait "$pid"
echo "Process $pid finished"
else
echo "Process id : $pid"
fi fi
fi fi
else else
# #
# Run (on single processor) # Serial
# #
if [ "$screenOpt" = true ] echo "Application : $appName ($nProcs processes)"
if [ "$logMode" != "none" ]
then then
[ "$appendOpt" = true ] && teeOpts=" -a" echo "Output : $logFile"
echo "Executing: $APPLICATION $(echoArgs "$@") | tee $teeOpts log &" fi
$APPLICATION "$@" | tee $teeOpts log & echo "Executing : $APPLICATION $(echoArgs "$@")"
wait $! if [ "$optScreen" = true ]
else
if [ "$appendOpt" = true ]
then then
echo "Executing: $APPLICATION $(echoArgs "$@") >> log 2>&1 &" case "$logMode" in
$APPLICATION "$@" >> log 2>&1 & none)
"$APPLICATION" "$@" &
;;
append)
"$APPLICATION" "$@" | tee -a "$logFile" &
;;
*)
"$APPLICATION" "$@" | tee "$logFile" &
;;
esac
pid=$!
echo "Process id : $pid"
wait "$pid"
else else
echo "Executing: $APPLICATION $(echoArgs "$@") > log 2>&1 &" case "$logMode" in
$APPLICATION "$@" > log 2>&1 & none)
fi "$APPLICATION" "$@" > /dev/null 2>&1 &
;;
append)
"$APPLICATION" "$@" >> "$logFile" 2>&1 &
;;
*)
"$APPLICATION" "$@" > "$logFile" 2>&1 &
;;
esac
pid=$! pid=$!
if [ "$waitOpt" = true ] if [ "$optWait" = true ]
then then
wait $pid echo "Waiting for process $pid to finish"
wait "$pid"
echo "Process $pid finished"
else
echo "Process id : $pid"
fi fi
fi fi
fi fi
......
#!/bin/bash
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
# \\/ M anipulation |
#-------------------------------------------------------------------------------
# License
# This file is part of OpenFOAM.
#
# OpenFOAM is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
#
# Script
# foamExec <application> ...
#
# Description
# Runs an application (with arguments) after first sourcing the OpenFOAM
# etc/bashrc file from the project directory
#
# This script must exist in $WM_PROJECT_DIR/bin/tools
#
# Can useful for parallel runs. For example,
#
# mpirun -n <nProcs> \
# projectDir/bin/tools/foamExec <simpleFoam> ... -parallel
#
#------------------------------------------------------------------------------
usage() {
exec 1>&2
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
cat<<USAGE
Usage: ${0##*/} [OPTION] <application> ...
options:
-help Print the usage
Runs an application (with arguments) after first sourcing the OpenFOAM
etc/bashrc file from the project directory
USAGE
exit 1
}
# Report error and exit
die()
{
exec 1>&2
echo
echo "Error encountered:"
while [ "$#" -ge 1 ]; do echo " $1"; shift; done
echo
echo "See '$0 -help' for usage"
echo
exit 1
}
#-------------------------------------------------------------------------------
toolsDir="${0%/*}" # The bin/tools dir
projectDir="${toolsDir%/bin/tools}" # Project dir
# Parse options
while [ "$#" -gt 0 ]
do
case "$1" in
-h | -help*)
usage
;;
--)
shift
break
;;
-*)
die "unknown option: '$1'"
;;
*)
break
;;
esac
shift
done
#-------------------------------------------------------------------------------
[ "$#" -ge 1 ] || die "No application specified"
[ -d "$projectDir" ] || {
echo "Error: no project dir: $projectDir" 1>&2
exit 2
}
[ -f "$projectDir/etc/bashrc" ] || {
echo "Error: file not found: $projectDir/etc/bashrc" 1>&2
exit 2
}
# Source bashrc within a function to preserve command-line arguments
# - this will not have aliases, but working non-interactively anyhow
sourceBashrc()
{
. "$projectDir/etc/bashrc" $FOAM_SETTINGS
}
sourceBashrc
exec "$@"
#------------------------------------------------------------------------------
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment