Skip to content
Alexey Matveichev edited this page Nov 17, 2017 · 12 revisions

Environment setup

Why

Surely after installation one can just keep terminal open and do everything in that only opened terminal with environment being set up. Or one can execute every time source $HOME/OpenFOAM/OpenFOAM-2.B.C/etc/bashrc after firing up a terminal (also one needs to mount disk image after reboot). But it is easier to automate the process.

Default version approach

This approach assumes you are using certain default OpenFOAM(R) version. You create a file with the version inside, script reads the version, mounts disk image if it is necessary and sets up environment upon launch of the terminal.

First you create a file with a version number:

$ mkdir -p $HOME/.OpenFOAM
$ echo '2.B.C' > $HOME/.OpenFOAM/OpenFOAM-release

and the code below will read file, check if disk image is mounted, mount it if necessary, and finally set up the environment:

if [ -f "$HOME/.OpenFOAM/OpenFOAM-release" ]; then
    FOAM_DISK_IMAGE=${FOAM_DISK_IMAGE:-"$HOME/OpenFOAM.sparsebundle"}
    FOAM_MOUNT_POINT=${FOAM_MOUNT_POINT:-"$HOME/OpenFOAM"}
    FOAM_VERSION=$(cat $HOME/.OpenFOAM/OpenFOAM-release)
    if [ ! -f "$HOME/OpenFOAM/OpenFOAM-$FOAM_VERSION/etc/bashrc" ]; then
        hdiutil attach -quiet -mountpoint "$FOAM_MOUNT_POINT" "$FOAM_DISK_IMAGE" &&
                . "$HOME/OpenFOAM/OpenFOAM-$FOAM_VERSION/etc/bashrc"
    else
        source "$HOME/OpenFOAM/OpenFOAM-$FOAM_VERSION/etc/bashrc"
    fi
fi

You can copy-n-paste above snippet to your $HOME/.profile and restart terminal.

Note, you could have another bash initialization files in your home folder, which have higher priority than .profile. Here is an excerpt from bash manual page (bold is mine):

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

If this is the case, add above piece of code to the initialization file you have.

"Function" approach

This approach is suitable when you have several OpenFOAM versions you are working with. So there is no environment set up after terminal launch, you need to execute function corresponding to chosen version.

mount_disk_image () {
    local foam_mount_point=$HOME/OpenFOAM
    local foam_disk_image=OpenFOAM.sparsebundle

    cd $HOME
    # Attempt to mount image
    hdiutil attach -quiet -mountpoint $foam_mount_point $foam_disk_image
    cd -
    return 0
}

ofxxx () {
    local version="$1"
    local foam_mount_point="$HOME/OpenFOAM"
    [ ! -f "$foam_mount_point/OpenFOAM-$version/etc/bashrc" ] && mount_disk_image
    [ -n "$WM_PROJECT" ] && . $WM_PROJECT_DIR/etc/config/unset.sh
    if [ -f "$foam_mount_point/OpenFOAM-$version/etc/bashrc" ]; then
        source "$foam_mount_point/OpenFOAM-$version/etc/bashrc"
    fi
}

of222 () {
    ofxxx '2.2.2'
}

of22x () {
    ofxxx '2.2.x'
}

of230 () {
    ofxxx '2.3.0'
}

of23x () {
    ofxxx '2.3.x'
}

You can add more functions corresponding for the versions you would like to use.

Combining two approaches

This approach assumes you are using certain OpenFOAM version by default but then it allows you to switch between versions calling a function. If you'd like to utilize this variant of environment setup, you can download openfoam-env-setup.sh into home folder file and add source $HOME/openfoam-env-setup.sh to your $HOME/.profile.

$FOAM_RUN folder

As in general there is no need in case sensitive file system for case files, one can move $FOAM_RUN ($HOME/OpenFOAM/user-2.B.C/run) folder from disk image to anywhere else (ex. Documents folder) and then create symlink. After you've set up environment, you can run the following commands:

$ mkdir -p $FOAM_RUN
$ mkdir -p $HOME/Documents/OpenFOAM
$ mv $HOME/OpenFOAM/user-2.B.C/run $HOME/Documents/OpenFOAM/run-2.B.C
$ ln -s $HOME/Documents/OpenFOAM/run-2.B.C $FOAM_RUN