Skip to content
Snippets Groups Projects
foamExec 4.26 KiB
Newer Older
Henry's avatar
Henry committed
#!/bin/bash
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
Henry Weller's avatar
Henry Weller committed
#   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
#    \\/     M anipulation  | Copyright (C) 2018 OpenCFD Ltd.
#-------------------------------------------------------------------------------
# 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
#
# Description
#     Usage: foamExec [-version=foamVersion] <foamCommand> ...
#
#     Runs the <foamVersion> version of executable <foamCommand>
#     with the rest of the arguments.
#
#     Can also be used for parallel runs. For example,
#     \code
#         mpirun -np <nProcs> \
#             foamExec -version=VERSION <foamCommand> ... -parallel
#     This script must exist in $WM_PROJECT_INST_DIR/OpenFOAM-<VERSION>/bin
#     or $WM_PROJECT_INST_DIR/openfoam<VERSION>/bin (debian)
#     foamEtcFile located in the same directory as this script
#    foamEtcFile
#------------------------------------------------------------------------------
usage() {
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE
Usage: ${0##*/} [OPTION] <application> ...
  -mode=MODE        Any combination of u(user), g(group), o(other)
  -prefix=DIR       Specify an alternative installation prefix
  -version=VER      Specify alternative OpenFOAM version (eg, 3.0, 1612, ...)
                    pass through to foamEtcFile
  -help             Print the usage
Run a particular OpenFOAM version of <APPLICATION>
#-------------------------------------------------------------------------------
binDir="${0%/*}"                # The bin dir
projectDir="${binDir%/bin}"     # The project dir
# prefixDir="${projectDir%/*}"    # The prefix dir (same as $WM_PROJECT_INST_DIR)
projectVersion="${WM_PROJECT_VERSION:-unknown}"
unset etcOpts
# parse options
while [ "$#" -gt 0 ]
do
    -mode=*)
        etcOpts="$etcOpts $1"       # pass-thru to foamEtcFile
        ;;
    -prefix=/*)
        etcOpts="$etcOpts $1"       # pass-thru to foamEtcFile
        ;;
    -version=*)
        etcOpts="$etcOpts $1"       # pass-thru to foamEtcFile
        projectVersion="${1#*=}"    # for reporting
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        etcOpts="$etcOpts $1 $2"    # pass-thru to foamEtcFile
        shift
        ;;
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        etcOpts="$etcOpts $1 $2"    # pass-thru to foamEtcFile
        shift
        ;;
    -v | -version)
        [ "$#" -ge 2 ] || usage "'$1' option requires an argument"
        etcOpts="$etcOpts $1 $2"    # pass-thru to foamEtcFile
        projectVersion="$2"         # for reporting

# Find and source the OpenFOAM <etc/bashrc>
# placed in function to preserve command-line arguments
    rcFile="$($binDir/foamEtcFile $etcOpts bashrc)" || {
        echo "Error: bashrc file could not be found for OpenFOAM-$projectVersion" 1>&2
        exit 2
    . $rcFile $FOAM_SETTINGS
[ "$#" -ge 1 ] || usage "no application specified"

exec "$@"

#------------------------------------------------------------------------------