ENH: add wrapper for starting OpenFOAM as an interactive bash session

- call also be used non-interactively for simple 'one-shot' use of
  OpenFOAM utilities or solvers

STYLE: use dash instead of brackets for '- see www.OpenFOAM.com'

- less cluttered in combination with API information
This commit is contained in:
Mark Olesen 2019-09-25 17:36:10 +02:00 committed by Andrew Heather
parent fd7fe96e9f
commit 043d55e382
7 changed files with 286 additions and 10 deletions

View File

@ -68,7 +68,7 @@ not guaranteed to have any correspondence to the OpenFOAM release
information is embedded into each application. For example, as
displayed from `blockMesh -help`:
```
Using: OpenFOAM-v1812.local (1812) (see www.OpenFOAM.com)
Using: OpenFOAM-v1812.local (1812) - see www.OpenFOAM.com
Build: 65d6551ff7-20190530 (patch=190531)
Arch: LSB;label=32;scalar=64
```

178
bin/tools/openfoam Executable file
View File

@ -0,0 +1,178 @@
#!/bin/sh
#------------------------------------------------------------------------------
# ========= |
# \\ / 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, licensed under GNU General Public License
# <http://www.gnu.org/licenses/>.
#
# Script
# openfoam [args]
#
# Description
# Open an interactive bash session with an OpenFOAM environment,
# or run an OpenFOAM application (with arguments) after first sourcing
# the OpenFOAM etc/bashrc file from the project directory.
#
# This script normally exists in $WM_PROJECT_DIR/bin/tools
#
#------------------------------------------------------------------------------
# Hard-coded value (eg, with autoconfig)
projectDir="@PROJECT_DIR@"
if [ -z "$projectDir" ] || [ "${projectDir#@}" != "$projectDir" ]
then
# Auto-detect from location
toolsDir="${0%/*}" # The bin/tools dir
projectDir="${toolsDir%/bin/tools}" # Project dir
if [ "$projectDir" = "$toolsDir" ] # Need to try harder
then
projectDir="$(\cd $(dirname $0)/../.. && \pwd -L)" || unset projectDir
fi
fi
#------------------------------------------------------------------------------
usage() {
exec 1>&2
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
cat<<USAGE
Usage: ${0##*/} [OPTION] [application ...]
options:
-foam=DIR Specify alternative OpenFOAM directory
-sp Single precision
-dp Double precision
-spdp Mixed single/double precision
-int32 | -int64 The label-size
-help Print the usage
Open an interactive bash session with an OpenFOAM environment,
or run an OpenFOAM application (with arguments) after first sourcing
the OpenFOAM etc/bashrc file from the project directory
USAGE
exit 1
}
#-------------------------------------------------------------------------------
# Only preserve settings for non-interactive?
if [ "$#" -eq 0 ]
then
unset _foamSettings FOAM_SETTINGS
else
_foamSettings="$FOAM_SETTINGS"
fi
# Parse options
while [ "$#" -gt 0 ]
do
case "$1" in
-h | -help*)
usage
;;
-foam=*)
projectDir="${1#*=}"
;;
-sp | -SP)
# WM_PRECISION_OPTION=...
_foamSettings="$_foamSettings${_foamSettings:+ }WM_PRECISION_OPTION=SP"
;;
-dp | -DP)
# WM_PRECISION_OPTION=...
_foamSettings="$_foamSettings${_foamSettings:+ }WM_PRECISION_OPTION=DP"
;;
-spdp | -SPDP)
# WM_PRECISION_OPTION=...
_foamSettings="$_foamSettings${_foamSettings:+ }WM_PRECISION_OPTION=SPDP"
;;
-int32 | -int64)
# WM_LABEL_SIZE=...
_foamSettings="$_foamSettings${_foamSettings:+ }WM_LABEL_SIZE=${1#-int}"
;;
--)
shift
break
;;
-*)
echo "Error: unknown option: '$1'" 1>&2
exit 1
;;
*)
break
;;
esac
shift
done
#-------------------------------------------------------------------------------
# Remove current OpenFOAM environment
if [ -d "$WM_PROJECT_DIR" ] && [ -f "$WM_PROJECT_DIR/etc/config.sh/unset" ]
then
. "$WM_PROJECT_DIR/etc/config.sh/unset"
fi
[ -d "$projectDir" ] || {
echo "Error: no project dir: $projectDir" 1>&2
exit 2
}
_foamSourceBashEnv="$projectDir/etc/bashrc"
if [ "$#" -eq 0 ]
then
# Interactive shell
_foamSourceBashEnv="$projectDir/bin/tools/source-bashrc"
fi
[ -f "$_foamSourceBashEnv" ] || {
echo "Error: file not found: $_foamSourceBashEnv" 1>&2
exit 2
}
if [ "$#" -eq 0 ]
then
# Source user ~/.bashrc and OpenFOAM etc/bashrc.
# 1) Can either use a tmp file, or 2) chain off to a dedicated file
# We use a dedicated file.
if [ -n "$_foamSettings" ]
then
export FOAM_SETTINGS="$_foamSettings"
fi
## echo "Source with $_foamSourceBashEnv with '$FOAM_SETTINGS'" 1>&2
# Interactive shell (newer bash can use --init-file instead of --rcfile)
exec bash --rcfile "$_foamSourceBashEnv" -i
else
# Non-interactive
# Source bashrc within a function to preserve command-line arguments
# - this will not have aliases, but working non-interactively anyhow
sourceBashrc()
{
. "$_foamSourceBashEnv" $_foamSettings
}
sourceBashrc
exec "$@"
fi
#------------------------------------------------------------------------------

93
bin/tools/source-bashrc Normal file
View File

@ -0,0 +1,93 @@
#----------------------------------*-sh-*--------------------------------------
# ========= |
# \\ / 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, licensed under GNU General Public License
# <http://www.gnu.org/licenses/>.
#
# File
# bin/tools/source-bashrc
#
# Description
# Source user ~/.bashrc and OpenFOAM etc/bashrc
#
# This file is normally not sourced manually,
# but from bash with the --rcfile option.
#------------------------------------------------------------------------------
# Hard-coded value (eg, with autoconfig)
projectDir="@PROJECT_DIR@"
if [ -z "$projectDir" ] || [ "${projectDir#@}" != "$projectDir" ]
then
# Auto-detect (as per OpenFOAM etc/bashrc)
# --
# Assuming this file is $WM_PROJECT_DIR/bin/tools/source-bashrc,
# the next lines should work when sourced by BASH or ZSH shells.
# --
projectDir="${BASH_SOURCE:-${ZSH_NAME:+$0}}"
[ -n "$projectDir" ] && projectDir="$(\cd $(dirname $projectDir)/../.. && \pwd -L)" || unset projectDir
fi
#------------------------------------------------------------------------------
if [ -d "$projectDir" ]
then
_foamSourceBashEnv="$projectDir/etc/bashrc"
else
unset _foamSourceBashEnv
fi
# Source the user bashrc first.
# Simply hope that they don't unset/reset _foamSourceBashEnv !!
if [ -f "$HOME/.bashrc" ]
then
. "$HOME/.bashrc"
fi
# Source the OpenFOAM etc/bashrc
if [ -f "$_foamSourceBashEnv" ]
then
. "$_foamSourceBashEnv" $FOAM_SETTINGS
# Avoid further inheritance
unset FOAM_SETTINGS
# Some feedback
if [ -n "$PS1" ] && [ -d "$WM_PROJECT_DIR" ]
then
info="$(foamEtcFile -show-patch 2>/dev/null)"
# echo "Using: OpenFOAM-$WM_PROJECT_VERSION ($FOAM_API${info:+ patch=$info}) - see www.OpenFOAM.com" 1>&2
echo "Using: OpenFOAM-$WM_PROJECT_VERSION${info:+ (patch=$info)} - see www.OpenFOAM.com" 1>&2
echo "Arch: $WM_OPTIONS (mpi=$FOAM_MPI)" 1>&2
## echo "$WM_PROJECT_DIR" 1>&2
## echo 1>&2
# Set prompt as reminder that this is a shell session
# Chalmers likes this one:
# PS1="OpenFOAM${FOAM_API:+-$FOAM_API}:"'$(foamPwd)\n\u\$ '
PS1="OpenFOAM${FOAM_API:+-$FOAM_API}:"'\w/\n\u\$ '
fi
else
echo "Could not locate OpenFOAM etc/bashrc in '$projectDir'" 1>&2
fi
echo "OpenFOAM shell session - use exit to quit" 1>&2
echo 1>&2
# Cleanup variables (done as final statement for a clean exit code)
unset _foamSourceBashEnv projectDir
#------------------------------------------------------------------------------

View File

@ -95,6 +95,8 @@ projectDir="$HOME/OpenFOAM/OpenFOAM-$WM_PROJECT_VERSION"
# projectDir="/opt/openfoam/OpenFOAM-$WM_PROJECT_VERSION"
# projectDir="/usr/local/OpenFOAM/OpenFOAM-$WM_PROJECT_VERSION"
################################################################################
# Or optionally hard-coded (eg, with autoconfig)
# projectDir="@PROJECT_DIR@"
: # Safety statement (if the user removed all fallback values)
# [FOAM_SIGFPE] - Trap floating-point exceptions.

View File

@ -2,7 +2,7 @@
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
# \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd.
# \\/ M anipulation |
#------------------------------------------------------------------------------
# | Copyright (C) 2011-2016 OpenFOAM Foundation
@ -111,15 +111,16 @@ foamPV()
unset -f foamPwd 2>/dev/null
foamPwd()
{
if [ -d "$WM_PROJECT_DIR" ]
if [ -n "$WM_PROJECT_DIR" ]
then
echo "$PWD" | sed \
-e "s#^${FOAM_RUN}#\$FOAM_RUN#;" \
-e "s#^${WM_PROJECT_DIR}#\$WM_PROJECT_DIR#;" \
-e "s#^${WM_PROJECT_USER_DIR}#\$WM_PROJECT_USER_DIR#;" \
-e "s#^${HOME}#\$HOME#";
echo "$PWD/" | sed \
-e "s#^${FOAM_RUN}/#\$FOAM_RUN/#" \
-e "s#^${WM_PROJECT_DIR}/#\$WM_PROJECT_DIR/#" \
-e "s#^${WM_PROJECT_USER_DIR}/#\$WM_PROJECT_USER_DIR/#" \
-e "s#^${HOME}/#~/#" \
;
else
echo "$PWD" | sed -e "s#^${HOME}#\$HOME#;"
echo "$PWD/" | sed -e "s#^${HOME}/#~/#";
fi
}

View File

@ -98,6 +98,8 @@ set projectDir=`lsof +p $$ |& \
# set projectDir="/opt/openfoam/OpenFOAM-$WM_PROJECT_VERSION"
# set projectDir="/usr/local/OpenFOAM/OpenFOAM-$WM_PROJECT_VERSION"
################################################################################
# Or optionally hard-coded (eg, with autoconfig)
# set projectDir="@PROJECT_DIR@"
# [FOAM_SIGFPE] - Trap floating-point exceptions.
# - overrides the 'trapFpe' controlDict entry

View File

@ -44,7 +44,7 @@ bool Foam::foamVersion::patched()
void Foam::foamVersion::printBuildInfo(const bool full)
{
Info<< "Using: OpenFOAM-" << foamVersion::version.c_str()
<< " (" << foamVersion::api << ") (see www.OpenFOAM.com)\n"
<< " (" << foamVersion::api << ") - see www.OpenFOAM.com\n"
<< "Build: " << foamVersion::build.c_str();
if (foamVersion::patched())